metamodel: MMLocalClass::[] aborts if no global class
[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 meth 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 meth 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 meth import_local_classes
48 do
49 for g in _global_classes do
50 if self[g] != null 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 attr _direct_parents: Array[MMAncestor] = new Array[MMAncestor]
59
60 # Is the class computing super.
61 # Used to detect specialization loops.
62 attr _computing_super: Bool = false
63
64 # Compute super classes of a class
65 meth 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 meth 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 # Inherit global properties for a class
110 meth inherit_global_properties
111 do
112 if _global_properties != null then return
113
114 _global_properties = new HashSet[MMGlobalProperty]
115 _properties_by_name = new HashMap[Symbol, Array[MMGlobalProperty]]
116 _local_property_by_global = new HashMap[MMGlobalProperty, MMLocalProperty]
117
118 var names = _properties_by_name
119 var set = _global_properties
120 for c in che.direct_greaters do
121 for glob in c.global_properties do
122 if set.has(glob) then continue
123
124 set.add(glob) # Add the global property
125
126 # Do not inherit constructors trough specialization
127 #print "{c.module}::{c} -> {module}::{self} for {glob.local_property.local_class.module}::{glob.local_property.local_class}::{glob.local_property} : {glob.is_init}"
128 if glob.is_init and glob.intro.local_class.global != global then
129 #print "pass"
130 continue
131 end
132
133 make_visible_an_inherited_global_property(glob)
134 end
135 end
136 end
137
138 # Make the name of a global property meaningful in the class
139 meth make_visible_an_inherited_global_property(glob: MMGlobalProperty)
140 do
141 var names = _properties_by_name
142 var gname = glob.intro.name
143 var conf_set: Array[MMGlobalProperty]
144 if names.has_key(gname) then
145 conf_set = names[gname]
146 else
147 conf_set = new Array[MMGlobalProperty]
148 names[gname] = conf_set
149 end
150 conf_set.add(glob)
151 end
152
153 # Add super stype of this current local class
154 meth add_direct_parent(p: MMAncestor)
155 do
156 _direct_parents.add(p)
157 end
158
159 # Are super-class already computed?
160 meth computed_super_classes: Bool
161 do
162 return _crhe != null and _cshe != null
163 end
164
165 # Are ancestors already computed
166 meth computed_ancestors: Bool
167 do
168 return _ancestors != null
169 end
170
171 # Get the ancestor for a given class
172 # TODO: is this useful?
173 private meth ancestor_for(c: MMLocalClass): MMAncestor
174 do
175 assert ancestors != null
176
177 if _ancestors.has_key(c) then
178 return _ancestors[c]
179 end
180 var a = c.for_module(module)
181 assert cshe <= a
182 var ra: MMAncestor
183 if _ancestors.has_key(a) then
184 ra = _ancestors[a]
185 else if c.global == _global then
186 ra = new MMRefineAncestor(self,c)
187 else
188 ra = new MMSpecAncestor(get_type,c.get_type)
189 end
190 _ancestors[c] = ra
191 return ra
192 end
193
194 redef meth [](glob)
195 do
196 if _local_property_by_global.has_key(glob) then
197 return _local_property_by_global[glob]
198 else if has_global_property(glob) then
199 return inherit_local_property(glob)
200 else
201 abort
202 end
203 end
204
205 # Add default super class in direct parent and in super classes if this is not the Object class
206 private meth add_default_any_class(supers: Array[MMLocalClass])
207 do
208 if supers.is_empty and name != once ("Object".to_symbol) then
209 var t_any = module.type_any
210 supers.add(t_any.local_class)
211 var default = new MMDefaultAncestor(self, t_any)
212 add_direct_parent(default)
213 end
214 end
215
216 # Adding inherited class from previous refinement of self
217 private meth add_super_classes(supers: Array[MMLocalClass])
218 do
219 assert _crhe != null
220 for ref in _crhe.direct_greaters do
221 assert ref.cshe != null
222 for sup in ref.cshe.direct_greaters do
223 var cla = sup.for_module(_module)
224 assert cla != null
225 supers.add(cla)
226 end
227 end
228 end
229
230 # Add self parents of this local class
231 private meth add_explicit_classes(supers: Array[MMLocalClass])
232 do
233 for p in _direct_parents do
234 supers.add(p.local_class)
235 end
236 end
237
238 # Ensure all super parents are computed
239 private meth compute_super_parents(supers: Array[MMLocalClass])
240 do
241 for p in supers do
242 assert p != null
243 p.compute_super_classes
244 end
245 end
246
247 # compute all ancestors for a class (multiple)
248 private meth build_ancestors: Array[MMAncestor]
249 do
250 var all_ancestors = new Array[MMAncestor]
251 # Refined classes are ancestors
252 assert _crhe != null
253 for p in _crhe.direct_greaters do
254 assert p != self
255 var anc = new MMRefineAncestor(self, p)
256 anc.add_in(all_ancestors)
257 end
258 for anc in _direct_parents do
259 assert anc.local_class != self
260 anc.add_in(all_ancestors)
261 end
262 return all_ancestors
263 end
264
265 # Build an ancestor map indexed by LocalClass
266 private meth group_ancestors(all: Array[MMAncestor]): Map[MMLocalClass, Set[MMAncestor]]
267 do
268 #print "process {self}"
269 var map = new HashMap[MMLocalClass, Set[MMAncestor]]
270 for a in all do
271 var c = a.local_class
272 #print "ancestor is"
273 #print " {c}"
274 var set: Set[MMAncestor]
275 c.compute_ancestors
276 if map.has_key(c) then
277 set = map[c]
278 else
279 set = new HashSet[MMAncestor]
280 map[c] = set
281 end
282 set.add(a)
283 end
284 return map
285 end
286
287 # Remove duplicate ancestors and merge if compatible, in the other case do an error
288 private meth merge_ancestors(set: Set[MMAncestor]): MMAncestor
289 do
290 var marks = new HashSet[MMAncestor]
291 var res = new Array[MMAncestor]
292 for t in set do
293 var it = set.iterator
294 var search = true
295 while it.is_ok and search do
296
297 var a = t == it.item
298 a = marks.has(it.item)
299 a = it.item.stype < t.stype
300
301 if not(t == it.item or marks.has(it.item)) and
302 (it.item.stype < t.stype) then
303 marks.add(t)
304 search = false
305 end
306 it.next
307 end
308 if not marks.has(t) then
309 res.add(t)
310 end
311 end
312
313 if res.length > 1 then
314 stderr.write("Fatal error: Incompatibles ancestors for {self.name}: {res.join(", ")}\n")
315 exit(1)
316 end
317 return res.first
318 end
319
320 # Inherit a local property
321 # Is lazily called
322 # FIXME: dont crash lazily
323 private meth inherit_local_property(glob: MMGlobalProperty): MMLocalProperty
324 do
325 assert not _local_property_by_global.has_key(glob)
326 assert glob != null
327
328 var impl: MMLocalProperty
329
330 var ghier = glob.property_hierarchy
331 var supers = che.direct_greaters
332 if ghier.length == 1 then
333 # Unredefined property
334 impl = glob.intro
335 else if supers.length == 1 then
336 # Single inheritance
337 impl = supers.first[glob]
338 else
339 # Hard multiple inheritance
340 # First compute the set of bottom properties
341 var impls = new ArraySet[MMLocalProperty]
342 for sc in supers do
343 if sc.has_global_property(glob) then impls.add(sc[glob])
344 end
345 # Second, extract most specific
346 var impls2 = ghier.select_smallests(impls)
347 # Conflict case (FIXME)
348 if impls2.length != 1 then
349 stderr.write("Fatal error: inherit_local_property error\n")
350 print("------- {module}::{self} {glob.intro.full_name}")
351 for i in impls2 do
352 print(" {i.full_name}")
353 end
354 print("------- {glob.property_hierarchy.first}")
355 print("------- {glob.property_hierarchy.to_dot}")
356 exit(1)
357 end
358 impl = impls2.first
359 end
360
361 # FIXME: Why these 3 lines ?
362 #var ac = ancestor_for(impl.local_class)
363 #ac.local_class.inherit_global_properties
364 #var a = ac.stype
365 #assert a.local_class != self
366
367 # Register the local property
368 _local_property_by_global[glob] = impl
369
370 return impl
371 end
372 end
373
374 redef class MMLocalProperty
375 # Attach self to a global property
376 meth inherit_global(g: MMGlobalProperty)
377 do
378 set_global(g)
379 var impls = new Array[MMLocalProperty]
380 for sc in local_class.che.direct_greaters do
381 if not sc.has_global_property(g) then continue
382 impls.add(sc[g])
383 end
384 g.add_local_property(self, impls)
385 end
386 end
387
388 redef class MMAncestor
389 # Add this ancestor and it's super one in tab
390 private meth add_in(tab: Array[MMAncestor])
391 do
392 assert ancestor: stype != null
393 assert local_class: stype.local_class != null
394 tab.add(self)
395 stype.local_class.compute_ancestors
396 for anc in stype.local_class.ancestors do
397 var aaa = anc.stype.for_module(stype.module)
398 var a = aaa.adapt_to(stype).for_module(inheriter.module)
399 if a.local_class != inheriter.local_class then
400 var it = tab.iterator
401 var b = true
402 while it.is_ok and b do
403 b = not ( it.item.inheriter == inheriter and it.item.stype == a)
404 it.next
405 end
406 if b then
407 tab.add(new MMSpecAncestor(inheriter,a))
408 end
409 end
410 end
411 end
412 end
413
414 ##########################################
415
416 # A local class that is a pure importation of an other local class
417 class MMImplicitLocalClass
418 special MMLocalClass
419 init(mod: MMModule, g: MMGlobalClass)
420 do
421 var cla = g.intro
422 super(cla.name, cla.arity)
423 mod.add_local_class(self)
424 set_global(g)
425 end
426 end
427
428 class MMRefineAncestor
429 special MMAncestor
430 redef readable attr _local_class: MMLocalClass
431
432 init(b: MMLocalClass, a: MMLocalClass)
433 do
434 _local_class = a
435 inheriter = b.get_type
436 stype = _local_class.get_type
437 end
438 end
439
440
441 class MMSpecAncestor
442 special MMAncestor
443 redef meth local_class do return stype.local_class
444
445 init(inheriter: MMType, stype: MMType)
446 do
447 _inheriter = inheriter
448 _stype = stype
449 end
450 end
451
452 class MMDefaultAncestor
453 special MMAncestor
454 redef meth local_class do return stype.local_class
455
456 init(b: MMLocalClass, anc: MMType)
457 do
458 assert b != null
459 assert b.module != null
460 assert anc != null
461 inheriter = b.get_type
462 stype = anc
463 end
464 end