2 * Copyright (C) 1995-2007 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.h
22 * @brief Utility routines for inheritance representation
23 * @author Goetz Lindenmaier
27 * This file supplies a set of utility routines for the inheritance
30 * Inheritance is represented in Firm by two relations: sub/supertype
31 * between class types, overwrites/ovwerwrittenby between entities.
33 * - Classify pairs of types/entities in the inheritance relations.
34 * - Resolve implicit inheritance.
35 * - Compute the transitive closure of the subclass/superclass and
36 * overwrites/overwrittenby relation.
38 * @see type.h entity.h
40 #ifndef FIRM_TR_INHERITANCE_H
41 #define FIRM_TR_INHERITANCE_H
43 #include "firm_types.h"
47 /* ----------------------------------------------------------------------- */
48 /* Classify pairs of types/entities in the inheritance relations. */
49 /* ----------------------------------------------------------------------- */
51 /** Returns true if low is subclass of high.
53 * Low is a subclass of high if low == high or if low is a subclass of
54 * a subclass of high. I.e, we search in all subtypes of high for low.
55 * @@@ this can be implemented more efficient if we know the set of all
56 * subclasses of high. */
57 int is_SubClass_of(ir_type *low, ir_type *high);
59 /** Subclass check for pointers to classes.
61 * Dereferences at both types the same amount of pointer types (as
62 * many as possible). If the remaining types are both class types
63 * and subclasses, returns true, else false. Can also be called with
65 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
67 /** Returns true if high is superclass of low.
69 * Low is a subclass of high if low == high or if low is a subclass of
70 * a subclass of high. I.e, we search in all subtypes of high for low.
71 * @@@ this can be implemented more efficient if we know the set of all
72 * subclasses of high. */
73 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
75 /** Superclass check for pointers to classes.
77 * Dereferences at both types the same amount of pointer types (as
78 * many as possible). If the remaining types are both class types
79 * and superclasses, returns true, else false. Can also be called with
81 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
83 /** Returns true if high is (transitive) overwritten by low.
85 * Returns false if high == low. */
86 int is_overwritten_by(ir_entity *high, ir_entity *low);
88 /** Resolve polymorphism in the inheritance relation.
90 * Returns the dynamically referenced entity if the static entity and the
91 * dynamic type are given.
92 * Searches downwards in overwritten tree. */
93 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
95 /* ----------------------------------------------------------------------- */
96 /* Resolve implicit inheritance. */
97 /* ----------------------------------------------------------------------- */
99 /** Default name mangling for inherited entities.
101 * Returns an ident that consists of the name of type followed by an
102 * underscore and the name (not ld_name) of the entity. */
103 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
105 /** Type of argument functions for inheritance resolver.
107 * @param ent The entity in the super type that will be overwritten
108 * by the newly generated entity, for which this name is
110 * @param clss The class type in which the new entity will be placed.
112 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
114 /** Resolve implicit inheritance.
116 * Resolves the implicit inheritance supplied by firm. Firm defines,
117 * that each entity that is not overwritten in a subclass is
118 * inherited to this subclass without change implicitly. This
119 * function generates entities that explicitly represent this
120 * inheritance. It generates for each entity overwriting entities in
121 * all subclasses of the owner of the entity, if the entity is not
122 * overwritten in that subclass.
124 * The name of the new entity is generated with the function passed.
125 * If the function is NULL, the default_mangle_inherited_name() is
128 * This function was moved here from firmlower 3/2005.
130 void resolve_inheritance(mangle_inherited_name_func *mfunc);
133 /* ----------------------------------------------------------------------- */
134 /* The transitive closure of the subclass/superclass and */
135 /* overwrites/overwrittenby relation. */
137 /* A walk over the ir (O(#types+#entities)) computes the transitive */
138 /* closure. Adding a new type/entity or changing the basic relations in */
139 /* some other way invalidates the transitive closure, i.e., it is not */
140 /* updated by the basic functions. */
142 /* The transitive edges are held in a set, not in an array as the */
143 /* underlying relation. */
145 /* Do the sets contain the node itself? I assume NOT! */
146 /* ----------------------------------------------------------------------- */
148 /** The state of the transitive closure.
150 * @todo: we could manage the state for each relation separately. Invalidating
151 * the entity relations does not mean invalidating the class relation. */
153 inh_transitive_closure_none, /**< Closure is not computed, can not be accessed. */
154 inh_transitive_closure_valid, /**< Closure computed and valid. */
155 inh_transitive_closure_invalid, /**< Closure invalid, but can be accessed. */
156 inh_transitive_closure_max /**< Invalid value. */
157 } inh_transitive_closure_state;
159 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
160 void invalidate_irp_inh_transitive_closure_state(void);
161 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
164 /** Compute transitive closure of the subclass/superclass and
165 * overwrites/overwrittenby relation.
167 * This function walks over the ir (O(#types+#entities)) to compute the
168 * transitive closure. */
169 void compute_inh_transitive_closure(void);
171 /** Free memory occupied by the transitive closure information. */
172 void free_inh_transitive_closure(void);
175 /* - subtype ------------------------------------------------------------- */
177 /** Iterate over all transitive subtypes. */
178 ir_type *get_class_trans_subtype_first(ir_type *tp);
179 ir_type *get_class_trans_subtype_next (ir_type *tp);
180 int is_class_trans_subtype (ir_type *tp, ir_type *subtp);
182 /* - supertype ----------------------------------------------------------- */
184 /** Iterate over all transitive supertypes. */
185 ir_type *get_class_trans_supertype_first(ir_type *tp);
186 ir_type *get_class_trans_supertype_next (ir_type *tp);
188 /* - overwrittenby ------------------------------------------------------- */
190 /** Iterate over all entities that transitive overwrite this entities. */
191 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent);
192 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent);
194 /* - overwrites ---------------------------------------------------------- */
196 /** Iterate over all transitive overwritten entities. */
197 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent);
198 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent);
201 /* ----------------------------------------------------------------------- */
202 /** The state of Cast operations that cast class types or pointers to class
205 * The state expresses, how far Cast operations conform with the class
209 * class B1 extends A {}
210 * class B2 extends A {}
211 * class C extends B1 {}
212 * normalized: Cast operations conform with the inheritance relation.
213 * I.e., the type of the operand of a Cast is either a super= or a sub-
214 * type of the type casted to. Example: (A)((B2) (new C())).
215 * transitive: Cast operations conform with the transitive inheritance
216 * relation. Example: (A)(new C()).
217 * any: Cast operations do not conform with the transitive inheritance
218 * relation. Example: (B2)(new B1())
222 /* ----------------------------------------------------------------------- */
224 /** Flags for class cast state.
226 * The state in irp is always smaller or equal to the state of any
229 * We rely on the ordering of the enum. */
231 ir_class_casts_any = 0, /**< There are class casts that do not cast in conformance with
232 the class hierarchy. @@@ So far this does not happen in Firm. */
233 ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
234 ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
235 ir_class_casts_state_max
236 } ir_class_cast_state;
237 char *get_class_cast_state_string(ir_class_cast_state s);
239 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
240 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
241 void set_irp_class_cast_state(ir_class_cast_state s);
242 ir_class_cast_state get_irp_class_cast_state(void);
244 /** Verify the class cast state of an irg.
246 * Asserts if state is to high, outputs warning if state is to low
247 * and firm verbosity is set.
249 void verify_irg_class_cast_state(ir_graph *irg);
250 #endif /* FIRM_TR_INHERITANCE_H */