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