3 * @file tp_inheritance.h
5 * Project: libFIRM <br>
6 * File name: ir/tr/tp_inheritance.h <br>
7 * Purpose: Utility routines for inheritance representation <br>
8 * Author: Goetz Lindenmaier <br>
11 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
12 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
15 * This file supplies a set of utility routines for the inheritance
18 * Inheritance is represented in Firm by two relations: sub/supertype
19 * between class types, overwrites/ovwerwrittenby between entities.
21 * - Classify pairs of types/entities in the inheritance relations.
22 * - Resolve implicit inheritance.
23 * - Compute the transitive closure of the subclass/superclass and
24 * overwrites/overwrittenby relation.
26 * @see type.h entity.h
29 #ifndef _TR_INHERITANCE_H_
30 #define _TR_INHERITANCE_H_
33 /*#include "entity.h"*/
37 /* to resolve recursion between entity.h and irgraph.h */
38 #ifndef _IR_GRAPH_TYPEDEF_
39 #define _IR_GRAPH_TYPEDEF_
40 typedef struct ir_graph ir_graph;
43 /* ----------------------------------------------------------------------- */
44 /* Classify pairs of types/entities in the inheritance relations. */
45 /* ----------------------------------------------------------------------- */
47 /** Returns true if low is subclass of high.
49 * Low is a subclass of high if low == high or if low is a subclass of
50 * a subclass of high. I.e, we search in all subtypes of high for low.
51 * @@@ this can be implemented more efficient if we know the set of all
52 * subclasses of high. */
53 int is_subclass_of(type *low, type *high);
55 /** Subclass check for pointers to classes.
57 * Dereferences at both types the same amount of pointer types (as
58 * many as possible). If the remaining types are both class types
59 * and subclasses, returns true, else false. Can also be called with
61 int is_subclass_ptr_of(type *low, type *high);
63 /** Returns true if high is superclass of low.
65 * Low is a subclass of high if low == high or if low is a subclass of
66 * a subclass of high. I.e, we search in all subtypes of high for low.
67 * @@@ this can be implemented more efficient if we know the set of all
68 * subclasses of high. */
69 int is_superclass_of(type *high, type *low);
71 /** Superclass check for pointers to classes.
73 * Dereferences at both types the same amount of pointer types (as
74 * many as possible). If the remaining types are both class types
75 * and superclasses, returns true, else false. Can also be called with
77 int is_subclass_ptr_of(type *low, type *high);
79 /** Returns true if high is (transitive) overwritten by low.
81 * Returns false if high == low. */
82 int is_overwritten_by(entity *high, entity *low);
84 /** Resolve polymorphy in the inheritance relation.
86 * Returns the dynamically referenced entity if the static entity and the
87 * dynamic type are given.
88 * Searches downwards in overwritten tree. */
89 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent);
91 /* ----------------------------------------------------------------------- */
92 /* Resolve implicit inheritance. */
93 /* ----------------------------------------------------------------------- */
95 /** Default name mangling for inherited entities.
97 * Returns an ident that consists of the name of type followed by an
98 * underscore and the name (not ld_name) of the entity. */
99 ident *default_mangle_inherited_name(entity *super, type *clss);
101 /** Type of argument functions for inheritance resolver.
103 * @param super The entity in the super type that will be overwritten
104 * by the newly generated entity, for which this name is
106 * @param clss The class type in which the new entity will be placed.
108 typedef ident *mangle_inherited_name_func(entity *super, type *clss);
110 /** Resolve implicit inheritance.
112 * Resolves the implicit inheritance supplied by firm. Firm defines,
113 * that each entity that is not overwritten in a subclass is
114 * inherited to this subclass without change implicitly. This
115 * function generates entities that explicitly represent this
116 * inheritance. It generates for each entity overwriting entities in
117 * all subclasses of the owner of the entity, if the entity is not
118 * overwritten in that subclass.
120 * The name of the new entity is generated with the function passed.
121 * If the function is NULL, the default_mangle_inherited_name() is
124 * This function was moved here from firmlower 3/2005.
126 void resolve_inheritance(mangle_inherited_name_func *mfunc);
129 /* ----------------------------------------------------------------------- */
130 /* The transitive closure of the subclass/superclass and */
131 /* overwrites/overwrittenby relation. */
133 /* A walk over the ir (O(#types+#entities)) computes the transitive */
134 /* closure. Adding a new type/entity or changing the basic relations in */
135 /* some other way invalidates the transitive closure, i.e., it is not */
136 /* updated by the basic functions. */
138 /* The transitive edges are held in a set, not in an array as the */
139 /* underlying relation. */
141 /* Do the sets contain the node itself? I assume NOT! */
142 /* ----------------------------------------------------------------------- */
144 /** The state of the transitive closure.
146 * @TODO: we could manage the state for each relation separately. Invalidating
147 * the entity relations does not mean invalidating the class relation. */
149 inh_transitive_closure_none, /**< Closure is not computed, can not be accessed. */
150 inh_transitive_closure_valid, /**< Closure computed and valid. */
151 inh_transitive_closure_invalid, /**< Closure invalid, but can be accessed. */
152 inh_transitive_closure_max /**< Invalid value. */
153 } inh_transitive_closure_state;
155 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
156 void invalidate_irp_inh_transitive_closure_state(void);
157 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
160 /** Compute transitive closure of the subclass/superclass and
161 * overwrites/overwrittenby relation.
163 * This function walks over the ir (O(#types+#entities)) to compute the
164 * transitive closure. */
165 void compute_inh_transitive_closure(void);
167 /** Free memory occupied by the transitive closure information. */
168 void free_inh_transitive_closure(void);
171 /* - subtype ------------------------------------------------------------- */
173 /** Iterate over all transitive subtypes. */
174 type *get_class_trans_subtype_first(type *tp);
175 type *get_class_trans_subtype_next (type *tp);
176 int is_class_trans_subtype (type *tp, type *subtp);
178 /* - supertype ----------------------------------------------------------- */
180 /** Iterate over all transitive supertypes. */
181 type *get_class_trans_supertype_first(type *tp);
182 type *get_class_trans_supertype_next (type *tp);
184 /* - overwrittenby ------------------------------------------------------- */
186 /** Iterate over all entities that transitive overwrite this entities. */
187 entity *get_entity_trans_overwrittenby_first(entity *ent);
188 entity *get_entity_trans_overwrittenby_next (entity *ent);
190 /* - overwrites ---------------------------------------------------------- */
192 /** Iterate over all transitive overwritten entities. */
193 entity *get_entity_trans_overwrites_first(entity *ent);
194 entity *get_entity_trans_overwrites_next (entity *ent);
197 /* ----------------------------------------------------------------------- */
198 /** The state of Cast operations that cast class types or pointers to class
201 * The state expresses, how far Cast operations conform with the class
205 * class B1 extends A {}
206 * class B2 extends A {}
207 * class C extends B1 {}
208 * normalized: Cast operations conform with the inheritance relation.
209 * I.e., the type of the operand of a Cast is either a super= or a sub-
210 * type of the type casted to. Example: (A)((B2) (new C())).
211 * transitive: Cast operations conform with the transitive inheritance
212 * relation. Example: (A)(new C()).
213 * any: Cast operations do not conform with the transitive inheritance
214 * relation. Example: (B2)(new B1())
218 /* ----------------------------------------------------------------------- */
220 /** Flags for class cast state.
222 * The state in irp is always smaller or equal to the state of any
225 * We rely on the ordering of the enum. */
227 ir_class_casts_any = 0, /**< There are class casts that do not cast in conformance with
228 the class hierarchy. @@@ So far this does not happen in Firm. */
229 ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
230 ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
231 ir_class_casts_state_max,
232 } ir_class_cast_state;
233 char *get_class_cast_state_string(ir_class_cast_state s);
235 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
236 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
237 void set_irp_class_cast_state(ir_class_cast_state s);
238 ir_class_cast_state get_irp_class_cast_state(void);
240 /** Verify the class cast state of an irg.
242 * Asserts if state is to high, outputs warning if state is to low
243 * and firm verbosity is set.
245 void verify_irg_class_cast_state(ir_graph *irg);
246 #endif /* _TR_INHERITANCE_H_ */