3 * file type.c - implementation of the datastructure to hold
5 * (C) 2001 by Universitaet Karlsruhe
6 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
8 * This module supplies a datastructure to represent all types
9 * known in the compiled program. This includes types specified
10 * in the program as well as types defined by the language. In the
11 * view of the intermediate representation there is no difference
12 * between these types.
14 * There exist several kinds of types, arranged by the structure of
15 * the type. A type is described by a set of attributes. Some of
16 * these attributes are common to all types, others depend on the
19 * Types are different from the modes defined in irmode: Types are
20 * on the level of the programming language, modes at the level of
21 * the target processor.
23 * @see type_t.h type tpop
33 # include "typegmod_t.h"
40 /*******************************************************************/
42 /*******************************************************************/
44 unsigned long type_visited;
45 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
46 INLINE unsigned long get_master_type_visited() { return type_visited; }
47 INLINE void inc_master_type_visited() { type_visited++; }
49 void free_type(type *tp) {
50 /* Remove from list of all types */
52 /* Free the attributes of the type. */
54 /* Free entities automatically allocated with the type */
55 if (is_array_type(tp))
56 free_entity(get_array_element_entity(tp));
57 /* And now the type itself... */
62 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
66 assert(type_op != type_id);
68 node_size = offsetof (type, attr) + type_op->attr_size;
69 res = (type *) xmalloc (node_size);
70 add_irp_type(res); /* Remember the new type global. */
73 res->type_op = type_op;
76 res->state = layout_undefined;
84 void free_type_attrs(type *tp) {
85 switch(get_type_tpop_code(tp)) {
86 case tpo_class: { free_class_attrs(tp); } break;
87 case tpo_struct: { free_struct_attrs(tp); } break;
88 case tpo_method: { free_method_attrs(tp); } break;
89 case tpo_union: { free_union_attrs(tp); } break;
90 case tpo_array: { free_array_attrs(tp); } break;
91 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
92 case tpo_pointer: { free_pointer_attrs(tp); } break;
93 case tpo_primitive: { free_primitive_attrs(tp); } break;
98 /* set/get the link field */
99 void *get_type_link(type *tp)
101 assert(tp && tp->kind == k_type);
105 void set_type_link(type *tp, void *l)
107 assert(tp && tp->kind == k_type);
111 tp_op* get_type_tpop(type *tp) {
112 assert(tp && tp->kind == k_type);
116 ident* get_type_tpop_nameid(type *tp) {
117 assert(tp && tp->kind == k_type);
118 return tp->type_op->name;
121 const char* get_type_tpop_name(type *tp) {
122 assert(tp && tp->kind == k_type);
123 return id_to_str(tp->type_op->name);
126 tp_opcode get_type_tpop_code(type *tp) {
127 assert(tp && tp->kind == k_type);
128 return tp->type_op->code;
131 ir_mode* get_type_mode(type *tp) {
132 assert(tp && tp->kind == k_type);
136 void set_type_mode(type *tp, ir_mode* m) {
137 assert(tp && tp->kind == k_type);
139 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
140 /* Modes of primitives must be data */
141 ((tp->type_op != type_enumeration) || mode_is_int(m)));
142 /* Modes of enumerations must be integers */
144 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
145 /* For pointer, primitive and enumeration size depends on the mode. */
146 tp->size = get_mode_size(m);
151 ident* get_type_ident(type *tp) {
152 assert(tp && tp->kind == k_type);
156 void set_type_ident(type *tp, ident* id) {
157 assert(tp && tp->kind == k_type);
161 const char* get_type_name(type *tp) {
162 assert(tp && tp->kind == k_type);
163 return (id_to_str(tp->name));
166 int get_type_size(type *tp) {
167 assert(tp && tp->kind == k_type);
172 set_type_size(type *tp, int size) {
173 assert(tp && tp->kind == k_type);
174 /* For pointer enumeration and primitive size depends on the mode.
175 Methods don't have a size. */
176 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
177 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
182 get_type_state(type *tp) {
183 assert(tp && tp->kind == k_type);
188 set_type_state(type *tp, type_state state) {
189 assert(tp && tp->kind == k_type);
191 if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
192 (tp->type_op == type_method))
195 /* Just a correctness check: */
196 if (state == layout_fixed) {
198 switch (get_type_tpop_code(tp)) {
201 assert(get_type_size(tp) > -1);
202 if (tp != get_glob_type())
203 for (i = 0; i < get_class_n_members(tp); i++) {
204 assert(get_entity_offset(get_class_member(tp, i)) > -1);
205 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
206 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
211 /* assert(get_type_size(tp) > -1); @@@ lowerfirm geht nicht durch */
212 for (i = 0; i < get_struct_n_members(tp); i++) {
213 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
214 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
223 Assure that only innermost dimension is dynamic? */
225 case tpo_enumeration:
227 assert(get_type_mode != NULL);
228 for (i = 0; i < get_enumeration_n_enums(tp); i++)
229 assert(get_enumeration_enum(tp, i) != NULL);
237 unsigned long get_type_visited(type *tp) {
238 assert(tp && tp->kind == k_type);
242 void set_type_visited(type *tp, unsigned long num) {
243 assert(tp && tp->kind == k_type);
246 /* Sets visited field in type to type_visited. */
247 void mark_type_visited(type *tp) {
248 assert(tp && tp->kind == k_type);
249 assert(tp->visit < type_visited);
250 tp->visit = type_visited;
252 /* @@@ name clash with master flag
253 bool type_visited(type *tp) {
254 assert(tp && tp->kind == k_type);
255 return tp->visit >= type_visited;
257 bool type_not_visited(type *tp) {
258 assert(tp && tp->kind == k_type);
259 return tp->visit < type_visited;
263 int is_type (void *thing) {
265 if (get_kind(thing) == k_type)
272 bool equal_type(type *typ1, type *typ2) {
277 if (typ1 == typ2) return true;
279 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
280 (get_type_name(typ1) != get_type_name(typ2)) ||
281 (get_type_mode(typ1) != get_type_mode(typ2)) ||
282 (get_type_state(typ1) != get_type_state(typ2)))
284 if ((get_type_state(typ1) == layout_fixed) &&
285 (get_type_size(typ1) != get_type_size(typ2)))
288 switch(get_type_tpop_code(typ1)) {
290 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
291 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
292 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
293 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
294 /** Compare the members **/
295 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
296 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
297 /* First sort the members of typ2 */
298 for (i = 0; i < get_class_n_members(typ1); i++) {
299 entity *e1 = get_class_member(typ1, i);
300 for (j = 0; j < get_class_n_members(typ2); j++) {
301 entity *e2 = get_class_member(typ2, j);
302 if (get_entity_name(e1) == get_entity_name(e2))
306 for (i = 0; i < get_class_n_members(typ1); i++) {
307 if (!m[i] || /* Found no counterpart */
308 !equal_entity(get_class_member(typ1, i), m[i]))
311 /** Compare the supertypes **/
312 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
313 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
314 /* First sort the supertypes of typ2 */
315 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
316 type *t1 = get_class_supertype(typ1, i);
317 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
318 type *t2 = get_class_supertype(typ2, j);
319 if (get_type_name(t2) == get_type_name(t1))
323 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
324 if (!t[i] || /* Found no counterpart */
325 get_class_supertype(typ1, i) != t[i])
330 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
331 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
332 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
333 /* First sort the members of lt */
334 for (i = 0; i < get_struct_n_members(typ1); i++) {
335 entity *e1 = get_struct_member(typ1, i);
336 for (j = 0; j < get_struct_n_members(typ2); j++) {
337 entity *e2 = get_struct_member(typ2, j);
338 if (get_entity_name(e1) == get_entity_name(e2))
342 for (i = 0; i < get_struct_n_members(typ1); i++) {
343 if (!m[i] || /* Found no counterpart */
344 !equal_entity(get_struct_member(typ1, i), m[i]))
349 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
350 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
351 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
352 for (i = 0; i < get_method_n_params(typ1); i++) {
353 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
356 for (i = 0; i < get_method_n_ress(typ1); i++) {
357 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
362 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
363 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
364 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
365 /* First sort the members of lt */
366 for (i = 0; i < get_union_n_members(typ1); i++) {
367 entity *e1 = get_union_member(typ1, i);
368 for (j = 0; j < get_union_n_members(typ2); j++) {
369 entity *e2 = get_union_member(typ2, j);
370 if (get_entity_name(e1) == get_entity_name(e2))
374 for (i = 0; i < get_union_n_members(typ1); i++) {
375 if (!m[i] || /* Found no counterpart */
376 !equal_entity(get_union_member(typ1, i), m[i]))
381 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
383 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
385 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
386 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
387 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
389 if (get_array_order(typ1, i) != get_array_order(typ2, i))
390 assert(0 && "type compare with different dimension orders not implemented");
393 case tpo_enumeration: {
394 assert(0 && "enumerations not implemented");
397 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
400 case tpo_primitive: {
407 bool smaller_type (type *st, type *lt) {
411 if (st == lt) return true;
413 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
416 switch(get_type_tpop_code(st)) {
418 return is_subclass_of(st, lt);
421 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
422 m = alloca(sizeof(entity *) * get_struct_n_members(st));
423 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
424 /* First sort the members of lt */
425 for (i = 0; i < get_struct_n_members(st); i++) {
426 entity *se = get_struct_member(st, i);
427 for (j = 0; j < get_struct_n_members(lt); j++) {
428 entity *le = get_struct_member(lt, j);
429 if (get_entity_name(le) == get_entity_name(se))
433 for (i = 0; i < get_struct_n_members(st); i++) {
434 if (!m[i] || /* Found no counterpart */
435 !smaller_type(get_entity_type(get_struct_member(st, i)),
436 get_entity_type(m[i])))
441 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
442 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
443 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
444 for (i = 0; i < get_method_n_params(st); i++) {
445 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
448 for (i = 0; i < get_method_n_ress(st); i++) {
449 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
454 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
455 m = alloca(sizeof(entity *) * get_union_n_members(st));
456 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
457 /* First sort the members of lt */
458 for (i = 0; i < get_union_n_members(st); i++) {
459 entity *se = get_union_member(st, i);
460 for (j = 0; j < get_union_n_members(lt); j++) {
461 entity *le = get_union_member(lt, j);
462 if (get_entity_name(le) == get_entity_name(se))
466 for (i = 0; i < get_union_n_members(st); i++) {
467 if (!m[i] || /* Found no counterpart */
468 !smaller_type(get_entity_type(get_union_member(st, i)),
469 get_entity_type(m[i])))
474 type *set, *let; /* small/large elt. type */
475 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
477 set = get_array_element_type(st);
478 let = get_array_element_type(lt);
480 /* If the elt types are different, set must be convertible
481 to let, and they must have the same size so that address
482 computations work out. To have a size the layout must
484 if ((get_type_state(set) != layout_fixed) ||
485 (get_type_state(let) != layout_fixed))
487 if (!smaller_type(set, let) ||
488 get_type_size(set) != get_type_size(let))
491 for(i = 0; i < get_array_n_dimensions(st); i++) {
492 if (get_array_lower_bound(lt, i))
493 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
495 if (get_array_upper_bound(lt, i))
496 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
500 case tpo_enumeration: {
501 assert(0 && "enumerations not implemented");
504 if (!smaller_type(get_pointer_points_to_type(st),
505 get_pointer_points_to_type(lt)))
508 case tpo_primitive: {
509 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
517 /*******************************************************************/
519 /*******************************************************************/
521 /* create a new class type */
522 INLINE type *new_type_class (ident *name) {
525 res = new_type(type_class, NULL, name);
527 res->attr.ca.members = NEW_ARR_F (entity *, 1);
528 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
529 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
530 res->attr.ca.peculiarity = existent;
531 res->attr.ca.dfn = 0;
535 type *new_d_type_class (ident *name, dbg_info* db) {
536 type *res = new_type_class (name);
537 set_type_dbg_info(res, db);
540 INLINE void free_class_attrs(type *clss) {
541 assert(clss && (clss->type_op == type_class));
542 DEL_ARR_F(clss->attr.ca.members);
543 DEL_ARR_F(clss->attr.ca.subtypes);
544 DEL_ARR_F(clss->attr.ca.supertypes);
547 /* manipulate private fields of class type */
548 void add_class_member (type *clss, entity *member) {
549 assert(clss && (clss->type_op == type_class));
550 ARR_APP1 (entity *, clss->attr.ca.members, member);
552 int get_class_n_members (type *clss) {
553 assert(clss && (clss->type_op == type_class));
554 return (ARR_LEN (clss->attr.ca.members))-1;
556 int get_class_member_index(type *clss, entity *mem) {
558 assert(clss && (clss->type_op == type_class));
559 for (i = 0; i < get_class_n_members(clss); i++)
560 if (get_class_member(clss, i) == mem)
564 entity *get_class_member (type *clss, int pos) {
565 assert(clss && (clss->type_op == type_class));
566 assert(pos >= 0 && pos < get_class_n_members(clss));
567 return clss->attr.ca.members[pos+1];
569 void set_class_member (type *clss, entity *member, int pos) {
570 assert(clss && (clss->type_op == type_class));
571 assert(pos >= 0 && pos < get_class_n_members(clss));
572 clss->attr.ca.members[pos+1] = member;
574 void set_class_members (type *clss, entity **members, int arity) {
576 assert(clss && (clss->type_op == type_class));
577 DEL_ARR_F(clss->attr.ca.members);
578 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
579 for (i = 0; i < arity; i++) {
580 set_entity_owner(members[i], clss);
581 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
584 void remove_class_member(type *clss, entity *member) {
586 assert(clss && (clss->type_op == type_class));
587 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
588 if (clss->attr.ca.members[i] == member) {
589 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
590 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
591 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
597 void add_class_subtype (type *clss, type *subtype) {
599 assert(clss && (clss->type_op == type_class));
600 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
601 for (i = 0; i < get_class_n_supertypes(subtype); i++)
602 if (get_class_supertype(subtype, i) == clss)
603 /* Class already registered */
605 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
607 int get_class_n_subtypes (type *clss) {
608 assert(clss && (clss->type_op == type_class));
609 return (ARR_LEN (clss->attr.ca.subtypes))-1;
611 type *get_class_subtype (type *clss, int pos) {
612 assert(clss && (clss->type_op == type_class));
613 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
614 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
616 void set_class_subtype (type *clss, type *subtype, int pos) {
617 assert(clss && (clss->type_op == type_class));
618 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
619 clss->attr.ca.subtypes[pos+1] = subtype;
621 void remove_class_subtype(type *clss, type *subtype) {
623 assert(clss && (clss->type_op == type_class));
624 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
625 if (clss->attr.ca.subtypes[i] == subtype) {
626 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
627 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
628 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
633 void add_class_supertype (type *clss, type *supertype) {
635 assert(clss && (clss->type_op == type_class));
636 assert(supertype && (supertype -> type_op == type_class));
637 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
638 for (i = 0; i < get_class_n_subtypes(supertype); i++)
639 if (get_class_subtype(supertype, i) == clss)
640 /* Class already registered */
642 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
644 int get_class_n_supertypes (type *clss) {
645 assert(clss && (clss->type_op == type_class));
646 return (ARR_LEN (clss->attr.ca.supertypes))-1;
648 int get_class_supertype_index(type *clss, type *super_clss) {
650 assert(clss && (clss->type_op == type_class));
651 assert(super_clss && (super_clss->type_op == type_class));
652 for (i = 0; i < get_class_n_supertypes(clss); i++)
653 if (get_class_supertype(clss, i) == super_clss)
657 type *get_class_supertype (type *clss, int pos) {
658 assert(clss && (clss->type_op == type_class));
659 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
660 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
662 void set_class_supertype (type *clss, type *supertype, int pos) {
663 assert(clss && (clss->type_op == type_class));
664 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
665 clss->attr.ca.supertypes[pos+1] = supertype;
667 void remove_class_supertype(type *clss, type *supertype) {
669 assert(clss && (clss->type_op == type_class));
670 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
671 if (clss->attr.ca.supertypes[i] == supertype) {
672 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
673 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
674 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
679 INLINE peculiarity get_class_peculiarity (type *clss) {
680 assert(clss && (clss->type_op == type_class));
681 return clss->attr.ca.peculiarity;
683 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
684 assert(clss && (clss->type_op == type_class));
685 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
686 clss->attr.ca.peculiarity = pec;
689 void set_class_dfn (type *clss, int dfn)
691 clss->attr.ca.dfn = dfn;
694 int get_class_dfn (type *clss)
696 return (clss->attr.ca.dfn);
700 bool is_class_type(type *clss) {
702 if (clss->type_op == type_class) return 1; else return 0;
705 bool is_subclass_of(type *low, type *high) {
707 assert(is_class_type(low) && is_class_type(high));
708 if (low == high) return true;
709 /* depth first search from high downwards. */
710 for (i = 0; i < get_class_n_subtypes(high); i++) {
711 if (low == get_class_subtype(high, i))
713 if (is_subclass_of(low, get_class_subtype(high, i)))
719 /*******************************************************************/
721 /*******************************************************************/
723 /* create a new type struct */
724 INLINE type *new_type_struct (ident *name) {
726 res = new_type(type_struct, NULL, name);
727 res->attr.sa.members = NEW_ARR_F (entity *, 1);
730 type *new_d_type_struct (ident *name, dbg_info* db) {
731 type *res = new_type_struct (name);
732 set_type_dbg_info(res, db);
735 INLINE void free_struct_attrs (type *strct) {
736 assert(strct && (strct->type_op == type_struct));
737 DEL_ARR_F(strct->attr.sa.members);
740 /* manipulate private fields of struct */
741 int get_struct_n_members (type *strct) {
742 assert(strct && (strct->type_op == type_struct));
743 return (ARR_LEN (strct->attr.sa.members))-1;
745 void add_struct_member (type *strct, entity *member) {
746 assert(strct && (strct->type_op == type_struct));
747 assert(get_type_tpop(get_entity_type(member)) != type_method);
748 /* @@@ lowerfirm geht nicht durch */
749 ARR_APP1 (entity *, strct->attr.sa.members, member);
751 entity *get_struct_member (type *strct, int pos) {
752 assert(strct && (strct->type_op == type_struct));
753 assert(pos >= 0 && pos < get_struct_n_members(strct));
754 return strct->attr.sa.members[pos+1];
756 void set_struct_member (type *strct, int pos, entity *member) {
757 assert(strct && (strct->type_op == type_struct));
758 assert(pos >= 0 && pos < get_struct_n_members(strct));
759 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
760 strct->attr.sa.members[pos+1] = member;
762 void remove_struct_member(type *strct, entity *member) {
764 assert(strct && (strct->type_op == type_struct));
765 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
766 if (strct->attr.sa.members[i] == member) {
767 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
768 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
769 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
775 bool is_struct_type(type *strct) {
777 if (strct->type_op == type_struct) return 1; else return 0;
780 /*******************************************************************/
782 /*******************************************************************/
784 /* Create a new method type.
785 N_param is the number of parameters, n_res the number of results. */
786 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
788 res = new_type(type_method, mode_P, name);
789 res->state = layout_fixed;
790 res->size = get_mode_size(mode_P);
791 res->attr.ma.n_params = n_param;
792 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
793 res->attr.ma.n_res = n_res;
794 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
795 res->attr.ma.variadicity = non_variadic;
798 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
799 type *res = new_type_method (name, n_param, n_res);
800 set_type_dbg_info(res, db);
803 INLINE void free_method_attrs(type *method) {
804 assert(method && (method->type_op == type_method));
805 free(method->attr.ma.param_type);
806 free(method->attr.ma.res_type);
808 /* manipulate private fields of method. */
809 int get_method_n_params (type *method) {
810 assert(method && (method->type_op == type_method));
811 return method->attr.ma.n_params;
813 type *get_method_param_type(type *method, int pos) {
814 assert(method && (method->type_op == type_method));
815 assert(pos >= 0 && pos < get_method_n_params(method));
816 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
818 void set_method_param_type(type *method, int pos, type* tp) {
819 assert(method && (method->type_op == type_method));
820 assert(pos >= 0 && pos < get_method_n_params(method));
821 method->attr.ma.param_type[pos] = tp;
824 int get_method_n_ress (type *method) {
825 assert(method && (method->type_op == type_method));
826 return method->attr.ma.n_res;
828 type *get_method_res_type(type *method, int pos) {
829 assert(method && (method->type_op == type_method));
830 assert(pos >= 0 && pos < get_method_n_ress(method));
831 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
833 void set_method_res_type(type *method, int pos, type* tp) {
834 assert(method && (method->type_op == type_method));
835 assert(pos >= 0 && pos < get_method_n_ress(method));
836 method->attr.ma.res_type[pos] = tp;
839 variadicity get_method_variadicity(type *method)
841 assert(method && (method->type_op == type_method));
842 return method->attr.ma.variadicity;
845 void set_method_variadicity(type *method, variadicity vari)
847 assert(method && (method->type_op == type_method));
848 method->attr.ma.variadicity = vari;
852 bool is_method_type (type *method) {
854 if (method->type_op == type_method) return 1; else return 0;
857 /*******************************************************************/
859 /*******************************************************************/
861 /* create a new type uni */
862 INLINE type *new_type_union (ident *name) {
864 res = new_type(type_union, NULL, name);
865 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
866 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
867 res->attr.ua.members = NEW_ARR_F (entity *, 1);
870 type *new_d_type_union (ident *name, dbg_info* db) {
871 type *res = new_type_union (name);
872 set_type_dbg_info(res, db);
875 INLINE void free_union_attrs (type *uni) {
876 assert(uni && (uni->type_op == type_union));
877 DEL_ARR_F(uni->attr.ua.members);
879 /* manipulate private fields of union */
881 int get_union_n_types (type *uni) {
882 assert(uni && (uni->type_op == type_union));
883 return uni->attr.ua.n_types;
885 type *get_union_unioned_type (type *uni, int pos) {
886 assert(uni && (uni->type_op == type_union));
887 assert(pos >= 0 && pos < get_union_n_types(uni));
888 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
890 void set_union_unioned_type (type *uni, int pos, type *tp) {
891 assert(uni && (uni->type_op == type_union));
892 assert(pos >= 0 && pos < get_union_n_types(uni));
893 uni->attr.ua.unioned_type[pos] = tp;
895 ident *get_union_delim_nameid (type *uni, int pos) {
896 assert(uni && (uni->type_op == type_union));
897 assert(pos >= 0 && pos < get_union_n_types(uni));
898 return uni->attr.ua.delim_names[pos];
900 const char *get_union_delim_name (type *uni, int pos) {
901 assert(uni && (uni->type_op == type_union));
902 assert(pos >= 0 && pos < get_union_n_types(uni));
903 return id_to_str(uni->attr.ua.delim_names[pos]);
905 void set_union_delim_nameid (type *uni, int pos, ident *id) {
906 assert(uni && (uni->type_op == type_union));
907 assert(pos >= 0 && pos < get_union_n_types(uni));
908 uni->attr.ua.delim_names[pos] = id;
911 int get_union_n_members (type *uni) {
912 assert(uni && (uni->type_op == type_union));
913 return (ARR_LEN (uni->attr.ua.members))-1;
915 void add_union_member (type *uni, entity *member) {
916 assert(uni && (uni->type_op == type_union));
917 ARR_APP1 (entity *, uni->attr.ua.members, member);
919 entity *get_union_member (type *uni, int pos) {
920 assert(uni && (uni->type_op == type_union));
921 assert(pos >= 0 && pos < get_union_n_members(uni));
922 return uni->attr.ua.members[pos+1];
924 void set_union_member (type *uni, int pos, entity *member) {
925 assert(uni && (uni->type_op == type_union));
926 assert(pos >= 0 && pos < get_union_n_members(uni));
927 uni->attr.ua.members[pos+1] = member;
929 void remove_union_member(type *uni, entity *member) {
931 assert(uni && (uni->type_op == type_union));
932 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
933 if (uni->attr.ua.members[i] == member) {
934 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
935 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
936 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
942 bool is_union_type (type *uni) {
944 if (uni->type_op == type_union) return 1; else return 0;
947 /*******************************************************************/
949 /*******************************************************************/
952 /* create a new type array -- set dimension sizes independently */
953 INLINE type *new_type_array (ident *name, int n_dimensions,
954 type *element_type) {
957 assert(!is_method_type(element_type));
958 res = new_type(type_array, NULL, name);
959 res->attr.aa.n_dimensions = n_dimensions;
960 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
961 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
962 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
964 for (i = 0; i < n_dimensions; i++) {
965 res->attr.aa.lower_bound[i] = NULL;
966 res->attr.aa.upper_bound[i] = NULL;
967 res->attr.aa.order[i] = i;
969 res->attr.aa.element_type = element_type;
970 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
973 type *new_d_type_array (ident *name, int n_dimensions,
974 type *element_type, dbg_info* db) {
975 type *res = new_type_array (name, n_dimensions, element_type);
976 set_type_dbg_info(res, db);
980 INLINE void free_array_attrs (type *array) {
981 assert(array && (array->type_op == type_array));
982 free(array->attr.aa.lower_bound);
983 free(array->attr.aa.upper_bound);
986 /* manipulate private fields of array type */
987 int get_array_n_dimensions (type *array) {
988 assert(array && (array->type_op == type_array));
989 return array->attr.aa.n_dimensions;
993 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
994 ir_node * upper_bound) {
995 assert(array && (array->type_op == type_array));
996 array->attr.aa.lower_bound[dimension] = lower_bound;
997 array->attr.aa.upper_bound[dimension] = upper_bound;
1000 set_array_bounds_int (type *array, int dimension, int lower_bound,
1002 ir_graph *rem = current_ir_graph;
1003 current_ir_graph = get_const_code_irg();
1004 set_array_bounds (array, dimension,
1005 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)),
1006 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1007 current_ir_graph = rem;
1010 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1011 assert(array && (array->type_op == type_array));
1012 array->attr.aa.lower_bound[dimension] = lower_bound;
1014 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1015 ir_graph *rem = current_ir_graph;
1016 current_ir_graph = get_const_code_irg();
1017 set_array_lower_bound (array, dimension,
1018 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)));
1019 current_ir_graph = rem;
1022 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1023 assert(array && (array->type_op == type_array));
1024 array->attr.aa.upper_bound[dimension] = upper_bound;
1026 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1027 ir_graph *rem = current_ir_graph;
1028 current_ir_graph = get_const_code_irg();
1029 set_array_upper_bound (array, dimension,
1030 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1031 current_ir_graph = rem;
1033 ir_node * get_array_lower_bound (type *array, int dimension) {
1034 assert(array && (array->type_op == type_array));
1035 return array->attr.aa.lower_bound[dimension];
1037 ir_node * get_array_upper_bound (type *array, int dimension) {
1038 assert(array && (array->type_op == type_array));
1039 return array->attr.aa.upper_bound[dimension];
1042 void set_array_order (type *array, int dimension, int order) {
1043 assert(array && (array->type_op == type_array));
1044 array->attr.aa.order[dimension] = order;
1046 int get_array_order (type *array, int dimension) {
1047 assert(array && (array->type_op == type_array));
1048 return array->attr.aa.order[dimension];
1051 void set_array_element_type (type *array, type *tp) {
1052 assert(array && (array->type_op == type_array));
1053 assert(!is_method_type(tp));
1054 array->attr.aa.element_type = tp;
1056 type *get_array_element_type (type *array) {
1057 assert(array && (array->type_op == type_array));
1058 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1061 void set_array_element_entity (type *array, entity *ent) {
1062 assert(array && (array->type_op == type_array));
1063 assert((get_entity_type(ent)->type_op != type_method));
1064 array->attr.aa.element_ent = ent;
1065 array->attr.aa.element_type = get_entity_type(ent);
1067 entity *get_array_element_entity (type *array) {
1068 assert(array && (array->type_op == type_array));
1069 return array->attr.aa.element_ent;
1073 bool is_array_type (type *array) {
1075 if (array->type_op == type_array) return 1; else return 0;
1078 /*******************************************************************/
1079 /** TYPE_ENUMERATION **/
1080 /*******************************************************************/
1082 /* create a new type enumeration -- set the enumerators independently */
1083 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1086 res = new_type(type_enumeration, NULL, name);
1087 res->attr.ea.n_enums = n_enums;
1088 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1089 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1090 for (i = 0; i < n_enums; i++) {
1091 res->attr.ea.enumer[i] = NULL;
1092 res->attr.ea.enum_nameid = NULL;
1096 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1097 type *res = new_type_enumeration (name, n_enums);
1098 set_type_dbg_info(res, db);
1102 INLINE void free_enumeration_attrs(type *enumeration) {
1103 assert(enumeration && (enumeration->type_op == type_enumeration));
1104 free(enumeration->attr.ea.enumer);
1105 free(enumeration->attr.ea.enum_nameid);
1108 /* manipulate fields of enumeration type. */
1109 int get_enumeration_n_enums (type *enumeration) {
1110 assert(enumeration && (enumeration->type_op == type_enumeration));
1111 return enumeration->attr.ea.n_enums;
1113 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1114 assert(enumeration && (enumeration->type_op == type_enumeration));
1115 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1116 enumeration->attr.ea.enumer[pos] = con;
1118 tarval *get_enumeration_enum (type *enumeration, int pos) {
1119 assert(enumeration && (enumeration->type_op == type_enumeration));
1120 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1121 return enumeration->attr.ea.enumer[pos];
1123 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1124 assert(enumeration && (enumeration->type_op == type_enumeration));
1125 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1126 enumeration->attr.ea.enum_nameid[pos] = id;
1128 ident *get_enumeration_nameid (type *enumeration, int pos) {
1129 assert(enumeration && (enumeration->type_op == type_enumeration));
1130 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1131 return enumeration->attr.ea.enum_nameid[pos];
1133 const char *get_enumeration_name(type *enumeration, int pos) {
1134 assert(enumeration && (enumeration->type_op == type_enumeration));
1135 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1136 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1140 bool is_enumeration_type (type *enumeration) {
1141 assert(enumeration);
1142 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1145 /*******************************************************************/
1146 /** TYPE_POINTER **/
1147 /*******************************************************************/
1149 /* Create a new type pointer */
1150 INLINE type *new_type_pointer (ident *name, type *points_to) {
1152 res = new_type(type_pointer, mode_P, name);
1153 res->attr.pa.points_to = points_to;
1154 res->size = get_mode_size(res->mode);
1155 res->state = layout_fixed;
1158 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1159 type *res = new_type_pointer (name, points_to);
1160 set_type_dbg_info(res, db);
1163 INLINE void free_pointer_attrs (type *pointer) {
1164 assert(pointer && (pointer->type_op == type_pointer));
1166 /* manipulate fields of type_pointer */
1167 void set_pointer_points_to_type (type *pointer, type *tp) {
1168 assert(pointer && (pointer->type_op == type_pointer));
1169 pointer->attr.pa.points_to = tp;
1171 type *get_pointer_points_to_type (type *pointer) {
1172 assert(pointer && (pointer->type_op == type_pointer));
1173 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1177 bool is_pointer_type (type *pointer) {
1179 if (pointer->type_op == type_pointer) return 1; else return 0;
1183 /*******************************************************************/
1184 /** TYPE_PRIMITIVE **/
1185 /*******************************************************************/
1187 /* create a new type primitive */
1188 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1190 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1191 res = new_type(type_primitive, mode, name);
1192 res->size = get_mode_size(mode);
1193 res->state = layout_fixed;
1196 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1197 type *res = new_type_primitive (name, mode);
1198 set_type_dbg_info(res, db);
1201 INLINE void free_primitive_attrs (type *primitive) {
1202 assert(primitive && (primitive->type_op == type_primitive));
1206 bool is_primitive_type (type *primitive) {
1207 assert(primitive && primitive->kind == k_type);
1208 if (primitive->type_op == type_primitive) return 1; else return 0;
1211 /*******************************************************************/
1212 /** common functionality **/
1213 /*******************************************************************/
1216 INLINE int is_atomic_type(type *tp) {
1217 assert(tp && tp->kind == k_type);
1218 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1219 is_enumeration_type(tp));
1221 INLINE int is_compound_type(type *tp) {
1222 assert(tp && tp->kind == k_type);
1223 return (is_class_type(tp) || is_struct_type(tp) ||
1224 is_array_type(tp) || is_union_type(tp));