BugFix: allow method entities to be initialized by an const_initializer.
[libfirm] / ir / tr / entity_t.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /*
21  * @file
22  * @brief   Representation of all program known entities -- private header.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #ifndef FIRM_TR_ENTITY_T_H
27 #define FIRM_TR_ENTITY_T_H
28
29 #include <assert.h>
30
31 #include "typerep.h"
32 #include "type_t.h"
33 #include "ident.h"
34 #include "pseudo_irg.h"
35 #include "compound_path.h"
36
37 typedef struct ir_initializer_base_t {
38         ir_initializer_kind_t kind;
39 } ir_initializer_base_t;
40
41 /**
42  * An compound initializer.
43  */
44 typedef struct ir_initializer_compound_t {
45         ir_initializer_base_t  base;
46         unsigned               n_initializers;
47         ir_initializer_t      *initializers[1];
48 } ir_initializer_compound_t;
49
50 /**
51  * An initializer containing an ir_node,
52  */
53 typedef struct ir_initializer_const_t {
54         ir_initializer_base_t  base;
55         ir_node               *value;
56 } ir_initializer_const_t ;
57
58 /**
59  * An initializer containing a tarval.
60  */
61 typedef struct ir_initializer_tarval_t {
62         ir_initializer_base_t  base;
63         tarval                *value;
64 } ir_initializer_tarval_t ;
65
66 union ir_initializer_t {
67         ir_initializer_kind_t      kind;
68         ir_initializer_base_t      base;
69         ir_initializer_compound_t  compound;
70         ir_initializer_const_t     consti;
71         ir_initializer_tarval_t    tarval;
72 };
73
74 /** The attributes for compound entities. */
75 typedef struct compound_ent_attr {
76         ir_node **values;     /**< constant values of compound entities. */
77         compound_graph_path **val_paths;
78                              /**< paths corresponding to constant values. */
79 } compound_ent_attr;
80
81 /** A reserved value for "not yet set". */
82 #define VTABLE_NUM_NOT_SET ((unsigned)(-1))
83
84 /** The attributes for methods. */
85 typedef struct method_ent_attr {
86         ir_graph *irg;                 /**< The corresponding irg if known.
87                                             The ir_graph constructor automatically sets this field. */
88         unsigned irg_add_properties;   /**< Additional graph properties can be
89                                             stored in a entity if no irg is available. */
90
91         unsigned vtable_number;        /**< For a dynamically called method, the number assigned
92                                             in the virtual function table. */
93
94         ptr_access_kind *param_access; /**< the parameter access */
95         unsigned *param_weight;        /**< The weight of method's parameters. Parameters
96                                             with a high weight are good candidates for procedure cloning. */
97 } method_ent_attr;
98
99 /** additional attributes for code entities */
100 typedef struct code_ent_attr {
101         ir_label_t  label;       /** label of the basic block */
102 } code_ent_attr;
103
104
105 /**
106  * An abstract data type to represent program entities.
107  *
108  * @see  ir_type
109  */
110 struct ir_entity {
111         firm_kind kind;       /**< The dynamic type tag for entity. */
112         ident *name;          /**< The name of this entity. */
113         ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
114                                    name.  If the field is read before written a default
115                                    mangling is applies.  The name of the owner is prepended
116                                    to the name of the entity, separated by a underscore.
117                                    E.g.,  for a class `A' with field `a' this
118                                    is the ident for `A_a'. */
119         ir_type *type;        /**< The type of this entity, e.g., a method type, a
120                                    basic type of the language or a class itself. */
121         ir_type *owner;       /**< The compound type (e.g. class type) this entity belongs to. */
122         unsigned linkage:10;        /**< Specifies linkage type */
123         unsigned volatility:1;      /**< Specifies volatility of entities content.*/
124         unsigned aligned:1;         /**< Specifies alignment of entities content. */
125         unsigned usage:4;           /**< flag indicating usage types of this entity,
126                                          see ir_entity_usage. */
127         unsigned compiler_gen:1;    /**< If set, this entity was compiler generated.
128                                      */
129         unsigned visibility:3;      /**< @deprecated */
130         unsigned allocation:3;      /**< @deprecated */
131         unsigned peculiarity:3;     /**< @deprecated */
132         unsigned final:1;           /**< @deprecated */
133         int offset;                 /**< Offset in bytes for this entity. Fixed
134                                          when layout of owner is determined. */
135         unsigned alignment;         /**< entity alignment in bytes */
136         unsigned char offset_bit_remainder;
137                                     /**< If the entity is a bit field, this is the
138                                          offset of the start of the bit field
139                                          within the byte specified by offset. */
140         ir_visited_t visit;         /**< visited counter for walks of the type
141                                          information. */
142         struct dbg_info *dbi;       /**< A pointer to information for debug support.
143                                      */
144         void *link;                 /**< To store some intermediate information. */
145         ir_type *repr_class;        /**< If this entity represents a class info, the
146                                          associated class. */
147
148         /* ------------- fields for entities owned by a class type ---------------*/
149
150         ir_entity **overwrites;     /**< A list of entities this entity overwrites.
151                                      */
152         ir_entity **overwrittenby;  /**< A list of entities that overwrite this
153                                          entity. */
154
155         /* ------------- fields for atomic entities  --------------- */
156         ir_initializer_t *initializer; /**< entity initializer */
157         union {
158                 /* ------------- fields for compound entities -------------- */
159                 compound_ent_attr cmpd_attr;
160                 /* ------------- fields for method entities ---------------- */
161                 method_ent_attr   mtd_attr;
162                 /* fields for code entities */
163                 code_ent_attr     code_attr;
164         } attr; /**< type specific attributes */
165
166         /* ------------- fields for analyses ---------------*/
167
168 #ifdef DEBUG_libfirm
169         long nr;             /**< A unique node number for each node to make output
170                                   readable. */
171 #endif
172 };
173
174 /** Initialize the entity module. */
175 void firm_init_entity(void);
176
177 /* ----------------------- inline functions ------------------------ */
178 static inline int _is_entity(const void *thing)
179 {
180         return get_kind(thing) == k_entity;
181 }
182
183 static inline const char *_get_entity_name(const ir_entity *ent)
184 {
185         assert(ent && ent->kind == k_entity);
186         return get_id_str(get_entity_ident(ent));
187 }
188
189 static inline ident *_get_entity_ident(const ir_entity *ent)
190 {
191         assert(ent && ent->kind == k_entity);
192         return ent->name;
193 }
194
195 static inline void _set_entity_ident(ir_entity *ent, ident *id)
196 {
197         assert(ent && ent->kind == k_entity);
198         ent->name = id;
199 }
200
201 static inline ir_type *_get_entity_owner(const ir_entity *ent)
202 {
203         assert(ent && ent->kind == k_entity);
204         return ent->owner;
205 }
206
207 static inline ident *_get_entity_ld_ident(const ir_entity *ent)
208 {
209         assert(ent && ent->kind == k_entity);
210         if (ent->ld_name == NULL)
211                 return ent->name;
212         return ent->ld_name;
213 }
214
215 static inline void _set_entity_ld_ident(ir_entity *ent, ident *ld_ident)
216 {
217         assert(ent && ent->kind == k_entity);
218         ent->ld_name = ld_ident;
219 }
220
221 static inline const char *_get_entity_ld_name(const ir_entity *ent)
222 {
223         assert(ent && ent->kind == k_entity);
224         return get_id_str(get_entity_ld_ident(ent));
225 }
226
227 static inline ir_type *_get_entity_type(const ir_entity *ent)
228 {
229         assert(ent && ent->kind == k_entity);
230         return ent->type;
231 }
232
233 static inline void _set_entity_type(ir_entity *ent, ir_type *type)
234 {
235         assert(ent && ent->kind == k_entity);
236         ent->type = type;
237 }
238
239 static inline ir_linkage _get_entity_linkage(const ir_entity *ent)
240 {
241         assert(ent && ent->kind == k_entity);
242         return ent->linkage;
243 }
244
245 static inline ir_volatility _get_entity_volatility(const ir_entity *ent)
246 {
247         assert(ent && ent->kind == k_entity);
248         return ent->volatility;
249 }
250
251 static inline void _set_entity_volatility(ir_entity *ent, ir_volatility vol)
252 {
253         assert(ent && ent->kind == k_entity);
254         ent->volatility = vol;
255 }
256
257 static inline unsigned _get_entity_alignment(const ir_entity *ent)
258 {
259         assert(ent && ent->kind == k_entity);
260         return ent->alignment;
261 }
262
263 static inline void _set_entity_alignment(ir_entity *ent, unsigned alignment)
264 {
265         assert(ent && ent->kind == k_entity);
266         ent->alignment = alignment;
267 }
268
269 static inline ir_align _get_entity_aligned(const ir_entity *ent)
270 {
271         assert(ent && ent->kind == k_entity);
272         return ent->aligned;
273 }
274
275 static inline void _set_entity_aligned(ir_entity *ent, ir_align a)
276 {
277         assert(ent && ent->kind == k_entity);
278         ent->aligned = a;
279 }
280
281 static inline int _is_entity_compiler_generated(const ir_entity *ent)
282 {
283         assert(ent && ent->kind == k_entity);
284         return ent->compiler_gen;
285 }
286
287 static inline void _set_entity_compiler_generated(ir_entity *ent, int flag)
288 {
289         assert(ent && ent->kind == k_entity);
290         ent->compiler_gen = flag ? 1 : 0;
291 }
292
293 static inline ir_entity_usage _get_entity_usage(const ir_entity *ent)
294 {
295         assert(ent && ent->kind == k_entity);
296         return ent->usage;
297 }
298
299 static inline void _set_entity_usage(ir_entity *ent, ir_entity_usage state)
300 {
301         assert(ent && ent->kind == k_entity);
302         ent->usage = state;
303 }
304
305 static inline int _get_entity_offset(const ir_entity *ent)
306 {
307         assert(ent && ent->kind == k_entity);
308         return ent->offset;
309 }
310
311 static inline void _set_entity_offset(ir_entity *ent, int offset)
312 {
313         assert(ent && ent->kind == k_entity);
314         ent->offset = offset;
315 }
316
317 static inline unsigned char _get_entity_offset_bits_remainder(const ir_entity *ent)
318 {
319         assert(ent && ent->kind == k_entity);
320         return ent->offset_bit_remainder;
321 }
322
323 static inline void _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset)
324 {
325         assert(ent && ent->kind == k_entity);
326         ent->offset_bit_remainder = offset;
327 }
328
329 static inline void *_get_entity_link(const ir_entity *ent)
330 {
331         assert(ent && ent->kind == k_entity);
332         return ent->link;
333 }
334
335 static inline void _set_entity_link(ir_entity *ent, void *l)
336 {
337         assert(ent && ent->kind == k_entity);
338         ent->link = l;
339 }
340
341 static inline ir_graph *_get_entity_irg(const ir_entity *ent)
342 {
343         ir_graph *irg;
344         assert(ent && ent->kind == k_entity);
345         if (!is_Method_type(ent->type) || ent == unknown_entity) {
346                 return NULL;
347         }
348
349         irg = ent->attr.mtd_attr.irg;
350         if (irg != NULL && !get_visit_pseudo_irgs()     && is_pseudo_ir_graph(irg))
351                 return NULL;
352         return irg;
353 }
354
355 static inline ir_visited_t _get_entity_visited(const ir_entity *ent)
356 {
357         assert(ent && ent->kind == k_entity);
358         return ent->visit;
359 }
360
361 static inline void _set_entity_visited(ir_entity *ent, ir_visited_t num)
362 {
363         assert(ent && ent->kind == k_entity);
364         ent->visit = num;
365 }
366
367 static inline void _mark_entity_visited(ir_entity *ent)
368 {
369         assert(ent && ent->kind == k_entity);
370         ent->visit = firm_type_visited;
371 }
372
373 static inline int _entity_visited(const ir_entity *ent)
374 {
375         return _get_entity_visited(ent) >= firm_type_visited;
376 }
377
378 static inline int _entity_not_visited(const ir_entity *ent)
379 {
380         return _get_entity_visited(ent) < firm_type_visited;
381 }
382
383 static inline ir_type *_get_entity_repr_class(const ir_entity *ent)
384 {
385         assert(ent && ent->kind == k_entity);
386         return ent->repr_class;
387 }
388
389 static inline dbg_info *_get_entity_dbg_info(const ir_entity *ent)
390 {
391         return ent->dbi;
392 }
393
394 static inline void _set_entity_dbg_info(ir_entity *ent, dbg_info *db)
395 {
396         ent->dbi = db;
397 }
398
399 int is_entity_final(const ir_entity *entity);
400
401 #define is_entity(thing)                         _is_entity(thing)
402 #define get_entity_name(ent)                     _get_entity_name(ent)
403 #define get_entity_ident(ent)                    _get_entity_ident(ent)
404 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
405 #define get_entity_owner(ent)                    _get_entity_owner(ent)
406 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
407 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
408 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
409 #define get_entity_type(ent)                     _get_entity_type(ent)
410 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
411 #define get_entity_linkage(ent)                  _get_entity_linkage(ent)
412 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
413 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
414 #define set_entity_alignment(ent, alignment)     _set_entity_alignment(ent, alignment)
415 #define get_entity_alignment(ent)                _get_entity_alignment(ent)
416 #define get_entity_align(ent)                    _get_entity_align(ent)
417 #define set_entity_align(ent, a)                 _set_entity_align(ent, a)
418 #define is_entity_compiler_generated(ent)        _is_entity_compiler_generated(ent)
419 #define set_entity_compiler_generated(ent, flag) _set_entity_compiler_generated(ent, flag)
420 #define get_entity_usage(ent)                    _get_entity_usage(ent)
421 #define set_entity_usage(ent, flags)             _set_entity_usage(ent, flags)
422 #define get_entity_offset(ent)                   _get_entity_offset(ent)
423 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
424 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
425 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
426 #define get_entity_link(ent)                     _get_entity_link(ent)
427 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
428 #define get_entity_irg(ent)                      _get_entity_irg(ent)
429 #define get_entity_visited(ent)                  _get_entity_visited(ent)
430 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
431 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
432 #define entity_visited(ent)                      _entity_visited(ent)
433 #define entity_not_visited(ent)                  _entity_not_visited(ent)
434 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
435 #define get_entity_dbg_info(ent)                 _get_entity_dbg_info(ent)
436 #define set_entity_dbg_info(ent, db)             _set_entity_dbg_info(ent, db)
437
438 #endif