6f7e0e023466a6dce070a7f02ad03db5b4870420
[libfirm] / ir / tr / type.h
1 /****h* libfirm/type
2  *
3  * NAME
4  *   file type.h - datastructure to hold type information.
5  * COPYRIGHT
6  *  (C) 2001 by Universitaet Karlsruhe
7  * AUTHORS
8  *  Goetz Lindenmaier
9  *
10  * NOTES
11  *  This module supplies a datastructure to represent all types
12  *  known in the compiled program.  This includes types specified
13  *  in the program as well as types defined by the language.  In the
14  *  view of the intermediate representation there is no difference
15  *  between these types.
16  *
17  *  There exist several kinds of types, arranged by the structure of
18  *  the type.  A type is described by a set of attributes.  Some of
19  *  these attributes are common to all types, others depend on the
20  *  kind of the type.
21  *
22  *  Types are different from the modes defined in irmode:  Types are
23  *  on the level of the programming language, modes at the level of
24  *  the target processor.
25  *
26  * SEE ALSO
27  *   tpop.h
28  *****
29  */
30 # ifndef _TYPE_H_
31 # define _TYPE_H_
32
33 # include "tpop.h"
34 # include "common.h"
35 # include "ident.h"
36 # include "irmode.h"
37 # include "bool.h"
38
39 #ifndef _ENTITY_TYPEDEF_
40 #define _ENTITY_TYPEDEF_
41 /* to resolve recursion between entity.h and type.h */
42 typedef struct entity entity;
43 #endif
44
45 /****s* type/type
46  *
47  * NAME
48  *   type - An abstract data type to represent types.
49  * NOTE
50  *  This is the abstract data type with which any type known in the
51  *  compiled program can be represented.  This includes types specified
52  *  in the program as well as types defined by the language.  In the
53  *  view of the intermediate representation there is no difference
54  *  between these types.
55  *
56  *  There exist several kinds of types, arranged by the structure of
57  *  the type.  These are distinguished by a type opcode.
58  *  A type is described by a set of attributes.  Some of these attributes
59  *  are common to all types, others depend on the kind of the type.
60  *
61  *  The following describes the common attributes.  They can only be
62  *  accessed by the functions given below.
63  *
64  * ATTRIBUTES
65  *  The common fields are:
66  *
67  *  firm_kind    A firm_kind tag containing k_type.  This is useful
68  *               for dynamically checking whether a node is a type node.
69  *  type_op      A tp_op specifying the kind of the type.
70  *  mode         The mode to be used to represent the type on a machine.
71  *               @@@ maybe not global field??
72  *  name         An identifier specifying the name of the type.  To be
73  *               set by the frontend.
74  *  size         The size of the type, i.e. an entity of this type will
75  *               occupy size bytes in memory.  In several cases this is
76  *               determined when fixing the layout of this type (class,
77  *               struct, union, array, enumeration).
78  *  visit        A counter for walks of the type information.
79  *
80  *  These fields can only be accessed via access functions.
81  *
82  *  Depending on the value of type_op, i.e., depending on the kind of the
83  *  type the adt contains further attributes.  These are documented below.
84  * SEE ALSO
85  *   class, struct, method, union, array, enumeration, pointer, primitive
86  * SOURCE
87  */
88 typedef struct type type;
89
90 tp_op*      get_type_tpop(type *tp);
91 ident*      get_type_tpop_nameid(type *tp);
92 const char* get_type_tpop_name(type *tp);
93 tp_opcode   get_type_tpop_code(type *tp);
94
95 ir_mode*    get_type_mode(type *tp);
96 void        set_type_mode(type *tp, ir_mode* m);
97
98 ident*      get_type_nameid(type *tp);
99 void        set_type_nameid(type *tp, ident* id);
100 const char* get_type_name(type *tp);
101
102 int         get_type_size(type *tp);
103 void        set_type_size(type *tp, int size);
104
105 unsigned long get_type_visited(type *tp);
106 void        set_type_visited(type *tp, unsigned long num);
107 /* Sets visited field in type to type_visited. */
108 void        mark_type_visited(type *tp);
109 /*****/
110
111 /****v* type/visited
112  *
113  * NAME
114  *   type_visited -  visited flag to traverse the type information
115  * PURPOSE
116  *   Increase this flag by one before traversing the type information.
117  *   Mark type nodes as visited by set_type_visited(type, type_visited).
118  *   Check whether node was already visited by comparing get_type_visited(type)
119  *   and type_visited.
120  *   Or use the function to walk all types.
121  * SEE ALSO
122  *   typewalk
123  * SOURCE
124  */
125 extern unsigned long type_visited;
126 /*****/
127
128 /****f* type/is_type
129  *
130  * NAME
131  *   is_type - Checks whether a pointer points to a type.
132  * SYNOPSIS
133  *   bool is_type            (void *thing);
134  * INPUTS
135  *   thing - a pointer
136  * RESULT
137  *   true if the thing is a type, else false
138  ***
139  */
140 int is_type            (void *thing);
141
142 /****** type/class
143  * NAME
144  *  Representation of a class type.
145  * NOTE
146  *  If the type opcode is set to type_class the type represents class
147  *  types.  A list of fields and methods is associated with a class.
148  *  Further a class can inherit from and bequest to other classes.
149  *  @@@ value class???
150  * ATTRIBUTES
151  *  The following attributes are private to this type kind.
152  *  member     All entities belonging to this class.  This are methode entities
153  *             which have type_method or fields that can have any of the
154  *             following type kinds: type_class, type_struct, type_union,
155  *             type_array, type_enumeration, type_pointer, type_primitive.
156  *
157  *  subtypes   A list of direct subclasses.
158  *
159  *  supertypes A list of direct superclasses.
160  *
161  *  These are dynamic lists that can be grown with an "add_" function,
162  *  but not shrinked.
163  * SOURCE
164  */
165 /* create a new class type */
166 type   *new_type_class (ident *name);
167
168 /* manipulate private fields of class type  */
169 void    add_class_member   (type *clss, entity *member);
170 int     get_class_n_member (type *clss);
171 entity *get_class_member   (type *clss, int pos);
172 void    set_class_member   (type *clss, entity *member, int pos);
173
174 void    add_class_subtype   (type *clss, type *subtype);
175 int     get_class_n_subtype (type *clss);
176 type   *get_class_subtype   (type *clss, int pos);
177 void    set_class_subtype   (type *clss, type *subtype, int pos);
178
179 void    add_class_supertype   (type *clss, type *supertype);
180 int     get_class_n_supertype (type *clss);
181 type   *get_class_supertype   (type *clss, int pos);
182 void    set_class_supertype   (type *clss, type *supertype, int pos);
183
184 /* typecheck */
185 bool    is_class_type(type *clss);
186 /*****/
187
188 /****** type/struct
189  * NAME
190  *  Representation of a struct type.
191  * NOTE
192  *  Type_strct represents aggregate types that consist of a list
193  *  of fields.
194  * ATTRIBUTES
195  *  member   All entities belonging to this class.  This are the fields
196  *           that can have any of the following types:  type_class,
197  *           type_struct, type_union, type_array, type_enumeration,
198  *           type_pointer, type_primitive.
199  *           This is a dynamic list that can be grown with an "add_" function,
200  *           but not shrinked.
201  *           This is a dynamic list that can be grown with an "add_" function,
202  *           but not shrinked.
203  * SOURCE
204  */
205 /* create a new type struct */
206 type   *new_type_struct (ident *name);
207
208 /* manipulate private fields of struct */
209 void    add_struct_member   (type *strct, entity *member);
210 int     get_struct_n_member (type *strct);
211 entity *get_struct_member   (type *strct, int pos);
212 void    set_struct_member   (type *strct, int pos, entity *member);
213
214 /* typecheck */
215 bool    is_struct_type(type *strct);
216 /*****/
217
218 /****** type/method
219  * NAME
220  *  Representation of a method type.
221  * NOTE
222  *  A method type represents a method, function or procedure type.
223  *  It contains a list of the parameter and result types, as these
224  *  are part of the type description.  These lists should not
225  *  be changed by a optimization, as a change creates a new method
226  *  type.  Therefore optimizations should allocated new method types.
227  *  The set_ routines are only for construction by a frontend.
228  * ATTRIBUTES
229  *  n_params    Number of parameters to the procedure.
230  *              A procedure in FIRM has only call by value parameters.
231  *
232  *  param_type  A list with the types of parameters.  This list is ordered.
233  *              The nth type in this list corresponds to the nth element
234  *              in the parameter tuple that is a result of the start node.
235  *              (See ircons.h for more information.)
236  *
237  *  n_res       The number of results of the method.  In general, procedures
238  *              have zero results, functions one.
239  *
240  *  res_type    A list with the types of parameters.  This list is ordered.
241  *              The nth type in this list corresponds to the nth input to
242  *              Return nodes.  (See ircons.h for more information.)
243  * SOURCE
244  */
245
246 /* Create a new method type.
247    N_param is the number of parameters, n_res the number of results.
248    The arrays for the parameter and result types are not initialized by
249    the constructor. */
250 type *new_type_method (ident *name, int n_param, int n_res);
251
252 /* manipulate private fields of method. */
253 int   get_method_n_params  (type *method);
254 type *get_method_param_type(type *method, int pos);
255 void  set_method_param_type(type *method, int pos, type* type);
256
257 int   get_method_n_res   (type *method);
258 type *get_method_res_type(type *method, int pos);
259 void  set_method_res_type(type *method, int pos, type* type);
260
261 /* typecheck */
262 bool  is_method_type     (type *method);
263 /*****/
264
265 /****** type/union
266  * NAME
267  *   Representation of a union type.
268  * NOTE
269  *   The union type represents union types.
270  * ATTRIBUTES
271  *   n_types        Number of unioned types.
272  *   members        Entities for unioned types.  Fixed length array.
273  *                  This is a dynamic list that can be grown with an "add_" function,
274  *                  but not shrinked.
275  * SOURCE
276  */
277 /* create a new type union  */
278 type   *new_type_union (ident *name);
279
280 /* manipulate private fields of struct */
281 int     get_union_n_members      (type *uni);
282 void    add_union_member (type *uni, entity *member);
283 entity *get_union_member (type *uni, int pos);
284 void    set_union_member (type *uni, int pos, entity *member);
285
286 /* typecheck */
287 bool    is_union_type          (type *uni);
288 /*****/
289
290 #if 0
291 /* We don't need these if the union has entities, which it now
292    does. The entities are necessary for the analysis algorithms. */
293 type  *get_union_unioned_type (type *uni, int pos);
294 void   set_union_unioned_type (type *uni, int pos, type *type);
295
296 ident *get_union_delim_nameid (type *uni, int pos);
297 const char *get_union_delim_name (type *uni, int pos);
298 void   set_union_delim_nameid (type *uni, int pos, ident *id);
299 #endif
300
301 /****** type/array
302  * NAME
303  *   Representation of an array type.
304  * NOTE
305  *   The array type represents rectangular multi dimensional arrays.
306  * ATTRIBUTES
307  *   n_dimensions     Number of array dimensions.
308  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
309  *   *upper_bound     Upper bounds or dimensions.
310  *   *element_type    The type of the array elements.
311  * SOURCE
312  */
313 /* create a new type array -- set dimension sizes independently */
314 type *new_type_array         (ident *name, int n_dimensions);
315
316 /* manipulate private fields of array type */
317 int   get_array_n_dimensions (type *array);
318 void  set_array_bounds       (type *array, int dimension, int lower_bound,
319                                                           int upper_bound);
320 void  set_array_lower_bound  (type *array, int dimension, int lower_bound);
321 void  set_array_upper_bound  (type *array, int dimension, int upper_bound);
322 int   get_array_lower_bound  (type *array, int dimension);
323 int   get_array_upper_bound  (type *array, int dimension);
324
325 void  set_array_element_type (type *array, type *type);
326 type *get_array_element_type (type *array);
327
328 /* typecheck */
329 bool   is_array_type         (type *array);
330 /*****/
331
332 /****** type/enumeration
333  * NAME
334  *  Representation of an enumeration type.
335  * NOTE
336  *  Enumeration types need not necessarily be represented explicitly
337  *  by Firm types, as the frontend can lower them to integer constants as
338  *  well.  For debugging purposes or similar tasks this information is useful.
339  * ATTRIBUTES
340  *   *enum           The target values representing the constants used to
341  *                   represent individual enumerations.
342  *   *enum_nameid    Idents containing the source program name of the enumeration
343  *                   constants
344  *
345 *****
346 */
347 /* create a new type enumeration -- set the enumerators independently */
348 type   *new_type_enumeration    (ident *name, int n_enums);
349
350 /* manipulate fields of enumeration type. */
351 int     get_enumeration_n_enums (type *enumeration);
352
353 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
354 tarval *get_enumeration_enum    (type *enumeration, int pos);
355
356 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
357 ident  *get_enumeration_nameid  (type *enumeration, int pos);
358 const char *get_enumeration_name(type *enumeration, int pos);
359
360 /* typecheck */
361 bool    is_enumeration_type     (type *enumeration);
362 /*****/
363
364 /****** type/pointer
365  * NAME
366  *   Representation of a pointer type.
367  * NOTE
368  *   Pointer types.
369  * ATTRIBUTES
370  *   points_to       The type of the entity this pointer points to.
371  * SOURCE
372  */
373 /* Create a new type pointer */
374 type *new_type_pointer           (ident *name, type *points_to);
375
376 /* manipulate fields of type_pointer */
377 void  set_pointer_points_to_type (type *pointer, type *type);
378 type *get_pointer_points_to_type (type *pointer);
379
380 /* typecheck */
381 bool  is_pointer_type            (type *pointer);
382 /*****/
383
384 /****** type/primitive
385  * NAME
386  *   Representation of a primitive type.
387  * NOTE
388  *   Primitive types are types that represent indivisible data values that
389  *   map directly to modes.  They don't have a private attribute.  The
390  *   important information they carry is held in the common mode field.
391  * SOURCE
392 */
393 /* create a new type primitive */
394 type *new_type_primitive (ident *name, ir_mode *mode);
395
396 /* typecheck */
397 bool  is_primitive_type  (type *primitive);
398 /*****/
399
400 # endif /* _TYPE_H_ */