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