3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * file type.c - implementation of the datastructure to hold
17 * (C) 2001 by Universitaet Karlsruhe
20 * This module supplies a datastructure to represent all types
21 * known in the compiled program. This includes types specified
22 * in the program as well as types defined by the language. In the
23 * view of the intermediate representation there is no difference
24 * between these types.
26 * There exist several kinds of types, arranged by the structure of
27 * the type. A type is described by a set of attributes. Some of
28 * these attributes are common to all types, others depend on the
31 * Types are different from the modes defined in irmode: Types are
32 * on the level of the programming language, modes at the level of
33 * the target processor.
35 * @see type_t.h type tpop
48 # include "irprog_t.h"
51 # include "typegmod.h"
57 /*******************************************************************/
59 /*******************************************************************/
61 type *none_type; type *get_none_type(void) { return none_type; }
62 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
66 /** Returns a new, unique number to number nodes or the like. */
67 int get_irp_new_node_nr(void);
70 /* Suffixes added to types used for pass-by-value representations. */
71 static ident *value_params_suffix = NULL;
72 static ident *value_ress_suffix = NULL;
74 void init_type(void) {
75 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
76 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
78 /* construct none and unknown type. */
79 none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
80 set_type_size_bits(none_type, 0);
81 set_type_state (none_type, layout_fixed);
82 remove_irp_type(none_type);
83 unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
84 set_type_size_bits(unknown_type, 0);
85 set_type_state (unknown_type, layout_fixed);
86 remove_irp_type(unknown_type);
89 unsigned long type_visited;
91 void (set_master_type_visited)(unsigned long val) { __set_master_type_visited(val); }
92 unsigned long (get_master_type_visited)(void) { return __get_master_type_visited(); }
93 void (inc_master_type_visited)(void) { __inc_master_type_visited(); }
97 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
101 assert(type_op != type_id);
102 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
104 node_size = offsetof(type, attr) + type_op->attr_size;
105 res = (type *) xmalloc (node_size);
106 memset((void *)res, 0, node_size);
107 add_irp_type(res); /* Remember the new type global. */
110 res->type_op = type_op;
113 res->state = layout_undefined;
119 res->nr = get_irp_new_node_nr();
125 void free_type(type *tp) {
126 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
128 /* Remove from list of all types */
130 /* Free the attributes of the type. */
132 /* Free entities automatically allocated with the type */
133 if (is_array_type(tp))
134 free_entity(get_array_element_entity(tp));
135 /* And now the type itself... */
140 void free_type_entities(type *tp) {
141 switch(get_type_tpop_code(tp)) {
142 case tpo_class: { free_class_entities(tp); } break;
143 case tpo_struct: { free_struct_entities(tp); } break;
144 case tpo_method: { free_method_entities(tp); } break;
145 case tpo_union: { free_union_entities(tp); } break;
146 case tpo_array: { free_array_entities(tp); } break;
147 case tpo_enumeration: { free_enumeration_entities(tp); } break;
148 case tpo_pointer: { free_pointer_entities(tp); } break;
149 case tpo_primitive: { free_primitive_entities(tp); } break;
154 void free_type_attrs(type *tp) {
155 switch(get_type_tpop_code(tp)) {
156 case tpo_class: { free_class_attrs(tp); } break;
157 case tpo_struct: { free_struct_attrs(tp); } break;
158 case tpo_method: { free_method_attrs(tp); } break;
159 case tpo_union: { free_union_attrs(tp); } break;
160 case tpo_array: { free_array_attrs(tp); } break;
161 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
162 case tpo_pointer: { free_pointer_attrs(tp); } break;
163 case tpo_primitive: { free_primitive_attrs(tp); } break;
168 /* set/get the link field */
169 void *(get_type_link)(const type *tp)
171 return __get_type_link(tp);
174 void (set_type_link)(type *tp, void *l)
176 __set_type_link(tp, l);
179 tp_op *(get_type_tpop)(const type *tp) {
180 return __get_type_tpop(tp);
183 ident *(get_type_tpop_nameid)(const type *tp) {
184 return __get_type_tpop_nameid(tp);
187 const char* get_type_tpop_name(const type *tp) {
188 assert(tp && tp->kind == k_type);
189 return get_id_str(tp->type_op->name);
192 tp_opcode (get_type_tpop_code)(const type *tp) {
193 return __get_type_tpop_code(tp);
196 ir_mode *(get_type_mode)(const type *tp) {
197 return __get_type_mode(tp);
200 void set_type_mode(type *tp, ir_mode* m) {
201 assert(tp && tp->kind == k_type);
203 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
204 /* Modes of primitives must be data */
205 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
206 /* Modes of enumerations must be integers */
207 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
208 /* Modes of pointers must be references. */
210 switch (get_type_tpop_code(tp)) {
212 /* For primitive size depends on the mode. */
213 tp->size = get_mode_size_bits(m);
216 case tpo_enumeration:
218 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
219 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
220 tp->size = get_mode_size_bits(m);
225 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
226 assert(get_type_state(tp) == layout_fixed &&
227 tp->size == get_mode_size_bits(m) &&
228 "mode don't match struct/class layout");
232 assert(0 && "setting a mode is NOT allowed for this type");
236 ident *(get_type_ident)(const type *tp) {
237 return __get_type_ident(tp);
240 void (set_type_ident)(type *tp, ident* id) {
241 __set_type_ident(tp, id);
244 /* Outputs a unique number for this node */
245 long (get_type_nr)(const type *tp) {
246 return __get_type_nr(tp);
249 const char* get_type_name(const type *tp) {
250 assert(tp && tp->kind == k_type);
251 return (get_id_str(tp->name));
254 int (get_type_size_bytes)(const type *tp) {
255 return __get_type_size_bytes(tp);
258 int (get_type_size_bits)(const type *tp) {
259 return __get_type_size_bits(tp);
263 set_type_size_bits(type *tp, int size) {
264 assert(tp && tp->kind == k_type);
265 /* For pointer enumeration and primitive size depends on the mode.
266 Methods don't have a size. */
267 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
268 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
269 if (tp->type_op == type_primitive)
272 /* argh: we must allow to set negative values as "invalid size" */
273 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
274 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
280 set_type_size_bytes(type *tp, int size) {
281 set_type_size_bits(tp, 8*size);
284 int get_type_alignment_bytes(type *tp) {
285 int align = get_type_alignment_bits(tp);
287 return align < 0 ? align : (align + 7) >> 3;
290 int get_type_alignment_bits(type *tp) {
296 /* alignment NOT set calculate it "on demand" */
298 align = get_mode_size_bits(tp->mode);
299 else if (is_array_type(tp))
300 align = get_type_alignment_bits(get_array_element_type(tp));
301 else if (is_compound_type(tp)) {
302 int i, n = get_compound_n_members(tp);
305 for (i = 0; i < n; ++i) {
306 type *t = get_entity_type(get_compound_member(tp, i));
307 int a = get_type_alignment_bits(t);
313 else if (is_method_type(tp))
323 set_type_alignment_bits(type *tp, int align) {
324 assert(tp && tp->kind == k_type);
325 /* Methods don't have an alignment. */
326 if (tp->type_op != type_method) {
332 set_type_alignment_bytes(type *tp, int align) {
333 set_type_size_bits(tp, 8*align);
336 type_state (get_type_state)(const type *tp) {
337 return __get_type_state(tp);
341 set_type_state(type *tp, type_state state) {
342 assert(tp && tp->kind == k_type);
344 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
345 (tp->type_op == type_method))
348 /* Just a correctness check: */
349 if (state == layout_fixed) {
351 switch (get_type_tpop_code(tp)) {
354 assert(get_type_size_bits(tp) > -1);
355 if (tp != get_glob_type()) {
356 int n_mem = get_class_n_members(tp);
357 for (i = 0; i < n_mem; i++) {
358 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
359 { DDMT(tp); DDME(get_class_member(tp, i)); }
360 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
362 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
363 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
370 assert(get_type_size_bits(tp) > -1);
371 for (i = 0; i < get_struct_n_members(tp); i++) {
372 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
373 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
382 Assure that only innermost dimension is dynamic? */
384 case tpo_enumeration:
386 assert(get_type_mode != NULL);
387 for (i = 0; i < get_enumeration_n_enums(tp); i++)
388 assert(get_enumeration_enum(tp, i) != NULL);
396 unsigned long (get_type_visited)(const type *tp) {
397 return __get_type_visited(tp);
400 void (set_type_visited)(type *tp, unsigned long num) {
401 __set_type_visited(tp, num);
404 /* Sets visited field in type to type_visited. */
405 void (mark_type_visited)(type *tp) {
406 __mark_type_visited(tp);
409 /* @@@ name clash with master flag
410 int (type_visited)(const type *tp) {
411 return __type_visited(tp);
414 int (type_not_visited)(const type *tp) {
415 return __type_not_visited(tp);
418 int (is_type)(const void *thing) {
419 return __is_type(thing);
422 bool equal_type(type *typ1, type *typ2) {
427 if (typ1 == typ2) return true;
429 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
430 (get_type_ident(typ1) != get_type_ident(typ2)) ||
431 (get_type_mode(typ1) != get_type_mode(typ2)) ||
432 (get_type_state(typ1) != get_type_state(typ2)))
434 if ((get_type_state(typ1) == layout_fixed) &&
435 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
438 switch(get_type_tpop_code(typ1)) {
440 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
441 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
442 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
443 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
444 /** Compare the members **/
445 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
446 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
447 /* First sort the members of typ2 */
448 for (i = 0; i < get_class_n_members(typ1); i++) {
449 entity *e1 = get_class_member(typ1, i);
450 for (j = 0; j < get_class_n_members(typ2); j++) {
451 entity *e2 = get_class_member(typ2, j);
452 if (get_entity_name(e1) == get_entity_name(e2))
456 for (i = 0; i < get_class_n_members(typ1); i++) {
457 if (!m[i] || /* Found no counterpart */
458 !equal_entity(get_class_member(typ1, i), m[i]))
461 /** Compare the supertypes **/
462 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
463 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
464 /* First sort the supertypes of typ2 */
465 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
466 type *t1 = get_class_supertype(typ1, i);
467 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
468 type *t2 = get_class_supertype(typ2, j);
469 if (get_type_ident(t2) == get_type_ident(t1))
473 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
474 if (!t[i] || /* Found no counterpart */
475 get_class_supertype(typ1, i) != t[i])
480 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
481 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
482 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
483 /* First sort the members of lt */
484 for (i = 0; i < get_struct_n_members(typ1); i++) {
485 entity *e1 = get_struct_member(typ1, i);
486 for (j = 0; j < get_struct_n_members(typ2); j++) {
487 entity *e2 = get_struct_member(typ2, j);
488 if (get_entity_name(e1) == get_entity_name(e2))
492 for (i = 0; i < get_struct_n_members(typ1); i++) {
493 if (!m[i] || /* Found no counterpart */
494 !equal_entity(get_struct_member(typ1, i), m[i]))
499 int n_param1, n_param2;
501 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
502 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
504 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
505 n_param1 = get_method_n_params(typ1);
506 n_param2 = get_method_n_params(typ2);
509 n_param1 = get_method_first_variadic_param_index(typ1);
510 n_param2 = get_method_first_variadic_param_index(typ2);
513 if (n_param1 != n_param2) return false;
515 for (i = 0; i < n_param1; i++) {
516 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
519 for (i = 0; i < get_method_n_ress(typ1); i++) {
520 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
525 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
526 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
527 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
528 /* First sort the members of lt */
529 for (i = 0; i < get_union_n_members(typ1); i++) {
530 entity *e1 = get_union_member(typ1, i);
531 for (j = 0; j < get_union_n_members(typ2); j++) {
532 entity *e2 = get_union_member(typ2, j);
533 if (get_entity_name(e1) == get_entity_name(e2))
537 for (i = 0; i < get_union_n_members(typ1); i++) {
538 if (!m[i] || /* Found no counterpart */
539 !equal_entity(get_union_member(typ1, i), m[i]))
544 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
546 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
548 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
549 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
550 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
552 if (get_array_order(typ1, i) != get_array_order(typ2, i))
553 assert(0 && "type compare with different dimension orders not implemented");
556 case tpo_enumeration: {
557 assert(0 && "enumerations not implemented");
560 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
563 case tpo_primitive: {
570 bool smaller_type (type *st, type *lt) {
574 if (st == lt) return true;
576 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
579 switch(get_type_tpop_code(st)) {
581 return is_subclass_of(st, lt);
584 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
585 m = alloca(sizeof(entity *) * get_struct_n_members(st));
586 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
587 /* First sort the members of lt */
588 for (i = 0; i < get_struct_n_members(st); i++) {
589 entity *se = get_struct_member(st, i);
590 for (j = 0; j < get_struct_n_members(lt); j++) {
591 entity *le = get_struct_member(lt, j);
592 if (get_entity_name(le) == get_entity_name(se))
596 for (i = 0; i < get_struct_n_members(st); i++) {
597 if (!m[i] || /* Found no counterpart */
598 !smaller_type(get_entity_type(get_struct_member(st, i)),
599 get_entity_type(m[i])))
604 /** FIXME: is this still true? */
605 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
606 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
607 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
608 for (i = 0; i < get_method_n_params(st); i++) {
609 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
612 for (i = 0; i < get_method_n_ress(st); i++) {
613 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
618 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
619 m = alloca(sizeof(entity *) * get_union_n_members(st));
620 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
621 /* First sort the members of lt */
622 for (i = 0; i < get_union_n_members(st); i++) {
623 entity *se = get_union_member(st, i);
624 for (j = 0; j < get_union_n_members(lt); j++) {
625 entity *le = get_union_member(lt, j);
626 if (get_entity_name(le) == get_entity_name(se))
630 for (i = 0; i < get_union_n_members(st); i++) {
631 if (!m[i] || /* Found no counterpart */
632 !smaller_type(get_entity_type(get_union_member(st, i)),
633 get_entity_type(m[i])))
638 type *set, *let; /* small/large elt. type */
639 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
641 set = get_array_element_type(st);
642 let = get_array_element_type(lt);
644 /* If the elt types are different, set must be convertible
645 to let, and they must have the same size so that address
646 computations work out. To have a size the layout must
648 if ((get_type_state(set) != layout_fixed) ||
649 (get_type_state(let) != layout_fixed))
651 if (!smaller_type(set, let) ||
652 get_type_size_bits(set) != get_type_size_bits(let))
655 for(i = 0; i < get_array_n_dimensions(st); i++) {
656 if (get_array_lower_bound(lt, i))
657 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
659 if (get_array_upper_bound(lt, i))
660 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
664 case tpo_enumeration: {
665 assert(0 && "enumerations not implemented");
668 if (!smaller_type(get_pointer_points_to_type(st),
669 get_pointer_points_to_type(lt)))
672 case tpo_primitive: {
673 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
681 /*-----------------------------------------------------------------*/
683 /*-----------------------------------------------------------------*/
685 /* create a new class type */
686 type *new_type_class (ident *name) {
689 res = new_type(type_class, NULL, name);
691 res->attr.ca.members = NEW_ARR_F (entity *, 0);
692 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
693 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
694 res->attr.ca.peculiarity = peculiarity_existent;
695 res->attr.ca.dfn = 0;
699 type *new_d_type_class (ident *name, dbg_info* db) {
700 type *res = new_type_class (name);
701 set_type_dbg_info(res, db);
705 void free_class_entities(type *clss) {
707 assert(clss && (clss->type_op == type_class));
708 for (i = get_class_n_members(clss)-1; i >= 0; --i)
709 free_entity(get_class_member(clss, i));
712 void free_class_attrs(type *clss) {
713 assert(clss && (clss->type_op == type_class));
714 DEL_ARR_F(clss->attr.ca.members);
715 DEL_ARR_F(clss->attr.ca.subtypes);
716 DEL_ARR_F(clss->attr.ca.supertypes);
719 /* manipulate private fields of class type */
720 void add_class_member (type *clss, entity *member) {
721 assert(clss && (clss->type_op == type_class));
722 ARR_APP1 (entity *, clss->attr.ca.members, member);
725 int (get_class_n_members) (const type *clss) {
726 return __get_class_n_members(clss);
729 int get_class_member_index(type *clss, entity *mem) {
731 assert(clss && (clss->type_op == type_class));
732 for (i = 0; i < get_class_n_members(clss); i++)
733 if (get_class_member(clss, i) == mem)
738 entity *(get_class_member) (const type *clss, int pos) {
739 return __get_class_member(clss, pos);
742 entity *get_class_member_by_name(type *clss, ident *name) {
744 assert(clss && (clss->type_op == type_class));
745 n_mem = get_class_n_members(clss);
746 for (i = 0; i < n_mem; ++i) {
747 entity *mem = get_class_member(clss, i);
748 if (get_entity_ident(mem) == name) return mem;
753 void set_class_member (type *clss, entity *member, int pos) {
754 assert(clss && (clss->type_op == type_class));
755 assert(pos >= 0 && pos < get_class_n_members(clss));
756 clss->attr.ca.members[pos] = member;
758 void set_class_members (type *clss, entity **members, int arity) {
760 assert(clss && (clss->type_op == type_class));
761 DEL_ARR_F(clss->attr.ca.members);
762 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
763 for (i = 0; i < arity; i++) {
764 set_entity_owner(members[i], clss);
765 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
768 void remove_class_member(type *clss, entity *member) {
770 assert(clss && (clss->type_op == type_class));
771 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
772 if (clss->attr.ca.members[i] == member) {
773 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
774 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
775 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
781 void add_class_subtype (type *clss, type *subtype) {
783 assert(clss && (clss->type_op == type_class));
784 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
785 for (i = 0; i < get_class_n_supertypes(subtype); i++)
786 if (get_class_supertype(subtype, i) == clss)
787 /* Class already registered */
789 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
791 int get_class_n_subtypes (const type *clss) {
792 assert(clss && (clss->type_op == type_class));
793 return (ARR_LEN (clss->attr.ca.subtypes));
795 type *get_class_subtype (type *clss, int pos) {
796 assert(clss && (clss->type_op == type_class));
797 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
798 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
800 void set_class_subtype (type *clss, type *subtype, int pos) {
801 assert(clss && (clss->type_op == type_class));
802 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
803 clss->attr.ca.subtypes[pos] = subtype;
805 void remove_class_subtype(type *clss, type *subtype) {
807 assert(clss && (clss->type_op == type_class));
808 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
809 if (clss->attr.ca.subtypes[i] == subtype) {
810 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
811 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
812 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
817 void add_class_supertype (type *clss, type *supertype) {
819 assert(clss && (clss->type_op == type_class));
820 assert(supertype && (supertype -> type_op == type_class));
821 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
822 for (i = 0; i < get_class_n_subtypes(supertype); i++)
823 if (get_class_subtype(supertype, i) == clss)
824 /* Class already registered */
826 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
828 int get_class_n_supertypes (const type *clss) {
829 assert(clss && (clss->type_op == type_class));
830 return (ARR_LEN (clss->attr.ca.supertypes));
832 int get_class_supertype_index(type *clss, type *super_clss) {
834 assert(clss && (clss->type_op == type_class));
835 assert(super_clss && (super_clss->type_op == type_class));
836 for (i = 0; i < get_class_n_supertypes(clss); i++)
837 if (get_class_supertype(clss, i) == super_clss)
841 type *get_class_supertype (type *clss, int pos) {
842 assert(clss && (clss->type_op == type_class));
843 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
844 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
846 void set_class_supertype (type *clss, type *supertype, int pos) {
847 assert(clss && (clss->type_op == type_class));
848 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
849 clss->attr.ca.supertypes[pos] = supertype;
851 void remove_class_supertype(type *clss, type *supertype) {
853 assert(clss && (clss->type_op == type_class));
854 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
855 if (clss->attr.ca.supertypes[i] == supertype) {
856 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
857 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
858 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
863 const char *get_peculiarity_string(peculiarity p) {
865 case peculiarity_description:
866 return "peculiarity_description";
867 case peculiarity_inherited:
868 return "peculiarity_inherited";
870 return "peculiarity_existent";
874 peculiarity get_class_peculiarity (const type *clss) {
875 assert(clss && (clss->type_op == type_class));
876 return clss->attr.ca.peculiarity;
879 void set_class_peculiarity (type *clss, peculiarity pec) {
880 assert(clss && (clss->type_op == type_class));
881 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
882 clss->attr.ca.peculiarity = pec;
885 void set_class_dfn (type *clss, int dfn)
887 clss->attr.ca.dfn = dfn;
890 int get_class_dfn (const type *clss)
892 return (clss->attr.ca.dfn);
896 int (is_class_type)(const type *clss) {
897 return __is_class_type(clss);
900 bool is_subclass_of(type *low, type *high) {
902 assert(is_class_type(low) && is_class_type(high));
903 if (low == high) return true;
904 /* depth first search from high downwards. */
905 for (i = 0; i < get_class_n_subtypes(high); i++) {
906 if (low == get_class_subtype(high, i))
908 if (is_subclass_of(low, get_class_subtype(high, i)))
914 /*----------------------------------------------------------------**/
916 /*----------------------------------------------------------------**/
918 /* create a new type struct */
919 type *new_type_struct (ident *name) {
921 res = new_type(type_struct, NULL, name);
922 res->attr.sa.members = NEW_ARR_F (entity *, 0);
925 type *new_d_type_struct (ident *name, dbg_info* db) {
926 type *res = new_type_struct (name);
927 set_type_dbg_info(res, db);
930 void free_struct_entities (type *strct) {
932 assert(strct && (strct->type_op == type_struct));
933 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
934 free_entity(get_struct_member(strct, i));
936 void free_struct_attrs (type *strct) {
937 assert(strct && (strct->type_op == type_struct));
938 DEL_ARR_F(strct->attr.sa.members);
941 /* manipulate private fields of struct */
942 int get_struct_n_members (const type *strct) {
943 assert(strct && (strct->type_op == type_struct));
944 return (ARR_LEN (strct->attr.sa.members));
947 void add_struct_member (type *strct, entity *member) {
948 assert(strct && (strct->type_op == type_struct));
949 assert(get_type_tpop(get_entity_type(member)) != type_method);
950 /* @@@ lowerfirm geht nicht durch */
951 ARR_APP1 (entity *, strct->attr.sa.members, member);
954 entity *get_struct_member (const type *strct, int pos) {
955 assert(strct && (strct->type_op == type_struct));
956 assert(pos >= 0 && pos < get_struct_n_members(strct));
957 return strct->attr.sa.members[pos];
960 int get_struct_member_index(type *strct, entity *mem) {
962 assert(strct && (strct->type_op == type_struct));
963 for (i = 0; i < get_struct_n_members(strct); i++)
964 if (get_struct_member(strct, i) == mem)
969 void set_struct_member (type *strct, int pos, entity *member) {
970 assert(strct && (strct->type_op == type_struct));
971 assert(pos >= 0 && pos < get_struct_n_members(strct));
972 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
973 strct->attr.sa.members[pos] = member;
975 void remove_struct_member(type *strct, entity *member) {
977 assert(strct && (strct->type_op == type_struct));
978 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
979 if (strct->attr.sa.members[i] == member) {
980 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
981 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
982 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
988 int (is_struct_type)(const type *strct) {
989 return __is_struct_type(strct);
992 /*******************************************************************/
994 /*******************************************************************/
997 * Lazy construction of value argument / result representation.
998 * Constructs a struct type and its member. The types of the members
999 * are passed in the argument list.
1001 * @param name name of the type constructed
1002 * @param len number of fields
1003 * @param tps array of field types with length len
1005 static INLINE type *
1006 build_value_type(ident *name, int len, type **tps) {
1008 type *res = new_type_struct(name);
1009 /* Remove type from type list. Must be treated differently than other types. */
1010 remove_irp_type_from_list(res);
1011 for (i = 0; i < len; i++) {
1012 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1013 if (tps[i]) elt_type = tps[i];
1014 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1019 /* Create a new method type.
1020 N_param is the number of parameters, n_res the number of results. */
1021 type *new_type_method (ident *name, int n_param, int n_res) {
1024 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1025 res = new_type(type_method, mode_P_mach, name);
1026 res->state = layout_fixed;
1027 res->size = get_mode_size_bits(mode_P_mach);
1028 res->attr.ma.n_params = n_param;
1029 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
1030 res->attr.ma.value_params = NULL;
1031 res->attr.ma.n_res = n_res;
1032 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
1033 res->attr.ma.value_ress = NULL;
1034 res->attr.ma.variadicity = variadicity_non_variadic;
1035 res->attr.ma.first_variadic_param = -1;
1040 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1041 type *res = new_type_method (name, n_param, n_res);
1042 set_type_dbg_info(res, db);
1046 void free_method_entities(type *method) {
1047 assert(method && (method->type_op == type_method));
1050 /* Attention: also frees entities in value parameter subtypes! */
1051 void free_method_attrs(type *method) {
1052 assert(method && (method->type_op == type_method));
1053 free(method->attr.ma.param_type);
1054 free(method->attr.ma.res_type);
1055 if (method->attr.ma.value_params) {
1056 free_type_entities(method->attr.ma.value_params);
1057 free_type(method->attr.ma.value_params);
1059 if (method->attr.ma.value_ress) {
1060 free_type_entities(method->attr.ma.value_ress);
1061 free_type(method->attr.ma.value_ress);
1065 /* manipulate private fields of method. */
1066 int get_method_n_params (const type *method) {
1067 assert(method && (method->type_op == type_method));
1068 return method->attr.ma.n_params;
1071 type *get_method_param_type(type *method, int pos) {
1073 assert(method && (method->type_op == type_method));
1074 assert(pos >= 0 && pos < get_method_n_params(method));
1075 res = method->attr.ma.param_type[pos];
1076 assert(res != NULL && "empty method param type");
1077 return method->attr.ma.param_type[pos] = skip_tid(res);
1080 void set_method_param_type(type *method, int pos, type* tp) {
1081 assert(method && (method->type_op == type_method));
1082 assert(pos >= 0 && pos < get_method_n_params(method));
1083 method->attr.ma.param_type[pos] = tp;
1084 /* If information constructed set pass-by-value representation. */
1085 if (method->attr.ma.value_params) {
1086 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1087 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1091 /* Returns an entity that represents the copied value argument. Only necessary
1092 for compounds passed by value. */
1093 entity *get_method_value_param_ent(type *method, int pos) {
1094 assert(method && (method->type_op == type_method));
1095 assert(pos >= 0 && pos < get_method_n_params(method));
1096 if (!method->attr.ma.value_params)
1097 method->attr.ma.value_params
1098 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1099 get_method_n_params(method), method->attr.ma.param_type);
1100 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1101 != method->attr.ma.value_params)
1102 && "param type not yet set");
1103 return get_struct_member(method->attr.ma.value_params, pos);
1107 * Returns a type that represents the copied value arguments.
1109 type *get_method_value_param_type(const type *method)
1111 assert(method && (method->type_op == type_method));
1112 return method->attr.ma.value_params;
1115 int get_method_n_ress (const type *method) {
1116 assert(method && (method->type_op == type_method));
1117 return method->attr.ma.n_res;
1120 type *get_method_res_type(type *method, int pos) {
1122 assert(method && (method->type_op == type_method));
1123 assert(pos >= 0 && pos < get_method_n_ress(method));
1124 res = method->attr.ma.res_type[pos];
1125 assert(res != NULL && "empty method return type");
1126 return method->attr.ma.res_type[pos] = skip_tid(res);
1129 void set_method_res_type(type *method, int pos, type* tp) {
1130 assert(method && (method->type_op == type_method));
1131 assert(pos >= 0 && pos < get_method_n_ress(method));
1132 /* set the result type */
1133 method->attr.ma.res_type[pos] = tp;
1134 /* If information constructed set pass-by-value representation. */
1135 if (method->attr.ma.value_ress) {
1136 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1137 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1141 /* Returns an entity that represents the copied value result. Only necessary
1142 for compounds passed by value. */
1143 entity *get_method_value_res_ent(type *method, int pos) {
1144 assert(method && (method->type_op == type_method));
1145 assert(pos >= 0 && pos < get_method_n_ress(method));
1146 if (!method->attr.ma.value_ress)
1147 method->attr.ma.value_ress
1148 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1149 get_method_n_ress(method), method->attr.ma.res_type);
1150 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1151 && "result type not yet set");
1152 return get_struct_member(method->attr.ma.value_ress, pos);
1156 * Returns a type that represents the copied value results.
1158 type *get_method_value_res_type(const type *method) {
1159 assert(method && (method->type_op == type_method));
1160 return method->attr.ma.value_ress;
1163 /* Returns the null-terminated name of this variadicity. */
1164 const char *get_variadicity_name(variadicity vari)
1166 #define X(a) case a: return #a
1168 X(variadicity_non_variadic);
1169 X(variadicity_variadic);
1176 variadicity get_method_variadicity(const type *method)
1178 assert(method && (method->type_op == type_method));
1179 return method->attr.ma.variadicity;
1182 void set_method_variadicity(type *method, variadicity vari)
1184 assert(method && (method->type_op == type_method));
1185 method->attr.ma.variadicity = vari;
1189 * Returns the first variadic parameter index of a type.
1190 * If this index was NOT set, the index of the last parameter
1191 * of the method type plus one is returned for variadic functions.
1192 * Non-variadic function types always return -1 here.
1194 int get_method_first_variadic_param_index(const type *method)
1196 assert(method && (method->type_op == type_method));
1198 if (method->attr.ma.variadicity == variadicity_non_variadic)
1201 if (method->attr.ma.first_variadic_param == -1)
1202 return get_method_n_params(method);
1203 return method->attr.ma.first_variadic_param;
1207 * Sets the first variadic parameter index. This allows to specify
1208 * a complete call type (containing the type of all parameters)
1209 * but still have the knowledge, which parameter must be passed as
1212 void set_method_first_variadic_param_index(type *method, int index)
1214 assert(method && (method->type_op == type_method));
1215 assert(index >= 0 && index <= get_method_n_params(method));
1217 method->attr.ma.first_variadic_param = index;
1221 int (is_method_type)(const type *method) {
1222 return __is_method_type(method);
1225 /*-----------------------------------------------------------------*/
1227 /*-----------------------------------------------------------------*/
1229 /* create a new type uni */
1230 type *new_type_union (ident *name) {
1232 res = new_type(type_union, NULL, name);
1233 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
1234 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
1235 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1238 type *new_d_type_union (ident *name, dbg_info* db) {
1239 type *res = new_type_union (name);
1240 set_type_dbg_info(res, db);
1243 void free_union_entities (type *uni) {
1245 assert(uni && (uni->type_op == type_union));
1246 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1247 free_entity(get_union_member(uni, i));
1249 void free_union_attrs (type *uni) {
1250 assert(uni && (uni->type_op == type_union));
1251 DEL_ARR_F(uni->attr.ua.members);
1253 /* manipulate private fields of union */
1255 int get_union_n_types (type *uni) {
1256 assert(uni && (uni->type_op == type_union));
1257 return uni->attr.ua.n_types;
1259 type *get_union_unioned_type (type *uni, int pos) {
1260 assert(uni && (uni->type_op == type_union));
1261 assert(pos >= 0 && pos < get_union_n_types(uni));
1262 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1264 void set_union_unioned_type (type *uni, int pos, type *tp) {
1265 assert(uni && (uni->type_op == type_union));
1266 assert(pos >= 0 && pos < get_union_n_types(uni));
1267 uni->attr.ua.unioned_type[pos] = tp;
1269 ident *get_union_delim_nameid (type *uni, int pos) {
1270 assert(uni && (uni->type_op == type_union));
1271 assert(pos >= 0 && pos < get_union_n_types(uni));
1272 return uni->attr.ua.delim_names[pos];
1274 const char *get_union_delim_name (type *uni, int pos) {
1275 assert(uni && (uni->type_op == type_union));
1276 assert(pos >= 0 && pos < get_union_n_types(uni));
1277 return get_id_str(uni->attr.ua.delim_names[pos]);
1279 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1280 assert(uni && (uni->type_op == type_union));
1281 assert(pos >= 0 && pos < get_union_n_types(uni));
1282 uni->attr.ua.delim_names[pos] = id;
1285 int get_union_n_members (const type *uni) {
1286 assert(uni && (uni->type_op == type_union));
1287 return (ARR_LEN (uni->attr.ua.members));
1289 void add_union_member (type *uni, entity *member) {
1290 assert(uni && (uni->type_op == type_union));
1291 ARR_APP1 (entity *, uni->attr.ua.members, member);
1293 entity *get_union_member (const type *uni, int pos) {
1294 assert(uni && (uni->type_op == type_union));
1295 assert(pos >= 0 && pos < get_union_n_members(uni));
1296 return uni->attr.ua.members[pos];
1298 void set_union_member (type *uni, int pos, entity *member) {
1299 assert(uni && (uni->type_op == type_union));
1300 assert(pos >= 0 && pos < get_union_n_members(uni));
1301 uni->attr.ua.members[pos] = member;
1303 void remove_union_member(type *uni, entity *member) {
1305 assert(uni && (uni->type_op == type_union));
1306 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1307 if (uni->attr.ua.members[i] == member) {
1308 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1309 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1310 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1316 int (is_union_type)(const type *uni) {
1317 return __is_union_type(uni);
1320 /*-----------------------------------------------------------------*/
1322 /*-----------------------------------------------------------------*/
1325 /* create a new type array -- set dimension sizes independently */
1326 type *new_type_array (ident *name, int n_dimensions,
1327 type *element_type) {
1330 ir_graph *rem = current_ir_graph;
1331 assert(!is_method_type(element_type));
1333 res = new_type(type_array, NULL, name);
1334 res->attr.aa.n_dimensions = n_dimensions;
1335 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1336 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1337 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1339 current_ir_graph = get_const_code_irg();
1340 for (i = 0; i < n_dimensions; i++) {
1341 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1342 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1343 res->attr.aa.order[i] = i;
1345 current_ir_graph = rem;
1347 res->attr.aa.element_type = element_type;
1348 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1353 type *new_d_type_array (ident *name, int n_dimensions,
1354 type *element_type, dbg_info* db) {
1355 type *res = new_type_array (name, n_dimensions, element_type);
1356 set_type_dbg_info(res, db);
1360 void free_array_entities (type *array) {
1361 assert(array && (array->type_op == type_array));
1364 void free_array_attrs (type *array) {
1365 assert(array && (array->type_op == type_array));
1366 free(array->attr.aa.lower_bound);
1367 free(array->attr.aa.upper_bound);
1370 /* manipulate private fields of array type */
1371 int get_array_n_dimensions (const type *array) {
1372 assert(array && (array->type_op == type_array));
1373 return array->attr.aa.n_dimensions;
1377 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1378 ir_node * upper_bound) {
1379 assert(array && (array->type_op == type_array));
1380 assert(lower_bound && "lower_bound node may not be NULL.");
1381 assert(upper_bound && "upper_bound node may not be NULL.");
1382 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1383 array->attr.aa.lower_bound[dimension] = lower_bound;
1384 array->attr.aa.upper_bound[dimension] = upper_bound;
1387 set_array_bounds_int (type *array, int dimension, int lower_bound,
1389 ir_graph *rem = current_ir_graph;
1390 current_ir_graph = get_const_code_irg();
1391 set_array_bounds (array, dimension,
1392 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1393 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1394 current_ir_graph = rem;
1397 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1398 assert(array && (array->type_op == type_array));
1399 assert(lower_bound && "lower_bound node may not be NULL.");
1400 array->attr.aa.lower_bound[dimension] = lower_bound;
1402 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1403 ir_graph *rem = current_ir_graph;
1404 current_ir_graph = get_const_code_irg();
1405 set_array_lower_bound (array, dimension,
1406 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1407 current_ir_graph = rem;
1410 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1411 assert(array && (array->type_op == type_array));
1412 assert(upper_bound && "upper_bound node may not be NULL.");
1413 array->attr.aa.upper_bound[dimension] = upper_bound;
1415 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1416 ir_graph *rem = current_ir_graph;
1417 current_ir_graph = get_const_code_irg();
1418 set_array_upper_bound (array, dimension,
1419 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1420 current_ir_graph = rem;
1422 int has_array_lower_bound (const type *array, int dimension) {
1423 assert(array && (array->type_op == type_array));
1424 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1426 ir_node *get_array_lower_bound (const type *array, int dimension) {
1427 assert(array && (array->type_op == type_array));
1428 return array->attr.aa.lower_bound[dimension];
1430 long get_array_lower_bound_int (const type *array, int dimension) {
1432 assert(array && (array->type_op == type_array));
1433 node = array->attr.aa.lower_bound[dimension];
1434 assert(get_irn_op(node) == op_Const);
1435 return get_tarval_long(get_Const_tarval(node));
1437 int has_array_upper_bound (const type *array, int dimension) {
1438 assert(array && (array->type_op == type_array));
1439 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1441 ir_node * get_array_upper_bound (const type *array, int dimension) {
1442 assert(array && (array->type_op == type_array));
1443 return array->attr.aa.upper_bound[dimension];
1445 long get_array_upper_bound_int (const type *array, int dimension) {
1447 assert(array && (array->type_op == type_array));
1448 node = array->attr.aa.upper_bound[dimension];
1449 assert(get_irn_op(node) == op_Const);
1450 return get_tarval_long(get_Const_tarval(node));
1453 void set_array_order (type *array, int dimension, int order) {
1454 assert(array && (array->type_op == type_array));
1455 array->attr.aa.order[dimension] = order;
1457 int get_array_order (const type *array, int dimension) {
1458 assert(array && (array->type_op == type_array));
1459 return array->attr.aa.order[dimension];
1462 void set_array_element_type (type *array, type *tp) {
1463 assert(array && (array->type_op == type_array));
1464 assert(!is_method_type(tp));
1465 array->attr.aa.element_type = tp;
1467 type *get_array_element_type (type *array) {
1468 assert(array && (array->type_op == type_array));
1469 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1472 void set_array_element_entity (type *array, entity *ent) {
1473 assert(array && (array->type_op == type_array));
1474 assert((get_entity_type(ent)->type_op != type_method));
1475 array->attr.aa.element_ent = ent;
1476 array->attr.aa.element_type = get_entity_type(ent);
1478 entity *get_array_element_entity (const type *array) {
1479 assert(array && (array->type_op == type_array));
1480 return array->attr.aa.element_ent;
1484 int (is_array_type)(const type *array) {
1485 return __is_array_type(array);
1488 /*-----------------------------------------------------------------*/
1489 /* TYPE_ENUMERATION */
1490 /*-----------------------------------------------------------------*/
1492 /* create a new type enumeration -- set the enumerators independently */
1493 type *new_type_enumeration (ident *name, int n_enums) {
1495 res = new_type(type_enumeration, NULL, name);
1496 res->attr.ea.n_enums = n_enums;
1497 res->attr.ea.enumer = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
1498 res->attr.ea.enum_nameid = (ident **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1499 memset(res->attr.ea.enumer, 0, sizeof(res->attr.ea.enumer[0]) * n_enums);
1500 memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1503 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1504 type *res = new_type_enumeration (name, n_enums);
1505 set_type_dbg_info(res, db);
1509 void free_enumeration_entities(type *enumeration) {
1510 assert(enumeration && (enumeration->type_op == type_enumeration));
1512 void free_enumeration_attrs(type *enumeration) {
1513 assert(enumeration && (enumeration->type_op == type_enumeration));
1514 free(enumeration->attr.ea.enumer);
1515 free(enumeration->attr.ea.enum_nameid);
1518 /* manipulate fields of enumeration type. */
1519 int get_enumeration_n_enums (const type *enumeration) {
1520 assert(enumeration && (enumeration->type_op == type_enumeration));
1521 return enumeration->attr.ea.n_enums;
1523 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1524 assert(enumeration && (enumeration->type_op == type_enumeration));
1525 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1526 enumeration->attr.ea.enumer[pos] = con;
1528 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1529 assert(enumeration && (enumeration->type_op == type_enumeration));
1530 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1531 return enumeration->attr.ea.enumer[pos];
1533 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1534 assert(enumeration && (enumeration->type_op == type_enumeration));
1535 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1536 enumeration->attr.ea.enum_nameid[pos] = id;
1538 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1539 assert(enumeration && (enumeration->type_op == type_enumeration));
1540 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1541 return enumeration->attr.ea.enum_nameid[pos];
1543 const char *get_enumeration_name(const type *enumeration, int pos) {
1544 assert(enumeration && (enumeration->type_op == type_enumeration));
1545 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1546 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1550 int (is_enumeration_type)(const type *enumeration) {
1551 return __is_enumeration_type(enumeration);
1554 /*-----------------------------------------------------------------*/
1556 /*-----------------------------------------------------------------*/
1558 /* Create a new type pointer */
1559 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1561 assert(mode_is_reference(ptr_mode));
1562 res = new_type(type_pointer, ptr_mode, name);
1563 res->attr.pa.points_to = points_to;
1564 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1565 res->size = get_mode_size_bits(res->mode);
1566 res->state = layout_fixed;
1569 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1570 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1571 set_type_dbg_info(res, db);
1574 void free_pointer_entities (type *pointer) {
1575 assert(pointer && (pointer->type_op == type_pointer));
1577 void free_pointer_attrs (type *pointer) {
1578 assert(pointer && (pointer->type_op == type_pointer));
1580 /* manipulate fields of type_pointer */
1581 void set_pointer_points_to_type (type *pointer, type *tp) {
1582 assert(pointer && (pointer->type_op == type_pointer));
1583 pointer->attr.pa.points_to = tp;
1585 type *get_pointer_points_to_type (type *pointer) {
1586 assert(pointer && (pointer->type_op == type_pointer));
1587 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1591 int (is_pointer_type)(const type *pointer) {
1592 return __is_pointer_type(pointer);
1595 /* Returns the first pointer type that has as points_to tp.
1596 * Not efficient: O(#types).
1597 * If not found returns unknown_type. */
1598 type *find_pointer_type_to_type (type *tp) {
1600 for (i = 0; i < get_irp_n_types(); ++i) {
1601 type *found = get_irp_type(i);
1602 if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1605 return unknown_type;
1610 /*-----------------------------------------------------------------*/
1611 /* TYPE_PRIMITIVE */
1612 /*-----------------------------------------------------------------*/
1614 /* create a new type primitive */
1615 type *new_type_primitive (ident *name, ir_mode *mode) {
1617 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1618 res = new_type(type_primitive, mode, name);
1619 res->size = get_mode_size_bits(mode);
1620 res->state = layout_fixed;
1623 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1624 type *res = new_type_primitive (name, mode);
1625 set_type_dbg_info(res, db);
1628 void free_primitive_entities (type *primitive) {
1629 assert(primitive && (primitive->type_op == type_primitive));
1631 void free_primitive_attrs (type *primitive) {
1632 assert(primitive && (primitive->type_op == type_primitive));
1636 int (is_primitive_type)(const type *primitive) {
1637 return __is_primitive_type(primitive);
1640 /*-----------------------------------------------------------------*/
1641 /* common functionality */
1642 /*-----------------------------------------------------------------*/
1645 int (is_atomic_type)(const type *tp) {
1646 return __is_atomic_type(tp);
1650 * Gets the number of elements in a firm compound type.
1652 int get_compound_n_members(const type *tp)
1656 if (is_struct_type(tp))
1657 res = get_struct_n_members(tp);
1658 else if (is_class_type(tp))
1659 res = get_class_n_members(tp);
1660 else if (is_union_type(tp))
1661 res = get_union_n_members(tp);
1663 assert(0 && "need struct, union or class for member count");
1669 * Gets the member of a firm compound type at position pos.
1671 entity *get_compound_member(const type *tp, int pos)
1675 if (is_struct_type(tp))
1676 res = get_struct_member(tp, pos);
1677 else if (is_class_type(tp))
1678 res = get_class_member(tp, pos);
1679 else if (is_union_type(tp))
1680 res = get_union_member(tp, pos);
1683 assert(0 && "need struct, union or class to get a member");
1691 int is_compound_type(const type *tp) {
1692 assert(tp && tp->kind == k_type);
1693 return (is_class_type(tp) || is_struct_type(tp) ||
1694 is_array_type(tp) || is_union_type(tp));