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