- get_entity_nr() now takes an cont entity
[libfirm] / ir / tr / entity.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity.c
4  * Purpose:     Representation of all program known entities.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier, Michael Beck
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef HAVE_STRING_H
17 # include <string.h>
18 #endif
19 #ifdef HAVE_STDLIB_H
20 # include <stdlib.h>
21 #endif
22 #ifdef HAVE_STDDEF_H
23 # include <stddef.h>
24 #endif
25 #ifdef HAVE_MALLOC_H
26 # include <malloc.h>
27 #endif
28 #ifdef HAVE_ALLOCA_H
29 # include <alloca.h>
30 #endif
31
32 #include "firm_common_t.h"
33
34 #include "xmalloc.h"
35 #include "entity_t.h"
36 #include "mangle.h"
37 #include "typegmod.h"
38 #include "array.h"
39 #include "irtools.h"
40 #include "irhooks.h"
41 #include "irprintf.h"
42
43 /* All this is needed to build the constant node for methods: */
44 #include "irprog_t.h"
45 #include "ircons.h"
46 #include "tv_t.h"
47 #include "irdump.h"  /* for output if errors occur. */
48
49 #include "callgraph.h"  /* for dumping debug output */
50
51 /*******************************************************************/
52 /** general                                                       **/
53 /*******************************************************************/
54
55 ir_entity *unknown_entity = NULL;
56
57 ir_entity *get_unknown_entity(void) { return unknown_entity; }
58
59 #define UNKNOWN_ENTITY_NAME "unknown_entity"
60
61 /*-----------------------------------------------------------------*/
62 /* ENTITY                                                          */
63 /*-----------------------------------------------------------------*/
64
65 /**
66  * Add an entity to it's already set owner type.
67  */
68 static INLINE void insert_entity_in_owner(ir_entity *ent) {
69         ir_type *owner = ent->owner;
70         switch (get_type_tpop_code(owner)) {
71         case tpo_class:
72                 add_class_member(owner, ent);
73                 break;
74         case tpo_struct:
75                 add_struct_member(owner, ent);
76                 break;
77         case tpo_union:
78                 add_union_member(owner, ent);
79                 break;
80         case tpo_array:
81                 set_array_element_entity(owner, ent);
82                 break;
83         default: assert(0);
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 = xmalloc(sizeof(*res));
106         memset(res, 0, sizeof(*res));
107
108         res->kind    = k_entity;
109         res->name    = name;
110         res->ld_name = NULL;
111         res->type    = type;
112         res->owner   = owner;
113
114         res->allocation           = allocation_automatic;
115         res->visibility           = visibility_local;
116         res->volatility           = volatility_non_volatile;
117         res->stickyness           = stickyness_unsticky;
118         res->peculiarity          = peculiarity_existent;
119         res->final                = 0;
120         res->compiler_gen         = 0;
121         res->offset               = -1;
122         res->offset_bit_remainder = 0;
123         res->link                 = NULL;
124         res->repr_class           = NULL;
125
126         if (is_Method_type(type)) {
127                 symconst_symbol sym;
128                 sym.entity_p            = res;
129                 rem                     = current_ir_graph;
130                 current_ir_graph        = get_const_code_irg();
131                 res->value              = new_SymConst(sym, symconst_addr_ent);
132                 current_ir_graph        = rem;
133                 res->allocation         = allocation_static;
134                 res->variability        = variability_constant;
135                 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
136                 res->attr.mtd_attr.vtable_number      = VTABLE_NUM_NOT_SET;
137                 res->attr.mtd_attr.param_access       = NULL;
138                 res->attr.mtd_attr.param_weight       = NULL;
139                 res->attr.mtd_attr.irg                = NULL;
140                 res->attr.mtd_attr.section            = section_text;
141         } else if (is_compound_type(type)) {
142                 res->variability = variability_uninitialized;
143                 res->value       = NULL;
144                 res->attr.cmpd_attr.values    = NULL;
145                 res->attr.cmpd_attr.val_paths = NULL;
146         } else {
147                 res->variability = variability_uninitialized;
148                 res->value       = NULL;
149         }
150
151         if (is_Class_type(owner)) {
152                 res->overwrites    = NEW_ARR_F(ir_entity *, 0);
153                 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
154         } else {
155                 res->overwrites    = NULL;
156                 res->overwrittenby = NULL;
157         }
158
159 #ifdef DEBUG_libfirm
160         res->nr = get_irp_new_node_nr();
161 #endif /* DEBUG_libfirm */
162
163         res->visit = 0;
164         set_entity_dbg_info(res, db);
165
166         return res;
167 }  /* new_rd_entity */
168
169 ir_entity *
170 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
171         ir_entity *res;
172
173         assert(is_compound_type(owner));
174         res = new_rd_entity(db, owner, name, type);
175         /* Remember entity in it's owner. */
176         insert_entity_in_owner(res);
177
178         hook_new_entity(res);
179         return res;
180 }  /* new_d_entity */
181
182 ir_entity *
183 new_entity(ir_type *owner, ident *name, ir_type *type) {
184         return new_d_entity(owner, name, type, NULL);
185 }  /* new_entity */
186
187 /**
188  * Free entity attributes.
189  *
190  * @param ent  the entity
191  */
192 static void free_entity_attrs(ir_entity *ent) {
193         int i;
194         if (get_type_tpop(get_entity_owner(ent)) == type_class) {
195                 DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
196                 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
197         } else {
198                 assert(ent->overwrites == NULL);
199                 assert(ent->overwrittenby == NULL);
200         }
201         if (is_compound_entity(ent)) {
202                 if (ent->attr.cmpd_attr.val_paths) {
203                         for (i = 0; i < get_compound_ent_n_values(ent); i++)
204                                 if (ent->attr.cmpd_attr.val_paths[i]) {
205                                         /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ;  * @@@ warum nich? */
206                                         /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
207                                         /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
208                                 }
209                                 ent->attr.cmpd_attr.val_paths = NULL;
210                 }
211                 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
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 ir_entity *
226 copy_entity_own(ir_entity *old, ir_type *new_owner) {
227         ir_entity *newe;
228         assert(is_entity(old));
229         assert(is_compound_type(new_owner));
230
231         if (old->owner == new_owner) return old;
232         newe = xmalloc(sizeof(*newe));
233         memcpy(newe, old, sizeof(*newe));
234         newe->owner = new_owner;
235         if (is_Class_type(new_owner)) {
236                 newe->overwrites    = NEW_ARR_F(ir_entity *, 0);
237                 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
238         }
239 #ifdef DEBUG_libfirm
240         newe->nr = get_irp_new_node_nr();
241 #endif
242
243         insert_entity_in_owner(newe);
244
245         return newe;
246 }  /* copy_entity_own */
247
248 ir_entity *
249 copy_entity_name(ir_entity *old, ident *new_name) {
250         ir_entity *newe;
251         assert(old && old->kind == k_entity);
252
253         if (old->name == new_name) return old;
254         newe = xmalloc(sizeof(*newe));
255         memcpy(newe, old, sizeof(*newe));
256         newe->name = new_name;
257         newe->ld_name = NULL;
258         if (is_Class_type(newe->owner)) {
259                 newe->overwrites    = DUP_ARR_F(ir_entity *, old->overwrites);
260                 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
261         }
262 #ifdef DEBUG_libfirm
263         newe->nr = get_irp_new_node_nr();
264 #endif
265
266         insert_entity_in_owner(newe);
267
268         return newe;
269 }  /* copy_entity_name */
270
271
272 void
273 free_entity(ir_entity *ent) {
274         assert(ent && ent->kind == k_entity);
275         free_entity_attrs(ent);
276         ent->kind = k_BAD;
277         free(ent);
278 }  /* free_entity */
279
280 /* Outputs a unique number for this node */
281 long
282 get_entity_nr(const ir_entity *ent) {
283         assert(ent && ent->kind == k_entity);
284 #ifdef DEBUG_libfirm
285         return ent->nr;
286 #else
287         return (long)PTR_TO_INT(ent);
288 #endif
289 }  /* get_entity_nr */
290
291 const char *
292 (get_entity_name)(const ir_entity *ent) {
293         return _get_entity_name(ent);
294 }  /* get_entity_name */
295
296 ident *
297 (get_entity_ident)(const ir_entity *ent) {
298         return _get_entity_ident(ent);
299 }  /* get_entity_ident */
300
301 void
302 (set_entity_ident)(ir_entity *ent, ident *id) {
303         _set_entity_ident(ent, id);
304 }  /* set_entity_ident */
305
306 ir_type *
307 (get_entity_owner)(ir_entity *ent) {
308         return _get_entity_owner(ent);
309 }  /* get_entity_owner */
310
311 void
312 set_entity_owner(ir_entity *ent, ir_type *owner) {
313         assert(is_entity(ent));
314         assert(is_compound_type(owner));
315         ent->owner = owner;
316 }  /* set_entity_owner */
317
318 ident *
319 (get_entity_ld_ident)(ir_entity *ent) {
320         return _get_entity_ld_ident(ent);
321 }  /* get_entity_ld_ident */
322
323 void
324 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
325         _set_entity_ld_ident(ent, ld_ident);
326 }  /* set_entity_ld_ident */
327
328 const char *
329 (get_entity_ld_name)(ir_entity *ent) {
330         return _get_entity_ld_name(ent);
331 }  /* get_entity_ld_name */
332
333 ir_type *
334 (get_entity_type)(ir_entity *ent) {
335         return _get_entity_type(ent);
336 }  /* get_entity_type */
337
338 void
339 (set_entity_type)(ir_entity *ent, ir_type *type) {
340         _set_entity_type(ent, type);
341 }  /* set_entity_type */
342
343 ir_allocation
344 (get_entity_allocation)(const ir_entity *ent) {
345         return _get_entity_allocation(ent);
346 }  /* get_entity_allocation */
347
348 void
349 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
350         _set_entity_allocation(ent, al);
351 }  /* set_entity_allocation */
352
353 /* return the name of the visibility */
354 const char *get_allocation_name(ir_allocation all)
355 {
356 #define X(a)    case a: return #a
357         switch (all) {
358         X(allocation_automatic);
359         X(allocation_parameter);
360         X(allocation_dynamic);
361         X(allocation_static);
362     default: return "BAD VALUE";
363         }
364 #undef X
365 }  /* get_allocation_name */
366
367
368 ir_visibility
369 (get_entity_visibility)(const ir_entity *ent) {
370         return _get_entity_visibility(ent);
371 }  /* get_entity_visibility */
372
373 void
374 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
375         assert(ent && ent->kind == k_entity);
376         if (vis != visibility_local)
377                 assert((ent->allocation == allocation_static) ||
378                 (ent->allocation == allocation_automatic));
379                 /* @@@ Test that the owner type is not local, but how??
380         && get_class_visibility(get_entity_owner(ent)) != local));*/
381         ent->visibility = vis;
382 }  /* set_entity_visibility */
383
384 /* return the name of the visibility */
385 const char *get_visibility_name(ir_visibility vis)
386 {
387 #define X(a)    case a: return #a
388         switch (vis) {
389         X(visibility_local);
390         X(visibility_external_visible);
391         X(visibility_external_allocated);
392     default: return "BAD VALUE";
393         }
394 #undef X
395 }  /* get_visibility_name */
396
397 ir_variability
398 (get_entity_variability)(const ir_entity *ent) {
399         return _get_entity_variability(ent);
400 }  /* get_entity_variability */
401
402 void
403 set_entity_variability(ir_entity *ent, ir_variability var)
404 {
405         assert(ent && ent->kind == k_entity);
406         if (var == variability_part_constant)
407                 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
408
409         if ((is_compound_type(ent->type)) &&
410                 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
411                 /* Allocate data structures for constant values */
412                 ent->attr.cmpd_attr.values    = NEW_ARR_F(ir_node *, 0);
413                 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
414         }
415         if ((is_atomic_type(ent->type)) &&
416                 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
417                 /* Set default constant value. */
418                 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
419         }
420
421         if ((is_compound_type(ent->type)) &&
422                 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
423                 /* Free data structures for constant values */
424                 DEL_ARR_F(ent->attr.cmpd_attr.values);    ent->attr.cmpd_attr.values    = NULL;
425                 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
426         }
427         ent->variability = var;
428 }  /* set_entity_variability */
429
430 /* return the name of the variability */
431 const char *get_variability_name(ir_variability var)
432 {
433 #define X(a)    case a: return #a
434         switch (var) {
435         X(variability_uninitialized);
436         X(variability_initialized);
437         X(variability_part_constant);
438         X(variability_constant);
439     default: return "BAD VALUE";
440         }
441 #undef X
442 }  /* get_variability_name */
443
444 ir_volatility
445 (get_entity_volatility)(const ir_entity *ent) {
446         return _get_entity_volatility(ent);
447 }  /* get_entity_volatility */
448
449 void
450 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
451         _set_entity_volatility(ent, vol);
452 }  /* set_entity_volatility */
453
454 /* return the name of the volatility */
455 const char *get_volatility_name(ir_volatility var)
456 {
457 #define X(a)    case a: return #a
458         switch (var) {
459         X(volatility_non_volatile);
460         X(volatility_is_volatile);
461     default: return "BAD VALUE";
462         }
463 #undef X
464 }  /* get_volatility_name */
465
466 ir_peculiarity
467 (get_entity_peculiarity)(const ir_entity *ent) {
468         return _get_entity_peculiarity(ent);
469 }  /* get_entity_peculiarity */
470
471 void
472 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
473         _set_entity_peculiarity(ent, pec);
474 }  /* set_entity_peculiarity */
475
476 /* Checks if an entity cannot be overridden anymore. */
477 int (get_entity_final)(const ir_entity *ent) {
478         return _get_entity_final(ent);
479 }  /* get_entity_final */
480
481 /* Sets/resets the final flag of an entity. */
482 void (set_entity_final)(ir_entity *ent, int final) {
483   _set_entity_final(ent, final);
484 }
485
486 /* Checks if an entity is compiler generated */
487 int is_entity_compiler_generated(const ir_entity *ent) {
488         assert(is_entity(ent));
489         return ent->compiler_gen;
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         assert(is_entity(ent));
495         ent->compiler_gen = flag ? 1 : 0;
496 }  /* set_entity_compiler_generated */
497
498 /* Get the entity's stickyness */
499 ir_stickyness
500 (get_entity_stickyness)(const ir_entity *ent) {
501         return _get_entity_stickyness(ent);
502 }  /* get_entity_stickyness */
503
504 /* Set the entity's stickyness */
505 void
506 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
507         _set_entity_stickyness(ent, stickyness);
508 }  /* set_entity_stickyness */
509
510 /* Set has no effect for existent entities of type method. */
511 ir_node *
512 get_atomic_ent_value(ir_entity *ent)
513 {
514         assert(ent && is_atomic_entity(ent));
515         assert(ent->variability != variability_uninitialized);
516         return skip_Id(ent->value);
517 }  /* get_atomic_ent_value */
518
519 void
520 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
521         assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
522         if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
523                 return;
524         ent->value = val;
525 }  /* set_atomic_ent_value */
526
527 /* Returns true if the the node is representable as code on
528  *  const_code_irg. */
529 int is_irn_const_expression(ir_node *n) {
530         ir_mode *m;
531
532         /* we are in danger iff an exception will arise. TODO: be more precisely,
533          * for instance Div. will NOT rise if divisor != 0
534          */
535         if (is_binop(n) && !is_fragile_op(n))
536                 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
537
538         m = get_irn_mode(n);
539         switch (get_irn_opcode(n)) {
540         case iro_Const:
541         case iro_SymConst:
542         case iro_Unknown:
543                 return 1;
544         case iro_Conv:
545         case iro_Cast:
546                 return is_irn_const_expression(get_irn_n(n, 0));
547         default:
548                 break;
549         }
550         return 0;
551 }  /* is_irn_const_expression */
552
553 /*
554  * Copies a firm subgraph that complies to the restrictions for
555  * constant expressions to current_block in current_ir_graph.
556  */
557 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
558         ir_node *nn;
559         ir_mode *m;
560
561         /* @@@ GL I think  we should implement this using the routines from irgopt for
562                dead node elimination/inlineing. */
563
564         m = get_irn_mode(n);
565         switch (get_irn_opcode(n)) {
566         case iro_Const:
567                 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
568                 break;
569         case iro_SymConst:
570                 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
571                         get_SymConst_value_type(n));
572                 break;
573         case iro_Add:
574                 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
575                         copy_const_value(dbg, get_Add_right(n)), m); break;
576         case iro_Sub:
577                 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
578                         copy_const_value(dbg, get_Sub_right(n)), m); break;
579         case iro_Mul:
580                 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
581                         copy_const_value(dbg, get_Mul_right(n)), m); break;
582         case iro_And:
583                 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
584                         copy_const_value(dbg, get_And_right(n)), m); break;
585         case iro_Or:
586                 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
587                         copy_const_value(dbg, get_Or_right(n)), m); break;
588         case iro_Eor:
589                 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
590                         copy_const_value(dbg, get_Eor_right(n)), m); break;
591         case iro_Cast:
592                 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
593         case iro_Conv:
594                 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
595         case iro_Unknown:
596                 nn = new_d_Unknown(m); break;
597         default:
598                 DDMN(n);
599                 assert(0 && "opcode invalid or not implemented");
600                 nn = NULL;
601                 break;
602         }
603         return nn;
604 }  /* copy_const_value */
605
606 /* Creates a new compound graph path. */
607 compound_graph_path *
608 new_compound_graph_path(ir_type *tp, int length) {
609         compound_graph_path *res;
610
611         assert(is_compound_type(tp));
612         assert(length > 0);
613
614         res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
615         memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
616         res->kind         = k_ir_compound_graph_path;
617         res->tp           = tp;
618         res->len          = length;
619
620         return res;
621 }  /* new_compound_graph_path */
622
623 /* Frees an graph path object */
624 void free_compound_graph_path (compound_graph_path *gr) {
625         assert(gr && is_compound_graph_path(gr));
626         gr->kind = k_BAD;
627         free(gr);
628 }  /* free_compound_graph_path */
629
630 /* Returns non-zero if an object is a compound graph path */
631 int is_compound_graph_path(const void *thing) {
632         return (get_kind(thing) == k_ir_compound_graph_path);
633 }  /* is_compound_graph_path */
634
635 /* Checks whether the path up to pos is correct. If the path contains a NULL,
636  *  assumes the path is not complete and returns 'true'. */
637 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
638         int i;
639         ir_entity *node;
640         ir_type *owner = gr->tp;
641
642         for (i = 0; i <= pos; i++) {
643                 node = get_compound_graph_path_node(gr, i);
644                 if (node == NULL)
645                         /* Path not yet complete. */
646                         return 1;
647                 if (get_entity_owner(node) != owner)
648                         return 0;
649                 owner = get_entity_type(node);
650         }
651         if (pos == get_compound_graph_path_length(gr))
652                 if (!is_atomic_type(owner))
653                         return 0;
654                 return 1;
655 }  /* is_proper_compound_graph_path */
656
657 /* Returns the length of a graph path */
658 int get_compound_graph_path_length(const compound_graph_path *gr) {
659         assert(gr && is_compound_graph_path(gr));
660         return gr->len;
661 }  /* get_compound_graph_path_length */
662
663 ir_entity *
664 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
665         assert(gr && is_compound_graph_path(gr));
666         assert(pos >= 0 && pos < gr->len);
667         return gr->list[pos].node;
668 }  /* get_compound_graph_path_node */
669
670 void
671 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
672         assert(gr && is_compound_graph_path(gr));
673         assert(pos >= 0 && pos < gr->len);
674         assert(is_entity(node));
675         gr->list[pos].node = node;
676         assert(is_proper_compound_graph_path(gr, pos));
677 }  /* set_compound_graph_path_node */
678
679 int
680 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
681         assert(gr && is_compound_graph_path(gr));
682         assert(pos >= 0 && pos < gr->len);
683         return gr->list[pos].index;
684 }  /* get_compound_graph_path_array_index */
685
686 void
687 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
688         assert(gr && is_compound_graph_path(gr));
689         assert(pos >= 0 && pos < gr->len);
690         gr->list[pos].index = index;
691 }  /* set_compound_graph_path_array_index */
692
693 /* A value of a compound entity is a pair of value and the corresponding path to a member of
694    the compound. */
695 void
696 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
697         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
698         assert(is_compound_graph_path(path));
699         ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
700         ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
701 }  /* add_compound_ent_value_w_path */
702
703 void
704 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
705         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
706         assert(is_compound_graph_path(path));
707         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
708         ent->attr.cmpd_attr.values[pos]    = val;
709         ent->attr.cmpd_attr.val_paths[pos] = path;
710 }  /* set_compound_ent_value_w_path */
711
712 int
713 get_compound_ent_n_values(ir_entity *ent) {
714         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
715         return ARR_LEN(ent->attr.cmpd_attr.values);
716 }  /* get_compound_ent_n_values */
717
718 ir_node *
719 get_compound_ent_value(ir_entity *ent, int pos) {
720         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
721         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
722         return ent->attr.cmpd_attr.values[pos];
723 }  /* get_compound_ent_value */
724
725 compound_graph_path *
726 get_compound_ent_value_path(ir_entity *ent, int pos) {
727         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
728         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
729         return ent->attr.cmpd_attr.val_paths[pos];
730 }  /* get_compound_ent_value_path */
731
732 /**
733  * Returns non-zero, if two compound_graph_pathes are equal
734  *
735  * @param path1            the first path
736  * @param visited_indices
737  * @param path2            the second path
738  */
739 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
740         int i;
741         int len1 = get_compound_graph_path_length(path1);
742         int len2 = get_compound_graph_path_length(path2);
743
744         if (len2 != len1) return 0;
745
746         for (i = 0; i < len1; i++) {
747                 ir_type *tp;
748                 ir_entity *node1 = get_compound_graph_path_node(path1, i);
749                 ir_entity *node2 = get_compound_graph_path_node(path2, i);
750
751                 if (node1 != node2) return 0;
752
753                 /* FIXME: Strange code. What is it good for? */
754                 tp = get_entity_owner(node1);
755                 if (is_Array_type(tp)) {
756                         long low;
757
758                         /* Compute the index of this node. */
759                         assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
760
761                         low = get_array_lower_bound_int(tp, 0);
762                         if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
763                                 visited_indices[i]++;
764                                 return 0;
765                         } else
766                                 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
767                 }
768         }
769         return 1;
770 }  /* equal_paths */
771
772 /**
773  * Returns the position of a value with the given path.
774  * The path must contain array indices for all array element entities.
775  *
776  * @todo  This implementation is very low and should be replaced when the new tree oriented
777  *        value representation is finally implemented.
778  */
779 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
780         int i, n_paths = get_compound_ent_n_values(ent);
781         int *visited_indices;
782         int path_len = get_compound_graph_path_length(path);
783
784         NEW_ARR_A(int *, visited_indices, path_len);
785         memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
786         for (i = 0; i < n_paths; i ++) {
787                 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
788                         return i;
789         }
790
791 #if 0
792         {
793                 int j;
794                 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
795                 printf("Entity %s : ", get_entity_name(ent));
796                 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
797                         ir_entity *node = get_compound_graph_path_node(path, j);
798                         printf("%s", get_entity_name(node));
799                         if (is_Array_type(get_entity_owner(node)))
800                                 printf("[%d]", get_compound_graph_path_array_index(path, j));
801                 }
802                 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
803         }
804 #endif
805
806         assert(0 && "path not found");
807         return -1;
808 }  /* get_compound_ent_pos_by_path */
809
810 /* Returns a constant value given the access path.
811  *  The path must contain array indices for all array element entities. */
812 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
813         return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
814 }  /* get_compound_ent_value_by_path */
815
816
817 void
818 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
819         int i;
820         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
821         for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
822                 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
823                 if (path->list[path->len-1].node == value_ent) {
824                         for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
825                                 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
826                                 ent->attr.cmpd_attr.values[i]    = ent->attr.cmpd_attr.values[i+1];
827                         }
828                         ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
829                         ARR_SETLEN(ir_node*,   ent->attr.cmpd_attr.values,    ARR_LEN(ent->attr.cmpd_attr.values)    - 1);
830                         break;
831                 }
832         }
833 }  /* remove_compound_ent_value */
834
835 void
836 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
837         compound_graph_path *path;
838         ir_type *owner_tp = get_entity_owner(member);
839         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
840         path = new_compound_graph_path(get_entity_type(ent), 1);
841         path->list[0].node = member;
842         if (is_Array_type(owner_tp)) {
843                 int max;
844                 int i, n;
845
846                 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
847                 max = get_array_lower_bound_int(owner_tp, 0) -1;
848                 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
849                         int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
850                         if (index > max) {
851                                 max = index;
852                         }
853                 }
854                 path->list[0].index = max + 1;
855         }
856         add_compound_ent_value_w_path(ent, val, path);
857 }  /* add_compound_ent_value */
858
859
860 ir_entity *
861 get_compound_ent_value_member(ir_entity *ent, int pos) {
862         compound_graph_path *path;
863         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
864         path = get_compound_ent_value_path(ent, pos);
865
866         return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
867 }  /* get_compound_ent_value_member */
868
869 void
870 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
871         compound_graph_path *path;
872         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
873         path = get_compound_ent_value_path(ent, pos);
874         set_compound_graph_path_node(path, 0, member);
875         set_compound_ent_value_w_path(ent, val, path, pos);
876 }  /* set_compound_ent_value */
877
878 void
879 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
880         int i;
881         ir_graph *rem = current_ir_graph;
882         ir_type *arrtp = get_entity_type(ent);
883         ir_node *val;
884         ir_type *elttp = get_array_element_type(arrtp);
885
886         assert(is_Array_type(arrtp));
887         assert(get_array_n_dimensions(arrtp) == 1);
888         /* One bound is sufficient, the number of constant fields makes the
889            size. */
890         assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
891         assert(get_entity_variability(ent) != variability_uninitialized);
892         current_ir_graph = get_const_code_irg();
893
894         for (i = 0; i < num_vals; i++) {
895                 val = new_Const_type(values[i], elttp);
896                 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
897                 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
898         }
899         current_ir_graph = rem;
900 }  /* set_array_entity_values */
901
902 /* Return the overall offset of value at position pos in bytes. */
903 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
904         compound_graph_path *path;
905         int path_len, i;
906         int offset = 0;
907
908         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
909
910         path = get_compound_ent_value_path(ent, pos);
911         path_len = get_compound_graph_path_length(path);
912
913         for (i = 0; i < path_len; ++i) {
914                 ir_entity *node = get_compound_graph_path_node(path, i);
915                 ir_type *node_tp = get_entity_type(node);
916                 ir_type *owner_tp = get_entity_owner(node);
917
918                 if (is_Array_type(owner_tp)) {
919                         int size  = get_type_size_bits(node_tp);
920                         int align = get_type_alignment_bits(node_tp);
921                         if(size % align > 0) {
922                                 size += align - (size % align);
923                         }
924                         assert(size % 8 == 0);
925                         size /= 8;
926                         offset += size * get_compound_graph_path_array_index(path, i);
927                 } else {
928                         offset += get_entity_offset(node);
929                 }
930         }
931
932         return offset;
933 }  /* get_compound_ent_value_offset_bytes */
934
935 /* Return the offset in bits from the last byte address. */
936 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
937         compound_graph_path *path;
938         int path_len;
939         ir_entity *last_node;
940
941         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
942
943         path = get_compound_ent_value_path(ent, pos);
944         path_len = get_compound_graph_path_length(path);
945         last_node = get_compound_graph_path_node(path, path_len - 1);
946
947         return get_entity_offset_bits_remainder(last_node);
948 }  /* get_compound_ent_value_offset_bit_remainder */
949
950 typedef struct {
951         int n_elems;      /**< number of elements the array can hold */
952         int current_elem; /**< current array index */
953         ir_entity *ent;
954 } array_info;
955
956 /* Compute the array indices in compound graph paths of initialized entities.
957  *
958  *  All arrays must have fixed lower and upper bounds.  One array can
959  *  have an open bound.  If there are several open bounds, we do
960  *  nothing.  There must be initializer elements for all array
961  *  elements.  Uses the link field in the array element entities.  The
962  *  array bounds must be representable as ints.
963  *
964  * WARNING: it is impossible to get this 100% right with the current
965  *          design... (in array of structs you cant know when a struct is
966  *          really finished and the next array element starts)
967  *
968  *  (If the bounds are not representable as ints we have to represent
969  *  the indices as firm nodes.  But still we must be able to
970  *  evaluate the index against the upper bound.)
971  */
972 int compute_compound_ent_array_indices(ir_entity *ent) {
973         ir_type *tp = get_entity_type(ent);
974         int i, n_vals;
975         int max_len = 0;
976         array_info *array_infos;
977
978         assert(is_compound_type(tp));
979
980         if (!is_compound_type(tp) ||
981                 (ent->variability == variability_uninitialized))
982                 return 1;
983
984         n_vals = get_compound_ent_n_values(ent);
985         for(i = 0; i < n_vals; ++i) {
986                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
987                 int len = get_compound_graph_path_length(path);
988                 if(len > max_len)
989                         max_len = len;
990         }
991
992         array_infos = alloca(max_len * sizeof(array_infos[0]));
993         memset(array_infos, 0, max_len * sizeof(array_infos[0]));
994
995         for(i = 0; i < n_vals; ++i) {
996                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
997                 int path_len = get_compound_graph_path_length(path);
998                 int j;
999                 int needadd = 0;
1000                 ir_entity *prev_node = NULL;
1001
1002                 for(j = path_len-1; j >= 0; --j) {
1003                         int dim, dims;
1004                         int n_elems;
1005                         ir_entity *node = get_compound_graph_path_node(path, j);
1006                         const ir_type *node_type = get_entity_type(node);
1007                         array_info *info = &array_infos[j];
1008
1009                         if(is_atomic_entity(node)) {
1010                                 needadd = 1;
1011                                 set_compound_graph_path_array_index(path, j, -1);
1012                                 prev_node = node;
1013                                 continue;
1014                         } else if(is_compound_type(node_type) && !is_Array_type(node_type)) {
1015                                 int n_members = get_compound_n_members(node_type);
1016                                 ir_entity *last = get_compound_member(node_type, n_members - 1);
1017                                 if(needadd && last == prev_node) {
1018                                         needadd = 1;
1019                                 } else {
1020                                         needadd = 0;
1021                                 }
1022                                 set_compound_graph_path_array_index(path, j, -1);
1023                                 prev_node = node;
1024                                 continue;
1025                         }
1026
1027                         if(info->ent != node) {
1028                                 n_elems = 1;
1029                                 dims = get_array_n_dimensions(node_type);
1030                                 for(dim = 0; dim < dims; ++dim) {
1031                                         long lower_bound = 0;
1032                                         long upper_bound = -1;
1033
1034                                         if(has_array_lower_bound(node_type, 0)) {
1035                                                 lower_bound = get_array_lower_bound_int(node_type, 0);
1036                                         }
1037                                         if(has_array_upper_bound(node_type, 0)) {
1038                                                 upper_bound = get_array_upper_bound_int(node_type, 0);
1039                                                 assert(upper_bound >= lower_bound);
1040                                                 n_elems *= (upper_bound - lower_bound);
1041                                         } else {
1042                                                 assert(dim == dims-1);
1043                                                 n_elems = -1;
1044                                         }
1045                                 }
1046
1047                                 info->ent = node;
1048                                 info->n_elems = n_elems;
1049                                 info->current_elem = 0;
1050                         }
1051
1052                         set_compound_graph_path_array_index(path, j, info->current_elem);
1053
1054                         if(needadd) {
1055                                 info->current_elem++;
1056                                 if(info->current_elem >= info->n_elems) {
1057                                         needadd = 1;
1058                                         info->current_elem = 0;
1059                                 } else {
1060                                         needadd = 0;
1061                                 }
1062                         }
1063
1064                         prev_node = node;
1065                 }
1066         }
1067
1068         return 1;
1069 }  /* compute_compound_ent_array_indices */
1070
1071 int
1072 (get_entity_offset)(const ir_entity *ent) {
1073         return _get_entity_offset(ent);
1074 }  /* get_entity_offset */
1075
1076 void
1077 (set_entity_offset)(ir_entity *ent, int offset) {
1078         _set_entity_offset(ent, offset);
1079 }  /* set_entity_offset */
1080
1081 unsigned char
1082 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1083         return _get_entity_offset_bits_remainder(ent);
1084 }  /* get_entity_offset_bits_remainder */
1085
1086 void
1087 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1088         _set_entity_offset_bits_remainder(ent, offset);
1089 }  /* set_entity_offset_bits_remainder */
1090
1091 void
1092 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1093 #ifndef NDEBUG
1094         ir_type *owner     = get_entity_owner(ent);
1095         ir_type *ovw_ovner = get_entity_owner(overwritten);
1096         assert(is_Class_type(owner));
1097         assert(is_Class_type(ovw_ovner));
1098         assert(! is_class_final(ovw_ovner));
1099 #endif /* NDEBUG */
1100         ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1101         ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1102 }  /* add_entity_overwrites */
1103
1104 int
1105 get_entity_n_overwrites(ir_entity *ent) {
1106         assert(is_Class_type(get_entity_owner(ent)));
1107         return (ARR_LEN(ent->overwrites));
1108 }  /* get_entity_n_overwrites */
1109
1110 int
1111 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1112         int i;
1113         assert(is_Class_type(get_entity_owner(ent)));
1114         for (i = 0; i < get_entity_n_overwrites(ent); i++)
1115                 if (get_entity_overwrites(ent, i) == overwritten)
1116                         return i;
1117         return -1;
1118 }  /* get_entity_overwrites_index */
1119
1120 ir_entity *
1121 get_entity_overwrites(ir_entity *ent, int pos) {
1122         assert(is_Class_type(get_entity_owner(ent)));
1123         assert(pos < get_entity_n_overwrites(ent));
1124         return ent->overwrites[pos];
1125 }  /* get_entity_overwrites */
1126
1127 void
1128 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1129         assert(is_Class_type(get_entity_owner(ent)));
1130         assert(pos < get_entity_n_overwrites(ent));
1131         ent->overwrites[pos] = overwritten;
1132 }  /* set_entity_overwrites */
1133
1134 void
1135 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1136         int i;
1137         assert(is_Class_type(get_entity_owner(ent)));
1138         for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1139                 if (ent->overwrites[i] == overwritten) {
1140                         for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1141                                 ent->overwrites[i] = ent->overwrites[i+1];
1142                         ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1143                         break;
1144                 }
1145 }  /* remove_entity_overwrites */
1146
1147 void
1148 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1149         add_entity_overwrites(overwrites, ent);
1150 }  /* add_entity_overwrittenby */
1151
1152 int
1153 get_entity_n_overwrittenby(ir_entity *ent) {
1154         assert(is_Class_type(get_entity_owner(ent)));
1155         return (ARR_LEN (ent->overwrittenby));
1156 }  /* get_entity_n_overwrittenby */
1157
1158 int
1159 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1160         int i;
1161         assert(is_Class_type(get_entity_owner(ent)));
1162         for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1163                 if (get_entity_overwrittenby(ent, i) == overwrites)
1164                         return i;
1165         return -1;
1166 }  /* get_entity_overwrittenby_index */
1167
1168 ir_entity *
1169 get_entity_overwrittenby(ir_entity *ent, int pos) {
1170         assert(is_Class_type(get_entity_owner(ent)));
1171         assert(pos < get_entity_n_overwrittenby(ent));
1172         return ent->overwrittenby[pos];
1173 }  /* get_entity_overwrittenby */
1174
1175 void
1176 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1177         assert(is_Class_type(get_entity_owner(ent)));
1178         assert(pos < get_entity_n_overwrittenby(ent));
1179         ent->overwrittenby[pos] = overwrites;
1180 }  /* set_entity_overwrittenby */
1181
1182 void    remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1183         int i;
1184         assert(is_Class_type(get_entity_owner(ent)));
1185         for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1186                 if (ent->overwrittenby[i] == overwrites) {
1187                         for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1188                                 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1189                         ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1190                         break;
1191                 }
1192 }  /* remove_entity_overwrittenby */
1193
1194 /* A link to store intermediate information */
1195 void *
1196 (get_entity_link)(const ir_entity *ent) {
1197         return _get_entity_link(ent);
1198 }  /* get_entity_link */
1199
1200 void
1201 (set_entity_link)(ir_entity *ent, void *l) {
1202         _set_entity_link(ent, l);
1203 }  /* set_entity_link */
1204
1205 ir_graph *
1206 (get_entity_irg)(const ir_entity *ent) {
1207         return _get_entity_irg(ent);
1208 }  /* get_entity_irg */
1209
1210 void
1211 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1212         assert(is_method_entity(ent));
1213         /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1214          * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1215          * aber erhalten bleiben soll?  Wandle die Entitaet in description oder
1216          * inherited um! */
1217         /* assert(irg); */
1218         assert((irg  && ent->peculiarity == peculiarity_existent) ||
1219                 (!irg && (ent->peculiarity == peculiarity_existent)
1220                 && (ent -> visibility == visibility_external_allocated)) ||
1221                 (!irg && ent->peculiarity == peculiarity_description) ||
1222                 (!irg && ent->peculiarity == peculiarity_inherited));
1223         ent->attr.mtd_attr.irg = irg;
1224 }  /* set_entity_irg */
1225
1226 unsigned get_entity_vtable_number(const ir_entity *ent) {
1227         assert(is_method_entity((ir_entity *)ent));
1228         return ent->attr.mtd_attr.vtable_number;
1229 }  /* get_entity_vtable_number */
1230
1231 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1232         assert(is_method_entity(ent));
1233         ent->attr.mtd_attr.vtable_number = vtable_number;
1234 }  /* set_entity_vtable_number */
1235
1236 /* Returns the section of a method. */
1237 ir_img_section get_method_img_section(const ir_entity *ent) {
1238         assert(is_method_entity((ir_entity *)ent));
1239         return ent->attr.mtd_attr.section;
1240 }  /* get_method_img_section */
1241
1242 /* Sets the section of a method. */
1243 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1244         assert(is_method_entity(ent));
1245         ent->attr.mtd_attr.section = section;
1246 }  /* set_method_img_section */
1247
1248 int
1249 (is_entity)(const void *thing) {
1250         return _is_entity(thing);
1251 }  /* is_entity */
1252
1253 int is_atomic_entity(ir_entity *ent) {
1254         ir_type *t      = get_entity_type(ent);
1255         const tp_op *op = get_type_tpop(t);
1256         return (op == type_primitive || op == type_pointer ||
1257                 op == type_enumeration || op == type_method);
1258 }  /* is_atomic_entity */
1259
1260 int is_compound_entity(ir_entity *ent) {
1261         ir_type     *t  = get_entity_type(ent);
1262         const tp_op *op = get_type_tpop(t);
1263         return (op == type_class || op == type_struct ||
1264                 op == type_array || op == type_union);
1265 }  /* is_compound_entity */
1266
1267 int is_method_entity(ir_entity *ent) {
1268         ir_type *t = get_entity_type(ent);
1269         return is_Method_type(t);
1270 }  /* is_method_entity */
1271
1272 /**
1273  * @todo not implemented!!! */
1274 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1275         fprintf(stderr, " calling unimplemented equal entity!!! \n");
1276         return 1;
1277 }  /* equal_entity */
1278
1279
1280 unsigned long (get_entity_visited)(ir_entity *ent) {
1281         return _get_entity_visited(ent);
1282 }  /* get_entity_visited */
1283
1284 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1285         _set_entity_visited(ent, num);
1286 }  /* set_entity_visited */
1287
1288 /* Sets visited field in ir_entity to entity_visited. */
1289 void (mark_entity_visited)(ir_entity *ent) {
1290         _mark_entity_visited(ent);
1291 }  /* mark_entity_visited */
1292
1293 int (entity_visited)(ir_entity *ent) {
1294         return _entity_visited(ent);
1295 }  /* entity_visited */
1296
1297 int (entity_not_visited)(ir_entity *ent) {
1298         return _entity_not_visited(ent);
1299 }  /* entity_not_visited */
1300
1301 /* Returns the mask of the additional entity properties. */
1302 unsigned get_entity_additional_properties(ir_entity *ent) {
1303         ir_graph *irg;
1304
1305         assert(is_method_entity(ent));
1306
1307         /* first check, if the graph has additional properties */
1308         irg = get_entity_irg(ent);
1309
1310         if (irg)
1311                 return get_irg_additional_properties(irg);
1312
1313         if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1314                 return get_method_additional_properties(get_entity_type(ent));
1315
1316         return ent->attr.mtd_attr.irg_add_properties;
1317 }  /* get_entity_additional_properties */
1318
1319 /* Sets the mask of the additional graph properties. */
1320 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1321 {
1322         ir_graph *irg;
1323
1324         assert(is_method_entity(ent));
1325
1326         /* first check, if the graph exists */
1327         irg = get_entity_irg(ent);
1328         if (irg)
1329                 set_irg_additional_properties(irg, property_mask);
1330         else {
1331     /* do not allow to set the mtp_property_inherited flag or
1332                 * the automatic inheritance of flags will not work */
1333                 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1334         }
1335 }  /* set_entity_additional_properties */
1336
1337 /* Sets one additional graph property. */
1338 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1339 {
1340         ir_graph *irg;
1341
1342         assert(is_method_entity(ent));
1343
1344         /* first check, if the graph exists */
1345         irg = get_entity_irg(ent);
1346         if (irg)
1347                 set_irg_additional_property(irg, flag);
1348         else {
1349                 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1350
1351                 if (mask & mtp_property_inherited)
1352                         mask = get_method_additional_properties(get_entity_type(ent));
1353
1354                         /* do not allow to set the mtp_property_inherited flag or
1355                 * the automatic inheritance of flags will not work */
1356                 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1357         }
1358 }  /* set_entity_additional_property */
1359
1360 /* Returns the class type that this type info entity represents or NULL
1361    if ent is no type info entity. */
1362 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1363         return _get_entity_repr_class(ent);
1364 }  /* get_entity_repr_class */
1365
1366 /* Initialize entity module. */
1367 void firm_init_entity(void)
1368 {
1369         symconst_symbol sym;
1370
1371         assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1372         assert(!unknown_entity && "Call firm_init_entity() only once!");
1373
1374         unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1375         set_entity_visibility(unknown_entity, visibility_external_allocated);
1376         set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1377
1378         current_ir_graph      = get_const_code_irg();
1379         sym.entity_p          = unknown_entity;
1380         unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1381 }  /* firm_init_entity */