1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Object model of the Nit language
19 # This module define the entities of the Nit meta-model like modules,
20 # classes, types and properties
22 # It also provide an API to build and query models.
24 # All model classes starts with the M letter (`MModule`, `MClass`, etc.)
28 # TODO: liearization, extern stuff
29 # FIXME: better handling of the types
36 private import more_collections
40 var mclasses
: Array[MClass] = new Array[MClass]
42 # All known properties
43 var mproperties
: Array[MProperty] = new Array[MProperty]
45 # Hierarchy of class definition.
47 # Each classdef is associated with its super-classdefs in regard to
48 # its module of definition.
49 var mclassdef_hierarchy
: POSet[MClassDef] = new POSet[MClassDef]
51 # Class-type hierarchy restricted to the introduction.
53 # The idea is that what is true on introduction is always true whatever
54 # the module considered.
55 # Therefore, this hierarchy is used for a fast positive subtype check.
57 # This poset will evolve in a monotonous way:
58 # * Two non connected nodes will remain unconnected
59 # * New nodes can appear with new edges
60 private var intro_mtype_specialization_hierarchy
: POSet[MClassType] = new POSet[MClassType]
62 # Global overlapped class-type hierarchy.
63 # The hierarchy when all modules are combined.
64 # Therefore, this hierarchy is used for a fast negative subtype check.
66 # This poset will evolve in an anarchic way. Loops can even be created.
68 # FIXME decide what to do on loops
69 private var full_mtype_specialization_hierarchy
: POSet[MClassType] = new POSet[MClassType]
71 # Collections of classes grouped by their short name
72 private var mclasses_by_name
: MultiHashMap[String, MClass] = new MultiHashMap[String, MClass]
74 # Return all class named `name`.
76 # If such a class does not exist, null is returned
77 # (instead of an empty array)
79 # Visibility or modules are not considered
80 fun get_mclasses_by_name
(name
: String): nullable Array[MClass]
82 if mclasses_by_name
.has_key
(name
) then
83 return mclasses_by_name
[name
]
89 # Collections of properties grouped by their short name
90 private var mproperties_by_name
: MultiHashMap[String, MProperty] = new MultiHashMap[String, MProperty]
92 # Return all properties named `name`.
94 # If such a property does not exist, null is returned
95 # (instead of an empty array)
97 # Visibility or modules are not considered
98 fun get_mproperties_by_name
(name
: String): nullable Array[MProperty]
100 if not mproperties_by_name
.has_key
(name
) then
103 return mproperties_by_name
[name
]
108 var null_type
: MNullType = new MNullType(self)
112 # All the classes introduced in the module
113 var intro_mclasses
: Array[MClass] = new Array[MClass]
115 # All the class definitions of the module
116 # (introduction and refinement)
117 var mclassdefs
: Array[MClassDef] = new Array[MClassDef]
119 # Does the current module has a given class `mclass`?
120 # Return true if the mmodule introduces, refines or imports a class.
121 # Visibility is not considered.
122 fun has_mclass
(mclass
: MClass): Bool
124 return self.in_importation
<= mclass
.intro_mmodule
127 # Full hierarchy of introduced ans imported classes.
129 # Create a new hierarchy got by flattening the classes for the module
130 # and its imported modules.
131 # Visibility is not considered.
133 # Note: this function is expensive and is usually used for the main
134 # module of a program only. Do not use it to do you own subtype
136 fun flatten_mclass_hierarchy
: POSet[MClass]
138 var res
= self.flatten_mclass_hierarchy_cache
139 if res
!= null then return res
140 res
= new POSet[MClass]
141 for m
in self.in_importation
.greaters
do
142 for cd
in m
.mclassdefs
do
145 for s
in cd
.supertypes
do
146 res
.add_edge
(c
, s
.mclass
)
150 self.flatten_mclass_hierarchy_cache
= res
154 # Sort a given array of classes using the linerarization order of the module
155 # The most general is first, the most specific is last
156 fun linearize_mclasses
(mclasses
: Array[MClass])
158 self.flatten_mclass_hierarchy
.sort
(mclasses
)
161 # Sort a given array of class definitions using the linerarization order of the module
162 # the refinement link is stronger than the specialisation link
163 # The most general is first, the most specific is last
164 fun linearize_mclassdefs
(mclassdefs
: Array[MClassDef])
166 var sorter
= new MClassDefSorter(self)
167 sorter
.sort
(mclassdefs
)
170 # Sort a given array of property definitions using the linerarization order of the module
171 # the refinement link is stronger than the specialisation link
172 # The most general is first, the most specific is last
173 fun linearize_mpropdefs
(mpropdefs
: Array[MPropDef])
175 var sorter
= new MPropDefSorter(self)
176 sorter
.sort
(mpropdefs
)
179 private var flatten_mclass_hierarchy_cache
: nullable POSet[MClass] = null
181 # The primitive type `Object`, the root of the class hierarchy
182 fun object_type
: MClassType
184 var res
= self.object_type_cache
185 if res
!= null then return res
186 res
= self.get_primitive_class
("Object").mclass_type
187 self.object_type_cache
= res
191 private var object_type_cache
: nullable MClassType
193 # The primitive type `Bool`
194 fun bool_type
: MClassType
196 var res
= self.bool_type_cache
197 if res
!= null then return res
198 res
= self.get_primitive_class
("Bool").mclass_type
199 self.bool_type_cache
= res
203 private var bool_type_cache
: nullable MClassType
205 # The primitive type `Sys`, the main type of the program, if any
206 fun sys_type
: nullable MClassType
208 var clas
= self.model
.get_mclasses_by_name
("Sys")
209 if clas
== null then return null
210 return get_primitive_class
("Sys").mclass_type
213 # Force to get the primitive class named `name` or abort
214 fun get_primitive_class
(name
: String): MClass
216 var cla
= self.model
.get_mclasses_by_name
(name
)
218 if name
== "Bool" then
219 var c
= new MClass(self, name
, 0, enum_kind
, public_visibility
)
220 var cladef
= new MClassDef(self, c
.mclass_type
, new Location(null, 0,0,0,0), new Array[String])
223 print
("Fatal Error: no primitive class {name}")
226 if cla
.length
!= 1 then
227 var msg
= "Fatal Error: more than one primitive class {name}:"
228 for c
in cla
do msg
+= " {c.full_name}"
235 # Try to get the primitive method named `name` on the type `recv`
236 fun try_get_primitive_method
(name
: String, recv
: MClass): nullable MMethod
238 var props
= self.model
.get_mproperties_by_name
(name
)
239 if props
== null then return null
240 var res
: nullable MMethod = null
241 for mprop
in props
do
242 assert mprop
isa MMethod
243 var intro
= mprop
.intro_mclassdef
244 for mclassdef
in recv
.mclassdefs
do
245 if not self.in_importation
.greaters
.has
(mclassdef
.mmodule
) then continue
246 if not mclassdef
.in_hierarchy
.greaters
.has
(intro
) then continue
249 else if res
!= mprop
then
250 print
("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
259 private class MClassDefSorter
260 super AbstractSorter[MClassDef]
262 redef fun compare
(a
, b
)
266 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
267 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
271 private class MPropDefSorter
272 super AbstractSorter[MPropDef]
274 redef fun compare
(pa
, pb
)
280 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
281 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
287 # `MClass` are global to the model; it means that a `MClass` is not bound to a
288 # specific `MModule`.
290 # This characteristic helps the reasoning about classes in a program since a
291 # single `MClass` object always denote the same class.
292 # However, because a `MClass` is global, it does not really have properties nor
293 # belong to a hierarchy since the property and the
294 # hierarchy of a class depends of a module.
298 # The module that introduce the class
299 # While classes are not bound to a specific module,
300 # the introducing module is used for naming an visibility
301 var intro_mmodule
: MModule
303 # The short name of the class
304 # In Nit, the name of a class cannot evolve in refinements
307 # The canonical name of the class
308 # Example: `"owner::module::MyClass"`
309 fun full_name
: String
311 return "{self.intro_mmodule.full_name}::{name}"
314 # The number of generic formal parameters
315 # 0 if the class is not generic
318 # The kind of the class (interface, abstract class, etc.)
319 # In Nit, the kind of a class cannot evolve in refinements
322 # The visibility of the class
323 # In Nit, the visibility of a class cannot evolve in refinements
324 var visibility
: MVisibility
326 init(intro_mmodule
: MModule, name
: String, arity
: Int, kind
: MClassKind, visibility
: MVisibility)
328 self.intro_mmodule
= intro_mmodule
332 self.visibility
= visibility
333 intro_mmodule
.intro_mclasses
.add
(self)
334 var model
= intro_mmodule
.model
335 model
.mclasses_by_name
.add_one
(name
, self)
336 model
.mclasses
.add
(self)
338 # Create the formal parameter types
340 var mparametertypes
= new Array[MParameterType]
341 for i
in [0..arity
[ do
342 var mparametertype
= new MParameterType(self, i
)
343 mparametertypes
.add
(mparametertype
)
345 var mclass_type
= new MGenericType(self, mparametertypes
)
346 self.mclass_type
= mclass_type
347 self.get_mtype_cache
.add
(mclass_type
)
349 self.mclass_type
= new MClassType(self)
353 # All class definitions (introduction and refinements)
354 var mclassdefs
: Array[MClassDef] = new Array[MClassDef]
357 redef fun to_s
do return self.name
359 # The definition that introduced the class
360 # Warning: the introduction is the first `MClassDef` object associated
361 # to self. If self is just created without having any associated
362 # definition, this method will abort
365 assert has_a_first_definition
: not mclassdefs
.is_empty
366 return mclassdefs
.first
369 # Return the class `self` in the class hierarchy of the module `mmodule`.
371 # SEE: `MModule::flatten_mclass_hierarchy`
372 # REQUIRE: `mmodule.has_mclass(self)`
373 fun in_hierarchy
(mmodule
: MModule): POSetElement[MClass]
375 return mmodule
.flatten_mclass_hierarchy
[self]
378 # The principal static type of the class.
380 # For non-generic class, mclass_type is the only `MClassType` based
383 # For a generic class, the arguments are the formal parameters.
384 # i.e.: for the class Array[E:Object], the `mclass_type` is Array[E].
385 # If you want Array[Object] the see `MClassDef::bound_mtype`
387 # For generic classes, the mclass_type is also the way to get a formal
388 # generic parameter type.
390 # To get other types based on a generic class, see `get_mtype`.
392 # ENSURE: `mclass_type.mclass == self`
393 var mclass_type
: MClassType
395 # Return a generic type based on the class
396 # Is the class is not generic, then the result is `mclass_type`
398 # REQUIRE: `mtype_arguments.length == self.arity`
399 fun get_mtype
(mtype_arguments
: Array[MType]): MClassType
401 assert mtype_arguments
.length
== self.arity
402 if self.arity
== 0 then return self.mclass_type
403 for t
in self.get_mtype_cache
do
404 if t
.arguments
== mtype_arguments
then
408 var res
= new MGenericType(self, mtype_arguments
)
409 self.get_mtype_cache
.add res
413 private var get_mtype_cache
: Array[MGenericType] = new Array[MGenericType]
417 # A definition (an introduction or a refinement) of a class in a module
419 # A `MClassDef` is associated with an explicit (or almost) definition of a
420 # class. Unlike `MClass`, a `MClassDef` is a local definition that belong to
425 # The module where the definition is
428 # The associated `MClass`
431 # The bounded type associated to the mclassdef
433 # For a non-generic class, `bound_mtype` and `mclass.mclass_type`
437 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
438 # If you want Array[E], then see `mclass.mclass_type`
440 # ENSURE: `bound_mtype.mclass == self.mclass`
441 var bound_mtype
: MClassType
443 # Name of each formal generic parameter (in order of declaration)
444 var parameter_names
: Array[String]
446 # The origin of the definition
447 var location
: Location
449 # Internal name combining the module and the class
450 # Example: "mymodule#MyClass"
451 redef var to_s
: String
453 init(mmodule
: MModule, bound_mtype
: MClassType, location
: Location, parameter_names
: Array[String])
455 assert bound_mtype
.mclass
.arity
== parameter_names
.length
456 self.bound_mtype
= bound_mtype
457 self.mmodule
= mmodule
458 self.mclass
= bound_mtype
.mclass
459 self.location
= location
460 mmodule
.mclassdefs
.add
(self)
461 mclass
.mclassdefs
.add
(self)
462 self.parameter_names
= parameter_names
463 self.to_s
= "{mmodule}#{mclass}"
466 # All declared super-types
467 # FIXME: quite ugly but not better idea yet
468 var supertypes
: Array[MClassType] = new Array[MClassType]
470 # Register some super-types for the class (ie "super SomeType")
472 # The hierarchy must not already be set
473 # REQUIRE: `self.in_hierarchy == null`
474 fun set_supertypes
(supertypes
: Array[MClassType])
476 assert unique_invocation
: self.in_hierarchy
== null
477 var mmodule
= self.mmodule
478 var model
= mmodule
.model
479 var mtype
= self.bound_mtype
481 for supertype
in supertypes
do
482 self.supertypes
.add
(supertype
)
484 # Register in full_type_specialization_hierarchy
485 model
.full_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
486 # Register in intro_type_specialization_hierarchy
487 if mclass
.intro_mmodule
== mmodule
and supertype
.mclass
.intro_mmodule
== mmodule
then
488 model
.intro_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
494 # Collect the super-types (set by set_supertypes) to build the hierarchy
496 # This function can only invoked once by class
497 # REQUIRE: `self.in_hierarchy == null`
498 # ENSURE: `self.in_hierarchy != null`
501 assert unique_invocation
: self.in_hierarchy
== null
502 var model
= mmodule
.model
503 var res
= model
.mclassdef_hierarchy
.add_node
(self)
504 self.in_hierarchy
= res
505 var mtype
= self.bound_mtype
507 # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
508 # The simpliest way is to attach it to collect_mclassdefs
509 for mclassdef
in mtype
.collect_mclassdefs
(mmodule
) do
510 res
.poset
.add_edge
(self, mclassdef
)
514 # The view of the class definition in `mclassdef_hierarchy`
515 var in_hierarchy
: nullable POSetElement[MClassDef] = null
517 # Is the definition the one that introduced `mclass`?
518 fun is_intro
: Bool do return mclass
.intro
== self
520 # All properties introduced by the classdef
521 var intro_mproperties
: Array[MProperty] = new Array[MProperty]
523 # All property definitions in the class (introductions and redefinitions)
524 var mpropdefs
: Array[MPropDef] = new Array[MPropDef]
527 # A global static type
529 # MType are global to the model; it means that a `MType` is not bound to a
530 # specific `MModule`.
531 # This characteristic helps the reasoning about static types in a program
532 # since a single `MType` object always denote the same type.
534 # However, because a `MType` is global, it does not really have properties
535 # nor have subtypes to a hierarchy since the property and the class hierarchy
536 # depends of a module.
537 # Moreover, virtual types an formal generic parameter types also depends on
538 # a receiver to have sense.
540 # Therefore, most method of the types require a module and an anchor.
541 # The module is used to know what are the classes and the specialization
543 # The anchor is used to know what is the bound of the virtual types and formal
544 # generic parameter types.
546 # MType are not directly usable to get properties. See the `anchor_to` method
547 # and the `MClassType` class.
549 # FIXME: the order of the parameters is not the best. We mus pick on from:
550 # * foo(mmodule, anchor, othertype)
551 # * foo(othertype, anchor, mmodule)
552 # * foo(anchor, mmodule, othertype)
553 # * foo(othertype, mmodule, anchor)
557 # The model of the type
558 fun model
: Model is abstract
560 # Return true if `self` is an subtype of `sup`.
561 # The typing is done using the standard typing policy of Nit.
563 # REQUIRE: `anchor == null implies not self.need_anchor and not sup.need_anchor`
564 # REQUIRE: `anchor != null implies self.can_resolve_for(anchor, null, mmodule) and sup.can_resolve_for(anchor, null, mmodule)`
565 fun is_subtype
(mmodule
: MModule, anchor
: nullable MClassType, sup
: MType): Bool
568 if sub
== sup
then return true
569 if anchor
== null then
570 assert not sub
.need_anchor
571 assert not sup
.need_anchor
573 assert sub
.can_resolve_for
(anchor
, null, mmodule
)
574 assert sup
.can_resolve_for
(anchor
, null, mmodule
)
577 # First, resolve the formal types to a common version in the receiver
578 # The trick here is that fixed formal type will be associed to the bound
579 # And unfixed formal types will be associed to a canonical formal type.
580 if sub
isa MParameterType or sub
isa MVirtualType then
581 assert anchor
!= null
582 sub
= sub
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, false)
584 if sup
isa MParameterType or sup
isa MVirtualType then
585 assert anchor
!= null
586 sup
= sup
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, false)
589 # Does `sup` accept null or not?
590 # Discard the nullable marker if it exists
591 var sup_accept_null
= false
592 if sup
isa MNullableType then
593 sup_accept_null
= true
595 else if sup
isa MNullType then
596 sup_accept_null
= true
599 # Can `sub` provide null or not?
600 # Thus we can match with `sup_accept_null`
601 # Also discard the nullable marker if it exists
602 if sub
isa MNullableType then
603 if not sup_accept_null
then return false
605 else if sub
isa MNullType then
606 return sup_accept_null
608 # Now the case of direct null and nullable is over.
610 # A unfixed formal type can only accept itself
611 if sup
isa MParameterType or sup
isa MVirtualType then
615 # If `sub` is a formal type, then it is accepted if its bound is accepted
616 if sub
isa MParameterType or sub
isa MVirtualType then
617 assert anchor
!= null
618 sub
= sub
.anchor_to
(mmodule
, anchor
)
620 # Manage the second layer of null/nullable
621 if sub
isa MNullableType then
622 if not sup_accept_null
then return false
624 else if sub
isa MNullType then
625 return sup_accept_null
629 assert sub
isa MClassType # It is the only remaining type
631 if sup
isa MNullType then
632 # `sup` accepts only null
636 assert sup
isa MClassType # It is the only remaining type
638 # Now both are MClassType, we need to dig
640 if sub
== sup
then return true
642 if anchor
== null then anchor
= sub
# UGLY: any anchor will work
643 var resolved_sub
= sub
.anchor_to
(mmodule
, anchor
)
644 var res
= resolved_sub
.collect_mclasses
(mmodule
).has
(sup
.mclass
)
645 if res
== false then return false
646 if not sup
isa MGenericType then return true
647 var sub2
= sub
.supertype_to
(mmodule
, anchor
, sup
.mclass
)
648 assert sub2
.mclass
== sup
.mclass
649 for i
in [0..sup
.mclass
.arity
[ do
650 var sub_arg
= sub2
.arguments
[i
]
651 var sup_arg
= sup
.arguments
[i
]
652 res
= sub_arg
.is_subtype
(mmodule
, anchor
, sup_arg
)
653 if res
== false then return false
658 # The base class type on which self is based
660 # This base type is used to get property (an internally to perform
661 # unsafe type comparison).
663 # Beware: some types (like null) are not based on a class thus this
666 # Basically, this function transform the virtual types and parameter
667 # types to their bounds.
671 # class B super A end
673 # class Y super X end
681 # Map[T,U] anchor_to H #-> Map[B,Y]
683 # Explanation of the example:
684 # In H, T is set to B, because "H super G[B]", and U is bound to Y,
685 # because "redef type U: Y". Therefore, Map[T, U] is bound to
688 # ENSURE: `not self.need_anchor implies result == self`
689 # ENSURE: `not result.need_anchor`
690 fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MType
692 if not need_anchor
then return self
693 assert not anchor
.need_anchor
694 # Just resolve to the anchor and clear all the virtual types
695 var res
= self.resolve_for
(anchor
, null, mmodule
, true)
696 assert not res
.need_anchor
700 # Does `self` contain a virtual type or a formal generic parameter type?
701 # In order to remove those types, you usually want to use `anchor_to`.
702 fun need_anchor
: Bool do return true
704 # Return the supertype when adapted to a class.
706 # In Nit, for each super-class of a type, there is a equivalent super-type.
710 # class H[V] super G[V, Bool] end
711 # H[Int] supertype_to G #-> G[Int, Bool]
713 # REQUIRE: `super_mclass` is a super-class of `self`
714 # REQUIRE: `self.need_anchor implies anchor != null and self.can_resolve_for(anchor, null, mmodule)`
715 # ENSURE: `result.mclass = super_mclass`
716 fun supertype_to
(mmodule
: MModule, anchor
: nullable MClassType, super_mclass
: MClass): MClassType
718 if super_mclass
.arity
== 0 then return super_mclass
.mclass_type
719 if self isa MClassType and self.mclass
== super_mclass
then return self
721 if self.need_anchor
then
722 assert anchor
!= null
723 resolved_self
= self.anchor_to
(mmodule
, anchor
)
727 var supertypes
= resolved_self
.collect_mtypes
(mmodule
)
728 for supertype
in supertypes
do
729 if supertype
.mclass
== super_mclass
then
730 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
731 return supertype
.resolve_for
(self, anchor
, mmodule
, false)
737 # Replace formals generic types in self with resolved values in `mtype`
738 # If `cleanup_virtual` is true, then virtual types are also replaced
741 # This function returns self if `need_anchor` is false.
746 # class H[F] super G[F] end
749 # * Array[E].resolve_for(H[Int]) #-> Array[Int]
750 # * Array[E].resolve_for(G[Z], X[Int]) #-> Array[Z]
752 # Explanation of the example:
753 # * Array[E].need_anchor is true because there is a formal generic parameter type E
754 # * E makes sense for H[Int] because E is a formal parameter of G and H specialize G
755 # * Since "H[F] super G[F]", E is in fact F for H
756 # * More specifically, in H[Int], E is Int
757 # * So, in H[Int], Array[E] is Array[Int]
759 # This function is mainly used to inherit a signature.
760 # Because, unlike `anchor_to`, we do not want a full resolution of
761 # a type but only an adapted version of it.
766 # fun foo(e:E):E is abstract
768 # class B super A[Int] end
770 # The signature on foo is (e: E): E
771 # If we resolve the signature for B, we get (e:Int):Int
776 # fun foo(e:E) is abstract
780 # fun bar do a.foo(x) # <- x is here
783 # The first question is: is foo available on `a`?
785 # The static type of a is `A[Array[F]]`, that is an open type.
786 # in order to find a method `foo`, whe must look at a resolved type.
788 # A[Array[F]].anchor_to(B[nullable Object]) #-> A[Array[nullable Object]]
790 # the method `foo` exists in `A[Array[nullable Object]]`, therefore `foo` exists for `a`.
792 # The next question is: what is the accepted types for `x`?
794 # the signature of `foo` is `foo(e:E)`, thus we must resolve the type E
796 # E.resolve_for(A[Array[F]],B[nullable Object]) #-> Array[F]
798 # The resolution can be done because `E` make sense for the class A (see `can_resolve_for`)
800 # TODO: Explain the cleanup_virtual
802 # FIXME: the parameter `cleanup_virtual` is just a bad idea, but having
803 # two function instead of one seems also to be a bad idea.
805 # REQUIRE: `can_resolve_for(mtype, anchor, mmodule)`
806 # ENSURE: `not self.need_anchor implies result == self`
807 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MType is abstract
809 # Can the type be resolved?
811 # In order to resolve open types, the formal types must make sence.
820 # * E.can_resolve_for(A[Int]) #-> true, E make sense in A
821 # * E.can_resolve_for(B[Int]) #-> false, E does not make sense in B
822 # * B[E].can_resolve_for(A[F], B[Object]) #-> true,
823 # B[E] is a red hearing only the E is important,
826 # REQUIRE: `anchor != null implies not anchor.need_anchor`
827 # REQUIRE: `mtype.need_anchor implies anchor != null and mtype.can_resolve_for(anchor, null, mmodule)`
828 # ENSURE: `not self.need_anchor implies result == true`
829 fun can_resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule): Bool is abstract
831 # Return the nullable version of the type
832 # If the type is already nullable then self is returned
833 fun as_nullable
: MType
835 var res
= self.as_nullable_cache
836 if res
!= null then return res
837 res
= new MNullableType(self)
838 self.as_nullable_cache
= res
842 private var as_nullable_cache
: nullable MType = null
845 # The deph of the type seen as a tree.
852 # Formal types have a depth of 1.
858 # The length of the type seen as a tree.
865 # Formal types have a length of 1.
871 # Compute all the classdefs inherited/imported.
872 # The returned set contains:
873 # * the class definitions from `mmodule` and its imported modules
874 # * the class definitions of this type and its super-types
876 # This function is used mainly internally.
878 # REQUIRE: `not self.need_anchor`
879 fun collect_mclassdefs
(mmodule
: MModule): Set[MClassDef] is abstract
881 # Compute all the super-classes.
882 # This function is used mainly internally.
884 # REQUIRE: `not self.need_anchor`
885 fun collect_mclasses
(mmodule
: MModule): Set[MClass] is abstract
887 # Compute all the declared super-types.
888 # Super-types are returned as declared in the classdefs (verbatim).
889 # This function is used mainly internally.
891 # REQUIRE: `not self.need_anchor`
892 fun collect_mtypes
(mmodule
: MModule): Set[MClassType] is abstract
894 # Is the property in self for a given module
895 # This method does not filter visibility or whatever
897 # REQUIRE: `not self.need_anchor`
898 fun has_mproperty
(mmodule
: MModule, mproperty
: MProperty): Bool
900 assert not self.need_anchor
901 return self.collect_mclassdefs
(mmodule
).has
(mproperty
.intro_mclassdef
)
905 # A type based on a class.
907 # `MClassType` have properties (see `has_mproperty`).
911 # The associated class
914 redef fun model
do return self.mclass
.intro_mmodule
.model
916 private init(mclass
: MClass)
921 # The formal arguments of the type
922 # ENSURE: `result.length == self.mclass.arity`
923 var arguments
: Array[MType] = new Array[MType]
925 redef fun to_s
do return mclass
.to_s
927 redef fun need_anchor
do return false
929 redef fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MClassType
931 return super.as(MClassType)
934 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MClassType do return self
936 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
938 redef fun collect_mclassdefs
(mmodule
)
940 assert not self.need_anchor
941 var cache
= self.collect_mclassdefs_cache
942 if not cache
.has_key
(mmodule
) then
943 self.collect_things
(mmodule
)
945 return cache
[mmodule
]
948 redef fun collect_mclasses
(mmodule
)
950 assert not self.need_anchor
951 var cache
= self.collect_mclasses_cache
952 if not cache
.has_key
(mmodule
) then
953 self.collect_things
(mmodule
)
955 return cache
[mmodule
]
958 redef fun collect_mtypes
(mmodule
)
960 assert not self.need_anchor
961 var cache
= self.collect_mtypes_cache
962 if not cache
.has_key
(mmodule
) then
963 self.collect_things
(mmodule
)
965 return cache
[mmodule
]
968 # common implementation for `collect_mclassdefs`, `collect_mclasses`, and `collect_mtypes`.
969 private fun collect_things
(mmodule
: MModule)
971 var res
= new HashSet[MClassDef]
972 var seen
= new HashSet[MClass]
973 var types
= new HashSet[MClassType]
974 seen
.add
(self.mclass
)
975 var todo
= [self.mclass
]
976 while not todo
.is_empty
do
977 var mclass
= todo
.pop
978 #print "process {mclass}"
979 for mclassdef
in mclass
.mclassdefs
do
980 if not mmodule
.in_importation
<= mclassdef
.mmodule
then continue
981 #print " process {mclassdef}"
983 for supertype
in mclassdef
.supertypes
do
985 var superclass
= supertype
.mclass
986 if seen
.has
(superclass
) then continue
987 #print " add {superclass}"
993 collect_mclassdefs_cache
[mmodule
] = res
994 collect_mclasses_cache
[mmodule
] = seen
995 collect_mtypes_cache
[mmodule
] = types
998 private var collect_mclassdefs_cache
: HashMap[MModule, Set[MClassDef]] = new HashMap[MModule, Set[MClassDef]]
999 private var collect_mclasses_cache
: HashMap[MModule, Set[MClass]] = new HashMap[MModule, Set[MClass]]
1000 private var collect_mtypes_cache
: HashMap[MModule, Set[MClassType]] = new HashMap[MModule, Set[MClassType]]
1004 # A type based on a generic class.
1005 # A generic type a just a class with additional formal generic arguments.
1009 private init(mclass
: MClass, arguments
: Array[MType])
1012 assert self.mclass
.arity
== arguments
.length
1013 self.arguments
= arguments
1015 self.need_anchor
= false
1016 for t
in arguments
do
1017 if t
.need_anchor
then
1018 self.need_anchor
= true
1023 self.to_s
= "{mclass}[{arguments.join(", ")}]"
1026 # Recursively print the type of the arguments within brackets.
1027 # Example: `"Map[String, List[Int]]"`
1028 redef var to_s
: String
1030 redef var need_anchor
: Bool
1032 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1034 if not need_anchor
then return self
1035 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1036 var types
= new Array[MType]
1037 for t
in arguments
do
1038 types
.add
(t
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1040 return mclass
.get_mtype
(types
)
1043 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1045 if not need_anchor
then return true
1046 for t
in arguments
do
1047 if not t
.can_resolve_for
(mtype
, anchor
, mmodule
) then return false
1056 for a
in self.arguments
do
1058 if d
> dmax
then dmax
= d
1066 for a
in self.arguments
do
1073 # A virtual formal type.
1077 # The property associated with the type.
1078 # Its the definitions of this property that determine the bound or the virtual type.
1079 var mproperty
: MProperty
1081 redef fun model
do return self.mproperty
.intro_mclassdef
.mmodule
.model
1083 # Lookup the bound for a given resolved_receiver
1084 # The result may be a other virtual type (or a parameter type)
1086 # The result is returned exactly as declared in the "type" property (verbatim).
1088 # In case of conflict, the method aborts.
1089 fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1091 assert not resolved_receiver
.need_anchor
1092 var props
= self.mproperty
.lookup_definitions
(mmodule
, resolved_receiver
)
1093 if props
.is_empty
then
1095 else if props
.length
== 1 then
1096 return props
.first
.as(MVirtualTypeDef).bound
.as(not null)
1098 var types
= new ArraySet[MType]
1100 types
.add
(p
.as(MVirtualTypeDef).bound
.as(not null))
1102 if types
.length
== 1 then
1108 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1110 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1111 # self is a virtual type declared (or inherited) in mtype
1112 # The point of the function it to get the bound of the virtual type that make sense for mtype
1113 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1114 #print "{class_name}: {self}/{mtype}/{anchor}?"
1115 var resolved_reciever
1116 if mtype
.need_anchor
then
1117 assert anchor
!= null
1118 resolved_reciever
= mtype
.resolve_for
(anchor
, null, mmodule
, true)
1120 resolved_reciever
= mtype
1122 # Now, we can get the bound
1123 var verbatim_bound
= lookup_bound
(mmodule
, resolved_reciever
)
1124 # The bound is exactly as declared in the "type" property, so we must resolve it again
1125 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1126 #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}"
1128 # What to return here? There is a bunch a special cases:
1129 # If 'cleanup_virtual' we must return the resolved type, since we cannot return self
1130 if cleanup_virtual
then return res
1131 # 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
1132 if resolved_reciever
isa MNullableType then resolved_reciever
= resolved_reciever
.mtype
1133 if resolved_reciever
.as(MClassType).mclass
.kind
== enum_kind
then return res
1134 # 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.
1135 if res
isa MVirtualType then return res
1136 # 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
1137 if res
isa MClassType and res
.mclass
.kind
== enum_kind
then return res
1138 # TODO: Add 'fixed' virtual type in the specification.
1139 # TODO: What if bound to a MParameterType?
1140 # Note that Nullable types can always be redefined by the non nullable version, so there is no specific case on it.
1142 # If anything apply, then `self' cannot be resolved, so return self
1146 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1148 if mtype
.need_anchor
then
1149 assert anchor
!= null
1150 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1152 return mtype
.has_mproperty
(mmodule
, mproperty
)
1155 redef fun to_s
do return self.mproperty
.to_s
1157 init(mproperty
: MProperty)
1159 self.mproperty
= mproperty
1163 # The type associated the a formal parameter generic type of a class
1165 # Each parameter type is associated to a specific class.
1166 # It's mean that all refinements of a same class "share" the parameter type,
1167 # but that a generic subclass has its on parameter types.
1169 # However, in the sense of the meta-model, the a parameter type of a class is
1170 # a valid types in a subclass. The "in the sense of the meta-model" is
1171 # important because, in the Nit language, the programmer cannot refers
1172 # directly to the parameter types of the super-classes.
1176 # fun e: E is abstract
1181 # In the class definition B[F], `F` is a valid type but `E` is not.
1182 # However, `self.e` is a valid method call, and the signature of `e` is
1185 # Note that parameter types are shared among class refinements.
1186 # Therefore parameter only have an internal name (see `to_s` for details).
1187 # TODO: Add a `name_for` to get better messages.
1188 class MParameterType
1191 # The generic class where the parameter belong
1194 redef fun model
do return self.mclass
.intro_mmodule
.model
1196 # The position of the parameter (0 for the first parameter)
1197 # FIXME: is `position` a better name?
1200 # Internal name of the parameter type
1201 # Names of parameter types changes in each class definition
1202 # Therefore, this method return an internal name.
1203 # Example: return "G#1" for the second parameter of the class G
1204 # FIXME: add a way to get the real name in a classdef
1205 redef fun to_s
do return "{mclass}#{rank}"
1207 # Resolve the bound for a given resolved_receiver
1208 # The result may be a other virtual type (or a parameter type)
1209 fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1211 assert not resolved_receiver
.need_anchor
1212 var goalclass
= self.mclass
1213 var supertypes
= resolved_receiver
.collect_mtypes
(mmodule
)
1214 for t
in supertypes
do
1215 if t
.mclass
== goalclass
then
1216 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
1217 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
1218 var res
= t
.arguments
[self.rank
]
1225 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1227 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1228 #print "{class_name}: {self}/{mtype}/{anchor}?"
1230 if mtype
isa MGenericType and mtype
.mclass
== self.mclass
then
1231 return mtype
.arguments
[self.rank
]
1234 # self is a parameter type of mtype (or of a super-class of mtype)
1235 # The point of the function it to get the bound of the virtual type that make sense for mtype
1236 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1237 # FIXME: What happend here is far from clear. Thus this part must be validated and clarified
1238 var resolved_receiver
1239 if mtype
.need_anchor
then
1240 assert anchor
!= null
1241 resolved_receiver
= mtype
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, true)
1243 resolved_receiver
= mtype
1245 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1246 if resolved_receiver
isa MParameterType then
1247 assert resolved_receiver
.mclass
== anchor
.mclass
1248 resolved_receiver
= anchor
.arguments
[resolved_receiver
.rank
]
1249 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1251 assert resolved_receiver
isa MClassType
1253 # Eh! The parameter is in the current class.
1254 # So we return the corresponding argument, no mater what!
1255 if resolved_receiver
.mclass
== self.mclass
then
1256 var res
= resolved_receiver
.arguments
[self.rank
]
1257 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
1261 if resolved_receiver
.need_anchor
then
1262 assert anchor
!= null
1263 resolved_receiver
= resolved_receiver
.resolve_for
(anchor
, null, mmodule
, false)
1265 # Now, we can get the bound
1266 var verbatim_bound
= lookup_bound
(mmodule
, resolved_receiver
)
1267 # The bound is exactly as declared in the "type" property, so we must resolve it again
1268 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1270 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
1275 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1277 if mtype
.need_anchor
then
1278 assert anchor
!= null
1279 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1281 return mtype
.collect_mclassdefs
(mmodule
).has
(mclass
.intro
)
1284 init(mclass
: MClass, rank
: Int)
1286 self.mclass
= mclass
1291 # A type prefixed with "nullable"
1295 # The base type of the nullable type
1298 redef fun model
do return self.mtype
.model
1303 self.to_s
= "nullable {mtype}"
1306 redef var to_s
: String
1308 redef fun need_anchor
do return mtype
.need_anchor
1309 redef fun as_nullable
do return self
1310 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1312 var res
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1313 return res
.as_nullable
1316 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1318 return self.mtype
.can_resolve_for
(mtype
, anchor
, mmodule
)
1321 redef fun depth
do return self.mtype
.depth
1323 redef fun length
do return self.mtype
.length
1325 redef fun collect_mclassdefs
(mmodule
)
1327 assert not self.need_anchor
1328 return self.mtype
.collect_mclassdefs
(mmodule
)
1331 redef fun collect_mclasses
(mmodule
)
1333 assert not self.need_anchor
1334 return self.mtype
.collect_mclasses
(mmodule
)
1337 redef fun collect_mtypes
(mmodule
)
1339 assert not self.need_anchor
1340 return self.mtype
.collect_mtypes
(mmodule
)
1344 # The type of the only value null
1346 # The is only one null type per model, see `MModel::null_type`.
1349 redef var model
: Model
1350 protected init(model
: Model)
1354 redef fun to_s
do return "null"
1355 redef fun as_nullable
do return self
1356 redef fun need_anchor
do return false
1357 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
) do return self
1358 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
1360 redef fun collect_mclassdefs
(mmodule
) do return new HashSet[MClassDef]
1362 redef fun collect_mclasses
(mmodule
) do return new HashSet[MClass]
1364 redef fun collect_mtypes
(mmodule
) do return new HashSet[MClassType]
1367 # A signature of a method
1371 # The each parameter (in order)
1372 var mparameters
: Array[MParameter]
1374 # The return type (null for a procedure)
1375 var return_mtype
: nullable MType
1380 var t
= self.return_mtype
1381 if t
!= null then dmax
= t
.depth
1382 for p
in mparameters
do
1383 var d
= p
.mtype
.depth
1384 if d
> dmax
then dmax
= d
1392 var t
= self.return_mtype
1393 if t
!= null then res
+= t
.length
1394 for p
in mparameters
do
1395 res
+= p
.mtype
.length
1400 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1401 init(mparameters
: Array[MParameter], return_mtype
: nullable MType)
1403 var vararg_rank
= -1
1404 for i
in [0..mparameters
.length
[ do
1405 var parameter
= mparameters
[i
]
1406 if parameter
.is_vararg
then
1407 assert vararg_rank
== -1
1411 self.mparameters
= mparameters
1412 self.return_mtype
= return_mtype
1413 self.vararg_rank
= vararg_rank
1416 # The rank of the ellipsis (`...`) for vararg (starting from 0).
1417 # value is -1 if there is no vararg.
1418 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1419 var vararg_rank
: Int
1421 # The number or parameters
1422 fun arity
: Int do return mparameters
.length
1426 var b
= new FlatBuffer
1427 if not mparameters
.is_empty
then
1429 for i
in [0..mparameters
.length
[ do
1430 var mparameter
= mparameters
[i
]
1431 if i
> 0 then b
.append
(", ")
1432 b
.append
(mparameter
.name
)
1434 b
.append
(mparameter
.mtype
.to_s
)
1435 if mparameter
.is_vararg
then
1441 var ret
= self.return_mtype
1449 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MSignature
1451 var params
= new Array[MParameter]
1452 for p
in self.mparameters
do
1453 params
.add
(p
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1455 var ret
= self.return_mtype
1457 ret
= ret
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1459 var res
= new MSignature(params
, ret
)
1464 # A parameter in a signature
1466 # The name of the parameter
1469 # The static type of the parameter
1472 # Is the parameter a vararg?
1478 return "{name}: {mtype}..."
1480 return "{name}: {mtype}"
1484 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MParameter
1486 if not self.mtype
.need_anchor
then return self
1487 var newtype
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1488 var res
= new MParameter(self.name
, newtype
, self.is_vararg
)
1493 # A service (global property) that generalize method, attribute, etc.
1495 # `MProperty` are global to the model; it means that a `MProperty` is not bound
1496 # to a specific `MModule` nor a specific `MClass`.
1498 # A MProperty gather definitions (see `mpropdefs`) ; one for the introduction
1499 # and the other in subclasses and in refinements.
1501 # A `MProperty` is used to denotes services in polymorphic way (ie. independent
1502 # of any dynamic type).
1503 # For instance, a call site "x.foo" is associated to a `MProperty`.
1504 abstract class MProperty
1507 # The associated MPropDef subclass.
1508 # The two specialization hierarchy are symmetric.
1509 type MPROPDEF: MPropDef
1511 # The classdef that introduce the property
1512 # While a property is not bound to a specific module, or class,
1513 # the introducing mclassdef is used for naming and visibility
1514 var intro_mclassdef
: MClassDef
1516 # The (short) name of the property
1519 # The canonical name of the property
1520 # Example: "owner::my_module::MyClass::my_method"
1521 fun full_name
: String
1523 return "{self.intro_mclassdef.mmodule.full_name}::{self.intro_mclassdef.mclass.name}::{name}"
1526 # The visibility of the property
1527 var visibility
: MVisibility
1529 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1531 self.intro_mclassdef
= intro_mclassdef
1533 self.visibility
= visibility
1534 intro_mclassdef
.intro_mproperties
.add
(self)
1535 var model
= intro_mclassdef
.mmodule
.model
1536 model
.mproperties_by_name
.add_one
(name
, self)
1537 model
.mproperties
.add
(self)
1540 # All definitions of the property.
1541 # The first is the introduction,
1542 # The other are redefinitions (in refinements and in subclasses)
1543 var mpropdefs
: Array[MPROPDEF] = new Array[MPROPDEF]
1545 # The definition that introduced the property
1546 # Warning: the introduction is the first `MPropDef` object
1547 # associated to self. If self is just created without having any
1548 # associated definition, this method will abort
1549 fun intro
: MPROPDEF do return mpropdefs
.first
1552 redef fun to_s
do return name
1554 # Return the most specific property definitions defined or inherited by a type.
1555 # The selection knows that refinement is stronger than specialization;
1556 # however, in case of conflict more than one property are returned.
1557 # If mtype does not know mproperty then an empty array is returned.
1559 # If you want the really most specific property, then look at `lookup_first_definition`
1560 fun lookup_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1562 assert not mtype
.need_anchor
1563 if mtype
isa MNullableType then mtype
= mtype
.mtype
1565 var cache
= self.lookup_definitions_cache
[mmodule
, mtype
]
1566 if cache
!= null then return cache
1568 #print "select prop {mproperty} for {mtype} in {self}"
1569 # First, select all candidates
1570 var candidates
= new Array[MPROPDEF]
1571 for mpropdef
in self.mpropdefs
do
1572 # If the definition is not imported by the module, then skip
1573 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1574 # If the definition is not inherited by the type, then skip
1575 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1577 candidates
.add
(mpropdef
)
1579 # Fast track for only one candidate
1580 if candidates
.length
<= 1 then
1581 self.lookup_definitions_cache
[mmodule
, mtype
] = candidates
1585 # Second, filter the most specific ones
1586 return select_most_specific
(mmodule
, candidates
)
1589 private var lookup_definitions_cache
: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1591 # Return the most specific property definitions inherited by a type.
1592 # The selection knows that refinement is stronger than specialization;
1593 # however, in case of conflict more than one property are returned.
1594 # If mtype does not know mproperty then an empty array is returned.
1596 # If you want the really most specific property, then look at `lookup_next_definition`
1598 # FIXME: Move to `MPropDef`?
1599 fun lookup_super_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1601 assert not mtype
.need_anchor
1602 if mtype
isa MNullableType then mtype
= mtype
.mtype
1604 # First, select all candidates
1605 var candidates
= new Array[MPROPDEF]
1606 for mpropdef
in self.mpropdefs
do
1607 # If the definition is not imported by the module, then skip
1608 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1609 # If the definition is not inherited by the type, then skip
1610 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1611 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1612 if mtype
== mpropdef
.mclassdef
.bound_mtype
and mmodule
== mpropdef
.mclassdef
.mmodule
then continue
1614 candidates
.add
(mpropdef
)
1616 # Fast track for only one candidate
1617 if candidates
.length
<= 1 then return candidates
1619 # Second, filter the most specific ones
1620 return select_most_specific
(mmodule
, candidates
)
1623 # Return an array containing olny the most specific property definitions
1624 # This is an helper function for `lookup_definitions` and `lookup_super_definitions`
1625 private fun select_most_specific
(mmodule
: MModule, candidates
: Array[MPROPDEF]): Array[MPROPDEF]
1627 var res
= new Array[MPROPDEF]
1628 for pd1
in candidates
do
1629 var cd1
= pd1
.mclassdef
1632 for pd2
in candidates
do
1633 if pd2
== pd1
then continue # do not compare with self!
1634 var cd2
= pd2
.mclassdef
1636 if c2
.mclass_type
== c1
.mclass_type
then
1637 if cd2
.mmodule
.in_importation
< cd1
.mmodule
then
1638 # cd2 refines cd1; therefore we skip pd1
1642 else if cd2
.bound_mtype
.is_subtype
(mmodule
, null, cd1
.bound_mtype
) and cd2
.bound_mtype
!= cd1
.bound_mtype
then
1643 # cd2 < cd1; therefore we skip pd1
1652 if res
.is_empty
then
1653 print
"All lost! {candidates.join(", ")}"
1654 # FIXME: should be abort!
1659 # Return the most specific definition in the linearization of `mtype`.
1661 # If you want to know the next properties in the linearization,
1662 # look at `MPropDef::lookup_next_definition`.
1664 # FIXME: the linearisation is still unspecified
1666 # REQUIRE: `not mtype.need_anchor`
1667 # REQUIRE: `mtype.has_mproperty(mmodule, self)`
1668 fun lookup_first_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
1670 assert mtype
.has_mproperty
(mmodule
, self)
1671 return lookup_all_definitions
(mmodule
, mtype
).first
1674 # Return all definitions in a linearisation order
1675 # Most speficic first, most general last
1676 fun lookup_all_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1678 assert not mtype
.need_anchor
1679 if mtype
isa MNullableType then mtype
= mtype
.mtype
1681 var cache
= self.lookup_all_definitions_cache
[mmodule
, mtype
]
1682 if cache
!= null then return cache
1684 #print "select prop {mproperty} for {mtype} in {self}"
1685 # First, select all candidates
1686 var candidates
= new Array[MPROPDEF]
1687 for mpropdef
in self.mpropdefs
do
1688 # If the definition is not imported by the module, then skip
1689 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1690 # If the definition is not inherited by the type, then skip
1691 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1693 candidates
.add
(mpropdef
)
1695 # Fast track for only one candidate
1696 if candidates
.length
<= 1 then
1697 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1701 mmodule
.linearize_mpropdefs
(candidates
)
1702 candidates
= candidates
.reversed
1703 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1707 private var lookup_all_definitions_cache
: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1714 redef type MPROPDEF: MMethodDef
1716 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1721 # Is the property defined at the top_level of the module?
1722 # Currently such a property are stored in `Object`
1723 var is_toplevel
: Bool writable = false
1725 # Is the property a constructor?
1726 # Warning, this property can be inherited by subclasses with or without being a constructor
1727 # therefore, you should use `is_init_for` the verify if the property is a legal constructor for a given class
1728 var is_init
: Bool writable = false
1730 # The the property a 'new' contructor?
1731 var is_new
: Bool writable = false
1733 # Is the property a legal constructor for a given class?
1734 # As usual, visibility is not considered.
1735 # FIXME not implemented
1736 fun is_init_for
(mclass
: MClass): Bool
1742 # A global attribute
1746 redef type MPROPDEF: MAttributeDef
1748 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1754 # A global virtual type
1755 class MVirtualTypeProp
1758 redef type MPROPDEF: MVirtualTypeDef
1760 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1765 # The formal type associated to the virtual type property
1766 var mvirtualtype
: MVirtualType = new MVirtualType(self)
1769 # A definition of a property (local property)
1771 # Unlike `MProperty`, a `MPropDef` is a local definition that belong to a
1772 # specific class definition (which belong to a specific module)
1773 abstract class MPropDef
1776 # The associated `MProperty` subclass.
1777 # the two specialization hierarchy are symmetric
1778 type MPROPERTY: MProperty
1781 type MPROPDEF: MPropDef
1783 # The origin of the definition
1784 var location
: Location
1786 # The class definition where the property definition is
1787 var mclassdef
: MClassDef
1789 # The associated global property
1790 var mproperty
: MPROPERTY
1792 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1794 self.mclassdef
= mclassdef
1795 self.mproperty
= mproperty
1796 self.location
= location
1797 mclassdef
.mpropdefs
.add
(self)
1798 mproperty
.mpropdefs
.add
(self)
1799 self.to_s
= "{mclassdef}#{mproperty}"
1802 # Internal name combining the module, the class and the property
1803 # Example: "mymodule#MyClass#mymethod"
1804 redef var to_s
: String
1806 # Is self the definition that introduce the property?
1807 fun is_intro
: Bool do return mproperty
.intro
== self
1809 # Return the next definition in linearization of `mtype`.
1811 # This method is used to determine what method is called by a super.
1813 # REQUIRE: `not mtype.need_anchor`
1814 fun lookup_next_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
1816 assert not mtype
.need_anchor
1818 var mpropdefs
= self.mproperty
.lookup_all_definitions
(mmodule
, mtype
)
1819 var i
= mpropdefs
.iterator
1820 while i
.is_ok
and i
.item
!= self do i
.next
1821 assert has_property
: i
.is_ok
1823 assert has_next_property
: i
.is_ok
1828 # A local definition of a method
1832 redef type MPROPERTY: MMethod
1833 redef type MPROPDEF: MMethodDef
1835 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1840 # The signature attached to the property definition
1841 var msignature
: nullable MSignature writable = null
1843 # Is the method definition abstract?
1844 var is_abstract
: Bool writable = false
1846 # Is the method definition intern?
1847 var is_intern
writable = false
1849 # Is the method definition extern?
1850 var is_extern
writable = false
1853 # A local definition of an attribute
1857 redef type MPROPERTY: MAttribute
1858 redef type MPROPDEF: MAttributeDef
1860 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1865 # The static type of the attribute
1866 var static_mtype
: nullable MType writable = null
1869 # A local definition of a virtual type
1870 class MVirtualTypeDef
1873 redef type MPROPERTY: MVirtualTypeProp
1874 redef type MPROPDEF: MVirtualTypeDef
1876 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1881 # The bound of the virtual type
1882 var bound
: nullable MType writable = null
1889 # * `interface_kind`
1893 # Note this class is basically an enum.
1894 # FIXME: use a real enum once user-defined enums are available
1896 redef var to_s
: String
1898 # Is a constructor required?
1900 private init(s
: String, need_init
: Bool)
1903 self.need_init
= need_init
1906 # Can a class of kind `self` specializes a class of kine `other`?
1907 fun can_specialize
(other
: MClassKind): Bool
1909 if other
== interface_kind
then return true # everybody can specialize interfaces
1910 if self == interface_kind
or self == enum_kind
then
1911 # no other case for interfaces
1913 else if self == extern_kind
then
1914 # only compatible with themselve
1915 return self == other
1916 else if other
== enum_kind
or other
== extern_kind
then
1917 # abstract_kind and concrete_kind are incompatible
1920 # remain only abstract_kind and concrete_kind
1925 fun abstract_kind
: MClassKind do return once
new MClassKind("abstract class", true)
1926 fun concrete_kind
: MClassKind do return once
new MClassKind("class", true)
1927 fun interface_kind
: MClassKind do return once
new MClassKind("interface", false)
1928 fun enum_kind
: MClassKind do return once
new MClassKind("enum", false)
1929 fun extern_kind
: MClassKind do return once
new MClassKind("extern class", false)