eb8e89f95ac700462e6cf8bb2d36b5161abb767c
[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
54 /*******************************************************************/
55 /** general                                                       **/
56 /*******************************************************************/
57
58 /* initalize entity module */
59 void init_entity (void);
60
61 /*******************************************************************/
62 /** ENTITY                                                        **/
63 /*******************************************************************/
64
65 #ifndef _IR_GRAPH_TYPEDEF_
66 #define _IR_GRAPH_TYPEDEF_
67 /* to resolve recursion between entity.h and irgraph.h */
68 typedef struct ir_graph ir_graph;
69 #endif
70
71 /****s* entity/entity
72  *
73  * NAME
74  *   entity - An abstract data type to represent program entites.
75  * NOTE
76  *
77  * ATTRIBUTES
78  *   owner      A compound type this entity is a part of.
79  *   type       The type of this entity.
80  *   name       The string that represents this entity in the source program.
81  *   allocation A flag saying whether the entity is dynamically or statically
82  *              allocated (values: dynamic_allocated,  static_allocated).
83  *              @@@ Does this make sense???
84  *   visibility A flag indicating the visibility of this entity (values: local,
85  *              external_visible,  external_allocated)
86  *   variability A flag indicating the variability of this entity (values:
87  *              uninitialized, initalized, part_constant, constant)
88  *   offset     The offset of the entity within the compound object.  Only set
89  *              if IR in the state "@@@"  Wie nennen wir den??
90  *   overwrites A list of entities overwritten by this entity.  This list is only
91  *              existent if the owner of this entity is a class.  The members in
92  *              this list must be entities of super classes.
93  *   link       A void* to associate some additional inforamtion with the entity.
94  *   irg        If the entity is a method this is the ir graph that represents the
95  *              code of the method.
96  *
97  *
98  *  These fields can only be accessed via access functions.
99  *
100  * SEE ALSO
101  *   type
102  * SOURCE
103  */
104
105 #ifndef _ENTITY_TYPEDEF_
106 #define _ENTITY_TYPEDEF_
107 /* to resolve recursion between entity.h and type.h */
108 typedef struct entity entity;
109 #endif
110
111 /* Creates a new entity.
112    Automatically inserts the entity as a member of owner.
113    Entity is automatic_allocated and uninitialize except if the type
114    is type_method, then it is static_allocated and constant.  The constant
115    value is a pointer to the method.
116    Visibility is local, offset -1, and it is not volatile. */
117 entity     *new_entity (type *owner, ident *name, type *type);
118 /* Copies the entity if the new_owner is different from the
119    owner of the old entity.  Else returns the old entity.
120    Automatically inserts the new entity as a member of owner. */
121 entity     *copy_entity_own (entity *old, type *new_owner);
122 /* Copies the entity if the new_name is different from the
123    name of the old entity.  Else returns the old entity.
124    Automatically inserts the new entity as a member of owner.
125    The mangled name ld_name is set to NULL. */
126 entity     *copy_entity_name (entity *old, ident *new_name);
127
128 /** manipulate fields of entity **/
129 const char *get_entity_name     (entity *ent);
130 ident      *get_entity_ident    (entity *ent);
131 /* returns the mangled name of the entity.  If the mangled name is
132    set it returns the existing name.  Else it generates a name
133    with mangle_entity() and remembers this new name internally. */
134 ident      *get_entity_ld_ident (entity *ent);
135 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
136 const char *get_entity_ld_name (entity *end);
137
138 /*
139 char       *get_entity_ld_name  (entity *ent);
140 void        set_entity_ld_name  (entity *ent, char *ld_name);
141 */
142
143 type       *get_entity_owner (entity *ent);
144 /* Sets the owner field in entity to owner. */
145 void        set_entity_owner (entity *ent, type *owner);
146 inline void assert_legal_owner_of_ent(type *owner);
147
148 type     *get_entity_type (entity *ent);
149 void      set_entity_type (entity *ent, type *type);
150
151 typedef enum {
152   automatic_allocated,/* The entity is allocated during runtime, implicitly
153                          as component of a compound type.   This is the default. */
154   dynamic_allocated,  /* The entity is allocated during runtime, explicitly
155                          by an Alloc node. */
156   static_allocated    /* The entity is allocated statically.  We can use a
157                          SymConst(?) as address of the entity. */
158 } ent_allocation;
159
160 ent_allocation get_entity_allocation (entity *ent);
161 void           set_entity_allocation (entity *ent, ent_allocation al);
162
163 /* This enumeration flags the visibility of entities.  This is necessary
164    for partial compilation. */
165 typedef enum {
166   local,              /* The entity is only visible locally.  This is the default. */
167   external_visible,   /* The entity is visible to other external program parts, but
168                          it is defined here.  It may not be optimized away.  The entity must
169                          be static_allocated. */
170   external_allocated  /* The entity is defined and allocated externaly.  This compilation
171                          must not allocate memory for this entity. The entity must
172                          be static_allocated. */
173 } ent_visibility;
174
175 ent_visibility get_entity_visibility (entity *ent);
176 void           set_entity_visibility (entity *ent, ent_visibility vis);
177
178 /* This enumeration flags the variability of entities. */
179 typedef enum {
180   uninitialized,    /* The content of the entity is completely unknown. */
181   initialized,       /* After allocation the entity is initalized with the
182                        value given somewhere in the entity. */
183   part_constant,    /* For entities of compound types.  Some members of the entity
184                        are constant.  The others are uninitialized.  Those members
185                        given a value for are constant. */
186   constant          /* The entity is constant. */
187 } ent_variability;
188
189 ent_variability get_entity_variability (entity *ent);
190 void            set_entity_variability (entity *ent, ent_variability var);
191
192 /* This enumeration flags the volatility of entities. */
193 typedef enum {
194   non_volatile,    /* The entity is not volatile */
195   is_volatile      /* The entity is volatile */
196 } ent_volatility;
197
198 ent_volatility get_entity_volatility (entity *ent);
199 void           set_entity_volatility (entity *ent, ent_volatility vol);
200
201 /* Set has no effect for entities of type method. */
202 ir_node *get_atomic_ent_value(entity *ent);
203 void     set_atomic_ent_value(entity *ent, ir_node *val);
204 /* Copies the value represented by the entity to current_block
205    in current_ir_graph. */
206 ir_node *copy_atomic_ent_value(entity *ent);
207
208 /* A value of a compound entity is a pair of value and the corresponding
209    member of the compound. */
210 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
211 int      get_compound_ent_n_values(entity *ent);
212 ir_node *get_compound_ent_value(entity *ent, int pos);
213 entity  *get_compound_ent_value_member(entity *ent, int pos);
214 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
215 /* Copies the value pos of the entity to current_block in current_ir_graph. */
216 ir_node *copy_compound_ent_value(entity *ent, int pos);
217
218 /* Only set if layout = fixed. */
219 int       get_entity_offset (entity *ent);
220 void      set_entity_offset (entity *ent, int offset);
221
222 /* Overwrites is a field that specifies that an access to the overwritten
223    entity in the supertype must use this entity.  It's a list as with
224    multiple inheritance several enitites can be overwritten.  This field
225    is mostly useful for method entities.
226    If a Sel node selects an entity that is overwritten by other entities it
227    must return a pointer to the entity of the dynamic type of the pointer
228    that is passed to it.  Lowering of the Sel node must assure this. */
229 void    add_entity_overwrites   (entity *ent, entity *overwritten);
230 int     get_entity_n_overwrites (entity *ent);
231 entity *get_entity_overwrites   (entity *ent, int pos);
232 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
233 /* Do we need a second relation "overwritten"? */
234
235 /* A link to store intermediate information */
236 void*   get_entity_link(entity *ent);
237 void    set_entity_link(entity *ent, void *l);
238
239 /* The entity knows the corresponding irg if the entity is a method.
240    This allows to get from a Call to the called irg. */
241 ir_graph *get_entity_irg(entity *ent);
242 void      set_entity_irg(entity *ent, ir_graph *irg);
243
244
245 /* Returns true if the type of the entity is a primitive, pointer
246    enumeration or method type. */
247 int is_atomic_entity(entity *ent);
248 /* Returns true if the type of the entity is a class, structure,
249    array or union type. */
250 int is_compound_entity(entity *ent);
251
252
253 /*****/
254
255 # endif /* _ENTITY_H_ */