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.
17 * Implementation of the datastructure to hold
20 * (C) 2001 by Universitaet Karlsruhe
23 * This module supplies a datastructure to represent all types
24 * known in the compiled program. This includes types specified
25 * in the program as well as types defined by the language. In the
26 * view of the intermediate representation there is no difference
27 * between these types.
29 * There exist several kinds of types, arranged by the structure of
30 * the type. A type is described by a set of attributes. Some of
31 * these attributes are common to all types, others depend on the
34 * Types are different from the modes defined in irmode: Types are
35 * on the level of the programming language, modes at the level of
36 * the target processor.
38 * @see type_t.h type tpop
61 # include "irprog_t.h"
64 # include "typegmod.h"
70 /*-----------------------------------------------------------------*/
72 /*-----------------------------------------------------------------*/
74 type *firm_none_type; type *get_none_type(void) { return firm_none_type; }
75 type *firm_unknown_type; type *get_unknown_type(void) { return firm_unknown_type; }
79 /* Returns a new, unique number to number nodes or the like. */
80 int get_irp_new_node_nr(void);
83 /* Suffixes added to types used for pass-by-value representations. */
84 static ident *value_params_suffix = NULL;
85 static ident *value_ress_suffix = NULL;
87 /* Initialize the type module. */
88 void firm_init_type(dbg_info *builtin_db)
90 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
91 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
93 /* construct none and unknown type. */
94 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
95 set_type_size_bits(firm_none_type, 0);
96 set_type_state (firm_none_type, layout_fixed);
97 remove_irp_type(firm_none_type);
99 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
100 set_type_size_bits(firm_unknown_type, 0);
101 set_type_state (firm_unknown_type, layout_fixed);
102 remove_irp_type(firm_unknown_type);
105 /** the global type visited flag */
106 unsigned long firm_type_visited;
108 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
109 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
110 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
114 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
118 assert(type_op != type_id);
119 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
121 node_size = offsetof(type, attr) + type_op->attr_size;
122 res = xmalloc(node_size);
123 memset(res, 0, node_size);
126 res->type_op = type_op;
129 res->visibility = visibility_external_allocated;
131 res->state = layout_undefined;
138 res->nr = get_irp_new_node_nr();
139 #endif /* defined DEBUG_libfirm */
141 add_irp_type(res); /* Remember the new type global. */
146 void free_type(type *tp) {
147 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
149 /* Remove from list of all types */
151 /* Free the attributes of the type. */
153 /* Free entities automatically allocated with the type */
154 if (is_Array_type(tp))
155 free_entity(get_array_element_entity(tp));
156 /* And now the type itself... */
161 void free_type_entities(type *tp) {
162 switch(get_type_tpop_code(tp)) {
163 case tpo_class: { free_class_entities(tp); } break;
164 case tpo_struct: { free_struct_entities(tp); } break;
165 case tpo_method: { free_method_entities(tp); } break;
166 case tpo_union: { free_union_entities(tp); } break;
167 case tpo_array: { free_array_entities(tp); } break;
168 case tpo_enumeration: { free_enumeration_entities(tp); } break;
169 case tpo_pointer: { free_pointer_entities(tp); } break;
170 case tpo_primitive: { free_primitive_entities(tp); } break;
175 void free_type_attrs(type *tp) {
176 switch(get_type_tpop_code(tp)) {
177 case tpo_class: { free_class_attrs(tp); } break;
178 case tpo_struct: { free_struct_attrs(tp); } break;
179 case tpo_method: { free_method_attrs(tp); } break;
180 case tpo_union: { free_union_attrs(tp); } break;
181 case tpo_array: { free_array_attrs(tp); } break;
182 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
183 case tpo_pointer: { free_pointer_attrs(tp); } break;
184 case tpo_primitive: { free_primitive_attrs(tp); } break;
189 /* set/get the link field */
190 void *(get_type_link)(const type *tp)
192 return _get_type_link(tp);
195 void (set_type_link)(type *tp, void *l)
197 _set_type_link(tp, l);
200 const tp_op *(get_type_tpop)(const type *tp) {
201 return _get_type_tpop(tp);
204 ident *(get_type_tpop_nameid)(const type *tp) {
205 return _get_type_tpop_nameid(tp);
208 const char* get_type_tpop_name(const type *tp) {
209 assert(tp && tp->kind == k_type);
210 return get_id_str(tp->type_op->name);
213 tp_opcode (get_type_tpop_code)(const type *tp) {
214 return _get_type_tpop_code(tp);
217 ir_mode *(get_type_mode)(const type *tp) {
218 return _get_type_mode(tp);
221 void set_type_mode(type *tp, ir_mode* m) {
222 assert(tp && tp->kind == k_type);
224 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
225 /* Modes of primitives must be data */
226 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
227 /* Modes of enumerations must be integers */
228 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
229 /* Modes of pointers must be references. */
231 switch (get_type_tpop_code(tp)) {
233 /* For primitive size depends on the mode. */
234 tp->size = get_mode_size_bits(m);
237 case tpo_enumeration:
239 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
240 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
241 tp->size = get_mode_size_bits(m);
246 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
247 assert(get_type_state(tp) == layout_fixed &&
248 tp->size == get_mode_size_bits(m) &&
249 "mode don't match struct/class layout");
253 assert(0 && "setting a mode is NOT allowed for this type");
257 ident *(get_type_ident)(const type *tp) {
258 return _get_type_ident(tp);
261 void (set_type_ident)(type *tp, ident* id) {
262 _set_type_ident(tp, id);
265 /* Outputs a unique number for this node */
266 long get_type_nr(const type *tp) {
275 const char* get_type_name(const type *tp) {
276 assert(tp && tp->kind == k_type);
277 return (get_id_str(tp->name));
280 int (get_type_size_bytes)(const type *tp) {
281 return _get_type_size_bytes(tp);
284 int (get_type_size_bits)(const type *tp) {
285 return _get_type_size_bits(tp);
289 visibility get_type_visibility (const type *tp) {
291 visibility res = visibility_local;
292 if (is_compound_type(tp)) {
294 if (is_Array_type(tp)) {
295 entity *mem = get_array_element_entity(tp);
296 if (get_entity_visibility(mem) != visibility_local)
297 res = visibility_external_visible;
299 int i, n_mems = get_compound_n_members(tp);
300 for (i = 0; i < n_mems; ++i) {
301 entity *mem = get_compound_member(tp, i);
302 if (get_entity_visibility(mem) != visibility_local)
303 res = visibility_external_visible;
310 return tp->visibility;
313 void set_type_visibility (type *tp, visibility v) {
316 /* check for correctness */
317 if (v != visibility_external_allocated) {
318 visibility res = visibility_local;
319 if (is_compound_type(tp)) {
320 if (is_Array_type(tp)) {
321 entity *mem = get_array_element_entity(tp);
322 if (get_entity_visibility(mem) > res)
323 res = get_entity_visibility(mem);
325 int i, n_mems = get_compound_n_members(tp);
326 for (i = 0; i < n_mems; ++i) {
327 entity *mem = get_compound_member(tp, i);
328 if (get_entity_visibility(mem) > res)
329 res = get_entity_visibility(mem);
340 set_type_size_bits(type *tp, int size) {
341 assert(tp && tp->kind == k_type);
342 /* For pointer enumeration and primitive size depends on the mode.
343 Methods don't have a size. */
344 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
345 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
346 if (tp->type_op == type_primitive)
349 /* argh: we must allow to set negative values as "invalid size" */
350 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
351 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
357 set_type_size_bytes(type *tp, int size) {
358 set_type_size_bits(tp, 8*size);
361 int get_type_alignment_bytes(type *tp) {
362 int align = get_type_alignment_bits(tp);
364 return align < 0 ? align : (align + 7) >> 3;
367 int get_type_alignment_bits(type *tp) {
373 /* alignment NOT set calculate it "on demand" */
375 align = get_mode_size_bits(tp->mode);
376 else if (is_Array_type(tp))
377 align = get_type_alignment_bits(get_array_element_type(tp));
378 else if (is_compound_type(tp)) {
379 int i, n = get_compound_n_members(tp);
382 for (i = 0; i < n; ++i) {
383 type *t = get_entity_type(get_compound_member(tp, i));
384 int a = get_type_alignment_bits(t);
390 else if (is_Method_type(tp))
400 set_type_alignment_bits(type *tp, int align) {
401 assert(tp && tp->kind == k_type);
402 /* Methods don't have an alignment. */
403 if (tp->type_op != type_method) {
409 set_type_alignment_bytes(type *tp, int align) {
410 set_type_size_bits(tp, 8*align);
413 /* Returns a human readable string for the enum entry. */
414 const char *get_type_state_name(type_state s) {
415 #define X(a) case a: return #a;
425 type_state (get_type_state)(const type *tp) {
426 return _get_type_state(tp);
430 set_type_state(type *tp, type_state state) {
431 assert(tp && tp->kind == k_type);
433 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
434 (tp->type_op == type_method))
437 /* Just a correctness check: */
438 if (state == layout_fixed) {
440 switch (get_type_tpop_code(tp)) {
443 assert(get_type_size_bits(tp) > -1);
444 if (tp != get_glob_type()) {
445 int n_mem = get_class_n_members(tp);
446 for (i = 0; i < n_mem; i++) {
447 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
448 { DDMT(tp); DDME(get_class_member(tp, i)); }
449 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
451 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
452 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
459 assert(get_type_size_bits(tp) > -1);
460 for (i = 0; i < get_struct_n_members(tp); i++) {
461 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
462 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
471 Assure that only innermost dimension is dynamic? */
473 case tpo_enumeration:
475 assert(get_type_mode != NULL);
476 for (i = 0; i < get_enumeration_n_enums(tp); i++)
477 assert(get_enumeration_enum(tp, i) != NULL);
485 unsigned long (get_type_visited)(const type *tp) {
486 return _get_type_visited(tp);
489 void (set_type_visited)(type *tp, unsigned long num) {
490 _set_type_visited(tp, num);
493 /* Sets visited field in type to type_visited. */
494 void (mark_type_visited)(type *tp) {
495 _mark_type_visited(tp);
498 int (type_visited)(const type *tp) {
499 return _type_visited(tp);
502 int (type_not_visited)(const type *tp) {
503 return _type_not_visited(tp);
506 int (is_type)(const void *thing) {
507 return _is_type(thing);
510 /* Checks whether two types are structural equal.*/
511 int equal_type(type *typ1, type *typ2) {
516 if (typ1 == typ2) return 1;
518 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
519 (get_type_ident(typ1) != get_type_ident(typ2)) ||
520 (get_type_mode(typ1) != get_type_mode(typ2)) ||
521 (get_type_state(typ1) != get_type_state(typ2)))
523 if ((get_type_state(typ1) == layout_fixed) &&
524 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
527 switch(get_type_tpop_code(typ1)) {
529 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
530 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
531 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
532 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
533 /** Compare the members **/
534 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
535 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
536 /* First sort the members of typ2 */
537 for (i = 0; i < get_class_n_members(typ1); i++) {
538 entity *e1 = get_class_member(typ1, i);
539 for (j = 0; j < get_class_n_members(typ2); j++) {
540 entity *e2 = get_class_member(typ2, j);
541 if (get_entity_name(e1) == get_entity_name(e2))
545 for (i = 0; i < get_class_n_members(typ1); i++) {
546 if (!m[i] || /* Found no counterpart */
547 !equal_entity(get_class_member(typ1, i), m[i]))
550 /** Compare the supertypes **/
551 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
552 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
553 /* First sort the supertypes of typ2 */
554 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
555 type *t1 = get_class_supertype(typ1, i);
556 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
557 type *t2 = get_class_supertype(typ2, j);
558 if (get_type_ident(t2) == get_type_ident(t1))
562 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
563 if (!t[i] || /* Found no counterpart */
564 get_class_supertype(typ1, i) != t[i])
569 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
570 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
571 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
572 /* First sort the members of lt */
573 for (i = 0; i < get_struct_n_members(typ1); i++) {
574 entity *e1 = get_struct_member(typ1, i);
575 for (j = 0; j < get_struct_n_members(typ2); j++) {
576 entity *e2 = get_struct_member(typ2, j);
577 if (get_entity_name(e1) == get_entity_name(e2))
581 for (i = 0; i < get_struct_n_members(typ1); i++) {
582 if (!m[i] || /* Found no counterpart */
583 !equal_entity(get_struct_member(typ1, i), m[i]))
588 int n_param1, n_param2;
590 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
591 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
593 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
594 n_param1 = get_method_n_params(typ1);
595 n_param2 = get_method_n_params(typ2);
598 n_param1 = get_method_first_variadic_param_index(typ1);
599 n_param2 = get_method_first_variadic_param_index(typ2);
602 if (n_param1 != n_param2) return 0;
604 for (i = 0; i < n_param1; i++) {
605 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
608 for (i = 0; i < get_method_n_ress(typ1); i++) {
609 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
614 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
615 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
616 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
617 /* First sort the members of lt */
618 for (i = 0; i < get_union_n_members(typ1); i++) {
619 entity *e1 = get_union_member(typ1, i);
620 for (j = 0; j < get_union_n_members(typ2); j++) {
621 entity *e2 = get_union_member(typ2, j);
622 if (get_entity_name(e1) == get_entity_name(e2))
626 for (i = 0; i < get_union_n_members(typ1); i++) {
627 if (!m[i] || /* Found no counterpart */
628 !equal_entity(get_union_member(typ1, i), m[i]))
633 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
635 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
637 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
638 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
639 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
641 if (get_array_order(typ1, i) != get_array_order(typ2, i))
642 assert(0 && "type compare with different dimension orders not implemented");
645 case tpo_enumeration: {
646 assert(0 && "enumerations not implemented");
649 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
652 case tpo_primitive: {
659 /* Checks whether two types are structural comparable. */
660 int smaller_type (type *st, type *lt) {
664 if (st == lt) return 1;
666 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
669 switch(get_type_tpop_code(st)) {
671 return is_subclass_of(st, lt);
674 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
675 m = alloca(sizeof(entity *) * get_struct_n_members(st));
676 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
677 /* First sort the members of lt */
678 for (i = 0; i < get_struct_n_members(st); i++) {
679 entity *se = get_struct_member(st, i);
680 for (j = 0; j < get_struct_n_members(lt); j++) {
681 entity *le = get_struct_member(lt, j);
682 if (get_entity_name(le) == get_entity_name(se))
686 for (i = 0; i < get_struct_n_members(st); i++) {
687 if (!m[i] || /* Found no counterpart */
688 !smaller_type(get_entity_type(get_struct_member(st, i)),
689 get_entity_type(m[i])))
694 /** FIXME: is this still 1? */
695 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
696 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
697 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
698 for (i = 0; i < get_method_n_params(st); i++) {
699 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
702 for (i = 0; i < get_method_n_ress(st); i++) {
703 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
708 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
709 m = alloca(sizeof(entity *) * get_union_n_members(st));
710 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
711 /* First sort the members of lt */
712 for (i = 0; i < get_union_n_members(st); i++) {
713 entity *se = get_union_member(st, i);
714 for (j = 0; j < get_union_n_members(lt); j++) {
715 entity *le = get_union_member(lt, j);
716 if (get_entity_name(le) == get_entity_name(se))
720 for (i = 0; i < get_union_n_members(st); i++) {
721 if (!m[i] || /* Found no counterpart */
722 !smaller_type(get_entity_type(get_union_member(st, i)),
723 get_entity_type(m[i])))
728 type *set, *let; /* small/large elt. type */
729 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
731 set = get_array_element_type(st);
732 let = get_array_element_type(lt);
734 /* If the elt types are different, set must be convertible
735 to let, and they must have the same size so that address
736 computations work out. To have a size the layout must
738 if ((get_type_state(set) != layout_fixed) ||
739 (get_type_state(let) != layout_fixed))
741 if (!smaller_type(set, let) ||
742 get_type_size_bits(set) != get_type_size_bits(let))
745 for(i = 0; i < get_array_n_dimensions(st); i++) {
746 if (get_array_lower_bound(lt, i))
747 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
749 if (get_array_upper_bound(lt, i))
750 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
754 case tpo_enumeration: {
755 assert(0 && "enumerations not implemented");
758 if (!smaller_type(get_pointer_points_to_type(st),
759 get_pointer_points_to_type(lt)))
762 case tpo_primitive: {
763 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
771 /*-----------------------------------------------------------------*/
773 /*-----------------------------------------------------------------*/
775 /* create a new class type */
776 type *new_d_type_class (ident *name, dbg_info *db) {
779 res = new_type(type_class, NULL, name, db);
781 res->attr.ca.members = NEW_ARR_F (entity *, 0);
782 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
783 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
784 res->attr.ca.peculiarity = peculiarity_existent;
785 res->attr.ca.dfn = 0;
789 type *new_type_class (ident *name) {
790 return new_d_type_class (name, NULL);
793 void free_class_entities(type *clss) {
795 assert(clss && (clss->type_op == type_class));
796 for (i = get_class_n_members(clss)-1; i >= 0; --i)
797 free_entity(get_class_member(clss, i));
800 void free_class_attrs(type *clss) {
801 assert(clss && (clss->type_op == type_class));
802 DEL_ARR_F(clss->attr.ca.members);
803 DEL_ARR_F(clss->attr.ca.subtypes);
804 DEL_ARR_F(clss->attr.ca.supertypes);
807 /* manipulate private fields of class type */
808 void add_class_member (type *clss, entity *member) {
809 assert(clss && (clss->type_op == type_class));
810 assert(clss != get_entity_type(member) && "recursive type");
811 ARR_APP1 (entity *, clss->attr.ca.members, member);
814 int (get_class_n_members) (const type *clss) {
815 return _get_class_n_members(clss);
818 int get_class_member_index(type *clss, entity *mem) {
820 assert(clss && (clss->type_op == type_class));
821 for (i = 0; i < get_class_n_members(clss); i++)
822 if (get_class_member(clss, i) == mem)
827 entity *(get_class_member) (const type *clss, int pos) {
828 return _get_class_member(clss, pos);
831 entity *get_class_member_by_name(type *clss, ident *name) {
833 assert(clss && (clss->type_op == type_class));
834 n_mem = get_class_n_members(clss);
835 for (i = 0; i < n_mem; ++i) {
836 entity *mem = get_class_member(clss, i);
837 if (get_entity_ident(mem) == name) return mem;
842 void set_class_member (type *clss, entity *member, int pos) {
843 assert(clss && (clss->type_op == type_class));
844 assert(pos >= 0 && pos < get_class_n_members(clss));
845 clss->attr.ca.members[pos] = member;
847 void set_class_members (type *clss, entity **members, int arity) {
849 assert(clss && (clss->type_op == type_class));
850 DEL_ARR_F(clss->attr.ca.members);
851 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
852 for (i = 0; i < arity; i++) {
853 set_entity_owner(members[i], clss);
854 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
857 void remove_class_member(type *clss, entity *member) {
859 assert(clss && (clss->type_op == type_class));
860 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
861 if (clss->attr.ca.members[i] == member) {
862 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
863 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
864 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
870 void add_class_subtype (type *clss, type *subtype) {
872 assert(clss && (clss->type_op == type_class));
873 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
874 for (i = 0; i < get_class_n_supertypes(subtype); i++)
875 if (get_class_supertype(subtype, i) == clss)
876 /* Class already registered */
878 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
880 int get_class_n_subtypes (const type *clss) {
881 assert(clss && (clss->type_op == type_class));
882 return (ARR_LEN (clss->attr.ca.subtypes));
884 type *get_class_subtype (type *clss, int pos) {
885 assert(clss && (clss->type_op == type_class));
886 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
887 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
889 int get_class_subtype_index(type *clss, const type *subclass) {
890 int i, n_subtypes = get_class_n_subtypes(clss);
891 assert(is_Class_type(subclass));
892 for (i = 0; i < n_subtypes; ++i) {
893 if (get_class_subtype(clss, i) == subclass) return i;
897 void set_class_subtype (type *clss, type *subtype, int pos) {
898 assert(clss && (clss->type_op == type_class));
899 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
900 clss->attr.ca.subtypes[pos] = subtype;
902 void remove_class_subtype(type *clss, type *subtype) {
904 assert(clss && (clss->type_op == type_class));
905 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
906 if (clss->attr.ca.subtypes[i] == subtype) {
907 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
908 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
909 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
914 void add_class_supertype (type *clss, type *supertype) {
916 assert(clss && (clss->type_op == type_class));
917 assert(supertype && (supertype -> type_op == type_class));
918 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
919 for (i = 0; i < get_class_n_subtypes(supertype); i++)
920 if (get_class_subtype(supertype, i) == clss)
921 /* Class already registered */
923 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
925 int get_class_n_supertypes (const type *clss) {
926 assert(clss && (clss->type_op == type_class));
927 return (ARR_LEN (clss->attr.ca.supertypes));
929 int get_class_supertype_index(type *clss, type *super_clss) {
930 int i, n_supertypes = get_class_n_supertypes(clss);
931 assert(super_clss && (super_clss->type_op == type_class));
932 for (i = 0; i < n_supertypes; i++)
933 if (get_class_supertype(clss, i) == super_clss)
937 type *get_class_supertype (type *clss, int pos) {
938 assert(clss && (clss->type_op == type_class));
939 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
940 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
942 void set_class_supertype (type *clss, type *supertype, int pos) {
943 assert(clss && (clss->type_op == type_class));
944 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
945 clss->attr.ca.supertypes[pos] = supertype;
947 void remove_class_supertype(type *clss, type *supertype) {
949 assert(clss && (clss->type_op == type_class));
950 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
951 if (clss->attr.ca.supertypes[i] == supertype) {
952 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
953 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
954 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
959 const char *get_peculiarity_string(peculiarity p) {
960 #define X(a) case a: return #a
962 X(peculiarity_description);
963 X(peculiarity_inherited);
964 X(peculiarity_existent);
967 return "invalid peculiarity";
970 peculiarity get_class_peculiarity (const type *clss) {
971 assert(clss && (clss->type_op == type_class));
972 return clss->attr.ca.peculiarity;
975 void set_class_peculiarity (type *clss, peculiarity pec) {
976 assert(clss && (clss->type_op == type_class));
977 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
978 clss->attr.ca.peculiarity = pec;
981 void set_class_dfn (type *clss, int dfn)
983 clss->attr.ca.dfn = dfn;
986 int get_class_dfn (const type *clss)
988 return (clss->attr.ca.dfn);
992 int (is_Class_type)(const type *clss) {
993 return _is_class_type(clss);
996 /*----------------------------------------------------------------**/
998 /*----------------------------------------------------------------**/
1000 /* create a new type struct */
1001 type *new_d_type_struct(ident *name, dbg_info *db) {
1002 type *res = new_type(type_struct, NULL, name, db);
1004 res->attr.sa.members = NEW_ARR_F(entity *, 0);
1007 type *new_type_struct (ident *name) {
1008 return new_d_type_struct (name, NULL);
1011 void free_struct_entities (type *strct) {
1013 assert(strct && (strct->type_op == type_struct));
1014 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1015 free_entity(get_struct_member(strct, i));
1017 void free_struct_attrs (type *strct) {
1018 assert(strct && (strct->type_op == type_struct));
1019 DEL_ARR_F(strct->attr.sa.members);
1022 /* manipulate private fields of struct */
1023 int get_struct_n_members (const type *strct) {
1024 assert(strct && (strct->type_op == type_struct));
1025 return (ARR_LEN (strct->attr.sa.members));
1028 void add_struct_member (type *strct, entity *member) {
1029 assert(strct && (strct->type_op == type_struct));
1030 assert(get_type_tpop(get_entity_type(member)) != type_method);
1031 /* @@@ lowerfirm geht nicht durch */
1032 assert(strct != get_entity_type(member) && "recursive type");
1033 ARR_APP1 (entity *, strct->attr.sa.members, member);
1036 entity *get_struct_member (const type *strct, int pos) {
1037 assert(strct && (strct->type_op == type_struct));
1038 assert(pos >= 0 && pos < get_struct_n_members(strct));
1039 return strct->attr.sa.members[pos];
1042 int get_struct_member_index(type *strct, entity *mem) {
1044 assert(strct && (strct->type_op == type_struct));
1045 for (i = 0; i < get_struct_n_members(strct); i++)
1046 if (get_struct_member(strct, i) == mem)
1051 void set_struct_member (type *strct, int pos, entity *member) {
1052 assert(strct && (strct->type_op == type_struct));
1053 assert(pos >= 0 && pos < get_struct_n_members(strct));
1054 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1055 strct->attr.sa.members[pos] = member;
1057 void remove_struct_member(type *strct, entity *member) {
1059 assert(strct && (strct->type_op == type_struct));
1060 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1061 if (strct->attr.sa.members[i] == member) {
1062 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1063 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1064 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1070 int (is_Struct_type)(const type *strct) {
1071 return _is_struct_type(strct);
1074 /*******************************************************************/
1076 /*******************************************************************/
1079 * Lazy construction of value argument / result representation.
1080 * Constructs a struct type and its member. The types of the members
1081 * are passed in the argument list.
1083 * @param name name of the type constructed
1084 * @param len number of fields
1085 * @param tps array of field types with length len
1087 static INLINE type *
1088 build_value_type(ident *name, int len, type **tps) {
1090 type *res = new_type_struct(name);
1091 /* Remove type from type list. Must be treated differently than other types. */
1092 remove_irp_type(res);
1093 for (i = 0; i < len; i++) {
1094 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1095 if (tps[i]) elt_type = tps[i];
1096 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1101 /* Create a new method type.
1102 N_param is the number of parameters, n_res the number of results. */
1103 type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1106 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1107 res = new_type(type_method, mode_P_code, name, db);
1108 res->state = layout_fixed;
1109 res->size = get_mode_size_bits(mode_P_code);
1110 res->attr.ma.n_params = n_param;
1111 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1112 res->attr.ma.value_params = NULL;
1113 res->attr.ma.n_res = n_res;
1114 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1115 res->attr.ma.value_ress = NULL;
1116 res->attr.ma.variadicity = variadicity_non_variadic;
1117 res->attr.ma.first_variadic_param = -1;
1122 type *new_type_method(ident *name, int n_param, int n_res) {
1123 return new_d_type_method(name, n_param, n_res, NULL);
1126 void free_method_entities(type *method) {
1127 assert(method && (method->type_op == type_method));
1130 /* Attention: also frees entities in value parameter subtypes! */
1131 void free_method_attrs(type *method) {
1132 assert(method && (method->type_op == type_method));
1133 free(method->attr.ma.param_type);
1134 free(method->attr.ma.res_type);
1135 if (method->attr.ma.value_params) {
1136 free_type_entities(method->attr.ma.value_params);
1137 free_type(method->attr.ma.value_params);
1139 if (method->attr.ma.value_ress) {
1140 free_type_entities(method->attr.ma.value_ress);
1141 free_type(method->attr.ma.value_ress);
1145 /* manipulate private fields of method. */
1146 int get_method_n_params (const type *method) {
1147 assert(method && (method->type_op == type_method));
1148 return method->attr.ma.n_params;
1151 type *get_method_param_type(type *method, int pos) {
1153 assert(method && (method->type_op == type_method));
1154 assert(pos >= 0 && pos < get_method_n_params(method));
1155 res = method->attr.ma.param_type[pos];
1156 assert(res != NULL && "empty method param type");
1157 return method->attr.ma.param_type[pos] = skip_tid(res);
1160 void set_method_param_type(type *method, int pos, type* tp) {
1161 assert(method && (method->type_op == type_method));
1162 assert(pos >= 0 && pos < get_method_n_params(method));
1163 method->attr.ma.param_type[pos] = tp;
1164 /* If information constructed set pass-by-value representation. */
1165 if (method->attr.ma.value_params) {
1166 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1167 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1171 /* Returns an entity that represents the copied value argument. Only necessary
1172 for compounds passed by value. */
1173 entity *get_method_value_param_ent(type *method, int pos) {
1174 assert(method && (method->type_op == type_method));
1175 assert(pos >= 0 && pos < get_method_n_params(method));
1176 if (!method->attr.ma.value_params)
1177 method->attr.ma.value_params
1178 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1179 get_method_n_params(method), method->attr.ma.param_type);
1180 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1181 != method->attr.ma.value_params)
1182 && "param type not yet set");
1183 return get_struct_member(method->attr.ma.value_params, pos);
1187 * Returns a type that represents the copied value arguments.
1189 type *get_method_value_param_type(const type *method)
1191 assert(method && (method->type_op == type_method));
1192 return method->attr.ma.value_params;
1195 int get_method_n_ress (const type *method) {
1196 assert(method && (method->type_op == type_method));
1197 return method->attr.ma.n_res;
1200 type *get_method_res_type(type *method, int pos) {
1202 assert(method && (method->type_op == type_method));
1203 assert(pos >= 0 && pos < get_method_n_ress(method));
1204 res = method->attr.ma.res_type[pos];
1205 assert(res != NULL && "empty method return type");
1206 return method->attr.ma.res_type[pos] = skip_tid(res);
1209 void set_method_res_type(type *method, int pos, type* tp) {
1210 assert(method && (method->type_op == type_method));
1211 assert(pos >= 0 && pos < get_method_n_ress(method));
1212 /* set the result type */
1213 method->attr.ma.res_type[pos] = tp;
1214 /* If information constructed set pass-by-value representation. */
1215 if (method->attr.ma.value_ress) {
1216 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1217 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1221 /* Returns an entity that represents the copied value result. Only necessary
1222 for compounds passed by value. */
1223 entity *get_method_value_res_ent(type *method, int pos) {
1224 assert(method && (method->type_op == type_method));
1225 assert(pos >= 0 && pos < get_method_n_ress(method));
1226 if (!method->attr.ma.value_ress)
1227 method->attr.ma.value_ress
1228 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1229 get_method_n_ress(method), method->attr.ma.res_type);
1230 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1231 && "result type not yet set");
1232 return get_struct_member(method->attr.ma.value_ress, pos);
1236 * Returns a type that represents the copied value results.
1238 type *get_method_value_res_type(const type *method) {
1239 assert(method && (method->type_op == type_method));
1240 return method->attr.ma.value_ress;
1243 /* Returns the null-terminated name of this variadicity. */
1244 const char *get_variadicity_name(variadicity vari)
1246 #define X(a) case a: return #a
1248 X(variadicity_non_variadic);
1249 X(variadicity_variadic);
1256 variadicity get_method_variadicity(const type *method)
1258 assert(method && (method->type_op == type_method));
1259 return method->attr.ma.variadicity;
1262 void set_method_variadicity(type *method, variadicity vari)
1264 assert(method && (method->type_op == type_method));
1265 method->attr.ma.variadicity = vari;
1269 * Returns the first variadic parameter index of a type.
1270 * If this index was NOT set, the index of the last parameter
1271 * of the method type plus one is returned for variadic functions.
1272 * Non-variadic function types always return -1 here.
1274 int get_method_first_variadic_param_index(const type *method)
1276 assert(method && (method->type_op == type_method));
1278 if (method->attr.ma.variadicity == variadicity_non_variadic)
1281 if (method->attr.ma.first_variadic_param == -1)
1282 return get_method_n_params(method);
1283 return method->attr.ma.first_variadic_param;
1287 * Sets the first variadic parameter index. This allows to specify
1288 * a complete call type (containing the type of all parameters)
1289 * but still have the knowledge, which parameter must be passed as
1292 void set_method_first_variadic_param_index(type *method, int index)
1294 assert(method && (method->type_op == type_method));
1295 assert(index >= 0 && index <= get_method_n_params(method));
1297 method->attr.ma.first_variadic_param = index;
1301 int (is_Method_type)(const type *method) {
1302 return _is_method_type(method);
1305 /*-----------------------------------------------------------------*/
1307 /*-----------------------------------------------------------------*/
1309 /* create a new type uni */
1310 type *new_d_type_union(ident *name, dbg_info *db) {
1311 type *res = new_type(type_union, NULL, name, db);
1313 res->attr.ua.members = NEW_ARR_F(entity *, 0);
1316 type *new_type_union(ident *name) {
1317 return new_d_type_union(name, NULL);
1319 void free_union_entities(type *uni) {
1321 assert(uni && (uni->type_op == type_union));
1322 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1323 free_entity(get_union_member(uni, i));
1325 void free_union_attrs (type *uni) {
1326 assert(uni && (uni->type_op == type_union));
1327 DEL_ARR_F(uni->attr.ua.members);
1329 /* manipulate private fields of union */
1330 int get_union_n_members (const type *uni) {
1331 assert(uni && (uni->type_op == type_union));
1332 return (ARR_LEN (uni->attr.ua.members));
1334 void add_union_member (type *uni, entity *member) {
1335 assert(uni && (uni->type_op == type_union));
1336 assert(uni != get_entity_type(member) && "recursive type");
1337 ARR_APP1 (entity *, uni->attr.ua.members, member);
1339 entity *get_union_member (const type *uni, int pos) {
1340 assert(uni && (uni->type_op == type_union));
1341 assert(pos >= 0 && pos < get_union_n_members(uni));
1342 return uni->attr.ua.members[pos];
1344 void set_union_member (type *uni, int pos, entity *member) {
1345 assert(uni && (uni->type_op == type_union));
1346 assert(pos >= 0 && pos < get_union_n_members(uni));
1347 uni->attr.ua.members[pos] = member;
1349 void remove_union_member(type *uni, entity *member) {
1351 assert(uni && (uni->type_op == type_union));
1352 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1353 if (uni->attr.ua.members[i] == member) {
1354 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1355 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1356 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1362 int (is_Union_type)(const type *uni) {
1363 return _is_union_type(uni);
1366 /*-----------------------------------------------------------------*/
1368 /*-----------------------------------------------------------------*/
1371 /* create a new type array -- set dimension sizes independently */
1372 type *new_d_type_array(ident *name, int n_dimensions, type *element_type, dbg_info *db) {
1376 ir_graph *rem = current_ir_graph;
1378 assert(!is_Method_type(element_type));
1380 res = new_type(type_array, NULL, name, db);
1381 res->attr.aa.n_dimensions = n_dimensions;
1382 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1383 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1384 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1386 current_ir_graph = get_const_code_irg();
1387 unk = new_Unknown( mode_Iu);
1388 for (i = 0; i < n_dimensions; i++) {
1389 res->attr.aa.lower_bound[i] =
1390 res->attr.aa.upper_bound[i] = unk;
1391 res->attr.aa.order[i] = i;
1393 current_ir_graph = rem;
1395 res->attr.aa.element_type = element_type;
1396 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1401 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1402 return new_d_type_array(name, n_dimensions, element_type, NULL);
1405 void free_array_entities (type *array) {
1406 assert(array && (array->type_op == type_array));
1409 void free_array_attrs (type *array) {
1410 assert(array && (array->type_op == type_array));
1411 free(array->attr.aa.lower_bound);
1412 free(array->attr.aa.upper_bound);
1415 /* manipulate private fields of array type */
1416 int get_array_n_dimensions (const type *array) {
1417 assert(array && (array->type_op == type_array));
1418 return array->attr.aa.n_dimensions;
1422 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1423 ir_node * upper_bound) {
1424 assert(array && (array->type_op == type_array));
1425 assert(lower_bound && "lower_bound node may not be NULL.");
1426 assert(upper_bound && "upper_bound node may not be NULL.");
1427 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1428 array->attr.aa.lower_bound[dimension] = lower_bound;
1429 array->attr.aa.upper_bound[dimension] = upper_bound;
1432 set_array_bounds_int (type *array, int dimension, int lower_bound,
1434 ir_graph *rem = current_ir_graph;
1435 current_ir_graph = get_const_code_irg();
1436 set_array_bounds (array, dimension,
1437 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1438 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1439 current_ir_graph = rem;
1442 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1443 assert(array && (array->type_op == type_array));
1444 assert(lower_bound && "lower_bound node may not be NULL.");
1445 array->attr.aa.lower_bound[dimension] = lower_bound;
1447 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1448 ir_graph *rem = current_ir_graph;
1449 current_ir_graph = get_const_code_irg();
1450 set_array_lower_bound (array, dimension,
1451 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1452 current_ir_graph = rem;
1455 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1456 assert(array && (array->type_op == type_array));
1457 assert(upper_bound && "upper_bound node may not be NULL.");
1458 array->attr.aa.upper_bound[dimension] = upper_bound;
1460 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1461 ir_graph *rem = current_ir_graph;
1462 current_ir_graph = get_const_code_irg();
1463 set_array_upper_bound (array, dimension,
1464 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1465 current_ir_graph = rem;
1467 int has_array_lower_bound (const type *array, int dimension) {
1468 assert(array && (array->type_op == type_array));
1469 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1471 ir_node *get_array_lower_bound (const type *array, int dimension) {
1472 assert(array && (array->type_op == type_array));
1473 return array->attr.aa.lower_bound[dimension];
1475 long get_array_lower_bound_int (const type *array, int dimension) {
1477 assert(array && (array->type_op == type_array));
1478 node = array->attr.aa.lower_bound[dimension];
1479 assert(get_irn_op(node) == op_Const);
1480 return get_tarval_long(get_Const_tarval(node));
1482 int has_array_upper_bound (const type *array, int dimension) {
1483 assert(array && (array->type_op == type_array));
1484 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1486 ir_node * get_array_upper_bound (const type *array, int dimension) {
1487 assert(array && (array->type_op == type_array));
1488 return array->attr.aa.upper_bound[dimension];
1490 long get_array_upper_bound_int (const type *array, int dimension) {
1492 assert(array && (array->type_op == type_array));
1493 node = array->attr.aa.upper_bound[dimension];
1494 assert(get_irn_op(node) == op_Const);
1495 return get_tarval_long(get_Const_tarval(node));
1498 void set_array_order (type *array, int dimension, int order) {
1499 assert(array && (array->type_op == type_array));
1500 array->attr.aa.order[dimension] = order;
1503 int get_array_order (const type *array, int dimension) {
1504 assert(array && (array->type_op == type_array));
1505 return array->attr.aa.order[dimension];
1508 int find_array_dimension(const type *array, int order) {
1511 assert(array && (array->type_op == type_array));
1513 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1514 if (array->attr.aa.order[dim] == order)
1520 void set_array_element_type (type *array, type *tp) {
1521 assert(array && (array->type_op == type_array));
1522 assert(!is_Method_type(tp));
1523 array->attr.aa.element_type = tp;
1525 type *get_array_element_type (type *array) {
1526 assert(array && (array->type_op == type_array));
1527 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1530 void set_array_element_entity (type *array, entity *ent) {
1531 assert(array && (array->type_op == type_array));
1532 assert((get_entity_type(ent)->type_op != type_method));
1533 array->attr.aa.element_ent = ent;
1534 array->attr.aa.element_type = get_entity_type(ent);
1536 entity *get_array_element_entity (const type *array) {
1537 assert(array && (array->type_op == type_array));
1538 return array->attr.aa.element_ent;
1542 int (is_Array_type)(const type *array) {
1543 return _is_array_type(array);
1546 /*-----------------------------------------------------------------*/
1547 /* TYPE_ENUMERATION */
1548 /*-----------------------------------------------------------------*/
1550 /* create a new type enumeration -- set the enumerators independently */
1551 type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1552 type *res = new_type(type_enumeration, NULL, name, db);
1554 res->attr.ea.n_enums = n_enums;
1555 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1556 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1559 type *new_type_enumeration(ident *name, int n_enums) {
1560 return new_d_type_enumeration(name, n_enums, NULL);
1563 void free_enumeration_entities(type *enumeration) {
1564 assert(enumeration && (enumeration->type_op == type_enumeration));
1566 void free_enumeration_attrs(type *enumeration) {
1567 assert(enumeration && (enumeration->type_op == type_enumeration));
1568 free(enumeration->attr.ea.enumer);
1569 free(enumeration->attr.ea.enum_nameid);
1572 /* manipulate fields of enumeration type. */
1573 int get_enumeration_n_enums (const type *enumeration) {
1574 assert(enumeration && (enumeration->type_op == type_enumeration));
1575 return enumeration->attr.ea.n_enums;
1577 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1578 assert(enumeration && (enumeration->type_op == type_enumeration));
1579 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1580 enumeration->attr.ea.enumer[pos] = con;
1582 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1583 assert(enumeration && (enumeration->type_op == type_enumeration));
1584 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1585 return enumeration->attr.ea.enumer[pos];
1587 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1588 assert(enumeration && (enumeration->type_op == type_enumeration));
1589 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1590 enumeration->attr.ea.enum_nameid[pos] = id;
1592 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1593 assert(enumeration && (enumeration->type_op == type_enumeration));
1594 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1595 return enumeration->attr.ea.enum_nameid[pos];
1597 const char *get_enumeration_name(const type *enumeration, int pos) {
1598 assert(enumeration && (enumeration->type_op == type_enumeration));
1599 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1600 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1604 int (is_Enumeration_type)(const type *enumeration) {
1605 return _is_enumeration_type(enumeration);
1608 /*-----------------------------------------------------------------*/
1610 /*-----------------------------------------------------------------*/
1612 /* Create a new type pointer */
1613 type *new_d_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1616 assert(mode_is_reference(ptr_mode));
1617 res = new_type(type_pointer, ptr_mode, name, db);
1618 res->attr.pa.points_to = points_to;
1619 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1620 res->size = get_mode_size_bits(res->mode);
1621 res->state = layout_fixed;
1624 type *new_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode) {
1625 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1627 void free_pointer_entities (type *pointer) {
1628 assert(pointer && (pointer->type_op == type_pointer));
1630 void free_pointer_attrs (type *pointer) {
1631 assert(pointer && (pointer->type_op == type_pointer));
1633 /* manipulate fields of type_pointer */
1634 void set_pointer_points_to_type (type *pointer, type *tp) {
1635 assert(pointer && (pointer->type_op == type_pointer));
1636 pointer->attr.pa.points_to = tp;
1638 type *get_pointer_points_to_type (type *pointer) {
1639 assert(pointer && (pointer->type_op == type_pointer));
1640 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1644 int (is_Pointer_type)(const type *pointer) {
1645 return _is_pointer_type(pointer);
1648 /* Returns the first pointer type that has as points_to tp.
1649 * Not efficient: O(#types).
1650 * If not found returns firm_unknown_type. */
1651 type *find_pointer_type_to_type (type *tp) {
1653 for (i = 0; i < get_irp_n_types(); ++i) {
1654 type *found = get_irp_type(i);
1655 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1658 return firm_unknown_type;
1663 /*-----------------------------------------------------------------*/
1664 /* TYPE_PRIMITIVE */
1665 /*-----------------------------------------------------------------*/
1667 /* create a new type primitive */
1668 type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1670 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1671 res = new_type(type_primitive, mode, name, db);
1672 res->size = get_mode_size_bits(mode);
1673 res->state = layout_fixed;
1676 type *new_type_primitive(ident *name, ir_mode *mode) {
1677 return new_d_type_primitive(name, mode, NULL);
1679 void free_primitive_entities (type *primitive) {
1680 assert(primitive && (primitive->type_op == type_primitive));
1682 void free_primitive_attrs (type *primitive) {
1683 assert(primitive && (primitive->type_op == type_primitive));
1687 int (is_Primitive_type)(const type *primitive) {
1688 return _is_primitive_type(primitive);
1691 /*-----------------------------------------------------------------*/
1692 /* common functionality */
1693 /*-----------------------------------------------------------------*/
1696 int (is_atomic_type)(const type *tp) {
1697 return _is_atomic_type(tp);
1701 * Gets the number of elements in a firm compound type.
1703 int get_compound_n_members(const type *tp)
1707 if (is_Struct_type(tp))
1708 res = get_struct_n_members(tp);
1709 else if (is_Class_type(tp))
1710 res = get_class_n_members(tp);
1711 else if (is_Union_type(tp))
1712 res = get_union_n_members(tp);
1714 assert(0 && "need struct, union or class for member count");
1720 * Gets the member of a firm compound type at position pos.
1722 entity *get_compound_member(const type *tp, int pos)
1726 if (is_Struct_type(tp))
1727 res = get_struct_member(tp, pos);
1728 else if (is_Class_type(tp))
1729 res = get_class_member(tp, pos);
1730 else if (is_Union_type(tp))
1731 res = get_union_member(tp, pos);
1734 assert(0 && "need struct, union or class to get a member");
1741 int is_compound_type(const type *tp) {
1742 assert(tp && tp->kind == k_type);
1743 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1746 /* Checks, whether a type is a frame type */
1747 int is_frame_type(const type *tp) {
1748 return tp->frame_type;
1751 /* Makes a new frame type. */
1752 type *new_type_frame(ident *name)
1754 type *res = new_type_class(name);
1756 res->frame_type = 1;
1758 /* Remove type from type list. Must be treated differently than other types. */
1759 remove_irp_type(res);