syntactic changes for jni generator / crecoder
[libfirm] / ir / tr / entity.h
1 /*
2 **  Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
3 **  All rights reserved.
4 **
5 **  Authors: Martin Trapp, Christian Schaefer,
6 **           Goetz Lindenmaier
7 **
8 **  entity.h:  entities represent all program known objects.
9 **
10 **  An entity is the representation of program known objects in Firm.
11 **  The primary concept of entities is to represent members of complex
12 **  types, i.e., fields and methods of classes.  As not all programming
13 **  language model all variables and methods as members of some class,
14 **  the concept of entities is extended to cover also local and global
15 **  variables, and arbitrary procedures.
16 **
17 **  An entity always specifies the type of the object it represents and
18 **  the type of the object it is a part of, the owner of the entity.
19 **  Originally this is the type of the class of which the entity is a
20 **  member.
21 **  The owner of local variables is the procedure they are defined in.
22 **  The owner of global variables and procedures visible in the whole
23 **  program is a universally defined class type "GlobalType".  The owner
24 **  of procedures defined in the scope of an other procedure is the
25 **  enclosing procedure.
26 **
27 **  In detail the datastructure entity has the following fields:
28 **
29 **  ident *name     Name of this entity as specified in the source code.
30 **                  Only unequivocal in conjuction with scope.
31 **  ident *ld_name  Unique name of this entity, i.e., the mangled
32 **                  name.  E.g., for a class `A' with field `a' this
33 **                  is the ident for `A_a'.
34 **  type *type      The type of this entity, e.g., a method type, a
35 **                  basic type of the language or a class itself.
36 **  type *owner;    The class this entity belongs to.  In case of local
37 **                  variables the method they are defined in.
38 **  int offset;     Offset in byte for this entity.  Fixed when layout
39 **                  of owner is determined.
40 **  ir_graph *irg;  If (type == method_type) this is the corresponding irg.
41 **                  The ir_graph constructor automatically sets this field.
42 **                  If (type != method_type) access of this field will cause
43 **                  an assertion.
44 */
45
46 /* $Id$ */
47
48 # ifndef _ENTITY_H_
49 # define _ENTITY_H_
50
51 # include "ident.h"
52 # include "type.h"
53 # include "dbginfo.h"
54
55 /*******************************************************************/
56 /** general                                                       **/
57 /*******************************************************************/
58
59 /* initalize entity module */
60 void init_entity (void);
61
62 /*******************************************************************/
63 /** ENTITY                                                        **/
64 /*******************************************************************/
65
66 /* to resolve recursion between entity.h and irgraph.h */
67 #ifndef _IR_GRAPH_TYPEDEF_
68 #define _IR_GRAPH_TYPEDEF_
69 typedef struct ir_graph ir_graph;
70 #endif
71
72 /****s* entity/entity
73  *
74  * NAME
75  *   entity - An abstract data type to represent program entites.
76  * NOTE
77  *
78  * ATTRIBUTES
79  *   owner      A compound type this entity is a part of.
80  *   type       The type of this entity.
81  *   name       The string that represents this entity in the source program.
82  *   allocation A flag saying whether the entity is dynamically or statically
83  *              allocated (values: dynamic_allocated,  static_allocated,
84  *              automatic_allocated).
85  *   visibility A flag indicating the visibility of this entity (values: local,
86  *              external_visible,  external_allocated)
87  *   variability A flag indicating the variability of this entity (values:
88  *              uninitialized, initalized, part_constant, constant)
89  *   volatility @@@
90  *   offset     The offset of the entity within the compound object.  Only set
91  *              if the owner in the state "layout_fixed".
92  *   overwrites A list of entities overwritten by this entity.  This list is only
93  *              existent if the owner of this entity is a class.  The members in
94  *              this list must be entities of super classes.
95  *   overwrittenby A list of entities that overwrite this entity.  This list is only
96  *              existent if the owner of this entity is a class.  The members in
97  *              this list must be entities of sub classes.
98  *   link       A void* to associate some additional information with the entity.
99  *   irg        If the entity is a method this is the ir graph that represents the
100  *              code of the method.
101  *   peculiarity The peculiarity of the entity.  If the entity is a method this
102  *              indicates whether the entity represents
103  *              a real method or whether it only exists to describe an interface.
104  *              In that case there nowhere exists code for this entity and this entity
105  *              is never dynamically used in the code.
106  *              Values: description, existent.  Default: existent.
107  *   visited   visited flag.  Master flag is type_visited.
108  *
109  *  These fields can only be accessed via access functions.
110  *
111  * SEE ALSO
112  *   type
113  * SOURCE
114  */
115
116 /* to resolve recursion between entity.h and type.h */
117 #ifndef _ENTITY_TYPEDEF_
118 #define _ENTITY_TYPEDEF_
119 typedef struct entity entity;
120 #endif
121
122
123 /* Creates a new entity.
124    Automatically inserts the entity as a member of owner.
125    Entity is automatic_allocated and uninitialize except if the type
126    is type_method, then it is static_allocated and constant.  The constant
127    value is a pointer to the method.
128    Visibility is local, offset -1, and it is not volatile. */
129 entity     *new_entity (type *owner, ident *name, type *tp);
130 entity     *new_d_entity (type *owner, ident *name, type *tp, dbg_info *db);
131 /* Copies the entity if the new_owner is different from the
132    owner of the old entity.  Else returns the old entity.
133    Automatically inserts the new entity as a member of owner. */
134 entity     *copy_entity_own (entity *old, type *new_owner);
135 /* Copies the entity if the new_name is different from the
136    name of the old entity.  Else returns the old entity.
137    Automatically inserts the new entity as a member of owner.
138    The mangled name ld_name is set to NULL. */
139 entity     *copy_entity_name (entity *old, ident *new_name);
140 /* Frees the entity.  The owner will still contain the pointer to this
141    entity, as well as all other references! */
142 void        free_entity (entity *ent);
143
144 /** manipulate fields of entity **/
145 const char *get_entity_name     (entity *ent);
146 ident      *get_entity_ident    (entity *ent);
147
148 /* returns the mangled name of the entity.  If the mangled name is
149    set it returns the existing name.  Else it generates a name
150    with mangle_entity() and remembers this new name internally. */
151 ident      *get_entity_ld_ident (entity *ent);
152 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
153 const char *get_entity_ld_name (entity *end);
154
155 type       *get_entity_owner (entity *ent);
156 /* Sets the owner field in entity to owner.  Don't forget to add
157    ent to owner!! */
158 void        set_entity_owner (entity *ent, type *owner);
159 INLINE void assert_legal_owner_of_ent(type *owner);
160
161 type     *get_entity_type (entity *ent);
162 void      set_entity_type (entity *ent, type *tp);
163
164 typedef enum {
165   automatic_allocated, /* The entity is allocated during runtime, implicitly
166                           as component of a compound type.   This is the default. */
167   parameter_allocated, /* The entity is a parameter.  It is also automatic allocated.
168                           We distinguish the allocation of paramters from the allocation
169                           of local variables as their placement depends on the calling
170                           conventions. */
171   dynamic_allocated,   /* The entity is allocated during runtime, explicitly
172                           by an Alloc node. */
173   static_allocated     /* The entity is allocated statically.  We can use a
174                           SymConst(?) as address of the entity. */
175 } ent_allocation;
176
177 ent_allocation get_entity_allocation (entity *ent);
178 void           set_entity_allocation (entity *ent, ent_allocation al);
179
180 /* This enumeration flags the visibility of entities.  This is necessary
181    for partial compilation. */
182 typedef enum {
183   local,              /* The entity is only visible locally.  This is the default. */
184   external_visible,   /* The entity is visible to other external program parts, but
185                          it is defined here.  It may not be optimized away.  The entity must
186                          be static_allocated. */
187   external_allocated  /* The entity is defined and allocated externally.  This compilation
188                          must not allocate memory for this entity. The entity must
189                          be static_allocated.  This can also be an external defined
190                          method. */
191 } ent_visibility;
192
193 ent_visibility get_entity_visibility (entity *ent);
194 void           set_entity_visibility (entity *ent, ent_visibility vis);
195
196 /* This enumeration flags the variability of entities. */
197 typedef enum {
198   uninitialized,    /* The content of the entity is completely unknown. */
199   initialized,      /* After allocation the entity is initalized with the
200                        value given somewhere in the entity. */
201   part_constant,    /* For entities of compound types.  Some members of the entity
202                        are constant.  The others are uninitialized.  Those members
203                        given a value for are constant. */
204   constant          /* The entity is constant. */
205 } ent_variability;
206
207 ent_variability get_entity_variability (entity *ent);
208 void            set_entity_variability (entity *ent, ent_variability var);
209
210 /* This enumeration flags the volatility of entities. */
211 typedef enum {
212   non_volatile,    /* The entity is not volatile */
213   is_volatile      /* The entity is volatile */
214 } ent_volatility;
215
216 ent_volatility get_entity_volatility (entity *ent);
217 void           set_entity_volatility (entity *ent, ent_volatility vol);
218
219 /* Only set if layout = fixed. */
220 int       get_entity_offset (entity *ent);
221 void      set_entity_offset (entity *ent, int offset);
222
223 /* A link to store intermediate information */
224 void*   get_entity_link(entity *ent);
225 void    set_entity_link(entity *ent, void *l);
226
227 /** Fields of method entities **/
228 /* The entity knows the corresponding irg if the entity is a method.
229    This allows to get from a Call to the called irg.
230    Only entities of peculiarity "existent" can have a corresponding irg,
231    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
232 ir_graph *get_entity_irg(entity *ent);
233 void      set_entity_irg(entity *ent, ir_graph *irg);
234
235 /* For the definition of enumeration peculiarity see type.h */
236 peculiarity get_entity_peculiarity (entity *ent);
237 void        set_entity_peculiarity (entity *ent, peculiarity pec);
238
239 /** Representation of constant values of entites **/
240 /* Set current_ir_graph to get_const_code_irg() to generate a constant
241    expression. */
242 /* Copies a firm subgraph that complies to the restrictions for
243    constant expressions to current_block in current_ir_graph. */
244 ir_node *copy_const_value(ir_node *n);
245
246 /* Set has no effect for entities of type method. */
247 ir_node *get_atomic_ent_value(entity *ent);
248 void     set_atomic_ent_value(entity *ent, ir_node *val);
249
250 /* A value of a compound entity is a pair of a value and the corresponding
251    member of the compound. */
252 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
253 int      get_compound_ent_n_values(entity *ent);
254 ir_node *get_compound_ent_value(entity *ent, int pos);
255 entity  *get_compound_ent_value_member(entity *ent, int pos);
256 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
257 void     remove_compound_ent_value(entity *ent, entity *value_ent);
258 /* Inits the entity ent witch must be of a one dimensional
259    array type with the values given in the values array.
260    The array must have a lower and an upper bound.  Keeps the
261    order of values. Does not test whether the number of values
262    fits into the given array size.  Does not test whether the
263    values have the proper mode for the array. */
264 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
265
266 /** Fields of entities with a class type as owner **/
267 /* Overwrites is a field that specifies that an access to the overwritten
268    entity in the supertype must use this entity.  It's a list as with
269    multiple inheritance several enitites can be overwritten.  This field
270    is mostly useful for method entities.
271    If a Sel node selects an entity that is overwritten by other entities it
272    must return a pointer to the entity of the dynamic type of the pointer
273    that is passed to it.  Lowering of the Sel node must assure this.
274    Overwrittenby is the inverse of overwrites.  Both add routines add
275    both relations, they only differ in the order of arguments. */
276 void    add_entity_overwrites   (entity *ent, entity *overwritten);
277 int     get_entity_n_overwrites (entity *ent);
278 entity *get_entity_overwrites   (entity *ent, int pos);
279 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
280
281 void    add_entity_overwrittenby   (entity *ent, entity *overwrites);
282 int     get_entity_n_overwrittenby (entity *ent);
283 entity *get_entity_overwrittenby   (entity *ent, int pos);
284 void    set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites);
285
286 /* Returns true if the type of the entity is a primitive, pointer
287    enumeration or method type. */
288 int is_atomic_entity(entity *ent);
289 /* Returns true if the type of the entity is a class, structure,
290    array or union type. */
291 int is_compound_entity(entity *ent);
292
293 /* Returns true if ent1 and ent2 have are equal except for their owner.
294    Two entities are equal if
295     - they have the same type (the same C-struct)
296     - ...?
297 */
298 bool equal_entity(entity *ent1, entity *ent2);
299
300
301 unsigned long get_entity_visited(entity *ent);
302 void        set_entity_visited(entity *ent, unsigned long num);
303 /* Sets visited field in entity to entity_visited. */
304 void        mark_entity_visited(entity *ent);
305
306
307 /*****/
308
309 # endif /* _ENTITY_H_ */