Bugfix
[libfirm] / ir / tr / entity.h
1 /*
2 *  Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
3 *  All rights reserved.
4 *
5 */
6
7 /**
8 *  @file entity.h
9 *
10 *  Entities represent all program known objects.
11 *
12 *  @author Martin Trapp, Christian Schaefer
13 *  @author Goetz Lindenmaier
14 *
15 *  An entity is the representation of program known objects in Firm.
16 *  The primary concept of entities is to represent members of complex
17 *  types, i.e., fields and methods of classes.  As not all programming
18 *  language model all variables and methods as members of some class,
19 *  the concept of entities is extended to cover also local and global
20 *  variables, and arbitrary procedures.
21 *
22 *  An entity always specifies the type of the object it represents and
23 *  the type of the object it is a part of, the owner of the entity.
24 *  Originally this is the type of the class of which the entity is a
25 *  member.
26 *  The owner of local variables is the procedure they are defined in.
27 *  The owner of global variables and procedures visible in the whole
28 *  program is a universally defined class type "GlobalType".  The owner
29 *  of procedures defined in the scope of an other procedure is the
30 *  enclosing procedure.
31 *
32 *  In detail the datastructure entity has the following fields:
33 *
34 *  - ident *name:    Name of this entity as specified in the source code.
35 *                    Only unequivocal in conjuction with scope.
36 *  - ident *ld_name: Unique name of this entity, i.e., the mangled
37 *                    name.  E.g., for a class `A' with field `a' this
38 *                    is the ident for `A_a'.
39 *  - type *type:     The type of this entity, e.g., a method type, a
40 *                    basic type of the language or a class itself.
41 *  - type *owner:    The class this entity belongs to.  In case of local
42 *                    variables the method they are defined in.
43 *  - int offset:     Offset in byte for this entity.  Fixed when layout
44 *                    of owner is determined.
45 *  - ir_graph *irg:  If (type == method_type) this is the corresponding irg.
46 *                    The ir_graph constructor automatically sets this field.
47 *                    If (type != method_type) access of this field will cause
48 *                    an assertion.
49 */
50
51 /* $Id$ */
52
53 # ifndef _ENTITY_H_
54 # define _ENTITY_H_
55
56 # include "ident.h"
57 # include "type.h"
58 # include "dbginfo.h"
59
60 /*-----------------------------------------------------------------*/
61 /* general                                                         */
62 /*-----------------------------------------------------------------*/
63
64 /** Initalize entity module. */
65 void init_entity (void);
66
67 /*-----------------------------------------------------------------*/
68 /* ENTITY                                                          */
69 /*-----------------------------------------------------------------*/
70
71 /* to resolve recursion between entity.h and irgraph.h */
72 #ifndef _IR_GRAPH_TYPEDEF_
73 #define _IR_GRAPH_TYPEDEF_
74 typedef struct ir_graph ir_graph;
75 #endif
76
77 /**
78  *
79  *   An abstract data type to represent program entites.
80  *
81  *   @param owner      A compound type this entity is a part of.
82  *   @param type       The type of this entity.
83  *   @param name       The string that represents this entity in the source program.
84  *   @param allocation A flag saying whether the entity is dynamically or statically
85  *              allocated (values: dynamic_allocated,  static_allocated,
86  *              automatic_allocated).
87  *   @param visibility A flag indicating the visibility of this entity (values: local,
88  *              external_visible,  external_allocated)
89  *   @param variability A flag indicating the variability of this entity (values:
90  *              uninitialized, initalized, part_constant, constant)
91  *   @param volatility @@@
92  *   @param offset     The offset of the entity within the compound object.  Only set
93  *              if the owner in the state "layout_fixed".
94  *   @param overwrites A list of entities overwritten by this entity.  This list is only
95  *              existent if the owner of this entity is a class.  The members in
96  *              this list must be entities of super classes.
97  *   @param overwrittenby A list of entities that overwrite this entity.  This list is only
98  *              existent if the owner of this entity is a class.  The members in
99  *              this list must be entities of sub classes.
100  *   @param link       A void* to associate some additional information with the entity.
101  *   @param irg        If the entity is a method this is the ir graph that represents the
102  *              code of the method.
103  *   @param peculiarity The peculiarity of the entity.  If the entity is a method this
104  *              indicates whether the entity represents
105  *              a real method or whether it only exists to describe an interface.
106  *              In that case there nowhere exists code for this entity and this entity
107  *              is never dynamically used in the code.
108  *              Values: description, existent.  Default: existent.
109  *   @param visited   visited flag.  Master flag is type_visited.
110  *
111  *  @param These fields can only be accessed via access functions.
112  *
113  * @see  type
114  */
115
116 /* to resolve recursion between entity.h and type.h */
117 /** the type of an entity */
118 #ifndef _ENTITY_TYPEDEF_
119 #define _ENTITY_TYPEDEF_
120 typedef struct entity entity;
121 #endif
122
123 /**
124  * Creates a new entity.
125  *
126  * Automatically inserts the entity as a member of owner.
127  * Entity is automatic_allocated and uninitialize except if the type
128  * is type_method, then it is static_allocated and constant.  The constant
129  * value is a pointer to the method.
130  * Visibility is local, offset -1, and it is not volatile.
131  */
132 entity     *new_entity (type *owner, ident *name, type *tp);
133
134 /**
135  * Creates a new entity.
136  *
137  * Automatically inserts the entity as a member of owner.
138  * Entity is automatic_allocated and uninitialize except if the type
139  * is type_method, then it is static_allocated and constant.  The constant
140  * value is a pointer to the method.
141  * Visibility is local, offset -1, and it is not volatile.
142  */
143 entity     *new_d_entity (type *owner, ident *name, type *tp, dbg_info *db);
144
145 /**
146  * Copies the entity if the new_owner is different from the
147  * owner of the old entity,  else returns the old entity.
148  *
149  * Automatically inserts the new entity as a member of owner.
150  * Resets the overwrites/overwritten_by fields.
151  */
152 entity     *copy_entity_own (entity *old, type *new_owner);
153
154 /**
155  * Copies the entity if the new_name is different from the
156  * name of the old entity, else returns the old entity.
157  *
158  * Automatically inserts the new entity as a member of owner.
159  * The mangled name ld_name is set to NULL.
160  */
161 entity     *copy_entity_name (entity *old, ident *new_name);
162
163 /**
164  * Frees the entity.
165  *
166  * The owner will still contain the pointer to this
167  * entity, as well as all other references!
168  */
169 void        free_entity (entity *ent);
170
171 /** Returns the name of an entity. */
172 const char *get_entity_name     (entity *ent);
173
174 /** Returns the ident of an entity. */
175 ident      *get_entity_ident    (entity *ent);
176
177 /** Returns the mangled name of the entity.
178  *
179  * If the mangled name is set it returns the existing name.
180  * Else it generates a name with mangle_entity()
181  * and remembers this new name internally.
182  */
183 ident      *get_entity_ld_ident (entity *ent);
184
185 /** Sets the mangled name of the entity. */
186 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
187
188 /** Returns the mangled name of the entity as a string. */
189 const char *get_entity_ld_name (entity *end);
190
191 /** Returns the owner of the entity. */
192 type       *get_entity_owner (entity *ent);
193
194 /** Sets the owner field in entity to owner.  Don't forget to add
195    ent to owner!! */
196 void        set_entity_owner (entity *ent, type *owner);
197
198 /** Asserts if the type owner is neither a compound type or an array */
199 INLINE void assert_legal_owner_of_ent(type *owner);
200
201 /** Returns the type of an entity. */
202 type     *get_entity_type (entity *ent);
203
204 /** Sets the type of an entity. */
205 void      set_entity_type (entity *ent, type *tp);
206
207 /** The allocation type. */
208 typedef enum {
209   automatic_allocated, /**< The entity is allocated during runtime, implicitly
210                           as component of a compound type.   This is the default. */
211   parameter_allocated, /**< The entity is a parameter.  It is also automatic allocated.
212                           We distinguish the allocation of paramters from the allocation
213                           of local variables as their placement depends on the calling
214                           conventions. */
215   dynamic_allocated,   /**< The entity is allocated during runtime, explicitly
216                           by an Alloc node. */
217   static_allocated     /**< The entity is allocated statically.  We can use a
218                           SymConst(?) as address of the entity. */
219 } ent_allocation;
220
221 /** Returns the allocation type of an entity. */
222 ent_allocation get_entity_allocation (entity *ent);
223
224 /** Sets the allocation type of an entity. */
225 void           set_entity_allocation (entity *ent, ent_allocation al);
226
227 /** Return the name of the visibility */
228 const char *get_allocation_name(ent_allocation vis);
229
230 /**
231  * This enumeration flags the visibility of entities.  This is necessary
232  * for partial compilation.
233  */
234 typedef enum {
235   local,              /**< The entity is only visible locally.  This is the default. */
236   external_visible,   /**< The entity is visible to other external program parts, but
237                          it is defined here.  It may not be optimized away.  The entity must
238                          be static_allocated. */
239   external_allocated  /**< The entity is defined and allocated externally.  This compilation
240                          must not allocate memory for this entity. The entity must
241                          be static_allocated.  This can also be an external defined
242                          method. */
243 } ent_visibility;
244
245 /** Returns the visibility of an entity. */
246 ent_visibility get_entity_visibility (entity *ent);
247
248 /** Sets the visibility of an entity. */
249 void           set_entity_visibility (entity *ent, ent_visibility vis);
250
251 /** Return the name of the visibility */
252 const char *get_visibility_name(ent_visibility vis);
253
254 /** This enumeration flags the variability of entities. */
255 typedef enum {
256   uninitialized,    /**< The content of the entity is completely unknown. */
257   initialized,      /**< After allocation the entity is initalized with the
258                        value given somewhere in the entity. */
259   part_constant,    /**< For entities of compound types.  Some members of the entity
260                        are constant.  The others are uninitialized.  Those members
261                        given a value for are constant. */
262   constant          /**< The entity is constant. */
263 } ent_variability;
264
265 /** Returns the variability of an entity. */
266 ent_variability get_entity_variability (entity *ent);
267
268 /** Sets the variability of an entity. */
269 void            set_entity_variability (entity *ent, ent_variability var);
270
271 /** Return the name of the variablity. */
272 const char *get_variability_name(ent_variability var);
273
274 /** This enumeration flags the volatility of entities. */
275 typedef enum {
276   non_volatile,    /**< The entity is not volatile */
277   is_volatile      /**< The entity is volatile */
278 } ent_volatility;
279
280 /** Returns the volatility of an entity. */
281 ent_volatility get_entity_volatility (entity *ent);
282
283 /** Sets the volatility of an entity. */
284 void           set_entity_volatility (entity *ent, ent_volatility vol);
285
286 /* Return the name of the volatility. */
287 const char *get_volatility_name(ent_volatility var);
288
289 /** Returns the offset of an entity (in a compound). Only set if layout = fixed. */
290 int       get_entity_offset (entity *ent);
291
292 /** Sets the offset of an entity (in a compound). */
293 void      set_entity_offset (entity *ent, int offset);
294
295 /** Returns the stored intermediate information. */
296 void*   get_entity_link(entity *ent);
297
298 /** Stores new intermediate information. */
299 void    set_entity_link(entity *ent, void *l);
300
301 /* -- Fields of method entities -- */
302 /* The entity knows the corresponding irg if the entity is a method.
303    This allows to get from a Call to the called irg.
304    Only entities of peculiarity "existent" can have a corresponding irg,
305    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
306 ir_graph *get_entity_irg(entity *ent);
307 void      set_entity_irg(entity *ent, ir_graph *irg);
308
309 /** Return the peculiarity of an entity. */
310 peculiarity get_entity_peculiarity (entity *ent);
311
312 /** Sets the peculiarity of an entity. */
313 void        set_entity_peculiarity (entity *ent, peculiarity pec);
314
315 /** Return the name of the peculiarity. */
316 const char *get_peculiarity_name(peculiarity var);
317
318 /* -- Representation of constant values of entites -- */
319 /* Set current_ir_graph to get_const_code_irg() to generate a constant
320    expression. */
321 /* Copies a firm subgraph that complies to the restrictions for
322    constant expressions to current_block in current_ir_graph. */
323 ir_node *copy_const_value(ir_node *n);
324
325 /* Set has no effect for existent entities of type method. */
326 ir_node *get_atomic_ent_value(entity *ent);
327 void     set_atomic_ent_value(entity *ent, ir_node *val);
328
329 /* The following type describes a path to a leave in the compound graph.
330    Node 0 in the path must be an entity of type tp given in the constructor.  If
331    the type of this element is compound, the path node 1 is an element of the type
332    of node 0 an so forth, until an entity of atomic type is reached. */
333 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
334 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
335 typedef struct compound_graph_path compound_graph_path;
336 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
337 compound_graph_path *new_compound_graph_path(type *tp, int length);
338 int     is_compound_graph_path(void *thing);
339 void    free_compound_graph_path (compound_graph_path *gr);
340 int     get_compound_graph_path_length(compound_graph_path *gr);
341 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
342 void    set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node);
343
344 /* A value of a compound entity is a pair of a value and the description of the
345    corresponding access path to the member of the compound.  */
346 void     add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path);
347 void     set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos);
348 int      get_compound_ent_n_values(entity *ent);
349 ir_node *get_compound_ent_value(entity *ent, int pos);
350 compound_graph_path *get_compound_ent_value_path(entity *ent, int pos);
351 /* Removes all constant entries where the path ends at value_ent. Does not
352    free the memory of the paths.  (The same path might be used for several
353    constant entities. */
354 void     remove_compound_ent_value(entity *ent, entity *value_ent);
355
356 /* Some languages support only trivial access paths, i.e., the member is a
357    direct, atomic member of the constant entities type. In this case the
358    corresponding entity can be accessed directly.  The following functions
359    allow direct access. */
360 /* generates a Path with length 1 */
361 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
362 /* Returns the last member in the path */
363 entity  *get_compound_ent_value_member(entity *ent, int pos);
364 /* Sets the path at pos 0 */
365 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
366
367
368
369 /** Inits the entity ent witch must be of a one dimensional
370    array type with the values given in the values array.
371    The array must have a lower and an upper bound.  Keeps the
372    order of values. Does not test whether the number of values
373    fits into the given array size.  Does not test whether the
374    values have the proper mode for the array. */
375 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
376
377 /* --- Fields of entities with a class type as owner --- */
378 /* Overwrites is a field that specifies that an access to the overwritten
379    entity in the supertype must use this entity.  It's a list as with
380    multiple inheritance several enitites can be overwritten.  This field
381    is mostly useful for method entities.
382    If a Sel node selects an entity that is overwritten by other entities it
383    must return a pointer to the entity of the dynamic type of the pointer
384    that is passed to it.  Lowering of the Sel node must assure this.
385    Overwrittenby is the inverse of overwrites.  Both add routines add
386    both relations, they only differ in the order of arguments. */
387 void    add_entity_overwrites   (entity *ent, entity *overwritten);
388 int     get_entity_n_overwrites (entity *ent);
389 int     get_entity_overwrites_index(entity *ent, entity *overwritten);
390 entity *get_entity_overwrites   (entity *ent, int pos);
391 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
392 void    remove_entity_overwrites(entity *ent, entity *overwritten);
393
394 void    add_entity_overwrittenby   (entity *ent, entity *overwrites);
395 int     get_entity_n_overwrittenby (entity *ent);
396 int     get_entity_overwrittenby_index(entity *ent, entity *overwrites);
397 entity *get_entity_overwrittenby   (entity *ent, int pos);
398 void    set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites);
399 void    remove_entity_overwrittenby(entity *ent, entity *overwrites);
400
401 /**
402  *   Checks whether a pointer points to an entity.
403  *
404  *   @param thing     an arbitrary pointer
405  *
406  *   @return
407  *       true if the thing is an entity, else false
408  */
409 int is_entity (void *thing);
410
411 /** Returns true if the type of the entity is a primitive, pointer
412    enumeration or method type. */
413 int is_atomic_entity(entity *ent);
414 /** Returns true if the type of the entity is a class, structure,
415    array or union type. */
416 int is_compound_entity(entity *ent);
417
418 /** Returns true if ent1 and ent2 have are equal except for their owner.
419    Two entities are equal if
420     - they have the same type (the same C-struct)
421     - ...?
422 */
423 bool equal_entity(entity *ent1, entity *ent2);
424
425
426 /** Outputs a unique number for this entity if libfirm is compiled for
427    debugging, (configure with --enable-debug) else returns 0. */
428 INLINE long get_entity_nr(entity *ent);
429
430 /** Returns the entitys visited count. */
431 unsigned long get_entity_visited(entity *ent);
432
433 /** Sets the entitys visited count. */
434 void        set_entity_visited(entity *ent, unsigned long num);
435
436 /** Sets visited field in entity to entity_visited. */
437 void        mark_entity_visited(entity *ent);
438
439 /** Returns true if this entity was visited. */
440 bool        entity_visited(entity *ent);
441
442 /** Returns true if this entity was not visited. */
443 bool        entity_not_visited(entity *ent);
444
445 # endif /* _ENTITY_H_ */