1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer &
7 ** type.c: datastructures to hold type information.
11 # include "irprog.h" /* So that constructors can add the type to global
16 unsigned long type_visited = 0;
23 /*******************************************************************/
25 /*******************************************************************/
28 new_type_class (ident *name)//, int members)
32 res = (type_class *) xmalloc (sizeof (type_class));
33 add_irp_type((type *) res); /* Remember the new type global. */
34 res->kind = k_type_class;
37 res->members = NEW_ARR_F (entity *, 1);
38 res->subtypes = NEW_ARR_F (type_class *, 1);
39 res->supertypes = NEW_ARR_F (type_class *, 1);
46 /* manipulate fields of type_class */
48 get_class_name (type_class *class) {
50 return id_to_str(class->name);
55 get_class_ident (type_class *class) {
61 void set_class_name (type_class *class, char *name);
62 void set_class_ident (type_class *class, ident* ident);
67 add_class_member (type_class *class, entity *member)
69 ARR_APP1 (entity *, class->members, member);
73 get_class_member (type_class *class, int pos)
76 return class->members[pos+1];
80 set_class_member (type_class *class, entity *member, int pos)
82 class->members[pos+1] = member;
86 get_class_n_member (type_class *class)
91 res = (ARR_LEN (class->members))-1;
97 add_class_subtype (type_class *class, type_class *subtype)
99 ARR_APP1 (type_class *, class->subtypes, subtype);
103 get_class_subtype (type_class *class, int pos)
106 return class->subtypes[pos+1];
110 set_class_subtype (type_class *class, type_class *subtype, int pos)
112 class->subtypes[pos+1] = subtype;
116 get_class_n_subtype (type_class *class)
119 return (ARR_LEN (class->subtypes))-1;
122 /* field: supertype */
124 add_class_supertype (type_class *class, type_class *supertype)
126 ARR_APP1 (type_class *, class->supertypes, supertype);
130 get_class_supertype (type_class *class, int pos)
133 return class->supertypes[pos+1];
137 set_class_supertype (type_class *class, type_class *supertype, int pos)
139 class->supertypes[pos+1] = supertype;
143 get_class_n_supertype (type_class *class)
146 return (ARR_LEN (class->supertypes))-1;
149 /*******************************************************************/
151 /*******************************************************************/
154 new_type_strct (ident *name)//, int members)
158 res = (type_strct *) xmalloc (sizeof (type_strct));
159 add_irp_type((type *) res); /* Remember the new type global. */
160 res->kind = k_type_strct;
163 res->members = NEW_ARR_F (entity *, 1);
169 /* manipulate fields of type_strct */
172 get_strct_name (type_strct *strct) {
174 return ID_TO_STR(strct->name);
179 get_strct_ident (type_strct *strct) {
185 get_strct_n_member (type_strct *strct)
190 res = (ARR_LEN (strct->members))-1;
195 add_strct_member (type_strct *strct, entity *member)
197 ARR_APP1 (type_strct *, strct->members, member);
201 get_strct_member (type_strct *strct, int pos)
204 return strct->members[pos+1];
208 set_strct_member (type_strct *strct, int pos, entity *member)
210 strct->members[pos+1] = member;
214 void set_strct_name (type_strct *strct, char *name);
215 void set_strct_ident (type_strct *strct, ident* ident);
219 /*******************************************************************/
221 /*******************************************************************/
223 /* create a new type_method */
225 new_type_method (ident *name, int arity, int n_res)
229 res = (type_method *) xmalloc (sizeof (type_method));
230 add_irp_type((type *) res); /* Remember the new type global. */
231 res->kind = k_type_method;
232 res->name = name; // do I need the name, or is the name in entity sufficient?
234 res->param_type = (type **) xmalloc (sizeof (type *) * arity);
236 res->res_type = (type **) xmalloc (sizeof (type *) * n_res);
243 /* manipulate fields of type_method */
245 get_method_name (type_method *method) {
247 return ID_TO_STR(method->name);
251 get_method_ident (type_method *method) {
257 void set_method_name (type_method *method, char *name);
258 void set_method_ident (type_method *method, ident* ident);
263 get_method_n_params (type_method *method) {
264 return method->arity;
268 get_method_arity (type_method *method) {
269 return method->arity;
274 set_method_arity (type_method *method, int arity) {
275 method->arity = arity;
276 / change array size, somehow copy. *
281 get_method_param_type(type_method *method, int pos) {
282 return method->param_type[pos];
286 set_method_param_type(type_method *method, int pos, type* type) {
287 method->param_type[pos] = type;
292 get_method_n_res (type_method *method) {
293 return method->n_res;
298 set_method_n_res (type_method *method, int n_res) {
299 method->n_res = n_res;
304 get_method_res_type(type_method *method, int pos) {
305 return method->res_type[pos];
309 set_method_res_type(type_method *method, int pos, type* type) {
310 method->res_type[pos] = type;
314 /*******************************************************************/
316 /*******************************************************************/
318 /* create a new type_union -- set unioned types by hand. */
320 new_type_union (ident *name, int n_types)
324 res = (type_union *) xmalloc (sizeof (type_union));
325 add_irp_type((type *) res); /* Remember the new type global. */
326 res->kind = k_type_union;
327 res->name = name; // do I need a name?
328 res->n_types = n_types;
330 res->unioned_type = (int *) xmalloc (sizeof (int) * n_types);
338 /* manipulate fields of type_union */
341 get_union_name (type_union *uni) {
343 return ID_TO_STR(uni->name);
348 get_union_ident (type_union *uni) {
354 void set_union_name (type_union *union, char *name);
355 void set_union_ident (type_union *union, ident* ident);
358 int get_union_n_types (type_union *union);
359 void set_union_n_types (type_union *union, int n);
360 type *get_union_unioned_type (type_union *union, int pos);
361 void set_union_unioned_type (type_union *union, int pos, type *type);
364 /*******************************************************************/
366 /*******************************************************************/
368 /* create a new type_array */
370 new_type_array (ident *name, int n_dimensions)
374 res = (type_array *) xmalloc (sizeof (type_array));
375 add_irp_type((type *) res); /* Remember the new type global. */
376 res->kind = k_type_array;
378 res->n_dimensions = n_dimensions;
379 res->lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
380 res->upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
387 /* manipulate fields of type_array */
390 get_array_name (type_array *array) {
392 return ID_TO_STR(array->name);
397 get_array_ident (type_array *array) {
403 void set_array_name (type_array *array, char *name);
404 void set_array_ident (type_array *array, ident* ident);
408 set_array_dimensions (type_array* array, int n) {
409 array->n_dimensions = n;
413 get_array_dimensions (type_array* array) {
414 return array->n_dimensions;
418 set_array_bounds (type_array* array, int dimension, int lower_bound,
420 array->lower_bound[dimension-1] = lower_bound;
421 array->upper_bound[dimension-1] = upper_bound;
425 set_array_lower_bound (type_array* array, int dimension, int lower_bound) {
426 array->lower_bound[dimension-1] = lower_bound;
430 set_array_upper_bound (type_array* array, int dimension, int upper_bound) {
431 array->upper_bound[dimension-1] = upper_bound;
435 get_array_lower_bound (type_array* array, int dimension) {
436 return array->lower_bound[dimension-1];
440 get_array_upper_bound (type_array* array, int dimension) {
441 return array->upper_bound[dimension-1];
444 inline void set_array_element_type (type_array *array, type *type) {
445 array->element_type = type;
449 get_array_element_type (type_array *array) {
450 return array->element_type;
454 /*******************************************************************/
455 /** TYPE_ENUMERATION **/
456 /*******************************************************************/
458 /* create a new type enumeration -- set the enumerators independently */
460 new_type_enumeration (ident *name /* , int n_enums */)
462 type_enumeration *res;
464 res = (type_enumeration *) xmalloc (sizeof (type_enumeration));
465 add_irp_type((type *) res); /* Remember the new type global. */
466 res->kind = k_type_enumeration;
469 res->n_enums = n_enums;
470 res->enum = (int *) xmalloc (sizeof (int) * n_enums);
478 /* manipulate fields of type_enumeration */
481 get_enumeration_name (type_enumeration *enumeration) {
483 return ID_TO_STR(enumeration->name);
488 get_enumeration_ident (type_enumeration *enumeration) {
490 return enumeration->name;
494 void set_enumeration_name (type_enumeration *enumeration, char *name);
495 void set_enumeration_ident (type_enumeration *enumeration, ident* ident);
498 void set_enumeration_n_enums (type_enumeration *enumeration, int n);
499 int *get_enumeration_n_enums (type_enumeration *enumeration);
500 void set_enumeration_enum (type_enumeration *enumeration, int pos,
502 ir_node *get_enumeration_enum (type_enumeration *enumeration, int pos);
506 /*******************************************************************/
508 /*******************************************************************/
510 /* create a new type pointer */
512 new_type_pointer (ident *name, type *points_to)
516 res = (type_pointer *) xmalloc (sizeof (type_pointer));
517 add_irp_type((type *) res); /* Remember the new type global. */
518 res->kind = k_type_pointer;
520 res->points_to = points_to;
527 /* manipulate fields of type_pointer */
530 get_pointer_name (type_pointer *pointer) {
532 return ID_TO_STR(pointer->name);
537 get_pointer_ident (type_pointer *pointer) {
539 return pointer->name;
543 void set_pointer_name (type_pointer *pointer, char *name);
544 void set_pointer_ident (type_pointer *pointer, ident* ident);
548 set_pointer_points_to_type (type_pointer *pointer, type* type) {
549 pointer->points_to = type;
553 get_pointer_points_to_type (type_pointer *pointer) {
554 return pointer->points_to;
558 /*******************************************************************/
559 /** TYPE_PRIMITIVE **/
560 /*******************************************************************/
562 /* create a new type_primitive */
563 inline type_primitive *
564 new_type_primitive (ident *name, ir_mode *mode)
568 res = (type_primitive *) xmalloc (sizeof (type_primitive));
569 add_irp_type((type *) res); /* Remember the new type global. */
570 res->kind = k_type_primitive;
579 /* manipulate fields of type_primitive */
583 get_primitive_name (type_primitive *primitive) {
585 return ID_TO_STR(primitive->name);
590 get_primitive_ident (type_primitive *primitive) {
592 return primitive->name;
595 void set_primitive_name (type_primitive *primitive, char *name);
596 void set_primitive_ident (type_primitive *primitive, ident* ident);
600 get_primitive_mode (type_primitive *primitive) {
601 return primitive->mode;
605 set_primitive_mode (type_primitive *primitive, ir_mode *mode) {
606 primitive->mode = mode;
612 /*******************************************************************/
613 /** To manage all different types the same **/
614 /*******************************************************************/
618 is_type(void *thing) {
621 kind = get_kind(thing);
622 if ( (kind == k_type_class)
623 || (kind == k_type_strct)
624 || (kind == k_type_method)
625 || (kind == k_type_union)
626 || (kind == k_type_array)
627 || (kind == k_type_enumeration)
628 || (kind == k_type_pointer)
629 || (kind == k_type_primitive))