new support for initialization of compound entities.
[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 "entity.h"
42 # include "typegmod.h"
43 # include "mangle.h"
44
45 /** A path in a compund graph. */
46 struct compound_graph_path {
47   firm_kind kind;       /**< dynamic type tag for compound graph path. */
48   type *tp;             /**< The type this path belongs to. */
49   int len;              /**< length of the path */
50   int *arr_indicees;    /**< List of array indicees.  To compute position of
51                              array elements */
52   entity *nodes[1];     /**< List of entities of length len to express the
53                              access path. */
54 };
55
56 /** the type of an entity */
57 struct entity {
58   firm_kind kind;       /**< dynamic type tag for entity. */
59   ident *name;          /**< name of this entity */
60   ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
61                            name.  If the field is read before written a default
62                            magling is applies.  The name of the owner is prepended
63                            to the name of the entity, separated by a underscore.
64                            E.g.,  for a class `A' with field `a' this
65                            is the ident for `A_a'. */
66   type *type;           /**< The type of this entity, e.g., a method type, a
67                            basic type of the language or a class itself */
68   type *owner;          /**< The compound type (e.g. class type) this entity belongs to. */
69   ent_allocation allocation;  /**< Distinguishes static and dynamically allocated
70                  entities and some further cases. */
71   ent_visibility visibility;  /**< Specifies visibility to external program
72                  fragments */
73   ent_variability variability;  /**< Specifies variability of entities content */
74   ent_volatility volatility;    /**< Specifies volatility of entities content */
75   ent_stickyness stickyness;    /**< Specifies whether this entity is sticky  */
76   int  offset;          /**< Offset in bits for this entity.  Fixed when layout
77                            of owner is determined.  */
78   void *link;           /**< To store some intermediate information */
79   unsigned long visit;  /**< visited counter for walks of the type information */
80   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
81
82   /* ------------- fields for atomic entities  ---------------*/
83   ir_node *value;            /**< value if entity is not of variability uninitialized.
84                                Only for atomic entities. */
85
86   /* ------------- fields for compound entities ---------------*/
87   ir_node **values;     /**< constant values of compound entities. Only available if
88                           variablility not uninitialized.  Must be set for variability constant
89                            */
90   compound_graph_path **val_paths;    /**< paths corresponding to constant values. Only available if
91                           variablility not uninitialized.  Must be set for variability constant */
92
93   /* ------------- fields for entities owned by a class type ---------------*/
94   entity **overwrites;  /**< A list of entities this entity overwrites. */
95   entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
96
97   /* ------------- fields for methods ---------------*/
98   enum peculiarity peculiarity;
99   ir_graph *irg;        /**< If (type == method_type) this is the corresponding irg.
100                The ir_graph constructor automatically sets this field.
101                Yes, it must be here. */
102 #ifdef DEBUG_libfirm
103   int nr;             /**< a unique node number for each node to make output
104                   readable. */
105 #endif
106 };
107
108 /* ----------------------- inline functions ------------------------ */
109 static INLINE int
110 __is_entity(void *thing) {
111   return get_kind(thing) == k_entity;
112 }
113
114 static INLINE const char *
115 __get_entity_name(entity *ent) {
116   assert(ent && ent->kind == k_entity);
117   return get_id_str(get_entity_ident(ent));
118 }
119
120 static INLINE ident *
121 __get_entity_ident(entity *ent) {
122   assert(ent && ent->kind == k_entity);
123   return ent->name;
124 }
125
126 static INLINE type *
127 __get_entity_owner(entity *ent) {
128   assert(ent && ent->kind == k_entity);
129   return ent->owner = skip_tid(ent->owner);
130 }
131
132 static INLINE ident *
133 __get_entity_ld_ident(entity *ent)
134 {
135   assert(ent && ent->kind == k_entity);
136   if (ent->ld_name == NULL)
137     ent->ld_name = mangle_entity(ent);
138   return ent->ld_name;
139 }
140
141 static INLINE void
142 __set_entity_ld_ident(entity *ent, ident *ld_ident) {
143   assert(ent && ent->kind == k_entity);
144   ent->ld_name = ld_ident;
145 }
146
147 static INLINE const char *
148 __get_entity_ld_name(entity *ent) {
149   assert(ent && ent->kind == k_entity);
150   return get_id_str(get_entity_ld_ident(ent));
151 }
152
153 static INLINE type *
154 __get_entity_type(entity *ent) {
155   assert(ent && ent->kind == k_entity);
156   return ent->type = skip_tid(ent->type);
157 }
158
159 static INLINE void
160 __set_entity_type(entity *ent, type *type) {
161   assert(ent && ent->kind == k_entity);
162   ent->type = type;
163 }
164
165 static INLINE ent_allocation
166 __get_entity_allocation(entity *ent) {
167   assert(ent && ent->kind == k_entity);
168   return ent->allocation;
169 }
170
171 static INLINE void
172 __set_entity_allocation(entity *ent, ent_allocation al) {
173   assert(ent && ent->kind == k_entity);
174   ent->allocation = al;
175 }
176
177 static INLINE ent_visibility
178 __get_entity_visibility(entity *ent) {
179   assert(ent && ent->kind == k_entity);
180   return ent->visibility;
181 }
182
183 static INLINE ent_variability
184 __get_entity_variability(entity *ent) {
185   assert(ent && ent->kind == k_entity);
186   return ent->variability;
187 }
188
189 static INLINE ent_volatility
190 __get_entity_volatility(entity *ent) {
191   assert(ent && ent->kind == k_entity);
192   return ent->volatility;
193 }
194
195 static INLINE void
196 __set_entity_volatility(entity *ent, ent_volatility vol) {
197   assert(ent && ent->kind == k_entity);
198   ent->volatility = vol;
199 }
200
201 static INLINE peculiarity
202 __get_entity_peculiarity(entity *ent) {
203   assert(ent && ent->kind == k_entity);
204   return ent->peculiarity;
205 }
206
207 /**
208  * @todo why peculiarity only for methods
209  */
210 static INLINE void
211 __set_entity_peculiarity(entity *ent, peculiarity pec) {
212   assert(ent && ent->kind == k_entity);
213   /* @@@ why peculiarity only for methods? */
214   assert(is_method_type(ent->type));
215   ent->peculiarity = pec;
216 }
217
218 static INLINE ent_stickyness
219 __get_entity_stickyness(entity *ent) {
220   assert(ent && ent->kind == k_entity);
221   return ent->stickyness;
222 }
223
224 static INLINE void
225 __set_entity_stickyness(entity *ent, ent_stickyness stickyness)
226 {
227   assert(ent && ent->kind == k_entity);
228   ent->stickyness = stickyness;
229 }
230
231 static INLINE int
232 __get_entity_offset_bits(entity *ent) {
233   assert(ent && ent->kind == k_entity);
234   return ent->offset;
235 }
236
237 static INLINE int
238 __get_entity_offset_bytes(entity *ent) {
239   int bits = __get_entity_offset_bits(ent);
240
241   if (bits & 7) return -1;
242   return bits >> 3;
243 }
244
245 static INLINE void
246 __set_entity_offset_bits(entity *ent, int offset) {
247   assert(ent && ent->kind == k_entity);
248   ent->offset = offset;
249 }
250
251 static INLINE void
252 __set_entity_offset_bytes(entity *ent, int offset) {
253   __set_entity_offset_bits(ent, offset * 8);
254 }
255
256 static INLINE void *
257 __get_entity_link(entity *ent) {
258   assert(ent && ent->kind == k_entity);
259   return ent->link;
260 }
261
262 static INLINE void
263 __set_entity_link(entity *ent, void *l) {
264   assert(ent && ent->kind == k_entity);
265   ent->link = l;
266 }
267
268 static INLINE ir_graph *
269 __get_entity_irg(entity *ent) {
270   assert(ent && ent->kind == k_entity);
271   assert(is_method_type(ent->type));
272   return ent->irg;
273 }
274
275 #define is_entity(thing)                        __is_entity(thing)
276 #define get_entity_name(ent)                    __get_entity_name(ent)
277 #define get_entity_ident(ent)                   __get_entity_ident(ent)
278 #define get_entity_owner(ent)                   __get_entity_owner(ent)
279 #define get_entity_ld_ident(ent)                __get_entity_ld_ident(ent)
280 #define set_entity_ld_ident(ent, ld_ident)      __set_entity_ld_ident(ent, ld_ident)
281 #define get_entity_ld_name(ent)                 __get_entity_ld_name(ent)
282 #define get_entity_type(ent)                    __get_entity_type(ent)
283 #define set_entity_type(ent, type)              __set_entity_type(ent, type)
284 #define get_entity_allocation(ent)              __get_entity_allocation(ent)
285 #define set_entity_allocation(ent, al)          __set_entity_allocation(ent, al)
286 #define get_entity_visibility(ent)              __get_entity_visibility(ent)
287 #define get_entity_variability(ent)             __get_entity_variability(ent)
288 #define get_entity_volatility(ent)              __get_entity_volatility(ent)
289 #define set_entity_volatility(ent, vol)         __set_entity_volatility(ent, vol)
290 #define get_entity_peculiarity(ent)             __get_entity_peculiarity(ent)
291 #define set_entity_peculiarity(ent, pec)        __set_entity_peculiarity(ent, pec)
292 #define get_entity_stickyness(ent)              __get_entity_stickyness(ent)
293 #define set_entity_stickyness(ent, stickyness)  __set_entity_stickyness(ent, stickyness)
294 #define get_entity_offset_bits(ent)             __get_entity_offset_bits(ent)
295 #define get_entity_offset_bytes(ent)            __get_entity_offset_bytes(ent)
296 #define set_entity_offset_bits(ent, offset)     __set_entity_offset_bits(ent, offset)
297 #define set_entity_offset_bytes(ent, offset)    __set_entity_offset_bytes(ent, offset)
298 #define get_entity_link(ent)                    __get_entity_link(ent)
299 #define set_entity_link(ent, l)                 __set_entity_link(ent, l)
300 #define get_entity_irg(ent)                     __get_entity_irg(ent)
301
302 # endif /* _ENTITY_T_H_ */