ev. removed mem leak: overwrites array allocated for all ents, freed only if owner...
[libfirm] / ir / tr / entity.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity.c
4  * Purpose:     Representation of all program known entities.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include <stdlib.h>
18 # include <stddef.h>
19 # include <string.h>
20
21 # include "entity_t.h"
22 # include "mangle.h"
23 # include "typegmod.h"
24 # include "array.h"
25 /* All this is needed to build the constant node for methods: */
26 # include "irprog_t.h"
27 # include "ircons.h"
28 # include "tv_t.h"
29
30 /*******************************************************************/
31 /** general                                                       **/
32 /*******************************************************************/
33
34 void
35 init_entity (void)
36 {
37 }
38
39 /*-----------------------------------------------------------------*/
40 /* ENTITY                                                          */
41 /*-----------------------------------------------------------------*/
42
43 void insert_entity_in_owner (entity *ent) {
44   type *owner = ent->owner;
45   switch (get_type_tpop_code(owner)) {
46   case tpo_class: {
47     add_class_member (owner, ent);
48   } break;
49   case tpo_struct: {
50     add_struct_member (owner, ent);
51   } break;
52   case tpo_union: {
53     add_union_member (owner, ent);
54   } break;
55   case tpo_array: {
56     set_array_element_entity(owner, ent);
57   } break;
58   default: assert(0);
59   }
60 }
61
62 entity *
63 new_entity (type *owner, ident *name, type *type)
64 {
65   entity *res;
66   ir_graph *rem;
67
68   assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
69
70   res = (entity *) xmalloc (sizeof (entity));
71   res->kind = k_entity;
72   assert_legal_owner_of_ent(owner);
73   res->owner = owner;
74   res->name = name;
75   res->type = type;
76
77   if (get_type_tpop(type) == type_method)
78     res->allocation = allocation_static;
79   else
80     res->allocation = allocation_automatic;
81
82   res->visibility = visibility_local;
83   res->offset = -1;
84   if (is_method_type(type)) {
85     res->variability = variability_constant;
86     rem = current_ir_graph;
87     current_ir_graph = get_const_code_irg();
88     res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
89     current_ir_graph = rem;
90   } else {
91     res->variability = variability_uninitialized;
92     res->value  = NULL;
93     res->values = NULL;
94     res->val_paths = NULL;
95   }
96   res->peculiarity   = peculiarity_existent;
97   res->volatility    = volatility_non_volatile;
98   res->ld_name       = NULL;
99   if (is_class_type(owner)) {
100     res->overwrites    = NEW_ARR_F(entity *, 0);
101     res->overwrittenby = NEW_ARR_F(entity *, 0);
102   } else {
103     res->overwrites    = NULL;
104     res->overwrittenby = NULL;
105   }
106   res->irg = NULL;
107
108 #ifdef DEBUG_libfirm
109   res->nr = get_irp_new_node_nr();
110 #endif
111
112   res->visit = 0;
113
114   /* Remember entity in it's owner. */
115   insert_entity_in_owner (res);
116   return res;
117 }
118 entity *
119 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
120   entity *res = new_entity(owner, name, type);
121   set_entity_dbg_info(res, db);
122   return res;
123 }
124
125 void    free_compound_graph_path (compound_graph_path *gr);
126 int     is_compound_graph_path(void *thing);
127 int     get_compound_graph_path_length(compound_graph_path *gr);
128 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
129 int     get_compound_ent_n_values(entity *ent);
130
131 void free_entity_attrs(entity *ent) {
132   int i;
133   assert(ent);
134   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
135     DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
136     DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
137   } else {
138     assert(ent->overwrites == NULL);
139     assert(ent->overwrittenby == NULL);
140   }
141   /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
142   if (ent->val_paths) {
143     if (is_compound_entity(ent))
144       for (i = 0; i < get_compound_ent_n_values(ent); i++)
145         if (ent->val_paths[i]) ;
146     /* free_compound_graph_path(ent->val_paths[i]) ;  * @@@ warum nich? */
147     /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
148     /* DEL_ARR_F(ent->val_paths); */
149   }
150   ent->val_paths = NULL;
151   ent->values = NULL;
152 }
153
154 entity *
155 copy_entity_own (entity *old, type *new_owner) {
156   entity *new;
157
158   assert_legal_owner_of_ent(new_owner);
159   if (old->owner == new_owner) return old;
160   new = (entity *) xmalloc (sizeof (entity));
161   memcpy (new, old, sizeof (entity));
162   new->owner = new_owner;
163   if (is_class_type(new_owner)) {
164     new->overwrites    = NEW_ARR_F(entity *, 0);
165     new->overwrittenby = NEW_ARR_F(entity *, 0);
166   }
167 #ifdef DEBUG_libfirm
168   new->nr = get_irp_new_node_nr();
169 #endif
170
171   insert_entity_in_owner (new);
172
173   return new;
174 }
175
176 entity *
177 copy_entity_name (entity *old, ident *new_name) {
178   entity *new;
179
180   if (old->name == new_name) return old;
181   new = (entity *) xmalloc (sizeof (entity));
182   memcpy (new, old, sizeof (entity));
183   new->name = new_name;
184   new->ld_name = NULL;
185   if (is_class_type(new->owner)) {
186     new->overwrites    = DUP_ARR_F(entity *, old->overwrites);
187     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
188   }
189 #ifdef DEBUG_libfirm
190   new->nr = get_irp_new_node_nr();
191 #endif
192
193   insert_entity_in_owner (new);
194
195   return new;
196 }
197
198
199 void
200 free_entity (entity *ent) {
201   free_tarval_entity(ent);
202   free_entity_attrs(ent);
203   ent->kind = k_BAD;
204   free(ent);
205 }
206
207 /* Outputs a unique number for this node */
208 long
209 get_entity_nr(entity *ent) {
210   assert(ent);
211 #ifdef DEBUG_libfirm
212   return ent->nr;
213 #else
214   return 0;
215 #endif
216 }
217
218 const char *
219 get_entity_name (entity *ent) {
220   assert (ent);
221   return get_id_str(get_entity_ident(ent));
222 }
223
224 ident *
225 get_entity_ident    (entity *ent) {
226   assert(ent);
227   return ent->name;
228 }
229
230 /*
231 void   set_entitye_ld_name  (entity *, char *ld_name);
232 void   set_entity_ld_ident (entity *, ident *ld_ident);
233 */
234
235 type *
236 get_entity_owner (entity *ent) {
237   return ent->owner = skip_tid(ent->owner);
238 }
239
240 void
241 set_entity_owner (entity *ent, type *owner) {
242   assert_legal_owner_of_ent(owner);
243   ent->owner = owner;
244 }
245
246 void   /* should this go into type.c? */
247 assert_legal_owner_of_ent(type *owner) {
248   assert (get_type_tpop_code(owner) == tpo_class ||
249           get_type_tpop_code(owner) == tpo_union ||
250           get_type_tpop_code(owner) == tpo_struct ||
251       get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
252                             -- to select fields! */
253 }
254
255 ident *
256 get_entity_ld_ident (entity *ent)
257 {
258   if (ent->ld_name == NULL)
259     ent->ld_name = mangle_entity (ent);
260   return ent->ld_name;
261 }
262
263 void
264 set_entity_ld_ident (entity *ent, ident *ld_ident) {
265   ent->ld_name = ld_ident;
266 }
267
268 const char *
269 get_entity_ld_name (entity *ent) {
270   return get_id_str(get_entity_ld_ident(ent));
271 }
272
273 /*
274 char  *get_entity_ld_name  (entity *);
275 void   set_entity_ld_name  (entity *, char *ld_name);
276 */
277
278 type *
279 get_entity_type (entity *ent) {
280   return ent->type = skip_tid(ent->type);
281 }
282
283 void
284 set_entity_type (entity *ent, type *type) {
285   ent->type = type;
286 }
287
288
289 ent_allocation
290 get_entity_allocation (entity *ent) {
291   return ent->allocation;
292 }
293
294 void
295 set_entity_allocation (entity *ent, ent_allocation al) {
296   ent->allocation = al;
297 }
298
299 /* return the name of the visibility */
300 const char *get_allocation_name(ent_allocation all)
301 {
302 #define X(a)    case a: return #a
303   switch (all) {
304     X(allocation_automatic);
305     X(allocation_parameter);
306     X(allocation_dynamic);
307     X(allocation_static);
308     default: return "BAD VALUE";
309   }
310 #undef X
311 }
312
313
314 ent_visibility
315 get_entity_visibility (entity *ent) {
316   return ent->visibility;
317 }
318
319 void
320 set_entity_visibility (entity *ent, ent_visibility vis) {
321   if (vis != visibility_local)
322     assert((ent->allocation == allocation_static) ||
323        (ent->allocation == allocation_automatic));
324   /* @@@ Test that the owner type is not local, but how??
325          && get_class_visibility(get_entity_owner(ent)) != local));*/
326   ent->visibility = vis;
327 }
328
329 /* return the name of the visibility */
330 const char *get_visibility_name(ent_visibility vis)
331 {
332 #define X(a)    case a: return #a
333   switch (vis) {
334     X(visibility_local);
335     X(visibility_external_visible);
336     X(visibility_external_allocated);
337     default: return "BAD VALUE";
338   }
339 #undef X
340 }
341
342 ent_variability
343 get_entity_variability (entity *ent) {
344   return ent->variability;
345 }
346
347 void
348 set_entity_variability (entity *ent, ent_variability var)
349 {
350   if (var == variability_part_constant)
351     assert(is_class_type(ent->type) || is_struct_type(ent->type));
352
353   if ((is_compound_type(ent->type)) &&
354       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
355     /* Allocate datastructures for constant values */
356     ent->values    = NEW_ARR_F(ir_node *, 0);
357     ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
358   }
359
360   if ((is_compound_type(ent->type)) &&
361       (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
362     /* Free datastructures for constant values */
363     DEL_ARR_F(ent->values);    ent->values    = NULL;
364     DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
365   }
366   ent->variability = var;
367 }
368
369 /* return the name of the variablity */
370 const char *get_variability_name(ent_variability var)
371 {
372 #define X(a)    case a: return #a
373   switch (var) {
374     X(variability_uninitialized);
375     X(variability_initialized);
376     X(variability_part_constant);
377     X(variability_constant);
378     default: return "BAD VALUE";
379   }
380 #undef X
381 }
382
383 ent_volatility
384 get_entity_volatility (entity *ent) {
385   assert (ent);
386   return ent->volatility;
387 }
388
389 void
390 set_entity_volatility (entity *ent, ent_volatility vol) {
391   assert (ent);
392   ent->volatility = vol;
393 }
394
395 /* return the name of the volatility */
396 const char *get_volatility_name(ent_volatility var)
397 {
398 #define X(a)    case a: return #a
399   switch (var) {
400     X(volatility_non_volatile);
401     X(volatility_is_volatile);
402     default: return "BAD VALUE";
403   }
404 #undef X
405 }
406
407 peculiarity
408 get_entity_peculiarity (entity *ent) {
409   assert (ent);
410   return ent->peculiarity;
411 }
412
413 void
414 set_entity_peculiarity (entity *ent, peculiarity pec) {
415   assert (ent);
416   /* @@@ why peculiarity only for methods? */
417   assert (is_method_type(ent->type));
418   ent->peculiarity = pec;
419 }
420
421 /* return the name of the peculiarity */
422 const char *get_peculiarity_name(peculiarity var)
423 {
424 #define X(a)    case a: return #a
425   switch (var) {
426     X(peculiarity_description);
427     X(peculiarity_inherited);
428     X(peculiarity_existent);
429     default: return "BAD VALUE";
430   }
431 #undef X
432 }
433
434 /* Set has no effect for existent entities of type method. */
435 ir_node *
436 get_atomic_ent_value(entity *ent)
437 {
438   assert(ent);
439   assert(is_atomic_entity(ent));
440   assert(ent->variability != variability_uninitialized);
441   return ent->value;
442 }
443
444 void
445 set_atomic_ent_value(entity *ent, ir_node *val) {
446   assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
447   if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
448     return;
449   ent->value = val;
450 }
451
452 /* Returns true if the the node is representable as code on
453  *  const_code_irg. */
454 int is_irn_const_expression(ir_node *n) {
455   ir_mode *m;
456
457   m = get_irn_mode(n);
458   switch(get_irn_opcode(n)) {
459   case iro_Const:
460   case iro_SymConst:
461   case iro_Unknown:
462     return true; break;
463   case iro_Add:
464     if (is_irn_const_expression(get_Add_left(n)))
465       return is_irn_const_expression(get_Add_right(n));
466   case iro_Conv:
467   case iro_Cast:
468     return is_irn_const_expression(get_irn_n(n, 0));
469   default:
470     return false;
471     break;
472   }
473   return false;
474 }
475
476
477 ir_node *copy_const_value(ir_node *n) {
478   ir_node *nn;
479   ir_mode *m;
480
481   m = get_irn_mode(n);
482   switch(get_irn_opcode(n)) {
483   case iro_Const:
484     nn = new_Const(m, get_Const_tarval(n)); break;
485   case iro_SymConst:
486     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
487   case iro_Add:
488     nn = new_Add(copy_const_value(get_Add_left(n)),
489          copy_const_value(get_Add_right(n)), m); break;
490   case iro_Cast:
491     nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
492   case iro_Conv:
493     nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
494   case iro_Unknown:
495     nn = new_Unknown(m); break;
496   default:
497     DDMN(n);
498     assert(0 && "opdope invalid or not implemented");
499     nn = NULL;
500     break;
501   }
502   return nn;
503 }
504
505 compound_graph_path *
506 new_compound_graph_path(type *tp, int length) {
507   compound_graph_path *res;
508   assert(is_type(tp) && is_compound_type(tp));
509   assert(length > 0);
510
511   res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
512   res->kind = k_ir_compound_graph_path;
513   res->tp = tp;
514   res->len = length;
515   memset(res->nodes, 0, sizeof(entity *) * length);
516   return res;
517 }
518
519 void
520 free_compound_graph_path (compound_graph_path *gr) {
521   assert(gr && is_compound_graph_path(gr));
522   gr->kind = k_BAD;
523   free(gr);
524 }
525
526 int
527 is_compound_graph_path(void *thing) {
528   return (get_kind(thing) == k_ir_compound_graph_path);
529 }
530
531 /* checks whether nodes 0..pos are correct (all lie on a path.) */
532 /* @@@ not implemented */
533 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
534   int i;
535   entity *node;
536   type *owner = gr->tp;
537   for (i = 0; i <= pos; i++) {
538     node = get_compound_graph_path_node(gr, i);
539     if (get_entity_owner(node) != owner) return false;
540     owner = get_entity_type(node);
541   }
542   if (pos == get_compound_graph_path_length(gr))
543     if (!is_atomic_type(owner)) return false;
544   return true;
545 }
546
547 int
548 get_compound_graph_path_length(compound_graph_path *gr) {
549   assert(gr && is_compound_graph_path(gr));
550   return gr->len;
551 }
552
553 entity *
554 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
555   assert(gr && is_compound_graph_path(gr));
556   assert(pos >= 0 && pos < gr->len);
557   return gr->nodes[pos];
558 }
559
560 void
561 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
562   assert(gr && is_compound_graph_path(gr));
563   assert(pos >= 0 && pos < gr->len);
564   assert(is_entity(node));
565   gr->nodes[pos] = node;
566   assert(is_proper_compound_graph_path(gr, pos));
567 }
568
569 /* A value of a compound entity is a pair of value and the corresponding path to a member of
570    the compound. */
571 void
572 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
573   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
574   ARR_APP1 (ir_node *, ent->values, val);
575   ARR_APP1 (compound_graph_path *, ent->val_paths, path);
576 }
577
578 void
579 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
580   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
581   ent->values[pos] = val;
582   ent->val_paths[pos] = path;
583 }
584
585 int
586 get_compound_ent_n_values(entity *ent) {
587   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
588   return (ARR_LEN (ent->values));
589 }
590
591 ir_node  *
592 get_compound_ent_value(entity *ent, int pos) {
593   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
594   return ent->values[pos];
595 }
596
597 compound_graph_path *
598 get_compound_ent_value_path(entity *ent, int pos) {
599   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
600   return ent->val_paths[pos];
601 }
602
603 void
604 remove_compound_ent_value(entity *ent, entity *value_ent) {
605   int i;
606   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
607   for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
608     compound_graph_path *path = ent->val_paths[i];
609     if (path->nodes[path->len-1] == value_ent) {
610       for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
611     ent->val_paths[i] = ent->val_paths[i+1];
612     ent->values[i]   = ent->values[i+1];
613       }
614       ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
615       ARR_SETLEN(ir_node*, ent->values,    ARR_LEN(ent->values)    - 1);
616       break;
617     }
618   }
619 }
620
621 void
622 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
623   compound_graph_path *path;
624   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
625   path = new_compound_graph_path(get_entity_owner(ent), 1);
626   path->nodes[0] = member;
627   add_compound_ent_value_w_path(ent, val, path);
628 }
629
630 /* Copies the firm subgraph referenced by val to const_code_irg and adds
631    the node as constant initialization to ent.
632    The subgraph may not contain control flow operations.
633 void
634 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
635   ir_graph *rem = current_ir_graph;
636
637   assert(get_entity_variability(ent) != variability_uninitialized);
638   current_ir_graph = get_const_code_irg();
639
640   val = copy_const_value(val);
641   add_compound_ent_value(ent, val, member);
642   current_ir_graph = rem;
643   }*/
644
645 /* Copies the value i of the entity to current_block in current_ir_graph.
646 ir_node *
647 copy_compound_ent_value(entity *ent, int pos) {
648   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
649   return copy_const_value(ent->values[pos+1]);
650   }*/
651
652 entity   *
653 get_compound_ent_value_member(entity *ent, int pos) {
654   compound_graph_path *path;
655   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
656   path = get_compound_ent_value_path(ent, pos);
657
658   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
659 }
660
661 void
662 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
663   compound_graph_path *path;
664   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
665   path = get_compound_ent_value_path(ent, pos);
666   set_compound_graph_path_node(path, 0, member);
667   set_compound_ent_value_w_path(ent, val, path, pos);
668 }
669
670 void
671 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
672   int i;
673   ir_graph *rem = current_ir_graph;
674   type *arrtp = get_entity_type(ent);
675   ir_node *val;
676
677   assert(is_array_type(arrtp));
678   assert(get_array_n_dimensions(arrtp) == 1);
679   /* One bound is sufficient, the nunmber of constant fields makes the
680      size. */
681   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
682   assert(get_entity_variability(ent) != variability_uninitialized);
683   current_ir_graph = get_const_code_irg();
684
685   for (i = 0; i < num_vals; i++) {
686     val = new_Const(get_tarval_mode (values[i]), values[i]);
687     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
688   }
689   current_ir_graph = rem;
690 }
691
692 int
693 get_entity_offset (entity *ent) {
694   return ent->offset;
695 }
696
697 void
698 set_entity_offset (entity *ent, int offset) {
699   ent->offset = offset;
700 }
701
702 void
703 add_entity_overwrites   (entity *ent, entity *overwritten) {
704   assert(ent);
705   assert(is_class_type(get_entity_owner(ent)));
706   ARR_APP1 (entity *, ent->overwrites, overwritten);
707   ARR_APP1 (entity *, overwritten->overwrittenby, ent);
708 }
709
710 int
711 get_entity_n_overwrites (entity *ent) {
712   assert(ent);
713   assert(is_class_type(get_entity_owner(ent)));
714   return (ARR_LEN (ent->overwrites));
715 }
716
717 int
718 get_entity_overwrites_index(entity *ent, entity *overwritten) {
719   int i;
720   assert(ent && is_class_type(get_entity_owner(ent)));
721   for (i = 0; i < get_entity_n_overwrites(ent); i++)
722     if (get_entity_overwrites(ent, i) == overwritten)
723       return i;
724   return -1;
725 }
726
727 entity *
728 get_entity_overwrites   (entity *ent, int pos) {
729   assert(ent);
730   assert(is_class_type(get_entity_owner(ent)));
731   assert(pos < get_entity_n_overwrites(ent));
732   return ent->overwrites[pos];
733 }
734
735 void
736 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
737   assert(ent);
738   assert(is_class_type(get_entity_owner(ent)));
739   assert(pos < get_entity_n_overwrites(ent));
740   ent->overwrites[pos] = overwritten;
741 }
742
743 void
744 remove_entity_overwrites(entity *ent, entity *overwritten) {
745   int i;
746   assert(ent && is_class_type(get_entity_owner(ent)));
747   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
748     if (ent->overwrites[i] == overwritten) {
749       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
750         ent->overwrites[i] = ent->overwrites[i+1];
751       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
752       break;
753     }
754 }
755
756 void
757 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
758   assert(ent);
759   assert(is_class_type(get_entity_owner(ent)));
760   add_entity_overwrites(overwrites, ent);
761 }
762
763 int
764 get_entity_n_overwrittenby (entity *ent) {
765   assert(ent);
766   assert(is_class_type(get_entity_owner(ent)));
767   return (ARR_LEN (ent->overwrittenby));
768 }
769
770 int
771 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
772   int i;
773   assert(ent && is_class_type(get_entity_owner(ent)));
774   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
775     if (get_entity_overwrittenby(ent, i) == overwrites)
776       return i;
777   return -1;
778 }
779
780 entity *
781 get_entity_overwrittenby   (entity *ent, int pos) {
782   assert(ent);
783   assert(is_class_type(get_entity_owner(ent)));
784   assert(pos < get_entity_n_overwrittenby(ent));
785   return ent->overwrittenby[pos];
786 }
787
788 void
789 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
790   assert(ent);
791   assert(is_class_type(get_entity_owner(ent)));
792   assert(pos < get_entity_n_overwrittenby(ent));
793   ent->overwrittenby[pos] = overwrites;
794 }
795
796 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
797   int i;
798   assert(ent && is_class_type(get_entity_owner(ent)));
799   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
800     if (ent->overwrittenby[i] == overwrites) {
801       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
802         ent->overwrittenby[i] = ent->overwrittenby[i+1];
803       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
804       break;
805     }
806 }
807
808 /* A link to store intermediate information */
809 void *
810 get_entity_link(entity *ent) {
811   assert(ent);
812   return ent->link;
813 }
814
815 void
816 set_entity_link(entity *ent, void *l) {
817   assert(ent);
818   ent->link = l;
819 }
820
821 ir_graph *
822 get_entity_irg(entity *ent) {
823   assert (ent);
824   assert (is_method_type(ent->type));
825   return ent->irg;
826 }
827
828 void
829 set_entity_irg(entity *ent, ir_graph *irg) {
830   assert (ent && ent->type);
831   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
832    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
833    * aber erhalten bleiben soll. */
834   /* assert (irg); */
835   assert (is_method_type(ent->type));
836   assert (ent->peculiarity == peculiarity_existent);
837   ent->irg = irg;
838 }
839
840 int is_entity (void *thing) {
841   assert(thing);
842   if (get_kind(thing) == k_entity)
843     return 1;
844   else
845     return 0;
846 }
847
848 int is_atomic_entity(entity *ent) {
849   type* t = get_entity_type(ent);
850   return (is_primitive_type(t) || is_pointer_type(t) ||
851       is_enumeration_type(t) || is_method_type(t));
852 }
853
854 int is_compound_entity(entity *ent) {
855   type* t = get_entity_type(ent);
856   return (is_class_type(t) || is_struct_type(t) ||
857       is_array_type(t) || is_union_type(t));
858 }
859
860 /* @@@ not implemnted!!! */
861 bool equal_entity(entity *ent1, entity *ent2) {
862   printf(" calling unimplemented equal entity!!! \n");
863   return true;
864 }
865
866
867 unsigned long get_entity_visited(entity *ent) {
868   assert (ent);
869   return ent->visit;
870 }
871 void        set_entity_visited(entity *ent, unsigned long num) {
872   assert (ent);
873   ent->visit = num;
874 }
875 /* Sets visited field in entity to entity_visited. */
876 void        mark_entity_visited(entity *ent) {
877   assert (ent);
878   ent->visit = type_visited;
879 }
880
881
882 bool entity_visited(entity *ent) {
883   return get_entity_visited(ent) >= type_visited;
884 }
885
886 bool entity_not_visited(entity *ent) {
887   return get_entity_visited(ent) < type_visited;
888 }
889
890 /* Need two routines because I want to assert the result. */
891 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
892   int i, n_overwrittenby;
893   entity *res = NULL;
894
895   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
896
897   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
898   for (i = 0; i < n_overwrittenby; ++i) {
899     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
900     if (res) break;
901   }
902
903   return res;
904 }
905
906 /* Returns the dynamically referenced entity if the static entity and the
907  *  dynamic type are given.
908  *  Search downwards in overwritten tree. */
909 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
910   entity *res = resolve_ent_polymorphy2(dynamic_class, static_ent);
911   if (!res) {
912     printf(" Could not find entity "); DDME(static_ent);
913     printf("  in "); DDMT(dynamic_class);
914     printf("\n");
915     dump_entity(static_ent);
916     dump_type(get_entity_owner(static_ent));
917     dump_type(dynamic_class);
918
919   }
920   assert(res);
921   return res;
922 }
923
924
925
926 /*******************************************************************/
927 /** Debug aides                                                   **/
928 /*******************************************************************/
929
930
931 #if 1 || DEBUG_libfirm
932 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
933
934 #define X(a)    case a: printf(#a); break
935 void dump_entity (entity *ent) {
936   int i, j;
937   type *owner = get_entity_owner(ent);
938   type *type  = get_entity_type(ent);
939   printf("entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
940   printf("  type:  %s (%ld)\n", get_type_name(type),  get_type_nr(type));
941   printf("  owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
942
943   if (get_entity_n_overwrites(ent) > 0) {
944     printf ("  overwrites:\n");
945     for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
946       entity *ov = get_entity_overwrites(ent, i);
947       printf("    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
948     }
949   } else {
950     printf("  Does not overwrite other entities. \n");
951   }
952   if (get_entity_n_overwrittenby(ent) > 0) {
953     printf ("  overwritten by:\n");
954     for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
955       entity *ov = get_entity_overwrittenby(ent, i);
956       printf("    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
957     }
958   } else {
959     printf("  Is not overwriten by other entities. \n");
960   }
961
962   printf ("  allocation:  ");
963   switch (get_entity_allocation(ent)) {
964     X(allocation_dynamic);
965     X(allocation_automatic);
966     X(allocation_static);
967     X(allocation_parameter);
968   }
969
970   printf ("\n  visibility:  ");
971   switch (get_entity_visibility(ent)) {
972     X(visibility_local);
973     X(visibility_external_visible);
974     X(visibility_external_allocated);
975   }
976
977   printf ("\n  variability: ");
978   switch (get_entity_variability(ent)) {
979     X(variability_uninitialized);
980     X(variability_initialized);
981     X(variability_part_constant);
982     X(variability_constant);
983   }
984
985   if (get_entity_variability(ent) != variability_uninitialized) {
986     if (is_atomic_entity(ent)) {
987       printf("\n  atomic value: ");
988       dump_node_opcode(stdout, get_atomic_ent_value(ent));
989     } else {
990       printf("\n  compound values:");
991       for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
992     compound_graph_path *path = get_compound_ent_value_path(ent, i);
993     entity *ent0 = get_compound_graph_path_node(path, 0);
994     printf("\n    %2d %s", get_entity_offset(ent0), get_entity_name(ent0));
995     for (j = 1; j < get_compound_graph_path_length(path); ++j)
996       printf(".%s", get_entity_name(get_compound_graph_path_node(path, j)));
997     printf("\t = ");
998     dump_node_opcode(stdout, get_compound_ent_value(ent, i));
999       }
1000     }
1001   }
1002
1003   printf ("\n  volatility:  ");
1004   switch (get_entity_volatility(ent)) {
1005     X(volatility_non_volatile);
1006     X(volatility_is_volatile);
1007   }
1008
1009   printf("\n  peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1010   printf("\n  ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1011   printf("\n  offset:  %d", get_entity_offset(ent));
1012   if (is_method_type(get_entity_type(ent))) {
1013     if (get_entity_irg(ent))   /* can be null */
1014       { printf ("\n  irg = %ld", get_irg_graph_nr(get_entity_irg(ent))); }
1015     else
1016       { printf ("\n  irg = NULL"); }
1017   }
1018   printf("\n\n");
1019 }
1020 #undef X
1021 #else  /* DEBUG_libfirm */
1022 void dump_entity (entity *ent) {}
1023 #endif /* DEBUG_libfirm */