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);
67 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
69 node_size = offsetof (type, attr) + type_op->attr_size;
70 res = (type *) xmalloc (node_size);
71 add_irp_type(res); /* Remember the new type global. */
74 res->type_op = type_op;
77 res->state = layout_undefined;
85 void free_type_attrs(type *tp) {
86 switch(get_type_tpop_code(tp)) {
87 case tpo_class: { free_class_attrs(tp); } break;
88 case tpo_struct: { free_struct_attrs(tp); } break;
89 case tpo_method: { free_method_attrs(tp); } break;
90 case tpo_union: { free_union_attrs(tp); } break;
91 case tpo_array: { free_array_attrs(tp); } break;
92 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
93 case tpo_pointer: { free_pointer_attrs(tp); } break;
94 case tpo_primitive: { free_primitive_attrs(tp); } break;
99 /* set/get the link field */
100 void *get_type_link(type *tp)
102 assert(tp && tp->kind == k_type);
106 void set_type_link(type *tp, void *l)
108 assert(tp && tp->kind == k_type);
112 tp_op* get_type_tpop(type *tp) {
113 assert(tp && tp->kind == k_type);
117 ident* get_type_tpop_nameid(type *tp) {
118 assert(tp && tp->kind == k_type);
119 return tp->type_op->name;
122 const char* get_type_tpop_name(type *tp) {
123 assert(tp && tp->kind == k_type);
124 return id_to_str(tp->type_op->name);
127 tp_opcode get_type_tpop_code(type *tp) {
128 assert(tp && tp->kind == k_type);
129 return tp->type_op->code;
132 ir_mode* get_type_mode(type *tp) {
133 assert(tp && tp->kind == k_type);
137 void set_type_mode(type *tp, ir_mode* m) {
138 assert(tp && tp->kind == k_type);
140 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
141 /* Modes of primitives must be data */
142 ((tp->type_op != type_enumeration) || mode_is_int(m)));
143 /* Modes of enumerations must be integers */
145 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
146 /* For pointer, primitive and enumeration size depends on the mode. */
147 tp->size = get_mode_size(m);
152 ident* get_type_ident(type *tp) {
153 assert(tp && tp->kind == k_type);
157 void set_type_ident(type *tp, ident* id) {
158 assert(tp && tp->kind == k_type);
162 const char* get_type_name(type *tp) {
163 assert(tp && tp->kind == k_type);
164 return (id_to_str(tp->name));
167 int get_type_size(type *tp) {
168 assert(tp && tp->kind == k_type);
173 set_type_size(type *tp, int size) {
174 assert(tp && tp->kind == k_type);
175 /* For pointer enumeration and primitive size depends on the mode.
176 Methods don't have a size. */
177 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
178 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
183 get_type_state(type *tp) {
184 assert(tp && tp->kind == k_type);
189 set_type_state(type *tp, type_state state) {
190 assert(tp && tp->kind == k_type);
192 if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
193 (tp->type_op == type_method))
196 /* Just a correctness check: */
197 if (state == layout_fixed) {
199 switch (get_type_tpop_code(tp)) {
202 assert(get_type_size(tp) > -1);
203 if (tp != get_glob_type())
204 for (i = 0; i < get_class_n_members(tp); i++) {
205 assert(get_entity_offset(get_class_member(tp, i)) > -1);
206 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
207 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
212 /* assert(get_type_size(tp) > -1); @@@ lowerfirm geht nicht durch */
213 for (i = 0; i < get_struct_n_members(tp); i++) {
214 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
215 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
224 Assure that only innermost dimension is dynamic? */
226 case tpo_enumeration:
228 assert(get_type_mode != NULL);
229 for (i = 0; i < get_enumeration_n_enums(tp); i++)
230 assert(get_enumeration_enum(tp, i) != NULL);
238 unsigned long get_type_visited(type *tp) {
239 assert(tp && tp->kind == k_type);
243 void set_type_visited(type *tp, unsigned long num) {
244 assert(tp && tp->kind == k_type);
247 /* Sets visited field in type to type_visited. */
248 void mark_type_visited(type *tp) {
249 assert(tp && tp->kind == k_type);
250 assert(tp->visit < type_visited);
251 tp->visit = type_visited;
253 /* @@@ name clash with master flag
254 bool type_visited(type *tp) {
255 assert(tp && tp->kind == k_type);
256 return tp->visit >= type_visited;
258 bool type_not_visited(type *tp) {
259 assert(tp && tp->kind == k_type);
260 return tp->visit < type_visited;
264 int is_type (void *thing) {
266 if (get_kind(thing) == k_type)
273 bool equal_type(type *typ1, type *typ2) {
278 if (typ1 == typ2) return true;
280 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
281 (get_type_name(typ1) != get_type_name(typ2)) ||
282 (get_type_mode(typ1) != get_type_mode(typ2)) ||
283 (get_type_state(typ1) != get_type_state(typ2)))
285 if ((get_type_state(typ1) == layout_fixed) &&
286 (get_type_size(typ1) != get_type_size(typ2)))
289 switch(get_type_tpop_code(typ1)) {
291 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
292 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
293 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
294 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
295 /** Compare the members **/
296 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
297 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
298 /* First sort the members of typ2 */
299 for (i = 0; i < get_class_n_members(typ1); i++) {
300 entity *e1 = get_class_member(typ1, i);
301 for (j = 0; j < get_class_n_members(typ2); j++) {
302 entity *e2 = get_class_member(typ2, j);
303 if (get_entity_name(e1) == get_entity_name(e2))
307 for (i = 0; i < get_class_n_members(typ1); i++) {
308 if (!m[i] || /* Found no counterpart */
309 !equal_entity(get_class_member(typ1, i), m[i]))
312 /** Compare the supertypes **/
313 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
314 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
315 /* First sort the supertypes of typ2 */
316 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
317 type *t1 = get_class_supertype(typ1, i);
318 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
319 type *t2 = get_class_supertype(typ2, j);
320 if (get_type_name(t2) == get_type_name(t1))
324 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
325 if (!t[i] || /* Found no counterpart */
326 get_class_supertype(typ1, i) != t[i])
331 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
332 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
333 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
334 /* First sort the members of lt */
335 for (i = 0; i < get_struct_n_members(typ1); i++) {
336 entity *e1 = get_struct_member(typ1, i);
337 for (j = 0; j < get_struct_n_members(typ2); j++) {
338 entity *e2 = get_struct_member(typ2, j);
339 if (get_entity_name(e1) == get_entity_name(e2))
343 for (i = 0; i < get_struct_n_members(typ1); i++) {
344 if (!m[i] || /* Found no counterpart */
345 !equal_entity(get_struct_member(typ1, i), m[i]))
350 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
351 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
352 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
353 for (i = 0; i < get_method_n_params(typ1); i++) {
354 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
357 for (i = 0; i < get_method_n_ress(typ1); i++) {
358 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
363 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
364 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
365 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
366 /* First sort the members of lt */
367 for (i = 0; i < get_union_n_members(typ1); i++) {
368 entity *e1 = get_union_member(typ1, i);
369 for (j = 0; j < get_union_n_members(typ2); j++) {
370 entity *e2 = get_union_member(typ2, j);
371 if (get_entity_name(e1) == get_entity_name(e2))
375 for (i = 0; i < get_union_n_members(typ1); i++) {
376 if (!m[i] || /* Found no counterpart */
377 !equal_entity(get_union_member(typ1, i), m[i]))
382 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
384 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
386 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
387 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
388 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
390 if (get_array_order(typ1, i) != get_array_order(typ2, i))
391 assert(0 && "type compare with different dimension orders not implemented");
394 case tpo_enumeration: {
395 assert(0 && "enumerations not implemented");
398 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
401 case tpo_primitive: {
408 bool smaller_type (type *st, type *lt) {
412 if (st == lt) return true;
414 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
417 switch(get_type_tpop_code(st)) {
419 return is_subclass_of(st, lt);
422 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
423 m = alloca(sizeof(entity *) * get_struct_n_members(st));
424 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
425 /* First sort the members of lt */
426 for (i = 0; i < get_struct_n_members(st); i++) {
427 entity *se = get_struct_member(st, i);
428 for (j = 0; j < get_struct_n_members(lt); j++) {
429 entity *le = get_struct_member(lt, j);
430 if (get_entity_name(le) == get_entity_name(se))
434 for (i = 0; i < get_struct_n_members(st); i++) {
435 if (!m[i] || /* Found no counterpart */
436 !smaller_type(get_entity_type(get_struct_member(st, i)),
437 get_entity_type(m[i])))
442 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
443 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
444 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
445 for (i = 0; i < get_method_n_params(st); i++) {
446 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
449 for (i = 0; i < get_method_n_ress(st); i++) {
450 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
455 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
456 m = alloca(sizeof(entity *) * get_union_n_members(st));
457 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
458 /* First sort the members of lt */
459 for (i = 0; i < get_union_n_members(st); i++) {
460 entity *se = get_union_member(st, i);
461 for (j = 0; j < get_union_n_members(lt); j++) {
462 entity *le = get_union_member(lt, j);
463 if (get_entity_name(le) == get_entity_name(se))
467 for (i = 0; i < get_union_n_members(st); i++) {
468 if (!m[i] || /* Found no counterpart */
469 !smaller_type(get_entity_type(get_union_member(st, i)),
470 get_entity_type(m[i])))
475 type *set, *let; /* small/large elt. type */
476 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
478 set = get_array_element_type(st);
479 let = get_array_element_type(lt);
481 /* If the elt types are different, set must be convertible
482 to let, and they must have the same size so that address
483 computations work out. To have a size the layout must
485 if ((get_type_state(set) != layout_fixed) ||
486 (get_type_state(let) != layout_fixed))
488 if (!smaller_type(set, let) ||
489 get_type_size(set) != get_type_size(let))
492 for(i = 0; i < get_array_n_dimensions(st); i++) {
493 if (get_array_lower_bound(lt, i))
494 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
496 if (get_array_upper_bound(lt, i))
497 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
501 case tpo_enumeration: {
502 assert(0 && "enumerations not implemented");
505 if (!smaller_type(get_pointer_points_to_type(st),
506 get_pointer_points_to_type(lt)))
509 case tpo_primitive: {
510 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
518 /*******************************************************************/
520 /*******************************************************************/
522 /* create a new class type */
523 INLINE type *new_type_class (ident *name) {
526 res = new_type(type_class, NULL, name);
528 res->attr.ca.members = NEW_ARR_F (entity *, 1);
529 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
530 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
531 res->attr.ca.peculiarity = existent;
532 res->attr.ca.dfn = 0;
536 type *new_d_type_class (ident *name, dbg_info* db) {
537 type *res = new_type_class (name);
538 set_type_dbg_info(res, db);
541 INLINE void free_class_attrs(type *clss) {
542 assert(clss && (clss->type_op == type_class));
543 DEL_ARR_F(clss->attr.ca.members);
544 DEL_ARR_F(clss->attr.ca.subtypes);
545 DEL_ARR_F(clss->attr.ca.supertypes);
548 /* manipulate private fields of class type */
549 void add_class_member (type *clss, entity *member) {
550 assert(clss && (clss->type_op == type_class));
551 ARR_APP1 (entity *, clss->attr.ca.members, member);
553 int get_class_n_members (type *clss) {
554 assert(clss && (clss->type_op == type_class));
555 return (ARR_LEN (clss->attr.ca.members))-1;
557 int get_class_member_index(type *clss, entity *mem) {
559 assert(clss && (clss->type_op == type_class));
560 for (i = 0; i < get_class_n_members(clss); i++)
561 if (get_class_member(clss, i) == mem)
565 entity *get_class_member (type *clss, int pos) {
566 assert(clss && (clss->type_op == type_class));
567 assert(pos >= 0 && pos < get_class_n_members(clss));
568 return clss->attr.ca.members[pos+1];
570 void set_class_member (type *clss, entity *member, int pos) {
571 assert(clss && (clss->type_op == type_class));
572 assert(pos >= 0 && pos < get_class_n_members(clss));
573 clss->attr.ca.members[pos+1] = member;
575 void set_class_members (type *clss, entity **members, int arity) {
577 assert(clss && (clss->type_op == type_class));
578 DEL_ARR_F(clss->attr.ca.members);
579 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
580 for (i = 0; i < arity; i++) {
581 set_entity_owner(members[i], clss);
582 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
585 void remove_class_member(type *clss, entity *member) {
587 assert(clss && (clss->type_op == type_class));
588 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
589 if (clss->attr.ca.members[i] == member) {
590 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
591 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
592 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
598 void add_class_subtype (type *clss, type *subtype) {
600 assert(clss && (clss->type_op == type_class));
601 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
602 for (i = 0; i < get_class_n_supertypes(subtype); i++)
603 if (get_class_supertype(subtype, i) == clss)
604 /* Class already registered */
606 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
608 int get_class_n_subtypes (type *clss) {
609 assert(clss && (clss->type_op == type_class));
610 return (ARR_LEN (clss->attr.ca.subtypes))-1;
612 type *get_class_subtype (type *clss, int pos) {
613 assert(clss && (clss->type_op == type_class));
614 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
615 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
617 void set_class_subtype (type *clss, type *subtype, int pos) {
618 assert(clss && (clss->type_op == type_class));
619 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
620 clss->attr.ca.subtypes[pos+1] = subtype;
622 void remove_class_subtype(type *clss, type *subtype) {
624 assert(clss && (clss->type_op == type_class));
625 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
626 if (clss->attr.ca.subtypes[i] == subtype) {
627 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
628 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
629 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
634 void add_class_supertype (type *clss, type *supertype) {
636 assert(clss && (clss->type_op == type_class));
637 assert(supertype && (supertype -> type_op == type_class));
638 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
639 for (i = 0; i < get_class_n_subtypes(supertype); i++)
640 if (get_class_subtype(supertype, i) == clss)
641 /* Class already registered */
643 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
645 int get_class_n_supertypes (type *clss) {
646 assert(clss && (clss->type_op == type_class));
647 return (ARR_LEN (clss->attr.ca.supertypes))-1;
649 int get_class_supertype_index(type *clss, type *super_clss) {
651 assert(clss && (clss->type_op == type_class));
652 assert(super_clss && (super_clss->type_op == type_class));
653 for (i = 0; i < get_class_n_supertypes(clss); i++)
654 if (get_class_supertype(clss, i) == super_clss)
658 type *get_class_supertype (type *clss, int pos) {
659 assert(clss && (clss->type_op == type_class));
660 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
661 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
663 void set_class_supertype (type *clss, type *supertype, int pos) {
664 assert(clss && (clss->type_op == type_class));
665 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
666 clss->attr.ca.supertypes[pos+1] = supertype;
668 void remove_class_supertype(type *clss, type *supertype) {
670 assert(clss && (clss->type_op == type_class));
671 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
672 if (clss->attr.ca.supertypes[i] == supertype) {
673 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
674 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
675 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
680 INLINE peculiarity get_class_peculiarity (type *clss) {
681 assert(clss && (clss->type_op == type_class));
682 return clss->attr.ca.peculiarity;
684 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
685 assert(clss && (clss->type_op == type_class));
686 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
687 clss->attr.ca.peculiarity = pec;
690 void set_class_dfn (type *clss, int dfn)
692 clss->attr.ca.dfn = dfn;
695 int get_class_dfn (type *clss)
697 return (clss->attr.ca.dfn);
701 bool is_class_type(type *clss) {
703 if (clss->type_op == type_class) return 1; else return 0;
706 bool is_subclass_of(type *low, type *high) {
708 assert(is_class_type(low) && is_class_type(high));
709 if (low == high) return true;
710 /* depth first search from high downwards. */
711 for (i = 0; i < get_class_n_subtypes(high); i++) {
712 if (low == get_class_subtype(high, i))
714 if (is_subclass_of(low, get_class_subtype(high, i)))
720 /*******************************************************************/
722 /*******************************************************************/
724 /* create a new type struct */
725 INLINE type *new_type_struct (ident *name) {
727 res = new_type(type_struct, NULL, name);
728 res->attr.sa.members = NEW_ARR_F (entity *, 1);
731 type *new_d_type_struct (ident *name, dbg_info* db) {
732 type *res = new_type_struct (name);
733 set_type_dbg_info(res, db);
736 INLINE void free_struct_attrs (type *strct) {
737 assert(strct && (strct->type_op == type_struct));
738 DEL_ARR_F(strct->attr.sa.members);
741 /* manipulate private fields of struct */
742 int get_struct_n_members (type *strct) {
743 assert(strct && (strct->type_op == type_struct));
744 return (ARR_LEN (strct->attr.sa.members))-1;
746 void add_struct_member (type *strct, entity *member) {
747 assert(strct && (strct->type_op == type_struct));
748 assert(get_type_tpop(get_entity_type(member)) != type_method);
749 /* @@@ lowerfirm geht nicht durch */
750 ARR_APP1 (entity *, strct->attr.sa.members, member);
752 entity *get_struct_member (type *strct, int pos) {
753 assert(strct && (strct->type_op == type_struct));
754 assert(pos >= 0 && pos < get_struct_n_members(strct));
755 return strct->attr.sa.members[pos+1];
757 void set_struct_member (type *strct, int pos, entity *member) {
758 assert(strct && (strct->type_op == type_struct));
759 assert(pos >= 0 && pos < get_struct_n_members(strct));
760 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
761 strct->attr.sa.members[pos+1] = member;
763 void remove_struct_member(type *strct, entity *member) {
765 assert(strct && (strct->type_op == type_struct));
766 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
767 if (strct->attr.sa.members[i] == member) {
768 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
769 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
770 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
776 bool is_struct_type(type *strct) {
778 if (strct->type_op == type_struct) return 1; else return 0;
781 /*******************************************************************/
783 /*******************************************************************/
785 /* Create a new method type.
786 N_param is the number of parameters, n_res the number of results. */
787 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
789 res = new_type(type_method, mode_P, name);
790 res->state = layout_fixed;
791 res->size = get_mode_size(mode_P);
792 res->attr.ma.n_params = n_param;
793 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
794 res->attr.ma.n_res = n_res;
795 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
796 res->attr.ma.variadicity = non_variadic;
799 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
800 type *res = new_type_method (name, n_param, n_res);
801 set_type_dbg_info(res, db);
804 INLINE void free_method_attrs(type *method) {
805 assert(method && (method->type_op == type_method));
806 free(method->attr.ma.param_type);
807 free(method->attr.ma.res_type);
809 /* manipulate private fields of method. */
810 int get_method_n_params (type *method) {
811 assert(method && (method->type_op == type_method));
812 return method->attr.ma.n_params;
814 type *get_method_param_type(type *method, int pos) {
815 assert(method && (method->type_op == type_method));
816 assert(pos >= 0 && pos < get_method_n_params(method));
817 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
819 void set_method_param_type(type *method, int pos, type* tp) {
820 assert(method && (method->type_op == type_method));
821 assert(pos >= 0 && pos < get_method_n_params(method));
822 method->attr.ma.param_type[pos] = tp;
825 int get_method_n_ress (type *method) {
826 assert(method && (method->type_op == type_method));
827 return method->attr.ma.n_res;
829 type *get_method_res_type(type *method, int pos) {
830 assert(method && (method->type_op == type_method));
831 assert(pos >= 0 && pos < get_method_n_ress(method));
832 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
834 void set_method_res_type(type *method, int pos, type* tp) {
835 assert(method && (method->type_op == type_method));
836 assert(pos >= 0 && pos < get_method_n_ress(method));
837 method->attr.ma.res_type[pos] = tp;
840 variadicity get_method_variadicity(type *method)
842 assert(method && (method->type_op == type_method));
843 return method->attr.ma.variadicity;
846 void set_method_variadicity(type *method, variadicity vari)
848 assert(method && (method->type_op == type_method));
849 method->attr.ma.variadicity = vari;
853 bool is_method_type (type *method) {
855 if (method->type_op == type_method) return 1; else return 0;
858 /*******************************************************************/
860 /*******************************************************************/
862 /* create a new type uni */
863 INLINE type *new_type_union (ident *name) {
865 res = new_type(type_union, NULL, name);
866 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
867 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
868 res->attr.ua.members = NEW_ARR_F (entity *, 1);
871 type *new_d_type_union (ident *name, dbg_info* db) {
872 type *res = new_type_union (name);
873 set_type_dbg_info(res, db);
876 INLINE void free_union_attrs (type *uni) {
877 assert(uni && (uni->type_op == type_union));
878 DEL_ARR_F(uni->attr.ua.members);
880 /* manipulate private fields of union */
882 int get_union_n_types (type *uni) {
883 assert(uni && (uni->type_op == type_union));
884 return uni->attr.ua.n_types;
886 type *get_union_unioned_type (type *uni, int pos) {
887 assert(uni && (uni->type_op == type_union));
888 assert(pos >= 0 && pos < get_union_n_types(uni));
889 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
891 void set_union_unioned_type (type *uni, int pos, type *tp) {
892 assert(uni && (uni->type_op == type_union));
893 assert(pos >= 0 && pos < get_union_n_types(uni));
894 uni->attr.ua.unioned_type[pos] = tp;
896 ident *get_union_delim_nameid (type *uni, int pos) {
897 assert(uni && (uni->type_op == type_union));
898 assert(pos >= 0 && pos < get_union_n_types(uni));
899 return uni->attr.ua.delim_names[pos];
901 const char *get_union_delim_name (type *uni, int pos) {
902 assert(uni && (uni->type_op == type_union));
903 assert(pos >= 0 && pos < get_union_n_types(uni));
904 return id_to_str(uni->attr.ua.delim_names[pos]);
906 void set_union_delim_nameid (type *uni, int pos, ident *id) {
907 assert(uni && (uni->type_op == type_union));
908 assert(pos >= 0 && pos < get_union_n_types(uni));
909 uni->attr.ua.delim_names[pos] = id;
912 int get_union_n_members (type *uni) {
913 assert(uni && (uni->type_op == type_union));
914 return (ARR_LEN (uni->attr.ua.members))-1;
916 void add_union_member (type *uni, entity *member) {
917 assert(uni && (uni->type_op == type_union));
918 ARR_APP1 (entity *, uni->attr.ua.members, member);
920 entity *get_union_member (type *uni, int pos) {
921 assert(uni && (uni->type_op == type_union));
922 assert(pos >= 0 && pos < get_union_n_members(uni));
923 return uni->attr.ua.members[pos+1];
925 void set_union_member (type *uni, int pos, entity *member) {
926 assert(uni && (uni->type_op == type_union));
927 assert(pos >= 0 && pos < get_union_n_members(uni));
928 uni->attr.ua.members[pos+1] = member;
930 void remove_union_member(type *uni, entity *member) {
932 assert(uni && (uni->type_op == type_union));
933 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
934 if (uni->attr.ua.members[i] == member) {
935 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
936 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
937 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
943 bool is_union_type (type *uni) {
945 if (uni->type_op == type_union) return 1; else return 0;
948 /*******************************************************************/
950 /*******************************************************************/
953 /* create a new type array -- set dimension sizes independently */
954 INLINE type *new_type_array (ident *name, int n_dimensions,
955 type *element_type) {
958 assert(!is_method_type(element_type));
959 res = new_type(type_array, NULL, name);
960 res->attr.aa.n_dimensions = n_dimensions;
961 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
962 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
963 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
965 for (i = 0; i < n_dimensions; i++) {
966 res->attr.aa.lower_bound[i] = NULL;
967 res->attr.aa.upper_bound[i] = NULL;
968 res->attr.aa.order[i] = i;
970 res->attr.aa.element_type = element_type;
971 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
974 type *new_d_type_array (ident *name, int n_dimensions,
975 type *element_type, dbg_info* db) {
976 type *res = new_type_array (name, n_dimensions, element_type);
977 set_type_dbg_info(res, db);
981 INLINE void free_array_attrs (type *array) {
982 assert(array && (array->type_op == type_array));
983 free(array->attr.aa.lower_bound);
984 free(array->attr.aa.upper_bound);
987 /* manipulate private fields of array type */
988 int get_array_n_dimensions (type *array) {
989 assert(array && (array->type_op == type_array));
990 return array->attr.aa.n_dimensions;
994 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
995 ir_node * upper_bound) {
996 assert(array && (array->type_op == type_array));
997 array->attr.aa.lower_bound[dimension] = lower_bound;
998 array->attr.aa.upper_bound[dimension] = upper_bound;
1001 set_array_bounds_int (type *array, int dimension, int lower_bound,
1003 ir_graph *rem = current_ir_graph;
1004 current_ir_graph = get_const_code_irg();
1005 set_array_bounds (array, dimension,
1006 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)),
1007 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1008 current_ir_graph = rem;
1011 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1012 assert(array && (array->type_op == type_array));
1013 array->attr.aa.lower_bound[dimension] = lower_bound;
1015 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1016 ir_graph *rem = current_ir_graph;
1017 current_ir_graph = get_const_code_irg();
1018 set_array_lower_bound (array, dimension,
1019 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)));
1020 current_ir_graph = rem;
1023 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1024 assert(array && (array->type_op == type_array));
1025 array->attr.aa.upper_bound[dimension] = upper_bound;
1027 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1028 ir_graph *rem = current_ir_graph;
1029 current_ir_graph = get_const_code_irg();
1030 set_array_upper_bound (array, dimension,
1031 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1032 current_ir_graph = rem;
1034 ir_node * get_array_lower_bound (type *array, int dimension) {
1035 assert(array && (array->type_op == type_array));
1036 return array->attr.aa.lower_bound[dimension];
1038 ir_node * get_array_upper_bound (type *array, int dimension) {
1039 assert(array && (array->type_op == type_array));
1040 return array->attr.aa.upper_bound[dimension];
1043 void set_array_order (type *array, int dimension, int order) {
1044 assert(array && (array->type_op == type_array));
1045 array->attr.aa.order[dimension] = order;
1047 int get_array_order (type *array, int dimension) {
1048 assert(array && (array->type_op == type_array));
1049 return array->attr.aa.order[dimension];
1052 void set_array_element_type (type *array, type *tp) {
1053 assert(array && (array->type_op == type_array));
1054 assert(!is_method_type(tp));
1055 array->attr.aa.element_type = tp;
1057 type *get_array_element_type (type *array) {
1058 assert(array && (array->type_op == type_array));
1059 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1062 void set_array_element_entity (type *array, entity *ent) {
1063 assert(array && (array->type_op == type_array));
1064 assert((get_entity_type(ent)->type_op != type_method));
1065 array->attr.aa.element_ent = ent;
1066 array->attr.aa.element_type = get_entity_type(ent);
1068 entity *get_array_element_entity (type *array) {
1069 assert(array && (array->type_op == type_array));
1070 return array->attr.aa.element_ent;
1074 bool is_array_type (type *array) {
1076 if (array->type_op == type_array) return 1; else return 0;
1079 /*******************************************************************/
1080 /** TYPE_ENUMERATION **/
1081 /*******************************************************************/
1083 /* create a new type enumeration -- set the enumerators independently */
1084 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1087 res = new_type(type_enumeration, NULL, name);
1088 res->attr.ea.n_enums = n_enums;
1089 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1090 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1091 for (i = 0; i < n_enums; i++) {
1092 res->attr.ea.enumer[i] = NULL;
1093 res->attr.ea.enum_nameid = NULL;
1097 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1098 type *res = new_type_enumeration (name, n_enums);
1099 set_type_dbg_info(res, db);
1103 INLINE void free_enumeration_attrs(type *enumeration) {
1104 assert(enumeration && (enumeration->type_op == type_enumeration));
1105 free(enumeration->attr.ea.enumer);
1106 free(enumeration->attr.ea.enum_nameid);
1109 /* manipulate fields of enumeration type. */
1110 int get_enumeration_n_enums (type *enumeration) {
1111 assert(enumeration && (enumeration->type_op == type_enumeration));
1112 return enumeration->attr.ea.n_enums;
1114 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1115 assert(enumeration && (enumeration->type_op == type_enumeration));
1116 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1117 enumeration->attr.ea.enumer[pos] = con;
1119 tarval *get_enumeration_enum (type *enumeration, int pos) {
1120 assert(enumeration && (enumeration->type_op == type_enumeration));
1121 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1122 return enumeration->attr.ea.enumer[pos];
1124 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1125 assert(enumeration && (enumeration->type_op == type_enumeration));
1126 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1127 enumeration->attr.ea.enum_nameid[pos] = id;
1129 ident *get_enumeration_nameid (type *enumeration, int pos) {
1130 assert(enumeration && (enumeration->type_op == type_enumeration));
1131 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1132 return enumeration->attr.ea.enum_nameid[pos];
1134 const char *get_enumeration_name(type *enumeration, int pos) {
1135 assert(enumeration && (enumeration->type_op == type_enumeration));
1136 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1137 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1141 bool is_enumeration_type (type *enumeration) {
1142 assert(enumeration);
1143 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1146 /*******************************************************************/
1147 /** TYPE_POINTER **/
1148 /*******************************************************************/
1150 /* Create a new type pointer */
1151 INLINE type *new_type_pointer (ident *name, type *points_to) {
1153 res = new_type(type_pointer, mode_P, name);
1154 res->attr.pa.points_to = points_to;
1155 res->size = get_mode_size(res->mode);
1156 res->state = layout_fixed;
1159 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1160 type *res = new_type_pointer (name, points_to);
1161 set_type_dbg_info(res, db);
1164 INLINE void free_pointer_attrs (type *pointer) {
1165 assert(pointer && (pointer->type_op == type_pointer));
1167 /* manipulate fields of type_pointer */
1168 void set_pointer_points_to_type (type *pointer, type *tp) {
1169 assert(pointer && (pointer->type_op == type_pointer));
1170 pointer->attr.pa.points_to = tp;
1172 type *get_pointer_points_to_type (type *pointer) {
1173 assert(pointer && (pointer->type_op == type_pointer));
1174 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1178 bool is_pointer_type (type *pointer) {
1180 if (pointer->type_op == type_pointer) return 1; else return 0;
1184 /*******************************************************************/
1185 /** TYPE_PRIMITIVE **/
1186 /*******************************************************************/
1188 /* create a new type primitive */
1189 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1191 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1192 res = new_type(type_primitive, mode, name);
1193 res->size = get_mode_size(mode);
1194 res->state = layout_fixed;
1197 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1198 type *res = new_type_primitive (name, mode);
1199 set_type_dbg_info(res, db);
1202 INLINE void free_primitive_attrs (type *primitive) {
1203 assert(primitive && (primitive->type_op == type_primitive));
1207 bool is_primitive_type (type *primitive) {
1208 assert(primitive && primitive->kind == k_type);
1209 if (primitive->type_op == type_primitive) return 1; else return 0;
1212 /*******************************************************************/
1213 /** common functionality **/
1214 /*******************************************************************/
1217 INLINE int is_atomic_type(type *tp) {
1218 assert(tp && tp->kind == k_type);
1219 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1220 is_enumeration_type(tp));
1222 INLINE int is_compound_type(type *tp) {
1223 assert(tp && tp->kind == k_type);
1224 return (is_class_type(tp) || is_struct_type(tp) ||
1225 is_array_type(tp) || is_union_type(tp));