2 * Copyright (C) 1995-2011 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)
58 ir_type *super, *inhenttype;
59 ir_entity *inhent, *thisent;
60 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
62 for (i = 0; i < get_class_n_supertypes(clss); i++) {
63 super = get_class_supertype(clss, i);
64 assert(is_Class_type(super) && "not a class");
65 for (j = 0; j < get_class_n_members(super); j++) {
66 inhent = get_class_member(super, j);
67 inhenttype = get_entity_type(inhent);
68 /* check whether inhent is already overwritten */
70 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
71 thisent = get_class_member(clss, k);
72 for (l = 0; l < get_entity_n_overwrites(thisent); l++) {
73 if (inhent == get_entity_overwrites(thisent, l)) {
74 /* overwritten - do not copy */
82 thisent = copy_entity_own(inhent, clss);
83 add_entity_overwrites(thisent, inhent);
84 if (get_entity_peculiarity(inhent) == peculiarity_existent)
85 set_entity_peculiarity(thisent, peculiarity_inherited);
86 set_entity_ld_ident(thisent, mfunc(inhent, clss));
87 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
88 assert(is_atomic_entity(inhent) && /* @@@ */
89 "Inheritance of constant, compound entities not implemented");
90 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
91 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
98 /* Resolve implicit inheritance.
100 * Resolves the implicit inheritance supplied by firm.
102 void resolve_inheritance(mangle_inherited_name_func *mfunc)
105 mfunc = default_mangle_inherited_name;
106 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
110 /* ----------------------------------------------------------------------- */
111 /* The transitive closure of the subclass/superclass and */
112 /* overwrites/overwrittenby relation. */
114 /* A walk over the ir (O(#types+#entities)) computes the transitive */
115 /* closure. Adding a new type/entity or changing the basic relations in */
116 /* some other way invalidates the transitive closure, i.e., it is not */
117 /* updated by the basic functions. */
119 /* All functions are named as their counterparts for the basic relations, */
120 /* adding the infix 'trans_'. */
121 /* ----------------------------------------------------------------------- */
123 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
125 irp->inh_trans_closure_state = s;
127 void invalidate_irp_inh_transitive_closure_state(void)
129 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
130 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
132 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
134 return irp->inh_trans_closure_state;
137 static void assert_valid_state(void)
139 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
140 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
143 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144 /* There is a set that extends each entity/type with two new */
145 /* fields: one for the upwards directed relation: 'up' (supertype, */
146 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
147 /* type, overwrittenby. These fields contain psets (and maybe later */
148 /* arrays) listing all subtypes... */
149 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
157 const firm_kind *kind; /**< An entity or type. */
161 /* We use this set for all types and entities. */
162 static set *tr_inh_trans_set = NULL;
165 * Compare two tr_inh_trans_tp entries.
167 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
169 const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
170 const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
173 return ef1->kind != ef2->kind;
177 * calculate the hash value of an tr_inh_trans_tp
179 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
181 return HASH_PTR(v->kind);
184 /* This always completes successfully. */
185 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
187 tr_inh_trans_tp a, *found;
190 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
192 found = (tr_inh_trans_tp*)set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
194 a.directions[d_up] = pset_new_ptr(16);
195 a.directions[d_down] = pset_new_ptr(16);
196 found = (tr_inh_trans_tp*)set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
201 static pset *get_entity_map(const ir_entity *ent, dir d)
203 tr_inh_trans_tp *found;
205 assert(is_entity(ent));
206 found = get_firm_kind_entry((const firm_kind *)ent);
207 return found->directions[d];
210 static pset *get_type_map(const ir_type *tp, dir d)
212 tr_inh_trans_tp *found;
215 found = get_firm_kind_entry((const firm_kind *)tp);
216 return found->directions[d];
221 * Walk over all types reachable from tp in the sub/supertype
222 * relation and compute the closure for the two downwards directed
225 * The walk in the dag formed by the relation is tricky: We must visit
226 * all subtypes before visiting the supertypes. So we first walk down.
227 * Then we can compute the closure for this type. Then we walk up.
228 * As we call ourselves recursive, and walk in both directions, there
229 * can be cycles. So we have to make sure, that if we visit a node
230 * a second time (in a walk up) we do nothing. For this we increment
231 * the master visited flag twice.
232 * If the type is marked with master_flag_visited-1 it is on the stack.
233 * If it is marked with master_flag_visited it is fully processed.
235 * Well, we still miss some candidates ... */
236 static void compute_down_closure(ir_type *tp)
238 pset *myset, *subset;
239 size_t i, n_subtypes, n_members, n_supertypes;
240 ir_visited_t master_visited = get_master_type_visited();
242 assert(is_Class_type(tp));
244 set_type_visited(tp, master_visited-1);
246 /* Recursive descend. */
247 n_subtypes = get_class_n_subtypes(tp);
248 for (i = 0; i < n_subtypes; ++i) {
249 ir_type *stp = get_class_subtype(tp, i);
250 if (get_type_visited(stp) < master_visited-1) {
251 compute_down_closure(stp);
256 myset = get_type_map(tp, d_down);
257 for (i = 0; i < n_subtypes; ++i) {
258 ir_type *stp = get_class_subtype(tp, i);
259 subset = get_type_map(stp, d_down);
260 pset_insert_ptr(myset, stp);
261 pset_insert_pset_ptr(myset, subset);
265 n_members = get_class_n_members(tp);
266 for (i = 0; i < n_members; ++i) {
267 ir_entity *mem = get_class_member(tp, i);
268 size_t j, n_overwrittenby = get_entity_n_overwrittenby(mem);
270 myset = get_entity_map(mem, d_down);
271 for (j = 0; j < n_overwrittenby; ++j) {
272 ir_entity *ov = get_entity_overwrittenby(mem, j);
273 subset = get_entity_map(ov, d_down);
274 pset_insert_ptr(myset, ov);
275 pset_insert_pset_ptr(myset, subset);
279 mark_type_visited(tp);
282 n_supertypes = get_class_n_supertypes(tp);
283 for (i = 0; i < n_supertypes; ++i) {
284 ir_type *stp = get_class_supertype(tp, i);
285 if (get_type_visited(stp) < master_visited-1) {
286 compute_down_closure(stp);
291 static void compute_up_closure(ir_type *tp)
293 pset *myset, *subset;
294 size_t i, n_subtypes, n_members, n_supertypes;
295 ir_visited_t master_visited = get_master_type_visited();
297 assert(is_Class_type(tp));
299 set_type_visited(tp, master_visited-1);
301 /* Recursive descend. */
302 n_supertypes = get_class_n_supertypes(tp);
303 for (i = 0; i < n_supertypes; ++i) {
304 ir_type *stp = get_class_supertype(tp, i);
305 if (get_type_visited(stp) < get_master_type_visited()-1) {
306 compute_up_closure(stp);
311 myset = get_type_map(tp, d_up);
312 for (i = 0; i < n_supertypes; ++i) {
313 ir_type *stp = get_class_supertype(tp, i);
314 subset = get_type_map(stp, d_up);
315 pset_insert_ptr(myset, stp);
316 pset_insert_pset_ptr(myset, subset);
320 n_members = get_class_n_members(tp);
321 for (i = 0; i < n_members; ++i) {
322 ir_entity *mem = get_class_member(tp, i);
323 size_t j, n_overwrites = get_entity_n_overwrites(mem);
325 myset = get_entity_map(mem, d_up);
326 for (j = 0; j < n_overwrites; ++j) {
327 ir_entity *ov = get_entity_overwrites(mem, j);
328 subset = get_entity_map(ov, d_up);
329 pset_insert_pset_ptr(myset, subset);
330 pset_insert_ptr(myset, ov);
334 mark_type_visited(tp);
337 n_subtypes = get_class_n_subtypes(tp);
338 for (i = 0; i < n_subtypes; ++i) {
339 ir_type *stp = get_class_subtype(tp, i);
340 if (get_type_visited(stp) < master_visited-1) {
341 compute_up_closure(stp);
346 /** Compute the transitive closure of the subclass/superclass and
347 * overwrites/overwrittenby relation.
349 * This function walks over the ir (O(#types+#entities)) to compute the
350 * transitive closure. */
351 void compute_inh_transitive_closure(void)
353 size_t i, n_types = get_irp_n_types();
354 free_inh_transitive_closure();
356 /* The 'down' relation */
357 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
358 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
359 inc_master_type_visited();
360 for (i = 0; i < n_types; ++i) {
361 ir_type *tp = get_irp_type(i);
362 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
363 size_t j, n_subtypes = get_class_n_subtypes(tp);
364 int has_unmarked_subtype = 0;
366 assert(get_type_visited(tp) < get_master_type_visited()-1);
367 for (j = 0; j < n_subtypes; ++j) {
368 ir_type *stp = get_class_subtype(tp, j);
369 if (type_not_visited(stp)) {
370 has_unmarked_subtype = 1;
375 /* This is a good starting point. */
376 if (!has_unmarked_subtype)
377 compute_down_closure(tp);
381 /* The 'up' relation */
382 inc_master_type_visited();
383 inc_master_type_visited();
384 for (i = 0; i < n_types; ++i) {
385 ir_type *tp = get_irp_type(i);
386 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
387 size_t j, n_supertypes = get_class_n_supertypes(tp);
388 int has_unmarked_supertype = 0;
390 assert(get_type_visited(tp) < get_master_type_visited()-1);
391 for (j = 0; j < n_supertypes; ++j) {
392 ir_type *stp = get_class_supertype(tp, j);
393 if (type_not_visited(stp)) {
394 has_unmarked_supertype = 1;
399 /* This is a good starting point. */
400 if (!has_unmarked_supertype)
401 compute_up_closure(tp);
405 irp->inh_trans_closure_state = inh_transitive_closure_valid;
406 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
409 /** Free memory occupied by the transitive closure information. */
410 void free_inh_transitive_closure(void)
412 if (tr_inh_trans_set) {
413 tr_inh_trans_tp *elt;
414 for (elt = (tr_inh_trans_tp*)set_first(tr_inh_trans_set); elt != NULL;
415 elt = (tr_inh_trans_tp*)set_next(tr_inh_trans_set)) {
416 del_pset(elt->directions[d_up]);
417 del_pset(elt->directions[d_down]);
419 del_set(tr_inh_trans_set);
420 tr_inh_trans_set = NULL;
422 irp->inh_trans_closure_state = inh_transitive_closure_none;
425 /* - subtype ------------------------------------------------------------- */
427 ir_type *get_class_trans_subtype_first(const ir_type *tp)
429 assert_valid_state();
430 return (ir_type*)pset_first(get_type_map(tp, d_down));
433 ir_type *get_class_trans_subtype_next(const ir_type *tp)
435 assert_valid_state();
436 return (ir_type*)pset_next(get_type_map(tp, d_down));
439 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
441 assert_valid_state();
442 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
445 /* - supertype ----------------------------------------------------------- */
447 ir_type *get_class_trans_supertype_first(const ir_type *tp)
449 assert_valid_state();
450 return (ir_type*)pset_first(get_type_map(tp, d_up));
453 ir_type *get_class_trans_supertype_next(const ir_type *tp)
455 assert_valid_state();
456 return (ir_type*)pset_next(get_type_map(tp, d_up));
459 /* - overwrittenby ------------------------------------------------------- */
461 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
463 assert_valid_state();
464 return (ir_entity*)pset_first(get_entity_map(ent, d_down));
467 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
469 assert_valid_state();
470 return (ir_entity*)pset_next(get_entity_map(ent, d_down));
473 /* - overwrites ---------------------------------------------------------- */
476 /** Iterate over all transitive overwritten entities. */
477 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
479 assert_valid_state();
480 return (ir_entity*)pset_first(get_entity_map(ent, d_up));
483 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
485 assert_valid_state();
486 return (ir_entity*)pset_next(get_entity_map(ent, d_up));
490 /* ----------------------------------------------------------------------- */
491 /* Classify pairs of types/entities in the inheritance relations. */
492 /* ----------------------------------------------------------------------- */
494 /** Returns true if low is subclass of high. */
495 static int check_is_SubClass_of(ir_type *low, ir_type *high)
497 size_t i, n_subtypes;
499 /* depth first search from high downwards. */
500 n_subtypes = get_class_n_subtypes(high);
501 for (i = 0; i < n_subtypes; i++) {
502 ir_type *stp = get_class_subtype(high, i);
503 if (low == stp) return 1;
504 if (is_SubClass_of(low, stp))
510 /* Returns true if low is subclass of high. */
511 int is_SubClass_of(ir_type *low, ir_type *high)
513 assert(is_Class_type(low) && is_Class_type(high));
515 if (low == high) return 1;
517 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
518 pset *m = get_type_map(high, d_down);
519 return pset_find_ptr(m, low) ? 1 : 0;
521 return check_is_SubClass_of(low, high);
525 /* Subclass check for pointers to classes.
527 * Dereferences at both types the same amount of pointer types (as
528 * many as possible). If the remaining types are both class types
529 * and subclasses, returns true, else false. Can also be called with
530 * two class types. */
531 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
533 while (is_Pointer_type(low) && is_Pointer_type(high)) {
534 low = get_pointer_points_to_type(low);
535 high = get_pointer_points_to_type(high);
538 if (is_Class_type(low) && is_Class_type(high))
539 return is_SubClass_of(low, high);
543 int is_overwritten_by(ir_entity *high, ir_entity *low)
545 size_t i, n_overwrittenby;
546 assert(is_entity(low) && is_entity(high));
548 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
549 pset *m = get_entity_map(high, d_down);
550 return pset_find_ptr(m, low) ? 1 : 0;
553 /* depth first search from high downwards. */
554 n_overwrittenby = get_entity_n_overwrittenby(high);
555 for (i = 0; i < n_overwrittenby; i++) {
556 ir_entity *ov = get_entity_overwrittenby(high, i);
557 if (low == ov) return 1;
558 if (is_overwritten_by(low, ov))
564 /** Resolve polymorphy in the inheritance relation.
566 * Returns the dynamically referenced entity if the static entity and the
567 * dynamic type are given.
568 * Search downwards in overwritten tree.
570 * Need two routines because I want to assert the result.
572 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
574 size_t i, n_overwrittenby;
576 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
578 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
579 for (i = 0; i < n_overwrittenby; ++i) {
580 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
581 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
587 /* Resolve polymorphy in the inheritance relation.
589 * Returns the dynamically referenced entity if the static entity and the
590 * dynamic type are given.
591 * Search downwards in overwritten tree.
593 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
596 assert(static_ent && is_entity(static_ent));
598 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
606 /* ----------------------------------------------------------------------- */
607 /* Class cast state handling. */
608 /* ----------------------------------------------------------------------- */
610 /* - State handling. ----------------------------------------- */
612 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
614 if (get_irp_class_cast_state() > s)
615 set_irp_class_cast_state(s);
616 irg->class_cast_state = s;
619 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
621 return irg->class_cast_state;
624 void set_irp_class_cast_state(ir_class_cast_state s)
628 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
629 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
631 irp->class_cast_state = s;
634 ir_class_cast_state get_irp_class_cast_state(void)
636 return irp->class_cast_state;
639 const char *get_class_cast_state_string(ir_class_cast_state s)
641 #define X(a) case a: return #a
643 X(ir_class_casts_any);
644 X(ir_class_casts_transitive);
645 X(ir_class_casts_normalized);
646 X(ir_class_casts_state_max);
647 default: return "invalid class cast state";
652 /* - State verification. ------------------------------------- */
654 typedef struct ccs_env {
655 ir_class_cast_state expected_state;
656 ir_class_cast_state worst_situation;
660 * Walker: check Casts.
662 static void verify_irn_class_cast_state(ir_node *n, void *env)
664 ccs_env *ccs = (ccs_env *)env;
665 ir_class_cast_state this_state = ir_class_casts_any;
666 ir_type *fromtype, *totype;
669 if (!is_Cast(n)) return;
671 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
672 totype = get_Cast_type(n);
674 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
675 totype = get_pointer_points_to_type(totype);
676 fromtype = get_pointer_points_to_type(fromtype);
680 if (!is_Class_type(totype)) return;
682 if (is_SubClass_of(totype, fromtype) ||
683 is_SubClass_of(fromtype, totype)) {
684 this_state = ir_class_casts_transitive;
685 if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
686 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ||
687 fromtype == totype) {
688 this_state = ir_class_casts_normalized;
692 if (!(this_state >= ccs->expected_state)) {
693 ir_printf(" Node is %+F\n", n);
694 ir_printf(" totype %+F\n", totype);
695 ir_printf(" fromtype %+F\n", fromtype);
696 ir_printf(" this_state: %s, exp. state: %s\n",
697 get_class_cast_state_string(this_state),
698 get_class_cast_state_string(ccs->expected_state));
699 assert(this_state >= ccs->expected_state &&
700 "invalid state class cast state setting in graph");
703 if (this_state < ccs->worst_situation)
704 ccs->worst_situation = this_state;
707 /** Verify that the graph meets requirements of state set. */
708 void verify_irg_class_cast_state(ir_graph *irg)
712 FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
714 env.expected_state = get_irg_class_cast_state(irg);
715 env.worst_situation = ir_class_casts_normalized;
717 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
719 if ((env.worst_situation > env.expected_state)) {
720 DB((dbg, LEVEL_1, "Note: class cast state is set lower than reqired "
721 "in graph \n\t%+F\n", irg));
722 DB((dbg, LEVEL_1, " state is %s, reqired is %s\n",
723 get_class_cast_state_string(env.expected_state),
724 get_class_cast_state_string(env.worst_situation)));