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