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