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.
35 # include "typegmod_t.h"
38 /*******************************************************************/
40 /*******************************************************************/
42 unsigned long type_visited;
45 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
49 assert(type_op != type_id);
51 node_size = offsetof (type, attr) + type_op->attr_size;
52 res = (type *) xmalloc (node_size);
53 add_irp_type(res); /* Remember the new type global. */
56 res->type_op = type_op;
59 res->state = layout_undefined;
67 void free_type_attrs(type *tp) {
68 switch(get_type_tpop_code(tp)) {
69 case tpo_class: { free_class_attrs(tp); } break;
70 case tpo_struct: { free_struct_attrs(tp); } break;
71 case tpo_method: { free_method_attrs(tp); } break;
72 case tpo_union: { free_union_attrs(tp); } break;
73 case tpo_array: { free_array_attrs(tp); } break;
74 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
75 case tpo_pointer: { free_pointer_attrs(tp); } break;
76 case tpo_primitive: { free_primitive_attrs(tp); } break;
81 /* set/get the link field */
82 void *get_type_link(type *tp)
88 void set_type_link(type *tp, void *l)
94 tp_op* get_type_tpop(type *tp) {
99 ident* get_type_tpop_nameid(type *tp) {
101 return tp->type_op->name;
104 const char* get_type_tpop_name(type *tp) {
106 return id_to_str(tp->type_op->name);
109 tp_opcode get_type_tpop_code(type *tp) {
111 return tp->type_op->code;
114 ir_mode* get_type_mode(type *tp) {
119 void set_type_mode(type *tp, ir_mode* m) {
122 /* For pointer and primitive size depends on the mode. */
123 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
124 tp->size == get_mode_size(m);
127 ident* get_type_ident(type *tp) {
132 void set_type_ident(type *tp, ident* id) {
137 const char* get_type_name(type *tp) {
139 return id_to_str(tp->name);
142 int get_type_size(type *tp) {
148 set_type_size(type *tp, int size) {
150 /* For pointer and primitive size depends on the mode. */
151 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
156 get_type_state(type *tp) {
162 set_type_state(type *tp, type_state state) {
164 /* For pointer and primitive always fixed. */
165 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive)) {
166 /* @@@ assert that the layout really is fixed!!! */
171 unsigned long get_type_visited(type *tp) {
176 void set_type_visited(type *tp, unsigned long num) {
180 /* Sets visited field in type to type_visited. */
181 void mark_type_visited(type *tp) {
183 assert(tp->visit < type_visited);
184 tp->visit = type_visited;
187 int is_type (void *thing) {
189 if (get_kind(thing) == k_type)
195 /*******************************************************************/
197 /*******************************************************************/
199 /* create a new class type */
200 type *new_type_class (ident *name) {
203 res = new_type(type_class, NULL, name);
205 res->attr.ca.members = NEW_ARR_F (entity *, 1);
206 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
207 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
211 inline void free_class_attrs(type *clss) {
212 assert(clss && (clss->type_op == type_class));
213 DEL_ARR_F(clss->attr.ca.members);
214 DEL_ARR_F(clss->attr.ca.subtypes);
215 DEL_ARR_F(clss->attr.ca.supertypes);
217 /* manipulate private fields of class type */
218 void add_class_member (type *clss, entity *member) {
219 assert(clss && (clss->type_op == type_class));
220 ARR_APP1 (entity *, clss->attr.ca.members, member);
222 int get_class_n_member (type *clss) {
223 assert(clss && (clss->type_op == type_class));
224 return (ARR_LEN (clss->attr.ca.members))-1;
226 entity *get_class_member (type *clss, int pos) {
227 assert(clss && (clss->type_op == type_class));
228 return clss->attr.ca.members[pos+1];
230 void set_class_member (type *clss, entity *member, int pos) {
231 assert(clss && (clss->type_op == type_class));
232 clss->attr.ca.members[pos+1] = member;
234 void remove_class_member(type *clss, entity *member) {
236 assert(clss && (clss->type_op == type_class));
237 for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
238 if (clss->attr.ca.members[i+1] == member) {
239 for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
240 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
241 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
246 void add_class_subtype (type *clss, type *subtype) {
248 assert(clss && (clss->type_op == type_class));
249 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
250 for (i = 0; i < get_class_n_supertype(subtype); i++)
251 if (get_class_supertype(subtype, i) == clss)
252 /* Class already registered */
254 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
256 int get_class_n_subtype (type *clss) {
257 assert(clss && (clss->type_op == type_class));
258 return (ARR_LEN (clss->attr.ca.subtypes))-1;
260 type *get_class_subtype (type *clss, int pos) {
261 assert(clss && (clss->type_op == type_class));
262 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
264 void set_class_subtype (type *clss, type *subtype, int pos) {
265 assert(clss && (clss->type_op == type_class));
266 clss->attr.ca.subtypes[pos+1] = subtype;
268 void remove_class_subtype(type *clss, type *subtype) {
270 assert(clss && (clss->type_op == type_class));
271 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
272 if (clss->attr.ca.subtypes[i+1] == subtype) {
273 for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
274 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
275 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
280 void add_class_supertype (type *clss, type *supertype) {
282 assert(clss && (clss->type_op == type_class));
283 assert(supertype && (supertype -> type_op == type_class));
284 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
285 for (i = 0; i < get_class_n_subtype(supertype); i++)
286 if (get_class_subtype(supertype, i) == clss)
287 /* Class already registered */
289 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
291 int get_class_n_supertype (type *clss) {
292 assert(clss && (clss->type_op == type_class));
293 return (ARR_LEN (clss->attr.ca.supertypes))-1;
295 type *get_class_supertype (type *clss, int pos) {
296 assert(clss && (clss->type_op == type_class));
297 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
299 void set_class_supertype (type *clss, type *supertype, int pos) {
300 assert(clss && (clss->type_op == type_class));
301 clss->attr.ca.supertypes[pos+1] = supertype;
303 void remove_class_supertype(type *clss, type *supertype) {
305 assert(clss && (clss->type_op == type_class));
306 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
307 if (clss->attr.ca.supertypes[i+1] == supertype) {
308 for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
309 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
310 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
315 bool is_class_type(type *clss) {
317 if (clss->type_op == type_class) return 1; else return 0;
320 /*******************************************************************/
322 /*******************************************************************/
324 /* create a new type struct */
325 type *new_type_struct (ident *name) {
327 res = new_type(type_struct, NULL, name);
328 res->attr.sa.members = NEW_ARR_F (entity *, 1);
331 inline void free_struct_attrs (type *strct) {
332 assert(strct && (strct->type_op == type_struct));
333 DEL_ARR_F(strct->attr.sa.members);
335 /* manipulate private fields of struct */
336 void add_struct_member (type *strct, entity *member) {
337 assert(strct && (strct->type_op == type_struct));
338 ARR_APP1 (entity *, strct->attr.sa.members, member);
340 int get_struct_n_member (type *strct) {
341 assert(strct && (strct->type_op == type_struct));
342 return (ARR_LEN (strct->attr.sa.members))-1;
344 entity *get_struct_member (type *strct, int pos) {
345 assert(strct && (strct->type_op == type_struct));
346 return strct->attr.sa.members[pos+1];
348 void set_struct_member (type *strct, int pos, entity *member) {
349 assert(strct && (strct->type_op == type_struct));
350 strct->attr.sa.members[pos+1] = member;
352 void remove_struct_member(type *strct, entity *member) {
354 assert(strct && (strct->type_op == type_struct));
355 for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
356 if (strct->attr.sa.members[i+1] == member) {
357 for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
358 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
359 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
364 bool is_struct_type(type *strct) {
366 if (strct->type_op == type_struct) return 1; else return 0;
369 /*******************************************************************/
371 /*******************************************************************/
373 /* Create a new method type.
374 N_param is the number of parameters, n_res the number of results. */
375 type *new_type_method (ident *name, int n_param, int n_res) {
377 res = new_type(type_method, NULL, name);
378 res->attr.ma.n_params = n_param;
379 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
380 res->attr.ma.n_res = n_res;
381 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
384 inline void free_method_attrs(type *method) {
385 assert(method && (method->type_op == type_method));
386 free(method->attr.ma.param_type);
387 free(method->attr.ma.res_type);
389 /* manipulate private fields of method. */
390 int get_method_n_params (type *method) {
391 assert(method && (method->type_op == type_method));
392 return method->attr.ma.n_params;
394 type *get_method_param_type(type *method, int pos) {
395 assert(method && (method->type_op == type_method));
396 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
398 void set_method_param_type(type *method, int pos, type* type) {
399 assert(method && (method->type_op == type_method));
400 method->attr.ma.param_type[pos] = type;
403 int get_method_n_res (type *method) {
404 assert(method && (method->type_op == type_method));
405 return method->attr.ma.n_res;
407 type *get_method_res_type(type *method, int pos) {
408 assert(method && (method->type_op == type_method));
409 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
411 void set_method_res_type(type *method, int pos, type* type) {
412 assert(method && (method->type_op == type_method));
413 method->attr.ma.res_type[pos] = type;
417 bool is_method_type (type *method) {
419 if (method->type_op == type_method) return 1; else return 0;
423 /*******************************************************************/
425 /*******************************************************************/
427 /* create a new type uni */
428 type *new_type_uni (ident *name) {
430 res = new_type(type_union, NULL, name);
431 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
432 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
433 res->attr.ua.members = NEW_ARR_F (entity *, 1);
436 inline void free_union_attrs (type *uni) {
437 assert(uni && (uni->type_op == type_union));
438 DEL_ARR_F(uni->attr.ua.members);
440 /* manipulate private fields of struct */
442 int get_union_n_types (type *uni) {
443 assert(uni && (uni->type_op == type_union));
444 return uni->attr.ua.n_types;
446 type *get_union_unioned_type (type *uni, int pos) {
447 assert(uni && (uni->type_op == type_union));
448 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
450 void set_union_unioned_type (type *uni, int pos, type *type) {
451 assert(uni && (uni->type_op == type_union));
452 uni->attr.ua.unioned_type[pos] = type;
454 ident *get_union_delim_nameid (type *uni, int pos) {
455 assert(uni && (uni->type_op == type_union));
456 return uni->attr.ua.delim_names[pos];
458 const char *get_union_delim_name (type *uni, int pos) {
459 assert(uni && (uni->type_op == type_union));
460 return id_to_str(uni->attr.ua.delim_names[pos]);
462 void set_union_delim_nameid (type *uni, int pos, ident *id) {
463 assert(uni && (uni->type_op == type_union));
464 uni->attr.ua.delim_names[pos] = id;
467 int get_union_n_members (type *uni) {
468 assert(uni && (uni->type_op == type_union));
469 return (ARR_LEN (uni->attr.ua.members))-1;
471 void add_union_member (type *uni, entity *member) {
472 assert(uni && (uni->type_op == type_union));
473 ARR_APP1 (entity *, uni->attr.ua.members, member);
475 entity *get_union_member (type *uni, int pos) {
476 assert(uni && (uni->type_op == type_union));
477 return uni->attr.ua.members[pos+1];
479 void set_union_member (type *uni, int pos, entity *member) {
480 assert(uni && (uni->type_op == type_union));
481 uni->attr.ua.members[pos+1] = member;
483 void remove_union_member(type *uni, entity *member) {
485 assert(uni && (uni->type_op == type_union));
486 for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
487 if (uni->attr.ua.members[i+1] == member) {
488 for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
489 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
490 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
496 bool is_union_type (type *uni) {
498 if (uni->type_op == type_union) return 1; else return 0;
501 /*******************************************************************/
503 /*******************************************************************/
506 /* create a new type array -- set dimension sizes independently */
507 type *new_type_array (ident *name, int n_dimensions,
508 type *element_type) {
510 res = new_type(type_array, NULL, name);
511 res->attr.aa.n_dimensions = n_dimensions;
512 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
513 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
514 res->attr.aa.element_type = element_type;
515 new_entity(res, name, element_type);
518 inline void free_array_attrs (type *array) {
519 assert(array && (array->type_op == type_array));
520 free(array->attr.aa.lower_bound);
521 free(array->attr.aa.upper_bound);
524 /* manipulate private fields of array type */
525 int get_array_n_dimensions (type *array) {
526 assert(array && (array->type_op == type_array));
527 return array->attr.aa.n_dimensions;
529 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
530 ir_node * upper_bound) {
531 assert(array && (array->type_op == type_array));
532 array->attr.aa.lower_bound[dimension] = lower_bound;
533 array->attr.aa.upper_bound[dimension] = upper_bound;
535 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
536 assert(array && (array->type_op == type_array));
537 array->attr.aa.lower_bound[dimension] = lower_bound;
539 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
540 assert(array && (array->type_op == type_array));
541 array->attr.aa.upper_bound[dimension] = upper_bound;
543 ir_node * get_array_lower_bound (type *array, int dimension) {
544 assert(array && (array->type_op == type_array));
545 return array->attr.aa.lower_bound[dimension];
547 ir_node * get_array_upper_bound (type *array, int dimension) {
548 assert(array && (array->type_op == type_array));
549 return array->attr.aa.upper_bound[dimension];
551 void set_array_element_type (type *array, type *type) {
552 assert(array && (array->type_op == type_array));
553 array->attr.aa.element_type = type;
555 type *get_array_element_type (type *array) {
556 assert(array && (array->type_op == type_array));
557 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
559 void set_array_element_entity (type *array, entity *ent) {
560 assert(array && (array->type_op == type_array));
561 array->attr.aa.element_ent = ent;
563 entity *get_array_element_entity (type *array) {
564 assert(array && (array->type_op == type_array));
565 return array->attr.aa.element_ent;
569 bool is_array_type (type *array) {
571 if (array->type_op == type_array) return 1; else return 0;
574 /*******************************************************************/
575 /** TYPE_ENUMERATION **/
576 /*******************************************************************/
578 /* create a new type enumeration -- set the enumerators independently */
579 type *new_type_enumeration (ident *name, int n_enums) {
581 res = new_type(type_enumeration, NULL, name);
582 res->attr.ea.n_enums = n_enums;
583 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
584 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
587 inline void free_enumeration_attrs(type *enumeration) {
588 assert(enumeration && (enumeration->type_op == type_enumeration));
589 free(enumeration->attr.ea.enumer);
590 free(enumeration->attr.ea.enum_nameid);
593 /* manipulate fields of enumeration type. */
594 int get_enumeration_n_enums (type *enumeration) {
595 assert(enumeration && (enumeration->type_op == type_enumeration));
596 return enumeration->attr.ea.n_enums;
598 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
599 assert(enumeration && (enumeration->type_op == type_enumeration));
600 enumeration->attr.ea.enumer[pos] = con;
602 tarval *get_enumeration_enum (type *enumeration, int pos) {
603 assert(enumeration && (enumeration->type_op == type_enumeration));
604 return enumeration->attr.ea.enumer[pos];
606 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
607 assert(enumeration && (enumeration->type_op == type_enumeration));
608 enumeration->attr.ea.enum_nameid[pos] = id;
610 ident *get_enumeration_nameid (type *enumeration, int pos) {
611 assert(enumeration && (enumeration->type_op == type_enumeration));
612 return enumeration->attr.ea.enum_nameid[pos];
614 const char *get_enumeration_name(type *enumeration, int pos) {
615 assert(enumeration && (enumeration->type_op == type_enumeration));
616 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
620 bool is_enumeration_type (type *enumeration) {
622 if (enumeration->type_op == type_enumeration) return 1; else return 0;
625 /*******************************************************************/
627 /*******************************************************************/
629 /* Create a new type pointer */
630 type *new_type_pointer (ident *name, type *points_to) {
632 res = new_type(type_pointer, mode_p, name);
633 res->attr.pa.points_to = points_to;
634 res->size = get_mode_size(res->mode);
635 res->state = layout_fixed;
638 inline void free_pointer_attrs (type *pointer) {
639 assert(pointer && (pointer->type_op == type_pointer));
641 /* manipulate fields of type_pointer */
642 void set_pointer_points_to_type (type *pointer, type *type) {
643 assert(pointer && (pointer->type_op == type_pointer));
644 pointer->attr.pa.points_to = type;
646 type *get_pointer_points_to_type (type *pointer) {
647 assert(pointer && (pointer->type_op == type_pointer));
648 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
652 bool is_pointer_type (type *pointer) {
654 if (pointer->type_op == type_pointer) return 1; else return 0;
658 /*******************************************************************/
659 /** TYPE_PRIMITIVE **/
660 /*******************************************************************/
662 /* create a new type primitive */
663 type *new_type_primitive (ident *name, ir_mode *mode) {
665 res = new_type(type_primitive, mode, name);
666 res->size = get_mode_size(mode);
667 res->state = layout_fixed;
670 inline void free_primitive_attrs (type *primitive) {
671 assert(primitive && (primitive->type_op == type_primitive));
675 bool is_primitive_type (type *primitive) {
677 if (primitive->type_op == type_primitive) return 1; else return 0;