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