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