3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * file type.c - implementation of the datastructure to hold
17 * (C) 2001 by Universitaet Karlsruhe
20 * This module supplies a datastructure to represent all types
21 * known in the compiled program. This includes types specified
22 * in the program as well as types defined by the language. In the
23 * view of the intermediate representation there is no difference
24 * between these types.
26 * There exist several kinds of types, arranged by the structure of
27 * the type. A type is described by a set of attributes. Some of
28 * these attributes are common to all types, others depend on the
31 * Types are different from the modes defined in irmode: Types are
32 * on the level of the programming language, modes at the level of
33 * the target processor.
35 * @see type_t.h type tpop
48 # include "irprog_t.h"
51 # include "typegmod.h"
57 /*******************************************************************/
59 /*******************************************************************/
61 type *none_type; type *get_none_type(void) { return none_type; }
62 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
66 /** Returns a new, unique number to number nodes or the like. */
67 int get_irp_new_node_nr(void);
70 /* Suffixes added to types used for pass-by-value representations. */
71 static ident *value_params_suffix = NULL;
72 static ident *value_ress_suffix = NULL;
74 void init_type(void) {
75 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
76 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
78 /* construct none and unknown type. */
79 none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
80 set_type_size_bits(none_type, 0);
81 set_type_state (none_type, layout_fixed);
82 remove_irp_type(none_type);
83 unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
84 set_type_size_bits(unknown_type, 0);
85 set_type_state (unknown_type, layout_fixed);
86 remove_irp_type(unknown_type);
89 unsigned long type_visited;
91 void (set_master_type_visited)(unsigned long val) { __set_master_type_visited(val); }
92 unsigned long (get_master_type_visited)(void) { return __get_master_type_visited(); }
93 void (inc_master_type_visited)(void) { __inc_master_type_visited(); }
97 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
101 assert(type_op != type_id);
102 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
104 node_size = offsetof(type, attr) + type_op->attr_size;
105 res = (type *) xmalloc (node_size);
106 memset((void *)res, 0, node_size);
107 add_irp_type(res); /* Remember the new type global. */
110 res->type_op = type_op;
113 res->state = layout_undefined;
119 res->nr = get_irp_new_node_nr();
120 res->c_name = (char*) get_id_str (name);
121 #endif /* defined DEBUG_libfirm */
126 void free_type(type *tp) {
127 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
129 /* Remove from list of all types */
131 /* Free the attributes of the type. */
133 /* Free entities automatically allocated with the type */
134 if (is_array_type(tp))
135 free_entity(get_array_element_entity(tp));
136 /* And now the type itself... */
141 void free_type_entities(type *tp) {
142 switch(get_type_tpop_code(tp)) {
143 case tpo_class: { free_class_entities(tp); } break;
144 case tpo_struct: { free_struct_entities(tp); } break;
145 case tpo_method: { free_method_entities(tp); } break;
146 case tpo_union: { free_union_entities(tp); } break;
147 case tpo_array: { free_array_entities(tp); } break;
148 case tpo_enumeration: { free_enumeration_entities(tp); } break;
149 case tpo_pointer: { free_pointer_entities(tp); } break;
150 case tpo_primitive: { free_primitive_entities(tp); } break;
155 void free_type_attrs(type *tp) {
156 switch(get_type_tpop_code(tp)) {
157 case tpo_class: { free_class_attrs(tp); } break;
158 case tpo_struct: { free_struct_attrs(tp); } break;
159 case tpo_method: { free_method_attrs(tp); } break;
160 case tpo_union: { free_union_attrs(tp); } break;
161 case tpo_array: { free_array_attrs(tp); } break;
162 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
163 case tpo_pointer: { free_pointer_attrs(tp); } break;
164 case tpo_primitive: { free_primitive_attrs(tp); } break;
169 /* set/get the link field */
170 void *(get_type_link)(const type *tp)
172 return __get_type_link(tp);
175 void (set_type_link)(type *tp, void *l)
177 __set_type_link(tp, l);
180 tp_op *(get_type_tpop)(const type *tp) {
181 return __get_type_tpop(tp);
184 ident *(get_type_tpop_nameid)(const type *tp) {
185 return __get_type_tpop_nameid(tp);
188 const char* get_type_tpop_name(const type *tp) {
189 assert(tp && tp->kind == k_type);
190 return get_id_str(tp->type_op->name);
193 tp_opcode (get_type_tpop_code)(const type *tp) {
194 return __get_type_tpop_code(tp);
197 ir_mode *(get_type_mode)(const type *tp) {
198 return __get_type_mode(tp);
201 void set_type_mode(type *tp, ir_mode* m) {
202 assert(tp && tp->kind == k_type);
204 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
205 /* Modes of primitives must be data */
206 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
207 /* Modes of enumerations must be integers */
208 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
209 /* Modes of pointers must be references. */
211 switch (get_type_tpop_code(tp)) {
213 /* For primitive size depends on the mode. */
214 tp->size = get_mode_size_bits(m);
217 case tpo_enumeration:
219 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
220 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
221 tp->size = get_mode_size_bits(m);
226 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
227 assert(get_type_state(tp) == layout_fixed &&
228 tp->size == get_mode_size_bits(m) &&
229 "mode don't match struct/class layout");
233 assert(0 && "setting a mode is NOT allowed for this type");
237 ident *(get_type_ident)(const type *tp) {
238 return __get_type_ident(tp);
241 void (set_type_ident)(type *tp, ident* id) {
242 __set_type_ident(tp, id);
245 /* Outputs a unique number for this node */
246 long (get_type_nr)(const type *tp) {
247 return __get_type_nr(tp);
250 const char* get_type_name(const type *tp) {
251 assert(tp && tp->kind == k_type);
252 return (get_id_str(tp->name));
255 int (get_type_size_bytes)(const type *tp) {
256 return __get_type_size_bytes(tp);
259 int (get_type_size_bits)(const type *tp) {
260 return __get_type_size_bits(tp);
264 set_type_size_bits(type *tp, int size) {
265 assert(tp && tp->kind == k_type);
266 /* For pointer enumeration and primitive size depends on the mode.
267 Methods don't have a size. */
268 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
269 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
270 if (tp->type_op == type_primitive)
273 /* argh: we must allow to set negative values as "invalid size" */
274 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
275 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
281 set_type_size_bytes(type *tp, int size) {
282 set_type_size_bits(tp, 8*size);
285 int get_type_alignment_bytes(type *tp) {
286 int align = get_type_alignment_bits(tp);
288 return align < 0 ? align : (align + 7) >> 3;
291 int get_type_alignment_bits(type *tp) {
297 /* alignment NOT set calculate it "on demand" */
299 align = get_mode_size_bits(tp->mode);
300 else if (is_array_type(tp))
301 align = get_type_alignment_bits(get_array_element_type(tp));
302 else if (is_compound_type(tp)) {
303 int i, n = get_compound_n_members(tp);
306 for (i = 0; i < n; ++i) {
307 type *t = get_entity_type(get_compound_member(tp, i));
308 int a = get_type_alignment_bits(t);
314 else if (is_method_type(tp))
324 set_type_alignment_bits(type *tp, int align) {
325 assert(tp && tp->kind == k_type);
326 /* Methods don't have an alignment. */
327 if (tp->type_op != type_method) {
333 set_type_alignment_bytes(type *tp, int align) {
334 set_type_size_bits(tp, 8*align);
337 type_state (get_type_state)(const type *tp) {
338 return __get_type_state(tp);
342 set_type_state(type *tp, type_state state) {
343 assert(tp && tp->kind == k_type);
345 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
346 (tp->type_op == type_method))
349 /* Just a correctness check: */
350 if (state == layout_fixed) {
352 switch (get_type_tpop_code(tp)) {
355 assert(get_type_size_bits(tp) > -1);
356 if (tp != get_glob_type()) {
357 int n_mem = get_class_n_members(tp);
358 for (i = 0; i < n_mem; i++) {
359 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
360 { DDMT(tp); DDME(get_class_member(tp, i)); }
361 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
363 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
364 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
371 assert(get_type_size_bits(tp) > -1);
372 for (i = 0; i < get_struct_n_members(tp); i++) {
373 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
374 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
383 Assure that only innermost dimension is dynamic? */
385 case tpo_enumeration:
387 assert(get_type_mode != NULL);
388 for (i = 0; i < get_enumeration_n_enums(tp); i++)
389 assert(get_enumeration_enum(tp, i) != NULL);
397 unsigned long (get_type_visited)(const type *tp) {
398 return __get_type_visited(tp);
401 void (set_type_visited)(type *tp, unsigned long num) {
402 __set_type_visited(tp, num);
405 /* Sets visited field in type to type_visited. */
406 void (mark_type_visited)(type *tp) {
407 __mark_type_visited(tp);
410 /* @@@ name clash with master flag
411 int (type_visited)(const type *tp) {
412 return __type_visited(tp);
415 int (type_not_visited)(const type *tp) {
416 return __type_not_visited(tp);
419 int (is_type)(const void *thing) {
420 return __is_type(thing);
423 bool equal_type(type *typ1, type *typ2) {
428 if (typ1 == typ2) return true;
430 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
431 (get_type_ident(typ1) != get_type_ident(typ2)) ||
432 (get_type_mode(typ1) != get_type_mode(typ2)) ||
433 (get_type_state(typ1) != get_type_state(typ2)))
435 if ((get_type_state(typ1) == layout_fixed) &&
436 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
439 switch(get_type_tpop_code(typ1)) {
441 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
442 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
443 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
444 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
445 /** Compare the members **/
446 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
447 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
448 /* First sort the members of typ2 */
449 for (i = 0; i < get_class_n_members(typ1); i++) {
450 entity *e1 = get_class_member(typ1, i);
451 for (j = 0; j < get_class_n_members(typ2); j++) {
452 entity *e2 = get_class_member(typ2, j);
453 if (get_entity_name(e1) == get_entity_name(e2))
457 for (i = 0; i < get_class_n_members(typ1); i++) {
458 if (!m[i] || /* Found no counterpart */
459 !equal_entity(get_class_member(typ1, i), m[i]))
462 /** Compare the supertypes **/
463 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
464 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
465 /* First sort the supertypes of typ2 */
466 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
467 type *t1 = get_class_supertype(typ1, i);
468 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
469 type *t2 = get_class_supertype(typ2, j);
470 if (get_type_ident(t2) == get_type_ident(t1))
474 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
475 if (!t[i] || /* Found no counterpart */
476 get_class_supertype(typ1, i) != t[i])
481 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
482 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
483 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
484 /* First sort the members of lt */
485 for (i = 0; i < get_struct_n_members(typ1); i++) {
486 entity *e1 = get_struct_member(typ1, i);
487 for (j = 0; j < get_struct_n_members(typ2); j++) {
488 entity *e2 = get_struct_member(typ2, j);
489 if (get_entity_name(e1) == get_entity_name(e2))
493 for (i = 0; i < get_struct_n_members(typ1); i++) {
494 if (!m[i] || /* Found no counterpart */
495 !equal_entity(get_struct_member(typ1, i), m[i]))
500 int n_param1, n_param2;
502 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
503 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
505 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
506 n_param1 = get_method_n_params(typ1);
507 n_param2 = get_method_n_params(typ2);
510 n_param1 = get_method_first_variadic_param_index(typ1);
511 n_param2 = get_method_first_variadic_param_index(typ2);
514 if (n_param1 != n_param2) return false;
516 for (i = 0; i < n_param1; i++) {
517 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
520 for (i = 0; i < get_method_n_ress(typ1); i++) {
521 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
526 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
527 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
528 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
529 /* First sort the members of lt */
530 for (i = 0; i < get_union_n_members(typ1); i++) {
531 entity *e1 = get_union_member(typ1, i);
532 for (j = 0; j < get_union_n_members(typ2); j++) {
533 entity *e2 = get_union_member(typ2, j);
534 if (get_entity_name(e1) == get_entity_name(e2))
538 for (i = 0; i < get_union_n_members(typ1); i++) {
539 if (!m[i] || /* Found no counterpart */
540 !equal_entity(get_union_member(typ1, i), m[i]))
545 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
547 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
549 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
550 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
551 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
553 if (get_array_order(typ1, i) != get_array_order(typ2, i))
554 assert(0 && "type compare with different dimension orders not implemented");
557 case tpo_enumeration: {
558 assert(0 && "enumerations not implemented");
561 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
564 case tpo_primitive: {
571 bool smaller_type (type *st, type *lt) {
575 if (st == lt) return true;
577 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
580 switch(get_type_tpop_code(st)) {
582 return is_subclass_of(st, lt);
585 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
586 m = alloca(sizeof(entity *) * get_struct_n_members(st));
587 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
588 /* First sort the members of lt */
589 for (i = 0; i < get_struct_n_members(st); i++) {
590 entity *se = get_struct_member(st, i);
591 for (j = 0; j < get_struct_n_members(lt); j++) {
592 entity *le = get_struct_member(lt, j);
593 if (get_entity_name(le) == get_entity_name(se))
597 for (i = 0; i < get_struct_n_members(st); i++) {
598 if (!m[i] || /* Found no counterpart */
599 !smaller_type(get_entity_type(get_struct_member(st, i)),
600 get_entity_type(m[i])))
605 /** FIXME: is this still true? */
606 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
607 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
608 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
609 for (i = 0; i < get_method_n_params(st); i++) {
610 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
613 for (i = 0; i < get_method_n_ress(st); i++) {
614 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
619 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
620 m = alloca(sizeof(entity *) * get_union_n_members(st));
621 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
622 /* First sort the members of lt */
623 for (i = 0; i < get_union_n_members(st); i++) {
624 entity *se = get_union_member(st, i);
625 for (j = 0; j < get_union_n_members(lt); j++) {
626 entity *le = get_union_member(lt, j);
627 if (get_entity_name(le) == get_entity_name(se))
631 for (i = 0; i < get_union_n_members(st); i++) {
632 if (!m[i] || /* Found no counterpart */
633 !smaller_type(get_entity_type(get_union_member(st, i)),
634 get_entity_type(m[i])))
639 type *set, *let; /* small/large elt. type */
640 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
642 set = get_array_element_type(st);
643 let = get_array_element_type(lt);
645 /* If the elt types are different, set must be convertible
646 to let, and they must have the same size so that address
647 computations work out. To have a size the layout must
649 if ((get_type_state(set) != layout_fixed) ||
650 (get_type_state(let) != layout_fixed))
652 if (!smaller_type(set, let) ||
653 get_type_size_bits(set) != get_type_size_bits(let))
656 for(i = 0; i < get_array_n_dimensions(st); i++) {
657 if (get_array_lower_bound(lt, i))
658 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
660 if (get_array_upper_bound(lt, i))
661 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
665 case tpo_enumeration: {
666 assert(0 && "enumerations not implemented");
669 if (!smaller_type(get_pointer_points_to_type(st),
670 get_pointer_points_to_type(lt)))
673 case tpo_primitive: {
674 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
682 /*-----------------------------------------------------------------*/
684 /*-----------------------------------------------------------------*/
686 /* create a new class type */
687 type *new_type_class (ident *name) {
690 res = new_type(type_class, NULL, name);
692 res->attr.ca.members = NEW_ARR_F (entity *, 0);
693 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
694 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
695 res->attr.ca.peculiarity = peculiarity_existent;
696 res->attr.ca.dfn = 0;
700 type *new_d_type_class (ident *name, dbg_info* db) {
701 type *res = new_type_class (name);
702 set_type_dbg_info(res, db);
706 void free_class_entities(type *clss) {
708 assert(clss && (clss->type_op == type_class));
709 for (i = get_class_n_members(clss)-1; i >= 0; --i)
710 free_entity(get_class_member(clss, i));
713 void free_class_attrs(type *clss) {
714 assert(clss && (clss->type_op == type_class));
715 DEL_ARR_F(clss->attr.ca.members);
716 DEL_ARR_F(clss->attr.ca.subtypes);
717 DEL_ARR_F(clss->attr.ca.supertypes);
720 /* manipulate private fields of class type */
721 void add_class_member (type *clss, entity *member) {
722 assert(clss && (clss->type_op == type_class));
723 ARR_APP1 (entity *, clss->attr.ca.members, member);
726 int (get_class_n_members) (const type *clss) {
727 return __get_class_n_members(clss);
730 int get_class_member_index(type *clss, entity *mem) {
732 assert(clss && (clss->type_op == type_class));
733 for (i = 0; i < get_class_n_members(clss); i++)
734 if (get_class_member(clss, i) == mem)
739 entity *(get_class_member) (const type *clss, int pos) {
740 return __get_class_member(clss, pos);
743 entity *get_class_member_by_name(type *clss, ident *name) {
745 assert(clss && (clss->type_op == type_class));
746 n_mem = get_class_n_members(clss);
747 for (i = 0; i < n_mem; ++i) {
748 entity *mem = get_class_member(clss, i);
749 if (get_entity_ident(mem) == name) return mem;
754 void set_class_member (type *clss, entity *member, int pos) {
755 assert(clss && (clss->type_op == type_class));
756 assert(pos >= 0 && pos < get_class_n_members(clss));
757 clss->attr.ca.members[pos] = member;
759 void set_class_members (type *clss, entity **members, int arity) {
761 assert(clss && (clss->type_op == type_class));
762 DEL_ARR_F(clss->attr.ca.members);
763 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
764 for (i = 0; i < arity; i++) {
765 set_entity_owner(members[i], clss);
766 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
769 void remove_class_member(type *clss, entity *member) {
771 assert(clss && (clss->type_op == type_class));
772 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
773 if (clss->attr.ca.members[i] == member) {
774 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
775 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
776 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
782 void add_class_subtype (type *clss, type *subtype) {
784 assert(clss && (clss->type_op == type_class));
785 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
786 for (i = 0; i < get_class_n_supertypes(subtype); i++)
787 if (get_class_supertype(subtype, i) == clss)
788 /* Class already registered */
790 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
792 int get_class_n_subtypes (const type *clss) {
793 assert(clss && (clss->type_op == type_class));
794 return (ARR_LEN (clss->attr.ca.subtypes));
796 type *get_class_subtype (type *clss, int pos) {
797 assert(clss && (clss->type_op == type_class));
798 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
799 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
801 void set_class_subtype (type *clss, type *subtype, int pos) {
802 assert(clss && (clss->type_op == type_class));
803 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
804 clss->attr.ca.subtypes[pos] = subtype;
806 void remove_class_subtype(type *clss, type *subtype) {
808 assert(clss && (clss->type_op == type_class));
809 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
810 if (clss->attr.ca.subtypes[i] == subtype) {
811 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
812 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
813 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
818 void add_class_supertype (type *clss, type *supertype) {
820 assert(clss && (clss->type_op == type_class));
821 assert(supertype && (supertype -> type_op == type_class));
822 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
823 for (i = 0; i < get_class_n_subtypes(supertype); i++)
824 if (get_class_subtype(supertype, i) == clss)
825 /* Class already registered */
827 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
829 int get_class_n_supertypes (const type *clss) {
830 assert(clss && (clss->type_op == type_class));
831 return (ARR_LEN (clss->attr.ca.supertypes));
833 int get_class_supertype_index(type *clss, type *super_clss) {
835 assert(clss && (clss->type_op == type_class));
836 assert(super_clss && (super_clss->type_op == type_class));
837 for (i = 0; i < get_class_n_supertypes(clss); i++)
838 if (get_class_supertype(clss, i) == super_clss)
842 type *get_class_supertype (type *clss, int pos) {
843 assert(clss && (clss->type_op == type_class));
844 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
845 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
847 void set_class_supertype (type *clss, type *supertype, int pos) {
848 assert(clss && (clss->type_op == type_class));
849 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
850 clss->attr.ca.supertypes[pos] = supertype;
852 void remove_class_supertype(type *clss, type *supertype) {
854 assert(clss && (clss->type_op == type_class));
855 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
856 if (clss->attr.ca.supertypes[i] == supertype) {
857 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
858 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
859 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
864 const char *get_peculiarity_string(peculiarity p) {
866 case peculiarity_description:
867 return "peculiarity_description";
868 case peculiarity_inherited:
869 return "peculiarity_inherited";
871 return "peculiarity_existent";
875 peculiarity get_class_peculiarity (const type *clss) {
876 assert(clss && (clss->type_op == type_class));
877 return clss->attr.ca.peculiarity;
880 void set_class_peculiarity (type *clss, peculiarity pec) {
881 assert(clss && (clss->type_op == type_class));
882 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
883 clss->attr.ca.peculiarity = pec;
886 void set_class_dfn (type *clss, int dfn)
888 clss->attr.ca.dfn = dfn;
891 int get_class_dfn (const type *clss)
893 return (clss->attr.ca.dfn);
897 int (is_class_type)(const type *clss) {
898 return __is_class_type(clss);
901 bool is_subclass_of(type *low, type *high) {
903 assert(is_class_type(low) && is_class_type(high));
904 if (low == high) return true;
905 /* depth first search from high downwards. */
906 for (i = 0; i < get_class_n_subtypes(high); i++) {
907 if (low == get_class_subtype(high, i))
909 if (is_subclass_of(low, get_class_subtype(high, i)))
915 /*----------------------------------------------------------------**/
917 /*----------------------------------------------------------------**/
919 /* create a new type struct */
920 type *new_type_struct (ident *name) {
922 res = new_type(type_struct, NULL, name);
923 res->attr.sa.members = NEW_ARR_F (entity *, 0);
926 type *new_d_type_struct (ident *name, dbg_info* db) {
927 type *res = new_type_struct (name);
928 set_type_dbg_info(res, db);
931 void free_struct_entities (type *strct) {
933 assert(strct && (strct->type_op == type_struct));
934 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
935 free_entity(get_struct_member(strct, i));
937 void free_struct_attrs (type *strct) {
938 assert(strct && (strct->type_op == type_struct));
939 DEL_ARR_F(strct->attr.sa.members);
942 /* manipulate private fields of struct */
943 int get_struct_n_members (const type *strct) {
944 assert(strct && (strct->type_op == type_struct));
945 return (ARR_LEN (strct->attr.sa.members));
948 void add_struct_member (type *strct, entity *member) {
949 assert(strct && (strct->type_op == type_struct));
950 assert(get_type_tpop(get_entity_type(member)) != type_method);
951 /* @@@ lowerfirm geht nicht durch */
952 ARR_APP1 (entity *, strct->attr.sa.members, member);
955 entity *get_struct_member (const type *strct, int pos) {
956 assert(strct && (strct->type_op == type_struct));
957 assert(pos >= 0 && pos < get_struct_n_members(strct));
958 return strct->attr.sa.members[pos];
961 int get_struct_member_index(type *strct, entity *mem) {
963 assert(strct && (strct->type_op == type_struct));
964 for (i = 0; i < get_struct_n_members(strct); i++)
965 if (get_struct_member(strct, i) == mem)
970 void set_struct_member (type *strct, int pos, entity *member) {
971 assert(strct && (strct->type_op == type_struct));
972 assert(pos >= 0 && pos < get_struct_n_members(strct));
973 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
974 strct->attr.sa.members[pos] = member;
976 void remove_struct_member(type *strct, entity *member) {
978 assert(strct && (strct->type_op == type_struct));
979 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
980 if (strct->attr.sa.members[i] == member) {
981 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
982 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
983 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
989 int (is_struct_type)(const type *strct) {
990 return __is_struct_type(strct);
993 /*******************************************************************/
995 /*******************************************************************/
998 * Lazy construction of value argument / result representation.
999 * Constructs a struct type and its member. The types of the members
1000 * are passed in the argument list.
1002 * @param name name of the type constructed
1003 * @param len number of fields
1004 * @param tps array of field types with length len
1006 static INLINE type *
1007 build_value_type(ident *name, int len, type **tps) {
1009 type *res = new_type_struct(name);
1010 /* Remove type from type list. Must be treated differently than other types. */
1011 remove_irp_type_from_list(res);
1012 for (i = 0; i < len; i++) {
1013 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1014 if (tps[i]) elt_type = tps[i];
1015 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1020 /* Create a new method type.
1021 N_param is the number of parameters, n_res the number of results. */
1022 type *new_type_method (ident *name, int n_param, int n_res) {
1025 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1026 res = new_type(type_method, mode_P_mach, name);
1027 res->state = layout_fixed;
1028 res->size = get_mode_size_bits(mode_P_mach);
1029 res->attr.ma.n_params = n_param;
1030 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
1031 res->attr.ma.value_params = NULL;
1032 res->attr.ma.n_res = n_res;
1033 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
1034 res->attr.ma.value_ress = NULL;
1035 res->attr.ma.variadicity = variadicity_non_variadic;
1036 res->attr.ma.first_variadic_param = -1;
1041 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1042 type *res = new_type_method (name, n_param, n_res);
1043 set_type_dbg_info(res, db);
1047 void free_method_entities(type *method) {
1048 assert(method && (method->type_op == type_method));
1051 /* Attention: also frees entities in value parameter subtypes! */
1052 void free_method_attrs(type *method) {
1053 assert(method && (method->type_op == type_method));
1054 free(method->attr.ma.param_type);
1055 free(method->attr.ma.res_type);
1056 if (method->attr.ma.value_params) {
1057 free_type_entities(method->attr.ma.value_params);
1058 free_type(method->attr.ma.value_params);
1060 if (method->attr.ma.value_ress) {
1061 free_type_entities(method->attr.ma.value_ress);
1062 free_type(method->attr.ma.value_ress);
1066 /* manipulate private fields of method. */
1067 int get_method_n_params (const type *method) {
1068 assert(method && (method->type_op == type_method));
1069 return method->attr.ma.n_params;
1072 type *get_method_param_type(type *method, int pos) {
1074 assert(method && (method->type_op == type_method));
1075 assert(pos >= 0 && pos < get_method_n_params(method));
1076 res = method->attr.ma.param_type[pos];
1077 assert(res != NULL && "empty method param type");
1078 return method->attr.ma.param_type[pos] = skip_tid(res);
1081 void set_method_param_type(type *method, int pos, type* tp) {
1082 assert(method && (method->type_op == type_method));
1083 assert(pos >= 0 && pos < get_method_n_params(method));
1084 method->attr.ma.param_type[pos] = tp;
1085 /* If information constructed set pass-by-value representation. */
1086 if (method->attr.ma.value_params) {
1087 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1088 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1092 /* Returns an entity that represents the copied value argument. Only necessary
1093 for compounds passed by value. */
1094 entity *get_method_value_param_ent(type *method, int pos) {
1095 assert(method && (method->type_op == type_method));
1096 assert(pos >= 0 && pos < get_method_n_params(method));
1097 if (!method->attr.ma.value_params)
1098 method->attr.ma.value_params
1099 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1100 get_method_n_params(method), method->attr.ma.param_type);
1101 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1102 != method->attr.ma.value_params)
1103 && "param type not yet set");
1104 return get_struct_member(method->attr.ma.value_params, pos);
1108 * Returns a type that represents the copied value arguments.
1110 type *get_method_value_param_type(const type *method)
1112 assert(method && (method->type_op == type_method));
1113 return method->attr.ma.value_params;
1116 int get_method_n_ress (const type *method) {
1117 assert(method && (method->type_op == type_method));
1118 return method->attr.ma.n_res;
1121 type *get_method_res_type(type *method, int pos) {
1123 assert(method && (method->type_op == type_method));
1124 assert(pos >= 0 && pos < get_method_n_ress(method));
1125 res = method->attr.ma.res_type[pos];
1126 assert(res != NULL && "empty method return type");
1127 return method->attr.ma.res_type[pos] = skip_tid(res);
1130 void set_method_res_type(type *method, int pos, type* tp) {
1131 assert(method && (method->type_op == type_method));
1132 assert(pos >= 0 && pos < get_method_n_ress(method));
1133 /* set the result type */
1134 method->attr.ma.res_type[pos] = tp;
1135 /* If information constructed set pass-by-value representation. */
1136 if (method->attr.ma.value_ress) {
1137 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1138 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1142 /* Returns an entity that represents the copied value result. Only necessary
1143 for compounds passed by value. */
1144 entity *get_method_value_res_ent(type *method, int pos) {
1145 assert(method && (method->type_op == type_method));
1146 assert(pos >= 0 && pos < get_method_n_ress(method));
1147 if (!method->attr.ma.value_ress)
1148 method->attr.ma.value_ress
1149 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1150 get_method_n_ress(method), method->attr.ma.res_type);
1151 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1152 && "result type not yet set");
1153 return get_struct_member(method->attr.ma.value_ress, pos);
1157 * Returns a type that represents the copied value results.
1159 type *get_method_value_res_type(const type *method) {
1160 assert(method && (method->type_op == type_method));
1161 return method->attr.ma.value_ress;
1164 /* Returns the null-terminated name of this variadicity. */
1165 const char *get_variadicity_name(variadicity vari)
1167 #define X(a) case a: return #a
1169 X(variadicity_non_variadic);
1170 X(variadicity_variadic);
1177 variadicity get_method_variadicity(const type *method)
1179 assert(method && (method->type_op == type_method));
1180 return method->attr.ma.variadicity;
1183 void set_method_variadicity(type *method, variadicity vari)
1185 assert(method && (method->type_op == type_method));
1186 method->attr.ma.variadicity = vari;
1190 * Returns the first variadic parameter index of a type.
1191 * If this index was NOT set, the index of the last parameter
1192 * of the method type plus one is returned for variadic functions.
1193 * Non-variadic function types always return -1 here.
1195 int get_method_first_variadic_param_index(const type *method)
1197 assert(method && (method->type_op == type_method));
1199 if (method->attr.ma.variadicity == variadicity_non_variadic)
1202 if (method->attr.ma.first_variadic_param == -1)
1203 return get_method_n_params(method);
1204 return method->attr.ma.first_variadic_param;
1208 * Sets the first variadic parameter index. This allows to specify
1209 * a complete call type (containing the type of all parameters)
1210 * but still have the knowledge, which parameter must be passed as
1213 void set_method_first_variadic_param_index(type *method, int index)
1215 assert(method && (method->type_op == type_method));
1216 assert(index >= 0 && index <= get_method_n_params(method));
1218 method->attr.ma.first_variadic_param = index;
1222 int (is_method_type)(const type *method) {
1223 return __is_method_type(method);
1226 /*-----------------------------------------------------------------*/
1228 /*-----------------------------------------------------------------*/
1230 /* create a new type uni */
1231 type *new_type_union (ident *name) {
1233 res = new_type(type_union, NULL, name);
1234 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
1235 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
1236 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1239 type *new_d_type_union (ident *name, dbg_info* db) {
1240 type *res = new_type_union (name);
1241 set_type_dbg_info(res, db);
1244 void free_union_entities (type *uni) {
1246 assert(uni && (uni->type_op == type_union));
1247 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1248 free_entity(get_union_member(uni, i));
1250 void free_union_attrs (type *uni) {
1251 assert(uni && (uni->type_op == type_union));
1252 DEL_ARR_F(uni->attr.ua.members);
1254 /* manipulate private fields of union */
1256 int get_union_n_types (type *uni) {
1257 assert(uni && (uni->type_op == type_union));
1258 return uni->attr.ua.n_types;
1260 type *get_union_unioned_type (type *uni, int pos) {
1261 assert(uni && (uni->type_op == type_union));
1262 assert(pos >= 0 && pos < get_union_n_types(uni));
1263 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1265 void set_union_unioned_type (type *uni, int pos, type *tp) {
1266 assert(uni && (uni->type_op == type_union));
1267 assert(pos >= 0 && pos < get_union_n_types(uni));
1268 uni->attr.ua.unioned_type[pos] = tp;
1270 ident *get_union_delim_nameid (type *uni, int pos) {
1271 assert(uni && (uni->type_op == type_union));
1272 assert(pos >= 0 && pos < get_union_n_types(uni));
1273 return uni->attr.ua.delim_names[pos];
1275 const char *get_union_delim_name (type *uni, int pos) {
1276 assert(uni && (uni->type_op == type_union));
1277 assert(pos >= 0 && pos < get_union_n_types(uni));
1278 return get_id_str(uni->attr.ua.delim_names[pos]);
1280 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1281 assert(uni && (uni->type_op == type_union));
1282 assert(pos >= 0 && pos < get_union_n_types(uni));
1283 uni->attr.ua.delim_names[pos] = id;
1286 int get_union_n_members (const type *uni) {
1287 assert(uni && (uni->type_op == type_union));
1288 return (ARR_LEN (uni->attr.ua.members));
1290 void add_union_member (type *uni, entity *member) {
1291 assert(uni && (uni->type_op == type_union));
1292 ARR_APP1 (entity *, uni->attr.ua.members, member);
1294 entity *get_union_member (const type *uni, int pos) {
1295 assert(uni && (uni->type_op == type_union));
1296 assert(pos >= 0 && pos < get_union_n_members(uni));
1297 return uni->attr.ua.members[pos];
1299 void set_union_member (type *uni, int pos, entity *member) {
1300 assert(uni && (uni->type_op == type_union));
1301 assert(pos >= 0 && pos < get_union_n_members(uni));
1302 uni->attr.ua.members[pos] = member;
1304 void remove_union_member(type *uni, entity *member) {
1306 assert(uni && (uni->type_op == type_union));
1307 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1308 if (uni->attr.ua.members[i] == member) {
1309 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1310 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1311 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1317 int (is_union_type)(const type *uni) {
1318 return __is_union_type(uni);
1321 /*-----------------------------------------------------------------*/
1323 /*-----------------------------------------------------------------*/
1326 /* create a new type array -- set dimension sizes independently */
1327 type *new_type_array (ident *name, int n_dimensions,
1328 type *element_type) {
1331 ir_graph *rem = current_ir_graph;
1332 assert(!is_method_type(element_type));
1334 res = new_type(type_array, NULL, name);
1335 res->attr.aa.n_dimensions = n_dimensions;
1336 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1337 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1338 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1340 current_ir_graph = get_const_code_irg();
1341 for (i = 0; i < n_dimensions; i++) {
1342 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1343 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1344 res->attr.aa.order[i] = i;
1346 current_ir_graph = rem;
1348 res->attr.aa.element_type = element_type;
1349 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1354 type *new_d_type_array (ident *name, int n_dimensions,
1355 type *element_type, dbg_info* db) {
1356 type *res = new_type_array (name, n_dimensions, element_type);
1357 set_type_dbg_info(res, db);
1361 void free_array_entities (type *array) {
1362 assert(array && (array->type_op == type_array));
1365 void free_array_attrs (type *array) {
1366 assert(array && (array->type_op == type_array));
1367 free(array->attr.aa.lower_bound);
1368 free(array->attr.aa.upper_bound);
1371 /* manipulate private fields of array type */
1372 int get_array_n_dimensions (const type *array) {
1373 assert(array && (array->type_op == type_array));
1374 return array->attr.aa.n_dimensions;
1378 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1379 ir_node * upper_bound) {
1380 assert(array && (array->type_op == type_array));
1381 assert(lower_bound && "lower_bound node may not be NULL.");
1382 assert(upper_bound && "upper_bound node may not be NULL.");
1383 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1384 array->attr.aa.lower_bound[dimension] = lower_bound;
1385 array->attr.aa.upper_bound[dimension] = upper_bound;
1388 set_array_bounds_int (type *array, int dimension, int lower_bound,
1390 ir_graph *rem = current_ir_graph;
1391 current_ir_graph = get_const_code_irg();
1392 set_array_bounds (array, dimension,
1393 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1394 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1395 current_ir_graph = rem;
1398 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1399 assert(array && (array->type_op == type_array));
1400 assert(lower_bound && "lower_bound node may not be NULL.");
1401 array->attr.aa.lower_bound[dimension] = lower_bound;
1403 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1404 ir_graph *rem = current_ir_graph;
1405 current_ir_graph = get_const_code_irg();
1406 set_array_lower_bound (array, dimension,
1407 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1408 current_ir_graph = rem;
1411 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1412 assert(array && (array->type_op == type_array));
1413 assert(upper_bound && "upper_bound node may not be NULL.");
1414 array->attr.aa.upper_bound[dimension] = upper_bound;
1416 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1417 ir_graph *rem = current_ir_graph;
1418 current_ir_graph = get_const_code_irg();
1419 set_array_upper_bound (array, dimension,
1420 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1421 current_ir_graph = rem;
1423 int has_array_lower_bound (const type *array, int dimension) {
1424 assert(array && (array->type_op == type_array));
1425 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1427 ir_node *get_array_lower_bound (const type *array, int dimension) {
1428 assert(array && (array->type_op == type_array));
1429 return array->attr.aa.lower_bound[dimension];
1431 long get_array_lower_bound_int (const type *array, int dimension) {
1433 assert(array && (array->type_op == type_array));
1434 node = array->attr.aa.lower_bound[dimension];
1435 assert(get_irn_op(node) == op_Const);
1436 return get_tarval_long(get_Const_tarval(node));
1438 int has_array_upper_bound (const type *array, int dimension) {
1439 assert(array && (array->type_op == type_array));
1440 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1442 ir_node * get_array_upper_bound (const type *array, int dimension) {
1443 assert(array && (array->type_op == type_array));
1444 return array->attr.aa.upper_bound[dimension];
1446 long get_array_upper_bound_int (const type *array, int dimension) {
1448 assert(array && (array->type_op == type_array));
1449 node = array->attr.aa.upper_bound[dimension];
1450 assert(get_irn_op(node) == op_Const);
1451 return get_tarval_long(get_Const_tarval(node));
1454 void set_array_order (type *array, int dimension, int order) {
1455 assert(array && (array->type_op == type_array));
1456 array->attr.aa.order[dimension] = order;
1458 int get_array_order (const type *array, int dimension) {
1459 assert(array && (array->type_op == type_array));
1460 return array->attr.aa.order[dimension];
1463 void set_array_element_type (type *array, type *tp) {
1464 assert(array && (array->type_op == type_array));
1465 assert(!is_method_type(tp));
1466 array->attr.aa.element_type = tp;
1468 type *get_array_element_type (type *array) {
1469 assert(array && (array->type_op == type_array));
1470 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1473 void set_array_element_entity (type *array, entity *ent) {
1474 assert(array && (array->type_op == type_array));
1475 assert((get_entity_type(ent)->type_op != type_method));
1476 array->attr.aa.element_ent = ent;
1477 array->attr.aa.element_type = get_entity_type(ent);
1479 entity *get_array_element_entity (const type *array) {
1480 assert(array && (array->type_op == type_array));
1481 return array->attr.aa.element_ent;
1485 int (is_array_type)(const type *array) {
1486 return __is_array_type(array);
1489 /*-----------------------------------------------------------------*/
1490 /* TYPE_ENUMERATION */
1491 /*-----------------------------------------------------------------*/
1493 /* create a new type enumeration -- set the enumerators independently */
1494 type *new_type_enumeration (ident *name, int n_enums) {
1496 res = new_type(type_enumeration, NULL, name);
1497 res->attr.ea.n_enums = n_enums;
1498 res->attr.ea.enumer = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
1499 res->attr.ea.enum_nameid = (ident **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1500 memset(res->attr.ea.enumer, 0, sizeof(res->attr.ea.enumer[0]) * n_enums);
1501 memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1504 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1505 type *res = new_type_enumeration (name, n_enums);
1506 set_type_dbg_info(res, db);
1510 void free_enumeration_entities(type *enumeration) {
1511 assert(enumeration && (enumeration->type_op == type_enumeration));
1513 void free_enumeration_attrs(type *enumeration) {
1514 assert(enumeration && (enumeration->type_op == type_enumeration));
1515 free(enumeration->attr.ea.enumer);
1516 free(enumeration->attr.ea.enum_nameid);
1519 /* manipulate fields of enumeration type. */
1520 int get_enumeration_n_enums (const type *enumeration) {
1521 assert(enumeration && (enumeration->type_op == type_enumeration));
1522 return enumeration->attr.ea.n_enums;
1524 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1525 assert(enumeration && (enumeration->type_op == type_enumeration));
1526 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1527 enumeration->attr.ea.enumer[pos] = con;
1529 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1530 assert(enumeration && (enumeration->type_op == type_enumeration));
1531 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1532 return enumeration->attr.ea.enumer[pos];
1534 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1535 assert(enumeration && (enumeration->type_op == type_enumeration));
1536 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1537 enumeration->attr.ea.enum_nameid[pos] = id;
1539 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1540 assert(enumeration && (enumeration->type_op == type_enumeration));
1541 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1542 return enumeration->attr.ea.enum_nameid[pos];
1544 const char *get_enumeration_name(const type *enumeration, int pos) {
1545 assert(enumeration && (enumeration->type_op == type_enumeration));
1546 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1547 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1551 int (is_enumeration_type)(const type *enumeration) {
1552 return __is_enumeration_type(enumeration);
1555 /*-----------------------------------------------------------------*/
1557 /*-----------------------------------------------------------------*/
1559 /* Create a new type pointer */
1560 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1562 assert(mode_is_reference(ptr_mode));
1563 res = new_type(type_pointer, ptr_mode, name);
1564 res->attr.pa.points_to = points_to;
1565 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1566 res->size = get_mode_size_bits(res->mode);
1567 res->state = layout_fixed;
1570 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1571 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1572 set_type_dbg_info(res, db);
1575 void free_pointer_entities (type *pointer) {
1576 assert(pointer && (pointer->type_op == type_pointer));
1578 void free_pointer_attrs (type *pointer) {
1579 assert(pointer && (pointer->type_op == type_pointer));
1581 /* manipulate fields of type_pointer */
1582 void set_pointer_points_to_type (type *pointer, type *tp) {
1583 assert(pointer && (pointer->type_op == type_pointer));
1584 pointer->attr.pa.points_to = tp;
1586 type *get_pointer_points_to_type (type *pointer) {
1587 assert(pointer && (pointer->type_op == type_pointer));
1588 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1592 int (is_pointer_type)(const type *pointer) {
1593 return __is_pointer_type(pointer);
1596 /* Returns the first pointer type that has as points_to tp.
1597 * Not efficient: O(#types).
1598 * If not found returns unknown_type. */
1599 type *find_pointer_type_to_type (type *tp) {
1601 for (i = 0; i < get_irp_n_types(); ++i) {
1602 type *found = get_irp_type(i);
1603 if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1606 return unknown_type;
1611 /*-----------------------------------------------------------------*/
1612 /* TYPE_PRIMITIVE */
1613 /*-----------------------------------------------------------------*/
1615 /* create a new type primitive */
1616 type *new_type_primitive (ident *name, ir_mode *mode) {
1618 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1619 res = new_type(type_primitive, mode, name);
1620 res->size = get_mode_size_bits(mode);
1621 res->state = layout_fixed;
1624 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1625 type *res = new_type_primitive (name, mode);
1626 set_type_dbg_info(res, db);
1629 void free_primitive_entities (type *primitive) {
1630 assert(primitive && (primitive->type_op == type_primitive));
1632 void free_primitive_attrs (type *primitive) {
1633 assert(primitive && (primitive->type_op == type_primitive));
1637 int (is_primitive_type)(const type *primitive) {
1638 return __is_primitive_type(primitive);
1641 /*-----------------------------------------------------------------*/
1642 /* common functionality */
1643 /*-----------------------------------------------------------------*/
1646 int (is_atomic_type)(const type *tp) {
1647 return __is_atomic_type(tp);
1651 * Gets the number of elements in a firm compound type.
1653 int get_compound_n_members(const type *tp)
1657 if (is_struct_type(tp))
1658 res = get_struct_n_members(tp);
1659 else if (is_class_type(tp))
1660 res = get_class_n_members(tp);
1661 else if (is_union_type(tp))
1662 res = get_union_n_members(tp);
1664 assert(0 && "need struct, union or class for member count");
1670 * Gets the member of a firm compound type at position pos.
1672 entity *get_compound_member(const type *tp, int pos)
1676 if (is_struct_type(tp))
1677 res = get_struct_member(tp, pos);
1678 else if (is_class_type(tp))
1679 res = get_class_member(tp, pos);
1680 else if (is_union_type(tp))
1681 res = get_union_member(tp, pos);
1684 assert(0 && "need struct, union or class to get a member");
1692 int is_compound_type(const type *tp) {
1693 assert(tp && tp->kind == k_type);
1694 return (is_class_type(tp) || is_struct_type(tp) ||
1695 is_array_type(tp) || is_union_type(tp));