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