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
29 #include "irgraph_t.h"
37 /* ----------------------------------------------------------------------- */
38 /* Resolve implicit inheritance. */
39 /* ----------------------------------------------------------------------- */
41 ident *default_mangle_inherited_name(const ir_entity *super, const ir_type *clss)
43 return id_mangle_u(new_id_from_str("inh"), id_mangle_u(get_class_ident(clss), get_entity_ident(super)));
46 /** Replicates all entities in all super classes that are not overwritten
47 by an entity of this class. */
48 static void copy_entities_from_superclass(ir_type *clss, void *env)
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 /* 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_linkage(inhent) & IR_LINKAGE_CONSTANT) {
84 assert(is_atomic_entity(inhent) && /* @@@ */
85 "Inheritance of constant, compound entities not implemented");
86 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
87 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
94 void resolve_inheritance(mangle_inherited_name_func *mfunc)
97 mfunc = default_mangle_inherited_name;
98 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
102 /* ----------------------------------------------------------------------- */
103 /* The transitive closure of the subclass/superclass and */
104 /* overwrites/overwrittenby relation. */
106 /* A walk over the ir (O(#types+#entities)) computes the transitive */
107 /* closure. Adding a new type/entity or changing the basic relations in */
108 /* some other way invalidates the transitive closure, i.e., it is not */
109 /* updated by the basic functions. */
111 /* All functions are named as their counterparts for the basic relations, */
112 /* adding the infix 'trans_'. */
113 /* ----------------------------------------------------------------------- */
115 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
117 irp->inh_trans_closure_state = s;
119 void invalidate_irp_inh_transitive_closure_state(void)
121 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
122 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
124 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)
131 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
132 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
135 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136 /* There is a set that extends each entity/type with two new */
137 /* fields: one for the upwards directed relation: 'up' (supertype, */
138 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
139 /* type, overwrittenby. These fields contain psets (and maybe later */
140 /* arrays) listing all subtypes... */
141 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
149 const firm_kind *kind; /**< An entity or type. */
153 /* We use this set for all types and entities. */
154 static set *tr_inh_trans_set = NULL;
157 * Compare two tr_inh_trans_tp entries.
159 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
161 const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
162 const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
165 return ef1->kind != ef2->kind;
169 * calculate the hash value of an tr_inh_trans_tp
171 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
173 return hash_ptr(v->kind);
176 /* This always completes successfully. */
177 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
179 tr_inh_trans_tp a, *found;
182 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
184 found = set_find(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
186 a.directions[d_up] = pset_new_ptr(16);
187 a.directions[d_down] = pset_new_ptr(16);
188 found = set_insert(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
193 static pset *get_entity_map(const ir_entity *ent, dir d)
195 tr_inh_trans_tp *found;
197 assert(is_entity(ent));
198 found = get_firm_kind_entry((const firm_kind *)ent);
199 return found->directions[d];
202 static pset *get_type_map(const ir_type *tp, dir d)
204 tr_inh_trans_tp *found;
207 found = get_firm_kind_entry((const firm_kind *)tp);
208 return found->directions[d];
213 * Walk over all types reachable from tp in the sub/supertype
214 * relation and compute the closure for the two downwards directed
217 * The walk in the dag formed by the relation is tricky: We must visit
218 * all subtypes before visiting the supertypes. So we first walk down.
219 * Then we can compute the closure for this type. Then we walk up.
220 * As we call ourselves recursive, and walk in both directions, there
221 * can be cycles. So we have to make sure, that if we visit a node
222 * a second time (in a walk up) we do nothing. For this we increment
223 * the master visited flag twice.
224 * If the type is marked with master_flag_visited-1 it is on the stack.
225 * If it is marked with master_flag_visited it is fully processed.
227 * Well, we still miss some candidates ... */
228 static void compute_down_closure(ir_type *tp)
230 pset *myset, *subset;
231 size_t i, n_subtypes, n_members, n_supertypes;
232 ir_visited_t 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 ir_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 ir_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 ir_entity *mem = get_class_member(tp, i);
260 size_t 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 ir_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 ir_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(ir_type *tp)
285 pset *myset, *subset;
286 size_t i, n_subtypes, n_members, n_supertypes;
287 ir_visited_t master_visited = get_master_type_visited();
289 assert(is_Class_type(tp));
291 set_type_visited(tp, master_visited-1);
293 /* Recursive descend. */
294 n_supertypes = get_class_n_supertypes(tp);
295 for (i = 0; i < n_supertypes; ++i) {
296 ir_type *stp = get_class_supertype(tp, i);
297 if (get_type_visited(stp) < get_master_type_visited()-1) {
298 compute_up_closure(stp);
303 myset = get_type_map(tp, d_up);
304 for (i = 0; i < n_supertypes; ++i) {
305 ir_type *stp = get_class_supertype(tp, i);
306 subset = get_type_map(stp, d_up);
307 pset_insert_ptr(myset, stp);
308 pset_insert_pset_ptr(myset, subset);
312 n_members = get_class_n_members(tp);
313 for (i = 0; i < n_members; ++i) {
314 ir_entity *mem = get_class_member(tp, i);
315 size_t j, n_overwrites = get_entity_n_overwrites(mem);
317 myset = get_entity_map(mem, d_up);
318 for (j = 0; j < n_overwrites; ++j) {
319 ir_entity *ov = get_entity_overwrites(mem, j);
320 subset = get_entity_map(ov, d_up);
321 pset_insert_pset_ptr(myset, subset);
322 pset_insert_ptr(myset, ov);
326 mark_type_visited(tp);
329 n_subtypes = get_class_n_subtypes(tp);
330 for (i = 0; i < n_subtypes; ++i) {
331 ir_type *stp = get_class_subtype(tp, i);
332 if (get_type_visited(stp) < master_visited-1) {
333 compute_up_closure(stp);
338 void compute_inh_transitive_closure(void)
340 size_t i, n_types = get_irp_n_types();
341 free_inh_transitive_closure();
343 /* The 'down' relation */
344 irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
345 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
346 inc_master_type_visited();
347 for (i = 0; i < n_types; ++i) {
348 ir_type *tp = get_irp_type(i);
349 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
350 size_t j, n_subtypes = get_class_n_subtypes(tp);
351 int has_unmarked_subtype = 0;
353 assert(get_type_visited(tp) < get_master_type_visited()-1);
354 for (j = 0; j < n_subtypes; ++j) {
355 ir_type *stp = get_class_subtype(tp, j);
356 if (type_not_visited(stp)) {
357 has_unmarked_subtype = 1;
362 /* This is a good starting point. */
363 if (!has_unmarked_subtype)
364 compute_down_closure(tp);
368 /* The 'up' relation */
369 inc_master_type_visited();
370 inc_master_type_visited();
371 for (i = 0; i < n_types; ++i) {
372 ir_type *tp = get_irp_type(i);
373 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
374 size_t j, n_supertypes = get_class_n_supertypes(tp);
375 int has_unmarked_supertype = 0;
377 assert(get_type_visited(tp) < get_master_type_visited()-1);
378 for (j = 0; j < n_supertypes; ++j) {
379 ir_type *stp = get_class_supertype(tp, j);
380 if (type_not_visited(stp)) {
381 has_unmarked_supertype = 1;
386 /* This is a good starting point. */
387 if (!has_unmarked_supertype)
388 compute_up_closure(tp);
392 irp->inh_trans_closure_state = inh_transitive_closure_valid;
393 irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
396 void free_inh_transitive_closure(void)
398 if (tr_inh_trans_set) {
399 foreach_set(tr_inh_trans_set, tr_inh_trans_tp, elt) {
400 del_pset(elt->directions[d_up]);
401 del_pset(elt->directions[d_down]);
403 del_set(tr_inh_trans_set);
404 tr_inh_trans_set = NULL;
406 irp->inh_trans_closure_state = inh_transitive_closure_none;
409 /* - subtype ------------------------------------------------------------- */
411 ir_type *get_class_trans_subtype_first(const ir_type *tp)
413 assert_valid_state();
414 return pset_first(ir_type, get_type_map(tp, d_down));
417 ir_type *get_class_trans_subtype_next(const ir_type *tp)
419 assert_valid_state();
420 return pset_next(ir_type, get_type_map(tp, d_down));
423 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
425 assert_valid_state();
426 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
429 /* - supertype ----------------------------------------------------------- */
431 ir_type *get_class_trans_supertype_first(const ir_type *tp)
433 assert_valid_state();
434 return pset_first(ir_type, get_type_map(tp, d_up));
437 ir_type *get_class_trans_supertype_next(const ir_type *tp)
439 assert_valid_state();
440 return pset_next(ir_type, get_type_map(tp, d_up));
443 /* - overwrittenby ------------------------------------------------------- */
445 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
447 assert_valid_state();
448 return pset_first(ir_entity, get_entity_map(ent, d_down));
451 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
453 assert_valid_state();
454 return pset_next(ir_entity, get_entity_map(ent, d_down));
457 /* - overwrites ---------------------------------------------------------- */
460 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
462 assert_valid_state();
463 return pset_first(ir_entity, get_entity_map(ent, d_up));
466 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
468 assert_valid_state();
469 return pset_next(ir_entity, 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)
480 size_t i, n_subtypes;
482 /* depth first search from high downwards. */
483 n_subtypes = get_class_n_subtypes(high);
484 for (i = 0; i < n_subtypes; i++) {
485 ir_type *stp = get_class_subtype(high, i);
486 if (low == stp) return 1;
487 if (is_SubClass_of(low, stp))
493 int is_SubClass_of(ir_type *low, ir_type *high)
495 assert(is_Class_type(low) && is_Class_type(high));
497 if (low == high) return 1;
499 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
500 pset *m = get_type_map(high, d_down);
501 return pset_find_ptr(m, low) ? 1 : 0;
503 return check_is_SubClass_of(low, high);
506 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
508 while (is_Pointer_type(low) && is_Pointer_type(high)) {
509 low = get_pointer_points_to_type(low);
510 high = get_pointer_points_to_type(high);
513 if (is_Class_type(low) && is_Class_type(high))
514 return is_SubClass_of(low, high);
518 int is_overwritten_by(ir_entity *high, ir_entity *low)
520 size_t i, n_overwrittenby;
521 assert(is_entity(low) && is_entity(high));
523 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
524 pset *m = get_entity_map(high, d_down);
525 return pset_find_ptr(m, low) ? 1 : 0;
528 /* depth first search from high downwards. */
529 n_overwrittenby = get_entity_n_overwrittenby(high);
530 for (i = 0; i < n_overwrittenby; i++) {
531 ir_entity *ov = get_entity_overwrittenby(high, i);
532 if (low == ov) return 1;
533 if (is_overwritten_by(low, ov))
539 /** Resolve polymorphy in the inheritance relation.
541 * Returns the dynamically referenced entity if the static entity and the
542 * dynamic type are given.
543 * Search downwards in overwritten tree.
545 * Need two routines because I want to assert the result.
547 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
549 size_t i, n_overwrittenby;
551 ir_type *owner = get_entity_owner(static_ent);
552 if (owner == dynamic_class) return static_ent;
554 // if the owner of the static_ent already is more special than the dynamic
555 // type to check against - stop here.
556 if (! is_SubClass_of(dynamic_class, owner)) return NULL;
558 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
559 for (i = 0; i < n_overwrittenby; ++i) {
560 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
561 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
565 // No further specialization of static_ent has been found
569 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)
590 if (get_irp_class_cast_state() > s)
591 set_irp_class_cast_state(s);
592 irg->class_cast_state = s;
595 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
597 return irg->class_cast_state;
600 void set_irp_class_cast_state(ir_class_cast_state s)
604 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
605 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
607 irp->class_cast_state = s;
610 ir_class_cast_state get_irp_class_cast_state(void)
612 return irp->class_cast_state;