removed C99 constructs
[libfirm] / ir / tr / tr_inheritance.c
1 /**
2  *
3  * @file tp_inheritance.c
4  *
5  * Project:     libFIRM                                                   <br>
6  * File name:   ir/tr/tp_inheritance.c                                    <br>
7  * Purpose:     Utility routines for inheritance representation           <br>
8  * Author:      Goetz Lindenmaier                                         <br>
9  * Modified by:                                                           <br>
10  * Created:                                                               <br>
11  * Copyright:   (c) 2001-2005 Universität Karlsruhe                       <br>
12  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE. <br>
13  * CVS-ID:      $Id$
14  *
15  *
16  *
17  *  @see  type.h entity.h
18  */
19
20 #include "type.h"
21 #include "entity.h"
22 #include "typewalk.h"
23 #include "irprog_t.h"
24 #include "pset.h"
25 #include "set.h"
26 #include "mangle.h"
27 //#include ".h"
28
29
30
31 /* ----------------------------------------------------------------------- */
32 /* Resolve implicit inheritance.                                           */
33 /* ----------------------------------------------------------------------- */
34
35 ident *default_mangle_inherited_name(entity *super, type *clss) {
36   return mangle_u(get_type_ident(clss), get_entity_ident(super));
37 }
38
39 /** Replicates all entities in all super classes that are not overwritten
40    by an entity of this class. */
41 static void copy_entities_from_superclass(type *clss, void *env)
42 {
43   int i, j, k, l;
44   int overwritten;
45   type *super, *inhenttype;
46   entity *inhent, *thisent;
47   mangle_inherited_name_func *mfunc = (mangle_inherited_name_func *)env;
48
49   for(i = 0; i < get_class_n_supertypes(clss); i++) {
50     super = get_class_supertype(clss, i);
51     assert(is_Class_type(super) && "not a class");
52     for(j = 0; j < get_class_n_members(super); j++) {
53       inhent = get_class_member(super, j);
54       inhenttype = get_entity_type(inhent);
55       /* check whether inhent is already overwritten */
56       overwritten = 0;
57       for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
58               thisent = get_class_member(clss, k);
59               for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
60                 if(inhent == get_entity_overwrites(thisent, l)) {
61                   /* overwritten - do not copy */
62                   overwritten = 1;
63                   break;
64                 }
65               }
66       }
67       /* Inherit entity */
68       if (!overwritten) {
69               thisent = copy_entity_own(inhent, clss);
70               add_entity_overwrites(thisent, inhent);
71               set_entity_peculiarity(thisent, peculiarity_inherited);
72               set_entity_ld_ident(thisent, mfunc(inhent, clss));
73               if (get_entity_variability(inhent) == variability_constant) {
74                 assert(is_atomic_entity(inhent) &&  /* @@@ */
75                        "Inheritance of constant, compound entities not implemented");
76                 set_entity_variability(thisent, variability_constant);
77                 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
78               }
79       }
80     }
81   }
82 }
83
84 /* Resolve implicit inheritance.
85  *
86  *  Resolves the implicit inheritance supplied by firm.
87  */
88 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
89   if (!mfunc)
90     mfunc = default_mangle_inherited_name;
91   class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)mfunc);
92 }
93
94
95 /* ----------------------------------------------------------------------- */
96 /* The transitive closure of the subclass/superclass and                   */
97 /* overwrites/overwrittenby relation.                                      */
98 /*                                                                         */
99 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
100 /* closure.  Adding a new type/entity or changing the basic relations in   */
101 /* some other way invalidates the transitive closure, i.e., it is not      */
102 /* updated by the basic functions.                                         */
103 /*                                                                         */
104 /* All functions are named as their counterparts for the basic relations,  */
105 /* adding the infix 'trans_'.                                              */
106 /* ----------------------------------------------------------------------- */
107
108 void                        set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
109   irp->inh_trans_closure_state = s;
110 }
111 void                        invalidate_irp_inh_transitive_closure_state(void) {
112   if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
113     irp->inh_trans_closure_state = inh_transitive_closure_invalid;
114 }
115 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
116   return irp->inh_trans_closure_state;
117 }
118
119 static void assert_valid_state(void) {
120   assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
121          irp->inh_trans_closure_state == inh_transitive_closure_invalid);
122 }
123
124 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
125 /* There is a set that extends each entity/type with two new               */
126 /* fields:  one for the upwards directed relation: 'up' (supertype,        */
127 /* overwrites) and one for the downwards directed relation: 'down' (sub-   */
128 /* type, overwrittenby.  These fields contain psets (and maybe later       */
129 /* arrays) listing all subtypes...                                         */
130 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131
132 typedef struct {
133   firm_kind *kind;   /* An entity or type. */
134   pset *up;
135   pset *down;
136 } tr_inh_trans_tp;
137
138 /* We use this set for all types and entities.  */
139 static set *tr_inh_trans_set = NULL;
140
141 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
142   tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
143   tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
144   return (ef1->kind != ef2->kind);
145 }
146
147 static INLINE unsigned int tr_inh_trans_hash(void *e) {
148   void *v = (void *) ((tr_inh_trans_tp *)e)->kind;
149   return HASH_PTR(v);
150 }
151
152 typedef enum {
153   d_up,
154   d_down,
155 } dir;
156
157 /* This always completes successfully. */
158 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
159   tr_inh_trans_tp a, *found;
160   a.kind = k;
161
162   if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
163
164   found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
165   if (!found) {
166     a.up   = pset_new_ptr(16);
167     a.down = pset_new_ptr(16);
168     found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
169   }
170   return found;
171 }
172
173 static pset *get_entity_map(entity *ent, dir d) {
174   tr_inh_trans_tp *found;
175
176   assert(is_entity(ent));
177   found = get_firm_kind_entry((firm_kind *)ent);
178   return (d == d_up) ? found->up : found->down;
179 }
180 /*
181 static void  add_entity_map(entity *ent, dir d, entity *new) {
182   tr_inh_trans_tp *found;
183
184   assert(is_entity(ent) && is_entity(new));
185   tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
186   if (d == d_up)
187     pset_insert_ptr(found->up,   new);
188   else
189     pset_insert_ptr(found->down, new);
190 }
191 */
192 static pset *get_type_map(type *tp, dir d) {
193   tr_inh_trans_tp *found;
194
195   assert(is_type(tp));
196   found = get_firm_kind_entry((firm_kind *)tp);
197   return (d == d_up) ? found->up : found->down;
198 }
199 /*
200 static void  add_type_map(type *tp, dir d, type *new) {
201   tr_inh_trans_tp *found;
202
203   assert(is_type(tp) && is_type(new));
204   found = get_firm_kind_entry((firm_kind *)tp);
205   if (d == d_up) pset_insert_ptr(found->up,   new);
206   else           pset_insert_ptr(found->down, new);
207 }
208 */
209
210
211 /**
212  * Walk over all types reachable from tp in the sub/supertype
213  * relation and compute the closure for the two downwards directed
214  * relations.
215  *
216  * The walk in the dag formed by the relation is tricky:  We must visit
217  * all subtypes before visiting the supertypes.  So we first walk down.
218  * Then we can compute the closure for this type.  Then we walk up.
219  * As we call ourselves recursive, and walk in both directions, there
220  * can be cycles.  So we have to make sure, that if we visit a node
221  * a second time (in a walk up) we do nothing.  For this we increment
222  * the master visited flag twice.
223  * If the type is marked with master_flag_visited-1 it is on the stack.
224  * If it is marked with master_flag_visited it is fully processed.
225  *
226  * Well, we still miss some candidates ... */
227 static void compute_down_closure(type *tp) {
228   pset *myset, *subset;
229   int i, n_subtypes, n_members, n_supertypes;
230   int master_visited = get_master_type_visited();
231
232   assert(is_Class_type(tp));
233
234   set_type_visited(tp, master_visited-1);
235
236   /* Recursive descend. */
237   n_subtypes = get_class_n_subtypes(tp);
238   for (i = 0; i < n_subtypes; ++i) {
239     type *stp = get_class_subtype(tp, i);
240     if (type_not_visited(stp)) {
241       assert(get_type_visited(tp) < master_visited-1);
242       compute_down_closure(stp);
243     }
244   }
245
246   /* types */
247   myset = get_type_map(tp, d_down);
248   for (i = 0; i < n_subtypes; ++i) {
249     type *stp = get_class_subtype(tp, i);
250     subset = get_type_map(stp, d_down);
251     pset_insert_ptr(myset, stp);
252     pset_insert_pset_ptr(myset, subset);
253   }
254
255   /* entities */
256   n_members = get_class_n_members(tp);
257   for (i = 0; i < n_members; ++i) {
258     entity *mem = get_class_member(tp, i);
259     int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
260
261     myset = get_entity_map(mem, d_down);
262     for (j = 0; j > n_overwrittenby; ++j) {
263       entity *ov = get_entity_overwrittenby(mem, j);
264       subset = get_entity_map(ov, d_down);
265       pset_insert_pset_ptr(myset, subset);
266       pset_insert_ptr(myset, ov);
267     }
268   }
269
270   mark_type_visited(tp);
271
272   /* Walk up. */
273   n_supertypes = get_class_n_supertypes(tp);
274   for (i = 0; i < n_supertypes; ++i) {
275     type *stp = get_class_supertype(tp, i);
276     if (get_type_visited(tp) < master_visited-1) {
277       compute_down_closure(stp);
278     }
279   }
280 }
281
282 static void compute_up_closure(type *tp) {
283   pset *myset, *subset;
284   int i, n_subtypes, n_members, n_supertypes;
285   int master_visited = get_master_type_visited();
286
287   assert(is_Class_type(tp));
288
289   set_type_visited(tp, master_visited-1);
290
291   /* Recursive descend. */
292   n_supertypes = get_class_n_supertypes(tp);
293   for (i = 0; i < n_supertypes; ++i) {
294     type *stp = get_class_supertype(tp, i);
295     if (type_not_visited(stp)) {
296       assert(get_type_visited(tp) < get_master_type_visited()-1);
297       compute_up_closure(stp);
298     }
299   }
300
301   /* types */
302   myset = get_type_map(tp, d_up);
303   for (i = 0; i < n_supertypes; ++i) {
304     type *stp = get_class_supertype(tp, i);
305     subset = get_type_map(stp, d_up);
306     pset_insert_ptr(myset, stp);
307     pset_insert_pset_ptr(myset, subset);
308   }
309
310   /* entities */
311   n_members = get_class_n_members(tp);
312   for (i = 0; i < n_members; ++i) {
313     entity *mem = get_class_member(tp, i);
314     int j, n_overwrites = get_entity_n_overwrites(mem);
315
316     myset = get_entity_map(mem, d_up);
317     for (j = 0; j > n_overwrites; ++j) {
318       entity *ov = get_entity_overwrites(mem, j);
319       subset = get_entity_map(ov, d_up);
320       pset_insert_pset_ptr(myset, subset);
321       pset_insert_ptr(myset, ov);
322     }
323   }
324
325   mark_type_visited(tp);
326
327   /* Walk down. */
328   n_subtypes = get_class_n_subtypes(tp);
329   for (i = 0; i < n_subtypes; ++i) {
330     type *stp = get_class_subtype(tp, i);
331     if (get_type_visited(tp) < master_visited-1) {
332       compute_up_closure(stp);
333     }
334   }
335 }
336
337 /** Compute the transitive closure of the subclass/superclass and
338  *  overwrites/overwrittenby relation.
339  *
340  *  This function walks over the ir (O(#types+#entities)) to compute the
341  *  transitive closure.    */
342 void compute_inh_transitive_closure(void) {
343   int i, n_types = get_irp_n_types();
344   free_inh_transitive_closure();
345
346   /* The 'down' relation */
347   inc_master_type_visited();  /* Inc twice: one if on stack, second if values computed. */
348   inc_master_type_visited();
349   for (i = 0; i < n_types; ++i) {
350     type *tp = get_irp_type(i);
351     if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
352       int j, n_subtypes = get_class_n_subtypes(tp);
353       int has_unmarked_subtype = false;
354
355       assert(get_type_visited(tp) < get_master_type_visited()-1);
356       for (j = 0; j < n_subtypes && !has_unmarked_subtype; ++j) {
357               type *stp = get_class_subtype(tp, j);
358               if (type_not_visited(stp)) has_unmarked_subtype = true;
359       }
360
361       /* This is a good starting point. */
362       if (!has_unmarked_subtype)
363               compute_down_closure(tp);
364     }
365   }
366
367   /* The 'up' relation */
368   inc_master_type_visited();
369   inc_master_type_visited();
370   for (i = 0; i < n_types; ++i) {
371     type *tp = get_irp_type(i);
372     if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
373       int j, n_supertypes = get_class_n_supertypes(tp);
374       int has_unmarked_supertype = false;
375
376       assert(get_type_visited(tp) < get_master_type_visited()-1);
377       for (j = 0; j < n_supertypes && !has_unmarked_supertype; ++j) {
378               type *stp = get_class_supertype(tp, j);
379               if (type_not_visited(stp)) has_unmarked_supertype = true;
380       }
381
382       /* This is a good starting point. */
383       if (!has_unmarked_supertype)
384               compute_up_closure(tp);
385     }
386   }
387
388   irp->inh_trans_closure_state = inh_transitive_closure_valid;
389 }
390
391 /** Free memory occupied by the transitive closure information. */
392 void free_inh_transitive_closure(void) {
393   if (tr_inh_trans_set) {
394     tr_inh_trans_tp *elt;
395     for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
396       del_pset(elt->up);
397       del_pset(elt->down);
398     }
399     del_set(tr_inh_trans_set);
400     tr_inh_trans_set = NULL;
401   }
402   irp->inh_trans_closure_state = inh_transitive_closure_none;
403 }
404
405 /* - subtype ------------------------------------------------------------- */
406
407 type *get_class_trans_subtype_first(type *tp) {
408   assert_valid_state();
409   return pset_first(get_type_map(tp, d_down));
410 }
411
412 type *get_class_trans_subtype_next (type *tp) {
413   assert_valid_state();
414   return pset_next(get_type_map(tp, d_down));
415 }
416
417 /* - supertype ----------------------------------------------------------- */
418
419 type *get_class_trans_supertype_first(type *tp) {
420   assert_valid_state();
421   return pset_first(get_type_map(tp, d_up));
422 }
423
424 type *get_class_trans_supertype_next (type *tp) {
425   assert_valid_state();
426   return pset_next(get_type_map(tp, d_up));
427 }
428
429 /* - overwrittenby ------------------------------------------------------- */
430
431 entity *get_entity_trans_overwrittenby_first(entity *ent) {
432   assert_valid_state();
433   return pset_first(get_entity_map(ent, d_down));
434 }
435
436 entity *get_entity_trans_overwrittenby_next (entity *ent) {
437   assert_valid_state();
438   return pset_next(get_entity_map(ent, d_down));
439 }
440
441 /* - overwrites ---------------------------------------------------------- */
442
443
444 /** Iterate over all transitive overwritten entities. */
445 entity *get_entity_trans_overwrites_first(entity *ent) {
446   assert_valid_state();
447   return pset_first(get_entity_map(ent, d_up));
448 }
449
450 entity *get_entity_trans_overwrites_next (entity *ent) {
451   assert_valid_state();
452   return pset_next(get_entity_map(ent, d_up));
453 }
454
455
456
457
458
459 /* ----------------------------------------------------------------------- */
460 /* Classify pairs of types/entities in the inheritance relations.          */
461 /* ----------------------------------------------------------------------- */
462
463 /* Returns true if low is subclass of high. */
464 int is_subclass_of(type *low, type *high) {
465   int i, n_subtypes;
466   assert(is_Class_type(low) && is_Class_type(high));
467
468   if (low == high) return 1;
469
470   if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
471     pset *m = get_type_map(high, d_down);
472     return pset_find_ptr(m, low) ? 1 : 0;
473   }
474
475   /* depth first search from high downwards. */
476   n_subtypes = get_class_n_subtypes(high);
477   for (i = 0; i < n_subtypes; i++) {
478     type *stp = get_class_subtype(high, i);
479     if (low == stp) return 1;
480     if (is_subclass_of(low, stp))
481       return 1;
482   }
483   return 0;
484 }
485
486 int is_overwritten_by(entity *high, entity *low) {
487   int i, n_overwrittenby;
488   assert(is_entity(low) && is_entity(high));
489
490   if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
491     pset *m = get_entity_map(high, d_down);
492     return pset_find_ptr(m, low) ? 1 : 0;
493   }
494
495   /* depth first search from high downwards. */
496   n_overwrittenby = get_entity_n_overwrittenby(high);
497   for (i = 0; i < n_overwrittenby; i++) {
498     entity *ov = get_entity_overwrittenby(high, i);
499     if (low == ov) return 1;
500     if (is_overwritten_by(low, ov))
501       return 1;
502   }
503   return 0;
504 }
505
506
507 /** Need two routines because I want to assert the result. */
508 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) {
509   int i, n_overwrittenby;
510   entity *res = NULL;
511
512   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
513
514   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
515   for (i = 0; i < n_overwrittenby; ++i) {
516     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
517     if (res)
518       break;
519   }
520
521   return res;
522 }
523
524 /* Resolve polymorphy in the inheritance relation.
525  *
526  * Returns the dynamically referenced entity if the static entity and the
527  * dynamic type are given.
528  * Search downwards in overwritten tree. */
529 entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) {
530   entity *res;
531   assert(static_ent && is_entity(static_ent));
532
533   res = resolve_ent_polymorphy2(dynamic_class, static_ent);
534   assert(res);
535
536   return res;
537 }