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