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)
46 return id_mangle_u(new_id_from_str("inh"), id_mangle_u(get_class_ident(clss), get_entity_ident(super)));
49 /** Replicates all entities in all super classes that are not overwritten
50 by an entity of this class. */
51 static void copy_entities_from_superclass(ir_type *clss, void *env)
55 ir_type *super, *inhenttype;
56 ir_entity *inhent, *thisent;
57 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
59 for(i = 0; i < get_class_n_supertypes(clss); i++) {
60 super = get_class_supertype(clss, i);
61 assert(is_Class_type(super) && "not a class");
62 for(j = 0; j < get_class_n_members(super); j++) {
63 inhent = get_class_member(super, j);
64 inhenttype = get_entity_type(inhent);
65 /* check whether inhent is already overwritten */
67 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
68 thisent = get_class_member(clss, k);
69 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
70 if(inhent == get_entity_overwrites(thisent, l)) {
71 /* overwritten - do not copy */
79 thisent = copy_entity_own(inhent, clss);
80 add_entity_overwrites(thisent, inhent);
81 if (get_entity_peculiarity(inhent) == peculiarity_existent)
82 set_entity_peculiarity(thisent, peculiarity_inherited);
83 set_entity_ld_ident(thisent, mfunc(inhent, clss));
84 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
85 assert(is_atomic_entity(inhent) && /* @@@ */
86 "Inheritance of constant, compound entities not implemented");
87 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
88 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
95 /* Resolve implicit inheritance.
97 * Resolves the implicit inheritance supplied by firm.
99 void resolve_inheritance(mangle_inherited_name_func *mfunc)
102 mfunc = default_mangle_inherited_name;
103 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
107 /* ----------------------------------------------------------------------- */
108 /* The transitive closure of the subclass/superclass and */
109 /* overwrites/overwrittenby relation. */
111 /* A walk over the ir (O(#types+#entities)) computes the transitive */
112 /* closure. Adding a new type/entity or changing the basic relations in */
113 /* some other way invalidates the transitive closure, i.e., it is not */
114 /* updated by the basic functions. */
116 /* All functions are named as their counterparts for the basic relations, */
117 /* adding the infix 'trans_'. */
118 /* ----------------------------------------------------------------------- */
120 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
122 irp->inh_trans_closure_state = s;
124 void invalidate_irp_inh_transitive_closure_state(void)
126 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
127 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
129 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
131 return irp->inh_trans_closure_state;
134 static void assert_valid_state(void)
136 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
137 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
140 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141 /* There is a set that extends each entity/type with two new */
142 /* fields: one for the upwards directed relation: 'up' (supertype, */
143 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
144 /* type, overwrittenby. These fields contain psets (and maybe later */
145 /* arrays) listing all subtypes... */
146 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154 const firm_kind *kind; /**< An entity or type. */
158 /* We use this set for all types and entities. */
159 static set *tr_inh_trans_set = NULL;
162 * Compare two tr_inh_trans_tp entries.
164 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
166 const tr_inh_trans_tp *ef1 = e1;
167 const tr_inh_trans_tp *ef2 = e2;
170 return ef1->kind != ef2->kind;
174 * calculate the hash value of an tr_inh_trans_tp
176 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
178 return HASH_PTR(v->kind);
181 /* This always completes successfully. */
182 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
184 tr_inh_trans_tp a, *found;
187 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
189 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
191 a.directions[d_up] = pset_new_ptr(16);
192 a.directions[d_down] = pset_new_ptr(16);
193 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
198 static pset *get_entity_map(const ir_entity *ent, dir d)
200 tr_inh_trans_tp *found;
202 assert(is_entity(ent));
203 found = get_firm_kind_entry((const firm_kind *)ent);
204 return found->directions[d];
207 static pset *get_type_map(const ir_type *tp, dir d)
209 tr_inh_trans_tp *found;
212 found = get_firm_kind_entry((const firm_kind *)tp);
213 return found->directions[d];
218 * Walk over all types reachable from tp in the sub/supertype
219 * relation and compute the closure for the two downwards directed
222 * The walk in the dag formed by the relation is tricky: We must visit
223 * all subtypes before visiting the supertypes. So we first walk down.
224 * Then we can compute the closure for this type. Then we walk up.
225 * As we call ourselves recursive, and walk in both directions, there
226 * can be cycles. So we have to make sure, that if we visit a node
227 * a second time (in a walk up) we do nothing. For this we increment
228 * the master visited flag twice.
229 * If the type is marked with master_flag_visited-1 it is on the stack.
230 * If it is marked with master_flag_visited it is fully processed.
232 * Well, we still miss some candidates ... */
233 static void compute_down_closure(ir_type *tp)
235 pset *myset, *subset;
236 int i, n_subtypes, n_members, n_supertypes;
237 ir_visited_t master_visited = get_master_type_visited();
239 assert(is_Class_type(tp));
241 set_type_visited(tp, master_visited-1);
243 /* Recursive descend. */
244 n_subtypes = get_class_n_subtypes(tp);
245 for (i = 0; i < n_subtypes; ++i) {
246 ir_type *stp = get_class_subtype(tp, i);
247 if (get_type_visited(stp) < master_visited-1) {
248 compute_down_closure(stp);
253 myset = get_type_map(tp, d_down);
254 for (i = 0; i < n_subtypes; ++i) {
255 ir_type *stp = get_class_subtype(tp, i);
256 subset = get_type_map(stp, d_down);
257 pset_insert_ptr(myset, stp);
258 pset_insert_pset_ptr(myset, subset);
262 n_members = get_class_n_members(tp);
263 for (i = 0; i < n_members; ++i) {
264 ir_entity *mem = get_class_member(tp, i);
265 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
267 myset = get_entity_map(mem, d_down);
268 for (j = 0; j < n_overwrittenby; ++j) {
269 ir_entity *ov = get_entity_overwrittenby(mem, j);
270 subset = get_entity_map(ov, d_down);
271 pset_insert_ptr(myset, ov);
272 pset_insert_pset_ptr(myset, subset);
276 mark_type_visited(tp);
279 n_supertypes = get_class_n_supertypes(tp);
280 for (i = 0; i < n_supertypes; ++i) {
281 ir_type *stp = get_class_supertype(tp, i);
282 if (get_type_visited(stp) < master_visited-1) {
283 compute_down_closure(stp);
288 static void compute_up_closure(ir_type *tp)
290 pset *myset, *subset;
291 int i, n_subtypes, n_members, n_supertypes;
292 ir_visited_t master_visited = get_master_type_visited();
294 assert(is_Class_type(tp));
296 set_type_visited(tp, master_visited-1);
298 /* Recursive descend. */
299 n_supertypes = get_class_n_supertypes(tp);
300 for (i = 0; i < n_supertypes; ++i) {
301 ir_type *stp = get_class_supertype(tp, i);
302 if (get_type_visited(stp) < get_master_type_visited()-1) {
303 compute_up_closure(stp);
308 myset = get_type_map(tp, d_up);
309 for (i = 0; i < n_supertypes; ++i) {
310 ir_type *stp = get_class_supertype(tp, i);
311 subset = get_type_map(stp, d_up);
312 pset_insert_ptr(myset, stp);
313 pset_insert_pset_ptr(myset, subset);
317 n_members = get_class_n_members(tp);
318 for (i = 0; i < n_members; ++i) {
319 ir_entity *mem = get_class_member(tp, i);
320 int j, n_overwrites = get_entity_n_overwrites(mem);
322 myset = get_entity_map(mem, d_up);
323 for (j = 0; j < n_overwrites; ++j) {
324 ir_entity *ov = get_entity_overwrites(mem, j);
325 subset = get_entity_map(ov, d_up);
326 pset_insert_pset_ptr(myset, subset);
327 pset_insert_ptr(myset, ov);
331 mark_type_visited(tp);
334 n_subtypes = get_class_n_subtypes(tp);
335 for (i = 0; i < n_subtypes; ++i) {
336 ir_type *stp = get_class_subtype(tp, i);
337 if (get_type_visited(stp) < master_visited-1) {
338 compute_up_closure(stp);
343 /** Compute the transitive closure of the subclass/superclass and
344 * overwrites/overwrittenby relation.
346 * This function walks over the ir (O(#types+#entities)) to compute the
347 * transitive closure. */
348 void compute_inh_transitive_closure(void)
350 int i, n_types = get_irp_n_types();
351 free_inh_transitive_closure();
353 /* The 'down' relation */
354 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
355 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
356 inc_master_type_visited();
357 for (i = 0; i < n_types; ++i) {
358 ir_type *tp = get_irp_type(i);
359 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
360 int j, n_subtypes = get_class_n_subtypes(tp);
361 int has_unmarked_subtype = 0;
363 assert(get_type_visited(tp) < get_master_type_visited()-1);
364 for (j = 0; j < n_subtypes; ++j) {
365 ir_type *stp = get_class_subtype(tp, j);
366 if (type_not_visited(stp)) {
367 has_unmarked_subtype = 1;
372 /* This is a good starting point. */
373 if (!has_unmarked_subtype)
374 compute_down_closure(tp);
378 /* The 'up' relation */
379 inc_master_type_visited();
380 inc_master_type_visited();
381 for (i = 0; i < n_types; ++i) {
382 ir_type *tp = get_irp_type(i);
383 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
384 int j, n_supertypes = get_class_n_supertypes(tp);
385 int has_unmarked_supertype = 0;
387 assert(get_type_visited(tp) < get_master_type_visited()-1);
388 for (j = 0; j < n_supertypes; ++j) {
389 ir_type *stp = get_class_supertype(tp, j);
390 if (type_not_visited(stp)) {
391 has_unmarked_supertype = 1;
396 /* This is a good starting point. */
397 if (!has_unmarked_supertype)
398 compute_up_closure(tp);
402 irp->inh_trans_closure_state = inh_transitive_closure_valid;
403 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
406 /** Free memory occupied by the transitive closure information. */
407 void free_inh_transitive_closure(void)
409 if (tr_inh_trans_set) {
410 tr_inh_trans_tp *elt;
411 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
412 del_pset(elt->directions[d_up]);
413 del_pset(elt->directions[d_down]);
415 del_set(tr_inh_trans_set);
416 tr_inh_trans_set = NULL;
418 irp->inh_trans_closure_state = inh_transitive_closure_none;
421 /* - subtype ------------------------------------------------------------- */
423 ir_type *get_class_trans_subtype_first(const ir_type *tp)
425 assert_valid_state();
426 return pset_first(get_type_map(tp, d_down));
429 ir_type *get_class_trans_subtype_next(const ir_type *tp)
431 assert_valid_state();
432 return pset_next(get_type_map(tp, d_down));
435 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
437 assert_valid_state();
438 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
441 /* - supertype ----------------------------------------------------------- */
443 ir_type *get_class_trans_supertype_first(const ir_type *tp)
445 assert_valid_state();
446 return pset_first(get_type_map(tp, d_up));
449 ir_type *get_class_trans_supertype_next(const ir_type *tp)
451 assert_valid_state();
452 return pset_next(get_type_map(tp, d_up));
455 /* - overwrittenby ------------------------------------------------------- */
457 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
459 assert_valid_state();
460 return pset_first(get_entity_map(ent, d_down));
463 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
465 assert_valid_state();
466 return pset_next(get_entity_map(ent, d_down));
469 /* - overwrites ---------------------------------------------------------- */
472 /** Iterate over all transitive overwritten entities. */
473 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
475 assert_valid_state();
476 return pset_first(get_entity_map(ent, d_up));
479 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
481 assert_valid_state();
482 return pset_next(get_entity_map(ent, d_up));
486 /* ----------------------------------------------------------------------- */
487 /* Classify pairs of types/entities in the inheritance relations. */
488 /* ----------------------------------------------------------------------- */
490 /** Returns true if low is subclass of high. */
491 static int check_is_SubClass_of(ir_type *low, ir_type *high)
495 /* depth first search from high downwards. */
496 n_subtypes = get_class_n_subtypes(high);
497 for (i = 0; i < n_subtypes; i++) {
498 ir_type *stp = get_class_subtype(high, i);
499 if (low == stp) return 1;
500 if (is_SubClass_of(low, stp))
506 /* Returns true if low is subclass of high. */
507 int is_SubClass_of(ir_type *low, ir_type *high)
509 assert(is_Class_type(low) && is_Class_type(high));
511 if (low == high) return 1;
513 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
514 pset *m = get_type_map(high, d_down);
515 return pset_find_ptr(m, low) ? 1 : 0;
517 return check_is_SubClass_of(low, high);
521 /* Subclass check for pointers to classes.
523 * Dereferences at both types the same amount of pointer types (as
524 * many as possible). If the remaining types are both class types
525 * and subclasses, returns true, else false. Can also be called with
526 * two class types. */
527 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
529 while (is_Pointer_type(low) && is_Pointer_type(high)) {
530 low = get_pointer_points_to_type(low);
531 high = get_pointer_points_to_type(high);
534 if (is_Class_type(low) && is_Class_type(high))
535 return is_SubClass_of(low, high);
539 int is_overwritten_by(ir_entity *high, ir_entity *low)
541 int i, n_overwrittenby;
542 assert(is_entity(low) && is_entity(high));
544 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
545 pset *m = get_entity_map(high, d_down);
546 return pset_find_ptr(m, low) ? 1 : 0;
549 /* depth first search from high downwards. */
550 n_overwrittenby = get_entity_n_overwrittenby(high);
551 for (i = 0; i < n_overwrittenby; i++) {
552 ir_entity *ov = get_entity_overwrittenby(high, i);
553 if (low == ov) return 1;
554 if (is_overwritten_by(low, ov))
560 /** Resolve polymorphy in the inheritance relation.
562 * Returns the dynamically referenced entity if the static entity and the
563 * dynamic type are given.
564 * Search downwards in overwritten tree.
566 * Need two routines because I want to assert the result.
568 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
570 int i, n_overwrittenby;
572 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
574 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
575 for (i = 0; i < n_overwrittenby; ++i) {
576 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
577 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
583 /* Resolve polymorphy in the inheritance relation.
585 * Returns the dynamically referenced entity if the static entity and the
586 * dynamic type are given.
587 * Search downwards in overwritten tree.
589 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
592 assert(static_ent && is_entity(static_ent));
594 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
602 /* ----------------------------------------------------------------------- */
603 /* Class cast state handling. */
604 /* ----------------------------------------------------------------------- */
606 /* - State handling. ----------------------------------------- */
608 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
610 if (get_irp_class_cast_state() > s)
611 set_irp_class_cast_state(s);
612 irg->class_cast_state = s;
615 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
617 return irg->class_cast_state;
620 void set_irp_class_cast_state(ir_class_cast_state s)
624 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
625 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
627 irp->class_cast_state = s;
630 ir_class_cast_state get_irp_class_cast_state(void)
632 return irp->class_cast_state;
635 const char *get_class_cast_state_string(ir_class_cast_state s)
637 #define X(a) case a: return #a
639 X(ir_class_casts_any);
640 X(ir_class_casts_transitive);
641 X(ir_class_casts_normalized);
642 X(ir_class_casts_state_max);
643 default: return "invalid class cast state";
648 /* - State verification. ------------------------------------- */
650 typedef struct ccs_env {
651 ir_class_cast_state expected_state;
652 ir_class_cast_state worst_situation;
656 * Walker: check Casts.
658 static void verify_irn_class_cast_state(ir_node *n, void *env)
660 ccs_env *ccs = (ccs_env *)env;
661 ir_class_cast_state this_state = ir_class_casts_any;
662 ir_type *fromtype, *totype;
665 if (!is_Cast(n)) return;
667 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
668 totype = get_Cast_type(n);
670 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
671 totype = get_pointer_points_to_type(totype);
672 fromtype = get_pointer_points_to_type(fromtype);
676 if (!is_Class_type(totype)) return;
678 if (is_SubClass_of(totype, fromtype) ||
679 is_SubClass_of(fromtype, totype) ) {
680 this_state = ir_class_casts_transitive;
681 if ((get_class_supertype_index(totype, fromtype) != -1) ||
682 (get_class_supertype_index(fromtype, totype) != -1) ||
683 fromtype == totype) {
684 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
685 if ((get_class_supertype_index(totype, fromtype) == -1) &&
686 (get_class_supertype_index(fromtype, totype) == -1) ) { */
687 this_state = ir_class_casts_normalized;
691 if (!(this_state >= ccs->expected_state)) {
692 ir_printf(" Node is %+F\n", n);
693 ir_printf(" totype %+F\n", totype);
694 ir_printf(" fromtype %+F\n", fromtype);
695 ir_printf(" this_state: %s, exp. state: %s\n",
696 get_class_cast_state_string(this_state),
697 get_class_cast_state_string(ccs->expected_state));
698 assert(this_state >= ccs->expected_state &&
699 "invalid state class cast state setting in graph");
702 if (this_state < ccs->worst_situation)
703 ccs->worst_situation = this_state;
706 /** Verify that the graph meets requirements of state set. */
707 void verify_irg_class_cast_state(ir_graph *irg)
711 FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
713 env.expected_state = get_irg_class_cast_state(irg);
714 env.worst_situation = ir_class_casts_normalized;
716 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
718 if ((env.worst_situation > env.expected_state)) {
719 DB((dbg, LEVEL_1, "Note: class cast state is set lower than reqired "
720 "in graph \n\t%+F\n", irg));
721 DB((dbg, LEVEL_1, " state is %s, reqired is %s\n",
722 get_class_cast_state_string(env.expected_state),
723 get_class_cast_state_string(env.worst_situation)));