Removed depency of USE_GCC_INLINE, fixed inlining (hopefully)
[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 byte 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 /* $Id$ */
58
59 # ifndef _ENTITY_H_
60 # define _ENTITY_H_
61
62 # include "ident.h"
63 # include "type.h"
64 # include "dbginfo.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.  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 uninitialize 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  * Entity is automatic_allocated and uninitialize 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  */
158 entity     *copy_entity_own (entity *old, type *new_owner);
159
160 /**
161  * Copies the entity if the new_name is different from the
162  * name of the old entity, else returns the old entity.
163  *
164  * Automatically inserts the new entity as a member of owner.
165  * The mangled name ld_name is set to NULL.
166  */
167 entity     *copy_entity_name (entity *old, ident *new_name);
168
169 /**
170  * Frees the entity.
171  *
172  * The owner will still contain the pointer to this
173  * entity, as well as all other references!
174  */
175 void        free_entity (entity *ent);
176
177 /** Returns the name of an entity. */
178 const char *get_entity_name     (entity *ent);
179
180 /** Returns the ident of an entity. */
181 ident      *get_entity_ident    (entity *ent);
182
183 /** Returns the mangled name of the entity.
184  *
185  * If the mangled name is set it returns the existing name.
186  * Else it generates a name with mangle_entity()
187  * and remembers this new name internally.
188  */
189 ident      *get_entity_ld_ident (entity *ent);
190
191 /** Sets the mangled name of the entity. */
192 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
193
194 /** Returns the mangled name of the entity as a string. */
195 const char *get_entity_ld_name (entity *end);
196
197 /** Returns the owner of the entity. */
198 type       *get_entity_owner (entity *ent);
199
200 /** Sets the owner field in entity to owner.  Don't forget to add
201    ent to owner!! */
202 void        set_entity_owner (entity *ent, type *owner);
203
204 /** Asserts if the type owner is neither a compound type or an array */
205 void assert_legal_owner_of_ent(type *owner);
206
207 /** Returns the type of an entity. */
208 type     *get_entity_type (entity *ent);
209
210 /** Sets the type of an entity. */
211 void      set_entity_type (entity *ent, type *tp);
212
213 /** The allocation type. */
214 typedef enum {
215   allocation_automatic, /**< The entity is allocated during runtime, implicitly
216                              as component of a compound type.   This is the default. */
217   allocation_parameter, /**< The entity is a parameter.  It is also automatic allocated.
218                              We distinguish the allocation of paramters from the allocation
219                              of local variables as their placement depends on the calling
220                              conventions. */
221   allocation_dynamic,   /**< The entity is allocated during runtime, explicitly
222                              by an Alloc node. */
223   allocation_static     /**< The entity is allocated statically.  We can use a
224                              Const as address of the entity. */
225 } ent_allocation;
226
227 /** Returns the allocation type of an entity. */
228 ent_allocation get_entity_allocation (entity *ent);
229
230 /** Sets the allocation type of an entity. */
231 void           set_entity_allocation (entity *ent, ent_allocation al);
232
233 /** Return the name of the allocation type. */
234 const char *get_allocation_name(ent_allocation vis);
235
236 /**
237  * This enumeration flags the visibility of entities.  This is necessary
238  * for partial compilation.
239  */
240 typedef enum {
241   visibility_local,              /**< The entity is only visible locally.  This is the default. */
242   visibility_external_visible,   /**< The entity is visible to other external program parts, but
243                                       it is defined here.  It may not be optimized away.  The entity must
244                                       be static_allocated. */
245   visibility_external_allocated  /**< The entity is defined and allocated externally.  This compilation
246                                       must not allocate memory for this entity. The entity must
247                                       be static_allocated.  This can also be an external defined
248                                       method. */
249 } ent_visibility;
250
251 /** Returns the visibility of an entity. */
252 ent_visibility get_entity_visibility (entity *ent);
253
254 /** Sets the visibility of an entity. */
255 void           set_entity_visibility (entity *ent, ent_visibility vis);
256
257 /** Return the name of the visibility */
258 const char *get_visibility_name(ent_visibility vis);
259
260 /** This enumeration flags the variability of entities. */
261 typedef enum {
262   variability_uninitialized,    /**< The content of the entity is completely unknown. */
263   variability_initialized,      /**< After allocation the entity is initalized with the
264                                      value given somewhere in the entity. */
265   variability_part_constant,    /**< For entities of compound types.  Some members of the entity
266                                      are constant.  The others are uninitialized.  Those members
267                                      given a value for are constant. */
268   variability_constant          /**< The entity is constant. */
269 } ent_variability;
270
271 /** Returns the variability of an entity. */
272 ent_variability get_entity_variability (entity *ent);
273
274 /** Sets the variability of an entity. */
275 void            set_entity_variability (entity *ent, ent_variability var);
276
277 /** Return the name of the variablity. */
278 const char *get_variability_name(ent_variability var);
279
280 /** This enumeration flags the volatility of entities. */
281 typedef enum {
282   volatility_non_volatile,    /**< The entity is not volatile */
283   volatility_is_volatile      /**< The entity is volatile */
284 } ent_volatility;
285
286 /** Returns the volatility of an entity. */
287 ent_volatility get_entity_volatility (entity *ent);
288
289 /** Sets the volatility of an entity. */
290 void           set_entity_volatility (entity *ent, ent_volatility vol);
291
292 /* Return the name of the volatility. */
293 const char *get_volatility_name(ent_volatility var);
294
295 /** Returns the offset of an entity (in a compound). Only set if layout = fixed. */
296 int       get_entity_offset (entity *ent);
297
298 /** Sets the offset of an entity (in a compound). */
299 void      set_entity_offset (entity *ent, int offset);
300
301 /** Returns the stored intermediate information. */
302 void*   get_entity_link(entity *ent);
303
304 /** Stores new intermediate information. */
305 void    set_entity_link(entity *ent, void *l);
306
307 /* -- Fields of method entities -- */
308 /* The entity knows the corresponding irg if the entity is a method.
309    This allows to get from a Call to the called irg.
310    Only entities of peculiarity "existent" can have a corresponding irg,
311    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
312 ir_graph *get_entity_irg(entity *ent);
313 void      set_entity_irg(entity *ent, ir_graph *irg);
314
315 /** Return the peculiarity of an entity. */
316 peculiarity get_entity_peculiarity (entity *ent);
317
318 /** Sets the peculiarity of an entity. */
319 void        set_entity_peculiarity (entity *ent, peculiarity pec);
320
321 /** Return the name of the peculiarity. */
322 const char *get_peculiarity_name(peculiarity var);
323
324 /* -- Representation of constant values of entites -- */
325 /** Returns true if the the node is representable as code on
326  *  const_code_irg. */
327 int      is_irn_const_expression(ir_node *n);
328 /* Set current_ir_graph to get_const_code_irg() to generate a constant
329    expression. */
330 /* Copies a firm subgraph that complies to the restrictions for
331    constant expressions to current_block in current_ir_graph. */
332 ir_node *copy_const_value(ir_node *n);
333
334 /* Set has no effect for existent entities of type method. */
335 ir_node *get_atomic_ent_value(entity *ent);
336 void     set_atomic_ent_value(entity *ent, ir_node *val);
337
338 /* The following type describes a path to a leave in the compound graph.
339    Node 0 in the path must be an entity of type tp given in the constructor.  If
340    the type of this element is compound, the path node 1 is an element of the type
341    of node 0 an so forth, until an entity of atomic type is reached. */
342 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
343 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
344 typedef struct compound_graph_path compound_graph_path;
345 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
346 compound_graph_path *new_compound_graph_path(type *tp, int length);
347 int     is_compound_graph_path(void *thing);
348 void    free_compound_graph_path (compound_graph_path *gr);
349 int     get_compound_graph_path_length(compound_graph_path *gr);
350 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
351 void    set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node);
352
353 /* A value of a compound entity is a pair of a value and the description of the
354    corresponding access path to the member of the compound.  */
355 void     add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path);
356 void     set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos);
357 int      get_compound_ent_n_values(entity *ent);
358 ir_node *get_compound_ent_value(entity *ent, int pos);
359 compound_graph_path *get_compound_ent_value_path(entity *ent, int pos);
360 /* Removes all constant entries where the path ends at value_ent. Does not
361    free the memory of the paths.  (The same path might be used for several
362    constant entities. */
363 void     remove_compound_ent_value(entity *ent, entity *value_ent);
364
365 /* Some languages support only trivial access paths, i.e., the member is a
366    direct, atomic member of the constant entities type. In this case the
367    corresponding entity can be accessed directly.  The following functions
368    allow direct access. */
369 /* generates a Path with length 1 */
370 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
371 /* Returns the last member in the path */
372 entity  *get_compound_ent_value_member(entity *ent, int pos);
373 /* Sets the path at pos 0 */
374 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
375
376
377
378 /** Inits the entity ent witch must be of a one dimensional
379    array type with the values given in the values array.
380    The array must have a lower and an upper bound.  Keeps the
381    order of values. Does not test whether the number of values
382    fits into the given array size.  Does not test whether the
383    values have the proper mode for the array. */
384 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
385
386 /* --- Fields of entities with a class type as owner --- */
387 /* Overwrites is a field that specifies that an access to the overwritten
388    entity in the supertype must use this entity.  It's a list as with
389    multiple inheritance several enitites can be overwritten.  This field
390    is mostly useful for method entities.
391    If a Sel node selects an entity that is overwritten by other entities it
392    must return a pointer to the entity of the dynamic type of the pointer
393    that is passed to it.  Lowering of the Sel node must assure this.
394    Overwrittenby is the inverse of overwrites.  Both add routines add
395    both relations, they only differ in the order of arguments. */
396 void    add_entity_overwrites   (entity *ent, entity *overwritten);
397 int     get_entity_n_overwrites (entity *ent);
398 int     get_entity_overwrites_index(entity *ent, entity *overwritten);
399 entity *get_entity_overwrites   (entity *ent, int pos);
400 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
401 void    remove_entity_overwrites(entity *ent, entity *overwritten);
402
403 void    add_entity_overwrittenby   (entity *ent, entity *overwrites);
404 int     get_entity_n_overwrittenby (entity *ent);
405 int     get_entity_overwrittenby_index(entity *ent, entity *overwrites);
406 entity *get_entity_overwrittenby   (entity *ent, int pos);
407 void    set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites);
408 void    remove_entity_overwrittenby(entity *ent, entity *overwrites);
409
410 /**
411  *   Checks whether a pointer points to an entity.
412  *
413  *   @param thing     an arbitrary pointer
414  *
415  *   @return
416  *       true if the thing is an entity, else false
417  */
418 int is_entity (void *thing);
419
420 /** Returns true if the type of the entity is a primitive, pointer
421    enumeration or method type. */
422 int is_atomic_entity(entity *ent);
423 /** Returns true if the type of the entity is a class, structure,
424    array or union type. */
425 int is_compound_entity(entity *ent);
426
427 /** Returns true if ent1 and ent2 have are equal except for their owner.
428    Two entities are equal if
429     - they have the same type (the same C-struct)
430     - ...?
431 */
432 bool equal_entity(entity *ent1, entity *ent2);
433
434
435 /** Outputs a unique number for this entity if libfirm is compiled for
436    debugging, (configure with --enable-debug) else returns 0. */
437 long get_entity_nr(entity *ent);
438
439 /** Returns the entitys visited count. */
440 unsigned long get_entity_visited(entity *ent);
441
442 /** Sets the entitys visited count. */
443 void        set_entity_visited(entity *ent, unsigned long num);
444
445 /** Sets visited field in entity to entity_visited. */
446 void        mark_entity_visited(entity *ent);
447
448 /** Returns true if this entity was visited. */
449 bool        entity_visited(entity *ent);
450
451 /** Returns true if this entity was not visited. */
452 bool        entity_not_visited(entity *ent);
453
454 /** Returns the dynamically referenced entity if the static entity and the
455  *  dynamic type are given. */
456 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent);
457
458
459 /*-----------------------------------------------------------------*/
460 /*  Debug aides                                                    */
461 /*-----------------------------------------------------------------*/
462
463
464 /** Write the entity and all its attributes to stdout.
465  *
466  *  Writes the entity and all its attributes to stdout if DEBUG_libfirm
467  *  is set.  Else does nothing. */
468 void    dump_entity (entity *ent);
469
470
471 # endif /* _ENTITY_H_ */