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