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