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