minor bug fix to previous changes
[libfirm] / ir / tr / type.c
1 /****h* libfirm/type.c
2  *
3  * NAME
4  *   file type.c - implementation of the datastructure to hold
5  *   type information.
6  * COPYRIGHT
7  *  (C) 2001 by Universitaet Karlsruhe
8  * AUTHORS
9  *  Martin Trapp, Christian Schaefer, Goetz Lindenmaier
10  *
11  * NOTES
12  *  This module supplies a datastructure to represent all types
13  *  known in the compiled program.  This includes types specified
14  *  in the program as well as types defined by the language.  In the
15  *  view of the intermediate representation there is no difference
16  *  between these types.
17  *
18  *  There exist several kinds of types, arranged by the structure of
19  *  the type.  A type is described by a set of attributes.  Some of
20  *  these attributes are common to all types, others depend on the
21  *  kind of the type.
22  *
23  *  Types are different from the modes defined in irmode:  Types are
24  *  on the level of the programming language, modes at the level of
25  *  the target processor.
26  *
27  * SEE ALSO
28  *   type_t.h type tpop
29  *****
30  */
31 # include <stdlib.h>
32 # include <stddef.h>
33 # include "type_t.h"
34 # include "tpop_t.h"
35 # include "array.h"
36
37 /*******************************************************************/
38 /** TYPE                                                          **/
39 /*******************************************************************/
40
41 unsigned long type_visited;
42
43 inline type *
44 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
45   type *res;
46
47   int node_size = offsetof (type, attr) +  type_op->attr_size;
48   res = (type *) xmalloc (node_size);
49   add_irp_type(res);   /* Remember the new type global. */
50
51   res->kind = k_type;
52   res->type_op = type_op;
53   res->mode = mode;
54   res->name = name;
55   res->state = layout_undefined;
56   res->size = -1;
57   res->visit = 0;
58
59   return res;
60 }
61
62 tp_op*      get_type_tpop(type *tp) {
63   assert(tp);
64   return tp->type_op;
65 }
66
67 ident*      get_type_tpop_nameid(type *tp) {
68   assert(tp);
69   return tp->type_op->name;
70 }
71 const char* get_type_tpop_name(type *tp) {
72   assert(tp);
73   return id_to_str(tp->type_op->name);
74 }
75 tp_opcode    get_type_tpop_code(type *tp) {
76   assert(tp);
77   return tp->type_op->code;
78 }
79 ir_mode*    get_type_mode(type *tp) {
80   assert(tp);
81   return tp->mode;
82 }
83 void        set_type_mode(type *tp, ir_mode* m) {
84   assert(tp);
85   tp->mode = m;
86   /* For pointer and primitive size depends on the mode. */
87   if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
88     tp->size == get_mode_size(m);
89 }
90 ident*      get_type_nameid(type *tp) {
91   assert(tp);
92   return tp->name;
93 }
94 void        set_type_nameid(type *tp, ident* id) {
95   assert(tp);
96   tp->name = id;
97 }
98 const char* get_type_name(type *tp) {
99   assert(tp);
100   return id_to_str(tp->name);
101 }
102 int         get_type_size(type *tp) {
103   assert(tp);
104   return tp->size;
105 }
106
107 void
108 set_type_size(type *tp, int size) {
109   assert(tp);
110   /* For pointer and primitive size depends on the mode. */
111   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
112     tp->size = size;
113 }
114
115 type_state
116 get_type_state(type *tp) {
117   assert(tp);
118   return tp->state;
119 }
120
121 void
122 set_type_state(type *tp, type_state state) {
123   assert(tp);
124   /* For pointer and primitive always fixed. */
125   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
126     tp->state = state;
127 }
128
129 unsigned long get_type_visited(type *tp) {
130   assert(tp);
131   return tp->visit;
132 }
133
134 void        set_type_visited(type *tp, unsigned long num) {
135   assert(tp);
136   tp->visit = num;
137 }
138 /* Sets visited field in type to type_visited. */
139 void        mark_type_visited(type *tp) {
140   assert(tp);
141   assert(tp->visit < type_visited);
142   tp->visit = type_visited;
143 }
144
145 int is_type            (void *thing) {
146   assert(thing);
147   if (get_kind(thing) == k_type)
148     return 1;
149   else
150     return 0;
151 }
152
153 /*******************************************************************/
154 /** TYPE_CLASS                                                    **/
155 /*******************************************************************/
156
157 /* create a new class type */
158 type   *new_type_class (ident *name) {
159   type *res;
160
161   res = new_type(type_class, NULL, name);
162
163   res->attr.ca.members    = NEW_ARR_F (entity *, 1);
164   res->attr.ca.subtypes   = NEW_ARR_F (type *, 1);
165   res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
166
167   return res;
168 }
169 /* manipulate private fields of class type  */
170 void    add_class_member   (type *clss, entity *member) {
171   assert(clss && (clss->type_op == type_class));
172   ARR_APP1 (entity *, clss->attr.ca.members, member);
173 }
174 int     get_class_n_member (type *clss) {
175   assert(clss && (clss->type_op == type_class));
176   return (ARR_LEN (clss->attr.ca.members))-1;
177 }
178 entity *get_class_member   (type *clss, int pos) {
179   assert(clss && (clss->type_op == type_class));
180   return clss->attr.ca.members[pos+1];
181 }
182 void    set_class_member   (type *clss, entity *member, int pos) {
183   assert(clss && (clss->type_op == type_class));
184   clss->attr.ca.members[pos+1] = member;
185 }
186 void    remove_class_member(type *clss, entity *member) {
187   int i;
188   assert(clss && (clss->type_op == type_class));
189   for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
190     if (clss->attr.ca.members[i+1] == member) {
191       for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
192         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
193       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
194       break;
195     }
196 }
197
198 void    add_class_subtype   (type *clss, type *subtype) {
199   assert(clss && (clss->type_op == type_class));
200   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
201   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
202 }
203 int     get_class_n_subtype (type *clss) {
204   assert(clss && (clss->type_op == type_class));
205   return (ARR_LEN (clss->attr.ca.subtypes))-1;
206 }
207 type   *get_class_subtype   (type *clss, int pos) {
208   assert(clss && (clss->type_op == type_class));
209   return clss->attr.ca.subtypes[pos+1];
210 }
211 void    set_class_subtype   (type *clss, type *subtype, int pos) {
212   assert(clss && (clss->type_op == type_class));
213   clss->attr.ca.subtypes[pos+1] = subtype;
214 }
215 void    remove_class_subtype(type *clss, type *subtype) {
216   int i;
217   assert(clss && (clss->type_op == type_class));
218   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
219     if (clss->attr.ca.subtypes[i+1] == subtype) {
220       for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
221         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
222       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
223       break;
224     }
225 }
226
227 void    add_class_supertype   (type *clss, type *supertype) {
228   assert(clss && (clss->type_op == type_class));
229   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
230   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
231 }
232 int     get_class_n_supertype (type *clss) {
233   assert(clss && (clss->type_op == type_class));
234   return (ARR_LEN (clss->attr.ca.supertypes))-1;
235 }
236 type   *get_class_supertype   (type *clss, int pos) {
237   assert(clss && (clss->type_op == type_class));
238   return clss->attr.ca.supertypes[pos+1];
239 }
240 void    set_class_supertype   (type *clss, type *supertype, int pos) {
241   assert(clss && (clss->type_op == type_class));
242   clss->attr.ca.supertypes[pos+1] = supertype;
243 }
244 void    remove_class_supertype(type *clss, type *supertype) {
245   int i;
246   assert(clss && (clss->type_op == type_class));
247   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
248     if (clss->attr.ca.supertypes[i+1] == supertype) {
249       for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
250         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
251       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
252       break;
253     }
254 }
255 /* typecheck */
256 bool    is_class_type(type *clss) {
257   assert(clss);
258   if (clss->type_op == type_class) return 1; else return 0;
259 }
260
261 /*******************************************************************/
262 /** TYPE_STRUCT                                                   **/
263 /*******************************************************************/
264
265 /* create a new type struct */
266 type   *new_type_struct (ident *name) {
267   type *res;
268   res = new_type(type_struct, NULL, name);
269   res->attr.sa.members = NEW_ARR_F (entity *, 1);
270   return res;
271 }
272 /* manipulate private fields of struct */
273 void    add_struct_member   (type *strct, entity *member) {
274   assert(strct && (strct->type_op == type_struct));
275   ARR_APP1 (entity *, strct->attr.sa.members, member);
276 }
277 int     get_struct_n_member (type *strct) {
278   assert(strct && (strct->type_op == type_struct));
279   return (ARR_LEN (strct->attr.sa.members))-1;
280 }
281 entity *get_struct_member   (type *strct, int pos) {
282   assert(strct && (strct->type_op == type_struct));
283   return strct->attr.sa.members[pos+1];
284 }
285 void    set_struct_member   (type *strct, int pos, entity *member) {
286   assert(strct && (strct->type_op == type_struct));
287   strct->attr.sa.members[pos+1] = member;
288 }
289 void    remove_struct_member(type *strct, entity *member) {
290   int i;
291   assert(strct && (strct->type_op == type_struct));
292   for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
293     if (strct->attr.sa.members[i+1] == member) {
294       for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
295         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
296       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
297       break;
298     }
299 }
300 /* typecheck */
301 bool    is_struct_type(type *strct) {
302   assert(strct);
303   if (strct->type_op == type_struct) return 1; else return 0;
304 }
305
306 /*******************************************************************/
307 /** TYPE_METHOD                                                   **/
308 /*******************************************************************/
309
310 /* Create a new method type.
311    N_param is the number of parameters, n_res the number of results.  */
312 type *new_type_method (ident *name, int n_param, int n_res) {
313   type *res;
314   res = new_type(type_method, NULL, name);
315   res->attr.ma.n_params   = n_param;
316   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
317   res->attr.ma.n_res      = n_res;
318   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
319   return res;
320 }
321
322 /* manipulate private fields of method. */
323 int   get_method_n_params  (type *method) {
324   assert(method && (method->type_op == type_method));
325   return method->attr.ma.n_params;
326 }
327 type *get_method_param_type(type *method, int pos) {
328   assert(method && (method->type_op == type_method));
329   return method->attr.ma.param_type[pos];
330 }
331 void  set_method_param_type(type *method, int pos, type* type) {
332   assert(method && (method->type_op == type_method));
333   method->attr.ma.param_type[pos] = type;
334 }
335
336 int   get_method_n_res   (type *method) {
337   assert(method && (method->type_op == type_method));
338   return method->attr.ma.n_res;
339 }
340 type *get_method_res_type(type *method, int pos) {
341   assert(method && (method->type_op == type_method));
342   return method->attr.ma.res_type[pos];
343 }
344 void  set_method_res_type(type *method, int pos, type* type) {
345   assert(method && (method->type_op == type_method));
346   method->attr.ma.res_type[pos] = type;
347 }
348
349 /* typecheck */
350 bool  is_method_type     (type *method) {
351   assert(method);
352   if (method->type_op == type_method) return 1; else return 0;
353 }
354 /*****/
355
356 /*******************************************************************/
357 /** TYPE_UNION                                                    **/
358 /*******************************************************************/
359
360 /* create a new type uni */
361 type  *new_type_uni (ident *name) {
362   type *res;
363   res = new_type(type_union, NULL, name);
364   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
365     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
366   res->attr.ua.members = NEW_ARR_F (entity *, 1);
367   return res;
368 }
369 /* manipulate private fields of struct */
370 #if 0
371 int    get_union_n_types      (type *uni) {
372   assert(uni && (uni->type_op == type_union));
373   return uni->attr.ua.n_types;
374 }
375 type  *get_union_unioned_type (type *uni, int pos) {
376   assert(uni && (uni->type_op == type_union));
377   return uni->attr.ua.unioned_type[pos];
378 }
379 void   set_union_unioned_type (type *uni, int pos, type *type) {
380   assert(uni && (uni->type_op == type_union));
381   uni->attr.ua.unioned_type[pos] = type;
382 }
383 ident *get_union_delim_nameid (type *uni, int pos) {
384   assert(uni && (uni->type_op == type_union));
385   return uni->attr.ua.delim_names[pos];
386 }
387 const char *get_union_delim_name (type *uni, int pos) {
388   assert(uni && (uni->type_op == type_union));
389   return id_to_str(uni->attr.ua.delim_names[pos]);
390 }
391 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
392   assert(uni && (uni->type_op == type_union));
393   uni->attr.ua.delim_names[pos] = id;
394 }
395 #endif
396 int    get_union_n_members      (type *uni) {
397   assert(uni && (uni->type_op == type_union));
398   return (ARR_LEN (uni->attr.ua.members))-1;
399 }
400 void    add_union_member   (type *uni, entity *member) {
401   assert(uni && (uni->type_op == type_union));
402   ARR_APP1 (entity *, uni->attr.ua.members, member);
403 }
404 entity  *get_union_member (type *uni, int pos) {
405   assert(uni && (uni->type_op == type_union));
406   return uni->attr.ua.members[pos+1];
407 }
408 void   set_union_member (type *uni, int pos, entity *member) {
409   assert(uni && (uni->type_op == type_union));
410   uni->attr.ua.members[pos+1] = member;
411 }
412 void   remove_union_member(type *uni, entity *member) {
413   int i;
414   assert(uni && (uni->type_op == type_union));
415   for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
416     if (uni->attr.ua.members[i+1] == member) {
417       for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
418         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
419       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
420       break;
421     }
422 }
423
424 /* typecheck */
425 bool   is_union_type         (type *uni) {
426   assert(uni);
427   if (uni->type_op == type_union) return 1; else return 0;
428 }
429
430 /*******************************************************************/
431 /** TYPE_ARRAY                                                    **/
432 /*******************************************************************/
433
434
435 /* create a new type array -- set dimension sizes independently */
436 type *new_type_array         (ident *name, int n_dimensions,
437                               type *element_type) {
438   type *res;
439   res = new_type(type_array, NULL, name);
440   res->attr.aa.n_dimensions = n_dimensions;
441   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
442   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
443   res->attr.aa.element_type = element_type;
444   new_entity(res, name, element_type);
445   return res;
446 }
447
448 /* manipulate private fields of array type */
449 int   get_array_n_dimensions (type *array) {
450   assert(array && (array->type_op == type_array));
451   return array->attr.aa.n_dimensions;
452 }
453 void  set_array_bounds       (type *array, int dimension, ir_node * lower_bound,
454                                                           ir_node * upper_bound) {
455   assert(array && (array->type_op == type_array));
456   array->attr.aa.lower_bound[dimension] = lower_bound;
457   array->attr.aa.upper_bound[dimension] = upper_bound;
458 }
459 void  set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
460   assert(array && (array->type_op == type_array));
461   array->attr.aa.lower_bound[dimension] = lower_bound;
462 }
463 void  set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
464   assert(array && (array->type_op == type_array));
465   array->attr.aa.upper_bound[dimension] = upper_bound;
466 }
467 ir_node * get_array_lower_bound  (type *array, int dimension) {
468   assert(array && (array->type_op == type_array));
469   return array->attr.aa.lower_bound[dimension];
470 }
471 ir_node * get_array_upper_bound  (type *array, int dimension) {
472   assert(array && (array->type_op == type_array));
473   return array->attr.aa.upper_bound[dimension];
474 }
475 void  set_array_element_type (type *array, type *type) {
476   assert(array && (array->type_op == type_array));
477   array->attr.aa.element_type = type;
478 }
479 type *get_array_element_type (type *array) {
480   assert(array && (array->type_op == type_array));
481   return array->attr.aa.element_type;
482 }
483 void  set_array_element_entity (type *array, entity *ent) {
484   assert(array && (array->type_op == type_array));
485   array->attr.aa.element_ent = ent;
486 }
487 entity *get_array_element_entity (type *array) {
488   assert(array && (array->type_op == type_array));
489   return array->attr.aa.element_ent;
490 }
491
492 /* typecheck */
493 bool   is_array_type         (type *array) {
494   assert(array);
495   if (array->type_op == type_array) return 1; else return 0;
496 }
497
498 /*******************************************************************/
499 /** TYPE_ENUMERATION                                              **/
500 /*******************************************************************/
501
502 /* create a new type enumeration -- set the enumerators independently */
503 type   *new_type_enumeration    (ident *name, int n_enums) {
504   type *res;
505   res = new_type(type_enumeration, NULL, name);
506   res->attr.ea.n_enums     = n_enums;
507   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
508   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
509   return res;
510 }
511
512 /* manipulate fields of enumeration type. */
513 int     get_enumeration_n_enums (type *enumeration) {
514   assert(enumeration && (enumeration->type_op == type_enumeration));
515   return enumeration->attr.ea.n_enums;
516 }
517 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
518   assert(enumeration && (enumeration->type_op == type_enumeration));
519   enumeration->attr.ea.enumer[pos] = con;
520 }
521 tarval *get_enumeration_enum    (type *enumeration, int pos) {
522   assert(enumeration && (enumeration->type_op == type_enumeration));
523   return enumeration->attr.ea.enumer[pos];
524 }
525 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
526   assert(enumeration && (enumeration->type_op == type_enumeration));
527   enumeration->attr.ea.enum_nameid[pos] = id;
528 }
529 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
530   assert(enumeration && (enumeration->type_op == type_enumeration));
531   return enumeration->attr.ea.enum_nameid[pos];
532 }
533 const char *get_enumeration_name(type *enumeration, int pos) {
534   assert(enumeration && (enumeration->type_op == type_enumeration));
535   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
536 }
537
538 /* typecheck */
539 bool    is_enumeration_type     (type *enumeration) {
540   assert(enumeration);
541   if (enumeration->type_op == type_enumeration) return 1; else return 0;
542 }
543
544 /*******************************************************************/
545 /** TYPE_POINTER                                                  **/
546 /*******************************************************************/
547
548 /* Create a new type pointer */
549 type *new_type_pointer           (ident *name, type *points_to) {
550   type *res;
551   res = new_type(type_pointer, mode_p, name);
552   res->attr.pa.points_to = points_to;
553   res->size = get_mode_size(res->mode);
554   res->state = layout_fixed;
555   return res;
556 }
557 /* manipulate fields of type_pointer */
558 void  set_pointer_points_to_type (type *pointer, type *type) {
559   assert(pointer && (pointer->type_op == type_pointer));
560   pointer->attr.pa.points_to = type;
561 }
562 type *get_pointer_points_to_type (type *pointer) {
563   assert(pointer && (pointer->type_op == type_pointer));
564   return pointer->attr.pa.points_to;
565 }
566
567 /* typecheck */
568 bool  is_pointer_type            (type *pointer) {
569   assert(pointer);
570   if (pointer->type_op == type_pointer) return 1; else return 0;
571 }
572
573
574 /*******************************************************************/
575 /** TYPE_PRIMITIVE                                                **/
576 /*******************************************************************/
577
578 /* create a new type primitive */
579 type *new_type_primitive (ident *name, ir_mode *mode) {
580   type *res;
581   res = new_type(type_primitive, mode, name);
582   res->size = get_mode_size(mode);
583   res->state = layout_fixed;
584   return res;
585 }
586
587 /* typecheck */
588 bool  is_primitive_type  (type *primitive) {
589   assert(primitive);
590   if (primitive->type_op == type_primitive) return 1; else return 0;
591 }