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 # Classes, types and properties
19 # All three concepts are defined in this same module because these are strongly connected:
20 # * types are based on classes
21 # * classes contains properties
22 # * some properties are types (virtual types)
24 # TODO: liearization, extern stuff
25 # FIXME: better handling of the types
31 private import more_collections
35 var mclasses
= new Array[MClass]
37 # All known properties
38 var mproperties
= new Array[MProperty]
40 # Hierarchy of class definition.
42 # Each classdef is associated with its super-classdefs in regard to
43 # its module of definition.
44 var mclassdef_hierarchy
= new POSet[MClassDef]
46 # Class-type hierarchy restricted to the introduction.
48 # The idea is that what is true on introduction is always true whatever
49 # the module considered.
50 # Therefore, this hierarchy is used for a fast positive subtype check.
52 # This poset will evolve in a monotonous way:
53 # * Two non connected nodes will remain unconnected
54 # * New nodes can appear with new edges
55 private var intro_mtype_specialization_hierarchy
= new POSet[MClassType]
57 # Global overlapped class-type hierarchy.
58 # The hierarchy when all modules are combined.
59 # Therefore, this hierarchy is used for a fast negative subtype check.
61 # This poset will evolve in an anarchic way. Loops can even be created.
63 # FIXME decide what to do on loops
64 private var full_mtype_specialization_hierarchy
= new POSet[MClassType]
66 # Collections of classes grouped by their short name
67 private var mclasses_by_name
= new MultiHashMap[String, MClass]
69 # Return all class named `name`.
71 # If such a class does not exist, null is returned
72 # (instead of an empty array)
74 # Visibility or modules are not considered
75 fun get_mclasses_by_name
(name
: String): nullable Array[MClass]
77 if mclasses_by_name
.has_key
(name
) then
78 return mclasses_by_name
[name
]
84 # Collections of properties grouped by their short name
85 private var mproperties_by_name
= new MultiHashMap[String, MProperty]
87 # Return all properties named `name`.
89 # If such a property does not exist, null is returned
90 # (instead of an empty array)
92 # Visibility or modules are not considered
93 fun get_mproperties_by_name
(name
: String): nullable Array[MProperty]
95 if not mproperties_by_name
.has_key
(name
) then
98 return mproperties_by_name
[name
]
103 var null_type
= new MNullType(self)
105 # Build an ordered tree with from `concerns`
106 fun concerns_tree
(mconcerns
: Collection[MConcern]): ConcernsTree do
107 var seen
= new HashSet[MConcern]
108 var res
= new ConcernsTree
110 var todo
= new Array[MConcern]
111 todo
.add_all mconcerns
113 while not todo
.is_empty
do
115 if seen
.has
(c
) then continue
116 var pc
= c
.parent_concern
130 # An OrderedTree that can be easily refined for display purposes
132 super OrderedTree[MConcern]
136 # All the classes introduced in the module
137 var intro_mclasses
= new Array[MClass]
139 # All the class definitions of the module
140 # (introduction and refinement)
141 var mclassdefs
= new Array[MClassDef]
143 # Does the current module has a given class `mclass`?
144 # Return true if the mmodule introduces, refines or imports a class.
145 # Visibility is not considered.
146 fun has_mclass
(mclass
: MClass): Bool
148 return self.in_importation
<= mclass
.intro_mmodule
151 # Full hierarchy of introduced ans imported classes.
153 # Create a new hierarchy got by flattening the classes for the module
154 # and its imported modules.
155 # Visibility is not considered.
157 # Note: this function is expensive and is usually used for the main
158 # module of a program only. Do not use it to do you own subtype
160 fun flatten_mclass_hierarchy
: POSet[MClass]
162 var res
= self.flatten_mclass_hierarchy_cache
163 if res
!= null then return res
164 res
= new POSet[MClass]
165 for m
in self.in_importation
.greaters
do
166 for cd
in m
.mclassdefs
do
169 for s
in cd
.supertypes
do
170 res
.add_edge
(c
, s
.mclass
)
174 self.flatten_mclass_hierarchy_cache
= res
178 # Sort a given array of classes using the linearization order of the module
179 # The most general is first, the most specific is last
180 fun linearize_mclasses
(mclasses
: Array[MClass])
182 self.flatten_mclass_hierarchy
.sort
(mclasses
)
185 # Sort a given array of class definitions using the linearization order of the module
186 # the refinement link is stronger than the specialisation link
187 # The most general is first, the most specific is last
188 fun linearize_mclassdefs
(mclassdefs
: Array[MClassDef])
190 var sorter
= new MClassDefSorter(self)
191 sorter
.sort
(mclassdefs
)
194 # Sort a given array of property definitions using the linearization order of the module
195 # the refinement link is stronger than the specialisation link
196 # The most general is first, the most specific is last
197 fun linearize_mpropdefs
(mpropdefs
: Array[MPropDef])
199 var sorter
= new MPropDefSorter(self)
200 sorter
.sort
(mpropdefs
)
203 private var flatten_mclass_hierarchy_cache
: nullable POSet[MClass] = null
205 # The primitive type `Object`, the root of the class hierarchy
206 fun object_type
: MClassType
208 var res
= self.object_type_cache
209 if res
!= null then return res
210 res
= self.get_primitive_class
("Object").mclass_type
211 self.object_type_cache
= res
215 private var object_type_cache
: nullable MClassType
217 # The type `Pointer`, super class to all extern classes
218 var pointer_type
: MClassType = self.get_primitive_class
("Pointer").mclass_type
is lazy
220 # The primitive type `Bool`
221 fun bool_type
: MClassType
223 var res
= self.bool_type_cache
224 if res
!= null then return res
225 res
= self.get_primitive_class
("Bool").mclass_type
226 self.bool_type_cache
= res
230 private var bool_type_cache
: nullable MClassType
232 # The primitive type `Sys`, the main type of the program, if any
233 fun sys_type
: nullable MClassType
235 var clas
= self.model
.get_mclasses_by_name
("Sys")
236 if clas
== null then return null
237 return get_primitive_class
("Sys").mclass_type
240 # The primitive type `Finalizable`
241 # Used to tag classes that need to be finalized.
242 fun finalizable_type
: nullable MClassType
244 var clas
= self.model
.get_mclasses_by_name
("Finalizable")
245 if clas
== null then return null
246 return get_primitive_class
("Finalizable").mclass_type
249 # Force to get the primitive class named `name` or abort
250 fun get_primitive_class
(name
: String): MClass
252 var cla
= self.model
.get_mclasses_by_name
(name
)
254 if name
== "Bool" and self.model
.get_mclasses_by_name
("Object") != null then
255 # Bool is injected because it is needed by engine to code the result
256 # of the implicit casts.
257 var c
= new MClass(self, name
, null, enum_kind
, public_visibility
)
258 var cladef
= new MClassDef(self, c
.mclass_type
, new Location(null, 0,0,0,0))
259 cladef
.set_supertypes
([object_type
])
260 cladef
.add_in_hierarchy
263 print
("Fatal Error: no primitive class {name}")
266 if cla
.length
!= 1 then
267 var msg
= "Fatal Error: more than one primitive class {name}:"
268 for c
in cla
do msg
+= " {c.full_name}"
275 # Try to get the primitive method named `name` on the type `recv`
276 fun try_get_primitive_method
(name
: String, recv
: MClass): nullable MMethod
278 var props
= self.model
.get_mproperties_by_name
(name
)
279 if props
== null then return null
280 var res
: nullable MMethod = null
281 for mprop
in props
do
282 assert mprop
isa MMethod
283 var intro
= mprop
.intro_mclassdef
284 for mclassdef
in recv
.mclassdefs
do
285 if not self.in_importation
.greaters
.has
(mclassdef
.mmodule
) then continue
286 if not mclassdef
.in_hierarchy
.greaters
.has
(intro
) then continue
289 else if res
!= mprop
then
290 print
("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
299 private class MClassDefSorter
301 redef type COMPARED: MClassDef
303 redef fun compare
(a
, b
)
307 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
308 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
312 private class MPropDefSorter
314 redef type COMPARED: MPropDef
316 redef fun compare
(pa
, pb
)
322 if ca
!= cb
then return mmodule
.flatten_mclass_hierarchy
.compare
(ca
, cb
)
323 return mmodule
.model
.mclassdef_hierarchy
.compare
(a
, b
)
329 # `MClass` are global to the model; it means that a `MClass` is not bound to a
330 # specific `MModule`.
332 # This characteristic helps the reasoning about classes in a program since a
333 # single `MClass` object always denote the same class.
335 # The drawback is that classes (`MClass`) contain almost nothing by themselves.
336 # These do not really have properties nor belong to a hierarchy since the property and the
337 # hierarchy of a class depends of the refinement in the modules.
339 # Most services on classes require the precision of a module, and no one can asks what are
340 # the super-classes of a class nor what are properties of a class without precising what is
341 # the module considered.
343 # For instance, during the typing of a source-file, the module considered is the module of the file.
344 # eg. the question *is the method `foo` exists in the class `Bar`?* must be reformulated into
345 # *is the method `foo` exists in the class `Bar` in the current module?*
347 # During some global analysis, the module considered may be the main module of the program.
351 # The module that introduce the class
352 # While classes are not bound to a specific module,
353 # the introducing module is used for naming an visibility
354 var intro_mmodule
: MModule
356 # The short name of the class
357 # In Nit, the name of a class cannot evolve in refinements
358 redef var name
: String
360 # The canonical name of the class
362 # It is the name of the class prefixed by the full_name of the `intro_mmodule`
363 # Example: `"owner::module::MyClass"`
364 redef var full_name
is lazy
do return "{self.intro_mmodule.full_name}::{name}"
366 redef var c_name
is lazy
do return "{intro_mmodule.c_name}__{name.to_cmangle}"
368 # The number of generic formal parameters
369 # 0 if the class is not generic
370 var arity
: Int is noinit
372 # Each generic formal parameters in order.
373 # is empty if the class is not generic
374 var mparameters
= new Array[MParameterType]
376 protected fun setup_parameter_names
(parameter_names
: nullable Array[String]) is
379 if parameter_names
== null then
382 self.arity
= parameter_names
.length
385 # Create the formal parameter types
387 assert parameter_names
!= null
388 var mparametertypes
= new Array[MParameterType]
389 for i
in [0..arity
[ do
390 var mparametertype
= new MParameterType(self, i
, parameter_names
[i
])
391 mparametertypes
.add
(mparametertype
)
393 self.mparameters
= mparametertypes
394 var mclass_type
= new MGenericType(self, mparametertypes
)
395 self.mclass_type
= mclass_type
396 self.get_mtype_cache
[mparametertypes
] = mclass_type
398 self.mclass_type
= new MClassType(self)
402 # The kind of the class (interface, abstract class, etc.)
403 # In Nit, the kind of a class cannot evolve in refinements
406 # The visibility of the class
407 # In Nit, the visibility of a class cannot evolve in refinements
408 var visibility
: MVisibility
412 intro_mmodule
.intro_mclasses
.add
(self)
413 var model
= intro_mmodule
.model
414 model
.mclasses_by_name
.add_one
(name
, self)
415 model
.mclasses
.add
(self)
418 redef fun model
do return intro_mmodule
.model
420 # All class definitions (introduction and refinements)
421 var mclassdefs
= new Array[MClassDef]
424 redef fun to_s
do return self.name
426 # The definition that introduces the class.
428 # Warning: such a definition may not exist in the early life of the object.
429 # In this case, the method will abort.
430 var intro
: MClassDef is noinit
432 # Return the class `self` in the class hierarchy of the module `mmodule`.
434 # SEE: `MModule::flatten_mclass_hierarchy`
435 # REQUIRE: `mmodule.has_mclass(self)`
436 fun in_hierarchy
(mmodule
: MModule): POSetElement[MClass]
438 return mmodule
.flatten_mclass_hierarchy
[self]
441 # The principal static type of the class.
443 # For non-generic class, mclass_type is the only `MClassType` based
446 # For a generic class, the arguments are the formal parameters.
447 # i.e.: for the class Array[E:Object], the `mclass_type` is Array[E].
448 # If you want Array[Object] the see `MClassDef::bound_mtype`
450 # For generic classes, the mclass_type is also the way to get a formal
451 # generic parameter type.
453 # To get other types based on a generic class, see `get_mtype`.
455 # ENSURE: `mclass_type.mclass == self`
456 var mclass_type
: MClassType is noinit
458 # Return a generic type based on the class
459 # Is the class is not generic, then the result is `mclass_type`
461 # REQUIRE: `mtype_arguments.length == self.arity`
462 fun get_mtype
(mtype_arguments
: Array[MType]): MClassType
464 assert mtype_arguments
.length
== self.arity
465 if self.arity
== 0 then return self.mclass_type
466 var res
= get_mtype_cache
.get_or_null
(mtype_arguments
)
467 if res
!= null then return res
468 res
= new MGenericType(self, mtype_arguments
)
469 self.get_mtype_cache
[mtype_arguments
.to_a
] = res
473 private var get_mtype_cache
= new HashMap[Array[MType], MGenericType]
477 # A definition (an introduction or a refinement) of a class in a module
479 # A `MClassDef` is associated with an explicit (or almost) definition of a
480 # class. Unlike `MClass`, a `MClassDef` is a local definition that belong to
481 # a specific class and a specific module, and contains declarations like super-classes
484 # It is the class definitions that are the backbone of most things in the model:
485 # ClassDefs are defined with regard with other classdefs.
486 # Refinement and specialization are combined to produce a big poset called the `Model::mclassdef_hierarchy`.
488 # Moreover, the extension and the intention of types is defined by looking at the MClassDefs.
492 # The module where the definition is
495 # The associated `MClass`
496 var mclass
: MClass is noinit
498 # The bounded type associated to the mclassdef
500 # For a non-generic class, `bound_mtype` and `mclass.mclass_type`
504 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
505 # If you want Array[E], then see `mclass.mclass_type`
507 # ENSURE: `bound_mtype.mclass == self.mclass`
508 var bound_mtype
: MClassType
510 # The origin of the definition
511 var location
: Location
513 # Internal name combining the module and the class
514 # Example: "mymodule#MyClass"
515 redef var to_s
: String is noinit
519 self.mclass
= bound_mtype
.mclass
520 mmodule
.mclassdefs
.add
(self)
521 mclass
.mclassdefs
.add
(self)
522 if mclass
.intro_mmodule
== mmodule
then
523 assert not isset mclass
._intro
526 self.to_s
= "{mmodule}#{mclass}"
529 # Actually the name of the `mclass`
530 redef fun name
do return mclass
.name
532 # The module and class name separated by a '#'.
534 # The short-name of the class is used for introduction.
535 # Example: "my_module#MyClass"
537 # The full-name of the class is used for refinement.
538 # Example: "my_module#intro_module::MyClass"
539 redef var full_name
is lazy
do
541 return "{mmodule.full_name}#{mclass.name}"
543 return "{mmodule.full_name}#{mclass.full_name}"
547 redef var c_name
is lazy
do
551 return "{mmodule.c_name}__{mclass.c_name.to_cmangle}"
555 redef fun model
do return mmodule
.model
557 # All declared super-types
558 # FIXME: quite ugly but not better idea yet
559 var supertypes
= new Array[MClassType]
561 # Register some super-types for the class (ie "super SomeType")
563 # The hierarchy must not already be set
564 # REQUIRE: `self.in_hierarchy == null`
565 fun set_supertypes
(supertypes
: Array[MClassType])
567 assert unique_invocation
: self.in_hierarchy
== null
568 var mmodule
= self.mmodule
569 var model
= mmodule
.model
570 var mtype
= self.bound_mtype
572 for supertype
in supertypes
do
573 self.supertypes
.add
(supertype
)
575 # Register in full_type_specialization_hierarchy
576 model
.full_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
577 # Register in intro_type_specialization_hierarchy
578 if mclass
.intro_mmodule
== mmodule
and supertype
.mclass
.intro_mmodule
== mmodule
then
579 model
.intro_mtype_specialization_hierarchy
.add_edge
(mtype
, supertype
)
585 # Collect the super-types (set by set_supertypes) to build the hierarchy
587 # This function can only invoked once by class
588 # REQUIRE: `self.in_hierarchy == null`
589 # ENSURE: `self.in_hierarchy != null`
592 assert unique_invocation
: self.in_hierarchy
== null
593 var model
= mmodule
.model
594 var res
= model
.mclassdef_hierarchy
.add_node
(self)
595 self.in_hierarchy
= res
596 var mtype
= self.bound_mtype
598 # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
599 # The simpliest way is to attach it to collect_mclassdefs
600 for mclassdef
in mtype
.collect_mclassdefs
(mmodule
) do
601 res
.poset
.add_edge
(self, mclassdef
)
605 # The view of the class definition in `mclassdef_hierarchy`
606 var in_hierarchy
: nullable POSetElement[MClassDef] = null
608 # Is the definition the one that introduced `mclass`?
609 fun is_intro
: Bool do return mclass
.intro
== self
611 # All properties introduced by the classdef
612 var intro_mproperties
= new Array[MProperty]
614 # All property definitions in the class (introductions and redefinitions)
615 var mpropdefs
= new Array[MPropDef]
618 # A global static type
620 # MType are global to the model; it means that a `MType` is not bound to a
621 # specific `MModule`.
622 # This characteristic helps the reasoning about static types in a program
623 # since a single `MType` object always denote the same type.
625 # However, because a `MType` is global, it does not really have properties
626 # nor have subtypes to a hierarchy since the property and the class hierarchy
627 # depends of a module.
628 # Moreover, virtual types an formal generic parameter types also depends on
629 # a receiver to have sense.
631 # Therefore, most method of the types require a module and an anchor.
632 # The module is used to know what are the classes and the specialization
634 # The anchor is used to know what is the bound of the virtual types and formal
635 # generic parameter types.
637 # MType are not directly usable to get properties. See the `anchor_to` method
638 # and the `MClassType` class.
640 # FIXME: the order of the parameters is not the best. We mus pick on from:
641 # * foo(mmodule, anchor, othertype)
642 # * foo(othertype, anchor, mmodule)
643 # * foo(anchor, mmodule, othertype)
644 # * foo(othertype, mmodule, anchor)
648 redef fun name
do return to_s
650 # Return true if `self` is an subtype of `sup`.
651 # The typing is done using the standard typing policy of Nit.
653 # REQUIRE: `anchor == null implies not self.need_anchor and not sup.need_anchor`
654 # REQUIRE: `anchor != null implies self.can_resolve_for(anchor, null, mmodule) and sup.can_resolve_for(anchor, null, mmodule)`
655 fun is_subtype
(mmodule
: MModule, anchor
: nullable MClassType, sup
: MType): Bool
658 if sub
== sup
then return true
660 #print "1.is {sub} a {sup}? ===="
662 if anchor
== null then
663 assert not sub
.need_anchor
664 assert not sup
.need_anchor
666 # First, resolve the formal types to the simplest equivalent forms in the receiver
667 assert sub
.can_resolve_for
(anchor
, null, mmodule
)
668 sub
= sub
.lookup_fixed
(mmodule
, anchor
)
669 assert sup
.can_resolve_for
(anchor
, null, mmodule
)
670 sup
= sup
.lookup_fixed
(mmodule
, anchor
)
673 # Does `sup` accept null or not?
674 # Discard the nullable marker if it exists
675 var sup_accept_null
= false
676 if sup
isa MNullableType then
677 sup_accept_null
= true
679 else if sup
isa MNullType then
680 sup_accept_null
= true
683 # Can `sub` provide null or not?
684 # Thus we can match with `sup_accept_null`
685 # Also discard the nullable marker if it exists
686 if sub
isa MNullableType then
687 if not sup_accept_null
then return false
689 else if sub
isa MNullType then
690 return sup_accept_null
692 # Now the case of direct null and nullable is over.
694 # If `sub` is a formal type, then it is accepted if its bound is accepted
695 while sub
isa MParameterType or sub
isa MVirtualType do
696 #print "3.is {sub} a {sup}?"
698 # A unfixed formal type can only accept itself
699 if sub
== sup
then return true
701 assert anchor
!= null
702 sub
= sub
.lookup_bound
(mmodule
, anchor
)
704 #print "3.is {sub} a {sup}?"
706 # Manage the second layer of null/nullable
707 if sub
isa MNullableType then
708 if not sup_accept_null
then return false
710 else if sub
isa MNullType then
711 return sup_accept_null
714 #print "4.is {sub} a {sup}? <- no more resolution"
716 assert sub
isa MClassType # It is the only remaining type
718 # A unfixed formal type can only accept itself
719 if sup
isa MParameterType or sup
isa MVirtualType then
723 if sup
isa MNullType then
724 # `sup` accepts only null
728 assert sup
isa MClassType # It is the only remaining type
730 # Now both are MClassType, we need to dig
732 if sub
== sup
then return true
734 if anchor
== null then anchor
= sub
# UGLY: any anchor will work
735 var resolved_sub
= sub
.anchor_to
(mmodule
, anchor
)
736 var res
= resolved_sub
.collect_mclasses
(mmodule
).has
(sup
.mclass
)
737 if res
== false then return false
738 if not sup
isa MGenericType then return true
739 var sub2
= sub
.supertype_to
(mmodule
, anchor
, sup
.mclass
)
740 assert sub2
.mclass
== sup
.mclass
741 for i
in [0..sup
.mclass
.arity
[ do
742 var sub_arg
= sub2
.arguments
[i
]
743 var sup_arg
= sup
.arguments
[i
]
744 res
= sub_arg
.is_subtype
(mmodule
, anchor
, sup_arg
)
745 if res
== false then return false
750 # The base class type on which self is based
752 # This base type is used to get property (an internally to perform
753 # unsafe type comparison).
755 # Beware: some types (like null) are not based on a class thus this
758 # Basically, this function transform the virtual types and parameter
759 # types to their bounds.
764 # class B super A end
766 # class Y super X end
775 # Map[T,U] anchor_to H #-> Map[B,Y]
777 # Explanation of the example:
778 # In H, T is set to B, because "H super G[B]", and U is bound to Y,
779 # because "redef type U: Y". Therefore, Map[T, U] is bound to
782 # ENSURE: `not self.need_anchor implies result == self`
783 # ENSURE: `not result.need_anchor`
784 fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MType
786 if not need_anchor
then return self
787 assert not anchor
.need_anchor
788 # Just resolve to the anchor and clear all the virtual types
789 var res
= self.resolve_for
(anchor
, null, mmodule
, true)
790 assert not res
.need_anchor
794 # Does `self` contain a virtual type or a formal generic parameter type?
795 # In order to remove those types, you usually want to use `anchor_to`.
796 fun need_anchor
: Bool do return true
798 # Return the supertype when adapted to a class.
800 # In Nit, for each super-class of a type, there is a equivalent super-type.
806 # class H[V] super G[V, Bool] end
808 # H[Int] supertype_to G #-> G[Int, Bool]
811 # REQUIRE: `super_mclass` is a super-class of `self`
812 # REQUIRE: `self.need_anchor implies anchor != null and self.can_resolve_for(anchor, null, mmodule)`
813 # ENSURE: `result.mclass = super_mclass`
814 fun supertype_to
(mmodule
: MModule, anchor
: nullable MClassType, super_mclass
: MClass): MClassType
816 if super_mclass
.arity
== 0 then return super_mclass
.mclass_type
817 if self isa MClassType and self.mclass
== super_mclass
then return self
819 if self.need_anchor
then
820 assert anchor
!= null
821 resolved_self
= self.anchor_to
(mmodule
, anchor
)
825 var supertypes
= resolved_self
.collect_mtypes
(mmodule
)
826 for supertype
in supertypes
do
827 if supertype
.mclass
== super_mclass
then
828 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
829 return supertype
.resolve_for
(self, anchor
, mmodule
, false)
835 # Replace formals generic types in self with resolved values in `mtype`
836 # If `cleanup_virtual` is true, then virtual types are also replaced
839 # This function returns self if `need_anchor` is false.
845 # class H[F] super G[F] end
849 # * Array[E].resolve_for(H[Int]) #-> Array[Int]
850 # * Array[E].resolve_for(G[Z], X[Int]) #-> Array[Z]
852 # Explanation of the example:
853 # * Array[E].need_anchor is true because there is a formal generic parameter type E
854 # * E makes sense for H[Int] because E is a formal parameter of G and H specialize G
855 # * Since "H[F] super G[F]", E is in fact F for H
856 # * More specifically, in H[Int], E is Int
857 # * So, in H[Int], Array[E] is Array[Int]
859 # This function is mainly used to inherit a signature.
860 # Because, unlike `anchor_to`, we do not want a full resolution of
861 # a type but only an adapted version of it.
867 # fun foo(e:E):E is abstract
869 # class B super A[Int] end
872 # The signature on foo is (e: E): E
873 # If we resolve the signature for B, we get (e:Int):Int
879 # fun foo(e:E):E is abstract
883 # fun bar do a.foo(x) # <- x is here
887 # The first question is: is foo available on `a`?
889 # The static type of a is `A[Array[F]]`, that is an open type.
890 # in order to find a method `foo`, whe must look at a resolved type.
892 # A[Array[F]].anchor_to(C[nullable Object]) #-> A[Array[nullable Object]]
894 # the method `foo` exists in `A[Array[nullable Object]]`, therefore `foo` exists for `a`.
896 # The next question is: what is the accepted types for `x`?
898 # the signature of `foo` is `foo(e:E)`, thus we must resolve the type E
900 # E.resolve_for(A[Array[F]],C[nullable Object]) #-> Array[F]
902 # The resolution can be done because `E` make sense for the class A (see `can_resolve_for`)
904 # FIXME: the parameter `cleanup_virtual` is just a bad idea, but having
905 # two function instead of one seems also to be a bad idea.
907 # REQUIRE: `can_resolve_for(mtype, anchor, mmodule)`
908 # ENSURE: `not self.need_anchor implies result == self`
909 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MType is abstract
911 # Resolve formal type to its verbatim bound.
912 # If the type is not formal, just return self
914 # The result is returned exactly as declared in the "type" property (verbatim).
915 # So it could be another formal type.
917 # In case of conflict, the method aborts.
918 fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType do return self
920 # Resolve the formal type to its simplest equivalent form.
922 # Formal types are either free or fixed.
923 # When it is fixed, it means that it is equivalent with a simpler type.
924 # When a formal type is free, it means that it is only equivalent with itself.
925 # This method return the most simple equivalent type of `self`.
927 # This method is mainly used for subtype test in order to sanely compare fixed.
929 # By default, return self.
930 # See the redefinitions for specific behavior in each kind of type.
931 fun lookup_fixed
(mmodule
: MModule, resolved_receiver
: MType): MType do return self
933 # Can the type be resolved?
935 # In order to resolve open types, the formal types must make sence.
945 # E.can_resolve_for(A[Int]) #-> true, E make sense in A
947 # E.can_resolve_for(B[Int]) #-> false, E does not make sense in B
949 # B[E].can_resolve_for(A[F], B[Object]) #-> true,
950 # # B[E] is a red hearing only the E is important,
951 # # E make sense in A
954 # REQUIRE: `anchor != null implies not anchor.need_anchor`
955 # REQUIRE: `mtype.need_anchor implies anchor != null and mtype.can_resolve_for(anchor, null, mmodule)`
956 # ENSURE: `not self.need_anchor implies result == true`
957 fun can_resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule): Bool is abstract
959 # Return the nullable version of the type
960 # If the type is already nullable then self is returned
961 fun as_nullable
: MType
963 var res
= self.as_nullable_cache
964 if res
!= null then return res
965 res
= new MNullableType(self)
966 self.as_nullable_cache
= res
970 # Return the not nullable version of the type
971 # Is the type is already not nullable, then self is returned.
973 # Note: this just remove the `nullable` notation, but the result can still contains null.
974 # For instance if `self isa MNullType` or self is a formal type bounded by a nullable type.
975 fun as_notnullable
: MType
980 private var as_nullable_cache
: nullable MType = null
983 # The depth of the type seen as a tree.
990 # Formal types have a depth of 1.
996 # The length of the type seen as a tree.
1003 # Formal types have a length of 1.
1009 # Compute all the classdefs inherited/imported.
1010 # The returned set contains:
1011 # * the class definitions from `mmodule` and its imported modules
1012 # * the class definitions of this type and its super-types
1014 # This function is used mainly internally.
1016 # REQUIRE: `not self.need_anchor`
1017 fun collect_mclassdefs
(mmodule
: MModule): Set[MClassDef] is abstract
1019 # Compute all the super-classes.
1020 # This function is used mainly internally.
1022 # REQUIRE: `not self.need_anchor`
1023 fun collect_mclasses
(mmodule
: MModule): Set[MClass] is abstract
1025 # Compute all the declared super-types.
1026 # Super-types are returned as declared in the classdefs (verbatim).
1027 # This function is used mainly internally.
1029 # REQUIRE: `not self.need_anchor`
1030 fun collect_mtypes
(mmodule
: MModule): Set[MClassType] is abstract
1032 # Is the property in self for a given module
1033 # This method does not filter visibility or whatever
1035 # REQUIRE: `not self.need_anchor`
1036 fun has_mproperty
(mmodule
: MModule, mproperty
: MProperty): Bool
1038 assert not self.need_anchor
1039 return self.collect_mclassdefs
(mmodule
).has
(mproperty
.intro_mclassdef
)
1043 # A type based on a class.
1045 # `MClassType` have properties (see `has_mproperty`).
1049 # The associated class
1052 redef fun model
do return self.mclass
.intro_mmodule
.model
1054 # TODO: private init because strongly bounded to its mclass. see `mclass.mclass_type`
1056 # The formal arguments of the type
1057 # ENSURE: `result.length == self.mclass.arity`
1058 var arguments
= new Array[MType]
1060 redef fun to_s
do return mclass
.to_s
1062 redef fun full_name
do return mclass
.full_name
1064 redef fun c_name
do return mclass
.c_name
1066 redef fun need_anchor
do return false
1068 redef fun anchor_to
(mmodule
: MModule, anchor
: MClassType): MClassType
1070 return super.as(MClassType)
1073 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MClassType do return self
1075 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
1077 redef fun collect_mclassdefs
(mmodule
)
1079 assert not self.need_anchor
1080 var cache
= self.collect_mclassdefs_cache
1081 if not cache
.has_key
(mmodule
) then
1082 self.collect_things
(mmodule
)
1084 return cache
[mmodule
]
1087 redef fun collect_mclasses
(mmodule
)
1089 if collect_mclasses_last_module
== mmodule
then return collect_mclasses_last_module_cache
1090 assert not self.need_anchor
1091 var cache
= self.collect_mclasses_cache
1092 if not cache
.has_key
(mmodule
) then
1093 self.collect_things
(mmodule
)
1095 var res
= cache
[mmodule
]
1096 collect_mclasses_last_module
= mmodule
1097 collect_mclasses_last_module_cache
= res
1101 private var collect_mclasses_last_module
: nullable MModule = null
1102 private var collect_mclasses_last_module_cache
: Set[MClass] is noinit
1104 redef fun collect_mtypes
(mmodule
)
1106 assert not self.need_anchor
1107 var cache
= self.collect_mtypes_cache
1108 if not cache
.has_key
(mmodule
) then
1109 self.collect_things
(mmodule
)
1111 return cache
[mmodule
]
1114 # common implementation for `collect_mclassdefs`, `collect_mclasses`, and `collect_mtypes`.
1115 private fun collect_things
(mmodule
: MModule)
1117 var res
= new HashSet[MClassDef]
1118 var seen
= new HashSet[MClass]
1119 var types
= new HashSet[MClassType]
1120 seen
.add
(self.mclass
)
1121 var todo
= [self.mclass
]
1122 while not todo
.is_empty
do
1123 var mclass
= todo
.pop
1124 #print "process {mclass}"
1125 for mclassdef
in mclass
.mclassdefs
do
1126 if not mmodule
.in_importation
<= mclassdef
.mmodule
then continue
1127 #print " process {mclassdef}"
1129 for supertype
in mclassdef
.supertypes
do
1130 types
.add
(supertype
)
1131 var superclass
= supertype
.mclass
1132 if seen
.has
(superclass
) then continue
1133 #print " add {superclass}"
1134 seen
.add
(superclass
)
1135 todo
.add
(superclass
)
1139 collect_mclassdefs_cache
[mmodule
] = res
1140 collect_mclasses_cache
[mmodule
] = seen
1141 collect_mtypes_cache
[mmodule
] = types
1144 private var collect_mclassdefs_cache
= new HashMap[MModule, Set[MClassDef]]
1145 private var collect_mclasses_cache
= new HashMap[MModule, Set[MClass]]
1146 private var collect_mtypes_cache
= new HashMap[MModule, Set[MClassType]]
1150 # A type based on a generic class.
1151 # A generic type a just a class with additional formal generic arguments.
1157 # TODO: private init because strongly bounded to its mclass. see `mclass.get_mtype`
1161 assert self.mclass
.arity
== arguments
.length
1163 self.need_anchor
= false
1164 for t
in arguments
do
1165 if t
.need_anchor
then
1166 self.need_anchor
= true
1171 self.to_s
= "{mclass}[{arguments.join(", ")}]"
1174 # The short-name of the class, then the full-name of each type arguments within brackets.
1175 # Example: `"Map[String, List[Int]]"`
1176 redef var to_s
: String is noinit
1178 # The full-name of the class, then the full-name of each type arguments within brackets.
1179 # Example: `"standard::Map[standard::String, standard::List[standard::Int]]"`
1180 redef var full_name
is lazy
do
1181 var args
= new Array[String]
1182 for t
in arguments
do
1183 args
.add t
.full_name
1185 return "{mclass.full_name}[{args.join(", ")}]}"
1188 redef var c_name
is lazy
do
1189 var res
= mclass
.c_name
1190 # Note: because the arity is known, a prefix notation is enough
1191 for t
in arguments
do
1198 redef var need_anchor
: Bool is noinit
1200 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1202 if not need_anchor
then return self
1203 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1204 var types
= new Array[MType]
1205 for t
in arguments
do
1206 types
.add
(t
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1208 return mclass
.get_mtype
(types
)
1211 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1213 if not need_anchor
then return true
1214 for t
in arguments
do
1215 if not t
.can_resolve_for
(mtype
, anchor
, mmodule
) then return false
1224 for a
in self.arguments
do
1226 if d
> dmax
then dmax
= d
1234 for a
in self.arguments
do
1241 # A virtual formal type.
1245 # The property associated with the type.
1246 # Its the definitions of this property that determine the bound or the virtual type.
1247 var mproperty
: MVirtualTypeProp
1249 redef fun model
do return self.mproperty
.intro_mclassdef
.mmodule
.model
1251 redef fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1253 return lookup_single_definition
(mmodule
, resolved_receiver
).bound
.as(not null)
1256 private fun lookup_single_definition
(mmodule
: MModule, resolved_receiver
: MType): MVirtualTypeDef
1258 assert not resolved_receiver
.need_anchor
1259 var props
= self.mproperty
.lookup_definitions
(mmodule
, resolved_receiver
)
1260 if props
.is_empty
then
1262 else if props
.length
== 1 then
1265 var types
= new ArraySet[MType]
1266 var res
= props
.first
1268 types
.add
(p
.bound
.as(not null))
1269 if not res
.is_fixed
then res
= p
1271 if types
.length
== 1 then
1277 # A VT is fixed when:
1278 # * the VT is (re-)defined with the annotation `is fixed`
1279 # * the VT is (indirectly) bound to an enum class (see `enum_kind`) since there is no subtype possible
1280 # * the receiver is an enum class since there is no subtype possible
1281 redef fun lookup_fixed
(mmodule
: MModule, resolved_receiver
: MType): MType
1283 assert not resolved_receiver
.need_anchor
1284 resolved_receiver
= resolved_receiver
.as_notnullable
1285 assert resolved_receiver
isa MClassType # It is the only remaining type
1287 var prop
= lookup_single_definition
(mmodule
, resolved_receiver
)
1288 var res
= prop
.bound
.as(not null)
1290 # Recursively lookup the fixed result
1291 res
= res
.lookup_fixed
(mmodule
, resolved_receiver
)
1293 # 1. For a fixed VT, return the resolved bound
1294 if prop
.is_fixed
then return res
1296 # 2. For a enum boud, return the bound
1297 if res
isa MClassType and res
.mclass
.kind
== enum_kind
then return res
1299 # 3. for a enum receiver return the bound
1300 if resolved_receiver
.mclass
.kind
== enum_kind
then return res
1305 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1307 if not cleanup_virtual
then return self
1308 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1309 # self is a virtual type declared (or inherited) in mtype
1310 # The point of the function it to get the bound of the virtual type that make sense for mtype
1311 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1312 #print "{class_name}: {self}/{mtype}/{anchor}?"
1313 var resolved_receiver
1314 if mtype
.need_anchor
then
1315 assert anchor
!= null
1316 resolved_receiver
= mtype
.resolve_for
(anchor
, null, mmodule
, true)
1318 resolved_receiver
= mtype
1320 # Now, we can get the bound
1321 var verbatim_bound
= lookup_bound
(mmodule
, resolved_receiver
)
1322 # The bound is exactly as declared in the "type" property, so we must resolve it again
1323 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1328 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1330 if mtype
.need_anchor
then
1331 assert anchor
!= null
1332 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1334 return mtype
.has_mproperty
(mmodule
, mproperty
)
1337 redef fun to_s
do return self.mproperty
.to_s
1339 redef fun full_name
do return self.mproperty
.full_name
1341 redef fun c_name
do return self.mproperty
.c_name
1344 # The type associated to a formal parameter generic type of a class
1346 # Each parameter type is associated to a specific class.
1347 # It means that all refinements of a same class "share" the parameter type,
1348 # but that a generic subclass has its own parameter types.
1350 # However, in the sense of the meta-model, a parameter type of a class is
1351 # a valid type in a subclass. The "in the sense of the meta-model" is
1352 # important because, in the Nit language, the programmer cannot refers
1353 # directly to the parameter types of the super-classes.
1358 # fun e: E is abstract
1364 # In the class definition B[F], `F` is a valid type but `E` is not.
1365 # However, `self.e` is a valid method call, and the signature of `e` is
1368 # Note that parameter types are shared among class refinements.
1369 # Therefore parameter only have an internal name (see `to_s` for details).
1370 class MParameterType
1373 # The generic class where the parameter belong
1376 redef fun model
do return self.mclass
.intro_mmodule
.model
1378 # The position of the parameter (0 for the first parameter)
1379 # FIXME: is `position` a better name?
1384 redef fun to_s
do return name
1386 redef var full_name
is lazy
do return "{mclass.full_name}::{name}"
1388 redef var c_name
is lazy
do return mclass
.c_name
+ "__" + "#{name}".to_cmangle
1390 redef fun lookup_bound
(mmodule
: MModule, resolved_receiver
: MType): MType
1392 assert not resolved_receiver
.need_anchor
1393 resolved_receiver
= resolved_receiver
.as_notnullable
1394 assert resolved_receiver
isa MClassType # It is the only remaining type
1395 var goalclass
= self.mclass
1396 if resolved_receiver
.mclass
== goalclass
then
1397 return resolved_receiver
.arguments
[self.rank
]
1399 var supertypes
= resolved_receiver
.collect_mtypes
(mmodule
)
1400 for t
in supertypes
do
1401 if t
.mclass
== goalclass
then
1402 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
1403 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
1404 var res
= t
.arguments
[self.rank
]
1411 # A PT is fixed when:
1412 # * Its bound is a enum class (see `enum_kind`).
1413 # The PT is just useless, but it is still a case.
1414 # * More usually, the `resolved_receiver` is a subclass of `self.mclass`,
1415 # so it is necessarily fixed in a `super` clause, either with a normal type
1416 # or with another PT.
1417 # See `resolve_for` for examples about related issues.
1418 redef fun lookup_fixed
(mmodule
: MModule, resolved_receiver
: MType): MType
1420 assert not resolved_receiver
.need_anchor
1421 resolved_receiver
= resolved_receiver
.as_notnullable
1422 assert resolved_receiver
isa MClassType # It is the only remaining type
1423 var res
= self.resolve_for
(resolved_receiver
.mclass
.mclass_type
, resolved_receiver
, mmodule
, false)
1427 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1429 assert can_resolve_for
(mtype
, anchor
, mmodule
)
1430 #print "{class_name}: {self}/{mtype}/{anchor}?"
1432 if mtype
isa MGenericType and mtype
.mclass
== self.mclass
then
1433 var res
= mtype
.arguments
[self.rank
]
1434 if anchor
!= null and res
.need_anchor
then
1435 # Maybe the result can be resolved more if are bound to a final class
1436 var r2
= res
.anchor_to
(mmodule
, anchor
)
1437 if r2
isa MClassType and r2
.mclass
.kind
== enum_kind
then return r2
1442 # self is a parameter type of mtype (or of a super-class of mtype)
1443 # The point of the function it to get the bound of the virtual type that make sense for mtype
1444 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1445 # FIXME: What happens here is far from clear. Thus this part must be validated and clarified
1446 var resolved_receiver
1447 if mtype
.need_anchor
then
1448 assert anchor
!= null
1449 resolved_receiver
= mtype
.resolve_for
(anchor
.mclass
.mclass_type
, anchor
, mmodule
, true)
1451 resolved_receiver
= mtype
1453 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1454 if resolved_receiver
isa MParameterType then
1455 assert resolved_receiver
.mclass
== anchor
.mclass
1456 resolved_receiver
= anchor
.arguments
[resolved_receiver
.rank
]
1457 if resolved_receiver
isa MNullableType then resolved_receiver
= resolved_receiver
.mtype
1459 assert resolved_receiver
isa MClassType # It is the only remaining type
1461 # Eh! The parameter is in the current class.
1462 # So we return the corresponding argument, no mater what!
1463 if resolved_receiver
.mclass
== self.mclass
then
1464 var res
= resolved_receiver
.arguments
[self.rank
]
1465 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
1469 if resolved_receiver
.need_anchor
then
1470 assert anchor
!= null
1471 resolved_receiver
= resolved_receiver
.resolve_for
(anchor
, null, mmodule
, false)
1473 # Now, we can get the bound
1474 var verbatim_bound
= lookup_bound
(mmodule
, resolved_receiver
)
1475 # The bound is exactly as declared in the "type" property, so we must resolve it again
1476 var res
= verbatim_bound
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1478 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
1483 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1485 if mtype
.need_anchor
then
1486 assert anchor
!= null
1487 mtype
= mtype
.anchor_to
(mmodule
, anchor
)
1489 return mtype
.collect_mclassdefs
(mmodule
).has
(mclass
.intro
)
1493 # A type prefixed with "nullable"
1497 # The base type of the nullable type
1500 redef fun model
do return self.mtype
.model
1504 self.to_s
= "nullable {mtype}"
1507 redef var to_s
: String is noinit
1509 redef var full_name
is lazy
do return "nullable {mtype.full_name}"
1511 redef var c_name
is lazy
do return "nullable__{mtype.c_name}"
1513 redef fun need_anchor
do return mtype
.need_anchor
1514 redef fun as_nullable
do return self
1515 redef fun as_notnullable
do return mtype
1516 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1518 var res
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1519 return res
.as_nullable
1522 redef fun can_resolve_for
(mtype
, anchor
, mmodule
)
1524 return self.mtype
.can_resolve_for
(mtype
, anchor
, mmodule
)
1527 # Efficiently returns `mtype.lookup_fixed(mmodule, resolved_receiver).as_nullable`
1528 redef fun lookup_fixed
(mmodule
, resolved_receiver
)
1530 var t
= mtype
.lookup_fixed
(mmodule
, resolved_receiver
)
1531 if t
== mtype
then return self
1532 return t
.as_nullable
1535 redef fun depth
do return self.mtype
.depth
1537 redef fun length
do return self.mtype
.length
1539 redef fun collect_mclassdefs
(mmodule
)
1541 assert not self.need_anchor
1542 return self.mtype
.collect_mclassdefs
(mmodule
)
1545 redef fun collect_mclasses
(mmodule
)
1547 assert not self.need_anchor
1548 return self.mtype
.collect_mclasses
(mmodule
)
1551 redef fun collect_mtypes
(mmodule
)
1553 assert not self.need_anchor
1554 return self.mtype
.collect_mtypes
(mmodule
)
1558 # The type of the only value null
1560 # The is only one null type per model, see `MModel::null_type`.
1563 redef var model
: Model
1564 redef fun to_s
do return "null"
1565 redef fun full_name
do return "null"
1566 redef fun c_name
do return "null"
1567 redef fun as_nullable
do return self
1568 redef fun need_anchor
do return false
1569 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
) do return self
1570 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
1572 redef fun collect_mclassdefs
(mmodule
) do return new HashSet[MClassDef]
1574 redef fun collect_mclasses
(mmodule
) do return new HashSet[MClass]
1576 redef fun collect_mtypes
(mmodule
) do return new HashSet[MClassType]
1579 # A signature of a method
1583 # The each parameter (in order)
1584 var mparameters
: Array[MParameter]
1586 # The return type (null for a procedure)
1587 var return_mtype
: nullable MType
1592 var t
= self.return_mtype
1593 if t
!= null then dmax
= t
.depth
1594 for p
in mparameters
do
1595 var d
= p
.mtype
.depth
1596 if d
> dmax
then dmax
= d
1604 var t
= self.return_mtype
1605 if t
!= null then res
+= t
.length
1606 for p
in mparameters
do
1607 res
+= p
.mtype
.length
1612 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1615 var vararg_rank
= -1
1616 for i
in [0..mparameters
.length
[ do
1617 var parameter
= mparameters
[i
]
1618 if parameter
.is_vararg
then
1619 assert vararg_rank
== -1
1623 self.vararg_rank
= vararg_rank
1626 # The rank of the ellipsis (`...`) for vararg (starting from 0).
1627 # value is -1 if there is no vararg.
1628 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1629 var vararg_rank
: Int is noinit
1631 # The number or parameters
1632 fun arity
: Int do return mparameters
.length
1636 var b
= new FlatBuffer
1637 if not mparameters
.is_empty
then
1639 for i
in [0..mparameters
.length
[ do
1640 var mparameter
= mparameters
[i
]
1641 if i
> 0 then b
.append
(", ")
1642 b
.append
(mparameter
.name
)
1644 b
.append
(mparameter
.mtype
.to_s
)
1645 if mparameter
.is_vararg
then
1651 var ret
= self.return_mtype
1659 redef fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MSignature
1661 var params
= new Array[MParameter]
1662 for p
in self.mparameters
do
1663 params
.add
(p
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
))
1665 var ret
= self.return_mtype
1667 ret
= ret
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1669 var res
= new MSignature(params
, ret
)
1674 # A parameter in a signature
1678 # The name of the parameter
1679 redef var name
: String
1681 # The static type of the parameter
1684 # Is the parameter a vararg?
1690 return "{name}: {mtype}..."
1692 return "{name}: {mtype}"
1696 fun resolve_for
(mtype
: MType, anchor
: nullable MClassType, mmodule
: MModule, cleanup_virtual
: Bool): MParameter
1698 if not self.mtype
.need_anchor
then return self
1699 var newtype
= self.mtype
.resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
)
1700 var res
= new MParameter(self.name
, newtype
, self.is_vararg
)
1704 redef fun model
do return mtype
.model
1707 # A service (global property) that generalize method, attribute, etc.
1709 # `MProperty` are global to the model; it means that a `MProperty` is not bound
1710 # to a specific `MModule` nor a specific `MClass`.
1712 # A MProperty gather definitions (see `mpropdefs`) ; one for the introduction
1713 # and the other in subclasses and in refinements.
1715 # A `MProperty` is used to denotes services in polymorphic way (ie. independent
1716 # of any dynamic type).
1717 # For instance, a call site "x.foo" is associated to a `MProperty`.
1718 abstract class MProperty
1721 # The associated MPropDef subclass.
1722 # The two specialization hierarchy are symmetric.
1723 type MPROPDEF: MPropDef
1725 # The classdef that introduce the property
1726 # While a property is not bound to a specific module, or class,
1727 # the introducing mclassdef is used for naming and visibility
1728 var intro_mclassdef
: MClassDef
1730 # The (short) name of the property
1731 redef var name
: String
1733 # The canonical name of the property.
1735 # It is the short-`name` prefixed by the short-name of the class and the full-name of the module.
1736 # Example: "my_project::my_module::MyClass::my_method"
1737 redef var full_name
is lazy
do
1738 return "{intro_mclassdef.mmodule.full_name}::{intro_mclassdef.mclass.name}::{name}"
1741 redef var c_name
is lazy
do
1742 return "{intro_mclassdef.mmodule.c_name}__{intro_mclassdef.mclass.c_name}__{name.to_cmangle}"
1745 # The visibility of the property
1746 var visibility
: MVisibility
1750 intro_mclassdef
.intro_mproperties
.add
(self)
1751 var model
= intro_mclassdef
.mmodule
.model
1752 model
.mproperties_by_name
.add_one
(name
, self)
1753 model
.mproperties
.add
(self)
1756 # All definitions of the property.
1757 # The first is the introduction,
1758 # The other are redefinitions (in refinements and in subclasses)
1759 var mpropdefs
= new Array[MPROPDEF]
1761 # The definition that introduces the property.
1763 # Warning: such a definition may not exist in the early life of the object.
1764 # In this case, the method will abort.
1765 var intro
: MPROPDEF is noinit
1767 redef fun model
do return intro
.model
1770 redef fun to_s
do return name
1772 # Return the most specific property definitions defined or inherited by a type.
1773 # The selection knows that refinement is stronger than specialization;
1774 # however, in case of conflict more than one property are returned.
1775 # If mtype does not know mproperty then an empty array is returned.
1777 # If you want the really most specific property, then look at `lookup_first_definition`
1778 fun lookup_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1780 assert not mtype
.need_anchor
1781 mtype
= mtype
.as_notnullable
1783 var cache
= self.lookup_definitions_cache
[mmodule
, mtype
]
1784 if cache
!= null then return cache
1786 #print "select prop {mproperty} for {mtype} in {self}"
1787 # First, select all candidates
1788 var candidates
= new Array[MPROPDEF]
1789 for mpropdef
in self.mpropdefs
do
1790 # If the definition is not imported by the module, then skip
1791 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1792 # If the definition is not inherited by the type, then skip
1793 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1795 candidates
.add
(mpropdef
)
1797 # Fast track for only one candidate
1798 if candidates
.length
<= 1 then
1799 self.lookup_definitions_cache
[mmodule
, mtype
] = candidates
1803 # Second, filter the most specific ones
1804 return select_most_specific
(mmodule
, candidates
)
1807 private var lookup_definitions_cache
= new HashMap2[MModule, MType, Array[MPROPDEF]]
1809 # Return the most specific property definitions inherited by a type.
1810 # The selection knows that refinement is stronger than specialization;
1811 # however, in case of conflict more than one property are returned.
1812 # If mtype does not know mproperty then an empty array is returned.
1814 # If you want the really most specific property, then look at `lookup_next_definition`
1816 # FIXME: Move to `MPropDef`?
1817 fun lookup_super_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1819 assert not mtype
.need_anchor
1820 mtype
= mtype
.as_notnullable
1822 # First, select all candidates
1823 var candidates
= new Array[MPROPDEF]
1824 for mpropdef
in self.mpropdefs
do
1825 # If the definition is not imported by the module, then skip
1826 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1827 # If the definition is not inherited by the type, then skip
1828 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1829 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1830 if mtype
== mpropdef
.mclassdef
.bound_mtype
and mmodule
== mpropdef
.mclassdef
.mmodule
then continue
1832 candidates
.add
(mpropdef
)
1834 # Fast track for only one candidate
1835 if candidates
.length
<= 1 then return candidates
1837 # Second, filter the most specific ones
1838 return select_most_specific
(mmodule
, candidates
)
1841 # Return an array containing olny the most specific property definitions
1842 # This is an helper function for `lookup_definitions` and `lookup_super_definitions`
1843 private fun select_most_specific
(mmodule
: MModule, candidates
: Array[MPROPDEF]): Array[MPROPDEF]
1845 var res
= new Array[MPROPDEF]
1846 for pd1
in candidates
do
1847 var cd1
= pd1
.mclassdef
1850 for pd2
in candidates
do
1851 if pd2
== pd1
then continue # do not compare with self!
1852 var cd2
= pd2
.mclassdef
1854 if c2
.mclass_type
== c1
.mclass_type
then
1855 if cd2
.mmodule
.in_importation
< cd1
.mmodule
then
1856 # cd2 refines cd1; therefore we skip pd1
1860 else if cd2
.bound_mtype
.is_subtype
(mmodule
, null, cd1
.bound_mtype
) and cd2
.bound_mtype
!= cd1
.bound_mtype
then
1861 # cd2 < cd1; therefore we skip pd1
1870 if res
.is_empty
then
1871 print
"All lost! {candidates.join(", ")}"
1872 # FIXME: should be abort!
1877 # Return the most specific definition in the linearization of `mtype`.
1879 # If you want to know the next properties in the linearization,
1880 # look at `MPropDef::lookup_next_definition`.
1882 # FIXME: the linearization is still unspecified
1884 # REQUIRE: `not mtype.need_anchor`
1885 # REQUIRE: `mtype.has_mproperty(mmodule, self)`
1886 fun lookup_first_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
1888 assert mtype
.has_mproperty
(mmodule
, self)
1889 return lookup_all_definitions
(mmodule
, mtype
).first
1892 # Return all definitions in a linearization order
1893 # Most specific first, most general last
1894 fun lookup_all_definitions
(mmodule
: MModule, mtype
: MType): Array[MPROPDEF]
1896 assert not mtype
.need_anchor
1897 mtype
= mtype
.as_notnullable
1899 var cache
= self.lookup_all_definitions_cache
[mmodule
, mtype
]
1900 if cache
!= null then return cache
1902 #print "select prop {mproperty} for {mtype} in {self}"
1903 # First, select all candidates
1904 var candidates
= new Array[MPROPDEF]
1905 for mpropdef
in self.mpropdefs
do
1906 # If the definition is not imported by the module, then skip
1907 if not mmodule
.in_importation
<= mpropdef
.mclassdef
.mmodule
then continue
1908 # If the definition is not inherited by the type, then skip
1909 if not mtype
.is_subtype
(mmodule
, null, mpropdef
.mclassdef
.bound_mtype
) then continue
1911 candidates
.add
(mpropdef
)
1913 # Fast track for only one candidate
1914 if candidates
.length
<= 1 then
1915 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1919 mmodule
.linearize_mpropdefs
(candidates
)
1920 candidates
= candidates
.reversed
1921 self.lookup_all_definitions_cache
[mmodule
, mtype
] = candidates
1925 private var lookup_all_definitions_cache
= new HashMap2[MModule, MType, Array[MPROPDEF]]
1932 redef type MPROPDEF: MMethodDef
1934 # Is the property defined at the top_level of the module?
1935 # Currently such a property are stored in `Object`
1936 var is_toplevel
: Bool = false is writable
1938 # Is the property a constructor?
1939 # Warning, this property can be inherited by subclasses with or without being a constructor
1940 # therefore, you should use `is_init_for` the verify if the property is a legal constructor for a given class
1941 var is_init
: Bool = false is writable
1943 # The constructor is a (the) root init with empty signature but a set of initializers
1944 var is_root_init
: Bool = false is writable
1946 # Is the property a 'new' constructor?
1947 var is_new
: Bool = false is writable
1949 # Is the property a legal constructor for a given class?
1950 # As usual, visibility is not considered.
1951 # FIXME not implemented
1952 fun is_init_for
(mclass
: MClass): Bool
1958 # A global attribute
1962 redef type MPROPDEF: MAttributeDef
1966 # A global virtual type
1967 class MVirtualTypeProp
1970 redef type MPROPDEF: MVirtualTypeDef
1972 # The formal type associated to the virtual type property
1973 var mvirtualtype
= new MVirtualType(self)
1976 # A definition of a property (local property)
1978 # Unlike `MProperty`, a `MPropDef` is a local definition that belong to a
1979 # specific class definition (which belong to a specific module)
1980 abstract class MPropDef
1983 # The associated `MProperty` subclass.
1984 # the two specialization hierarchy are symmetric
1985 type MPROPERTY: MProperty
1988 type MPROPDEF: MPropDef
1990 # The class definition where the property definition is
1991 var mclassdef
: MClassDef
1993 # The associated global property
1994 var mproperty
: MPROPERTY
1996 # The origin of the definition
1997 var location
: Location
2001 mclassdef
.mpropdefs
.add
(self)
2002 mproperty
.mpropdefs
.add
(self)
2003 if mproperty
.intro_mclassdef
== mclassdef
then
2004 assert not isset mproperty
._intro
2005 mproperty
.intro
= self
2007 self.to_s
= "{mclassdef}#{mproperty}"
2010 # Actually the name of the `mproperty`
2011 redef fun name
do return mproperty
.name
2013 # The full-name of mpropdefs combine the information about the `classdef` and the `mproperty`.
2015 # Therefore the combination of identifiers is awful,
2016 # the worst case being
2019 # "{mclassdef.mmodule.full_name}#{mclassdef.mclass.intro_mmodule.full_name}::{mclassdef.name}#{mproperty.intro_mclassdef.mmodule.full_name}::{mproperty.intro_mclassdef.name}::{name}"
2022 # Fortunately, the full-name is simplified when entities are repeated.
2023 # The simplest form is "my_module#MyClass#my_property".
2024 redef var full_name
is lazy
do
2025 var res
= new FlatBuffer
2026 res
.append mclassdef
.mmodule
.full_name
2028 if not mclassdef
.is_intro
then
2029 res
.append mclassdef
.mclass
.intro_mmodule
.full_name
2032 res
.append mclassdef
.name
2034 if mproperty
.intro_mclassdef
.mmodule
!= mclassdef
.mmodule
then
2035 res
.append mproperty
.intro_mclassdef
.mmodule
.full_name
2038 if mclassdef
.mclass
!= mproperty
.intro_mclassdef
.mclass
then
2039 res
.append mproperty
.intro_mclassdef
.mclass
.name
2046 redef var c_name
is lazy
do
2047 var res
= new FlatBuffer
2048 res
.append mclassdef
.c_name
2051 res
.append name
.to_cmangle
2053 res
.append mproperty
.c_name
.to_cmangle
2058 redef fun model
do return mclassdef
.model
2060 # Internal name combining the module, the class and the property
2061 # Example: "mymodule#MyClass#mymethod"
2062 redef var to_s
: String is noinit
2064 # Is self the definition that introduce the property?
2065 fun is_intro
: Bool do return mproperty
.intro
== self
2067 # Return the next definition in linearization of `mtype`.
2069 # This method is used to determine what method is called by a super.
2071 # REQUIRE: `not mtype.need_anchor`
2072 fun lookup_next_definition
(mmodule
: MModule, mtype
: MType): MPROPDEF
2074 assert not mtype
.need_anchor
2076 var mpropdefs
= self.mproperty
.lookup_all_definitions
(mmodule
, mtype
)
2077 var i
= mpropdefs
.iterator
2078 while i
.is_ok
and i
.item
!= self do i
.next
2079 assert has_property
: i
.is_ok
2081 assert has_next_property
: i
.is_ok
2086 # A local definition of a method
2090 redef type MPROPERTY: MMethod
2091 redef type MPROPDEF: MMethodDef
2093 # The signature attached to the property definition
2094 var msignature
: nullable MSignature = null is writable
2096 # The signature attached to the `new` call on a root-init
2097 # This is a concatenation of the signatures of the initializers
2099 # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
2100 var new_msignature
: nullable MSignature = null is writable
2102 # List of initialisers to call in root-inits
2104 # They could be setters or attributes
2106 # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
2107 var initializers
= new Array[MProperty]
2109 # Is the method definition abstract?
2110 var is_abstract
: Bool = false is writable
2112 # Is the method definition intern?
2113 var is_intern
= false is writable
2115 # Is the method definition extern?
2116 var is_extern
= false is writable
2118 # An optional constant value returned in functions.
2120 # Only some specific primitife value are accepted by engines.
2121 # Is used when there is no better implementation available.
2123 # Currently used only for the implementation of the `--define`
2124 # command-line option.
2125 # SEE: module `mixin`.
2126 var constant_value
: nullable Object = null is writable
2129 # A local definition of an attribute
2133 redef type MPROPERTY: MAttribute
2134 redef type MPROPDEF: MAttributeDef
2136 # The static type of the attribute
2137 var static_mtype
: nullable MType = null is writable
2140 # A local definition of a virtual type
2141 class MVirtualTypeDef
2144 redef type MPROPERTY: MVirtualTypeProp
2145 redef type MPROPDEF: MVirtualTypeDef
2147 # The bound of the virtual type
2148 var bound
: nullable MType = null is writable
2150 # Is the bound fixed?
2151 var is_fixed
= false is writable
2158 # * `interface_kind`
2162 # Note this class is basically an enum.
2163 # FIXME: use a real enum once user-defined enums are available
2165 redef var to_s
: String
2167 # Is a constructor required?
2170 # TODO: private init because enumeration.
2172 # Can a class of kind `self` specializes a class of kine `other`?
2173 fun can_specialize
(other
: MClassKind): Bool
2175 if other
== interface_kind
then return true # everybody can specialize interfaces
2176 if self == interface_kind
or self == enum_kind
then
2177 # no other case for interfaces
2179 else if self == extern_kind
then
2180 # only compatible with themselves
2181 return self == other
2182 else if other
== enum_kind
or other
== extern_kind
then
2183 # abstract_kind and concrete_kind are incompatible
2186 # remain only abstract_kind and concrete_kind
2191 # The class kind `abstract`
2192 fun abstract_kind
: MClassKind do return once
new MClassKind("abstract class", true)
2193 # The class kind `concrete`
2194 fun concrete_kind
: MClassKind do return once
new MClassKind("class", true)
2195 # The class kind `interface`
2196 fun interface_kind
: MClassKind do return once
new MClassKind("interface", false)
2197 # The class kind `enum`
2198 fun enum_kind
: MClassKind do return once
new MClassKind("enum", false)
2199 # The class kind `extern`
2200 fun extern_kind
: MClassKind do return once
new MClassKind("extern class", false)