added flag for frame types
[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
42               allowed. */
43 } stc_attr;
44
45 /** method attributes */
46 typedef struct {
47   int n_params;              /**< number of parameters */
48   type **param_type;         /**< code generation needs this information. */
49   type *value_params;        /**< A type whose entities represent copied value arguments. */
50   int n_res;                 /**< number of results */
51   type **res_type;           /**< array with result types */
52   type *value_ress;          /**< A type whose entities represent copied value results. */
53   variadicity variadicity;   /**< variadicity of the method. */
54   int first_variadic_param;  /**< index of the first variadic param or -1 if non-variadic .*/
55 } mtd_attr;
56
57 /** union attributes */
58 typedef struct {
59   int     n_types;
60   /* type  **unioned_type; * a list of unioned types. */
61   /* ident **delim_names;  * names of the union delimiters. */
62   entity **members;    /**< fields of this union. No method entities
63               allowed.  */
64
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  *
151  *   @return A new type of the given type.  The remaining private attributes are not
152  *           initialized.  The type is in state layout_undefined.
153  */
154 INLINE type *
155 new_type(tp_op *type_op,
156      ir_mode *mode,
157      ident* name);
158 void free_type_attrs       (type *tp);
159
160 INLINE void free_class_entities      (type *clss);
161 INLINE void free_struct_entities     (type *strct);
162 INLINE void free_method_entities     (type *method);
163 INLINE void free_union_entities      (type *uni);
164 INLINE void free_array_entities      (type *array);
165 INLINE void free_enumeration_entities(type *enumeration);
166 INLINE void free_pointer_entities    (type *pointer);
167 INLINE void free_primitive_entities  (type *primitive);
168
169 INLINE void free_class_attrs      (type *clss);
170 INLINE void free_struct_attrs     (type *strct);
171 INLINE void free_method_attrs     (type *method);
172 INLINE void free_union_attrs      (type *uni);
173 INLINE void free_array_attrs      (type *array);
174 INLINE void free_enumeration_attrs(type *enumeration);
175 INLINE void free_pointer_attrs    (type *pointer);
176 INLINE void free_primitive_attrs  (type *primitive);
177
178
179 /** initialize the type module */
180 void init_type (void);
181
182
183 /* ------------------- *
184  *  inline functions   *
185  * ------------------- */
186
187 extern unsigned long type_visited;
188
189 static INLINE void _set_master_type_visited(unsigned long val) { type_visited = val; }
190 static INLINE unsigned long _get_master_type_visited(void)     { return type_visited; }
191 static INLINE void _inc_master_type_visited(void)              { type_visited++; }
192
193 static INLINE void *
194 _get_type_link(const type *tp) {
195   assert(tp && tp->kind == k_type);
196   return(tp -> link);
197 }
198
199 static INLINE void
200 _set_type_link(type *tp, void *l) {
201   assert(tp && tp->kind == k_type);
202   tp -> link = l;
203 }
204
205 static INLINE const tp_op*
206 _get_type_tpop(const type *tp) {
207   assert(tp && tp->kind == k_type);
208   return tp->type_op;
209 }
210
211 static INLINE ident*
212 _get_type_tpop_nameid(const type *tp) {
213   assert(tp && tp->kind == k_type);
214   return get_tpop_ident(tp->type_op);
215 }
216
217 static INLINE tp_opcode
218 _get_type_tpop_code(const type *tp) {
219   assert(tp && tp->kind == k_type);
220   return get_tpop_code(tp->type_op);
221 }
222
223 static INLINE ir_mode *
224 _get_type_mode(const type *tp) {
225   assert(tp && tp->kind == k_type);
226   return tp->mode;
227 }
228
229 static INLINE ident *
230 _get_type_ident(const type *tp) {
231   assert(tp && tp->kind == k_type);
232   return tp->name;
233 }
234
235 static INLINE void
236 _set_type_ident(type *tp, ident* id) {
237   assert(tp && tp->kind == k_type);
238   tp->name = id;
239 }
240
241 static INLINE int
242 _get_type_size_bits(const type *tp) {
243   assert(tp && tp->kind == k_type);
244   return tp->size;
245 }
246
247 static INLINE int
248 _get_type_size_bytes(const type *tp) {
249   int size = _get_type_size_bits(tp);
250   if (size < 0)
251     return -1;
252   if ((size & 7) != 0) {
253     assert(0 && "cannot take byte size of this type");
254     return -1;
255   }
256   return size >> 3;
257 }
258
259 static INLINE type_state
260 _get_type_state(const type *tp) {
261   assert(tp && tp->kind == k_type);
262   return tp->state;
263 }
264
265 static INLINE unsigned long
266 _get_type_visited(const type *tp) {
267   assert(tp && tp->kind == k_type);
268   return tp->visit;
269 }
270
271 static INLINE void
272 _set_type_visited(type *tp, unsigned long num) {
273   assert(tp && tp->kind == k_type);
274   tp->visit = num;
275 }
276
277 static INLINE void
278 _mark_type_visited(type *tp) {
279   assert(tp && tp->kind == k_type);
280   assert(tp->visit < type_visited);
281   tp->visit = type_visited;
282 }
283
284 static INLINE int
285 _type_visited(const type *tp) {
286   assert(tp && tp->kind == k_type);
287   return tp->visit >= type_visited;
288 }
289
290 static INLINE int
291 _type_not_visited(const type *tp) {
292   assert(tp && tp->kind == k_type);
293   return tp->visit  < type_visited;
294 }
295
296 static INLINE int
297 _is_type(const void *thing) {
298   return (get_kind(thing) == k_type);
299 }
300
301 static INLINE int
302 _is_class_type(const type *clss) {
303   assert(clss);
304   return (clss->type_op == type_class);
305 }
306
307 static INLINE int
308 _get_class_n_members (const type *clss) {
309   assert(clss && (clss->type_op == type_class));
310   return (ARR_LEN (clss->attr.ca.members));
311 }
312
313 static INLINE entity *
314 _get_class_member   (const type *clss, int pos) {
315   assert(clss && (clss->type_op == type_class));
316   assert(pos >= 0 && pos < _get_class_n_members(clss));
317   return clss->attr.ca.members[pos];
318 }
319
320 static INLINE int
321 _is_struct_type(const type *strct) {
322   assert(strct);
323   return (strct->type_op == type_struct);
324 }
325
326 static INLINE int
327 _is_method_type(const type *method) {
328   assert(method);
329   return (method->type_op == type_method);
330 }
331
332 static INLINE int
333 _is_union_type(const type *uni) {
334   assert(uni);
335   return (uni->type_op == type_union);
336 }
337
338 static INLINE int
339 _is_array_type(const type *array) {
340   assert(array);
341   return (array->type_op == type_array);
342 }
343
344 static INLINE int
345 _is_enumeration_type(const type *enumeration) {
346   assert(enumeration);
347   return (enumeration->type_op == type_enumeration);
348 }
349
350 static INLINE int
351 _is_pointer_type(const type *pointer) {
352   assert(pointer);
353   return (pointer->type_op == type_pointer);
354 }
355
356 /** Returns true if a type is a primitive type. */
357 static INLINE int
358 _is_primitive_type(const type *primitive) {
359   assert(primitive && primitive->kind == k_type);
360   return (primitive->type_op == type_primitive);
361 }
362
363 static INLINE int
364 _is_atomic_type(const type *tp) {
365   assert(tp && tp->kind == k_type);
366   return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
367       _is_enumeration_type(tp));
368 }
369
370
371 #define set_master_type_visited(val)      _set_master_type_visited(val)
372 #define get_master_type_visited()         _get_master_type_visited()
373 #define inc_master_type_visited()         _inc_master_type_visited()
374 #define get_type_link(tp)                 _get_type_link(tp)
375 #define set_type_link(tp, l)              _set_type_link(tp, l)
376 #define get_type_tpop(tp)                 _get_type_tpop(tp)
377 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
378 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
379 #define get_type_mode(tp)                 _get_type_mode(tp)
380 #define get_type_ident(tp)                _get_type_ident(tp)
381 #define set_type_ident(tp, id)            _set_type_ident(tp, id)
382 #define get_type_size(tp)                 _get_type_size(tp)
383 #define get_type_state(tp)                _get_type_state(tp)
384 #define get_type_visited(tp)              _get_type_visited(tp)
385 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
386 #define mark_type_visited(tp)             _mark_type_visited(tp)
387 #define type_visited(tp)                  _type_visited(tp)
388 #define type_not_visited(tp)              _type_not_visited(tp)
389 #define is_type(thing)                    _is_type(thing)
390 #define is_Class_type(clss)               _is_class_type(clss)
391 #define get_class_n_members(clss)         _get_class_n_members(clss)
392 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
393 #define is_Struct_type(strct)             _is_struct_type(strct)
394 #define is_Method_type(method)            _is_method_type(method)
395 #define is_Union_type(uni)                _is_union_type(uni)
396 #define is_Array_type(array)              _is_array_type(array)
397 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
398 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
399 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
400 #define is_atomic_type(tp)                _is_atomic_type(tp)
401
402 # endif /* _TYPE_T_H_ */