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 assert(clss != get_entity_type(member) && "recursive type");
807 ARR_APP1 (entity *, clss->attr.ca.members, member);
810 int (get_class_n_members) (const type *clss) {
811 return _get_class_n_members(clss);
814 int get_class_member_index(type *clss, entity *mem) {
816 assert(clss && (clss->type_op == type_class));
817 for (i = 0; i < get_class_n_members(clss); i++)
818 if (get_class_member(clss, i) == mem)
823 entity *(get_class_member) (const type *clss, int pos) {
824 return _get_class_member(clss, pos);
827 entity *get_class_member_by_name(type *clss, ident *name) {
829 assert(clss && (clss->type_op == type_class));
830 n_mem = get_class_n_members(clss);
831 for (i = 0; i < n_mem; ++i) {
832 entity *mem = get_class_member(clss, i);
833 if (get_entity_ident(mem) == name) return mem;
838 void set_class_member (type *clss, entity *member, int pos) {
839 assert(clss && (clss->type_op == type_class));
840 assert(pos >= 0 && pos < get_class_n_members(clss));
841 clss->attr.ca.members[pos] = member;
843 void set_class_members (type *clss, entity **members, int arity) {
845 assert(clss && (clss->type_op == type_class));
846 DEL_ARR_F(clss->attr.ca.members);
847 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
848 for (i = 0; i < arity; i++) {
849 set_entity_owner(members[i], clss);
850 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
853 void remove_class_member(type *clss, entity *member) {
855 assert(clss && (clss->type_op == type_class));
856 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
857 if (clss->attr.ca.members[i] == member) {
858 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
859 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
860 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
866 void add_class_subtype (type *clss, type *subtype) {
868 assert(clss && (clss->type_op == type_class));
869 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
870 for (i = 0; i < get_class_n_supertypes(subtype); i++)
871 if (get_class_supertype(subtype, i) == clss)
872 /* Class already registered */
874 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
876 int get_class_n_subtypes (const type *clss) {
877 assert(clss && (clss->type_op == type_class));
878 return (ARR_LEN (clss->attr.ca.subtypes));
880 type *get_class_subtype (type *clss, int pos) {
881 assert(clss && (clss->type_op == type_class));
882 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
883 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
885 int get_class_subtype_index(type *clss, const type *subclass) {
886 int i, n_subtypes = get_class_n_subtypes(clss);
887 assert(is_Class_type(subclass));
888 for (i = 0; i < n_subtypes; ++i) {
889 if (get_class_subtype(clss, i) == subclass) return i;
893 void set_class_subtype (type *clss, type *subtype, int pos) {
894 assert(clss && (clss->type_op == type_class));
895 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
896 clss->attr.ca.subtypes[pos] = subtype;
898 void remove_class_subtype(type *clss, type *subtype) {
900 assert(clss && (clss->type_op == type_class));
901 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
902 if (clss->attr.ca.subtypes[i] == subtype) {
903 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
904 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
905 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
910 void add_class_supertype (type *clss, type *supertype) {
912 assert(clss && (clss->type_op == type_class));
913 assert(supertype && (supertype -> type_op == type_class));
914 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
915 for (i = 0; i < get_class_n_subtypes(supertype); i++)
916 if (get_class_subtype(supertype, i) == clss)
917 /* Class already registered */
919 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
921 int get_class_n_supertypes (const type *clss) {
922 assert(clss && (clss->type_op == type_class));
923 return (ARR_LEN (clss->attr.ca.supertypes));
925 int get_class_supertype_index(type *clss, type *super_clss) {
926 int i, n_supertypes = get_class_n_supertypes(clss);
927 assert(super_clss && (super_clss->type_op == type_class));
928 for (i = 0; i < n_supertypes; i++)
929 if (get_class_supertype(clss, i) == super_clss)
933 type *get_class_supertype (type *clss, int pos) {
934 assert(clss && (clss->type_op == type_class));
935 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
936 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
938 void set_class_supertype (type *clss, type *supertype, int pos) {
939 assert(clss && (clss->type_op == type_class));
940 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
941 clss->attr.ca.supertypes[pos] = supertype;
943 void remove_class_supertype(type *clss, type *supertype) {
945 assert(clss && (clss->type_op == type_class));
946 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
947 if (clss->attr.ca.supertypes[i] == supertype) {
948 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
949 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
950 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
955 const char *get_peculiarity_string(peculiarity p) {
956 #define X(a) case a: return #a
958 X(peculiarity_description);
959 X(peculiarity_inherited);
960 X(peculiarity_existent);
963 return "invalid peculiarity";
966 peculiarity get_class_peculiarity (const type *clss) {
967 assert(clss && (clss->type_op == type_class));
968 return clss->attr.ca.peculiarity;
971 void set_class_peculiarity (type *clss, peculiarity pec) {
972 assert(clss && (clss->type_op == type_class));
973 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
974 clss->attr.ca.peculiarity = pec;
977 void set_class_dfn (type *clss, int dfn)
979 clss->attr.ca.dfn = dfn;
982 int get_class_dfn (const type *clss)
984 return (clss->attr.ca.dfn);
988 int (is_Class_type)(const type *clss) {
989 return _is_class_type(clss);
992 /*----------------------------------------------------------------**/
994 /*----------------------------------------------------------------**/
996 /* create a new type struct */
997 type *new_type_struct (ident *name) {
999 res = new_type(type_struct, NULL, name);
1000 res->attr.sa.members = NEW_ARR_F (entity *, 0);
1003 type *new_d_type_struct (ident *name, dbg_info* db) {
1004 type *res = new_type_struct (name);
1005 set_type_dbg_info(res, db);
1008 void free_struct_entities (type *strct) {
1010 assert(strct && (strct->type_op == type_struct));
1011 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1012 free_entity(get_struct_member(strct, i));
1014 void free_struct_attrs (type *strct) {
1015 assert(strct && (strct->type_op == type_struct));
1016 DEL_ARR_F(strct->attr.sa.members);
1019 /* manipulate private fields of struct */
1020 int get_struct_n_members (const type *strct) {
1021 assert(strct && (strct->type_op == type_struct));
1022 return (ARR_LEN (strct->attr.sa.members));
1025 void add_struct_member (type *strct, entity *member) {
1026 assert(strct && (strct->type_op == type_struct));
1027 assert(get_type_tpop(get_entity_type(member)) != type_method);
1028 /* @@@ lowerfirm geht nicht durch */
1029 assert(strct != get_entity_type(member) && "recursive type");
1030 ARR_APP1 (entity *, strct->attr.sa.members, member);
1033 entity *get_struct_member (const type *strct, int pos) {
1034 assert(strct && (strct->type_op == type_struct));
1035 assert(pos >= 0 && pos < get_struct_n_members(strct));
1036 return strct->attr.sa.members[pos];
1039 int get_struct_member_index(type *strct, entity *mem) {
1041 assert(strct && (strct->type_op == type_struct));
1042 for (i = 0; i < get_struct_n_members(strct); i++)
1043 if (get_struct_member(strct, i) == mem)
1048 void set_struct_member (type *strct, int pos, entity *member) {
1049 assert(strct && (strct->type_op == type_struct));
1050 assert(pos >= 0 && pos < get_struct_n_members(strct));
1051 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1052 strct->attr.sa.members[pos] = member;
1054 void remove_struct_member(type *strct, entity *member) {
1056 assert(strct && (strct->type_op == type_struct));
1057 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1058 if (strct->attr.sa.members[i] == member) {
1059 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1060 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1061 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1067 int (is_Struct_type)(const type *strct) {
1068 return _is_struct_type(strct);
1071 /*******************************************************************/
1073 /*******************************************************************/
1076 * Lazy construction of value argument / result representation.
1077 * Constructs a struct type and its member. The types of the members
1078 * are passed in the argument list.
1080 * @param name name of the type constructed
1081 * @param len number of fields
1082 * @param tps array of field types with length len
1084 static INLINE type *
1085 build_value_type(ident *name, int len, type **tps) {
1087 type *res = new_type_struct(name);
1088 /* Remove type from type list. Must be treated differently than other types. */
1089 remove_irp_type_from_list(res);
1090 for (i = 0; i < len; i++) {
1091 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1092 if (tps[i]) elt_type = tps[i];
1093 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1098 /* Create a new method type.
1099 N_param is the number of parameters, n_res the number of results. */
1100 type *new_type_method (ident *name, int n_param, int n_res) {
1103 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1104 res = new_type(type_method, mode_P_mach, name);
1105 res->state = layout_fixed;
1106 res->size = get_mode_size_bits(mode_P_mach);
1107 res->attr.ma.n_params = n_param;
1108 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1109 res->attr.ma.value_params = NULL;
1110 res->attr.ma.n_res = n_res;
1111 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1112 res->attr.ma.value_ress = NULL;
1113 res->attr.ma.variadicity = variadicity_non_variadic;
1114 res->attr.ma.first_variadic_param = -1;
1119 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1120 type *res = new_type_method (name, n_param, n_res);
1121 set_type_dbg_info(res, db);
1125 void free_method_entities(type *method) {
1126 assert(method && (method->type_op == type_method));
1129 /* Attention: also frees entities in value parameter subtypes! */
1130 void free_method_attrs(type *method) {
1131 assert(method && (method->type_op == type_method));
1132 free(method->attr.ma.param_type);
1133 free(method->attr.ma.res_type);
1134 if (method->attr.ma.value_params) {
1135 free_type_entities(method->attr.ma.value_params);
1136 free_type(method->attr.ma.value_params);
1138 if (method->attr.ma.value_ress) {
1139 free_type_entities(method->attr.ma.value_ress);
1140 free_type(method->attr.ma.value_ress);
1144 /* manipulate private fields of method. */
1145 int get_method_n_params (const type *method) {
1146 assert(method && (method->type_op == type_method));
1147 return method->attr.ma.n_params;
1150 type *get_method_param_type(type *method, int pos) {
1152 assert(method && (method->type_op == type_method));
1153 assert(pos >= 0 && pos < get_method_n_params(method));
1154 res = method->attr.ma.param_type[pos];
1155 assert(res != NULL && "empty method param type");
1156 return method->attr.ma.param_type[pos] = skip_tid(res);
1159 void set_method_param_type(type *method, int pos, type* tp) {
1160 assert(method && (method->type_op == type_method));
1161 assert(pos >= 0 && pos < get_method_n_params(method));
1162 method->attr.ma.param_type[pos] = tp;
1163 /* If information constructed set pass-by-value representation. */
1164 if (method->attr.ma.value_params) {
1165 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1166 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1170 /* Returns an entity that represents the copied value argument. Only necessary
1171 for compounds passed by value. */
1172 entity *get_method_value_param_ent(type *method, int pos) {
1173 assert(method && (method->type_op == type_method));
1174 assert(pos >= 0 && pos < get_method_n_params(method));
1175 if (!method->attr.ma.value_params)
1176 method->attr.ma.value_params
1177 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1178 get_method_n_params(method), method->attr.ma.param_type);
1179 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1180 != method->attr.ma.value_params)
1181 && "param type not yet set");
1182 return get_struct_member(method->attr.ma.value_params, pos);
1186 * Returns a type that represents the copied value arguments.
1188 type *get_method_value_param_type(const type *method)
1190 assert(method && (method->type_op == type_method));
1191 return method->attr.ma.value_params;
1194 int get_method_n_ress (const type *method) {
1195 assert(method && (method->type_op == type_method));
1196 return method->attr.ma.n_res;
1199 type *get_method_res_type(type *method, int pos) {
1201 assert(method && (method->type_op == type_method));
1202 assert(pos >= 0 && pos < get_method_n_ress(method));
1203 res = method->attr.ma.res_type[pos];
1204 assert(res != NULL && "empty method return type");
1205 return method->attr.ma.res_type[pos] = skip_tid(res);
1208 void set_method_res_type(type *method, int pos, type* tp) {
1209 assert(method && (method->type_op == type_method));
1210 assert(pos >= 0 && pos < get_method_n_ress(method));
1211 /* set the result type */
1212 method->attr.ma.res_type[pos] = tp;
1213 /* If information constructed set pass-by-value representation. */
1214 if (method->attr.ma.value_ress) {
1215 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1216 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1220 /* Returns an entity that represents the copied value result. Only necessary
1221 for compounds passed by value. */
1222 entity *get_method_value_res_ent(type *method, int pos) {
1223 assert(method && (method->type_op == type_method));
1224 assert(pos >= 0 && pos < get_method_n_ress(method));
1225 if (!method->attr.ma.value_ress)
1226 method->attr.ma.value_ress
1227 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1228 get_method_n_ress(method), method->attr.ma.res_type);
1229 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1230 && "result type not yet set");
1231 return get_struct_member(method->attr.ma.value_ress, pos);
1235 * Returns a type that represents the copied value results.
1237 type *get_method_value_res_type(const type *method) {
1238 assert(method && (method->type_op == type_method));
1239 return method->attr.ma.value_ress;
1242 /* Returns the null-terminated name of this variadicity. */
1243 const char *get_variadicity_name(variadicity vari)
1245 #define X(a) case a: return #a
1247 X(variadicity_non_variadic);
1248 X(variadicity_variadic);
1255 variadicity get_method_variadicity(const type *method)
1257 assert(method && (method->type_op == type_method));
1258 return method->attr.ma.variadicity;
1261 void set_method_variadicity(type *method, variadicity vari)
1263 assert(method && (method->type_op == type_method));
1264 method->attr.ma.variadicity = vari;
1268 * Returns the first variadic parameter index of a type.
1269 * If this index was NOT set, the index of the last parameter
1270 * of the method type plus one is returned for variadic functions.
1271 * Non-variadic function types always return -1 here.
1273 int get_method_first_variadic_param_index(const type *method)
1275 assert(method && (method->type_op == type_method));
1277 if (method->attr.ma.variadicity == variadicity_non_variadic)
1280 if (method->attr.ma.first_variadic_param == -1)
1281 return get_method_n_params(method);
1282 return method->attr.ma.first_variadic_param;
1286 * Sets the first variadic parameter index. This allows to specify
1287 * a complete call type (containing the type of all parameters)
1288 * but still have the knowledge, which parameter must be passed as
1291 void set_method_first_variadic_param_index(type *method, int index)
1293 assert(method && (method->type_op == type_method));
1294 assert(index >= 0 && index <= get_method_n_params(method));
1296 method->attr.ma.first_variadic_param = index;
1300 int (is_Method_type)(const type *method) {
1301 return _is_method_type(method);
1304 /*-----------------------------------------------------------------*/
1306 /*-----------------------------------------------------------------*/
1308 /* create a new type uni */
1309 type *new_type_union (ident *name) {
1311 res = new_type(type_union, NULL, name);
1312 /*res->attr.ua.unioned_type = xcalloc(n_types, sizeof(res->attr.ua.unioned_type[0]));
1313 res->attr.ua.delim_names = xcalloc(n_types, sizeof(res->attr.ua.delim_names[0])); */
1314 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1317 type *new_d_type_union (ident *name, dbg_info* db) {
1318 type *res = new_type_union (name);
1319 set_type_dbg_info(res, db);
1322 void free_union_entities (type *uni) {
1324 assert(uni && (uni->type_op == type_union));
1325 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1326 free_entity(get_union_member(uni, i));
1328 void free_union_attrs (type *uni) {
1329 assert(uni && (uni->type_op == type_union));
1330 DEL_ARR_F(uni->attr.ua.members);
1332 /* manipulate private fields of union */
1334 int get_union_n_types (type *uni) {
1335 assert(uni && (uni->type_op == type_union));
1336 return uni->attr.ua.n_types;
1338 type *get_union_unioned_type (type *uni, int pos) {
1339 assert(uni && (uni->type_op == type_union));
1340 assert(pos >= 0 && pos < get_union_n_types(uni));
1341 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1343 void set_union_unioned_type (type *uni, int pos, type *tp) {
1344 assert(uni && (uni->type_op == type_union));
1345 assert(pos >= 0 && pos < get_union_n_types(uni));
1346 uni->attr.ua.unioned_type[pos] = tp;
1348 ident *get_union_delim_nameid (type *uni, int pos) {
1349 assert(uni && (uni->type_op == type_union));
1350 assert(pos >= 0 && pos < get_union_n_types(uni));
1351 return uni->attr.ua.delim_names[pos];
1353 const char *get_union_delim_name (type *uni, int pos) {
1354 assert(uni && (uni->type_op == type_union));
1355 assert(pos >= 0 && pos < get_union_n_types(uni));
1356 return get_id_str(uni->attr.ua.delim_names[pos]);
1358 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1359 assert(uni && (uni->type_op == type_union));
1360 assert(pos >= 0 && pos < get_union_n_types(uni));
1361 uni->attr.ua.delim_names[pos] = id;
1364 int get_union_n_members (const type *uni) {
1365 assert(uni && (uni->type_op == type_union));
1366 return (ARR_LEN (uni->attr.ua.members));
1368 void add_union_member (type *uni, entity *member) {
1369 assert(uni && (uni->type_op == type_union));
1370 assert(uni != get_entity_type(member) && "recursive type");
1371 ARR_APP1 (entity *, uni->attr.ua.members, member);
1373 entity *get_union_member (const type *uni, int pos) {
1374 assert(uni && (uni->type_op == type_union));
1375 assert(pos >= 0 && pos < get_union_n_members(uni));
1376 return uni->attr.ua.members[pos];
1378 void set_union_member (type *uni, int pos, entity *member) {
1379 assert(uni && (uni->type_op == type_union));
1380 assert(pos >= 0 && pos < get_union_n_members(uni));
1381 uni->attr.ua.members[pos] = member;
1383 void remove_union_member(type *uni, entity *member) {
1385 assert(uni && (uni->type_op == type_union));
1386 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1387 if (uni->attr.ua.members[i] == member) {
1388 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1389 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1390 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1396 int (is_Union_type)(const type *uni) {
1397 return _is_union_type(uni);
1400 /*-----------------------------------------------------------------*/
1402 /*-----------------------------------------------------------------*/
1405 /* create a new type array -- set dimension sizes independently */
1406 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1409 ir_graph *rem = current_ir_graph;
1410 assert(!is_Method_type(element_type));
1412 res = new_type(type_array, NULL, name);
1413 res->attr.aa.n_dimensions = n_dimensions;
1414 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1415 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1416 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1418 current_ir_graph = get_const_code_irg();
1419 for (i = 0; i < n_dimensions; i++) {
1420 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1421 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1422 res->attr.aa.order[i] = i;
1424 current_ir_graph = rem;
1426 res->attr.aa.element_type = element_type;
1427 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1432 type *new_d_type_array (ident *name, int n_dimensions,
1433 type *element_type, dbg_info* db) {
1434 type *res = new_type_array (name, n_dimensions, element_type);
1435 set_type_dbg_info(res, db);
1439 void free_array_entities (type *array) {
1440 assert(array && (array->type_op == type_array));
1443 void free_array_attrs (type *array) {
1444 assert(array && (array->type_op == type_array));
1445 free(array->attr.aa.lower_bound);
1446 free(array->attr.aa.upper_bound);
1449 /* manipulate private fields of array type */
1450 int get_array_n_dimensions (const type *array) {
1451 assert(array && (array->type_op == type_array));
1452 return array->attr.aa.n_dimensions;
1456 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1457 ir_node * upper_bound) {
1458 assert(array && (array->type_op == type_array));
1459 assert(lower_bound && "lower_bound node may not be NULL.");
1460 assert(upper_bound && "upper_bound node may not be NULL.");
1461 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1462 array->attr.aa.lower_bound[dimension] = lower_bound;
1463 array->attr.aa.upper_bound[dimension] = upper_bound;
1466 set_array_bounds_int (type *array, int dimension, int lower_bound,
1468 ir_graph *rem = current_ir_graph;
1469 current_ir_graph = get_const_code_irg();
1470 set_array_bounds (array, dimension,
1471 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1472 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1473 current_ir_graph = rem;
1476 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1477 assert(array && (array->type_op == type_array));
1478 assert(lower_bound && "lower_bound node may not be NULL.");
1479 array->attr.aa.lower_bound[dimension] = lower_bound;
1481 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1482 ir_graph *rem = current_ir_graph;
1483 current_ir_graph = get_const_code_irg();
1484 set_array_lower_bound (array, dimension,
1485 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1486 current_ir_graph = rem;
1489 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1490 assert(array && (array->type_op == type_array));
1491 assert(upper_bound && "upper_bound node may not be NULL.");
1492 array->attr.aa.upper_bound[dimension] = upper_bound;
1494 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1495 ir_graph *rem = current_ir_graph;
1496 current_ir_graph = get_const_code_irg();
1497 set_array_upper_bound (array, dimension,
1498 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1499 current_ir_graph = rem;
1501 int has_array_lower_bound (const type *array, int dimension) {
1502 assert(array && (array->type_op == type_array));
1503 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1505 ir_node *get_array_lower_bound (const type *array, int dimension) {
1506 assert(array && (array->type_op == type_array));
1507 return array->attr.aa.lower_bound[dimension];
1509 long get_array_lower_bound_int (const type *array, int dimension) {
1511 assert(array && (array->type_op == type_array));
1512 node = array->attr.aa.lower_bound[dimension];
1513 assert(get_irn_op(node) == op_Const);
1514 return get_tarval_long(get_Const_tarval(node));
1516 int has_array_upper_bound (const type *array, int dimension) {
1517 assert(array && (array->type_op == type_array));
1518 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1520 ir_node * get_array_upper_bound (const type *array, int dimension) {
1521 assert(array && (array->type_op == type_array));
1522 return array->attr.aa.upper_bound[dimension];
1524 long get_array_upper_bound_int (const type *array, int dimension) {
1526 assert(array && (array->type_op == type_array));
1527 node = array->attr.aa.upper_bound[dimension];
1528 assert(get_irn_op(node) == op_Const);
1529 return get_tarval_long(get_Const_tarval(node));
1532 void set_array_order (type *array, int dimension, int order) {
1533 assert(array && (array->type_op == type_array));
1534 array->attr.aa.order[dimension] = order;
1537 int get_array_order (const type *array, int dimension) {
1538 assert(array && (array->type_op == type_array));
1539 return array->attr.aa.order[dimension];
1542 int find_array_dimension(const type *array, int order) {
1545 assert(array && (array->type_op == type_array));
1547 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1548 if (array->attr.aa.order[dim] == order)
1554 void set_array_element_type (type *array, type *tp) {
1555 assert(array && (array->type_op == type_array));
1556 assert(!is_Method_type(tp));
1557 array->attr.aa.element_type = tp;
1559 type *get_array_element_type (type *array) {
1560 assert(array && (array->type_op == type_array));
1561 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1564 void set_array_element_entity (type *array, entity *ent) {
1565 assert(array && (array->type_op == type_array));
1566 assert((get_entity_type(ent)->type_op != type_method));
1567 array->attr.aa.element_ent = ent;
1568 array->attr.aa.element_type = get_entity_type(ent);
1570 entity *get_array_element_entity (const type *array) {
1571 assert(array && (array->type_op == type_array));
1572 return array->attr.aa.element_ent;
1576 int (is_Array_type)(const type *array) {
1577 return _is_array_type(array);
1580 /*-----------------------------------------------------------------*/
1581 /* TYPE_ENUMERATION */
1582 /*-----------------------------------------------------------------*/
1584 /* create a new type enumeration -- set the enumerators independently */
1585 type *new_type_enumeration (ident *name, int n_enums) {
1587 res = new_type(type_enumeration, NULL, name);
1588 res->attr.ea.n_enums = n_enums;
1589 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1590 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1593 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1594 type *res = new_type_enumeration (name, n_enums);
1595 set_type_dbg_info(res, db);
1599 void free_enumeration_entities(type *enumeration) {
1600 assert(enumeration && (enumeration->type_op == type_enumeration));
1602 void free_enumeration_attrs(type *enumeration) {
1603 assert(enumeration && (enumeration->type_op == type_enumeration));
1604 free(enumeration->attr.ea.enumer);
1605 free(enumeration->attr.ea.enum_nameid);
1608 /* manipulate fields of enumeration type. */
1609 int get_enumeration_n_enums (const type *enumeration) {
1610 assert(enumeration && (enumeration->type_op == type_enumeration));
1611 return enumeration->attr.ea.n_enums;
1613 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1614 assert(enumeration && (enumeration->type_op == type_enumeration));
1615 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1616 enumeration->attr.ea.enumer[pos] = con;
1618 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1619 assert(enumeration && (enumeration->type_op == type_enumeration));
1620 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1621 return enumeration->attr.ea.enumer[pos];
1623 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1624 assert(enumeration && (enumeration->type_op == type_enumeration));
1625 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1626 enumeration->attr.ea.enum_nameid[pos] = id;
1628 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1629 assert(enumeration && (enumeration->type_op == type_enumeration));
1630 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1631 return enumeration->attr.ea.enum_nameid[pos];
1633 const char *get_enumeration_name(const type *enumeration, int pos) {
1634 assert(enumeration && (enumeration->type_op == type_enumeration));
1635 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1636 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1640 int (is_Enumeration_type)(const type *enumeration) {
1641 return _is_enumeration_type(enumeration);
1644 /*-----------------------------------------------------------------*/
1646 /*-----------------------------------------------------------------*/
1648 /* Create a new type pointer */
1649 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1651 assert(mode_is_reference(ptr_mode));
1652 res = new_type(type_pointer, ptr_mode, name);
1653 res->attr.pa.points_to = points_to;
1654 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1655 res->size = get_mode_size_bits(res->mode);
1656 res->state = layout_fixed;
1659 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1660 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1661 set_type_dbg_info(res, db);
1664 void free_pointer_entities (type *pointer) {
1665 assert(pointer && (pointer->type_op == type_pointer));
1667 void free_pointer_attrs (type *pointer) {
1668 assert(pointer && (pointer->type_op == type_pointer));
1670 /* manipulate fields of type_pointer */
1671 void set_pointer_points_to_type (type *pointer, type *tp) {
1672 assert(pointer && (pointer->type_op == type_pointer));
1673 pointer->attr.pa.points_to = tp;
1675 type *get_pointer_points_to_type (type *pointer) {
1676 assert(pointer && (pointer->type_op == type_pointer));
1677 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1681 int (is_Pointer_type)(const type *pointer) {
1682 return _is_pointer_type(pointer);
1685 /* Returns the first pointer type that has as points_to tp.
1686 * Not efficient: O(#types).
1687 * If not found returns firm_unknown_type. */
1688 type *find_pointer_type_to_type (type *tp) {
1690 for (i = 0; i < get_irp_n_types(); ++i) {
1691 type *found = get_irp_type(i);
1692 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1695 return firm_unknown_type;
1700 /*-----------------------------------------------------------------*/
1701 /* TYPE_PRIMITIVE */
1702 /*-----------------------------------------------------------------*/
1704 /* create a new type primitive */
1705 type *new_type_primitive (ident *name, ir_mode *mode) {
1707 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1708 res = new_type(type_primitive, mode, name);
1709 res->size = get_mode_size_bits(mode);
1710 res->state = layout_fixed;
1713 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1714 type *res = new_type_primitive (name, mode);
1715 set_type_dbg_info(res, db);
1718 void free_primitive_entities (type *primitive) {
1719 assert(primitive && (primitive->type_op == type_primitive));
1721 void free_primitive_attrs (type *primitive) {
1722 assert(primitive && (primitive->type_op == type_primitive));
1726 int (is_Primitive_type)(const type *primitive) {
1727 return _is_primitive_type(primitive);
1730 /*-----------------------------------------------------------------*/
1731 /* common functionality */
1732 /*-----------------------------------------------------------------*/
1735 int (is_atomic_type)(const type *tp) {
1736 return _is_atomic_type(tp);
1740 * Gets the number of elements in a firm compound type.
1742 int get_compound_n_members(const type *tp)
1746 if (is_Struct_type(tp))
1747 res = get_struct_n_members(tp);
1748 else if (is_Class_type(tp))
1749 res = get_class_n_members(tp);
1750 else if (is_Union_type(tp))
1751 res = get_union_n_members(tp);
1753 assert(0 && "need struct, union or class for member count");
1759 * Gets the member of a firm compound type at position pos.
1761 entity *get_compound_member(const type *tp, int pos)
1765 if (is_Struct_type(tp))
1766 res = get_struct_member(tp, pos);
1767 else if (is_Class_type(tp))
1768 res = get_class_member(tp, pos);
1769 else if (is_Union_type(tp))
1770 res = get_union_member(tp, pos);
1773 assert(0 && "need struct, union or class to get a member");
1781 int is_compound_type(const type *tp) {
1782 assert(tp && tp->kind == k_type);
1783 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;