Checks wheater names contains spaces (not allowed in general)
[libfirm] / ir / tr / entity.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Martin Trapp, Christian Schaefer
5 *
6 */
7
8 /* $Id$ */
9
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13
14 # include <stdlib.h>
15 # include <stddef.h>
16 # include <string.h>
17
18 # include "entity_t.h"
19 # include "mangle.h"
20 # include "typegmod_t.h"
21 # include "array.h"
22 /* All this is needed to build the constant node for methods: */
23 # include "irprog.h"
24 # include "ircons.h"
25
26 /*******************************************************************/
27 /** general                                                       **/
28 /*******************************************************************/
29
30 void
31 init_entity (void)
32 {
33 }
34
35 /*******************************************************************/
36 /** ENTITY                                                        **/
37 /*******************************************************************/
38
39 INLINE type *get_entity_owner (entity *ent);
40
41 INLINE void insert_entity_in_owner (entity *ent) {
42   type *owner = ent->owner;
43   switch (get_type_tpop_code(owner)) {
44   case tpo_class: {
45     add_class_member (owner, ent);
46   } break;
47   case tpo_struct: {
48     add_struct_member (owner, ent);
49   } break;
50   case tpo_union: {
51     add_union_member (owner, ent);
52   } break;
53   case tpo_array: {
54     set_array_element_entity(owner, ent);
55   } break;
56   default: assert(0);
57   }
58 }
59
60 entity *
61 new_entity (type *owner, ident *name, type *type)
62 {
63   entity *res;
64   ir_graph *rem;
65
66   assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
67
68   res = (entity *) malloc (sizeof (entity));
69   res->kind = k_entity;
70   assert_legal_owner_of_ent(owner);
71   res->owner = owner;
72   res->name = name;
73   res->type = type;
74   if (get_type_tpop(type) == type_method)
75     res->allocation = static_allocated;
76   else
77     res->allocation = automatic_allocated;
78   res->visibility = local;
79   res->offset = -1;
80   if (is_method_type(type)) {
81     res->variability = constant;
82     rem = current_ir_graph;
83     current_ir_graph = get_const_code_irg();
84     res->value = new_Const(mode_P, tarval_P_from_entity(res));
85     current_ir_graph = rem;
86   } else {
87     res->variability = uninitialized;
88   }
89   res->peculiarity = existent;
90   res->volatility = non_volatile;
91   res->ld_name = NULL;
92   res->overwrites = NEW_ARR_F(entity *, 1);
93   res->overwrittenby = NEW_ARR_F(entity *, 1);
94
95   res->irg = NULL;
96
97   res->visit = 0;
98
99   /* Remember entity in it's owner. */
100   insert_entity_in_owner (res);
101   return res;
102 }
103 entity *
104 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
105   entity *res = new_entity(owner, name, type);
106   set_entity_dbg_info(res, db);
107   return res;
108 }
109 INLINE void free_entity_attrs(entity *ent) {
110   assert(ent);
111   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
112     DEL_ARR_F(ent->overwrites);
113     DEL_ARR_F(ent->overwrittenby);
114   }
115 }
116
117 entity *
118 copy_entity_own (entity *old, type *new_owner) {
119   entity *new;
120
121   assert_legal_owner_of_ent(new_owner);
122   if (old->owner == new_owner) return old;
123   new = (entity *) malloc (sizeof (entity));
124   memcpy (new, old, sizeof (entity));
125   new->owner = new_owner;
126   /*
127   if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
128       (get_type_tpop(new_owner) == type_class)) {
129     new->overwrites = DUP_ARR_F(entity *, old->overwrites);
130     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
131   } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
132              (get_type_tpop(new_owner) == type_class)) {
133     new->overwrites = NEW_ARR_F(entity *, 1);
134     new->overwrittenby = NEW_ARR_F(entity *, 1);
135   }
136   */
137   if (is_class_type(new_owner)) {
138     new->overwrites = NEW_ARR_F(entity *, 1);
139     new->overwrittenby = NEW_ARR_F(entity *, 1);
140   }
141
142   insert_entity_in_owner (new);
143
144   return new;
145 }
146
147 entity *
148 copy_entity_name (entity *old, ident *new_name) {
149   entity *new;
150
151   if (old->name == new_name) return old;
152   new = (entity *) malloc (sizeof (entity));
153   memcpy (new, old, sizeof (entity));
154   new->name = new_name;
155   new->ld_name = NULL;
156   new->overwrites = DUP_ARR_F(entity *, old->overwrites);
157   new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
158
159   insert_entity_in_owner (new);
160
161   return new;
162 }
163
164 void
165 free_entity (entity *ent) {
166   free_entity_attrs(ent);
167   free(ent);
168 }
169
170 INLINE const char *
171 get_entity_name (entity *ent) {
172   assert (ent);
173   return id_to_str(get_entity_ident(ent));
174 }
175
176 ident *
177 get_entity_ident    (entity *ent) {
178   assert(ent);
179   return ent->name;
180 }
181
182 /*
183 void   set_entitye_ld_name  (entity *, char *ld_name);
184 void   set_entity_ld_ident (entity *, ident *ld_ident);
185 */
186
187 INLINE type *
188 get_entity_owner (entity *ent) {
189   return ent->owner = skip_tid(ent->owner);
190 }
191
192 INLINE void
193 set_entity_owner (entity *ent, type *owner) {
194   assert_legal_owner_of_ent(owner);
195   ent->owner = owner;
196 }
197
198 INLINE void   /* should this go into type.c? */
199 assert_legal_owner_of_ent(type *owner) {
200   assert (get_type_tpop_code(owner) == tpo_class ||
201           get_type_tpop_code(owner) == tpo_union ||
202           get_type_tpop_code(owner) == tpo_struct ||
203           get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
204                                                         -- to select fields! */
205 }
206
207 INLINE ident *
208 get_entity_ld_ident (entity *ent)
209 {
210   if (ent->ld_name == NULL)
211     ent->ld_name = mangle_entity (ent);
212   return ent->ld_name;
213 }
214
215 INLINE void
216 set_entity_ld_ident (entity *ent, ident *ld_ident) {
217   ent->ld_name = ld_ident;
218 }
219
220 INLINE const char *
221 get_entity_ld_name (entity *ent) {
222   return id_to_str(get_entity_ld_ident(ent));
223 }
224
225 /*
226 char  *get_entity_ld_name  (entity *);
227 void   set_entity_ld_name  (entity *, char *ld_name);
228 */
229
230 INLINE type *
231 get_entity_type (entity *ent) {
232   return ent->type = skip_tid(ent->type);
233 }
234
235 INLINE void
236 set_entity_type (entity *ent, type *type) {
237   ent->type = type;
238 }
239
240
241 INLINE ent_allocation
242 get_entity_allocation (entity *ent) {
243   return ent->allocation;
244 }
245
246 INLINE void
247 set_entity_allocation (entity *ent, ent_allocation al) {
248   ent->allocation = al;
249 }
250
251
252 INLINE ent_visibility
253 get_entity_visibility (entity *ent) {
254   return ent->visibility;
255 }
256
257 INLINE void
258 set_entity_visibility (entity *ent, ent_visibility vis) {
259   if (vis != local)
260     assert((ent->allocation == static_allocated) ||
261            (ent->allocation == automatic_allocated));
262   // @@@ Test that the owner type is not local, but how??
263   //       && get_class_visibility(get_entity_owner(ent)) != local));
264   ent->visibility = vis;
265 }
266
267 INLINE ent_variability
268 get_entity_variability (entity *ent) {
269   return ent->variability;
270 }
271
272 INLINE void
273 set_entity_variability (entity *ent, ent_variability var){
274   if (var == part_constant)
275     assert(is_class_type(ent->type) || is_struct_type(ent->type));
276   if ((is_compound_type(ent->type)) &&
277       (ent->variability == uninitialized) && (var != uninitialized)) {
278     /* Allocate datastructures for constant values */
279     ent->values = NEW_ARR_F(ir_node *, 1);
280     ent->val_ents = NEW_ARR_F(entity *, 1);
281   }
282   if ((is_compound_type(ent->type)) &&
283       (var == uninitialized) && (ent->variability != uninitialized)) {
284     /* Free datastructures for constant values */
285     DEL_ARR_F(ent->values);
286     DEL_ARR_F(ent->val_ents);
287   }
288   ent->variability = var;
289 }
290
291
292 INLINE ent_volatility
293 get_entity_volatility (entity *ent) {
294   assert (ent);
295   return ent->volatility;
296 }
297
298 INLINE void
299 set_entity_volatility (entity *ent, ent_volatility vol) {
300   assert (ent);
301   ent->volatility = vol;
302 }
303
304 INLINE peculiarity
305 get_entity_peculiarity (entity *ent) {
306   assert (ent);
307   return ent->peculiarity;
308 }
309
310 INLINE void
311 set_entity_peculiarity (entity *ent, peculiarity pec) {
312   assert (ent);
313   /* @@@ why peculiarity only for methods? */
314   assert (is_method_type(ent->type));
315   ent->peculiarity = pec;
316 }
317
318 /* Set has no effect for entities of type method. */
319 INLINE ir_node *
320 get_atomic_ent_value(entity *ent) {
321   assert(ent); assert(is_atomic_entity(ent));
322   assert((ent->variability != uninitialized));
323   return ent->value;
324 }
325
326 INLINE void
327 set_atomic_ent_value(entity *ent, ir_node *val) {
328   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
329   if (is_method_type(ent->type)) return;
330   ent->value = val;
331 }
332
333
334 ir_node *copy_const_value(ir_node *n) {
335   ir_node *nn;
336   ir_mode *m;
337
338   m = get_irn_mode(n);
339   switch(get_irn_opcode(n)) {
340   case iro_Const:
341     nn = new_Const(m, get_Const_tarval(n)); break;
342   case iro_SymConst:
343     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
344   case iro_Add:
345     nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
346   default:
347     assert(0 && "opdope invalid or not implemented"); break;
348   }
349   return nn;
350 }
351
352 /* A value of a compound entity is a pair of value and the corresponding member of
353    the compound. */
354 INLINE void
355 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
356   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
357   ARR_APP1 (ir_node *, ent->values, val);
358   ARR_APP1 (entity *, ent->val_ents, member);
359 }
360
361 /* Copies the firm subgraph referenced by val to const_code_irg and adds
362    the node as constant initialization to ent.
363    The subgraph may not contain control flow operations.
364 INLINE void
365 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
366   ir_graph *rem = current_ir_graph;
367
368   assert(get_entity_variability(ent) != uninitialized);
369   current_ir_graph = get_const_code_irg();
370
371   val = copy_const_value(val);
372   add_compound_ent_value(ent, val, member);
373   current_ir_graph = rem;
374   }*/
375
376 INLINE int
377 get_compound_ent_n_values(entity *ent) {
378   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
379   return (ARR_LEN (ent->values))-1;
380 }
381
382 INLINE ir_node  *
383 get_compound_ent_value(entity *ent, int pos) {
384   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
385   return ent->values[pos+1];
386 }
387
388 /* Copies the value i of the entity to current_block in current_ir_graph.
389 ir_node *
390 copy_compound_ent_value(entity *ent, int pos) {
391   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
392   return copy_const_value(ent->values[pos+1]);
393   }*/
394
395 INLINE entity   *
396 get_compound_ent_value_member(entity *ent, int pos) {
397   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
398   return ent->val_ents[pos+1];
399 }
400
401 INLINE void
402 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
403   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
404   ent->values[pos+1] = val;
405   ent->val_ents[pos+1] = member;
406 }
407
408 void
409 remove_compound_ent_value(entity *ent, entity *value_ent) {
410   int i;
411   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
412   for (i = 1; i < (ARR_LEN (ent->val_ents)); i++) {
413     if (ent->val_ents[i] == value_ent) {
414       for(; i < (ARR_LEN (ent->val_ents))-1; i++) {
415         ent->val_ents[i] = ent->val_ents[i+1];
416         ent->values[i]   = ent->values[i+1];
417       }
418       ARR_SETLEN(entity*,  ent->val_ents, ARR_LEN(ent->val_ents) - 1);
419       ARR_SETLEN(ir_node*, ent->values,   ARR_LEN(ent->values) - 1);
420       break;
421     }
422   }
423 }
424
425 void
426 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
427   int i;
428   ir_graph *rem = current_ir_graph;
429   type *arrtp = get_entity_type(ent);
430   ir_node *val;
431
432   assert(is_array_type(arrtp));
433   assert(get_array_n_dimensions(arrtp) == 1);
434   /* One bound is sufficient, the nunmber of constant fields makes the
435      size. */
436   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
437   assert(get_entity_variability(ent) != uninitialized);
438   current_ir_graph = get_const_code_irg();
439
440   for (i = 0; i < num_vals; i++) {
441     val = new_Const(get_tv_mode (values[i]), values[i]);
442     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
443   }
444   current_ir_graph = rem;
445 }
446
447 INLINE int
448 get_entity_offset (entity *ent) {
449   return ent->offset;
450 }
451
452 INLINE void
453 set_entity_offset (entity *ent, int offset) {
454   ent->offset = offset;
455 }
456
457 INLINE void
458 add_entity_overwrites   (entity *ent, entity *overwritten) {
459   assert(ent);
460   assert(is_class_type(get_entity_owner(ent)));
461   ARR_APP1 (entity *, ent->overwrites, overwritten);
462   ARR_APP1 (entity *, overwritten->overwrittenby, ent);
463 }
464
465 INLINE int
466 get_entity_n_overwrites (entity *ent) {
467   assert(ent);
468   assert(is_class_type(get_entity_owner(ent)));
469   return (ARR_LEN (ent->overwrites))-1;
470 }
471
472 int
473 get_entity_overwrites_index(entity *ent, entity *overwritten) {
474   int i;
475   assert(ent && is_class_type(get_entity_owner(ent)));
476   for (i = 0; i < get_entity_n_overwrites(ent); i++)
477     if (get_entity_overwrites(ent, i) == overwritten)
478       return i;
479   return -1;
480 }
481
482 INLINE entity *
483 get_entity_overwrites   (entity *ent, int pos) {
484   assert(ent);
485   assert(is_class_type(get_entity_owner(ent)));
486   assert(pos < get_entity_n_overwrites(ent));
487   return ent->overwrites[pos+1];
488 }
489
490 INLINE void
491 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
492   assert(ent);
493   assert(is_class_type(get_entity_owner(ent)));
494   assert(pos < get_entity_n_overwrites(ent));
495   ent->overwrites[pos+1] = overwritten;
496 }
497
498 void
499 remove_entity_overwrites(entity *ent, entity *overwritten) {
500   int i;
501   assert(ent && is_class_type(get_entity_owner(ent)));
502   for (i = 1; i < (ARR_LEN (ent->overwrites)); i++)
503     if (ent->overwrites[i] == overwritten) {
504       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
505         ent->overwrites[i] = ent->overwrites[i+1];
506       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
507       break;
508     }
509 }
510
511 INLINE void
512 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
513   assert(ent);
514   assert(is_class_type(get_entity_owner(ent)));
515   add_entity_overwrites(overwrites, ent);
516 }
517
518 INLINE int
519 get_entity_n_overwrittenby (entity *ent) {
520   assert(ent);
521   assert(is_class_type(get_entity_owner(ent)));
522   return (ARR_LEN (ent->overwrittenby))-1;
523 }
524
525 int
526 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
527   int i;
528   assert(ent && is_class_type(get_entity_owner(ent)));
529   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
530     if (get_entity_overwrittenby(ent, i) == overwrites)
531       return i;
532   return -1;
533 }
534
535 INLINE entity *
536 get_entity_overwrittenby   (entity *ent, int pos) {
537   assert(ent);
538   assert(is_class_type(get_entity_owner(ent)));
539   assert(pos < get_entity_n_overwrittenby(ent));
540   return ent->overwrittenby[pos+1];
541 }
542
543 INLINE void
544 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
545   assert(ent);
546   assert(is_class_type(get_entity_owner(ent)));
547   assert(pos < get_entity_n_overwrittenby(ent));
548   ent->overwrittenby[pos+1] = overwrites;
549 }
550
551 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
552   int i;
553   assert(ent && is_class_type(get_entity_owner(ent)));
554   for (i = 1; i < (ARR_LEN (ent->overwrittenby)); i++)
555     if (ent->overwrittenby[i] == overwrites) {
556       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
557         ent->overwrittenby[i] = ent->overwrittenby[i+1];
558       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
559       break;
560     }
561 }
562
563 /* A link to store intermediate information */
564 void *
565 get_entity_link(entity *ent) {
566   assert(ent);
567   return ent->link;
568 }
569
570 void
571 set_entity_link(entity *ent, void *l) {
572   assert(ent);
573   ent->link = l;
574 }
575
576 INLINE ir_graph *
577 get_entity_irg(entity *ent) {
578   assert (ent);
579   assert (is_method_type(ent->type));
580   return ent->irg;
581 }
582
583 INLINE void
584 set_entity_irg(entity *ent, ir_graph *irg) {
585   assert (ent && ent->type);
586   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
587    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
588    * aber erhalten bleiben soll. */
589   /* assert (irg); */
590   assert (is_method_type(ent->type));
591   assert (ent->peculiarity == existent);
592   ent->irg = irg;
593 }
594
595 int is_entity (void *thing) {
596   assert(thing);
597   if (get_kind(thing) == k_entity)
598     return 1;
599   else
600     return 0;
601 }
602
603 int is_atomic_entity(entity *ent) {
604   type* t = get_entity_type(ent);
605   return (is_primitive_type(t) || is_pointer_type(t) ||
606           is_enumeration_type(t) || is_method_type(t));
607 }
608
609 int is_compound_entity(entity *ent) {
610   type* t = get_entity_type(ent);
611   return (is_class_type(t) || is_struct_type(t) ||
612           is_array_type(t) || is_union_type(t));
613 }
614
615 /* @@@ not implemnted!!! */
616 bool equal_entity(entity *ent1, entity *ent2) {
617   printf(" calling unimplemented equal entity!!! \n");
618   return true;
619 }
620
621
622 unsigned long get_entity_visited(entity *ent) {
623   assert (ent);
624   return ent->visit;
625 }
626 void        set_entity_visited(entity *ent, unsigned long num) {
627   assert (ent);
628   ent->visit = num;
629 }
630 /* Sets visited field in entity to entity_visited. */
631 void        mark_entity_visited(entity *ent) {
632   assert (ent);
633   ent->visit = type_visited;
634 }
635
636
637 INLINE bool entity_visited(entity *ent) {
638   return get_entity_visited(ent) >= type_visited;
639 }
640 INLINE bool entity_not_visited(entity *ent) {
641   return get_entity_visited(ent) < type_visited;
642 }