5495678972fcea36791b23cf685bb44ed75025d3
[libfirm] / ir / tr / type_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type_t.h
4  * Purpose:     Representation of types -- private header.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 # ifndef _TYPE_T_H_
14 # define _TYPE_T_H_
15
16 # include "firm_config.h"
17 # include "type.h"
18 # include "tpop_t.h"
19
20 # include "array.h"
21
22 /**
23  * @file type_t.h
24  * This file contains the datatypes hidden in type.h.
25  *
26  * @author Goetz Lindenmaier
27  * @see  type.h tpop_t.h tpop.h
28  */
29
30 /** class attributes */
31 typedef struct {
32   entity **members;    /**< fields and methods of this class */
33   type   **subtypes;   /**< direct subtypes */
34   type   **supertypes; /**< direct supertypes */
35   peculiarity peculiarity;
36   int dfn;             /**< number used for 'instanceof' operator */
37 } cls_attr;
38
39 /** struct attributes */
40 typedef struct {
41   entity **members;    /**< fields of this struct. No method entities allowed. */
42 } stc_attr;
43
44 /** A (type, entity) pair */
45 typedef struct {
46   type   *tp;          /**< a type */
47   entity *ent;         /**< an entity */
48 } tp_ent_pair;
49
50 /** method attributes */
51 typedef struct {
52   int n_params;              /**< number of parameters */
53   tp_ent_pair *param_type;   /**< array of parameter type/value entities pairs */
54   type *value_params;        /**< A type whose entities represent copied value arguments. */
55   int n_res;                 /**< number of results */
56   tp_ent_pair *res_type;     /**< array of result type/value entity pairs */
57   type *value_ress;          /**< A type whose entities represent copied value results. */
58   variadicity variadicity;   /**< variadicity of the method. */
59   int first_variadic_param;  /**< index of the first variadic param or -1 if non-variadic .*/
60 } mtd_attr;
61
62 /** union attributes */
63 typedef struct {
64   entity **members;    /**< fields of this union. No method entities allowed. */
65 } uni_attr;
66
67 /** array attributes */
68 typedef struct {
69   int   n_dimensions;  /**< Number of array dimensions.  */
70   ir_node **lower_bound;   /**< Lower bounds of dimensions.  Usually all 0. */
71   ir_node **upper_bound;   /**< Upper bounds or dimensions. */
72   int *order;              /**< Ordering of dimensions. */
73   type *element_type;  /**< The type of the array elements. */
74   entity *element_ent; /**< Entity for the array elements, to be used for
75               element selection with Sel. */
76 } arr_attr;
77
78 /** enum attributes */
79 typedef struct {
80   int      n_enums;    /**< Number of enumerators. */
81   tarval **enumer;     /**< Contains all constants that represent a member
82                           of the enum -- enumerators. */
83   ident  **enum_nameid;/**< Contains the names of the enum fields as specified by
84                           the source program */
85 } enm_attr;
86
87 /** pointer attributes */
88 typedef struct {
89   type *points_to;     /**< The type of the entity the pointer points to. */
90 } ptr_attr;
91
92 /*
93 typedef struct {   * No private attr yet! *
94 } pri_attr; */
95
96
97 /*
98 typedef struct {        * No private attr, must be smaller than others! *
99 } id_attr;
100 */
101
102 /** General type attributes. */
103 typedef union {
104   cls_attr ca;      /**< attributes of a class type */
105   stc_attr sa;      /**< attributes of a struct type */
106   mtd_attr ma;      /**< attributes of a method type */
107   uni_attr ua;      /**< attributes of an union type */
108   arr_attr aa;      /**< attributes of an array type */
109   enm_attr ea;      /**< attributes of an enumeration type */
110   ptr_attr pa;      /**< attributes of a pointer type */
111 } tp_attr;
112
113 /** the structure of a type */
114 struct type {
115   firm_kind kind;          /**< the firm kind, must be k_type */
116   const tp_op *type_op;    /**< the type operation of the type */
117   ident *name;             /**< The name of the type */
118   visibility visibility;   /**< Visibility of entities of this type. */
119   char frame_type;         /**< True if this is a frame type, false else */
120   type_state state;        /**< Represents the types state: layout undefined or
121                                 fixed. */
122   int size;                /**< Size of an entity of this type. This is determined
123                                 when fixing the layout of this class.  Size must be
124                                 given in bits. */
125   int align;               /**< Alignment of an entity of this type. This should be
126                                 set according to the source language needs. If not set it's
127                                 calculated automatically by get_type_alignment().
128                                 Alignment must be given in bits. */
129   ir_mode *mode;           /**< The mode for atomic types */
130   unsigned long visit;     /**< visited counter for walks of the type information */
131   void *link;              /**< holds temporary data - like in irnode_t.h */
132   struct dbg_info *dbi;    /**< A pointer to information for debug support. */
133
134   /* ------------- fields for analyses ---------------*/
135
136 #ifdef DEBUG_libfirm
137   int nr;             /**< a unique node number for each node to make output
138                            readable. */
139 #endif
140   tp_attr attr;            /* type kind specific fields. This must be the last
141                   entry in this struct!  Varying size! */
142 };
143
144 /**
145  *   Creates a new type representation:
146  *
147  *   @param type_op  the kind of this type.  May not be type_id.
148  *   @param mode     the mode to be used for this type, may be NULL
149  *   @param name     an ident for the name of this type.
150  *   @param db       debug info
151  *
152  *   @return A new type of the given type.  The remaining private attributes are not
153  *           initialized.  The type is in state layout_undefined.
154  */
155 type *
156 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
157 void free_type_attrs       (type *tp);
158
159 void free_class_entities      (type *clss);
160 void free_struct_entities     (type *strct);
161 void free_method_entities     (type *method);
162 void free_union_entities      (type *uni);
163 void free_array_entities      (type *array);
164 void free_enumeration_entities(type *enumeration);
165 void free_pointer_entities    (type *pointer);
166
167 void free_array_automatic_entities(type *array);
168
169 void free_class_attrs      (type *clss);
170 void free_struct_attrs     (type *strct);
171 void free_method_attrs     (type *method);
172 void free_union_attrs      (type *uni);
173 void free_array_attrs      (type *array);
174 void free_enumeration_attrs(type *enumeration);
175 void free_pointer_attrs    (type *pointer);
176
177 void set_class_mode(type *tp, ir_mode *mode);
178 void set_struct_mode(type *tp, ir_mode *mode);
179 void set_pointer_mode(type *tp, ir_mode *mode);
180 void set_primitive_mode(type *tp, ir_mode *mode);
181 void set_enumeration_mode(type *tp, ir_mode *mode);
182
183 void set_class_size_bits(type *tp, int bits);
184 void set_struct_size_bits(type *tp, int bits);
185 void set_union_size_bits(type *tp, int bits);
186 void set_array_size_bits(type *tp, int size);
187 void set_default_size_bits(type *tp, int size);
188
189 /**
190  * Initialize the type module.
191  *
192  * @param builtin_db  debug info for builtin objects
193  */
194 void firm_init_type(dbg_info *builtin_db);
195
196
197 /* ------------------- *
198  *  inline functions   *
199  * ------------------- */
200
201 extern unsigned long firm_type_visited;
202
203 static INLINE void _set_master_type_visited(unsigned long val) { firm_type_visited = val; }
204 static INLINE unsigned long _get_master_type_visited(void)     { return firm_type_visited; }
205 static INLINE void _inc_master_type_visited(void)              { ++firm_type_visited; }
206
207 static INLINE void *
208 _get_type_link(const type *tp) {
209   assert(tp && tp->kind == k_type);
210   return(tp -> link);
211 }
212
213 static INLINE void
214 _set_type_link(type *tp, void *l) {
215   assert(tp && tp->kind == k_type);
216   tp -> link = l;
217 }
218
219 static INLINE const tp_op*
220 _get_type_tpop(const type *tp) {
221   assert(tp && tp->kind == k_type);
222   return tp->type_op;
223 }
224
225 static INLINE ident*
226 _get_type_tpop_nameid(const type *tp) {
227   assert(tp && tp->kind == k_type);
228   return get_tpop_ident(tp->type_op);
229 }
230
231 static INLINE tp_opcode
232 _get_type_tpop_code(const type *tp) {
233   assert(tp && tp->kind == k_type);
234   return get_tpop_code(tp->type_op);
235 }
236
237 static INLINE ir_mode *
238 _get_type_mode(const type *tp) {
239   assert(tp && tp->kind == k_type);
240   return tp->mode;
241 }
242
243 static INLINE ident *
244 _get_type_ident(const type *tp) {
245   assert(tp && tp->kind == k_type);
246   return tp->name;
247 }
248
249 static INLINE void
250 _set_type_ident(type *tp, ident* id) {
251   assert(tp && tp->kind == k_type);
252   tp->name = id;
253 }
254
255 static INLINE int
256 _get_type_size_bits(const type *tp) {
257   assert(tp && tp->kind == k_type);
258   return tp->size;
259 }
260
261 static INLINE int
262 _get_type_size_bytes(const type *tp) {
263   int size = _get_type_size_bits(tp);
264   if (size < 0)
265     return -1;
266   if ((size & 7) != 0) {
267     assert(0 && "cannot take byte size of this type");
268     return -1;
269   }
270   return size >> 3;
271 }
272
273 static INLINE type_state
274 _get_type_state(const type *tp) {
275   assert(tp && tp->kind == k_type);
276   return tp->state;
277 }
278
279 static INLINE unsigned long
280 _get_type_visited(const type *tp) {
281   assert(tp && tp->kind == k_type);
282   return tp->visit;
283 }
284
285 static INLINE void
286 _set_type_visited(type *tp, unsigned long num) {
287   assert(tp && tp->kind == k_type);
288   tp->visit = num;
289 }
290
291 static INLINE void
292 _mark_type_visited(type *tp) {
293   assert(tp && tp->kind == k_type);
294   assert(tp->visit < firm_type_visited);
295   tp->visit = firm_type_visited;
296 }
297
298 static INLINE int
299 _type_visited(const type *tp) {
300   assert(tp && tp->kind == k_type);
301   return tp->visit >= firm_type_visited;
302 }
303
304 static INLINE int
305 _type_not_visited(const type *tp) {
306   assert(tp && tp->kind == k_type);
307   return tp->visit  < firm_type_visited;
308 }
309
310 static INLINE int
311 _is_type(const void *thing) {
312   return (get_kind(thing) == k_type);
313 }
314
315 static INLINE int
316 _is_class_type(const type *clss) {
317   assert(clss);
318   return (clss->type_op == type_class);
319 }
320
321 static INLINE int
322 _get_class_n_members (const type *clss) {
323   assert(clss && (clss->type_op == type_class));
324   return (ARR_LEN (clss->attr.ca.members));
325 }
326
327 static INLINE entity *
328 _get_class_member   (const type *clss, int pos) {
329   assert(clss && (clss->type_op == type_class));
330   assert(pos >= 0 && pos < _get_class_n_members(clss));
331   return clss->attr.ca.members[pos];
332 }
333
334 static INLINE int
335 _is_struct_type(const type *strct) {
336   assert(strct);
337   return (strct->type_op == type_struct);
338 }
339
340 static INLINE int
341 _is_method_type(const type *method) {
342   assert(method);
343   return (method->type_op == type_method);
344 }
345
346 static INLINE int
347 _is_union_type(const type *uni) {
348   assert(uni);
349   return (uni->type_op == type_union);
350 }
351
352 static INLINE int
353 _is_array_type(const type *array) {
354   assert(array);
355   return (array->type_op == type_array);
356 }
357
358 static INLINE int
359 _is_enumeration_type(const type *enumeration) {
360   assert(enumeration);
361   return (enumeration->type_op == type_enumeration);
362 }
363
364 static INLINE int
365 _is_pointer_type(const type *pointer) {
366   assert(pointer);
367   return (pointer->type_op == type_pointer);
368 }
369
370 /** Returns true if a type is a primitive type. */
371 static INLINE int
372 _is_primitive_type(const type *primitive) {
373   assert(primitive && primitive->kind == k_type);
374   return (primitive->type_op == type_primitive);
375 }
376
377 static INLINE int
378 _is_atomic_type(const type *tp) {
379   assert(tp && tp->kind == k_type);
380   return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
381       _is_enumeration_type(tp));
382 }
383
384 static INLINE int
385 _get_method_n_params(const type *method) {
386   assert(method && (method->type_op == type_method));
387   return method->attr.ma.n_params;
388 }
389
390 static INLINE int
391 _get_method_n_ress(const type *method) {
392   assert(method && (method->type_op == type_method));
393   return method->attr.ma.n_res;
394 }
395
396
397 #define set_master_type_visited(val)      _set_master_type_visited(val)
398 #define get_master_type_visited()         _get_master_type_visited()
399 #define inc_master_type_visited()         _inc_master_type_visited()
400 #define get_type_link(tp)                 _get_type_link(tp)
401 #define set_type_link(tp, l)              _set_type_link(tp, l)
402 #define get_type_tpop(tp)                 _get_type_tpop(tp)
403 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
404 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
405 #define get_type_mode(tp)                 _get_type_mode(tp)
406 #define get_type_ident(tp)                _get_type_ident(tp)
407 #define set_type_ident(tp, id)            _set_type_ident(tp, id)
408 #define get_type_size(tp)                 _get_type_size(tp)
409 #define get_type_state(tp)                _get_type_state(tp)
410 #define get_type_visited(tp)              _get_type_visited(tp)
411 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
412 #define mark_type_visited(tp)             _mark_type_visited(tp)
413 #define type_visited(tp)                  _type_visited(tp)
414 #define type_not_visited(tp)              _type_not_visited(tp)
415 #define is_type(thing)                    _is_type(thing)
416 #define is_Class_type(clss)               _is_class_type(clss)
417 #define get_class_n_members(clss)         _get_class_n_members(clss)
418 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
419 #define is_Struct_type(strct)             _is_struct_type(strct)
420 #define is_Method_type(method)            _is_method_type(method)
421 #define is_Union_type(uni)                _is_union_type(uni)
422 #define is_Array_type(array)              _is_array_type(array)
423 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
424 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
425 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
426 #define is_atomic_type(tp)                _is_atomic_type(tp)
427 #define get_method_n_params(method)       _get_method_n_params(method)
428 #define get_method_n_ress(method)         _get_method_n_ress(method)
429
430 # endif /* _TYPE_T_H_ */