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