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