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);
262 get_method_arity (type_method *method) {
263 return method->arity;
268 set_method_arity (type_method *method, int arity) {
269 method->arity = arity;
270 / change array size, somehow copy. *
275 get_method_param_type(type_method *method, int pos) {
276 return method->param_type[pos];
280 set_method_param_type(type_method *method, int pos, type* type) {
281 method->param_type[pos] = type;
286 get_method_n_res (type_method *method) {
287 return method->n_res;
292 set_method_n_res (type_method *method, int n_res) {
293 method->n_res = n_res;
298 get_method_res_type(type_method *method, int pos) {
299 return method->res_type[pos];
303 set_method_res_type(type_method *method, int pos, type* type) {
304 method->res_type[pos] = type;
308 /*******************************************************************/
310 /*******************************************************************/
312 /* create a new type_union -- set unioned types by hand. */
314 new_type_union (ident *name, int n_types)
318 res = (type_union *) xmalloc (sizeof (type_union));
319 add_irp_type((type *) res); /* Remember the new type global. */
320 res->kind = k_type_union;
321 res->name = name; // do I need a name?
322 res->n_types = n_types;
324 res->unioned_type = (int *) xmalloc (sizeof (int) * n_types);
332 /* manipulate fields of type_union */
335 get_union_name (type_union *uni) {
337 return ID_TO_STR(uni->name);
342 get_union_ident (type_union *uni) {
348 void set_union_name (type_union *union, char *name);
349 void set_union_ident (type_union *union, ident* ident);
352 int get_union_n_types (type_union *union);
353 void set_union_n_types (type_union *union, int n);
354 type *get_union_unioned_type (type_union *union, int pos);
355 void set_union_unioned_type (type_union *union, int pos, type *type);
358 /*******************************************************************/
360 /*******************************************************************/
362 /* create a new type_array */
364 new_type_array (ident *name, int n_dimensions)
368 res = (type_array *) xmalloc (sizeof (type_array));
369 add_irp_type((type *) res); /* Remember the new type global. */
370 res->kind = k_type_array;
372 res->n_dimensions = n_dimensions;
373 res->lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
374 res->upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
381 /* manipulate fields of type_array */
384 get_array_name (type_array *array) {
386 return ID_TO_STR(array->name);
391 get_array_ident (type_array *array) {
397 void set_array_name (type_array *array, char *name);
398 void set_array_ident (type_array *array, ident* ident);
402 set_array_dimensions (type_array* array, int n) {
403 array->n_dimensions = n;
407 get_array_dimensions (type_array* array) {
408 return array->n_dimensions;
412 set_array_bounds (type_array* array, int dimension, int lower_bound,
414 array->lower_bound[dimension-1] = lower_bound;
415 array->upper_bound[dimension-1] = upper_bound;
419 set_array_lower_bound (type_array* array, int dimension, int lower_bound) {
420 array->lower_bound[dimension-1] = lower_bound;
424 set_array_upper_bound (type_array* array, int dimension, int upper_bound) {
425 array->upper_bound[dimension-1] = upper_bound;
429 get_array_lower_bound (type_array* array, int dimension) {
430 return array->lower_bound[dimension-1];
434 get_array_upper_bound (type_array* array, int dimension) {
435 return array->upper_bound[dimension-1];
438 inline void set_array_element_type (type_array *array, type *type) {
439 array->element_type = type;
443 get_array_element_type (type_array *array) {
444 return array->element_type;
448 /*******************************************************************/
449 /** TYPE_ENUMERATION **/
450 /*******************************************************************/
452 /* create a new type enumeration -- set the enumerators independently */
454 new_type_enumeration (ident *name /* , int n_enums */)
456 type_enumeration *res;
458 res = (type_enumeration *) xmalloc (sizeof (type_enumeration));
459 add_irp_type((type *) res); /* Remember the new type global. */
460 res->kind = k_type_enumeration;
463 res->n_enums = n_enums;
464 res->enum = (int *) xmalloc (sizeof (int) * n_enums);
472 /* manipulate fields of type_enumeration */
475 get_enumeration_name (type_enumeration *enumeration) {
477 return ID_TO_STR(enumeration->name);
482 get_enumeration_ident (type_enumeration *enumeration) {
484 return enumeration->name;
488 void set_enumeration_name (type_enumeration *enumeration, char *name);
489 void set_enumeration_ident (type_enumeration *enumeration, ident* ident);
492 void set_enumeration_n_enums (type_enumeration *enumeration, int n);
493 int *get_enumeration_n_enums (type_enumeration *enumeration);
494 void set_enumeration_enum (type_enumeration *enumeration, int pos,
496 ir_node *get_enumeration_enum (type_enumeration *enumeration, int pos);
500 /*******************************************************************/
502 /*******************************************************************/
504 /* create a new type pointer */
506 new_type_pointer (ident *name, type *points_to)
510 res = (type_pointer *) xmalloc (sizeof (type_pointer));
511 add_irp_type((type *) res); /* Remember the new type global. */
512 res->kind = k_type_pointer;
514 res->points_to = points_to;
521 /* manipulate fields of type_pointer */
524 get_pointer_name (type_pointer *pointer) {
526 return ID_TO_STR(pointer->name);
531 get_pointer_ident (type_pointer *pointer) {
533 return pointer->name;
537 void set_pointer_name (type_pointer *pointer, char *name);
538 void set_pointer_ident (type_pointer *pointer, ident* ident);
542 set_pointer_points_to_type (type_pointer *pointer, type* type) {
543 pointer->points_to = type;
547 get_pointer_points_to_type (type_pointer *pointer) {
548 return pointer->points_to;
552 /*******************************************************************/
553 /** TYPE_PRIMITIVE **/
554 /*******************************************************************/
556 /* create a new type_primitive */
557 inline type_primitive *
558 new_type_primitive (ident *name, ir_mode *mode)
562 res = (type_primitive *) xmalloc (sizeof (type_primitive));
563 add_irp_type((type *) res); /* Remember the new type global. */
564 res->kind = k_type_primitive;
573 /* manipulate fields of type_primitive */
577 get_primitive_name (type_primitive *primitive) {
579 return ID_TO_STR(primitive->name);
584 get_primitive_ident (type_primitive *primitive) {
586 return primitive->name;
589 void set_primitive_name (type_primitive *primitive, char *name);
590 void set_primitive_ident (type_primitive *primitive, ident* ident);
594 get_primitive_mode (type_primitive *primitive) {
595 return primitive->mode;
599 set_primitive_mode (type_primitive *primitive, ir_mode *mode) {
600 primitive->mode = mode;
606 /*******************************************************************/
607 /** To manage all different types the same **/
608 /*******************************************************************/
612 is_type(void *thing) {
615 kind = get_kind(thing);
616 if ( (kind == k_type_class)
617 || (kind == k_type_strct)
618 || (kind == k_type_method)
619 || (kind == k_type_union)
620 || (kind == k_type_array)
621 || (kind == k_type_enumeration)
622 || (kind == k_type_pointer)
623 || (kind == k_type_primitive))