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