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