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