modelbuilder: `resolve_mtype` require a MClassDef instead of a AClassdef
authorJean Privat <jean@pryen.org>
Mon, 2 Jun 2014 19:09:57 +0000 (15:09 -0400)
committerJean Privat <jean@pryen.org>
Tue, 3 Jun 2014 18:53:33 +0000 (14:53 -0400)
This also simplify some the clients.

Signed-off-by: Jean Privat <jean@pryen.org>

src/metrics/static_types_metrics.nit
src/modelize_class.nit
src/modelize_property.nit
src/nitni/nitni_callbacks.nit
src/typing.nit

index e0fb5c3..888c8e2 100644 (file)
@@ -54,7 +54,8 @@ private class ATypeCounterVisitor
        redef fun visit(n)
        do
                if n isa AType then
-                       var mtype = modelbuilder.resolve_mtype(self.nclassdef, n)
+                       var mclassdef = self.nclassdef.mclassdef
+                       var mtype = modelbuilder.resolve_mtype(mclassdef.mmodule, mclassdef, n)
                        if mtype != null then
                                self.typecount.inc(mtype)
                        end
index a72bfec..219cbb6 100644 (file)
@@ -133,7 +133,7 @@ redef class ModelBuilder
                                var nfd = nclassdef.n_formaldefs[i]
                                var nfdt = nfd.n_type
                                if nfdt != null then
-                                       var bound = resolve_mtype_unchecked(nclassdef, nfdt, false)
+                                       var bound = resolve_mtype_unchecked(mmodule, null, nfdt, false)
                                        if bound == null then return # Forward error
                                        if bound.need_anchor then
                                                # No F-bounds!
@@ -194,7 +194,7 @@ redef class ModelBuilder
                        for nsc in nclassdef.n_superclasses do
                                specobject = false
                                var ntype = nsc.n_type
-                               var mtype = resolve_mtype_unchecked(nclassdef, ntype, false)
+                               var mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, false)
                                if mtype == null then continue # Skip because of error
                                if not mtype isa MClassType then
                                        error(ntype, "Error: supertypes cannot be a formal type")
@@ -293,11 +293,12 @@ redef class ModelBuilder
                # Check unchecked ntypes
                for nclassdef in nmodule.n_classdefs do
                        if nclassdef isa AStdClassdef then
+                               var mclassdef = nclassdef.mclassdef
                                # check bound of formal parameter
                                for nfd in  nclassdef.n_formaldefs do
                                        var nfdt = nfd.n_type
                                        if nfdt != null and nfdt.mtype != null then
-                                               var bound = resolve_mtype(nclassdef, nfdt)
+                                               var bound = resolve_mtype(mmodule, mclassdef, nfdt)
                                                if bound == null then return # Forward error
                                        end
                                end
@@ -305,7 +306,7 @@ redef class ModelBuilder
                                for nsc in nclassdef.n_superclasses do
                                        var ntype = nsc.n_type
                                        if ntype.mtype != null then
-                                               var mtype = resolve_mtype(nclassdef, ntype)
+                                               var mtype = resolve_mtype(mmodule, mclassdef, ntype)
                                                if mtype == null then return # Forward error
                                        end
                                end
@@ -378,15 +379,12 @@ redef class ModelBuilder
        var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
 
        # Return the static type associated to the node `ntype`.
-       # `nclassdef` is the context where the call is made (used to understand formal types)
-       # The mmodule used as context is `nclassdef.mmodule`
+       # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
        # In case of problem, an error is displayed on `ntype` and null is returned.
        # FIXME: the name "resolve_mtype" is awful
-       fun resolve_mtype_unchecked(nclassdef: AClassdef, ntype: AType, with_virtual: Bool): nullable MType
+       fun resolve_mtype_unchecked(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType, with_virtual: Bool): nullable MType
        do
                var name = ntype.n_id.text
-               var mclassdef = nclassdef.mclassdef
-               var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
                var res: MType
 
                # Check virtual type
@@ -441,7 +439,7 @@ redef class ModelBuilder
                        else
                                var mtypes = new Array[MType]
                                for nt in ntype.n_types do
-                                       var mt = resolve_mtype_unchecked(nclassdef, nt, with_virtual)
+                                       var mt = resolve_mtype_unchecked(mmodule, mclassdef, nt, with_virtual)
                                        if mt == null then return null # Forward error
                                        mtypes.add(mt)
                                end
@@ -458,27 +456,26 @@ redef class ModelBuilder
        end
 
        # Return the static type associated to the node `ntype`.
-       # `nclassdef` is the context where the call is made (used to understand formal types)
-       # The mmodule used as context is `nclassdef.mmodule`
+       # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
        # In case of problem, an error is displayed on `ntype` and null is returned.
        # FIXME: the name "resolve_mtype" is awful
-       fun resolve_mtype(nclassdef: AClassdef, ntype: AType): nullable MType
+       fun resolve_mtype(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType): nullable MType
        do
                var mtype = ntype.mtype
-               if mtype == null then mtype = resolve_mtype_unchecked(nclassdef, ntype, true)
+               if mtype == null then mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
                if mtype == null then return null # Forward error
 
                if ntype.checked_mtype then return mtype
                if mtype isa MGenericType then
-                       var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
-                       var mclassdef = nclassdef.mclassdef
                        var mclass = mtype.mclass
                        for i in [0..mclass.arity[ do
                                var bound = mclass.intro.bound_mtype.arguments[i]
                                var nt = ntype.n_types[i]
-                               var mt = resolve_mtype(nclassdef, nt)
+                               var mt = resolve_mtype(mmodule, mclassdef, nt)
                                if mt == null then return null # forward error
-                               if not mt.is_subtype(mmodule, mclassdef.bound_mtype, bound) then
+                               var anchor
+                               if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
+                               if not mt.is_subtype(mmodule, anchor, bound) then
                                        error(nt, "Type error: expected {bound}, got {mt}")
                                        return null
                                end
index 56ff873..e039c67 100644 (file)
@@ -343,15 +343,16 @@ redef class ASignature
        var ret_type: nullable MType = null
 
        # Visit and fill information about a signature
-       private fun visit_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef): Bool
+       private fun visit_signature(modelbuilder: ModelBuilder, mclassdef: MClassDef): Bool
        do
+               var mmodule = mclassdef.mmodule
                var param_names = self.param_names
                var param_types = self.param_types
                for np in self.n_params do
                        param_names.add(np.n_id.text)
                        var ntype = np.n_type
                        if ntype != null then
-                               var mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
+                               var mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
                                if mtype == null then return false # Skip error
                                for i in [0..param_names.length-param_types.length[ do
                                        param_types.add(mtype)
@@ -368,7 +369,7 @@ redef class ASignature
                end
                var ntype = self.n_type
                if ntype != null then
-                       self.ret_type = modelbuilder.resolve_mtype(nclassdef, ntype)
+                       self.ret_type = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
                        if self.ret_type == null then return false # Skip errir
                end
 
@@ -491,7 +492,8 @@ redef class AMethPropdef
        do
                var mpropdef = self.mpropdef
                if mpropdef == null then return # Error thus skiped
-               var mmodule = mpropdef.mclassdef.mmodule
+               var mclassdef = mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
                var nsig = self.n_signature
 
                # Retrieve info from the signature AST
@@ -500,7 +502,7 @@ redef class AMethPropdef
                var vararg_rank = -1
                var ret_type: nullable MType = null # Return type from the AST
                if nsig != null then
-                       if not nsig.visit_signature(modelbuilder, nclassdef) then return
+                       if not nsig.visit_signature(modelbuilder, mclassdef) then return
                        param_names = nsig.param_names
                        param_types = nsig.param_types
                        vararg_rank = nsig.vararg_rank
@@ -768,12 +770,13 @@ redef class AAttrPropdef
        do
                var mpropdef = self.mpropdef
                if mpropdef == null then return # Error thus skiped
-               var mmodule = mpropdef.mclassdef.mmodule
+               var mclassdef = mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
                var mtype: nullable MType = null
 
                var ntype = self.n_type
                if ntype != null then
-                       mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
+                       mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
                        if mtype == null then return
                end
 
@@ -781,7 +784,7 @@ redef class AAttrPropdef
                if mtype == null then
                        if nexpr != null then
                                if nexpr isa ANewExpr then
-                                       mtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
+                                       mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
                                else if nexpr isa AIntExpr then
                                        var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
                                        if cla != null then mtype = cla.mclass_type
@@ -810,7 +813,7 @@ redef class AAttrPropdef
                else
                        assert ntype != null
                        if nexpr isa ANewExpr then
-                               var xmtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
+                               var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
                                if xmtype == mtype and modelbuilder.toolcontext.opt_warn.value >= 2 then
                                        modelbuilder.warning(ntype, "Warning: useless type definition")
                                end
@@ -967,11 +970,12 @@ redef class ATypePropdef
        do
                var mpropdef = self.mpropdef
                if mpropdef == null then return # Error thus skiped
-               var mmodule = mpropdef.mclassdef.mmodule
+               var mclassdef = mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
                var mtype: nullable MType = null
 
                var ntype = self.n_type
-               mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
+               mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
                if mtype == null then return
 
                mpropdef.bound = mtype
index 6f85029..604f4f9 100644 (file)
@@ -298,9 +298,8 @@ redef class AFullPropExternCall
        do
                var mmodule = npropdef.mpropdef.mclassdef.mmodule
                var mclassdef = npropdef.mpropdef.mclassdef
-               var nclassdef = toolcontext.modelbuilder.mclassdef2nclassdef[mclassdef]
                var mclass_type = mclassdef.bound_mtype
-               var mtype = toolcontext.modelbuilder.resolve_mtype(nclassdef, n_type)
+               var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
 
                if mtype == null then return
 
@@ -333,8 +332,7 @@ redef class AInitPropExternCall
        do
                var mmodule = npropdef.mpropdef.mclassdef.mmodule
                var mclassdef = npropdef.mpropdef.mclassdef
-               var nclassdef = toolcontext.modelbuilder.mclassdef2nclassdef[mclassdef]
-               var mtype = toolcontext.modelbuilder.resolve_mtype(nclassdef, n_type)
+               var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
                if mtype == null then return
 
                if not mtype isa MClassType then
@@ -388,9 +386,10 @@ redef class ACastAsExternCall
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_from_type, true)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_to_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_from_type, true)
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_to_type, true)
                super
        end
 end
@@ -401,8 +400,9 @@ redef class AAsNullableExternCall
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
                super
        end
 end
@@ -417,8 +417,9 @@ redef class AAsNotNullableExternCall
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
                super
        end
 end
index db7560f..3db1ca3 100644 (file)
@@ -190,7 +190,7 @@ private class TypeVisitor
 
        fun resolve_mtype(node: AType): nullable MType
        do
-               return self.modelbuilder.resolve_mtype(self.nclassdef, node)
+               return self.modelbuilder.resolve_mtype(mmodule, mpropdef.mclassdef, node)
        end
 
        fun try_get_mclass(node: ANode, name: String): nullable MClass