test_parser: add option `-x` to output XML
[nit.git] / src / modelbuilder_base.nit
index b940049..93423df 100644 (file)
@@ -81,6 +81,16 @@ class ModelBuilder
                return res
        end
 
+       # Like `try_get_mclass_by_name` but display an error message when the class is not found
+       fun get_mclass_by_name(node: ANode, mmodule: MModule, name: String): nullable MClass
+       do
+               var mclass = try_get_mclass_by_name(node, mmodule, name)
+               if mclass == null then
+                       error(node, "Type Error: missing primitive class `{name}'.")
+               end
+               return mclass
+       end
+
        # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
        # Visibility in modules is correctly handled.
        # Protected properties are returned (it is up to the caller to check and reject protected properties).
@@ -305,13 +315,15 @@ class ModelBuilder
                if mtype isa MGenericType then
                        var mclass = mtype.mclass
                        for i in [0..mclass.arity[ do
-                               var bound = mclass.intro.bound_mtype.arguments[i]
+                               var intro = mclass.try_intro
+                               if intro == null then return null # skip error
+                               var bound = intro.bound_mtype.arguments[i]
                                var nt = ntype.n_types[i]
                                var mt = resolve_mtype(mmodule, mclassdef, nt)
                                if mt == null then return null # forward error
                                var anchor
                                if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
-                               if not mt.is_subtype(mmodule, anchor, bound) then
+                               if not check_subtype(nt, mmodule, anchor, mt, bound) then
                                        error(nt, "Type error: expected {bound}, got {mt}")
                                        return null
                                end
@@ -320,6 +332,26 @@ class ModelBuilder
                ntype.checked_mtype = true
                return mtype
        end
+
+       # Check that `sub` is a subtype of `sup`.
+       # Do not display an error message.
+       #
+       # This method is used a an entry point for the modelize phase to test static subtypes.
+       # Some refinements could redefine it to collect statictics.
+       fun check_subtype(node: ANode, mmodule: MModule, anchor: nullable MClassType, sub, sup: MType): Bool
+       do
+               return sub.is_subtype(mmodule, anchor, sup)
+       end
+
+       # Check that `sub` and `sup` are equvalent types.
+       # Do not display an error message.
+       #
+       # This method is used a an entry point for the modelize phase to test static equivalent types.
+       # Some refinements could redefine it to collect statictics.
+       fun check_sametype(node: ANode, mmodule: MModule, anchor: nullable MClassType, sub, sup: MType): Bool
+       do
+               return sub.is_subtype(mmodule, anchor, sup) and sup.is_subtype(mmodule, anchor, sub)
+       end
 end
 
 redef class AType
@@ -355,7 +387,7 @@ redef class ADoc
        do
                var res = mdoc_cache
                if res != null then return res
-               res = new MDoc
+               res = new MDoc(location)
                for c in n_comment do
                        var text = c.text
                        if text.length < 2 then