src: use MFormalType for type checks when it makes sense
[nit.git] / src / model / model.nit
index b483543..bace060 100644 (file)
@@ -195,31 +195,40 @@ redef class MModule
        private var flatten_mclass_hierarchy_cache: nullable POSet[MClass] = null
 
        # The primitive type `Object`, the root of the class hierarchy
-       fun object_type: MClassType
-       do
-               var res = self.object_type_cache
-               if res != null then return res
-               res = self.get_primitive_class("Object").mclass_type
-               self.object_type_cache = res
-               return res
-       end
-
-       private var object_type_cache: nullable MClassType
+       var object_type: MClassType = self.get_primitive_class("Object").mclass_type is lazy
 
        # The type `Pointer`, super class to all extern classes
        var pointer_type: MClassType = self.get_primitive_class("Pointer").mclass_type is lazy
 
        # The primitive type `Bool`
-       fun bool_type: MClassType
-       do
-               var res = self.bool_type_cache
-               if res != null then return res
-               res = self.get_primitive_class("Bool").mclass_type
-               self.bool_type_cache = res
-               return res
-       end
+       var bool_type: MClassType = self.get_primitive_class("Bool").mclass_type is lazy
+
+       # The primitive type `Int`
+       var int_type: MClassType = self.get_primitive_class("Int").mclass_type is lazy
+
+       # The primitive type `Char`
+       var char_type: MClassType = self.get_primitive_class("Char").mclass_type is lazy
+
+       # The primitive type `Float`
+       var float_type: MClassType = self.get_primitive_class("Float").mclass_type is lazy
 
-       private var bool_type_cache: nullable MClassType
+       # The primitive type `String`
+       var string_type: MClassType = self.get_primitive_class("String").mclass_type is lazy
+
+       # The primitive type `NativeString`
+       var native_string_type: MClassType = self.get_primitive_class("NativeString").mclass_type is lazy
+
+       # A primitive type of `Array`
+       fun array_type(elt_type: MType): MClassType do return array_class.get_mtype([elt_type])
+
+       # The primitive class `Array`
+       var array_class: MClass = self.get_primitive_class("Array") is lazy
+
+       # A primitive type of `NativeArray`
+       fun native_array_type(elt_type: MType): MClassType do return native_array_class.get_mtype([elt_type])
+
+       # The primitive class `NativeArray`
+       var native_array_class: MClass = self.get_primitive_class("NativeArray") is lazy
 
        # The primitive type `Sys`, the main type of the program, if any
        fun sys_type: nullable MClassType
@@ -468,6 +477,9 @@ class MClass
        end
 
        private var get_mtype_cache = new HashMap[Array[MType], MGenericType]
+
+       # Is there a `new` factory to allow the pseudo instantiation?
+       var has_new_factory = false is writable
 end
 
 
@@ -701,7 +713,7 @@ abstract class MType
                # Now the case of direct null and nullable is over.
 
                # If `sub` is a formal type, then it is accepted if its bound is accepted
-               while sub isa MParameterType or sub isa MVirtualType do
+               while sub isa MFormalType do
                        #print "3.is {sub} a {sup}?"
 
                        # A unfixed formal type can only accept itself
@@ -725,7 +737,7 @@ abstract class MType
                assert sub isa MClassType # It is the only remaining type
 
                # A unfixed formal type can only accept itself
-               if sup isa MParameterType or sup isa MVirtualType then
+               if sup isa MFormalType then
                        return false
                end
 
@@ -1247,9 +1259,17 @@ class MGenericType
        end
 end
 
+# A formal type (either virtual of parametric).
+#
+# The main issue with formal types is that they offer very little information on their own
+# and need a context (anchor and mmodule) to be useful.
+abstract class MFormalType
+       super MType
+end
+
 # A virtual formal type.
 class MVirtualType
-       super MType
+       super MFormalType
 
        # The property associated with the type.
        # Its the definitions of this property that determine the bound or the virtual type.
@@ -1377,7 +1397,7 @@ end
 # Note that parameter types are shared among class refinements.
 # Therefore parameter only have an internal name (see `to_s` for details).
 class MParameterType
-       super MType
+       super MFormalType
 
        # The generic class where the parameter belong
        var mclass: MClass