3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck
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-2005 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
63 # include "irprog_t.h"
66 # include "typegmod.h"
73 /*-----------------------------------------------------------------*/
75 /*-----------------------------------------------------------------*/
77 type *firm_none_type; type *get_none_type(void) { return firm_none_type; }
78 type *firm_unknown_type; type *get_unknown_type(void) { return firm_unknown_type; }
82 /* Returns a new, unique number to number nodes or the like. */
83 int get_irp_new_node_nr(void);
86 /* Suffixes added to types used for pass-by-value representations. */
87 static ident *value_params_suffix = NULL;
88 static ident *value_ress_suffix = NULL;
90 /* Initialize the type module. */
91 void firm_init_type(dbg_info *builtin_db)
93 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
94 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
96 /* construct none and unknown type. */
97 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
98 set_type_size_bits(firm_none_type, 0);
99 set_type_state (firm_none_type, layout_fixed);
100 remove_irp_type(firm_none_type);
102 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
103 set_type_size_bits(firm_unknown_type, 0);
104 set_type_state (firm_unknown_type, layout_fixed);
105 remove_irp_type(firm_unknown_type);
108 /** the global type visited flag */
109 unsigned long firm_type_visited;
111 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
112 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
113 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
116 * Creates a new type representation.
119 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
123 assert(type_op != type_id);
124 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
126 node_size = offsetof(type, attr) + type_op->attr_size;
127 res = xmalloc(node_size);
128 memset(res, 0, node_size);
131 res->type_op = type_op;
134 res->visibility = visibility_external_allocated;
136 res->state = layout_undefined;
143 res->nr = get_irp_new_node_nr();
144 #endif /* defined DEBUG_libfirm */
146 add_irp_type(res); /* Remember the new type global. */
151 void free_type(type *tp) {
152 const tp_op *op = get_type_tpop(tp);
154 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
156 /* Remove from list of all types */
158 /* Free the attributes of the type. */
160 /* Free entities automatically allocated with the type */
161 if (op->ops.free_auto_entities)
162 op->ops.free_auto_entities(tp);
163 /* And now the type itself... */
168 void free_type_entities(type *tp) {
169 const tp_op *tpop = get_type_tpop(tp);
171 if (tpop->ops.free_entities)
172 tpop->ops.free_entities(tp);
175 void free_type_attrs(type *tp) {
176 const tp_op *tpop = get_type_tpop(tp);
178 if (tpop->ops.free_attrs)
179 tpop->ops.free_attrs(tp);
182 /* set/get the link field */
183 void *(get_type_link)(const type *tp) {
184 return _get_type_link(tp);
187 void (set_type_link)(type *tp, void *l) {
188 _set_type_link(tp, l);
191 const tp_op *(get_type_tpop)(const type *tp) {
192 return _get_type_tpop(tp);
195 ident *(get_type_tpop_nameid)(const type *tp) {
196 return _get_type_tpop_nameid(tp);
199 const char* get_type_tpop_name(const type *tp) {
200 assert(tp && tp->kind == k_type);
201 return get_id_str(tp->type_op->name);
204 tp_opcode (get_type_tpop_code)(const type *tp) {
205 return _get_type_tpop_code(tp);
208 ir_mode *(get_type_mode)(const type *tp) {
209 return _get_type_mode(tp);
212 void set_type_mode(type *tp, ir_mode *mode) {
213 const tp_op *tpop = get_type_tpop(tp);
215 if (tpop->ops.set_type_mode)
216 tpop->ops.set_type_mode(tp, mode);
218 assert(0 && "setting a mode is NOT allowed for this type");
221 ident *(get_type_ident)(const type *tp) {
222 return _get_type_ident(tp);
225 void (set_type_ident)(type *tp, ident* id) {
226 _set_type_ident(tp, id);
229 /* Outputs a unique number for this node */
230 long get_type_nr(const type *tp) {
239 const char* get_type_name(const type *tp) {
240 assert(tp && tp->kind == k_type);
241 return (get_id_str(tp->name));
244 int (get_type_size_bytes)(const type *tp) {
245 return _get_type_size_bytes(tp);
248 int (get_type_size_bits)(const type *tp) {
249 return _get_type_size_bits(tp);
253 visibility get_type_visibility (const type *tp) {
255 visibility res = visibility_local;
256 if (is_compound_type(tp)) {
258 if (is_Array_type(tp)) {
259 entity *mem = get_array_element_entity(tp);
260 if (get_entity_visibility(mem) != visibility_local)
261 res = visibility_external_visible;
263 int i, n_mems = get_compound_n_members(tp);
264 for (i = 0; i < n_mems; ++i) {
265 entity *mem = get_compound_member(tp, i);
266 if (get_entity_visibility(mem) != visibility_local)
267 res = visibility_external_visible;
274 return tp->visibility;
277 void set_type_visibility (type *tp, visibility v) {
280 /* check for correctness */
281 if (v != visibility_external_allocated) {
282 visibility res = visibility_local;
283 if (is_compound_type(tp)) {
284 if (is_Array_type(tp)) {
285 entity *mem = get_array_element_entity(tp);
286 if (get_entity_visibility(mem) > res)
287 res = get_entity_visibility(mem);
289 int i, n_mems = get_compound_n_members(tp);
290 for (i = 0; i < n_mems; ++i) {
291 entity *mem = get_compound_member(tp, i);
292 if (get_entity_visibility(mem) > res)
293 res = get_entity_visibility(mem);
304 set_type_size_bits(type *tp, int size) {
305 const tp_op *tpop = get_type_tpop(tp);
307 if (tpop->ops.set_type_size)
308 tpop->ops.set_type_size(tp, size);
310 assert(0 && "Cannot set size for this type");
314 set_type_size_bytes(type *tp, int size) {
315 set_type_size_bits(tp, 8*size);
318 int get_type_alignment_bytes(type *tp) {
319 int align = get_type_alignment_bits(tp);
321 return align < 0 ? align : (align + 7) >> 3;
324 int get_type_alignment_bits(type *tp) {
330 /* alignment NOT set calculate it "on demand" */
332 align = get_mode_size_bits(tp->mode);
333 else if (is_Array_type(tp))
334 align = get_type_alignment_bits(get_array_element_type(tp));
335 else if (is_compound_type(tp)) {
336 int i, n = get_compound_n_members(tp);
339 for (i = 0; i < n; ++i) {
340 type *t = get_entity_type(get_compound_member(tp, i));
341 int a = get_type_alignment_bits(t);
347 else if (is_Method_type(tp))
357 set_type_alignment_bits(type *tp, int align) {
358 assert(tp && tp->kind == k_type);
359 /* Methods don't have an alignment. */
360 if (tp->type_op != type_method) {
366 set_type_alignment_bytes(type *tp, int align) {
367 set_type_alignment_bits(tp, 8*align);
370 /* Returns a human readable string for the enum entry. */
371 const char *get_type_state_name(type_state s) {
372 #define X(a) case a: return #a;
382 type_state (get_type_state)(const type *tp) {
383 return _get_type_state(tp);
387 set_type_state(type *tp, type_state state) {
388 assert(tp && tp->kind == k_type);
390 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
391 (tp->type_op == type_method))
394 /* Just a correctness check: */
395 if (state == layout_fixed) {
397 switch (get_type_tpop_code(tp)) {
400 assert(get_type_size_bits(tp) > -1);
401 if (tp != get_glob_type()) {
402 int n_mem = get_class_n_members(tp);
403 for (i = 0; i < n_mem; i++) {
404 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
405 { DDMT(tp); DDME(get_class_member(tp, i)); }
406 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
408 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
409 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
416 assert(get_type_size_bits(tp) > -1);
417 for (i = 0; i < get_struct_n_members(tp); i++) {
418 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
419 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
428 Assure that only innermost dimension is dynamic? */
430 case tpo_enumeration:
432 assert(get_type_mode != NULL);
433 for (i = 0; i < get_enumeration_n_enums(tp); i++)
434 assert(get_enumeration_enum(tp, i) != NULL);
442 unsigned long (get_type_visited)(const type *tp) {
443 return _get_type_visited(tp);
446 void (set_type_visited)(type *tp, unsigned long num) {
447 _set_type_visited(tp, num);
450 /* Sets visited field in type to type_visited. */
451 void (mark_type_visited)(type *tp) {
452 _mark_type_visited(tp);
455 int (type_visited)(const type *tp) {
456 return _type_visited(tp);
459 int (type_not_visited)(const type *tp) {
460 return _type_not_visited(tp);
463 int (is_type)(const void *thing) {
464 return _is_type(thing);
467 /* Checks whether two types are structural equal.*/
468 int equal_type(type *typ1, type *typ2) {
473 if (typ1 == typ2) return 1;
475 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
476 (get_type_ident(typ1) != get_type_ident(typ2)) ||
477 (get_type_mode(typ1) != get_type_mode(typ2)) ||
478 (get_type_state(typ1) != get_type_state(typ2)))
480 if ((get_type_state(typ1) == layout_fixed) &&
481 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
484 switch (get_type_tpop_code(typ1)) {
486 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
487 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
488 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
489 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
490 /** Compare the members **/
491 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
492 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
493 /* First sort the members of typ2 */
494 for (i = 0; i < get_class_n_members(typ1); i++) {
495 entity *e1 = get_class_member(typ1, i);
496 for (j = 0; j < get_class_n_members(typ2); j++) {
497 entity *e2 = get_class_member(typ2, j);
498 if (get_entity_name(e1) == get_entity_name(e2))
502 for (i = 0; i < get_class_n_members(typ1); i++) {
503 if (!m[i] || /* Found no counterpart */
504 !equal_entity(get_class_member(typ1, i), m[i]))
507 /** Compare the supertypes **/
508 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
509 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
510 /* First sort the supertypes of typ2 */
511 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
512 type *t1 = get_class_supertype(typ1, i);
513 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
514 type *t2 = get_class_supertype(typ2, j);
515 if (get_type_ident(t2) == get_type_ident(t1))
519 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
520 if (!t[i] || /* Found no counterpart */
521 get_class_supertype(typ1, i) != t[i])
526 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
527 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
528 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
529 /* First sort the members of lt */
530 for (i = 0; i < get_struct_n_members(typ1); i++) {
531 entity *e1 = get_struct_member(typ1, i);
532 for (j = 0; j < get_struct_n_members(typ2); j++) {
533 entity *e2 = get_struct_member(typ2, j);
534 if (get_entity_name(e1) == get_entity_name(e2))
538 for (i = 0; i < get_struct_n_members(typ1); i++) {
539 if (!m[i] || /* Found no counterpart */
540 !equal_entity(get_struct_member(typ1, i), m[i]))
545 int n_param1, n_param2;
547 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
548 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
550 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
551 n_param1 = get_method_n_params(typ1);
552 n_param2 = get_method_n_params(typ2);
555 n_param1 = get_method_first_variadic_param_index(typ1);
556 n_param2 = get_method_first_variadic_param_index(typ2);
559 if (n_param1 != n_param2) return 0;
561 for (i = 0; i < n_param1; i++) {
562 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
565 for (i = 0; i < get_method_n_ress(typ1); i++) {
566 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
571 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
572 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
573 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
574 /* First sort the members of lt */
575 for (i = 0; i < get_union_n_members(typ1); i++) {
576 entity *e1 = get_union_member(typ1, i);
577 for (j = 0; j < get_union_n_members(typ2); j++) {
578 entity *e2 = get_union_member(typ2, j);
579 if (get_entity_name(e1) == get_entity_name(e2))
583 for (i = 0; i < get_union_n_members(typ1); i++) {
584 if (!m[i] || /* Found no counterpart */
585 !equal_entity(get_union_member(typ1, i), m[i]))
590 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
592 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
594 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
595 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
596 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
598 if (get_array_order(typ1, i) != get_array_order(typ2, i))
599 assert(0 && "type compare with different dimension orders not implemented");
602 case tpo_enumeration: {
603 assert(0 && "enumerations not implemented");
606 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
609 case tpo_primitive: {
616 /* Checks whether two types are structural comparable. */
617 int smaller_type (type *st, type *lt) {
621 if (st == lt) return 1;
623 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
626 switch(get_type_tpop_code(st)) {
628 return is_subclass_of(st, lt);
631 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
632 m = alloca(sizeof(entity *) * get_struct_n_members(st));
633 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
634 /* First sort the members of lt */
635 for (i = 0; i < get_struct_n_members(st); i++) {
636 entity *se = get_struct_member(st, i);
637 for (j = 0; j < get_struct_n_members(lt); j++) {
638 entity *le = get_struct_member(lt, j);
639 if (get_entity_name(le) == get_entity_name(se))
643 for (i = 0; i < get_struct_n_members(st); i++) {
644 if (!m[i] || /* Found no counterpart */
645 !smaller_type(get_entity_type(get_struct_member(st, i)),
646 get_entity_type(m[i])))
651 /** FIXME: is this still 1? */
652 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
653 if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
654 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
655 for (i = 0; i < get_method_n_params(st); i++) {
656 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
659 for (i = 0; i < get_method_n_ress(st); i++) {
660 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
665 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
666 m = alloca(sizeof(entity *) * get_union_n_members(st));
667 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
668 /* First sort the members of lt */
669 for (i = 0; i < get_union_n_members(st); i++) {
670 entity *se = get_union_member(st, i);
671 for (j = 0; j < get_union_n_members(lt); j++) {
672 entity *le = get_union_member(lt, j);
673 if (get_entity_name(le) == get_entity_name(se))
677 for (i = 0; i < get_union_n_members(st); i++) {
678 if (!m[i] || /* Found no counterpart */
679 !smaller_type(get_entity_type(get_union_member(st, i)),
680 get_entity_type(m[i])))
685 type *set, *let; /* small/large elt. type */
686 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
688 set = get_array_element_type(st);
689 let = get_array_element_type(lt);
691 /* If the element types are different, set must be convertible
692 to let, and they must have the same size so that address
693 computations work out. To have a size the layout must
695 if ((get_type_state(set) != layout_fixed) ||
696 (get_type_state(let) != layout_fixed))
698 if (!smaller_type(set, let) ||
699 get_type_size_bits(set) != get_type_size_bits(let))
702 for(i = 0; i < get_array_n_dimensions(st); i++) {
703 if (get_array_lower_bound(lt, i))
704 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
706 if (get_array_upper_bound(lt, i))
707 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
711 case tpo_enumeration: {
712 assert(0 && "enumerations not implemented");
715 if (!smaller_type(get_pointer_points_to_type(st),
716 get_pointer_points_to_type(lt)))
719 case tpo_primitive: {
720 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
728 /*-----------------------------------------------------------------*/
730 /*-----------------------------------------------------------------*/
732 /* create a new class type */
733 type *new_d_type_class (ident *name, dbg_info *db) {
736 res = new_type(type_class, NULL, name, db);
738 res->attr.ca.members = NEW_ARR_F (entity *, 0);
739 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
740 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
741 res->attr.ca.peculiarity = peculiarity_existent;
742 res->attr.ca.dfn = 0;
747 type *new_type_class (ident *name) {
748 return new_d_type_class (name, NULL);
751 void free_class_entities(type *clss) {
753 assert(clss && (clss->type_op == type_class));
754 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
755 free_entity(get_class_member(clss, i));
758 void free_class_attrs(type *clss) {
759 assert(clss && (clss->type_op == type_class));
760 DEL_ARR_F(clss->attr.ca.members);
761 DEL_ARR_F(clss->attr.ca.subtypes);
762 DEL_ARR_F(clss->attr.ca.supertypes);
765 /* manipulate private fields of class type */
766 void add_class_member (type *clss, entity *member) {
767 assert(clss && (clss->type_op == type_class));
768 assert(clss != get_entity_type(member) && "recursive type");
769 ARR_APP1 (entity *, clss->attr.ca.members, member);
772 int (get_class_n_members) (const type *clss) {
773 return _get_class_n_members(clss);
776 int get_class_member_index(type *clss, entity *mem) {
778 assert(clss && (clss->type_op == type_class));
779 for (i = 0; i < get_class_n_members(clss); i++)
780 if (get_class_member(clss, i) == mem)
785 entity *(get_class_member) (const type *clss, int pos) {
786 return _get_class_member(clss, pos);
789 entity *get_class_member_by_name(type *clss, ident *name) {
791 assert(clss && (clss->type_op == type_class));
792 n_mem = get_class_n_members(clss);
793 for (i = 0; i < n_mem; ++i) {
794 entity *mem = get_class_member(clss, i);
795 if (get_entity_ident(mem) == name) return mem;
800 void set_class_member (type *clss, entity *member, int pos) {
801 assert(clss && (clss->type_op == type_class));
802 assert(pos >= 0 && pos < get_class_n_members(clss));
803 clss->attr.ca.members[pos] = member;
805 void set_class_members (type *clss, entity **members, int arity) {
807 assert(clss && (clss->type_op == type_class));
808 DEL_ARR_F(clss->attr.ca.members);
809 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
810 for (i = 0; i < arity; i++) {
811 set_entity_owner(members[i], clss);
812 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
815 void remove_class_member(type *clss, entity *member) {
817 assert(clss && (clss->type_op == type_class));
818 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
819 if (clss->attr.ca.members[i] == member) {
820 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
821 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
822 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
828 void add_class_subtype (type *clss, type *subtype) {
830 assert(clss && (clss->type_op == type_class));
831 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
832 for (i = 0; i < get_class_n_supertypes(subtype); i++)
833 if (get_class_supertype(subtype, i) == clss)
834 /* Class already registered */
836 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
838 int get_class_n_subtypes (const type *clss) {
839 assert(clss && (clss->type_op == type_class));
840 return (ARR_LEN (clss->attr.ca.subtypes));
842 type *get_class_subtype (type *clss, int pos) {
843 assert(clss && (clss->type_op == type_class));
844 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
845 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
847 int get_class_subtype_index(type *clss, const type *subclass) {
848 int i, n_subtypes = get_class_n_subtypes(clss);
849 assert(is_Class_type(subclass));
850 for (i = 0; i < n_subtypes; ++i) {
851 if (get_class_subtype(clss, i) == subclass) return i;
855 void set_class_subtype (type *clss, type *subtype, int pos) {
856 assert(clss && (clss->type_op == type_class));
857 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
858 clss->attr.ca.subtypes[pos] = subtype;
860 void remove_class_subtype(type *clss, type *subtype) {
862 assert(clss && (clss->type_op == type_class));
863 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
864 if (clss->attr.ca.subtypes[i] == subtype) {
865 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
866 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
867 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
872 void add_class_supertype (type *clss, type *supertype) {
874 assert(clss && (clss->type_op == type_class));
875 assert(supertype && (supertype -> type_op == type_class));
876 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
877 for (i = 0; i < get_class_n_subtypes(supertype); i++)
878 if (get_class_subtype(supertype, i) == clss)
879 /* Class already registered */
881 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
883 int get_class_n_supertypes (const type *clss) {
884 assert(clss && (clss->type_op == type_class));
885 return (ARR_LEN (clss->attr.ca.supertypes));
887 int get_class_supertype_index(type *clss, type *super_clss) {
888 int i, n_supertypes = get_class_n_supertypes(clss);
889 assert(super_clss && (super_clss->type_op == type_class));
890 for (i = 0; i < n_supertypes; i++)
891 if (get_class_supertype(clss, i) == super_clss)
895 type *get_class_supertype (type *clss, int pos) {
896 assert(clss && (clss->type_op == type_class));
897 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
898 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
900 void set_class_supertype (type *clss, type *supertype, int pos) {
901 assert(clss && (clss->type_op == type_class));
902 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
903 clss->attr.ca.supertypes[pos] = supertype;
905 void remove_class_supertype(type *clss, type *supertype) {
907 assert(clss && (clss->type_op == type_class));
908 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
909 if (clss->attr.ca.supertypes[i] == supertype) {
910 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
911 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
912 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
917 const char *get_peculiarity_string(peculiarity p) {
918 #define X(a) case a: return #a
920 X(peculiarity_description);
921 X(peculiarity_inherited);
922 X(peculiarity_existent);
925 return "invalid peculiarity";
928 peculiarity get_class_peculiarity (const type *clss) {
929 assert(clss && (clss->type_op == type_class));
930 return clss->attr.ca.peculiarity;
933 void set_class_peculiarity (type *clss, peculiarity pec) {
934 assert(clss && (clss->type_op == type_class));
935 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
936 clss->attr.ca.peculiarity = pec;
939 void set_class_dfn (type *clss, int dfn)
941 clss->attr.ca.dfn = dfn;
944 int get_class_dfn (const type *clss)
946 return (clss->attr.ca.dfn);
950 int (is_Class_type)(const type *clss) {
951 return _is_class_type(clss);
954 void set_class_mode(type *tp, ir_mode *mode) {
955 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
956 assert(get_type_state(tp) == layout_fixed &&
957 tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
961 void set_class_size_bits(type *tp, int size) {
962 /* argh: we must allow to set negative values as "invalid size" */
963 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
964 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
967 /*----------------------------------------------------------------**/
969 /*----------------------------------------------------------------**/
971 /* create a new type struct */
972 type *new_d_type_struct(ident *name, dbg_info *db) {
973 type *res = new_type(type_struct, NULL, name, db);
975 res->attr.sa.members = NEW_ARR_F(entity *, 0);
980 type *new_type_struct (ident *name) {
981 return new_d_type_struct (name, NULL);
984 void free_struct_entities (type *strct) {
986 assert(strct && (strct->type_op == type_struct));
987 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
988 free_entity(get_struct_member(strct, i));
990 void free_struct_attrs (type *strct) {
991 assert(strct && (strct->type_op == type_struct));
992 DEL_ARR_F(strct->attr.sa.members);
995 /* manipulate private fields of struct */
996 int get_struct_n_members (const type *strct) {
997 assert(strct && (strct->type_op == type_struct));
998 return (ARR_LEN (strct->attr.sa.members));
1001 void add_struct_member (type *strct, entity *member) {
1002 assert(strct && (strct->type_op == type_struct));
1003 assert(get_type_tpop(get_entity_type(member)) != type_method);
1004 /* @@@ lowerfirm geht nicht durch */
1005 assert(strct != get_entity_type(member) && "recursive type");
1006 ARR_APP1 (entity *, strct->attr.sa.members, member);
1009 entity *get_struct_member (const type *strct, int pos) {
1010 assert(strct && (strct->type_op == type_struct));
1011 assert(pos >= 0 && pos < get_struct_n_members(strct));
1012 return strct->attr.sa.members[pos];
1015 int get_struct_member_index(type *strct, entity *mem) {
1017 assert(strct && (strct->type_op == type_struct));
1018 for (i = 0; i < get_struct_n_members(strct); i++)
1019 if (get_struct_member(strct, i) == mem)
1024 void set_struct_member (type *strct, int pos, entity *member) {
1025 assert(strct && (strct->type_op == type_struct));
1026 assert(pos >= 0 && pos < get_struct_n_members(strct));
1027 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1028 strct->attr.sa.members[pos] = member;
1030 void remove_struct_member(type *strct, entity *member) {
1032 assert(strct && (strct->type_op == type_struct));
1033 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1034 if (strct->attr.sa.members[i] == member) {
1035 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1036 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1037 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1043 int (is_Struct_type)(const type *strct) {
1044 return _is_struct_type(strct);
1047 void set_struct_mode(type *tp, ir_mode *mode) {
1048 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1049 assert(get_type_state(tp) == layout_fixed &&
1050 tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1054 void set_struct_size_bits(type *tp, int size) {
1055 /* argh: we must allow to set negative values as "invalid size" */
1056 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1057 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1060 /*******************************************************************/
1062 /*******************************************************************/
1065 * Lazy construction of value argument / result representation.
1066 * Constructs a struct type and its member. The types of the members
1067 * are passed in the argument list.
1069 * @param name name of the type constructed
1070 * @param len number of fields
1071 * @param tps array of field types with length len
1073 static INLINE type *
1074 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1076 type *res = new_type_struct(name);
1077 /* Remove type from type list. Must be treated differently than other types. */
1078 remove_irp_type(res);
1079 for (i = 0; i < len; i++) {
1080 /* use res as default if corresponding type is not yet set. */
1081 type *elt_type = tps[i].tp ? tps[i].tp : res;
1083 tps[i].ent = new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1088 /* Create a new method type.
1089 N_param is the number of parameters, n_res the number of results. */
1090 type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1093 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1094 res = new_type(type_method, mode_P_code, name, db);
1095 res->state = layout_fixed;
1096 res->size = get_mode_size_bits(mode_P_code);
1097 res->attr.ma.n_params = n_param;
1098 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1099 res->attr.ma.value_params = NULL;
1100 res->attr.ma.n_res = n_res;
1101 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1102 res->attr.ma.value_ress = NULL;
1103 res->attr.ma.variadicity = variadicity_non_variadic;
1104 res->attr.ma.first_variadic_param = -1;
1109 type *new_type_method(ident *name, int n_param, int n_res) {
1110 return new_d_type_method(name, n_param, n_res, NULL);
1113 void free_method_entities(type *method) {
1114 assert(method && (method->type_op == type_method));
1117 /* Attention: also frees entities in value parameter subtypes! */
1118 void free_method_attrs(type *method) {
1119 assert(method && (method->type_op == type_method));
1120 free(method->attr.ma.param_type);
1121 free(method->attr.ma.res_type);
1122 if (method->attr.ma.value_params) {
1123 free_type_entities(method->attr.ma.value_params);
1124 free_type(method->attr.ma.value_params);
1126 if (method->attr.ma.value_ress) {
1127 free_type_entities(method->attr.ma.value_ress);
1128 free_type(method->attr.ma.value_ress);
1132 /* manipulate private fields of method. */
1133 int (get_method_n_params)(const type *method) {
1134 return _get_method_n_params(method);
1137 type *get_method_param_type(type *method, int pos) {
1139 assert(method && (method->type_op == type_method));
1140 assert(pos >= 0 && pos < get_method_n_params(method));
1141 res = method->attr.ma.param_type[pos].tp;
1142 assert(res != NULL && "empty method param type");
1143 return method->attr.ma.param_type[pos].tp = skip_tid(res);
1146 void set_method_param_type(type *method, int pos, type* tp) {
1147 assert(method && (method->type_op == type_method));
1148 assert(pos >= 0 && pos < get_method_n_params(method));
1149 method->attr.ma.param_type[pos].tp = tp;
1150 /* If information constructed set pass-by-value representation. */
1151 if (method->attr.ma.value_params) {
1152 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1153 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1157 /* Returns an entity that represents the copied value argument. Only necessary
1158 for compounds passed by value. */
1159 entity *get_method_value_param_ent(type *method, int pos) {
1160 assert(method && (method->type_op == type_method));
1161 assert(pos >= 0 && pos < get_method_n_params(method));
1163 if (!method->attr.ma.value_params) {
1164 /* parameter value type not created yet, build */
1165 method->attr.ma.value_params
1166 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1167 get_method_n_params(method), method->attr.ma.param_type);
1170 * build_value_type() sets the method->attr.ma.value_params type as default if
1173 assert((get_entity_type(method->attr.ma.param_type[pos].ent) != method->attr.ma.value_params)
1174 && "param type not yet set");
1175 return method->attr.ma.param_type[pos].ent;
1179 * Returns a type that represents the copied value arguments.
1181 type *get_method_value_param_type(const type *method)
1183 assert(method && (method->type_op == type_method));
1184 return method->attr.ma.value_params;
1187 int (get_method_n_ress)(const type *method) {
1188 return _get_method_n_ress(method);
1191 type *get_method_res_type(type *method, int pos) {
1193 assert(method && (method->type_op == type_method));
1194 assert(pos >= 0 && pos < get_method_n_ress(method));
1195 res = method->attr.ma.res_type[pos].tp;
1196 assert(res != NULL && "empty method return type");
1197 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1200 void set_method_res_type(type *method, int pos, type* tp) {
1201 assert(method && (method->type_op == type_method));
1202 assert(pos >= 0 && pos < get_method_n_ress(method));
1203 /* set the result type */
1204 method->attr.ma.res_type[pos].tp = tp;
1205 /* If information constructed set pass-by-value representation. */
1206 if (method->attr.ma.value_ress) {
1207 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1208 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1212 /* Returns an entity that represents the copied value result. Only necessary
1213 for compounds passed by value. */
1214 entity *get_method_value_res_ent(type *method, int pos) {
1215 assert(method && (method->type_op == type_method));
1216 assert(pos >= 0 && pos < get_method_n_ress(method));
1218 if (!method->attr.ma.value_ress) {
1219 /* result value type not created yet, build */
1220 method->attr.ma.value_ress
1221 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1222 get_method_n_ress(method), method->attr.ma.res_type);
1225 * build_value_type() sets the method->attr.ma.value_ress type as default if
1228 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1229 && "result type not yet set");
1231 return method->attr.ma.res_type[pos].ent;
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_d_type_union(ident *name, dbg_info *db) {
1310 type *res = new_type(type_union, NULL, name, db);
1312 res->attr.ua.members = NEW_ARR_F(entity *, 0);
1317 type *new_type_union(ident *name) {
1318 return new_d_type_union(name, NULL);
1321 void free_union_entities(type *uni) {
1323 assert(uni && (uni->type_op == type_union));
1324 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1325 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);
1333 /* manipulate private fields of union */
1334 int get_union_n_members (const type *uni) {
1335 assert(uni && (uni->type_op == type_union));
1336 return (ARR_LEN (uni->attr.ua.members));
1338 void add_union_member (type *uni, entity *member) {
1339 assert(uni && (uni->type_op == type_union));
1340 assert(uni != get_entity_type(member) && "recursive type");
1341 ARR_APP1 (entity *, uni->attr.ua.members, member);
1343 entity *get_union_member (const type *uni, int pos) {
1344 assert(uni && (uni->type_op == type_union));
1345 assert(pos >= 0 && pos < get_union_n_members(uni));
1346 return uni->attr.ua.members[pos];
1348 void set_union_member (type *uni, int pos, entity *member) {
1349 assert(uni && (uni->type_op == type_union));
1350 assert(pos >= 0 && pos < get_union_n_members(uni));
1351 uni->attr.ua.members[pos] = member;
1353 void remove_union_member(type *uni, entity *member) {
1355 assert(uni && (uni->type_op == type_union));
1356 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1357 if (uni->attr.ua.members[i] == member) {
1358 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1359 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1360 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1366 int (is_Union_type)(const type *uni) {
1367 return _is_union_type(uni);
1370 void set_union_size_bits(type *tp, int size) {
1371 /* argh: we must allow to set negative values as "invalid size" */
1372 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1373 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1376 /*-----------------------------------------------------------------*/
1378 /*-----------------------------------------------------------------*/
1381 /* create a new type array -- set dimension sizes independently */
1382 type *new_d_type_array(ident *name, int n_dimensions, type *element_type, dbg_info *db) {
1386 ir_graph *rem = current_ir_graph;
1388 assert(!is_Method_type(element_type));
1390 res = new_type(type_array, NULL, name, db);
1391 res->attr.aa.n_dimensions = n_dimensions;
1392 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1393 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1394 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1396 current_ir_graph = get_const_code_irg();
1397 unk = new_Unknown( mode_Iu);
1398 for (i = 0; i < n_dimensions; i++) {
1399 res->attr.aa.lower_bound[i] =
1400 res->attr.aa.upper_bound[i] = unk;
1401 res->attr.aa.order[i] = i;
1403 current_ir_graph = rem;
1405 res->attr.aa.element_type = element_type;
1406 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1411 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1412 return new_d_type_array(name, n_dimensions, element_type, NULL);
1415 void free_array_automatic_entities(type *array) {
1416 assert(array && (array->type_op == type_array));
1417 free_entity(get_array_element_entity(array));
1420 void free_array_entities (type *array) {
1421 assert(array && (array->type_op == type_array));
1424 void free_array_attrs (type *array) {
1425 assert(array && (array->type_op == type_array));
1426 free(array->attr.aa.lower_bound);
1427 free(array->attr.aa.upper_bound);
1430 /* manipulate private fields of array type */
1431 int get_array_n_dimensions (const type *array) {
1432 assert(array && (array->type_op == type_array));
1433 return array->attr.aa.n_dimensions;
1437 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1438 ir_node * upper_bound) {
1439 assert(array && (array->type_op == type_array));
1440 assert(lower_bound && "lower_bound node may not be NULL.");
1441 assert(upper_bound && "upper_bound node may not be NULL.");
1442 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1443 array->attr.aa.lower_bound[dimension] = lower_bound;
1444 array->attr.aa.upper_bound[dimension] = upper_bound;
1447 set_array_bounds_int (type *array, int dimension, int lower_bound,
1449 ir_graph *rem = current_ir_graph;
1450 current_ir_graph = get_const_code_irg();
1451 set_array_bounds (array, dimension,
1452 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1453 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1454 current_ir_graph = rem;
1457 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1458 assert(array && (array->type_op == type_array));
1459 assert(lower_bound && "lower_bound node may not be NULL.");
1460 array->attr.aa.lower_bound[dimension] = lower_bound;
1462 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1463 ir_graph *rem = current_ir_graph;
1464 current_ir_graph = get_const_code_irg();
1465 set_array_lower_bound (array, dimension,
1466 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1467 current_ir_graph = rem;
1470 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1471 assert(array && (array->type_op == type_array));
1472 assert(upper_bound && "upper_bound node may not be NULL.");
1473 array->attr.aa.upper_bound[dimension] = upper_bound;
1475 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1476 ir_graph *rem = current_ir_graph;
1477 current_ir_graph = get_const_code_irg();
1478 set_array_upper_bound (array, dimension,
1479 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1480 current_ir_graph = rem;
1482 int has_array_lower_bound (const type *array, int dimension) {
1483 assert(array && (array->type_op == type_array));
1484 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1486 ir_node *get_array_lower_bound (const type *array, int dimension) {
1487 assert(array && (array->type_op == type_array));
1488 return array->attr.aa.lower_bound[dimension];
1490 long get_array_lower_bound_int (const type *array, int dimension) {
1492 assert(array && (array->type_op == type_array));
1493 node = array->attr.aa.lower_bound[dimension];
1494 assert(get_irn_op(node) == op_Const);
1495 return get_tarval_long(get_Const_tarval(node));
1497 int has_array_upper_bound (const type *array, int dimension) {
1498 assert(array && (array->type_op == type_array));
1499 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1501 ir_node * get_array_upper_bound (const type *array, int dimension) {
1502 assert(array && (array->type_op == type_array));
1503 return array->attr.aa.upper_bound[dimension];
1505 long get_array_upper_bound_int (const type *array, int dimension) {
1507 assert(array && (array->type_op == type_array));
1508 node = array->attr.aa.upper_bound[dimension];
1509 assert(get_irn_op(node) == op_Const);
1510 return get_tarval_long(get_Const_tarval(node));
1513 void set_array_order (type *array, int dimension, int order) {
1514 assert(array && (array->type_op == type_array));
1515 array->attr.aa.order[dimension] = order;
1518 int get_array_order (const type *array, int dimension) {
1519 assert(array && (array->type_op == type_array));
1520 return array->attr.aa.order[dimension];
1523 int find_array_dimension(const type *array, int order) {
1526 assert(array && (array->type_op == type_array));
1528 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1529 if (array->attr.aa.order[dim] == order)
1535 void set_array_element_type (type *array, type *tp) {
1536 assert(array && (array->type_op == type_array));
1537 assert(!is_Method_type(tp));
1538 array->attr.aa.element_type = tp;
1540 type *get_array_element_type (type *array) {
1541 assert(array && (array->type_op == type_array));
1542 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1545 void set_array_element_entity (type *array, entity *ent) {
1546 assert(array && (array->type_op == type_array));
1547 assert((get_entity_type(ent)->type_op != type_method));
1548 array->attr.aa.element_ent = ent;
1549 array->attr.aa.element_type = get_entity_type(ent);
1551 entity *get_array_element_entity (const type *array) {
1552 assert(array && (array->type_op == type_array));
1553 return array->attr.aa.element_ent;
1557 int (is_Array_type)(const type *array) {
1558 return _is_array_type(array);
1561 void set_array_size_bits(type *tp, int size) {
1562 /* FIXME: Here we should make some checks with the element type size */
1565 /*-----------------------------------------------------------------*/
1566 /* TYPE_ENUMERATION */
1567 /*-----------------------------------------------------------------*/
1569 /* create a new type enumeration -- set the enumerators independently */
1570 type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1571 type *res = new_type(type_enumeration, NULL, name, db);
1573 res->attr.ea.n_enums = n_enums;
1574 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1575 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1580 type *new_type_enumeration(ident *name, int n_enums) {
1581 return new_d_type_enumeration(name, n_enums, NULL);
1584 void free_enumeration_entities(type *enumeration) {
1585 assert(enumeration && (enumeration->type_op == type_enumeration));
1587 void free_enumeration_attrs(type *enumeration) {
1588 assert(enumeration && (enumeration->type_op == type_enumeration));
1589 free(enumeration->attr.ea.enumer);
1590 free(enumeration->attr.ea.enum_nameid);
1593 /* manipulate fields of enumeration type. */
1594 int get_enumeration_n_enums (const type *enumeration) {
1595 assert(enumeration && (enumeration->type_op == type_enumeration));
1596 return enumeration->attr.ea.n_enums;
1598 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1599 assert(enumeration && (enumeration->type_op == type_enumeration));
1600 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1601 enumeration->attr.ea.enumer[pos] = con;
1603 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1604 assert(enumeration && (enumeration->type_op == type_enumeration));
1605 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1606 return enumeration->attr.ea.enumer[pos];
1608 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1609 assert(enumeration && (enumeration->type_op == type_enumeration));
1610 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1611 enumeration->attr.ea.enum_nameid[pos] = id;
1613 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1614 assert(enumeration && (enumeration->type_op == type_enumeration));
1615 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1616 return enumeration->attr.ea.enum_nameid[pos];
1618 const char *get_enumeration_name(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 get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1625 int (is_Enumeration_type)(const type *enumeration) {
1626 return _is_enumeration_type(enumeration);
1629 void set_enumeration_mode(type *tp, ir_mode *mode) {
1630 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1631 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1632 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1634 tp->size = get_mode_size_bits(mode);
1638 /*-----------------------------------------------------------------*/
1640 /*-----------------------------------------------------------------*/
1642 /* Create a new type pointer */
1643 type *new_d_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1646 assert(mode_is_reference(ptr_mode));
1647 res = new_type(type_pointer, ptr_mode, name, db);
1648 res->attr.pa.points_to = points_to;
1649 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1650 res->size = get_mode_size_bits(res->mode);
1651 res->state = layout_fixed;
1656 type *new_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode) {
1657 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1660 void free_pointer_entities (type *pointer) {
1661 assert(pointer && (pointer->type_op == type_pointer));
1664 void free_pointer_attrs (type *pointer) {
1665 assert(pointer && (pointer->type_op == type_pointer));
1668 /* manipulate fields of type_pointer */
1669 void set_pointer_points_to_type (type *pointer, type *tp) {
1670 assert(pointer && (pointer->type_op == type_pointer));
1671 pointer->attr.pa.points_to = tp;
1674 type *get_pointer_points_to_type (type *pointer) {
1675 assert(pointer && (pointer->type_op == type_pointer));
1676 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1680 int (is_Pointer_type)(const type *pointer) {
1681 return _is_pointer_type(pointer);
1684 void set_pointer_mode(type *tp, ir_mode *mode) {
1685 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1686 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1687 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1689 tp->size = get_mode_size_bits(mode);
1693 /* Returns the first pointer type that has as points_to tp.
1694 * Not efficient: O(#types).
1695 * If not found returns firm_unknown_type. */
1696 type *find_pointer_type_to_type (type *tp) {
1698 for (i = 0; i < get_irp_n_types(); ++i) {
1699 type *found = get_irp_type(i);
1700 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1703 return firm_unknown_type;
1707 /*-----------------------------------------------------------------*/
1708 /* TYPE_PRIMITIVE */
1709 /*-----------------------------------------------------------------*/
1711 /* create a new type primitive */
1712 type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1714 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1715 res = new_type(type_primitive, mode, name, db);
1716 res->size = get_mode_size_bits(mode);
1717 res->state = layout_fixed;
1722 type *new_type_primitive(ident *name, ir_mode *mode) {
1723 return new_d_type_primitive(name, mode, NULL);
1727 int (is_Primitive_type)(const type *primitive) {
1728 return _is_primitive_type(primitive);
1731 void set_primitive_mode(type *tp, ir_mode *mode) {
1732 /* Modes of primitives must be data */
1733 assert(mode_is_data(mode));
1735 /* For primitive size depends on the mode. */
1736 tp->size = get_mode_size_bits(mode);
1741 /*-----------------------------------------------------------------*/
1742 /* common functionality */
1743 /*-----------------------------------------------------------------*/
1746 int (is_atomic_type)(const type *tp) {
1747 return _is_atomic_type(tp);
1751 * Gets the number of elements in a firm compound type.
1753 int get_compound_n_members(const type *tp)
1755 const tp_op *op = get_type_tpop(tp);
1758 if (op->ops.get_n_members)
1759 res = op->ops.get_n_members(tp);
1761 assert(0 && "no member count for this type");
1767 * Gets the member of a firm compound type at position pos.
1769 entity *get_compound_member(const type *tp, int pos)
1771 const tp_op *op = get_type_tpop(tp);
1774 if (op->ops.get_member)
1775 res = op->ops.get_member(tp, pos);
1777 assert(0 && "no members in this type");
1782 int is_compound_type(const type *tp) {
1783 assert(tp && tp->kind == k_type);
1784 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1787 /* Checks, whether a type is a frame type */
1788 int is_frame_type(const type *tp) {
1789 return tp->frame_type;
1792 /* Makes a new frame type. */
1793 type *new_type_frame(ident *name)
1795 type *res = new_type_class(name);
1797 res->frame_type = 1;
1799 /* Remove type from type list. Must be treated differently than other types. */
1800 remove_irp_type(res);
1805 /* set the type size for the unknown and none type */
1806 void set_default_size_bits(type *tp, int size) {