3 * @file tp_inheritance.c
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>
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 set_entity_peculiarity(thisent, peculiarity_inherited);
74 set_entity_ld_ident(thisent, mfunc(inhent, clss));
75 if (get_entity_variability(inhent) == variability_constant) {
76 assert(is_atomic_entity(inhent) && /* @@@ */
77 "Inheritance of constant, compound entities not implemented");
78 set_entity_variability(thisent, variability_constant);
79 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
86 /* Resolve implicit inheritance.
88 * Resolves the implicit inheritance supplied by firm.
90 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
92 mfunc = default_mangle_inherited_name;
93 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)mfunc);
97 /* ----------------------------------------------------------------------- */
98 /* The transitive closure of the subclass/superclass and */
99 /* overwrites/overwrittenby relation. */
101 /* A walk over the ir (O(#types+#entities)) computes the transitive */
102 /* closure. Adding a new type/entity or changing the basic relations in */
103 /* some other way invalidates the transitive closure, i.e., it is not */
104 /* updated by the basic functions. */
106 /* All functions are named as their counterparts for the basic relations, */
107 /* adding the infix 'trans_'. */
108 /* ----------------------------------------------------------------------- */
110 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
111 irp->inh_trans_closure_state = s;
113 void invalidate_irp_inh_transitive_closure_state(void) {
114 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
115 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
117 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
118 return irp->inh_trans_closure_state;
121 static void assert_valid_state(void) {
122 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
123 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
126 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127 /* There is a set that extends each entity/type with two new */
128 /* fields: one for the upwards directed relation: 'up' (supertype, */
129 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
130 /* type, overwrittenby. These fields contain psets (and maybe later */
131 /* arrays) listing all subtypes... */
132 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135 firm_kind *kind; /* An entity or type. */
140 /* We use this set for all types and entities. */
141 static set *tr_inh_trans_set = NULL;
143 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
144 tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
145 tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
146 return (ef1->kind != ef2->kind);
149 static INLINE unsigned int tr_inh_trans_hash(void *e) {
150 void *v = (void *) ((tr_inh_trans_tp *)e)->kind;
159 /* This always completes successfully. */
160 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
161 tr_inh_trans_tp a, *found;
164 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
166 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
168 a.up = pset_new_ptr(16);
169 a.down = pset_new_ptr(16);
170 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
175 static pset *get_entity_map(entity *ent, dir d) {
176 tr_inh_trans_tp *found;
178 assert(is_entity(ent));
179 found = get_firm_kind_entry((firm_kind *)ent);
180 return (d == d_up) ? found->up : found->down;
183 static void add_entity_map(entity *ent, dir d, entity *new) {
184 tr_inh_trans_tp *found;
186 assert(is_entity(ent) && is_entity(new));
187 tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
189 pset_insert_ptr(found->up, new);
191 pset_insert_ptr(found->down, new);
194 static pset *get_type_map(type *tp, dir d) {
195 tr_inh_trans_tp *found;
198 found = get_firm_kind_entry((firm_kind *)tp);
199 return (d == d_up) ? found->up : found->down;
202 static void add_type_map(type *tp, dir d, type *new) {
203 tr_inh_trans_tp *found;
205 assert(is_type(tp) && is_type(new));
206 found = get_firm_kind_entry((firm_kind *)tp);
207 if (d == d_up) pset_insert_ptr(found->up, new);
208 else pset_insert_ptr(found->down, new);
214 * Walk over all types reachable from tp in the sub/supertype
215 * relation and compute the closure for the two downwards directed
218 * The walk in the dag formed by the relation is tricky: We must visit
219 * all subtypes before visiting the supertypes. So we first walk down.
220 * Then we can compute the closure for this type. Then we walk up.
221 * As we call ourselves recursive, and walk in both directions, there
222 * can be cycles. So we have to make sure, that if we visit a node
223 * a second time (in a walk up) we do nothing. For this we increment
224 * the master visited flag twice.
225 * If the type is marked with master_flag_visited-1 it is on the stack.
226 * If it is marked with master_flag_visited it is fully processed.
228 * Well, we still miss some candidates ... */
229 static void compute_down_closure(type *tp) {
230 pset *myset, *subset;
231 int i, n_subtypes, n_members, n_supertypes;
232 unsigned long master_visited = get_master_type_visited();
234 assert(is_Class_type(tp));
236 set_type_visited(tp, master_visited-1);
238 /* Recursive descend. */
239 n_subtypes = get_class_n_subtypes(tp);
240 for (i = 0; i < n_subtypes; ++i) {
241 type *stp = get_class_subtype(tp, i);
242 if (get_type_visited(stp) < master_visited-1) {
243 compute_down_closure(stp);
248 myset = get_type_map(tp, d_down);
249 for (i = 0; i < n_subtypes; ++i) {
250 type *stp = get_class_subtype(tp, i);
251 subset = get_type_map(stp, d_down);
252 pset_insert_ptr(myset, stp);
253 pset_insert_pset_ptr(myset, subset);
257 n_members = get_class_n_members(tp);
258 for (i = 0; i < n_members; ++i) {
259 entity *mem = get_class_member(tp, i);
260 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
262 myset = get_entity_map(mem, d_down);
263 for (j = 0; j < n_overwrittenby; ++j) {
264 entity *ov = get_entity_overwrittenby(mem, j);
265 subset = get_entity_map(ov, d_down);
266 pset_insert_ptr(myset, ov);
267 pset_insert_pset_ptr(myset, subset);
271 mark_type_visited(tp);
274 n_supertypes = get_class_n_supertypes(tp);
275 for (i = 0; i < n_supertypes; ++i) {
276 type *stp = get_class_supertype(tp, i);
277 if (get_type_visited(stp) < master_visited-1) {
278 compute_down_closure(stp);
283 static void compute_up_closure(type *tp) {
284 pset *myset, *subset;
285 int i, n_subtypes, n_members, n_supertypes;
286 unsigned long master_visited = get_master_type_visited();
288 assert(is_Class_type(tp));
290 set_type_visited(tp, master_visited-1);
292 /* Recursive descend. */
293 n_supertypes = get_class_n_supertypes(tp);
294 for (i = 0; i < n_supertypes; ++i) {
295 type *stp = get_class_supertype(tp, i);
296 if (get_type_visited(stp) < get_master_type_visited()-1) {
297 compute_up_closure(stp);
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);
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);
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);
325 mark_type_visited(tp);
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(stp) < master_visited-1) {
332 compute_up_closure(stp);
337 /** Compute the transitive closure of the subclass/superclass and
338 * overwrites/overwrittenby relation.
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();
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;
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;
361 /* This is a good starting point. */
362 if (!has_unmarked_subtype)
363 compute_down_closure(tp);
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;
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;
382 /* This is a good starting point. */
383 if (!has_unmarked_supertype)
384 compute_up_closure(tp);
388 irp->inh_trans_closure_state = inh_transitive_closure_valid;
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)) {
399 del_set(tr_inh_trans_set);
400 tr_inh_trans_set = NULL;
402 irp->inh_trans_closure_state = inh_transitive_closure_none;
405 /* - subtype ------------------------------------------------------------- */
407 type *get_class_trans_subtype_first(type *tp) {
408 assert_valid_state();
409 return pset_first(get_type_map(tp, d_down));
412 type *get_class_trans_subtype_next (type *tp) {
413 assert_valid_state();
414 return pset_next(get_type_map(tp, d_down));
417 int is_class_trans_subtype (type *tp, type *subtp) {
418 assert_valid_state();
419 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
422 /* - supertype ----------------------------------------------------------- */
424 type *get_class_trans_supertype_first(type *tp) {
425 assert_valid_state();
426 return pset_first(get_type_map(tp, d_up));
429 type *get_class_trans_supertype_next (type *tp) {
430 assert_valid_state();
431 return pset_next(get_type_map(tp, d_up));
434 /* - overwrittenby ------------------------------------------------------- */
436 entity *get_entity_trans_overwrittenby_first(entity *ent) {
437 assert_valid_state();
438 return pset_first(get_entity_map(ent, d_down));
441 entity *get_entity_trans_overwrittenby_next (entity *ent) {
442 assert_valid_state();
443 return pset_next(get_entity_map(ent, d_down));
446 /* - overwrites ---------------------------------------------------------- */
449 /** Iterate over all transitive overwritten entities. */
450 entity *get_entity_trans_overwrites_first(entity *ent) {
451 assert_valid_state();
452 return pset_first(get_entity_map(ent, d_up));
455 entity *get_entity_trans_overwrites_next (entity *ent) {
456 assert_valid_state();
457 return pset_next(get_entity_map(ent, d_up));
464 /* ----------------------------------------------------------------------- */
465 /* Classify pairs of types/entities in the inheritance relations. */
466 /* ----------------------------------------------------------------------- */
468 /* Returns true if low is subclass of high. */
469 int is_subclass_of(type *low, type *high) {
471 assert(is_Class_type(low) && is_Class_type(high));
473 if (low == high) return 1;
475 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
476 pset *m = get_type_map(high, d_down);
477 return pset_find_ptr(m, low) ? 1 : 0;
480 /* depth first search from high downwards. */
481 n_subtypes = get_class_n_subtypes(high);
482 for (i = 0; i < n_subtypes; i++) {
483 type *stp = get_class_subtype(high, i);
484 if (low == stp) return 1;
485 if (is_subclass_of(low, stp))
492 /* Subclass check for pointers to classes.
494 * Dereferences at both types the same amount of pointer types (as
495 * many as possible). If the remaining types are both class types
496 * and subclasses, returns true, else false. Can also be called with
497 * two class types. */
498 int is_subclass_ptr_of(type *low, type *high) {
499 while (is_Pointer_type(low) && is_Pointer_type(high)) {
500 low = get_pointer_points_to_type(low);
501 high = get_pointer_points_to_type(high);
504 if (is_Class_type(low) && is_Class_type(high))
505 return is_subclass_of(low, high);
509 int is_superclass_of(type *high, type *low) {
510 return is_subclass_of(low, high);
513 int is_superclass_ptr_of(type *high, type *low) {
514 return is_subclass_ptr_of(low, high);
517 int is_overwritten_by(entity *high, entity *low) {
518 int i, n_overwrittenby;
519 assert(is_entity(low) && is_entity(high));
521 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
522 pset *m = get_entity_map(high, d_down);
523 return pset_find_ptr(m, low) ? 1 : 0;
526 /* depth first search from high downwards. */
527 n_overwrittenby = get_entity_n_overwrittenby(high);
528 for (i = 0; i < n_overwrittenby; i++) {
529 entity *ov = get_entity_overwrittenby(high, i);
530 if (low == ov) return 1;
531 if (is_overwritten_by(low, ov))
538 /** Need two routines because I want to assert the result. */
539 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) {
540 int i, n_overwrittenby;
543 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
545 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
546 for (i = 0; i < n_overwrittenby; ++i) {
547 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
555 /* Resolve polymorphy in the inheritance relation.
557 * Returns the dynamically referenced entity if the static entity and the
558 * dynamic type are given.
559 * Search downwards in overwritten tree. */
560 entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) {
562 assert(static_ent && is_entity(static_ent));
564 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
572 /* ----------------------------------------------------------------------- */
573 /* Class cast state handling. */
574 /* ----------------------------------------------------------------------- */
576 /* - State handling. ----------------------------------------- */
578 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
579 if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
580 irg->class_cast_state = s;
583 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
584 return irg->class_cast_state;
587 void set_irp_class_cast_state(ir_class_cast_state s) {
589 for (i = 0; i < get_irp_n_irgs(); ++i)
590 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
591 irp->class_cast_state = s;
594 ir_class_cast_state get_irp_class_cast_state(void) {
595 return irp->class_cast_state;
598 char *get_class_cast_state_string(ir_class_cast_state s) {
599 #define X(a) case a: return #a
601 X(ir_class_casts_any);
602 X(ir_class_casts_transitive);
603 X(ir_class_casts_normalized);
604 X(ir_class_casts_state_max);
605 default: return "invalid class cast state";
610 /* - State verification. ------------------------------------- */
612 typedef struct ccs_env {
613 ir_class_cast_state expected_state;
614 ir_class_cast_state worst_situation;
617 void verify_irn_class_cast_state(ir_node *n, void *env) {
618 ccs_env *ccs = (ccs_env *)env;
619 ir_class_cast_state this_state = ir_class_casts_any;
621 if (get_irn_op(n) != op_Cast) return;
623 type *fromtype = get_irn_typeinfo_type(get_Cast_op(n));
624 type *totype = get_Cast_type(n);
627 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
628 totype = get_pointer_points_to_type(totype);
629 fromtype = get_pointer_points_to_type(fromtype);
633 if (!is_Class_type(totype)) return;
635 if (is_subclass_of(totype, fromtype) ||
636 is_subclass_of(fromtype, totype) ) {
637 this_state = ir_class_casts_transitive;
638 if ((get_class_supertype_index(totype, fromtype) == -1) &&
639 (get_class_supertype_index(fromtype, totype) == -1) ) {
640 this_state = ir_class_casts_normalized;
644 assert(this_state >= ccs->expected_state &&
645 "invalid state class cast state setting in graph");
647 if (this_state < ccs->worst_situation)
648 ccs->worst_situation = this_state;
652 /** Verify that the graph meets reqirements of state set. */
653 void verify_irg_class_cast_state(ir_graph *irg) {
656 env.expected_state = get_irg_class_cast_state(irg);
657 env.worst_situation = ir_class_casts_normalized;
659 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
661 if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
662 printf("Note: class cast state is set lower than reqired in graph\n ");
664 printf(" state is %s, reqired is %s\n",
665 get_class_cast_state_string(env.expected_state),
666 get_class_cast_state_string(env.worst_situation));