3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * file type.c - implementation of the datastructure to hold
17 * (C) 2001 by Universitaet Karlsruhe
20 * This module supplies a datastructure to represent all types
21 * known in the compiled program. This includes types specified
22 * in the program as well as types defined by the language. In the
23 * view of the intermediate representation there is no difference
24 * between these types.
26 * There exist several kinds of types, arranged by the structure of
27 * the type. A type is described by a set of attributes. Some of
28 * these attributes are common to all types, others depend on the
31 * Types are different from the modes defined in irmode: Types are
32 * on the level of the programming language, modes at the level of
33 * the target processor.
35 * @see type_t.h type tpop
58 # include "irprog_t.h"
61 # include "typegmod.h"
67 /*******************************************************************/
69 /*******************************************************************/
71 type *none_type; type *get_none_type(void) { return none_type; }
72 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
76 /** Returns a new, unique number to number nodes or the like. */
77 int get_irp_new_node_nr(void);
80 /* Suffixes added to types used for pass-by-value representations. */
81 static ident *value_params_suffix = NULL;
82 static ident *value_ress_suffix = NULL;
84 void init_type(void) {
85 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
86 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
88 /* construct none and unknown type. */
89 none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
90 set_type_size_bits(none_type, 0);
91 set_type_state (none_type, layout_fixed);
92 remove_irp_type(none_type);
93 unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
94 set_type_size_bits(unknown_type, 0);
95 set_type_state (unknown_type, layout_fixed);
96 remove_irp_type(unknown_type);
99 unsigned long type_visited;
101 void (set_master_type_visited)(unsigned long val) { __set_master_type_visited(val); }
102 unsigned long (get_master_type_visited)(void) { return __get_master_type_visited(); }
103 void (inc_master_type_visited)(void) { __inc_master_type_visited(); }
107 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
111 assert(type_op != type_id);
112 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
114 node_size = offsetof(type, attr) + type_op->attr_size;
115 res = (type *) xmalloc (node_size);
116 memset((void *)res, 0, node_size);
117 add_irp_type(res); /* Remember the new type global. */
120 res->type_op = type_op;
123 res->state = layout_undefined;
129 res->nr = get_irp_new_node_nr();
130 res->c_name = (char*) get_id_str (name);
131 #endif /* defined DEBUG_libfirm */
136 void free_type(type *tp) {
137 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
139 /* Remove from list of all types */
141 /* Free the attributes of the type. */
143 /* Free entities automatically allocated with the type */
144 if (is_array_type(tp))
145 free_entity(get_array_element_entity(tp));
146 /* And now the type itself... */
151 void free_type_entities(type *tp) {
152 switch(get_type_tpop_code(tp)) {
153 case tpo_class: { free_class_entities(tp); } break;
154 case tpo_struct: { free_struct_entities(tp); } break;
155 case tpo_method: { free_method_entities(tp); } break;
156 case tpo_union: { free_union_entities(tp); } break;
157 case tpo_array: { free_array_entities(tp); } break;
158 case tpo_enumeration: { free_enumeration_entities(tp); } break;
159 case tpo_pointer: { free_pointer_entities(tp); } break;
160 case tpo_primitive: { free_primitive_entities(tp); } break;
165 void free_type_attrs(type *tp) {
166 switch(get_type_tpop_code(tp)) {
167 case tpo_class: { free_class_attrs(tp); } break;
168 case tpo_struct: { free_struct_attrs(tp); } break;
169 case tpo_method: { free_method_attrs(tp); } break;
170 case tpo_union: { free_union_attrs(tp); } break;
171 case tpo_array: { free_array_attrs(tp); } break;
172 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
173 case tpo_pointer: { free_pointer_attrs(tp); } break;
174 case tpo_primitive: { free_primitive_attrs(tp); } break;
179 /* set/get the link field */
180 void *(get_type_link)(const type *tp)
182 return __get_type_link(tp);
185 void (set_type_link)(type *tp, void *l)
187 __set_type_link(tp, l);
190 tp_op *(get_type_tpop)(const type *tp) {
191 return __get_type_tpop(tp);
194 ident *(get_type_tpop_nameid)(const type *tp) {
195 return __get_type_tpop_nameid(tp);
198 const char* get_type_tpop_name(const type *tp) {
199 assert(tp && tp->kind == k_type);
200 return get_id_str(tp->type_op->name);
203 tp_opcode (get_type_tpop_code)(const type *tp) {
204 return __get_type_tpop_code(tp);
207 ir_mode *(get_type_mode)(const type *tp) {
208 return __get_type_mode(tp);
211 void set_type_mode(type *tp, ir_mode* m) {
212 assert(tp && tp->kind == k_type);
214 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
215 /* Modes of primitives must be data */
216 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
217 /* Modes of enumerations must be integers */
218 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
219 /* Modes of pointers must be references. */
221 switch (get_type_tpop_code(tp)) {
223 /* For primitive size depends on the mode. */
224 tp->size = get_mode_size_bits(m);
227 case tpo_enumeration:
229 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
230 assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
231 tp->size = get_mode_size_bits(m);
236 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
237 assert(get_type_state(tp) == layout_fixed &&
238 tp->size == get_mode_size_bits(m) &&
239 "mode don't match struct/class layout");
243 assert(0 && "setting a mode is NOT allowed for this type");
247 ident *(get_type_ident)(const type *tp) {
248 return __get_type_ident(tp);
251 void (set_type_ident)(type *tp, ident* id) {
252 __set_type_ident(tp, id);
255 /* Outputs a unique number for this node */
256 long (get_type_nr)(const type *tp) {
257 return __get_type_nr(tp);
260 const char* get_type_name(const type *tp) {
261 assert(tp && tp->kind == k_type);
262 return (get_id_str(tp->name));
265 int (get_type_size_bytes)(const type *tp) {
266 return __get_type_size_bytes(tp);
269 int (get_type_size_bits)(const type *tp) {
270 return __get_type_size_bits(tp);
274 set_type_size_bits(type *tp, int size) {
275 assert(tp && tp->kind == k_type);
276 /* For pointer enumeration and primitive size depends on the mode.
277 Methods don't have a size. */
278 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
279 (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
280 if (tp->type_op == type_primitive)
283 /* argh: we must allow to set negative values as "invalid size" */
284 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
285 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
291 set_type_size_bytes(type *tp, int size) {
292 set_type_size_bits(tp, 8*size);
295 int get_type_alignment_bytes(type *tp) {
296 int align = get_type_alignment_bits(tp);
298 return align < 0 ? align : (align + 7) >> 3;
301 int get_type_alignment_bits(type *tp) {
307 /* alignment NOT set calculate it "on demand" */
309 align = get_mode_size_bits(tp->mode);
310 else if (is_array_type(tp))
311 align = get_type_alignment_bits(get_array_element_type(tp));
312 else if (is_compound_type(tp)) {
313 int i, n = get_compound_n_members(tp);
316 for (i = 0; i < n; ++i) {
317 type *t = get_entity_type(get_compound_member(tp, i));
318 int a = get_type_alignment_bits(t);
324 else if (is_method_type(tp))
334 set_type_alignment_bits(type *tp, int align) {
335 assert(tp && tp->kind == k_type);
336 /* Methods don't have an alignment. */
337 if (tp->type_op != type_method) {
343 set_type_alignment_bytes(type *tp, int align) {
344 set_type_size_bits(tp, 8*align);
347 type_state (get_type_state)(const type *tp) {
348 return __get_type_state(tp);
352 set_type_state(type *tp, type_state state) {
353 assert(tp && tp->kind == k_type);
355 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
356 (tp->type_op == type_method))
359 /* Just a correctness check: */
360 if (state == layout_fixed) {
362 switch (get_type_tpop_code(tp)) {
365 assert(get_type_size_bits(tp) > -1);
366 if (tp != get_glob_type()) {
367 int n_mem = get_class_n_members(tp);
368 for (i = 0; i < n_mem; i++) {
369 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
370 { DDMT(tp); DDME(get_class_member(tp, i)); }
371 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
373 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
374 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
381 assert(get_type_size_bits(tp) > -1);
382 for (i = 0; i < get_struct_n_members(tp); i++) {
383 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
384 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
393 Assure that only innermost dimension is dynamic? */
395 case tpo_enumeration:
397 assert(get_type_mode != NULL);
398 for (i = 0; i < get_enumeration_n_enums(tp); i++)
399 assert(get_enumeration_enum(tp, i) != NULL);
407 unsigned long (get_type_visited)(const type *tp) {
408 return __get_type_visited(tp);
411 void (set_type_visited)(type *tp, unsigned long num) {
412 __set_type_visited(tp, num);
415 /* Sets visited field in type to type_visited. */
416 void (mark_type_visited)(type *tp) {
417 __mark_type_visited(tp);
420 /* @@@ name clash with master flag
421 int (type_visited)(const type *tp) {
422 return __type_visited(tp);
425 int (type_not_visited)(const type *tp) {
426 return __type_not_visited(tp);
429 int (is_type)(const void *thing) {
430 return __is_type(thing);
433 bool equal_type(type *typ1, type *typ2) {
438 if (typ1 == typ2) return true;
440 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
441 (get_type_ident(typ1) != get_type_ident(typ2)) ||
442 (get_type_mode(typ1) != get_type_mode(typ2)) ||
443 (get_type_state(typ1) != get_type_state(typ2)))
445 if ((get_type_state(typ1) == layout_fixed) &&
446 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
449 switch(get_type_tpop_code(typ1)) {
451 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
452 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
453 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
454 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
455 /** Compare the members **/
456 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
457 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
458 /* First sort the members of typ2 */
459 for (i = 0; i < get_class_n_members(typ1); i++) {
460 entity *e1 = get_class_member(typ1, i);
461 for (j = 0; j < get_class_n_members(typ2); j++) {
462 entity *e2 = get_class_member(typ2, j);
463 if (get_entity_name(e1) == get_entity_name(e2))
467 for (i = 0; i < get_class_n_members(typ1); i++) {
468 if (!m[i] || /* Found no counterpart */
469 !equal_entity(get_class_member(typ1, i), m[i]))
472 /** Compare the supertypes **/
473 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
474 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
475 /* First sort the supertypes of typ2 */
476 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
477 type *t1 = get_class_supertype(typ1, i);
478 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
479 type *t2 = get_class_supertype(typ2, j);
480 if (get_type_ident(t2) == get_type_ident(t1))
484 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
485 if (!t[i] || /* Found no counterpart */
486 get_class_supertype(typ1, i) != t[i])
491 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
492 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
493 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
494 /* First sort the members of lt */
495 for (i = 0; i < get_struct_n_members(typ1); i++) {
496 entity *e1 = get_struct_member(typ1, i);
497 for (j = 0; j < get_struct_n_members(typ2); j++) {
498 entity *e2 = get_struct_member(typ2, j);
499 if (get_entity_name(e1) == get_entity_name(e2))
503 for (i = 0; i < get_struct_n_members(typ1); i++) {
504 if (!m[i] || /* Found no counterpart */
505 !equal_entity(get_struct_member(typ1, i), m[i]))
510 int n_param1, n_param2;
512 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
513 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
515 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
516 n_param1 = get_method_n_params(typ1);
517 n_param2 = get_method_n_params(typ2);
520 n_param1 = get_method_first_variadic_param_index(typ1);
521 n_param2 = get_method_first_variadic_param_index(typ2);
524 if (n_param1 != n_param2) return false;
526 for (i = 0; i < n_param1; i++) {
527 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
530 for (i = 0; i < get_method_n_ress(typ1); i++) {
531 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
536 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
537 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
538 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
539 /* First sort the members of lt */
540 for (i = 0; i < get_union_n_members(typ1); i++) {
541 entity *e1 = get_union_member(typ1, i);
542 for (j = 0; j < get_union_n_members(typ2); j++) {
543 entity *e2 = get_union_member(typ2, j);
544 if (get_entity_name(e1) == get_entity_name(e2))
548 for (i = 0; i < get_union_n_members(typ1); i++) {
549 if (!m[i] || /* Found no counterpart */
550 !equal_entity(get_union_member(typ1, i), m[i]))
555 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
557 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
559 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
560 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
561 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
563 if (get_array_order(typ1, i) != get_array_order(typ2, i))
564 assert(0 && "type compare with different dimension orders not implemented");
567 case tpo_enumeration: {
568 assert(0 && "enumerations not implemented");
571 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
574 case tpo_primitive: {
581 bool smaller_type (type *st, type *lt) {
585 if (st == lt) return true;
587 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
590 switch(get_type_tpop_code(st)) {
592 return is_subclass_of(st, lt);
595 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
596 m = alloca(sizeof(entity *) * get_struct_n_members(st));
597 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
598 /* First sort the members of lt */
599 for (i = 0; i < get_struct_n_members(st); i++) {
600 entity *se = get_struct_member(st, i);
601 for (j = 0; j < get_struct_n_members(lt); j++) {
602 entity *le = get_struct_member(lt, j);
603 if (get_entity_name(le) == get_entity_name(se))
607 for (i = 0; i < get_struct_n_members(st); i++) {
608 if (!m[i] || /* Found no counterpart */
609 !smaller_type(get_entity_type(get_struct_member(st, i)),
610 get_entity_type(m[i])))
615 /** FIXME: is this still true? */
616 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
617 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
618 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
619 for (i = 0; i < get_method_n_params(st); i++) {
620 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
623 for (i = 0; i < get_method_n_ress(st); i++) {
624 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
629 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
630 m = alloca(sizeof(entity *) * get_union_n_members(st));
631 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
632 /* First sort the members of lt */
633 for (i = 0; i < get_union_n_members(st); i++) {
634 entity *se = get_union_member(st, i);
635 for (j = 0; j < get_union_n_members(lt); j++) {
636 entity *le = get_union_member(lt, j);
637 if (get_entity_name(le) == get_entity_name(se))
641 for (i = 0; i < get_union_n_members(st); i++) {
642 if (!m[i] || /* Found no counterpart */
643 !smaller_type(get_entity_type(get_union_member(st, i)),
644 get_entity_type(m[i])))
649 type *set, *let; /* small/large elt. type */
650 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
652 set = get_array_element_type(st);
653 let = get_array_element_type(lt);
655 /* If the elt types are different, set must be convertible
656 to let, and they must have the same size so that address
657 computations work out. To have a size the layout must
659 if ((get_type_state(set) != layout_fixed) ||
660 (get_type_state(let) != layout_fixed))
662 if (!smaller_type(set, let) ||
663 get_type_size_bits(set) != get_type_size_bits(let))
666 for(i = 0; i < get_array_n_dimensions(st); i++) {
667 if (get_array_lower_bound(lt, i))
668 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
670 if (get_array_upper_bound(lt, i))
671 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
675 case tpo_enumeration: {
676 assert(0 && "enumerations not implemented");
679 if (!smaller_type(get_pointer_points_to_type(st),
680 get_pointer_points_to_type(lt)))
683 case tpo_primitive: {
684 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
692 /*-----------------------------------------------------------------*/
694 /*-----------------------------------------------------------------*/
696 /* create a new class type */
697 type *new_type_class (ident *name) {
700 res = new_type(type_class, NULL, name);
702 res->attr.ca.members = NEW_ARR_F (entity *, 0);
703 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
704 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
705 res->attr.ca.peculiarity = peculiarity_existent;
706 res->attr.ca.dfn = 0;
710 type *new_d_type_class (ident *name, dbg_info* db) {
711 type *res = new_type_class (name);
712 set_type_dbg_info(res, db);
716 void free_class_entities(type *clss) {
718 assert(clss && (clss->type_op == type_class));
719 for (i = get_class_n_members(clss)-1; i >= 0; --i)
720 free_entity(get_class_member(clss, i));
723 void free_class_attrs(type *clss) {
724 assert(clss && (clss->type_op == type_class));
725 DEL_ARR_F(clss->attr.ca.members);
726 DEL_ARR_F(clss->attr.ca.subtypes);
727 DEL_ARR_F(clss->attr.ca.supertypes);
730 /* manipulate private fields of class type */
731 void add_class_member (type *clss, entity *member) {
732 assert(clss && (clss->type_op == type_class));
733 ARR_APP1 (entity *, clss->attr.ca.members, member);
736 int (get_class_n_members) (const type *clss) {
737 return __get_class_n_members(clss);
740 int get_class_member_index(type *clss, entity *mem) {
742 assert(clss && (clss->type_op == type_class));
743 for (i = 0; i < get_class_n_members(clss); i++)
744 if (get_class_member(clss, i) == mem)
749 entity *(get_class_member) (const type *clss, int pos) {
750 return __get_class_member(clss, pos);
753 entity *get_class_member_by_name(type *clss, ident *name) {
755 assert(clss && (clss->type_op == type_class));
756 n_mem = get_class_n_members(clss);
757 for (i = 0; i < n_mem; ++i) {
758 entity *mem = get_class_member(clss, i);
759 if (get_entity_ident(mem) == name) return mem;
764 void set_class_member (type *clss, entity *member, int pos) {
765 assert(clss && (clss->type_op == type_class));
766 assert(pos >= 0 && pos < get_class_n_members(clss));
767 clss->attr.ca.members[pos] = member;
769 void set_class_members (type *clss, entity **members, int arity) {
771 assert(clss && (clss->type_op == type_class));
772 DEL_ARR_F(clss->attr.ca.members);
773 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
774 for (i = 0; i < arity; i++) {
775 set_entity_owner(members[i], clss);
776 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
779 void remove_class_member(type *clss, entity *member) {
781 assert(clss && (clss->type_op == type_class));
782 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
783 if (clss->attr.ca.members[i] == member) {
784 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
785 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
786 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
792 void add_class_subtype (type *clss, type *subtype) {
794 assert(clss && (clss->type_op == type_class));
795 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
796 for (i = 0; i < get_class_n_supertypes(subtype); i++)
797 if (get_class_supertype(subtype, i) == clss)
798 /* Class already registered */
800 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
802 int get_class_n_subtypes (const type *clss) {
803 assert(clss && (clss->type_op == type_class));
804 return (ARR_LEN (clss->attr.ca.subtypes));
806 type *get_class_subtype (type *clss, int pos) {
807 assert(clss && (clss->type_op == type_class));
808 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
809 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
811 void set_class_subtype (type *clss, type *subtype, int pos) {
812 assert(clss && (clss->type_op == type_class));
813 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
814 clss->attr.ca.subtypes[pos] = subtype;
816 void remove_class_subtype(type *clss, type *subtype) {
818 assert(clss && (clss->type_op == type_class));
819 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
820 if (clss->attr.ca.subtypes[i] == subtype) {
821 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
822 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
823 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
828 void add_class_supertype (type *clss, type *supertype) {
830 assert(clss && (clss->type_op == type_class));
831 assert(supertype && (supertype -> type_op == type_class));
832 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
833 for (i = 0; i < get_class_n_subtypes(supertype); i++)
834 if (get_class_subtype(supertype, i) == clss)
835 /* Class already registered */
837 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
839 int get_class_n_supertypes (const type *clss) {
840 assert(clss && (clss->type_op == type_class));
841 return (ARR_LEN (clss->attr.ca.supertypes));
843 int get_class_supertype_index(type *clss, type *super_clss) {
845 assert(clss && (clss->type_op == type_class));
846 assert(super_clss && (super_clss->type_op == type_class));
847 for (i = 0; i < get_class_n_supertypes(clss); i++)
848 if (get_class_supertype(clss, i) == super_clss)
852 type *get_class_supertype (type *clss, int pos) {
853 assert(clss && (clss->type_op == type_class));
854 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
855 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
857 void set_class_supertype (type *clss, type *supertype, int pos) {
858 assert(clss && (clss->type_op == type_class));
859 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
860 clss->attr.ca.supertypes[pos] = supertype;
862 void remove_class_supertype(type *clss, type *supertype) {
864 assert(clss && (clss->type_op == type_class));
865 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
866 if (clss->attr.ca.supertypes[i] == supertype) {
867 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
868 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
869 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
874 const char *get_peculiarity_string(peculiarity p) {
876 case peculiarity_description:
877 return "peculiarity_description";
878 case peculiarity_inherited:
879 return "peculiarity_inherited";
881 return "peculiarity_existent";
885 peculiarity get_class_peculiarity (const type *clss) {
886 assert(clss && (clss->type_op == type_class));
887 return clss->attr.ca.peculiarity;
890 void set_class_peculiarity (type *clss, peculiarity pec) {
891 assert(clss && (clss->type_op == type_class));
892 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
893 clss->attr.ca.peculiarity = pec;
896 void set_class_dfn (type *clss, int dfn)
898 clss->attr.ca.dfn = dfn;
901 int get_class_dfn (const type *clss)
903 return (clss->attr.ca.dfn);
907 int (is_class_type)(const type *clss) {
908 return __is_class_type(clss);
911 bool is_subclass_of(type *low, type *high) {
913 assert(is_class_type(low) && is_class_type(high));
914 if (low == high) return true;
915 /* depth first search from high downwards. */
916 for (i = 0; i < get_class_n_subtypes(high); i++) {
917 if (low == get_class_subtype(high, i))
919 if (is_subclass_of(low, get_class_subtype(high, i)))
925 /*----------------------------------------------------------------**/
927 /*----------------------------------------------------------------**/
929 /* create a new type struct */
930 type *new_type_struct (ident *name) {
932 res = new_type(type_struct, NULL, name);
933 res->attr.sa.members = NEW_ARR_F (entity *, 0);
936 type *new_d_type_struct (ident *name, dbg_info* db) {
937 type *res = new_type_struct (name);
938 set_type_dbg_info(res, db);
941 void free_struct_entities (type *strct) {
943 assert(strct && (strct->type_op == type_struct));
944 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
945 free_entity(get_struct_member(strct, i));
947 void free_struct_attrs (type *strct) {
948 assert(strct && (strct->type_op == type_struct));
949 DEL_ARR_F(strct->attr.sa.members);
952 /* manipulate private fields of struct */
953 int get_struct_n_members (const type *strct) {
954 assert(strct && (strct->type_op == type_struct));
955 return (ARR_LEN (strct->attr.sa.members));
958 void add_struct_member (type *strct, entity *member) {
959 assert(strct && (strct->type_op == type_struct));
960 assert(get_type_tpop(get_entity_type(member)) != type_method);
961 /* @@@ lowerfirm geht nicht durch */
962 ARR_APP1 (entity *, strct->attr.sa.members, member);
965 entity *get_struct_member (const type *strct, int pos) {
966 assert(strct && (strct->type_op == type_struct));
967 assert(pos >= 0 && pos < get_struct_n_members(strct));
968 return strct->attr.sa.members[pos];
971 int get_struct_member_index(type *strct, entity *mem) {
973 assert(strct && (strct->type_op == type_struct));
974 for (i = 0; i < get_struct_n_members(strct); i++)
975 if (get_struct_member(strct, i) == mem)
980 void set_struct_member (type *strct, int pos, entity *member) {
981 assert(strct && (strct->type_op == type_struct));
982 assert(pos >= 0 && pos < get_struct_n_members(strct));
983 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
984 strct->attr.sa.members[pos] = member;
986 void remove_struct_member(type *strct, entity *member) {
988 assert(strct && (strct->type_op == type_struct));
989 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
990 if (strct->attr.sa.members[i] == member) {
991 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
992 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
993 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
999 int (is_struct_type)(const type *strct) {
1000 return __is_struct_type(strct);
1003 /*******************************************************************/
1005 /*******************************************************************/
1008 * Lazy construction of value argument / result representation.
1009 * Constructs a struct type and its member. The types of the members
1010 * are passed in the argument list.
1012 * @param name name of the type constructed
1013 * @param len number of fields
1014 * @param tps array of field types with length len
1016 static INLINE type *
1017 build_value_type(ident *name, int len, type **tps) {
1019 type *res = new_type_struct(name);
1020 /* Remove type from type list. Must be treated differently than other types. */
1021 remove_irp_type_from_list(res);
1022 for (i = 0; i < len; i++) {
1023 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
1024 if (tps[i]) elt_type = tps[i];
1025 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1030 /* Create a new method type.
1031 N_param is the number of parameters, n_res the number of results. */
1032 type *new_type_method (ident *name, int n_param, int n_res) {
1035 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
1036 res = new_type(type_method, mode_P_mach, name);
1037 res->state = layout_fixed;
1038 res->size = get_mode_size_bits(mode_P_mach);
1039 res->attr.ma.n_params = n_param;
1040 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
1041 res->attr.ma.value_params = NULL;
1042 res->attr.ma.n_res = n_res;
1043 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
1044 res->attr.ma.value_ress = NULL;
1045 res->attr.ma.variadicity = variadicity_non_variadic;
1046 res->attr.ma.first_variadic_param = -1;
1051 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
1052 type *res = new_type_method (name, n_param, n_res);
1053 set_type_dbg_info(res, db);
1057 void free_method_entities(type *method) {
1058 assert(method && (method->type_op == type_method));
1061 /* Attention: also frees entities in value parameter subtypes! */
1062 void free_method_attrs(type *method) {
1063 assert(method && (method->type_op == type_method));
1064 free(method->attr.ma.param_type);
1065 free(method->attr.ma.res_type);
1066 if (method->attr.ma.value_params) {
1067 free_type_entities(method->attr.ma.value_params);
1068 free_type(method->attr.ma.value_params);
1070 if (method->attr.ma.value_ress) {
1071 free_type_entities(method->attr.ma.value_ress);
1072 free_type(method->attr.ma.value_ress);
1076 /* manipulate private fields of method. */
1077 int get_method_n_params (const type *method) {
1078 assert(method && (method->type_op == type_method));
1079 return method->attr.ma.n_params;
1082 type *get_method_param_type(type *method, int pos) {
1084 assert(method && (method->type_op == type_method));
1085 assert(pos >= 0 && pos < get_method_n_params(method));
1086 res = method->attr.ma.param_type[pos];
1087 assert(res != NULL && "empty method param type");
1088 return method->attr.ma.param_type[pos] = skip_tid(res);
1091 void set_method_param_type(type *method, int pos, type* tp) {
1092 assert(method && (method->type_op == type_method));
1093 assert(pos >= 0 && pos < get_method_n_params(method));
1094 method->attr.ma.param_type[pos] = tp;
1095 /* If information constructed set pass-by-value representation. */
1096 if (method->attr.ma.value_params) {
1097 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1098 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1102 /* Returns an entity that represents the copied value argument. Only necessary
1103 for compounds passed by value. */
1104 entity *get_method_value_param_ent(type *method, int pos) {
1105 assert(method && (method->type_op == type_method));
1106 assert(pos >= 0 && pos < get_method_n_params(method));
1107 if (!method->attr.ma.value_params)
1108 method->attr.ma.value_params
1109 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1110 get_method_n_params(method), method->attr.ma.param_type);
1111 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1112 != method->attr.ma.value_params)
1113 && "param type not yet set");
1114 return get_struct_member(method->attr.ma.value_params, pos);
1118 * Returns a type that represents the copied value arguments.
1120 type *get_method_value_param_type(const type *method)
1122 assert(method && (method->type_op == type_method));
1123 return method->attr.ma.value_params;
1126 int get_method_n_ress (const type *method) {
1127 assert(method && (method->type_op == type_method));
1128 return method->attr.ma.n_res;
1131 type *get_method_res_type(type *method, int pos) {
1133 assert(method && (method->type_op == type_method));
1134 assert(pos >= 0 && pos < get_method_n_ress(method));
1135 res = method->attr.ma.res_type[pos];
1136 assert(res != NULL && "empty method return type");
1137 return method->attr.ma.res_type[pos] = skip_tid(res);
1140 void set_method_res_type(type *method, int pos, type* tp) {
1141 assert(method && (method->type_op == type_method));
1142 assert(pos >= 0 && pos < get_method_n_ress(method));
1143 /* set the result type */
1144 method->attr.ma.res_type[pos] = tp;
1145 /* If information constructed set pass-by-value representation. */
1146 if (method->attr.ma.value_ress) {
1147 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1148 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1152 /* Returns an entity that represents the copied value result. Only necessary
1153 for compounds passed by value. */
1154 entity *get_method_value_res_ent(type *method, int pos) {
1155 assert(method && (method->type_op == type_method));
1156 assert(pos >= 0 && pos < get_method_n_ress(method));
1157 if (!method->attr.ma.value_ress)
1158 method->attr.ma.value_ress
1159 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1160 get_method_n_ress(method), method->attr.ma.res_type);
1161 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1162 && "result type not yet set");
1163 return get_struct_member(method->attr.ma.value_ress, pos);
1167 * Returns a type that represents the copied value results.
1169 type *get_method_value_res_type(const type *method) {
1170 assert(method && (method->type_op == type_method));
1171 return method->attr.ma.value_ress;
1174 /* Returns the null-terminated name of this variadicity. */
1175 const char *get_variadicity_name(variadicity vari)
1177 #define X(a) case a: return #a
1179 X(variadicity_non_variadic);
1180 X(variadicity_variadic);
1187 variadicity get_method_variadicity(const type *method)
1189 assert(method && (method->type_op == type_method));
1190 return method->attr.ma.variadicity;
1193 void set_method_variadicity(type *method, variadicity vari)
1195 assert(method && (method->type_op == type_method));
1196 method->attr.ma.variadicity = vari;
1200 * Returns the first variadic parameter index of a type.
1201 * If this index was NOT set, the index of the last parameter
1202 * of the method type plus one is returned for variadic functions.
1203 * Non-variadic function types always return -1 here.
1205 int get_method_first_variadic_param_index(const type *method)
1207 assert(method && (method->type_op == type_method));
1209 if (method->attr.ma.variadicity == variadicity_non_variadic)
1212 if (method->attr.ma.first_variadic_param == -1)
1213 return get_method_n_params(method);
1214 return method->attr.ma.first_variadic_param;
1218 * Sets the first variadic parameter index. This allows to specify
1219 * a complete call type (containing the type of all parameters)
1220 * but still have the knowledge, which parameter must be passed as
1223 void set_method_first_variadic_param_index(type *method, int index)
1225 assert(method && (method->type_op == type_method));
1226 assert(index >= 0 && index <= get_method_n_params(method));
1228 method->attr.ma.first_variadic_param = index;
1232 int (is_method_type)(const type *method) {
1233 return __is_method_type(method);
1236 /*-----------------------------------------------------------------*/
1238 /*-----------------------------------------------------------------*/
1240 /* create a new type uni */
1241 type *new_type_union (ident *name) {
1243 res = new_type(type_union, NULL, name);
1244 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
1245 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
1246 res->attr.ua.members = NEW_ARR_F (entity *, 0);
1249 type *new_d_type_union (ident *name, dbg_info* db) {
1250 type *res = new_type_union (name);
1251 set_type_dbg_info(res, db);
1254 void free_union_entities (type *uni) {
1256 assert(uni && (uni->type_op == type_union));
1257 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1258 free_entity(get_union_member(uni, i));
1260 void free_union_attrs (type *uni) {
1261 assert(uni && (uni->type_op == type_union));
1262 DEL_ARR_F(uni->attr.ua.members);
1264 /* manipulate private fields of union */
1266 int get_union_n_types (type *uni) {
1267 assert(uni && (uni->type_op == type_union));
1268 return uni->attr.ua.n_types;
1270 type *get_union_unioned_type (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.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1275 void set_union_unioned_type (type *uni, int pos, type *tp) {
1276 assert(uni && (uni->type_op == type_union));
1277 assert(pos >= 0 && pos < get_union_n_types(uni));
1278 uni->attr.ua.unioned_type[pos] = tp;
1280 ident *get_union_delim_nameid (type *uni, int pos) {
1281 assert(uni && (uni->type_op == type_union));
1282 assert(pos >= 0 && pos < get_union_n_types(uni));
1283 return uni->attr.ua.delim_names[pos];
1285 const char *get_union_delim_name (type *uni, int pos) {
1286 assert(uni && (uni->type_op == type_union));
1287 assert(pos >= 0 && pos < get_union_n_types(uni));
1288 return get_id_str(uni->attr.ua.delim_names[pos]);
1290 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1291 assert(uni && (uni->type_op == type_union));
1292 assert(pos >= 0 && pos < get_union_n_types(uni));
1293 uni->attr.ua.delim_names[pos] = id;
1296 int get_union_n_members (const type *uni) {
1297 assert(uni && (uni->type_op == type_union));
1298 return (ARR_LEN (uni->attr.ua.members));
1300 void add_union_member (type *uni, entity *member) {
1301 assert(uni && (uni->type_op == type_union));
1302 ARR_APP1 (entity *, uni->attr.ua.members, member);
1304 entity *get_union_member (const type *uni, int pos) {
1305 assert(uni && (uni->type_op == type_union));
1306 assert(pos >= 0 && pos < get_union_n_members(uni));
1307 return uni->attr.ua.members[pos];
1309 void set_union_member (type *uni, int pos, entity *member) {
1310 assert(uni && (uni->type_op == type_union));
1311 assert(pos >= 0 && pos < get_union_n_members(uni));
1312 uni->attr.ua.members[pos] = member;
1314 void remove_union_member(type *uni, entity *member) {
1316 assert(uni && (uni->type_op == type_union));
1317 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1318 if (uni->attr.ua.members[i] == member) {
1319 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1320 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1321 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1327 int (is_union_type)(const type *uni) {
1328 return __is_union_type(uni);
1331 /*-----------------------------------------------------------------*/
1333 /*-----------------------------------------------------------------*/
1336 /* create a new type array -- set dimension sizes independently */
1337 type *new_type_array (ident *name, int n_dimensions,
1338 type *element_type) {
1341 ir_graph *rem = current_ir_graph;
1342 assert(!is_method_type(element_type));
1344 res = new_type(type_array, NULL, name);
1345 res->attr.aa.n_dimensions = n_dimensions;
1346 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1347 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1348 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1350 current_ir_graph = get_const_code_irg();
1351 for (i = 0; i < n_dimensions; i++) {
1352 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1353 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1354 res->attr.aa.order[i] = i;
1356 current_ir_graph = rem;
1358 res->attr.aa.element_type = element_type;
1359 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1364 type *new_d_type_array (ident *name, int n_dimensions,
1365 type *element_type, dbg_info* db) {
1366 type *res = new_type_array (name, n_dimensions, element_type);
1367 set_type_dbg_info(res, db);
1371 void free_array_entities (type *array) {
1372 assert(array && (array->type_op == type_array));
1375 void free_array_attrs (type *array) {
1376 assert(array && (array->type_op == type_array));
1377 free(array->attr.aa.lower_bound);
1378 free(array->attr.aa.upper_bound);
1381 /* manipulate private fields of array type */
1382 int get_array_n_dimensions (const type *array) {
1383 assert(array && (array->type_op == type_array));
1384 return array->attr.aa.n_dimensions;
1388 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1389 ir_node * upper_bound) {
1390 assert(array && (array->type_op == type_array));
1391 assert(lower_bound && "lower_bound node may not be NULL.");
1392 assert(upper_bound && "upper_bound node may not be NULL.");
1393 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1394 array->attr.aa.lower_bound[dimension] = lower_bound;
1395 array->attr.aa.upper_bound[dimension] = upper_bound;
1398 set_array_bounds_int (type *array, int dimension, int lower_bound,
1400 ir_graph *rem = current_ir_graph;
1401 current_ir_graph = get_const_code_irg();
1402 set_array_bounds (array, dimension,
1403 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1404 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1405 current_ir_graph = rem;
1408 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1409 assert(array && (array->type_op == type_array));
1410 assert(lower_bound && "lower_bound node may not be NULL.");
1411 array->attr.aa.lower_bound[dimension] = lower_bound;
1413 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1414 ir_graph *rem = current_ir_graph;
1415 current_ir_graph = get_const_code_irg();
1416 set_array_lower_bound (array, dimension,
1417 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1418 current_ir_graph = rem;
1421 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1422 assert(array && (array->type_op == type_array));
1423 assert(upper_bound && "upper_bound node may not be NULL.");
1424 array->attr.aa.upper_bound[dimension] = upper_bound;
1426 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1427 ir_graph *rem = current_ir_graph;
1428 current_ir_graph = get_const_code_irg();
1429 set_array_upper_bound (array, dimension,
1430 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1431 current_ir_graph = rem;
1433 int has_array_lower_bound (const type *array, int dimension) {
1434 assert(array && (array->type_op == type_array));
1435 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1437 ir_node *get_array_lower_bound (const type *array, int dimension) {
1438 assert(array && (array->type_op == type_array));
1439 return array->attr.aa.lower_bound[dimension];
1441 long get_array_lower_bound_int (const type *array, int dimension) {
1443 assert(array && (array->type_op == type_array));
1444 node = array->attr.aa.lower_bound[dimension];
1445 assert(get_irn_op(node) == op_Const);
1446 return get_tarval_long(get_Const_tarval(node));
1448 int has_array_upper_bound (const type *array, int dimension) {
1449 assert(array && (array->type_op == type_array));
1450 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1452 ir_node * get_array_upper_bound (const type *array, int dimension) {
1453 assert(array && (array->type_op == type_array));
1454 return array->attr.aa.upper_bound[dimension];
1456 long get_array_upper_bound_int (const type *array, int dimension) {
1458 assert(array && (array->type_op == type_array));
1459 node = array->attr.aa.upper_bound[dimension];
1460 assert(get_irn_op(node) == op_Const);
1461 return get_tarval_long(get_Const_tarval(node));
1464 void set_array_order (type *array, int dimension, int order) {
1465 assert(array && (array->type_op == type_array));
1466 array->attr.aa.order[dimension] = order;
1468 int get_array_order (const type *array, int dimension) {
1469 assert(array && (array->type_op == type_array));
1470 return array->attr.aa.order[dimension];
1473 void set_array_element_type (type *array, type *tp) {
1474 assert(array && (array->type_op == type_array));
1475 assert(!is_method_type(tp));
1476 array->attr.aa.element_type = tp;
1478 type *get_array_element_type (type *array) {
1479 assert(array && (array->type_op == type_array));
1480 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1483 void set_array_element_entity (type *array, entity *ent) {
1484 assert(array && (array->type_op == type_array));
1485 assert((get_entity_type(ent)->type_op != type_method));
1486 array->attr.aa.element_ent = ent;
1487 array->attr.aa.element_type = get_entity_type(ent);
1489 entity *get_array_element_entity (const type *array) {
1490 assert(array && (array->type_op == type_array));
1491 return array->attr.aa.element_ent;
1495 int (is_array_type)(const type *array) {
1496 return __is_array_type(array);
1499 /*-----------------------------------------------------------------*/
1500 /* TYPE_ENUMERATION */
1501 /*-----------------------------------------------------------------*/
1503 /* create a new type enumeration -- set the enumerators independently */
1504 type *new_type_enumeration (ident *name, int n_enums) {
1506 res = new_type(type_enumeration, NULL, name);
1507 res->attr.ea.n_enums = n_enums;
1508 res->attr.ea.enumer = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
1509 res->attr.ea.enum_nameid = (ident **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1510 memset(res->attr.ea.enumer, 0, sizeof(res->attr.ea.enumer[0]) * n_enums);
1511 memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1514 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1515 type *res = new_type_enumeration (name, n_enums);
1516 set_type_dbg_info(res, db);
1520 void free_enumeration_entities(type *enumeration) {
1521 assert(enumeration && (enumeration->type_op == type_enumeration));
1523 void free_enumeration_attrs(type *enumeration) {
1524 assert(enumeration && (enumeration->type_op == type_enumeration));
1525 free(enumeration->attr.ea.enumer);
1526 free(enumeration->attr.ea.enum_nameid);
1529 /* manipulate fields of enumeration type. */
1530 int get_enumeration_n_enums (const type *enumeration) {
1531 assert(enumeration && (enumeration->type_op == type_enumeration));
1532 return enumeration->attr.ea.n_enums;
1534 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1535 assert(enumeration && (enumeration->type_op == type_enumeration));
1536 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1537 enumeration->attr.ea.enumer[pos] = con;
1539 tarval *get_enumeration_enum (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.enumer[pos];
1544 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1545 assert(enumeration && (enumeration->type_op == type_enumeration));
1546 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1547 enumeration->attr.ea.enum_nameid[pos] = id;
1549 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1550 assert(enumeration && (enumeration->type_op == type_enumeration));
1551 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1552 return enumeration->attr.ea.enum_nameid[pos];
1554 const char *get_enumeration_name(const type *enumeration, int pos) {
1555 assert(enumeration && (enumeration->type_op == type_enumeration));
1556 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1557 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1561 int (is_enumeration_type)(const type *enumeration) {
1562 return __is_enumeration_type(enumeration);
1565 /*-----------------------------------------------------------------*/
1567 /*-----------------------------------------------------------------*/
1569 /* Create a new type pointer */
1570 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1572 assert(mode_is_reference(ptr_mode));
1573 res = new_type(type_pointer, ptr_mode, name);
1574 res->attr.pa.points_to = points_to;
1575 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1576 res->size = get_mode_size_bits(res->mode);
1577 res->state = layout_fixed;
1580 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1581 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1582 set_type_dbg_info(res, db);
1585 void free_pointer_entities (type *pointer) {
1586 assert(pointer && (pointer->type_op == type_pointer));
1588 void free_pointer_attrs (type *pointer) {
1589 assert(pointer && (pointer->type_op == type_pointer));
1591 /* manipulate fields of type_pointer */
1592 void set_pointer_points_to_type (type *pointer, type *tp) {
1593 assert(pointer && (pointer->type_op == type_pointer));
1594 pointer->attr.pa.points_to = tp;
1596 type *get_pointer_points_to_type (type *pointer) {
1597 assert(pointer && (pointer->type_op == type_pointer));
1598 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1602 int (is_pointer_type)(const type *pointer) {
1603 return __is_pointer_type(pointer);
1606 /* Returns the first pointer type that has as points_to tp.
1607 * Not efficient: O(#types).
1608 * If not found returns unknown_type. */
1609 type *find_pointer_type_to_type (type *tp) {
1611 for (i = 0; i < get_irp_n_types(); ++i) {
1612 type *found = get_irp_type(i);
1613 if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1616 return unknown_type;
1621 /*-----------------------------------------------------------------*/
1622 /* TYPE_PRIMITIVE */
1623 /*-----------------------------------------------------------------*/
1625 /* create a new type primitive */
1626 type *new_type_primitive (ident *name, ir_mode *mode) {
1628 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1629 res = new_type(type_primitive, mode, name);
1630 res->size = get_mode_size_bits(mode);
1631 res->state = layout_fixed;
1634 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1635 type *res = new_type_primitive (name, mode);
1636 set_type_dbg_info(res, db);
1639 void free_primitive_entities (type *primitive) {
1640 assert(primitive && (primitive->type_op == type_primitive));
1642 void free_primitive_attrs (type *primitive) {
1643 assert(primitive && (primitive->type_op == type_primitive));
1647 int (is_primitive_type)(const type *primitive) {
1648 return __is_primitive_type(primitive);
1651 /*-----------------------------------------------------------------*/
1652 /* common functionality */
1653 /*-----------------------------------------------------------------*/
1656 int (is_atomic_type)(const type *tp) {
1657 return __is_atomic_type(tp);
1661 * Gets the number of elements in a firm compound type.
1663 int get_compound_n_members(const type *tp)
1667 if (is_struct_type(tp))
1668 res = get_struct_n_members(tp);
1669 else if (is_class_type(tp))
1670 res = get_class_n_members(tp);
1671 else if (is_union_type(tp))
1672 res = get_union_n_members(tp);
1674 assert(0 && "need struct, union or class for member count");
1680 * Gets the member of a firm compound type at position pos.
1682 entity *get_compound_member(const type *tp, int pos)
1686 if (is_struct_type(tp))
1687 res = get_struct_member(tp, pos);
1688 else if (is_class_type(tp))
1689 res = get_class_member(tp, pos);
1690 else if (is_union_type(tp))
1691 res = get_union_member(tp, pos);
1694 assert(0 && "need struct, union or class to get a member");
1702 int is_compound_type(const type *tp) {
1703 assert(tp && tp->kind == k_type);
1704 return (is_class_type(tp) || is_struct_type(tp) ||
1705 is_array_type(tp) || is_union_type(tp));