3 * @file tr_inheritance.c
5 * Project: libFIRM <br>
6 * File name: ir/tr/tr_inheritance.c <br>
7 * Purpose: Utility routines for inheritance representation <br>
8 * Author: Goetz Lindenmaier <br>
11 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
12 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
17 * @see type.h entity.h
23 #include "irgraph_t.h"
33 /* ----------------------------------------------------------------------- */
34 /* Resolve implicit inheritance. */
35 /* ----------------------------------------------------------------------- */
37 ident *default_mangle_inherited_name(entity *super, type *clss) {
38 return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
41 /** Replicates all entities in all super classes that are not overwritten
42 by an entity of this class. */
43 static void copy_entities_from_superclass(type *clss, void *env)
47 type *super, *inhenttype;
48 entity *inhent, *thisent;
49 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
51 for(i = 0; i < get_class_n_supertypes(clss); i++) {
52 super = get_class_supertype(clss, i);
53 assert(is_Class_type(super) && "not a class");
54 for(j = 0; j < get_class_n_members(super); j++) {
55 inhent = get_class_member(super, j);
56 inhenttype = get_entity_type(inhent);
57 /* check whether inhent is already overwritten */
59 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
60 thisent = get_class_member(clss, k);
61 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
62 if(inhent == get_entity_overwrites(thisent, l)) {
63 /* overwritten - do not copy */
71 thisent = copy_entity_own(inhent, clss);
72 add_entity_overwrites(thisent, inhent);
73 if (get_entity_peculiarity(inhent) == peculiarity_existent)
74 set_entity_peculiarity(thisent, peculiarity_inherited);
75 set_entity_ld_ident(thisent, mfunc(inhent, clss));
76 if (get_entity_variability(inhent) == variability_constant) {
77 assert(is_atomic_entity(inhent) && /* @@@ */
78 "Inheritance of constant, compound entities not implemented");
79 set_entity_variability(thisent, variability_constant);
80 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
87 /* Resolve implicit inheritance.
89 * Resolves the implicit inheritance supplied by firm.
91 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
93 mfunc = default_mangle_inherited_name;
94 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
98 /* ----------------------------------------------------------------------- */
99 /* The transitive closure of the subclass/superclass and */
100 /* overwrites/overwrittenby relation. */
102 /* A walk over the ir (O(#types+#entities)) computes the transitive */
103 /* closure. Adding a new type/entity or changing the basic relations in */
104 /* some other way invalidates the transitive closure, i.e., it is not */
105 /* updated by the basic functions. */
107 /* All functions are named as their counterparts for the basic relations, */
108 /* adding the infix 'trans_'. */
109 /* ----------------------------------------------------------------------- */
111 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
112 irp->inh_trans_closure_state = s;
114 void invalidate_irp_inh_transitive_closure_state(void) {
115 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
116 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
118 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
119 return irp->inh_trans_closure_state;
122 static void assert_valid_state(void) {
123 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
124 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
127 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
128 /* There is a set that extends each entity/type with two new */
129 /* fields: one for the upwards directed relation: 'up' (supertype, */
130 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
131 /* type, overwrittenby. These fields contain psets (and maybe later */
132 /* arrays) listing all subtypes... */
133 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136 firm_kind *kind; /* An entity or type. */
141 /* We use this set for all types and entities. */
142 static set *tr_inh_trans_set = NULL;
144 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
145 tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
146 tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
147 return (ef1->kind != ef2->kind);
150 static INLINE unsigned int tr_inh_trans_hash(void *e) {
151 void *v = (void *) ((tr_inh_trans_tp *)e)->kind;
160 /* This always completes successfully. */
161 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
162 tr_inh_trans_tp a, *found;
165 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
167 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
169 a.up = pset_new_ptr(16);
170 a.down = pset_new_ptr(16);
171 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
176 static pset *get_entity_map(entity *ent, dir d) {
177 tr_inh_trans_tp *found;
179 assert(is_entity(ent));
180 found = get_firm_kind_entry((firm_kind *)ent);
181 return (d == d_up) ? found->up : found->down;
184 static void add_entity_map(entity *ent, dir d, entity *new) {
185 tr_inh_trans_tp *found;
187 assert(is_entity(ent) && is_entity(new));
188 tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
190 pset_insert_ptr(found->up, new);
192 pset_insert_ptr(found->down, new);
195 static pset *get_type_map(type *tp, dir d) {
196 tr_inh_trans_tp *found;
199 found = get_firm_kind_entry((firm_kind *)tp);
200 return (d == d_up) ? found->up : found->down;
203 static void add_type_map(type *tp, dir d, type *new) {
204 tr_inh_trans_tp *found;
206 assert(is_type(tp) && is_type(new));
207 found = get_firm_kind_entry((firm_kind *)tp);
208 if (d == d_up) pset_insert_ptr(found->up, new);
209 else pset_insert_ptr(found->down, new);
215 * Walk over all types reachable from tp in the sub/supertype
216 * relation and compute the closure for the two downwards directed
219 * The walk in the dag formed by the relation is tricky: We must visit
220 * all subtypes before visiting the supertypes. So we first walk down.
221 * Then we can compute the closure for this type. Then we walk up.
222 * As we call ourselves recursive, and walk in both directions, there
223 * can be cycles. So we have to make sure, that if we visit a node
224 * a second time (in a walk up) we do nothing. For this we increment
225 * the master visited flag twice.
226 * If the type is marked with master_flag_visited-1 it is on the stack.
227 * If it is marked with master_flag_visited it is fully processed.
229 * Well, we still miss some candidates ... */
230 static void compute_down_closure(type *tp) {
231 pset *myset, *subset;
232 int i, n_subtypes, n_members, n_supertypes;
233 unsigned long master_visited = get_master_type_visited();
235 assert(is_Class_type(tp));
237 set_type_visited(tp, master_visited-1);
239 /* Recursive descend. */
240 n_subtypes = get_class_n_subtypes(tp);
241 for (i = 0; i < n_subtypes; ++i) {
242 type *stp = get_class_subtype(tp, i);
243 if (get_type_visited(stp) < master_visited-1) {
244 compute_down_closure(stp);
249 myset = get_type_map(tp, d_down);
250 for (i = 0; i < n_subtypes; ++i) {
251 type *stp = get_class_subtype(tp, i);
252 subset = get_type_map(stp, d_down);
253 pset_insert_ptr(myset, stp);
254 pset_insert_pset_ptr(myset, subset);
258 n_members = get_class_n_members(tp);
259 for (i = 0; i < n_members; ++i) {
260 entity *mem = get_class_member(tp, i);
261 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
263 myset = get_entity_map(mem, d_down);
264 for (j = 0; j < n_overwrittenby; ++j) {
265 entity *ov = get_entity_overwrittenby(mem, j);
266 subset = get_entity_map(ov, d_down);
267 pset_insert_ptr(myset, ov);
268 pset_insert_pset_ptr(myset, subset);
272 mark_type_visited(tp);
275 n_supertypes = get_class_n_supertypes(tp);
276 for (i = 0; i < n_supertypes; ++i) {
277 type *stp = get_class_supertype(tp, i);
278 if (get_type_visited(stp) < master_visited-1) {
279 compute_down_closure(stp);
284 static void compute_up_closure(type *tp) {
285 pset *myset, *subset;
286 int i, n_subtypes, n_members, n_supertypes;
287 unsigned long master_visited = get_master_type_visited();
289 assert(is_Class_type(tp));
291 set_type_visited(tp, master_visited-1);
293 /* Recursive descend. */
294 n_supertypes = get_class_n_supertypes(tp);
295 for (i = 0; i < n_supertypes; ++i) {
296 type *stp = get_class_supertype(tp, i);
297 if (get_type_visited(stp) < get_master_type_visited()-1) {
298 compute_up_closure(stp);
303 myset = get_type_map(tp, d_up);
304 for (i = 0; i < n_supertypes; ++i) {
305 type *stp = get_class_supertype(tp, i);
306 subset = get_type_map(stp, d_up);
307 pset_insert_ptr(myset, stp);
308 pset_insert_pset_ptr(myset, subset);
312 n_members = get_class_n_members(tp);
313 for (i = 0; i < n_members; ++i) {
314 entity *mem = get_class_member(tp, i);
315 int j, n_overwrites = get_entity_n_overwrites(mem);
317 myset = get_entity_map(mem, d_up);
318 for (j = 0; j < n_overwrites; ++j) {
319 entity *ov = get_entity_overwrites(mem, j);
320 subset = get_entity_map(ov, d_up);
321 pset_insert_pset_ptr(myset, subset);
322 pset_insert_ptr(myset, ov);
326 mark_type_visited(tp);
329 n_subtypes = get_class_n_subtypes(tp);
330 for (i = 0; i < n_subtypes; ++i) {
331 type *stp = get_class_subtype(tp, i);
332 if (get_type_visited(stp) < master_visited-1) {
333 compute_up_closure(stp);
338 /** Compute the transitive closure of the subclass/superclass and
339 * overwrites/overwrittenby relation.
341 * This function walks over the ir (O(#types+#entities)) to compute the
342 * transitive closure. */
343 void compute_inh_transitive_closure(void) {
344 int i, n_types = get_irp_n_types();
345 free_inh_transitive_closure();
347 /* The 'down' relation */
348 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
349 inc_master_type_visited();
350 for (i = 0; i < n_types; ++i) {
351 type *tp = get_irp_type(i);
352 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
353 int j, n_subtypes = get_class_n_subtypes(tp);
354 int has_unmarked_subtype = 0;
356 assert(get_type_visited(tp) < get_master_type_visited()-1);
357 for (j = 0; j < n_subtypes; ++j) {
358 type *stp = get_class_subtype(tp, j);
359 if (type_not_visited(stp)) {
360 has_unmarked_subtype = 1;
365 /* This is a good starting point. */
366 if (!has_unmarked_subtype)
367 compute_down_closure(tp);
371 /* The 'up' relation */
372 inc_master_type_visited();
373 inc_master_type_visited();
374 for (i = 0; i < n_types; ++i) {
375 type *tp = get_irp_type(i);
376 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
377 int j, n_supertypes = get_class_n_supertypes(tp);
378 int has_unmarked_supertype = 0;
380 assert(get_type_visited(tp) < get_master_type_visited()-1);
381 for (j = 0; j < n_supertypes; ++j) {
382 type *stp = get_class_supertype(tp, j);
383 if (type_not_visited(stp)) {
384 has_unmarked_supertype = 1;
389 /* This is a good starting point. */
390 if (!has_unmarked_supertype)
391 compute_up_closure(tp);
395 irp->inh_trans_closure_state = inh_transitive_closure_valid;
398 /** Free memory occupied by the transitive closure information. */
399 void free_inh_transitive_closure(void) {
400 if (tr_inh_trans_set) {
401 tr_inh_trans_tp *elt;
402 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
406 del_set(tr_inh_trans_set);
407 tr_inh_trans_set = NULL;
409 irp->inh_trans_closure_state = inh_transitive_closure_none;
412 /* - subtype ------------------------------------------------------------- */
414 type *get_class_trans_subtype_first(type *tp) {
415 assert_valid_state();
416 return pset_first(get_type_map(tp, d_down));
419 type *get_class_trans_subtype_next (type *tp) {
420 assert_valid_state();
421 return pset_next(get_type_map(tp, d_down));
424 int is_class_trans_subtype (type *tp, type *subtp) {
425 assert_valid_state();
426 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
429 /* - supertype ----------------------------------------------------------- */
431 type *get_class_trans_supertype_first(type *tp) {
432 assert_valid_state();
433 return pset_first(get_type_map(tp, d_up));
436 type *get_class_trans_supertype_next (type *tp) {
437 assert_valid_state();
438 return pset_next(get_type_map(tp, d_up));
441 /* - overwrittenby ------------------------------------------------------- */
443 entity *get_entity_trans_overwrittenby_first(entity *ent) {
444 assert_valid_state();
445 return pset_first(get_entity_map(ent, d_down));
448 entity *get_entity_trans_overwrittenby_next (entity *ent) {
449 assert_valid_state();
450 return pset_next(get_entity_map(ent, d_down));
453 /* - overwrites ---------------------------------------------------------- */
456 /** Iterate over all transitive overwritten entities. */
457 entity *get_entity_trans_overwrites_first(entity *ent) {
458 assert_valid_state();
459 return pset_first(get_entity_map(ent, d_up));
462 entity *get_entity_trans_overwrites_next (entity *ent) {
463 assert_valid_state();
464 return pset_next(get_entity_map(ent, d_up));
471 /* ----------------------------------------------------------------------- */
472 /* Classify pairs of types/entities in the inheritance relations. */
473 /* ----------------------------------------------------------------------- */
475 /* Returns true if low is subclass of high. */
476 int is_subclass_of(type *low, type *high) {
478 assert(is_Class_type(low) && is_Class_type(high));
480 if (low == high) return 1;
482 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
483 pset *m = get_type_map(high, d_down);
484 return pset_find_ptr(m, low) ? 1 : 0;
487 /* depth first search from high downwards. */
488 n_subtypes = get_class_n_subtypes(high);
489 for (i = 0; i < n_subtypes; i++) {
490 type *stp = get_class_subtype(high, i);
491 if (low == stp) return 1;
492 if (is_subclass_of(low, stp))
499 /* Subclass check for pointers to classes.
501 * Dereferences at both types the same amount of pointer types (as
502 * many as possible). If the remaining types are both class types
503 * and subclasses, returns true, else false. Can also be called with
504 * two class types. */
505 int is_subclass_ptr_of(type *low, type *high) {
506 while (is_Pointer_type(low) && is_Pointer_type(high)) {
507 low = get_pointer_points_to_type(low);
508 high = get_pointer_points_to_type(high);
511 if (is_Class_type(low) && is_Class_type(high))
512 return is_subclass_of(low, high);
516 int is_superclass_of(type *high, type *low) {
517 return is_subclass_of(low, high);
520 int is_superclass_ptr_of(type *high, type *low) {
521 return is_subclass_ptr_of(low, high);
524 int is_overwritten_by(entity *high, entity *low) {
525 int i, n_overwrittenby;
526 assert(is_entity(low) && is_entity(high));
528 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
529 pset *m = get_entity_map(high, d_down);
530 return pset_find_ptr(m, low) ? 1 : 0;
533 /* depth first search from high downwards. */
534 n_overwrittenby = get_entity_n_overwrittenby(high);
535 for (i = 0; i < n_overwrittenby; i++) {
536 entity *ov = get_entity_overwrittenby(high, i);
537 if (low == ov) return 1;
538 if (is_overwritten_by(low, ov))
545 /** Need two routines because I want to assert the result. */
546 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) {
547 int i, n_overwrittenby;
550 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
552 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
553 for (i = 0; i < n_overwrittenby; ++i) {
554 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
562 /* Resolve polymorphy in the inheritance relation.
564 * Returns the dynamically referenced entity if the static entity and the
565 * dynamic type are given.
566 * Search downwards in overwritten tree. */
567 entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) {
569 assert(static_ent && is_entity(static_ent));
571 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
579 /* ----------------------------------------------------------------------- */
580 /* Class cast state handling. */
581 /* ----------------------------------------------------------------------- */
583 /* - State handling. ----------------------------------------- */
585 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
586 if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
587 irg->class_cast_state = s;
590 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
591 return irg->class_cast_state;
594 void set_irp_class_cast_state(ir_class_cast_state s) {
596 for (i = 0; i < get_irp_n_irgs(); ++i)
597 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
598 irp->class_cast_state = s;
601 ir_class_cast_state get_irp_class_cast_state(void) {
602 return irp->class_cast_state;
605 char *get_class_cast_state_string(ir_class_cast_state s) {
606 #define X(a) case a: return #a
608 X(ir_class_casts_any);
609 X(ir_class_casts_transitive);
610 X(ir_class_casts_normalized);
611 X(ir_class_casts_state_max);
612 default: return "invalid class cast state";
617 /* - State verification. ------------------------------------- */
619 typedef struct ccs_env {
620 ir_class_cast_state expected_state;
621 ir_class_cast_state worst_situation;
624 void verify_irn_class_cast_state(ir_node *n, void *env) {
625 ccs_env *ccs = (ccs_env *)env;
626 ir_class_cast_state this_state = ir_class_casts_any;
627 type *fromtype, *totype;
630 if (get_irn_op(n) != op_Cast) return;
632 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
633 totype = get_Cast_type(n);
635 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
636 totype = get_pointer_points_to_type(totype);
637 fromtype = get_pointer_points_to_type(fromtype);
641 if (!is_Class_type(totype)) return;
643 if (is_subclass_of(totype, fromtype) ||
644 is_subclass_of(fromtype, totype) ) {
645 this_state = ir_class_casts_transitive;
646 if ((get_class_supertype_index(totype, fromtype) != -1) ||
647 (get_class_supertype_index(fromtype, totype) != -1) ||
648 fromtype == totype) {
649 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
650 if ((get_class_supertype_index(totype, fromtype) == -1) &&
651 (get_class_supertype_index(fromtype, totype) == -1) ) { */
652 this_state = ir_class_casts_normalized;
656 if (!(this_state >= ccs->expected_state)) {
657 printf(" Node is "); DDMN(n);
658 printf(" totype "); DDMT(totype);
659 printf(" fromtype "); DDMT(fromtype);
660 printf(" this_state: %s, exp. state: %s\n",
661 get_class_cast_state_string(this_state),
662 get_class_cast_state_string(ccs->expected_state));
663 assert(this_state >= ccs->expected_state &&
664 "invalid state class cast state setting in graph");
667 if (this_state < ccs->worst_situation)
668 ccs->worst_situation = this_state;
672 /** Verify that the graph meets requirements of state set. */
673 void verify_irg_class_cast_state(ir_graph *irg) {
676 env.expected_state = get_irg_class_cast_state(irg);
677 env.worst_situation = ir_class_casts_normalized;
679 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
681 if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
682 printf("Note: class cast state is set lower than reqired in graph\n ");
684 printf(" state is %s, reqired is %s\n",
685 get_class_cast_state_string(env.expected_state),
686 get_class_cast_state_string(env.worst_situation));