Merge branch 'pu/parser_tables_in_c' into wip
[nit.git] / src / metamodel / inheritance.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Compute importation of classes and inheritance of properties
19 package inheritance
20
21 intrude import static_type
22
23 redef class MMModule
24 # The root of the class hierarchy
25 fun type_any: MMType
26 do
27 var c_name = class_by_name(once ("Object".to_symbol))
28 return c_name.get_type
29 end
30
31 # Import global classes from supermodules
32 fun import_global_classes
33 do
34 var globals = new HashMap[MMGlobalClass,HashSet[MMLocalClass]]
35 assert mhe != null
36 for mod in mhe.direct_greaters do
37 for glob in mod.global_classes do
38 if global_classes.has(glob) then continue
39 _global_classes.add(glob)
40 _global_class_by_name[glob.name] = glob
41 end
42 end
43
44 end
45
46 # Create implicit local classes for global classes that are not refined
47 fun import_local_classes
48 do
49 for g in _global_classes do
50 if _local_class_by_global.has_key(g) then continue
51 var impl = new MMImplicitLocalClass(self, g)
52 end
53 end
54 end
55
56 redef class MMLocalClass
57 # List of all parents
58 var _direct_parents: Array[MMAncestor] = new Array[MMAncestor]
59
60 # Is the class computing super.
61 # Used to detect specialization loops.
62 var _computing_super: Bool = false
63
64 # Compute super classes of a class
65 fun compute_super_classes
66 do
67 if computed_super_classes then
68 # do no recompute if allready done
69 return
70 else if _computing_super then
71 stderr.write("Fatal error: Inheritance loop for class {self}\n")
72 exit(1)
73 end
74 _computing_super = true
75
76 var supers = new Array[MMLocalClass]
77 add_explicit_classes(supers)
78 add_super_classes(supers)
79 add_default_any_class(supers)
80 compute_super_parents(supers)
81 var set = new HashSet[MMLocalClass]
82 set.add_all(supers)
83 var u = set.to_a
84 module.set_supers_class(self,u)
85 assert _crhe != null
86 assert _cshe != null
87 _computing_super = false
88 end
89
90 # Compute ancestors for a class
91 fun compute_ancestors
92 do
93 assert computed_super_classes
94 if computed_ancestors then return
95
96 var ancestors = group_ancestors(build_ancestors)
97 _ancestors = new HashMap[MMLocalClass, MMAncestor]
98
99 for set in ancestors do
100 if set.length == 1 then
101 add_ancestor(set.first)
102 else
103 var ma = merge_ancestors(set)
104 add_ancestor(merge_ancestors(set))
105 end
106 end
107 end
108
109 var _are_global_properties_inherited: Bool = false
110
111 # Inherit global properties for a class
112 private fun inherit_global_properties
113 do
114 if _are_global_properties_inherited then return
115 _are_global_properties_inherited = true
116
117 var names = _properties_by_name
118 var set = _global_properties
119 for c in che.direct_greaters do
120 for glob in c.global_properties do
121 if set.has(glob) then continue
122
123 set.add(glob) # Add the global property
124
125 # Do not inherit constructors trough specialization
126 #print "{c.module}::{c} -> {module}::{self} for {glob.local_property.local_class.module}::{glob.local_property.local_class}::{glob.local_property} : {glob.is_init}"
127 if glob.is_init and glob.intro.local_class.global != global then
128 #print "pass"
129 continue
130 end
131
132 make_visible_an_inherited_global_property(glob)
133 end
134 end
135 end
136
137 redef fun global_properties
138 do
139 if _are_global_properties_inherited then return _global_properties
140 assert computed_super_classes
141 inherit_global_properties
142 return _global_properties
143 end
144
145 redef fun has_global_property(g)
146 do
147 # has_global_property can be called during the construction of the class
148 # hierarchy to check that a type "X" is not a formal type.
149 if not computed_super_classes then return false
150
151 var set = _global_properties
152 if set.has(g) then return true
153 for c in che.direct_greaters do
154 if c.has_global_property(g) then
155 set.add(g)
156 return true
157 end
158 end
159 return false
160 end
161
162 redef fun has_global_property_by_name(n)
163 do
164 # has_global_property can be called during the construction of the class
165 # hierarchy to check that a type "X" is not a formal type.
166 if not computed_super_classes then return false
167
168 # Ensure that super-classes are constructed
169 compute_super_classes
170
171 if _properties_by_name.has_key(n) then
172 return _properties_by_name[n].length == 1
173 end
174 var set = _global_properties
175 var nset
176 if _properties_by_name.has_key(n) then
177 nset = _properties_by_name[n]
178 else
179 nset = new Array[MMGlobalProperty]
180 _properties_by_name[n] = nset
181 end
182 for c in che.direct_greaters do
183 if c.has_global_property_by_name(n) then
184 var g = c.get_property_by_name(n)
185 if not set.has(g) then set.add(g)
186 if g.is_init and g.intro.local_class.global != global then continue
187 if nset.has(g) then continue
188 nset.add(g)
189 end
190 end
191 return nset.length == 1
192 end
193
194 # Make the name of a global property meaningful in the class
195 fun make_visible_an_inherited_global_property(glob: MMGlobalProperty)
196 do
197 var names = _properties_by_name
198 var gname = glob.intro.name
199 var conf_set: Array[MMGlobalProperty]
200 if names.has_key(gname) then
201 conf_set = names[gname]
202 else
203 conf_set = new Array[MMGlobalProperty]
204 names[gname] = conf_set
205 end
206 conf_set.add(glob)
207 end
208
209 # Add super stype of this current local class
210 fun add_direct_parent(p: MMAncestor)
211 do
212 _direct_parents.add(p)
213 end
214
215 # Are super-class already computed?
216 fun computed_super_classes: Bool
217 do
218 return _crhe != null and _cshe != null
219 end
220
221 # Are ancestors already computed
222 fun computed_ancestors: Bool
223 do
224 return _ancestors != null
225 end
226
227 # Get the ancestor for a given class
228 # TODO: is this useful?
229 private fun ancestor_for(c: MMLocalClass): MMAncestor
230 do
231 assert ancestors != null
232
233 if _ancestors.has_key(c) then
234 return _ancestors[c]
235 end
236 var a = c.for_module(module)
237 assert cshe <= a
238 var ra: MMAncestor
239 if _ancestors.has_key(a) then
240 ra = _ancestors[a]
241 else if c.global == _global then
242 ra = new MMRefineAncestor(self,c)
243 else
244 ra = new MMSpecAncestor(get_type,c.get_type)
245 end
246 _ancestors[c] = ra
247 return ra
248 end
249
250 redef fun [](glob)
251 do
252 if _local_property_by_global.has_key(glob) then
253 return _local_property_by_global[glob]
254 else if has_global_property(glob) then
255 return inherit_local_property(glob)
256 else
257 abort
258 end
259 end
260
261 # Add default super class in direct parent and in super classes if this is not the Object class
262 private fun add_default_any_class(supers: Array[MMLocalClass])
263 do
264 if supers.is_empty and name != once ("Object".to_symbol) then
265 var t_any = module.type_any
266 supers.add(t_any.local_class)
267 var default = new MMDefaultAncestor(self, t_any)
268 add_direct_parent(default)
269 end
270 end
271
272 # Adding inherited class from previous refinement of self
273 private fun add_super_classes(supers: Array[MMLocalClass])
274 do
275 assert _crhe != null
276 for ref in _crhe.direct_greaters do
277 for sup in ref.cshe.direct_greaters do
278 var cla = sup.for_module(_module)
279 supers.add(cla)
280 end
281 end
282 end
283
284 # Add self parents of this local class
285 private fun add_explicit_classes(supers: Array[MMLocalClass])
286 do
287 for p in _direct_parents do
288 supers.add(p.local_class)
289 end
290 end
291
292 # Ensure all super parents are computed
293 private fun compute_super_parents(supers: Array[MMLocalClass])
294 do
295 for p in supers do
296 p.compute_super_classes
297 end
298 end
299
300 # compute all ancestors for a class (multiple)
301 private fun build_ancestors: Array[MMAncestor]
302 do
303 var all_ancestors = new Array[MMAncestor]
304 # Refined classes are ancestors
305 assert _crhe != null
306 for p in _crhe.direct_greaters do
307 assert p != self
308 var anc = new MMRefineAncestor(self, p)
309 anc.add_in(all_ancestors)
310 end
311 for anc in _direct_parents do
312 assert anc.local_class != self
313 anc.add_in(all_ancestors)
314 end
315 return all_ancestors
316 end
317
318 # Build an ancestor map indexed by LocalClass
319 private fun group_ancestors(all: Array[MMAncestor]): Map[MMLocalClass, Set[MMAncestor]]
320 do
321 #print "process {self}"
322 var map = new HashMap[MMLocalClass, Set[MMAncestor]]
323 for a in all do
324 var c = a.local_class
325 #print "ancestor is"
326 #print " {c}"
327 var set: Set[MMAncestor]
328 c.compute_ancestors
329 if map.has_key(c) then
330 set = map[c]
331 else
332 set = new HashSet[MMAncestor]
333 map[c] = set
334 end
335 set.add(a)
336 end
337 return map
338 end
339
340 # Remove duplicate ancestors and merge if compatible, in the other case do an error
341 private fun merge_ancestors(set: Set[MMAncestor]): MMAncestor
342 do
343 var marks = new HashSet[MMAncestor]
344 var res = new Array[MMAncestor]
345 for t in set do
346 var it = set.iterator
347 var search = true
348 while it.is_ok and search do
349
350 var a = t == it.item
351 a = marks.has(it.item)
352 a = it.item.stype < t.stype
353
354 if not(t == it.item or marks.has(it.item)) and
355 (it.item.stype < t.stype) then
356 marks.add(t)
357 search = false
358 end
359 it.next
360 end
361 if not marks.has(t) then
362 res.add(t)
363 end
364 end
365
366 if res.length > 1 then
367 stderr.write("Fatal error: Incompatibles ancestors for {self.name}: {res.join(", ")}\n")
368 exit(1)
369 end
370 return res.first
371 end
372
373 # Inherit a local property
374 # Is lazily called
375 # FIXME: dont crash lazily
376 private fun inherit_local_property(glob: MMGlobalProperty): MMLocalProperty
377 do
378 assert not _local_property_by_global.has_key(glob)
379
380 var impl: MMLocalProperty
381
382 var ghier = glob.property_hierarchy
383 var supers = che.direct_greaters
384 if ghier.length == 1 then
385 # Unredefined property
386 impl = glob.intro
387 else if supers.length == 1 then
388 # Single inheritance
389 impl = supers.first[glob]
390 else
391 # Hard multiple inheritance
392 # First compute the set of bottom properties
393 var impls = new ArraySet[MMLocalProperty]
394 for sc in supers do
395 if sc.has_global_property(glob) then impls.add(sc[glob])
396 end
397 # Second, extract most specific
398 var impls2 = ghier.select_smallests(impls)
399 # Conflict case (FIXME)
400 if impls2.length != 1 then
401 stderr.write("Fatal error: inherit_local_property error\n")
402 print("------- {module}::{self} {glob.intro.full_name}")
403 for i in impls2 do
404 print(" {i.full_name}")
405 end
406 print("------- {glob.property_hierarchy.first}")
407 print("------- {glob.property_hierarchy.to_dot}")
408 exit(1)
409 end
410 impl = impls2.first
411 end
412
413 # FIXME: Why these 3 lines ?
414 #var ac = ancestor_for(impl.local_class)
415 #ac.local_class.inherit_global_properties
416 #var a = ac.stype
417 #assert a.local_class != self
418
419 # Register the local property
420 _local_property_by_global[glob] = impl
421
422 return impl
423 end
424 end
425
426 redef class MMLocalProperty
427 # Attach self to a global property
428 fun inherit_global(g: MMGlobalProperty)
429 do
430 set_global(g)
431 var impls = new Array[MMLocalProperty]
432 for sc in local_class.che.direct_greaters do
433 if not sc.has_global_property(g) then continue
434 impls.add(sc[g])
435 end
436 g.add_local_property(self, impls)
437 end
438 end
439
440 redef class MMAncestor
441 # Add this ancestor and it's super one in tab
442 private fun add_in(tab: Array[MMAncestor])
443 do
444 tab.add(self)
445 stype.local_class.compute_ancestors
446 for anc in stype.local_class.ancestors.as(not null) do
447 var aaa = anc.stype.for_module(stype.module)
448 var a = aaa.adapt_to(stype).for_module(inheriter.module)
449 if a.local_class != inheriter.local_class then
450 var it = tab.iterator
451 var b = true
452 while it.is_ok and b do
453 b = not ( it.item.inheriter == inheriter and it.item.stype == a)
454 it.next
455 end
456 if b then
457 tab.add(new MMSpecAncestor(inheriter,a))
458 end
459 end
460 end
461 end
462 end
463
464 ##########################################
465
466 # A local class that is a pure importation of an other local class
467 class MMImplicitLocalClass
468 special MMLocalClass
469 init(mod: MMModule, g: MMGlobalClass)
470 do
471 var cla = g.intro
472 super(mod, cla.name, cla.arity)
473 set_global(g)
474 end
475 end
476
477 class MMRefineAncestor
478 special MMAncestor
479 redef readable var _local_class: MMLocalClass
480
481 init(b: MMLocalClass, a: MMLocalClass)
482 do
483 _local_class = a
484 inheriter = b.get_type
485 stype = _local_class.get_type
486 end
487 end
488
489
490 class MMSpecAncestor
491 special MMAncestor
492 redef fun local_class do return stype.local_class
493
494 init(inheriter: MMType, stype: MMType)
495 do
496 _inheriter = inheriter
497 _stype = stype
498 end
499 end
500
501 class MMDefaultAncestor
502 special MMAncestor
503 redef fun local_class do return stype.local_class
504
505 init(b: MMLocalClass, anc: MMType)
506 do
507 inheriter = b.get_type
508 stype = anc
509 end
510 end