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