1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Martin Trapp, Christian Schaefer
18 # include "entity_t.h"
20 # include "typegmod_t.h"
22 /* All this is needed to build the constant node for methods: */
26 /*******************************************************************/
28 /*******************************************************************/
35 /*******************************************************************/
37 /*******************************************************************/
39 INLINE type *get_entity_owner (entity *ent);
41 INLINE void insert_entity_in_owner (entity *ent) {
42 type *owner = ent->owner;
43 switch (get_type_tpop_code(owner)) {
45 add_class_member (owner, ent);
48 add_struct_member (owner, ent);
51 add_union_member (owner, ent);
54 set_array_element_entity(owner, ent);
61 new_entity (type *owner, ident *name, type *type)
66 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
68 res = (entity *) malloc (sizeof (entity));
70 assert_legal_owner_of_ent(owner);
74 if (get_type_tpop(type) == type_method)
75 res->allocation = static_allocated;
77 res->allocation = automatic_allocated;
78 res->visibility = local;
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;
87 res->variability = uninitialized;
89 res->peculiarity = existent;
90 res->volatility = non_volatile;
92 res->overwrites = NEW_ARR_F(entity *, 1);
93 res->overwrittenby = NEW_ARR_F(entity *, 1);
99 /* Remember entity in it's owner. */
100 insert_entity_in_owner (res);
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);
109 INLINE void free_entity_attrs(entity *ent) {
111 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
112 DEL_ARR_F(ent->overwrites);
113 DEL_ARR_F(ent->overwrittenby);
118 copy_entity_own (entity *old, type *new_owner) {
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;
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);
137 if (is_class_type(new_owner)) {
138 new->overwrites = NEW_ARR_F(entity *, 1);
139 new->overwrittenby = NEW_ARR_F(entity *, 1);
142 insert_entity_in_owner (new);
148 copy_entity_name (entity *old, ident *new_name) {
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;
156 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
157 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
159 insert_entity_in_owner (new);
165 free_entity (entity *ent) {
166 free_entity_attrs(ent);
171 get_entity_name (entity *ent) {
173 return id_to_str(get_entity_ident(ent));
177 get_entity_ident (entity *ent) {
183 void set_entitye_ld_name (entity *, char *ld_name);
184 void set_entity_ld_ident (entity *, ident *ld_ident);
188 get_entity_owner (entity *ent) {
189 return ent->owner = skip_tid(ent->owner);
193 set_entity_owner (entity *ent, type *owner) {
194 assert_legal_owner_of_ent(owner);
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! */
208 get_entity_ld_ident (entity *ent)
210 if (ent->ld_name == NULL)
211 ent->ld_name = mangle_entity (ent);
216 set_entity_ld_ident (entity *ent, ident *ld_ident) {
217 ent->ld_name = ld_ident;
221 get_entity_ld_name (entity *ent) {
222 return id_to_str(get_entity_ld_ident(ent));
226 char *get_entity_ld_name (entity *);
227 void set_entity_ld_name (entity *, char *ld_name);
231 get_entity_type (entity *ent) {
232 return ent->type = skip_tid(ent->type);
236 set_entity_type (entity *ent, type *type) {
241 INLINE ent_allocation
242 get_entity_allocation (entity *ent) {
243 return ent->allocation;
247 set_entity_allocation (entity *ent, ent_allocation al) {
248 ent->allocation = al;
252 INLINE ent_visibility
253 get_entity_visibility (entity *ent) {
254 return ent->visibility;
258 set_entity_visibility (entity *ent, ent_visibility vis) {
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;
267 INLINE ent_variability
268 get_entity_variability (entity *ent) {
269 return ent->variability;
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);
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);
288 ent->variability = var;
292 INLINE ent_volatility
293 get_entity_volatility (entity *ent) {
295 return ent->volatility;
299 set_entity_volatility (entity *ent, ent_volatility vol) {
301 ent->volatility = vol;
305 get_entity_peculiarity (entity *ent) {
307 return ent->peculiarity;
311 set_entity_peculiarity (entity *ent, peculiarity pec) {
313 /* @@@ why peculiarity only for methods? */
314 assert (is_method_type(ent->type));
315 ent->peculiarity = pec;
318 /* Set has no effect for entities of type method. */
320 get_atomic_ent_value(entity *ent) {
321 assert(ent); assert(is_atomic_entity(ent));
322 assert((ent->variability != uninitialized));
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;
334 ir_node *copy_const_value(ir_node *n) {
339 switch(get_irn_opcode(n)) {
341 nn = new_Const(m, get_Const_tarval(n)); break;
343 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
345 nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
347 assert(0 && "opdope invalid or not implemented"); break;
352 /* A value of a compound entity is a pair of value and the corresponding member of
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);
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.
365 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
366 ir_graph *rem = current_ir_graph;
368 assert(get_entity_variability(ent) != uninitialized);
369 current_ir_graph = get_const_code_irg();
371 val = copy_const_value(val);
372 add_compound_ent_value(ent, val, member);
373 current_ir_graph = rem;
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;
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];
388 /* Copies the value i of the entity to current_block in current_ir_graph.
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]);
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];
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;
409 remove_compound_ent_value(entity *ent, entity *value_ent) {
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];
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);
426 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
428 ir_graph *rem = current_ir_graph;
429 type *arrtp = get_entity_type(ent);
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
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();
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));
444 current_ir_graph = rem;
448 get_entity_offset (entity *ent) {
453 set_entity_offset (entity *ent, int offset) {
454 ent->offset = offset;
458 add_entity_overwrites (entity *ent, entity *overwritten) {
460 assert(is_class_type(get_entity_owner(ent)));
461 ARR_APP1 (entity *, ent->overwrites, overwritten);
462 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
466 get_entity_n_overwrites (entity *ent) {
468 assert(is_class_type(get_entity_owner(ent)));
469 return (ARR_LEN (ent->overwrites))-1;
473 get_entity_overwrites_index(entity *ent, entity *overwritten) {
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)
483 get_entity_overwrites (entity *ent, int pos) {
485 assert(is_class_type(get_entity_owner(ent)));
486 assert(pos < get_entity_n_overwrites(ent));
487 return ent->overwrites[pos+1];
491 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
493 assert(is_class_type(get_entity_owner(ent)));
494 assert(pos < get_entity_n_overwrites(ent));
495 ent->overwrites[pos+1] = overwritten;
499 remove_entity_overwrites(entity *ent, entity *overwritten) {
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);
512 add_entity_overwrittenby (entity *ent, entity *overwrites) {
514 assert(is_class_type(get_entity_owner(ent)));
515 add_entity_overwrites(overwrites, ent);
519 get_entity_n_overwrittenby (entity *ent) {
521 assert(is_class_type(get_entity_owner(ent)));
522 return (ARR_LEN (ent->overwrittenby))-1;
526 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
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)
536 get_entity_overwrittenby (entity *ent, int pos) {
538 assert(is_class_type(get_entity_owner(ent)));
539 assert(pos < get_entity_n_overwrittenby(ent));
540 return ent->overwrittenby[pos+1];
544 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
546 assert(is_class_type(get_entity_owner(ent)));
547 assert(pos < get_entity_n_overwrittenby(ent));
548 ent->overwrittenby[pos+1] = overwrites;
551 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
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);
563 /* A link to store intermediate information */
565 get_entity_link(entity *ent) {
571 set_entity_link(entity *ent, void *l) {
577 get_entity_irg(entity *ent) {
579 assert (is_method_type(ent->type));
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. */
590 assert (is_method_type(ent->type));
591 assert (ent->peculiarity == existent);
595 int is_entity (void *thing) {
597 if (get_kind(thing) == k_entity)
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));
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));
615 /* @@@ not implemnted!!! */
616 bool equal_entity(entity *ent1, entity *ent2) {
617 printf(" calling unimplemented equal entity!!! \n");
622 unsigned long get_entity_visited(entity *ent) {
626 void set_entity_visited(entity *ent, unsigned long num) {
630 /* Sets visited field in entity to entity_visited. */
631 void mark_entity_visited(entity *ent) {
633 ent->visit = type_visited;
637 INLINE bool entity_visited(entity *ent) {
638 return get_entity_visited(ent) >= type_visited;
640 INLINE bool entity_not_visited(entity *ent) {
641 return get_entity_visited(ent) < type_visited;