Make it use fourcc.h and unified naming a little.
[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 attributs */
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 attributs */
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 attributs */
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 attributs */
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 attributs */
88 typedef struct {
89   type *points_to;     /**< The type of the enitity 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 attributs. */
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;
116   tp_op *type_op;
117   ident *name;
118   type_state state;        /**< Represents the types state: layout undefined or
119                   fixed. */
120   int size;                /**< Size of an entity of this type. This is determined
121                   when fixing the layout of this class.  Size must be
122                   given in bits. */
123   int align;               /**< Alignment of an entity of this type. This should be
124                                 set according to the source language needs. If not set it's
125                                 calculated automatically by get_type_alignment().
126                                 Alignment must be given in bits. */
127   ir_mode *mode;           /**< The mode for atomic types */
128   unsigned long visit;     /**< visited counter for walks of the type information */
129   void *link;              /**< holds temporary data - like in irnode_t.h */
130   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
131
132   /* ------------- fields for analyses ---------------*/
133
134 #ifdef DEBUG_libfirm
135   int nr;             /**< a unique node number for each node to make output
136                   readable. */
137   char *c_name;                 /**< since idents are opaque, provide the name in cleartext */
138 #endif
139   tp_attr attr;            /* type kind specific fields. This must be the last
140                   entry in this struct!  Varying size! */
141 };
142
143 /**
144  *   Creates a new type representation:
145  *
146  *   @param type_op - the kind of this type.  May not be type_id.
147  *   @param mode    - the mode to be used for this type, may be NULL
148  *   @param name    - an ident for the name of this type.
149  *   @return a new type of the given type.  The remaining private attributes are not
150  *   @return initalized.  The type is in state layout_undefined.
151  *
152  */
153 INLINE type *
154 new_type(tp_op *type_op,
155      ir_mode *mode,
156      ident* name);
157 void free_type_attrs       (type *tp);
158
159 INLINE void free_class_entities      (type *clss);
160 INLINE void free_struct_entities     (type *strct);
161 INLINE void free_method_entities     (type *method);
162 INLINE void free_union_entities      (type *uni);
163 INLINE void free_array_entities      (type *array);
164 INLINE void free_enumeration_entities(type *enumeration);
165 INLINE void free_pointer_entities    (type *pointer);
166 INLINE void free_primitive_entities  (type *primitive);
167
168 INLINE void free_class_attrs      (type *clss);
169 INLINE void free_struct_attrs     (type *strct);
170 INLINE void free_method_attrs     (type *method);
171 INLINE void free_union_attrs      (type *uni);
172 INLINE void free_array_attrs      (type *array);
173 INLINE void free_enumeration_attrs(type *enumeration);
174 INLINE void free_pointer_attrs    (type *pointer);
175 INLINE void free_primitive_attrs  (type *primitive);
176
177
178 /** initialize the type module */
179 void init_type (void);
180
181
182 /* ------------------- *
183  *  inline functions   *
184  * ------------------- */
185
186 extern unsigned long type_visited;
187
188 static INLINE void __set_master_type_visited(unsigned long val) { type_visited = val; }
189 static INLINE unsigned long __get_master_type_visited(void)     { return type_visited; }
190 static INLINE void __inc_master_type_visited(void)              { type_visited++; }
191
192 static INLINE void *
193 __get_type_link(const type *tp) {
194   assert(tp && tp->kind == k_type);
195   return(tp -> link);
196 }
197
198 static INLINE void
199 __set_type_link(type *tp, void *l) {
200   assert(tp && tp->kind == k_type);
201   tp -> link = l;
202 }
203
204 static INLINE tp_op*
205 __get_type_tpop(const type *tp) {
206   assert(tp && tp->kind == k_type);
207   return tp->type_op;
208 }
209
210 static INLINE ident*
211 __get_type_tpop_nameid(const type *tp) {
212   assert(tp && tp->kind == k_type);
213   return get_tpop_ident(tp->type_op);
214 }
215
216 static INLINE tp_opcode
217 __get_type_tpop_code(const type *tp) {
218   assert(tp && tp->kind == k_type);
219   return get_tpop_code(tp->type_op);
220 }
221
222 static INLINE ir_mode *
223 __get_type_mode(const type *tp) {
224   assert(tp && tp->kind == k_type);
225   return tp->mode;
226 }
227
228 static INLINE ident *
229 __get_type_ident(const type *tp) {
230   assert(tp && tp->kind == k_type);
231   return tp->name;
232 }
233
234 static INLINE void
235 __set_type_ident(type *tp, ident* id) {
236   assert(tp && tp->kind == k_type);
237   tp->name = id;
238 }
239
240 static INLINE long
241 __get_type_nr(const type *tp) {
242   assert(tp);
243 #ifdef DEBUG_libfirm
244   return tp->nr;
245 #else
246   return (long)tp;
247 #endif
248 }
249
250 static INLINE int
251 __get_type_size_bits(const type *tp) {
252   assert(tp && tp->kind == k_type);
253   return tp->size;
254 }
255
256 static INLINE int
257 __get_type_size_bytes(const type *tp) {
258   int size = __get_type_size_bits(tp);
259   if (size < 0)
260     return -1;
261   if ((size & 7) != 0) {
262     assert(0 && "cannot take byte size of this type");
263     return -1;
264   }
265   return size >> 3;
266 }
267
268 static INLINE type_state
269 __get_type_state(const type *tp) {
270   assert(tp && tp->kind == k_type);
271   return tp->state;
272 }
273
274 static INLINE unsigned long
275 __get_type_visited(const type *tp) {
276   assert(tp && tp->kind == k_type);
277   return tp->visit;
278 }
279
280 static INLINE void
281 __set_type_visited(type *tp, unsigned long num) {
282   assert(tp && tp->kind == k_type);
283   tp->visit = num;
284 }
285
286 static INLINE void
287 __mark_type_visited(type *tp) {
288   assert(tp && tp->kind == k_type);
289   assert(tp->visit < type_visited);
290   tp->visit = type_visited;
291 }
292
293 static INLINE int
294 __type_visited(const type *tp) {
295   assert(tp && tp->kind == k_type);
296   return tp->visit >= type_visited;
297 }
298
299 static INLINE int
300 __type_not_visited(const type *tp) {
301   assert(tp && tp->kind == k_type);
302   return tp->visit  < type_visited;
303 }
304
305 static INLINE int
306 __is_type(const void *thing) {
307   return (get_kind(thing) == k_type);
308 }
309
310 static INLINE int
311 __is_class_type(const type *clss) {
312   assert(clss);
313   return (clss->type_op == type_class);
314 }
315
316 static INLINE int
317 __get_class_n_members (const type *clss) {
318   assert(clss && (clss->type_op == type_class));
319   return (ARR_LEN (clss->attr.ca.members));
320 }
321
322 static INLINE entity *
323 __get_class_member   (const type *clss, int pos) {
324   assert(clss && (clss->type_op == type_class));
325   assert(pos >= 0 && pos < __get_class_n_members(clss));
326   return clss->attr.ca.members[pos];
327 }
328
329 static INLINE int
330 __is_struct_type(const type *strct) {
331   assert(strct);
332   return (strct->type_op == type_struct);
333 }
334
335 static INLINE int
336 __is_method_type(const type *method) {
337   assert(method);
338   return (method->type_op == type_method);
339 }
340
341 static INLINE int
342 __is_union_type(const type *uni) {
343   assert(uni);
344   return (uni->type_op == type_union);
345 }
346
347 static INLINE int
348 __is_array_type(const type *array) {
349   assert(array);
350   return (array->type_op == type_array);
351 }
352
353 static INLINE int
354 __is_enumeration_type(const type *enumeration) {
355   assert(enumeration);
356   return (enumeration->type_op == type_enumeration);
357 }
358
359 static INLINE int
360 __is_pointer_type(const type *pointer) {
361   assert(pointer);
362   return (pointer->type_op == type_pointer);
363 }
364
365 /** Returns true if a type is a primitive type. */
366 static INLINE int
367 __is_primitive_type(const type *primitive) {
368   assert(primitive && primitive->kind == k_type);
369   return (primitive->type_op == type_primitive);
370 }
371
372 static INLINE int
373 __is_atomic_type(const type *tp) {
374   assert(tp && tp->kind == k_type);
375   return (is_primitive_type(tp) || is_pointer_type(tp) ||
376       is_enumeration_type(tp));
377 }
378
379
380 #define set_master_type_visited(val)      __set_master_type_visited(val)
381 #define get_master_type_visited()         __get_master_type_visited()
382 #define inc_master_type_visited()         __inc_master_type_visited()
383 #define get_type_link(tp)                 __get_type_link(tp)
384 #define set_type_link(tp, l)              __set_type_link(tp, l)
385 #define get_type_tpop(tp)                 __get_type_tpop(tp)
386 #define get_type_tpop_nameid(tp)          __get_type_tpop_nameid(tp)
387 #define get_type_tpop_code(tp)            __get_type_tpop_code(tp)
388 #define get_type_mode(tp)                 __get_type_mode(tp)
389 #define get_type_ident(tp)                __get_type_ident(tp)
390 #define set_type_ident(tp, id)            __set_type_ident(tp, id)
391 #define get_type_nr(tp)                   __get_type_nr(tp)
392 #define get_type_size(tp)                 __get_type_size(tp)
393 #define get_type_state(tp)                __get_type_state(tp)
394 #define get_type_visited(tp)              __get_type_visited(tp)
395 #define set_type_visited(tp, num)         __set_type_visited(tp, num)
396 #define mark_type_visited(tp)             __mark_type_visited(tp)
397 #define type_visited(tp)                  __type_visited(tp)
398 #define type_not_visited(tp)              __type_not_visited(tp)
399 #define is_type(thing)                    __is_type(thing)
400 #define is_class_type(clss)               __is_class_type(clss)
401 #define get_class_n_members(clss)         __get_class_n_members(clss)
402 #define get_class_member(clss, pos)       __get_class_member(clss, pos)
403 #define is_struct_type(strct)             __is_struct_type(strct)
404 #define is_method_type(method)            __is_method_type(method)
405 #define is_union_type(uni)                __is_union_type(uni)
406 #define is_array_type(array)              __is_array_type(array)
407 #define is_enumeration_type(enumeration)  __is_enumeration_type(enumeration)
408 #define is_pointer_type(pointer)          __is_pointer_type(pointer)
409 #define is_primitive_type(primitive)      __is_primitive_type(primitive)
410 #define is_atomic_type(tp)                __is_atomic_type(tp)
411
412 # endif /* _TYPE_T_H_ */