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