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, closures, extern stuff
29 # FIXME: better handling of the types
35 private import more_collections
39 var mclasses
: Array[MClass] = new Array[MClass]
41 # All known properties
42 var mproperties
: Array[MProperty] = new Array[MProperty]
44 # Hierarchy of class definition.
46 # Each classdef is associated with its super-classdefs in regard to
47 # its module of definition.
48 var mclassdef_hierarchy
: POSet[MClassDef] = new POSet[MClassDef]
50 # Class-type hierarchy restricted to the introduction.
52 # The idea is that what is true on introduction is always true whatever
53 # the module considered.
54 # Therefore, this hierarchy is used for a fast positive subtype check.
56 # This poset will evolve in a monotonous way:
57 # * Two non connected nodes will remain unconnected
58 # * New nodes can appear with new edges
59 private var intro_mtype_specialization_hierarchy
: POSet[MClassType] = new POSet[MClassType]
61 # Global overlapped class-type hierarchy.
62 # The hierarchy when all modules are combined.
63 # Therefore, this hierarchy is used for a fast negative subtype check.
65 # This poset will evolve in an anarchic way. Loops can even be created.
67 # FIXME decide what to do on loops
68 private var full_mtype_specialization_hierarchy
: POSet[MClassType] = new POSet[MClassType]
70 # Collections of classes grouped by their short name
71 private var mclasses_by_name
: MultiHashMap[String, MClass] = new MultiHashMap[String, MClass]
73 # Return all class named `name'.
75 # If such a class does not exist, null is returned
76 # (instead of an empty array)
78 # Visibility or modules are not considered
79 fun get_mclasses_by_name
(name
: String): nullable Array[MClass]
81 if mclasses_by_name
.has_key
(name
) then
82 return mclasses_by_name
[name
]
88 # Collections of properties grouped by their short name
89 private var mproperties_by_name
: MultiHashMap[String, MProperty] = new MultiHashMap[String, MProperty]
91 # Return all properties named `name'.
93 # If such a property does not exist, null is returned
94 # (instead of an empty array)
96 # Visibility or modules are not considered
97 fun get_mproperties_by_name
(name
: String): nullable Array[MProperty]
99 if not mproperties_by_name
.has_key
(name
) then
102 return mproperties_by_name
[name
]
107 var null_type
: MNullType = new MNullType(self)
111 # All the classes introduced in the module
112 var intro_mclasses
: Array[MClass] = new Array[MClass]
114 # All the class definitions of the module
115 # (introduction and refinement)
116 var mclassdefs
: Array[MClassDef] = new Array[MClassDef]
118 # Does the current module has a given class `mclass'?
119 # Return true if the mmodule introduces, refines or imports a class.
120 # Visibility is not considered.
121 fun has_mclass
(mclass
: MClass): Bool
123 return self.in_importation
<= mclass
.intro_mmodule
126 # Full hierarchy of introduced ans imported classes.
128 # Create a new hierarchy got by flattening the classes for the module
129 # and its imported modules.
130 # Visibility is not considered.
132 # Note: this function is expensive and is usually used for the main
133 # module of a program only. Do not use it to do you own subtype
135 fun flatten_mclass_hierarchy
: POSet[MClass]
137 var res
= self.flatten_mclass_hierarchy_cache
138 if res
!= null then return res
139 res
= new POSet[MClass]
140 for m
in self.in_importation
.greaters
do
141 for cd
in m
.mclassdefs
do
144 for s
in cd
.supertypes
do
145 res
.add_edge
(c
, s
.mclass
)
149 self.flatten_mclass_hierarchy_cache
= res
153 # Sort a given array of classes using the linerarization order of the module
154 # The most general is first, the most specific is last
155 fun linearize_mclasses
(mclasses
: Array[MClass])
157 self.flatten_mclass_hierarchy
.sort
(mclasses
)
160 # Sort a given array of class definitions using the linerarization order of the module
161 # the refinement link is stronger than the specialisation link
162 # The most general is first, the most specific is last
163 fun linearize_mclassdefs
(mclassdefs
: Array[MClassDef])
165 var sorter
= new MClassDefSorter(self)
166 sorter
.sort
(mclassdefs
)
169 # Sort a given array of property definitions using the linerarization order of the module
170 # the refinement link is stronger than the specialisation link
171 # The most general is first, the most specific is last
172 fun linearize_mpropdefs
(mpropdefs
: Array[MPropDef])
174 var sorter
= new MPropDefSorter(self)
175 sorter
.sort
(mpropdefs
)
178 private var flatten_mclass_hierarchy_cache
: nullable POSet[MClass] = null
180 # The primitive type Object, the root of the class hierarchy
181 fun object_type
: MClassType
183 var res
= self.object_type_cache
184 if res
!= null then return res
185 res
= self.get_primitive_class
("Object").mclass_type
186 self.object_type_cache
= res
190 private var object_type_cache
: nullable MClassType
192 # The primitive type Bool
193 fun bool_type
: MClassType
195 var res
= self.bool_type_cache
196 if res
!= null then return res
197 res
= self.get_primitive_class
("Bool").mclass_type
198 self.bool_type_cache
= res
202 private var bool_type_cache
: nullable MClassType
204 # The primitive type Sys, the main type of the program, if any
205 fun sys_type
: nullable MClassType
207 var clas
= self.model
.get_mclasses_by_name
("Sys")
208 if clas
== null then return null
209 return get_primitive_class
("Sys").mclass_type
212 # Force to get the primitive class named `name' or abort
213 fun get_primitive_class
(name
: String): MClass
215 var cla
= self.model
.get_mclasses_by_name
(name
)
217 if name
== "Bool" then
218 var c
= new MClass(self, name
, 0, enum_kind
, public_visibility
)
219 var cladef
= new MClassDef(self, c
.mclass_type
, new Location(null, 0,0,0,0), new Array[String])
222 print
("Fatal Error: no primitive class {name}")
225 assert cla
.length
== 1 else print cla
.join
(", ")
229 # Try to get the primitive method named `name' on the type `recv'
230 fun try_get_primitive_method
(name
: String, recv
: MClass): nullable MMethod
232 var props
= self.model
.get_mproperties_by_name
(name
)
233 if props
== null then return null
234 var res
: nullable MMethod = null
235 for mprop
in props
do
236 assert mprop
isa MMethod
237 var intro
= mprop
.intro_mclassdef
238 for mclassdef
in recv
.mclassdefs
do
239 if not self.in_importation
.greaters
.has
(mclassdef
.mmodule
) then continue
240 if not mclassdef
.in_hierarchy
.greaters
.has
(intro
) then continue
243 else if res
!= mprop
then
244 print
("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
253 private class MClassDefSorter
254 super AbstractSorter[MClassDef]
256 redef fun compare
(a
, b
)
260 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
261 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
265 private class MPropDefSorter
266 super AbstractSorter[MPropDef]
268 redef fun compare
(pa
, pb
)
274 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
275 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
281 # MClass are global to the model; it means that a MClass is not bound to a
282 # specific `MModule`.
284 # This characteristic helps the reasoning about classes in a program since a
285 # single MClass object always denote the same class.
286 # However, because a MClass is global, it does not really have properties nor
287 # belong to a hierarchy since the property and the
288 # hierarchy of a class depends of a module.
290 # The module that introduce the class
291 # While classes are not bound to a specific module,
292 # the introducing module is used for naming an visibility
293 var intro_mmodule
: MModule
295 # The short name of the class
296 # In Nit, the name of a class cannot evolve in refinements
299 # The canonical name of the class
300 # Example: "owner::module::MyClass"
301 fun full_name
: String
303 return "{self.intro_mmodule.full_name}::{name}"
306 # The number of generic formal parameters
307 # 0 if the class is not generic
310 # The kind of the class (interface, abstract class, etc.)
311 # In Nit, the kind of a class cannot evolve in refinements
314 # The visibility of the class
315 # In Nit, the visibility of a class cannot evolve in refinements
316 var visibility
: MVisibility
318 init(intro_mmodule
: MModule, name
: String, arity
: Int, kind
: MClassKind, visibility
: MVisibility)
320 self.intro_mmodule
= intro_mmodule
324 self.visibility
= visibility
325 intro_mmodule
.intro_mclasses
.add
(self)
326 var model
= intro_mmodule
.model
327 model
.mclasses_by_name
.add_one
(name
, self)
328 model
.mclasses
.add
(self)
330 # Create the formal parameter types
332 var mparametertypes
= new Array[MParameterType]
333 for i
in [0..arity
[ do
334 var mparametertype
= new MParameterType(self, i
)
335 mparametertypes
.add
(mparametertype
)
337 var mclass_type
= new MGenericType(self, mparametertypes
)
338 self.mclass_type
= mclass_type
339 self.get_mtype_cache
.add
(mclass_type
)
341 self.mclass_type
= new MClassType(self)
345 # All class definitions (introduction and refinements)
346 var mclassdefs
: Array[MClassDef] = new Array[MClassDef]
349 redef fun to_s
do return self.name
351 # The definition that introduced the class
352 # Warning: the introduction is the first `MClassDef' object associated
353 # to self. If self is just created without having any associated
354 # definition, this method will abort
357 assert has_a_first_definition
: not mclassdefs
.is_empty
358 return mclassdefs
.first
361 # Return the class `self' in the class hierarchy of the module `mmodule'.
363 # SEE: MModule::flatten_mclass_hierarchy
364 # REQUIRE: mmodule.has_mclass(self)
365 fun in_hierarchy
(mmodule
: MModule): POSetElement[MClass]
367 return mmodule
.flatten_mclass_hierarchy
[self]
370 # The principal static type of the class.
372 # For non-generic class, mclass_type is the only MClassType based
375 # For a generic class, the arguments are the formal parameters.
376 # i.e.: for the class `Array[E:Object]', the mtype is Array[E].
377 # If you want `Array[Object]' the see `MClassDef::bound_mtype'
379 # For generic classes, the mclass_type is also the way to get a formal
380 # generic parameter type.
382 # To get other types based on a generic class, see `get_mtype'.
384 # ENSURE: mclass_type.mclass == self
385 var mclass_type
: MClassType
387 # Return a generic type based on the class
388 # Is the class is not generic, then the result is `mclass_type'
390 # REQUIRE: type_arguments.length == self.arity
391 fun get_mtype
(mtype_arguments
: Array[MType]): MClassType
393 assert mtype_arguments
.length
== self.arity
394 if self.arity
== 0 then return self.mclass_type
395 for t
in self.get_mtype_cache
do
396 if t
.arguments
== mtype_arguments
then
400 var res
= new MGenericType(self, mtype_arguments
)
401 self.get_mtype_cache
.add res
405 private var get_mtype_cache
: Array[MGenericType] = new Array[MGenericType]
409 # A definition (an introduction or a refinement) of a class in a module
411 # A MClassDef is associated with an explicit (or almost) definition of a
412 # class. Unlike MClass, a MClassDef is a local definition that belong to
415 # The module where the definition is
418 # The associated MClass
421 # The bounded type associated to the mclassdef
423 # For a non-generic class, `bound_mtype' and `mclass.mclass_type'
427 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
428 # If you want Array[E], then see `mclass.mclass_type'
430 # ENSURE: bound_mtype.mclass = self.mclass
431 var bound_mtype
: MClassType
433 # Name of each formal generic parameter (in order of declaration)
434 var parameter_names
: Array[String]
436 # The origin of the definition
437 var location
: Location
439 # Internal name combining the module and the class
440 # Example: "mymodule#MyClass"
441 redef var to_s
: String
443 init(mmodule
: MModule, bound_mtype
: MClassType, location
: Location, parameter_names
: Array[String])
445 assert bound_mtype
.mclass
.arity
== parameter_names
.length
446 self.bound_mtype
= bound_mtype
447 self.mmodule
= mmodule
448 self.mclass
= bound_mtype
.mclass
449 self.location
= location
450 mmodule
.mclassdefs
.add
(self)
451 mclass
.mclassdefs
.add
(self)
452 self.parameter_names
= parameter_names
453 self.to_s
= "{mmodule}#{mclass}"
456 # All declared super-types
457 # FIXME: quite ugly but not better idea yet
458 var supertypes
: Array[MClassType] = new Array[MClassType]
460 # Register some super-types for the class (ie "super SomeType")
462 # The hierarchy must not already be set
463 # REQUIRE: self.in_hierarchy == null
464 fun set_supertypes
(supertypes
: Array[MClassType])
466 assert unique_invocation
: self.in_hierarchy
== null
467 var mmodule
= self.mmodule
468 var model
= mmodule
.model
469 var mtype
= self.bound_mtype
471 for supertype
in supertypes
do
472 self.supertypes
.add
(supertype
)
474 # Register in full_type_specialization_hierarchy
475 model
.full_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
476 # Register in intro_type_specialization_hierarchy
477 if mclass
.intro_mmodule
== mmodule
and supertype
.mclass
.intro_mmodule
== mmodule
then
478 model
.intro_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
484 # Collect the super-types (set by set_supertypes) to build the hierarchy
486 # This function can only invoked once by class
487 # REQUIRE: self.in_hierarchy == null
488 # ENSURE: self.in_hierarchy != null
491 assert unique_invocation
: self.in_hierarchy
== null
492 var model
= mmodule
.model
493 var res
= model
.mclassdef_hierarchy
.add_node
(self)
494 self.in_hierarchy
= res
495 var mtype
= self.bound_mtype
497 # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
498 # The simpliest way is to attach it to collect_mclassdefs
499 for mclassdef
in mtype
.collect_mclassdefs
(mmodule
) do
500 res
.poset
.add_edge
(self, mclassdef
)
504 # The view of the class definition in `mclassdef_hierarchy'
505 var in_hierarchy
: nullable POSetElement[MClassDef] = null
507 # Is the definition the one that introduced `mclass`?
508 fun is_intro
: Bool do return mclass
.intro
== self
510 # All properties introduced by the classdef
511 var intro_mproperties
: Array[MProperty] = new Array[MProperty]
513 # All property definitions in the class (introductions and redefinitions)
514 var mpropdefs
: Array[MPropDef] = new Array[MPropDef]
517 # A global static type
519 # MType are global to the model; it means that a MType is not bound to a
520 # specific `MModule`.
521 # This characteristic helps the reasoning about static types in a program
522 # since a single MType object always denote the same type.
524 # However, because a MType is global, it does not really have properties
525 # nor have subtypes to a hierarchy since the property and the class hierarchy
526 # depends of a module.
527 # Moreover, virtual types an formal generic parameter types also depends on
528 # a receiver to have sense.
530 # Therefore, most method of the types require a module and an anchor.
531 # The module is used to know what are the classes and the specialization
533 # The anchor is used to know what is the bound of the virtual types and formal
534 # generic parameter types.
536 # MType are not directly usable to get properties. See the `anchor_to' method
537 # and the `MClassType' class.
539 # FIXME: the order of the parameters is not the best. We mus pick on from:
540 # * foo(mmodule, anchor, othertype)
541 # * foo(othertype, anchor, mmodule)
542 # * foo(anchor, mmodule, othertype)
543 # * foo(othertype, mmodule, anchor)
546 # The model of the type
547 fun model
: Model is abstract
549 # Return true if `self' is an subtype of `sup'.
550 # The typing is done using the standard typing policy of Nit.
552 # REQUIRE: anchor == null implies not self.need_anchor and not sup.need_anchor
553 # REQUIRE: anchor != null implies self.can_resolve_for(anchor, null, mmodule) and sup.can_resolve_for(anchor, null, mmodule)
554 fun is_subtype
(mmodule
: MModule, anchor
: nullable MClassType, sup
: MType): Bool
557 if sub
== sup
then return true
558 if anchor
== null then
559 assert not sub
.need_anchor
560 assert not sup
.need_anchor
562 assert sub
.can_resolve_for
(anchor
, null, mmodule
)
563 assert sup
.can_resolve_for
(anchor
, null, mmodule
)
566 # First, resolve the formal types to a common version in the receiver
567 # The trick here is that fixed formal type will be associed to the bound
568 # And unfixed formal types will be associed to a canonical formal type.
569 if sub
isa MParameterType or sub
isa MVirtualType then
570 assert anchor
!= null
571 sub
= sub
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, false)
573 if sup
isa MParameterType or sup
isa MVirtualType then
574 assert anchor
!= null
575 sup
= sup
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, false)
578 # Does `sup` accept null or not?
579 # Discard the nullable marker if it exists
580 var sup_accept_null
= false
581 if sup
isa MNullableType then
582 sup_accept_null
= true
584 else if sup
isa MNullType then
585 sup_accept_null
= true
588 # Can `sub` provide null or not?
589 # Thus we can match with `sup_accept_null`
590 # Also discard the nullable marker if it exists
591 if sub
isa MNullableType then
592 if not sup_accept_null
then return false
594 else if sub
isa MNullType then
595 return sup_accept_null
597 # Now the case of direct null and nullable is over.
599 # A unfixed formal type can only accept itself
600 if sup
isa MParameterType or sup
isa MVirtualType then
604 # If `sub` is a formal type, then it is accepted if its bound is accepted
605 if sub
isa MParameterType or sub
isa MVirtualType then
606 assert anchor
!= null
607 sub
= sub
.anchor_to
(mmodule
, anchor
)
609 # Manage the second layer of null/nullable
610 if sub
isa MNullableType then
611 if not sup_accept_null
then return false
613 else if sub
isa MNullType then
614 return sup_accept_null
618 assert sub
isa MClassType # It is the only remaining type
620 if sup
isa MNullType then
621 # `sup` accepts only null
625 assert sup
isa MClassType # It is the only remaining type
627 # Now both are MClassType, we need to dig
629 if sub
== sup
then return true
631 if anchor
== null then anchor
= sub
# UGLY: any anchor will work
632 var resolved_sub
= sub
.anchor_to
(mmodule
, anchor
)
633 var res
= resolved_sub
.collect_mclasses
(mmodule
).has
(sup
.mclass
)
634 if res
== false then return false
635 if not sup
isa MGenericType then return true
636 var sub2
= sub
.supertype_to
(mmodule
, anchor
, sup
.mclass
)
637 assert sub2
.mclass
== sup
.mclass
638 for i
in [0..sup
.mclass
.arity
[ do
639 var sub_arg
= sub2
.arguments
[i
]
640 var sup_arg
= sup
.arguments
[i
]
641 res
= sub_arg
.is_subtype
(mmodule
, anchor
, sup_arg
)
642 if res
== false then return false
647 # The base class type on which self is based
649 # This base type is used to get property (an internally to perform
650 # unsafe type comparison).
652 # Beware: some types (like null) are not based on a class thus this
655 # Basically, this function transform the virtual types and parameter
656 # types to their bounds.
666 # Map[T,U] anchor_to H #-> Map[C,Y]
668 # Explanation of the example:
669 # In H, T is set to C, because "H super G[C]", and U is bound to Y,
670 # because "redef type U: Y". Therefore, Map[T, U] is bound to
673 # ENSURE: not self.need_anchor implies return == self
674 # ENSURE: not return.need_anchor
675 fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MType
677 if not need_anchor
then return self
678 assert not anchor
.need_anchor
679 # Just resolve to the anchor and clear all the virtual types
680 var res
= self.resolve_for
(anchor
, null, mmodule
, true)
681 assert not res
.need_anchor
685 # Does `self' contain a virtual type or a formal generic parameter type?
686 # In order to remove those types, you usually want to use `anchor_to'.
687 fun need_anchor
: Bool do return true
689 # Return the supertype when adapted to a class.
691 # In Nit, for each super-class of a type, there is a equivalent super-type.
695 # class H[V] super G[V, Bool]
696 # H[Int] supertype_to G #-> G[Int, Bool]
698 # REQUIRE: `super_mclass' is a super-class of `self'
699 # REQUIRE: self.need_anchor implies anchor != null and self.can_resolve_for(anchor, null, mmodule)
700 # ENSURE: return.mclass = mclass
701 fun supertype_to
(mmodule
: MModule, anchor
: nullable MClassType, super_mclass
: MClass): MClassType
703 if super_mclass
.arity
== 0 then return super_mclass
.mclass_type
704 if self isa MClassType and self.mclass
== super_mclass
then return self
706 if self.need_anchor
then
707 assert anchor
!= null
708 resolved_self
= self.anchor_to
(mmodule
, anchor
)
712 var supertypes
= resolved_self
.collect_mtypes
(mmodule
)
713 for supertype
in supertypes
do
714 if supertype
.mclass
== super_mclass
then
715 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
716 return supertype
.resolve_for
(self, anchor
, mmodule
, false)
722 # Replace formals generic types in self with resolved values in `mtype'
723 # If `cleanup_virtual' is true, then virtual types are also replaced
726 # This function returns self if `need_anchor' is false.
731 # class H[F] super G[F]
734 # Array[E].resolve_for(H[Int]) #-> Array[Int]
735 # Array[E].resolve_for(G[Z], X[Int]) #-> Array[Z]
737 # Explanation of the example:
738 # * Array[E].need_anchor is true because there is a formal generic
740 # * E makes sense for H[Int] because E is a formal parameter of G
742 # * Since "H[F] super G[F]", E is in fact F for H
743 # * More specifically, in H[Int], E is Int
744 # * So, in H[Int], Array[E] is Array[Int]
746 # This function is mainly used to inherit a signature.
747 # Because, unlike `anchor_to', we do not want a full resolution of
748 # a type but only an adapted version of it.
755 # class B super A[Int] end
757 # The signature on foo is (e: E): E
758 # If we resolve the signature for B, we get (e:Int):Int
763 # fun foo(e:E) is abstract
767 # fun bar do a.foo(x) # <- x is here
770 # The first question is: is foo available on `a`?
772 # The static type of a is `A[Array[F]]`, that is an open type.
773 # in order to find a method `foo`, whe must look at a resolved type.
775 # A[Array[F]].anchor_to(B[nullable Object]) #-> A[Array[nullable Object]]
777 # the method `foo` exists in `A[Array[nullable Object]]`, therefore `foo` exists for `a`.
779 # The next question is: what is the accepted types for `x'?
781 # the signature of `foo` is `foo(e:E)`, thus we must resolve the type E
783 # E.resolve_for(A[Array[F]],B[nullable Object]) #-> Array[F]
785 # The resolution can be done because `E` make sense for the class A (see `can_resolve_for`)
787 # TODO: Explain the cleanup_virtual
789 # FIXME: the parameter `cleanup_virtual' is just a bad idea, but having
790 # two function instead of one seems also to be a bad idea.
792 # REQUIRE: can_resolve_for(mtype, anchor, mmodule)
793 # ENSURE: not self.need_anchor implies return == self
794 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MType is abstract
796 # Can the type be resolved?
798 # In order to resolve open types, the formal types must make sence.
807 # E.can_resolve_for(A[Int]) #-> true, E make sense in A
808 # E.can_resolve_for(B[Int]) #-> false, E does not make sense in B
809 # B[E].can_resolve_for(A[F], B[Object]) #-> true,
810 # B[E] is a red hearing only the E is important,
813 # REQUIRE: anchor != null implies not anchor.need_anchor
814 # REQUIRE: mtype.need_anchor implies anchor != null and mtype.can_resolve_for(anchor, null, mmodule)
815 # ENSURE: not self.need_anchor implies return == true
816 fun can_resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule): Bool is abstract
818 # Return the nullable version of the type
819 # If the type is already nullable then self is returned
820 fun as_nullable
: MType
822 var res
= self.as_nullable_cache
823 if res
!= null then return res
824 res
= new MNullableType(self)
825 self.as_nullable_cache
= res
829 private var as_nullable_cache
: nullable MType = null
832 # The deph of the type seen as a tree.
839 # Formal types have a depth of 1.
845 # The length of the type seen as a tree.
852 # Formal types have a length of 1.
858 # Compute all the classdefs inherited/imported.
859 # The returned set contains:
860 # * the class definitions from `mmodule` and its imported modules
861 # * the class definitions of this type and its super-types
863 # This function is used mainly internally.
865 # REQUIRE: not self.need_anchor
866 fun collect_mclassdefs
(mmodule
: MModule): Set[MClassDef] is abstract
868 # Compute all the super-classes.
869 # This function is used mainly internally.
871 # REQUIRE: not self.need_anchor
872 fun collect_mclasses
(mmodule
: MModule): Set[MClass] is abstract
874 # Compute all the declared super-types.
875 # Super-types are returned as declared in the classdefs (verbatim).
876 # This function is used mainly internally.
878 # REQUIRE: not self.need_anchor
879 fun collect_mtypes
(mmodule
: MModule): Set[MClassType] is abstract
881 # Is the property in self for a given module
882 # This method does not filter visibility or whatever
884 # REQUIRE: not self.need_anchor
885 fun has_mproperty
(mmodule
: MModule, mproperty
: MProperty): Bool
887 assert not self.need_anchor
888 return self.collect_mclassdefs
(mmodule
).has
(mproperty
.intro_mclassdef
)
892 # A type based on a class.
894 # MClassType have properties (see `has_property').
898 # The associated class
901 redef fun model
do return self.mclass
.intro_mmodule
.model
903 private init(mclass
: MClass)
908 # The formal arguments of the type
909 # ENSURE: return.length == self.mclass.arity
910 var arguments
: Array[MType] = new Array[MType]
912 redef fun to_s
do return mclass
.to_s
914 redef fun need_anchor
do return false
916 redef fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MClassType
918 return super.as(MClassType)
921 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MClassType do return self
923 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
925 redef fun collect_mclassdefs
(mmodule
)
927 assert not self.need_anchor
928 var cache
= self.collect_mclassdefs_cache
929 if not cache
.has_key
(mmodule
) then
930 self.collect_things
(mmodule
)
932 return cache
[mmodule
]
935 redef fun collect_mclasses
(mmodule
)
937 assert not self.need_anchor
938 var cache
= self.collect_mclasses_cache
939 if not cache
.has_key
(mmodule
) then
940 self.collect_things
(mmodule
)
942 return cache
[mmodule
]
945 redef fun collect_mtypes
(mmodule
)
947 assert not self.need_anchor
948 var cache
= self.collect_mtypes_cache
949 if not cache
.has_key
(mmodule
) then
950 self.collect_things
(mmodule
)
952 return cache
[mmodule
]
955 # common implementation for `collect_mclassdefs', `collect_mclasses', and `collect_mtypes'.
956 private fun collect_things
(mmodule
: MModule)
958 var res
= new HashSet[MClassDef]
959 var seen
= new HashSet[MClass]
960 var types
= new HashSet[MClassType]
961 seen
.add
(self.mclass
)
962 var todo
= [self.mclass
]
963 while not todo
.is_empty
do
964 var mclass
= todo
.pop
965 #print "process {mclass}"
966 for mclassdef
in mclass
.mclassdefs
do
967 if not mmodule
.in_importation
<= mclassdef
.mmodule
then continue
968 #print " process {mclassdef}"
970 for supertype
in mclassdef
.supertypes
do
972 var superclass
= supertype
.mclass
973 if seen
.has
(superclass
) then continue
974 #print " add {superclass}"
980 collect_mclassdefs_cache
[mmodule
] = res
981 collect_mclasses_cache
[mmodule
] = seen
982 collect_mtypes_cache
[mmodule
] = types
985 private var collect_mclassdefs_cache
: HashMap[MModule, Set[MClassDef]] = new HashMap[MModule, Set[MClassDef]]
986 private var collect_mclasses_cache
: HashMap[MModule, Set[MClass]] = new HashMap[MModule, Set[MClass]]
987 private var collect_mtypes_cache
: HashMap[MModule, Set[MClassType]] = new HashMap[MModule, Set[MClassType]]
991 # A type based on a generic class.
992 # A generic type a just a class with additional formal generic arguments.
996 private init(mclass
: MClass, arguments
: Array[MType])
999 assert self.mclass
.arity
== arguments
.length
1000 self.arguments
= arguments
1002 self.need_anchor
= false
1003 for t
in arguments
do
1004 if t
.need_anchor
then
1005 self.need_anchor
= true
1010 self.to_s
= "{mclass}[{arguments.join(", ")}]"
1013 # Recursively print the type of the arguments within brackets.
1014 # Example: "Map[String, List[Int]]"
1015 redef var to_s
: String
1017 redef var need_anchor
: Bool
1019 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1021 if not need_anchor
then return self
1022 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1023 var types
= new Array[MType]
1024 for t
in arguments
do
1025 types
.add
(t
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1027 return mclass
.get_mtype
(types
)
1030 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1032 if not need_anchor
then return true
1033 for t
in arguments
do
1034 if not t
.can_resolve_for
(mtype
, anchor
, mmodule
) then return false
1043 for a
in self.arguments
do
1045 if d
> dmax
then dmax
= d
1053 for a
in self.arguments
do
1060 # A virtual formal type.
1064 # The property associated with the type.
1065 # Its the definitions of this property that determine the bound or the virtual type.
1066 var mproperty
: MProperty
1068 redef fun model
do return self.mproperty
.intro_mclassdef
.mmodule
.model
1070 # Lookup the bound for a given resolved_receiver
1071 # The result may be a other virtual type (or a parameter type)
1073 # The result is returned exactly as declared in the "type" property (verbatim).
1075 # In case of conflict, the method aborts.
1076 fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1078 assert not resolved_receiver
.need_anchor
1079 var props
= self.mproperty
.lookup_definitions
(mmodule
, resolved_receiver
)
1080 if props
.is_empty
then
1082 else if props
.length
== 1 then
1083 return props
.first
.as(MVirtualTypeDef).bound
.as(not null)
1085 var types
= new ArraySet[MType]
1087 types
.add
(p
.as(MVirtualTypeDef).bound
.as(not null))
1089 if types
.length
== 1 then
1095 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1097 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1098 # self is a virtual type declared (or inherited) in mtype
1099 # The point of the function it to get the bound of the virtual type that make sense for mtype
1100 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1101 #print "{class_name}: {self}/{mtype}/{anchor}?"
1102 var resolved_reciever
1103 if mtype
.need_anchor
then
1104 assert anchor
!= null
1105 resolved_reciever
= mtype
.resolve_for
(anchor
, null, mmodule
, true)
1107 resolved_reciever
= mtype
1109 # Now, we can get the bound
1110 var verbatim_bound
= lookup_bound
(mmodule
, resolved_reciever
)
1111 # The bound is exactly as declared in the "type" property, so we must resolve it again
1112 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1113 #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}"
1115 # What to return here? There is a bunch a special cases:
1116 # If 'cleanup_virtual' we must return the resolved type, since we cannot return self
1117 if cleanup_virtual
then return res
1118 # 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
1119 if resolved_reciever
isa MNullableType then resolved_reciever
= resolved_reciever
.mtype
1120 if resolved_reciever
.as(MClassType).mclass
.kind
== enum_kind
then return res
1121 # 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.
1122 if res
isa MVirtualType then return res
1123 # 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
1124 if res
isa MClassType and res
.mclass
.kind
== enum_kind
then return res
1125 # TODO: Add 'fixed' virtual type in the specification.
1126 # TODO: What if bound to a MParameterType?
1127 # Note that Nullable types can always be redefined by the non nullable version, so there is no specific case on it.
1129 # If anything apply, then `self' cannot be resolved, so return self
1133 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1135 if mtype
.need_anchor
then
1136 assert anchor
!= null
1137 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1139 return mtype
.has_mproperty
(mmodule
, mproperty
)
1142 redef fun to_s
do return self.mproperty
.to_s
1144 init(mproperty
: MProperty)
1146 self.mproperty
= mproperty
1150 # The type associated the a formal parameter generic type of a class
1152 # Each parameter type is associated to a specific class.
1153 # It's mean that all refinements of a same class "share" the parameter type,
1154 # but that a generic subclass has its on parameter types.
1156 # However, in the sense of the meta-model, the a parameter type of a class is
1157 # a valid types in a subclass. The "in the sense of the meta-model" is
1158 # important because, in the Nit language, the programmer cannot refers
1159 # directly to the parameter types of the super-classes.
1163 # fun e: E is abstract
1168 # In the class definition B[F], `F' is a valid type but `E' is not.
1169 # However, `self.e' is a valid method call, and the signature of `e' is
1172 # Note that parameter types are shared among class refinements.
1173 # Therefore parameter only have an internal name (see `to_s' for details).
1174 # TODO: Add a 'name_for' to get better messages.
1175 class MParameterType
1178 # The generic class where the parameter belong
1181 redef fun model
do return self.mclass
.intro_mmodule
.model
1183 # The position of the parameter (0 for the first parameter)
1184 # FIXME: is `position' a better name?
1187 # Internal name of the parameter type
1188 # Names of parameter types changes in each class definition
1189 # Therefore, this method return an internal name.
1190 # Example: return "G#1" for the second parameter of the class G
1191 # FIXME: add a way to get the real name in a classdef
1192 redef fun to_s
do return "{mclass}#{rank}"
1194 # Resolve the bound for a given resolved_receiver
1195 # The result may be a other virtual type (or a parameter type)
1196 fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1198 assert not resolved_receiver
.need_anchor
1199 var goalclass
= self.mclass
1200 var supertypes
= resolved_receiver
.collect_mtypes
(mmodule
)
1201 for t
in supertypes
do
1202 if t
.mclass
== goalclass
then
1203 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
1204 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
1205 var res
= t
.arguments
[self.rank
]
1212 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1214 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1215 #print "{class_name}: {self}/{mtype}/{anchor}?"
1217 if mtype
isa MGenericType and mtype
.mclass
== self.mclass
then
1218 return mtype
.arguments
[self.rank
]
1221 # self is a parameter type of mtype (or of a super-class of mtype)
1222 # The point of the function it to get the bound of the virtual type that make sense for mtype
1223 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1224 # FIXME: What happend here is far from clear. Thus this part must be validated and clarified
1225 var resolved_receiver
1226 if mtype
.need_anchor
then
1227 assert anchor
!= null
1228 resolved_receiver
= mtype
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, true)
1230 resolved_receiver
= mtype
1232 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1233 if resolved_receiver
isa MParameterType then
1234 assert resolved_receiver
.mclass
== anchor
.mclass
1235 resolved_receiver
= anchor
.arguments
[resolved_receiver
.rank
]
1236 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1238 assert resolved_receiver
isa MClassType
1240 # Eh! The parameter is in the current class.
1241 # So we return the corresponding argument, no mater what!
1242 if resolved_receiver
.mclass
== self.mclass
then
1243 var res
= resolved_receiver
.arguments
[self.rank
]
1244 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
1248 if resolved_receiver
.need_anchor
then
1249 assert anchor
!= null
1250 resolved_receiver
= resolved_receiver
.resolve_for
(anchor
, null, mmodule
, false)
1252 # Now, we can get the bound
1253 var verbatim_bound
= lookup_bound
(mmodule
, resolved_receiver
)
1254 # The bound is exactly as declared in the "type" property, so we must resolve it again
1255 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1257 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
1262 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1264 if mtype
.need_anchor
then
1265 assert anchor
!= null
1266 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1268 return mtype
.collect_mclassdefs
(mmodule
).has
(mclass
.intro
)
1271 init(mclass
: MClass, rank
: Int)
1273 self.mclass
= mclass
1278 # A type prefixed with "nullable"
1282 # The base type of the nullable type
1285 redef fun model
do return self.mtype
.model
1290 self.to_s
= "nullable {mtype}"
1293 redef var to_s
: String
1295 redef fun need_anchor
do return mtype
.need_anchor
1296 redef fun as_nullable
do return self
1297 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1299 var res
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1300 return res
.as_nullable
1303 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1305 return self.mtype
.can_resolve_for
(mtype
, anchor
, mmodule
)
1308 redef fun depth
do return self.mtype
.depth
1310 redef fun length
do return self.mtype
.length
1312 redef fun collect_mclassdefs
(mmodule
)
1314 assert not self.need_anchor
1315 return self.mtype
.collect_mclassdefs
(mmodule
)
1318 redef fun collect_mclasses
(mmodule
)
1320 assert not self.need_anchor
1321 return self.mtype
.collect_mclasses
(mmodule
)
1324 redef fun collect_mtypes
(mmodule
)
1326 assert not self.need_anchor
1327 return self.mtype
.collect_mtypes
(mmodule
)
1331 # The type of the only value null
1333 # The is only one null type per model, see `MModel::null_type'.
1336 redef var model
: Model
1337 protected init(model
: Model)
1341 redef fun to_s
do return "null"
1342 redef fun as_nullable
do return self
1343 redef fun need_anchor
do return false
1344 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
) do return self
1345 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
1347 redef fun collect_mclassdefs
(mmodule
) do return new HashSet[MClassDef]
1349 redef fun collect_mclasses
(mmodule
) do return new HashSet[MClass]
1351 redef fun collect_mtypes
(mmodule
) do return new HashSet[MClassType]
1354 # A signature of a method (or a closure)
1358 # The each parameter (in order)
1359 var mparameters
: Array[MParameter]
1361 var mclosures
= new Array[MParameter]
1363 # The return type (null for a procedure)
1364 var return_mtype
: nullable MType
1369 var t
= self.return_mtype
1370 if t
!= null then dmax
= t
.depth
1371 for p
in mparameters
do
1372 var d
= p
.mtype
.depth
1373 if d
> dmax
then dmax
= d
1375 for p
in mclosures
do
1376 var d
= p
.mtype
.depth
1377 if d
> dmax
then dmax
= d
1385 var t
= self.return_mtype
1386 if t
!= null then res
+= t
.length
1387 for p
in mparameters
do
1388 res
+= p
.mtype
.length
1390 for p
in mclosures
do
1391 res
+= p
.mtype
.length
1396 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1397 init(mparameters
: Array[MParameter], return_mtype
: nullable MType)
1399 var vararg_rank
= -1
1400 for i
in [0..mparameters
.length
[ do
1401 var parameter
= mparameters
[i
]
1402 if parameter
.is_vararg
then
1403 assert vararg_rank
== -1
1407 self.mparameters
= mparameters
1408 self.return_mtype
= return_mtype
1409 self.vararg_rank
= vararg_rank
1412 # The rank of the ellipsis (...) for vararg (starting from 0).
1413 # value is -1 if there is no vararg.
1414 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1415 var vararg_rank
: Int
1417 # The number or parameters
1418 fun arity
: Int do return mparameters
.length
1423 if not mparameters
.is_empty
then
1425 for i
in [0..mparameters
.length
[ do
1426 var mparameter
= mparameters
[i
]
1427 if i
> 0 then b
.append
(", ")
1428 b
.append
(mparameter
.name
)
1430 b
.append
(mparameter
.mtype
.to_s
)
1431 if mparameter
.is_vararg
then
1437 var ret
= self.return_mtype
1445 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MSignature
1447 var params
= new Array[MParameter]
1448 for p
in self.mparameters
do
1449 params
.add
(p
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1451 var ret
= self.return_mtype
1453 ret
= ret
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1455 var res
= new MSignature(params
, ret
)
1456 for p
in self.mclosures
do
1457 res
.mclosures
.add
(p
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1463 # A parameter in a signature
1465 # The name of the parameter
1468 # The static type of the parameter
1471 # Is the parameter a vararg?
1474 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MParameter
1476 if not self.mtype
.need_anchor
then return self
1477 var newtype
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1478 var res
= new MParameter(self.name
, newtype
, self.is_vararg
)
1483 # A service (global property) that generalize method, attribute, etc.
1485 # MProperty are global to the model; it means that a MProperty is not bound
1486 # to a specific `MModule` nor a specific `MClass`.
1488 # A MProperty gather definitions (see `mpropdefs') ; one for the introduction
1489 # and the other in subclasses and in refinements.
1491 # A MProperty is used to denotes services in polymorphic way (ie. independent
1492 # of any dynamic type).
1493 # For instance, a call site "x.foo" is associated to a MProperty.
1494 abstract class MProperty
1495 # The associated MPropDef subclass.
1496 # The two specialization hierarchy are symmetric.
1497 type MPROPDEF: MPropDef
1499 # The classdef that introduce the property
1500 # While a property is not bound to a specific module, or class,
1501 # the introducing mclassdef is used for naming and visibility
1502 var intro_mclassdef
: MClassDef
1504 # The (short) name of the property
1507 # The canonical name of the property
1508 # Example: "owner::my_module::MyClass::my_method"
1509 fun full_name
: String
1511 return "{self.intro_mclassdef.mmodule.full_name}::{self.intro_mclassdef.mclass.name}::{name}"
1514 # The visibility of the property
1515 var visibility
: MVisibility
1517 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1519 self.intro_mclassdef
= intro_mclassdef
1521 self.visibility
= visibility
1522 intro_mclassdef
.intro_mproperties
.add
(self)
1523 var model
= intro_mclassdef
.mmodule
.model
1524 model
.mproperties_by_name
.add_one
(name
, self)
1525 model
.mproperties
.add
(self)
1528 # All definitions of the property.
1529 # The first is the introduction,
1530 # The other are redefinitions (in refinements and in subclasses)
1531 var mpropdefs
: Array[MPROPDEF] = new Array[MPROPDEF]
1533 # The definition that introduced the property
1534 # Warning: the introduction is the first `MPropDef' object
1535 # associated to self. If self is just created without having any
1536 # associated definition, this method will abort
1537 fun intro
: MPROPDEF do return mpropdefs
.first
1540 redef fun to_s
do return name
1542 # Return the most specific property definitions defined or inherited by a type.
1543 # The selection knows that refinement is stronger than specialization;
1544 # however, in case of conflict more than one property are returned.
1545 # If mtype does not know mproperty then an empty array is returned.
1547 # If you want the really most specific property, then look at `lookup_first_definition`
1548 fun lookup_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1550 assert not mtype
.need_anchor
1551 if mtype
isa MNullableType then mtype
= mtype
.mtype
1553 var cache
= self.lookup_definitions_cache
[mmodule
, mtype
]
1554 if cache
!= null then return cache
1556 #print "select prop {mproperty} for {mtype} in {self}"
1557 # First, select all candidates
1558 var candidates
= new Array[MPROPDEF]
1559 for mpropdef
in self.mpropdefs
do
1560 # If the definition is not imported by the module, then skip
1561 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1562 # If the definition is not inherited by the type, then skip
1563 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1565 candidates
.add
(mpropdef
)
1567 # Fast track for only one candidate
1568 if candidates
.length
<= 1 then
1569 self.lookup_definitions_cache
[mmodule
, mtype
] = candidates
1573 # Second, filter the most specific ones
1574 var res
= new Array[MPROPDEF]
1575 for pd1
in candidates
do
1576 var cd1
= pd1
.mclassdef
1579 for pd2
in candidates
do
1580 if pd2
== pd1
then continue # do not compare with self!
1581 var cd2
= pd2
.mclassdef
1583 if c2
.mclass_type
== c1
.mclass_type
then
1584 if cd2
.mmodule
.in_importation
<= cd1
.mmodule
then
1585 # cd2 refines cd1; therefore we skip pd1
1589 else if cd2
.bound_mtype
.is_subtype
(mmodule
, null, cd1
.bound_mtype
) then
1590 # cd2 < cd1; therefore we skip pd1
1599 if res
.is_empty
then
1600 print
"All lost! {candidates.join(", ")}"
1601 # FIXME: should be abort!
1603 self.lookup_definitions_cache
[mmodule
, mtype
] = res
1607 private var lookup_definitions_cache
: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1609 # Return the most specific property definitions inherited by a type.
1610 # The selection knows that refinement is stronger than specialization;
1611 # however, in case of conflict more than one property are returned.
1612 # If mtype does not know mproperty then an empty array is returned.
1614 # If you want the really most specific property, then look at `lookup_next_definition`
1616 # FIXME: Move to MPropDef?
1617 fun lookup_super_definitions
(mmodule
: MModule, mtype
: MType): Array[MPropDef]
1619 assert not mtype
.need_anchor
1620 if mtype
isa MNullableType then mtype
= mtype
.mtype
1622 # First, select all candidates
1623 var candidates
= new Array[MPropDef]
1624 for mpropdef
in self.mpropdefs
do
1625 # If the definition is not imported by the module, then skip
1626 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1627 # If the definition is not inherited by the type, then skip
1628 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1629 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1630 if mtype
== mpropdef
.mclassdef
.bound_mtype
and mmodule
== mpropdef
.mclassdef
.mmodule
then continue
1632 candidates
.add
(mpropdef
)
1634 # Fast track for only one candidate
1635 if candidates
.length
<= 1 then return candidates
1637 # Second, filter the most specific ones
1638 var res
= new Array[MPropDef]
1639 for pd1
in candidates
do
1640 var cd1
= pd1
.mclassdef
1643 for pd2
in candidates
do
1644 if pd2
== pd1
then continue # do not compare with self!
1645 var cd2
= pd2
.mclassdef
1647 if c2
.mclass_type
== c1
.mclass_type
then
1648 if cd2
.mmodule
.in_importation
<= cd1
.mmodule
then
1649 # cd2 refines cd1; therefore we skip pd1
1653 else if cd2
.bound_mtype
.is_subtype
(mmodule
, null, cd1
.bound_mtype
) then
1654 # cd2 < cd1; therefore we skip pd1
1663 if res
.is_empty
then
1664 print
"All lost! {candidates.join(", ")}"
1665 # FIXME: should be abort!
1670 # Return the most specific definition in the linearization of `mtype`.
1672 # If you want to know the next properties in the linearization,
1673 # look at `MPropDef::lookup_next_definition`.
1675 # FIXME: the linearisation is still unspecified
1677 # REQUIRE: not mtype.need_anchor
1678 # REQUIRE: mtype.has_mproperty(mmodule, self)
1679 fun lookup_first_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
1681 return lookup_all_definitions
(mmodule
, mtype
).first
1684 # Return all definitions in a linearisation order
1685 # Most speficic first, most general last
1686 fun lookup_all_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1688 assert not mtype
.need_anchor
1689 if mtype
isa MNullableType then mtype
= mtype
.mtype
1691 var cache
= self.lookup_all_definitions_cache
[mmodule
, mtype
]
1692 if cache
!= null then return cache
1694 #print "select prop {mproperty} for {mtype} in {self}"
1695 # First, select all candidates
1696 var candidates
= new Array[MPROPDEF]
1697 for mpropdef
in self.mpropdefs
do
1698 # If the definition is not imported by the module, then skip
1699 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1700 # If the definition is not inherited by the type, then skip
1701 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1703 candidates
.add
(mpropdef
)
1705 # Fast track for only one candidate
1706 if candidates
.length
<= 1 then
1707 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1711 mmodule
.linearize_mpropdefs
(candidates
)
1712 candidates
= candidates
.reversed
1713 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1717 private var lookup_all_definitions_cache
: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1724 redef type MPROPDEF: MMethodDef
1726 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1731 # Is the property a constructor?
1732 # Warning, this property can be inherited by subclasses with or without being a constructor
1733 # therefore, you should use `is_init_for' the verify if the property is a legal constructor for a given class
1734 var is_init
: Bool writable = false
1736 # The the property a 'new' contructor?
1737 var is_new
: Bool writable = false
1739 # Is the property a legal constructor for a given class?
1740 # As usual, visibility is not considered.
1741 # FIXME not implemented
1742 fun is_init_for
(mclass
: MClass): Bool
1748 # A global attribute
1752 redef type MPROPDEF: MAttributeDef
1754 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1760 # A global virtual type
1761 class MVirtualTypeProp
1764 redef type MPROPDEF: MVirtualTypeDef
1766 init(intro_mclassdef
: MClassDef, name
: String, visibility
: MVisibility)
1771 # The formal type associated to the virtual type property
1772 var mvirtualtype
: MVirtualType = new MVirtualType(self)
1775 # A definition of a property (local property)
1777 # Unlike MProperty, a MPropDef is a local definition that belong to a
1778 # specific class definition (which belong to a specific module)
1779 abstract class MPropDef
1781 # The associated MProperty subclass.
1782 # the two specialization hierarchy are symmetric
1783 type MPROPERTY: MProperty
1786 type MPROPDEF: MPropDef
1788 # The origin of the definition
1789 var location
: Location
1791 # The class definition where the property definition is
1792 var mclassdef
: MClassDef
1794 # The associated global property
1795 var mproperty
: MPROPERTY
1797 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1799 self.mclassdef
= mclassdef
1800 self.mproperty
= mproperty
1801 self.location
= location
1802 mclassdef
.mpropdefs
.add
(self)
1803 mproperty
.mpropdefs
.add
(self)
1804 self.to_s
= "{mclassdef}#{mproperty}"
1807 # Internal name combining the module, the class and the property
1808 # Example: "mymodule#MyClass#mymethod"
1809 redef var to_s
: String
1811 # Is self the definition that introduce the property?
1812 fun is_intro
: Bool do return mproperty
.intro
== self
1814 # Return the next definition in linearization of `mtype`.
1816 # This method is used to determine what method is called by a super.
1818 # REQUIRE: not mtype.need_anchor
1819 fun lookup_next_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
1821 assert not mtype
.need_anchor
1823 var mpropdefs
= self.mproperty
.lookup_all_definitions
(mmodule
, mtype
)
1824 var i
= mpropdefs
.iterator
1825 while i
.is_ok
and i
.item
!= self do i
.next
1826 assert has_property
: i
.is_ok
1828 assert has_next_property
: i
.is_ok
1833 # A local definition of a method
1837 redef type MPROPERTY: MMethod
1838 redef type MPROPDEF: MMethodDef
1840 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1845 # The signature attached to the property definition
1846 var msignature
: nullable MSignature writable = null
1848 # The the method definition abstract?
1849 var is_abstract
: Bool writable = false
1852 # A local definition of an attribute
1856 redef type MPROPERTY: MAttribute
1857 redef type MPROPDEF: MAttributeDef
1859 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1864 # The static type of the attribute
1865 var static_mtype
: nullable MType writable = null
1868 # A local definition of a virtual type
1869 class MVirtualTypeDef
1872 redef type MPROPERTY: MVirtualTypeProp
1873 redef type MPROPDEF: MVirtualTypeDef
1875 init(mclassdef
: MClassDef, mproperty
: MPROPERTY, location
: Location)
1880 # The bound of the virtual type
1881 var bound
: nullable MType writable = null
1892 # Note this class is basically an enum.
1893 # FIXME: use a real enum once user-defined enums are available
1895 redef var to_s
: String
1897 # Is a constructor required?
1899 private init(s
: String, need_init
: Bool)
1902 self.need_init
= need_init
1906 fun abstract_kind
: MClassKind do return once
new MClassKind("abstract class", true)
1907 fun concrete_kind
: MClassKind do return once
new MClassKind("class", true)
1908 fun interface_kind
: MClassKind do return once
new MClassKind("interface", false)
1909 fun enum_kind
: MClassKind do return once
new MClassKind("enum", false)
1910 fun extern_kind
: MClassKind do return once
new MClassKind("extern", false)