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);
120 add_irp_type(res); /* Remember the new type global. */
123 res->type_op = type_op;
126 res->visibility = visibility_external_allocated;
127 res->state = layout_undefined;
133 res->nr = get_irp_new_node_nr();
134 #endif /* defined DEBUG_libfirm */
139 void free_type(type *tp) {
140 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
142 /* Remove from list of all types */
144 /* Free the attributes of the type. */
146 /* Free entities automatically allocated with the type */
147 if (is_Array_type(tp))
148 free_entity(get_array_element_entity(tp));
149 /* And now the type itself... */
154 void free_type_entities(type *tp) {
155 switch(get_type_tpop_code(tp)) {
156 case tpo_class: { free_class_entities(tp); } break;
157 case tpo_struct: { free_struct_entities(tp); } break;
158 case tpo_method: { free_method_entities(tp); } break;
159 case tpo_union: { free_union_entities(tp); } break;
160 case tpo_array: { free_array_entities(tp); } break;
161 case tpo_enumeration: { free_enumeration_entities(tp); } break;
162 case tpo_pointer: { free_pointer_entities(tp); } break;
163 case tpo_primitive: { free_primitive_entities(tp); } break;
168 void free_type_attrs(type *tp) {
169 switch(get_type_tpop_code(tp)) {
170 case tpo_class: { free_class_attrs(tp); } break;
171 case tpo_struct: { free_struct_attrs(tp); } break;
172 case tpo_method: { free_method_attrs(tp); } break;
173 case tpo_union: { free_union_attrs(tp); } break;
174 case tpo_array: { free_array_attrs(tp); } break;
175 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
176 case tpo_pointer: { free_pointer_attrs(tp); } break;
177 case tpo_primitive: { free_primitive_attrs(tp); } break;
182 /* set/get the link field */
183 void *(get_type_link)(const type *tp)
185 return _get_type_link(tp);
188 void (set_type_link)(type *tp, void *l)
190 _set_type_link(tp, l);
193 const tp_op *(get_type_tpop)(const type *tp) {
194 return _get_type_tpop(tp);
197 ident *(get_type_tpop_nameid)(const type *tp) {
198 return _get_type_tpop_nameid(tp);
201 const char* get_type_tpop_name(const type *tp) {
202 assert(tp && tp->kind == k_type);
203 return get_id_str(tp->type_op->name);
206 tp_opcode (get_type_tpop_code)(const type *tp) {
207 return _get_type_tpop_code(tp);
210 ir_mode *(get_type_mode)(const type *tp) {
211 return _get_type_mode(tp);
214 void set_type_mode(type *tp, ir_mode* m) {
215 assert(tp && tp->kind == k_type);
217 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
218 /* Modes of primitives must be data */
219 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
220 /* Modes of enumerations must be integers */
221 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
222 /* Modes of pointers must be references. */
224 switch (get_type_tpop_code(tp)) {
226 /* For primitive size depends on the mode. */
227 tp->size = get_mode_size_bits(m);
230 case tpo_enumeration:
232 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
233 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
234 tp->size = get_mode_size_bits(m);
239 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
240 assert(get_type_state(tp) == layout_fixed &&
241 tp->size == get_mode_size_bits(m) &&
242 "mode don't match struct/class layout");
246 assert(0 && "setting a mode is NOT allowed for this type");
250 ident *(get_type_ident)(const type *tp) {
251 return _get_type_ident(tp);
254 void (set_type_ident)(type *tp, ident* id) {
255 _set_type_ident(tp, id);
258 /* Outputs a unique number for this node */
259 long get_type_nr(const type *tp) {
268 const char* get_type_name(const type *tp) {
269 assert(tp && tp->kind == k_type);
270 return (get_id_str(tp->name));
273 int (get_type_size_bytes)(const type *tp) {
274 return _get_type_size_bytes(tp);
277 int (get_type_size_bits)(const type *tp) {
278 return _get_type_size_bits(tp);
282 visibility get_type_visibility (const type *tp) {
284 visibility res = visibility_local;
285 if (is_compound_type(tp)) {
287 if (is_Array_type(tp)) {
288 entity *mem = get_array_element_entity(tp);
289 if (get_entity_visibility(mem) != visibility_local)
290 res = visibility_external_visible;
292 int i, n_mems = get_compound_n_members(tp);
293 for (i = 0; i < n_mems; ++i) {
294 entity *mem = get_compound_member(tp, i);
295 if (get_entity_visibility(mem) != visibility_local)
296 res = visibility_external_visible;
303 return tp->visibility;
306 void set_type_visibility (type *tp, visibility v) {
309 /* check for correctness */
310 if (v != visibility_external_allocated) {
311 visibility res = visibility_local;
312 if (is_compound_type(tp)) {
313 if (is_Array_type(tp)) {
314 entity *mem = get_array_element_entity(tp);
315 if (get_entity_visibility(mem) > res)
316 res = get_entity_visibility(mem);
318 int i, n_mems = get_compound_n_members(tp);
319 for (i = 0; i < n_mems; ++i) {
320 entity *mem = get_compound_member(tp, i);
321 if (get_entity_visibility(mem) > res)
322 res = get_entity_visibility(mem);
333 set_type_size_bits(type *tp, int size) {
334 assert(tp && tp->kind == k_type);
335 /* For pointer enumeration and primitive size depends on the mode.
336 Methods don't have a size. */
337 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
338 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
339 if (tp->type_op == type_primitive)
342 /* argh: we must allow to set negative values as "invalid size" */
343 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
344 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
350 set_type_size_bytes(type *tp, int size) {
351 set_type_size_bits(tp, 8*size);
354 int get_type_alignment_bytes(type *tp) {
355 int align = get_type_alignment_bits(tp);
357 return align < 0 ? align : (align + 7) >> 3;
360 int get_type_alignment_bits(type *tp) {
366 /* alignment NOT set calculate it "on demand" */
368 align = get_mode_size_bits(tp->mode);
369 else if (is_Array_type(tp))
370 align = get_type_alignment_bits(get_array_element_type(tp));
371 else if (is_compound_type(tp)) {
372 int i, n = get_compound_n_members(tp);
375 for (i = 0; i < n; ++i) {
376 type *t = get_entity_type(get_compound_member(tp, i));
377 int a = get_type_alignment_bits(t);
383 else if (is_Method_type(tp))
393 set_type_alignment_bits(type *tp, int align) {
394 assert(tp && tp->kind == k_type);
395 /* Methods don't have an alignment. */
396 if (tp->type_op != type_method) {
402 set_type_alignment_bytes(type *tp, int align) {
403 set_type_size_bits(tp, 8*align);
406 /* Returns a human readable string for the enum entry. */
407 const char *get_type_state_name(type_state s) {
408 #define X(a) case a: return #a;
418 type_state (get_type_state)(const type *tp) {
419 return _get_type_state(tp);
423 set_type_state(type *tp, type_state state) {
424 assert(tp && tp->kind == k_type);
426 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
427 (tp->type_op == type_method))
430 /* Just a correctness check: */
431 if (state == layout_fixed) {
433 switch (get_type_tpop_code(tp)) {
436 assert(get_type_size_bits(tp) > -1);
437 if (tp != get_glob_type()) {
438 int n_mem = get_class_n_members(tp);
439 for (i = 0; i < n_mem; i++) {
440 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
441 { DDMT(tp); DDME(get_class_member(tp, i)); }
442 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
444 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
445 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
452 assert(get_type_size_bits(tp) > -1);
453 for (i = 0; i < get_struct_n_members(tp); i++) {
454 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
455 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
464 Assure that only innermost dimension is dynamic? */
466 case tpo_enumeration:
468 assert(get_type_mode != NULL);
469 for (i = 0; i < get_enumeration_n_enums(tp); i++)
470 assert(get_enumeration_enum(tp, i) != NULL);
478 unsigned long (get_type_visited)(const type *tp) {
479 return _get_type_visited(tp);
482 void (set_type_visited)(type *tp, unsigned long num) {
483 _set_type_visited(tp, num);
486 /* Sets visited field in type to type_visited. */
487 void (mark_type_visited)(type *tp) {
488 _mark_type_visited(tp);
491 /* @@@ name clash with master flag
492 int (type_visited)(const type *tp) {
493 return _type_visited(tp);
496 int (type_not_visited)(const type *tp) {
497 return _type_not_visited(tp);
500 int (is_type)(const void *thing) {
501 return _is_type(thing);
504 /* Checks whether two types are structural equal.*/
505 int equal_type(type *typ1, type *typ2) {
510 if (typ1 == typ2) return 1;
512 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
513 (get_type_ident(typ1) != get_type_ident(typ2)) ||
514 (get_type_mode(typ1) != get_type_mode(typ2)) ||
515 (get_type_state(typ1) != get_type_state(typ2)))
517 if ((get_type_state(typ1) == layout_fixed) &&
518 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
521 switch(get_type_tpop_code(typ1)) {
523 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
524 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
525 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
526 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
527 /** Compare the members **/
528 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
529 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
530 /* First sort the members of typ2 */
531 for (i = 0; i < get_class_n_members(typ1); i++) {
532 entity *e1 = get_class_member(typ1, i);
533 for (j = 0; j < get_class_n_members(typ2); j++) {
534 entity *e2 = get_class_member(typ2, j);
535 if (get_entity_name(e1) == get_entity_name(e2))
539 for (i = 0; i < get_class_n_members(typ1); i++) {
540 if (!m[i] || /* Found no counterpart */
541 !equal_entity(get_class_member(typ1, i), m[i]))
544 /** Compare the supertypes **/
545 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
546 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
547 /* First sort the supertypes of typ2 */
548 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
549 type *t1 = get_class_supertype(typ1, i);
550 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
551 type *t2 = get_class_supertype(typ2, j);
552 if (get_type_ident(t2) == get_type_ident(t1))
556 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
557 if (!t[i] || /* Found no counterpart */
558 get_class_supertype(typ1, i) != t[i])
563 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
564 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
565 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
566 /* First sort the members of lt */
567 for (i = 0; i < get_struct_n_members(typ1); i++) {
568 entity *e1 = get_struct_member(typ1, i);
569 for (j = 0; j < get_struct_n_members(typ2); j++) {
570 entity *e2 = get_struct_member(typ2, j);
571 if (get_entity_name(e1) == get_entity_name(e2))
575 for (i = 0; i < get_struct_n_members(typ1); i++) {
576 if (!m[i] || /* Found no counterpart */
577 !equal_entity(get_struct_member(typ1, i), m[i]))
582 int n_param1, n_param2;
584 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
585 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
587 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
588 n_param1 = get_method_n_params(typ1);
589 n_param2 = get_method_n_params(typ2);
592 n_param1 = get_method_first_variadic_param_index(typ1);
593 n_param2 = get_method_first_variadic_param_index(typ2);
596 if (n_param1 != n_param2) return 0;
598 for (i = 0; i < n_param1; i++) {
599 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
602 for (i = 0; i < get_method_n_ress(typ1); i++) {
603 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
608 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
609 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
610 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
611 /* First sort the members of lt */
612 for (i = 0; i < get_union_n_members(typ1); i++) {
613 entity *e1 = get_union_member(typ1, i);
614 for (j = 0; j < get_union_n_members(typ2); j++) {
615 entity *e2 = get_union_member(typ2, j);
616 if (get_entity_name(e1) == get_entity_name(e2))
620 for (i = 0; i < get_union_n_members(typ1); i++) {
621 if (!m[i] || /* Found no counterpart */
622 !equal_entity(get_union_member(typ1, i), m[i]))
627 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
629 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
631 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
632 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
633 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
635 if (get_array_order(typ1, i) != get_array_order(typ2, i))
636 assert(0 && "type compare with different dimension orders not implemented");
639 case tpo_enumeration: {
640 assert(0 && "enumerations not implemented");
643 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
646 case tpo_primitive: {
653 /* Checks whether two types are structural comparable. */
654 int smaller_type (type *st, type *lt) {
658 if (st == lt) return 1;
660 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
663 switch(get_type_tpop_code(st)) {
665 return is_subclass_of(st, lt);
668 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
669 m = alloca(sizeof(entity *) * get_struct_n_members(st));
670 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
671 /* First sort the members of lt */
672 for (i = 0; i < get_struct_n_members(st); i++) {
673 entity *se = get_struct_member(st, i);
674 for (j = 0; j < get_struct_n_members(lt); j++) {
675 entity *le = get_struct_member(lt, j);
676 if (get_entity_name(le) == get_entity_name(se))
680 for (i = 0; i < get_struct_n_members(st); i++) {
681 if (!m[i] || /* Found no counterpart */
682 !smaller_type(get_entity_type(get_struct_member(st, i)),
683 get_entity_type(m[i])))
688 /** FIXME: is this still 1? */
689 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
690 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
691 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
692 for (i = 0; i < get_method_n_params(st); i++) {
693 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
696 for (i = 0; i < get_method_n_ress(st); i++) {
697 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
702 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
703 m = alloca(sizeof(entity *) * get_union_n_members(st));
704 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
705 /* First sort the members of lt */
706 for (i = 0; i < get_union_n_members(st); i++) {
707 entity *se = get_union_member(st, i);
708 for (j = 0; j < get_union_n_members(lt); j++) {
709 entity *le = get_union_member(lt, j);
710 if (get_entity_name(le) == get_entity_name(se))
714 for (i = 0; i < get_union_n_members(st); i++) {
715 if (!m[i] || /* Found no counterpart */
716 !smaller_type(get_entity_type(get_union_member(st, i)),
717 get_entity_type(m[i])))
722 type *set, *let; /* small/large elt. type */
723 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
725 set = get_array_element_type(st);
726 let = get_array_element_type(lt);
728 /* If the elt types are different, set must be convertible
729 to let, and they must have the same size so that address
730 computations work out. To have a size the layout must
732 if ((get_type_state(set) != layout_fixed) ||
733 (get_type_state(let) != layout_fixed))
735 if (!smaller_type(set, let) ||
736 get_type_size_bits(set) != get_type_size_bits(let))
739 for(i = 0; i < get_array_n_dimensions(st); i++) {
740 if (get_array_lower_bound(lt, i))
741 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
743 if (get_array_upper_bound(lt, i))
744 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
748 case tpo_enumeration: {
749 assert(0 && "enumerations not implemented");
752 if (!smaller_type(get_pointer_points_to_type(st),
753 get_pointer_points_to_type(lt)))
756 case tpo_primitive: {
757 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
765 /*-----------------------------------------------------------------*/
767 /*-----------------------------------------------------------------*/
769 /* create a new class type */
770 type *new_type_class (ident *name) {
773 res = new_type(type_class, NULL, name);
775 res->attr.ca.members = NEW_ARR_F (entity *, 0);
776 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
777 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
778 res->attr.ca.peculiarity = peculiarity_existent;
779 res->attr.ca.dfn = 0;
783 type *new_d_type_class (ident *name, dbg_info* db) {
784 type *res = new_type_class (name);
785 set_type_dbg_info(res, db);
789 void free_class_entities(type *clss) {
791 assert(clss && (clss->type_op == type_class));
792 for (i = get_class_n_members(clss)-1; i >= 0; --i)
793 free_entity(get_class_member(clss, i));
796 void free_class_attrs(type *clss) {
797 assert(clss && (clss->type_op == type_class));
798 DEL_ARR_F(clss->attr.ca.members);
799 DEL_ARR_F(clss->attr.ca.subtypes);
800 DEL_ARR_F(clss->attr.ca.supertypes);
803 /* manipulate private fields of class type */
804 void add_class_member (type *clss, entity *member) {
805 assert(clss && (clss->type_op == type_class));
806 ARR_APP1 (entity *, clss->attr.ca.members, member);
809 int (get_class_n_members) (const type *clss) {
810 return _get_class_n_members(clss);
813 int get_class_member_index(type *clss, entity *mem) {
815 assert(clss && (clss->type_op == type_class));
816 for (i = 0; i < get_class_n_members(clss); i++)
817 if (get_class_member(clss, i) == mem)
822 entity *(get_class_member) (const type *clss, int pos) {
823 return _get_class_member(clss, pos);
826 entity *get_class_member_by_name(type *clss, ident *name) {
828 assert(clss && (clss->type_op == type_class));
829 n_mem = get_class_n_members(clss);
830 for (i = 0; i < n_mem; ++i) {
831 entity *mem = get_class_member(clss, i);
832 if (get_entity_ident(mem) == name) return mem;
837 void set_class_member (type *clss, entity *member, int pos) {
838 assert(clss && (clss->type_op == type_class));
839 assert(pos >= 0 && pos < get_class_n_members(clss));
840 clss->attr.ca.members[pos] = member;
842 void set_class_members (type *clss, entity **members, int arity) {
844 assert(clss && (clss->type_op == type_class));
845 DEL_ARR_F(clss->attr.ca.members);
846 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
847 for (i = 0; i < arity; i++) {
848 set_entity_owner(members[i], clss);
849 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
852 void remove_class_member(type *clss, entity *member) {
854 assert(clss && (clss->type_op == type_class));
855 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
856 if (clss->attr.ca.members[i] == member) {
857 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
858 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
859 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
865 void add_class_subtype (type *clss, type *subtype) {
867 assert(clss && (clss->type_op == type_class));
868 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
869 for (i = 0; i < get_class_n_supertypes(subtype); i++)
870 if (get_class_supertype(subtype, i) == clss)
871 /* Class already registered */
873 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
875 int get_class_n_subtypes (const type *clss) {
876 assert(clss && (clss->type_op == type_class));
877 return (ARR_LEN (clss->attr.ca.subtypes));
879 type *get_class_subtype (type *clss, int pos) {
880 assert(clss && (clss->type_op == type_class));
881 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
882 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
884 int get_class_subtype_index(type *clss, const type *subclass) {
885 int i, n_subtypes = get_class_n_subtypes(clss);
886 assert(is_Class_type(subclass));
887 for (i = 0; i < n_subtypes; ++i) {
888 if (get_class_subtype(clss, i) == subclass) return i;
892 void set_class_subtype (type *clss, type *subtype, int pos) {
893 assert(clss && (clss->type_op == type_class));
894 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
895 clss->attr.ca.subtypes[pos] = subtype;
897 void remove_class_subtype(type *clss, type *subtype) {
899 assert(clss && (clss->type_op == type_class));
900 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
901 if (clss->attr.ca.subtypes[i] == subtype) {
902 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
903 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
904 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
909 void add_class_supertype (type *clss, type *supertype) {
911 assert(clss && (clss->type_op == type_class));
912 assert(supertype && (supertype -> type_op == type_class));
913 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
914 for (i = 0; i < get_class_n_subtypes(supertype); i++)
915 if (get_class_subtype(supertype, i) == clss)
916 /* Class already registered */
918 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
920 int get_class_n_supertypes (const type *clss) {
921 assert(clss && (clss->type_op == type_class));
922 return (ARR_LEN (clss->attr.ca.supertypes));
924 int get_class_supertype_index(type *clss, type *super_clss) {
925 int i, n_supertypes = get_class_n_supertypes(clss);
926 assert(super_clss && (super_clss->type_op == type_class));
927 for (i = 0; i < n_supertypes; i++)
928 if (get_class_supertype(clss, i) == super_clss)
932 type *get_class_supertype (type *clss, int pos) {
933 assert(clss && (clss->type_op == type_class));
934 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
935 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
937 void set_class_supertype (type *clss, type *supertype, int pos) {
938 assert(clss && (clss->type_op == type_class));
939 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
940 clss->attr.ca.supertypes[pos] = supertype;
942 void remove_class_supertype(type *clss, type *supertype) {
944 assert(clss && (clss->type_op == type_class));
945 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
946 if (clss->attr.ca.supertypes[i] == supertype) {
947 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
948 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
949 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
954 const char *get_peculiarity_string(peculiarity p) {
955 #define X(a) case a: return #a
957 X(peculiarity_description);
958 X(peculiarity_inherited);
959 X(peculiarity_existent);
962 return "invalid peculiarity";
965 peculiarity get_class_peculiarity (const type *clss) {
966 assert(clss && (clss->type_op == type_class));
967 return clss->attr.ca.peculiarity;
970 void set_class_peculiarity (type *clss, peculiarity pec) {
971 assert(clss && (clss->type_op == type_class));
972 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
973 clss->attr.ca.peculiarity = pec;
976 void set_class_dfn (type *clss, int dfn)
978 clss->attr.ca.dfn = dfn;
981 int get_class_dfn (const type *clss)
983 return (clss->attr.ca.dfn);
987 int (is_Class_type)(const type *clss) {
988 return _is_class_type(clss);
991 /*----------------------------------------------------------------**/
993 /*----------------------------------------------------------------**/
995 /* create a new type struct */
996 type *new_type_struct (ident *name) {
998 res = new_type(type_struct, NULL, name);
999 res->attr.sa.members = NEW_ARR_F (entity *, 0);
1002 type *new_d_type_struct (ident *name, dbg_info* db) {
1003 type *res = new_type_struct (name);
1004 set_type_dbg_info(res, db);
1007 void free_struct_entities (type *strct) {
1009 assert(strct && (strct->type_op == type_struct));
1010 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1011 free_entity(get_struct_member(strct, i));
1013 void free_struct_attrs (type *strct) {
1014 assert(strct && (strct->type_op == type_struct));
1015 DEL_ARR_F(strct->attr.sa.members);
1018 /* manipulate private fields of struct */
1019 int get_struct_n_members (const type *strct) {
1020 assert(strct && (strct->type_op == type_struct));
1021 return (ARR_LEN (strct->attr.sa.members));
1024 void add_struct_member (type *strct, entity *member) {
1025 assert(strct && (strct->type_op == type_struct));
1026 assert(get_type_tpop(get_entity_type(member)) != type_method);
1027 /* @@@ lowerfirm geht nicht durch */
1028 ARR_APP1 (entity *, strct->attr.sa.members, member);
1031 entity *get_struct_member (const type *strct, int pos) {
1032 assert(strct && (strct->type_op == type_struct));
1033 assert(pos >= 0 && pos < get_struct_n_members(strct));
1034 return strct->attr.sa.members[pos];
1037 int get_struct_member_index(type *strct, entity *mem) {
1039 assert(strct && (strct->type_op == type_struct));
1040 for (i = 0; i < get_struct_n_members(strct); i++)
1041 if (get_struct_member(strct, i) == mem)
1046 void set_struct_member (type *strct, int pos, entity *member) {
1047 assert(strct && (strct->type_op == type_struct));
1048 assert(pos >= 0 && pos < get_struct_n_members(strct));
1049 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1050 strct->attr.sa.members[pos] = member;
1052 void remove_struct_member(type *strct, entity *member) {
1054 assert(strct && (strct->type_op == type_struct));
1055 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1056 if (strct->attr.sa.members[i] == member) {
1057 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1058 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1059 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1065 int (is_Struct_type)(const type *strct) {
1066 return _is_struct_type(strct);
1069 /*******************************************************************/
1071 /*******************************************************************/
1074 * Lazy construction of value argument / result representation.
1075 * Constructs a struct type and its member. The types of the members
1076 * are passed in the argument list.
1078 * @param name name of the type constructed
1079 * @param len number of fields
1080 * @param tps array of field types with length len
1082 static INLINE type *
1083 build_value_type(ident *name, int len, type **tps) {
1085 type *res = new_type_struct(name);
1086 /* Remove type from type list. Must be treated differently than other types. */
1087 remove_irp_type_from_list(res);
1088 for (i = 0; i < len; i++) {
1089 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1090 if (tps[i]) elt_type = tps[i];
1091 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1096 /* Create a new method type.
1097 N_param is the number of parameters, n_res the number of results. */
1098 type *new_type_method (ident *name, int n_param, int n_res) {
1101 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1102 res = new_type(type_method, mode_P_mach, name);
1103 res->state = layout_fixed;
1104 res->size = get_mode_size_bits(mode_P_mach);
1105 res->attr.ma.n_params = n_param;
1106 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1107 res->attr.ma.value_params = NULL;
1108 res->attr.ma.n_res = n_res;
1109 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1110 res->attr.ma.value_ress = NULL;
1111 res->attr.ma.variadicity = variadicity_non_variadic;
1112 res->attr.ma.first_variadic_param = -1;
1117 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1118 type *res = new_type_method (name, n_param, n_res);
1119 set_type_dbg_info(res, db);
1123 void free_method_entities(type *method) {
1124 assert(method && (method->type_op == type_method));
1127 /* Attention: also frees entities in value parameter subtypes! */
1128 void free_method_attrs(type *method) {
1129 assert(method && (method->type_op == type_method));
1130 free(method->attr.ma.param_type);
1131 free(method->attr.ma.res_type);
1132 if (method->attr.ma.value_params) {
1133 free_type_entities(method->attr.ma.value_params);
1134 free_type(method->attr.ma.value_params);
1136 if (method->attr.ma.value_ress) {
1137 free_type_entities(method->attr.ma.value_ress);
1138 free_type(method->attr.ma.value_ress);
1142 /* manipulate private fields of method. */
1143 int get_method_n_params (const type *method) {
1144 assert(method && (method->type_op == type_method));
1145 return method->attr.ma.n_params;
1148 type *get_method_param_type(type *method, int pos) {
1150 assert(method && (method->type_op == type_method));
1151 assert(pos >= 0 && pos < get_method_n_params(method));
1152 res = method->attr.ma.param_type[pos];
1153 assert(res != NULL && "empty method param type");
1154 return method->attr.ma.param_type[pos] = skip_tid(res);
1157 void set_method_param_type(type *method, int pos, type* tp) {
1158 assert(method && (method->type_op == type_method));
1159 assert(pos >= 0 && pos < get_method_n_params(method));
1160 method->attr.ma.param_type[pos] = tp;
1161 /* If information constructed set pass-by-value representation. */
1162 if (method->attr.ma.value_params) {
1163 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1164 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1168 /* Returns an entity that represents the copied value argument. Only necessary
1169 for compounds passed by value. */
1170 entity *get_method_value_param_ent(type *method, int pos) {
1171 assert(method && (method->type_op == type_method));
1172 assert(pos >= 0 && pos < get_method_n_params(method));
1173 if (!method->attr.ma.value_params)
1174 method->attr.ma.value_params
1175 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1176 get_method_n_params(method), method->attr.ma.param_type);
1177 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1178 != method->attr.ma.value_params)
1179 && "param type not yet set");
1180 return get_struct_member(method->attr.ma.value_params, pos);
1184 * Returns a type that represents the copied value arguments.
1186 type *get_method_value_param_type(const type *method)
1188 assert(method && (method->type_op == type_method));
1189 return method->attr.ma.value_params;
1192 int get_method_n_ress (const type *method) {
1193 assert(method && (method->type_op == type_method));
1194 return method->attr.ma.n_res;
1197 type *get_method_res_type(type *method, int pos) {
1199 assert(method && (method->type_op == type_method));
1200 assert(pos >= 0 && pos < get_method_n_ress(method));
1201 res = method->attr.ma.res_type[pos];
1202 assert(res != NULL && "empty method return type");
1203 return method->attr.ma.res_type[pos] = skip_tid(res);
1206 void set_method_res_type(type *method, int pos, type* tp) {
1207 assert(method && (method->type_op == type_method));
1208 assert(pos >= 0 && pos < get_method_n_ress(method));
1209 /* set the result type */
1210 method->attr.ma.res_type[pos] = tp;
1211 /* If information constructed set pass-by-value representation. */
1212 if (method->attr.ma.value_ress) {
1213 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1214 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1218 /* Returns an entity that represents the copied value result. Only necessary
1219 for compounds passed by value. */
1220 entity *get_method_value_res_ent(type *method, int pos) {
1221 assert(method && (method->type_op == type_method));
1222 assert(pos >= 0 && pos < get_method_n_ress(method));
1223 if (!method->attr.ma.value_ress)
1224 method->attr.ma.value_ress
1225 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1226 get_method_n_ress(method), method->attr.ma.res_type);
1227 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1228 && "result type not yet set");
1229 return get_struct_member(method->attr.ma.value_ress, pos);
1233 * Returns a type that represents the copied value results.
1235 type *get_method_value_res_type(const type *method) {
1236 assert(method && (method->type_op == type_method));
1237 return method->attr.ma.value_ress;
1240 /* Returns the null-terminated name of this variadicity. */
1241 const char *get_variadicity_name(variadicity vari)
1243 #define X(a) case a: return #a
1245 X(variadicity_non_variadic);
1246 X(variadicity_variadic);
1253 variadicity get_method_variadicity(const type *method)
1255 assert(method && (method->type_op == type_method));
1256 return method->attr.ma.variadicity;
1259 void set_method_variadicity(type *method, variadicity vari)
1261 assert(method && (method->type_op == type_method));
1262 method->attr.ma.variadicity = vari;
1266 * Returns the first variadic parameter index of a type.
1267 * If this index was NOT set, the index of the last parameter
1268 * of the method type plus one is returned for variadic functions.
1269 * Non-variadic function types always return -1 here.
1271 int get_method_first_variadic_param_index(const type *method)
1273 assert(method && (method->type_op == type_method));
1275 if (method->attr.ma.variadicity == variadicity_non_variadic)
1278 if (method->attr.ma.first_variadic_param == -1)
1279 return get_method_n_params(method);
1280 return method->attr.ma.first_variadic_param;
1284 * Sets the first variadic parameter index. This allows to specify
1285 * a complete call type (containing the type of all parameters)
1286 * but still have the knowledge, which parameter must be passed as
1289 void set_method_first_variadic_param_index(type *method, int index)
1291 assert(method && (method->type_op == type_method));
1292 assert(index >= 0 && index <= get_method_n_params(method));
1294 method->attr.ma.first_variadic_param = index;
1298 int (is_Method_type)(const type *method) {
1299 return _is_method_type(method);
1302 /*-----------------------------------------------------------------*/
1304 /*-----------------------------------------------------------------*/
1306 /* create a new type uni */
1307 type *new_type_union (ident *name) {
1309 res = new_type(type_union, NULL, name);
1310 /*res->attr.ua.unioned_type = xcalloc(n_types, sizeof(res->attr.ua.unioned_type[0]));
1311 res->attr.ua.delim_names = xcalloc(n_types, sizeof(res->attr.ua.delim_names[0])); */
1312 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1315 type *new_d_type_union (ident *name, dbg_info* db) {
1316 type *res = new_type_union (name);
1317 set_type_dbg_info(res, db);
1320 void free_union_entities (type *uni) {
1322 assert(uni && (uni->type_op == type_union));
1323 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1324 free_entity(get_union_member(uni, i));
1326 void free_union_attrs (type *uni) {
1327 assert(uni && (uni->type_op == type_union));
1328 DEL_ARR_F(uni->attr.ua.members);
1330 /* manipulate private fields of union */
1332 int get_union_n_types (type *uni) {
1333 assert(uni && (uni->type_op == type_union));
1334 return uni->attr.ua.n_types;
1336 type *get_union_unioned_type (type *uni, int pos) {
1337 assert(uni && (uni->type_op == type_union));
1338 assert(pos >= 0 && pos < get_union_n_types(uni));
1339 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1341 void set_union_unioned_type (type *uni, int pos, type *tp) {
1342 assert(uni && (uni->type_op == type_union));
1343 assert(pos >= 0 && pos < get_union_n_types(uni));
1344 uni->attr.ua.unioned_type[pos] = tp;
1346 ident *get_union_delim_nameid (type *uni, int pos) {
1347 assert(uni && (uni->type_op == type_union));
1348 assert(pos >= 0 && pos < get_union_n_types(uni));
1349 return uni->attr.ua.delim_names[pos];
1351 const char *get_union_delim_name (type *uni, int pos) {
1352 assert(uni && (uni->type_op == type_union));
1353 assert(pos >= 0 && pos < get_union_n_types(uni));
1354 return get_id_str(uni->attr.ua.delim_names[pos]);
1356 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1357 assert(uni && (uni->type_op == type_union));
1358 assert(pos >= 0 && pos < get_union_n_types(uni));
1359 uni->attr.ua.delim_names[pos] = id;
1362 int get_union_n_members (const type *uni) {
1363 assert(uni && (uni->type_op == type_union));
1364 return (ARR_LEN (uni->attr.ua.members));
1366 void add_union_member (type *uni, entity *member) {
1367 assert(uni && (uni->type_op == type_union));
1368 ARR_APP1 (entity *, uni->attr.ua.members, member);
1370 entity *get_union_member (const type *uni, int pos) {
1371 assert(uni && (uni->type_op == type_union));
1372 assert(pos >= 0 && pos < get_union_n_members(uni));
1373 return uni->attr.ua.members[pos];
1375 void set_union_member (type *uni, int pos, entity *member) {
1376 assert(uni && (uni->type_op == type_union));
1377 assert(pos >= 0 && pos < get_union_n_members(uni));
1378 uni->attr.ua.members[pos] = member;
1380 void remove_union_member(type *uni, entity *member) {
1382 assert(uni && (uni->type_op == type_union));
1383 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1384 if (uni->attr.ua.members[i] == member) {
1385 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1386 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1387 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1393 int (is_Union_type)(const type *uni) {
1394 return _is_union_type(uni);
1397 /*-----------------------------------------------------------------*/
1399 /*-----------------------------------------------------------------*/
1402 /* create a new type array -- set dimension sizes independently */
1403 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1406 ir_graph *rem = current_ir_graph;
1407 assert(!is_Method_type(element_type));
1409 res = new_type(type_array, NULL, name);
1410 res->attr.aa.n_dimensions = n_dimensions;
1411 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1412 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1413 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1415 current_ir_graph = get_const_code_irg();
1416 for (i = 0; i < n_dimensions; i++) {
1417 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1418 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1419 res->attr.aa.order[i] = i;
1421 current_ir_graph = rem;
1423 res->attr.aa.element_type = element_type;
1424 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1429 type *new_d_type_array (ident *name, int n_dimensions,
1430 type *element_type, dbg_info* db) {
1431 type *res = new_type_array (name, n_dimensions, element_type);
1432 set_type_dbg_info(res, db);
1436 void free_array_entities (type *array) {
1437 assert(array && (array->type_op == type_array));
1440 void free_array_attrs (type *array) {
1441 assert(array && (array->type_op == type_array));
1442 free(array->attr.aa.lower_bound);
1443 free(array->attr.aa.upper_bound);
1446 /* manipulate private fields of array type */
1447 int get_array_n_dimensions (const type *array) {
1448 assert(array && (array->type_op == type_array));
1449 return array->attr.aa.n_dimensions;
1453 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1454 ir_node * upper_bound) {
1455 assert(array && (array->type_op == type_array));
1456 assert(lower_bound && "lower_bound node may not be NULL.");
1457 assert(upper_bound && "upper_bound node may not be NULL.");
1458 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1459 array->attr.aa.lower_bound[dimension] = lower_bound;
1460 array->attr.aa.upper_bound[dimension] = upper_bound;
1463 set_array_bounds_int (type *array, int dimension, int lower_bound,
1465 ir_graph *rem = current_ir_graph;
1466 current_ir_graph = get_const_code_irg();
1467 set_array_bounds (array, dimension,
1468 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1469 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1470 current_ir_graph = rem;
1473 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1474 assert(array && (array->type_op == type_array));
1475 assert(lower_bound && "lower_bound node may not be NULL.");
1476 array->attr.aa.lower_bound[dimension] = lower_bound;
1478 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1479 ir_graph *rem = current_ir_graph;
1480 current_ir_graph = get_const_code_irg();
1481 set_array_lower_bound (array, dimension,
1482 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1483 current_ir_graph = rem;
1486 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1487 assert(array && (array->type_op == type_array));
1488 assert(upper_bound && "upper_bound node may not be NULL.");
1489 array->attr.aa.upper_bound[dimension] = upper_bound;
1491 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1492 ir_graph *rem = current_ir_graph;
1493 current_ir_graph = get_const_code_irg();
1494 set_array_upper_bound (array, dimension,
1495 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1496 current_ir_graph = rem;
1498 int has_array_lower_bound (const type *array, int dimension) {
1499 assert(array && (array->type_op == type_array));
1500 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1502 ir_node *get_array_lower_bound (const type *array, int dimension) {
1503 assert(array && (array->type_op == type_array));
1504 return array->attr.aa.lower_bound[dimension];
1506 long get_array_lower_bound_int (const type *array, int dimension) {
1508 assert(array && (array->type_op == type_array));
1509 node = array->attr.aa.lower_bound[dimension];
1510 assert(get_irn_op(node) == op_Const);
1511 return get_tarval_long(get_Const_tarval(node));
1513 int has_array_upper_bound (const type *array, int dimension) {
1514 assert(array && (array->type_op == type_array));
1515 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1517 ir_node * get_array_upper_bound (const type *array, int dimension) {
1518 assert(array && (array->type_op == type_array));
1519 return array->attr.aa.upper_bound[dimension];
1521 long get_array_upper_bound_int (const type *array, int dimension) {
1523 assert(array && (array->type_op == type_array));
1524 node = array->attr.aa.upper_bound[dimension];
1525 assert(get_irn_op(node) == op_Const);
1526 return get_tarval_long(get_Const_tarval(node));
1529 void set_array_order (type *array, int dimension, int order) {
1530 assert(array && (array->type_op == type_array));
1531 array->attr.aa.order[dimension] = order;
1534 int get_array_order (const type *array, int dimension) {
1535 assert(array && (array->type_op == type_array));
1536 return array->attr.aa.order[dimension];
1539 int find_array_dimension(const type *array, int order) {
1542 assert(array && (array->type_op == type_array));
1544 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1545 if (array->attr.aa.order[dim] == order)
1551 void set_array_element_type (type *array, type *tp) {
1552 assert(array && (array->type_op == type_array));
1553 assert(!is_Method_type(tp));
1554 array->attr.aa.element_type = tp;
1556 type *get_array_element_type (type *array) {
1557 assert(array && (array->type_op == type_array));
1558 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1561 void set_array_element_entity (type *array, entity *ent) {
1562 assert(array && (array->type_op == type_array));
1563 assert((get_entity_type(ent)->type_op != type_method));
1564 array->attr.aa.element_ent = ent;
1565 array->attr.aa.element_type = get_entity_type(ent);
1567 entity *get_array_element_entity (const type *array) {
1568 assert(array && (array->type_op == type_array));
1569 return array->attr.aa.element_ent;
1573 int (is_Array_type)(const type *array) {
1574 return _is_array_type(array);
1577 /*-----------------------------------------------------------------*/
1578 /* TYPE_ENUMERATION */
1579 /*-----------------------------------------------------------------*/
1581 /* create a new type enumeration -- set the enumerators independently */
1582 type *new_type_enumeration (ident *name, int n_enums) {
1584 res = new_type(type_enumeration, NULL, name);
1585 res->attr.ea.n_enums = n_enums;
1586 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1587 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1590 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1591 type *res = new_type_enumeration (name, n_enums);
1592 set_type_dbg_info(res, db);
1596 void free_enumeration_entities(type *enumeration) {
1597 assert(enumeration && (enumeration->type_op == type_enumeration));
1599 void free_enumeration_attrs(type *enumeration) {
1600 assert(enumeration && (enumeration->type_op == type_enumeration));
1601 free(enumeration->attr.ea.enumer);
1602 free(enumeration->attr.ea.enum_nameid);
1605 /* manipulate fields of enumeration type. */
1606 int get_enumeration_n_enums (const type *enumeration) {
1607 assert(enumeration && (enumeration->type_op == type_enumeration));
1608 return enumeration->attr.ea.n_enums;
1610 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1611 assert(enumeration && (enumeration->type_op == type_enumeration));
1612 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1613 enumeration->attr.ea.enumer[pos] = con;
1615 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1616 assert(enumeration && (enumeration->type_op == type_enumeration));
1617 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1618 return enumeration->attr.ea.enumer[pos];
1620 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1621 assert(enumeration && (enumeration->type_op == type_enumeration));
1622 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1623 enumeration->attr.ea.enum_nameid[pos] = id;
1625 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1626 assert(enumeration && (enumeration->type_op == type_enumeration));
1627 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1628 return enumeration->attr.ea.enum_nameid[pos];
1630 const char *get_enumeration_name(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 get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1637 int (is_Enumeration_type)(const type *enumeration) {
1638 return _is_enumeration_type(enumeration);
1641 /*-----------------------------------------------------------------*/
1643 /*-----------------------------------------------------------------*/
1645 /* Create a new type pointer */
1646 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1648 assert(mode_is_reference(ptr_mode));
1649 res = new_type(type_pointer, ptr_mode, name);
1650 res->attr.pa.points_to = points_to;
1651 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1652 res->size = get_mode_size_bits(res->mode);
1653 res->state = layout_fixed;
1656 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1657 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1658 set_type_dbg_info(res, db);
1661 void free_pointer_entities (type *pointer) {
1662 assert(pointer && (pointer->type_op == type_pointer));
1664 void free_pointer_attrs (type *pointer) {
1665 assert(pointer && (pointer->type_op == type_pointer));
1667 /* manipulate fields of type_pointer */
1668 void set_pointer_points_to_type (type *pointer, type *tp) {
1669 assert(pointer && (pointer->type_op == type_pointer));
1670 pointer->attr.pa.points_to = tp;
1672 type *get_pointer_points_to_type (type *pointer) {
1673 assert(pointer && (pointer->type_op == type_pointer));
1674 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1678 int (is_Pointer_type)(const type *pointer) {
1679 return _is_pointer_type(pointer);
1682 /* Returns the first pointer type that has as points_to tp.
1683 * Not efficient: O(#types).
1684 * If not found returns firm_unknown_type. */
1685 type *find_pointer_type_to_type (type *tp) {
1687 for (i = 0; i < get_irp_n_types(); ++i) {
1688 type *found = get_irp_type(i);
1689 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1692 return firm_unknown_type;
1697 /*-----------------------------------------------------------------*/
1698 /* TYPE_PRIMITIVE */
1699 /*-----------------------------------------------------------------*/
1701 /* create a new type primitive */
1702 type *new_type_primitive (ident *name, ir_mode *mode) {
1704 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1705 res = new_type(type_primitive, mode, name);
1706 res->size = get_mode_size_bits(mode);
1707 res->state = layout_fixed;
1710 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1711 type *res = new_type_primitive (name, mode);
1712 set_type_dbg_info(res, db);
1715 void free_primitive_entities (type *primitive) {
1716 assert(primitive && (primitive->type_op == type_primitive));
1718 void free_primitive_attrs (type *primitive) {
1719 assert(primitive && (primitive->type_op == type_primitive));
1723 int (is_Primitive_type)(const type *primitive) {
1724 return _is_primitive_type(primitive);
1727 /*-----------------------------------------------------------------*/
1728 /* common functionality */
1729 /*-----------------------------------------------------------------*/
1732 int (is_atomic_type)(const type *tp) {
1733 return _is_atomic_type(tp);
1737 * Gets the number of elements in a firm compound type.
1739 int get_compound_n_members(const type *tp)
1743 if (is_Struct_type(tp))
1744 res = get_struct_n_members(tp);
1745 else if (is_Class_type(tp))
1746 res = get_class_n_members(tp);
1747 else if (is_Union_type(tp))
1748 res = get_union_n_members(tp);
1750 assert(0 && "need struct, union or class for member count");
1756 * Gets the member of a firm compound type at position pos.
1758 entity *get_compound_member(const type *tp, int pos)
1762 if (is_Struct_type(tp))
1763 res = get_struct_member(tp, pos);
1764 else if (is_Class_type(tp))
1765 res = get_class_member(tp, pos);
1766 else if (is_Union_type(tp))
1767 res = get_union_member(tp, pos);
1770 assert(0 && "need struct, union or class to get a member");
1778 int is_compound_type(const type *tp) {
1779 assert(tp && tp->kind == k_type);
1780 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;