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