8d92a070cf425eb5f8d73aee0a5ad6256a5ef2f4
[libfirm] / ir / tr / entity.c
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.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31
32 #include "xmalloc.h"
33 #include "entity_t.h"
34 #include "array.h"
35 #include "irtools.h"
36 #include "irhooks.h"
37 #include "irprintf.h"
38
39 #include "irprog_t.h"
40 #include "ircons.h"
41 #include "tv_t.h"
42 #include "irdump.h"
43 #include "irgraph_t.h"
44 #include "callgraph.h"
45 #include "error.h"
46 #include "compound_path.h"
47
48 /*-----------------------------------------------------------------*/
49 /** general                                                       **/
50 /*-----------------------------------------------------------------*/
51
52 ir_entity *unknown_entity = NULL;
53
54 ir_entity *get_unknown_entity(void) { return unknown_entity; }
55
56 /** The name of the unknown entity. */
57 #define UNKNOWN_ENTITY_NAME "unknown_entity"
58
59 /*-----------------------------------------------------------------*/
60 /* ENTITY                                                          */
61 /*-----------------------------------------------------------------*/
62
63 /**
64  * Add an entity to it's already set owner type.
65  */
66 static inline void insert_entity_in_owner(ir_entity *ent) {
67         ir_type *owner = ent->owner;
68         switch (get_type_tpop_code(owner)) {
69         case tpo_class:
70                 add_class_member(owner, ent);
71                 break;
72         case tpo_struct:
73                 add_struct_member(owner, ent);
74                 break;
75         case tpo_union:
76                 add_union_member(owner, ent);
77                 break;
78         case tpo_array:
79                 set_array_element_entity(owner, ent);
80                 break;
81         default:
82                 panic("Unsupported type kind");
83         }
84 }  /* insert_entity_in_owner */
85
86 /**
87  * Creates a new entity. This entity is NOT inserted in the owner type.
88  *
89  * @param db     debug info for this entity
90  * @param owner  the owner type of the new entity
91  * @param name   the name of the new entity
92  * @param type   the type of the new entity
93  *
94  * @return the new created entity
95  */
96 static inline ir_entity *
97 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
98 {
99         ir_entity *res;
100         ir_graph *rem;
101
102         assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
103
104         res = XMALLOCZ(ir_entity);
105
106         res->kind    = k_entity;
107         res->name    = name;
108         res->ld_name = NULL;
109         res->type    = type;
110         res->owner   = owner;
111
112         res->volatility           = volatility_non_volatile;
113         res->aligned              = align_is_aligned;
114         res->usage                = ir_usage_unknown;
115         res->compiler_gen         = 0;
116         res->offset               = -1;
117         res->offset_bit_remainder = 0;
118         res->alignment            = 0;
119         res->link                 = NULL;
120         res->repr_class           = NULL;
121
122         if (is_Method_type(type)) {
123                 symconst_symbol sym;
124                 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
125                 sym.entity_p            = res;
126                 rem                     = current_ir_graph;
127                 current_ir_graph        = get_const_code_irg();
128                 set_atomic_ent_value(res, new_SymConst(mode, sym, symconst_addr_ent));
129                 current_ir_graph        = rem;
130                 res->linkage            = IR_LINKAGE_CONSTANT;
131                 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
132                 res->attr.mtd_attr.vtable_number      = VTABLE_NUM_NOT_SET;
133                 res->attr.mtd_attr.param_access       = NULL;
134                 res->attr.mtd_attr.param_weight       = NULL;
135                 res->attr.mtd_attr.irg                = NULL;
136         } else if (is_compound_type(type)) {
137                 res->attr.cmpd_attr.values    = NULL;
138                 res->attr.cmpd_attr.val_paths = NULL;
139         } else if (is_code_type(type)) {
140                 res->attr.code_attr.label = (ir_label_t) -1;
141         }
142
143         if (is_Class_type(owner)) {
144                 res->overwrites    = NEW_ARR_F(ir_entity *, 0);
145                 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
146         } else {
147                 res->overwrites    = NULL;
148                 res->overwrittenby = NULL;
149         }
150
151 #ifdef DEBUG_libfirm
152         res->nr = get_irp_new_node_nr();
153 #endif /* DEBUG_libfirm */
154
155         res->visit = 0;
156         set_entity_dbg_info(res, db);
157
158         return res;
159 }  /* new_rd_entity */
160
161 ir_entity *
162 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
163         ir_entity *res;
164
165         assert(is_compound_type(owner));
166         res = new_rd_entity(db, owner, name, type);
167         /* Remember entity in it's owner. */
168         insert_entity_in_owner(res);
169
170         hook_new_entity(res);
171         return res;
172 }  /* new_d_entity */
173
174 ir_entity *
175 new_entity(ir_type *owner, ident *name, ir_type *type) {
176         return new_d_entity(owner, name, type, NULL);
177 }  /* new_entity */
178
179 /**
180  * Free entity attributes.
181  *
182  * @param ent  the entity
183  */
184 static void free_entity_attrs(ir_entity *ent)
185 {
186         int i;
187         if (get_type_tpop(get_entity_owner(ent)) == type_class) {
188                 DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
189                 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
190         } else {
191                 assert(ent->overwrites == NULL);
192                 assert(ent->overwrittenby == NULL);
193         }
194         if (ent->initializer != NULL) {
195                 /* TODO: free initializers */
196         } else if (entity_has_compound_ent_values(ent)) {
197                 if (ent->attr.cmpd_attr.val_paths) {
198                         for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
199                                 if (ent->attr.cmpd_attr.val_paths[i]) {
200                                         /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ;  * @@@ warum nich? */
201                                         /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
202                                         /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
203                                 }
204                                 ent->attr.cmpd_attr.val_paths = NULL;
205                 }
206         }
207         if (is_compound_entity(ent)) {
208                 if (ent->attr.cmpd_attr.values) {
209                         /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
210                 }
211                 ent->attr.cmpd_attr.values = NULL;
212         } else if (is_method_entity(ent)) {
213                 if (ent->attr.mtd_attr.param_access) {
214                         DEL_ARR_F(ent->attr.mtd_attr.param_access);
215                         ent->attr.mtd_attr.param_access = NULL;
216                 }
217                 if (ent->attr.mtd_attr.param_weight) {
218                         DEL_ARR_F(ent->attr.mtd_attr.param_weight);
219                         ent->attr.mtd_attr.param_weight = NULL;
220                 }
221         }
222 }  /* free_entity_attrs */
223
224 /**
225  * Creates a deep copy of an entity.
226  */
227 static ir_entity *deep_entity_copy(ir_entity *old)
228 {
229         ir_entity *newe = XMALLOC(ir_entity);
230
231         *newe = *old;
232         if (old->initializer != NULL) {
233                 /* FIXME: the initializers are NOT copied */
234         } else if (entity_has_compound_ent_values(old)) {
235                 newe->attr.cmpd_attr.values    = NULL;
236                 newe->attr.cmpd_attr.val_paths = NULL;
237                 if (old->attr.cmpd_attr.values)
238                         newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
239
240                 /* FIXME: the compound graph paths are NOT copied */
241                 if (old->attr.cmpd_attr.val_paths)
242                         newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
243         } else if (is_method_entity(old)) {
244                 /* do NOT copy them, reanalyze. This might be the best solution */
245                 newe->attr.mtd_attr.param_access = NULL;
246                 newe->attr.mtd_attr.param_weight = NULL;
247         }
248
249 #ifdef DEBUG_libfirm
250         newe->nr = get_irp_new_node_nr();
251 #endif
252         return newe;
253 }
254 /*
255  * Copies the entity if the new_owner is different from the
256  * owner of the old entity,  else returns the old entity.
257  */
258 ir_entity *
259 copy_entity_own(ir_entity *old, ir_type *new_owner) {
260         ir_entity *newe;
261         assert(is_entity(old));
262         assert(is_compound_type(new_owner));
263         assert(get_type_state(new_owner) != layout_fixed);
264
265         if (old->owner == new_owner)
266                 return old;
267
268         /* create a deep copy so we are safe of aliasing and double-freeing. */
269         newe = deep_entity_copy(old);
270         newe->owner = new_owner;
271
272         if (is_Class_type(new_owner)) {
273                 newe->overwrites    = NEW_ARR_F(ir_entity *, 0);
274                 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
275         }
276
277         insert_entity_in_owner(newe);
278         return newe;
279 }  /* copy_entity_own */
280
281 ir_entity *
282 copy_entity_name(ir_entity *old, ident *new_name) {
283         ir_entity *newe;
284         assert(old && old->kind == k_entity);
285
286         if (old->name == new_name) return old;
287         newe = deep_entity_copy(old);
288         newe->name = new_name;
289         newe->ld_name = NULL;
290
291         if (is_Class_type(newe->owner)) {
292                 newe->overwrites    = DUP_ARR_F(ir_entity *, old->overwrites);
293                 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
294         }
295         insert_entity_in_owner(newe);
296
297         return newe;
298 }  /* copy_entity_name */
299
300 void
301 free_entity(ir_entity *ent) {
302         assert(ent && ent->kind == k_entity);
303         free_entity_attrs(ent);
304         ent->kind = k_BAD;
305         free(ent);
306 }  /* free_entity */
307
308 /* Outputs a unique number for this node */
309 long
310 get_entity_nr(const ir_entity *ent) {
311         assert(ent && ent->kind == k_entity);
312 #ifdef DEBUG_libfirm
313         return ent->nr;
314 #else
315         return (long)PTR_TO_INT(ent);
316 #endif
317 }  /* get_entity_nr */
318
319 const char *
320 (get_entity_name)(const ir_entity *ent) {
321         return _get_entity_name(ent);
322 }
323
324 ident *
325 (get_entity_ident)(const ir_entity *ent) {
326         return _get_entity_ident(ent);
327 }
328
329 void
330 (set_entity_ident)(ir_entity *ent, ident *id) {
331         _set_entity_ident(ent, id);
332 }
333
334 ir_type *
335 (get_entity_owner)(const ir_entity *ent) {
336         return _get_entity_owner(ent);
337 }
338
339 void
340 set_entity_owner(ir_entity *ent, ir_type *owner) {
341         assert(is_entity(ent));
342         assert(is_compound_type(owner));
343         ent->owner = owner;
344 }
345
346 ident *(get_entity_ld_ident)(const ir_entity *ent)
347 {
348         return _get_entity_ld_ident(ent);
349 }
350
351 void
352 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
353         _set_entity_ld_ident(ent, ld_ident);
354 }
355
356 const char *(get_entity_ld_name)(const ir_entity *ent)
357 {
358         return _get_entity_ld_name(ent);
359 }
360
361 ir_type *
362 (get_entity_type)(const ir_entity *ent) {
363         return _get_entity_type(ent);
364 }
365
366 void
367 (set_entity_type)(ir_entity *ent, ir_type *type) {
368         _set_entity_type(ent, type);
369 }
370
371 ir_volatility
372 (get_entity_volatility)(const ir_entity *ent) {
373         return _get_entity_volatility(ent);
374 }
375
376 void
377 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
378         _set_entity_volatility(ent, vol);
379 }
380
381 /* Return the name of the volatility. */
382 const char *get_volatility_name(ir_volatility var)
383 {
384 #define X(a)    case a: return #a
385         switch (var) {
386         X(volatility_non_volatile);
387         X(volatility_is_volatile);
388     default: return "BAD VALUE";
389         }
390 #undef X
391 }  /* get_volatility_name */
392
393 ir_align
394 (get_entity_aligned)(const ir_entity *ent) {
395         return _get_entity_aligned(ent);
396 }
397
398 void
399 (set_entity_aligned)(ir_entity *ent, ir_align a) {
400         _set_entity_aligned(ent, a);
401 }
402
403 unsigned
404 (get_entity_alignment)(const ir_entity *ent) {
405         return _get_entity_alignment(ent);
406 }
407
408 void
409 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
410         _set_entity_alignment(ent, alignment);
411 }
412
413 /* Return the name of the alignment. */
414 const char *get_align_name(ir_align a)
415 {
416 #define X(a)    case a: return #a
417         switch (a) {
418         X(align_non_aligned);
419         X(align_is_aligned);
420         default: return "BAD VALUE";
421         }
422 #undef X
423 }  /* get_align_name */
424
425 void
426 set_entity_label(ir_entity *ent, ir_label_t label)
427 {
428         ent->attr.code_attr.label = label;
429 }
430
431 ir_label_t get_entity_label(const ir_entity *ent)
432 {
433         return ent->attr.code_attr.label;
434 }
435
436 static void verify_linkage(ir_entity *entity)
437 {
438         ir_linkage linkage = entity->linkage;
439         /* local and extern are mutually exclusive */
440         (void) linkage;
441         assert(! ((linkage & IR_LINKAGE_EXTERN) && (linkage & IR_LINKAGE_LOCAL)));
442         if (!is_method_entity(entity) && (linkage & IR_LINKAGE_EXTERN)) {
443                 assert(!entity_has_definition(entity));
444         }
445         assert(! ((linkage & IR_LINKAGE_CONSTANT) && (linkage & IR_LINKAGE_WEAK)));
446 }
447
448 void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
449 {
450         entity->linkage = linkage;
451         verify_linkage(entity);
452 }
453
454 ir_linkage (get_entity_linkage)(const ir_entity *entity)
455 {
456         return get_entity_linkage(entity);
457 }
458
459 void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
460 {
461         entity->linkage |= linkage;
462         verify_linkage(entity);
463 }
464
465 void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
466 {
467         entity->linkage &= ~linkage;
468         verify_linkage(entity);
469 }
470
471 /* Checks if an entity is compiler generated */
472 int (is_entity_compiler_generated)(const ir_entity *ent) {
473         return _is_entity_compiler_generated(ent);
474 }  /* is_entity_compiler_generated */
475
476 /* Sets/resets the compiler generated flag */
477 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
478         _set_entity_compiler_generated(ent, flag);
479 }  /* set_entity_compiler_generated */
480
481 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
482         return _get_entity_usage(ent);
483 }
484
485 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
486         _set_entity_usage(ent, flags);
487 }
488
489 /* Set has no effect for existent entities of type method. */
490 ir_node *get_atomic_ent_value(ir_entity *entity)
491 {
492         ir_initializer_t *initializer = get_entity_initializer(entity);
493
494         assert(entity && is_atomic_entity(entity));
495         if (initializer == NULL) {
496                 ir_type *type = get_entity_type(entity);
497                 return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
498         }
499
500         switch (get_initializer_kind(initializer)) {
501         case IR_INITIALIZER_NULL: {
502                 ir_type *type = get_entity_type(entity);
503                 ir_mode *mode = get_type_mode(type);
504                 return new_r_Const(get_const_code_irg(), get_mode_null(mode));
505         }
506         case IR_INITIALIZER_TARVAL: {
507                 tarval *tv = get_initializer_tarval_value(initializer);
508                 return new_r_Const(get_const_code_irg(), tv);
509         }
510         case IR_INITIALIZER_CONST:
511                 return get_initializer_const_value(initializer);
512         case IR_INITIALIZER_COMPOUND:
513                 panic("compound initializer in atomic entity not allowed (%+F)", entity);
514         }
515
516         panic("invalid initializer kind in get_atomic_ent_value(%+F)", entity);
517 }
518
519 void set_atomic_ent_value(ir_entity *entity, ir_node *val)
520 {
521         ir_initializer_t *initializer;
522
523         assert(is_atomic_entity(entity));
524         assert(get_entity_peculiarity(entity) != peculiarity_description);
525
526         assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
527         initializer = create_initializer_const(val);
528         entity->initializer = initializer;
529 }
530
531 /* Returns true if the the node is representable as code on
532  *  const_code_irg. */
533 int is_irn_const_expression(ir_node *n) {
534         ir_mode *m;
535
536         /* we are in danger iff an exception will arise. TODO: be more precisely,
537          * for instance Div. will NOT rise if divisor != 0
538          */
539         if (is_binop(n) && !is_fragile_op(n))
540                 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
541
542         m = get_irn_mode(n);
543         switch (get_irn_opcode(n)) {
544         case iro_Const:
545         case iro_SymConst:
546         case iro_Unknown:
547                 return 1;
548         case iro_Conv:
549         case iro_Cast:
550                 return is_irn_const_expression(get_irn_n(n, 0));
551         default:
552                 break;
553         }
554         return 0;
555 }  /* is_irn_const_expression */
556
557 /*
558  * Copies a firm subgraph that complies to the restrictions for
559  * constant expressions to current_block in current_ir_graph.
560  */
561 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
562         ir_node *nn;
563         ir_mode *m;
564
565         /* @@@ GL I think  we should implement this using the routines from irgopt for
566                dead node elimination/inlineing. */
567
568         m = get_irn_mode(n);
569         switch (get_irn_opcode(n)) {
570         case iro_Const:
571                 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
572                 break;
573         case iro_SymConst:
574                 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
575                         get_SymConst_value_type(n));
576                 break;
577         case iro_Add:
578                 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
579                         copy_const_value(dbg, get_Add_right(n)), m); break;
580         case iro_Sub:
581                 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
582                         copy_const_value(dbg, get_Sub_right(n)), m); break;
583         case iro_Mul:
584                 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
585                         copy_const_value(dbg, get_Mul_right(n)), m); break;
586         case iro_And:
587                 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
588                         copy_const_value(dbg, get_And_right(n)), m); break;
589         case iro_Or:
590                 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
591                         copy_const_value(dbg, get_Or_right(n)), m); break;
592         case iro_Eor:
593                 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
594                         copy_const_value(dbg, get_Eor_right(n)), m); break;
595         case iro_Cast:
596                 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
597         case iro_Conv:
598                 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
599         case iro_Unknown:
600                 nn = new_Unknown(m); break;
601         default:
602                 assert(0 && "opcode invalid or not implemented");
603                 nn = NULL;
604                 break;
605         }
606         return nn;
607 }  /* copy_const_value */
608
609 /** Return the name of the initializer kind. */
610 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
611 {
612 #define X(a)    case a: return #a
613         switch (ini) {
614         X(IR_INITIALIZER_CONST);
615         X(IR_INITIALIZER_TARVAL);
616         X(IR_INITIALIZER_NULL);
617         X(IR_INITIALIZER_COMPOUND);
618     default: return "BAD VALUE";
619         }
620 #undef X
621 }
622
623 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
624
625 ir_initializer_t *get_initializer_null(void)
626 {
627         return &null_initializer;
628 }
629
630 ir_initializer_t *create_initializer_const(ir_node *value)
631 {
632         struct obstack *obst = get_irg_obstack(get_const_code_irg());
633
634         ir_initializer_t *initializer
635                 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
636         initializer->kind         = IR_INITIALIZER_CONST;
637         initializer->consti.value = value;
638
639         return initializer;
640 }
641
642 ir_initializer_t *create_initializer_tarval(tarval *tv)
643 {
644         struct obstack *obst = get_irg_obstack(get_const_code_irg());
645
646         ir_initializer_t *initializer
647                 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
648         initializer->kind         = IR_INITIALIZER_TARVAL;
649         initializer->tarval.value = tv;
650
651         return initializer;
652 }
653
654 ir_initializer_t *create_initializer_compound(unsigned n_entries)
655 {
656         struct obstack *obst = get_irg_obstack(get_const_code_irg());
657
658         size_t i;
659         size_t size  = sizeof(ir_initializer_compound_t)
660                      + (n_entries-1) * sizeof(ir_initializer_t*);
661
662         ir_initializer_t *initializer = obstack_alloc(obst, size);
663         initializer->kind                    = IR_INITIALIZER_COMPOUND;
664         initializer->compound.n_initializers = n_entries;
665
666         for(i = 0; i < n_entries; ++i) {
667                 initializer->compound.initializers[i] = get_initializer_null();
668         }
669
670         return initializer;
671 }
672
673 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
674 {
675         assert(initializer->kind == IR_INITIALIZER_CONST);
676         return skip_Id(initializer->consti.value);
677 }
678
679 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
680 {
681         assert(initializer->kind == IR_INITIALIZER_TARVAL);
682         return initializer->tarval.value;
683 }
684
685 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
686 {
687         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
688         return initializer->compound.n_initializers;
689 }
690
691 void set_initializer_compound_value(ir_initializer_t *initializer,
692                                     unsigned index, ir_initializer_t *value)
693 {
694         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
695         assert(index < initializer->compound.n_initializers);
696
697         initializer->compound.initializers[index] = value;
698 }
699
700 ir_initializer_t *get_initializer_compound_value(
701                 const ir_initializer_t *initializer, unsigned index)
702 {
703         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
704         assert(index < initializer->compound.n_initializers);
705
706         return initializer->compound.initializers[index];
707 }
708
709 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
710 {
711         return initializer->kind;
712 }
713
714 static void check_entity_initializer(ir_entity *entity)
715 {
716 #ifndef NDEBUG
717         ir_initializer_t *initializer = entity->initializer;
718         switch (initializer->kind) {
719         case IR_INITIALIZER_COMPOUND:
720                 assert(is_compound_entity(entity));
721                 break;
722         case IR_INITIALIZER_CONST:
723         case IR_INITIALIZER_TARVAL:
724                 assert(is_atomic_entity(entity));
725                 break;
726         case IR_INITIALIZER_NULL:
727                 break;
728         }
729 #endif
730 }
731
732 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
733 {
734         entity->initializer = initializer;
735         check_entity_initializer(entity);
736 }
737
738 int has_entity_initializer(const ir_entity *entity)
739 {
740         return entity->initializer != NULL;
741 }
742
743 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
744 {
745         return entity->initializer;
746 }
747
748 int (get_entity_offset)(const ir_entity *ent)
749 {
750         return _get_entity_offset(ent);
751 }
752
753 void (set_entity_offset)(ir_entity *ent, int offset)
754 {
755         _set_entity_offset(ent, offset);
756 }
757
758 unsigned char (get_entity_offset_bits_remainder)(const ir_entity *ent)
759 {
760         return _get_entity_offset_bits_remainder(ent);
761 }
762
763 void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
764 {
765         _set_entity_offset_bits_remainder(ent, offset);
766 }
767
768 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
769 {
770 #ifndef NDEBUG
771         ir_type *owner     = get_entity_owner(ent);
772         ir_type *ovw_ovner = get_entity_owner(overwritten);
773         assert(is_Class_type(owner));
774         assert(is_Class_type(ovw_ovner));
775         assert(! is_class_final(ovw_ovner));
776 #endif /* NDEBUG */
777         ARR_APP1(ir_entity *, ent->overwrites, overwritten);
778         ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
779 }
780
781 int get_entity_n_overwrites(const ir_entity *ent)
782 {
783         assert(is_Class_type(get_entity_owner(ent)));
784         return (ARR_LEN(ent->overwrites));
785 }
786
787 int get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
788 {
789         int i, n;
790         assert(is_Class_type(get_entity_owner(ent)));
791         n = get_entity_n_overwrites(ent);
792         for (i = 0; i < n; ++i) {
793                 if (get_entity_overwrites(ent, i) == overwritten)
794                         return i;
795         }
796         return -1;
797 }
798
799 ir_entity *get_entity_overwrites(const ir_entity *ent, int pos)
800 {
801         assert(is_Class_type(get_entity_owner(ent)));
802         assert(pos < get_entity_n_overwrites(ent));
803         return ent->overwrites[pos];
804 }
805
806 void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten)
807 {
808         assert(is_Class_type(get_entity_owner(ent)));
809         assert(pos < get_entity_n_overwrites(ent));
810         ent->overwrites[pos] = overwritten;
811 }
812
813 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
814 {
815         int i, n;
816         assert(is_Class_type(get_entity_owner(ent)));
817         n = ARR_LEN(ent->overwrites);
818         for (i = 0; i < n; ++i) {
819                 if (ent->overwrites[i] == overwritten) {
820                         for (; i < n - 1; i++)
821                                 ent->overwrites[i] = ent->overwrites[i+1];
822                         ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
823                         break;
824                 }
825         }
826 }
827
828 void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
829 {
830         add_entity_overwrites(overwrites, ent);
831 }
832
833 int get_entity_n_overwrittenby(const ir_entity *ent)
834 {
835         assert(is_Class_type(get_entity_owner(ent)));
836         return ARR_LEN(ent->overwrittenby);
837 }
838
839 int get_entity_overwrittenby_index(const ir_entity *ent, ir_entity *overwrites)
840 {
841         int i, n;
842         assert(is_Class_type(get_entity_owner(ent)));
843         n = get_entity_n_overwrittenby(ent);
844         for (i = 0; i < n; ++i) {
845                 if (get_entity_overwrittenby(ent, i) == overwrites)
846                         return i;
847         }
848         return -1;
849 }
850
851 ir_entity *get_entity_overwrittenby(const ir_entity *ent, int pos)
852 {
853         assert(is_Class_type(get_entity_owner(ent)));
854         assert(pos < get_entity_n_overwrittenby(ent));
855         return ent->overwrittenby[pos];
856 }
857
858 void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites)
859 {
860         assert(is_Class_type(get_entity_owner(ent)));
861         assert(pos < get_entity_n_overwrittenby(ent));
862         ent->overwrittenby[pos] = overwrites;
863 }
864
865 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
866 {
867         int i, n;
868         assert(is_Class_type(get_entity_owner(ent)));
869
870         n = ARR_LEN(ent->overwrittenby);
871         for (i = 0; i < n; ++i) {
872                 if (ent->overwrittenby[i] == overwrites) {
873                         for(; i < n - 1; ++i)
874                                 ent->overwrittenby[i] = ent->overwrittenby[i+1];
875                         ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
876                         break;
877                 }
878         }
879 }
880
881 void *(get_entity_link)(const ir_entity *ent)
882 {
883         return _get_entity_link(ent);
884 }
885
886 void (set_entity_link)(ir_entity *ent, void *l)
887 {
888         _set_entity_link(ent, l);
889 }
890
891 ir_graph *(get_entity_irg)(const ir_entity *ent)
892 {
893         return _get_entity_irg(ent);
894 }
895
896 void set_entity_irg(ir_entity *ent, ir_graph *irg)
897 {
898         assert(is_method_entity(ent));
899         assert(get_entity_peculiarity(ent) == peculiarity_existent);
900         ent->attr.mtd_attr.irg = irg;
901 }
902
903 unsigned get_entity_vtable_number(const ir_entity *ent)
904 {
905         assert(is_method_entity((ir_entity *)ent));
906         return ent->attr.mtd_attr.vtable_number;
907 }
908
909 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
910 {
911         assert(is_method_entity(ent));
912         ent->attr.mtd_attr.vtable_number = vtable_number;
913 }
914
915 int (is_entity)(const void *thing)
916 {
917         return _is_entity(thing);
918 }
919
920 int is_atomic_entity(const ir_entity *ent)
921 {
922         ir_type *t      = get_entity_type(ent);
923         const tp_op *op = get_type_tpop(t);
924         return (op == type_primitive || op == type_pointer ||
925                 op == type_enumeration || op == type_method);
926 }
927
928 int is_compound_entity(const ir_entity *ent)
929 {
930         ir_type     *t  = get_entity_type(ent);
931         const tp_op *op = get_type_tpop(t);
932         return (op == type_class || op == type_struct ||
933                 op == type_array || op == type_union);
934 }
935
936 int is_method_entity(const ir_entity *ent)
937 {
938         ir_type *t = get_entity_type(ent);
939         return is_Method_type(t);
940 }
941
942 ir_visited_t (get_entity_visited)(const ir_entity *ent)
943 {
944         return _get_entity_visited(ent);
945 }
946
947 void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
948 {
949         _set_entity_visited(ent, num);
950 }
951
952 void (mark_entity_visited)(ir_entity *ent)
953 {
954         _mark_entity_visited(ent);
955 }
956
957 int (entity_visited)(const ir_entity *ent)
958 {
959         return _entity_visited(ent);
960 }
961
962 int (entity_not_visited)(const ir_entity *ent)
963 {
964         return _entity_not_visited(ent);
965 }
966
967 unsigned get_entity_additional_properties(const ir_entity *ent)
968 {
969         ir_graph *irg;
970
971         assert(is_method_entity(ent));
972
973         /* first check, if the graph has additional properties */
974         irg = get_entity_irg(ent);
975
976         if (irg)
977                 return get_irg_additional_properties(irg);
978
979         if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
980                 return get_method_additional_properties(get_entity_type(ent));
981
982         return ent->attr.mtd_attr.irg_add_properties;
983 }
984
985 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
986 {
987         ir_graph *irg;
988
989         assert(is_method_entity(ent));
990
991         /* first check, if the graph exists */
992         irg = get_entity_irg(ent);
993         if (irg)
994                 set_irg_additional_properties(irg, property_mask);
995         else {
996     /* do not allow to set the mtp_property_inherited flag or
997                 * the automatic inheritance of flags will not work */
998                 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
999         }
1000 }
1001
1002 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1003 {
1004         ir_graph *irg;
1005
1006         assert(is_method_entity(ent));
1007
1008         /* first check, if the graph exists */
1009         irg = get_entity_irg(ent);
1010         if (irg)
1011                 set_irg_additional_property(irg, flag);
1012         else {
1013                 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1014
1015                 if (mask & mtp_property_inherited)
1016                         mask = get_method_additional_properties(get_entity_type(ent));
1017
1018                         /* do not allow to set the mtp_property_inherited flag or
1019                 * the automatic inheritance of flags will not work */
1020                 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1021         }
1022 }
1023
1024 /* Returns the class type that this type info entity represents or NULL
1025    if ent is no type info entity. */
1026 ir_type *(get_entity_repr_class)(const ir_entity *ent)
1027 {
1028         return _get_entity_repr_class(ent);
1029 }
1030
1031 dbg_info *(get_entity_dbg_info)(const ir_entity *ent)
1032 {
1033         return _get_entity_dbg_info(ent);
1034 }
1035
1036 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db)
1037 {
1038         _set_entity_dbg_info(ent, db);
1039 }
1040
1041 int entity_is_externally_visible(const ir_entity *entity)
1042 {
1043         return (get_entity_linkage(entity) & IR_LINKAGE_LOCAL) == 0;
1044 }
1045
1046 int entity_has_definition(const ir_entity *entity)
1047 {
1048         return entity->initializer != NULL
1049                 || get_entity_irg(entity) != NULL
1050                 || entity_has_compound_ent_values(entity);
1051 }
1052
1053 void firm_init_entity(void)
1054 {
1055         symconst_symbol sym;
1056
1057         assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1058         assert(!unknown_entity && "Call firm_init_entity() only once!");
1059
1060         unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1061         set_entity_linkage(unknown_entity, IR_LINKAGE_EXTERN);
1062
1063         set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1064
1065         current_ir_graph = get_const_code_irg();
1066         sym.entity_p     = unknown_entity;
1067 }
1068
1069 ir_allocation get_entity_allocation(const ir_entity *entity)
1070 {
1071         return entity->allocation;
1072 }
1073
1074 void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
1075 {
1076         entity->allocation = allocation;
1077 }
1078
1079 ir_peculiarity get_entity_peculiarity(const ir_entity *entity)
1080 {
1081         return entity->peculiarity;
1082 }
1083
1084 void set_entity_peculiarity(ir_entity *entity, ir_peculiarity peculiarity)
1085 {
1086         entity->peculiarity = peculiarity;
1087 }
1088
1089 void set_entity_final(ir_entity *entity, int final)
1090 {
1091         entity->final = final;
1092 }
1093
1094 int is_entity_final(const ir_entity *entity)
1095 {
1096         return entity->final;
1097 }