new model: move force_get_primitive_method to modelbuilder
[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 abort
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 private fun intro: MClassDef
299 do
300 assert has_a_first_definition: not mclassdefs.is_empty
301 return mclassdefs.first
302 end
303
304 # The principal static type of the class.
305 #
306 # For non-generic class, mclass_type is the only MClassType based
307 # on self.
308 #
309 # For a generic class, the arguments are the formal parameters.
310 # i.e.: for the class `Array[E:Object]', the mtype is Array[E].
311 # If you want `Array[Object]' the see `MClassDef::bound_mtype'
312 #
313 # For generic classes, the mclass_type is also the way to get a formal
314 # generic parameter type.
315 #
316 # To get other types based on a generic class, see `get_mtype'.
317 #
318 # ENSURE: mclass_type.mclass == self
319 var mclass_type: MClassType
320
321 # Return a generic type based on the class
322 # Is the class is not generic, then the result is `mclass_type'
323 #
324 # REQUIRE: type_arguments.length == self.arity
325 fun get_mtype(mtype_arguments: Array[MType]): MClassType
326 do
327 assert mtype_arguments.length == self.arity
328 if self.arity == 0 then return self.mclass_type
329 for t in self.get_mtype_cache do
330 if t.arguments == mtype_arguments then
331 return t
332 end
333 end
334 var res = new MGenericType(self, mtype_arguments)
335 self.get_mtype_cache.add res
336 return res
337 end
338
339 private var get_mtype_cache: Array[MGenericType] = new Array[MGenericType]
340 end
341
342
343 # A definition (an introduction or a refinement) of a class in a module
344 #
345 # A MClassDef is associated with an explicit (or almost) definition of a
346 # class. Unlike MClass, a MClassDef is a local definition that belong to
347 # a specific module
348 class MClassDef
349 # The module where the definition is
350 var mmodule: MModule
351
352 # The associated MClass
353 var mclass: MClass
354
355 # The bounded type associated to the mclassdef
356 #
357 # For a non-generic class, `bound_mtype' and `mclass.mclass_type'
358 # are the same type.
359 #
360 # Example:
361 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
362 # If you want Array[E], then see `mclass.mclass_type'
363 #
364 # ENSURE: bound_mtype.mclass = self.mclass
365 var bound_mtype: MClassType
366
367 # Name of each formal generic parameter (in order of declaration)
368 var parameter_names: Array[String]
369
370 # The origin of the definition
371 var location: Location
372
373 # Internal name combining the module and the class
374 # Example: "mymodule#MyClass"
375 redef fun to_s do return "{mmodule}#{mclass}"
376
377 init(mmodule: MModule, bound_mtype: MClassType, location: Location, parameter_names: Array[String])
378 do
379 assert bound_mtype.mclass.arity == parameter_names.length
380 self.bound_mtype = bound_mtype
381 self.mmodule = mmodule
382 self.mclass = bound_mtype.mclass
383 self.location = location
384 mmodule.mclassdefs.add(self)
385 mclass.mclassdefs.add(self)
386 self.parameter_names = parameter_names
387 end
388
389 # All declared super-types
390 # FIXME: quite ugly but not better idea yet
391 var supertypes: Array[MClassType] = new Array[MClassType]
392
393 # Register the super-types for the class (ie "super SomeType")
394 # This function can only invoked once by class
395 fun set_supertypes(supertypes: Array[MClassType])
396 do
397 assert unique_invocation: self.in_hierarchy == null
398 var mmodule = self.mmodule
399 var model = mmodule.model
400 var res = model.mclassdef_hierarchy.add_node(self)
401 self.in_hierarchy = res
402 var mtype = self.bound_mtype
403
404 for supertype in supertypes do
405 self.supertypes.add(supertype)
406
407 # Register in full_type_specialization_hierarchy
408 model.full_mtype_specialization_hierarchy.add_edge(mtype, supertype)
409 # Register in intro_type_specialization_hierarchy
410 if mclass.intro_mmodule == mmodule and supertype.mclass.intro_mmodule == mmodule then
411 model.intro_mtype_specialization_hierarchy.add_edge(mtype, supertype)
412 end
413 end
414
415 for mclassdef in mtype.collect_mclassdefs(mmodule) do
416 res.poset.add_edge(self, mclassdef)
417 end
418 end
419
420 # The view of the class definition in `mclassdef_hierarchy'
421 var in_hierarchy: nullable POSetElement[MClassDef] = null
422
423 # Is the definition the one that introduced `mclass`?
424 fun is_intro: Bool do return mclass.intro == self
425
426 # All properties introduced by the classdef
427 var intro_mproperties: Array[MProperty] = new Array[MProperty]
428
429 # All property definitions in the class (introductions and redefinitions)
430 var mpropdefs: Array[MPropDef] = new Array[MPropDef]
431 end
432
433 # A global static type
434 #
435 # MType are global to the model; it means that a MType is not bound to a
436 # specific `MModule`.
437 # This characteristic helps the reasoning about static types in a program
438 # since a single MType object always denote the same type.
439 #
440 # However, because a MType is global, it does not really have properties
441 # nor have subtypes to a hierarchy since the property and the class hierarchy
442 # depends of a module.
443 # Moreover, virtual types an formal generic parameter types also depends on
444 # a receiver to have sense.
445 #
446 # Therefore, most method of the types require a module and an anchor.
447 # The module is used to know what are the classes and the specialization
448 # links.
449 # The anchor is used to know what is the bound of the virtual types and formal
450 # generic parameter types.
451 #
452 # MType are not directly usable to get properties. See the `anchor_to' method
453 # and the `MClassType' class.
454 #
455 # FIXME: the order of the parameters is not the best. We mus pick on from:
456 # * foo(mmodule, anchor, othertype)
457 # * foo(othertype, anchor, mmodule)
458 # * foo(anchor, mmodule, othertype)
459 # * foo(othertype, mmodule, anchor)
460 #
461 # FIXME: Add a 'is_valid_anchor' to improve imputability.
462 # Currently, anchors are used "as it" without check thus if the caller gives a
463 # bad anchor, then the method will likely crash (abort) in a bad case
464 #
465 # FIXME: maybe allways add an anchor with a nullable type (as in is_subtype)
466 abstract class MType
467
468 # The model of the type
469 fun model: Model is abstract
470
471 # Return true if `self' is an subtype of `sup'.
472 # The typing is done using the standard typing policy of Nit.
473 #
474 # REQUIRE: anchor == null implies not self.need_anchor and not sup.need_anchor
475 fun is_subtype(mmodule: MModule, anchor: nullable MClassType, sup: MType): Bool
476 do
477 var sub = self
478 if sub == sup then return true
479 if anchor == null then
480 assert not sub.need_anchor
481 assert not sup.need_anchor
482 end
483 # First, resolve the types
484 if sub isa MParameterType or sub isa MVirtualType then
485 assert anchor != null
486 sub = sub.resolve_for(anchor, anchor, mmodule, false)
487 end
488 if sup isa MParameterType or sup isa MVirtualType then
489 assert anchor != null
490 sup = sup.resolve_for(anchor, anchor, mmodule, false)
491 end
492
493 if sup isa MParameterType or sup isa MVirtualType or sup isa MNullType then
494 return sub == sup
495 end
496 if sub isa MParameterType or sub isa MVirtualType then
497 assert anchor != null
498 sub = sub.anchor_to(mmodule, anchor)
499 end
500 if sup isa MNullableType then
501 if sub isa MNullType then
502 return true
503 else if sub isa MNullableType then
504 return sub.mtype.is_subtype(mmodule, anchor, sup.mtype)
505 else if sub isa MClassType then
506 return sub.is_subtype(mmodule, anchor, sup.mtype)
507 else
508 abort
509 end
510 end
511
512 assert sup isa MClassType # It is the only remaining type
513 if sub isa MNullableType or sub isa MNullType then
514 return false
515 end
516
517 if sub == sup then return true
518
519 assert sub isa MClassType # It is the only remaining type
520 if anchor == null then anchor = sub # UGLY: any anchor will work
521 var resolved_sub = sub.anchor_to(mmodule, anchor)
522 var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
523 if res == false then return false
524 if not sup isa MGenericType then return true
525 var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
526 assert sub2.mclass == sup.mclass
527 assert sub2 isa MGenericType
528 for i in [0..sup.mclass.arity[ do
529 var sub_arg = sub2.arguments[i]
530 var sup_arg = sup.arguments[i]
531 res = sub_arg.is_subtype(mmodule, anchor, sup_arg)
532 if res == false then return false
533 end
534 return true
535 end
536
537 # The base class type on which self is based
538 #
539 # This base type is used to get property (an internally to perform
540 # unsafe type comparison).
541 #
542 # Beware: some types (like null) are not based on a class thus this
543 # method will crash
544 #
545 # Basically, this function transform the virtual types and parameter
546 # types to their bounds.
547 #
548 # Example
549 # class G[T: A]
550 # type U: X
551 # end
552 # class H
553 # super G[C]
554 # redef type U: Y
555 # end
556 # Map[T,U] anchor_to H #-> Map[C,Y]
557 #
558 # Explanation of the example:
559 # In H, T is set to C, because "H super G[C]", and U is bound to Y,
560 # because "redef type U: Y". Therefore, Map[T, U] is bound to
561 # Map[C, Y]
562 #
563 # ENSURE: not self.need_anchor implies return == self
564 # ENSURE: not return.need_anchor
565 fun anchor_to(mmodule: MModule, anchor: MClassType): MType
566 do
567 if not need_anchor then return self
568 assert not anchor.need_anchor
569 # Just resolve to the anchor and clear all the virtual types
570 var res = self.resolve_for(anchor, anchor, mmodule, true)
571 assert not res.need_anchor
572 return res
573 end
574
575 # Does `self' contain a virtual type or a formal generic parameter type?
576 # In order to remove those types, you usually want to use `anchor_to'.
577 fun need_anchor: Bool do return true
578
579 # Return the supertype when adapted to a class.
580 #
581 # In Nit, for each super-class of a type, there is a equivalent super-type.
582 #
583 # Example:
584 # class G[T, U]
585 # class H[V] super G[V, Bool]
586 # H[Int] supertype_to G #-> G[Int, Bool]
587 #
588 # REQUIRE: `super_mclass' is a super-class of `self'
589 # ENSURE: return.mclass = mclass
590 fun supertype_to(mmodule: MModule, anchor: MClassType, super_mclass: MClass): MClassType
591 do
592 if super_mclass.arity == 0 then return super_mclass.mclass_type
593 if self isa MClassType and self.mclass == super_mclass then return self
594 var resolved_self = self.anchor_to(mmodule, anchor)
595 var supertypes = resolved_self.collect_mtypes(mmodule)
596 for supertype in supertypes do
597 if supertype.mclass == super_mclass then
598 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
599 return supertype.resolve_for(self, anchor, mmodule, false)
600 end
601 end
602 abort
603 end
604
605 # Replace formals generic types in self with resolved values in `mtype'
606 # If `cleanup_virtual' is true, then virtual types are also replaced
607 # with their bounds
608 #
609 # This function returns self if `need_anchor' is false.
610 #
611 # Example:
612 # class G[E]
613 # class H[F] super G[F]
614 # Array[E] resolve_for H[Int] #-> Array[Int]
615 #
616 # Explanation of the example:
617 # * Array[E].need_anchor is true because there is a formal generic
618 # parameter type E
619 # * E makes sense for H[Int] because E is a formal parameter of G
620 # and H specialize G
621 # * Since "H[F] super G[F]", E is in fact F for H
622 # * More specifically, in H[Int], E is Int
623 # * So, in H[Int], Array[E] is Array[Int]
624 #
625 # This function is mainly used to inherit a signature.
626 # Because, unlike `anchor_type', we do not want a full resolution of
627 # a type but only an adapted version of it.
628 #
629 # Example:
630 # class A[E]
631 # foo(e:E):E
632 # end
633 # class B super A[Int] end
634 #
635 # The signature on foo is (e: E): E
636 # If we resolve the signature for B, we get (e:Int):Int
637 #
638 # TODO: Explain the cleanup_virtual
639 #
640 # FIXME: the parameter `cleanup_virtual' is just a bad idea, but having
641 # two function instead of one seems also to be a bad idea.
642 #
643 # ENSURE: not self.need_anchor implies return == self
644 fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MType is abstract
645
646 # Return the nullable version of the type
647 # If the type is already nullable then self is returned
648 #
649 # FIXME: DO NOT WORK YET
650 fun as_nullable: MType
651 do
652 var res = self.as_nullable_cache
653 if res != null then return res
654 res = new MNullableType(self)
655 self.as_nullable_cache = res
656 return res
657 end
658
659 private var as_nullable_cache: nullable MType = null
660
661 # Compute all the classdefs inherited/imported.
662 # The returned set contains:
663 # * the class definitions from `mmodule` and its imported modules
664 # * the class definitions of this type and its super-types
665 #
666 # This function is used mainly internally.
667 #
668 # REQUIRE: not self.need_anchor
669 fun collect_mclassdefs(mmodule: MModule): Set[MClassDef] is abstract
670
671 # Compute all the super-classes.
672 # This function is used mainly internally.
673 #
674 # REQUIRE: not self.need_anchor
675 fun collect_mclasses(mmodule: MModule): Set[MClass] is abstract
676
677 # Compute all the declared super-types.
678 # Super-types are returned as declared in the classdefs (verbatim).
679 # This function is used mainly internally.
680 #
681 # REQUIRE: not self.need_anchor
682 fun collect_mtypes(mmodule: MModule): Set[MClassType] is abstract
683
684 # Is the property in self for a given module
685 # This method does not filter visibility or whatever
686 #
687 # REQUIRE: not self.need_anchor
688 fun has_mproperty(mmodule: MModule, mproperty: MProperty): Bool
689 do
690 assert not self.need_anchor
691 return self.collect_mclassdefs(mmodule).has(mproperty.intro_mclassdef)
692 end
693 end
694
695 # A type based on a class.
696 #
697 # MClassType have properties (see `has_property').
698 class MClassType
699 super MType
700
701 # The associated class
702 var mclass: MClass
703
704 redef fun model do return self.mclass.intro_mmodule.model
705
706 private init(mclass: MClass)
707 do
708 self.mclass = mclass
709 end
710
711 redef fun to_s do return mclass.to_s
712
713 redef fun need_anchor do return false
714
715 redef fun anchor_to(mmodule: MModule, anchor: MClassType): MClassType
716 do
717 return super.as(MClassType)
718 end
719
720 redef fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MClassType do return self
721
722 redef fun collect_mclassdefs(mmodule)
723 do
724 assert not self.need_anchor
725 var cache = self.collect_mclassdefs_cache
726 if not cache.has_key(mmodule) then
727 self.collect_things(mmodule)
728 end
729 return cache[mmodule]
730 end
731
732 redef fun collect_mclasses(mmodule)
733 do
734 assert not self.need_anchor
735 var cache = self.collect_mclasses_cache
736 if not cache.has_key(mmodule) then
737 self.collect_things(mmodule)
738 end
739 return cache[mmodule]
740 end
741
742 redef fun collect_mtypes(mmodule)
743 do
744 assert not self.need_anchor
745 var cache = self.collect_mtypes_cache
746 if not cache.has_key(mmodule) then
747 self.collect_things(mmodule)
748 end
749 return cache[mmodule]
750 end
751
752 # common implementation for `collect_mclassdefs', `collect_mclasses', and `collect_mtypes'.
753 private fun collect_things(mmodule: MModule)
754 do
755 var res = new HashSet[MClassDef]
756 var seen = new HashSet[MClass]
757 var types = new HashSet[MClassType]
758 seen.add(self.mclass)
759 var todo = [self.mclass]
760 while not todo.is_empty do
761 var mclass = todo.pop
762 #print "process {mclass}"
763 for mclassdef in mclass.mclassdefs do
764 if not mmodule.in_importation <= mclassdef.mmodule then continue
765 #print " process {mclassdef}"
766 res.add(mclassdef)
767 for supertype in mclassdef.supertypes do
768 types.add(supertype)
769 var superclass = supertype.mclass
770 if seen.has(superclass) then continue
771 #print " add {superclass}"
772 seen.add(superclass)
773 todo.add(superclass)
774 end
775 end
776 end
777 collect_mclassdefs_cache[mmodule] = res
778 collect_mclasses_cache[mmodule] = seen
779 collect_mtypes_cache[mmodule] = types
780 end
781
782 private var collect_mclassdefs_cache: HashMap[MModule, Set[MClassDef]] = new HashMap[MModule, Set[MClassDef]]
783 private var collect_mclasses_cache: HashMap[MModule, Set[MClass]] = new HashMap[MModule, Set[MClass]]
784 private var collect_mtypes_cache: HashMap[MModule, Set[MClassType]] = new HashMap[MModule, Set[MClassType]]
785
786 end
787
788 # A type based on a generic class.
789 # A generic type a just a class with additional formal generic arguments.
790 class MGenericType
791 super MClassType
792
793 private init(mclass: MClass, arguments: Array[MType])
794 do
795 super(mclass)
796 assert self.mclass.arity == arguments.length
797 self.arguments = arguments
798
799 self.need_anchor = false
800 for t in arguments do
801 if t.need_anchor then
802 self.need_anchor = true
803 break
804 end
805 end
806 end
807
808 # The formal arguments of the type
809 # ENSURE: return.length == self.mclass.arity
810 var arguments: Array[MType]
811
812 # Recursively print the type of the arguments within brackets.
813 # Example: "Map[String,List[Int]]"
814 redef fun to_s
815 do
816 return "{mclass}[{arguments.join(",")}]"
817 end
818
819 redef var need_anchor: Bool
820
821 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
822 do
823 if not need_anchor then return self
824 var types = new Array[MType]
825 for t in arguments do
826 types.add(t.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
827 end
828 return mclass.get_mtype(types)
829 end
830 end
831
832 # A virtual formal type.
833 class MVirtualType
834 super MType
835
836 # The property associated with the type.
837 # Its the definitions of this property that determine the bound or the virtual type.
838 var mproperty: MProperty
839
840 redef fun model do return self.mproperty.intro_mclassdef.mmodule.model
841
842 # Lookup the bound for a given resolved_receiver
843 # The result may be a other virtual type (or a parameter type)
844 #
845 # The result is returned exactly as declared in the "type" property (verbatim).
846 #
847 # In case of conflict, the method aborts.
848 fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
849 do
850 assert not resolved_receiver.need_anchor
851 var props = self.mproperty.lookup_definitions(mmodule, resolved_receiver)
852 if props.is_empty then
853 abort
854 else if props.length == 1 then
855 return props.first.as(MVirtualTypeDef).bound.as(not null)
856 end
857 var types = new ArraySet[MType]
858 for p in props do
859 types.add(p.as(MVirtualTypeDef).bound.as(not null))
860 end
861 if types.length == 1 then
862 return types.first
863 end
864 abort
865 end
866
867 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
868 do
869 if not cleanup_virtual then return self
870 # self is a virtual type declared (or inherited) in mtype
871 # The point of the function it to get the bound of the virtual type that make sense for mtype
872 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
873 #print "{class_name}: {self}/{mtype}/{anchor}?"
874 var resolved_reciever = mtype.resolve_for(anchor, anchor, mmodule, true)
875 # Now, we can get the bound
876 var verbatim_bound = lookup_bound(mmodule, resolved_reciever)
877 # The bound is exactly as declared in the "type" property, so we must resolve it again
878 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, true)
879 #print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}"
880 return res
881 end
882
883 redef fun to_s do return self.mproperty.to_s
884
885 init(mproperty: MProperty)
886 do
887 self.mproperty = mproperty
888 end
889 end
890
891 # The type associated the a formal parameter generic type of a class
892 #
893 # Each parameter type is associated to a specific class.
894 # It's mean that all refinements of a same class "share" the parameter type,
895 # but that a generic subclass has its on parameter types.
896 #
897 # However, in the sense of the meta-model, the a parameter type of a class is
898 # a valid types in a subclass. The "in the sense of the meta-model" is
899 # important because, in the Nit language, the programmer cannot refers
900 # directly to the parameter types of the super-classes.
901 #
902 # Example:
903 # class A[E]
904 # fun e: E is abstract
905 # end
906 # class B[F]
907 # super A[Array[F]]
908 # end
909 # In the class definition B[F], `F' is a valid type but `E' is not.
910 # However, `self.e' is a valid method call, and the signature of `e' is
911 # declared `e: E'.
912 #
913 # Note that parameter types are shared among class refinements.
914 # Therefore parameter only have an internal name (see `to_s' for details).
915 # TODO: Add a 'name_for' to get better messages.
916 class MParameterType
917 super MType
918
919 # The generic class where the parameter belong
920 var mclass: MClass
921
922 redef fun model do return self.mclass.intro_mmodule.model
923
924 # The position of the parameter (0 for the first parameter)
925 # FIXME: is `position' a better name?
926 var rank: Int
927
928 # Internal name of the parameter type
929 # Names of parameter types changes in each class definition
930 # Therefore, this method return an internal name.
931 # Example: return "G#1" for the second parameter of the class G
932 # FIXME: add a way to get the real name in a classdef
933 redef fun to_s do return "{mclass}#{rank}"
934
935 # Resolve the bound for a given resolved_receiver
936 # The result may be a other virtual type (or a parameter type)
937 fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
938 do
939 assert not resolved_receiver.need_anchor
940 var goalclass = self.mclass
941 var supertypes = resolved_receiver.collect_mtypes(mmodule)
942 for t in supertypes do
943 if t.mclass == goalclass then
944 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
945 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
946 assert t isa MGenericType
947 var res = t.arguments[self.rank]
948 return res
949 end
950 end
951 abort
952 end
953
954 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
955 do
956 #print "{class_name}: {self}/{mtype}/{anchor}?"
957
958 if mtype isa MGenericType and mtype.mclass == self.mclass then
959 return mtype.arguments[self.rank]
960 end
961
962 # self is a parameter type of mtype (or of a super-class of mtype)
963 # The point of the function it to get the bound of the virtual type that make sense for mtype
964 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
965 # FIXME: What happend here is far from clear. Thus this part must be validated and clarified
966 var resolved_receiver = mtype.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, true)
967 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
968 if resolved_receiver isa MParameterType then
969 assert resolved_receiver.mclass == anchor.mclass
970 resolved_receiver = anchor.as(MGenericType).arguments[resolved_receiver.rank]
971 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
972 end
973 assert resolved_receiver isa MClassType else print "{class_name}: {self}/{mtype}/{anchor}? {resolved_receiver}"
974
975 # Eh! The parameter is in the current class.
976 # So we return the corresponding argument, no mater what!
977 if resolved_receiver.mclass == self.mclass then
978 assert resolved_receiver isa MGenericType
979 var res = resolved_receiver.arguments[self.rank]
980 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
981 return res
982 end
983
984 resolved_receiver = resolved_receiver.resolve_for(anchor, anchor, mmodule, false)
985 # Now, we can get the bound
986 var verbatim_bound = lookup_bound(mmodule, resolved_receiver)
987 # The bound is exactly as declared in the "type" property, so we must resolve it again
988 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
989
990 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
991
992 return res
993 end
994
995 init(mclass: MClass, rank: Int)
996 do
997 self.mclass = mclass
998 self.rank = rank
999 end
1000 end
1001
1002 # A type prefixed with "nullable"
1003 # FIXME Stub implementation
1004 class MNullableType
1005 super MType
1006
1007 # The base type of the nullable type
1008 var mtype: MType
1009
1010 redef fun model do return self.mtype.model
1011
1012 init(mtype: MType)
1013 do
1014 self.mtype = mtype
1015 end
1016
1017 redef fun to_s do return "nullable {mtype}"
1018
1019 redef fun need_anchor do return mtype.need_anchor
1020 redef fun as_nullable do return self
1021 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1022 do
1023 var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1024 return res.as_nullable
1025 end
1026
1027 redef fun collect_mclassdefs(mmodule)
1028 do
1029 assert not self.need_anchor
1030 return self.mtype.collect_mclassdefs(mmodule)
1031 end
1032
1033 redef fun collect_mclasses(mmodule)
1034 do
1035 assert not self.need_anchor
1036 return self.mtype.collect_mclasses(mmodule)
1037 end
1038
1039 redef fun collect_mtypes(mmodule)
1040 do
1041 assert not self.need_anchor
1042 return self.mtype.collect_mtypes(mmodule)
1043 end
1044 end
1045
1046 # The type of the only value null
1047 #
1048 # The is only one null type per model, see `MModel::null_type'.
1049 class MNullType
1050 super MType
1051 redef var model: Model
1052 protected init(model: Model)
1053 do
1054 self.model = model
1055 end
1056 redef fun to_s do return "null"
1057 redef fun as_nullable do return self
1058 redef fun need_anchor do return false
1059 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
1060
1061 redef fun collect_mclassdefs(mmodule) do return new HashSet[MClassDef]
1062
1063 redef fun collect_mclasses(mmodule) do return new HashSet[MClass]
1064
1065 redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
1066 end
1067
1068 # A signature of a method (or a closure)
1069 class MSignature
1070 super MType
1071
1072 # The each parameter (in order)
1073 var mparameters: Array[MParameter]
1074
1075 var mclosures = new Array[MParameter]
1076
1077 # The return type (null for a procedure)
1078 var return_mtype: nullable MType
1079
1080 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1081 init(mparameters: Array[MParameter], return_mtype: nullable MType)
1082 do
1083 var vararg_rank = -1
1084 for i in [0..mparameters.length[ do
1085 var parameter = mparameters[i]
1086 if parameter.is_vararg then
1087 assert vararg_rank == -1
1088 vararg_rank = i
1089 end
1090 end
1091 self.mparameters = mparameters
1092 self.return_mtype = return_mtype
1093 self.vararg_rank = vararg_rank
1094 end
1095
1096 # The rank of the ellipsis (...) for vararg (starting from 0).
1097 # value is -1 if there is no vararg.
1098 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1099 var vararg_rank: Int
1100
1101 # The number or parameters
1102 fun arity: Int do return mparameters.length
1103
1104 redef fun to_s
1105 do
1106 var b = new Buffer
1107 if not mparameters.is_empty then
1108 b.append("(")
1109 for i in [0..mparameters.length[ do
1110 var mparameter = mparameters[i]
1111 if i > 0 then b.append(", ")
1112 b.append(mparameter.name)
1113 b.append(": ")
1114 b.append(mparameter.mtype.to_s)
1115 if mparameter.is_vararg then
1116 b.append("...")
1117 end
1118 end
1119 b.append(")")
1120 end
1121 var ret = self.return_mtype
1122 if ret != null then
1123 b.append(": ")
1124 b.append(ret.to_s)
1125 end
1126 return b.to_s
1127 end
1128
1129 redef fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MSignature
1130 do
1131 var params = new Array[MParameter]
1132 for p in self.mparameters do
1133 params.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1134 end
1135 var ret = self.return_mtype
1136 if ret != null then
1137 ret = ret.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1138 end
1139 var res = new MSignature(params, ret)
1140 for p in self.mclosures do
1141 res.mclosures.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1142 end
1143 return res
1144 end
1145 end
1146
1147 # A parameter in a signature
1148 class MParameter
1149 # The name of the parameter
1150 var name: String
1151
1152 # The static type of the parameter
1153 var mtype: MType
1154
1155 # Is the parameter a vararg?
1156 var is_vararg: Bool
1157
1158 fun resolve_for(mtype: MType, anchor: MClassType, mmodule: MModule, cleanup_virtual: Bool): MParameter
1159 do
1160 if not self.mtype.need_anchor then return self
1161 var newtype = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1162 var res = new MParameter(self.name, newtype, self.is_vararg)
1163 return res
1164 end
1165 end
1166
1167 # A service (global property) that generalize method, attribute, etc.
1168 #
1169 # MProperty are global to the model; it means that a MProperty is not bound
1170 # to a specific `MModule` nor a specific `MClass`.
1171 #
1172 # A MProperty gather definitions (see `mpropdefs') ; one for the introduction
1173 # and the other in subclasses and in refinements.
1174 #
1175 # A MProperty is used to denotes services in polymorphic way (ie. independent
1176 # of any dynamic type).
1177 # For instance, a call site "x.foo" is associated to a MProperty.
1178 abstract class MProperty
1179 # The associated MPropDef subclass.
1180 # The two specialization hierarchy are symmetric.
1181 type MPROPDEF: MPropDef
1182
1183 # The classdef that introduce the property
1184 # While a property is not bound to a specific module, or class,
1185 # the introducing mclassdef is used for naming and visibility
1186 var intro_mclassdef: MClassDef
1187
1188 # The (short) name of the property
1189 var name: String
1190
1191 # The canonical name of the property
1192 # Example: "owner::my_module::MyClass::my_method"
1193 fun full_name: String
1194 do
1195 return "{self.intro_mclassdef.mmodule.full_name}::{self.intro_mclassdef.mclass.name}::{name}"
1196 end
1197
1198 # The visibility of the property
1199 var visibility: MVisibility
1200
1201 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1202 do
1203 self.intro_mclassdef = intro_mclassdef
1204 self.name = name
1205 self.visibility = visibility
1206 intro_mclassdef.intro_mproperties.add(self)
1207 var model = intro_mclassdef.mmodule.model
1208 model.mproperties_by_name.add_one(name, self)
1209 model.mproperties.add(self)
1210 end
1211
1212 # All definitions of the property.
1213 # The first is the introduction,
1214 # The other are redefinitions (in refinements and in subclasses)
1215 var mpropdefs: Array[MPROPDEF] = new Array[MPROPDEF]
1216
1217 # The definition that introduced the property
1218 # Warning: the introduction is the first `MPropDef' object
1219 # associated to self. If self is just created without having any
1220 # associated definition, this method will abort
1221 fun intro: MPROPDEF do return mpropdefs.first
1222
1223 # Alias for `name'
1224 redef fun to_s do return name
1225
1226 # Return the most specific property definitions defined or inherited by a type.
1227 # The selection knows that refinement is stronger than specialization;
1228 # however, in case of conflict more than one property are returned.
1229 # If mtype does not know mproperty then an empty array is returned.
1230 #
1231 # If you want the really most specific property, then look at `lookup_first_definition`
1232 fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
1233 do
1234 assert not mtype.need_anchor
1235 if mtype isa MNullableType then mtype = mtype.mtype
1236
1237 var cache = self.lookup_definitions_cache[mmodule, mtype]
1238 if cache != null then return cache
1239
1240 #print "select prop {mproperty} for {mtype} in {self}"
1241 # First, select all candidates
1242 var candidates = new Array[MPROPDEF]
1243 for mpropdef in self.mpropdefs do
1244 # If the definition is not imported by the module, then skip
1245 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1246 # If the definition is not inherited by the type, then skip
1247 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1248 # Else, we keep it
1249 candidates.add(mpropdef)
1250 end
1251 # Fast track for only one candidate
1252 if candidates.length <= 1 then
1253 self.lookup_definitions_cache[mmodule, mtype] = candidates
1254 return candidates
1255 end
1256
1257 # Second, filter the most specific ones
1258 var res = new Array[MPROPDEF]
1259 for pd1 in candidates do
1260 var cd1 = pd1.mclassdef
1261 var c1 = cd1.mclass
1262 var keep = true
1263 for pd2 in candidates do
1264 if pd2 == pd1 then continue # do not compare with self!
1265 var cd2 = pd2.mclassdef
1266 var c2 = cd2.mclass
1267 if c2.mclass_type == c1.mclass_type then
1268 if cd2.mmodule.in_importation <= cd1.mmodule then
1269 # cd2 refines cd1; therefore we skip pd1
1270 keep = false
1271 break
1272 end
1273 else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
1274 # cd2 < cd1; therefore we skip pd1
1275 keep = false
1276 break
1277 end
1278 end
1279 if keep then
1280 res.add(pd1)
1281 end
1282 end
1283 if res.is_empty then
1284 print "All lost! {candidates.join(", ")}"
1285 # FIXME: should be abort!
1286 end
1287 self.lookup_definitions_cache[mmodule, mtype] = res
1288 return res
1289 end
1290
1291 private var lookup_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
1292
1293 # Return the most specific property definitions inherited by a type.
1294 # The selection knows that refinement is stronger than specialization;
1295 # however, in case of conflict more than one property are returned.
1296 # If mtype does not know mproperty then an empty array is returned.
1297 #
1298 # If you want the really most specific property, then look at `lookup_next_definition`
1299 #
1300 # FIXME: Move to MPropDef?
1301 fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPropDef]
1302 do
1303 assert not mtype.need_anchor
1304 if mtype isa MNullableType then mtype = mtype.mtype
1305
1306 # First, select all candidates
1307 var candidates = new Array[MPropDef]
1308 for mpropdef in self.mpropdefs do
1309 # If the definition is not imported by the module, then skip
1310 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1311 # If the definition is not inherited by the type, then skip
1312 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1313 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1314 if mtype == mpropdef.mclassdef.bound_mtype and mmodule == mpropdef.mclassdef.mmodule then continue
1315 # Else, we keep it
1316 candidates.add(mpropdef)
1317 end
1318 # Fast track for only one candidate
1319 if candidates.length <= 1 then return candidates
1320
1321 # Second, filter the most specific ones
1322 var res = new Array[MPropDef]
1323 for pd1 in candidates do
1324 var cd1 = pd1.mclassdef
1325 var c1 = cd1.mclass
1326 var keep = true
1327 for pd2 in candidates do
1328 if pd2 == pd1 then continue # do not compare with self!
1329 var cd2 = pd2.mclassdef
1330 var c2 = cd2.mclass
1331 if c2.mclass_type == c1.mclass_type then
1332 if cd2.mmodule.in_importation <= cd1.mmodule then
1333 # cd2 refines cd1; therefore we skip pd1
1334 keep = false
1335 break
1336 end
1337 else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
1338 # cd2 < cd1; therefore we skip pd1
1339 keep = false
1340 break
1341 end
1342 end
1343 if keep then
1344 res.add(pd1)
1345 end
1346 end
1347 if res.is_empty then
1348 print "All lost! {candidates.join(", ")}"
1349 # FIXME: should be abort!
1350 end
1351 return res
1352 end
1353
1354 # Return the most specific definition in the linearization of `mtype`.
1355 # If mtype does not know mproperty then null is returned.
1356 #
1357 # If you want to know the next properties in the linearization,
1358 # look at `MPropDef::lookup_next_definition`.
1359 #
1360 # FIXME: NOT YET IMPLEMENTED
1361 #
1362 # REQUIRE: not mtype.need_anchor
1363 fun lookup_first_definition(mmodule: MModule, mtype: MType): nullable MPROPDEF
1364 do
1365 assert not mtype.need_anchor
1366 return null
1367 end
1368 end
1369
1370 # A global method
1371 class MMethod
1372 super MProperty
1373
1374 redef type MPROPDEF: MMethodDef
1375
1376 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1377 do
1378 super
1379 end
1380
1381 # Is the property a constructor?
1382 # Warning, this property can be inherited by subclasses with or without being a constructor
1383 # therefore, you should use `is_init_for' the verify if the property is a legal constructor for a given class
1384 var is_init: Bool writable = false
1385
1386 # The the property a 'new' contructor?
1387 var is_new: Bool writable = false
1388
1389 # Is the property a legal constructor for a given class?
1390 # As usual, visibility is not considered.
1391 # FIXME not implemented
1392 fun is_init_for(mclass: MClass): Bool
1393 do
1394 return self.is_init
1395 end
1396 end
1397
1398 # A global attribute
1399 class MAttribute
1400 super MProperty
1401
1402 redef type MPROPDEF: MAttributeDef
1403
1404 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1405 do
1406 super
1407 end
1408 end
1409
1410 # A global virtual type
1411 class MVirtualTypeProp
1412 super MProperty
1413
1414 redef type MPROPDEF: MVirtualTypeDef
1415
1416 init(intro_mclassdef: MClassDef, name: String, visibility: MVisibility)
1417 do
1418 super
1419 end
1420
1421 # The formal type associated to the virtual type property
1422 var mvirtualtype: MVirtualType = new MVirtualType(self)
1423 end
1424
1425 # A definition of a property (local property)
1426 #
1427 # Unlike MProperty, a MPropDef is a local definition that belong to a
1428 # specific class definition (which belong to a specific module)
1429 abstract class MPropDef
1430
1431 # The associated MProperty subclass.
1432 # the two specialization hierarchy are symmetric
1433 type MPROPERTY: MProperty
1434
1435 # Self class
1436 type MPROPDEF: MPropDef
1437
1438 # The origin of the definition
1439 var location: Location
1440
1441 # The class definition where the property definition is
1442 var mclassdef: MClassDef
1443
1444 # The associated global property
1445 var mproperty: MPROPERTY
1446
1447 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1448 do
1449 self.mclassdef = mclassdef
1450 self.mproperty = mproperty
1451 self.location = location
1452 mclassdef.mpropdefs.add(self)
1453 mproperty.mpropdefs.add(self)
1454 end
1455
1456 # Internal name combining the module, the class and the property
1457 # Example: "mymodule#MyClass#mymethod"
1458 redef fun to_s
1459 do
1460 return "{mclassdef}#{mproperty}"
1461 end
1462
1463 # Is self the definition that introduce the property?
1464 fun is_intro: Bool do return mproperty.intro == self
1465
1466 # Return the next definition in linearization of `mtype`.
1467 # If there is no next method then null is returned.
1468 #
1469 # This method is used to determine what method is called by a super.
1470 #
1471 # FIXME: NOT YET IMPLEMENTED
1472 #
1473 # REQUIRE: not mtype.need_anchor
1474 fun lookup_next_definition(mmodule: MModule, mtype: MType): nullable MPROPDEF
1475 do
1476 assert not mtype.need_anchor
1477 return null
1478 end
1479 end
1480
1481 # A local definition of a method
1482 class MMethodDef
1483 super MPropDef
1484
1485 redef type MPROPERTY: MMethod
1486 redef type MPROPDEF: MMethodDef
1487
1488 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1489 do
1490 super
1491 end
1492
1493 # The signature attached to the property definition
1494 var msignature: nullable MSignature writable = null
1495 end
1496
1497 # A local definition of an attribute
1498 class MAttributeDef
1499 super MPropDef
1500
1501 redef type MPROPERTY: MAttribute
1502 redef type MPROPDEF: MAttributeDef
1503
1504 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1505 do
1506 super
1507 end
1508
1509 # The static type of the attribute
1510 var static_mtype: nullable MType writable = null
1511 end
1512
1513 # A local definition of a virtual type
1514 class MVirtualTypeDef
1515 super MPropDef
1516
1517 redef type MPROPERTY: MVirtualTypeProp
1518 redef type MPROPDEF: MVirtualTypeDef
1519
1520 init(mclassdef: MClassDef, mproperty: MPROPERTY, location: Location)
1521 do
1522 super
1523 end
1524
1525 # The bound of the virtual type
1526 var bound: nullable MType writable = null
1527 end
1528
1529 # A kind of class.
1530 #
1531 # * abstract_kind
1532 # * concrete_kind
1533 # * interface_kind
1534 # * enum_kind
1535 # * extern_kind
1536 #
1537 # Note this class is basically an enum.
1538 # FIXME: use a real enum once user-defined enums are available
1539 class MClassKind
1540 redef var to_s: String
1541
1542 # Is a constructor required?
1543 var need_init: Bool
1544 private init(s: String, need_init: Bool)
1545 do
1546 self.to_s = s
1547 self.need_init = need_init
1548 end
1549 end
1550
1551 fun abstract_kind: MClassKind do return once new MClassKind("abstract class", true)
1552 fun concrete_kind: MClassKind do return once new MClassKind("class", true)
1553 fun interface_kind: MClassKind do return once new MClassKind("interface", false)
1554 fun enum_kind: MClassKind do return once new MClassKind("enum", false)
1555 fun extern_kind: MClassKind do return once new MClassKind("extern", false)