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