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