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 void init_type(void) {
88 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
89 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
91 /* construct none and unknown type. */
92 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
93 set_type_size_bits(firm_none_type, 0);
94 set_type_state (firm_none_type, layout_fixed);
95 remove_irp_type(firm_none_type);
96 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
97 set_type_size_bits(firm_unknown_type, 0);
98 set_type_state (firm_unknown_type, layout_fixed);
99 remove_irp_type(firm_unknown_type);
102 unsigned long type_visited;
104 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
105 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
106 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
110 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
114 assert(type_op != type_id);
115 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
117 node_size = offsetof(type, attr) + type_op->attr_size;
118 res = xmalloc(node_size);
119 memset(res, 0, node_size);
122 res->type_op = type_op;
125 res->visibility = visibility_external_allocated;
127 res->state = layout_undefined;
133 res->nr = get_irp_new_node_nr();
134 #endif /* defined DEBUG_libfirm */
136 add_irp_type(res); /* Remember the new type global. */
141 void free_type(type *tp) {
142 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
144 /* Remove from list of all types */
146 /* Free the attributes of the type. */
148 /* Free entities automatically allocated with the type */
149 if (is_Array_type(tp))
150 free_entity(get_array_element_entity(tp));
151 /* And now the type itself... */
156 void free_type_entities(type *tp) {
157 switch(get_type_tpop_code(tp)) {
158 case tpo_class: { free_class_entities(tp); } break;
159 case tpo_struct: { free_struct_entities(tp); } break;
160 case tpo_method: { free_method_entities(tp); } break;
161 case tpo_union: { free_union_entities(tp); } break;
162 case tpo_array: { free_array_entities(tp); } break;
163 case tpo_enumeration: { free_enumeration_entities(tp); } break;
164 case tpo_pointer: { free_pointer_entities(tp); } break;
165 case tpo_primitive: { free_primitive_entities(tp); } break;
170 void free_type_attrs(type *tp) {
171 switch(get_type_tpop_code(tp)) {
172 case tpo_class: { free_class_attrs(tp); } break;
173 case tpo_struct: { free_struct_attrs(tp); } break;
174 case tpo_method: { free_method_attrs(tp); } break;
175 case tpo_union: { free_union_attrs(tp); } break;
176 case tpo_array: { free_array_attrs(tp); } break;
177 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
178 case tpo_pointer: { free_pointer_attrs(tp); } break;
179 case tpo_primitive: { free_primitive_attrs(tp); } break;
184 /* set/get the link field */
185 void *(get_type_link)(const type *tp)
187 return _get_type_link(tp);
190 void (set_type_link)(type *tp, void *l)
192 _set_type_link(tp, l);
195 const tp_op *(get_type_tpop)(const type *tp) {
196 return _get_type_tpop(tp);
199 ident *(get_type_tpop_nameid)(const type *tp) {
200 return _get_type_tpop_nameid(tp);
203 const char* get_type_tpop_name(const type *tp) {
204 assert(tp && tp->kind == k_type);
205 return get_id_str(tp->type_op->name);
208 tp_opcode (get_type_tpop_code)(const type *tp) {
209 return _get_type_tpop_code(tp);
212 ir_mode *(get_type_mode)(const type *tp) {
213 return _get_type_mode(tp);
216 void set_type_mode(type *tp, ir_mode* m) {
217 assert(tp && tp->kind == k_type);
219 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
220 /* Modes of primitives must be data */
221 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
222 /* Modes of enumerations must be integers */
223 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
224 /* Modes of pointers must be references. */
226 switch (get_type_tpop_code(tp)) {
228 /* For primitive size depends on the mode. */
229 tp->size = get_mode_size_bits(m);
232 case tpo_enumeration:
234 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
235 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
236 tp->size = get_mode_size_bits(m);
241 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
242 assert(get_type_state(tp) == layout_fixed &&
243 tp->size == get_mode_size_bits(m) &&
244 "mode don't match struct/class layout");
248 assert(0 && "setting a mode is NOT allowed for this type");
252 ident *(get_type_ident)(const type *tp) {
253 return _get_type_ident(tp);
256 void (set_type_ident)(type *tp, ident* id) {
257 _set_type_ident(tp, id);
260 /* Outputs a unique number for this node */
261 long get_type_nr(const type *tp) {
270 const char* get_type_name(const type *tp) {
271 assert(tp && tp->kind == k_type);
272 return (get_id_str(tp->name));
275 int (get_type_size_bytes)(const type *tp) {
276 return _get_type_size_bytes(tp);
279 int (get_type_size_bits)(const type *tp) {
280 return _get_type_size_bits(tp);
284 visibility get_type_visibility (const type *tp) {
286 visibility res = visibility_local;
287 if (is_compound_type(tp)) {
289 if (is_Array_type(tp)) {
290 entity *mem = get_array_element_entity(tp);
291 if (get_entity_visibility(mem) != visibility_local)
292 res = visibility_external_visible;
294 int i, n_mems = get_compound_n_members(tp);
295 for (i = 0; i < n_mems; ++i) {
296 entity *mem = get_compound_member(tp, i);
297 if (get_entity_visibility(mem) != visibility_local)
298 res = visibility_external_visible;
305 return tp->visibility;
308 void set_type_visibility (type *tp, visibility v) {
311 /* check for correctness */
312 if (v != visibility_external_allocated) {
313 visibility res = visibility_local;
314 if (is_compound_type(tp)) {
315 if (is_Array_type(tp)) {
316 entity *mem = get_array_element_entity(tp);
317 if (get_entity_visibility(mem) > res)
318 res = get_entity_visibility(mem);
320 int i, n_mems = get_compound_n_members(tp);
321 for (i = 0; i < n_mems; ++i) {
322 entity *mem = get_compound_member(tp, i);
323 if (get_entity_visibility(mem) > res)
324 res = get_entity_visibility(mem);
335 set_type_size_bits(type *tp, int size) {
336 assert(tp && tp->kind == k_type);
337 /* For pointer enumeration and primitive size depends on the mode.
338 Methods don't have a size. */
339 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
340 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
341 if (tp->type_op == type_primitive)
344 /* argh: we must allow to set negative values as "invalid size" */
345 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
346 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
352 set_type_size_bytes(type *tp, int size) {
353 set_type_size_bits(tp, 8*size);
356 int get_type_alignment_bytes(type *tp) {
357 int align = get_type_alignment_bits(tp);
359 return align < 0 ? align : (align + 7) >> 3;
362 int get_type_alignment_bits(type *tp) {
368 /* alignment NOT set calculate it "on demand" */
370 align = get_mode_size_bits(tp->mode);
371 else if (is_Array_type(tp))
372 align = get_type_alignment_bits(get_array_element_type(tp));
373 else if (is_compound_type(tp)) {
374 int i, n = get_compound_n_members(tp);
377 for (i = 0; i < n; ++i) {
378 type *t = get_entity_type(get_compound_member(tp, i));
379 int a = get_type_alignment_bits(t);
385 else if (is_Method_type(tp))
395 set_type_alignment_bits(type *tp, int align) {
396 assert(tp && tp->kind == k_type);
397 /* Methods don't have an alignment. */
398 if (tp->type_op != type_method) {
404 set_type_alignment_bytes(type *tp, int align) {
405 set_type_size_bits(tp, 8*align);
408 /* Returns a human readable string for the enum entry. */
409 const char *get_type_state_name(type_state s) {
410 #define X(a) case a: return #a;
420 type_state (get_type_state)(const type *tp) {
421 return _get_type_state(tp);
425 set_type_state(type *tp, type_state state) {
426 assert(tp && tp->kind == k_type);
428 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
429 (tp->type_op == type_method))
432 /* Just a correctness check: */
433 if (state == layout_fixed) {
435 switch (get_type_tpop_code(tp)) {
438 assert(get_type_size_bits(tp) > -1);
439 if (tp != get_glob_type()) {
440 int n_mem = get_class_n_members(tp);
441 for (i = 0; i < n_mem; i++) {
442 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
443 { DDMT(tp); DDME(get_class_member(tp, i)); }
444 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
446 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
447 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
454 assert(get_type_size_bits(tp) > -1);
455 for (i = 0; i < get_struct_n_members(tp); i++) {
456 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
457 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
466 Assure that only innermost dimension is dynamic? */
468 case tpo_enumeration:
470 assert(get_type_mode != NULL);
471 for (i = 0; i < get_enumeration_n_enums(tp); i++)
472 assert(get_enumeration_enum(tp, i) != NULL);
480 unsigned long (get_type_visited)(const type *tp) {
481 return _get_type_visited(tp);
484 void (set_type_visited)(type *tp, unsigned long num) {
485 _set_type_visited(tp, num);
488 /* Sets visited field in type to type_visited. */
489 void (mark_type_visited)(type *tp) {
490 _mark_type_visited(tp);
493 /* @@@ name clash with master flag
494 int (type_visited)(const type *tp) {
495 return _type_visited(tp);
498 int (type_not_visited)(const type *tp) {
499 return _type_not_visited(tp);
502 int (is_type)(const void *thing) {
503 return _is_type(thing);
506 /* Checks whether two types are structural equal.*/
507 int equal_type(type *typ1, type *typ2) {
512 if (typ1 == typ2) return 1;
514 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
515 (get_type_ident(typ1) != get_type_ident(typ2)) ||
516 (get_type_mode(typ1) != get_type_mode(typ2)) ||
517 (get_type_state(typ1) != get_type_state(typ2)))
519 if ((get_type_state(typ1) == layout_fixed) &&
520 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
523 switch(get_type_tpop_code(typ1)) {
525 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
526 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
527 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
528 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
529 /** Compare the members **/
530 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
531 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
532 /* First sort the members of typ2 */
533 for (i = 0; i < get_class_n_members(typ1); i++) {
534 entity *e1 = get_class_member(typ1, i);
535 for (j = 0; j < get_class_n_members(typ2); j++) {
536 entity *e2 = get_class_member(typ2, j);
537 if (get_entity_name(e1) == get_entity_name(e2))
541 for (i = 0; i < get_class_n_members(typ1); i++) {
542 if (!m[i] || /* Found no counterpart */
543 !equal_entity(get_class_member(typ1, i), m[i]))
546 /** Compare the supertypes **/
547 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
548 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
549 /* First sort the supertypes of typ2 */
550 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
551 type *t1 = get_class_supertype(typ1, i);
552 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
553 type *t2 = get_class_supertype(typ2, j);
554 if (get_type_ident(t2) == get_type_ident(t1))
558 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
559 if (!t[i] || /* Found no counterpart */
560 get_class_supertype(typ1, i) != t[i])
565 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
566 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
567 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
568 /* First sort the members of lt */
569 for (i = 0; i < get_struct_n_members(typ1); i++) {
570 entity *e1 = get_struct_member(typ1, i);
571 for (j = 0; j < get_struct_n_members(typ2); j++) {
572 entity *e2 = get_struct_member(typ2, j);
573 if (get_entity_name(e1) == get_entity_name(e2))
577 for (i = 0; i < get_struct_n_members(typ1); i++) {
578 if (!m[i] || /* Found no counterpart */
579 !equal_entity(get_struct_member(typ1, i), m[i]))
584 int n_param1, n_param2;
586 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
587 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
589 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
590 n_param1 = get_method_n_params(typ1);
591 n_param2 = get_method_n_params(typ2);
594 n_param1 = get_method_first_variadic_param_index(typ1);
595 n_param2 = get_method_first_variadic_param_index(typ2);
598 if (n_param1 != n_param2) return 0;
600 for (i = 0; i < n_param1; i++) {
601 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
604 for (i = 0; i < get_method_n_ress(typ1); i++) {
605 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
610 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
611 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
612 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
613 /* First sort the members of lt */
614 for (i = 0; i < get_union_n_members(typ1); i++) {
615 entity *e1 = get_union_member(typ1, i);
616 for (j = 0; j < get_union_n_members(typ2); j++) {
617 entity *e2 = get_union_member(typ2, j);
618 if (get_entity_name(e1) == get_entity_name(e2))
622 for (i = 0; i < get_union_n_members(typ1); i++) {
623 if (!m[i] || /* Found no counterpart */
624 !equal_entity(get_union_member(typ1, i), m[i]))
629 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
631 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
633 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
634 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
635 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
637 if (get_array_order(typ1, i) != get_array_order(typ2, i))
638 assert(0 && "type compare with different dimension orders not implemented");
641 case tpo_enumeration: {
642 assert(0 && "enumerations not implemented");
645 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
648 case tpo_primitive: {
655 /* Checks whether two types are structural comparable. */
656 int smaller_type (type *st, type *lt) {
660 if (st == lt) return 1;
662 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
665 switch(get_type_tpop_code(st)) {
667 return is_subclass_of(st, lt);
670 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
671 m = alloca(sizeof(entity *) * get_struct_n_members(st));
672 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
673 /* First sort the members of lt */
674 for (i = 0; i < get_struct_n_members(st); i++) {
675 entity *se = get_struct_member(st, i);
676 for (j = 0; j < get_struct_n_members(lt); j++) {
677 entity *le = get_struct_member(lt, j);
678 if (get_entity_name(le) == get_entity_name(se))
682 for (i = 0; i < get_struct_n_members(st); i++) {
683 if (!m[i] || /* Found no counterpart */
684 !smaller_type(get_entity_type(get_struct_member(st, i)),
685 get_entity_type(m[i])))
690 /** FIXME: is this still 1? */
691 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
692 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
693 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
694 for (i = 0; i < get_method_n_params(st); i++) {
695 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
698 for (i = 0; i < get_method_n_ress(st); i++) {
699 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
704 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
705 m = alloca(sizeof(entity *) * get_union_n_members(st));
706 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
707 /* First sort the members of lt */
708 for (i = 0; i < get_union_n_members(st); i++) {
709 entity *se = get_union_member(st, i);
710 for (j = 0; j < get_union_n_members(lt); j++) {
711 entity *le = get_union_member(lt, j);
712 if (get_entity_name(le) == get_entity_name(se))
716 for (i = 0; i < get_union_n_members(st); i++) {
717 if (!m[i] || /* Found no counterpart */
718 !smaller_type(get_entity_type(get_union_member(st, i)),
719 get_entity_type(m[i])))
724 type *set, *let; /* small/large elt. type */
725 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
727 set = get_array_element_type(st);
728 let = get_array_element_type(lt);
730 /* If the elt types are different, set must be convertible
731 to let, and they must have the same size so that address
732 computations work out. To have a size the layout must
734 if ((get_type_state(set) != layout_fixed) ||
735 (get_type_state(let) != layout_fixed))
737 if (!smaller_type(set, let) ||
738 get_type_size_bits(set) != get_type_size_bits(let))
741 for(i = 0; i < get_array_n_dimensions(st); i++) {
742 if (get_array_lower_bound(lt, i))
743 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
745 if (get_array_upper_bound(lt, i))
746 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
750 case tpo_enumeration: {
751 assert(0 && "enumerations not implemented");
754 if (!smaller_type(get_pointer_points_to_type(st),
755 get_pointer_points_to_type(lt)))
758 case tpo_primitive: {
759 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
767 /*-----------------------------------------------------------------*/
769 /*-----------------------------------------------------------------*/
771 /* create a new class type */
772 type *new_type_class (ident *name) {
775 res = new_type(type_class, NULL, name);
777 res->attr.ca.members = NEW_ARR_F (entity *, 0);
778 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
779 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
780 res->attr.ca.peculiarity = peculiarity_existent;
781 res->attr.ca.dfn = 0;
785 type *new_d_type_class (ident *name, dbg_info* db) {
786 type *res = new_type_class (name);
787 set_type_dbg_info(res, db);
791 void free_class_entities(type *clss) {
793 assert(clss && (clss->type_op == type_class));
794 for (i = get_class_n_members(clss)-1; i >= 0; --i)
795 free_entity(get_class_member(clss, i));
798 void free_class_attrs(type *clss) {
799 assert(clss && (clss->type_op == type_class));
800 DEL_ARR_F(clss->attr.ca.members);
801 DEL_ARR_F(clss->attr.ca.subtypes);
802 DEL_ARR_F(clss->attr.ca.supertypes);
805 /* manipulate private fields of class type */
806 void add_class_member (type *clss, entity *member) {
807 assert(clss && (clss->type_op == type_class));
808 assert(clss != get_entity_type(member) && "recursive type");
809 ARR_APP1 (entity *, clss->attr.ca.members, member);
812 int (get_class_n_members) (const type *clss) {
813 return _get_class_n_members(clss);
816 int get_class_member_index(type *clss, entity *mem) {
818 assert(clss && (clss->type_op == type_class));
819 for (i = 0; i < get_class_n_members(clss); i++)
820 if (get_class_member(clss, i) == mem)
825 entity *(get_class_member) (const type *clss, int pos) {
826 return _get_class_member(clss, pos);
829 entity *get_class_member_by_name(type *clss, ident *name) {
831 assert(clss && (clss->type_op == type_class));
832 n_mem = get_class_n_members(clss);
833 for (i = 0; i < n_mem; ++i) {
834 entity *mem = get_class_member(clss, i);
835 if (get_entity_ident(mem) == name) return mem;
840 void set_class_member (type *clss, entity *member, int pos) {
841 assert(clss && (clss->type_op == type_class));
842 assert(pos >= 0 && pos < get_class_n_members(clss));
843 clss->attr.ca.members[pos] = member;
845 void set_class_members (type *clss, entity **members, int arity) {
847 assert(clss && (clss->type_op == type_class));
848 DEL_ARR_F(clss->attr.ca.members);
849 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
850 for (i = 0; i < arity; i++) {
851 set_entity_owner(members[i], clss);
852 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
855 void remove_class_member(type *clss, entity *member) {
857 assert(clss && (clss->type_op == type_class));
858 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
859 if (clss->attr.ca.members[i] == member) {
860 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
861 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
862 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
868 void add_class_subtype (type *clss, type *subtype) {
870 assert(clss && (clss->type_op == type_class));
871 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
872 for (i = 0; i < get_class_n_supertypes(subtype); i++)
873 if (get_class_supertype(subtype, i) == clss)
874 /* Class already registered */
876 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
878 int get_class_n_subtypes (const type *clss) {
879 assert(clss && (clss->type_op == type_class));
880 return (ARR_LEN (clss->attr.ca.subtypes));
882 type *get_class_subtype (type *clss, int pos) {
883 assert(clss && (clss->type_op == type_class));
884 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
885 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
887 int get_class_subtype_index(type *clss, const type *subclass) {
888 int i, n_subtypes = get_class_n_subtypes(clss);
889 assert(is_Class_type(subclass));
890 for (i = 0; i < n_subtypes; ++i) {
891 if (get_class_subtype(clss, i) == subclass) return i;
895 void set_class_subtype (type *clss, type *subtype, int pos) {
896 assert(clss && (clss->type_op == type_class));
897 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
898 clss->attr.ca.subtypes[pos] = subtype;
900 void remove_class_subtype(type *clss, type *subtype) {
902 assert(clss && (clss->type_op == type_class));
903 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
904 if (clss->attr.ca.subtypes[i] == subtype) {
905 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
906 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
907 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
912 void add_class_supertype (type *clss, type *supertype) {
914 assert(clss && (clss->type_op == type_class));
915 assert(supertype && (supertype -> type_op == type_class));
916 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
917 for (i = 0; i < get_class_n_subtypes(supertype); i++)
918 if (get_class_subtype(supertype, i) == clss)
919 /* Class already registered */
921 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
923 int get_class_n_supertypes (const type *clss) {
924 assert(clss && (clss->type_op == type_class));
925 return (ARR_LEN (clss->attr.ca.supertypes));
927 int get_class_supertype_index(type *clss, type *super_clss) {
928 int i, n_supertypes = get_class_n_supertypes(clss);
929 assert(super_clss && (super_clss->type_op == type_class));
930 for (i = 0; i < n_supertypes; i++)
931 if (get_class_supertype(clss, i) == super_clss)
935 type *get_class_supertype (type *clss, int pos) {
936 assert(clss && (clss->type_op == type_class));
937 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
938 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
940 void set_class_supertype (type *clss, type *supertype, int pos) {
941 assert(clss && (clss->type_op == type_class));
942 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
943 clss->attr.ca.supertypes[pos] = supertype;
945 void remove_class_supertype(type *clss, type *supertype) {
947 assert(clss && (clss->type_op == type_class));
948 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
949 if (clss->attr.ca.supertypes[i] == supertype) {
950 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
951 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
952 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
957 const char *get_peculiarity_string(peculiarity p) {
958 #define X(a) case a: return #a
960 X(peculiarity_description);
961 X(peculiarity_inherited);
962 X(peculiarity_existent);
965 return "invalid peculiarity";
968 peculiarity get_class_peculiarity (const type *clss) {
969 assert(clss && (clss->type_op == type_class));
970 return clss->attr.ca.peculiarity;
973 void set_class_peculiarity (type *clss, peculiarity pec) {
974 assert(clss && (clss->type_op == type_class));
975 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
976 clss->attr.ca.peculiarity = pec;
979 void set_class_dfn (type *clss, int dfn)
981 clss->attr.ca.dfn = dfn;
984 int get_class_dfn (const type *clss)
986 return (clss->attr.ca.dfn);
990 int (is_Class_type)(const type *clss) {
991 return _is_class_type(clss);
994 /*----------------------------------------------------------------**/
996 /*----------------------------------------------------------------**/
998 /* create a new type struct */
999 type *new_type_struct (ident *name) {
1001 res = new_type(type_struct, NULL, name);
1002 res->attr.sa.members = NEW_ARR_F (entity *, 0);
1005 type *new_d_type_struct (ident *name, dbg_info* db) {
1006 type *res = new_type_struct (name);
1007 set_type_dbg_info(res, db);
1010 void free_struct_entities (type *strct) {
1012 assert(strct && (strct->type_op == type_struct));
1013 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1014 free_entity(get_struct_member(strct, i));
1016 void free_struct_attrs (type *strct) {
1017 assert(strct && (strct->type_op == type_struct));
1018 DEL_ARR_F(strct->attr.sa.members);
1021 /* manipulate private fields of struct */
1022 int get_struct_n_members (const type *strct) {
1023 assert(strct && (strct->type_op == type_struct));
1024 return (ARR_LEN (strct->attr.sa.members));
1027 void add_struct_member (type *strct, entity *member) {
1028 assert(strct && (strct->type_op == type_struct));
1029 assert(get_type_tpop(get_entity_type(member)) != type_method);
1030 /* @@@ lowerfirm geht nicht durch */
1031 assert(strct != get_entity_type(member) && "recursive type");
1032 ARR_APP1 (entity *, strct->attr.sa.members, member);
1035 entity *get_struct_member (const type *strct, int pos) {
1036 assert(strct && (strct->type_op == type_struct));
1037 assert(pos >= 0 && pos < get_struct_n_members(strct));
1038 return strct->attr.sa.members[pos];
1041 int get_struct_member_index(type *strct, entity *mem) {
1043 assert(strct && (strct->type_op == type_struct));
1044 for (i = 0; i < get_struct_n_members(strct); i++)
1045 if (get_struct_member(strct, i) == mem)
1050 void set_struct_member (type *strct, int pos, entity *member) {
1051 assert(strct && (strct->type_op == type_struct));
1052 assert(pos >= 0 && pos < get_struct_n_members(strct));
1053 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1054 strct->attr.sa.members[pos] = member;
1056 void remove_struct_member(type *strct, entity *member) {
1058 assert(strct && (strct->type_op == type_struct));
1059 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1060 if (strct->attr.sa.members[i] == member) {
1061 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1062 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1063 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1069 int (is_Struct_type)(const type *strct) {
1070 return _is_struct_type(strct);
1073 /*******************************************************************/
1075 /*******************************************************************/
1078 * Lazy construction of value argument / result representation.
1079 * Constructs a struct type and its member. The types of the members
1080 * are passed in the argument list.
1082 * @param name name of the type constructed
1083 * @param len number of fields
1084 * @param tps array of field types with length len
1086 static INLINE type *
1087 build_value_type(ident *name, int len, type **tps) {
1089 type *res = new_type_struct(name);
1090 /* Remove type from type list. Must be treated differently than other types. */
1091 remove_irp_type(res);
1092 for (i = 0; i < len; i++) {
1093 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1094 if (tps[i]) elt_type = tps[i];
1095 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1100 /* Create a new method type.
1101 N_param is the number of parameters, n_res the number of results. */
1102 type *new_type_method (ident *name, int n_param, int n_res) {
1105 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1106 res = new_type(type_method, mode_P_mach, name);
1107 res->state = layout_fixed;
1108 res->size = get_mode_size_bits(mode_P_mach);
1109 res->attr.ma.n_params = n_param;
1110 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1111 res->attr.ma.value_params = NULL;
1112 res->attr.ma.n_res = n_res;
1113 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1114 res->attr.ma.value_ress = NULL;
1115 res->attr.ma.variadicity = variadicity_non_variadic;
1116 res->attr.ma.first_variadic_param = -1;
1121 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1122 type *res = new_type_method (name, n_param, n_res);
1123 set_type_dbg_info(res, db);
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_type_union (ident *name) {
1313 res = new_type(type_union, NULL, name);
1314 /*res->attr.ua.unioned_type = xcalloc(n_types, sizeof(res->attr.ua.unioned_type[0]));
1315 res->attr.ua.delim_names = xcalloc(n_types, sizeof(res->attr.ua.delim_names[0])); */
1316 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1319 type *new_d_type_union (ident *name, dbg_info* db) {
1320 type *res = new_type_union (name);
1321 set_type_dbg_info(res, db);
1324 void free_union_entities (type *uni) {
1326 assert(uni && (uni->type_op == type_union));
1327 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1328 free_entity(get_union_member(uni, i));
1330 void free_union_attrs (type *uni) {
1331 assert(uni && (uni->type_op == type_union));
1332 DEL_ARR_F(uni->attr.ua.members);
1334 /* manipulate private fields of union */
1336 int get_union_n_types (type *uni) {
1337 assert(uni && (uni->type_op == type_union));
1338 return uni->attr.ua.n_types;
1340 type *get_union_unioned_type (type *uni, int pos) {
1341 assert(uni && (uni->type_op == type_union));
1342 assert(pos >= 0 && pos < get_union_n_types(uni));
1343 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1345 void set_union_unioned_type (type *uni, int pos, type *tp) {
1346 assert(uni && (uni->type_op == type_union));
1347 assert(pos >= 0 && pos < get_union_n_types(uni));
1348 uni->attr.ua.unioned_type[pos] = tp;
1350 ident *get_union_delim_nameid (type *uni, int pos) {
1351 assert(uni && (uni->type_op == type_union));
1352 assert(pos >= 0 && pos < get_union_n_types(uni));
1353 return uni->attr.ua.delim_names[pos];
1355 const char *get_union_delim_name (type *uni, int pos) {
1356 assert(uni && (uni->type_op == type_union));
1357 assert(pos >= 0 && pos < get_union_n_types(uni));
1358 return get_id_str(uni->attr.ua.delim_names[pos]);
1360 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1361 assert(uni && (uni->type_op == type_union));
1362 assert(pos >= 0 && pos < get_union_n_types(uni));
1363 uni->attr.ua.delim_names[pos] = id;
1366 int get_union_n_members (const type *uni) {
1367 assert(uni && (uni->type_op == type_union));
1368 return (ARR_LEN (uni->attr.ua.members));
1370 void add_union_member (type *uni, entity *member) {
1371 assert(uni && (uni->type_op == type_union));
1372 assert(uni != get_entity_type(member) && "recursive type");
1373 ARR_APP1 (entity *, uni->attr.ua.members, member);
1375 entity *get_union_member (const type *uni, int pos) {
1376 assert(uni && (uni->type_op == type_union));
1377 assert(pos >= 0 && pos < get_union_n_members(uni));
1378 return uni->attr.ua.members[pos];
1380 void set_union_member (type *uni, int pos, entity *member) {
1381 assert(uni && (uni->type_op == type_union));
1382 assert(pos >= 0 && pos < get_union_n_members(uni));
1383 uni->attr.ua.members[pos] = member;
1385 void remove_union_member(type *uni, entity *member) {
1387 assert(uni && (uni->type_op == type_union));
1388 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1389 if (uni->attr.ua.members[i] == member) {
1390 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1391 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1392 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1398 int (is_Union_type)(const type *uni) {
1399 return _is_union_type(uni);
1402 /*-----------------------------------------------------------------*/
1404 /*-----------------------------------------------------------------*/
1407 /* create a new type array -- set dimension sizes independently */
1408 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1411 ir_graph *rem = current_ir_graph;
1412 assert(!is_Method_type(element_type));
1414 res = new_type(type_array, NULL, name);
1415 res->attr.aa.n_dimensions = n_dimensions;
1416 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1417 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1418 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1420 current_ir_graph = get_const_code_irg();
1421 for (i = 0; i < n_dimensions; i++) {
1422 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1423 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1424 res->attr.aa.order[i] = i;
1426 current_ir_graph = rem;
1428 res->attr.aa.element_type = element_type;
1429 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1434 type *new_d_type_array (ident *name, int n_dimensions,
1435 type *element_type, dbg_info* db) {
1436 type *res = new_type_array (name, n_dimensions, element_type);
1437 set_type_dbg_info(res, db);
1441 void free_array_entities (type *array) {
1442 assert(array && (array->type_op == type_array));
1445 void free_array_attrs (type *array) {
1446 assert(array && (array->type_op == type_array));
1447 free(array->attr.aa.lower_bound);
1448 free(array->attr.aa.upper_bound);
1451 /* manipulate private fields of array type */
1452 int get_array_n_dimensions (const type *array) {
1453 assert(array && (array->type_op == type_array));
1454 return array->attr.aa.n_dimensions;
1458 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1459 ir_node * upper_bound) {
1460 assert(array && (array->type_op == type_array));
1461 assert(lower_bound && "lower_bound node may not be NULL.");
1462 assert(upper_bound && "upper_bound node may not be NULL.");
1463 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1464 array->attr.aa.lower_bound[dimension] = lower_bound;
1465 array->attr.aa.upper_bound[dimension] = upper_bound;
1468 set_array_bounds_int (type *array, int dimension, int lower_bound,
1470 ir_graph *rem = current_ir_graph;
1471 current_ir_graph = get_const_code_irg();
1472 set_array_bounds (array, dimension,
1473 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1474 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1475 current_ir_graph = rem;
1478 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1479 assert(array && (array->type_op == type_array));
1480 assert(lower_bound && "lower_bound node may not be NULL.");
1481 array->attr.aa.lower_bound[dimension] = lower_bound;
1483 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1484 ir_graph *rem = current_ir_graph;
1485 current_ir_graph = get_const_code_irg();
1486 set_array_lower_bound (array, dimension,
1487 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1488 current_ir_graph = rem;
1491 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1492 assert(array && (array->type_op == type_array));
1493 assert(upper_bound && "upper_bound node may not be NULL.");
1494 array->attr.aa.upper_bound[dimension] = upper_bound;
1496 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1497 ir_graph *rem = current_ir_graph;
1498 current_ir_graph = get_const_code_irg();
1499 set_array_upper_bound (array, dimension,
1500 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1501 current_ir_graph = rem;
1503 int has_array_lower_bound (const type *array, int dimension) {
1504 assert(array && (array->type_op == type_array));
1505 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1507 ir_node *get_array_lower_bound (const type *array, int dimension) {
1508 assert(array && (array->type_op == type_array));
1509 return array->attr.aa.lower_bound[dimension];
1511 long get_array_lower_bound_int (const type *array, int dimension) {
1513 assert(array && (array->type_op == type_array));
1514 node = array->attr.aa.lower_bound[dimension];
1515 assert(get_irn_op(node) == op_Const);
1516 return get_tarval_long(get_Const_tarval(node));
1518 int has_array_upper_bound (const type *array, int dimension) {
1519 assert(array && (array->type_op == type_array));
1520 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1522 ir_node * get_array_upper_bound (const type *array, int dimension) {
1523 assert(array && (array->type_op == type_array));
1524 return array->attr.aa.upper_bound[dimension];
1526 long get_array_upper_bound_int (const type *array, int dimension) {
1528 assert(array && (array->type_op == type_array));
1529 node = array->attr.aa.upper_bound[dimension];
1530 assert(get_irn_op(node) == op_Const);
1531 return get_tarval_long(get_Const_tarval(node));
1534 void set_array_order (type *array, int dimension, int order) {
1535 assert(array && (array->type_op == type_array));
1536 array->attr.aa.order[dimension] = order;
1539 int get_array_order (const type *array, int dimension) {
1540 assert(array && (array->type_op == type_array));
1541 return array->attr.aa.order[dimension];
1544 int find_array_dimension(const type *array, int order) {
1547 assert(array && (array->type_op == type_array));
1549 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1550 if (array->attr.aa.order[dim] == order)
1556 void set_array_element_type (type *array, type *tp) {
1557 assert(array && (array->type_op == type_array));
1558 assert(!is_Method_type(tp));
1559 array->attr.aa.element_type = tp;
1561 type *get_array_element_type (type *array) {
1562 assert(array && (array->type_op == type_array));
1563 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1566 void set_array_element_entity (type *array, entity *ent) {
1567 assert(array && (array->type_op == type_array));
1568 assert((get_entity_type(ent)->type_op != type_method));
1569 array->attr.aa.element_ent = ent;
1570 array->attr.aa.element_type = get_entity_type(ent);
1572 entity *get_array_element_entity (const type *array) {
1573 assert(array && (array->type_op == type_array));
1574 return array->attr.aa.element_ent;
1578 int (is_Array_type)(const type *array) {
1579 return _is_array_type(array);
1582 /*-----------------------------------------------------------------*/
1583 /* TYPE_ENUMERATION */
1584 /*-----------------------------------------------------------------*/
1586 /* create a new type enumeration -- set the enumerators independently */
1587 type *new_type_enumeration (ident *name, int n_enums) {
1589 res = new_type(type_enumeration, NULL, name);
1590 res->attr.ea.n_enums = n_enums;
1591 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1592 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1595 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1596 type *res = new_type_enumeration (name, n_enums);
1597 set_type_dbg_info(res, db);
1601 void free_enumeration_entities(type *enumeration) {
1602 assert(enumeration && (enumeration->type_op == type_enumeration));
1604 void free_enumeration_attrs(type *enumeration) {
1605 assert(enumeration && (enumeration->type_op == type_enumeration));
1606 free(enumeration->attr.ea.enumer);
1607 free(enumeration->attr.ea.enum_nameid);
1610 /* manipulate fields of enumeration type. */
1611 int get_enumeration_n_enums (const type *enumeration) {
1612 assert(enumeration && (enumeration->type_op == type_enumeration));
1613 return enumeration->attr.ea.n_enums;
1615 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1616 assert(enumeration && (enumeration->type_op == type_enumeration));
1617 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1618 enumeration->attr.ea.enumer[pos] = con;
1620 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1621 assert(enumeration && (enumeration->type_op == type_enumeration));
1622 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1623 return enumeration->attr.ea.enumer[pos];
1625 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1626 assert(enumeration && (enumeration->type_op == type_enumeration));
1627 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1628 enumeration->attr.ea.enum_nameid[pos] = id;
1630 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1631 assert(enumeration && (enumeration->type_op == type_enumeration));
1632 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1633 return enumeration->attr.ea.enum_nameid[pos];
1635 const char *get_enumeration_name(const type *enumeration, int pos) {
1636 assert(enumeration && (enumeration->type_op == type_enumeration));
1637 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1638 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1642 int (is_Enumeration_type)(const type *enumeration) {
1643 return _is_enumeration_type(enumeration);
1646 /*-----------------------------------------------------------------*/
1648 /*-----------------------------------------------------------------*/
1650 /* Create a new type pointer */
1651 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1653 assert(mode_is_reference(ptr_mode));
1654 res = new_type(type_pointer, ptr_mode, name);
1655 res->attr.pa.points_to = points_to;
1656 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1657 res->size = get_mode_size_bits(res->mode);
1658 res->state = layout_fixed;
1661 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1662 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1663 set_type_dbg_info(res, db);
1666 void free_pointer_entities (type *pointer) {
1667 assert(pointer && (pointer->type_op == type_pointer));
1669 void free_pointer_attrs (type *pointer) {
1670 assert(pointer && (pointer->type_op == type_pointer));
1672 /* manipulate fields of type_pointer */
1673 void set_pointer_points_to_type (type *pointer, type *tp) {
1674 assert(pointer && (pointer->type_op == type_pointer));
1675 pointer->attr.pa.points_to = tp;
1677 type *get_pointer_points_to_type (type *pointer) {
1678 assert(pointer && (pointer->type_op == type_pointer));
1679 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1683 int (is_Pointer_type)(const type *pointer) {
1684 return _is_pointer_type(pointer);
1687 /* Returns the first pointer type that has as points_to tp.
1688 * Not efficient: O(#types).
1689 * If not found returns firm_unknown_type. */
1690 type *find_pointer_type_to_type (type *tp) {
1692 for (i = 0; i < get_irp_n_types(); ++i) {
1693 type *found = get_irp_type(i);
1694 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1697 return firm_unknown_type;
1702 /*-----------------------------------------------------------------*/
1703 /* TYPE_PRIMITIVE */
1704 /*-----------------------------------------------------------------*/
1706 /* create a new type primitive */
1707 type *new_type_primitive (ident *name, ir_mode *mode) {
1709 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1710 res = new_type(type_primitive, mode, name);
1711 res->size = get_mode_size_bits(mode);
1712 res->state = layout_fixed;
1715 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1716 type *res = new_type_primitive (name, mode);
1717 set_type_dbg_info(res, db);
1720 void free_primitive_entities (type *primitive) {
1721 assert(primitive && (primitive->type_op == type_primitive));
1723 void free_primitive_attrs (type *primitive) {
1724 assert(primitive && (primitive->type_op == type_primitive));
1728 int (is_Primitive_type)(const type *primitive) {
1729 return _is_primitive_type(primitive);
1732 /*-----------------------------------------------------------------*/
1733 /* common functionality */
1734 /*-----------------------------------------------------------------*/
1737 int (is_atomic_type)(const type *tp) {
1738 return _is_atomic_type(tp);
1742 * Gets the number of elements in a firm compound type.
1744 int get_compound_n_members(const type *tp)
1748 if (is_Struct_type(tp))
1749 res = get_struct_n_members(tp);
1750 else if (is_Class_type(tp))
1751 res = get_class_n_members(tp);
1752 else if (is_Union_type(tp))
1753 res = get_union_n_members(tp);
1755 assert(0 && "need struct, union or class for member count");
1761 * Gets the member of a firm compound type at position pos.
1763 entity *get_compound_member(const type *tp, int pos)
1767 if (is_Struct_type(tp))
1768 res = get_struct_member(tp, pos);
1769 else if (is_Class_type(tp))
1770 res = get_class_member(tp, pos);
1771 else if (is_Union_type(tp))
1772 res = get_union_member(tp, pos);
1775 assert(0 && "need struct, union or class to get a member");
1782 int is_compound_type(const type *tp) {
1783 assert(tp && tp->kind == k_type);
1784 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1787 /* Checks, whether a type is a frame type */
1788 int is_frame_type(const type *tp) {
1789 return tp->frame_type;
1792 /* Makes a new frame type. */
1793 type *new_type_frame(ident *name)
1795 type *res = new_type_class(name);
1797 res->frame_type = 1;
1799 /* Remove type from type list. Must be treated differently than other types. */
1800 remove_irp_type(res);