c407ff635b900ced3cf36e3d2d9fcbccf4db2caf
[libfirm] / ir / tr / entity_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity_t.h
4  * Purpose:     Representation of all program known entities -- private header.
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_t.h
15  *
16  * entity.h:  entities represent all program known objects.
17  *
18  * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier
19  *
20  *  An entity is the representation of program known objects in Firm.
21  *  The primary concept of entities is to represent members of complex
22  *  types, i.e., fields and methods of classes.  As not all programming
23  *  language model all variables and methods as members of some class,
24  *  the concept of entities is extended to cover also local and global
25  *  variables, and arbitrary procedures.
26  *
27  *  An entity always specifies the type of the object it represents and
28  *  the type of the object it is a part of, the owner of the entity.
29  *  Originally this is the type of the class of which the entity is a
30  *  member.
31  *  The owner of local variables is the procedure they are defined in.
32  *  The owner of global variables and procedures visible in the whole
33  *  program is a universally defined class type "GlobalType".  The owner
34  *  of procedures defined in the scope of an other procedure is the
35  *  enclosing procedure.
36  */
37
38 # ifndef _ENTITY_T_H_
39 # define _ENTITY_T_H_
40
41 #include "firm_common_t.h"
42 #include "firm_config.h"
43
44 # include "entity.h"
45 # include "typegmod.h"
46 # include "mangle.h"
47 # include "pseudo_irg.h"
48
49
50 /** A path in a compund graph. */
51 struct compound_graph_path {
52   firm_kind kind;       /**< dynamic type tag for compound graph path. */
53   type *tp;             /**< The type this path belongs to. */
54   int len;              /**< length of the path */
55   int *arr_indicees;    /**< List of array indeces.  To compute position of
56                            array elements */
57   entity *nodes[1];     /**< List of entities of length len to express the
58                            access path. */
59 };
60
61 /** the type of an entity */
62 struct entity {
63   firm_kind kind;       /**< dynamic type tag for entity. */
64   ident *name;          /**< name of this entity */
65   ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
66                            name.  If the field is read before written a default
67                            mangling is applies.  The name of the owner is prepended
68                            to the name of the entity, separated by a underscore.
69                            E.g.,  for a class `A' with field `a' this
70                            is the ident for `A_a'. */
71   type *type;           /**< The type of this entity, e.g., a method type, a
72                            basic type of the language or a class itself */
73   type *owner;          /**< The compound type (e.g. class type) this entity belongs to. */
74   ent_allocation allocation;  /**< Distinguishes static and dynamically allocated
75                  entities and some further cases. */
76   ent_visibility visibility;  /**< Specifies visibility to external program
77                  fragments */
78   ent_variability variability;  /**< Specifies variability of entities content */
79   ent_volatility volatility;    /**< Specifies volatility of entities content */
80   ent_stickyness stickyness;    /**< Specifies whether this entity is sticky  */
81   int  offset;          /**< Offset in bits for this entity.  Fixed when layout
82                of owner is determined.  */
83   void *link;           /**< To store some intermediate information */
84   unsigned long visit;  /**< visited counter for walks of the type information */
85   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
86
87   /* ------------- fields for atomic entities  ---------------*/
88
89   ir_node *value;            /**< value if entity is not of variability uninitialized.
90                                Only for atomic entities. */
91
92   /* ------------- fields for compound entities ---------------*/
93
94   ir_node **values;     /**< constant values of compound entities. Only available if
95                variablility not uninitialized.  Must be set for variability constant
96                            */
97   compound_graph_path **val_paths; /**< paths corresponding to constant values. Only available if
98                       variablility not uninitialized.  Must be set for variability constant */
99
100   /* ------------- fields for entities owned by a class type ---------------*/
101
102   entity **overwrites;     /**< A list of entities this entity overwrites. */
103   entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
104
105   /* ------------- fields for methods ---------------*/
106
107   enum peculiarity peculiarity;
108   ir_graph *irg;        /**< If (type == method_type) this is the corresponding irg.
109                The ir_graph constructor automatically sets this field.
110                Yes, it must be here. */
111
112   /* ------------- fields for analyses ---------------*/
113
114
115 #ifdef DEBUG_libfirm
116   int nr;             /**< a unique node number for each node to make output
117                            readable. */
118 # endif /* DEBUG_libfirm */
119 };
120
121
122
123 /* ----------------------- inline functions ------------------------ */
124 static INLINE int
125 _is_entity(const void *thing) {
126   return get_kind(thing) == k_entity;
127 }
128
129 static INLINE const char *
130 _get_entity_name(const entity *ent) {
131   assert(ent && ent->kind == k_entity);
132   return get_id_str(get_entity_ident(ent));
133 }
134
135 static INLINE ident *
136 _get_entity_ident(const entity *ent) {
137   assert(ent && ent->kind == k_entity);
138   return ent->name;
139 }
140
141 static INLINE type *
142 _get_entity_owner(entity *ent) {
143   assert(ent && ent->kind == k_entity);
144   return ent->owner = skip_tid(ent->owner);
145 }
146
147 static INLINE ident *
148 _get_entity_ld_ident(entity *ent)
149 {
150   assert(ent && ent->kind == k_entity);
151   if (ent->ld_name == NULL)
152     ent->ld_name = mangle_entity(ent);
153   return ent->ld_name;
154 }
155
156 static INLINE void
157 _set_entity_ld_ident(entity *ent, ident *ld_ident) {
158   assert(ent && ent->kind == k_entity);
159   ent->ld_name = ld_ident;
160 }
161
162 static INLINE const char *
163 _get_entity_ld_name(entity *ent) {
164   assert(ent && ent->kind == k_entity);
165   return get_id_str(get_entity_ld_ident(ent));
166 }
167
168 static INLINE type *
169 _get_entity_type(entity *ent) {
170   assert(ent && ent->kind == k_entity);
171   return ent->type = skip_tid(ent->type);
172 }
173
174 static INLINE void
175 _set_entity_type(entity *ent, type *type) {
176   assert(ent && ent->kind == k_entity);
177   ent->type = type;
178 }
179
180 static INLINE ent_allocation
181 _get_entity_allocation(const entity *ent) {
182   assert(ent && ent->kind == k_entity);
183   return ent->allocation;
184 }
185
186 static INLINE void
187 _set_entity_allocation(entity *ent, ent_allocation al) {
188   assert(ent && ent->kind == k_entity);
189   ent->allocation = al;
190 }
191
192 static INLINE ent_visibility
193 _get_entity_visibility(const entity *ent) {
194   assert(ent && ent->kind == k_entity);
195   return ent->visibility;
196 }
197
198 static INLINE ent_variability
199 _get_entity_variability(const entity *ent) {
200   assert(ent && ent->kind == k_entity);
201   return ent->variability;
202 }
203
204 static INLINE ent_volatility
205 _get_entity_volatility(const entity *ent) {
206   assert(ent && ent->kind == k_entity);
207   return ent->volatility;
208 }
209
210 static INLINE void
211 _set_entity_volatility(entity *ent, ent_volatility vol) {
212   assert(ent && ent->kind == k_entity);
213   ent->volatility = vol;
214 }
215
216 static INLINE peculiarity
217 _get_entity_peculiarity(const entity *ent) {
218   assert(ent && ent->kind == k_entity);
219   return ent->peculiarity;
220 }
221
222 /**
223  * @todo Why peculiarity only for methods?
224  *       Good question.  Originally, there were only description and
225  *       existent.  The thought was, what sense does it make to
226  *       describe a field?  With inherited the situation changed.  So
227  *       I removed the assertion.  GL, 28.2.05
228  */
229 static INLINE void
230 _set_entity_peculiarity(entity *ent, peculiarity pec) {
231   assert(ent && ent->kind == k_entity);
232   /* @@@ why peculiarity only for methods? */
233   //assert(is_Method_type(ent->type));
234
235   ent->peculiarity = pec;
236 }
237
238 static INLINE ent_stickyness
239 _get_entity_stickyness(const entity *ent) {
240   assert(ent && ent->kind == k_entity);
241   return ent->stickyness;
242 }
243
244 static INLINE void
245 _set_entity_stickyness(entity *ent, ent_stickyness stickyness)
246 {
247   assert(ent && ent->kind == k_entity);
248   ent->stickyness = stickyness;
249 }
250
251 static INLINE int
252 _get_entity_offset_bits(const entity *ent) {
253   assert(ent && ent->kind == k_entity);
254   return ent->offset;
255 }
256
257 static INLINE int
258 _get_entity_offset_bytes(const entity *ent) {
259   int bits = _get_entity_offset_bits(ent);
260
261   if (bits & 7) return -1;
262   return bits >> 3;
263 }
264
265 static INLINE void
266 _set_entity_offset_bits(entity *ent, int offset) {
267   assert(ent && ent->kind == k_entity);
268   ent->offset = offset;
269 }
270
271 static INLINE void
272 _set_entity_offset_bytes(entity *ent, int offset) {
273   _set_entity_offset_bits(ent, offset * 8);
274 }
275
276 static INLINE void *
277 _get_entity_link(const entity *ent) {
278   assert(ent && ent->kind == k_entity);
279   return ent->link;
280 }
281
282 static INLINE void
283 _set_entity_link(entity *ent, void *l) {
284   assert(ent && ent->kind == k_entity);
285   ent->link = l;
286 }
287
288 static INLINE ir_graph *
289 _get_entity_irg(const entity *ent) {
290   assert(ent && ent->kind == k_entity);
291   assert(ent == unknown_entity || is_Method_type(ent->type));
292   if (!get_visit_pseudo_irgs() && ent->irg && is_pseudo_ir_graph(ent->irg))
293     return NULL;
294   return ent->irg;
295 }
296
297 #define is_entity(thing)                        _is_entity(thing)
298 #define get_entity_name(ent)                    _get_entity_name(ent)
299 #define get_entity_ident(ent)                   _get_entity_ident(ent)
300 #define get_entity_owner(ent)                   _get_entity_owner(ent)
301 #define get_entity_ld_ident(ent)                _get_entity_ld_ident(ent)
302 #define set_entity_ld_ident(ent, ld_ident)      _set_entity_ld_ident(ent, ld_ident)
303 #define get_entity_ld_name(ent)                 _get_entity_ld_name(ent)
304 #define get_entity_type(ent)                    _get_entity_type(ent)
305 #define set_entity_type(ent, type)              _set_entity_type(ent, type)
306 #define get_entity_allocation(ent)              _get_entity_allocation(ent)
307 #define set_entity_allocation(ent, al)          _set_entity_allocation(ent, al)
308 #define get_entity_visibility(ent)              _get_entity_visibility(ent)
309 #define get_entity_variability(ent)             _get_entity_variability(ent)
310 #define get_entity_volatility(ent)              _get_entity_volatility(ent)
311 #define set_entity_volatility(ent, vol)         _set_entity_volatility(ent, vol)
312 #define get_entity_peculiarity(ent)             _get_entity_peculiarity(ent)
313 #define set_entity_peculiarity(ent, pec)        _set_entity_peculiarity(ent, pec)
314 #define get_entity_stickyness(ent)              _get_entity_stickyness(ent)
315 #define set_entity_stickyness(ent, stickyness)  _set_entity_stickyness(ent, stickyness)
316 #define get_entity_offset_bits(ent)             _get_entity_offset_bits(ent)
317 #define get_entity_offset_bytes(ent)            _get_entity_offset_bytes(ent)
318 #define set_entity_offset_bits(ent, offset)     _set_entity_offset_bits(ent, offset)
319 #define set_entity_offset_bytes(ent, offset)    _set_entity_offset_bytes(ent, offset)
320 #define get_entity_link(ent)                    _get_entity_link(ent)
321 #define set_entity_link(ent, l)                 _set_entity_link(ent, l)
322 #define get_entity_irg(ent)                     _get_entity_irg(ent)
323
324 # endif /* _ENTITY_T_H_ */