fixed doxygen comment
[libfirm] / ir / tr / entity.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity.h
4  * Purpose:     Representation of all program known entities.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14 *  @file entity.h
15 *
16 *  Entities represent all program known objects.
17 *
18 *  @author Martin Trapp, Christian Schaefer
19 *  @author Goetz Lindenmaier
20 *
21 *  An entity is the representation of program known objects in Firm.
22 *  The primary concept of entities is to represent members of complex
23 *  types, i.e., fields and methods of classes.  As not all programming
24 *  language model all variables and methods as members of some class,
25 *  the concept of entities is extended to cover also local and global
26 *  variables, and arbitrary procedures.
27 *
28 *  An entity always specifies the type of the object it represents and
29 *  the type of the object it is a part of, the owner of the entity.
30 *  Originally this is the type of the class of which the entity is a
31 *  member.
32 *  The owner of local variables is the procedure they are defined in.
33 *  The owner of global variables and procedures visible in the whole
34 *  program is a universally defined class type "GlobalType".  The owner
35 *  of procedures defined in the scope of an other procedure is the
36 *  enclosing procedure.
37 *
38 *  In detail the datastructure entity has the following fields:
39 *
40 *  - ident *name:    Name of this entity as specified in the source code.
41 *                    Only unequivocal in conjuction with scope.
42 *  - ident *ld_name: Unique name of this entity, i.e., the mangled
43 *                    name.  E.g., for a class `A' with field `a' this
44 *                    is the ident for `A_a'.
45 *  - type *type:     The type of this entity, e.g., a method type, a
46 *                    basic type of the language or a class itself.
47 *  - type *owner:    The class this entity belongs to.  In case of local
48 *                    variables the method they are defined in.
49 *  - int offset:     Offset in bits for this entity.  Fixed when layout
50 *                    of owner is determined.
51 *  - ir_graph *irg:  If (type == method_type) this is the corresponding irg.
52 *                    The ir_graph constructor automatically sets this field.
53 *                    If (type != method_type) access of this field will cause
54 *                    an assertion.
55 */
56
57 # ifndef _ENTITY_H_
58 # define _ENTITY_H_
59
60 # include "ident.h"
61 # include "type.h"
62 # include "dbginfo.h"
63
64 # include "tr_inheritance.h"
65
66 /*-----------------------------------------------------------------*/
67 /* general                                                         */
68 /*-----------------------------------------------------------------*/
69
70 /** Initalize entity module. */
71 void init_entity (void);
72
73 /*-----------------------------------------------------------------*/
74 /* ENTITY                                                          */
75 /*-----------------------------------------------------------------*/
76
77 /* to resolve recursion between entity.h and irgraph.h */
78 #ifndef _IR_GRAPH_TYPEDEF_
79 #define _IR_GRAPH_TYPEDEF_
80 typedef struct ir_graph ir_graph;
81 #endif
82
83 /**
84  *
85  *   An abstract data type to represent program entites.
86  *
87  *   @param owner      A compound type this entity is a part of.
88  *   @param type       The type of this entity.
89  *   @param name       The string that represents this entity in the source program.
90  *   @param allocation A flag saying whether the entity is dynamically or statically
91  *              allocated (values: dynamic_allocated,  static_allocated,
92  *              automatic_allocated).
93  *   @param visibility A flag indicating the visibility of this entity (values: local,
94  *              external_visible,  external_allocated)
95  *   @param variability A flag indicating the variability of this entity (values:
96  *              uninitialized, initalized, part_constant, constant)
97  *   @param volatility @@@
98  *   @param offset     The offset of the entity within the compound object in bits.  Only set
99  *              if the owner in the state "layout_fixed".
100  *   @param overwrites A list of entities overwritten by this entity.  This list is only
101  *              existent if the owner of this entity is a class.  The members in
102  *              this list must be entities of super classes.
103  *   @param overwrittenby A list of entities that overwrite this entity.  This list is only
104  *              existent if the owner of this entity is a class.  The members in
105  *              this list must be entities of sub classes.
106  *   @param link       A void* to associate some additional information with the entity.
107  *   @param irg        If the entity is a method this is the ir graph that represents the
108  *              code of the method.
109  *   @param peculiarity The peculiarity of the entity.  If the entity is a method this
110  *              indicates whether the entity represents
111  *              a real method or whether it only exists to describe an interface.
112  *              In that case there nowhere exists code for this entity and this entity
113  *              is never dynamically used in the code.
114  *              Values: description, existent.  Default: existent.
115  *   @param visited   visited flag.  Master flag is type_visited.
116  *
117  *  @param These fields can only be accessed via access functions.
118  *
119  * @see  type
120  */
121
122 /* to resolve recursion between entity.h and type.h */
123 /** the type of an entity */
124 #ifndef _ENTITY_TYPEDEF_
125 #define _ENTITY_TYPEDEF_
126 typedef struct entity entity;
127 #endif
128
129 /**
130  * Creates a new entity.
131  *
132  * Automatically inserts the entity as a member of owner.
133  * Entity is automatic_allocated and uninitialized except if the type
134  * is type_method, then it is static_allocated and constant.  The constant
135  * value is a pointer to the method.
136  * Visibility is local, offset -1, and it is not volatile.
137  */
138 entity     *new_entity (type *owner, ident *name, type *tp);
139
140 /**
141  * Creates a new entity.
142  *
143  * Automatically inserts the entity as a member of owner.
144  * The entity is automatic allocated and uninitialized except if the type
145  * is type_method, then it is static allocated and constant.  The constant
146  * value is a pointer to the method.
147  * Visibility is local, offset -1, and it is not volatile.
148  */
149 entity     *new_d_entity (type *owner, ident *name, type *tp, dbg_info *db);
150
151 /**
152  * Copies the entity if the new_owner is different from the
153  * owner of the old entity,  else returns the old entity.
154  *
155  * Automatically inserts the new entity as a member of owner.
156  * Resets the overwrites/overwritten_by fields.
157  * Keeps the old atomic value.
158  *   @@@ Maybe we should change this.  If peculiarity of a method
159  *       is existent, we should add a new SymConst that points to
160  *       itself and not to the origin.  Right now we have to change
161  *       the peculiarity and then set a new atomic value by hand.
162  */
163 entity     *copy_entity_own (entity *old, type *new_owner);
164
165 /**
166  * Copies the entity if the new_name is different from the
167  * name of the old entity, else returns the old entity.
168  *
169  * Automatically inserts the new entity as a member of owner.
170  * The mangled name ld_name is set to NULL.
171  * Overwrites relation is copied from old.
172  */
173 entity     *copy_entity_name (entity *old, ident *new_name);
174
175 /**
176  * Frees the entity.
177  *
178  * The owner will still contain the pointer to this
179  * entity, as well as all other references!
180  */
181 void        free_entity (entity *ent);
182
183 /** Returns the name of an entity. */
184 const char *get_entity_name     (const entity *ent);
185
186 /** Returns the ident of an entity. */
187 ident      *get_entity_ident    (const entity *ent);
188
189 /** Returns the mangled name of the entity.
190  *
191  * If the mangled name is set it returns the existing name.
192  * Else it generates a name with mangle_entity()
193  * and remembers this new name internally.
194  */
195 ident      *get_entity_ld_ident (entity *ent);
196
197 /** Sets the mangled name of the entity. */
198 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
199
200 /** Returns the mangled name of the entity as a string. */
201 const char *get_entity_ld_name (entity *ent);
202
203 /** Returns the owner of the entity. */
204 type       *get_entity_owner (entity *ent);
205
206 /** Sets the owner field in entity to owner.  Don't forget to add
207    ent to owner!! */
208 void        set_entity_owner (entity *ent, type *owner);
209
210 /** Asserts if the type owner is either a compound type or an array */
211 void assert_legal_owner_of_ent(type *owner);
212
213 /** Returns the type of an entity. */
214 type     *get_entity_type (entity *ent);
215
216 /** Sets the type of an entity. */
217 void      set_entity_type (entity *ent, type *tp);
218
219 /** The allocation type. */
220 typedef enum {
221   allocation_automatic, /**< The entity is allocated during runtime, implicitly
222                  as component of a compound type.   This is the default. */
223   allocation_parameter, /**< The entity is a parameter.  It is also automatic allocated.
224                  We distinguish the allocation of parameters from the allocation
225                  of local variables as their placement depends on the calling
226                  conventions. */
227   allocation_dynamic,   /**< The entity is allocated during runtime, explicitly
228                  by an Alloc node. */
229   allocation_static     /**< The entity is allocated statically.  We can use a
230                              Const as address of the entity. */
231 } ent_allocation;
232
233 /** Returns the allocation type of an entity. */
234 ent_allocation get_entity_allocation (const entity *ent);
235
236 /** Sets the allocation type of an entity. */
237 void           set_entity_allocation (entity *ent, ent_allocation al);
238
239 /** Return the name of the allocation type. */
240 const char *get_allocation_name(ent_allocation vis);
241
242 /** Returns the visibility of an entity. */
243 visibility get_entity_visibility (const entity *ent);
244
245 /** Sets the visibility of an entity. */
246 void       set_entity_visibility (entity *ent, visibility vis);
247
248 /** Return the name of the visibility */
249 const char *get_visibility_name(visibility vis);
250
251 /** This enumeration flags the variability of entities. */
252 typedef enum {
253   variability_uninitialized,    /**< The content of the entity is completely unknown. */
254   variability_initialized,      /**< After allocation the entity is initialized with the
255                              value given somewhere in the entity. */
256   variability_part_constant,    /**< For entities of compound types.
257                                      The members of the entity are mixed constant,
258                                      initialized or uninitialized. */
259   variability_constant          /**< The entity is constant. */
260 } ent_variability;
261
262 /** Returns the variability of an entity. */
263 ent_variability get_entity_variability (const entity *ent);
264
265 /** Sets the variability of an entity. */
266 void            set_entity_variability (entity *ent, ent_variability var);
267
268 /** Return the name of the variability. */
269 const char *get_variability_name(ent_variability var);
270
271 /** This enumeration flags the volatility of entities. */
272 typedef enum {
273   volatility_non_volatile,    /**< The entity is not volatile */
274   volatility_is_volatile      /**< The entity is volatile */
275 } ent_volatility;
276
277 /** Returns the volatility of an entity. */
278 ent_volatility get_entity_volatility (const entity *ent);
279
280 /** Sets the volatility of an entity. */
281 void           set_entity_volatility (entity *ent, ent_volatility vol);
282
283 /** Return the name of the volatility. */
284 const char *get_volatility_name(ent_volatility var);
285
286 /** This enumeration flags the stickyness of an entity. */
287 typedef enum {
288   stickyness_unsticky,          /**< The entity can be removed from
289                                    the program, unless contraindicated
290                                    by other attributes */
291   stickyness_sticky             /**< The entity must remain in the
292                                    program in any case */
293 } ent_stickyness;
294
295 /** Get the entity's stickyness */
296 ent_stickyness get_entity_stickyness(const entity *ent);
297
298 /** Set the entity's stickyness */
299 void      set_entity_stickyness(entity *ent, ent_stickyness stickyness);
300
301 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
302 int       get_entity_offset_bytes(const entity *ent);
303
304 /** Returns the offset of an entity (in a compound) in bits. Only set if layout = fixed. */
305 int       get_entity_offset_bits(const entity *ent);
306
307 /** Sets the offset of an entity (in a compound) in bytes. */
308 void      set_entity_offset_bytes(entity *ent, int offset);
309
310 /** Sets the offset of an entity (in a compound) in bits. */
311 void      set_entity_offset_bits(entity *ent, int offset);
312
313 /** Returns the stored intermediate information. */
314 void*   get_entity_link(const entity *ent);
315
316 /** Stores new intermediate information. */
317 void    set_entity_link(entity *ent, void *l);
318
319 /* -- Fields of method entities -- */
320 /** The entity knows the corresponding irg if the entity is a method.
321    This allows to get from a Call to the called irg.
322    Only entities of peculiarity "existent" can have a corresponding irg,
323    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
324 ir_graph *get_entity_irg(const entity *ent);
325 void      set_entity_irg(entity *ent, ir_graph *irg);
326
327 /** Return the peculiarity of an entity. */
328 peculiarity get_entity_peculiarity (const entity *ent);
329
330 /** Sets the peculiarity of an entity. */
331 void        set_entity_peculiarity (entity *ent, peculiarity pec);
332
333 /** Return the name of the peculiarity. */
334 const char *get_peculiarity_name(peculiarity var);
335
336 /* -- Representation of constant values of entities -- */
337 /** Returns true if the the node is representable as code on
338  *  const_code_irg. */
339 int      is_irn_const_expression(ir_node *n);
340 /* Set current_ir_graph to get_const_code_irg() to generate a constant
341    expression. */
342
343 /**
344  * Copies a firm subgraph that complies to the restrictions for
345  * constant expressions to current_block in current_ir_graph.
346  */
347 ir_node *copy_const_value(ir_node *n);
348
349 /* Set has no effect for existent entities of type method. */
350 ir_node *get_atomic_ent_value(entity *ent);
351 void     set_atomic_ent_value(entity *ent, ir_node *val);
352
353 /**
354  * The following type describes a path to a leave in the compound graph.
355  * Node 0 in the path must be an entity of type tp given in the constructor.  If
356  * the type of this element is compound, the path node 1 is an element of the type
357  * of node 0 an so forth, until an entity of atomic type is reached.
358  */
359 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
360 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
361 typedef struct compound_graph_path compound_graph_path;
362 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
363
364 /** Creates a new compound graph path. */
365 compound_graph_path *new_compound_graph_path(type *tp, int length);
366
367 /** Returns non-zero if an object is a compound graph path */
368 int     is_compound_graph_path(void *thing);
369
370 /** Frees a graph path object */
371 void    free_compound_graph_path (compound_graph_path *gr);
372
373 /** Returns the length of a graph path */
374 int     get_compound_graph_path_length(compound_graph_path *gr);
375
376 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
377 void    set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node);
378 int     get_compound_graph_path_array_index(compound_graph_path *gr, int pos);
379 void    set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
380
381 /** Checks whether the path up to pos is correct. If the path contains a NULL,
382  *  assumes the path is not complete and returns 'true'. */
383 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
384
385 /* A value of a compound entity is a pair of a value and the description of the
386    corresponding access path to the member of the compound.  */
387 void     add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path);
388 void     set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos);
389 /** Returns the number of constant values needed to initialize the entity.
390  *
391  *  Asserts if the entity has variability_uninitialized.
392  * */
393 int      get_compound_ent_n_values(entity *ent);
394 /** Returns a constant value given the position. */
395 ir_node *get_compound_ent_value(entity *ent, int pos);
396 /** Returns the access path for value at position pos. */
397 compound_graph_path *get_compound_ent_value_path(entity *ent, int pos);
398 /** Returns the position of a value with the given path.
399  *  The path must contain array indicees for all array element entities. */
400 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path);
401 /** Returns a constant value given the access path.
402  *  The path must contain array indicees for all array element entities. */
403 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path);
404
405 /** Removes all constant entries where the path ends at value_ent. Does not
406    free the memory of the paths.  (The same path might be used for several
407    constant entities. */
408 void     remove_compound_ent_value(entity *ent, entity *value_ent);
409
410 /* Some languages support only trivial access paths, i.e., the member is a
411    direct, atomic member of the constant entities type. In this case the
412    corresponding entity can be accessed directly.  The following functions
413    allow direct access. */
414
415 /** generates a Path with length 1 */
416 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
417
418 /** Returns the last member in the path */
419 entity  *get_compound_ent_value_member(entity *ent, int pos);
420
421 /** Sets the path at pos 0 */
422 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
423
424 /** Initializes the entity ent which must be of a one dimensional
425    array type with the values given in the values array.
426    The array must have a lower and an upper bound.  Keeps the
427    order of values. Does not test whether the number of values
428    fits into the given array size.  Does not test whether the
429    values have the proper mode for the array. */
430 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
431
432 /** Return the overall offset of value at position pos in bits.
433  *
434  * This requires that the layout of all concerned types is fixed.
435  *
436  * @param ent Any entity of compound type with at least pos initialization values.
437  * @param pos The position of the value for which the offset is requested.
438  */
439 int  get_compound_ent_value_offset_bits(entity *ent, int pos);
440
441 /** Return the overall offset of value at position pos in bytes.
442  *
443  * This requires that the layout of all concerned types is fixed.
444  * Asserts if bit offset is not byte aligned.
445  *
446  * @param ent Any entity of compound type with at least pos initialization values.
447  * @param pos The position of the value for which the offset is requested.
448  */
449 int  get_compound_ent_value_offset_bytes(entity *ent, int pos);
450
451 /** Compute the array indicees in compound graph paths of initialized entities.
452  *
453  * All arrays must have fixed lower and upper bounds.  One array can
454  * have an open upper bound.  If there are several open bounds, we do
455  * nothing.  There must be initializer elements for all array
456  * elements.  Uses the link field in the array element entities.  The
457  * array bounds must be representable as ints.
458  *
459  * @param ent Any entity.
460  */
461 void compute_compound_ent_array_indicees(entity *ent);
462
463 /** Sort the values of the compound entity by their overall offset.
464  *
465  * This requires that the layout of all concerned types is fixed.
466  * If the entity has no initialization information the method just
467  * returns.  This is needed to dump the entity in a backend.
468  *
469  * @param ent Any entity.
470  */
471 void sort_compound_ent_values(entity *ent);
472
473
474 /* --- Fields of entities with a class type as owner --- */
475 /* Overwrites is a field that specifies that an access to the overwritten
476    entity in the supertype must use this entity.  It's a list as with
477    multiple inheritance several entities can be overwritten.  This field
478    is mostly useful for method entities.
479    If a Sel node selects an entity that is overwritten by other entities it
480    must return a pointer to the entity of the dynamic type of the pointer
481    that is passed to it.  Lowering of the Sel node must assure this.
482    Overwrittenby is the inverse of overwrites.  Both add routines add
483    both relations, they only differ in the order of arguments. */
484 void    add_entity_overwrites   (entity *ent, entity *overwritten);
485 int     get_entity_n_overwrites (entity *ent);
486 int     get_entity_overwrites_index(entity *ent, entity *overwritten);
487 entity *get_entity_overwrites   (entity *ent, int pos);
488 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
489 void    remove_entity_overwrites(entity *ent, entity *overwritten);
490
491 void    add_entity_overwrittenby   (entity *ent, entity *overwrites);
492 int     get_entity_n_overwrittenby (entity *ent);
493 int     get_entity_overwrittenby_index(entity *ent, entity *overwrites);
494 entity *get_entity_overwrittenby   (entity *ent, int pos);
495 void    set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites);
496 void    remove_entity_overwrittenby(entity *ent, entity *overwrites);
497
498 /**
499  *   Checks whether a pointer points to an entity.
500  *
501  *   @param thing     an arbitrary pointer
502  *
503  *   @return
504  *       true if the thing is an entity, else false
505  */
506 int is_entity (const void *thing);
507
508 /** Returns true if the type of the entity is a primitive, pointer
509    enumeration or method type. */
510 int is_atomic_entity(entity *ent);
511 /** Returns true if the type of the entity is a class, structure,
512    array or union type. */
513 int is_compound_entity(entity *ent);
514
515 /** Returns true if ent1 and ent2 have are equal except for their owner.
516    Two entities are equal if
517     - they have the same type (the same C-struct)
518     - ...?
519 */
520 bool equal_entity(entity *ent1, entity *ent2);
521
522 /** Outputs a unique number for this entity if libfirm is compiled for
523    debugging, (configure with --enable-debug) else returns 0. */
524 long get_entity_nr(entity *ent);
525
526 /** Returns the entities visited count. */
527 unsigned long get_entity_visited(entity *ent);
528
529 /** Sets the entities visited count. */
530 void        set_entity_visited(entity *ent, unsigned long num);
531
532 /** Sets visited field in entity to entity_visited. */
533 void        mark_entity_visited(entity *ent);
534
535 /** Returns true if this entity was visited. */
536 int        entity_visited(entity *ent);
537
538 /** Returns true if this entity was not visited. */
539 int        entity_not_visited(entity *ent);
540
541 /**
542  * @page unknown_entity
543  *
544  *  This entity is an auxiliary entity dedicated to support analyses.
545  *
546  *  The unknown entity represents that there could be an entity, but it is not
547  *  known.  This entity can be used to initialize fields before an analysis (not known
548  *  yet) or to represent the top of a lattice (could not be determined).  There exists
549  *  exactly one entity unknown. This entity has as owner and as type the unknown type. It is
550  *  allocated when initializing the entity module.
551  *
552  *  The entity can take the role of any entity, also methods.  It returns default
553  *  values in these cases.
554  *
555  *  The following values are set:
556  *    name          = "unknown_entity"
557  *    ld_name       = "unknown_entity"
558  *    owner         = unknown_type
559  *    type          = unknown_type
560  *    visibility    = visibility_external_allocated
561  *    offset        = -1
562  *    variability   = variability_uninitialized
563  *    value         = SymConst(unknown_entity)
564  *    values        = NULL
565  *    val_paths     = NULL
566  *    peculiarity   = peculiarity_existent
567  *    volatility    = volatility_non_volatile
568  *    stickyness    = stickyness_unsticky
569  *    ld_name       = NULL
570  *    overwrites    = NULL
571  *    overwrittenby = NULL
572  *    irg           = NULL
573  *    link          = NULL
574  *
575  */
576 /* A variable that contains the only unknown entity. */
577 extern entity *unknown_entity;
578 /* Returns the unknown entity */
579 entity *get_unknown_entity(void);
580
581 # endif /* _ENTITY_H_ */