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