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