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 for (j = 0; j < get_class_n_members(super); j++) {
62 inhent = get_class_member(super, j);
63 /* check whether inhent is already overwritten */
65 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
66 thisent = get_class_member(clss, k);
67 for (l = 0; l < get_entity_n_overwrites(thisent); l++) {
68 if (inhent == get_entity_overwrites(thisent, l)) {
69 /* overwritten - do not copy */
77 thisent = copy_entity_own(inhent, clss);
78 add_entity_overwrites(thisent, inhent);
79 if (get_entity_peculiarity(inhent) == peculiarity_existent)
80 set_entity_peculiarity(thisent, peculiarity_inherited);
81 set_entity_ld_ident(thisent, mfunc(inhent, clss));
82 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
83 assert(is_atomic_entity(inhent) && /* @@@ */
84 "Inheritance of constant, compound entities not implemented");
85 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
86 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
93 void resolve_inheritance(mangle_inherited_name_func *mfunc)
96 mfunc = default_mangle_inherited_name;
97 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
101 /* ----------------------------------------------------------------------- */
102 /* The transitive closure of the subclass/superclass and */
103 /* overwrites/overwrittenby relation. */
105 /* A walk over the ir (O(#types+#entities)) computes the transitive */
106 /* closure. Adding a new type/entity or changing the basic relations in */
107 /* some other way invalidates the transitive closure, i.e., it is not */
108 /* updated by the basic functions. */
110 /* All functions are named as their counterparts for the basic relations, */
111 /* adding the infix 'trans_'. */
112 /* ----------------------------------------------------------------------- */
114 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
116 irp->inh_trans_closure_state = s;
118 void invalidate_irp_inh_transitive_closure_state(void)
120 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
121 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
123 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
125 return irp->inh_trans_closure_state;
128 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)
160 const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
161 const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
164 return ef1->kind != ef2->kind;
168 * calculate the hash value of an tr_inh_trans_tp
170 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
172 return hash_ptr(v->kind);
175 /* This always completes successfully. */
176 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
178 tr_inh_trans_tp a, *found;
181 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
183 found = set_find(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
185 a.directions[d_up] = pset_new_ptr(16);
186 a.directions[d_down] = pset_new_ptr(16);
187 found = set_insert(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
192 static pset *get_entity_map(const ir_entity *ent, dir d)
194 tr_inh_trans_tp *found;
196 assert(is_entity(ent));
197 found = get_firm_kind_entry((const firm_kind *)ent);
198 return found->directions[d];
201 static pset *get_type_map(const ir_type *tp, dir d)
203 tr_inh_trans_tp *found;
206 found = get_firm_kind_entry((const firm_kind *)tp);
207 return found->directions[d];
212 * Walk over all types reachable from tp in the sub/supertype
213 * relation and compute the closure for the two downwards directed
216 * The walk in the dag formed by the relation is tricky: We must visit
217 * all subtypes before visiting the supertypes. So we first walk down.
218 * Then we can compute the closure for this type. Then we walk up.
219 * As we call ourselves recursive, and walk in both directions, there
220 * can be cycles. So we have to make sure, that if we visit a node
221 * a second time (in a walk up) we do nothing. For this we increment
222 * the master visited flag twice.
223 * If the type is marked with master_flag_visited-1 it is on the stack.
224 * If it is marked with master_flag_visited it is fully processed.
226 * Well, we still miss some candidates ... */
227 static void compute_down_closure(ir_type *tp)
229 pset *myset, *subset;
230 size_t i, n_subtypes, n_members, n_supertypes;
231 ir_visited_t master_visited = get_master_type_visited();
233 set_type_visited(tp, master_visited-1);
235 /* Recursive descend. */
236 n_subtypes = get_class_n_subtypes(tp);
237 for (i = 0; i < n_subtypes; ++i) {
238 ir_type *stp = get_class_subtype(tp, i);
239 if (get_type_visited(stp) < master_visited-1) {
240 compute_down_closure(stp);
245 myset = get_type_map(tp, d_down);
246 for (i = 0; i < n_subtypes; ++i) {
247 ir_type *stp = get_class_subtype(tp, i);
248 subset = get_type_map(stp, d_down);
249 pset_insert_ptr(myset, stp);
250 pset_insert_pset_ptr(myset, subset);
254 n_members = get_class_n_members(tp);
255 for (i = 0; i < n_members; ++i) {
256 ir_entity *mem = get_class_member(tp, i);
257 size_t j, n_overwrittenby = get_entity_n_overwrittenby(mem);
259 myset = get_entity_map(mem, d_down);
260 for (j = 0; j < n_overwrittenby; ++j) {
261 ir_entity *ov = get_entity_overwrittenby(mem, j);
262 subset = get_entity_map(ov, d_down);
263 pset_insert_ptr(myset, ov);
264 pset_insert_pset_ptr(myset, subset);
268 mark_type_visited(tp);
271 n_supertypes = get_class_n_supertypes(tp);
272 for (i = 0; i < n_supertypes; ++i) {
273 ir_type *stp = get_class_supertype(tp, i);
274 if (get_type_visited(stp) < master_visited-1) {
275 compute_down_closure(stp);
280 static void compute_up_closure(ir_type *tp)
282 pset *myset, *subset;
283 size_t i, n_subtypes, n_members, n_supertypes;
284 ir_visited_t master_visited = get_master_type_visited();
286 set_type_visited(tp, master_visited-1);
288 /* Recursive descend. */
289 n_supertypes = get_class_n_supertypes(tp);
290 for (i = 0; i < n_supertypes; ++i) {
291 ir_type *stp = get_class_supertype(tp, i);
292 if (get_type_visited(stp) < get_master_type_visited()-1) {
293 compute_up_closure(stp);
298 myset = get_type_map(tp, d_up);
299 for (i = 0; i < n_supertypes; ++i) {
300 ir_type *stp = get_class_supertype(tp, i);
301 subset = get_type_map(stp, d_up);
302 pset_insert_ptr(myset, stp);
303 pset_insert_pset_ptr(myset, subset);
307 n_members = get_class_n_members(tp);
308 for (i = 0; i < n_members; ++i) {
309 ir_entity *mem = get_class_member(tp, i);
310 size_t j, n_overwrites = get_entity_n_overwrites(mem);
312 myset = get_entity_map(mem, d_up);
313 for (j = 0; j < n_overwrites; ++j) {
314 ir_entity *ov = get_entity_overwrites(mem, j);
315 subset = get_entity_map(ov, d_up);
316 pset_insert_pset_ptr(myset, subset);
317 pset_insert_ptr(myset, ov);
321 mark_type_visited(tp);
324 n_subtypes = get_class_n_subtypes(tp);
325 for (i = 0; i < n_subtypes; ++i) {
326 ir_type *stp = get_class_subtype(tp, i);
327 if (get_type_visited(stp) < master_visited-1) {
328 compute_up_closure(stp);
333 void compute_inh_transitive_closure(void)
335 size_t i, n_types = get_irp_n_types();
336 free_inh_transitive_closure();
338 /* The 'down' relation */
339 irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
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 size_t 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 size_t 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;
388 irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
391 void free_inh_transitive_closure(void)
393 if (tr_inh_trans_set) {
394 foreach_set(tr_inh_trans_set, tr_inh_trans_tp, elt) {
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)
408 assert_valid_state();
409 return pset_first(ir_type, get_type_map(tp, d_down));
412 ir_type *get_class_trans_subtype_next(const ir_type *tp)
414 assert_valid_state();
415 return pset_next(ir_type, get_type_map(tp, d_down));
418 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
420 assert_valid_state();
421 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
424 /* - supertype ----------------------------------------------------------- */
426 ir_type *get_class_trans_supertype_first(const ir_type *tp)
428 assert_valid_state();
429 return pset_first(ir_type, get_type_map(tp, d_up));
432 ir_type *get_class_trans_supertype_next(const ir_type *tp)
434 assert_valid_state();
435 return pset_next(ir_type, get_type_map(tp, d_up));
438 /* - overwrittenby ------------------------------------------------------- */
440 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
442 assert_valid_state();
443 return pset_first(ir_entity, get_entity_map(ent, d_down));
446 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
448 assert_valid_state();
449 return pset_next(ir_entity, get_entity_map(ent, d_down));
452 /* - overwrites ---------------------------------------------------------- */
455 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
457 assert_valid_state();
458 return pset_first(ir_entity, get_entity_map(ent, d_up));
461 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
463 assert_valid_state();
464 return pset_next(ir_entity, get_entity_map(ent, d_up));
468 /* ----------------------------------------------------------------------- */
469 /* Classify pairs of types/entities in the inheritance relations. */
470 /* ----------------------------------------------------------------------- */
472 /** Returns true if low is subclass of high. */
473 static int check_is_SubClass_of(ir_type *low, ir_type *high)
475 size_t i, n_subtypes;
477 /* depth first search from high downwards. */
478 n_subtypes = get_class_n_subtypes(high);
479 for (i = 0; i < n_subtypes; i++) {
480 ir_type *stp = get_class_subtype(high, i);
481 if (low == stp) return 1;
482 if (is_SubClass_of(low, stp))
488 int is_SubClass_of(ir_type *low, ir_type *high)
490 assert(is_Class_type(low) && is_Class_type(high));
492 if (low == high) return 1;
494 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
495 pset *m = get_type_map(high, d_down);
496 return pset_find_ptr(m, low) ? 1 : 0;
498 return check_is_SubClass_of(low, high);
501 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
503 while (is_Pointer_type(low) && is_Pointer_type(high)) {
504 low = get_pointer_points_to_type(low);
505 high = get_pointer_points_to_type(high);
508 if (is_Class_type(low) && is_Class_type(high))
509 return is_SubClass_of(low, high);
513 int is_overwritten_by(ir_entity *high, ir_entity *low)
515 size_t i, n_overwrittenby;
516 assert(is_entity(low) && is_entity(high));
518 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
519 pset *m = get_entity_map(high, d_down);
520 return pset_find_ptr(m, low) ? 1 : 0;
523 /* depth first search from high downwards. */
524 n_overwrittenby = get_entity_n_overwrittenby(high);
525 for (i = 0; i < n_overwrittenby; i++) {
526 ir_entity *ov = get_entity_overwrittenby(high, i);
527 if (low == ov) return 1;
528 if (is_overwritten_by(low, ov))
534 /** Resolve polymorphy in the inheritance relation.
536 * Returns the dynamically referenced entity if the static entity and the
537 * dynamic type are given.
538 * Search downwards in overwritten tree.
540 * Need two routines because I want to assert the result.
542 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
544 size_t i, n_overwrittenby;
546 ir_type *owner = get_entity_owner(static_ent);
547 if (owner == dynamic_class) return static_ent;
549 // if the owner of the static_ent already is more special than the dynamic
550 // type to check against - stop here.
551 if (! is_SubClass_of(dynamic_class, owner)) return NULL;
553 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
554 for (i = 0; i < n_overwrittenby; ++i) {
555 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
556 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
560 // No further specialization of static_ent has been found
564 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
567 assert(static_ent && is_entity(static_ent));
569 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);