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