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