Merge: compiler: Refactor
authorJean Privat <jean@pryen.org>
Tue, 28 Mar 2017 12:27:44 +0000 (08:27 -0400)
committerJean Privat <jean@pryen.org>
Tue, 28 Mar 2017 12:27:44 +0000 (08:27 -0400)
Various minor refactorings done for PR #2397.

Pull-Request: #2398
Reviewed-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit
src/compiler/java_compiler.nit
src/compiler/separate_compiler.nit
src/model/model.nit
src/semantize/typing.nit

index 4ce8024..0d04167 100644 (file)
@@ -1450,7 +1450,7 @@ abstract class AbstractCompilerVisitor
        # Checks
 
        # Can value be null? (according to current knowledge)
-       fun maybenull(value: RuntimeVariable): Bool
+       fun maybe_null(value: RuntimeVariable): Bool
        do
                return value.mcasttype isa MNullableType or value.mcasttype isa MNullType
        end
@@ -1460,7 +1460,7 @@ abstract class AbstractCompilerVisitor
        do
                if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
 
-               if maybenull(recv) then
+               if maybe_null(recv) then
                        self.add("if (unlikely({recv} == NULL)) \{")
                        self.add_abort("Receiver is null")
                        self.add("\}")
@@ -3211,7 +3211,7 @@ redef class AAttrPropdef
                        assert arguments.length == 2
                        var recv = arguments.first
                        var arg = arguments[1]
-                       if is_optional and v.maybenull(arg) then
+                       if is_optional and v.maybe_null(arg) then
                                var value = v.new_var(self.mpropdef.static_mtype.as(not null))
                                v.add("if ({arg} == NULL) \{")
                                v.assign(value, evaluate_expr(v, recv))
@@ -3645,7 +3645,7 @@ redef class AOrElseExpr
                var res = v.new_var(self.mtype.as(not null))
                var i1 = v.expr(self.n_expr, null)
 
-               if not v.maybenull(i1) then return i1
+               if not v.maybe_null(i1) then return i1
 
                v.add("if ({i1}!=NULL) \{")
                v.assign(res, i1)
@@ -3898,7 +3898,7 @@ redef class AAsNotnullExpr
                var i = v.expr(self.n_expr, null)
                if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
 
-               if not v.maybenull(i) then return i
+               if not v.maybe_null(i) then return i
 
                v.add("if (unlikely({i} == NULL)) \{")
                v.add_abort("Cast failed")
index f871f61..60aea2b 100644 (file)
@@ -1301,7 +1301,7 @@ redef class MType
 
        # Is the associated Java type a primitive one?
        #
-       # ENSURE `result == (java_type != "Object")`
+       # ENSURE `result == (java_type != "RTVal")`
        var is_java_primitive: Bool is lazy do return java_type != "RTVal"
 end
 
index e486b64..45790b3 100644 (file)
@@ -1405,7 +1405,7 @@ class SeparateCompilerVisitor
                var res: nullable RuntimeVariable = null
                var recv = arguments.first
                var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_null.value or mmethod.name == "==" or mmethod.name == "!="
-               if maybenull(recv) and consider_null then
+               if maybe_null(recv) and consider_null then
                        self.add("if ({recv} == NULL) \{")
                        if mmethod.name == "==" or mmethod.name == "is_same_instance" then
                                res = self.new_var(bool_type)
@@ -2061,12 +2061,6 @@ class SeparateCompilerVisitor
                return k == interface_kind or t.is_c_primitive
        end
 
-       fun maybe_null(value: RuntimeVariable): Bool
-       do
-               var t = value.mcasttype
-               return t isa MNullableType or t isa MNullType
-       end
-
        redef fun array_instance(array, elttype)
        do
                var nclass = mmodule.native_array_class
index cfb5778..c1c950e 100644 (file)
@@ -103,6 +103,9 @@ redef class Model
        # The only null type
        var null_type = new MNullType(self)
 
+       # The only bottom type
+       var bottom_type: MBottomType = null_type.as_notnull
+
        # Build an ordered tree with from `concerns`
        fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do
                var seen = new HashSet[MConcern]
@@ -905,15 +908,16 @@ abstract class MType
        #
        # Explanation of the example:
        # In H, T is set to B, because "H super G[B]", and U is bound to Y,
-        # because "redef type U: Y". Therefore, Map[T, U] is bound to
+       # because "redef type U: Y". Therefore, Map[T, U] is bound to
        # Map[B, Y]
        #
+       # REQUIRE: `self.need_anchor implies anchor != null`
        # ENSURE: `not self.need_anchor implies result == self`
        # ENSURE: `not result.need_anchor`
-       fun anchor_to(mmodule: MModule, anchor: MClassType): MType
+       fun anchor_to(mmodule: MModule, anchor: nullable MClassType): MType
        do
                if not need_anchor then return self
-               assert not anchor.need_anchor
+               assert anchor != null and not anchor.need_anchor
                # Just resolve to the anchor and clear all the virtual types
                var res = self.resolve_for(anchor, null, mmodule, true)
                assert not res.need_anchor
@@ -1143,6 +1147,7 @@ abstract class MType
        # * H[G[A], B] -> 3
        #
        # Formal types have a depth of 1.
+       # Only `MClassType` and `MFormalType` nodes are counted.
        fun depth: Int
        do
                return 1
@@ -1156,6 +1161,7 @@ abstract class MType
        # * H[G[A], B] -> 4
        #
        # Formal types have a length of 1.
+       # Only `MClassType` and `MFormalType` nodes are counted.
        fun length: Int
        do
                return 1
@@ -1222,7 +1228,7 @@ class MClassType
 
        redef fun need_anchor do return false
 
-       redef fun anchor_to(mmodule: MModule, anchor: MClassType): MClassType
+       redef fun anchor_to(mmodule, anchor): MClassType
        do
                return super.as(MClassType)
        end
@@ -1819,7 +1825,7 @@ class MNullType
        redef fun c_name do return "null"
        redef fun as_nullable do return self
 
-       redef var as_notnull = new MBottomType(model) is lazy
+       redef var as_notnull: MBottomType = new MBottomType(model) is lazy
        redef fun need_anchor do return false
        redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
        redef fun can_resolve_for(mtype, anchor, mmodule) do return true
index 0bbe61b..d5cbb46 100644 (file)
@@ -81,11 +81,6 @@ private class TypeVisitor
 
        fun anchor_to(mtype: MType): MType
        do
-               var anchor = anchor
-               if anchor == null then
-                       assert not mtype.need_anchor
-                       return mtype
-               end
                return mtype.anchor_to(mmodule, anchor)
        end