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