4 * file type.c - implementation of the datastructure to hold
7 * (C) 2001 by Universitaet Karlsruhe
9 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
12 * This module supplies a datastructure to represent all types
13 * known in the compiled program. This includes types specified
14 * in the program as well as types defined by the language. In the
15 * view of the intermediate representation there is no difference
16 * between these types.
18 * There exist several kinds of types, arranged by the structure of
19 * the type. A type is described by a set of attributes. Some of
20 * these attributes are common to all types, others depend on the
23 * Types are different from the modes defined in irmode: Types are
24 * on the level of the programming language, modes at the level of
25 * the target processor.
38 # include "typegmod_t.h"
44 /*******************************************************************/
46 /*******************************************************************/
48 unsigned long type_visited;
51 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
55 assert(type_op != type_id);
57 node_size = offsetof (type, attr) + type_op->attr_size;
58 res = (type *) xmalloc (node_size);
59 add_irp_type(res); /* Remember the new type global. */
62 res->type_op = type_op;
65 res->state = layout_undefined;
73 void free_type_attrs(type *tp) {
74 switch(get_type_tpop_code(tp)) {
75 case tpo_class: { free_class_attrs(tp); } break;
76 case tpo_struct: { free_struct_attrs(tp); } break;
77 case tpo_method: { free_method_attrs(tp); } break;
78 case tpo_union: { free_union_attrs(tp); } break;
79 case tpo_array: { free_array_attrs(tp); } break;
80 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
81 case tpo_pointer: { free_pointer_attrs(tp); } break;
82 case tpo_primitive: { free_primitive_attrs(tp); } break;
87 /* set/get the link field */
88 void *get_type_link(type *tp)
94 void set_type_link(type *tp, void *l)
100 tp_op* get_type_tpop(type *tp) {
105 ident* get_type_tpop_nameid(type *tp) {
107 return tp->type_op->name;
110 const char* get_type_tpop_name(type *tp) {
112 return id_to_str(tp->type_op->name);
115 tp_opcode get_type_tpop_code(type *tp) {
117 return tp->type_op->code;
120 ir_mode* get_type_mode(type *tp) {
125 void set_type_mode(type *tp, ir_mode* m) {
128 /* For pointer and primitive size depends on the mode. */
129 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
130 tp->size == get_mode_size(m);
133 ident* get_type_ident(type *tp) {
138 void set_type_ident(type *tp, ident* id) {
143 const char* get_type_name(type *tp) {
145 return id_to_str(tp->name);
148 int get_type_size(type *tp) {
154 set_type_size(type *tp, int size) {
156 /* For pointer and primitive size depends on the mode. */
157 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
162 get_type_state(type *tp) {
168 set_type_state(type *tp, type_state state) {
170 /* For pointer and primitive always fixed. */
171 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive)) {
172 /* @@@ assert that the layout really is fixed!!! */
177 unsigned long get_type_visited(type *tp) {
182 void set_type_visited(type *tp, unsigned long num) {
186 /* Sets visited field in type to type_visited. */
187 void mark_type_visited(type *tp) {
189 assert(tp->visit < type_visited);
190 tp->visit = type_visited;
193 int is_type (void *thing) {
195 if (get_kind(thing) == k_type)
201 /*******************************************************************/
203 /*******************************************************************/
205 /* create a new class type */
206 type *new_type_class (ident *name) {
209 res = new_type(type_class, NULL, name);
211 res->attr.ca.members = NEW_ARR_F (entity *, 1);
212 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
213 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
217 inline void free_class_attrs(type *clss) {
218 assert(clss && (clss->type_op == type_class));
219 DEL_ARR_F(clss->attr.ca.members);
220 DEL_ARR_F(clss->attr.ca.subtypes);
221 DEL_ARR_F(clss->attr.ca.supertypes);
223 /* manipulate private fields of class type */
224 void add_class_member (type *clss, entity *member) {
225 assert(clss && (clss->type_op == type_class));
226 ARR_APP1 (entity *, clss->attr.ca.members, member);
228 int get_class_n_member (type *clss) {
229 assert(clss && (clss->type_op == type_class));
230 return (ARR_LEN (clss->attr.ca.members))-1;
232 entity *get_class_member (type *clss, int pos) {
233 assert(clss && (clss->type_op == type_class));
234 return clss->attr.ca.members[pos+1];
236 void set_class_member (type *clss, entity *member, int pos) {
237 assert(clss && (clss->type_op == type_class));
238 clss->attr.ca.members[pos+1] = member;
240 void remove_class_member(type *clss, entity *member) {
242 assert(clss && (clss->type_op == type_class));
243 for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
244 if (clss->attr.ca.members[i+1] == member) {
245 for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
246 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
247 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
252 void add_class_subtype (type *clss, type *subtype) {
254 assert(clss && (clss->type_op == type_class));
255 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
256 for (i = 0; i < get_class_n_supertype(subtype); i++)
257 if (get_class_supertype(subtype, i) == clss)
258 /* Class already registered */
260 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
262 int get_class_n_subtype (type *clss) {
263 assert(clss && (clss->type_op == type_class));
264 return (ARR_LEN (clss->attr.ca.subtypes))-1;
266 type *get_class_subtype (type *clss, int pos) {
267 assert(clss && (clss->type_op == type_class));
268 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
270 void set_class_subtype (type *clss, type *subtype, int pos) {
271 assert(clss && (clss->type_op == type_class));
272 clss->attr.ca.subtypes[pos+1] = subtype;
274 void remove_class_subtype(type *clss, type *subtype) {
276 assert(clss && (clss->type_op == type_class));
277 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
278 if (clss->attr.ca.subtypes[i+1] == subtype) {
279 for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
280 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
281 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
286 void add_class_supertype (type *clss, type *supertype) {
288 assert(clss && (clss->type_op == type_class));
289 assert(supertype && (supertype -> type_op == type_class));
290 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
291 for (i = 0; i < get_class_n_subtype(supertype); i++)
292 if (get_class_subtype(supertype, i) == clss)
293 /* Class already registered */
295 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
297 int get_class_n_supertype (type *clss) {
298 assert(clss && (clss->type_op == type_class));
299 return (ARR_LEN (clss->attr.ca.supertypes))-1;
301 type *get_class_supertype (type *clss, int pos) {
302 assert(clss && (clss->type_op == type_class));
303 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
305 void set_class_supertype (type *clss, type *supertype, int pos) {
306 assert(clss && (clss->type_op == type_class));
307 clss->attr.ca.supertypes[pos+1] = supertype;
309 void remove_class_supertype(type *clss, type *supertype) {
311 assert(clss && (clss->type_op == type_class));
312 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
313 if (clss->attr.ca.supertypes[i+1] == supertype) {
314 for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
315 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
316 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
321 bool is_class_type(type *clss) {
323 if (clss->type_op == type_class) return 1; else return 0;
326 /*******************************************************************/
328 /*******************************************************************/
330 /* create a new type struct */
331 type *new_type_struct (ident *name) {
333 res = new_type(type_struct, NULL, name);
334 res->attr.sa.members = NEW_ARR_F (entity *, 1);
337 inline void free_struct_attrs (type *strct) {
338 assert(strct && (strct->type_op == type_struct));
339 DEL_ARR_F(strct->attr.sa.members);
341 /* manipulate private fields of struct */
342 void add_struct_member (type *strct, entity *member) {
343 assert(strct && (strct->type_op == type_struct));
344 ARR_APP1 (entity *, strct->attr.sa.members, member);
346 int get_struct_n_member (type *strct) {
347 assert(strct && (strct->type_op == type_struct));
348 return (ARR_LEN (strct->attr.sa.members))-1;
350 entity *get_struct_member (type *strct, int pos) {
351 assert(strct && (strct->type_op == type_struct));
352 return strct->attr.sa.members[pos+1];
354 void set_struct_member (type *strct, int pos, entity *member) {
355 assert(strct && (strct->type_op == type_struct));
356 strct->attr.sa.members[pos+1] = member;
358 void remove_struct_member(type *strct, entity *member) {
360 assert(strct && (strct->type_op == type_struct));
361 for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
362 if (strct->attr.sa.members[i+1] == member) {
363 for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
364 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
365 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
370 bool is_struct_type(type *strct) {
372 if (strct->type_op == type_struct) return 1; else return 0;
375 /*******************************************************************/
377 /*******************************************************************/
379 /* Create a new method type.
380 N_param is the number of parameters, n_res the number of results. */
381 type *new_type_method (ident *name, int n_param, int n_res) {
383 res = new_type(type_method, NULL, name);
384 res->attr.ma.n_params = n_param;
385 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
386 res->attr.ma.n_res = n_res;
387 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
390 inline void free_method_attrs(type *method) {
391 assert(method && (method->type_op == type_method));
392 free(method->attr.ma.param_type);
393 free(method->attr.ma.res_type);
395 /* manipulate private fields of method. */
396 int get_method_n_params (type *method) {
397 assert(method && (method->type_op == type_method));
398 return method->attr.ma.n_params;
400 type *get_method_param_type(type *method, int pos) {
401 assert(method && (method->type_op == type_method));
402 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
404 void set_method_param_type(type *method, int pos, type* type) {
405 assert(method && (method->type_op == type_method));
406 method->attr.ma.param_type[pos] = type;
409 int get_method_n_res (type *method) {
410 assert(method && (method->type_op == type_method));
411 return method->attr.ma.n_res;
413 type *get_method_res_type(type *method, int pos) {
414 assert(method && (method->type_op == type_method));
415 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
417 void set_method_res_type(type *method, int pos, type* type) {
418 assert(method && (method->type_op == type_method));
419 method->attr.ma.res_type[pos] = type;
423 bool is_method_type (type *method) {
425 if (method->type_op == type_method) return 1; else return 0;
429 /*******************************************************************/
431 /*******************************************************************/
433 /* create a new type uni */
434 type *new_type_uni (ident *name) {
436 res = new_type(type_union, NULL, name);
437 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
438 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
439 res->attr.ua.members = NEW_ARR_F (entity *, 1);
442 inline void free_union_attrs (type *uni) {
443 assert(uni && (uni->type_op == type_union));
444 DEL_ARR_F(uni->attr.ua.members);
446 /* manipulate private fields of struct */
448 int get_union_n_types (type *uni) {
449 assert(uni && (uni->type_op == type_union));
450 return uni->attr.ua.n_types;
452 type *get_union_unioned_type (type *uni, int pos) {
453 assert(uni && (uni->type_op == type_union));
454 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
456 void set_union_unioned_type (type *uni, int pos, type *type) {
457 assert(uni && (uni->type_op == type_union));
458 uni->attr.ua.unioned_type[pos] = type;
460 ident *get_union_delim_nameid (type *uni, int pos) {
461 assert(uni && (uni->type_op == type_union));
462 return uni->attr.ua.delim_names[pos];
464 const char *get_union_delim_name (type *uni, int pos) {
465 assert(uni && (uni->type_op == type_union));
466 return id_to_str(uni->attr.ua.delim_names[pos]);
468 void set_union_delim_nameid (type *uni, int pos, ident *id) {
469 assert(uni && (uni->type_op == type_union));
470 uni->attr.ua.delim_names[pos] = id;
473 int get_union_n_members (type *uni) {
474 assert(uni && (uni->type_op == type_union));
475 return (ARR_LEN (uni->attr.ua.members))-1;
477 void add_union_member (type *uni, entity *member) {
478 assert(uni && (uni->type_op == type_union));
479 ARR_APP1 (entity *, uni->attr.ua.members, member);
481 entity *get_union_member (type *uni, int pos) {
482 assert(uni && (uni->type_op == type_union));
483 return uni->attr.ua.members[pos+1];
485 void set_union_member (type *uni, int pos, entity *member) {
486 assert(uni && (uni->type_op == type_union));
487 uni->attr.ua.members[pos+1] = member;
489 void remove_union_member(type *uni, entity *member) {
491 assert(uni && (uni->type_op == type_union));
492 for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
493 if (uni->attr.ua.members[i+1] == member) {
494 for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
495 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
496 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
502 bool is_union_type (type *uni) {
504 if (uni->type_op == type_union) return 1; else return 0;
507 /*******************************************************************/
509 /*******************************************************************/
512 /* create a new type array -- set dimension sizes independently */
513 type *new_type_array (ident *name, int n_dimensions,
514 type *element_type) {
516 res = new_type(type_array, NULL, name);
517 res->attr.aa.n_dimensions = n_dimensions;
518 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
519 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
520 res->attr.aa.element_type = element_type;
521 new_entity(res, mangle(name, id_from_str("elem_ent", 8)), element_type);
524 inline void free_array_attrs (type *array) {
525 assert(array && (array->type_op == type_array));
526 free(array->attr.aa.lower_bound);
527 free(array->attr.aa.upper_bound);
530 /* manipulate private fields of array type */
531 int get_array_n_dimensions (type *array) {
532 assert(array && (array->type_op == type_array));
533 return array->attr.aa.n_dimensions;
535 void set_array_bounds_int (type *array, int dimension, int lower_bound,
538 assert(array && (array->type_op == type_array));
539 rem = current_ir_graph;
540 current_ir_graph = get_const_code_irg();
541 array->attr.aa.lower_bound[dimension] =
542 new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
543 array->attr.aa.upper_bound[dimension] =
544 new_Const(mode_I, tarval_from_long (mode_I, upper_bound));
545 current_ir_graph = rem;
548 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
549 ir_node * upper_bound) {
550 assert(array && (array->type_op == type_array));
551 array->attr.aa.lower_bound[dimension] = lower_bound;
552 array->attr.aa.upper_bound[dimension] = upper_bound;
554 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
555 assert(array && (array->type_op == type_array));
556 array->attr.aa.lower_bound[dimension] = lower_bound;
558 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
559 assert(array && (array->type_op == type_array));
560 array->attr.aa.upper_bound[dimension] = upper_bound;
562 ir_node * get_array_lower_bound (type *array, int dimension) {
563 assert(array && (array->type_op == type_array));
564 return array->attr.aa.lower_bound[dimension];
566 ir_node * get_array_upper_bound (type *array, int dimension) {
567 assert(array && (array->type_op == type_array));
568 return array->attr.aa.upper_bound[dimension];
570 void set_array_element_type (type *array, type *type) {
571 assert(array && (array->type_op == type_array));
572 array->attr.aa.element_type = type;
574 type *get_array_element_type (type *array) {
575 assert(array && (array->type_op == type_array));
576 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
578 void set_array_element_entity (type *array, entity *ent) {
579 assert(array && (array->type_op == type_array));
580 array->attr.aa.element_ent = ent;
582 entity *get_array_element_entity (type *array) {
583 assert(array && (array->type_op == type_array));
584 return array->attr.aa.element_ent;
588 bool is_array_type (type *array) {
590 if (array->type_op == type_array) return 1; else return 0;
593 /*******************************************************************/
594 /** TYPE_ENUMERATION **/
595 /*******************************************************************/
597 /* create a new type enumeration -- set the enumerators independently */
598 type *new_type_enumeration (ident *name, int n_enums) {
600 res = new_type(type_enumeration, NULL, name);
601 res->attr.ea.n_enums = n_enums;
602 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
603 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
606 inline void free_enumeration_attrs(type *enumeration) {
607 assert(enumeration && (enumeration->type_op == type_enumeration));
608 free(enumeration->attr.ea.enumer);
609 free(enumeration->attr.ea.enum_nameid);
612 /* manipulate fields of enumeration type. */
613 int get_enumeration_n_enums (type *enumeration) {
614 assert(enumeration && (enumeration->type_op == type_enumeration));
615 return enumeration->attr.ea.n_enums;
617 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
618 assert(enumeration && (enumeration->type_op == type_enumeration));
619 enumeration->attr.ea.enumer[pos] = con;
621 tarval *get_enumeration_enum (type *enumeration, int pos) {
622 assert(enumeration && (enumeration->type_op == type_enumeration));
623 return enumeration->attr.ea.enumer[pos];
625 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
626 assert(enumeration && (enumeration->type_op == type_enumeration));
627 enumeration->attr.ea.enum_nameid[pos] = id;
629 ident *get_enumeration_nameid (type *enumeration, int pos) {
630 assert(enumeration && (enumeration->type_op == type_enumeration));
631 return enumeration->attr.ea.enum_nameid[pos];
633 const char *get_enumeration_name(type *enumeration, int pos) {
634 assert(enumeration && (enumeration->type_op == type_enumeration));
635 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
639 bool is_enumeration_type (type *enumeration) {
641 if (enumeration->type_op == type_enumeration) return 1; else return 0;
644 /*******************************************************************/
646 /*******************************************************************/
648 /* Create a new type pointer */
649 type *new_type_pointer (ident *name, type *points_to) {
651 res = new_type(type_pointer, mode_p, name);
652 res->attr.pa.points_to = points_to;
653 res->size = get_mode_size(res->mode);
654 res->state = layout_fixed;
657 inline void free_pointer_attrs (type *pointer) {
658 assert(pointer && (pointer->type_op == type_pointer));
660 /* manipulate fields of type_pointer */
661 void set_pointer_points_to_type (type *pointer, type *type) {
662 assert(pointer && (pointer->type_op == type_pointer));
663 pointer->attr.pa.points_to = type;
665 type *get_pointer_points_to_type (type *pointer) {
666 assert(pointer && (pointer->type_op == type_pointer));
667 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
671 bool is_pointer_type (type *pointer) {
673 if (pointer->type_op == type_pointer) return 1; else return 0;
677 /*******************************************************************/
678 /** TYPE_PRIMITIVE **/
679 /*******************************************************************/
681 /* create a new type primitive */
682 type *new_type_primitive (ident *name, ir_mode *mode) {
684 res = new_type(type_primitive, mode, name);
685 res->size = get_mode_size(mode);
686 res->state = layout_fixed;
689 inline void free_primitive_attrs (type *primitive) {
690 assert(primitive && (primitive->type_op == type_primitive));
694 bool is_primitive_type (type *primitive) {
696 if (primitive->type_op == type_primitive) return 1; else return 0;
699 int is_atomic_type(type *tp) {
700 return (is_primitive_type(tp) || is_pointer_type(tp) ||
701 is_enumeration_type(tp));
703 int is_compound_type(type *tp) {
704 return (is_class_type(tp) || is_struct_type(tp) ||
705 is_array_type(tp) || is_union_type(tp));