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