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