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
23 * Project: libFIRM <br>
24 * File name: ir/tr/tp_inheritance.h <br>
25 * Purpose: Utility routines for inheritance representation <br>
26 * Author: Goetz Lindenmaier <br>
29 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
32 * This file supplies a set of utility routines for the inheritance
35 * Inheritance is represented in Firm by two relations: sub/supertype
36 * between class types, overwrites/ovwerwrittenby between entities.
38 * - Classify pairs of types/entities in the inheritance relations.
39 * - Resolve implicit inheritance.
40 * - Compute the transitive closure of the subclass/superclass and
41 * overwrites/overwrittenby relation.
43 * @see type.h entity.h
45 #ifndef _FIRM_TR_INHERITANCE_H_
46 #define _FIRM_TR_INHERITANCE_H_
48 #include "firm_types.h"
52 /* ----------------------------------------------------------------------- */
53 /* Classify pairs of types/entities in the inheritance relations. */
54 /* ----------------------------------------------------------------------- */
56 /** Returns true if low is subclass of high.
58 * Low is a subclass of high if low == high or if low is a subclass of
59 * a subclass of high. I.e, we search in all subtypes of high for low.
60 * @@@ this can be implemented more efficient if we know the set of all
61 * subclasses of high. */
62 int is_SubClass_of(ir_type *low, ir_type *high);
64 /** Subclass check for pointers to classes.
66 * Dereferences at both types the same amount of pointer types (as
67 * many as possible). If the remaining types are both class types
68 * and subclasses, returns true, else false. Can also be called with
70 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
72 /** Returns true if high is superclass of low.
74 * Low is a subclass of high if low == high or if low is a subclass of
75 * a subclass of high. I.e, we search in all subtypes of high for low.
76 * @@@ this can be implemented more efficient if we know the set of all
77 * subclasses of high. */
78 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
80 /** Superclass check for pointers to classes.
82 * Dereferences at both types the same amount of pointer types (as
83 * many as possible). If the remaining types are both class types
84 * and superclasses, returns true, else false. Can also be called with
86 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
88 /** Returns true if high is (transitive) overwritten by low.
90 * Returns false if high == low. */
91 int is_overwritten_by(ir_entity *high, ir_entity *low);
93 /** Resolve polymorphism in the inheritance relation.
95 * Returns the dynamically referenced entity if the static entity and the
96 * dynamic type are given.
97 * Searches downwards in overwritten tree. */
98 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
100 /* ----------------------------------------------------------------------- */
101 /* Resolve implicit inheritance. */
102 /* ----------------------------------------------------------------------- */
104 /** Default name mangling for inherited entities.
106 * Returns an ident that consists of the name of type followed by an
107 * underscore and the name (not ld_name) of the entity. */
108 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
110 /** Type of argument functions for inheritance resolver.
112 * @param ent The entity in the super type that will be overwritten
113 * by the newly generated entity, for which this name is
115 * @param clss The class type in which the new entity will be placed.
117 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
119 /** Resolve implicit inheritance.
121 * Resolves the implicit inheritance supplied by firm. Firm defines,
122 * that each entity that is not overwritten in a subclass is
123 * inherited to this subclass without change implicitly. This
124 * function generates entities that explicitly represent this
125 * inheritance. It generates for each entity overwriting entities in
126 * all subclasses of the owner of the entity, if the entity is not
127 * overwritten in that subclass.
129 * The name of the new entity is generated with the function passed.
130 * If the function is NULL, the default_mangle_inherited_name() is
133 * This function was moved here from firmlower 3/2005.
135 void resolve_inheritance(mangle_inherited_name_func *mfunc);
138 /* ----------------------------------------------------------------------- */
139 /* The transitive closure of the subclass/superclass and */
140 /* overwrites/overwrittenby relation. */
142 /* A walk over the ir (O(#types+#entities)) computes the transitive */
143 /* closure. Adding a new type/entity or changing the basic relations in */
144 /* some other way invalidates the transitive closure, i.e., it is not */
145 /* updated by the basic functions. */
147 /* The transitive edges are held in a set, not in an array as the */
148 /* underlying relation. */
150 /* Do the sets contain the node itself? I assume NOT! */
151 /* ----------------------------------------------------------------------- */
153 /** The state of the transitive closure.
155 * @todo: we could manage the state for each relation separately. Invalidating
156 * the entity relations does not mean invalidating the class relation. */
158 inh_transitive_closure_none, /**< Closure is not computed, can not be accessed. */
159 inh_transitive_closure_valid, /**< Closure computed and valid. */
160 inh_transitive_closure_invalid, /**< Closure invalid, but can be accessed. */
161 inh_transitive_closure_max /**< Invalid value. */
162 } inh_transitive_closure_state;
164 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
165 void invalidate_irp_inh_transitive_closure_state(void);
166 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
169 /** Compute transitive closure of the subclass/superclass and
170 * overwrites/overwrittenby relation.
172 * This function walks over the ir (O(#types+#entities)) to compute the
173 * transitive closure. */
174 void compute_inh_transitive_closure(void);
176 /** Free memory occupied by the transitive closure information. */
177 void free_inh_transitive_closure(void);
180 /* - subtype ------------------------------------------------------------- */
182 /** Iterate over all transitive subtypes. */
183 ir_type *get_class_trans_subtype_first(ir_type *tp);
184 ir_type *get_class_trans_subtype_next (ir_type *tp);
185 int is_class_trans_subtype (ir_type *tp, ir_type *subtp);
187 /* - supertype ----------------------------------------------------------- */
189 /** Iterate over all transitive supertypes. */
190 ir_type *get_class_trans_supertype_first(ir_type *tp);
191 ir_type *get_class_trans_supertype_next (ir_type *tp);
193 /* - overwrittenby ------------------------------------------------------- */
195 /** Iterate over all entities that transitive overwrite this entities. */
196 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent);
197 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent);
199 /* - overwrites ---------------------------------------------------------- */
201 /** Iterate over all transitive overwritten entities. */
202 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent);
203 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent);
206 /* ----------------------------------------------------------------------- */
207 /** The state of Cast operations that cast class types or pointers to class
210 * The state expresses, how far Cast operations conform with the class
214 * class B1 extends A {}
215 * class B2 extends A {}
216 * class C extends B1 {}
217 * normalized: Cast operations conform with the inheritance relation.
218 * I.e., the type of the operand of a Cast is either a super= or a sub-
219 * type of the type casted to. Example: (A)((B2) (new C())).
220 * transitive: Cast operations conform with the transitive inheritance
221 * relation. Example: (A)(new C()).
222 * any: Cast operations do not conform with the transitive inheritance
223 * relation. Example: (B2)(new B1())
227 /* ----------------------------------------------------------------------- */
229 /** Flags for class cast state.
231 * The state in irp is always smaller or equal to the state of any
234 * We rely on the ordering of the enum. */
236 ir_class_casts_any = 0, /**< There are class casts that do not cast in conformance with
237 the class hierarchy. @@@ So far this does not happen in Firm. */
238 ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
239 ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
240 ir_class_casts_state_max
241 } ir_class_cast_state;
242 char *get_class_cast_state_string(ir_class_cast_state s);
244 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
245 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
246 void set_irp_class_cast_state(ir_class_cast_state s);
247 ir_class_cast_state get_irp_class_cast_state(void);
249 /** Verify the class cast state of an irg.
251 * Asserts if state is to high, outputs warning if state is to low
252 * and firm verbosity is set.
254 void verify_irg_class_cast_state(ir_graph *irg);
255 #endif /* _FIRM_TR_INHERITANCE_H_ */