3 * file type.c - implementation of the datastructure to hold
5 * (C) 2001 by Universitaet Karlsruhe
6 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
8 * This module supplies a datastructure to represent all types
9 * known in the compiled program. This includes types specified
10 * in the program as well as types defined by the language. In the
11 * view of the intermediate representation there is no difference
12 * between these types.
14 * There exist several kinds of types, arranged by the structure of
15 * the type. A type is described by a set of attributes. Some of
16 * these attributes are common to all types, others depend on the
19 * Types are different from the modes defined in irmode: Types are
20 * on the level of the programming language, modes at the level of
21 * the target processor.
23 * @see type_t.h type tpop
37 # include "typegmod_t.h"
44 /*******************************************************************/
46 /*******************************************************************/
49 /** Returns a new, unique number to number nodes or the like. */
50 int get_irp_new_node_nr(void);
53 unsigned long type_visited;
54 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
55 INLINE unsigned long get_master_type_visited() { return type_visited; }
56 INLINE void inc_master_type_visited() { type_visited++; }
58 void free_type(type *tp) {
59 /* Remove from list of all types */
61 /* Free the attributes of the type. */
63 /* Free entities automatically allocated with the type */
64 if (is_array_type(tp))
65 free_entity(get_array_element_entity(tp));
66 /* And now the type itself... */
71 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
75 assert(type_op != type_id);
76 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
78 node_size = offsetof (type, attr) + type_op->attr_size;
79 res = (type *) xmalloc (node_size);
80 add_irp_type(res); /* Remember the new type global. */
83 res->type_op = type_op;
86 res->state = layout_undefined;
91 res->nr = get_irp_new_node_nr();
97 void free_type_attrs(type *tp) {
98 switch(get_type_tpop_code(tp)) {
99 case tpo_class: { free_class_attrs(tp); } break;
100 case tpo_struct: { free_struct_attrs(tp); } break;
101 case tpo_method: { free_method_attrs(tp); } break;
102 case tpo_union: { free_union_attrs(tp); } break;
103 case tpo_array: { free_array_attrs(tp); } break;
104 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
105 case tpo_pointer: { free_pointer_attrs(tp); } break;
106 case tpo_primitive: { free_primitive_attrs(tp); } break;
111 /* set/get the link field */
112 void *get_type_link(type *tp)
114 assert(tp && tp->kind == k_type);
118 void set_type_link(type *tp, void *l)
120 assert(tp && tp->kind == k_type);
124 tp_op* get_type_tpop(type *tp) {
125 assert(tp && tp->kind == k_type);
129 ident* get_type_tpop_nameid(type *tp) {
130 assert(tp && tp->kind == k_type);
131 return tp->type_op->name;
134 const char* get_type_tpop_name(type *tp) {
135 assert(tp && tp->kind == k_type);
136 return id_to_str(tp->type_op->name);
139 tp_opcode get_type_tpop_code(type *tp) {
140 assert(tp && tp->kind == k_type);
141 return tp->type_op->code;
144 ir_mode* get_type_mode(type *tp) {
145 assert(tp && tp->kind == k_type);
149 void set_type_mode(type *tp, ir_mode* m) {
150 assert(tp && tp->kind == k_type);
152 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
153 /* Modes of primitives must be data */
154 ((tp->type_op != type_enumeration) || mode_is_int(m)));
155 /* Modes of enumerations must be integers */
157 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
158 /* For pointer, primitive and enumeration size depends on the mode. */
159 tp->size = get_mode_size(m);
164 ident* get_type_ident(type *tp) {
165 assert(tp && tp->kind == k_type);
169 void set_type_ident(type *tp, ident* id) {
170 assert(tp && tp->kind == k_type);
174 /* Outputs a unique number for this node */
176 get_type_nr(type *tp) {
185 const char* get_type_name(type *tp) {
186 assert(tp && tp->kind == k_type);
187 return (id_to_str(tp->name));
190 int get_type_size(type *tp) {
191 assert(tp && tp->kind == k_type);
196 set_type_size(type *tp, int size) {
197 assert(tp && tp->kind == k_type);
198 /* For pointer enumeration and primitive size depends on the mode.
199 Methods don't have a size. */
200 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
201 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
206 get_type_state(type *tp) {
207 assert(tp && tp->kind == k_type);
212 set_type_state(type *tp, type_state state) {
213 assert(tp && tp->kind == k_type);
215 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
216 (tp->type_op == type_method))
219 /* Just a correctness check: */
220 if (state == layout_fixed) {
222 switch (get_type_tpop_code(tp)) {
225 assert(get_type_size(tp) > -1);
226 if (tp != get_glob_type())
227 for (i = 0; i < get_class_n_members(tp); i++) {
228 assert(get_entity_offset(get_class_member(tp, i)) > -1);
229 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
230 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
235 assert(get_type_size(tp) > -1);
236 for (i = 0; i < get_struct_n_members(tp); i++) {
237 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
238 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
247 Assure that only innermost dimension is dynamic? */
249 case tpo_enumeration:
251 assert(get_type_mode != NULL);
252 for (i = 0; i < get_enumeration_n_enums(tp); i++)
253 assert(get_enumeration_enum(tp, i) != NULL);
261 unsigned long get_type_visited(type *tp) {
262 assert(tp && tp->kind == k_type);
266 void set_type_visited(type *tp, unsigned long num) {
267 assert(tp && tp->kind == k_type);
270 /* Sets visited field in type to type_visited. */
271 void mark_type_visited(type *tp) {
272 assert(tp && tp->kind == k_type);
273 assert(tp->visit < type_visited);
274 tp->visit = type_visited;
276 /* @@@ name clash with master flag
277 bool type_visited(type *tp) {
278 assert(tp && tp->kind == k_type);
279 return tp->visit >= type_visited;
281 bool type_not_visited(type *tp) {
282 assert(tp && tp->kind == k_type);
283 return tp->visit < type_visited;
287 int is_type (void *thing) {
289 if (get_kind(thing) == k_type)
296 bool equal_type(type *typ1, type *typ2) {
301 if (typ1 == typ2) return true;
303 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
304 (get_type_name(typ1) != get_type_name(typ2)) ||
305 (get_type_mode(typ1) != get_type_mode(typ2)) ||
306 (get_type_state(typ1) != get_type_state(typ2)))
308 if ((get_type_state(typ1) == layout_fixed) &&
309 (get_type_size(typ1) != get_type_size(typ2)))
312 switch(get_type_tpop_code(typ1)) {
314 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
315 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
316 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
317 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
318 /** Compare the members **/
319 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
320 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
321 /* First sort the members of typ2 */
322 for (i = 0; i < get_class_n_members(typ1); i++) {
323 entity *e1 = get_class_member(typ1, i);
324 for (j = 0; j < get_class_n_members(typ2); j++) {
325 entity *e2 = get_class_member(typ2, j);
326 if (get_entity_name(e1) == get_entity_name(e2))
330 for (i = 0; i < get_class_n_members(typ1); i++) {
331 if (!m[i] || /* Found no counterpart */
332 !equal_entity(get_class_member(typ1, i), m[i]))
335 /** Compare the supertypes **/
336 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
337 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
338 /* First sort the supertypes of typ2 */
339 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
340 type *t1 = get_class_supertype(typ1, i);
341 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
342 type *t2 = get_class_supertype(typ2, j);
343 if (get_type_name(t2) == get_type_name(t1))
347 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
348 if (!t[i] || /* Found no counterpart */
349 get_class_supertype(typ1, i) != t[i])
354 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
355 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
356 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
357 /* First sort the members of lt */
358 for (i = 0; i < get_struct_n_members(typ1); i++) {
359 entity *e1 = get_struct_member(typ1, i);
360 for (j = 0; j < get_struct_n_members(typ2); j++) {
361 entity *e2 = get_struct_member(typ2, j);
362 if (get_entity_name(e1) == get_entity_name(e2))
366 for (i = 0; i < get_struct_n_members(typ1); i++) {
367 if (!m[i] || /* Found no counterpart */
368 !equal_entity(get_struct_member(typ1, i), m[i]))
373 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
374 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
375 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
376 for (i = 0; i < get_method_n_params(typ1); i++) {
377 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
380 for (i = 0; i < get_method_n_ress(typ1); i++) {
381 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
386 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
387 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
388 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
389 /* First sort the members of lt */
390 for (i = 0; i < get_union_n_members(typ1); i++) {
391 entity *e1 = get_union_member(typ1, i);
392 for (j = 0; j < get_union_n_members(typ2); j++) {
393 entity *e2 = get_union_member(typ2, j);
394 if (get_entity_name(e1) == get_entity_name(e2))
398 for (i = 0; i < get_union_n_members(typ1); i++) {
399 if (!m[i] || /* Found no counterpart */
400 !equal_entity(get_union_member(typ1, i), m[i]))
405 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
407 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
409 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
410 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
411 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
413 if (get_array_order(typ1, i) != get_array_order(typ2, i))
414 assert(0 && "type compare with different dimension orders not implemented");
417 case tpo_enumeration: {
418 assert(0 && "enumerations not implemented");
421 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
424 case tpo_primitive: {
431 bool smaller_type (type *st, type *lt) {
435 if (st == lt) return true;
437 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
440 switch(get_type_tpop_code(st)) {
442 return is_subclass_of(st, lt);
445 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
446 m = alloca(sizeof(entity *) * get_struct_n_members(st));
447 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
448 /* First sort the members of lt */
449 for (i = 0; i < get_struct_n_members(st); i++) {
450 entity *se = get_struct_member(st, i);
451 for (j = 0; j < get_struct_n_members(lt); j++) {
452 entity *le = get_struct_member(lt, j);
453 if (get_entity_name(le) == get_entity_name(se))
457 for (i = 0; i < get_struct_n_members(st); i++) {
458 if (!m[i] || /* Found no counterpart */
459 !smaller_type(get_entity_type(get_struct_member(st, i)),
460 get_entity_type(m[i])))
465 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
466 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
467 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
468 for (i = 0; i < get_method_n_params(st); i++) {
469 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
472 for (i = 0; i < get_method_n_ress(st); i++) {
473 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
478 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
479 m = alloca(sizeof(entity *) * get_union_n_members(st));
480 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
481 /* First sort the members of lt */
482 for (i = 0; i < get_union_n_members(st); i++) {
483 entity *se = get_union_member(st, i);
484 for (j = 0; j < get_union_n_members(lt); j++) {
485 entity *le = get_union_member(lt, j);
486 if (get_entity_name(le) == get_entity_name(se))
490 for (i = 0; i < get_union_n_members(st); i++) {
491 if (!m[i] || /* Found no counterpart */
492 !smaller_type(get_entity_type(get_union_member(st, i)),
493 get_entity_type(m[i])))
498 type *set, *let; /* small/large elt. type */
499 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
501 set = get_array_element_type(st);
502 let = get_array_element_type(lt);
504 /* If the elt types are different, set must be convertible
505 to let, and they must have the same size so that address
506 computations work out. To have a size the layout must
508 if ((get_type_state(set) != layout_fixed) ||
509 (get_type_state(let) != layout_fixed))
511 if (!smaller_type(set, let) ||
512 get_type_size(set) != get_type_size(let))
515 for(i = 0; i < get_array_n_dimensions(st); i++) {
516 if (get_array_lower_bound(lt, i))
517 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
519 if (get_array_upper_bound(lt, i))
520 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
524 case tpo_enumeration: {
525 assert(0 && "enumerations not implemented");
528 if (!smaller_type(get_pointer_points_to_type(st),
529 get_pointer_points_to_type(lt)))
532 case tpo_primitive: {
533 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
541 /*******************************************************************/
543 /*******************************************************************/
545 /* create a new class type */
546 INLINE type *new_type_class (ident *name) {
549 res = new_type(type_class, NULL, name);
551 res->attr.ca.members = NEW_ARR_F (entity *, 1);
552 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
553 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
554 res->attr.ca.peculiarity = existent;
555 res->attr.ca.dfn = 0;
559 type *new_d_type_class (ident *name, dbg_info* db) {
560 type *res = new_type_class (name);
561 set_type_dbg_info(res, db);
564 INLINE void free_class_attrs(type *clss) {
565 assert(clss && (clss->type_op == type_class));
566 DEL_ARR_F(clss->attr.ca.members);
567 DEL_ARR_F(clss->attr.ca.subtypes);
568 DEL_ARR_F(clss->attr.ca.supertypes);
571 /* manipulate private fields of class type */
572 void add_class_member (type *clss, entity *member) {
573 assert(clss && (clss->type_op == type_class));
574 ARR_APP1 (entity *, clss->attr.ca.members, member);
576 int get_class_n_members (type *clss) {
577 assert(clss && (clss->type_op == type_class));
578 return (ARR_LEN (clss->attr.ca.members))-1;
580 int get_class_member_index(type *clss, entity *mem) {
582 assert(clss && (clss->type_op == type_class));
583 for (i = 0; i < get_class_n_members(clss); i++)
584 if (get_class_member(clss, i) == mem)
588 entity *get_class_member (type *clss, int pos) {
589 assert(clss && (clss->type_op == type_class));
590 assert(pos >= 0 && pos < get_class_n_members(clss));
591 return clss->attr.ca.members[pos+1];
593 void set_class_member (type *clss, entity *member, int pos) {
594 assert(clss && (clss->type_op == type_class));
595 assert(pos >= 0 && pos < get_class_n_members(clss));
596 clss->attr.ca.members[pos+1] = member;
598 void set_class_members (type *clss, entity **members, int arity) {
600 assert(clss && (clss->type_op == type_class));
601 DEL_ARR_F(clss->attr.ca.members);
602 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
603 for (i = 0; i < arity; i++) {
604 set_entity_owner(members[i], clss);
605 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
608 void remove_class_member(type *clss, entity *member) {
610 assert(clss && (clss->type_op == type_class));
611 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
612 if (clss->attr.ca.members[i] == member) {
613 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
614 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
615 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
621 void add_class_subtype (type *clss, type *subtype) {
623 assert(clss && (clss->type_op == type_class));
624 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
625 for (i = 0; i < get_class_n_supertypes(subtype); i++)
626 if (get_class_supertype(subtype, i) == clss)
627 /* Class already registered */
629 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
631 int get_class_n_subtypes (type *clss) {
632 assert(clss && (clss->type_op == type_class));
633 return (ARR_LEN (clss->attr.ca.subtypes))-1;
635 type *get_class_subtype (type *clss, int pos) {
636 assert(clss && (clss->type_op == type_class));
637 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
638 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
640 void set_class_subtype (type *clss, type *subtype, int pos) {
641 assert(clss && (clss->type_op == type_class));
642 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
643 clss->attr.ca.subtypes[pos+1] = subtype;
645 void remove_class_subtype(type *clss, type *subtype) {
647 assert(clss && (clss->type_op == type_class));
648 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
649 if (clss->attr.ca.subtypes[i] == subtype) {
650 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
651 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
652 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
657 void add_class_supertype (type *clss, type *supertype) {
659 assert(clss && (clss->type_op == type_class));
660 assert(supertype && (supertype -> type_op == type_class));
661 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
662 for (i = 0; i < get_class_n_subtypes(supertype); i++)
663 if (get_class_subtype(supertype, i) == clss)
664 /* Class already registered */
666 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
668 int get_class_n_supertypes (type *clss) {
669 assert(clss && (clss->type_op == type_class));
670 return (ARR_LEN (clss->attr.ca.supertypes))-1;
672 int get_class_supertype_index(type *clss, type *super_clss) {
674 assert(clss && (clss->type_op == type_class));
675 assert(super_clss && (super_clss->type_op == type_class));
676 for (i = 0; i < get_class_n_supertypes(clss); i++)
677 if (get_class_supertype(clss, i) == super_clss)
681 type *get_class_supertype (type *clss, int pos) {
682 assert(clss && (clss->type_op == type_class));
683 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
684 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
686 void set_class_supertype (type *clss, type *supertype, int pos) {
687 assert(clss && (clss->type_op == type_class));
688 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
689 clss->attr.ca.supertypes[pos+1] = supertype;
691 void remove_class_supertype(type *clss, type *supertype) {
693 assert(clss && (clss->type_op == type_class));
694 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
695 if (clss->attr.ca.supertypes[i] == supertype) {
696 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
697 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
698 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
703 INLINE peculiarity get_class_peculiarity (type *clss) {
704 assert(clss && (clss->type_op == type_class));
705 return clss->attr.ca.peculiarity;
707 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
708 assert(clss && (clss->type_op == type_class));
709 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
710 clss->attr.ca.peculiarity = pec;
713 void set_class_dfn (type *clss, int dfn)
715 clss->attr.ca.dfn = dfn;
718 int get_class_dfn (type *clss)
720 return (clss->attr.ca.dfn);
724 bool is_class_type(type *clss) {
726 if (clss->type_op == type_class) return 1; else return 0;
729 bool is_subclass_of(type *low, type *high) {
731 assert(is_class_type(low) && is_class_type(high));
732 if (low == high) return true;
733 /* depth first search from high downwards. */
734 for (i = 0; i < get_class_n_subtypes(high); i++) {
735 if (low == get_class_subtype(high, i))
737 if (is_subclass_of(low, get_class_subtype(high, i)))
743 /*******************************************************************/
745 /*******************************************************************/
747 /* create a new type struct */
748 INLINE type *new_type_struct (ident *name) {
750 res = new_type(type_struct, NULL, name);
751 res->attr.sa.members = NEW_ARR_F (entity *, 1);
754 type *new_d_type_struct (ident *name, dbg_info* db) {
755 type *res = new_type_struct (name);
756 set_type_dbg_info(res, db);
759 INLINE void free_struct_attrs (type *strct) {
760 assert(strct && (strct->type_op == type_struct));
761 DEL_ARR_F(strct->attr.sa.members);
764 /* manipulate private fields of struct */
765 int get_struct_n_members (type *strct) {
766 assert(strct && (strct->type_op == type_struct));
767 return (ARR_LEN (strct->attr.sa.members))-1;
769 void add_struct_member (type *strct, entity *member) {
770 assert(strct && (strct->type_op == type_struct));
771 assert(get_type_tpop(get_entity_type(member)) != type_method);
772 /* @@@ lowerfirm geht nicht durch */
773 ARR_APP1 (entity *, strct->attr.sa.members, member);
775 entity *get_struct_member (type *strct, int pos) {
776 assert(strct && (strct->type_op == type_struct));
777 assert(pos >= 0 && pos < get_struct_n_members(strct));
778 return strct->attr.sa.members[pos+1];
780 void set_struct_member (type *strct, int pos, entity *member) {
781 assert(strct && (strct->type_op == type_struct));
782 assert(pos >= 0 && pos < get_struct_n_members(strct));
783 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
784 strct->attr.sa.members[pos+1] = member;
786 void remove_struct_member(type *strct, entity *member) {
788 assert(strct && (strct->type_op == type_struct));
789 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
790 if (strct->attr.sa.members[i] == member) {
791 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
792 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
793 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
799 bool is_struct_type(type *strct) {
801 if (strct->type_op == type_struct) return 1; else return 0;
804 /*******************************************************************/
806 /*******************************************************************/
808 /* Create a new method type.
809 N_param is the number of parameters, n_res the number of results. */
810 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
812 res = new_type(type_method, mode_P, name);
813 res->state = layout_fixed;
814 res->size = get_mode_size(mode_P);
815 res->attr.ma.n_params = n_param;
816 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
817 res->attr.ma.n_res = n_res;
818 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
819 res->attr.ma.variadicity = non_variadic;
823 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
824 type *res = new_type_method (name, n_param, n_res);
825 set_type_dbg_info(res, db);
828 INLINE void free_method_attrs(type *method) {
829 assert(method && (method->type_op == type_method));
830 free(method->attr.ma.param_type);
831 free(method->attr.ma.res_type);
833 /* manipulate private fields of method. */
834 int get_method_n_params (type *method) {
835 assert(method && (method->type_op == type_method));
836 return method->attr.ma.n_params;
838 type *get_method_param_type(type *method, int pos) {
839 assert(method && (method->type_op == type_method));
840 assert(pos >= 0 && pos < get_method_n_params(method));
841 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
843 void set_method_param_type(type *method, int pos, type* tp) {
844 assert(method && (method->type_op == type_method));
845 assert(pos >= 0 && pos < get_method_n_params(method));
846 method->attr.ma.param_type[pos] = tp;
849 int get_method_n_ress (type *method) {
850 assert(method && (method->type_op == type_method));
851 return method->attr.ma.n_res;
853 type *get_method_res_type(type *method, int pos) {
854 assert(method && (method->type_op == type_method));
855 assert(pos >= 0 && pos < get_method_n_ress(method));
856 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
858 void set_method_res_type(type *method, int pos, type* tp) {
859 assert(method && (method->type_op == type_method));
860 assert(pos >= 0 && pos < get_method_n_ress(method));
861 method->attr.ma.res_type[pos] = tp;
864 variadicity get_method_variadicity(type *method)
866 assert(method && (method->type_op == type_method));
867 return method->attr.ma.variadicity;
870 void set_method_variadicity(type *method, variadicity vari)
872 assert(method && (method->type_op == type_method));
873 method->attr.ma.variadicity = vari;
877 bool is_method_type (type *method) {
879 if (method->type_op == type_method) return 1; else return 0;
882 /*******************************************************************/
884 /*******************************************************************/
886 /* create a new type uni */
887 INLINE type *new_type_union (ident *name) {
889 res = new_type(type_union, NULL, name);
890 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
891 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
892 res->attr.ua.members = NEW_ARR_F (entity *, 1);
895 type *new_d_type_union (ident *name, dbg_info* db) {
896 type *res = new_type_union (name);
897 set_type_dbg_info(res, db);
900 INLINE void free_union_attrs (type *uni) {
901 assert(uni && (uni->type_op == type_union));
902 DEL_ARR_F(uni->attr.ua.members);
904 /* manipulate private fields of union */
906 int get_union_n_types (type *uni) {
907 assert(uni && (uni->type_op == type_union));
908 return uni->attr.ua.n_types;
910 type *get_union_unioned_type (type *uni, int pos) {
911 assert(uni && (uni->type_op == type_union));
912 assert(pos >= 0 && pos < get_union_n_types(uni));
913 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
915 void set_union_unioned_type (type *uni, int pos, type *tp) {
916 assert(uni && (uni->type_op == type_union));
917 assert(pos >= 0 && pos < get_union_n_types(uni));
918 uni->attr.ua.unioned_type[pos] = tp;
920 ident *get_union_delim_nameid (type *uni, int pos) {
921 assert(uni && (uni->type_op == type_union));
922 assert(pos >= 0 && pos < get_union_n_types(uni));
923 return uni->attr.ua.delim_names[pos];
925 const char *get_union_delim_name (type *uni, int pos) {
926 assert(uni && (uni->type_op == type_union));
927 assert(pos >= 0 && pos < get_union_n_types(uni));
928 return id_to_str(uni->attr.ua.delim_names[pos]);
930 void set_union_delim_nameid (type *uni, int pos, ident *id) {
931 assert(uni && (uni->type_op == type_union));
932 assert(pos >= 0 && pos < get_union_n_types(uni));
933 uni->attr.ua.delim_names[pos] = id;
936 int get_union_n_members (type *uni) {
937 assert(uni && (uni->type_op == type_union));
938 return (ARR_LEN (uni->attr.ua.members))-1;
940 void add_union_member (type *uni, entity *member) {
941 assert(uni && (uni->type_op == type_union));
942 ARR_APP1 (entity *, uni->attr.ua.members, member);
944 entity *get_union_member (type *uni, int pos) {
945 assert(uni && (uni->type_op == type_union));
946 assert(pos >= 0 && pos < get_union_n_members(uni));
947 return uni->attr.ua.members[pos+1];
949 void set_union_member (type *uni, int pos, entity *member) {
950 assert(uni && (uni->type_op == type_union));
951 assert(pos >= 0 && pos < get_union_n_members(uni));
952 uni->attr.ua.members[pos+1] = member;
954 void remove_union_member(type *uni, entity *member) {
956 assert(uni && (uni->type_op == type_union));
957 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
958 if (uni->attr.ua.members[i] == member) {
959 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
960 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
961 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
967 bool is_union_type (type *uni) {
969 if (uni->type_op == type_union) return 1; else return 0;
972 /*******************************************************************/
974 /*******************************************************************/
977 /* create a new type array -- set dimension sizes independently */
978 INLINE type *new_type_array (ident *name, int n_dimensions,
979 type *element_type) {
982 assert(!is_method_type(element_type));
983 res = new_type(type_array, NULL, name);
984 res->attr.aa.n_dimensions = n_dimensions;
985 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
986 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
987 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
989 for (i = 0; i < n_dimensions; i++) {
990 res->attr.aa.lower_bound[i] = NULL;
991 res->attr.aa.upper_bound[i] = NULL;
992 res->attr.aa.order[i] = i;
994 res->attr.aa.element_type = element_type;
995 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
999 type *new_d_type_array (ident *name, int n_dimensions,
1000 type *element_type, dbg_info* db) {
1001 type *res = new_type_array (name, n_dimensions, element_type);
1002 set_type_dbg_info(res, db);
1006 INLINE void free_array_attrs (type *array) {
1007 assert(array && (array->type_op == type_array));
1008 free(array->attr.aa.lower_bound);
1009 free(array->attr.aa.upper_bound);
1012 /* manipulate private fields of array type */
1013 int get_array_n_dimensions (type *array) {
1014 assert(array && (array->type_op == type_array));
1015 return array->attr.aa.n_dimensions;
1019 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1020 ir_node * upper_bound) {
1021 assert(array && (array->type_op == type_array));
1022 array->attr.aa.lower_bound[dimension] = lower_bound;
1023 array->attr.aa.upper_bound[dimension] = upper_bound;
1026 set_array_bounds_int (type *array, int dimension, int lower_bound,
1028 ir_graph *rem = current_ir_graph;
1029 current_ir_graph = get_const_code_irg();
1030 set_array_bounds (array, dimension,
1031 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)),
1032 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1033 current_ir_graph = rem;
1036 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1037 assert(array && (array->type_op == type_array));
1038 array->attr.aa.lower_bound[dimension] = lower_bound;
1040 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1041 ir_graph *rem = current_ir_graph;
1042 current_ir_graph = get_const_code_irg();
1043 set_array_lower_bound (array, dimension,
1044 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)));
1045 current_ir_graph = rem;
1048 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1049 assert(array && (array->type_op == type_array));
1050 array->attr.aa.upper_bound[dimension] = upper_bound;
1052 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1053 ir_graph *rem = current_ir_graph;
1054 current_ir_graph = get_const_code_irg();
1055 set_array_upper_bound (array, dimension,
1056 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
1057 current_ir_graph = rem;
1059 ir_node * get_array_lower_bound (type *array, int dimension) {
1060 assert(array && (array->type_op == type_array));
1061 return array->attr.aa.lower_bound[dimension];
1063 ir_node * get_array_upper_bound (type *array, int dimension) {
1064 assert(array && (array->type_op == type_array));
1065 return array->attr.aa.upper_bound[dimension];
1068 void set_array_order (type *array, int dimension, int order) {
1069 assert(array && (array->type_op == type_array));
1070 array->attr.aa.order[dimension] = order;
1072 int get_array_order (type *array, int dimension) {
1073 assert(array && (array->type_op == type_array));
1074 return array->attr.aa.order[dimension];
1077 void set_array_element_type (type *array, type *tp) {
1078 assert(array && (array->type_op == type_array));
1079 assert(!is_method_type(tp));
1080 array->attr.aa.element_type = tp;
1082 type *get_array_element_type (type *array) {
1083 assert(array && (array->type_op == type_array));
1084 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1087 void set_array_element_entity (type *array, entity *ent) {
1088 assert(array && (array->type_op == type_array));
1089 assert((get_entity_type(ent)->type_op != type_method));
1090 array->attr.aa.element_ent = ent;
1091 array->attr.aa.element_type = get_entity_type(ent);
1093 entity *get_array_element_entity (type *array) {
1094 assert(array && (array->type_op == type_array));
1095 return array->attr.aa.element_ent;
1099 bool is_array_type (type *array) {
1101 if (array->type_op == type_array) return 1; else return 0;
1104 /*******************************************************************/
1105 /** TYPE_ENUMERATION **/
1106 /*******************************************************************/
1108 /* create a new type enumeration -- set the enumerators independently */
1109 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1112 res = new_type(type_enumeration, NULL, name);
1113 res->attr.ea.n_enums = n_enums;
1114 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1115 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1116 for (i = 0; i < n_enums; i++) {
1117 res->attr.ea.enumer[i] = NULL;
1118 res->attr.ea.enum_nameid = NULL;
1122 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1123 type *res = new_type_enumeration (name, n_enums);
1124 set_type_dbg_info(res, db);
1128 INLINE void free_enumeration_attrs(type *enumeration) {
1129 assert(enumeration && (enumeration->type_op == type_enumeration));
1130 free(enumeration->attr.ea.enumer);
1131 free(enumeration->attr.ea.enum_nameid);
1134 /* manipulate fields of enumeration type. */
1135 int get_enumeration_n_enums (type *enumeration) {
1136 assert(enumeration && (enumeration->type_op == type_enumeration));
1137 return enumeration->attr.ea.n_enums;
1139 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1140 assert(enumeration && (enumeration->type_op == type_enumeration));
1141 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1142 enumeration->attr.ea.enumer[pos] = con;
1144 tarval *get_enumeration_enum (type *enumeration, int pos) {
1145 assert(enumeration && (enumeration->type_op == type_enumeration));
1146 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1147 return enumeration->attr.ea.enumer[pos];
1149 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1150 assert(enumeration && (enumeration->type_op == type_enumeration));
1151 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1152 enumeration->attr.ea.enum_nameid[pos] = id;
1154 ident *get_enumeration_nameid (type *enumeration, int pos) {
1155 assert(enumeration && (enumeration->type_op == type_enumeration));
1156 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1157 return enumeration->attr.ea.enum_nameid[pos];
1159 const char *get_enumeration_name(type *enumeration, int pos) {
1160 assert(enumeration && (enumeration->type_op == type_enumeration));
1161 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1162 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1166 bool is_enumeration_type (type *enumeration) {
1167 assert(enumeration);
1168 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1171 /*******************************************************************/
1172 /** TYPE_POINTER **/
1173 /*******************************************************************/
1175 /* Create a new type pointer */
1176 INLINE type *new_type_pointer (ident *name, type *points_to) {
1178 res = new_type(type_pointer, mode_P, name);
1179 res->attr.pa.points_to = points_to;
1180 res->size = get_mode_size(res->mode);
1181 res->state = layout_fixed;
1184 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1185 type *res = new_type_pointer (name, points_to);
1186 set_type_dbg_info(res, db);
1189 INLINE void free_pointer_attrs (type *pointer) {
1190 assert(pointer && (pointer->type_op == type_pointer));
1192 /* manipulate fields of type_pointer */
1193 void set_pointer_points_to_type (type *pointer, type *tp) {
1194 assert(pointer && (pointer->type_op == type_pointer));
1195 pointer->attr.pa.points_to = tp;
1197 type *get_pointer_points_to_type (type *pointer) {
1198 assert(pointer && (pointer->type_op == type_pointer));
1199 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1203 bool is_pointer_type (type *pointer) {
1205 if (pointer->type_op == type_pointer) return 1; else return 0;
1209 /*******************************************************************/
1210 /** TYPE_PRIMITIVE **/
1211 /*******************************************************************/
1213 /* create a new type primitive */
1214 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1216 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1217 res = new_type(type_primitive, mode, name);
1218 res->size = get_mode_size(mode);
1219 res->state = layout_fixed;
1222 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1223 type *res = new_type_primitive (name, mode);
1224 set_type_dbg_info(res, db);
1227 INLINE void free_primitive_attrs (type *primitive) {
1228 assert(primitive && (primitive->type_op == type_primitive));
1232 bool is_primitive_type (type *primitive) {
1233 assert(primitive && primitive->kind == k_type);
1234 if (primitive->type_op == type_primitive) return 1; else return 0;
1237 /*******************************************************************/
1238 /** common functionality **/
1239 /*******************************************************************/
1242 INLINE int is_atomic_type(type *tp) {
1243 assert(tp && tp->kind == k_type);
1244 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1245 is_enumeration_type(tp));
1247 INLINE int is_compound_type(type *tp) {
1248 assert(tp && tp->kind == k_type);
1249 return (is_class_type(tp) || is_struct_type(tp) ||
1250 is_array_type(tp) || is_union_type(tp));