src: transform all old writable in annotations
[nit.git] / src / model / model.nit
index be29e6f..765e601 100644 (file)
@@ -25,8 +25,6 @@
 # FIXME: better handling of the types
 module model
 
-import poset
-import location
 import mmodule
 import mdoc
 import ordered_tree
@@ -236,6 +234,13 @@ redef class MModule
                return get_primitive_class("Sys").mclass_type
        end
 
+       fun finalizable_type: nullable MClassType
+       do
+               var clas = self.model.get_mclasses_by_name("Finalizable")
+               if clas == null then return null
+               return get_primitive_class("Finalizable").mclass_type
+       end
+
        # Force to get the primitive class named `name` or abort
        fun get_primitive_class(name: String): MClass
        do
@@ -387,6 +392,8 @@ class MClass
                end
        end
 
+       redef fun model do return intro_mmodule.model
+
        # All class definitions (introduction and refinements)
        var mclassdefs: Array[MClassDef] = new Array[MClassDef]
 
@@ -510,6 +517,8 @@ class MClassDef
        # Actually the name of the `mclass`
        redef fun name do return mclass.name
 
+       redef fun model do return mmodule.model
+
        # All declared super-types
        # FIXME: quite ugly but not better idea yet
        var supertypes: Array[MClassType] = new Array[MClassType]
@@ -601,8 +610,7 @@ end
 abstract class MType
        super MEntity
 
-       # The model of the type
-       fun model: Model is abstract
+       redef fun name do return to_s
 
        # Return true if `self` is an subtype of `sup`.
        # The typing is done using the standard typing policy of Nit.
@@ -884,6 +892,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
 
 
@@ -1150,6 +1168,20 @@ class MVirtualType
                abort
        end
 
+       # Is the virtual type fixed for a given resolved_receiver?
+       fun is_fixed(mmodule: MModule, resolved_receiver: MType): Bool
+       do
+               assert not resolved_receiver.need_anchor
+               var props = self.mproperty.lookup_definitions(mmodule, resolved_receiver)
+               if props.is_empty then
+                       abort
+               end
+               for p in props do
+                       if p.as(MVirtualTypeDef).is_fixed then return true
+               end
+               return false
+       end
+
        redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
        do
                assert can_resolve_for(mtype, anchor, mmodule)
@@ -1178,6 +1210,8 @@ class MVirtualType
                if resolved_reciever.as(MClassType).mclass.kind == enum_kind then return res
                # If the resolved type isa MVirtualType, it means that self was bound to it, and cannot be unbound. self is just fixed. so return the resolution.
                if res isa MVirtualType then return res
+               # If we are final, just return the resolution
+               if is_fixed(mmodule, resolved_reciever) then return res
                # It the resolved type isa intern class, then there is no possible valid redefinition is any potentiel subclass. self is just fixed. so simply return the resolution
                if res isa MClassType and res.mclass.kind == enum_kind then return res
                # TODO: Add 'fixed' virtual type in the specification.
@@ -1273,7 +1307,13 @@ class MParameterType
                #print "{class_name}: {self}/{mtype}/{anchor}?"
 
                if mtype isa MGenericType and mtype.mclass == self.mclass then
-                       return mtype.arguments[self.rank]
+                       var res = mtype.arguments[self.rank]
+                       if anchor != null and res.need_anchor then
+                               # Maybe the result can be resolved more if are bound to a final class
+                               var r2 = res.anchor_to(mmodule, anchor)
+                               if r2 isa MClassType and r2.mclass.kind == enum_kind then return r2
+                       end
+                       return res
                end
 
                # self is a parameter type of mtype (or of a super-class of mtype)
@@ -1352,6 +1392,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)
@@ -1508,8 +1549,10 @@ end
 
 # A parameter in a signature
 class MParameter
+       super MEntity
+
        # The name of the parameter
-       var name: String
+       redef var name: String
 
        # The static type of the parameter
        var mtype: MType
@@ -1517,6 +1560,12 @@ class MParameter
        # Is the parameter a vararg?
        var is_vararg: Bool
 
+       init(name: String, mtype: MType, is_vararg: Bool) do
+               self.name = name
+               self.mtype = mtype
+               self.is_vararg = is_vararg
+       end
+
        redef fun to_s
        do
                if is_vararg then
@@ -1533,6 +1582,8 @@ class MParameter
                var res = new MParameter(self.name, newtype, self.is_vararg)
                return res
        end
+
+       redef fun model do return mtype.model
 end
 
 # A service (global property) that generalize method, attribute, etc.
@@ -1593,6 +1644,8 @@ abstract class MProperty
        # associated definition, this method will abort
        fun intro: MPROPDEF do return mpropdefs.first
 
+       redef fun model do return intro.model
+
        # Alias for `name`
        redef fun to_s do return name
 
@@ -1605,7 +1658,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
@@ -1644,7 +1697,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]
@@ -1721,7 +1774,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
@@ -1765,15 +1818,18 @@ class MMethod
 
        # Is the property defined at the top_level of the module?
        # Currently such a property are stored in `Object`
-       var is_toplevel: Bool writable = false
+       var is_toplevel: Bool = false is writable
 
        # Is the property a constructor?
        # Warning, this property can be inherited by subclasses with or without being a constructor
        # therefore, you should use `is_init_for` the verify if the property is a legal constructor for a given class
-       var is_init: Bool writable = false
+       var is_init: Bool = false is writable
+
+       # The constructor is a (the) root init with empty signature but a set of initializers
+       var is_root_init: Bool = false is writable
 
        # The the property a 'new' contructor?
-       var is_new: Bool writable = false
+       var is_new: Bool = false is writable
 
        # Is the property a legal constructor for a given class?
        # As usual, visibility is not considered.
@@ -1847,6 +1903,8 @@ abstract class MPropDef
        # Actually the name of the `mproperty`
        redef fun name do return mproperty.name
 
+       redef fun model do return mclassdef.model
+
        # Internal name combining the module, the class and the property
        # Example: "mymodule#MyClass#mymethod"
        redef var to_s: String
@@ -1886,16 +1944,29 @@ class MMethodDef
        end
 
        # The signature attached to the property definition
-       var msignature: nullable MSignature writable = null
+       var msignature: nullable MSignature = null is writable
+
+       # The signature attached to the `new` call on a root-init
+       # This is a concatenation of the signatures of the initializers
+       #
+       # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
+       var new_msignature: nullable MSignature = null is writable
+
+       # List of initialisers to call in root-inits
+       #
+       # They could be setters or attributes
+       #
+       # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
+       var initializers = new Array[MProperty]
 
        # Is the method definition abstract?
-       var is_abstract: Bool writable = false
+       var is_abstract: Bool = false is writable
 
        # Is the method definition intern?
-       var is_intern writable = false
+       var is_intern = false is writable
 
        # Is the method definition extern?
-       var is_extern writable = false
+       var is_extern = false is writable
 end
 
 # A local definition of an attribute
@@ -1911,7 +1982,7 @@ class MAttributeDef
        end
 
        # The static type of the attribute
-       var static_mtype: nullable MType writable = null
+       var static_mtype: nullable MType = null is writable
 end
 
 # A local definition of a virtual type
@@ -1927,7 +1998,10 @@ class MVirtualTypeDef
        end
 
        # The bound of the virtual type
-       var bound: nullable MType writable = null
+       var bound: nullable MType = null is writable
+
+       # Is the bound fixed?
+       var is_fixed = false is writable
 end
 
 # A kind of class.