2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * @file tr_inheritance.c
22 * @brief Utility routines for inheritance representation
23 * @author Goetz Lindenmaier
31 #include "irgraph_t.h"
40 /* ----------------------------------------------------------------------- */
41 /* Resolve implicit inheritance. */
42 /* ----------------------------------------------------------------------- */
44 ident *default_mangle_inherited_name(ir_entity *super, ir_type *clss) {
45 return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
48 /** Replicates all entities in all super classes that are not overwritten
49 by an entity of this class. */
50 static void copy_entities_from_superclass(ir_type *clss, void *env)
54 ir_type *super, *inhenttype;
55 ir_entity *inhent, *thisent;
56 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
58 for(i = 0; i < get_class_n_supertypes(clss); i++) {
59 super = get_class_supertype(clss, i);
60 assert(is_Class_type(super) && "not a class");
61 for(j = 0; j < get_class_n_members(super); j++) {
62 inhent = get_class_member(super, j);
63 inhenttype = get_entity_type(inhent);
64 /* check whether inhent is already overwritten */
66 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
67 thisent = get_class_member(clss, k);
68 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
69 if(inhent == get_entity_overwrites(thisent, l)) {
70 /* overwritten - do not copy */
78 thisent = copy_entity_own(inhent, clss);
79 add_entity_overwrites(thisent, inhent);
80 if (get_entity_peculiarity(inhent) == peculiarity_existent)
81 set_entity_peculiarity(thisent, peculiarity_inherited);
82 set_entity_ld_ident(thisent, mfunc(inhent, clss));
83 if (get_entity_variability(inhent) == variability_constant) {
84 assert(is_atomic_entity(inhent) && /* @@@ */
85 "Inheritance of constant, compound entities not implemented");
86 set_entity_variability(thisent, variability_constant);
87 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
94 /* Resolve implicit inheritance.
96 * Resolves the implicit inheritance supplied by firm.
98 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
100 mfunc = default_mangle_inherited_name;
101 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
105 /* ----------------------------------------------------------------------- */
106 /* The transitive closure of the subclass/superclass and */
107 /* overwrites/overwrittenby relation. */
109 /* A walk over the ir (O(#types+#entities)) computes the transitive */
110 /* closure. Adding a new type/entity or changing the basic relations in */
111 /* some other way invalidates the transitive closure, i.e., it is not */
112 /* updated by the basic functions. */
114 /* All functions are named as their counterparts for the basic relations, */
115 /* adding the infix 'trans_'. */
116 /* ----------------------------------------------------------------------- */
118 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
119 irp->inh_trans_closure_state = s;
121 void invalidate_irp_inh_transitive_closure_state(void) {
122 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
123 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
125 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
126 return irp->inh_trans_closure_state;
129 static void assert_valid_state(void) {
130 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
131 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
134 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135 /* There is a set that extends each entity/type with two new */
136 /* fields: one for the upwards directed relation: 'up' (supertype, */
137 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
138 /* type, overwrittenby. These fields contain psets (and maybe later */
139 /* arrays) listing all subtypes... */
140 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148 firm_kind *kind; /* An entity or type. */
152 /* We use this set for all types and entities. */
153 static set *tr_inh_trans_set = NULL;
155 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
156 tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
157 tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
158 return (ef1->kind != ef2->kind);
161 static INLINE unsigned int tr_inh_trans_hash(void *e) {
162 tr_inh_trans_tp *v = e;
163 return HASH_PTR(v->kind);
166 /* This always completes successfully. */
167 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
168 tr_inh_trans_tp a, *found;
171 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
173 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
175 a.directions[d_up] = pset_new_ptr(16);
176 a.directions[d_down] = pset_new_ptr(16);
177 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
182 static pset *get_entity_map(ir_entity *ent, dir d) {
183 tr_inh_trans_tp *found;
185 assert(is_entity(ent));
186 found = get_firm_kind_entry((firm_kind *)ent);
187 return found->directions[d];
190 static void add_entity_map(ir_entity *ent, dir d, ir_entity *new) {
191 tr_inh_trans_tp *found;
193 assert(is_entity(ent) && is_entity(new));
194 tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
195 pset_insert_ptr(found->directions[d], new);
198 static pset *get_type_map(ir_type *tp, dir d) {
199 tr_inh_trans_tp *found;
202 found = get_firm_kind_entry((firm_kind *)tp);
203 return found->directions[d];
206 static void add_type_map(ir_type *tp, dir d, type *new) {
207 tr_inh_trans_tp *found;
209 assert(is_type(tp) && is_type(new));
210 found = get_firm_kind_entry((firm_kind *)tp);
211 pset_insert_ptr(found->directions[d], new);
217 * Walk over all types reachable from tp in the sub/supertype
218 * relation and compute the closure for the two downwards directed
221 * The walk in the dag formed by the relation is tricky: We must visit
222 * all subtypes before visiting the supertypes. So we first walk down.
223 * Then we can compute the closure for this type. Then we walk up.
224 * As we call ourselves recursive, and walk in both directions, there
225 * can be cycles. So we have to make sure, that if we visit a node
226 * a second time (in a walk up) we do nothing. For this we increment
227 * the master visited flag twice.
228 * If the type is marked with master_flag_visited-1 it is on the stack.
229 * If it is marked with master_flag_visited it is fully processed.
231 * Well, we still miss some candidates ... */
232 static void compute_down_closure(ir_type *tp) {
233 pset *myset, *subset;
234 int i, n_subtypes, n_members, n_supertypes;
235 unsigned long master_visited = get_master_type_visited();
237 assert(is_Class_type(tp));
239 set_type_visited(tp, master_visited-1);
241 /* Recursive descend. */
242 n_subtypes = get_class_n_subtypes(tp);
243 for (i = 0; i < n_subtypes; ++i) {
244 ir_type *stp = get_class_subtype(tp, i);
245 if (get_type_visited(stp) < master_visited-1) {
246 compute_down_closure(stp);
251 myset = get_type_map(tp, d_down);
252 for (i = 0; i < n_subtypes; ++i) {
253 ir_type *stp = get_class_subtype(tp, i);
254 subset = get_type_map(stp, d_down);
255 pset_insert_ptr(myset, stp);
256 pset_insert_pset_ptr(myset, subset);
260 n_members = get_class_n_members(tp);
261 for (i = 0; i < n_members; ++i) {
262 ir_entity *mem = get_class_member(tp, i);
263 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
265 myset = get_entity_map(mem, d_down);
266 for (j = 0; j < n_overwrittenby; ++j) {
267 ir_entity *ov = get_entity_overwrittenby(mem, j);
268 subset = get_entity_map(ov, d_down);
269 pset_insert_ptr(myset, ov);
270 pset_insert_pset_ptr(myset, subset);
274 mark_type_visited(tp);
277 n_supertypes = get_class_n_supertypes(tp);
278 for (i = 0; i < n_supertypes; ++i) {
279 ir_type *stp = get_class_supertype(tp, i);
280 if (get_type_visited(stp) < master_visited-1) {
281 compute_down_closure(stp);
286 static void compute_up_closure(ir_type *tp) {
287 pset *myset, *subset;
288 int i, n_subtypes, n_members, n_supertypes;
289 unsigned long master_visited = get_master_type_visited();
291 assert(is_Class_type(tp));
293 set_type_visited(tp, master_visited-1);
295 /* Recursive descend. */
296 n_supertypes = get_class_n_supertypes(tp);
297 for (i = 0; i < n_supertypes; ++i) {
298 ir_type *stp = get_class_supertype(tp, i);
299 if (get_type_visited(stp) < get_master_type_visited()-1) {
300 compute_up_closure(stp);
305 myset = get_type_map(tp, d_up);
306 for (i = 0; i < n_supertypes; ++i) {
307 ir_type *stp = get_class_supertype(tp, i);
308 subset = get_type_map(stp, d_up);
309 pset_insert_ptr(myset, stp);
310 pset_insert_pset_ptr(myset, subset);
314 n_members = get_class_n_members(tp);
315 for (i = 0; i < n_members; ++i) {
316 ir_entity *mem = get_class_member(tp, i);
317 int j, n_overwrites = get_entity_n_overwrites(mem);
319 myset = get_entity_map(mem, d_up);
320 for (j = 0; j < n_overwrites; ++j) {
321 ir_entity *ov = get_entity_overwrites(mem, j);
322 subset = get_entity_map(ov, d_up);
323 pset_insert_pset_ptr(myset, subset);
324 pset_insert_ptr(myset, ov);
328 mark_type_visited(tp);
331 n_subtypes = get_class_n_subtypes(tp);
332 for (i = 0; i < n_subtypes; ++i) {
333 ir_type *stp = get_class_subtype(tp, i);
334 if (get_type_visited(stp) < master_visited-1) {
335 compute_up_closure(stp);
340 /** Compute the transitive closure of the subclass/superclass and
341 * overwrites/overwrittenby relation.
343 * This function walks over the ir (O(#types+#entities)) to compute the
344 * transitive closure. */
345 void compute_inh_transitive_closure(void) {
346 int i, n_types = get_irp_n_types();
347 free_inh_transitive_closure();
349 /* The 'down' relation */
350 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
351 inc_master_type_visited();
352 for (i = 0; i < n_types; ++i) {
353 ir_type *tp = get_irp_type(i);
354 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
355 int j, n_subtypes = get_class_n_subtypes(tp);
356 int has_unmarked_subtype = 0;
358 assert(get_type_visited(tp) < get_master_type_visited()-1);
359 for (j = 0; j < n_subtypes; ++j) {
360 ir_type *stp = get_class_subtype(tp, j);
361 if (type_not_visited(stp)) {
362 has_unmarked_subtype = 1;
367 /* This is a good starting point. */
368 if (!has_unmarked_subtype)
369 compute_down_closure(tp);
373 /* The 'up' relation */
374 inc_master_type_visited();
375 inc_master_type_visited();
376 for (i = 0; i < n_types; ++i) {
377 ir_type *tp = get_irp_type(i);
378 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
379 int j, n_supertypes = get_class_n_supertypes(tp);
380 int has_unmarked_supertype = 0;
382 assert(get_type_visited(tp) < get_master_type_visited()-1);
383 for (j = 0; j < n_supertypes; ++j) {
384 ir_type *stp = get_class_supertype(tp, j);
385 if (type_not_visited(stp)) {
386 has_unmarked_supertype = 1;
391 /* This is a good starting point. */
392 if (!has_unmarked_supertype)
393 compute_up_closure(tp);
397 irp->inh_trans_closure_state = inh_transitive_closure_valid;
400 /** Free memory occupied by the transitive closure information. */
401 void free_inh_transitive_closure(void) {
402 if (tr_inh_trans_set) {
403 tr_inh_trans_tp *elt;
404 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
405 del_pset(elt->directions[d_up]);
406 del_pset(elt->directions[d_down]);
408 del_set(tr_inh_trans_set);
409 tr_inh_trans_set = NULL;
411 irp->inh_trans_closure_state = inh_transitive_closure_none;
414 /* - subtype ------------------------------------------------------------- */
416 ir_type *get_class_trans_subtype_first(ir_type *tp) {
417 assert_valid_state();
418 return pset_first(get_type_map(tp, d_down));
421 ir_type *get_class_trans_subtype_next (ir_type *tp) {
422 assert_valid_state();
423 return pset_next(get_type_map(tp, d_down));
426 int is_class_trans_subtype (ir_type *tp, ir_type *subtp) {
427 assert_valid_state();
428 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
431 /* - supertype ----------------------------------------------------------- */
433 ir_type *get_class_trans_supertype_first(ir_type *tp) {
434 assert_valid_state();
435 return pset_first(get_type_map(tp, d_up));
438 ir_type *get_class_trans_supertype_next (ir_type *tp) {
439 assert_valid_state();
440 return pset_next(get_type_map(tp, d_up));
443 /* - overwrittenby ------------------------------------------------------- */
445 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent) {
446 assert_valid_state();
447 return pset_first(get_entity_map(ent, d_down));
450 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent) {
451 assert_valid_state();
452 return pset_next(get_entity_map(ent, d_down));
455 /* - overwrites ---------------------------------------------------------- */
458 /** Iterate over all transitive overwritten entities. */
459 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent) {
460 assert_valid_state();
461 return pset_first(get_entity_map(ent, d_up));
464 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent) {
465 assert_valid_state();
466 return pset_next(get_entity_map(ent, d_up));
473 /* ----------------------------------------------------------------------- */
474 /* Classify pairs of types/entities in the inheritance relations. */
475 /* ----------------------------------------------------------------------- */
477 /** Returns true if low is subclass of high. */
478 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
481 /* depth first search from high downwards. */
482 n_subtypes = get_class_n_subtypes(high);
483 for (i = 0; i < n_subtypes; i++) {
484 ir_type *stp = get_class_subtype(high, i);
485 if (low == stp) return 1;
486 if (is_SubClass_of(low, stp))
492 /* Returns true if low is subclass of high. */
493 int is_SubClass_of(ir_type *low, ir_type *high) {
494 assert(is_Class_type(low) && is_Class_type(high));
496 if (low == high) return 1;
498 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
499 pset *m = get_type_map(high, d_down);
500 return pset_find_ptr(m, low) ? 1 : 0;
502 return check_is_SubClass_of(low, high);
506 /* Subclass check for pointers to classes.
508 * Dereferences at both types the same amount of pointer types (as
509 * many as possible). If the remaining types are both class types
510 * and subclasses, returns true, else false. Can also be called with
511 * two class types. */
512 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
513 while (is_Pointer_type(low) && is_Pointer_type(high)) {
514 low = get_pointer_points_to_type(low);
515 high = get_pointer_points_to_type(high);
518 if (is_Class_type(low) && is_Class_type(high))
519 return is_SubClass_of(low, high);
523 int is_overwritten_by(ir_entity *high, ir_entity *low) {
524 int i, n_overwrittenby;
525 assert(is_entity(low) && is_entity(high));
527 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
528 pset *m = get_entity_map(high, d_down);
529 return pset_find_ptr(m, low) ? 1 : 0;
532 /* depth first search from high downwards. */
533 n_overwrittenby = get_entity_n_overwrittenby(high);
534 for (i = 0; i < n_overwrittenby; i++) {
535 ir_entity *ov = get_entity_overwrittenby(high, i);
536 if (low == ov) return 1;
537 if (is_overwritten_by(low, ov))
543 /** Resolve polymorphy in the inheritance relation.
545 * Returns the dynamically referenced entity if the static entity and the
546 * dynamic type are given.
547 * Search downwards in overwritten tree.
549 * Need two routines because I want to assert the result.
551 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
552 int i, n_overwrittenby;
554 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
556 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
557 for (i = 0; i < n_overwrittenby; ++i) {
558 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
559 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
565 /* Resolve polymorphy in the inheritance relation.
567 * Returns the dynamically referenced entity if the static entity and the
568 * dynamic type are given.
569 * Search downwards in overwritten tree. */
570 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
572 assert(static_ent && is_entity(static_ent));
574 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
582 /* ----------------------------------------------------------------------- */
583 /* Class cast state handling. */
584 /* ----------------------------------------------------------------------- */
586 /* - State handling. ----------------------------------------- */
588 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
589 if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
590 irg->class_cast_state = s;
593 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
594 return irg->class_cast_state;
597 void set_irp_class_cast_state(ir_class_cast_state s) {
599 for (i = 0; i < get_irp_n_irgs(); ++i)
600 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
601 irp->class_cast_state = s;
604 ir_class_cast_state get_irp_class_cast_state(void) {
605 return irp->class_cast_state;
608 char *get_class_cast_state_string(ir_class_cast_state s) {
609 #define X(a) case a: return #a
611 X(ir_class_casts_any);
612 X(ir_class_casts_transitive);
613 X(ir_class_casts_normalized);
614 X(ir_class_casts_state_max);
615 default: return "invalid class cast state";
620 /* - State verification. ------------------------------------- */
622 typedef struct ccs_env {
623 ir_class_cast_state expected_state;
624 ir_class_cast_state worst_situation;
627 void verify_irn_class_cast_state(ir_node *n, void *env) {
628 ccs_env *ccs = (ccs_env *)env;
629 ir_class_cast_state this_state = ir_class_casts_any;
630 ir_type *fromtype, *totype;
633 if (get_irn_op(n) != op_Cast) return;
635 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
636 totype = get_Cast_type(n);
638 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
639 totype = get_pointer_points_to_type(totype);
640 fromtype = get_pointer_points_to_type(fromtype);
644 if (!is_Class_type(totype)) return;
646 if (is_SubClass_of(totype, fromtype) ||
647 is_SubClass_of(fromtype, totype) ) {
648 this_state = ir_class_casts_transitive;
649 if ((get_class_supertype_index(totype, fromtype) != -1) ||
650 (get_class_supertype_index(fromtype, totype) != -1) ||
651 fromtype == totype) {
652 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
653 if ((get_class_supertype_index(totype, fromtype) == -1) &&
654 (get_class_supertype_index(fromtype, totype) == -1) ) { */
655 this_state = ir_class_casts_normalized;
659 if (!(this_state >= ccs->expected_state)) {
660 ir_printf(" Node is %+F\n", n);
661 ir_printf(" totype %+F\n", totype);
662 ir_printf(" fromtype %+F\n", fromtype);
663 ir_printf(" this_state: %s, exp. state: %s\n",
664 get_class_cast_state_string(this_state),
665 get_class_cast_state_string(ccs->expected_state));
666 assert(this_state >= ccs->expected_state &&
667 "invalid state class cast state setting in graph");
670 if (this_state < ccs->worst_situation)
671 ccs->worst_situation = this_state;
675 /** Verify that the graph meets requirements of state set. */
676 void verify_irg_class_cast_state(ir_graph *irg) {
679 env.expected_state = get_irg_class_cast_state(irg);
680 env.worst_situation = ir_class_casts_normalized;
682 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
684 if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
685 ir_printf("Note: class cast state is set lower than reqired in graph \n\t%+F\n", irg);
686 printf(" state is %s, reqired is %s\n",
687 get_class_cast_state_string(env.expected_state),
688 get_class_cast_state_string(env.worst_situation));