model: implements lookup_next_definition
[nit.git] / src / model / model.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Object model of the Nit language
18 #
19 # This module define the entities of the Nit meta-model like modules,
20 # classes, types and properties
21 #
22 # It also provide an API to build and query models.
23 #
24 # All model classes starts with the M letter (MModule, MClass, etc.)
25 #
26 # TODO: better doc
27 #
28 # TODO: liearization, closures, extern stuff
29 # FIXME: better handling of the types
30 module model
31
32 import poset
33 import location
34 import model_base
35
36 redef class Model
37 # All known classes
38 var mclasses: Array[MClass] = new Array[MClass]
39
40 # All known properties
41 var mproperties: Array[MProperty] = new Array[MProperty]
42
43 # Hierarchy of class definition.
44 #
45 # Each classdef is associated with its super-classdefs in regard to
46 # its module of definition.
47 var mclassdef_hierarchy: POSet[MClassDef] = new POSet[MClassDef]
48
49 # Class-type hierarchy restricted to the introduction.
50 #
51 # The idea is that what is true on introduction is always true whatever
52 # the module considered.
53 # Therefore, this hierarchy is used for a fast positive subtype check.
54 #
55 # This poset will evolve in a monotonous way:
56 # * Two non connected nodes will remain unconnected
57 # * New nodes can appear with new edges
58 private var intro_mtype_specialization_hierarchy: POSet[MClassType] = new POSet[MClassType]
59
60 # Global overlapped class-type hierarchy.
61 # The hierarchy when all modules are combined.
62 # Therefore, this hierarchy is used for a fast negative subtype check.
63 #
64 # This poset will evolve in an anarchic way. Loops can even be created.
65 #
66 # FIXME decide what to do on loops
67 private var full_mtype_specialization_hierarchy: POSet[MClassType] = new POSet[MClassType]
68
69 # Collections of classes grouped by their short name
70 private var mclasses_by_name: MultiHashMap[String, MClass] = new MultiHashMap[String, MClass]
71
72 # Return all class named `name'.
73 #
74 # If such a class does not exist, null is returned
75 # (instead of an empty array)
76 #
77 # Visibility or modules are not considered
78 fun get_mclasses_by_name(name: String): nullable Array[MClass]
79 do
80 if mclasses_by_name.has_key(name) then
81 return mclasses_by_name[name]
82 else
83 return null
84 end
85 end
86
87 # Collections of properties grouped by their short name
88 private var mproperties_by_name: MultiHashMap[String, MProperty] = new MultiHashMap[String, MProperty]
89
90 # Return all properties named `name'.
91 #
92 # If such a property does not exist, null is returned
93 # (instead of an empty array)
94 #
95 # Visibility or modules are not considered
96 fun get_mproperties_by_name(name: String): nullable Array[MProperty]
97 do
98 if not mproperties_by_name.has_key(name) then
99 return null
100 else
101 return mproperties_by_name[name]
102 end
103 end
104
105 # The only null type
106 var null_type: MNullType = new MNullType(self)
107 end
108
109 redef class MModule
110 # All the classes introduced in the module
111 var intro_mclasses: Array[MClass] = new Array[MClass]
112
113 # All the class definitions of the module
114 # (introduction and refinement)
115 var mclassdefs: Array[MClassDef] = new Array[MClassDef]
116
117 # Does the current module has a given class `mclass'?
118 # Return true if the mmodule introduces, refines or imports a class.
119 # Visibility is not considered.
120 fun has_mclass(mclass: MClass): Bool
121 do
122 return self.in_importation <= mclass.intro_mmodule
123 end
124
125 # Full hierarchy of introduced ans imported classes.
126 #
127 # Create a new hierarchy got by flattening the classes for the module
128 # and its imported modules.
129 # Visibility is not considered.
130 #
131 # Note: this function is expensive and is usually used for the main
132 # module of a program only. Do not use it to do you own subtype
133 # functions.
134 fun flatten_mclass_hierarchy: POSet[MClass]
135 do
136 var res = self.flatten_mclass_hierarchy_cache
137 if res != null then return res
138 res = new POSet[MClass]
139 for m in self.in_importation.greaters do
140 for cd in m.mclassdefs do
141 var c = cd.mclass
142 for s in cd.supertypes do
143 res.add_edge(c, s.mclass)
144 end
145 end
146 end
147 self.flatten_mclass_hierarchy_cache = res
148 return res
149 end
150
151 private var flatten_mclass_hierarchy_cache: nullable POSet[MClass] = null
152
153 # The primitive type Object, the root of the class hierarchy
154 fun object_type: MClassType
155 do
156 var res = self.object_type_cache
157 if res != null then return res
158 res = self.get_primitive_class("Object").mclass_type
159 self.object_type_cache = res
160 return res
161 end
162
163 private var object_type_cache: nullable MClassType
164
165 # The primitive type Bool
166 fun bool_type: MClassType
167 do
168 var res = self.bool_type_cache
169 if res != null then return res
170 res = self.get_primitive_class("Bool").mclass_type
171 self.bool_type_cache = res
172 return res
173 end
174
175 private var bool_type_cache: nullable MClassType
176
177 # The primitive type Sys, the main type of the program, if any
178 fun sys_type: nullable MClassType
179 do
180 var clas = self.model.get_mclasses_by_name("Sys")
181 if clas == null then return null
182 return get_primitive_class("Sys").mclass_type
183 end
184
185 # Force to get the primitive class named `name' or abort
186 fun get_primitive_class(name: String): MClass
187 do
188 var cla = self.model.get_mclasses_by_name(name)
189 if cla == null then
190 if name == "Bool" then
191 var c = new MClass(self, name, 0, enum_kind, public_visibility)
192 var cladef = new MClassDef(self, c.mclass_type, new Location(null, 0,0,0,0), new Array[String])
193 return c
194 end
195 print("Fatal Error: no primitive class {name}")
196 exit(1)
197 end
198 assert cla.length == 1 else print cla.join(", ")
199 return cla.first
200 end
201
202 # Try to get the primitive method named `name' on the type `recv'
203 fun try_get_primitive_method(name: String, recv: MType): nullable MMethod
204 do
205 var props = self.model.get_mproperties_by_name(name)
206 if props == null then return null
207 var res: nullable MMethod = null
208 for mprop in props do
209 assert mprop isa MMethod
210 if not recv.has_mproperty(self, mprop) then continue
211 if res == null then
212 res = mprop
213 else
214 print("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
215 abort
216 end
217 end
218 return res
219 end
220 end
221
222 # A named class
223 #
224 # MClass are global to the model; it means that a MClass is not bound to a
225 # specific `MModule`.
226 #
227 # This characteristic helps the reasoning about classes in a program since a
228 # single MClass object always denote the same class.
229 # However, because a MClass is global, it does not really have properties nor
230 # belong to a hierarchy since the property and the
231 # hierarchy of a class depends of a module.
232 class MClass
233 # The module that introduce the class
234 # While classes are not bound to a specific module,
235 # the introducing module is used for naming an visibility
236 var intro_mmodule: MModule
237
238 # The short name of the class
239 # In Nit, the name of a class cannot evolve in refinements
240 var name: String
241
242 # The canonical name of the class
243 # Example: "owner::module::MyClass"
244 fun full_name: String
245 do
246 return "{self.intro_mmodule.full_name}::{name}"
247 end
248
249 # The number of generic formal parameters
250 # 0 if the class is not generic
251 var arity: Int
252
253 # The kind of the class (interface, abstract class, etc.)
254 # In Nit, the kind of a class cannot evolve in refinements
255 var kind: MClassKind
256
257 # The visibility of the class
258 # In Nit, the visibility of a class cannot evolve in refinements
259 var visibility: MVisibility
260
261 init(intro_mmodule: MModule, name: String, arity: Int, kind: MClassKind, visibility: MVisibility)
262 do
263 self.intro_mmodule = intro_mmodule
264 self.name = name
265 self.arity = arity
266 self.kind = kind
267 self.visibility = visibility
268 intro_mmodule.intro_mclasses.add(self)
269 var model = intro_mmodule.model
270 model.mclasses_by_name.add_one(name, self)
271 model.mclasses.add(self)
272
273 # Create the formal parameter types
274 if arity > 0 then
275 var mparametertypes = new Array[MParameterType]
276 for i in [0..arity[ do
277 var mparametertype = new MParameterType(self, i)
278 mparametertypes.add(mparametertype)
279 end
280 var mclass_type = new MGenericType(self, mparametertypes)
281 self.mclass_type = mclass_type
282 self.get_mtype_cache.add(mclass_type)
283 else
284 self.mclass_type = new MClassType(self)
285 end
286 end
287
288 # All class definitions (introduction and refinements)
289 var mclassdefs: Array[MClassDef] = new Array[MClassDef]
290
291 # Alias for `name'
292 redef fun to_s do return self.name
293
294 # The definition that introduced the class
295 # Warning: the introduction is the first `MClassDef' object associated
296 # to self. If self is just created without having any associated
297 # definition, this method will abort
298 fun intro: MClassDef
299 do
300 assert has_a_first_definition: not mclassdefs.is_empty
301 return mclassdefs.first
302 end
303
304 # Return the class `self' in the class hierarchy of the module `mmodule'.
305 #
306 # SEE: MModule::flatten_mclass_hierarchy
307 # REQUIRE: mmodule.has_mclass(self)
308 fun in_hierarchy(mmodule: MModule): POSetElement[MClass]
309 do
310 return mmodule.flatten_mclass_hierarchy[self]
311 end
312
313 # The principal static type of the class.
314 #
315 # For non-generic class, mclass_type is the only MClassType based
316 # on self.
317 #
318 # For a generic class, the arguments are the formal parameters.
319 # i.e.: for the class `Array[E:Object]', the mtype is Array[E].
320 # If you want `Array[Object]' the see `MClassDef::bound_mtype'
321 #
322 # For generic classes, the mclass_type is also the way to get a formal
323 # generic parameter type.
324 #
325 # To get other types based on a generic class, see `get_mtype'.
326 #
327 # ENSURE: mclass_type.mclass == self
328 var mclass_type: MClassType
329
330 # Return a generic type based on the class
331 # Is the class is not generic, then the result is `mclass_type'
332 #
333 # REQUIRE: type_arguments.length == self.arity
334 fun get_mtype(mtype_arguments: Array[MType]): MClassType
335 do
336 assert mtype_arguments.length == self.arity
337 if self.arity == 0 then return self.mclass_type
338 for t in self.get_mtype_cache do
339 if t.arguments == mtype_arguments then
340 return t
341 end
342 end
343 var res = new MGenericType(self, mtype_arguments)
344 self.get_mtype_cache.add res
345 return res
346 end
347
348 private var get_mtype_cache: Array[MGenericType] = new Array[MGenericType]
349 end
350
351
352 # A definition (an introduction or a refinement) of a class in a module
353 #
354 # A MClassDef is associated with an explicit (or almost) definition of a
355 # class. Unlike MClass, a MClassDef is a local definition that belong to
356 # a specific module
357 class MClassDef
358 # The module where the definition is
359 var mmodule: MModule
360
361 # The associated MClass
362 var mclass: MClass
363
364 # The bounded type associated to the mclassdef
365 #
366 # For a non-generic class, `bound_mtype' and `mclass.mclass_type'
367 # are the same type.
368 #
369 # Example:
370 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
371 # If you want Array[E], then see `mclass.mclass_type'
372 #
373 # ENSURE: bound_mtype.mclass = self.mclass
374 var bound_mtype: MClassType
375
376 # Name of each formal generic parameter (in order of declaration)
377 var parameter_names: Array[String]
378
379 # The origin of the definition
380 var location: Location
381
382 # Internal name combining the module and the class
383 # Example: "mymodule#MyClass"
384 redef fun to_s do return "{mmodule}#{mclass}"
385
386 init(mmodule: MModule, bound_mtype: MClassType, location: Location, parameter_names: Array[String])
387 do
388 assert bound_mtype.mclass.arity == parameter_names.length
389 self.bound_mtype = bound_mtype
390 self.mmodule = mmodule
391 self.mclass = bound_mtype.mclass
392 self.location = location
393 mmodule.mclassdefs.add(self)
394 mclass.mclassdefs.add(self)
395 self.parameter_names = parameter_names
396 end
397
398 # All declared super-types
399 # FIXME: quite ugly but not better idea yet
400 var supertypes: Array[MClassType] = new Array[MClassType]
401
402 # Register some super-types for the class (ie "super SomeType")
403 #
404 # The hierarchy must not already be set
405 # REQUIRE: self.in_hierarchy == null
406 fun set_supertypes(supertypes: Array[MClassType])
407 do
408 assert unique_invocation: self.in_hierarchy == null
409 var mmodule = self.mmodule
410 var model = mmodule.model
411 var mtype = self.bound_mtype
412
413 for supertype in supertypes do
414 self.supertypes.add(supertype)
415
416 # Register in full_type_specialization_hierarchy
417 model.full_mtype_specialization_hierarchy.add_edge(mtype, supertype)
418 # Register in intro_type_specialization_hierarchy
419 if mclass.intro_mmodule == mmodule and supertype.mclass.intro_mmodule == mmodule then
420 model.intro_mtype_specialization_hierarchy.add_edge(mtype, supertype)
421 end
422 end
423
424 end
425
426 # Collect the super-types (set by set_supertypes) to build the hierarchy
427 #
428 # This function can only invoked once by class
429 # REQUIRE: self.in_hierarchy == null
430 # ENSURE: self.in_hierarchy != null
431 fun add_in_hierarchy
432 do
433 assert unique_invocation: self.in_hierarchy == null
434 var model = mmodule.model
435 var res = model.mclassdef_hierarchy.add_node(self)
436 self.in_hierarchy = res
437 var mtype = self.bound_mtype
438
439 # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
440 # The simpliest way is to attach it to collect_mclassdefs
441 for mclassdef in mtype.collect_mclassdefs(mmodule) do
442 res.poset.add_edge(self, mclassdef)
443 end
444 end
445
446 # The view of the class definition in `mclassdef_hierarchy'
447 var in_hierarchy: nullable POSetElement[MClassDef] = null
448
449 # Is the definition the one that introduced `mclass`?
450 fun is_intro: Bool do return mclass.intro == self
451
452 # All properties introduced by the classdef
453 var intro_mproperties: Array[MProperty] = new Array[MProperty]
454
455 # All property definitions in the class (introductions and redefinitions)
456 var mpropdefs: Array[MPropDef] = new Array[MPropDef]
457 end
458
459 # A global static type
460 #
461 # MType are global to the model; it means that a MType is not bound to a
462 # specific `MModule`.
463 # This characteristic helps the reasoning about static types in a program
464 # since a single MType object always denote the same type.
465 #
466 # However, because a MType is global, it does not really have properties
467 # nor have subtypes to a hierarchy since the property and the class hierarchy
468 # depends of a module.
469 # Moreover, virtual types an formal generic parameter types also depends on
470 # a receiver to have sense.
471 #
472 # Therefore, most method of the types require a module and an anchor.
473 # The module is used to know what are the classes and the specialization
474 # links.
475 # The anchor is used to know what is the bound of the virtual types and formal
476 # generic parameter types.
477 #
478 # MType are not directly usable to get properties. See the `anchor_to' method
479 # and the `MClassType' class.
480 #
481 # FIXME: the order of the parameters is not the best. We mus pick on from:
482 # * foo(mmodule, anchor, othertype)
483 # * foo(othertype, anchor, mmodule)
484 # * foo(anchor, mmodule, othertype)
485 # * foo(othertype, mmodule, anchor)
486 #
487 # FIXME: Add a 'is_valid_anchor' to improve imputability.
488 # Currently, anchors are used "as it" without check thus if the caller gives a
489 # bad anchor, then the method will likely crash (abort) in a bad case
490 #
491 # FIXME: maybe allways add an anchor with a nullable type (as in is_subtype)
492 abstract class MType
493
494 # The model of the type
495 fun model: Model is abstract
496
497 # Return true if `self' is an subtype of `sup'.
498 # The typing is done using the standard typing policy of Nit.
499 #
500 # REQUIRE: anchor == null implies not self.need_anchor and not sup.need_anchor
501 fun is_subtype(mmodule: MModule, anchor: nullable MClassType, sup: MType): Bool
502 do
503 var sub = self
504 if sub == sup then return true
505 if anchor == null then
506 assert not sub.need_anchor
507 assert not sup.need_anchor
508 end
509
510 # First, resolve the formal types to a common version in the receiver
511 # The trick here is that fixed formal type will be associed to the bound
512 # And unfixed formal types will be associed to a canonical formal type.
513 if sub isa MParameterType or sub isa MVirtualType then
514 assert anchor != null
515 sub = sub.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, false)
516 end
517 if sup isa MParameterType or sup isa MVirtualType then
518 assert anchor != null
519 sup = sup.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, false)
520 end
521
522 # Does `sup` accept null or not?
523 # Discard the nullable marker if it exists
524 var sup_accept_null = false
525 if sup isa MNullableType then
526 sup_accept_null = true
527 sup = sup.mtype
528 else if sup isa MNullType then
529 sup_accept_null = true
530 end
531
532 # Can `sub` provide null or not?
533 # Thus we can match with `sup_accept_null`
534 # Also discard the nullable marker if it exists
535 if sub isa MNullableType then
536 if not sup_accept_null then return false
537 sub = sub.mtype
538 else if sub isa MNullType then
539 return sup_accept_null
540 end
541 # Now the case of direct null and nullable is over.
542
543 # A unfixed formal type can only accept itself
544 if sup isa MParameterType or sup isa MVirtualType then
545 return sub == sup
546 end
547
548 # If `sub` is a formal type, then it is accepted if its bound is accepted
549 if sub isa MParameterType or sub isa MVirtualType then
550 assert anchor != null
551 sub = sub.anchor_to(mmodule, anchor)
552
553 # Manage the second layer of null/nullable
554 if sub isa MNullableType then
555 if not sup_accept_null then return false
556 sub = sub.mtype
557 else if sub isa MNullType then
558 return sup_accept_null
559 end
560 end
561
562 assert sub isa MClassType # It is the only remaining type
563
564 if sup isa MNullType then
565 # `sup` accepts only null
566 return false
567 end
568
569 assert sup isa MClassType # It is the only remaining type
570
571 # Now both are MClassType, we need to dig
572
573 if sub == sup then return true
574
575 if anchor == null then anchor = sub # UGLY: any anchor will work
576 var resolved_sub = sub.anchor_to(mmodule, anchor)
577 var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
578 if res == false then return false
579 if not sup isa MGenericType then return true
580 var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
581 assert sub2.mclass == sup.mclass
582 for i in [0..sup.mclass.arity[ do
583 var sub_arg = sub2.arguments[i]
584 var sup_arg = sup.arguments[i]
585 res = sub_arg.is_subtype(mmodule, anchor, sup_arg)
586 if res == false then return false
587 end
588 return true
589 end
590
591 # The base class type on which self is based
592 #
593 # This base type is used to get property (an internally to perform
594 # unsafe type comparison).
595 #
596 # Beware: some types (like null) are not based on a class thus this
597 # method will crash
598 #
599 # Basically, this function transform the virtual types and parameter
600 # types to their bounds.
601 #
602 # Example
603 # class G[T: A]
604 # type U: X
605 # end
606 # class H
607 # super G[C]
608 # redef type U: Y
609 # end
610 # Map[T,U] anchor_to H #-> Map[C,Y]
611 #
612 # Explanation of the example:
613 # In H, T is set to C, because "H super G[C]", and U is bound to Y,
614 # because "redef type U: Y". Therefore, Map[T, U] is bound to
615 # Map[C, Y]
616 #
617 # ENSURE: not self.need_anchor implies return == self
618 # ENSURE: not return.need_anchor
619 fun anchor_to(mmodule: MModule, anchor: MClassType): MType
620 do
621 if not need_anchor then return self
622 assert not anchor.need_anchor
623 # Just resolve to the anchor and clear all the virtual types
624 var res = self.resolve_for(anchor, anchor, mmodule, true)
625 assert not res.need_anchor
626 return res
627 end
628
629 # Does `self' contain a virtual type or a formal generic parameter type?
630 # In order to remove those types, you usually want to use `anchor_to'.
631 fun need_anchor: Bool do return true
632
633 # Return the supertype when adapted to a class.
634 #
635 # In Nit, for each super-class of a type, there is a equivalent super-type.
636 #
637 # Example:
638 # class G[T, U]
639 # class H[V] super G[V, Bool]
640 # H[Int] supertype_to G #-> G[Int, Bool]
641 #
642 # REQUIRE: `super_mclass' is a super-class of `self'
643 # ENSURE: return.mclass = mclass
644 fun supertype_to(mmodule: MModule, anchor: MClassType, super_mclass: MClass): MClassType
645 do
646 if super_mclass.arity == 0 then return super_mclass.mclass_type
647 if self isa MClassType and self.mclass == super_mclass then return self
648 var resolved_self = self.anchor_to(mmodule, anchor)
649 var supertypes = resolved_self.collect_mtypes(mmodule)
650 for supertype in supertypes do
651 if supertype.mclass == super_mclass then
652 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
653 return supertype.resolve_for(self, anchor, mmodule, false)
654 end
655 end
656 abort
657 end
658
659 # Replace formals generic types in self with resolved values in `mtype'
660 # If `cleanup_virtual' is true, then virtual types are also replaced
661 # with their bounds
662 #
663 # This function returns self if `need_anchor' is false.
664 #
665 # Example:
666 # class G[E]
667 # class H[F] super G[F]
668 # Array[E] resolve_for H[Int] #-> Array[Int]
669 #
670 # Explanation of the example:
671 # * Array[E].need_anchor is true because there is a formal generic
672 # parameter type E
673 # * E makes sense for H[Int] because E is a formal parameter of G
674 # and H specialize G
675 # * Since "H[F] super G[F]", E is in fact F for H
676 # * More specifically, in H[Int], E is Int
677 # * So, in H[Int], Array[E] is Array[Int]
678 #
679 # This function is mainly used to inherit a signature.
680 # Because, unlike `anchor_type', we do not want a full resolution of
681 # a type but only an adapted version of it.
682 #
683 # Example:
684 # class A[E]
685 # foo(e:E):E
686 # end
687 # class B super A[Int] end
688 #
689 # The signature on foo is (e: E): E
690 # If we resolve the signature for B, we get (e:Int):Int
691 #
692 # TODO: Explain the cleanup_virtual
693 #
694 # FIXME: the parameter `cleanup_virtual' is just a bad idea, but having
695 # two function instead of one seems also to be a bad idea.
696 #
697 # ENSURE: not self.need_anchor implies return == self
698 fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MType is abstract
699
700 # Return the nullable version of the type
701 # If the type is already nullable then self is returned
702 #
703 # FIXME: DO NOT WORK YET
704 fun as_nullable: MType
705 do
706 var res = self.as_nullable_cache
707 if res != null then return res
708 res = new MNullableType(self)
709 self.as_nullable_cache = res
710 return res
711 end
712
713 private var as_nullable_cache: nullable MType = null
714
715
716 # The deph of the type seen as a tree.
717 #
718 # A -> 1
719 # G[A] -> 2
720 # H[A, B] -> 2
721 # H[G[A], B] -> 3
722 #
723 # Formal types have a depth of 1.
724 fun depth: Int
725 do
726 return 1
727 end
728
729 # Compute all the classdefs inherited/imported.
730 # The returned set contains:
731 # * the class definitions from `mmodule` and its imported modules
732 # * the class definitions of this type and its super-types
733 #
734 # This function is used mainly internally.
735 #
736 # REQUIRE: not self.need_anchor
737 fun collect_mclassdefs(mmodule: MModule): Set[MClassDef] is abstract
738
739 # Compute all the super-classes.
740 # This function is used mainly internally.
741 #
742 # REQUIRE: not self.need_anchor
743 fun collect_mclasses(mmodule: MModule): Set[MClass] is abstract
744
745 # Compute all the declared super-types.
746 # Super-types are returned as declared in the classdefs (verbatim).
747 # This function is used mainly internally.
748 #
749 # REQUIRE: not self.need_anchor
750 fun collect_mtypes(mmodule: MModule): Set[MClassType] is abstract
751
752 # Is the property in self for a given module
753 # This method does not filter visibility or whatever
754 #
755 # REQUIRE: not self.need_anchor
756 fun has_mproperty(mmodule: MModule, mproperty: MProperty): Bool
757 do
758 assert not self.need_anchor
759 return self.collect_mclassdefs(mmodule).has(mproperty.intro_mclassdef)
760 end
761 end
762
763 # A type based on a class.
764 #
765 # MClassType have properties (see `has_property').
766 class MClassType
767 super MType
768
769 # The associated class
770 var mclass: MClass
771
772 redef fun model do return self.mclass.intro_mmodule.model
773
774 private init(mclass: MClass)
775 do
776 self.mclass = mclass
777 end
778
779 # The formal arguments of the type
780 # ENSURE: return.length == self.mclass.arity
781 var arguments: Array[MType] = new Array[MType]
782
783 redef fun to_s do return mclass.to_s
784
785 redef fun need_anchor do return false
786
787 redef fun anchor_to(mmodule: MModule, anchor: MClassType): MClassType
788 do
789 return super.as(MClassType)
790 end
791
792 redef fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MClassType do return self
793
794 redef fun collect_mclassdefs(mmodule)
795 do
796 assert not self.need_anchor
797 var cache = self.collect_mclassdefs_cache
798 if not cache.has_key(mmodule) then
799 self.collect_things(mmodule)
800 end
801 return cache[mmodule]
802 end
803
804 redef fun collect_mclasses(mmodule)
805 do
806 assert not self.need_anchor
807 var cache = self.collect_mclasses_cache
808 if not cache.has_key(mmodule) then
809 self.collect_things(mmodule)
810 end
811 return cache[mmodule]
812 end
813
814 redef fun collect_mtypes(mmodule)
815 do
816 assert not self.need_anchor
817 var cache = self.collect_mtypes_cache
818 if not cache.has_key(mmodule) then
819 self.collect_things(mmodule)
820 end
821 return cache[mmodule]
822 end
823
824 # common implementation for `collect_mclassdefs', `collect_mclasses', and `collect_mtypes'.
825 private fun collect_things(mmodule: MModule)
826 do
827 var res = new HashSet[MClassDef]
828 var seen = new HashSet[MClass]
829 var types = new HashSet[MClassType]
830 seen.add(self.mclass)
831 var todo = [self.mclass]
832 while not todo.is_empty do
833 var mclass = todo.pop
834 #print "process {mclass}"
835 for mclassdef in mclass.mclassdefs do
836 if not mmodule.in_importation <= mclassdef.mmodule then continue
837 #print " process {mclassdef}"
838 res.add(mclassdef)
839 for supertype in mclassdef.supertypes do
840 types.add(supertype)
841 var superclass = supertype.mclass
842 if seen.has(superclass) then continue
843 #print " add {superclass}"
844 seen.add(superclass)
845 todo.add(superclass)
846 end
847 end
848 end
849 collect_mclassdefs_cache[mmodule] = res
850 collect_mclasses_cache[mmodule] = seen
851 collect_mtypes_cache[mmodule] = types
852 end
853
854 private var collect_mclassdefs_cache: HashMap[MModule, Set[MClassDef]] = new HashMap[MModule, Set[MClassDef]]
855 private var collect_mclasses_cache: HashMap[MModule, Set[MClass]] = new HashMap[MModule, Set[MClass]]
856 private var collect_mtypes_cache: HashMap[MModule, Set[MClassType]] = new HashMap[MModule, Set[MClassType]]
857
858 end
859
860 # A type based on a generic class.
861 # A generic type a just a class with additional formal generic arguments.
862 class MGenericType
863 super MClassType
864
865 private init(mclass: MClass, arguments: Array[MType])
866 do
867 super(mclass)
868 assert self.mclass.arity == arguments.length
869 self.arguments = arguments
870
871 self.need_anchor = false
872 for t in arguments do
873 if t.need_anchor then
874 self.need_anchor = true
875 break
876 end
877 end
878 end
879
880 # Recursively print the type of the arguments within brackets.
881 # Example: "Map[String, List[Int]]"
882 redef fun to_s
883 do
884 return "{mclass}[{arguments.join(", ")}]"
885 end
886
887 redef var need_anchor: Bool
888
889 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
890 do
891 if not need_anchor then return self
892 var types = new Array[MType]
893 for t in arguments do
894 types.add(t.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
895 end
896 return mclass.get_mtype(types)
897 end
898
899 redef fun depth
900 do
901 var dmax = 0
902 for a in self.arguments do
903 var d = a.depth
904 if d > dmax then dmax = d
905 end
906 return dmax + 1
907 end
908 end
909
910 # A virtual formal type.
911 class MVirtualType
912 super MType
913
914 # The property associated with the type.
915 # Its the definitions of this property that determine the bound or the virtual type.
916 var mproperty: MProperty
917
918 redef fun model do return self.mproperty.intro_mclassdef.mmodule.model
919
920 # Lookup the bound for a given resolved_receiver
921 # The result may be a other virtual type (or a parameter type)
922 #
923 # The result is returned exactly as declared in the "type" property (verbatim).
924 #
925 # In case of conflict, the method aborts.
926 fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
927 do
928 assert not resolved_receiver.need_anchor
929 var props = self.mproperty.lookup_definitions(mmodule, resolved_receiver)
930 if props.is_empty then
931 abort
932 else if props.length == 1 then
933 return props.first.as(MVirtualTypeDef).bound.as(not null)
934 end
935 var types = new ArraySet[MType]
936 for p in props do
937 types.add(p.as(MVirtualTypeDef).bound.as(not null))
938 end
939 if types.length == 1 then
940 return types.first
941 end
942 abort
943 end
944
945 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
946 do
947 # self is a virtual type declared (or inherited) in mtype
948 # The point of the function it to get the bound of the virtual type that make sense for mtype
949 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
950 #print "{class_name}: {self}/{mtype}/{anchor}?"
951 var resolved_reciever = mtype.resolve_for(anchor, anchor, mmodule, true)
952 # Now, we can get the bound
953 var verbatim_bound = lookup_bound(mmodule, resolved_reciever)
954 # The bound is exactly as declared in the "type" property, so we must resolve it again
955 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
956 #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}"
957
958 # What to return here? There is a bunch a special cases:
959 # If 'cleanup_virtual' we must return the resolved type, since we cannot return self
960 if cleanup_virtual then return res
961 # 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
962 if resolved_reciever isa MNullableType then resolved_reciever = resolved_reciever.mtype
963 if resolved_reciever.as(MClassType).mclass.kind == enum_kind then return res
964 # 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.
965 if res isa MVirtualType then return res
966 # 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
967 if res isa MClassType and res.mclass.kind == enum_kind then return res
968 # TODO: Add 'fixed' virtual type in the specification.
969 # TODO: What if bound to a MParameterType?
970 # Note that Nullable types can always be redefined by the non nullable version, so there is no specific case on it.
971
972 # If anything apply, then `self' cannot be resolved, so return self
973 return self
974 end
975
976 redef fun to_s do return self.mproperty.to_s
977
978 init(mproperty: MProperty)
979 do
980 self.mproperty = mproperty
981 end
982 end
983
984 # The type associated the a formal parameter generic type of a class
985 #
986 # Each parameter type is associated to a specific class.
987 # It's mean that all refinements of a same class "share" the parameter type,
988 # but that a generic subclass has its on parameter types.
989 #
990 # However, in the sense of the meta-model, the a parameter type of a class is
991 # a valid types in a subclass. The "in the sense of the meta-model" is
992 # important because, in the Nit language, the programmer cannot refers
993 # directly to the parameter types of the super-classes.
994 #
995 # Example:
996 # class A[E]
997 # fun e: E is abstract
998 # end
999 # class B[F]
1000 # super A[Array[F]]
1001 # end
1002 # In the class definition B[F], `F' is a valid type but `E' is not.
1003 # However, `self.e' is a valid method call, and the signature of `e' is
1004 # declared `e: E'.
1005 #
1006 # Note that parameter types are shared among class refinements.
1007 # Therefore parameter only have an internal name (see `to_s' for details).
1008 # TODO: Add a 'name_for' to get better messages.
1009 class MParameterType
1010 super MType
1011
1012 # The generic class where the parameter belong
1013 var mclass: MClass
1014
1015 redef fun model do return self.mclass.intro_mmodule.model
1016
1017 # The position of the parameter (0 for the first parameter)
1018 # FIXME: is `position' a better name?
1019 var rank: Int
1020
1021 # Internal name of the parameter type
1022 # Names of parameter types changes in each class definition
1023 # Therefore, this method return an internal name.
1024 # Example: return "G#1" for the second parameter of the class G
1025 # FIXME: add a way to get the real name in a classdef
1026 redef fun to_s do return "{mclass}#{rank}"
1027
1028 # Resolve the bound for a given resolved_receiver
1029 # The result may be a other virtual type (or a parameter type)
1030 fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
1031 do
1032 assert not resolved_receiver.need_anchor
1033 var goalclass = self.mclass
1034 var supertypes = resolved_receiver.collect_mtypes(mmodule)
1035 for t in supertypes do
1036 if t.mclass == goalclass then
1037 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
1038 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
1039 var res = t.arguments[self.rank]
1040 return res
1041 end
1042 end
1043 abort
1044 end
1045
1046 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1047 do
1048 #print "{class_name}: {self}/{mtype}/{anchor}?"
1049
1050 if mtype isa MGenericType and mtype.mclass == self.mclass then
1051 return mtype.arguments[self.rank]
1052 end
1053
1054 # self is a parameter type of mtype (or of a super-class of mtype)
1055 # The point of the function it to get the bound of the virtual type that make sense for mtype
1056 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1057 # FIXME: What happend here is far from clear. Thus this part must be validated and clarified
1058 var resolved_receiver = mtype.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, true)
1059 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
1060 if resolved_receiver isa MParameterType then
1061 assert resolved_receiver.mclass == anchor.mclass
1062 resolved_receiver = anchor.arguments[resolved_receiver.rank]
1063 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
1064 end
1065 assert resolved_receiver isa MClassType else print "{class_name}: {self}/{mtype}/{anchor}? {resolved_receiver}"
1066
1067 # Eh! The parameter is in the current class.
1068 # So we return the corresponding argument, no mater what!
1069 if resolved_receiver.mclass == self.mclass then
1070 var res = resolved_receiver.arguments[self.rank]
1071 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
1072 return res
1073 end
1074
1075 resolved_receiver = resolved_receiver.resolve_for(anchor, anchor, mmodule, false)
1076 # Now, we can get the bound
1077 var verbatim_bound = lookup_bound(mmodule, resolved_receiver)
1078 # The bound is exactly as declared in the "type" property, so we must resolve it again
1079 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1080
1081 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
1082
1083 return res
1084 end
1085
1086 init(mclass: MClass, rank: Int)
1087 do
1088 self.mclass = mclass
1089 self.rank = rank
1090 end
1091 end
1092
1093 # A type prefixed with "nullable"
1094 # FIXME Stub implementation
1095 class MNullableType
1096 super MType
1097
1098 # The base type of the nullable type
1099 var mtype: MType
1100
1101 redef fun model do return self.mtype.model
1102
1103 init(mtype: MType)
1104 do
1105 self.mtype = mtype
1106 end
1107
1108 redef fun to_s do return "nullable {mtype}"
1109
1110 redef fun need_anchor do return mtype.need_anchor
1111 redef fun as_nullable do return self
1112 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1113 do
1114 var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1115 return res.as_nullable
1116 end
1117
1118 redef fun depth do return self.mtype.depth
1119
1120 redef fun collect_mclassdefs(mmodule)
1121 do
1122 assert not self.need_anchor
1123 return self.mtype.collect_mclassdefs(mmodule)
1124 end
1125
1126 redef fun collect_mclasses(mmodule)
1127 do
1128 assert not self.need_anchor
1129 return self.mtype.collect_mclasses(mmodule)
1130 end
1131
1132 redef fun collect_mtypes(mmodule)
1133 do
1134 assert not self.need_anchor
1135 return self.mtype.collect_mtypes(mmodule)
1136 end
1137 end
1138
1139 # The type of the only value null
1140 #
1141 # The is only one null type per model, see `MModel::null_type'.
1142 class MNullType
1143 super MType
1144 redef var model: Model
1145 protected init(model: Model)
1146 do
1147 self.model = model
1148 end
1149 redef fun to_s do return "null"
1150 redef fun as_nullable do return self
1151 redef fun need_anchor do return false
1152 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
1153
1154 redef fun collect_mclassdefs(mmodule) do return new HashSet[MClassDef]
1155
1156 redef fun collect_mclasses(mmodule) do return new HashSet[MClass]
1157
1158 redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
1159 end
1160
1161 # A signature of a method (or a closure)
1162 class MSignature
1163 super MType
1164
1165 # The each parameter (in order)
1166 var mparameters: Array[MParameter]
1167
1168 var mclosures = new Array[MParameter]
1169
1170 # The return type (null for a procedure)
1171 var return_mtype: nullable MType
1172
1173 redef fun depth
1174 do
1175 var dmax = 0
1176 var t = self.return_mtype
1177 if t != null then dmax = t.depth
1178 for p in mparameters do
1179 var d = p.mtype.depth
1180 if d > dmax then dmax = d
1181 end
1182 for p in mclosures do
1183 var d = p.mtype.depth
1184 if d > dmax then dmax = d
1185 end
1186 return dmax + 1
1187 end
1188
1189 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1190 init(mparameters: Array[MParameter], return_mtype: nullable MType)
1191 do
1192 var vararg_rank = -1
1193 for i in [0..mparameters.length[ do
1194 var parameter = mparameters[i]
1195 if parameter.is_vararg then
1196 assert vararg_rank == -1
1197 vararg_rank = i
1198 end
1199 end
1200 self.mparameters = mparameters
1201 self.return_mtype = return_mtype
1202 self.vararg_rank = vararg_rank
1203 end
1204
1205 # The rank of the ellipsis (...) for vararg (starting from 0).
1206 # value is -1 if there is no vararg.
1207 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1208 var vararg_rank: Int
1209
1210 # The number or parameters
1211 fun arity: Int do return mparameters.length
1212
1213 redef fun to_s
1214 do
1215 var b = new Buffer
1216 if not mparameters.is_empty then
1217 b.append("(")
1218 for i in [0..mparameters.length[ do
1219 var mparameter = mparameters[i]
1220 if i > 0 then b.append(", ")
1221 b.append(mparameter.name)
1222 b.append(": ")
1223 b.append(mparameter.mtype.to_s)
1224 if mparameter.is_vararg then
1225 b.append("...")
1226 end
1227 end
1228 b.append(")")
1229 end
1230 var ret = self.return_mtype
1231 if ret != null then
1232 b.append(": ")
1233 b.append(ret.to_s)
1234 end
1235 return b.to_s
1236 end
1237
1238 redef fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MSignature
1239 do
1240 var params = new Array[MParameter]
1241 for p in self.mparameters do
1242 params.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1243 end
1244 var ret = self.return_mtype
1245 if ret != null then
1246 ret = ret.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1247 end
1248 var res = new MSignature(params, ret)
1249 for p in self.mclosures do
1250 res.mclosures.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1251 end
1252 return res
1253 end
1254 end
1255
1256 # A parameter in a signature
1257 class MParameter
1258 # The name of the parameter
1259 var name: String
1260
1261 # The static type of the parameter
1262 var mtype: MType
1263
1264 # Is the parameter a vararg?
1265 var is_vararg: Bool
1266
1267 fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MParameter
1268 do
1269 if not self.mtype.need_anchor then return self
1270 var newtype = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1271 var res = new MParameter(self.name, newtype, self.is_vararg)
1272 return res
1273 end
1274 end
1275
1276 # A service (global property) that generalize method, attribute, etc.
1277 #
1278 # MProperty are global to the model; it means that a MProperty is not bound
1279 # to a specific `MModule` nor a specific `MClass`.
1280 #
1281 # A MProperty gather definitions (see `mpropdefs') ; one for the introduction
1282 # and the other in subclasses and in refinements.
1283 #
1284 # A MProperty is used to denotes services in polymorphic way (ie. independent
1285 # of any dynamic type).
1286 # For instance, a call site "x.foo" is associated to a MProperty.
1287 abstract class MProperty
1288 # The associated MPropDef subclass.
1289 # The two specialization hierarchy are symmetric.
1290 type MPROPDEF: MPropDef
1291
1292 # The classdef that introduce the property
1293 # While a property is not bound to a specific module, or class,
1294 # the introducing mclassdef is used for naming and visibility
1295 var intro_mclassdef: MClassDef
1296
1297 # The (short) name of the property
1298 var name: String
1299
1300 # The canonical name of the property
1301 # Example: "owner::my_module::MyClass::my_method"
1302 fun full_name: String
1303 do
1304 return "{self.intro_mclassdef.mmodule.full_name}::{self.intro_mclassdef.mclass.name}::{name}"
1305 end
1306
1307 # The visibility of the property
1308 var visibility: MVisibility
1309
1310 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1311 do
1312 self.intro_mclassdef = intro_mclassdef
1313 self.name = name
1314 self.visibility = visibility
1315 intro_mclassdef.intro_mproperties.add(self)
1316 var model = intro_mclassdef.mmodule.model
1317 model.mproperties_by_name.add_one(name, self)
1318 model.mproperties.add(self)
1319 end
1320
1321 # All definitions of the property.
1322 # The first is the introduction,
1323 # The other are redefinitions (in refinements and in subclasses)
1324 var mpropdefs: Array[MPROPDEF] = new Array[MPROPDEF]
1325
1326 # The definition that introduced the property
1327 # Warning: the introduction is the first `MPropDef' object
1328 # associated to self. If self is just created without having any
1329 # associated definition, this method will abort
1330 fun intro: MPROPDEF do return mpropdefs.first
1331
1332 # Alias for `name'
1333 redef fun to_s do return name
1334
1335 # Return the most specific property definitions defined or inherited by a type.
1336 # The selection knows that refinement is stronger than specialization;
1337 # however, in case of conflict more than one property are returned.
1338 # If mtype does not know mproperty then an empty array is returned.
1339 #
1340 # If you want the really most specific property, then look at `lookup_first_definition`
1341 fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
1342 do
1343 assert not mtype.need_anchor
1344 if mtype isa MNullableType then mtype = mtype.mtype
1345
1346 var cache = self.lookup_definitions_cache[mmodule, mtype]
1347 if cache != null then return cache
1348
1349 #print "select prop {mproperty} for {mtype} in {self}"
1350 # First, select all candidates
1351 var candidates = new Array[MPROPDEF]
1352 for mpropdef in self.mpropdefs do
1353 # If the definition is not imported by the module, then skip
1354 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1355 # If the definition is not inherited by the type, then skip
1356 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1357 # Else, we keep it
1358 candidates.add(mpropdef)
1359 end
1360 # Fast track for only one candidate
1361 if candidates.length <= 1 then
1362 self.lookup_definitions_cache[mmodule, mtype] = candidates
1363 return candidates
1364 end
1365
1366 # Second, filter the most specific ones
1367 var res = new Array[MPROPDEF]
1368 for pd1 in candidates do
1369 var cd1 = pd1.mclassdef
1370 var c1 = cd1.mclass
1371 var keep = true
1372 for pd2 in candidates do
1373 if pd2 == pd1 then continue # do not compare with self!
1374 var cd2 = pd2.mclassdef
1375 var c2 = cd2.mclass
1376 if c2.mclass_type == c1.mclass_type then
1377 if cd2.mmodule.in_importation <= cd1.mmodule then
1378 # cd2 refines cd1; therefore we skip pd1
1379 keep = false
1380 break
1381 end
1382 else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
1383 # cd2 < cd1; therefore we skip pd1
1384 keep = false
1385 break
1386 end
1387 end
1388 if keep then
1389 res.add(pd1)
1390 end
1391 end
1392 if res.is_empty then
1393 print "All lost! {candidates.join(", ")}"
1394 # FIXME: should be abort!
1395 end
1396 self.lookup_definitions_cache[mmodule, mtype] = res
1397 return res
1398 end
1399
1400 private var lookup_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1401
1402 # Return the most specific property definitions inherited by a type.
1403 # The selection knows that refinement is stronger than specialization;
1404 # however, in case of conflict more than one property are returned.
1405 # If mtype does not know mproperty then an empty array is returned.
1406 #
1407 # If you want the really most specific property, then look at `lookup_next_definition`
1408 #
1409 # FIXME: Move to MPropDef?
1410 fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPropDef]
1411 do
1412 assert not mtype.need_anchor
1413 if mtype isa MNullableType then mtype = mtype.mtype
1414
1415 # First, select all candidates
1416 var candidates = new Array[MPropDef]
1417 for mpropdef in self.mpropdefs do
1418 # If the definition is not imported by the module, then skip
1419 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1420 # If the definition is not inherited by the type, then skip
1421 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1422 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1423 if mtype == mpropdef.mclassdef.bound_mtype and mmodule == mpropdef.mclassdef.mmodule then continue
1424 # Else, we keep it
1425 candidates.add(mpropdef)
1426 end
1427 # Fast track for only one candidate
1428 if candidates.length <= 1 then return candidates
1429
1430 # Second, filter the most specific ones
1431 var res = new Array[MPropDef]
1432 for pd1 in candidates do
1433 var cd1 = pd1.mclassdef
1434 var c1 = cd1.mclass
1435 var keep = true
1436 for pd2 in candidates do
1437 if pd2 == pd1 then continue # do not compare with self!
1438 var cd2 = pd2.mclassdef
1439 var c2 = cd2.mclass
1440 if c2.mclass_type == c1.mclass_type then
1441 if cd2.mmodule.in_importation <= cd1.mmodule then
1442 # cd2 refines cd1; therefore we skip pd1
1443 keep = false
1444 break
1445 end
1446 else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
1447 # cd2 < cd1; therefore we skip pd1
1448 keep = false
1449 break
1450 end
1451 end
1452 if keep then
1453 res.add(pd1)
1454 end
1455 end
1456 if res.is_empty then
1457 print "All lost! {candidates.join(", ")}"
1458 # FIXME: should be abort!
1459 end
1460 return res
1461 end
1462
1463 # Return the most specific definition in the linearization of `mtype`.
1464 #
1465 # If you want to know the next properties in the linearization,
1466 # look at `MPropDef::lookup_next_definition`.
1467 #
1468 # FIXME: the linearisation is still unspecified
1469 #
1470 # REQUIRE: not mtype.need_anchor
1471 # REQUIRE: mtype.has_mproperty(mmodule, self)
1472 fun lookup_first_definition(mmodule: MModule, mtype: MType): MPROPDEF
1473 do
1474 assert not mtype.need_anchor
1475 assert mtype.has_mproperty(mmodule, self)
1476
1477 var candidates = self.lookup_definitions(mmodule, mtype)
1478 if candidates.length == 1 then return candidates.first
1479 assert candidates.length > 0
1480
1481 print "BADLINEXT chose {candidates.first} in: {candidates.join(", ")}"
1482 return candidates.first
1483 end
1484 end
1485
1486 # A global method
1487 class MMethod
1488 super MProperty
1489
1490 redef type MPROPDEF: MMethodDef
1491
1492 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1493 do
1494 super
1495 end
1496
1497 # Is the property a constructor?
1498 # Warning, this property can be inherited by subclasses with or without being a constructor
1499 # therefore, you should use `is_init_for' the verify if the property is a legal constructor for a given class
1500 var is_init: Bool writable = false
1501
1502 # The the property a 'new' contructor?
1503 var is_new: Bool writable = false
1504
1505 # Is the property a legal constructor for a given class?
1506 # As usual, visibility is not considered.
1507 # FIXME not implemented
1508 fun is_init_for(mclass: MClass): Bool
1509 do
1510 return self.is_init
1511 end
1512 end
1513
1514 # A global attribute
1515 class MAttribute
1516 super MProperty
1517
1518 redef type MPROPDEF: MAttributeDef
1519
1520 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1521 do
1522 super
1523 end
1524 end
1525
1526 # A global virtual type
1527 class MVirtualTypeProp
1528 super MProperty
1529
1530 redef type MPROPDEF: MVirtualTypeDef
1531
1532 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1533 do
1534 super
1535 end
1536
1537 # The formal type associated to the virtual type property
1538 var mvirtualtype: MVirtualType = new MVirtualType(self)
1539 end
1540
1541 # A definition of a property (local property)
1542 #
1543 # Unlike MProperty, a MPropDef is a local definition that belong to a
1544 # specific class definition (which belong to a specific module)
1545 abstract class MPropDef
1546
1547 # The associated MProperty subclass.
1548 # the two specialization hierarchy are symmetric
1549 type MPROPERTY: MProperty
1550
1551 # Self class
1552 type MPROPDEF: MPropDef
1553
1554 # The origin of the definition
1555 var location: Location
1556
1557 # The class definition where the property definition is
1558 var mclassdef: MClassDef
1559
1560 # The associated global property
1561 var mproperty: MPROPERTY
1562
1563 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1564 do
1565 self.mclassdef = mclassdef
1566 self.mproperty = mproperty
1567 self.location = location
1568 mclassdef.mpropdefs.add(self)
1569 mproperty.mpropdefs.add(self)
1570 end
1571
1572 # Internal name combining the module, the class and the property
1573 # Example: "mymodule#MyClass#mymethod"
1574 redef fun to_s
1575 do
1576 return "{mclassdef}#{mproperty}"
1577 end
1578
1579 # Is self the definition that introduce the property?
1580 fun is_intro: Bool do return mproperty.intro == self
1581
1582 # Return the next definition in linearization of `mtype`.
1583 #
1584 # This method is used to determine what method is called by a super.
1585 #
1586 # FIXME: IMPLEMENTED AS A static designation, it is ugly
1587 #
1588 # REQUIRE: not mtype.need_anchor
1589 fun lookup_next_definition(mmodule: MModule, mtype: MType): MPROPDEF
1590 do
1591 assert not mtype.need_anchor
1592
1593 var mpropdefs = self.mproperty.lookup_super_definitions(self.mclassdef.mmodule, self.mclassdef.bound_mtype)
1594 assert not mpropdefs.is_empty
1595 if mpropdefs.length > 1 then
1596 print "BADLINEXT chose next {mpropdefs.first} in: {mpropdefs.join(", ")}"
1597 end
1598 return mpropdefs.first
1599 end
1600 end
1601
1602 # A local definition of a method
1603 class MMethodDef
1604 super MPropDef
1605
1606 redef type MPROPERTY: MMethod
1607 redef type MPROPDEF: MMethodDef
1608
1609 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1610 do
1611 super
1612 end
1613
1614 # The signature attached to the property definition
1615 var msignature: nullable MSignature writable = null
1616 end
1617
1618 # A local definition of an attribute
1619 class MAttributeDef
1620 super MPropDef
1621
1622 redef type MPROPERTY: MAttribute
1623 redef type MPROPDEF: MAttributeDef
1624
1625 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1626 do
1627 super
1628 end
1629
1630 # The static type of the attribute
1631 var static_mtype: nullable MType writable = null
1632 end
1633
1634 # A local definition of a virtual type
1635 class MVirtualTypeDef
1636 super MPropDef
1637
1638 redef type MPROPERTY: MVirtualTypeProp
1639 redef type MPROPDEF: MVirtualTypeDef
1640
1641 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1642 do
1643 super
1644 end
1645
1646 # The bound of the virtual type
1647 var bound: nullable MType writable = null
1648 end
1649
1650 # A kind of class.
1651 #
1652 # * abstract_kind
1653 # * concrete_kind
1654 # * interface_kind
1655 # * enum_kind
1656 # * extern_kind
1657 #
1658 # Note this class is basically an enum.
1659 # FIXME: use a real enum once user-defined enums are available
1660 class MClassKind
1661 redef var to_s: String
1662
1663 # Is a constructor required?
1664 var need_init: Bool
1665 private init(s: String, need_init: Bool)
1666 do
1667 self.to_s = s
1668 self.need_init = need_init
1669 end
1670 end
1671
1672 fun abstract_kind: MClassKind do return once new MClassKind("abstract class", true)
1673 fun concrete_kind: MClassKind do return once new MClassKind("class", true)
1674 fun interface_kind: MClassKind do return once new MClassKind("interface", false)
1675 fun enum_kind: MClassKind do return once new MClassKind("enum", false)
1676 fun extern_kind: MClassKind do return once new MClassKind("extern", false)