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