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);
89 /* Remember entity in it's owner. */
90 insert_entity_in_owner (res);
93 inline void free_entity_attrs(entity *ent) {
95 DEL_ARR_F(ent->overwrites);
99 copy_entity_own (entity *old, type *new_owner) {
102 assert_legal_owner_of_ent(new_owner);
103 if (old->owner == new_owner) return old;
104 new = (entity *) malloc (sizeof (entity));
105 memcpy (new, old, sizeof (entity));
106 new->owner = new_owner;
107 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
109 insert_entity_in_owner (new);
115 copy_entity_name (entity *old, ident *new_name) {
118 if (old->name == new_name) return old;
119 new = (entity *) malloc (sizeof (entity));
120 memcpy (new, old, sizeof (entity));
121 new->name = new_name;
123 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
125 insert_entity_in_owner (new);
131 get_entity_name (entity *ent) {
133 return id_to_str(get_entity_ident(ent));
137 get_entity_ident (entity *ent) {
143 void set_entity_ld_name (entity *, char *ld_name);
144 void set_entity_ld_ident (entity *, ident *ld_ident);
148 get_entity_owner (entity *ent) {
149 return ent->owner = skip_tid(ent->owner);
153 set_entity_owner (entity *ent, type *owner) {
154 assert_legal_owner_of_ent(owner);
158 inline void /* should this go into type.c? */
159 assert_legal_owner_of_ent(type *owner) {
160 assert (get_type_tpop_code(owner) == tpo_class ||
161 get_type_tpop_code(owner) == tpo_union ||
162 get_type_tpop_code(owner) == tpo_struct ||
163 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
164 -- to select fields! */
168 get_entity_ld_ident (entity *ent)
170 if (ent->ld_name == NULL)
171 ent->ld_name = mangle_entity (ent);
176 set_entity_ld_ident (entity *ent, ident *ld_ident) {
177 ent->ld_name = ld_ident;
181 get_entity_ld_name (entity *ent) {
182 return id_to_str(get_entity_ld_ident(ent));
186 char *get_entity_ld_name (entity *);
187 void set_entity_ld_name (entity *, char *ld_name);
191 get_entity_type (entity *ent) {
192 return ent->type = skip_tid(ent->type);
196 set_entity_type (entity *ent, type *type) {
201 inline ent_allocation
202 get_entity_allocation (entity *ent) {
203 return ent->allocation;
207 set_entity_allocation (entity *ent, ent_allocation al) {
208 ent->allocation = al;
212 inline ent_visibility
213 get_entity_visibility (entity *ent) {
214 return ent->visibility;
218 set_entity_visibility (entity *ent, ent_visibility vis) {
219 if (vis != local) assert(ent->allocation == static_allocated);
220 ent->visibility = vis;
223 inline ent_variability
224 get_entity_variability (entity *ent) {
225 return ent->variability;
229 set_entity_variability (entity *ent, ent_variability var){
230 if (var == part_constant)
231 assert(is_class_type(ent->type) || is_struct_type(ent->type));
232 if ((is_compound_type(ent->type)) &&
233 (ent->variability == uninitialized) && (var != uninitialized)) {
234 /* Allocate datastructures for constant values */
235 ent->values = NEW_ARR_F(ir_node *, 1);
236 ent->val_ents = NEW_ARR_F(entity *, 1);
238 if ((is_compound_type(ent->type)) &&
239 (var == uninitialized) && (ent->variability != uninitialized)) {
240 /* Free datastructures for constant values */
241 DEL_ARR_F(ent->values);
242 DEL_ARR_F(ent->val_ents);
244 ent->variability = var;
248 inline ent_volatility
249 get_entity_volatility (entity *ent) {
250 return ent->volatility;
254 set_entity_volatility (entity *ent, ent_volatility vol) {
255 ent->volatility = vol;
258 /* Set has no effect for entities of type method. */
260 get_atomic_ent_value(entity *ent) {
261 assert(ent); assert(is_atomic_entity(ent)); assert((ent->variability != uninitialized));
266 set_atomic_ent_value(entity *ent, ir_node *val) {
267 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
268 if (is_method_type(ent->type)) return;
272 ir_node *copy_value(ir_node *n) {
277 switch(get_irn_opcode(n)) {
279 nn = new_Const(m, get_Const_tarval(n)); break;
281 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
283 nn = new_Add(copy_value(get_Add_left(n)), copy_value(get_Add_right(n)), m); break;
285 assert(0 && "opdope invalid or not implemented"); break;
290 /* Copies the value represented by the entity to current_block
291 in current_ir_graph. */
292 ir_node *copy_atomic_ent_value(entity *ent) {
293 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
294 return copy_value(ent->value);
297 /* A value of a compound entity is a pair of value and the corresponding member of
300 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
301 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
302 ARR_APP1 (ir_node *, ent->values, val);
303 ARR_APP1 (entity *, ent->val_ents, member);
307 get_compound_ent_n_values(entity *ent) {
308 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
309 return (ARR_LEN (ent->values))-1;
313 get_compound_ent_value(entity *ent, int pos) {
314 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
315 return ent->values[pos+1];
318 /* Copies the value i of the entity to current_block in current_ir_graph. */
319 ir_node *copy_compound_ent_value(entity *ent, int pos) {
320 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
321 return copy_value(ent->values[pos+1]);
325 get_compound_ent_value_member(entity *ent, int pos) {
326 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
327 return ent->val_ents[pos+1];
331 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
332 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
333 ent->values[pos+1] = val;
334 ent->val_ents[pos+1] = member;
338 get_entity_offset (entity *ent) {
343 set_entity_offset (entity *ent, int offset) {
344 ent->offset = offset;
348 add_entity_overwrites (entity *ent, entity *overwritten) {
350 ARR_APP1 (entity *, ent->overwrites, overwritten);
354 get_entity_n_overwrites (entity *ent){
356 return (ARR_LEN (ent->overwrites))-1;
360 get_entity_overwrites (entity *ent, int pos){
362 return ent->overwrites[pos+1];
366 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
368 ent->overwrites[pos+1] = overwritten;
371 /* A link to store intermediate information */
373 get_entity_link(entity *ent) {
379 set_entity_link(entity *ent, void *l) {
385 get_entity_irg(entity *ent) {
387 assert (is_method_type(ent->type));
392 set_entity_irg(entity *ent, ir_graph *irg) {
393 assert (ent && ent->type);
395 assert (is_method_type(ent->type));
399 int is_atomic_entity(entity *ent) {
400 type* t = get_entity_type(ent);
401 return (is_primitive_type(t) || is_pointer_type(t) ||
402 is_enumeration_type(t) || is_method_type(t));
405 int is_compound_entity(entity *ent) {
406 type* t = get_entity_type(ent);
407 return (is_class_type(t) || is_struct_type(t) ||
408 is_array_type(t) || is_union_type(t));