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 /* @@@ name clash with master flag
499 int (type_visited)(const type *tp) {
500 return _type_visited(tp);
503 int (type_not_visited)(const type *tp) {
504 return _type_not_visited(tp);
507 int (is_type)(const void *thing) {
508 return _is_type(thing);
511 /* Checks whether two types are structural equal.*/
512 int equal_type(type *typ1, type *typ2) {
517 if (typ1 == typ2) return 1;
519 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
520 (get_type_ident(typ1) != get_type_ident(typ2)) ||
521 (get_type_mode(typ1) != get_type_mode(typ2)) ||
522 (get_type_state(typ1) != get_type_state(typ2)))
524 if ((get_type_state(typ1) == layout_fixed) &&
525 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
528 switch(get_type_tpop_code(typ1)) {
530 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
531 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
532 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
533 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
534 /** Compare the members **/
535 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
536 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
537 /* First sort the members of typ2 */
538 for (i = 0; i < get_class_n_members(typ1); i++) {
539 entity *e1 = get_class_member(typ1, i);
540 for (j = 0; j < get_class_n_members(typ2); j++) {
541 entity *e2 = get_class_member(typ2, j);
542 if (get_entity_name(e1) == get_entity_name(e2))
546 for (i = 0; i < get_class_n_members(typ1); i++) {
547 if (!m[i] || /* Found no counterpart */
548 !equal_entity(get_class_member(typ1, i), m[i]))
551 /** Compare the supertypes **/
552 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
553 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
554 /* First sort the supertypes of typ2 */
555 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
556 type *t1 = get_class_supertype(typ1, i);
557 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
558 type *t2 = get_class_supertype(typ2, j);
559 if (get_type_ident(t2) == get_type_ident(t1))
563 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
564 if (!t[i] || /* Found no counterpart */
565 get_class_supertype(typ1, i) != t[i])
570 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
571 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
572 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
573 /* First sort the members of lt */
574 for (i = 0; i < get_struct_n_members(typ1); i++) {
575 entity *e1 = get_struct_member(typ1, i);
576 for (j = 0; j < get_struct_n_members(typ2); j++) {
577 entity *e2 = get_struct_member(typ2, j);
578 if (get_entity_name(e1) == get_entity_name(e2))
582 for (i = 0; i < get_struct_n_members(typ1); i++) {
583 if (!m[i] || /* Found no counterpart */
584 !equal_entity(get_struct_member(typ1, i), m[i]))
589 int n_param1, n_param2;
591 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
592 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
594 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
595 n_param1 = get_method_n_params(typ1);
596 n_param2 = get_method_n_params(typ2);
599 n_param1 = get_method_first_variadic_param_index(typ1);
600 n_param2 = get_method_first_variadic_param_index(typ2);
603 if (n_param1 != n_param2) return 0;
605 for (i = 0; i < n_param1; i++) {
606 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
609 for (i = 0; i < get_method_n_ress(typ1); i++) {
610 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
615 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
616 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
617 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
618 /* First sort the members of lt */
619 for (i = 0; i < get_union_n_members(typ1); i++) {
620 entity *e1 = get_union_member(typ1, i);
621 for (j = 0; j < get_union_n_members(typ2); j++) {
622 entity *e2 = get_union_member(typ2, j);
623 if (get_entity_name(e1) == get_entity_name(e2))
627 for (i = 0; i < get_union_n_members(typ1); i++) {
628 if (!m[i] || /* Found no counterpart */
629 !equal_entity(get_union_member(typ1, i), m[i]))
634 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
636 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
638 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
639 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
640 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
642 if (get_array_order(typ1, i) != get_array_order(typ2, i))
643 assert(0 && "type compare with different dimension orders not implemented");
646 case tpo_enumeration: {
647 assert(0 && "enumerations not implemented");
650 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
653 case tpo_primitive: {
660 /* Checks whether two types are structural comparable. */
661 int smaller_type (type *st, type *lt) {
665 if (st == lt) return 1;
667 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
670 switch(get_type_tpop_code(st)) {
672 return is_subclass_of(st, lt);
675 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
676 m = alloca(sizeof(entity *) * get_struct_n_members(st));
677 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
678 /* First sort the members of lt */
679 for (i = 0; i < get_struct_n_members(st); i++) {
680 entity *se = get_struct_member(st, i);
681 for (j = 0; j < get_struct_n_members(lt); j++) {
682 entity *le = get_struct_member(lt, j);
683 if (get_entity_name(le) == get_entity_name(se))
687 for (i = 0; i < get_struct_n_members(st); i++) {
688 if (!m[i] || /* Found no counterpart */
689 !smaller_type(get_entity_type(get_struct_member(st, i)),
690 get_entity_type(m[i])))
695 /** FIXME: is this still 1? */
696 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
697 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
698 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
699 for (i = 0; i < get_method_n_params(st); i++) {
700 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
703 for (i = 0; i < get_method_n_ress(st); i++) {
704 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
709 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
710 m = alloca(sizeof(entity *) * get_union_n_members(st));
711 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
712 /* First sort the members of lt */
713 for (i = 0; i < get_union_n_members(st); i++) {
714 entity *se = get_union_member(st, i);
715 for (j = 0; j < get_union_n_members(lt); j++) {
716 entity *le = get_union_member(lt, j);
717 if (get_entity_name(le) == get_entity_name(se))
721 for (i = 0; i < get_union_n_members(st); i++) {
722 if (!m[i] || /* Found no counterpart */
723 !smaller_type(get_entity_type(get_union_member(st, i)),
724 get_entity_type(m[i])))
729 type *set, *let; /* small/large elt. type */
730 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
732 set = get_array_element_type(st);
733 let = get_array_element_type(lt);
735 /* If the elt types are different, set must be convertible
736 to let, and they must have the same size so that address
737 computations work out. To have a size the layout must
739 if ((get_type_state(set) != layout_fixed) ||
740 (get_type_state(let) != layout_fixed))
742 if (!smaller_type(set, let) ||
743 get_type_size_bits(set) != get_type_size_bits(let))
746 for(i = 0; i < get_array_n_dimensions(st); i++) {
747 if (get_array_lower_bound(lt, i))
748 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
750 if (get_array_upper_bound(lt, i))
751 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
755 case tpo_enumeration: {
756 assert(0 && "enumerations not implemented");
759 if (!smaller_type(get_pointer_points_to_type(st),
760 get_pointer_points_to_type(lt)))
763 case tpo_primitive: {
764 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
772 /*-----------------------------------------------------------------*/
774 /*-----------------------------------------------------------------*/
776 /* create a new class type */
777 type *new_d_type_class (ident *name, dbg_info *db) {
780 res = new_type(type_class, NULL, name, db);
782 res->attr.ca.members = NEW_ARR_F (entity *, 0);
783 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
784 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
785 res->attr.ca.peculiarity = peculiarity_existent;
786 res->attr.ca.dfn = 0;
790 type *new_type_class (ident *name) {
791 return new_d_type_class (name, NULL);
794 void free_class_entities(type *clss) {
796 assert(clss && (clss->type_op == type_class));
797 for (i = get_class_n_members(clss)-1; i >= 0; --i)
798 free_entity(get_class_member(clss, i));
801 void free_class_attrs(type *clss) {
802 assert(clss && (clss->type_op == type_class));
803 DEL_ARR_F(clss->attr.ca.members);
804 DEL_ARR_F(clss->attr.ca.subtypes);
805 DEL_ARR_F(clss->attr.ca.supertypes);
808 /* manipulate private fields of class type */
809 void add_class_member (type *clss, entity *member) {
810 assert(clss && (clss->type_op == type_class));
811 assert(clss != get_entity_type(member) && "recursive type");
812 ARR_APP1 (entity *, clss->attr.ca.members, member);
815 int (get_class_n_members) (const type *clss) {
816 return _get_class_n_members(clss);
819 int get_class_member_index(type *clss, entity *mem) {
821 assert(clss && (clss->type_op == type_class));
822 for (i = 0; i < get_class_n_members(clss); i++)
823 if (get_class_member(clss, i) == mem)
828 entity *(get_class_member) (const type *clss, int pos) {
829 return _get_class_member(clss, pos);
832 entity *get_class_member_by_name(type *clss, ident *name) {
834 assert(clss && (clss->type_op == type_class));
835 n_mem = get_class_n_members(clss);
836 for (i = 0; i < n_mem; ++i) {
837 entity *mem = get_class_member(clss, i);
838 if (get_entity_ident(mem) == name) return mem;
843 void set_class_member (type *clss, entity *member, int pos) {
844 assert(clss && (clss->type_op == type_class));
845 assert(pos >= 0 && pos < get_class_n_members(clss));
846 clss->attr.ca.members[pos] = member;
848 void set_class_members (type *clss, entity **members, int arity) {
850 assert(clss && (clss->type_op == type_class));
851 DEL_ARR_F(clss->attr.ca.members);
852 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
853 for (i = 0; i < arity; i++) {
854 set_entity_owner(members[i], clss);
855 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
858 void remove_class_member(type *clss, entity *member) {
860 assert(clss && (clss->type_op == type_class));
861 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
862 if (clss->attr.ca.members[i] == member) {
863 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
864 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
865 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
871 void add_class_subtype (type *clss, type *subtype) {
873 assert(clss && (clss->type_op == type_class));
874 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
875 for (i = 0; i < get_class_n_supertypes(subtype); i++)
876 if (get_class_supertype(subtype, i) == clss)
877 /* Class already registered */
879 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
881 int get_class_n_subtypes (const type *clss) {
882 assert(clss && (clss->type_op == type_class));
883 return (ARR_LEN (clss->attr.ca.subtypes));
885 type *get_class_subtype (type *clss, int pos) {
886 assert(clss && (clss->type_op == type_class));
887 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
888 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
890 int get_class_subtype_index(type *clss, const type *subclass) {
891 int i, n_subtypes = get_class_n_subtypes(clss);
892 assert(is_Class_type(subclass));
893 for (i = 0; i < n_subtypes; ++i) {
894 if (get_class_subtype(clss, i) == subclass) return i;
898 void set_class_subtype (type *clss, type *subtype, int pos) {
899 assert(clss && (clss->type_op == type_class));
900 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
901 clss->attr.ca.subtypes[pos] = subtype;
903 void remove_class_subtype(type *clss, type *subtype) {
905 assert(clss && (clss->type_op == type_class));
906 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
907 if (clss->attr.ca.subtypes[i] == subtype) {
908 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
909 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
910 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
915 void add_class_supertype (type *clss, type *supertype) {
917 assert(clss && (clss->type_op == type_class));
918 assert(supertype && (supertype -> type_op == type_class));
919 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
920 for (i = 0; i < get_class_n_subtypes(supertype); i++)
921 if (get_class_subtype(supertype, i) == clss)
922 /* Class already registered */
924 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
926 int get_class_n_supertypes (const type *clss) {
927 assert(clss && (clss->type_op == type_class));
928 return (ARR_LEN (clss->attr.ca.supertypes));
930 int get_class_supertype_index(type *clss, type *super_clss) {
931 int i, n_supertypes = get_class_n_supertypes(clss);
932 assert(super_clss && (super_clss->type_op == type_class));
933 for (i = 0; i < n_supertypes; i++)
934 if (get_class_supertype(clss, i) == super_clss)
938 type *get_class_supertype (type *clss, int pos) {
939 assert(clss && (clss->type_op == type_class));
940 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
941 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
943 void set_class_supertype (type *clss, type *supertype, int pos) {
944 assert(clss && (clss->type_op == type_class));
945 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
946 clss->attr.ca.supertypes[pos] = supertype;
948 void remove_class_supertype(type *clss, type *supertype) {
950 assert(clss && (clss->type_op == type_class));
951 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
952 if (clss->attr.ca.supertypes[i] == supertype) {
953 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
954 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
955 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
960 const char *get_peculiarity_string(peculiarity p) {
961 #define X(a) case a: return #a
963 X(peculiarity_description);
964 X(peculiarity_inherited);
965 X(peculiarity_existent);
968 return "invalid peculiarity";
971 peculiarity get_class_peculiarity (const type *clss) {
972 assert(clss && (clss->type_op == type_class));
973 return clss->attr.ca.peculiarity;
976 void set_class_peculiarity (type *clss, peculiarity pec) {
977 assert(clss && (clss->type_op == type_class));
978 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
979 clss->attr.ca.peculiarity = pec;
982 void set_class_dfn (type *clss, int dfn)
984 clss->attr.ca.dfn = dfn;
987 int get_class_dfn (const type *clss)
989 return (clss->attr.ca.dfn);
993 int (is_Class_type)(const type *clss) {
994 return _is_class_type(clss);
997 /*----------------------------------------------------------------**/
999 /*----------------------------------------------------------------**/
1001 /* create a new type struct */
1002 type *new_d_type_struct(ident *name, dbg_info *db) {
1003 type *res = new_type(type_struct, NULL, name, db);
1005 res->attr.sa.members = NEW_ARR_F(entity *, 0);
1008 type *new_type_struct (ident *name) {
1009 return new_d_type_struct (name, NULL);
1012 void free_struct_entities (type *strct) {
1014 assert(strct && (strct->type_op == type_struct));
1015 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1016 free_entity(get_struct_member(strct, i));
1018 void free_struct_attrs (type *strct) {
1019 assert(strct && (strct->type_op == type_struct));
1020 DEL_ARR_F(strct->attr.sa.members);
1023 /* manipulate private fields of struct */
1024 int get_struct_n_members (const type *strct) {
1025 assert(strct && (strct->type_op == type_struct));
1026 return (ARR_LEN (strct->attr.sa.members));
1029 void add_struct_member (type *strct, entity *member) {
1030 assert(strct && (strct->type_op == type_struct));
1031 assert(get_type_tpop(get_entity_type(member)) != type_method);
1032 /* @@@ lowerfirm geht nicht durch */
1033 assert(strct != get_entity_type(member) && "recursive type");
1034 ARR_APP1 (entity *, strct->attr.sa.members, member);
1037 entity *get_struct_member (const type *strct, int pos) {
1038 assert(strct && (strct->type_op == type_struct));
1039 assert(pos >= 0 && pos < get_struct_n_members(strct));
1040 return strct->attr.sa.members[pos];
1043 int get_struct_member_index(type *strct, entity *mem) {
1045 assert(strct && (strct->type_op == type_struct));
1046 for (i = 0; i < get_struct_n_members(strct); i++)
1047 if (get_struct_member(strct, i) == mem)
1052 void set_struct_member (type *strct, int pos, entity *member) {
1053 assert(strct && (strct->type_op == type_struct));
1054 assert(pos >= 0 && pos < get_struct_n_members(strct));
1055 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1056 strct->attr.sa.members[pos] = member;
1058 void remove_struct_member(type *strct, entity *member) {
1060 assert(strct && (strct->type_op == type_struct));
1061 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1062 if (strct->attr.sa.members[i] == member) {
1063 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1064 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1065 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1071 int (is_Struct_type)(const type *strct) {
1072 return _is_struct_type(strct);
1075 /*******************************************************************/
1077 /*******************************************************************/
1080 * Lazy construction of value argument / result representation.
1081 * Constructs a struct type and its member. The types of the members
1082 * are passed in the argument list.
1084 * @param name name of the type constructed
1085 * @param len number of fields
1086 * @param tps array of field types with length len
1088 static INLINE type *
1089 build_value_type(ident *name, int len, type **tps) {
1091 type *res = new_type_struct(name);
1092 /* Remove type from type list. Must be treated differently than other types. */
1093 remove_irp_type(res);
1094 for (i = 0; i < len; i++) {
1095 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1096 if (tps[i]) elt_type = tps[i];
1097 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1102 /* Create a new method type.
1103 N_param is the number of parameters, n_res the number of results. */
1104 type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1107 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1108 res = new_type(type_method, mode_P_code, name, db);
1109 res->state = layout_fixed;
1110 res->size = get_mode_size_bits(mode_P_code);
1111 res->attr.ma.n_params = n_param;
1112 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1113 res->attr.ma.value_params = NULL;
1114 res->attr.ma.n_res = n_res;
1115 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1116 res->attr.ma.value_ress = NULL;
1117 res->attr.ma.variadicity = variadicity_non_variadic;
1118 res->attr.ma.first_variadic_param = -1;
1123 type *new_type_method(ident *name, int n_param, int n_res) {
1124 return new_d_type_method(name, n_param, n_res, NULL);
1127 void free_method_entities(type *method) {
1128 assert(method && (method->type_op == type_method));
1131 /* Attention: also frees entities in value parameter subtypes! */
1132 void free_method_attrs(type *method) {
1133 assert(method && (method->type_op == type_method));
1134 free(method->attr.ma.param_type);
1135 free(method->attr.ma.res_type);
1136 if (method->attr.ma.value_params) {
1137 free_type_entities(method->attr.ma.value_params);
1138 free_type(method->attr.ma.value_params);
1140 if (method->attr.ma.value_ress) {
1141 free_type_entities(method->attr.ma.value_ress);
1142 free_type(method->attr.ma.value_ress);
1146 /* manipulate private fields of method. */
1147 int get_method_n_params (const type *method) {
1148 assert(method && (method->type_op == type_method));
1149 return method->attr.ma.n_params;
1152 type *get_method_param_type(type *method, int pos) {
1154 assert(method && (method->type_op == type_method));
1155 assert(pos >= 0 && pos < get_method_n_params(method));
1156 res = method->attr.ma.param_type[pos];
1157 assert(res != NULL && "empty method param type");
1158 return method->attr.ma.param_type[pos] = skip_tid(res);
1161 void set_method_param_type(type *method, int pos, type* tp) {
1162 assert(method && (method->type_op == type_method));
1163 assert(pos >= 0 && pos < get_method_n_params(method));
1164 method->attr.ma.param_type[pos] = tp;
1165 /* If information constructed set pass-by-value representation. */
1166 if (method->attr.ma.value_params) {
1167 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1168 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1172 /* Returns an entity that represents the copied value argument. Only necessary
1173 for compounds passed by value. */
1174 entity *get_method_value_param_ent(type *method, int pos) {
1175 assert(method && (method->type_op == type_method));
1176 assert(pos >= 0 && pos < get_method_n_params(method));
1177 if (!method->attr.ma.value_params)
1178 method->attr.ma.value_params
1179 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1180 get_method_n_params(method), method->attr.ma.param_type);
1181 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1182 != method->attr.ma.value_params)
1183 && "param type not yet set");
1184 return get_struct_member(method->attr.ma.value_params, pos);
1188 * Returns a type that represents the copied value arguments.
1190 type *get_method_value_param_type(const type *method)
1192 assert(method && (method->type_op == type_method));
1193 return method->attr.ma.value_params;
1196 int get_method_n_ress (const type *method) {
1197 assert(method && (method->type_op == type_method));
1198 return method->attr.ma.n_res;
1201 type *get_method_res_type(type *method, int pos) {
1203 assert(method && (method->type_op == type_method));
1204 assert(pos >= 0 && pos < get_method_n_ress(method));
1205 res = method->attr.ma.res_type[pos];
1206 assert(res != NULL && "empty method return type");
1207 return method->attr.ma.res_type[pos] = skip_tid(res);
1210 void set_method_res_type(type *method, int pos, type* tp) {
1211 assert(method && (method->type_op == type_method));
1212 assert(pos >= 0 && pos < get_method_n_ress(method));
1213 /* set the result type */
1214 method->attr.ma.res_type[pos] = tp;
1215 /* If information constructed set pass-by-value representation. */
1216 if (method->attr.ma.value_ress) {
1217 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1218 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1222 /* Returns an entity that represents the copied value result. Only necessary
1223 for compounds passed by value. */
1224 entity *get_method_value_res_ent(type *method, int pos) {
1225 assert(method && (method->type_op == type_method));
1226 assert(pos >= 0 && pos < get_method_n_ress(method));
1227 if (!method->attr.ma.value_ress)
1228 method->attr.ma.value_ress
1229 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1230 get_method_n_ress(method), method->attr.ma.res_type);
1231 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1232 && "result type not yet set");
1233 return get_struct_member(method->attr.ma.value_ress, pos);
1237 * Returns a type that represents the copied value results.
1239 type *get_method_value_res_type(const type *method) {
1240 assert(method && (method->type_op == type_method));
1241 return method->attr.ma.value_ress;
1244 /* Returns the null-terminated name of this variadicity. */
1245 const char *get_variadicity_name(variadicity vari)
1247 #define X(a) case a: return #a
1249 X(variadicity_non_variadic);
1250 X(variadicity_variadic);
1257 variadicity get_method_variadicity(const type *method)
1259 assert(method && (method->type_op == type_method));
1260 return method->attr.ma.variadicity;
1263 void set_method_variadicity(type *method, variadicity vari)
1265 assert(method && (method->type_op == type_method));
1266 method->attr.ma.variadicity = vari;
1270 * Returns the first variadic parameter index of a type.
1271 * If this index was NOT set, the index of the last parameter
1272 * of the method type plus one is returned for variadic functions.
1273 * Non-variadic function types always return -1 here.
1275 int get_method_first_variadic_param_index(const type *method)
1277 assert(method && (method->type_op == type_method));
1279 if (method->attr.ma.variadicity == variadicity_non_variadic)
1282 if (method->attr.ma.first_variadic_param == -1)
1283 return get_method_n_params(method);
1284 return method->attr.ma.first_variadic_param;
1288 * Sets the first variadic parameter index. This allows to specify
1289 * a complete call type (containing the type of all parameters)
1290 * but still have the knowledge, which parameter must be passed as
1293 void set_method_first_variadic_param_index(type *method, int index)
1295 assert(method && (method->type_op == type_method));
1296 assert(index >= 0 && index <= get_method_n_params(method));
1298 method->attr.ma.first_variadic_param = index;
1302 int (is_Method_type)(const type *method) {
1303 return _is_method_type(method);
1306 /*-----------------------------------------------------------------*/
1308 /*-----------------------------------------------------------------*/
1310 /* create a new type uni */
1311 type *new_d_type_union(ident *name, dbg_info *db) {
1312 type *res = new_type(type_union, NULL, name, db);
1314 res->attr.ua.members = NEW_ARR_F(entity *, 0);
1317 type *new_type_union(ident *name) {
1318 return new_d_type_union(name, NULL);
1320 void free_union_entities(type *uni) {
1322 assert(uni && (uni->type_op == type_union));
1323 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1324 free_entity(get_union_member(uni, i));
1326 void free_union_attrs (type *uni) {
1327 assert(uni && (uni->type_op == type_union));
1328 DEL_ARR_F(uni->attr.ua.members);
1330 /* manipulate private fields of union */
1331 int get_union_n_members (const type *uni) {
1332 assert(uni && (uni->type_op == type_union));
1333 return (ARR_LEN (uni->attr.ua.members));
1335 void add_union_member (type *uni, entity *member) {
1336 assert(uni && (uni->type_op == type_union));
1337 assert(uni != get_entity_type(member) && "recursive type");
1338 ARR_APP1 (entity *, uni->attr.ua.members, member);
1340 entity *get_union_member (const type *uni, int pos) {
1341 assert(uni && (uni->type_op == type_union));
1342 assert(pos >= 0 && pos < get_union_n_members(uni));
1343 return uni->attr.ua.members[pos];
1345 void set_union_member (type *uni, int pos, entity *member) {
1346 assert(uni && (uni->type_op == type_union));
1347 assert(pos >= 0 && pos < get_union_n_members(uni));
1348 uni->attr.ua.members[pos] = member;
1350 void remove_union_member(type *uni, entity *member) {
1352 assert(uni && (uni->type_op == type_union));
1353 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1354 if (uni->attr.ua.members[i] == member) {
1355 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1356 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1357 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1363 int (is_Union_type)(const type *uni) {
1364 return _is_union_type(uni);
1367 /*-----------------------------------------------------------------*/
1369 /*-----------------------------------------------------------------*/
1372 /* create a new type array -- set dimension sizes independently */
1373 type *new_d_type_array(ident *name, int n_dimensions, type *element_type, dbg_info *db) {
1377 ir_graph *rem = current_ir_graph;
1379 assert(!is_Method_type(element_type));
1381 res = new_type(type_array, NULL, name, db);
1382 res->attr.aa.n_dimensions = n_dimensions;
1383 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1384 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1385 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1387 current_ir_graph = get_const_code_irg();
1388 unk = new_Unknown( mode_Iu);
1389 for (i = 0; i < n_dimensions; i++) {
1390 res->attr.aa.lower_bound[i] =
1391 res->attr.aa.upper_bound[i] = unk;
1392 res->attr.aa.order[i] = i;
1394 current_ir_graph = rem;
1396 res->attr.aa.element_type = element_type;
1397 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1402 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1403 return new_d_type_array(name, n_dimensions, element_type, NULL);
1406 void free_array_entities (type *array) {
1407 assert(array && (array->type_op == type_array));
1410 void free_array_attrs (type *array) {
1411 assert(array && (array->type_op == type_array));
1412 free(array->attr.aa.lower_bound);
1413 free(array->attr.aa.upper_bound);
1416 /* manipulate private fields of array type */
1417 int get_array_n_dimensions (const type *array) {
1418 assert(array && (array->type_op == type_array));
1419 return array->attr.aa.n_dimensions;
1423 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1424 ir_node * upper_bound) {
1425 assert(array && (array->type_op == type_array));
1426 assert(lower_bound && "lower_bound node may not be NULL.");
1427 assert(upper_bound && "upper_bound node may not be NULL.");
1428 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1429 array->attr.aa.lower_bound[dimension] = lower_bound;
1430 array->attr.aa.upper_bound[dimension] = upper_bound;
1433 set_array_bounds_int (type *array, int dimension, int lower_bound,
1435 ir_graph *rem = current_ir_graph;
1436 current_ir_graph = get_const_code_irg();
1437 set_array_bounds (array, dimension,
1438 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1439 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1440 current_ir_graph = rem;
1443 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1444 assert(array && (array->type_op == type_array));
1445 assert(lower_bound && "lower_bound node may not be NULL.");
1446 array->attr.aa.lower_bound[dimension] = lower_bound;
1448 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1449 ir_graph *rem = current_ir_graph;
1450 current_ir_graph = get_const_code_irg();
1451 set_array_lower_bound (array, dimension,
1452 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1453 current_ir_graph = rem;
1456 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1457 assert(array && (array->type_op == type_array));
1458 assert(upper_bound && "upper_bound node may not be NULL.");
1459 array->attr.aa.upper_bound[dimension] = upper_bound;
1461 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1462 ir_graph *rem = current_ir_graph;
1463 current_ir_graph = get_const_code_irg();
1464 set_array_upper_bound (array, dimension,
1465 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1466 current_ir_graph = rem;
1468 int has_array_lower_bound (const type *array, int dimension) {
1469 assert(array && (array->type_op == type_array));
1470 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1472 ir_node *get_array_lower_bound (const type *array, int dimension) {
1473 assert(array && (array->type_op == type_array));
1474 return array->attr.aa.lower_bound[dimension];
1476 long get_array_lower_bound_int (const type *array, int dimension) {
1478 assert(array && (array->type_op == type_array));
1479 node = array->attr.aa.lower_bound[dimension];
1480 assert(get_irn_op(node) == op_Const);
1481 return get_tarval_long(get_Const_tarval(node));
1483 int has_array_upper_bound (const type *array, int dimension) {
1484 assert(array && (array->type_op == type_array));
1485 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1487 ir_node * get_array_upper_bound (const type *array, int dimension) {
1488 assert(array && (array->type_op == type_array));
1489 return array->attr.aa.upper_bound[dimension];
1491 long get_array_upper_bound_int (const type *array, int dimension) {
1493 assert(array && (array->type_op == type_array));
1494 node = array->attr.aa.upper_bound[dimension];
1495 assert(get_irn_op(node) == op_Const);
1496 return get_tarval_long(get_Const_tarval(node));
1499 void set_array_order (type *array, int dimension, int order) {
1500 assert(array && (array->type_op == type_array));
1501 array->attr.aa.order[dimension] = order;
1504 int get_array_order (const type *array, int dimension) {
1505 assert(array && (array->type_op == type_array));
1506 return array->attr.aa.order[dimension];
1509 int find_array_dimension(const type *array, int order) {
1512 assert(array && (array->type_op == type_array));
1514 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1515 if (array->attr.aa.order[dim] == order)
1521 void set_array_element_type (type *array, type *tp) {
1522 assert(array && (array->type_op == type_array));
1523 assert(!is_Method_type(tp));
1524 array->attr.aa.element_type = tp;
1526 type *get_array_element_type (type *array) {
1527 assert(array && (array->type_op == type_array));
1528 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1531 void set_array_element_entity (type *array, entity *ent) {
1532 assert(array && (array->type_op == type_array));
1533 assert((get_entity_type(ent)->type_op != type_method));
1534 array->attr.aa.element_ent = ent;
1535 array->attr.aa.element_type = get_entity_type(ent);
1537 entity *get_array_element_entity (const type *array) {
1538 assert(array && (array->type_op == type_array));
1539 return array->attr.aa.element_ent;
1543 int (is_Array_type)(const type *array) {
1544 return _is_array_type(array);
1547 /*-----------------------------------------------------------------*/
1548 /* TYPE_ENUMERATION */
1549 /*-----------------------------------------------------------------*/
1551 /* create a new type enumeration -- set the enumerators independently */
1552 type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1553 type *res = new_type(type_enumeration, NULL, name, db);
1555 res->attr.ea.n_enums = n_enums;
1556 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1557 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1560 type *new_type_enumeration(ident *name, int n_enums) {
1561 return new_d_type_enumeration(name, n_enums, NULL);
1564 void free_enumeration_entities(type *enumeration) {
1565 assert(enumeration && (enumeration->type_op == type_enumeration));
1567 void free_enumeration_attrs(type *enumeration) {
1568 assert(enumeration && (enumeration->type_op == type_enumeration));
1569 free(enumeration->attr.ea.enumer);
1570 free(enumeration->attr.ea.enum_nameid);
1573 /* manipulate fields of enumeration type. */
1574 int get_enumeration_n_enums (const type *enumeration) {
1575 assert(enumeration && (enumeration->type_op == type_enumeration));
1576 return enumeration->attr.ea.n_enums;
1578 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1579 assert(enumeration && (enumeration->type_op == type_enumeration));
1580 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1581 enumeration->attr.ea.enumer[pos] = con;
1583 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1584 assert(enumeration && (enumeration->type_op == type_enumeration));
1585 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1586 return enumeration->attr.ea.enumer[pos];
1588 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1589 assert(enumeration && (enumeration->type_op == type_enumeration));
1590 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1591 enumeration->attr.ea.enum_nameid[pos] = id;
1593 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1594 assert(enumeration && (enumeration->type_op == type_enumeration));
1595 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1596 return enumeration->attr.ea.enum_nameid[pos];
1598 const char *get_enumeration_name(const type *enumeration, int pos) {
1599 assert(enumeration && (enumeration->type_op == type_enumeration));
1600 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1601 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1605 int (is_Enumeration_type)(const type *enumeration) {
1606 return _is_enumeration_type(enumeration);
1609 /*-----------------------------------------------------------------*/
1611 /*-----------------------------------------------------------------*/
1613 /* Create a new type pointer */
1614 type *new_d_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1617 assert(mode_is_reference(ptr_mode));
1618 res = new_type(type_pointer, ptr_mode, name, db);
1619 res->attr.pa.points_to = points_to;
1620 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1621 res->size = get_mode_size_bits(res->mode);
1622 res->state = layout_fixed;
1625 type *new_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode) {
1626 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1628 void free_pointer_entities (type *pointer) {
1629 assert(pointer && (pointer->type_op == type_pointer));
1631 void free_pointer_attrs (type *pointer) {
1632 assert(pointer && (pointer->type_op == type_pointer));
1634 /* manipulate fields of type_pointer */
1635 void set_pointer_points_to_type (type *pointer, type *tp) {
1636 assert(pointer && (pointer->type_op == type_pointer));
1637 pointer->attr.pa.points_to = tp;
1639 type *get_pointer_points_to_type (type *pointer) {
1640 assert(pointer && (pointer->type_op == type_pointer));
1641 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1645 int (is_Pointer_type)(const type *pointer) {
1646 return _is_pointer_type(pointer);
1649 /* Returns the first pointer type that has as points_to tp.
1650 * Not efficient: O(#types).
1651 * If not found returns firm_unknown_type. */
1652 type *find_pointer_type_to_type (type *tp) {
1654 for (i = 0; i < get_irp_n_types(); ++i) {
1655 type *found = get_irp_type(i);
1656 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1659 return firm_unknown_type;
1664 /*-----------------------------------------------------------------*/
1665 /* TYPE_PRIMITIVE */
1666 /*-----------------------------------------------------------------*/
1668 /* create a new type primitive */
1669 type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1671 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1672 res = new_type(type_primitive, mode, name, db);
1673 res->size = get_mode_size_bits(mode);
1674 res->state = layout_fixed;
1677 type *new_type_primitive(ident *name, ir_mode *mode) {
1678 return new_d_type_primitive(name, mode, NULL);
1680 void free_primitive_entities (type *primitive) {
1681 assert(primitive && (primitive->type_op == type_primitive));
1683 void free_primitive_attrs (type *primitive) {
1684 assert(primitive && (primitive->type_op == type_primitive));
1688 int (is_Primitive_type)(const type *primitive) {
1689 return _is_primitive_type(primitive);
1692 /*-----------------------------------------------------------------*/
1693 /* common functionality */
1694 /*-----------------------------------------------------------------*/
1697 int (is_atomic_type)(const type *tp) {
1698 return _is_atomic_type(tp);
1702 * Gets the number of elements in a firm compound type.
1704 int get_compound_n_members(const type *tp)
1708 if (is_Struct_type(tp))
1709 res = get_struct_n_members(tp);
1710 else if (is_Class_type(tp))
1711 res = get_class_n_members(tp);
1712 else if (is_Union_type(tp))
1713 res = get_union_n_members(tp);
1715 assert(0 && "need struct, union or class for member count");
1721 * Gets the member of a firm compound type at position pos.
1723 entity *get_compound_member(const type *tp, int pos)
1727 if (is_Struct_type(tp))
1728 res = get_struct_member(tp, pos);
1729 else if (is_Class_type(tp))
1730 res = get_class_member(tp, pos);
1731 else if (is_Union_type(tp))
1732 res = get_union_member(tp, pos);
1735 assert(0 && "need struct, union or class to get a member");
1742 int is_compound_type(const type *tp) {
1743 assert(tp && tp->kind == k_type);
1744 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1747 /* Checks, whether a type is a frame type */
1748 int is_frame_type(const type *tp) {
1749 return tp->frame_type;
1752 /* Makes a new frame type. */
1753 type *new_type_frame(ident *name)
1755 type *res = new_type_class(name);
1757 res->frame_type = 1;
1759 /* Remove type from type list. Must be treated differently than other types. */
1760 remove_irp_type(res);