2 * Copyright (C) 1995-2008 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
30 #include "irgraph_t.h"
38 DEBUG_ONLY(static firm_dbg_module_t *dbg);
40 /* ----------------------------------------------------------------------- */
41 /* Resolve implicit inheritance. */
42 /* ----------------------------------------------------------------------- */
44 ident *default_mangle_inherited_name(const ir_entity *super, const ir_type *clss) {
45 return id_mangle_u(new_id_from_str("inh"), id_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 const 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;
156 * Compare two tr_inh_trans_tp entries.
158 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
159 const tr_inh_trans_tp *ef1 = e1;
160 const tr_inh_trans_tp *ef2 = e2;
163 return ef1->kind != ef2->kind;
167 * calculate the hash value of an tr_inh_trans_tp
169 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v) {
170 return HASH_PTR(v->kind);
173 /* This always completes successfully. */
174 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k) {
175 tr_inh_trans_tp a, *found;
178 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
180 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
182 a.directions[d_up] = pset_new_ptr(16);
183 a.directions[d_down] = pset_new_ptr(16);
184 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
189 static pset *get_entity_map(const ir_entity *ent, dir d) {
190 tr_inh_trans_tp *found;
192 assert(is_entity(ent));
193 found = get_firm_kind_entry((const firm_kind *)ent);
194 return found->directions[d];
197 static pset *get_type_map(const ir_type *tp, dir d) {
198 tr_inh_trans_tp *found;
201 found = get_firm_kind_entry((const firm_kind *)tp);
202 return found->directions[d];
207 * Walk over all types reachable from tp in the sub/supertype
208 * relation and compute the closure for the two downwards directed
211 * The walk in the dag formed by the relation is tricky: We must visit
212 * all subtypes before visiting the supertypes. So we first walk down.
213 * Then we can compute the closure for this type. Then we walk up.
214 * As we call ourselves recursive, and walk in both directions, there
215 * can be cycles. So we have to make sure, that if we visit a node
216 * a second time (in a walk up) we do nothing. For this we increment
217 * the master visited flag twice.
218 * If the type is marked with master_flag_visited-1 it is on the stack.
219 * If it is marked with master_flag_visited it is fully processed.
221 * Well, we still miss some candidates ... */
222 static void compute_down_closure(ir_type *tp) {
223 pset *myset, *subset;
224 int i, n_subtypes, n_members, n_supertypes;
225 ir_visited_t master_visited = get_master_type_visited();
227 assert(is_Class_type(tp));
229 set_type_visited(tp, master_visited-1);
231 /* Recursive descend. */
232 n_subtypes = get_class_n_subtypes(tp);
233 for (i = 0; i < n_subtypes; ++i) {
234 ir_type *stp = get_class_subtype(tp, i);
235 if (get_type_visited(stp) < master_visited-1) {
236 compute_down_closure(stp);
241 myset = get_type_map(tp, d_down);
242 for (i = 0; i < n_subtypes; ++i) {
243 ir_type *stp = get_class_subtype(tp, i);
244 subset = get_type_map(stp, d_down);
245 pset_insert_ptr(myset, stp);
246 pset_insert_pset_ptr(myset, subset);
250 n_members = get_class_n_members(tp);
251 for (i = 0; i < n_members; ++i) {
252 ir_entity *mem = get_class_member(tp, i);
253 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
255 myset = get_entity_map(mem, d_down);
256 for (j = 0; j < n_overwrittenby; ++j) {
257 ir_entity *ov = get_entity_overwrittenby(mem, j);
258 subset = get_entity_map(ov, d_down);
259 pset_insert_ptr(myset, ov);
260 pset_insert_pset_ptr(myset, subset);
264 mark_type_visited(tp);
267 n_supertypes = get_class_n_supertypes(tp);
268 for (i = 0; i < n_supertypes; ++i) {
269 ir_type *stp = get_class_supertype(tp, i);
270 if (get_type_visited(stp) < master_visited-1) {
271 compute_down_closure(stp);
276 static void compute_up_closure(ir_type *tp) {
277 pset *myset, *subset;
278 int i, n_subtypes, n_members, n_supertypes;
279 ir_visited_t master_visited = get_master_type_visited();
281 assert(is_Class_type(tp));
283 set_type_visited(tp, master_visited-1);
285 /* Recursive descend. */
286 n_supertypes = get_class_n_supertypes(tp);
287 for (i = 0; i < n_supertypes; ++i) {
288 ir_type *stp = get_class_supertype(tp, i);
289 if (get_type_visited(stp) < get_master_type_visited()-1) {
290 compute_up_closure(stp);
295 myset = get_type_map(tp, d_up);
296 for (i = 0; i < n_supertypes; ++i) {
297 ir_type *stp = get_class_supertype(tp, i);
298 subset = get_type_map(stp, d_up);
299 pset_insert_ptr(myset, stp);
300 pset_insert_pset_ptr(myset, subset);
304 n_members = get_class_n_members(tp);
305 for (i = 0; i < n_members; ++i) {
306 ir_entity *mem = get_class_member(tp, i);
307 int j, n_overwrites = get_entity_n_overwrites(mem);
309 myset = get_entity_map(mem, d_up);
310 for (j = 0; j < n_overwrites; ++j) {
311 ir_entity *ov = get_entity_overwrites(mem, j);
312 subset = get_entity_map(ov, d_up);
313 pset_insert_pset_ptr(myset, subset);
314 pset_insert_ptr(myset, ov);
318 mark_type_visited(tp);
321 n_subtypes = get_class_n_subtypes(tp);
322 for (i = 0; i < n_subtypes; ++i) {
323 ir_type *stp = get_class_subtype(tp, i);
324 if (get_type_visited(stp) < master_visited-1) {
325 compute_up_closure(stp);
330 /** Compute the transitive closure of the subclass/superclass and
331 * overwrites/overwrittenby relation.
333 * This function walks over the ir (O(#types+#entities)) to compute the
334 * transitive closure. */
335 void compute_inh_transitive_closure(void) {
336 int i, n_types = get_irp_n_types();
337 free_inh_transitive_closure();
339 /* The 'down' relation */
340 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
341 inc_master_type_visited();
342 for (i = 0; i < n_types; ++i) {
343 ir_type *tp = get_irp_type(i);
344 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
345 int j, n_subtypes = get_class_n_subtypes(tp);
346 int has_unmarked_subtype = 0;
348 assert(get_type_visited(tp) < get_master_type_visited()-1);
349 for (j = 0; j < n_subtypes; ++j) {
350 ir_type *stp = get_class_subtype(tp, j);
351 if (type_not_visited(stp)) {
352 has_unmarked_subtype = 1;
357 /* This is a good starting point. */
358 if (!has_unmarked_subtype)
359 compute_down_closure(tp);
363 /* The 'up' relation */
364 inc_master_type_visited();
365 inc_master_type_visited();
366 for (i = 0; i < n_types; ++i) {
367 ir_type *tp = get_irp_type(i);
368 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
369 int j, n_supertypes = get_class_n_supertypes(tp);
370 int has_unmarked_supertype = 0;
372 assert(get_type_visited(tp) < get_master_type_visited()-1);
373 for (j = 0; j < n_supertypes; ++j) {
374 ir_type *stp = get_class_supertype(tp, j);
375 if (type_not_visited(stp)) {
376 has_unmarked_supertype = 1;
381 /* This is a good starting point. */
382 if (!has_unmarked_supertype)
383 compute_up_closure(tp);
387 irp->inh_trans_closure_state = inh_transitive_closure_valid;
390 /** Free memory occupied by the transitive closure information. */
391 void free_inh_transitive_closure(void) {
392 if (tr_inh_trans_set) {
393 tr_inh_trans_tp *elt;
394 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
395 del_pset(elt->directions[d_up]);
396 del_pset(elt->directions[d_down]);
398 del_set(tr_inh_trans_set);
399 tr_inh_trans_set = NULL;
401 irp->inh_trans_closure_state = inh_transitive_closure_none;
404 /* - subtype ------------------------------------------------------------- */
406 ir_type *get_class_trans_subtype_first(const ir_type *tp) {
407 assert_valid_state();
408 return pset_first(get_type_map(tp, d_down));
411 ir_type *get_class_trans_subtype_next(const ir_type *tp) {
412 assert_valid_state();
413 return pset_next(get_type_map(tp, d_down));
416 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp) {
417 assert_valid_state();
418 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
421 /* - supertype ----------------------------------------------------------- */
423 ir_type *get_class_trans_supertype_first(const ir_type *tp) {
424 assert_valid_state();
425 return pset_first(get_type_map(tp, d_up));
428 ir_type *get_class_trans_supertype_next(const ir_type *tp) {
429 assert_valid_state();
430 return pset_next(get_type_map(tp, d_up));
433 /* - overwrittenby ------------------------------------------------------- */
435 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent) {
436 assert_valid_state();
437 return pset_first(get_entity_map(ent, d_down));
440 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent) {
441 assert_valid_state();
442 return pset_next(get_entity_map(ent, d_down));
445 /* - overwrites ---------------------------------------------------------- */
448 /** Iterate over all transitive overwritten entities. */
449 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent) {
450 assert_valid_state();
451 return pset_first(get_entity_map(ent, d_up));
454 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent) {
455 assert_valid_state();
456 return pset_next(get_entity_map(ent, d_up));
460 /* ----------------------------------------------------------------------- */
461 /* Classify pairs of types/entities in the inheritance relations. */
462 /* ----------------------------------------------------------------------- */
464 /** Returns true if low is subclass of high. */
465 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
468 /* depth first search from high downwards. */
469 n_subtypes = get_class_n_subtypes(high);
470 for (i = 0; i < n_subtypes; i++) {
471 ir_type *stp = get_class_subtype(high, i);
472 if (low == stp) return 1;
473 if (is_SubClass_of(low, stp))
479 /* Returns true if low is subclass of high. */
480 int is_SubClass_of(ir_type *low, ir_type *high) {
481 assert(is_Class_type(low) && is_Class_type(high));
483 if (low == high) return 1;
485 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
486 pset *m = get_type_map(high, d_down);
487 return pset_find_ptr(m, low) ? 1 : 0;
489 return check_is_SubClass_of(low, high);
493 /* Subclass check for pointers to classes.
495 * Dereferences at both types the same amount of pointer types (as
496 * many as possible). If the remaining types are both class types
497 * and subclasses, returns true, else false. Can also be called with
498 * two class types. */
499 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
500 while (is_Pointer_type(low) && is_Pointer_type(high)) {
501 low = get_pointer_points_to_type(low);
502 high = get_pointer_points_to_type(high);
505 if (is_Class_type(low) && is_Class_type(high))
506 return is_SubClass_of(low, high);
510 int is_overwritten_by(ir_entity *high, ir_entity *low) {
511 int i, n_overwrittenby;
512 assert(is_entity(low) && is_entity(high));
514 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
515 pset *m = get_entity_map(high, d_down);
516 return pset_find_ptr(m, low) ? 1 : 0;
519 /* depth first search from high downwards. */
520 n_overwrittenby = get_entity_n_overwrittenby(high);
521 for (i = 0; i < n_overwrittenby; i++) {
522 ir_entity *ov = get_entity_overwrittenby(high, i);
523 if (low == ov) return 1;
524 if (is_overwritten_by(low, ov))
530 /** Resolve polymorphy in the inheritance relation.
532 * Returns the dynamically referenced entity if the static entity and the
533 * dynamic type are given.
534 * Search downwards in overwritten tree.
536 * Need two routines because I want to assert the result.
538 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
539 int i, n_overwrittenby;
541 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
543 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
544 for (i = 0; i < n_overwrittenby; ++i) {
545 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
546 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
552 /* Resolve polymorphy in the inheritance relation.
554 * Returns the dynamically referenced entity if the static entity and the
555 * dynamic type are given.
556 * Search downwards in overwritten tree.
558 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
560 assert(static_ent && is_entity(static_ent));
562 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
570 /* ----------------------------------------------------------------------- */
571 /* Class cast state handling. */
572 /* ----------------------------------------------------------------------- */
574 /* - State handling. ----------------------------------------- */
576 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
577 if (get_irp_class_cast_state() > s)
578 set_irp_class_cast_state(s);
579 irg->class_cast_state = s;
582 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg) {
583 return irg->class_cast_state;
586 void set_irp_class_cast_state(ir_class_cast_state s) {
589 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
590 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
592 irp->class_cast_state = s;
595 ir_class_cast_state get_irp_class_cast_state(void) {
596 return irp->class_cast_state;
599 const char *get_class_cast_state_string(ir_class_cast_state s) {
600 #define X(a) case a: return #a
602 X(ir_class_casts_any);
603 X(ir_class_casts_transitive);
604 X(ir_class_casts_normalized);
605 X(ir_class_casts_state_max);
606 default: return "invalid class cast state";
611 /* - State verification. ------------------------------------- */
613 typedef struct ccs_env {
614 ir_class_cast_state expected_state;
615 ir_class_cast_state worst_situation;
619 * Walker: check Casts.
621 static void verify_irn_class_cast_state(ir_node *n, void *env) {
622 ccs_env *ccs = (ccs_env *)env;
623 ir_class_cast_state this_state = ir_class_casts_any;
624 ir_type *fromtype, *totype;
627 if (!is_Cast(n)) return;
629 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
630 totype = get_Cast_type(n);
632 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
633 totype = get_pointer_points_to_type(totype);
634 fromtype = get_pointer_points_to_type(fromtype);
638 if (!is_Class_type(totype)) return;
640 if (is_SubClass_of(totype, fromtype) ||
641 is_SubClass_of(fromtype, totype) ) {
642 this_state = ir_class_casts_transitive;
643 if ((get_class_supertype_index(totype, fromtype) != -1) ||
644 (get_class_supertype_index(fromtype, totype) != -1) ||
645 fromtype == totype) {
646 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
647 if ((get_class_supertype_index(totype, fromtype) == -1) &&
648 (get_class_supertype_index(fromtype, totype) == -1) ) { */
649 this_state = ir_class_casts_normalized;
653 if (!(this_state >= ccs->expected_state)) {
654 ir_printf(" Node is %+F\n", n);
655 ir_printf(" totype %+F\n", totype);
656 ir_printf(" fromtype %+F\n", fromtype);
657 ir_printf(" this_state: %s, exp. state: %s\n",
658 get_class_cast_state_string(this_state),
659 get_class_cast_state_string(ccs->expected_state));
660 assert(this_state >= ccs->expected_state &&
661 "invalid state class cast state setting in graph");
664 if (this_state < ccs->worst_situation)
665 ccs->worst_situation = this_state;
668 /** Verify that the graph meets requirements of state set. */
669 void verify_irg_class_cast_state(ir_graph *irg) {
672 FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
674 env.expected_state = get_irg_class_cast_state(irg);
675 env.worst_situation = ir_class_casts_normalized;
677 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
679 if ((env.worst_situation > env.expected_state)) {
680 DB((dbg, LEVEL_1, "Note: class cast state is set lower than reqired "
681 "in graph \n\t%+F\n", irg));
682 DB((dbg, LEVEL_1, " state is %s, reqired is %s\n",
683 get_class_cast_state_string(env.expected_state),
684 get_class_cast_state_string(env.worst_situation)));