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