X-Git-Url: http://nitlanguage.org diff --git a/src/model/model.nit b/src/model/model.nit index f3c3164..9be5738 100644 --- a/src/model/model.nit +++ b/src/model/model.nit @@ -25,8 +25,6 @@ # FIXME: better handling of the types module model -import poset -import location import mmodule import mdoc import ordered_tree @@ -34,16 +32,16 @@ private import more_collections redef class Model # All known classes - var mclasses: Array[MClass] = new Array[MClass] + var mclasses = new Array[MClass] # All known properties - var mproperties: Array[MProperty] = new Array[MProperty] + var mproperties = new Array[MProperty] # Hierarchy of class definition. # # Each classdef is associated with its super-classdefs in regard to # its module of definition. - var mclassdef_hierarchy: POSet[MClassDef] = new POSet[MClassDef] + var mclassdef_hierarchy = new POSet[MClassDef] # Class-type hierarchy restricted to the introduction. # @@ -54,7 +52,7 @@ redef class Model # This poset will evolve in a monotonous way: # * Two non connected nodes will remain unconnected # * New nodes can appear with new edges - private var intro_mtype_specialization_hierarchy: POSet[MClassType] = new POSet[MClassType] + private var intro_mtype_specialization_hierarchy = new POSet[MClassType] # Global overlapped class-type hierarchy. # The hierarchy when all modules are combined. @@ -63,10 +61,10 @@ redef class Model # This poset will evolve in an anarchic way. Loops can even be created. # # FIXME decide what to do on loops - private var full_mtype_specialization_hierarchy: POSet[MClassType] = new POSet[MClassType] + private var full_mtype_specialization_hierarchy = new POSet[MClassType] # Collections of classes grouped by their short name - private var mclasses_by_name: MultiHashMap[String, MClass] = new MultiHashMap[String, MClass] + private var mclasses_by_name = new MultiHashMap[String, MClass] # Return all class named `name`. # @@ -84,7 +82,7 @@ redef class Model end # Collections of properties grouped by their short name - private var mproperties_by_name: MultiHashMap[String, MProperty] = new MultiHashMap[String, MProperty] + private var mproperties_by_name = new MultiHashMap[String, MProperty] # Return all properties named `name`. # @@ -102,7 +100,7 @@ redef class Model end # The only null type - var null_type: MNullType = new MNullType(self) + var null_type = new MNullType(self) # Build an ordered tree with from `concerns` fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do @@ -136,11 +134,11 @@ end redef class MModule # All the classes introduced in the module - var intro_mclasses: Array[MClass] = new Array[MClass] + var intro_mclasses = new Array[MClass] # All the class definitions of the module # (introduction and refinement) - var mclassdefs: Array[MClassDef] = new Array[MClassDef] + var mclassdefs = new Array[MClassDef] # Does the current module has a given class `mclass`? # Return true if the mmodule introduces, refines or imports a class. @@ -177,14 +175,14 @@ redef class MModule return res end - # Sort a given array of classes using the linerarization order of the module + # Sort a given array of classes using the linearization order of the module # The most general is first, the most specific is last fun linearize_mclasses(mclasses: Array[MClass]) do self.flatten_mclass_hierarchy.sort(mclasses) end - # Sort a given array of class definitions using the linerarization order of the module + # Sort a given array of class definitions using the linearization order of the module # the refinement link is stronger than the specialisation link # The most general is first, the most specific is last fun linearize_mclassdefs(mclassdefs: Array[MClassDef]) @@ -193,7 +191,7 @@ redef class MModule sorter.sort(mclassdefs) end - # Sort a given array of property definitions using the linerarization order of the module + # Sort a given array of property definitions using the linearization order of the module # the refinement link is stronger than the specialisation link # The most general is first, the most specific is last fun linearize_mpropdefs(mpropdefs: Array[MPropDef]) @@ -216,6 +214,9 @@ redef class MModule private var object_type_cache: nullable MClassType + # 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 @@ -236,6 +237,8 @@ redef class MModule return get_primitive_class("Sys").mclass_type end + # The primitive type `Finalizable` + # Used to tag classes that need to be finalized. fun finalizable_type: nullable MClassType do var clas = self.model.get_mclasses_by_name("Finalizable") @@ -249,8 +252,8 @@ redef class MModule var cla = self.model.get_mclasses_by_name(name) if cla == null then if name == "Bool" then - var c = new MClass(self, name, 0, enum_kind, public_visibility) - var cladef = new MClassDef(self, c.mclass_type, new Location(null, 0,0,0,0), new Array[String]) + var c = new MClass(self, name, null, enum_kind, public_visibility) + var cladef = new MClassDef(self, c.mclass_type, new Location(null, 0,0,0,0)) return c end print("Fatal Error: no primitive class {name}") @@ -290,7 +293,8 @@ redef class MModule end private class MClassDefSorter - super AbstractSorter[MClassDef] + super Comparator + redef type COMPARED: MClassDef var mmodule: MModule redef fun compare(a, b) do @@ -302,7 +306,8 @@ private class MClassDefSorter end private class MPropDefSorter - super AbstractSorter[MPropDef] + super Comparator + redef type COMPARED: MPropDef var mmodule: MModule redef fun compare(pa, pb) do @@ -357,35 +362,30 @@ class MClass # The number of generic formal parameters # 0 if the class is not generic - var arity: Int + var arity: Int is noinit - # The kind of the class (interface, abstract class, etc.) - # In Nit, the kind of a class cannot evolve in refinements - var kind: MClassKind + # Each generic formal parameters in order. + # is empty if the class is not generic + var mparameters = new Array[MParameterType] - # The visibility of the class - # In Nit, the visibility of a class cannot evolve in refinements - var visibility: MVisibility - - init(intro_mmodule: MModule, name: String, arity: Int, kind: MClassKind, visibility: MVisibility) + protected fun setup_parameter_names(parameter_names: nullable Array[String]) is + autoinit do - self.intro_mmodule = intro_mmodule - self.name = name - self.arity = arity - self.kind = kind - self.visibility = visibility - intro_mmodule.intro_mclasses.add(self) - var model = intro_mmodule.model - model.mclasses_by_name.add_one(name, self) - model.mclasses.add(self) + if parameter_names == null then + self.arity = 0 + else + self.arity = parameter_names.length + end # Create the formal parameter types if arity > 0 then + assert parameter_names != null var mparametertypes = new Array[MParameterType] for i in [0..arity[ do - var mparametertype = new MParameterType(self, i) + var mparametertype = new MParameterType(self, i, parameter_names[i]) mparametertypes.add(mparametertype) end + self.mparameters = mparametertypes var mclass_type = new MGenericType(self, mparametertypes) self.mclass_type = mclass_type self.get_mtype_cache.add(mclass_type) @@ -394,23 +394,35 @@ class MClass end end + # The kind of the class (interface, abstract class, etc.) + # In Nit, the kind of a class cannot evolve in refinements + var kind: MClassKind + + # The visibility of the class + # In Nit, the visibility of a class cannot evolve in refinements + var visibility: MVisibility + + init + do + intro_mmodule.intro_mclasses.add(self) + var model = intro_mmodule.model + model.mclasses_by_name.add_one(name, self) + model.mclasses.add(self) + end + redef fun model do return intro_mmodule.model # All class definitions (introduction and refinements) - var mclassdefs: Array[MClassDef] = new Array[MClassDef] + var mclassdefs = new Array[MClassDef] # Alias for `name` redef fun to_s do return self.name - # The definition that introduced the class - # Warning: the introduction is the first `MClassDef` object associated - # to self. If self is just created without having any associated - # definition, this method will abort - fun intro: MClassDef - do - assert has_a_first_definition: not mclassdefs.is_empty - return mclassdefs.first - end + # The definition that introduces the class. + # + # Warning: such a definition may not exist in the early life of the object. + # In this case, the method will abort. + var intro: MClassDef is noinit # Return the class `self` in the class hierarchy of the module `mmodule`. # @@ -436,7 +448,7 @@ class MClass # To get other types based on a generic class, see `get_mtype`. # # ENSURE: `mclass_type.mclass == self` - var mclass_type: MClassType + var mclass_type: MClassType is noinit # Return a generic type based on the class # Is the class is not generic, then the result is `mclass_type` @@ -456,7 +468,7 @@ class MClass return res end - private var get_mtype_cache: Array[MGenericType] = new Array[MGenericType] + private var get_mtype_cache = new Array[MGenericType] end @@ -479,7 +491,7 @@ class MClassDef var mmodule: MModule # The associated `MClass` - var mclass: MClass + var mclass: MClass is noinit # The bounded type associated to the mclassdef # @@ -493,26 +505,22 @@ class MClassDef # ENSURE: `bound_mtype.mclass == self.mclass` var bound_mtype: MClassType - # Name of each formal generic parameter (in order of declaration) - var parameter_names: Array[String] - # The origin of the definition var location: Location # Internal name combining the module and the class # Example: "mymodule#MyClass" - redef var to_s: String + redef var to_s: String is noinit - init(mmodule: MModule, bound_mtype: MClassType, location: Location, parameter_names: Array[String]) + init do - assert bound_mtype.mclass.arity == parameter_names.length - self.bound_mtype = bound_mtype - self.mmodule = mmodule self.mclass = bound_mtype.mclass - self.location = location mmodule.mclassdefs.add(self) mclass.mclassdefs.add(self) - self.parameter_names = parameter_names + if mclass.intro_mmodule == mmodule then + assert not isset mclass._intro + mclass.intro = self + end self.to_s = "{mmodule}#{mclass}" end @@ -523,7 +531,7 @@ class MClassDef # All declared super-types # FIXME: quite ugly but not better idea yet - var supertypes: Array[MClassType] = new Array[MClassType] + var supertypes = new Array[MClassType] # Register some super-types for the class (ie "super SomeType") # @@ -576,10 +584,10 @@ class MClassDef fun is_intro: Bool do return mclass.intro == self # All properties introduced by the classdef - var intro_mproperties: Array[MProperty] = new Array[MProperty] + var intro_mproperties = new Array[MProperty] # All property definitions in the class (introductions and redefinitions) - var mpropdefs: Array[MPropDef] = new Array[MPropDef] + var mpropdefs = new Array[MPropDef] end # A global static type @@ -632,8 +640,8 @@ abstract class MType end # First, resolve the formal types to a common version in the receiver - # The trick here is that fixed formal type will be associed to the bound - # And unfixed formal types will be associed to a canonical formal type. + # The trick here is that fixed formal type will be associated to the bound + # And unfixed formal types will be associated to a canonical formal type. if sub isa MParameterType or sub isa MVirtualType then assert anchor != null sub = sub.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, false) @@ -724,6 +732,7 @@ abstract class MType # types to their bounds. # # Example + # # class A end # class B super A end # class X end @@ -735,6 +744,7 @@ abstract class MType # super G[B] # redef type U: Y # end + # # Map[T,U] anchor_to H #-> Map[B,Y] # # Explanation of the example: @@ -763,9 +773,13 @@ abstract class MType # In Nit, for each super-class of a type, there is a equivalent super-type. # # Example: + # + # ~~~nitish # class G[T, U] end # class H[V] super G[V, Bool] end + # # H[Int] supertype_to G #-> G[Int, Bool] + # ~~~ # # REQUIRE: `super_mclass` is a super-class of `self` # REQUIRE: `self.need_anchor implies anchor != null and self.can_resolve_for(anchor, null, mmodule)` @@ -799,9 +813,11 @@ abstract class MType # # ## Example 1 # - # class G[E] end - # class H[F] super G[F] end - # class X[Z] end + # ~~~ + # class G[E] end + # class H[F] super G[F] end + # class X[Z] end + # ~~~ # # * Array[E].resolve_for(H[Int]) #-> Array[Int] # * Array[E].resolve_for(G[Z], X[Int]) #-> Array[Z] @@ -819,30 +835,34 @@ abstract class MType # # ## Example 2 # - # class A[E] - # fun foo(e:E):E is abstract - # end - # class B super A[Int] end + # ~~~ + # class A[E] + # fun foo(e:E):E is abstract + # end + # class B super A[Int] end + # ~~~ # # The signature on foo is (e: E): E # If we resolve the signature for B, we get (e:Int):Int # # ## Example 3 # - # class A[E] - # fun foo(e:E) is abstract - # end - # class B[F] - # var a: A[Array[F]] - # fun bar do a.foo(x) # <- x is here - # end + # ~~~nitish + # class A[E] + # fun foo(e:E):E is abstract + # end + # class C[F] + # var a: A[Array[F]] + # fun bar do a.foo(x) # <- x is here + # end + # ~~~ # # The first question is: is foo available on `a`? # # The static type of a is `A[Array[F]]`, that is an open type. # in order to find a method `foo`, whe must look at a resolved type. # - # A[Array[F]].anchor_to(B[nullable Object]) #-> A[Array[nullable Object]] + # A[Array[F]].anchor_to(C[nullable Object]) #-> A[Array[nullable Object]] # # the method `foo` exists in `A[Array[nullable Object]]`, therefore `foo` exists for `a`. # @@ -850,7 +870,7 @@ abstract class MType # # the signature of `foo` is `foo(e:E)`, thus we must resolve the type E # - # E.resolve_for(A[Array[F]],B[nullable Object]) #-> Array[F] + # E.resolve_for(A[Array[F]],C[nullable Object]) #-> Array[F] # # The resolution can be done because `E` make sense for the class A (see `can_resolve_for`) # @@ -872,11 +892,15 @@ abstract class MType # class B[F] # end # - # * E.can_resolve_for(A[Int]) #-> true, E make sense in A - # * E.can_resolve_for(B[Int]) #-> false, E does not make sense in B - # * B[E].can_resolve_for(A[F], B[Object]) #-> true, - # B[E] is a red hearing only the E is important, - # E make sense in A + # ~~~nitish + # E.can_resolve_for(A[Int]) #-> true, E make sense in A + # + # E.can_resolve_for(B[Int]) #-> false, E does not make sense in B + # + # B[E].can_resolve_for(A[F], B[Object]) #-> true, + # # B[E] is a red hearing only the E is important, + # # E make sense in A + # ~~~ # # REQUIRE: `anchor != null implies not anchor.need_anchor` # REQUIRE: `mtype.need_anchor implies anchor != null and mtype.can_resolve_for(anchor, null, mmodule)` @@ -898,7 +922,7 @@ abstract class MType # 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. + # For instance if `self isa MNullType` or self is a formal type bounded by a nullable type. fun as_notnullable: MType do return self @@ -907,7 +931,7 @@ abstract class MType private var as_nullable_cache: nullable MType = null - # The deph of the type seen as a tree. + # The depth of the type seen as a tree. # # * A -> 1 # * G[A] -> 2 @@ -978,14 +1002,11 @@ class MClassType redef fun model do return self.mclass.intro_mmodule.model - private init(mclass: MClass) - do - self.mclass = mclass - end + # TODO: private init because strongly bounded to its mclass. see `mclass.mclass_type` # The formal arguments of the type # ENSURE: `result.length == self.mclass.arity` - var arguments: Array[MType] = new Array[MType] + var arguments = new Array[MType] redef fun to_s do return mclass.to_s @@ -1060,9 +1081,9 @@ class MClassType collect_mtypes_cache[mmodule] = types end - private var collect_mclassdefs_cache: HashMap[MModule, Set[MClassDef]] = new HashMap[MModule, Set[MClassDef]] - private var collect_mclasses_cache: HashMap[MModule, Set[MClass]] = new HashMap[MModule, Set[MClass]] - private var collect_mtypes_cache: HashMap[MModule, Set[MClassType]] = new HashMap[MModule, Set[MClassType]] + private var collect_mclassdefs_cache = new HashMap[MModule, Set[MClassDef]] + private var collect_mclasses_cache = new HashMap[MModule, Set[MClass]] + private var collect_mtypes_cache = new HashMap[MModule, Set[MClassType]] end @@ -1071,11 +1092,13 @@ end class MGenericType super MClassType - private init(mclass: MClass, arguments: Array[MType]) + redef var arguments + + # TODO: private init because strongly bounded to its mclass. see `mclass.get_mtype` + + init do - super(mclass) assert self.mclass.arity == arguments.length - self.arguments = arguments self.need_anchor = false for t in arguments do @@ -1090,9 +1113,9 @@ class MGenericType # Recursively print the type of the arguments within brackets. # Example: `"Map[String, List[Int]]"` - redef var to_s: String + redef var to_s: String is noinit - redef var need_anchor: Bool + redef var need_anchor: Bool is noinit redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do @@ -1202,21 +1225,20 @@ class MVirtualType var verbatim_bound = lookup_bound(mmodule, resolved_reciever) # The bound is exactly as declared in the "type" property, so we must resolve it again var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual) - #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}" + #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_receiver}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}" # What to return here? There is a bunch a special cases: # If 'cleanup_virtual' we must return the resolved type, since we cannot return self if cleanup_virtual then return res - # If the reciever is a intern class, then the virtual type cannot be redefined since there is no possible subclass. self is just fixed. so simply return the resolution + # If the receiver is a intern class, then the virtual type cannot be redefined since there is no possible subclass. self is just fixed. so simply return the resolution if resolved_reciever isa MNullableType then resolved_reciever = resolved_reciever.mtype 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 the resolved type isa intern class, then there is no possible valid redefinition in any potential 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. # TODO: What if bound to a MParameterType? # Note that Nullable types can always be redefined by the non nullable version, so there is no specific case on it. @@ -1234,18 +1256,13 @@ class MVirtualType end redef fun to_s do return self.mproperty.to_s - - init(mproperty: MProperty) - do - self.mproperty = mproperty - end end -# The type associated the a formal parameter generic type of a class +# The type associated to a formal parameter generic type of a class # # Each parameter type is associated to a specific class. -# It's mean that all refinements of a same class "share" the parameter type, -# but that a generic subclass has its on parameter types. +# It means that all refinements of a same class "share" the parameter type, +# but that a generic subclass has its own parameter types. # # However, in the sense of the meta-model, a parameter type of a class is # a valid type in a subclass. The "in the sense of the meta-model" is @@ -1253,19 +1270,20 @@ end # directly to the parameter types of the super-classes. # # Example: +# # class A[E] # fun e: E is abstract # end # class B[F] # super A[Array[F]] # end +# # In the class definition B[F], `F` is a valid type but `E` is not. # However, `self.e` is a valid method call, and the signature of `e` is # declared `e: E`. # # Note that parameter types are shared among class refinements. # Therefore parameter only have an internal name (see `to_s` for details). -# TODO: Add a `name_for` to get better messages. class MParameterType super MType @@ -1278,12 +1296,9 @@ class MParameterType # FIXME: is `position` a better name? var rank: Int - # Internal name of the parameter type - # Names of parameter types changes in each class definition - # Therefore, this method return an internal name. - # Example: return "G#1" for the second parameter of the class G - # FIXME: add a way to get the real name in a classdef - redef fun to_s do return "{mclass}#{rank}" + redef var name + + redef fun to_s do return name # Resolve the bound for a given resolved_receiver # The result may be a other virtual type (or a parameter type) @@ -1321,7 +1336,7 @@ class MParameterType # self is a parameter type of mtype (or of a super-class of mtype) # The point of the function it to get the bound of the virtual type that make sense for mtype # But because mtype is maybe a virtual/formal type, we need to get a real receiver first - # FIXME: What happend here is far from clear. Thus this part must be validated and clarified + # FIXME: What happens here is far from clear. Thus this part must be validated and clarified var resolved_receiver if mtype.need_anchor then assert anchor != null @@ -1367,12 +1382,6 @@ class MParameterType end return mtype.collect_mclassdefs(mmodule).has(mclass.intro) end - - init(mclass: MClass, rank: Int) - do - self.mclass = mclass - self.rank = rank - end end # A type prefixed with "nullable" @@ -1384,13 +1393,12 @@ class MNullableType redef fun model do return self.mtype.model - init(mtype: MType) + init do - self.mtype = mtype self.to_s = "nullable {mtype}" end - redef var to_s: String + redef var to_s: String is noinit redef fun need_anchor do return mtype.need_anchor redef fun as_nullable do return self @@ -1435,10 +1443,6 @@ end class MNullType super MType redef var model: Model - protected init(model: Model) - do - self.model = model - end redef fun to_s do return "null" redef fun as_nullable do return self redef fun need_anchor do return false @@ -1486,7 +1490,7 @@ class MSignature end # REQUIRE: 1 <= mparameters.count p -> p.is_vararg - init(mparameters: Array[MParameter], return_mtype: nullable MType) + init do var vararg_rank = -1 for i in [0..mparameters.length[ do @@ -1496,15 +1500,13 @@ class MSignature vararg_rank = i end end - self.mparameters = mparameters - self.return_mtype = return_mtype self.vararg_rank = vararg_rank end # The rank of the ellipsis (`...`) for vararg (starting from 0). # value is -1 if there is no vararg. # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1 - var vararg_rank: Int + var vararg_rank: Int is noinit # The number or parameters fun arity: Int do return mparameters.length @@ -1562,12 +1564,6 @@ 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 @@ -1624,11 +1620,8 @@ abstract class MProperty # The visibility of the property var visibility: MVisibility - init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility) + init do - self.intro_mclassdef = intro_mclassdef - self.name = name - self.visibility = visibility intro_mclassdef.intro_mproperties.add(self) var model = intro_mclassdef.mmodule.model model.mproperties_by_name.add_one(name, self) @@ -1638,13 +1631,13 @@ abstract class MProperty # All definitions of the property. # The first is the introduction, # The other are redefinitions (in refinements and in subclasses) - var mpropdefs: Array[MPROPDEF] = new Array[MPROPDEF] + var mpropdefs = new Array[MPROPDEF] - # The definition that introduced the property - # Warning: the introduction is the first `MPropDef` object - # associated to self. If self is just created without having any - # associated definition, this method will abort - fun intro: MPROPDEF do return mpropdefs.first + # The definition that introduces the property. + # + # Warning: such a definition may not exist in the early life of the object. + # In this case, the method will abort. + var intro: MPROPDEF is noinit redef fun model do return intro.model @@ -1686,7 +1679,7 @@ abstract class MProperty return select_most_specific(mmodule, candidates) end - private var lookup_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]] + private var lookup_definitions_cache = new HashMap2[MModule, MType, Array[MPROPDEF]] # Return the most specific property definitions inherited by a type. # The selection knows that refinement is stronger than specialization; @@ -1761,7 +1754,7 @@ abstract class MProperty # If you want to know the next properties in the linearization, # look at `MPropDef::lookup_next_definition`. # - # FIXME: the linearisation is still unspecified + # FIXME: the linearization is still unspecified # # REQUIRE: `not mtype.need_anchor` # REQUIRE: `mtype.has_mproperty(mmodule, self)` @@ -1771,8 +1764,8 @@ abstract class MProperty return lookup_all_definitions(mmodule, mtype).first end - # Return all definitions in a linearisation order - # Most speficic first, most general last + # Return all definitions in a linearization order + # Most specific first, most general last fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF] do assert not mtype.need_anchor @@ -1804,7 +1797,7 @@ abstract class MProperty return candidates end - private var lookup_all_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]] + private var lookup_all_definitions_cache = new HashMap2[MModule, MType, Array[MPROPDEF]] end # A global method @@ -1813,25 +1806,20 @@ class MMethod redef type MPROPDEF: MMethodDef - init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility) - do - super - end - # 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 writable = false + var is_root_init: Bool = false is writable - # The the property a 'new' contructor? - var is_new: Bool writable = false + # Is the property a 'new' constructor? + var is_new: Bool = false is writable # Is the property a legal constructor for a given class? # As usual, visibility is not considered. @@ -1848,10 +1836,6 @@ class MAttribute redef type MPROPDEF: MAttributeDef - init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility) - do - super - end end # A global virtual type @@ -1860,13 +1844,8 @@ class MVirtualTypeProp redef type MPROPDEF: MVirtualTypeDef - init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility) - do - super - end - # The formal type associated to the virtual type property - var mvirtualtype: MVirtualType = new MVirtualType(self) + var mvirtualtype = new MVirtualType(self) end # A definition of a property (local property) @@ -1883,22 +1862,23 @@ abstract class MPropDef # Self class type MPROPDEF: MPropDef - # The origin of the definition - var location: Location - # The class definition where the property definition is var mclassdef: MClassDef # The associated global property var mproperty: MPROPERTY - init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location) + # The origin of the definition + var location: Location + + init do - self.mclassdef = mclassdef - self.mproperty = mproperty - self.location = location mclassdef.mpropdefs.add(self) mproperty.mpropdefs.add(self) + if mproperty.intro_mclassdef == mclassdef then + assert not isset mproperty._intro + mproperty.intro = self + end self.to_s = "{mclassdef}#{mproperty}" end @@ -1909,7 +1889,7 @@ abstract class MPropDef # Internal name combining the module, the class and the property # Example: "mymodule#MyClass#mymethod" - redef var to_s: String + redef var to_s: String is noinit # Is self the definition that introduce the property? fun is_intro: Bool do return mproperty.intro == self @@ -1940,19 +1920,14 @@ class MMethodDef redef type MPROPERTY: MMethod redef type MPROPDEF: MMethodDef - init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location) - do - super - 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 writable = null + var new_msignature: nullable MSignature = null is writable # List of initialisers to call in root-inits # @@ -1962,13 +1937,23 @@ class MMethodDef 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 + + # An optional constant value returned in functions. + # + # Only some specific primitife value are accepted by engines. + # Is used when there is no better implementation available. + # + # Currently used only for the implementation of the `--define` + # command-line option. + # SEE: module `mixin`. + var constant_value: nullable Object = null is writable end # A local definition of an attribute @@ -1978,13 +1963,8 @@ class MAttributeDef redef type MPROPERTY: MAttribute redef type MPROPDEF: MAttributeDef - init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location) - do - super - 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 @@ -1994,16 +1974,11 @@ class MVirtualTypeDef redef type MPROPERTY: MVirtualTypeProp redef type MPROPDEF: MVirtualTypeDef - init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location) - do - super - 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 writable = false + var is_fixed = false is writable end # A kind of class. @@ -2021,11 +1996,8 @@ class MClassKind # Is a constructor required? var need_init: Bool - private init(s: String, need_init: Bool) - do - self.to_s = s - self.need_init = need_init - end + + # TODO: private init because enumeration. # Can a class of kind `self` specializes a class of kine `other`? fun can_specialize(other: MClassKind): Bool @@ -2035,7 +2007,7 @@ class MClassKind # no other case for interfaces return false else if self == extern_kind then - # only compatible with themselve + # only compatible with themselves return self == other else if other == enum_kind or other == extern_kind then # abstract_kind and concrete_kind are incompatible @@ -2046,8 +2018,13 @@ class MClassKind end end +# The class kind `abstract` fun abstract_kind: MClassKind do return once new MClassKind("abstract class", true) +# The class kind `concrete` fun concrete_kind: MClassKind do return once new MClassKind("class", true) +# The class kind `interface` fun interface_kind: MClassKind do return once new MClassKind("interface", false) +# The class kind `enum` fun enum_kind: MClassKind do return once new MClassKind("enum", false) +# The class kind `extern` fun extern_kind: MClassKind do return once new MClassKind("extern class", false)