4 * file type.c - implementation of the datastructure to hold
7 * (C) 2001 by Universitaet Karlsruhe
9 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
12 * This module supplies a datastructure to represent all types
13 * known in the compiled program. This includes types specified
14 * in the program as well as types defined by the language. In the
15 * view of the intermediate representation there is no difference
16 * between these types.
18 * There exist several kinds of types, arranged by the structure of
19 * the type. A type is described by a set of attributes. Some of
20 * these attributes are common to all types, others depend on the
23 * Types are different from the modes defined in irmode: Types are
24 * on the level of the programming language, modes at the level of
25 * the target processor.
38 # include "typegmod_t.h"
45 /*******************************************************************/
47 /*******************************************************************/
49 unsigned long type_visited;
51 void free_type(type *tp) {
52 /* Remove from list of all types */
54 /* Free the attributes of the type. */
56 /* Free entities automatically allocated with the type */
57 if (is_array_type(tp))
58 free_entity(get_array_element_entity(tp));
59 /* And now the type itself... */
64 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
68 assert(type_op != type_id);
70 node_size = offsetof (type, attr) + type_op->attr_size;
71 res = (type *) xmalloc (node_size);
72 add_irp_type(res); /* Remember the new type global. */
75 res->type_op = type_op;
78 res->state = layout_undefined;
86 void free_type_attrs(type *tp) {
87 switch(get_type_tpop_code(tp)) {
88 case tpo_class: { free_class_attrs(tp); } break;
89 case tpo_struct: { free_struct_attrs(tp); } break;
90 case tpo_method: { free_method_attrs(tp); } break;
91 case tpo_union: { free_union_attrs(tp); } break;
92 case tpo_array: { free_array_attrs(tp); } break;
93 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
94 case tpo_pointer: { free_pointer_attrs(tp); } break;
95 case tpo_primitive: { free_primitive_attrs(tp); } break;
100 /* set/get the link field */
101 void *get_type_link(type *tp)
103 assert(tp && tp->kind == k_type);
107 void set_type_link(type *tp, void *l)
109 assert(tp && tp->kind == k_type);
113 tp_op* get_type_tpop(type *tp) {
114 assert(tp && tp->kind == k_type);
118 ident* get_type_tpop_nameid(type *tp) {
119 assert(tp && tp->kind == k_type);
120 return tp->type_op->name;
123 const char* get_type_tpop_name(type *tp) {
124 assert(tp && tp->kind == k_type);
125 return id_to_str(tp->type_op->name);
128 tp_opcode get_type_tpop_code(type *tp) {
129 assert(tp && tp->kind == k_type);
130 return tp->type_op->code;
133 ir_mode* get_type_mode(type *tp) {
134 assert(tp && tp->kind == k_type);
138 void set_type_mode(type *tp, ir_mode* m) {
139 assert(tp && tp->kind == k_type);
141 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
142 /* Modes of primitives must be data */
143 ((tp->type_op != type_enumeration) || mode_is_int(m)));
144 /* Modes of enumerations must be integers */
146 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
147 /* For pointer, primitive and enumeration size depends on the mode. */
148 tp->size = get_mode_size(m);
153 ident* get_type_ident(type *tp) {
154 assert(tp && tp->kind == k_type);
158 void set_type_ident(type *tp, ident* id) {
159 assert(tp && tp->kind == k_type);
163 const char* get_type_name(type *tp) {
164 assert(tp && tp->kind == k_type);
165 return (id_to_str(tp->name));
168 int get_type_size(type *tp) {
169 assert(tp && tp->kind == k_type);
174 set_type_size(type *tp, int size) {
175 assert(tp && tp->kind == k_type);
176 /* For pointer enumeration and primitive size depends on the mode.
177 Methods don't have a size. */
178 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
179 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
184 get_type_state(type *tp) {
185 assert(tp && tp->kind == k_type);
190 set_type_state(type *tp, type_state state) {
191 assert(tp && tp->kind == k_type);
193 if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
194 (tp->type_op == type_method))
197 /* Just a correctness check: */
198 if (state == layout_fixed) {
200 switch (get_type_tpop_code(tp)) {
203 assert(get_type_size(tp) > -1);
204 if (tp != get_glob_type())
205 for (i = 0; i < get_class_n_members(tp); i++) {
206 assert(get_entity_offset(get_class_member(tp, i)) > -1);
207 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
208 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
209 /* @@@ lowerfirm geht nicht durch */
214 /* assert(get_type_size(tp) > -1); @@@ lowerfirm geht nicht durch */
215 for (i = 0; i < get_struct_n_members(tp); i++) {
216 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
217 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
226 Assure that only innermost dimension is dynamic? */
228 case tpo_enumeration:
230 assert(get_type_mode != NULL);
231 for (i = 0; i < get_enumeration_n_enums(tp); i++)
232 assert(get_enumeration_enum(tp, i) != NULL);
240 unsigned long get_type_visited(type *tp) {
241 assert(tp && tp->kind == k_type);
245 void set_type_visited(type *tp, unsigned long num) {
246 assert(tp && tp->kind == k_type);
249 /* Sets visited field in type to type_visited. */
250 void mark_type_visited(type *tp) {
251 assert(tp && tp->kind == k_type);
252 assert(tp->visit < type_visited);
253 tp->visit = type_visited;
256 int is_type (void *thing) {
258 if (get_kind(thing) == k_type)
265 bool equal_type(type *typ1, type *typ2) {
270 if (typ1 == typ2) return true;
272 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
273 (get_type_name(typ1) != get_type_name(typ2)) ||
274 (get_type_mode(typ1) != get_type_mode(typ2)) ||
275 (get_type_state(typ1) != get_type_state(typ2)))
277 if ((get_type_state(typ1) == layout_fixed) &&
278 (get_type_size(typ1) != get_type_size(typ2)))
281 switch(get_type_tpop_code(typ1)) {
283 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
284 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
285 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
286 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
287 /** Compare the members **/
288 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
289 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
290 /* First sort the members of typ2 */
291 for (i = 0; i < get_class_n_members(typ1); i++) {
292 entity *e1 = get_class_member(typ1, i);
293 for (j = 0; j < get_class_n_members(typ2); j++) {
294 entity *e2 = get_class_member(typ2, j);
295 if (get_entity_name(e1) == get_entity_name(e2))
299 for (i = 0; i < get_class_n_members(typ1); i++) {
300 if (!m[i] || /* Found no counterpart */
301 !equal_entity(get_class_member(typ1, i), m[i]))
304 /** Compare the supertypes **/
305 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
306 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
307 /* First sort the supertypes of typ2 */
308 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
309 type *t1 = get_class_supertype(typ1, i);
310 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
311 type *t2 = get_class_supertype(typ2, j);
312 if (get_type_name(t2) == get_type_name(t1))
316 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
317 if (!t[i] || /* Found no counterpart */
318 get_class_supertype(typ1, i) != t[i])
323 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
324 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
325 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
326 /* First sort the members of lt */
327 for (i = 0; i < get_struct_n_members(typ1); i++) {
328 entity *e1 = get_struct_member(typ1, i);
329 for (j = 0; j < get_struct_n_members(typ2); j++) {
330 entity *e2 = get_struct_member(typ2, j);
331 if (get_entity_name(e1) == get_entity_name(e2))
335 for (i = 0; i < get_struct_n_members(typ1); i++) {
336 if (!m[i] || /* Found no counterpart */
337 !equal_entity(get_struct_member(typ1, i), m[i]))
342 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
343 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
344 for (i = 0; i < get_method_n_params(typ1); i++) {
345 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
348 for (i = 0; i < get_method_n_ress(typ1); i++) {
349 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
354 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
355 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
356 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
357 /* First sort the members of lt */
358 for (i = 0; i < get_union_n_members(typ1); i++) {
359 entity *e1 = get_union_member(typ1, i);
360 for (j = 0; j < get_union_n_members(typ2); j++) {
361 entity *e2 = get_union_member(typ2, j);
362 if (get_entity_name(e1) == get_entity_name(e2))
366 for (i = 0; i < get_union_n_members(typ1); i++) {
367 if (!m[i] || /* Found no counterpart */
368 !equal_entity(get_union_member(typ1, i), m[i]))
373 type *set, *let; /* small/large elt. type */
374 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
376 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
378 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
379 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
380 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
382 if (get_array_order(typ1, i) != get_array_order(typ2, i))
383 assert(0 && "type compare with different dimension orders not implemented");
386 case tpo_enumeration: {
387 assert(0 && "enumerations not implemented");
390 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
393 case tpo_primitive: {
400 bool smaller_type (type *st, type *lt) {
404 if (st == lt) return true;
406 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
409 switch(get_type_tpop_code(st)) {
411 return is_subclass_of(st, lt);
414 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
415 m = alloca(sizeof(entity *) * get_struct_n_members(st));
416 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
417 /* First sort the members of lt */
418 for (i = 0; i < get_struct_n_members(st); i++) {
419 entity *se = get_struct_member(st, i);
420 for (j = 0; j < get_struct_n_members(lt); j++) {
421 entity *le = get_struct_member(lt, j);
422 if (get_entity_name(le) == get_entity_name(se))
426 for (i = 0; i < get_struct_n_members(st); i++) {
427 if (!m[i] || /* Found no counterpart */
428 !smaller_type(get_entity_type(get_struct_member(st, i)),
429 get_entity_type(m[i])))
434 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
435 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
436 for (i = 0; i < get_method_n_params(st); i++) {
437 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
440 for (i = 0; i < get_method_n_ress(st); i++) {
441 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
446 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
447 m = alloca(sizeof(entity *) * get_union_n_members(st));
448 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
449 /* First sort the members of lt */
450 for (i = 0; i < get_union_n_members(st); i++) {
451 entity *se = get_union_member(st, i);
452 for (j = 0; j < get_union_n_members(lt); j++) {
453 entity *le = get_union_member(lt, j);
454 if (get_entity_name(le) == get_entity_name(se))
458 for (i = 0; i < get_union_n_members(st); i++) {
459 if (!m[i] || /* Found no counterpart */
460 !smaller_type(get_entity_type(get_union_member(st, i)),
461 get_entity_type(m[i])))
466 type *set, *let; /* small/large elt. type */
467 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
469 set = get_array_element_type(st);
470 let = get_array_element_type(lt);
472 /* If the elt types are different, set must be convertible
473 to let, and they must have the same size so that address
474 computations work out. To have a size the layout must
476 if ((get_type_state(set) != layout_fixed) ||
477 (get_type_state(let) != layout_fixed))
479 if (!smaller_type(set, let) ||
480 get_type_size(set) != get_type_size(let))
483 for(i = 0; i < get_array_n_dimensions(st); i++) {
484 if (get_array_lower_bound(lt, i))
485 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
487 if (get_array_upper_bound(lt, i))
488 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
492 case tpo_enumeration: {
493 assert(0 && "enumerations not implemented");
496 if (!smaller_type(get_pointer_points_to_type(st),
497 get_pointer_points_to_type(lt)))
500 case tpo_primitive: {
501 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
509 /*******************************************************************/
511 /*******************************************************************/
513 /* create a new class type */
514 type *new_type_class (ident *name) {
517 res = new_type(type_class, NULL, name);
519 res->attr.ca.members = NEW_ARR_F (entity *, 1);
520 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
521 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
522 res->attr.ca.peculiarity = existent;
523 res->attr.ca.dfn = 0;
527 INLINE void free_class_attrs(type *clss) {
528 assert(clss && (clss->type_op == type_class));
529 DEL_ARR_F(clss->attr.ca.members);
530 DEL_ARR_F(clss->attr.ca.subtypes);
531 DEL_ARR_F(clss->attr.ca.supertypes);
534 /* manipulate private fields of class type */
535 void add_class_member (type *clss, entity *member) {
536 assert(clss && (clss->type_op == type_class));
537 ARR_APP1 (entity *, clss->attr.ca.members, member);
539 int get_class_n_members (type *clss) {
540 assert(clss && (clss->type_op == type_class));
541 return (ARR_LEN (clss->attr.ca.members))-1;
543 entity *get_class_member (type *clss, int pos) {
544 assert(clss && (clss->type_op == type_class));
545 assert(pos >= 0 && pos < get_class_n_members(clss));
546 return clss->attr.ca.members[pos+1];
548 void set_class_member (type *clss, entity *member, int pos) {
549 assert(clss && (clss->type_op == type_class));
550 assert(pos >= 0 && pos < get_class_n_members(clss));
551 clss->attr.ca.members[pos+1] = member;
553 void set_class_members (type *clss, entity **members, int arity) {
555 assert(clss && (clss->type_op == type_class));
556 DEL_ARR_F(clss->attr.ca.members);
557 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
558 for (i = 0; i < arity; i++) {
559 set_entity_owner(members[i], clss);
560 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
563 void remove_class_member(type *clss, entity *member) {
565 assert(clss && (clss->type_op == type_class));
566 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++)
567 if (clss->attr.ca.members[i] == member) {
568 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
569 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
570 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
575 void add_class_subtype (type *clss, type *subtype) {
577 assert(clss && (clss->type_op == type_class));
578 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
579 for (i = 0; i < get_class_n_supertypes(subtype); i++)
580 if (get_class_supertype(subtype, i) == clss)
581 /* Class already registered */
583 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
585 int get_class_n_subtypes (type *clss) {
586 assert(clss && (clss->type_op == type_class));
587 return (ARR_LEN (clss->attr.ca.subtypes))-1;
589 type *get_class_subtype (type *clss, int pos) {
590 assert(clss && (clss->type_op == type_class));
591 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
592 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
594 void set_class_subtype (type *clss, type *subtype, int pos) {
595 assert(clss && (clss->type_op == type_class));
596 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
597 clss->attr.ca.subtypes[pos+1] = subtype;
599 void remove_class_subtype(type *clss, type *subtype) {
601 assert(clss && (clss->type_op == type_class));
602 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
603 if (clss->attr.ca.subtypes[i] == subtype) {
604 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
605 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
606 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
611 void add_class_supertype (type *clss, type *supertype) {
613 assert(clss && (clss->type_op == type_class));
614 assert(supertype && (supertype -> type_op == type_class));
615 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
616 for (i = 0; i < get_class_n_subtypes(supertype); i++)
617 if (get_class_subtype(supertype, i) == clss)
618 /* Class already registered */
620 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
622 int get_class_n_supertypes (type *clss) {
623 assert(clss && (clss->type_op == type_class));
624 return (ARR_LEN (clss->attr.ca.supertypes))-1;
626 type *get_class_supertype (type *clss, int pos) {
627 assert(clss && (clss->type_op == type_class));
628 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
629 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
631 void set_class_supertype (type *clss, type *supertype, int pos) {
632 assert(clss && (clss->type_op == type_class));
633 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
634 clss->attr.ca.supertypes[pos+1] = supertype;
636 void remove_class_supertype(type *clss, type *supertype) {
638 assert(clss && (clss->type_op == type_class));
639 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
640 if (clss->attr.ca.supertypes[i] == supertype) {
641 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
642 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
643 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
648 INLINE peculiarity get_class_peculiarity (type *clss) {
649 assert(clss && (clss->type_op == type_class));
650 return clss->attr.ca.peculiarity;
652 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
653 assert(clss && (clss->type_op == type_class));
654 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
655 clss->attr.ca.peculiarity = pec;
658 void set_class_dfn (type *clss, int dfn)
660 clss->attr.ca.dfn = dfn;
663 int get_class_dfn (type *clss)
665 return (clss->attr.ca.dfn);
669 bool is_class_type(type *clss) {
671 if (clss->type_op == type_class) return 1; else return 0;
674 bool is_subclass_of(type *low, type *high) {
676 assert(is_class_type(low) && is_class_type(high));
677 if (low == high) return true;
678 /* depth first search from high downwards. */
679 for (i = 0; i < get_class_n_subtypes(high); i++) {
680 if (low == get_class_subtype(high, i))
682 if (is_subclass_of(low, get_class_subtype(high, i)))
688 /*******************************************************************/
690 /*******************************************************************/
692 /* create a new type struct */
693 type *new_type_struct (ident *name) {
695 res = new_type(type_struct, NULL, name);
696 res->attr.sa.members = NEW_ARR_F (entity *, 1);
699 INLINE void free_struct_attrs (type *strct) {
700 assert(strct && (strct->type_op == type_struct));
701 DEL_ARR_F(strct->attr.sa.members);
704 /* manipulate private fields of struct */
705 int get_struct_n_members (type *strct) {
706 assert(strct && (strct->type_op == type_struct));
707 return (ARR_LEN (strct->attr.sa.members))-1;
709 void add_struct_member (type *strct, entity *member) {
710 assert(strct && (strct->type_op == type_struct));
711 assert(get_type_tpop(get_entity_type(member)) != type_method);
712 /* @@@ lowerfirm geht nicht durch */
713 ARR_APP1 (entity *, strct->attr.sa.members, member);
715 entity *get_struct_member (type *strct, int pos) {
716 assert(strct && (strct->type_op == type_struct));
717 assert(pos >= 0 && pos < get_struct_n_members(strct));
718 return strct->attr.sa.members[pos+1];
720 void set_struct_member (type *strct, int pos, entity *member) {
721 assert(strct && (strct->type_op == type_struct));
722 assert(pos >= 0 && pos < get_struct_n_members(strct));
723 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
724 strct->attr.sa.members[pos+1] = member;
726 void remove_struct_member(type *strct, entity *member) {
728 assert(strct && (strct->type_op == type_struct));
729 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
730 if (strct->attr.sa.members[i] == member) {
731 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
732 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
733 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
739 bool is_struct_type(type *strct) {
741 if (strct->type_op == type_struct) return 1; else return 0;
744 /*******************************************************************/
746 /*******************************************************************/
748 /* Create a new method type.
749 N_param is the number of parameters, n_res the number of results. */
750 type *new_type_method (ident *name, int n_param, int n_res) {
752 res = new_type(type_method, mode_p, name);
753 res->state = layout_fixed;
754 res->size = get_mode_size(mode_p);
755 res->attr.ma.n_params = n_param;
756 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
757 res->attr.ma.n_res = n_res;
758 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
761 INLINE void free_method_attrs(type *method) {
762 assert(method && (method->type_op == type_method));
763 free(method->attr.ma.param_type);
764 free(method->attr.ma.res_type);
766 /* manipulate private fields of method. */
767 int get_method_n_params (type *method) {
768 assert(method && (method->type_op == type_method));
769 return method->attr.ma.n_params;
771 type *get_method_param_type(type *method, int pos) {
772 assert(method && (method->type_op == type_method));
773 assert(pos >= 0 && pos < get_method_n_params(method));
774 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
776 void set_method_param_type(type *method, int pos, type* type) {
777 assert(method && (method->type_op == type_method));
778 assert(pos >= 0 && pos < get_method_n_params(method));
779 method->attr.ma.param_type[pos] = type;
782 int get_method_n_ress (type *method) {
783 assert(method && (method->type_op == type_method));
784 return method->attr.ma.n_res;
786 type *get_method_res_type(type *method, int pos) {
787 assert(method && (method->type_op == type_method));
788 assert(pos >= 0 && pos < get_method_n_ress(method));
789 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
791 void set_method_res_type(type *method, int pos, type* type) {
792 assert(method && (method->type_op == type_method));
793 assert(pos >= 0 && pos < get_method_n_ress(method));
794 method->attr.ma.res_type[pos] = type;
798 bool is_method_type (type *method) {
800 if (method->type_op == type_method) return 1; else return 0;
804 /*******************************************************************/
806 /*******************************************************************/
808 /* create a new type uni */
809 type *new_type_uni (ident *name) {
811 res = new_type(type_union, NULL, name);
812 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
813 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
814 res->attr.ua.members = NEW_ARR_F (entity *, 1);
817 INLINE void free_union_attrs (type *uni) {
818 assert(uni && (uni->type_op == type_union));
819 DEL_ARR_F(uni->attr.ua.members);
821 /* manipulate private fields of union */
823 int get_union_n_types (type *uni) {
824 assert(uni && (uni->type_op == type_union));
825 return uni->attr.ua.n_types;
827 type *get_union_unioned_type (type *uni, int pos) {
828 assert(uni && (uni->type_op == type_union));
829 assert(pos >= 0 && pos < get_union_n_types(uni));
830 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
832 void set_union_unioned_type (type *uni, int pos, type *type) {
833 assert(uni && (uni->type_op == type_union));
834 assert(pos >= 0 && pos < get_union_n_types(uni));
835 uni->attr.ua.unioned_type[pos] = type;
837 ident *get_union_delim_nameid (type *uni, int pos) {
838 assert(uni && (uni->type_op == type_union));
839 assert(pos >= 0 && pos < get_union_n_types(uni));
840 return uni->attr.ua.delim_names[pos];
842 const char *get_union_delim_name (type *uni, int pos) {
843 assert(uni && (uni->type_op == type_union));
844 assert(pos >= 0 && pos < get_union_n_types(uni));
845 return id_to_str(uni->attr.ua.delim_names[pos]);
847 void set_union_delim_nameid (type *uni, int pos, ident *id) {
848 assert(uni && (uni->type_op == type_union));
849 assert(pos >= 0 && pos < get_union_n_types(uni));
850 uni->attr.ua.delim_names[pos] = id;
853 int get_union_n_members (type *uni) {
854 assert(uni && (uni->type_op == type_union));
855 return (ARR_LEN (uni->attr.ua.members))-1;
857 void add_union_member (type *uni, entity *member) {
858 assert(uni && (uni->type_op == type_union));
859 ARR_APP1 (entity *, uni->attr.ua.members, member);
861 entity *get_union_member (type *uni, int pos) {
862 assert(uni && (uni->type_op == type_union));
863 assert(pos >= 0 && pos < get_union_n_members(uni));
864 return uni->attr.ua.members[pos+1];
866 void set_union_member (type *uni, int pos, entity *member) {
867 assert(uni && (uni->type_op == type_union));
868 assert(pos >= 0 && pos < get_union_n_members(uni));
869 uni->attr.ua.members[pos+1] = member;
871 void remove_union_member(type *uni, entity *member) {
873 assert(uni && (uni->type_op == type_union));
874 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
875 if (uni->attr.ua.members[i] == member) {
876 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
877 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
878 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
884 bool is_union_type (type *uni) {
886 if (uni->type_op == type_union) return 1; else return 0;
889 /*******************************************************************/
891 /*******************************************************************/
894 /* create a new type array -- set dimension sizes independently */
895 type *new_type_array (ident *name, int n_dimensions,
896 type *element_type) {
899 assert(!is_method_type(element_type));
900 res = new_type(type_array, NULL, name);
901 res->attr.aa.n_dimensions = n_dimensions;
902 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
903 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
904 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
906 for (i = 0; i < n_dimensions; i++) {
907 res->attr.aa.lower_bound[i] = NULL;
908 res->attr.aa.upper_bound[i] = NULL;
909 res->attr.aa.order[i] = i;
911 res->attr.aa.element_type = element_type;
912 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
916 INLINE void free_array_attrs (type *array) {
917 assert(array && (array->type_op == type_array));
918 free(array->attr.aa.lower_bound);
919 free(array->attr.aa.upper_bound);
922 /* manipulate private fields of array type */
923 int get_array_n_dimensions (type *array) {
924 assert(array && (array->type_op == type_array));
925 return array->attr.aa.n_dimensions;
929 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
930 ir_node * upper_bound) {
931 assert(array && (array->type_op == type_array));
932 array->attr.aa.lower_bound[dimension] = lower_bound;
933 array->attr.aa.upper_bound[dimension] = upper_bound;
936 set_array_bounds_int (type *array, int dimension, int lower_bound,
938 ir_graph *rem = current_ir_graph;
939 current_ir_graph = get_const_code_irg();
940 set_array_bounds (array, dimension,
941 new_Const(mode_I, tarval_from_long (mode_I, lower_bound)),
942 new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
943 current_ir_graph = rem;
946 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
947 assert(array && (array->type_op == type_array));
948 array->attr.aa.lower_bound[dimension] = lower_bound;
950 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
951 ir_graph *rem = current_ir_graph;
952 current_ir_graph = get_const_code_irg();
953 set_array_lower_bound (array, dimension,
954 new_Const(mode_I, tarval_from_long (mode_I, lower_bound)));
955 current_ir_graph = rem;
958 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
959 assert(array && (array->type_op == type_array));
960 array->attr.aa.upper_bound[dimension] = upper_bound;
962 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
963 ir_graph *rem = current_ir_graph;
964 current_ir_graph = get_const_code_irg();
965 set_array_upper_bound (array, dimension,
966 new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
967 current_ir_graph = rem;
969 ir_node * get_array_lower_bound (type *array, int dimension) {
970 assert(array && (array->type_op == type_array));
971 return array->attr.aa.lower_bound[dimension];
973 ir_node * get_array_upper_bound (type *array, int dimension) {
974 assert(array && (array->type_op == type_array));
975 return array->attr.aa.upper_bound[dimension];
978 void set_array_order (type *array, int dimension, int order) {
979 assert(array && (array->type_op == type_array));
980 array->attr.aa.order[dimension] = order;
982 int get_array_order (type *array, int dimension) {
983 assert(array && (array->type_op == type_array));
984 return array->attr.aa.order[dimension];
987 void set_array_element_type (type *array, type *type) {
988 assert(array && (array->type_op == type_array));
989 assert(!is_method_type(type));
990 array->attr.aa.element_type = type;
992 type *get_array_element_type (type *array) {
993 assert(array && (array->type_op == type_array));
994 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
997 void set_array_element_entity (type *array, entity *ent) {
998 assert(array && (array->type_op == type_array));
999 assert((get_entity_type(ent)->type_op != type_method));
1000 array->attr.aa.element_ent = ent;
1001 array->attr.aa.element_type = get_entity_type(ent);
1003 entity *get_array_element_entity (type *array) {
1004 assert(array && (array->type_op == type_array));
1005 return array->attr.aa.element_ent;
1009 bool is_array_type (type *array) {
1011 if (array->type_op == type_array) return 1; else return 0;
1014 /*******************************************************************/
1015 /** TYPE_ENUMERATION **/
1016 /*******************************************************************/
1018 /* create a new type enumeration -- set the enumerators independently */
1019 type *new_type_enumeration (ident *name, int n_enums) {
1022 res = new_type(type_enumeration, NULL, name);
1023 res->attr.ea.n_enums = n_enums;
1024 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1025 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1026 for (i = 0; i < n_enums; i++) {
1027 res->attr.ea.enumer[i] = NULL;
1028 res->attr.ea.enum_nameid = NULL;
1033 INLINE void free_enumeration_attrs(type *enumeration) {
1034 assert(enumeration && (enumeration->type_op == type_enumeration));
1035 free(enumeration->attr.ea.enumer);
1036 free(enumeration->attr.ea.enum_nameid);
1039 /* manipulate fields of enumeration type. */
1040 int get_enumeration_n_enums (type *enumeration) {
1041 assert(enumeration && (enumeration->type_op == type_enumeration));
1042 return enumeration->attr.ea.n_enums;
1044 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1045 assert(enumeration && (enumeration->type_op == type_enumeration));
1046 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1047 enumeration->attr.ea.enumer[pos] = con;
1049 tarval *get_enumeration_enum (type *enumeration, int pos) {
1050 assert(enumeration && (enumeration->type_op == type_enumeration));
1051 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1052 return enumeration->attr.ea.enumer[pos];
1054 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1055 assert(enumeration && (enumeration->type_op == type_enumeration));
1056 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1057 enumeration->attr.ea.enum_nameid[pos] = id;
1059 ident *get_enumeration_nameid (type *enumeration, int pos) {
1060 assert(enumeration && (enumeration->type_op == type_enumeration));
1061 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1062 return enumeration->attr.ea.enum_nameid[pos];
1064 const char *get_enumeration_name(type *enumeration, int pos) {
1065 assert(enumeration && (enumeration->type_op == type_enumeration));
1066 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1067 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1071 bool is_enumeration_type (type *enumeration) {
1072 assert(enumeration);
1073 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1076 /*******************************************************************/
1077 /** TYPE_POINTER **/
1078 /*******************************************************************/
1080 /* Create a new type pointer */
1081 type *new_type_pointer (ident *name, type *points_to) {
1083 res = new_type(type_pointer, mode_p, name);
1084 res->attr.pa.points_to = points_to;
1085 res->size = get_mode_size(res->mode);
1086 res->state = layout_fixed;
1089 INLINE void free_pointer_attrs (type *pointer) {
1090 assert(pointer && (pointer->type_op == type_pointer));
1092 /* manipulate fields of type_pointer */
1093 void set_pointer_points_to_type (type *pointer, type *type) {
1094 assert(pointer && (pointer->type_op == type_pointer));
1095 pointer->attr.pa.points_to = type;
1097 type *get_pointer_points_to_type (type *pointer) {
1098 assert(pointer && (pointer->type_op == type_pointer));
1099 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1103 bool is_pointer_type (type *pointer) {
1105 if (pointer->type_op == type_pointer) return 1; else return 0;
1109 /*******************************************************************/
1110 /** TYPE_PRIMITIVE **/
1111 /*******************************************************************/
1113 /* create a new type primitive */
1114 type *new_type_primitive (ident *name, ir_mode *mode) {
1116 /* @@@ assert( mode_is_data(mode) && (!mode == mode_p)); */
1117 res = new_type(type_primitive, mode, name);
1118 res->size = get_mode_size(mode);
1119 res->state = layout_fixed;
1122 INLINE void free_primitive_attrs (type *primitive) {
1123 assert(primitive && (primitive->type_op == type_primitive));
1127 bool is_primitive_type (type *primitive) {
1128 assert(primitive && primitive->kind == k_type);
1129 if (primitive->type_op == type_primitive) return 1; else return 0;
1132 /*******************************************************************/
1133 /** common functionality **/
1134 /*******************************************************************/
1137 INLINE int is_atomic_type(type *tp) {
1138 assert(tp && tp->kind == k_type);
1139 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1140 is_enumeration_type(tp));
1142 INLINE int is_compound_type(type *tp) {
1143 assert(tp && tp->kind == k_type);
1144 return (is_class_type(tp) || is_struct_type(tp) ||
1145 is_array_type(tp) || is_union_type(tp));