Updated header
[libfirm] / ir / tr / tr_inheritance.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file tr_inheritance.h
22  *
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>
27  * Modified by:                                                           <br>
28  * Created:                                                               <br>
29  * Copyright:   (c) 2001-2005 Universität Karlsruhe                       <br>
30  * CVS-ID:      $Id$
31  *
32  * This file supplies a set of utility routines for the inheritance
33  * representation.
34  *
35  * Inheritance is represented in Firm by two relations: sub/supertype
36  * between class types, overwrites/ovwerwrittenby between entities.
37  *
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.
42  *
43  *  @see  type.h entity.h
44  */
45 #ifndef _FIRM_TR_INHERITANCE_H_
46 #define _FIRM_TR_INHERITANCE_H_
47
48 #include "firm_types.h"
49 #include "type.h"
50 #include "ident.h"
51
52 /* ----------------------------------------------------------------------- */
53 /* Classify pairs of types/entities in the inheritance relations.          */
54 /* ----------------------------------------------------------------------- */
55
56 /** Returns true if low is subclass of high.
57  *
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);
63
64 /** Subclass check for pointers to classes.
65  *
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
69  *  two class types.  */
70 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
71
72 /** Returns true if high is superclass of low.
73  *
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)
79
80 /** Superclass check for pointers to classes.
81  *
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
85  *  two class types.  */
86 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
87
88 /** Returns true if high is (transitive) overwritten by low.
89  *
90  *  Returns false if high == low. */
91 int is_overwritten_by(ir_entity *high, ir_entity *low);
92
93 /** Resolve polymorphism in the inheritance relation.
94  *
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);
99
100 /* ----------------------------------------------------------------------- */
101 /* Resolve implicit inheritance.                                           */
102 /* ----------------------------------------------------------------------- */
103
104 /** Default name mangling for inherited entities.
105  *
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);
109
110 /** Type of argument functions for inheritance resolver.
111  *
112  * @param ent     The entity in the super type that will be overwritten
113  *                by the newly generated entity, for which this name is
114  *                used.
115  * @param clss    The class type in which the new entity will be placed.
116  */
117 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
118
119 /** Resolve implicit inheritance.
120  *
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.
128  *
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
131  *  used.
132  *
133  *  This function was moved here from firmlower 3/2005.
134  */
135 void resolve_inheritance(mangle_inherited_name_func *mfunc);
136
137
138 /* ----------------------------------------------------------------------- */
139 /* The transitive closure of the subclass/superclass and                   */
140 /* overwrites/overwrittenby relation.                                      */
141 /*                                                                         */
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.                                         */
146 /*                                                                         */
147 /* The transitive edges are held in a set, not in an array as the          */
148 /* underlying relation.                                                    */
149 /*                                                                         */
150 /* Do the sets contain the node itself?  I assume NOT!                     */
151 /* ----------------------------------------------------------------------- */
152
153 /** The state of the transitive closure.
154  *
155  *  @todo: we could manage the state for each relation separately.  Invalidating
156  *  the entity relations does not mean invalidating the class relation. */
157 typedef enum {
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;
163
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);
167
168
169 /** Compute transitive closure of the subclass/superclass and
170 * overwrites/overwrittenby relation.
171 *
172 * This function walks over the ir (O(#types+#entities)) to compute the
173 * transitive closure.    */
174 void compute_inh_transitive_closure(void);
175
176 /** Free memory occupied by the transitive closure information. */
177 void free_inh_transitive_closure(void);
178
179
180 /* - subtype ------------------------------------------------------------- */
181
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);
186
187 /* - supertype ----------------------------------------------------------- */
188
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);
192
193 /* - overwrittenby ------------------------------------------------------- */
194
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);
198
199 /* - overwrites ---------------------------------------------------------- */
200
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);
204
205
206 /* ----------------------------------------------------------------------- */
207 /** The state of Cast operations that cast class types or pointers to class
208  *  types.
209  *
210  * The state expresses, how far Cast operations conform with the class
211  * hierarchy.
212  *
213  *   class A {}
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())
224  *
225  *  @see: tropt.h
226  */
227 /* ----------------------------------------------------------------------- */
228
229 /** Flags for class cast state.
230  *
231  * The state in irp is always smaller or equal to the state of any
232  * irg.
233  *
234  * We rely on the ordering of the enum. */
235 typedef 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);
243
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);
248
249 /** Verify the class cast state of an irg.
250  *
251  *  Asserts if state is to high, outputs warning if state is to low
252  *  and firm verbosity is set.
253  */
254 void verify_irg_class_cast_state(ir_graph *irg);
255 #endif  /* _FIRM_TR_INHERITANCE_H_ */