1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
16 # include "entity_t.h"
18 # include "typegmod_t.h"
20 /* All this is needed to build the constant node for methods: */
24 /*******************************************************************/
26 /*******************************************************************/
33 /*******************************************************************/
35 /*******************************************************************/
37 inline void insert_entity_in_owner (entity *ent) {
38 type *owner = ent->owner;
39 switch (get_type_tpop_code(owner)) {
41 add_class_member (owner, ent);
44 add_struct_member (owner, ent);
47 add_union_member (owner, ent);
50 set_array_element_entity(owner, ent);
57 new_entity (type *owner, ident *name, type *type)
62 res = (entity *) malloc (sizeof (entity));
64 assert_legal_owner_of_ent(owner);
68 if (get_type_tpop(type) == type_method)
69 res->allocation = static_allocated;
71 res->allocation = automatic_allocated;
72 res->visibility = local;
74 if (is_method_type(type)) {
75 res->variability = constant;
76 rem = current_ir_graph;
77 current_ir_graph = get_const_code_irg();
78 res->value = new_Const(mode_p, tarval_p_from_entity(res));
79 current_ir_graph = rem;
81 res->variability = uninitialized;
83 res->volatility = non_volatile;
85 res->overwrites = NEW_ARR_F(entity *, 1);
91 /* Remember entity in it's owner. */
92 insert_entity_in_owner (res);
95 inline void free_entity_attrs(entity *ent) {
97 DEL_ARR_F(ent->overwrites);
101 copy_entity_own (entity *old, type *new_owner) {
104 assert_legal_owner_of_ent(new_owner);
105 if (old->owner == new_owner) return old;
106 new = (entity *) malloc (sizeof (entity));
107 memcpy (new, old, sizeof (entity));
108 new->owner = new_owner;
109 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
111 insert_entity_in_owner (new);
117 copy_entity_name (entity *old, ident *new_name) {
120 if (old->name == new_name) return old;
121 new = (entity *) malloc (sizeof (entity));
122 memcpy (new, old, sizeof (entity));
123 new->name = new_name;
125 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
127 insert_entity_in_owner (new);
133 get_entity_name (entity *ent) {
135 return id_to_str(get_entity_ident(ent));
139 get_entity_ident (entity *ent) {
145 void set_entity_ld_name (entity *, char *ld_name);
146 void set_entity_ld_ident (entity *, ident *ld_ident);
150 get_entity_owner (entity *ent) {
151 return ent->owner = skip_tid(ent->owner);
155 set_entity_owner (entity *ent, type *owner) {
156 assert_legal_owner_of_ent(owner);
160 inline void /* should this go into type.c? */
161 assert_legal_owner_of_ent(type *owner) {
162 assert (get_type_tpop_code(owner) == tpo_class ||
163 get_type_tpop_code(owner) == tpo_union ||
164 get_type_tpop_code(owner) == tpo_struct ||
165 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
166 -- to select fields! */
170 get_entity_ld_ident (entity *ent)
172 if (ent->ld_name == NULL)
173 ent->ld_name = mangle_entity (ent);
178 set_entity_ld_ident (entity *ent, ident *ld_ident) {
179 ent->ld_name = ld_ident;
183 get_entity_ld_name (entity *ent) {
184 return id_to_str(get_entity_ld_ident(ent));
188 char *get_entity_ld_name (entity *);
189 void set_entity_ld_name (entity *, char *ld_name);
193 get_entity_type (entity *ent) {
194 return ent->type = skip_tid(ent->type);
198 set_entity_type (entity *ent, type *type) {
203 inline ent_allocation
204 get_entity_allocation (entity *ent) {
205 return ent->allocation;
209 set_entity_allocation (entity *ent, ent_allocation al) {
210 ent->allocation = al;
214 inline ent_visibility
215 get_entity_visibility (entity *ent) {
216 return ent->visibility;
220 set_entity_visibility (entity *ent, ent_visibility vis) {
221 if (vis != local) assert(ent->allocation == static_allocated);
222 ent->visibility = vis;
225 inline ent_variability
226 get_entity_variability (entity *ent) {
227 return ent->variability;
231 set_entity_variability (entity *ent, ent_variability var){
232 if (var == part_constant)
233 assert(is_class_type(ent->type) || is_struct_type(ent->type));
234 if ((is_compound_type(ent->type)) &&
235 (ent->variability == uninitialized) && (var != uninitialized)) {
236 /* Allocate datastructures for constant values */
237 ent->values = NEW_ARR_F(ir_node *, 1);
238 ent->val_ents = NEW_ARR_F(entity *, 1);
240 if ((is_compound_type(ent->type)) &&
241 (var == uninitialized) && (ent->variability != uninitialized)) {
242 /* Free datastructures for constant values */
243 DEL_ARR_F(ent->values);
244 DEL_ARR_F(ent->val_ents);
246 ent->variability = var;
250 inline ent_volatility
251 get_entity_volatility (entity *ent) {
252 return ent->volatility;
256 set_entity_volatility (entity *ent, ent_volatility vol) {
257 ent->volatility = vol;
260 /* Set has no effect for entities of type method. */
262 get_atomic_ent_value(entity *ent) {
263 assert(ent); assert(is_atomic_entity(ent)); assert((ent->variability != uninitialized));
268 set_atomic_ent_value(entity *ent, ir_node *val) {
269 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
270 if (is_method_type(ent->type)) return;
274 ir_node *copy_value(ir_node *n) {
279 switch(get_irn_opcode(n)) {
281 nn = new_Const(m, get_Const_tarval(n)); break;
283 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
285 nn = new_Add(copy_value(get_Add_left(n)), copy_value(get_Add_right(n)), m); break;
287 assert(0 && "opdope invalid or not implemented"); break;
292 /* Copies the value represented by the entity to current_block
293 in current_ir_graph. */
294 ir_node *copy_atomic_ent_value(entity *ent) {
295 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
296 return copy_value(ent->value);
299 /* A value of a compound entity is a pair of value and the corresponding member of
302 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
303 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
304 ARR_APP1 (ir_node *, ent->values, val);
305 ARR_APP1 (entity *, ent->val_ents, member);
309 get_compound_ent_n_values(entity *ent) {
310 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
311 return (ARR_LEN (ent->values))-1;
315 get_compound_ent_value(entity *ent, int pos) {
316 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
317 return ent->values[pos+1];
320 /* Copies the value i of the entity to current_block in current_ir_graph. */
321 ir_node *copy_compound_ent_value(entity *ent, int pos) {
322 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
323 return copy_value(ent->values[pos+1]);
327 get_compound_ent_value_member(entity *ent, int pos) {
328 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
329 return ent->val_ents[pos+1];
333 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
334 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
335 ent->values[pos+1] = val;
336 ent->val_ents[pos+1] = member;
340 get_entity_offset (entity *ent) {
345 set_entity_offset (entity *ent, int offset) {
346 ent->offset = offset;
350 add_entity_overwrites (entity *ent, entity *overwritten) {
352 ARR_APP1 (entity *, ent->overwrites, overwritten);
356 get_entity_n_overwrites (entity *ent){
358 return (ARR_LEN (ent->overwrites))-1;
362 get_entity_overwrites (entity *ent, int pos){
364 return ent->overwrites[pos+1];
368 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
370 ent->overwrites[pos+1] = overwritten;
373 /* A link to store intermediate information */
375 get_entity_link(entity *ent) {
381 set_entity_link(entity *ent, void *l) {
387 get_entity_irg(entity *ent) {
389 assert (is_method_type(ent->type));
394 set_entity_irg(entity *ent, ir_graph *irg) {
395 assert (ent && ent->type);
397 assert (is_method_type(ent->type));
401 int is_atomic_entity(entity *ent) {
402 type* t = get_entity_type(ent);
403 return (is_primitive_type(t) || is_pointer_type(t) ||
404 is_enumeration_type(t) || is_method_type(t));
407 int is_compound_entity(entity *ent) {
408 type* t = get_entity_type(ent);
409 return (is_class_type(t) || is_struct_type(t) ||
410 is_array_type(t) || is_union_type(t));