Merge: model: add `MType::as_notnullable` to remove the `nullable` modifier
authorJean Privat <jean@pryen.org>
Mon, 28 Jul 2014 18:29:23 +0000 (14:29 -0400)
committerJean Privat <jean@pryen.org>
Mon, 28 Jul 2014 18:29:23 +0000 (14:29 -0400)
Basically replace

~~~
if m isa MNullableType then m = m.mtype
~~~

by

~~~
m = m.as_notnullable
~~~

Pull-Request: #619
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

src/highlight.nit
src/metrics/rta_metrics.nit
src/model/model.nit
src/modelize_property.nit
src/nitni/nitni_callbacks.nit
src/rapid_type_analysis.nit
src/separate_compiler.nit
src/typing.nit

index 07f5ed9..bfa0d0f 100644 (file)
@@ -858,7 +858,7 @@ redef class AType
        do
                var mt = mtype
                if mt == null then return null
-               if mt isa MNullableType then mt = mt.mtype
+               mt = mt.as_notnullable
                if mt isa MVirtualType or mt isa MParameterType then
                        res.add_class("nc_vt")
                end
index 40408f3..1224513 100644 (file)
@@ -376,7 +376,7 @@ redef class RapidTypeAnalysis
                super
                tnlc.values.inc(mtype)
 
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
                if mtype isa MClassType then
                        cnlc.values.inc(mtype.mclass)
                end
@@ -387,8 +387,7 @@ end
 
 redef class MType
        private fun signature_depth: Int do
-               var mtype = self
-               if mtype isa MNullableType then mtype = mtype.mtype
+               var mtype = self.as_notnullable
                if not mtype isa MGenericType then return 0
 
                var depth = 0
index 85aba5e..e2f853a 100644 (file)
@@ -887,6 +887,16 @@ abstract class MType
                return res
        end
 
+       # Return the not nullable version of the type
+       # Is the type is already not nullable, then self is returned.
+       #
+       # Note: this just remove the `nullable` notation, but the result can still contains null.
+       # For instance if `self isa MNullType` or self is a a formal type bounded by a nullable type.
+       fun as_notnullable: MType
+       do
+               return self
+       end
+
        private var as_nullable_cache: nullable MType = null
 
 
@@ -1377,6 +1387,7 @@ class MNullableType
 
        redef fun need_anchor do return mtype.need_anchor
        redef fun as_nullable do return self
+       redef fun as_notnullable do return mtype
        redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
        do
                var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
@@ -1642,7 +1653,7 @@ abstract class MProperty
        fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
        do
                assert not mtype.need_anchor
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
 
                var cache = self.lookup_definitions_cache[mmodule, mtype]
                if cache != null then return cache
@@ -1681,7 +1692,7 @@ abstract class MProperty
        fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
        do
                assert not mtype.need_anchor
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
 
                # First, select all candidates
                var candidates = new Array[MPROPDEF]
@@ -1758,7 +1769,7 @@ abstract class MProperty
        fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
        do
                assert not mtype.need_anchor
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
 
                var cache = self.lookup_all_definitions_cache[mmodule, mtype]
                if cache != null then return cache
index cc11d50..8273fb8 100644 (file)
@@ -187,7 +187,7 @@ redef class ModelBuilder
                # It is a case-by case
                var vis_type: nullable MVisibility = null # The own visibility of the type
                var mmodule_type: nullable MModule = null # The origial module of the type
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
                if mtype isa MClassType then
                        vis_type = mtype.mclass.visibility
                        mmodule_type = mtype.mclass.intro.mmodule
index 604f4f9..cb75bc8 100644 (file)
@@ -411,7 +411,7 @@ redef class AAsNotNullableExternCall
        redef fun from_mtype do return n_type.mtype.as_nullable
        redef fun to_mtype do
                var mtype = n_type.mtype.as(not null)
-               if mtype isa MNullableType then return mtype.mtype
+               mtype = mtype.as_notnullable
                return mtype
        end
 
index b2683c2..17d5e0c 100644 (file)
@@ -88,7 +88,7 @@ class RapidTypeAnalysis
                var mtype = callsite.recv
                var anchor = callsite.anchor
                if anchor != null then mtype = mtype.anchor_to(callsite.mmodule, anchor)
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
                assert mtype isa MClassType
                mtype = mtype.mclass.intro.bound_mtype
                var mproperty = callsite.mproperty
@@ -457,7 +457,7 @@ class RapidTypeVisitor
        do
                mtype = mtype.anchor_to(self.analysis.mainmodule, self.receiver)
                if mtype isa MNullType then return null
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
                assert mtype isa MClassType
                assert not mtype.need_anchor
                return mtype
index bf3cb12..273451f 100644 (file)
@@ -590,8 +590,7 @@ class SeparateCompiler
 
                # resolution table (for receiver)
                if is_live then
-                       var mclass_type = mtype
-                       if mclass_type isa MNullableType then mclass_type = mclass_type.mtype
+                       var mclass_type = mtype.as_notnullable
                        assert mclass_type isa MClassType
                        if resolution_tables[mclass_type].is_empty then
                                v.add_decl("NULL, /*NO RESOLUTIONS*/")
@@ -624,12 +623,7 @@ class SeparateCompiler
 
        fun compile_type_resolution_table(mtype: MType) do
 
-               var mclass_type: MClassType
-               if mtype isa MNullableType then
-                       mclass_type = mtype.mtype.as(MClassType)
-               else
-                       mclass_type = mtype.as(MClassType)
-               end
+               var mclass_type = mtype.as_notnullable.as(MClassType)
 
                # extern const struct resolution_table_X resolution_table_X
                self.provide_declaration("resolution_table_{mtype.c_name}", "extern const struct types resolution_table_{mtype.c_name};")
@@ -1585,8 +1579,7 @@ class SeparateCompilerVisitor
 
        fun can_be_primitive(value: RuntimeVariable): Bool
        do
-               var t = value.mcasttype
-               if t isa MNullableType then t = t.mtype
+               var t = value.mcasttype.as_notnullable
                if not t isa MClassType then return false
                var k = t.mclass.kind
                return k == interface_kind or t.ctype != "val*"
index 8dee027..8b84367 100644 (file)
@@ -284,7 +284,7 @@ private class TypeVisitor
                var erasure_cast = false
                var rettype = mpropdef.msignature.return_mtype
                if not recv_is_self and rettype != null then
-                       if rettype isa MNullableType then rettype = rettype.mtype
+                       rettype = rettype.as_notnullable
                        if rettype isa MParameterType then
                                var erased_rettype = msignature.return_mtype
                                assert erased_rettype != null
@@ -905,7 +905,7 @@ redef class AForExpr
                # anchor formal and virtual types
                if mtype.need_anchor then mtype = v.anchor_to(mtype)
 
-               if mtype isa MNullableType then mtype = mtype.mtype
+               mtype = mtype.as_notnullable
                self.coltype = mtype.as(MClassType)
 
                # get methods is_ok, next, item
@@ -1008,9 +1008,7 @@ redef class AOrElseExpr
                        return # Skip error
                end
 
-               if t1 isa MNullableType then
-                       t1 = t1.mtype
-               end
+               t1 = t1.as_notnullable
 
                var t = v.merge_types(self, [t1, t2])
                if t == null then