model: promote `c_name` from abstract-compiler to model
[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 # Classes, types and properties
18 #
19 # All three concepts are defined in this same module because these are strongly connected:
20 # * types are based on classes
21 # * classes contains properties
22 # * some properties are types (virtual types)
23 #
24 # TODO: liearization, extern stuff
25 # FIXME: better handling of the types
26 module model
27
28 import mmodule
29 import mdoc
30 import ordered_tree
31 private import more_collections
32
33 redef class Model
34 # All known classes
35 var mclasses = new Array[MClass]
36
37 # All known properties
38 var mproperties = new Array[MProperty]
39
40 # Hierarchy of class definition.
41 #
42 # Each classdef is associated with its super-classdefs in regard to
43 # its module of definition.
44 var mclassdef_hierarchy = new POSet[MClassDef]
45
46 # Class-type hierarchy restricted to the introduction.
47 #
48 # The idea is that what is true on introduction is always true whatever
49 # the module considered.
50 # Therefore, this hierarchy is used for a fast positive subtype check.
51 #
52 # This poset will evolve in a monotonous way:
53 # * Two non connected nodes will remain unconnected
54 # * New nodes can appear with new edges
55 private var intro_mtype_specialization_hierarchy = new POSet[MClassType]
56
57 # Global overlapped class-type hierarchy.
58 # The hierarchy when all modules are combined.
59 # Therefore, this hierarchy is used for a fast negative subtype check.
60 #
61 # This poset will evolve in an anarchic way. Loops can even be created.
62 #
63 # FIXME decide what to do on loops
64 private var full_mtype_specialization_hierarchy = new POSet[MClassType]
65
66 # Collections of classes grouped by their short name
67 private var mclasses_by_name = new MultiHashMap[String, MClass]
68
69 # Return all class named `name`.
70 #
71 # If such a class does not exist, null is returned
72 # (instead of an empty array)
73 #
74 # Visibility or modules are not considered
75 fun get_mclasses_by_name(name: String): nullable Array[MClass]
76 do
77 if mclasses_by_name.has_key(name) then
78 return mclasses_by_name[name]
79 else
80 return null
81 end
82 end
83
84 # Collections of properties grouped by their short name
85 private var mproperties_by_name = new MultiHashMap[String, MProperty]
86
87 # Return all properties named `name`.
88 #
89 # If such a property does not exist, null is returned
90 # (instead of an empty array)
91 #
92 # Visibility or modules are not considered
93 fun get_mproperties_by_name(name: String): nullable Array[MProperty]
94 do
95 if not mproperties_by_name.has_key(name) then
96 return null
97 else
98 return mproperties_by_name[name]
99 end
100 end
101
102 # The only null type
103 var null_type = new MNullType(self)
104
105 # Build an ordered tree with from `concerns`
106 fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do
107 var seen = new HashSet[MConcern]
108 var res = new ConcernsTree
109
110 var todo = new Array[MConcern]
111 todo.add_all mconcerns
112
113 while not todo.is_empty do
114 var c = todo.pop
115 if seen.has(c) then continue
116 var pc = c.parent_concern
117 if pc == null then
118 res.add(null, c)
119 else
120 res.add(pc, c)
121 todo.add(pc)
122 end
123 seen.add(c)
124 end
125
126 return res
127 end
128 end
129
130 # An OrderedTree that can be easily refined for display purposes
131 class ConcernsTree
132 super OrderedTree[MConcern]
133 end
134
135 redef class MModule
136 # All the classes introduced in the module
137 var intro_mclasses = new Array[MClass]
138
139 # All the class definitions of the module
140 # (introduction and refinement)
141 var mclassdefs = new Array[MClassDef]
142
143 # Does the current module has a given class `mclass`?
144 # Return true if the mmodule introduces, refines or imports a class.
145 # Visibility is not considered.
146 fun has_mclass(mclass: MClass): Bool
147 do
148 return self.in_importation <= mclass.intro_mmodule
149 end
150
151 # Full hierarchy of introduced ans imported classes.
152 #
153 # Create a new hierarchy got by flattening the classes for the module
154 # and its imported modules.
155 # Visibility is not considered.
156 #
157 # Note: this function is expensive and is usually used for the main
158 # module of a program only. Do not use it to do you own subtype
159 # functions.
160 fun flatten_mclass_hierarchy: POSet[MClass]
161 do
162 var res = self.flatten_mclass_hierarchy_cache
163 if res != null then return res
164 res = new POSet[MClass]
165 for m in self.in_importation.greaters do
166 for cd in m.mclassdefs do
167 var c = cd.mclass
168 res.add_node(c)
169 for s in cd.supertypes do
170 res.add_edge(c, s.mclass)
171 end
172 end
173 end
174 self.flatten_mclass_hierarchy_cache = res
175 return res
176 end
177
178 # Sort a given array of classes using the linearization order of the module
179 # The most general is first, the most specific is last
180 fun linearize_mclasses(mclasses: Array[MClass])
181 do
182 self.flatten_mclass_hierarchy.sort(mclasses)
183 end
184
185 # Sort a given array of class definitions using the linearization order of the module
186 # the refinement link is stronger than the specialisation link
187 # The most general is first, the most specific is last
188 fun linearize_mclassdefs(mclassdefs: Array[MClassDef])
189 do
190 var sorter = new MClassDefSorter(self)
191 sorter.sort(mclassdefs)
192 end
193
194 # Sort a given array of property definitions using the linearization order of the module
195 # the refinement link is stronger than the specialisation link
196 # The most general is first, the most specific is last
197 fun linearize_mpropdefs(mpropdefs: Array[MPropDef])
198 do
199 var sorter = new MPropDefSorter(self)
200 sorter.sort(mpropdefs)
201 end
202
203 private var flatten_mclass_hierarchy_cache: nullable POSet[MClass] = null
204
205 # The primitive type `Object`, the root of the class hierarchy
206 fun object_type: MClassType
207 do
208 var res = self.object_type_cache
209 if res != null then return res
210 res = self.get_primitive_class("Object").mclass_type
211 self.object_type_cache = res
212 return res
213 end
214
215 private var object_type_cache: nullable MClassType
216
217 # The type `Pointer`, super class to all extern classes
218 var pointer_type: MClassType = self.get_primitive_class("Pointer").mclass_type is lazy
219
220 # The primitive type `Bool`
221 fun bool_type: MClassType
222 do
223 var res = self.bool_type_cache
224 if res != null then return res
225 res = self.get_primitive_class("Bool").mclass_type
226 self.bool_type_cache = res
227 return res
228 end
229
230 private var bool_type_cache: nullable MClassType
231
232 # The primitive type `Sys`, the main type of the program, if any
233 fun sys_type: nullable MClassType
234 do
235 var clas = self.model.get_mclasses_by_name("Sys")
236 if clas == null then return null
237 return get_primitive_class("Sys").mclass_type
238 end
239
240 # The primitive type `Finalizable`
241 # Used to tag classes that need to be finalized.
242 fun finalizable_type: nullable MClassType
243 do
244 var clas = self.model.get_mclasses_by_name("Finalizable")
245 if clas == null then return null
246 return get_primitive_class("Finalizable").mclass_type
247 end
248
249 # Force to get the primitive class named `name` or abort
250 fun get_primitive_class(name: String): MClass
251 do
252 var cla = self.model.get_mclasses_by_name(name)
253 if cla == null then
254 if name == "Bool" and self.model.get_mclasses_by_name("Object") != null then
255 # Bool is injected because it is needed by engine to code the result
256 # of the implicit casts.
257 var c = new MClass(self, name, null, enum_kind, public_visibility)
258 var cladef = new MClassDef(self, c.mclass_type, new Location(null, 0,0,0,0))
259 cladef.set_supertypes([object_type])
260 cladef.add_in_hierarchy
261 return c
262 end
263 print("Fatal Error: no primitive class {name}")
264 exit(1)
265 end
266 if cla.length != 1 then
267 var msg = "Fatal Error: more than one primitive class {name}:"
268 for c in cla do msg += " {c.full_name}"
269 print msg
270 #exit(1)
271 end
272 return cla.first
273 end
274
275 # Try to get the primitive method named `name` on the type `recv`
276 fun try_get_primitive_method(name: String, recv: MClass): nullable MMethod
277 do
278 var props = self.model.get_mproperties_by_name(name)
279 if props == null then return null
280 var res: nullable MMethod = null
281 for mprop in props do
282 assert mprop isa MMethod
283 var intro = mprop.intro_mclassdef
284 for mclassdef in recv.mclassdefs do
285 if not self.in_importation.greaters.has(mclassdef.mmodule) then continue
286 if not mclassdef.in_hierarchy.greaters.has(intro) then continue
287 if res == null then
288 res = mprop
289 else if res != mprop then
290 print("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
291 abort
292 end
293 end
294 end
295 return res
296 end
297 end
298
299 private class MClassDefSorter
300 super Comparator
301 redef type COMPARED: MClassDef
302 var mmodule: MModule
303 redef fun compare(a, b)
304 do
305 var ca = a.mclass
306 var cb = b.mclass
307 if ca != cb then return mmodule.flatten_mclass_hierarchy.compare(ca, cb)
308 return mmodule.model.mclassdef_hierarchy.compare(a, b)
309 end
310 end
311
312 private class MPropDefSorter
313 super Comparator
314 redef type COMPARED: MPropDef
315 var mmodule: MModule
316 redef fun compare(pa, pb)
317 do
318 var a = pa.mclassdef
319 var b = pb.mclassdef
320 var ca = a.mclass
321 var cb = b.mclass
322 if ca != cb then return mmodule.flatten_mclass_hierarchy.compare(ca, cb)
323 return mmodule.model.mclassdef_hierarchy.compare(a, b)
324 end
325 end
326
327 # A named class
328 #
329 # `MClass` are global to the model; it means that a `MClass` is not bound to a
330 # specific `MModule`.
331 #
332 # This characteristic helps the reasoning about classes in a program since a
333 # single `MClass` object always denote the same class.
334 #
335 # The drawback is that classes (`MClass`) contain almost nothing by themselves.
336 # These do not really have properties nor belong to a hierarchy since the property and the
337 # hierarchy of a class depends of the refinement in the modules.
338 #
339 # Most services on classes require the precision of a module, and no one can asks what are
340 # the super-classes of a class nor what are properties of a class without precising what is
341 # the module considered.
342 #
343 # For instance, during the typing of a source-file, the module considered is the module of the file.
344 # eg. the question *is the method `foo` exists in the class `Bar`?* must be reformulated into
345 # *is the method `foo` exists in the class `Bar` in the current module?*
346 #
347 # During some global analysis, the module considered may be the main module of the program.
348 class MClass
349 super MEntity
350
351 # The module that introduce the class
352 # While classes are not bound to a specific module,
353 # the introducing module is used for naming an visibility
354 var intro_mmodule: MModule
355
356 # The short name of the class
357 # In Nit, the name of a class cannot evolve in refinements
358 redef var name: String
359
360 # The canonical name of the class
361 #
362 # It is the name of the class prefixed by the full_name of the `intro_mmodule`
363 # Example: `"owner::module::MyClass"`
364 redef var full_name is lazy do return "{self.intro_mmodule.full_name}::{name}"
365
366 redef var c_name is lazy do return "{intro_mmodule.c_name}__{name.to_cmangle}"
367
368 # The number of generic formal parameters
369 # 0 if the class is not generic
370 var arity: Int is noinit
371
372 # Each generic formal parameters in order.
373 # is empty if the class is not generic
374 var mparameters = new Array[MParameterType]
375
376 protected fun setup_parameter_names(parameter_names: nullable Array[String]) is
377 autoinit
378 do
379 if parameter_names == null then
380 self.arity = 0
381 else
382 self.arity = parameter_names.length
383 end
384
385 # Create the formal parameter types
386 if arity > 0 then
387 assert parameter_names != null
388 var mparametertypes = new Array[MParameterType]
389 for i in [0..arity[ do
390 var mparametertype = new MParameterType(self, i, parameter_names[i])
391 mparametertypes.add(mparametertype)
392 end
393 self.mparameters = mparametertypes
394 var mclass_type = new MGenericType(self, mparametertypes)
395 self.mclass_type = mclass_type
396 self.get_mtype_cache[mparametertypes] = mclass_type
397 else
398 self.mclass_type = new MClassType(self)
399 end
400 end
401
402 # The kind of the class (interface, abstract class, etc.)
403 # In Nit, the kind of a class cannot evolve in refinements
404 var kind: MClassKind
405
406 # The visibility of the class
407 # In Nit, the visibility of a class cannot evolve in refinements
408 var visibility: MVisibility
409
410 init
411 do
412 intro_mmodule.intro_mclasses.add(self)
413 var model = intro_mmodule.model
414 model.mclasses_by_name.add_one(name, self)
415 model.mclasses.add(self)
416 end
417
418 redef fun model do return intro_mmodule.model
419
420 # All class definitions (introduction and refinements)
421 var mclassdefs = new Array[MClassDef]
422
423 # Alias for `name`
424 redef fun to_s do return self.name
425
426 # The definition that introduces the class.
427 #
428 # Warning: such a definition may not exist in the early life of the object.
429 # In this case, the method will abort.
430 var intro: MClassDef is noinit
431
432 # Return the class `self` in the class hierarchy of the module `mmodule`.
433 #
434 # SEE: `MModule::flatten_mclass_hierarchy`
435 # REQUIRE: `mmodule.has_mclass(self)`
436 fun in_hierarchy(mmodule: MModule): POSetElement[MClass]
437 do
438 return mmodule.flatten_mclass_hierarchy[self]
439 end
440
441 # The principal static type of the class.
442 #
443 # For non-generic class, mclass_type is the only `MClassType` based
444 # on self.
445 #
446 # For a generic class, the arguments are the formal parameters.
447 # i.e.: for the class Array[E:Object], the `mclass_type` is Array[E].
448 # If you want Array[Object] the see `MClassDef::bound_mtype`
449 #
450 # For generic classes, the mclass_type is also the way to get a formal
451 # generic parameter type.
452 #
453 # To get other types based on a generic class, see `get_mtype`.
454 #
455 # ENSURE: `mclass_type.mclass == self`
456 var mclass_type: MClassType is noinit
457
458 # Return a generic type based on the class
459 # Is the class is not generic, then the result is `mclass_type`
460 #
461 # REQUIRE: `mtype_arguments.length == self.arity`
462 fun get_mtype(mtype_arguments: Array[MType]): MClassType
463 do
464 assert mtype_arguments.length == self.arity
465 if self.arity == 0 then return self.mclass_type
466 var res = get_mtype_cache.get_or_null(mtype_arguments)
467 if res != null then return res
468 res = new MGenericType(self, mtype_arguments)
469 self.get_mtype_cache[mtype_arguments.to_a] = res
470 return res
471 end
472
473 private var get_mtype_cache = new HashMap[Array[MType], MGenericType]
474 end
475
476
477 # A definition (an introduction or a refinement) of a class in a module
478 #
479 # A `MClassDef` is associated with an explicit (or almost) definition of a
480 # class. Unlike `MClass`, a `MClassDef` is a local definition that belong to
481 # a specific class and a specific module, and contains declarations like super-classes
482 # or properties.
483 #
484 # It is the class definitions that are the backbone of most things in the model:
485 # ClassDefs are defined with regard with other classdefs.
486 # Refinement and specialization are combined to produce a big poset called the `Model::mclassdef_hierarchy`.
487 #
488 # Moreover, the extension and the intention of types is defined by looking at the MClassDefs.
489 class MClassDef
490 super MEntity
491
492 # The module where the definition is
493 var mmodule: MModule
494
495 # The associated `MClass`
496 var mclass: MClass is noinit
497
498 # The bounded type associated to the mclassdef
499 #
500 # For a non-generic class, `bound_mtype` and `mclass.mclass_type`
501 # are the same type.
502 #
503 # Example:
504 # For the classdef Array[E: Object], the bound_mtype is Array[Object].
505 # If you want Array[E], then see `mclass.mclass_type`
506 #
507 # ENSURE: `bound_mtype.mclass == self.mclass`
508 var bound_mtype: MClassType
509
510 # The origin of the definition
511 var location: Location
512
513 # Internal name combining the module and the class
514 # Example: "mymodule#MyClass"
515 redef var to_s: String is noinit
516
517 init
518 do
519 self.mclass = bound_mtype.mclass
520 mmodule.mclassdefs.add(self)
521 mclass.mclassdefs.add(self)
522 if mclass.intro_mmodule == mmodule then
523 assert not isset mclass._intro
524 mclass.intro = self
525 end
526 self.to_s = "{mmodule}#{mclass}"
527 end
528
529 # Actually the name of the `mclass`
530 redef fun name do return mclass.name
531
532 # The module and class name separated by a '#'.
533 #
534 # The short-name of the class is used for introduction.
535 # Example: "my_module#MyClass"
536 #
537 # The full-name of the class is used for refinement.
538 # Example: "my_module#intro_module::MyClass"
539 redef var full_name is lazy do
540 if is_intro then
541 return "{mmodule.full_name}#{mclass.name}"
542 else
543 return "{mmodule.full_name}#{mclass.full_name}"
544 end
545 end
546
547 redef var c_name is lazy do
548 if is_intro then
549 return mclass.c_name
550 else
551 return "{mmodule.c_name}__{mclass.c_name.to_cmangle}"
552 end
553 end
554
555 redef fun model do return mmodule.model
556
557 # All declared super-types
558 # FIXME: quite ugly but not better idea yet
559 var supertypes = new Array[MClassType]
560
561 # Register some super-types for the class (ie "super SomeType")
562 #
563 # The hierarchy must not already be set
564 # REQUIRE: `self.in_hierarchy == null`
565 fun set_supertypes(supertypes: Array[MClassType])
566 do
567 assert unique_invocation: self.in_hierarchy == null
568 var mmodule = self.mmodule
569 var model = mmodule.model
570 var mtype = self.bound_mtype
571
572 for supertype in supertypes do
573 self.supertypes.add(supertype)
574
575 # Register in full_type_specialization_hierarchy
576 model.full_mtype_specialization_hierarchy.add_edge(mtype, supertype)
577 # Register in intro_type_specialization_hierarchy
578 if mclass.intro_mmodule == mmodule and supertype.mclass.intro_mmodule == mmodule then
579 model.intro_mtype_specialization_hierarchy.add_edge(mtype, supertype)
580 end
581 end
582
583 end
584
585 # Collect the super-types (set by set_supertypes) to build the hierarchy
586 #
587 # This function can only invoked once by class
588 # REQUIRE: `self.in_hierarchy == null`
589 # ENSURE: `self.in_hierarchy != null`
590 fun add_in_hierarchy
591 do
592 assert unique_invocation: self.in_hierarchy == null
593 var model = mmodule.model
594 var res = model.mclassdef_hierarchy.add_node(self)
595 self.in_hierarchy = res
596 var mtype = self.bound_mtype
597
598 # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
599 # The simpliest way is to attach it to collect_mclassdefs
600 for mclassdef in mtype.collect_mclassdefs(mmodule) do
601 res.poset.add_edge(self, mclassdef)
602 end
603 end
604
605 # The view of the class definition in `mclassdef_hierarchy`
606 var in_hierarchy: nullable POSetElement[MClassDef] = null
607
608 # Is the definition the one that introduced `mclass`?
609 fun is_intro: Bool do return mclass.intro == self
610
611 # All properties introduced by the classdef
612 var intro_mproperties = new Array[MProperty]
613
614 # All property definitions in the class (introductions and redefinitions)
615 var mpropdefs = new Array[MPropDef]
616 end
617
618 # A global static type
619 #
620 # MType are global to the model; it means that a `MType` is not bound to a
621 # specific `MModule`.
622 # This characteristic helps the reasoning about static types in a program
623 # since a single `MType` object always denote the same type.
624 #
625 # However, because a `MType` is global, it does not really have properties
626 # nor have subtypes to a hierarchy since the property and the class hierarchy
627 # depends of a module.
628 # Moreover, virtual types an formal generic parameter types also depends on
629 # a receiver to have sense.
630 #
631 # Therefore, most method of the types require a module and an anchor.
632 # The module is used to know what are the classes and the specialization
633 # links.
634 # The anchor is used to know what is the bound of the virtual types and formal
635 # generic parameter types.
636 #
637 # MType are not directly usable to get properties. See the `anchor_to` method
638 # and the `MClassType` class.
639 #
640 # FIXME: the order of the parameters is not the best. We mus pick on from:
641 # * foo(mmodule, anchor, othertype)
642 # * foo(othertype, anchor, mmodule)
643 # * foo(anchor, mmodule, othertype)
644 # * foo(othertype, mmodule, anchor)
645 abstract class MType
646 super MEntity
647
648 redef fun name do return to_s
649
650 # Return true if `self` is an subtype of `sup`.
651 # The typing is done using the standard typing policy of Nit.
652 #
653 # REQUIRE: `anchor == null implies not self.need_anchor and not sup.need_anchor`
654 # REQUIRE: `anchor != null implies self.can_resolve_for(anchor, null, mmodule) and sup.can_resolve_for(anchor, null, mmodule)`
655 fun is_subtype(mmodule: MModule, anchor: nullable MClassType, sup: MType): Bool
656 do
657 var sub = self
658 if sub == sup then return true
659
660 #print "1.is {sub} a {sup}? ===="
661
662 if anchor == null then
663 assert not sub.need_anchor
664 assert not sup.need_anchor
665 else
666 # First, resolve the formal types to the simplest equivalent forms in the receiver
667 assert sub.can_resolve_for(anchor, null, mmodule)
668 sub = sub.lookup_fixed(mmodule, anchor)
669 assert sup.can_resolve_for(anchor, null, mmodule)
670 sup = sup.lookup_fixed(mmodule, anchor)
671 end
672
673 # Does `sup` accept null or not?
674 # Discard the nullable marker if it exists
675 var sup_accept_null = false
676 if sup isa MNullableType then
677 sup_accept_null = true
678 sup = sup.mtype
679 else if sup isa MNullType then
680 sup_accept_null = true
681 end
682
683 # Can `sub` provide null or not?
684 # Thus we can match with `sup_accept_null`
685 # Also discard the nullable marker if it exists
686 if sub isa MNullableType then
687 if not sup_accept_null then return false
688 sub = sub.mtype
689 else if sub isa MNullType then
690 return sup_accept_null
691 end
692 # Now the case of direct null and nullable is over.
693
694 # If `sub` is a formal type, then it is accepted if its bound is accepted
695 while sub isa MParameterType or sub isa MVirtualType do
696 #print "3.is {sub} a {sup}?"
697
698 # A unfixed formal type can only accept itself
699 if sub == sup then return true
700
701 assert anchor != null
702 sub = sub.lookup_bound(mmodule, anchor)
703
704 #print "3.is {sub} a {sup}?"
705
706 # Manage the second layer of null/nullable
707 if sub isa MNullableType then
708 if not sup_accept_null then return false
709 sub = sub.mtype
710 else if sub isa MNullType then
711 return sup_accept_null
712 end
713 end
714 #print "4.is {sub} a {sup}? <- no more resolution"
715
716 assert sub isa MClassType # It is the only remaining type
717
718 # A unfixed formal type can only accept itself
719 if sup isa MParameterType or sup isa MVirtualType then
720 return false
721 end
722
723 if sup isa MNullType then
724 # `sup` accepts only null
725 return false
726 end
727
728 assert sup isa MClassType # It is the only remaining type
729
730 # Now both are MClassType, we need to dig
731
732 if sub == sup then return true
733
734 if anchor == null then anchor = sub # UGLY: any anchor will work
735 var resolved_sub = sub.anchor_to(mmodule, anchor)
736 var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
737 if res == false then return false
738 if not sup isa MGenericType then return true
739 var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
740 assert sub2.mclass == sup.mclass
741 for i in [0..sup.mclass.arity[ do
742 var sub_arg = sub2.arguments[i]
743 var sup_arg = sup.arguments[i]
744 res = sub_arg.is_subtype(mmodule, anchor, sup_arg)
745 if res == false then return false
746 end
747 return true
748 end
749
750 # The base class type on which self is based
751 #
752 # This base type is used to get property (an internally to perform
753 # unsafe type comparison).
754 #
755 # Beware: some types (like null) are not based on a class thus this
756 # method will crash
757 #
758 # Basically, this function transform the virtual types and parameter
759 # types to their bounds.
760 #
761 # Example
762 #
763 # class A end
764 # class B super A end
765 # class X end
766 # class Y super X end
767 # class G[T: A]
768 # type U: X
769 # end
770 # class H
771 # super G[B]
772 # redef type U: Y
773 # end
774 #
775 # Map[T,U] anchor_to H #-> Map[B,Y]
776 #
777 # Explanation of the example:
778 # In H, T is set to B, because "H super G[B]", and U is bound to Y,
779 # because "redef type U: Y". Therefore, Map[T, U] is bound to
780 # Map[B, Y]
781 #
782 # ENSURE: `not self.need_anchor implies result == self`
783 # ENSURE: `not result.need_anchor`
784 fun anchor_to(mmodule: MModule, anchor: MClassType): MType
785 do
786 if not need_anchor then return self
787 assert not anchor.need_anchor
788 # Just resolve to the anchor and clear all the virtual types
789 var res = self.resolve_for(anchor, null, mmodule, true)
790 assert not res.need_anchor
791 return res
792 end
793
794 # Does `self` contain a virtual type or a formal generic parameter type?
795 # In order to remove those types, you usually want to use `anchor_to`.
796 fun need_anchor: Bool do return true
797
798 # Return the supertype when adapted to a class.
799 #
800 # In Nit, for each super-class of a type, there is a equivalent super-type.
801 #
802 # Example:
803 #
804 # ~~~nitish
805 # class G[T, U] end
806 # class H[V] super G[V, Bool] end
807 #
808 # H[Int] supertype_to G #-> G[Int, Bool]
809 # ~~~
810 #
811 # REQUIRE: `super_mclass` is a super-class of `self`
812 # REQUIRE: `self.need_anchor implies anchor != null and self.can_resolve_for(anchor, null, mmodule)`
813 # ENSURE: `result.mclass = super_mclass`
814 fun supertype_to(mmodule: MModule, anchor: nullable MClassType, super_mclass: MClass): MClassType
815 do
816 if super_mclass.arity == 0 then return super_mclass.mclass_type
817 if self isa MClassType and self.mclass == super_mclass then return self
818 var resolved_self
819 if self.need_anchor then
820 assert anchor != null
821 resolved_self = self.anchor_to(mmodule, anchor)
822 else
823 resolved_self = self
824 end
825 var supertypes = resolved_self.collect_mtypes(mmodule)
826 for supertype in supertypes do
827 if supertype.mclass == super_mclass then
828 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
829 return supertype.resolve_for(self, anchor, mmodule, false)
830 end
831 end
832 abort
833 end
834
835 # Replace formals generic types in self with resolved values in `mtype`
836 # If `cleanup_virtual` is true, then virtual types are also replaced
837 # with their bounds.
838 #
839 # This function returns self if `need_anchor` is false.
840 #
841 # ## Example 1
842 #
843 # ~~~
844 # class G[E] end
845 # class H[F] super G[F] end
846 # class X[Z] end
847 # ~~~
848 #
849 # * Array[E].resolve_for(H[Int]) #-> Array[Int]
850 # * Array[E].resolve_for(G[Z], X[Int]) #-> Array[Z]
851 #
852 # Explanation of the example:
853 # * Array[E].need_anchor is true because there is a formal generic parameter type E
854 # * E makes sense for H[Int] because E is a formal parameter of G and H specialize G
855 # * Since "H[F] super G[F]", E is in fact F for H
856 # * More specifically, in H[Int], E is Int
857 # * So, in H[Int], Array[E] is Array[Int]
858 #
859 # This function is mainly used to inherit a signature.
860 # Because, unlike `anchor_to`, we do not want a full resolution of
861 # a type but only an adapted version of it.
862 #
863 # ## Example 2
864 #
865 # ~~~
866 # class A[E]
867 # fun foo(e:E):E is abstract
868 # end
869 # class B super A[Int] end
870 # ~~~
871 #
872 # The signature on foo is (e: E): E
873 # If we resolve the signature for B, we get (e:Int):Int
874 #
875 # ## Example 3
876 #
877 # ~~~nitish
878 # class A[E]
879 # fun foo(e:E):E is abstract
880 # end
881 # class C[F]
882 # var a: A[Array[F]]
883 # fun bar do a.foo(x) # <- x is here
884 # end
885 # ~~~
886 #
887 # The first question is: is foo available on `a`?
888 #
889 # The static type of a is `A[Array[F]]`, that is an open type.
890 # in order to find a method `foo`, whe must look at a resolved type.
891 #
892 # A[Array[F]].anchor_to(C[nullable Object]) #-> A[Array[nullable Object]]
893 #
894 # the method `foo` exists in `A[Array[nullable Object]]`, therefore `foo` exists for `a`.
895 #
896 # The next question is: what is the accepted types for `x`?
897 #
898 # the signature of `foo` is `foo(e:E)`, thus we must resolve the type E
899 #
900 # E.resolve_for(A[Array[F]],C[nullable Object]) #-> Array[F]
901 #
902 # The resolution can be done because `E` make sense for the class A (see `can_resolve_for`)
903 #
904 # FIXME: the parameter `cleanup_virtual` is just a bad idea, but having
905 # two function instead of one seems also to be a bad idea.
906 #
907 # REQUIRE: `can_resolve_for(mtype, anchor, mmodule)`
908 # ENSURE: `not self.need_anchor implies result == self`
909 fun resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule, cleanup_virtual: Bool): MType is abstract
910
911 # Resolve formal type to its verbatim bound.
912 # If the type is not formal, just return self
913 #
914 # The result is returned exactly as declared in the "type" property (verbatim).
915 # So it could be another formal type.
916 #
917 # In case of conflict, the method aborts.
918 fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType do return self
919
920 # Resolve the formal type to its simplest equivalent form.
921 #
922 # Formal types are either free or fixed.
923 # When it is fixed, it means that it is equivalent with a simpler type.
924 # When a formal type is free, it means that it is only equivalent with itself.
925 # This method return the most simple equivalent type of `self`.
926 #
927 # This method is mainly used for subtype test in order to sanely compare fixed.
928 #
929 # By default, return self.
930 # See the redefinitions for specific behavior in each kind of type.
931 fun lookup_fixed(mmodule: MModule, resolved_receiver: MType): MType do return self
932
933 # Can the type be resolved?
934 #
935 # In order to resolve open types, the formal types must make sence.
936 #
937 # ## Example
938 #
939 # class A[E]
940 # end
941 # class B[F]
942 # end
943 #
944 # ~~~nitish
945 # E.can_resolve_for(A[Int]) #-> true, E make sense in A
946 #
947 # E.can_resolve_for(B[Int]) #-> false, E does not make sense in B
948 #
949 # B[E].can_resolve_for(A[F], B[Object]) #-> true,
950 # # B[E] is a red hearing only the E is important,
951 # # E make sense in A
952 # ~~~
953 #
954 # REQUIRE: `anchor != null implies not anchor.need_anchor`
955 # REQUIRE: `mtype.need_anchor implies anchor != null and mtype.can_resolve_for(anchor, null, mmodule)`
956 # ENSURE: `not self.need_anchor implies result == true`
957 fun can_resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule): Bool is abstract
958
959 # Return the nullable version of the type
960 # If the type is already nullable then self is returned
961 fun as_nullable: MType
962 do
963 var res = self.as_nullable_cache
964 if res != null then return res
965 res = new MNullableType(self)
966 self.as_nullable_cache = res
967 return res
968 end
969
970 # Return the not nullable version of the type
971 # Is the type is already not nullable, then self is returned.
972 #
973 # Note: this just remove the `nullable` notation, but the result can still contains null.
974 # For instance if `self isa MNullType` or self is a formal type bounded by a nullable type.
975 fun as_notnullable: MType
976 do
977 return self
978 end
979
980 private var as_nullable_cache: nullable MType = null
981
982
983 # The depth of the type seen as a tree.
984 #
985 # * A -> 1
986 # * G[A] -> 2
987 # * H[A, B] -> 2
988 # * H[G[A], B] -> 3
989 #
990 # Formal types have a depth of 1.
991 fun depth: Int
992 do
993 return 1
994 end
995
996 # The length of the type seen as a tree.
997 #
998 # * A -> 1
999 # * G[A] -> 2
1000 # * H[A, B] -> 3
1001 # * H[G[A], B] -> 4
1002 #
1003 # Formal types have a length of 1.
1004 fun length: Int
1005 do
1006 return 1
1007 end
1008
1009 # Compute all the classdefs inherited/imported.
1010 # The returned set contains:
1011 # * the class definitions from `mmodule` and its imported modules
1012 # * the class definitions of this type and its super-types
1013 #
1014 # This function is used mainly internally.
1015 #
1016 # REQUIRE: `not self.need_anchor`
1017 fun collect_mclassdefs(mmodule: MModule): Set[MClassDef] is abstract
1018
1019 # Compute all the super-classes.
1020 # This function is used mainly internally.
1021 #
1022 # REQUIRE: `not self.need_anchor`
1023 fun collect_mclasses(mmodule: MModule): Set[MClass] is abstract
1024
1025 # Compute all the declared super-types.
1026 # Super-types are returned as declared in the classdefs (verbatim).
1027 # This function is used mainly internally.
1028 #
1029 # REQUIRE: `not self.need_anchor`
1030 fun collect_mtypes(mmodule: MModule): Set[MClassType] is abstract
1031
1032 # Is the property in self for a given module
1033 # This method does not filter visibility or whatever
1034 #
1035 # REQUIRE: `not self.need_anchor`
1036 fun has_mproperty(mmodule: MModule, mproperty: MProperty): Bool
1037 do
1038 assert not self.need_anchor
1039 return self.collect_mclassdefs(mmodule).has(mproperty.intro_mclassdef)
1040 end
1041 end
1042
1043 # A type based on a class.
1044 #
1045 # `MClassType` have properties (see `has_mproperty`).
1046 class MClassType
1047 super MType
1048
1049 # The associated class
1050 var mclass: MClass
1051
1052 redef fun model do return self.mclass.intro_mmodule.model
1053
1054 # TODO: private init because strongly bounded to its mclass. see `mclass.mclass_type`
1055
1056 # The formal arguments of the type
1057 # ENSURE: `result.length == self.mclass.arity`
1058 var arguments = new Array[MType]
1059
1060 redef fun to_s do return mclass.to_s
1061
1062 redef fun full_name do return mclass.full_name
1063
1064 redef fun c_name do return mclass.c_name
1065
1066 redef fun need_anchor do return false
1067
1068 redef fun anchor_to(mmodule: MModule, anchor: MClassType): MClassType
1069 do
1070 return super.as(MClassType)
1071 end
1072
1073 redef fun resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule, cleanup_virtual: Bool): MClassType do return self
1074
1075 redef fun can_resolve_for(mtype, anchor, mmodule) do return true
1076
1077 redef fun collect_mclassdefs(mmodule)
1078 do
1079 assert not self.need_anchor
1080 var cache = self.collect_mclassdefs_cache
1081 if not cache.has_key(mmodule) then
1082 self.collect_things(mmodule)
1083 end
1084 return cache[mmodule]
1085 end
1086
1087 redef fun collect_mclasses(mmodule)
1088 do
1089 if collect_mclasses_last_module == mmodule then return collect_mclasses_last_module_cache
1090 assert not self.need_anchor
1091 var cache = self.collect_mclasses_cache
1092 if not cache.has_key(mmodule) then
1093 self.collect_things(mmodule)
1094 end
1095 var res = cache[mmodule]
1096 collect_mclasses_last_module = mmodule
1097 collect_mclasses_last_module_cache = res
1098 return res
1099 end
1100
1101 private var collect_mclasses_last_module: nullable MModule = null
1102 private var collect_mclasses_last_module_cache: Set[MClass] is noinit
1103
1104 redef fun collect_mtypes(mmodule)
1105 do
1106 assert not self.need_anchor
1107 var cache = self.collect_mtypes_cache
1108 if not cache.has_key(mmodule) then
1109 self.collect_things(mmodule)
1110 end
1111 return cache[mmodule]
1112 end
1113
1114 # common implementation for `collect_mclassdefs`, `collect_mclasses`, and `collect_mtypes`.
1115 private fun collect_things(mmodule: MModule)
1116 do
1117 var res = new HashSet[MClassDef]
1118 var seen = new HashSet[MClass]
1119 var types = new HashSet[MClassType]
1120 seen.add(self.mclass)
1121 var todo = [self.mclass]
1122 while not todo.is_empty do
1123 var mclass = todo.pop
1124 #print "process {mclass}"
1125 for mclassdef in mclass.mclassdefs do
1126 if not mmodule.in_importation <= mclassdef.mmodule then continue
1127 #print " process {mclassdef}"
1128 res.add(mclassdef)
1129 for supertype in mclassdef.supertypes do
1130 types.add(supertype)
1131 var superclass = supertype.mclass
1132 if seen.has(superclass) then continue
1133 #print " add {superclass}"
1134 seen.add(superclass)
1135 todo.add(superclass)
1136 end
1137 end
1138 end
1139 collect_mclassdefs_cache[mmodule] = res
1140 collect_mclasses_cache[mmodule] = seen
1141 collect_mtypes_cache[mmodule] = types
1142 end
1143
1144 private var collect_mclassdefs_cache = new HashMap[MModule, Set[MClassDef]]
1145 private var collect_mclasses_cache = new HashMap[MModule, Set[MClass]]
1146 private var collect_mtypes_cache = new HashMap[MModule, Set[MClassType]]
1147
1148 end
1149
1150 # A type based on a generic class.
1151 # A generic type a just a class with additional formal generic arguments.
1152 class MGenericType
1153 super MClassType
1154
1155 redef var arguments
1156
1157 # TODO: private init because strongly bounded to its mclass. see `mclass.get_mtype`
1158
1159 init
1160 do
1161 assert self.mclass.arity == arguments.length
1162
1163 self.need_anchor = false
1164 for t in arguments do
1165 if t.need_anchor then
1166 self.need_anchor = true
1167 break
1168 end
1169 end
1170
1171 self.to_s = "{mclass}[{arguments.join(", ")}]"
1172 end
1173
1174 # The short-name of the class, then the full-name of each type arguments within brackets.
1175 # Example: `"Map[String, List[Int]]"`
1176 redef var to_s: String is noinit
1177
1178 # The full-name of the class, then the full-name of each type arguments within brackets.
1179 # Example: `"standard::Map[standard::String, standard::List[standard::Int]]"`
1180 redef var full_name is lazy do
1181 var args = new Array[String]
1182 for t in arguments do
1183 args.add t.full_name
1184 end
1185 return "{mclass.full_name}[{args.join(", ")}]}"
1186 end
1187
1188 redef var c_name is lazy do
1189 var res = mclass.c_name
1190 # Note: because the arity is known, a prefix notation is enough
1191 for t in arguments do
1192 res += "__"
1193 res += t.c_name
1194 end
1195 return res.to_s
1196 end
1197
1198 redef var need_anchor: Bool is noinit
1199
1200 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1201 do
1202 if not need_anchor then return self
1203 assert can_resolve_for(mtype, anchor, mmodule)
1204 var types = new Array[MType]
1205 for t in arguments do
1206 types.add(t.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1207 end
1208 return mclass.get_mtype(types)
1209 end
1210
1211 redef fun can_resolve_for(mtype, anchor, mmodule)
1212 do
1213 if not need_anchor then return true
1214 for t in arguments do
1215 if not t.can_resolve_for(mtype, anchor, mmodule) then return false
1216 end
1217 return true
1218 end
1219
1220
1221 redef fun depth
1222 do
1223 var dmax = 0
1224 for a in self.arguments do
1225 var d = a.depth
1226 if d > dmax then dmax = d
1227 end
1228 return dmax + 1
1229 end
1230
1231 redef fun length
1232 do
1233 var res = 1
1234 for a in self.arguments do
1235 res += a.length
1236 end
1237 return res
1238 end
1239 end
1240
1241 # A virtual formal type.
1242 class MVirtualType
1243 super MType
1244
1245 # The property associated with the type.
1246 # Its the definitions of this property that determine the bound or the virtual type.
1247 var mproperty: MVirtualTypeProp
1248
1249 redef fun model do return self.mproperty.intro_mclassdef.mmodule.model
1250
1251 redef fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
1252 do
1253 return lookup_single_definition(mmodule, resolved_receiver).bound.as(not null)
1254 end
1255
1256 private fun lookup_single_definition(mmodule: MModule, resolved_receiver: MType): MVirtualTypeDef
1257 do
1258 assert not resolved_receiver.need_anchor
1259 var props = self.mproperty.lookup_definitions(mmodule, resolved_receiver)
1260 if props.is_empty then
1261 abort
1262 else if props.length == 1 then
1263 return props.first
1264 end
1265 var types = new ArraySet[MType]
1266 var res = props.first
1267 for p in props do
1268 types.add(p.bound.as(not null))
1269 if not res.is_fixed then res = p
1270 end
1271 if types.length == 1 then
1272 return res
1273 end
1274 abort
1275 end
1276
1277 # A VT is fixed when:
1278 # * the VT is (re-)defined with the annotation `is fixed`
1279 # * the VT is (indirectly) bound to an enum class (see `enum_kind`) since there is no subtype possible
1280 # * the receiver is an enum class since there is no subtype possible
1281 redef fun lookup_fixed(mmodule: MModule, resolved_receiver: MType): MType
1282 do
1283 assert not resolved_receiver.need_anchor
1284 resolved_receiver = resolved_receiver.as_notnullable
1285 assert resolved_receiver isa MClassType # It is the only remaining type
1286
1287 var prop = lookup_single_definition(mmodule, resolved_receiver)
1288 var res = prop.bound.as(not null)
1289
1290 # Recursively lookup the fixed result
1291 res = res.lookup_fixed(mmodule, resolved_receiver)
1292
1293 # 1. For a fixed VT, return the resolved bound
1294 if prop.is_fixed then return res
1295
1296 # 2. For a enum boud, return the bound
1297 if res isa MClassType and res.mclass.kind == enum_kind then return res
1298
1299 # 3. for a enum receiver return the bound
1300 if resolved_receiver.mclass.kind == enum_kind then return res
1301
1302 return self
1303 end
1304
1305 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1306 do
1307 if not cleanup_virtual then return self
1308 assert can_resolve_for(mtype, anchor, mmodule)
1309 # self is a virtual type declared (or inherited) in mtype
1310 # The point of the function it to get the bound of the virtual type that make sense for mtype
1311 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1312 #print "{class_name}: {self}/{mtype}/{anchor}?"
1313 var resolved_receiver
1314 if mtype.need_anchor then
1315 assert anchor != null
1316 resolved_receiver = mtype.resolve_for(anchor, null, mmodule, true)
1317 else
1318 resolved_receiver = mtype
1319 end
1320 # Now, we can get the bound
1321 var verbatim_bound = lookup_bound(mmodule, resolved_receiver)
1322 # The bound is exactly as declared in the "type" property, so we must resolve it again
1323 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1324
1325 return res
1326 end
1327
1328 redef fun can_resolve_for(mtype, anchor, mmodule)
1329 do
1330 if mtype.need_anchor then
1331 assert anchor != null
1332 mtype = mtype.anchor_to(mmodule, anchor)
1333 end
1334 return mtype.has_mproperty(mmodule, mproperty)
1335 end
1336
1337 redef fun to_s do return self.mproperty.to_s
1338
1339 redef fun full_name do return self.mproperty.full_name
1340
1341 redef fun c_name do return self.mproperty.c_name
1342 end
1343
1344 # The type associated to a formal parameter generic type of a class
1345 #
1346 # Each parameter type is associated to a specific class.
1347 # It means that all refinements of a same class "share" the parameter type,
1348 # but that a generic subclass has its own parameter types.
1349 #
1350 # However, in the sense of the meta-model, a parameter type of a class is
1351 # a valid type in a subclass. The "in the sense of the meta-model" is
1352 # important because, in the Nit language, the programmer cannot refers
1353 # directly to the parameter types of the super-classes.
1354 #
1355 # Example:
1356 #
1357 # class A[E]
1358 # fun e: E is abstract
1359 # end
1360 # class B[F]
1361 # super A[Array[F]]
1362 # end
1363 #
1364 # In the class definition B[F], `F` is a valid type but `E` is not.
1365 # However, `self.e` is a valid method call, and the signature of `e` is
1366 # declared `e: E`.
1367 #
1368 # Note that parameter types are shared among class refinements.
1369 # Therefore parameter only have an internal name (see `to_s` for details).
1370 class MParameterType
1371 super MType
1372
1373 # The generic class where the parameter belong
1374 var mclass: MClass
1375
1376 redef fun model do return self.mclass.intro_mmodule.model
1377
1378 # The position of the parameter (0 for the first parameter)
1379 # FIXME: is `position` a better name?
1380 var rank: Int
1381
1382 redef var name
1383
1384 redef fun to_s do return name
1385
1386 redef var full_name is lazy do return "{mclass.full_name}::{name}"
1387
1388 redef var c_name is lazy do return mclass.c_name + "__" + "#{name}".to_cmangle
1389
1390 redef fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
1391 do
1392 assert not resolved_receiver.need_anchor
1393 resolved_receiver = resolved_receiver.as_notnullable
1394 assert resolved_receiver isa MClassType # It is the only remaining type
1395 var goalclass = self.mclass
1396 if resolved_receiver.mclass == goalclass then
1397 return resolved_receiver.arguments[self.rank]
1398 end
1399 var supertypes = resolved_receiver.collect_mtypes(mmodule)
1400 for t in supertypes do
1401 if t.mclass == goalclass then
1402 # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
1403 # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
1404 var res = t.arguments[self.rank]
1405 return res
1406 end
1407 end
1408 abort
1409 end
1410
1411 # A PT is fixed when:
1412 # * Its bound is a enum class (see `enum_kind`).
1413 # The PT is just useless, but it is still a case.
1414 # * More usually, the `resolved_receiver` is a subclass of `self.mclass`,
1415 # so it is necessarily fixed in a `super` clause, either with a normal type
1416 # or with another PT.
1417 # See `resolve_for` for examples about related issues.
1418 redef fun lookup_fixed(mmodule: MModule, resolved_receiver: MType): MType
1419 do
1420 assert not resolved_receiver.need_anchor
1421 resolved_receiver = resolved_receiver.as_notnullable
1422 assert resolved_receiver isa MClassType # It is the only remaining type
1423 var res = self.resolve_for(resolved_receiver.mclass.mclass_type, resolved_receiver, mmodule, false)
1424 return res
1425 end
1426
1427 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1428 do
1429 assert can_resolve_for(mtype, anchor, mmodule)
1430 #print "{class_name}: {self}/{mtype}/{anchor}?"
1431
1432 if mtype isa MGenericType and mtype.mclass == self.mclass then
1433 var res = mtype.arguments[self.rank]
1434 if anchor != null and res.need_anchor then
1435 # Maybe the result can be resolved more if are bound to a final class
1436 var r2 = res.anchor_to(mmodule, anchor)
1437 if r2 isa MClassType and r2.mclass.kind == enum_kind then return r2
1438 end
1439 return res
1440 end
1441
1442 # self is a parameter type of mtype (or of a super-class of mtype)
1443 # The point of the function it to get the bound of the virtual type that make sense for mtype
1444 # But because mtype is maybe a virtual/formal type, we need to get a real receiver first
1445 # FIXME: What happens here is far from clear. Thus this part must be validated and clarified
1446 var resolved_receiver
1447 if mtype.need_anchor then
1448 assert anchor != null
1449 resolved_receiver = mtype.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, true)
1450 else
1451 resolved_receiver = mtype
1452 end
1453 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
1454 if resolved_receiver isa MParameterType then
1455 assert resolved_receiver.mclass == anchor.mclass
1456 resolved_receiver = anchor.arguments[resolved_receiver.rank]
1457 if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
1458 end
1459 assert resolved_receiver isa MClassType # It is the only remaining type
1460
1461 # Eh! The parameter is in the current class.
1462 # So we return the corresponding argument, no mater what!
1463 if resolved_receiver.mclass == self.mclass then
1464 var res = resolved_receiver.arguments[self.rank]
1465 #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
1466 return res
1467 end
1468
1469 if resolved_receiver.need_anchor then
1470 assert anchor != null
1471 resolved_receiver = resolved_receiver.resolve_for(anchor, null, mmodule, false)
1472 end
1473 # Now, we can get the bound
1474 var verbatim_bound = lookup_bound(mmodule, resolved_receiver)
1475 # The bound is exactly as declared in the "type" property, so we must resolve it again
1476 var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1477
1478 #print "{class_name}: {self}/{mtype}/{anchor} -> indirect {res}"
1479
1480 return res
1481 end
1482
1483 redef fun can_resolve_for(mtype, anchor, mmodule)
1484 do
1485 if mtype.need_anchor then
1486 assert anchor != null
1487 mtype = mtype.anchor_to(mmodule, anchor)
1488 end
1489 return mtype.collect_mclassdefs(mmodule).has(mclass.intro)
1490 end
1491 end
1492
1493 # A type prefixed with "nullable"
1494 class MNullableType
1495 super MType
1496
1497 # The base type of the nullable type
1498 var mtype: MType
1499
1500 redef fun model do return self.mtype.model
1501
1502 init
1503 do
1504 self.to_s = "nullable {mtype}"
1505 end
1506
1507 redef var to_s: String is noinit
1508
1509 redef var full_name is lazy do return "nullable {mtype.full_name}"
1510
1511 redef var c_name is lazy do return "nullable__{mtype.c_name}"
1512
1513 redef fun need_anchor do return mtype.need_anchor
1514 redef fun as_nullable do return self
1515 redef fun as_notnullable do return mtype
1516 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1517 do
1518 var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1519 return res.as_nullable
1520 end
1521
1522 redef fun can_resolve_for(mtype, anchor, mmodule)
1523 do
1524 return self.mtype.can_resolve_for(mtype, anchor, mmodule)
1525 end
1526
1527 # Efficiently returns `mtype.lookup_fixed(mmodule, resolved_receiver).as_nullable`
1528 redef fun lookup_fixed(mmodule, resolved_receiver)
1529 do
1530 var t = mtype.lookup_fixed(mmodule, resolved_receiver)
1531 if t == mtype then return self
1532 return t.as_nullable
1533 end
1534
1535 redef fun depth do return self.mtype.depth
1536
1537 redef fun length do return self.mtype.length
1538
1539 redef fun collect_mclassdefs(mmodule)
1540 do
1541 assert not self.need_anchor
1542 return self.mtype.collect_mclassdefs(mmodule)
1543 end
1544
1545 redef fun collect_mclasses(mmodule)
1546 do
1547 assert not self.need_anchor
1548 return self.mtype.collect_mclasses(mmodule)
1549 end
1550
1551 redef fun collect_mtypes(mmodule)
1552 do
1553 assert not self.need_anchor
1554 return self.mtype.collect_mtypes(mmodule)
1555 end
1556 end
1557
1558 # The type of the only value null
1559 #
1560 # The is only one null type per model, see `MModel::null_type`.
1561 class MNullType
1562 super MType
1563 redef var model: Model
1564 redef fun to_s do return "null"
1565 redef fun full_name do return "null"
1566 redef fun c_name do return "null"
1567 redef fun as_nullable do return self
1568 redef fun need_anchor do return false
1569 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
1570 redef fun can_resolve_for(mtype, anchor, mmodule) do return true
1571
1572 redef fun collect_mclassdefs(mmodule) do return new HashSet[MClassDef]
1573
1574 redef fun collect_mclasses(mmodule) do return new HashSet[MClass]
1575
1576 redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
1577 end
1578
1579 # A signature of a method
1580 class MSignature
1581 super MType
1582
1583 # The each parameter (in order)
1584 var mparameters: Array[MParameter]
1585
1586 # The return type (null for a procedure)
1587 var return_mtype: nullable MType
1588
1589 redef fun depth
1590 do
1591 var dmax = 0
1592 var t = self.return_mtype
1593 if t != null then dmax = t.depth
1594 for p in mparameters do
1595 var d = p.mtype.depth
1596 if d > dmax then dmax = d
1597 end
1598 return dmax + 1
1599 end
1600
1601 redef fun length
1602 do
1603 var res = 1
1604 var t = self.return_mtype
1605 if t != null then res += t.length
1606 for p in mparameters do
1607 res += p.mtype.length
1608 end
1609 return res
1610 end
1611
1612 # REQUIRE: 1 <= mparameters.count p -> p.is_vararg
1613 init
1614 do
1615 var vararg_rank = -1
1616 for i in [0..mparameters.length[ do
1617 var parameter = mparameters[i]
1618 if parameter.is_vararg then
1619 assert vararg_rank == -1
1620 vararg_rank = i
1621 end
1622 end
1623 self.vararg_rank = vararg_rank
1624 end
1625
1626 # The rank of the ellipsis (`...`) for vararg (starting from 0).
1627 # value is -1 if there is no vararg.
1628 # Example: for "(a: Int, b: Bool..., c: Char)" #-> vararg_rank=1
1629 var vararg_rank: Int is noinit
1630
1631 # The number or parameters
1632 fun arity: Int do return mparameters.length
1633
1634 redef fun to_s
1635 do
1636 var b = new FlatBuffer
1637 if not mparameters.is_empty then
1638 b.append("(")
1639 for i in [0..mparameters.length[ do
1640 var mparameter = mparameters[i]
1641 if i > 0 then b.append(", ")
1642 b.append(mparameter.name)
1643 b.append(": ")
1644 b.append(mparameter.mtype.to_s)
1645 if mparameter.is_vararg then
1646 b.append("...")
1647 end
1648 end
1649 b.append(")")
1650 end
1651 var ret = self.return_mtype
1652 if ret != null then
1653 b.append(": ")
1654 b.append(ret.to_s)
1655 end
1656 return b.to_s
1657 end
1658
1659 redef fun resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule, cleanup_virtual: Bool): MSignature
1660 do
1661 var params = new Array[MParameter]
1662 for p in self.mparameters do
1663 params.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
1664 end
1665 var ret = self.return_mtype
1666 if ret != null then
1667 ret = ret.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1668 end
1669 var res = new MSignature(params, ret)
1670 return res
1671 end
1672 end
1673
1674 # A parameter in a signature
1675 class MParameter
1676 super MEntity
1677
1678 # The name of the parameter
1679 redef var name: String
1680
1681 # The static type of the parameter
1682 var mtype: MType
1683
1684 # Is the parameter a vararg?
1685 var is_vararg: Bool
1686
1687 redef fun to_s
1688 do
1689 if is_vararg then
1690 return "{name}: {mtype}..."
1691 else
1692 return "{name}: {mtype}"
1693 end
1694 end
1695
1696 fun resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule, cleanup_virtual: Bool): MParameter
1697 do
1698 if not self.mtype.need_anchor then return self
1699 var newtype = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
1700 var res = new MParameter(self.name, newtype, self.is_vararg)
1701 return res
1702 end
1703
1704 redef fun model do return mtype.model
1705 end
1706
1707 # A service (global property) that generalize method, attribute, etc.
1708 #
1709 # `MProperty` are global to the model; it means that a `MProperty` is not bound
1710 # to a specific `MModule` nor a specific `MClass`.
1711 #
1712 # A MProperty gather definitions (see `mpropdefs`) ; one for the introduction
1713 # and the other in subclasses and in refinements.
1714 #
1715 # A `MProperty` is used to denotes services in polymorphic way (ie. independent
1716 # of any dynamic type).
1717 # For instance, a call site "x.foo" is associated to a `MProperty`.
1718 abstract class MProperty
1719 super MEntity
1720
1721 # The associated MPropDef subclass.
1722 # The two specialization hierarchy are symmetric.
1723 type MPROPDEF: MPropDef
1724
1725 # The classdef that introduce the property
1726 # While a property is not bound to a specific module, or class,
1727 # the introducing mclassdef is used for naming and visibility
1728 var intro_mclassdef: MClassDef
1729
1730 # The (short) name of the property
1731 redef var name: String
1732
1733 # The canonical name of the property.
1734 #
1735 # It is the short-`name` prefixed by the short-name of the class and the full-name of the module.
1736 # Example: "my_project::my_module::MyClass::my_method"
1737 redef var full_name is lazy do
1738 return "{intro_mclassdef.mmodule.full_name}::{intro_mclassdef.mclass.name}::{name}"
1739 end
1740
1741 redef var c_name is lazy do
1742 return "{intro_mclassdef.mmodule.c_name}__{intro_mclassdef.mclass.c_name}__{name.to_cmangle}"
1743 end
1744
1745 # The visibility of the property
1746 var visibility: MVisibility
1747
1748 init
1749 do
1750 intro_mclassdef.intro_mproperties.add(self)
1751 var model = intro_mclassdef.mmodule.model
1752 model.mproperties_by_name.add_one(name, self)
1753 model.mproperties.add(self)
1754 end
1755
1756 # All definitions of the property.
1757 # The first is the introduction,
1758 # The other are redefinitions (in refinements and in subclasses)
1759 var mpropdefs = new Array[MPROPDEF]
1760
1761 # The definition that introduces the property.
1762 #
1763 # Warning: such a definition may not exist in the early life of the object.
1764 # In this case, the method will abort.
1765 var intro: MPROPDEF is noinit
1766
1767 redef fun model do return intro.model
1768
1769 # Alias for `name`
1770 redef fun to_s do return name
1771
1772 # Return the most specific property definitions defined or inherited by a type.
1773 # The selection knows that refinement is stronger than specialization;
1774 # however, in case of conflict more than one property are returned.
1775 # If mtype does not know mproperty then an empty array is returned.
1776 #
1777 # If you want the really most specific property, then look at `lookup_first_definition`
1778 fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
1779 do
1780 assert not mtype.need_anchor
1781 mtype = mtype.as_notnullable
1782
1783 var cache = self.lookup_definitions_cache[mmodule, mtype]
1784 if cache != null then return cache
1785
1786 #print "select prop {mproperty} for {mtype} in {self}"
1787 # First, select all candidates
1788 var candidates = new Array[MPROPDEF]
1789 for mpropdef in self.mpropdefs do
1790 # If the definition is not imported by the module, then skip
1791 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1792 # If the definition is not inherited by the type, then skip
1793 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1794 # Else, we keep it
1795 candidates.add(mpropdef)
1796 end
1797 # Fast track for only one candidate
1798 if candidates.length <= 1 then
1799 self.lookup_definitions_cache[mmodule, mtype] = candidates
1800 return candidates
1801 end
1802
1803 # Second, filter the most specific ones
1804 return select_most_specific(mmodule, candidates)
1805 end
1806
1807 private var lookup_definitions_cache = new HashMap2[MModule, MType, Array[MPROPDEF]]
1808
1809 # Return the most specific property definitions inherited by a type.
1810 # The selection knows that refinement is stronger than specialization;
1811 # however, in case of conflict more than one property are returned.
1812 # If mtype does not know mproperty then an empty array is returned.
1813 #
1814 # If you want the really most specific property, then look at `lookup_next_definition`
1815 #
1816 # FIXME: Move to `MPropDef`?
1817 fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
1818 do
1819 assert not mtype.need_anchor
1820 mtype = mtype.as_notnullable
1821
1822 # First, select all candidates
1823 var candidates = new Array[MPROPDEF]
1824 for mpropdef in self.mpropdefs do
1825 # If the definition is not imported by the module, then skip
1826 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1827 # If the definition is not inherited by the type, then skip
1828 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1829 # If the definition is defined by the type, then skip (we want the super, so e skip the current)
1830 if mtype == mpropdef.mclassdef.bound_mtype and mmodule == mpropdef.mclassdef.mmodule then continue
1831 # Else, we keep it
1832 candidates.add(mpropdef)
1833 end
1834 # Fast track for only one candidate
1835 if candidates.length <= 1 then return candidates
1836
1837 # Second, filter the most specific ones
1838 return select_most_specific(mmodule, candidates)
1839 end
1840
1841 # Return an array containing olny the most specific property definitions
1842 # This is an helper function for `lookup_definitions` and `lookup_super_definitions`
1843 private fun select_most_specific(mmodule: MModule, candidates: Array[MPROPDEF]): Array[MPROPDEF]
1844 do
1845 var res = new Array[MPROPDEF]
1846 for pd1 in candidates do
1847 var cd1 = pd1.mclassdef
1848 var c1 = cd1.mclass
1849 var keep = true
1850 for pd2 in candidates do
1851 if pd2 == pd1 then continue # do not compare with self!
1852 var cd2 = pd2.mclassdef
1853 var c2 = cd2.mclass
1854 if c2.mclass_type == c1.mclass_type then
1855 if cd2.mmodule.in_importation < cd1.mmodule then
1856 # cd2 refines cd1; therefore we skip pd1
1857 keep = false
1858 break
1859 end
1860 else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) and cd2.bound_mtype != cd1.bound_mtype then
1861 # cd2 < cd1; therefore we skip pd1
1862 keep = false
1863 break
1864 end
1865 end
1866 if keep then
1867 res.add(pd1)
1868 end
1869 end
1870 if res.is_empty then
1871 print "All lost! {candidates.join(", ")}"
1872 # FIXME: should be abort!
1873 end
1874 return res
1875 end
1876
1877 # Return the most specific definition in the linearization of `mtype`.
1878 #
1879 # If you want to know the next properties in the linearization,
1880 # look at `MPropDef::lookup_next_definition`.
1881 #
1882 # FIXME: the linearization is still unspecified
1883 #
1884 # REQUIRE: `not mtype.need_anchor`
1885 # REQUIRE: `mtype.has_mproperty(mmodule, self)`
1886 fun lookup_first_definition(mmodule: MModule, mtype: MType): MPROPDEF
1887 do
1888 assert mtype.has_mproperty(mmodule, self)
1889 return lookup_all_definitions(mmodule, mtype).first
1890 end
1891
1892 # Return all definitions in a linearization order
1893 # Most specific first, most general last
1894 fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
1895 do
1896 assert not mtype.need_anchor
1897 mtype = mtype.as_notnullable
1898
1899 var cache = self.lookup_all_definitions_cache[mmodule, mtype]
1900 if cache != null then return cache
1901
1902 #print "select prop {mproperty} for {mtype} in {self}"
1903 # First, select all candidates
1904 var candidates = new Array[MPROPDEF]
1905 for mpropdef in self.mpropdefs do
1906 # If the definition is not imported by the module, then skip
1907 if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
1908 # If the definition is not inherited by the type, then skip
1909 if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
1910 # Else, we keep it
1911 candidates.add(mpropdef)
1912 end
1913 # Fast track for only one candidate
1914 if candidates.length <= 1 then
1915 self.lookup_all_definitions_cache[mmodule, mtype] = candidates
1916 return candidates
1917 end
1918
1919 mmodule.linearize_mpropdefs(candidates)
1920 candidates = candidates.reversed
1921 self.lookup_all_definitions_cache[mmodule, mtype] = candidates
1922 return candidates
1923 end
1924
1925 private var lookup_all_definitions_cache = new HashMap2[MModule, MType, Array[MPROPDEF]]
1926 end
1927
1928 # A global method
1929 class MMethod
1930 super MProperty
1931
1932 redef type MPROPDEF: MMethodDef
1933
1934 # Is the property defined at the top_level of the module?
1935 # Currently such a property are stored in `Object`
1936 var is_toplevel: Bool = false is writable
1937
1938 # Is the property a constructor?
1939 # Warning, this property can be inherited by subclasses with or without being a constructor
1940 # therefore, you should use `is_init_for` the verify if the property is a legal constructor for a given class
1941 var is_init: Bool = false is writable
1942
1943 # The constructor is a (the) root init with empty signature but a set of initializers
1944 var is_root_init: Bool = false is writable
1945
1946 # Is the property a 'new' constructor?
1947 var is_new: Bool = false is writable
1948
1949 # Is the property a legal constructor for a given class?
1950 # As usual, visibility is not considered.
1951 # FIXME not implemented
1952 fun is_init_for(mclass: MClass): Bool
1953 do
1954 return self.is_init
1955 end
1956 end
1957
1958 # A global attribute
1959 class MAttribute
1960 super MProperty
1961
1962 redef type MPROPDEF: MAttributeDef
1963
1964 end
1965
1966 # A global virtual type
1967 class MVirtualTypeProp
1968 super MProperty
1969
1970 redef type MPROPDEF: MVirtualTypeDef
1971
1972 # The formal type associated to the virtual type property
1973 var mvirtualtype = new MVirtualType(self)
1974 end
1975
1976 # A definition of a property (local property)
1977 #
1978 # Unlike `MProperty`, a `MPropDef` is a local definition that belong to a
1979 # specific class definition (which belong to a specific module)
1980 abstract class MPropDef
1981 super MEntity
1982
1983 # The associated `MProperty` subclass.
1984 # the two specialization hierarchy are symmetric
1985 type MPROPERTY: MProperty
1986
1987 # Self class
1988 type MPROPDEF: MPropDef
1989
1990 # The class definition where the property definition is
1991 var mclassdef: MClassDef
1992
1993 # The associated global property
1994 var mproperty: MPROPERTY
1995
1996 # The origin of the definition
1997 var location: Location
1998
1999 init
2000 do
2001 mclassdef.mpropdefs.add(self)
2002 mproperty.mpropdefs.add(self)
2003 if mproperty.intro_mclassdef == mclassdef then
2004 assert not isset mproperty._intro
2005 mproperty.intro = self
2006 end
2007 self.to_s = "{mclassdef}#{mproperty}"
2008 end
2009
2010 # Actually the name of the `mproperty`
2011 redef fun name do return mproperty.name
2012
2013 # The full-name of mpropdefs combine the information about the `classdef` and the `mproperty`.
2014 #
2015 # Therefore the combination of identifiers is awful,
2016 # the worst case being
2017 #
2018 # ~~~nitish
2019 # "{mclassdef.mmodule.full_name}#{mclassdef.mclass.intro_mmodule.full_name}::{mclassdef.name}#{mproperty.intro_mclassdef.mmodule.full_name}::{mproperty.intro_mclassdef.name}::{name}"
2020 # ~~~
2021 #
2022 # Fortunately, the full-name is simplified when entities are repeated.
2023 # The simplest form is "my_module#MyClass#my_property".
2024 redef var full_name is lazy do
2025 var res = new FlatBuffer
2026 res.append mclassdef.mmodule.full_name
2027 res.append "#"
2028 if not mclassdef.is_intro then
2029 res.append mclassdef.mclass.intro_mmodule.full_name
2030 res.append "::"
2031 end
2032 res.append mclassdef.name
2033 res.append "#"
2034 if mproperty.intro_mclassdef.mmodule != mclassdef.mmodule then
2035 res.append mproperty.intro_mclassdef.mmodule.full_name
2036 res.append "::"
2037 end
2038 if mclassdef.mclass != mproperty.intro_mclassdef.mclass then
2039 res.append mproperty.intro_mclassdef.mclass.name
2040 res.append "::"
2041 end
2042 res.append name
2043 return res.to_s
2044 end
2045
2046 redef var c_name is lazy do
2047 var res = new FlatBuffer
2048 res.append mclassdef.c_name
2049 res.append "__"
2050 if is_intro then
2051 res.append name.to_cmangle
2052 else
2053 res.append mproperty.c_name.to_cmangle
2054 end
2055 return res.to_s
2056 end
2057
2058 redef fun model do return mclassdef.model
2059
2060 # Internal name combining the module, the class and the property
2061 # Example: "mymodule#MyClass#mymethod"
2062 redef var to_s: String is noinit
2063
2064 # Is self the definition that introduce the property?
2065 fun is_intro: Bool do return mproperty.intro == self
2066
2067 # Return the next definition in linearization of `mtype`.
2068 #
2069 # This method is used to determine what method is called by a super.
2070 #
2071 # REQUIRE: `not mtype.need_anchor`
2072 fun lookup_next_definition(mmodule: MModule, mtype: MType): MPROPDEF
2073 do
2074 assert not mtype.need_anchor
2075
2076 var mpropdefs = self.mproperty.lookup_all_definitions(mmodule, mtype)
2077 var i = mpropdefs.iterator
2078 while i.is_ok and i.item != self do i.next
2079 assert has_property: i.is_ok
2080 i.next
2081 assert has_next_property: i.is_ok
2082 return i.item
2083 end
2084 end
2085
2086 # A local definition of a method
2087 class MMethodDef
2088 super MPropDef
2089
2090 redef type MPROPERTY: MMethod
2091 redef type MPROPDEF: MMethodDef
2092
2093 # The signature attached to the property definition
2094 var msignature: nullable MSignature = null is writable
2095
2096 # The signature attached to the `new` call on a root-init
2097 # This is a concatenation of the signatures of the initializers
2098 #
2099 # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
2100 var new_msignature: nullable MSignature = null is writable
2101
2102 # List of initialisers to call in root-inits
2103 #
2104 # They could be setters or attributes
2105 #
2106 # REQUIRE `mproperty.is_root_init == (new_msignature != null)`
2107 var initializers = new Array[MProperty]
2108
2109 # Is the method definition abstract?
2110 var is_abstract: Bool = false is writable
2111
2112 # Is the method definition intern?
2113 var is_intern = false is writable
2114
2115 # Is the method definition extern?
2116 var is_extern = false is writable
2117
2118 # An optional constant value returned in functions.
2119 #
2120 # Only some specific primitife value are accepted by engines.
2121 # Is used when there is no better implementation available.
2122 #
2123 # Currently used only for the implementation of the `--define`
2124 # command-line option.
2125 # SEE: module `mixin`.
2126 var constant_value: nullable Object = null is writable
2127 end
2128
2129 # A local definition of an attribute
2130 class MAttributeDef
2131 super MPropDef
2132
2133 redef type MPROPERTY: MAttribute
2134 redef type MPROPDEF: MAttributeDef
2135
2136 # The static type of the attribute
2137 var static_mtype: nullable MType = null is writable
2138 end
2139
2140 # A local definition of a virtual type
2141 class MVirtualTypeDef
2142 super MPropDef
2143
2144 redef type MPROPERTY: MVirtualTypeProp
2145 redef type MPROPDEF: MVirtualTypeDef
2146
2147 # The bound of the virtual type
2148 var bound: nullable MType = null is writable
2149
2150 # Is the bound fixed?
2151 var is_fixed = false is writable
2152 end
2153
2154 # A kind of class.
2155 #
2156 # * `abstract_kind`
2157 # * `concrete_kind`
2158 # * `interface_kind`
2159 # * `enum_kind`
2160 # * `extern_kind`
2161 #
2162 # Note this class is basically an enum.
2163 # FIXME: use a real enum once user-defined enums are available
2164 class MClassKind
2165 redef var to_s: String
2166
2167 # Is a constructor required?
2168 var need_init: Bool
2169
2170 # TODO: private init because enumeration.
2171
2172 # Can a class of kind `self` specializes a class of kine `other`?
2173 fun can_specialize(other: MClassKind): Bool
2174 do
2175 if other == interface_kind then return true # everybody can specialize interfaces
2176 if self == interface_kind or self == enum_kind then
2177 # no other case for interfaces
2178 return false
2179 else if self == extern_kind then
2180 # only compatible with themselves
2181 return self == other
2182 else if other == enum_kind or other == extern_kind then
2183 # abstract_kind and concrete_kind are incompatible
2184 return false
2185 end
2186 # remain only abstract_kind and concrete_kind
2187 return true
2188 end
2189 end
2190
2191 # The class kind `abstract`
2192 fun abstract_kind: MClassKind do return once new MClassKind("abstract class", true)
2193 # The class kind `concrete`
2194 fun concrete_kind: MClassKind do return once new MClassKind("class", true)
2195 # The class kind `interface`
2196 fun interface_kind: MClassKind do return once new MClassKind("interface", false)
2197 # The class kind `enum`
2198 fun enum_kind: MClassKind do return once new MClassKind("enum", false)
2199 # The class kind `extern`
2200 fun extern_kind: MClassKind do return once new MClassKind("extern class", false)