3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * file type.c - implementation of the datastructure to hold
17 * (C) 2001 by Universitaet Karlsruhe
20 * This module supplies a datastructure to represent all types
21 * known in the compiled program. This includes types specified
22 * in the program as well as types defined by the language. In the
23 * view of the intermediate representation there is no difference
24 * between these types.
26 * There exist several kinds of types, arranged by the structure of
27 * the type. A type is described by a set of attributes. Some of
28 * these attributes are common to all types, others depend on the
31 * Types are different from the modes defined in irmode: Types are
32 * on the level of the programming language, modes at the level of
33 * the target processor.
35 * @see type_t.h type tpop
58 # include "irprog_t.h"
61 # include "typegmod.h"
67 /*******************************************************************/
69 /*******************************************************************/
71 type *firm_none_type; type *get_none_type(void) { return firm_none_type; }
72 type *firm_unknown_type; type *get_unknown_type(void) { return firm_unknown_type; }
76 /** Returns a new, unique number to number nodes or the like. */
77 int get_irp_new_node_nr(void);
80 /* Suffixes added to types used for pass-by-value representations. */
81 static ident *value_params_suffix = NULL;
82 static ident *value_ress_suffix = NULL;
84 void init_type(void) {
85 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
86 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
88 /* construct none and unknown type. */
89 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
90 set_type_size_bits(firm_none_type, 0);
91 set_type_state (firm_none_type, layout_fixed);
92 remove_irp_type(firm_none_type);
93 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
94 set_type_size_bits(firm_unknown_type, 0);
95 set_type_state (firm_unknown_type, layout_fixed);
96 remove_irp_type(firm_unknown_type);
99 unsigned long type_visited;
101 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
102 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
103 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
107 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
111 assert(type_op != type_id);
112 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
114 node_size = offsetof(type, attr) + type_op->attr_size;
115 res = xmalloc (node_size);
116 memset(res, 0, node_size);
117 add_irp_type(res); /* Remember the new type global. */
120 res->type_op = type_op;
123 res->visibility = visibility_external_allocated;
124 res->state = layout_undefined;
130 res->nr = get_irp_new_node_nr();
131 #endif /* defined DEBUG_libfirm */
136 void free_type(type *tp) {
137 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
139 /* Remove from list of all types */
141 /* Free the attributes of the type. */
143 /* Free entities automatically allocated with the type */
144 if (is_Array_type(tp))
145 free_entity(get_array_element_entity(tp));
146 /* And now the type itself... */
151 void free_type_entities(type *tp) {
152 switch(get_type_tpop_code(tp)) {
153 case tpo_class: { free_class_entities(tp); } break;
154 case tpo_struct: { free_struct_entities(tp); } break;
155 case tpo_method: { free_method_entities(tp); } break;
156 case tpo_union: { free_union_entities(tp); } break;
157 case tpo_array: { free_array_entities(tp); } break;
158 case tpo_enumeration: { free_enumeration_entities(tp); } break;
159 case tpo_pointer: { free_pointer_entities(tp); } break;
160 case tpo_primitive: { free_primitive_entities(tp); } break;
165 void free_type_attrs(type *tp) {
166 switch(get_type_tpop_code(tp)) {
167 case tpo_class: { free_class_attrs(tp); } break;
168 case tpo_struct: { free_struct_attrs(tp); } break;
169 case tpo_method: { free_method_attrs(tp); } break;
170 case tpo_union: { free_union_attrs(tp); } break;
171 case tpo_array: { free_array_attrs(tp); } break;
172 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
173 case tpo_pointer: { free_pointer_attrs(tp); } break;
174 case tpo_primitive: { free_primitive_attrs(tp); } break;
179 /* set/get the link field */
180 void *(get_type_link)(const type *tp)
182 return _get_type_link(tp);
185 void (set_type_link)(type *tp, void *l)
187 _set_type_link(tp, l);
190 const tp_op *(get_type_tpop)(const type *tp) {
191 return _get_type_tpop(tp);
194 ident *(get_type_tpop_nameid)(const type *tp) {
195 return _get_type_tpop_nameid(tp);
198 const char* get_type_tpop_name(const type *tp) {
199 assert(tp && tp->kind == k_type);
200 return get_id_str(tp->type_op->name);
203 tp_opcode (get_type_tpop_code)(const type *tp) {
204 return _get_type_tpop_code(tp);
207 ir_mode *(get_type_mode)(const type *tp) {
208 return _get_type_mode(tp);
211 void set_type_mode(type *tp, ir_mode* m) {
212 assert(tp && tp->kind == k_type);
214 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
215 /* Modes of primitives must be data */
216 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
217 /* Modes of enumerations must be integers */
218 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
219 /* Modes of pointers must be references. */
221 switch (get_type_tpop_code(tp)) {
223 /* For primitive size depends on the mode. */
224 tp->size = get_mode_size_bits(m);
227 case tpo_enumeration:
229 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
230 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
231 tp->size = get_mode_size_bits(m);
236 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
237 assert(get_type_state(tp) == layout_fixed &&
238 tp->size == get_mode_size_bits(m) &&
239 "mode don't match struct/class layout");
243 assert(0 && "setting a mode is NOT allowed for this type");
247 ident *(get_type_ident)(const type *tp) {
248 return _get_type_ident(tp);
251 void (set_type_ident)(type *tp, ident* id) {
252 _set_type_ident(tp, id);
255 /* Outputs a unique number for this node */
256 long get_type_nr(const type *tp) {
265 const char* get_type_name(const type *tp) {
266 assert(tp && tp->kind == k_type);
267 return (get_id_str(tp->name));
270 int (get_type_size_bytes)(const type *tp) {
271 return _get_type_size_bytes(tp);
274 int (get_type_size_bits)(const type *tp) {
275 return _get_type_size_bits(tp);
279 visibility get_type_visibility (const type *tp) {
281 visibility res = visibility_local;
282 if (is_compound_type(tp)) {
284 if (is_Array_type(tp)) {
285 entity *mem = get_array_element_entity(tp);
286 if (get_entity_visibility(mem) != visibility_local)
287 res = visibility_external_visible;
289 int i, n_mems = get_compound_n_members(tp);
290 for (i = 0; i < n_mems; ++i) {
291 entity *mem = get_compound_member(tp, i);
292 if (get_entity_visibility(mem) != visibility_local)
293 res = visibility_external_visible;
300 return tp->visibility;
303 void set_type_visibility (type *tp, visibility v) {
306 /* check for correctness */
307 if (v != visibility_external_allocated) {
308 visibility res = visibility_local;
309 if (is_compound_type(tp)) {
310 if (is_Array_type(tp)) {
311 entity *mem = get_array_element_entity(tp);
312 if (get_entity_visibility(mem) > res)
313 res = get_entity_visibility(mem);
315 int i, n_mems = get_compound_n_members(tp);
316 for (i = 0; i < n_mems; ++i) {
317 entity *mem = get_compound_member(tp, i);
318 if (get_entity_visibility(mem) > res)
319 res = get_entity_visibility(mem);
330 set_type_size_bits(type *tp, int size) {
331 assert(tp && tp->kind == k_type);
332 /* For pointer enumeration and primitive size depends on the mode.
333 Methods don't have a size. */
334 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
335 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
336 if (tp->type_op == type_primitive)
339 /* argh: we must allow to set negative values as "invalid size" */
340 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
341 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
347 set_type_size_bytes(type *tp, int size) {
348 set_type_size_bits(tp, 8*size);
351 int get_type_alignment_bytes(type *tp) {
352 int align = get_type_alignment_bits(tp);
354 return align < 0 ? align : (align + 7) >> 3;
357 int get_type_alignment_bits(type *tp) {
363 /* alignment NOT set calculate it "on demand" */
365 align = get_mode_size_bits(tp->mode);
366 else if (is_Array_type(tp))
367 align = get_type_alignment_bits(get_array_element_type(tp));
368 else if (is_compound_type(tp)) {
369 int i, n = get_compound_n_members(tp);
372 for (i = 0; i < n; ++i) {
373 type *t = get_entity_type(get_compound_member(tp, i));
374 int a = get_type_alignment_bits(t);
380 else if (is_Method_type(tp))
390 set_type_alignment_bits(type *tp, int align) {
391 assert(tp && tp->kind == k_type);
392 /* Methods don't have an alignment. */
393 if (tp->type_op != type_method) {
399 set_type_alignment_bytes(type *tp, int align) {
400 set_type_size_bits(tp, 8*align);
403 /* Returns a human readable string for the enum entry. */
404 const char *get_type_state_name(type_state s) {
405 #define X(a) case a: return #a;
415 type_state (get_type_state)(const type *tp) {
416 return _get_type_state(tp);
420 set_type_state(type *tp, type_state state) {
421 assert(tp && tp->kind == k_type);
423 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
424 (tp->type_op == type_method))
427 /* Just a correctness check: */
428 if (state == layout_fixed) {
430 switch (get_type_tpop_code(tp)) {
433 assert(get_type_size_bits(tp) > -1);
434 if (tp != get_glob_type()) {
435 int n_mem = get_class_n_members(tp);
436 for (i = 0; i < n_mem; i++) {
437 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
438 { DDMT(tp); DDME(get_class_member(tp, i)); }
439 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
441 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
442 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
449 assert(get_type_size_bits(tp) > -1);
450 for (i = 0; i < get_struct_n_members(tp); i++) {
451 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
452 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
461 Assure that only innermost dimension is dynamic? */
463 case tpo_enumeration:
465 assert(get_type_mode != NULL);
466 for (i = 0; i < get_enumeration_n_enums(tp); i++)
467 assert(get_enumeration_enum(tp, i) != NULL);
475 unsigned long (get_type_visited)(const type *tp) {
476 return _get_type_visited(tp);
479 void (set_type_visited)(type *tp, unsigned long num) {
480 _set_type_visited(tp, num);
483 /* Sets visited field in type to type_visited. */
484 void (mark_type_visited)(type *tp) {
485 _mark_type_visited(tp);
488 /* @@@ name clash with master flag
489 int (type_visited)(const type *tp) {
490 return _type_visited(tp);
493 int (type_not_visited)(const type *tp) {
494 return _type_not_visited(tp);
497 int (is_type)(const void *thing) {
498 return _is_type(thing);
501 /* Checks whether two types are structural equal.*/
502 int equal_type(type *typ1, type *typ2) {
507 if (typ1 == typ2) return 1;
509 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
510 (get_type_ident(typ1) != get_type_ident(typ2)) ||
511 (get_type_mode(typ1) != get_type_mode(typ2)) ||
512 (get_type_state(typ1) != get_type_state(typ2)))
514 if ((get_type_state(typ1) == layout_fixed) &&
515 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
518 switch(get_type_tpop_code(typ1)) {
520 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
521 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
522 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
523 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
524 /** Compare the members **/
525 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
526 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
527 /* First sort the members of typ2 */
528 for (i = 0; i < get_class_n_members(typ1); i++) {
529 entity *e1 = get_class_member(typ1, i);
530 for (j = 0; j < get_class_n_members(typ2); j++) {
531 entity *e2 = get_class_member(typ2, j);
532 if (get_entity_name(e1) == get_entity_name(e2))
536 for (i = 0; i < get_class_n_members(typ1); i++) {
537 if (!m[i] || /* Found no counterpart */
538 !equal_entity(get_class_member(typ1, i), m[i]))
541 /** Compare the supertypes **/
542 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
543 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
544 /* First sort the supertypes of typ2 */
545 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
546 type *t1 = get_class_supertype(typ1, i);
547 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
548 type *t2 = get_class_supertype(typ2, j);
549 if (get_type_ident(t2) == get_type_ident(t1))
553 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
554 if (!t[i] || /* Found no counterpart */
555 get_class_supertype(typ1, i) != t[i])
560 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
561 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
562 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
563 /* First sort the members of lt */
564 for (i = 0; i < get_struct_n_members(typ1); i++) {
565 entity *e1 = get_struct_member(typ1, i);
566 for (j = 0; j < get_struct_n_members(typ2); j++) {
567 entity *e2 = get_struct_member(typ2, j);
568 if (get_entity_name(e1) == get_entity_name(e2))
572 for (i = 0; i < get_struct_n_members(typ1); i++) {
573 if (!m[i] || /* Found no counterpart */
574 !equal_entity(get_struct_member(typ1, i), m[i]))
579 int n_param1, n_param2;
581 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
582 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
584 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
585 n_param1 = get_method_n_params(typ1);
586 n_param2 = get_method_n_params(typ2);
589 n_param1 = get_method_first_variadic_param_index(typ1);
590 n_param2 = get_method_first_variadic_param_index(typ2);
593 if (n_param1 != n_param2) return 0;
595 for (i = 0; i < n_param1; i++) {
596 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
599 for (i = 0; i < get_method_n_ress(typ1); i++) {
600 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
605 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
606 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
607 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
608 /* First sort the members of lt */
609 for (i = 0; i < get_union_n_members(typ1); i++) {
610 entity *e1 = get_union_member(typ1, i);
611 for (j = 0; j < get_union_n_members(typ2); j++) {
612 entity *e2 = get_union_member(typ2, j);
613 if (get_entity_name(e1) == get_entity_name(e2))
617 for (i = 0; i < get_union_n_members(typ1); i++) {
618 if (!m[i] || /* Found no counterpart */
619 !equal_entity(get_union_member(typ1, i), m[i]))
624 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
626 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
628 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
629 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
630 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
632 if (get_array_order(typ1, i) != get_array_order(typ2, i))
633 assert(0 && "type compare with different dimension orders not implemented");
636 case tpo_enumeration: {
637 assert(0 && "enumerations not implemented");
640 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
643 case tpo_primitive: {
650 /* Checks whether two types are structural comparable. */
651 int smaller_type (type *st, type *lt) {
655 if (st == lt) return 1;
657 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
660 switch(get_type_tpop_code(st)) {
662 return is_subclass_of(st, lt);
665 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
666 m = alloca(sizeof(entity *) * get_struct_n_members(st));
667 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
668 /* First sort the members of lt */
669 for (i = 0; i < get_struct_n_members(st); i++) {
670 entity *se = get_struct_member(st, i);
671 for (j = 0; j < get_struct_n_members(lt); j++) {
672 entity *le = get_struct_member(lt, j);
673 if (get_entity_name(le) == get_entity_name(se))
677 for (i = 0; i < get_struct_n_members(st); i++) {
678 if (!m[i] || /* Found no counterpart */
679 !smaller_type(get_entity_type(get_struct_member(st, i)),
680 get_entity_type(m[i])))
685 /** FIXME: is this still 1? */
686 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
687 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
688 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
689 for (i = 0; i < get_method_n_params(st); i++) {
690 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
693 for (i = 0; i < get_method_n_ress(st); i++) {
694 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
699 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
700 m = alloca(sizeof(entity *) * get_union_n_members(st));
701 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
702 /* First sort the members of lt */
703 for (i = 0; i < get_union_n_members(st); i++) {
704 entity *se = get_union_member(st, i);
705 for (j = 0; j < get_union_n_members(lt); j++) {
706 entity *le = get_union_member(lt, j);
707 if (get_entity_name(le) == get_entity_name(se))
711 for (i = 0; i < get_union_n_members(st); i++) {
712 if (!m[i] || /* Found no counterpart */
713 !smaller_type(get_entity_type(get_union_member(st, i)),
714 get_entity_type(m[i])))
719 type *set, *let; /* small/large elt. type */
720 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
722 set = get_array_element_type(st);
723 let = get_array_element_type(lt);
725 /* If the elt types are different, set must be convertible
726 to let, and they must have the same size so that address
727 computations work out. To have a size the layout must
729 if ((get_type_state(set) != layout_fixed) ||
730 (get_type_state(let) != layout_fixed))
732 if (!smaller_type(set, let) ||
733 get_type_size_bits(set) != get_type_size_bits(let))
736 for(i = 0; i < get_array_n_dimensions(st); i++) {
737 if (get_array_lower_bound(lt, i))
738 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
740 if (get_array_upper_bound(lt, i))
741 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
745 case tpo_enumeration: {
746 assert(0 && "enumerations not implemented");
749 if (!smaller_type(get_pointer_points_to_type(st),
750 get_pointer_points_to_type(lt)))
753 case tpo_primitive: {
754 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
762 /*-----------------------------------------------------------------*/
764 /*-----------------------------------------------------------------*/
766 /* create a new class type */
767 type *new_type_class (ident *name) {
770 res = new_type(type_class, NULL, name);
772 res->attr.ca.members = NEW_ARR_F (entity *, 0);
773 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
774 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
775 res->attr.ca.peculiarity = peculiarity_existent;
776 res->attr.ca.dfn = 0;
780 type *new_d_type_class (ident *name, dbg_info* db) {
781 type *res = new_type_class (name);
782 set_type_dbg_info(res, db);
786 void free_class_entities(type *clss) {
788 assert(clss && (clss->type_op == type_class));
789 for (i = get_class_n_members(clss)-1; i >= 0; --i)
790 free_entity(get_class_member(clss, i));
793 void free_class_attrs(type *clss) {
794 assert(clss && (clss->type_op == type_class));
795 DEL_ARR_F(clss->attr.ca.members);
796 DEL_ARR_F(clss->attr.ca.subtypes);
797 DEL_ARR_F(clss->attr.ca.supertypes);
800 /* manipulate private fields of class type */
801 void add_class_member (type *clss, entity *member) {
802 assert(clss && (clss->type_op == type_class));
803 ARR_APP1 (entity *, clss->attr.ca.members, member);
806 int (get_class_n_members) (const type *clss) {
807 return _get_class_n_members(clss);
810 int get_class_member_index(type *clss, entity *mem) {
812 assert(clss && (clss->type_op == type_class));
813 for (i = 0; i < get_class_n_members(clss); i++)
814 if (get_class_member(clss, i) == mem)
819 entity *(get_class_member) (const type *clss, int pos) {
820 return _get_class_member(clss, pos);
823 entity *get_class_member_by_name(type *clss, ident *name) {
825 assert(clss && (clss->type_op == type_class));
826 n_mem = get_class_n_members(clss);
827 for (i = 0; i < n_mem; ++i) {
828 entity *mem = get_class_member(clss, i);
829 if (get_entity_ident(mem) == name) return mem;
834 void set_class_member (type *clss, entity *member, int pos) {
835 assert(clss && (clss->type_op == type_class));
836 assert(pos >= 0 && pos < get_class_n_members(clss));
837 clss->attr.ca.members[pos] = member;
839 void set_class_members (type *clss, entity **members, int arity) {
841 assert(clss && (clss->type_op == type_class));
842 DEL_ARR_F(clss->attr.ca.members);
843 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
844 for (i = 0; i < arity; i++) {
845 set_entity_owner(members[i], clss);
846 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
849 void remove_class_member(type *clss, entity *member) {
851 assert(clss && (clss->type_op == type_class));
852 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
853 if (clss->attr.ca.members[i] == member) {
854 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
855 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
856 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
862 void add_class_subtype (type *clss, type *subtype) {
864 assert(clss && (clss->type_op == type_class));
865 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
866 for (i = 0; i < get_class_n_supertypes(subtype); i++)
867 if (get_class_supertype(subtype, i) == clss)
868 /* Class already registered */
870 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
872 int get_class_n_subtypes (const type *clss) {
873 assert(clss && (clss->type_op == type_class));
874 return (ARR_LEN (clss->attr.ca.subtypes));
876 type *get_class_subtype (type *clss, int pos) {
877 assert(clss && (clss->type_op == type_class));
878 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
879 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
881 int get_class_subtype_index(type *clss, const type *subclass) {
882 int i, n_subtypes = get_class_n_subtypes(clss);
883 assert(is_Class_type(subclass));
884 for (i = 0; i < n_subtypes; ++i) {
885 if (get_class_subtype(clss, i) == subclass) return i;
889 void set_class_subtype (type *clss, type *subtype, int pos) {
890 assert(clss && (clss->type_op == type_class));
891 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
892 clss->attr.ca.subtypes[pos] = subtype;
894 void remove_class_subtype(type *clss, type *subtype) {
896 assert(clss && (clss->type_op == type_class));
897 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
898 if (clss->attr.ca.subtypes[i] == subtype) {
899 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
900 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
901 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
906 void add_class_supertype (type *clss, type *supertype) {
908 assert(clss && (clss->type_op == type_class));
909 assert(supertype && (supertype -> type_op == type_class));
910 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
911 for (i = 0; i < get_class_n_subtypes(supertype); i++)
912 if (get_class_subtype(supertype, i) == clss)
913 /* Class already registered */
915 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
917 int get_class_n_supertypes (const type *clss) {
918 assert(clss && (clss->type_op == type_class));
919 return (ARR_LEN (clss->attr.ca.supertypes));
921 int get_class_supertype_index(type *clss, type *super_clss) {
922 int i, n_supertypes = get_class_n_supertypes(clss);
923 assert(super_clss && (super_clss->type_op == type_class));
924 for (i = 0; i < n_supertypes; i++)
925 if (get_class_supertype(clss, i) == super_clss)
929 type *get_class_supertype (type *clss, int pos) {
930 assert(clss && (clss->type_op == type_class));
931 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
932 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
934 void set_class_supertype (type *clss, type *supertype, int pos) {
935 assert(clss && (clss->type_op == type_class));
936 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
937 clss->attr.ca.supertypes[pos] = supertype;
939 void remove_class_supertype(type *clss, type *supertype) {
941 assert(clss && (clss->type_op == type_class));
942 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
943 if (clss->attr.ca.supertypes[i] == supertype) {
944 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
945 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
946 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
951 const char *get_peculiarity_string(peculiarity p) {
952 #define X(a) case a: return #a
954 X(peculiarity_description);
955 X(peculiarity_inherited);
956 X(peculiarity_existent);
959 return "invalid peculiarity";
962 peculiarity get_class_peculiarity (const type *clss) {
963 assert(clss && (clss->type_op == type_class));
964 return clss->attr.ca.peculiarity;
967 void set_class_peculiarity (type *clss, peculiarity pec) {
968 assert(clss && (clss->type_op == type_class));
969 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
970 clss->attr.ca.peculiarity = pec;
973 void set_class_dfn (type *clss, int dfn)
975 clss->attr.ca.dfn = dfn;
978 int get_class_dfn (const type *clss)
980 return (clss->attr.ca.dfn);
984 int (is_Class_type)(const type *clss) {
985 return _is_class_type(clss);
988 /*----------------------------------------------------------------**/
990 /*----------------------------------------------------------------**/
992 /* create a new type struct */
993 type *new_type_struct (ident *name) {
995 res = new_type(type_struct, NULL, name);
996 res->attr.sa.members = NEW_ARR_F (entity *, 0);
999 type *new_d_type_struct (ident *name, dbg_info* db) {
1000 type *res = new_type_struct (name);
1001 set_type_dbg_info(res, db);
1004 void free_struct_entities (type *strct) {
1006 assert(strct && (strct->type_op == type_struct));
1007 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1008 free_entity(get_struct_member(strct, i));
1010 void free_struct_attrs (type *strct) {
1011 assert(strct && (strct->type_op == type_struct));
1012 DEL_ARR_F(strct->attr.sa.members);
1015 /* manipulate private fields of struct */
1016 int get_struct_n_members (const type *strct) {
1017 assert(strct && (strct->type_op == type_struct));
1018 return (ARR_LEN (strct->attr.sa.members));
1021 void add_struct_member (type *strct, entity *member) {
1022 assert(strct && (strct->type_op == type_struct));
1023 assert(get_type_tpop(get_entity_type(member)) != type_method);
1024 /* @@@ lowerfirm geht nicht durch */
1025 ARR_APP1 (entity *, strct->attr.sa.members, member);
1028 entity *get_struct_member (const type *strct, int pos) {
1029 assert(strct && (strct->type_op == type_struct));
1030 assert(pos >= 0 && pos < get_struct_n_members(strct));
1031 return strct->attr.sa.members[pos];
1034 int get_struct_member_index(type *strct, entity *mem) {
1036 assert(strct && (strct->type_op == type_struct));
1037 for (i = 0; i < get_struct_n_members(strct); i++)
1038 if (get_struct_member(strct, i) == mem)
1043 void set_struct_member (type *strct, int pos, entity *member) {
1044 assert(strct && (strct->type_op == type_struct));
1045 assert(pos >= 0 && pos < get_struct_n_members(strct));
1046 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1047 strct->attr.sa.members[pos] = member;
1049 void remove_struct_member(type *strct, entity *member) {
1051 assert(strct && (strct->type_op == type_struct));
1052 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1053 if (strct->attr.sa.members[i] == member) {
1054 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1055 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1056 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1062 int (is_Struct_type)(const type *strct) {
1063 return _is_struct_type(strct);
1066 /*******************************************************************/
1068 /*******************************************************************/
1071 * Lazy construction of value argument / result representation.
1072 * Constructs a struct type and its member. The types of the members
1073 * are passed in the argument list.
1075 * @param name name of the type constructed
1076 * @param len number of fields
1077 * @param tps array of field types with length len
1079 static INLINE type *
1080 build_value_type(ident *name, int len, type **tps) {
1082 type *res = new_type_struct(name);
1083 /* Remove type from type list. Must be treated differently than other types. */
1084 remove_irp_type_from_list(res);
1085 for (i = 0; i < len; i++) {
1086 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1087 if (tps[i]) elt_type = tps[i];
1088 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1093 /* Create a new method type.
1094 N_param is the number of parameters, n_res the number of results. */
1095 type *new_type_method (ident *name, int n_param, int n_res) {
1098 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1099 res = new_type(type_method, mode_P_mach, name);
1100 res->state = layout_fixed;
1101 res->size = get_mode_size_bits(mode_P_mach);
1102 res->attr.ma.n_params = n_param;
1103 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1104 res->attr.ma.value_params = NULL;
1105 res->attr.ma.n_res = n_res;
1106 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1107 res->attr.ma.value_ress = NULL;
1108 res->attr.ma.variadicity = variadicity_non_variadic;
1109 res->attr.ma.first_variadic_param = -1;
1114 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1115 type *res = new_type_method (name, n_param, n_res);
1116 set_type_dbg_info(res, db);
1120 void free_method_entities(type *method) {
1121 assert(method && (method->type_op == type_method));
1124 /* Attention: also frees entities in value parameter subtypes! */
1125 void free_method_attrs(type *method) {
1126 assert(method && (method->type_op == type_method));
1127 free(method->attr.ma.param_type);
1128 free(method->attr.ma.res_type);
1129 if (method->attr.ma.value_params) {
1130 free_type_entities(method->attr.ma.value_params);
1131 free_type(method->attr.ma.value_params);
1133 if (method->attr.ma.value_ress) {
1134 free_type_entities(method->attr.ma.value_ress);
1135 free_type(method->attr.ma.value_ress);
1139 /* manipulate private fields of method. */
1140 int get_method_n_params (const type *method) {
1141 assert(method && (method->type_op == type_method));
1142 return method->attr.ma.n_params;
1145 type *get_method_param_type(type *method, int pos) {
1147 assert(method && (method->type_op == type_method));
1148 assert(pos >= 0 && pos < get_method_n_params(method));
1149 res = method->attr.ma.param_type[pos];
1150 assert(res != NULL && "empty method param type");
1151 return method->attr.ma.param_type[pos] = skip_tid(res);
1154 void set_method_param_type(type *method, int pos, type* tp) {
1155 assert(method && (method->type_op == type_method));
1156 assert(pos >= 0 && pos < get_method_n_params(method));
1157 method->attr.ma.param_type[pos] = tp;
1158 /* If information constructed set pass-by-value representation. */
1159 if (method->attr.ma.value_params) {
1160 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1161 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1165 /* Returns an entity that represents the copied value argument. Only necessary
1166 for compounds passed by value. */
1167 entity *get_method_value_param_ent(type *method, int pos) {
1168 assert(method && (method->type_op == type_method));
1169 assert(pos >= 0 && pos < get_method_n_params(method));
1170 if (!method->attr.ma.value_params)
1171 method->attr.ma.value_params
1172 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1173 get_method_n_params(method), method->attr.ma.param_type);
1174 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1175 != method->attr.ma.value_params)
1176 && "param type not yet set");
1177 return get_struct_member(method->attr.ma.value_params, pos);
1181 * Returns a type that represents the copied value arguments.
1183 type *get_method_value_param_type(const type *method)
1185 assert(method && (method->type_op == type_method));
1186 return method->attr.ma.value_params;
1189 int get_method_n_ress (const type *method) {
1190 assert(method && (method->type_op == type_method));
1191 return method->attr.ma.n_res;
1194 type *get_method_res_type(type *method, int pos) {
1196 assert(method && (method->type_op == type_method));
1197 assert(pos >= 0 && pos < get_method_n_ress(method));
1198 res = method->attr.ma.res_type[pos];
1199 assert(res != NULL && "empty method return type");
1200 return method->attr.ma.res_type[pos] = skip_tid(res);
1203 void set_method_res_type(type *method, int pos, type* tp) {
1204 assert(method && (method->type_op == type_method));
1205 assert(pos >= 0 && pos < get_method_n_ress(method));
1206 /* set the result type */
1207 method->attr.ma.res_type[pos] = tp;
1208 /* If information constructed set pass-by-value representation. */
1209 if (method->attr.ma.value_ress) {
1210 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1211 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1215 /* Returns an entity that represents the copied value result. Only necessary
1216 for compounds passed by value. */
1217 entity *get_method_value_res_ent(type *method, int pos) {
1218 assert(method && (method->type_op == type_method));
1219 assert(pos >= 0 && pos < get_method_n_ress(method));
1220 if (!method->attr.ma.value_ress)
1221 method->attr.ma.value_ress
1222 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1223 get_method_n_ress(method), method->attr.ma.res_type);
1224 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1225 && "result type not yet set");
1226 return get_struct_member(method->attr.ma.value_ress, pos);
1230 * Returns a type that represents the copied value results.
1232 type *get_method_value_res_type(const type *method) {
1233 assert(method && (method->type_op == type_method));
1234 return method->attr.ma.value_ress;
1237 /* Returns the null-terminated name of this variadicity. */
1238 const char *get_variadicity_name(variadicity vari)
1240 #define X(a) case a: return #a
1242 X(variadicity_non_variadic);
1243 X(variadicity_variadic);
1250 variadicity get_method_variadicity(const type *method)
1252 assert(method && (method->type_op == type_method));
1253 return method->attr.ma.variadicity;
1256 void set_method_variadicity(type *method, variadicity vari)
1258 assert(method && (method->type_op == type_method));
1259 method->attr.ma.variadicity = vari;
1263 * Returns the first variadic parameter index of a type.
1264 * If this index was NOT set, the index of the last parameter
1265 * of the method type plus one is returned for variadic functions.
1266 * Non-variadic function types always return -1 here.
1268 int get_method_first_variadic_param_index(const type *method)
1270 assert(method && (method->type_op == type_method));
1272 if (method->attr.ma.variadicity == variadicity_non_variadic)
1275 if (method->attr.ma.first_variadic_param == -1)
1276 return get_method_n_params(method);
1277 return method->attr.ma.first_variadic_param;
1281 * Sets the first variadic parameter index. This allows to specify
1282 * a complete call type (containing the type of all parameters)
1283 * but still have the knowledge, which parameter must be passed as
1286 void set_method_first_variadic_param_index(type *method, int index)
1288 assert(method && (method->type_op == type_method));
1289 assert(index >= 0 && index <= get_method_n_params(method));
1291 method->attr.ma.first_variadic_param = index;
1295 int (is_Method_type)(const type *method) {
1296 return _is_method_type(method);
1299 /*-----------------------------------------------------------------*/
1301 /*-----------------------------------------------------------------*/
1303 /* create a new type uni */
1304 type *new_type_union (ident *name) {
1306 res = new_type(type_union, NULL, name);
1307 /*res->attr.ua.unioned_type = xcalloc(n_types, sizeof(res->attr.ua.unioned_type[0]));
1308 res->attr.ua.delim_names = xcalloc(n_types, sizeof(res->attr.ua.delim_names[0])); */
1309 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1312 type *new_d_type_union (ident *name, dbg_info* db) {
1313 type *res = new_type_union (name);
1314 set_type_dbg_info(res, db);
1317 void free_union_entities (type *uni) {
1319 assert(uni && (uni->type_op == type_union));
1320 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1321 free_entity(get_union_member(uni, i));
1323 void free_union_attrs (type *uni) {
1324 assert(uni && (uni->type_op == type_union));
1325 DEL_ARR_F(uni->attr.ua.members);
1327 /* manipulate private fields of union */
1329 int get_union_n_types (type *uni) {
1330 assert(uni && (uni->type_op == type_union));
1331 return uni->attr.ua.n_types;
1333 type *get_union_unioned_type (type *uni, int pos) {
1334 assert(uni && (uni->type_op == type_union));
1335 assert(pos >= 0 && pos < get_union_n_types(uni));
1336 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1338 void set_union_unioned_type (type *uni, int pos, type *tp) {
1339 assert(uni && (uni->type_op == type_union));
1340 assert(pos >= 0 && pos < get_union_n_types(uni));
1341 uni->attr.ua.unioned_type[pos] = tp;
1343 ident *get_union_delim_nameid (type *uni, int pos) {
1344 assert(uni && (uni->type_op == type_union));
1345 assert(pos >= 0 && pos < get_union_n_types(uni));
1346 return uni->attr.ua.delim_names[pos];
1348 const char *get_union_delim_name (type *uni, int pos) {
1349 assert(uni && (uni->type_op == type_union));
1350 assert(pos >= 0 && pos < get_union_n_types(uni));
1351 return get_id_str(uni->attr.ua.delim_names[pos]);
1353 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1354 assert(uni && (uni->type_op == type_union));
1355 assert(pos >= 0 && pos < get_union_n_types(uni));
1356 uni->attr.ua.delim_names[pos] = id;
1359 int get_union_n_members (const type *uni) {
1360 assert(uni && (uni->type_op == type_union));
1361 return (ARR_LEN (uni->attr.ua.members));
1363 void add_union_member (type *uni, entity *member) {
1364 assert(uni && (uni->type_op == type_union));
1365 ARR_APP1 (entity *, uni->attr.ua.members, member);
1367 entity *get_union_member (const type *uni, int pos) {
1368 assert(uni && (uni->type_op == type_union));
1369 assert(pos >= 0 && pos < get_union_n_members(uni));
1370 return uni->attr.ua.members[pos];
1372 void set_union_member (type *uni, int pos, entity *member) {
1373 assert(uni && (uni->type_op == type_union));
1374 assert(pos >= 0 && pos < get_union_n_members(uni));
1375 uni->attr.ua.members[pos] = member;
1377 void remove_union_member(type *uni, entity *member) {
1379 assert(uni && (uni->type_op == type_union));
1380 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1381 if (uni->attr.ua.members[i] == member) {
1382 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1383 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1384 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1390 int (is_Union_type)(const type *uni) {
1391 return _is_union_type(uni);
1394 /*-----------------------------------------------------------------*/
1396 /*-----------------------------------------------------------------*/
1399 /* create a new type array -- set dimension sizes independently */
1400 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1403 ir_graph *rem = current_ir_graph;
1404 assert(!is_Method_type(element_type));
1406 res = new_type(type_array, NULL, name);
1407 res->attr.aa.n_dimensions = n_dimensions;
1408 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1409 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1410 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1412 current_ir_graph = get_const_code_irg();
1413 for (i = 0; i < n_dimensions; i++) {
1414 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1415 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1416 res->attr.aa.order[i] = i;
1418 current_ir_graph = rem;
1420 res->attr.aa.element_type = element_type;
1421 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1426 type *new_d_type_array (ident *name, int n_dimensions,
1427 type *element_type, dbg_info* db) {
1428 type *res = new_type_array (name, n_dimensions, element_type);
1429 set_type_dbg_info(res, db);
1433 void free_array_entities (type *array) {
1434 assert(array && (array->type_op == type_array));
1437 void free_array_attrs (type *array) {
1438 assert(array && (array->type_op == type_array));
1439 free(array->attr.aa.lower_bound);
1440 free(array->attr.aa.upper_bound);
1443 /* manipulate private fields of array type */
1444 int get_array_n_dimensions (const type *array) {
1445 assert(array && (array->type_op == type_array));
1446 return array->attr.aa.n_dimensions;
1450 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1451 ir_node * upper_bound) {
1452 assert(array && (array->type_op == type_array));
1453 assert(lower_bound && "lower_bound node may not be NULL.");
1454 assert(upper_bound && "upper_bound node may not be NULL.");
1455 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1456 array->attr.aa.lower_bound[dimension] = lower_bound;
1457 array->attr.aa.upper_bound[dimension] = upper_bound;
1460 set_array_bounds_int (type *array, int dimension, int lower_bound,
1462 ir_graph *rem = current_ir_graph;
1463 current_ir_graph = get_const_code_irg();
1464 set_array_bounds (array, dimension,
1465 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1466 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1467 current_ir_graph = rem;
1470 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1471 assert(array && (array->type_op == type_array));
1472 assert(lower_bound && "lower_bound node may not be NULL.");
1473 array->attr.aa.lower_bound[dimension] = lower_bound;
1475 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1476 ir_graph *rem = current_ir_graph;
1477 current_ir_graph = get_const_code_irg();
1478 set_array_lower_bound (array, dimension,
1479 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1480 current_ir_graph = rem;
1483 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1484 assert(array && (array->type_op == type_array));
1485 assert(upper_bound && "upper_bound node may not be NULL.");
1486 array->attr.aa.upper_bound[dimension] = upper_bound;
1488 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1489 ir_graph *rem = current_ir_graph;
1490 current_ir_graph = get_const_code_irg();
1491 set_array_upper_bound (array, dimension,
1492 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1493 current_ir_graph = rem;
1495 int has_array_lower_bound (const type *array, int dimension) {
1496 assert(array && (array->type_op == type_array));
1497 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1499 ir_node *get_array_lower_bound (const type *array, int dimension) {
1500 assert(array && (array->type_op == type_array));
1501 return array->attr.aa.lower_bound[dimension];
1503 long get_array_lower_bound_int (const type *array, int dimension) {
1505 assert(array && (array->type_op == type_array));
1506 node = array->attr.aa.lower_bound[dimension];
1507 assert(get_irn_op(node) == op_Const);
1508 return get_tarval_long(get_Const_tarval(node));
1510 int has_array_upper_bound (const type *array, int dimension) {
1511 assert(array && (array->type_op == type_array));
1512 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1514 ir_node * get_array_upper_bound (const type *array, int dimension) {
1515 assert(array && (array->type_op == type_array));
1516 return array->attr.aa.upper_bound[dimension];
1518 long get_array_upper_bound_int (const type *array, int dimension) {
1520 assert(array && (array->type_op == type_array));
1521 node = array->attr.aa.upper_bound[dimension];
1522 assert(get_irn_op(node) == op_Const);
1523 return get_tarval_long(get_Const_tarval(node));
1526 void set_array_order (type *array, int dimension, int order) {
1527 assert(array && (array->type_op == type_array));
1528 array->attr.aa.order[dimension] = order;
1531 int get_array_order (const type *array, int dimension) {
1532 assert(array && (array->type_op == type_array));
1533 return array->attr.aa.order[dimension];
1536 int find_array_dimension(const type *array, int order) {
1539 assert(array && (array->type_op == type_array));
1541 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1542 if (array->attr.aa.order[dim] == order)
1548 void set_array_element_type (type *array, type *tp) {
1549 assert(array && (array->type_op == type_array));
1550 assert(!is_Method_type(tp));
1551 array->attr.aa.element_type = tp;
1553 type *get_array_element_type (type *array) {
1554 assert(array && (array->type_op == type_array));
1555 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1558 void set_array_element_entity (type *array, entity *ent) {
1559 assert(array && (array->type_op == type_array));
1560 assert((get_entity_type(ent)->type_op != type_method));
1561 array->attr.aa.element_ent = ent;
1562 array->attr.aa.element_type = get_entity_type(ent);
1564 entity *get_array_element_entity (const type *array) {
1565 assert(array && (array->type_op == type_array));
1566 return array->attr.aa.element_ent;
1570 int (is_Array_type)(const type *array) {
1571 return _is_array_type(array);
1574 /*-----------------------------------------------------------------*/
1575 /* TYPE_ENUMERATION */
1576 /*-----------------------------------------------------------------*/
1578 /* create a new type enumeration -- set the enumerators independently */
1579 type *new_type_enumeration (ident *name, int n_enums) {
1581 res = new_type(type_enumeration, NULL, name);
1582 res->attr.ea.n_enums = n_enums;
1583 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1584 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1587 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1588 type *res = new_type_enumeration (name, n_enums);
1589 set_type_dbg_info(res, db);
1593 void free_enumeration_entities(type *enumeration) {
1594 assert(enumeration && (enumeration->type_op == type_enumeration));
1596 void free_enumeration_attrs(type *enumeration) {
1597 assert(enumeration && (enumeration->type_op == type_enumeration));
1598 free(enumeration->attr.ea.enumer);
1599 free(enumeration->attr.ea.enum_nameid);
1602 /* manipulate fields of enumeration type. */
1603 int get_enumeration_n_enums (const type *enumeration) {
1604 assert(enumeration && (enumeration->type_op == type_enumeration));
1605 return enumeration->attr.ea.n_enums;
1607 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1608 assert(enumeration && (enumeration->type_op == type_enumeration));
1609 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1610 enumeration->attr.ea.enumer[pos] = con;
1612 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1613 assert(enumeration && (enumeration->type_op == type_enumeration));
1614 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1615 return enumeration->attr.ea.enumer[pos];
1617 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1618 assert(enumeration && (enumeration->type_op == type_enumeration));
1619 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1620 enumeration->attr.ea.enum_nameid[pos] = id;
1622 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1623 assert(enumeration && (enumeration->type_op == type_enumeration));
1624 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1625 return enumeration->attr.ea.enum_nameid[pos];
1627 const char *get_enumeration_name(const type *enumeration, int pos) {
1628 assert(enumeration && (enumeration->type_op == type_enumeration));
1629 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1630 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1634 int (is_Enumeration_type)(const type *enumeration) {
1635 return _is_enumeration_type(enumeration);
1638 /*-----------------------------------------------------------------*/
1640 /*-----------------------------------------------------------------*/
1642 /* Create a new type pointer */
1643 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1645 assert(mode_is_reference(ptr_mode));
1646 res = new_type(type_pointer, ptr_mode, name);
1647 res->attr.pa.points_to = points_to;
1648 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1649 res->size = get_mode_size_bits(res->mode);
1650 res->state = layout_fixed;
1653 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1654 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1655 set_type_dbg_info(res, db);
1658 void free_pointer_entities (type *pointer) {
1659 assert(pointer && (pointer->type_op == type_pointer));
1661 void free_pointer_attrs (type *pointer) {
1662 assert(pointer && (pointer->type_op == type_pointer));
1664 /* manipulate fields of type_pointer */
1665 void set_pointer_points_to_type (type *pointer, type *tp) {
1666 assert(pointer && (pointer->type_op == type_pointer));
1667 pointer->attr.pa.points_to = tp;
1669 type *get_pointer_points_to_type (type *pointer) {
1670 assert(pointer && (pointer->type_op == type_pointer));
1671 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1675 int (is_Pointer_type)(const type *pointer) {
1676 return _is_pointer_type(pointer);
1679 /* Returns the first pointer type that has as points_to tp.
1680 * Not efficient: O(#types).
1681 * If not found returns firm_unknown_type. */
1682 type *find_pointer_type_to_type (type *tp) {
1684 for (i = 0; i < get_irp_n_types(); ++i) {
1685 type *found = get_irp_type(i);
1686 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1689 return firm_unknown_type;
1694 /*-----------------------------------------------------------------*/
1695 /* TYPE_PRIMITIVE */
1696 /*-----------------------------------------------------------------*/
1698 /* create a new type primitive */
1699 type *new_type_primitive (ident *name, ir_mode *mode) {
1701 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1702 res = new_type(type_primitive, mode, name);
1703 res->size = get_mode_size_bits(mode);
1704 res->state = layout_fixed;
1707 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1708 type *res = new_type_primitive (name, mode);
1709 set_type_dbg_info(res, db);
1712 void free_primitive_entities (type *primitive) {
1713 assert(primitive && (primitive->type_op == type_primitive));
1715 void free_primitive_attrs (type *primitive) {
1716 assert(primitive && (primitive->type_op == type_primitive));
1720 int (is_Primitive_type)(const type *primitive) {
1721 return _is_primitive_type(primitive);
1724 /*-----------------------------------------------------------------*/
1725 /* common functionality */
1726 /*-----------------------------------------------------------------*/
1729 int (is_atomic_type)(const type *tp) {
1730 return _is_atomic_type(tp);
1734 * Gets the number of elements in a firm compound type.
1736 int get_compound_n_members(const type *tp)
1740 if (is_Struct_type(tp))
1741 res = get_struct_n_members(tp);
1742 else if (is_Class_type(tp))
1743 res = get_class_n_members(tp);
1744 else if (is_Union_type(tp))
1745 res = get_union_n_members(tp);
1747 assert(0 && "need struct, union or class for member count");
1753 * Gets the member of a firm compound type at position pos.
1755 entity *get_compound_member(const type *tp, int pos)
1759 if (is_Struct_type(tp))
1760 res = get_struct_member(tp, pos);
1761 else if (is_Class_type(tp))
1762 res = get_class_member(tp, pos);
1763 else if (is_Union_type(tp))
1764 res = get_union_member(tp, pos);
1767 assert(0 && "need struct, union or class to get a member");
1775 int is_compound_type(const type *tp) {
1776 assert(tp && tp->kind == k_type);
1777 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;