added access routines for new flag in type_class struct
[libfirm] / ir / tr / type.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Martin Trapp, Christian Schaefer &
5 **          Goetz Lindenmaier
6 **
7 ** type.c: datastructures to hold type information.
8 */
9
10 # include "type.h"
11 # include "irprog.h"  /* So that constructors can add the type to global
12                          data structure. */
13 # include "array.h"
14
15 unsigned long type_visited = 0;
16
17 void
18 init (void)
19 {
20 }
21
22 /*******************************************************************/
23 /** TYPE_CLASS                                                    **/
24 /*******************************************************************/
25
26 type_class *
27 new_type_class (ident *name)//, int members)
28 {
29   type_class *res;
30
31   res = (type_class *) xmalloc (sizeof (type_class));
32   add_irp_type((type *) res);   /* Remember the new type global. */
33   res->kind = k_type_class;
34   res->name = name;
35
36   res->members = NEW_ARR_F (entity *, 1);
37   res->subtypes = NEW_ARR_F (type_class *, 1);
38   res->supertypes = NEW_ARR_F (type_class *, 1);
39   // res->n_members = 0;
40   // res->max_members = members;
41   // res->member = (entity **) xmalloc (sizeof (entity*) * members);
42
43   res->visit = 0;
44
45   return res;
46 }
47
48 /* manipulate fields of type_class */
49 char *
50 get_class_name  (type_class *class) {
51   assert(class);
52   return ID_TO_STR(class->name);
53 }
54
55 /* field: ident */
56 ident *
57 get_class_ident (type_class *class) {
58   assert(class);
59   return class->name;
60 }
61
62 /*
63 void   set_class_name  (type_class *class, char *name);
64 void   set_class_ident (type_class *class, ident* ident);
65 */
66
67 /* field: member */
68 void
69 add_class_member (type_class *class, entity *member)
70 {
71   ARR_APP1 (entity *, class->members, member);
72 }
73
74 entity *
75 get_class_member (type_class *class, int pos)
76 {
77   assert (class);
78   return class->members[pos+1];
79 }
80
81 void
82 set_class_member (type_class *class, entity *member, int pos)
83 {
84   class->members[pos+1] = member;
85 }
86
87 /* field: subtype */
88 void
89 add_class_subtype (type_class *class,  type_class *subtype)
90 {
91   ARR_APP1 (type_class *, class->subtypes, subtype);
92 }
93
94 type_class *
95 get_class_subtype (type_class *class, int pos)
96 {
97   assert (class);
98   return class->subtypes[pos+1];
99 }
100
101 void
102 set_class_subtype (type_class *class, type_class *subtype, int pos)
103 {
104   class->subtypes[pos+1] = subtype;
105 }
106
107 /* field: supertype */
108 void
109 add_class_supertype (type_class *class, type_class *supertype)
110 {
111   ARR_APP1 (type_class *, class->supertypes, supertype);
112 }
113
114 type_class *
115 get_class_supertype (type_class *class, int pos)
116 {
117   assert (class);
118   return class->supertypes[pos+1];
119 }
120
121 void
122 set_class_supertype (type_class *class, type_class *supertype, int pos)
123 {
124   class->supertypes[pos+1] = supertype;
125 }
126
127 /*******************************************************************/
128 /** TYPE_STRCT                                                   **/
129 /*******************************************************************/
130
131 type_strct *
132 new_type_strct (ident *name)//, int members)
133 {
134   type_strct *res;
135
136   res = (type_strct *) xmalloc (sizeof (type_strct));
137   add_irp_type((type *) res);   /* Remember the new type global. */
138   res->kind = k_type_strct;
139   res->name = name;
140
141   // res->n_members = 0;
142   // res->max_members = members;
143   // res->member = (entity **) xmalloc (sizeof (entity*) * members);
144
145   res->visit = 0;
146
147   return res;
148 }
149
150 /* manipulate fields of type_strct */
151 /*
152 char  *
153 get_strct_name  (type_strct *strct) {
154   assert(strct);
155   return ID_TO_STR(strct->name);
156 }
157 */
158
159 ident *
160 get_strct_ident (type_strct *strct) {
161   assert(strct);
162   return strct->name;
163 }
164
165 /*
166 void   set_strct_name  (type_strct *strct, char *name);
167 void   set_strct_ident (type_strct *strct, ident* ident);
168 */
169
170
171 /*******************************************************************/
172 /** TYPE_METHOD                                                   **/
173 /*******************************************************************/
174
175 /* create a new type_method */
176 type_method *
177 new_type_method (ident *name, int arity, int n_res)
178 {
179   type_method *res;
180
181   res = (type_method *) xmalloc (sizeof (type_method));
182   add_irp_type((type *) res);   /* Remember the new type global. */
183   res->kind = k_type_method;
184   res->name = name;   // do I need the name, or is the name in entity sufficient?
185   res->arity = arity;
186   res->param_type = (type **) xmalloc (sizeof (type *) * arity);
187   res->n_res  = n_res;
188   res->res_type = (type **) xmalloc (sizeof (type *) * n_res);
189
190   res->visit = 0;
191
192   return res;
193 }
194
195 /* manipulate fields of type_method */
196 /*
197 char *
198 get_method_name  (type_method *method) {
199   assert(method);
200   return ID_TO_STR(method->name);
201 }
202 */
203
204 ident *
205 get_method_ident (type_method *method) {
206   assert(method);
207   return method->name;
208 }
209
210 /*
211 void   set_method_name  (type_method *method, char *name);
212 void   set_method_ident (type_method *method, ident* ident);
213 */
214
215 inline int
216 get_method_arity (type_method *method) {
217   return method->arity;
218 }
219
220 /*
221 inline void
222 set_method_arity (type_method *method, int arity) {
223   method->arity = arity;
224   / change array size, somehow copy.  *
225 }
226 */
227
228 inline type *
229 get_method_param_type(type_method *method, int pos) {
230   return method->param_type[pos];
231 }
232
233 inline void
234 set_method_param_type(type_method *method, int pos, type* type) {
235   method->param_type[pos] = type;
236 }
237
238
239 inline int
240 get_method_n_res (type_method *method) {
241   return method->n_res;
242 }
243
244 /*
245 inline void
246 set_method_n_res (type_method *method, int n_res) {
247   method->n_res = n_res;
248 }
249 */
250
251 inline type *
252 get_method_res_type(type_method *method, int pos) {
253   return method->res_type[pos];
254 }
255
256 inline void
257 set_method_res_type(type_method *method, int pos, type* type) {
258   method->res_type[pos] = type;
259 }
260
261
262 /*******************************************************************/
263 /** TYPE_UNION                                                    **/
264 /*******************************************************************/
265
266 /* create a new type_union -- set unioned types by hand. */
267 type_union *
268 new_type_union (ident *name, int n_types)
269 {
270   type_union *res;
271
272   res = (type_union *) xmalloc (sizeof (type_union));
273   add_irp_type((type *) res);   /* Remember the new type global. */
274   res->kind = k_type_union;
275   res->name = name;   // do I need a name?
276   res->n_types = n_types;
277   /*
278   res->unioned_type = (int *) xmalloc (sizeof (int) * n_types);
279   */
280
281   res->visit = 0;
282
283   return res;
284 }
285
286 /* manipulate fields of type_union */
287 /*
288 char *
289 get_union_name  (type_union *uni) {
290   assert(uni);
291   return ID_TO_STR(uni->name);
292 }
293 */
294
295 ident *
296 get_union_ident (type_union *uni) {
297   assert(uni);
298   return uni->name;
299 }
300
301 /*
302 void   set_union_name  (type_union *union, char *name);
303 void   set_union_ident (type_union *union, ident* ident);
304 */
305 /*
306 int    get_union_n_types (type_union *union);
307 void   set_union_n_types (type_union *union, int n);
308 type  *get_union_unioned_type (type_union *union, int pos);
309 void   set_union_unioned_type (type_union *union, int pos, type *type);
310 */
311
312 /*******************************************************************/
313 /** TYPE_ARRAY                                                    **/
314 /*******************************************************************/
315
316 /* create a new type_array */
317 inline type_array *
318 new_type_array (ident *name, int n_dimensions)
319 {
320   type_array *res;
321
322   res = (type_array *) xmalloc (sizeof (type_array));
323   add_irp_type((type *) res);   /* Remember the new type global. */
324   res->kind = k_type_array;
325   res->name = name;
326   res->n_dimensions = n_dimensions;
327   res->lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
328   res->upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
329
330   res->visit = 0;
331
332   return res;
333 }
334
335 /* manipulate fields of type_array */
336 /*
337 char *
338 get_array_name  (type_array *array) {
339   assert(array);
340   return ID_TO_STR(array->name);
341 }
342 */
343
344 ident *
345 get_array_ident (type_array *array) {
346   assert(array);
347   return array->name;
348 }
349
350 /*
351 void   set_array_name  (type_array *array, char *name);
352 void   set_array_ident (type_array *array, ident* ident);
353 */
354
355 inline void
356 set_array_dimensions (type_array* array, int n) {
357   array->n_dimensions = n;
358 }
359
360 inline int
361 get_array_dimensions (type_array* array) {
362   return array->n_dimensions;
363 }
364
365 inline void
366 set_array_bounds (type_array* array, int dimension, int lower_bound,
367                   int upper_bound) {
368   array->lower_bound[dimension-1] = lower_bound;
369   array->upper_bound[dimension-1] = upper_bound;
370 }
371
372 inline void
373 set_array_lower_bound (type_array* array, int dimension, int lower_bound) {
374   array->lower_bound[dimension-1] = lower_bound;
375 }
376
377 inline void
378 set_array_upper_bound (type_array* array, int dimension, int upper_bound) {
379   array->upper_bound[dimension-1] = upper_bound;
380 }
381
382 inline int
383 get_array_lower_bound (type_array* array, int dimension) {
384   return array->lower_bound[dimension-1];
385 }
386
387 inline int
388 get_array_upper_bound (type_array* array, int dimension) {
389   return array->upper_bound[dimension-1];
390 }
391
392 inline void set_array_element_type (type_array *array, type *type) {
393   array->element_type = type;
394 }
395
396 inline type *
397 get_array_element_type (type_array *array) {
398   return array->element_type;
399 }
400
401
402 /*******************************************************************/
403 /** TYPE_ENUMERATION                                              **/
404 /*******************************************************************/
405
406 /* create a new type enumeration -- set the enumerators independently */
407 type_enumeration *
408 new_type_enumeration (ident *name /* , int n_enums */)
409 {
410   type_enumeration *res;
411
412   res = (type_enumeration *) xmalloc (sizeof (type_enumeration));
413   add_irp_type((type *) res);   /* Remember the new type global. */
414   res->kind = k_type_enumeration;
415   res->name = name;
416   /*
417   res->n_enums = n_enums;
418   res->enum = (int *) xmalloc (sizeof (int) * n_enums);
419   */
420
421   res->visit = 0;
422
423   return res;
424 }
425
426 /* manipulate fields of type_enumeration */
427 /*
428 char *
429 get_enumeration_name  (type_enumeration *enumeration) {
430   assert(enumeration);
431   return ID_TO_STR(enumeration->name);
432 }
433 */
434
435 ident *
436 get_enumeration_ident (type_enumeration *enumeration) {
437   assert(enumeration);
438   return enumeration->name;
439 }
440
441 /*
442 void   set_enumeration_name  (type_enumeration *enumeration, char *name);
443 void   set_enumeration_ident (type_enumeration *enumeration, ident* ident);
444 */
445 /*
446 void     set_enumeration_n_enums (type_enumeration *enumeration, int n);
447 int     *get_enumeration_n_enums (type_enumeration *enumeration);
448 void     set_enumeration_enum    (type_enumeration *enumeration, int pos,
449                                  ir_node const);
450 ir_node *get_enumeration_enum    (type_enumeration *enumeration, int pos);
451 */
452
453
454 /*******************************************************************/
455 /** TYPE_POINTER                                                  **/
456 /*******************************************************************/
457
458 /* create a new type pointer */
459 type_pointer *
460 new_type_pointer (ident *name, type *points_to)
461 {
462   type_pointer *res;
463
464   res = (type_pointer *) xmalloc (sizeof (type_pointer));
465   add_irp_type((type *) res);   /* Remember the new type global. */
466   res->kind = k_type_pointer;
467   res->name = name;
468   res->points_to = points_to;
469
470   res->visit = 0;
471
472   return res;
473 }
474
475 /* manipulate fields of type_pointer */
476 /*
477 char *
478 get_pointer_name  (type_pointer *pointer) {
479   assert(pointer);
480   return ID_TO_STR(pointer->name);
481 }
482 */
483
484 ident *
485 get_pointer_ident (type_pointer *pointer) {
486   assert(pointer);
487   return pointer->name;
488 }
489
490 /*
491 void   set_pointer_name  (type_pointer *pointer, char *name);
492 void   set_pointer_ident (type_pointer *pointer, ident* ident);
493 */
494
495 inline void
496 set_pointer_points_to_type (type_pointer *pointer, type* type) {
497   pointer->points_to = type;
498 }
499
500 inline type *
501 get_pointer_points_to_type (type_pointer *pointer) {
502   return pointer->points_to;
503 }
504
505
506 /*******************************************************************/
507 /** TYPE_PRIMITIVE                                                **/
508 /*******************************************************************/
509
510 /* create a new type_primitive */
511 inline type_primitive *
512 new_type_primitive (ident *name, ir_mode *mode)
513 {
514   type_primitive *res;
515
516   res = (type_primitive *) xmalloc (sizeof (type_primitive));
517   add_irp_type((type *) res);   /* Remember the new type global. */
518   res->kind = k_type_primitive;
519   res->name = name;
520   res->mode = mode;
521
522   res->visit = 0;
523
524   return res;
525 }
526
527 /* manipulate fields of type_primitive */
528 /*
529
530 char  *
531 get_primitive_name  (type_primitive *primitive) {
532   assert(primitive);
533   return ID_TO_STR(primitive->name);
534 }
535 */
536
537 ident *
538 get_primitive_ident (type_primitive *primitive) {
539   assert(primitive);
540   return primitive->name;
541 }
542 /*
543 void   set_primitive_name  (type_primitive *primitive, char *name);
544 void   set_primitive_ident (type_primitive *primitive, ident* ident);
545 */
546
547 inline ir_mode *
548 get_primitive_mode (type_primitive *primitive) {
549   return primitive->mode;
550 }
551
552 inline void
553 set_primitive_mode (type_primitive *primitive, ir_mode *mode) {
554   primitive->mode = mode;
555 }
556
557
558
559
560 /*******************************************************************/
561 /**  To manage all different types the same                       **/
562 /*******************************************************************/
563
564
565 int
566 is_type(void *thing) {
567   firm_kind kind;
568
569   kind = get_kind(thing);
570   if (   (kind == k_type_class)
571       || (kind == k_type_strct)
572       || (kind == k_type_method)
573       || (kind == k_type_union)
574       || (kind == k_type_array)
575       || (kind == k_type_enumeration)
576       || (kind == k_type_pointer)
577       || (kind == k_type_primitive))
578     return 1;
579   else
580     return 0;
581 }