2 * @file tr_inheritance.h
4 * Project: libFIRM <br>
5 * File name: ir/tr/tp_inheritance.h <br>
6 * Purpose: Utility routines for inheritance representation <br>
7 * Author: Goetz Lindenmaier <br>
10 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
14 * This file supplies a set of utility routines for the inheritance
17 * Inheritance is represented in Firm by two relations: sub/supertype
18 * between class types, overwrites/ovwerwrittenby between entities.
20 * - Classify pairs of types/entities in the inheritance relations.
21 * - Resolve implicit inheritance.
22 * - Compute the transitive closure of the subclass/superclass and
23 * overwrites/overwrittenby relation.
25 * @see type.h entity.h
27 #ifndef _FIRM_TR_INHERITANCE_H_
28 #define _FIRM_TR_INHERITANCE_H_
30 #include "firm_types.h"
34 /* ----------------------------------------------------------------------- */
35 /* Classify pairs of types/entities in the inheritance relations. */
36 /* ----------------------------------------------------------------------- */
38 /** Returns true if low is subclass of high.
40 * Low is a subclass of high if low == high or if low is a subclass of
41 * a subclass of high. I.e, we search in all subtypes of high for low.
42 * @@@ this can be implemented more efficient if we know the set of all
43 * subclasses of high. */
44 int is_SubClass_of(ir_type *low, ir_type *high);
46 /** Subclass check for pointers to classes.
48 * Dereferences at both types the same amount of pointer types (as
49 * many as possible). If the remaining types are both class types
50 * and subclasses, returns true, else false. Can also be called with
52 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
54 /** Returns true if high is superclass of low.
56 * Low is a subclass of high if low == high or if low is a subclass of
57 * a subclass of high. I.e, we search in all subtypes of high for low.
58 * @@@ this can be implemented more efficient if we know the set of all
59 * subclasses of high. */
60 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
62 /** Superclass check for pointers to classes.
64 * Dereferences at both types the same amount of pointer types (as
65 * many as possible). If the remaining types are both class types
66 * and superclasses, returns true, else false. Can also be called with
68 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
70 /** Returns true if high is (transitive) overwritten by low.
72 * Returns false if high == low. */
73 int is_overwritten_by(ir_entity *high, ir_entity *low);
75 /** Resolve polymorphism in the inheritance relation.
77 * Returns the dynamically referenced entity if the static entity and the
78 * dynamic type are given.
79 * Searches downwards in overwritten tree. */
80 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
82 /* ----------------------------------------------------------------------- */
83 /* Resolve implicit inheritance. */
84 /* ----------------------------------------------------------------------- */
86 /** Default name mangling for inherited entities.
88 * Returns an ident that consists of the name of type followed by an
89 * underscore and the name (not ld_name) of the entity. */
90 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
92 /** Type of argument functions for inheritance resolver.
94 * @param ent The entity in the super type that will be overwritten
95 * by the newly generated entity, for which this name is
97 * @param clss The class type in which the new entity will be placed.
99 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
101 /** Resolve implicit inheritance.
103 * Resolves the implicit inheritance supplied by firm. Firm defines,
104 * that each entity that is not overwritten in a subclass is
105 * inherited to this subclass without change implicitly. This
106 * function generates entities that explicitly represent this
107 * inheritance. It generates for each entity overwriting entities in
108 * all subclasses of the owner of the entity, if the entity is not
109 * overwritten in that subclass.
111 * The name of the new entity is generated with the function passed.
112 * If the function is NULL, the default_mangle_inherited_name() is
115 * This function was moved here from firmlower 3/2005.
117 void resolve_inheritance(mangle_inherited_name_func *mfunc);
120 /* ----------------------------------------------------------------------- */
121 /* The transitive closure of the subclass/superclass and */
122 /* overwrites/overwrittenby relation. */
124 /* A walk over the ir (O(#types+#entities)) computes the transitive */
125 /* closure. Adding a new type/entity or changing the basic relations in */
126 /* some other way invalidates the transitive closure, i.e., it is not */
127 /* updated by the basic functions. */
129 /* The transitive edges are held in a set, not in an array as the */
130 /* underlying relation. */
132 /* Do the sets contain the node itself? I assume NOT! */
133 /* ----------------------------------------------------------------------- */
135 /** The state of the transitive closure.
137 * @todo: we could manage the state for each relation separately. Invalidating
138 * the entity relations does not mean invalidating the class relation. */
140 inh_transitive_closure_none, /**< Closure is not computed, can not be accessed. */
141 inh_transitive_closure_valid, /**< Closure computed and valid. */
142 inh_transitive_closure_invalid, /**< Closure invalid, but can be accessed. */
143 inh_transitive_closure_max /**< Invalid value. */
144 } inh_transitive_closure_state;
146 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
147 void invalidate_irp_inh_transitive_closure_state(void);
148 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
151 /** Compute transitive closure of the subclass/superclass and
152 * overwrites/overwrittenby relation.
154 * This function walks over the ir (O(#types+#entities)) to compute the
155 * transitive closure. */
156 void compute_inh_transitive_closure(void);
158 /** Free memory occupied by the transitive closure information. */
159 void free_inh_transitive_closure(void);
162 /* - subtype ------------------------------------------------------------- */
164 /** Iterate over all transitive subtypes. */
165 ir_type *get_class_trans_subtype_first(ir_type *tp);
166 ir_type *get_class_trans_subtype_next (ir_type *tp);
167 int is_class_trans_subtype (ir_type *tp, ir_type *subtp);
169 /* - supertype ----------------------------------------------------------- */
171 /** Iterate over all transitive supertypes. */
172 ir_type *get_class_trans_supertype_first(ir_type *tp);
173 ir_type *get_class_trans_supertype_next (ir_type *tp);
175 /* - overwrittenby ------------------------------------------------------- */
177 /** Iterate over all entities that transitive overwrite this entities. */
178 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent);
179 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent);
181 /* - overwrites ---------------------------------------------------------- */
183 /** Iterate over all transitive overwritten entities. */
184 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent);
185 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent);
188 /* ----------------------------------------------------------------------- */
189 /** The state of Cast operations that cast class types or pointers to class
192 * The state expresses, how far Cast operations conform with the class
196 * class B1 extends A {}
197 * class B2 extends A {}
198 * class C extends B1 {}
199 * normalized: Cast operations conform with the inheritance relation.
200 * I.e., the type of the operand of a Cast is either a super= or a sub-
201 * type of the type casted to. Example: (A)((B2) (new C())).
202 * transitive: Cast operations conform with the transitive inheritance
203 * relation. Example: (A)(new C()).
204 * any: Cast operations do not conform with the transitive inheritance
205 * relation. Example: (B2)(new B1())
209 /* ----------------------------------------------------------------------- */
211 /** Flags for class cast state.
213 * The state in irp is always smaller or equal to the state of any
216 * We rely on the ordering of the enum. */
218 ir_class_casts_any = 0, /**< There are class casts that do not cast in conformance with
219 the class hierarchy. @@@ So far this does not happen in Firm. */
220 ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
221 ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
222 ir_class_casts_state_max
223 } ir_class_cast_state;
224 char *get_class_cast_state_string(ir_class_cast_state s);
226 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
227 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
228 void set_irp_class_cast_state(ir_class_cast_state s);
229 ir_class_cast_state get_irp_class_cast_state(void);
231 /** Verify the class cast state of an irg.
233 * Asserts if state is to high, outputs warning if state is to low
234 * and firm verbosity is set.
236 void verify_irg_class_cast_state(ir_graph *irg);
237 #endif /* _FIRM_TR_INHERITANCE_H_ */