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"
49 # include "typegmod.h"
56 /*******************************************************************/
58 /*******************************************************************/
60 type *none_type; type *get_none_type(void) { return none_type; }
61 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
65 /** Returns a new, unique number to number nodes or the like. */
66 int get_irp_new_node_nr(void);
69 /* Suffixes added to types used for pass-by-value representations. */
70 static ident *value_params_suffix = NULL;
71 static ident *value_ress_suffix = NULL;
73 void init_type(void) {
74 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
75 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
77 /* construct none and unknown type. */
78 none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"));
79 set_type_size (none_type, 0);
80 set_type_state (none_type, layout_fixed);
81 remove_irp_type(none_type);
82 unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
83 set_type_size (unknown_type, 0);
84 set_type_state (unknown_type, layout_fixed);
85 remove_irp_type(unknown_type);
88 unsigned long type_visited;
89 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
90 INLINE unsigned long get_master_type_visited() { return type_visited; }
91 INLINE void inc_master_type_visited() { type_visited++; }
95 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
99 assert(type_op != type_id);
100 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
102 node_size = offsetof (type, attr) + type_op->attr_size;
103 res = (type *) xmalloc (node_size);
104 add_irp_type(res); /* Remember the new type global. */
107 res->type_op = type_op;
110 res->state = layout_undefined;
115 res->nr = get_irp_new_node_nr();
121 void free_type(type *tp) {
122 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
124 /* Remove from list of all types */
126 /* Free the attributes of the type. */
128 /* Free entities automatically allocated with the type */
129 if (is_array_type(tp))
130 free_entity(get_array_element_entity(tp));
131 /* And now the type itself... */
136 void free_type_entities(type *tp) {
137 switch(get_type_tpop_code(tp)) {
138 case tpo_class: { free_class_entities(tp); } break;
139 case tpo_struct: { free_struct_entities(tp); } break;
140 case tpo_method: { free_method_entities(tp); } break;
141 case tpo_union: { free_union_entities(tp); } break;
142 case tpo_array: { free_array_entities(tp); } break;
143 case tpo_enumeration: { free_enumeration_entities(tp); } break;
144 case tpo_pointer: { free_pointer_entities(tp); } break;
145 case tpo_primitive: { free_primitive_entities(tp); } break;
150 void free_type_attrs(type *tp) {
151 switch(get_type_tpop_code(tp)) {
152 case tpo_class: { free_class_attrs(tp); } break;
153 case tpo_struct: { free_struct_attrs(tp); } break;
154 case tpo_method: { free_method_attrs(tp); } break;
155 case tpo_union: { free_union_attrs(tp); } break;
156 case tpo_array: { free_array_attrs(tp); } break;
157 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
158 case tpo_pointer: { free_pointer_attrs(tp); } break;
159 case tpo_primitive: { free_primitive_attrs(tp); } break;
164 /* set/get the link field */
165 void *get_type_link(type *tp)
167 assert(tp && tp->kind == k_type);
171 void set_type_link(type *tp, void *l)
173 assert(tp && tp->kind == k_type);
177 tp_op* get_type_tpop(type *tp) {
178 assert(tp && tp->kind == k_type);
182 ident* get_type_tpop_nameid(type *tp) {
183 assert(tp && tp->kind == k_type);
184 return tp->type_op->name;
187 const char* get_type_tpop_name(type *tp) {
188 assert(tp && tp->kind == k_type);
189 return get_id_str(tp->type_op->name);
192 tp_opcode get_type_tpop_code(type *tp) {
193 assert(tp && tp->kind == k_type);
194 return tp->type_op->code;
197 ir_mode* get_type_mode(type *tp) {
198 assert(tp && tp->kind == k_type);
202 void set_type_mode(type *tp, ir_mode* m) {
203 assert(tp && tp->kind == k_type);
205 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
206 /* Modes of primitives must be data */
207 ((tp->type_op != type_enumeration) || mode_is_int(m)) &&
208 /* Modes of enumerations must be integers */
209 ((tp->type_op != type_pointer) || mode_is_reference(m)) );
210 /* Modes of pointers must be references. */
212 if ((tp->type_op == type_primitive) ||
213 (tp->type_op == type_enumeration) ||
214 (tp->type_op == type_pointer) ) {
215 /* For pointer, primitive and enumeration size depends on the mode. */
216 assert((get_mode_size_bytes(m) != -1) && "unorthodox modes not implemented");
217 tp->size = get_mode_size_bytes(m);
222 ident* get_type_ident(type *tp) {
223 assert(tp && tp->kind == k_type);
227 void set_type_ident(type *tp, ident* id) {
228 assert(tp && tp->kind == k_type);
232 /* Outputs a unique number for this node */
234 get_type_nr(type *tp) {
243 const char* get_type_name(type *tp) {
244 assert(tp && tp->kind == k_type);
245 return (get_id_str(tp->name));
248 int get_type_size(type *tp) {
249 assert(tp && tp->kind == k_type);
254 set_type_size(type *tp, int size) {
255 assert(tp && tp->kind == k_type);
256 /* For pointer enumeration and primitive size depends on the mode.
257 Methods don't have a size. */
258 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
259 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
264 get_type_state(type *tp) {
265 assert(tp && tp->kind == k_type);
270 set_type_state(type *tp, type_state state) {
271 assert(tp && tp->kind == k_type);
273 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
274 (tp->type_op == type_method))
277 /* Just a correctness check: */
278 if (state == layout_fixed) {
280 switch (get_type_tpop_code(tp)) {
283 assert(get_type_size(tp) > -1);
284 if (tp != get_glob_type())
285 for (i = 0; i < get_class_n_members(tp); i++) {
286 if (get_entity_offset(get_class_member(tp, i)) <= -1)
287 { DDMT(tp); DDME(get_class_member(tp, i)); }
288 assert(get_entity_offset(get_class_member(tp, i)) > -1);
290 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
291 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
297 assert(get_type_size(tp) > -1);
298 for (i = 0; i < get_struct_n_members(tp); i++) {
299 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
300 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
309 Assure that only innermost dimension is dynamic? */
311 case tpo_enumeration:
313 assert(get_type_mode != NULL);
314 for (i = 0; i < get_enumeration_n_enums(tp); i++)
315 assert(get_enumeration_enum(tp, i) != NULL);
323 unsigned long get_type_visited(type *tp) {
324 assert(tp && tp->kind == k_type);
328 void set_type_visited(type *tp, unsigned long num) {
329 assert(tp && tp->kind == k_type);
332 /* Sets visited field in type to type_visited. */
333 void mark_type_visited(type *tp) {
334 assert(tp && tp->kind == k_type);
335 assert(tp->visit < type_visited);
336 tp->visit = type_visited;
338 /* @@@ name clash with master flag
339 bool type_visited(type *tp) {
340 assert(tp && tp->kind == k_type);
341 return tp->visit >= type_visited;
343 bool type_not_visited(type *tp) {
344 assert(tp && tp->kind == k_type);
345 return tp->visit < type_visited;
349 int is_type (void *thing) {
351 if (get_kind(thing) == k_type)
358 bool equal_type(type *typ1, type *typ2) {
363 if (typ1 == typ2) return true;
365 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
366 (get_type_ident(typ1) != get_type_ident(typ2)) ||
367 (get_type_mode(typ1) != get_type_mode(typ2)) ||
368 (get_type_state(typ1) != get_type_state(typ2)))
370 if ((get_type_state(typ1) == layout_fixed) &&
371 (get_type_size(typ1) != get_type_size(typ2)))
374 switch(get_type_tpop_code(typ1)) {
376 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
377 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
378 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
379 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
380 /** Compare the members **/
381 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
382 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
383 /* First sort the members of typ2 */
384 for (i = 0; i < get_class_n_members(typ1); i++) {
385 entity *e1 = get_class_member(typ1, i);
386 for (j = 0; j < get_class_n_members(typ2); j++) {
387 entity *e2 = get_class_member(typ2, j);
388 if (get_entity_name(e1) == get_entity_name(e2))
392 for (i = 0; i < get_class_n_members(typ1); i++) {
393 if (!m[i] || /* Found no counterpart */
394 !equal_entity(get_class_member(typ1, i), m[i]))
397 /** Compare the supertypes **/
398 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
399 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
400 /* First sort the supertypes of typ2 */
401 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
402 type *t1 = get_class_supertype(typ1, i);
403 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
404 type *t2 = get_class_supertype(typ2, j);
405 if (get_type_ident(t2) == get_type_ident(t1))
409 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
410 if (!t[i] || /* Found no counterpart */
411 get_class_supertype(typ1, i) != t[i])
416 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
417 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
418 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
419 /* First sort the members of lt */
420 for (i = 0; i < get_struct_n_members(typ1); i++) {
421 entity *e1 = get_struct_member(typ1, i);
422 for (j = 0; j < get_struct_n_members(typ2); j++) {
423 entity *e2 = get_struct_member(typ2, j);
424 if (get_entity_name(e1) == get_entity_name(e2))
428 for (i = 0; i < get_struct_n_members(typ1); i++) {
429 if (!m[i] || /* Found no counterpart */
430 !equal_entity(get_struct_member(typ1, i), m[i]))
435 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
436 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
437 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
438 for (i = 0; i < get_method_n_params(typ1); i++) {
439 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
442 for (i = 0; i < get_method_n_ress(typ1); i++) {
443 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
448 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
449 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
450 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
451 /* First sort the members of lt */
452 for (i = 0; i < get_union_n_members(typ1); i++) {
453 entity *e1 = get_union_member(typ1, i);
454 for (j = 0; j < get_union_n_members(typ2); j++) {
455 entity *e2 = get_union_member(typ2, j);
456 if (get_entity_name(e1) == get_entity_name(e2))
460 for (i = 0; i < get_union_n_members(typ1); i++) {
461 if (!m[i] || /* Found no counterpart */
462 !equal_entity(get_union_member(typ1, i), m[i]))
467 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
469 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
471 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
472 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
473 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
475 if (get_array_order(typ1, i) != get_array_order(typ2, i))
476 assert(0 && "type compare with different dimension orders not implemented");
479 case tpo_enumeration: {
480 assert(0 && "enumerations not implemented");
483 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
486 case tpo_primitive: {
493 bool smaller_type (type *st, type *lt) {
497 if (st == lt) return true;
499 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
502 switch(get_type_tpop_code(st)) {
504 return is_subclass_of(st, lt);
507 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
508 m = alloca(sizeof(entity *) * get_struct_n_members(st));
509 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
510 /* First sort the members of lt */
511 for (i = 0; i < get_struct_n_members(st); i++) {
512 entity *se = get_struct_member(st, i);
513 for (j = 0; j < get_struct_n_members(lt); j++) {
514 entity *le = get_struct_member(lt, j);
515 if (get_entity_name(le) == get_entity_name(se))
519 for (i = 0; i < get_struct_n_members(st); i++) {
520 if (!m[i] || /* Found no counterpart */
521 !smaller_type(get_entity_type(get_struct_member(st, i)),
522 get_entity_type(m[i])))
527 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
528 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
529 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
530 for (i = 0; i < get_method_n_params(st); i++) {
531 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
534 for (i = 0; i < get_method_n_ress(st); i++) {
535 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
540 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
541 m = alloca(sizeof(entity *) * get_union_n_members(st));
542 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
543 /* First sort the members of lt */
544 for (i = 0; i < get_union_n_members(st); i++) {
545 entity *se = get_union_member(st, i);
546 for (j = 0; j < get_union_n_members(lt); j++) {
547 entity *le = get_union_member(lt, j);
548 if (get_entity_name(le) == get_entity_name(se))
552 for (i = 0; i < get_union_n_members(st); i++) {
553 if (!m[i] || /* Found no counterpart */
554 !smaller_type(get_entity_type(get_union_member(st, i)),
555 get_entity_type(m[i])))
560 type *set, *let; /* small/large elt. type */
561 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
563 set = get_array_element_type(st);
564 let = get_array_element_type(lt);
566 /* If the elt types are different, set must be convertible
567 to let, and they must have the same size so that address
568 computations work out. To have a size the layout must
570 if ((get_type_state(set) != layout_fixed) ||
571 (get_type_state(let) != layout_fixed))
573 if (!smaller_type(set, let) ||
574 get_type_size(set) != get_type_size(let))
577 for(i = 0; i < get_array_n_dimensions(st); i++) {
578 if (get_array_lower_bound(lt, i))
579 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
581 if (get_array_upper_bound(lt, i))
582 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
586 case tpo_enumeration: {
587 assert(0 && "enumerations not implemented");
590 if (!smaller_type(get_pointer_points_to_type(st),
591 get_pointer_points_to_type(lt)))
594 case tpo_primitive: {
595 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
603 /*******************************************************************/
605 /*******************************************************************/
607 /* create a new class type */
608 INLINE type *new_type_class (ident *name) {
611 res = new_type(type_class, NULL, name);
613 res->attr.ca.members = NEW_ARR_F (entity *, 1);
614 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
615 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
616 res->attr.ca.peculiarity = peculiarity_existent;
617 res->attr.ca.dfn = 0;
621 type *new_d_type_class (ident *name, dbg_info* db) {
622 type *res = new_type_class (name);
623 set_type_dbg_info(res, db);
627 INLINE void free_class_entities(type *clss) {
629 assert(clss && (clss->type_op == type_class));
630 for (i = get_class_n_members(clss)-1; i >= 0; --i)
631 free_entity(get_class_member(clss, i));
634 INLINE void free_class_attrs(type *clss) {
635 assert(clss && (clss->type_op == type_class));
636 DEL_ARR_F(clss->attr.ca.members);
637 DEL_ARR_F(clss->attr.ca.subtypes);
638 DEL_ARR_F(clss->attr.ca.supertypes);
641 /* manipulate private fields of class type */
642 void add_class_member (type *clss, entity *member) {
643 assert(clss && (clss->type_op == type_class));
644 ARR_APP1 (entity *, clss->attr.ca.members, member);
646 int get_class_n_members (type *clss) {
647 assert(clss && (clss->type_op == type_class));
648 return (ARR_LEN (clss->attr.ca.members))-1;
650 int get_class_member_index(type *clss, entity *mem) {
652 assert(clss && (clss->type_op == type_class));
653 for (i = 0; i < get_class_n_members(clss); i++)
654 if (get_class_member(clss, i) == mem)
658 entity *get_class_member (type *clss, int pos) {
659 assert(clss && (clss->type_op == type_class));
660 assert(pos >= 0 && pos < get_class_n_members(clss));
661 return clss->attr.ca.members[pos+1];
663 entity *get_class_member_by_name(type *clss, ident *name) {
665 assert(clss && (clss->type_op == type_class));
666 n_mem = get_class_n_members(clss);
667 for (i = 0; i < n_mem; ++i) {
668 entity *mem = get_class_member(clss, i);
669 if (get_entity_ident(mem) == name) return mem;
674 void set_class_member (type *clss, entity *member, int pos) {
675 assert(clss && (clss->type_op == type_class));
676 assert(pos >= 0 && pos < get_class_n_members(clss));
677 clss->attr.ca.members[pos+1] = member;
679 void set_class_members (type *clss, entity **members, int arity) {
681 assert(clss && (clss->type_op == type_class));
682 DEL_ARR_F(clss->attr.ca.members);
683 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
684 for (i = 0; i < arity; i++) {
685 set_entity_owner(members[i], clss);
686 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
689 void remove_class_member(type *clss, entity *member) {
691 assert(clss && (clss->type_op == type_class));
692 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
693 if (clss->attr.ca.members[i] == member) {
694 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
695 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
696 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
702 void add_class_subtype (type *clss, type *subtype) {
704 assert(clss && (clss->type_op == type_class));
705 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
706 for (i = 0; i < get_class_n_supertypes(subtype); i++)
707 if (get_class_supertype(subtype, i) == clss)
708 /* Class already registered */
710 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
712 int get_class_n_subtypes (type *clss) {
713 assert(clss && (clss->type_op == type_class));
714 return (ARR_LEN (clss->attr.ca.subtypes))-1;
716 type *get_class_subtype (type *clss, int pos) {
717 assert(clss && (clss->type_op == type_class));
718 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
719 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
721 void set_class_subtype (type *clss, type *subtype, int pos) {
722 assert(clss && (clss->type_op == type_class));
723 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
724 clss->attr.ca.subtypes[pos+1] = subtype;
726 void remove_class_subtype(type *clss, type *subtype) {
728 assert(clss && (clss->type_op == type_class));
729 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
730 if (clss->attr.ca.subtypes[i] == subtype) {
731 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
732 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
733 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
738 void add_class_supertype (type *clss, type *supertype) {
740 assert(clss && (clss->type_op == type_class));
741 assert(supertype && (supertype -> type_op == type_class));
742 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
743 for (i = 0; i < get_class_n_subtypes(supertype); i++)
744 if (get_class_subtype(supertype, i) == clss)
745 /* Class already registered */
747 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
749 int get_class_n_supertypes (type *clss) {
750 assert(clss && (clss->type_op == type_class));
751 return (ARR_LEN (clss->attr.ca.supertypes))-1;
753 int get_class_supertype_index(type *clss, type *super_clss) {
755 assert(clss && (clss->type_op == type_class));
756 assert(super_clss && (super_clss->type_op == type_class));
757 for (i = 0; i < get_class_n_supertypes(clss); i++)
758 if (get_class_supertype(clss, i) == super_clss)
762 type *get_class_supertype (type *clss, int pos) {
763 assert(clss && (clss->type_op == type_class));
764 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
765 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
767 void set_class_supertype (type *clss, type *supertype, int pos) {
768 assert(clss && (clss->type_op == type_class));
769 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
770 clss->attr.ca.supertypes[pos+1] = supertype;
772 void remove_class_supertype(type *clss, type *supertype) {
774 assert(clss && (clss->type_op == type_class));
775 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
776 if (clss->attr.ca.supertypes[i] == supertype) {
777 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
778 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
779 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
784 char *get_peculiarity_string(peculiarity p) {
785 if (p == peculiarity_description)
786 return "peculiarity_description";
787 if (p == peculiarity_inherited)
788 return "peculiarity_inherited";
789 return "peculiarity_existent";
792 INLINE peculiarity get_class_peculiarity (type *clss) {
793 assert(clss && (clss->type_op == type_class));
794 return clss->attr.ca.peculiarity;
796 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
797 assert(clss && (clss->type_op == type_class));
798 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
799 clss->attr.ca.peculiarity = pec;
802 void set_class_dfn (type *clss, int dfn)
804 clss->attr.ca.dfn = dfn;
807 int get_class_dfn (type *clss)
809 return (clss->attr.ca.dfn);
813 bool is_class_type(type *clss) {
815 if (clss->type_op == type_class) return 1; else return 0;
818 bool is_subclass_of(type *low, type *high) {
820 assert(is_class_type(low) && is_class_type(high));
821 if (low == high) return true;
822 /* depth first search from high downwards. */
823 for (i = 0; i < get_class_n_subtypes(high); i++) {
824 if (low == get_class_subtype(high, i))
826 if (is_subclass_of(low, get_class_subtype(high, i)))
832 /*******************************************************************/
834 /*******************************************************************/
836 /* create a new type struct */
837 INLINE type *new_type_struct (ident *name) {
839 res = new_type(type_struct, NULL, name);
840 res->attr.sa.members = NEW_ARR_F (entity *, 1);
843 type *new_d_type_struct (ident *name, dbg_info* db) {
844 type *res = new_type_struct (name);
845 set_type_dbg_info(res, db);
848 INLINE void free_struct_entities (type *strct) {
850 assert(strct && (strct->type_op == type_struct));
851 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
852 free_entity(get_struct_member(strct, i));
854 INLINE void free_struct_attrs (type *strct) {
855 assert(strct && (strct->type_op == type_struct));
856 DEL_ARR_F(strct->attr.sa.members);
859 /* manipulate private fields of struct */
860 int get_struct_n_members (type *strct) {
861 assert(strct && (strct->type_op == type_struct));
862 return (ARR_LEN (strct->attr.sa.members))-1;
864 void add_struct_member (type *strct, entity *member) {
865 assert(strct && (strct->type_op == type_struct));
866 assert(get_type_tpop(get_entity_type(member)) != type_method);
867 /* @@@ lowerfirm geht nicht durch */
868 ARR_APP1 (entity *, strct->attr.sa.members, member);
870 entity *get_struct_member (type *strct, int pos) {
871 assert(strct && (strct->type_op == type_struct));
872 assert(pos >= 0 && pos < get_struct_n_members(strct));
873 return strct->attr.sa.members[pos+1];
875 void set_struct_member (type *strct, int pos, entity *member) {
876 assert(strct && (strct->type_op == type_struct));
877 assert(pos >= 0 && pos < get_struct_n_members(strct));
878 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
879 strct->attr.sa.members[pos+1] = member;
881 void remove_struct_member(type *strct, entity *member) {
883 assert(strct && (strct->type_op == type_struct));
884 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
885 if (strct->attr.sa.members[i] == member) {
886 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
887 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
888 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
894 bool is_struct_type(type *strct) {
896 if (strct->type_op == type_struct) return 1; else return 0;
899 /*******************************************************************/
901 /*******************************************************************/
903 /* Lazy construction of value argument / result representation. */
905 build_value_type(ident *name, int len, type **tps) {
907 type *res = new_type_struct(name);
908 /* Remove type from type list. Must be treated differently than other types. */
909 remove_irp_type_from_list(res);
910 for (i = 0; i < len; i++) {
911 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
912 if (tps[i]) elt_type = tps[i];
913 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
918 /* Create a new method type.
919 N_param is the number of parameters, n_res the number of results. */
920 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
922 res = new_type(type_method, mode_P_mach, name);
923 res->state = layout_fixed;
924 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
925 res->size = get_mode_size_bytes(mode_P_mach);
926 res->attr.ma.n_params = n_param;
927 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
928 res->attr.ma.value_params = NULL;
929 res->attr.ma.n_res = n_res;
930 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
931 res->attr.ma.value_ress = NULL;
932 res->attr.ma.variadicity = variadicity_non_variadic;
937 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
938 type *res = new_type_method (name, n_param, n_res);
939 set_type_dbg_info(res, db);
943 INLINE void free_method_entities(type *method) {
944 assert(method && (method->type_op == type_method));
946 /* Attention: also frees entities in value parameter subtypes! */
947 INLINE void free_method_attrs(type *method) {
948 assert(method && (method->type_op == type_method));
949 free(method->attr.ma.param_type);
950 free(method->attr.ma.res_type);
951 if (method->attr.ma.value_params) {
952 free_type_entities(method->attr.ma.value_params);
953 free_type(method->attr.ma.value_params);
955 if (method->attr.ma.value_ress) {
956 free_type_entities(method->attr.ma.value_ress);
957 free_type(method->attr.ma.value_ress);
961 /* manipulate private fields of method. */
962 int get_method_n_params (type *method) {
963 assert(method && (method->type_op == type_method));
964 return method->attr.ma.n_params;
966 type *get_method_param_type(type *method, int pos) {
968 assert(method && (method->type_op == type_method));
969 assert(pos >= 0 && pos < get_method_n_params(method));
970 res = method->attr.ma.param_type[pos];
971 assert(res != NULL && "empty method param type");
972 return method->attr.ma.param_type[pos] = skip_tid(res);
974 void set_method_param_type(type *method, int pos, type* tp) {
975 assert(method && (method->type_op == type_method));
976 assert(pos >= 0 && pos < get_method_n_params(method));
977 method->attr.ma.param_type[pos] = tp;
978 /* If information constructed set pass-by-value representation. */
979 if (method->attr.ma.value_params) {
980 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
981 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
984 /* Returns an entity that represents the copied value argument. Only necessary
985 for compounds passed by value. */
986 entity *get_method_value_param_ent(type *method, int pos) {
987 assert(method && (method->type_op == type_method));
988 assert(pos >= 0 && pos < get_method_n_params(method));
989 if (!method->attr.ma.value_params)
990 method->attr.ma.value_params
991 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
992 get_method_n_params(method), method->attr.ma.param_type);
993 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
994 != method->attr.ma.value_params)
995 && "param type not yet set");
996 return get_struct_member(method->attr.ma.value_params, pos);
999 type *get_method_value_res_type(type *method) {
1000 assert(method && (method->type_op == type_method));
1001 return method->attr.ma.value_params;
1005 int get_method_n_ress (type *method) {
1006 assert(method && (method->type_op == type_method));
1007 return method->attr.ma.n_res;
1009 type *get_method_res_type(type *method, int pos) {
1011 assert(method && (method->type_op == type_method));
1012 assert(pos >= 0 && pos < get_method_n_ress(method));
1013 res = method->attr.ma.res_type[pos];
1014 assert(res != NULL && "empty method return type");
1015 return method->attr.ma.res_type[pos] = skip_tid(res);
1017 void set_method_res_type(type *method, int pos, type* tp) {
1018 assert(method && (method->type_op == type_method));
1019 assert(pos >= 0 && pos < get_method_n_ress(method));
1020 /* set the result type */
1021 method->attr.ma.res_type[pos] = tp;
1022 /* If information constructed set pass-by-value representation. */
1023 if (method->attr.ma.value_ress) {
1024 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1025 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1028 /* Returns an entity that represents the copied value result. Only necessary
1029 for compounds passed by value. */
1030 entity *get_method_value_res_ent(type *method, int pos) {
1031 assert(method && (method->type_op == type_method));
1032 assert(pos >= 0 && pos < get_method_n_ress(method));
1033 if (!method->attr.ma.value_ress)
1034 method->attr.ma.value_ress
1035 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1036 get_method_n_ress(method), method->attr.ma.res_type);
1037 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1038 && "result type not yet set");
1039 return get_struct_member(method->attr.ma.value_ress, pos);
1042 /* Returns the null-terminated name of this variadicity. */
1043 const char *get_variadicity_name(variadicity vari)
1045 #define X(a) case a: return #a
1047 X(variadicity_non_variadic);
1048 X(variadicity_variadic);
1055 variadicity get_method_variadicity(type *method)
1057 assert(method && (method->type_op == type_method));
1058 return method->attr.ma.variadicity;
1061 void set_method_variadicity(type *method, variadicity vari)
1063 assert(method && (method->type_op == type_method));
1064 method->attr.ma.variadicity = vari;
1068 bool is_method_type (type *method) {
1070 if (method->type_op == type_method) return 1; else return 0;
1073 /*******************************************************************/
1075 /*******************************************************************/
1077 /* create a new type uni */
1078 INLINE type *new_type_union (ident *name) {
1080 res = new_type(type_union, NULL, name);
1081 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
1082 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
1083 res->attr.ua.members = NEW_ARR_F (entity *, 1);
1086 type *new_d_type_union (ident *name, dbg_info* db) {
1087 type *res = new_type_union (name);
1088 set_type_dbg_info(res, db);
1091 INLINE void free_union_entities (type *uni) {
1093 assert(uni && (uni->type_op == type_union));
1094 for (i = get_union_n_members(uni)-1; i >= 0; --i)
1095 free_entity(get_union_member(uni, i));
1097 INLINE void free_union_attrs (type *uni) {
1098 assert(uni && (uni->type_op == type_union));
1099 DEL_ARR_F(uni->attr.ua.members);
1101 /* manipulate private fields of union */
1103 int get_union_n_types (type *uni) {
1104 assert(uni && (uni->type_op == type_union));
1105 return uni->attr.ua.n_types;
1107 type *get_union_unioned_type (type *uni, int pos) {
1108 assert(uni && (uni->type_op == type_union));
1109 assert(pos >= 0 && pos < get_union_n_types(uni));
1110 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1112 void set_union_unioned_type (type *uni, int pos, type *tp) {
1113 assert(uni && (uni->type_op == type_union));
1114 assert(pos >= 0 && pos < get_union_n_types(uni));
1115 uni->attr.ua.unioned_type[pos] = tp;
1117 ident *get_union_delim_nameid (type *uni, int pos) {
1118 assert(uni && (uni->type_op == type_union));
1119 assert(pos >= 0 && pos < get_union_n_types(uni));
1120 return uni->attr.ua.delim_names[pos];
1122 const char *get_union_delim_name (type *uni, int pos) {
1123 assert(uni && (uni->type_op == type_union));
1124 assert(pos >= 0 && pos < get_union_n_types(uni));
1125 return get_id_str(uni->attr.ua.delim_names[pos]);
1127 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1128 assert(uni && (uni->type_op == type_union));
1129 assert(pos >= 0 && pos < get_union_n_types(uni));
1130 uni->attr.ua.delim_names[pos] = id;
1133 int get_union_n_members (type *uni) {
1134 assert(uni && (uni->type_op == type_union));
1135 return (ARR_LEN (uni->attr.ua.members))-1;
1137 void add_union_member (type *uni, entity *member) {
1138 assert(uni && (uni->type_op == type_union));
1139 ARR_APP1 (entity *, uni->attr.ua.members, member);
1141 entity *get_union_member (type *uni, int pos) {
1142 assert(uni && (uni->type_op == type_union));
1143 assert(pos >= 0 && pos < get_union_n_members(uni));
1144 return uni->attr.ua.members[pos+1];
1146 void set_union_member (type *uni, int pos, entity *member) {
1147 assert(uni && (uni->type_op == type_union));
1148 assert(pos >= 0 && pos < get_union_n_members(uni));
1149 uni->attr.ua.members[pos+1] = member;
1151 void remove_union_member(type *uni, entity *member) {
1153 assert(uni && (uni->type_op == type_union));
1154 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
1155 if (uni->attr.ua.members[i] == member) {
1156 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1157 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1158 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1164 bool is_union_type (type *uni) {
1166 if (uni->type_op == type_union) return 1; else return 0;
1169 /*******************************************************************/
1171 /*******************************************************************/
1174 /* create a new type array -- set dimension sizes independently */
1175 INLINE type *new_type_array (ident *name, int n_dimensions,
1176 type *element_type) {
1179 ir_graph *rem = current_ir_graph;
1180 assert(!is_method_type(element_type));
1182 res = new_type(type_array, NULL, name);
1183 res->attr.aa.n_dimensions = n_dimensions;
1184 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1185 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1186 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1188 current_ir_graph = get_const_code_irg();
1189 for (i = 0; i < n_dimensions; i++) {
1190 res->attr.aa.lower_bound[i] = new_Unknown(mode_Iu);
1191 res->attr.aa.upper_bound[i] = new_Unknown(mode_Iu);
1192 res->attr.aa.order[i] = i;
1194 current_ir_graph = rem;
1196 res->attr.aa.element_type = element_type;
1197 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1201 type *new_d_type_array (ident *name, int n_dimensions,
1202 type *element_type, dbg_info* db) {
1203 type *res = new_type_array (name, n_dimensions, element_type);
1204 set_type_dbg_info(res, db);
1208 INLINE void free_array_entities (type *array) {
1209 assert(array && (array->type_op == type_array));
1211 INLINE void free_array_attrs (type *array) {
1212 assert(array && (array->type_op == type_array));
1213 free(array->attr.aa.lower_bound);
1214 free(array->attr.aa.upper_bound);
1217 /* manipulate private fields of array type */
1218 int get_array_n_dimensions (type *array) {
1219 assert(array && (array->type_op == type_array));
1220 return array->attr.aa.n_dimensions;
1224 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1225 ir_node * upper_bound) {
1226 assert(array && (array->type_op == type_array));
1227 assert(lower_bound && "lower_bound node may not be NULL.");
1228 assert(upper_bound && "upper_bound node may not be NULL.");
1229 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1230 array->attr.aa.lower_bound[dimension] = lower_bound;
1231 array->attr.aa.upper_bound[dimension] = upper_bound;
1234 set_array_bounds_int (type *array, int dimension, int lower_bound,
1236 ir_graph *rem = current_ir_graph;
1237 current_ir_graph = get_const_code_irg();
1238 set_array_bounds (array, dimension,
1239 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1240 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1241 current_ir_graph = rem;
1244 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1245 assert(array && (array->type_op == type_array));
1246 assert(lower_bound && "lower_bound node may not be NULL.");
1247 array->attr.aa.lower_bound[dimension] = lower_bound;
1249 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1250 ir_graph *rem = current_ir_graph;
1251 current_ir_graph = get_const_code_irg();
1252 set_array_lower_bound (array, dimension,
1253 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1254 current_ir_graph = rem;
1257 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1258 assert(array && (array->type_op == type_array));
1259 assert(upper_bound && "upper_bound node may not be NULL.");
1260 array->attr.aa.upper_bound[dimension] = upper_bound;
1262 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1263 ir_graph *rem = current_ir_graph;
1264 current_ir_graph = get_const_code_irg();
1265 set_array_upper_bound (array, dimension,
1266 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1267 current_ir_graph = rem;
1269 int has_array_lower_bound (type *array, int dimension) {
1270 assert(array && (array->type_op == type_array));
1271 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1273 ir_node * get_array_lower_bound (type *array, int dimension) {
1274 assert(array && (array->type_op == type_array));
1275 return array->attr.aa.lower_bound[dimension];
1277 int has_array_upper_bound (type *array, int dimension) {
1278 assert(array && (array->type_op == type_array));
1279 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1281 ir_node * get_array_upper_bound (type *array, int dimension) {
1282 assert(array && (array->type_op == type_array));
1283 return array->attr.aa.upper_bound[dimension];
1286 void set_array_order (type *array, int dimension, int order) {
1287 assert(array && (array->type_op == type_array));
1288 array->attr.aa.order[dimension] = order;
1290 int get_array_order (type *array, int dimension) {
1291 assert(array && (array->type_op == type_array));
1292 return array->attr.aa.order[dimension];
1295 void set_array_element_type (type *array, type *tp) {
1296 assert(array && (array->type_op == type_array));
1297 assert(!is_method_type(tp));
1298 array->attr.aa.element_type = tp;
1300 type *get_array_element_type (type *array) {
1301 assert(array && (array->type_op == type_array));
1302 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1305 void set_array_element_entity (type *array, entity *ent) {
1306 assert(array && (array->type_op == type_array));
1307 assert((get_entity_type(ent)->type_op != type_method));
1308 array->attr.aa.element_ent = ent;
1309 array->attr.aa.element_type = get_entity_type(ent);
1311 entity *get_array_element_entity (type *array) {
1312 assert(array && (array->type_op == type_array));
1313 return array->attr.aa.element_ent;
1317 bool is_array_type (type *array) {
1319 if (array->type_op == type_array) return 1; else return 0;
1322 /*******************************************************************/
1323 /** TYPE_ENUMERATION **/
1324 /*******************************************************************/
1326 /* create a new type enumeration -- set the enumerators independently */
1327 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1329 res = new_type(type_enumeration, NULL, name);
1330 res->attr.ea.n_enums = n_enums;
1331 res->attr.ea.enumer = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
1332 res->attr.ea.enum_nameid = (ident **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1333 memset(res->attr.ea.enumer, 0, sizeof(res->attr.ea.enumer[0]) * n_enums);
1334 memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1337 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1338 type *res = new_type_enumeration (name, n_enums);
1339 set_type_dbg_info(res, db);
1343 INLINE void free_enumeration_entities(type *enumeration) {
1344 assert(enumeration && (enumeration->type_op == type_enumeration));
1346 INLINE void free_enumeration_attrs(type *enumeration) {
1347 assert(enumeration && (enumeration->type_op == type_enumeration));
1348 free(enumeration->attr.ea.enumer);
1349 free(enumeration->attr.ea.enum_nameid);
1352 /* manipulate fields of enumeration type. */
1353 int get_enumeration_n_enums (type *enumeration) {
1354 assert(enumeration && (enumeration->type_op == type_enumeration));
1355 return enumeration->attr.ea.n_enums;
1357 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1358 assert(enumeration && (enumeration->type_op == type_enumeration));
1359 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1360 enumeration->attr.ea.enumer[pos] = con;
1362 tarval *get_enumeration_enum (type *enumeration, int pos) {
1363 assert(enumeration && (enumeration->type_op == type_enumeration));
1364 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1365 return enumeration->attr.ea.enumer[pos];
1367 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1368 assert(enumeration && (enumeration->type_op == type_enumeration));
1369 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1370 enumeration->attr.ea.enum_nameid[pos] = id;
1372 ident *get_enumeration_nameid (type *enumeration, int pos) {
1373 assert(enumeration && (enumeration->type_op == type_enumeration));
1374 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1375 return enumeration->attr.ea.enum_nameid[pos];
1377 const char *get_enumeration_name(type *enumeration, int pos) {
1378 assert(enumeration && (enumeration->type_op == type_enumeration));
1379 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1380 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1384 bool is_enumeration_type (type *enumeration) {
1385 assert(enumeration);
1386 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1389 /*******************************************************************/
1390 /** TYPE_POINTER **/
1391 /*******************************************************************/
1393 /* Create a new type pointer */
1394 INLINE type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1396 assert(mode_is_reference(ptr_mode));
1397 res = new_type(type_pointer, ptr_mode, name);
1398 res->attr.pa.points_to = points_to;
1399 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1400 res->size = get_mode_size_bytes(res->mode);
1401 res->state = layout_fixed;
1404 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1405 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1406 set_type_dbg_info(res, db);
1409 INLINE void free_pointer_entities (type *pointer) {
1410 assert(pointer && (pointer->type_op == type_pointer));
1412 INLINE void free_pointer_attrs (type *pointer) {
1413 assert(pointer && (pointer->type_op == type_pointer));
1415 /* manipulate fields of type_pointer */
1416 void set_pointer_points_to_type (type *pointer, type *tp) {
1417 assert(pointer && (pointer->type_op == type_pointer));
1418 pointer->attr.pa.points_to = tp;
1420 type *get_pointer_points_to_type (type *pointer) {
1421 assert(pointer && (pointer->type_op == type_pointer));
1422 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1426 bool is_pointer_type (type *pointer) {
1428 if (pointer->type_op == type_pointer) return 1; else return 0;
1431 /* Returns the first pointer type that has as points_to tp.
1432 * Not efficient: O(#types).
1433 * If not found returns unknown_type. */
1434 type *find_pointer_type_to_type (type *tp) {
1436 for (i = 0; i < get_irp_n_types(); ++i) {
1437 type *found = get_irp_type(i);
1438 if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1441 return unknown_type;
1446 /*******************************************************************/
1447 /** TYPE_PRIMITIVE **/
1448 /*******************************************************************/
1450 /* create a new type primitive */
1451 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1453 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1454 res = new_type(type_primitive, mode, name);
1455 assert((get_mode_size_bytes(mode) != -1) && "unorthodox modes not implemented");
1456 res->size = get_mode_size_bytes(mode);
1457 res->state = layout_fixed;
1460 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1461 type *res = new_type_primitive (name, mode);
1462 set_type_dbg_info(res, db);
1465 INLINE void free_primitive_entities (type *primitive) {
1466 assert(primitive && (primitive->type_op == type_primitive));
1468 INLINE void free_primitive_attrs (type *primitive) {
1469 assert(primitive && (primitive->type_op == type_primitive));
1473 bool is_primitive_type (type *primitive) {
1474 assert(primitive && primitive->kind == k_type);
1475 if (primitive->type_op == type_primitive) return 1; else return 0;
1478 /*******************************************************************/
1479 /** common functionality **/
1480 /*******************************************************************/
1483 INLINE int is_atomic_type(type *tp) {
1484 assert(tp && tp->kind == k_type);
1485 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1486 is_enumeration_type(tp));
1490 * Gets the number of elements in a firm compound type.
1492 int get_compound_n_members(type *tp)
1496 if (is_struct_type(tp))
1497 res = get_struct_n_members(tp);
1498 else if (is_class_type(tp))
1499 res = get_class_n_members(tp);
1500 else if (is_union_type(tp))
1501 res = get_union_n_members(tp);
1503 assert(0 && "need struct, union or class for member count");
1509 * Gets the member of a firm compound type at position pos.
1511 entity *get_compound_member(type *tp, int pos)
1515 if (is_struct_type(tp))
1516 res = get_struct_member(tp, pos);
1517 else if (is_class_type(tp))
1518 res = get_class_member(tp, pos);
1519 else if (is_union_type(tp))
1520 res = get_union_member(tp, pos);
1523 assert(0 && "need struct, union or class to get a member");
1531 INLINE int is_compound_type(type *tp) {
1532 assert(tp && tp->kind == k_type);
1533 return (is_class_type(tp) || is_struct_type(tp) ||
1534 is_array_type(tp) || is_union_type(tp));
1541 #if 1 || DEBUG_libfirm
1542 INLINE int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1544 void dump_type (type *tp) {
1547 printf("%s type %s (%ld)\n", get_tpop_name(get_type_tpop(tp)), get_type_name(tp), get_type_nr(tp));
1549 switch (get_type_tpop_code(tp)) {
1552 printf(" members: ");
1553 for (i = 0; i < get_class_n_members(tp); ++i) {
1554 entity *mem = get_class_member(tp, i);
1555 printf("\n (%2d) %s:\t %s",
1556 get_entity_offset(mem), get_type_name(get_entity_type(mem)), get_entity_name(mem));
1561 printf("not implemented\n");
1565 #else /* DEBUG_libfirm */
1566 void dump_type (type *tp) {}
1567 #endif /* DEBUG_libfirm */