9d567598143b2c86c830c17cc1ae6931f157d46e
[libfirm] / ir / tr / type.h
1 /**
2  *
3  *   file type.h - datastructure to hold type information.
4  *  (C) 2001 by Universitaet Karlsruhe
5  *  Goetz Lindenmaier
6  *
7  *  This module supplies a datastructure to represent all types
8  *  known in the compiled program.  This includes types specified
9  *  in the program as well as types defined by the language.  In the
10  *  view of the intermediate representation there is no difference
11  *  between these types.
12  *
13  *  There exist several kinds of types, arranged by the structure of
14  *  the type.  A type is described by a set of attributes.  Some of
15  *  these attributes are common to all types, others depend on the
16  *  kind of the type.
17  *
18  *  Types are different from the modes defined in irmode:  Types are
19  *  on the level of the programming language, modes at the level of
20  *  the target processor.
21  *
22  * @see  tpop.h
23  */
24
25 /* $Id$ */
26
27 # ifndef _TYPE_H_
28 # define _TYPE_H_
29
30 # include "tpop.h"
31 # include "firm_common.h"
32 # include "ident.h"
33 # include "irmode.h"
34 # include <stdbool.h>
35 # include "dbginfo.h"
36
37
38 /* to resolve recursion between entity.h and type.h */
39 #ifndef _ENTITY_TYPEDEF_
40 #define _ENTITY_TYPEDEF_
41 typedef struct entity entity;
42 #endif
43
44 #ifndef _IR_NODE_TYPEDEF_
45 #define _IR_NODE_TYPEDEF_
46 typedef struct ir_node ir_node;
47 #endif
48
49 /**
50  *
51  *   An abstract data type to represent types.
52  *  This is the abstract data type with which any type known in the
53  *  compiled program can be represented.  This includes types specified
54  *  in the program as well as types defined by the language.  In the
55  *  view of the intermediate representation there is no difference
56  *  between these types.
57  *
58  *  There exist several kinds of types, arranged by the structure of
59  *  the type.  These are distinguished by a type opcode.
60  *  A type is described by a set of attributes.  Some of these attributes
61  *  are common to all types, others depend on the kind of the type.
62  *
63  *  The following describes the common attributes.  They can only be
64  *  accessed by the functions given below.
65  *
66  *  @param The common fields are:
67  *
68  *  @param firm_kind    A firm_kind tag containing k_type.  This is useful
69  *               for dynamically checking whether a node is a type node.
70  *  @param type_op      A tp_op specifying the kind of the type.
71  *  @param mode         The mode to be used to represent the type on a machine.
72  *               @@@ maybe not global field??
73  *  @param name         An identifier specifying the name of the type.  To be
74  *               set by the frontend.
75  *  @param size         The size of the type, i.e. an entity of this type will
76  *               occupy size bytes in memory.  In several cases this is
77  *               determined when fixing the layout of this type (class,
78  *               struct, union, array, enumeration).
79  *  @param state        The state of the type.  The state represents whether the
80  *               layout of the type is undefined or fixed (values: layout_undefined
81  *               or layout_fixed).  Compound types can have an undefined
82  *               layout.  The layout of the basic types primitive and pointer
83  *               is always layout_fixed.  If the layout of
84  *               compound types is fixed all entities must have an offset
85  *               and the size of the type must be set.
86  *               A fixed layout for enumeration types means that each enumeration
87  *               is associated with an implementation value.
88  *  @param visit        A counter for walks of the type information.
89  *  @param link         A void* to associate some additional information with the type.
90  *
91  *  @param These fields can only be accessed via access functions.
92  *
93  *  @param Depending on the value of type_op, i.e., depending on the kind of the
94  *  @param type the adt contains further attributes.  These are documented below.
95  * @see  class, struct, method, union, array, enumeration, pointer, primitive
96  */
97 #ifndef _TYPE_TYPEDEF_
98 #define _TYPE_TYPEDEF_
99 typedef struct type type;
100 #endif
101
102 # include "type_or_entity.h"
103
104 /* Frees the memory used by the type.   Does not free the entities
105    belonging to the type, except for the array element entity.  */
106 void        free_type(type *tp);
107
108 tp_op*      get_type_tpop(type *tp);
109 ident*      get_type_tpop_nameid(type *tp);
110 const char* get_type_tpop_name(type *tp);
111 tp_opcode   get_type_tpop_code(type *tp);
112
113 ident*      get_type_ident(type *tp);
114 void        set_type_ident(type *tp, ident* id);
115 const char* get_type_name(type *tp);
116
117 typedef enum {
118   layout_undefined,    /* The layout of this type is not defined.
119                           Address computation to access fields is not
120                           possible, fields must be accessed by Sel
121                           nodes.  This is the default value except for
122                           pointer, primitive and method types. */
123   layout_fixed         /* The layout is fixed, all component/member entities
124                           have an offset assigned.  Size of the type is known.
125                           Arrays can be accessed by explicit address
126                           computation. Default for pointer, primitive ane method
127                           types.  */
128 } type_state;
129 type_state  get_type_state(type *tp);
130 /* For primitives, pointer and method types the layout is always fixed.
131    This call is legal but has no effect. */
132 void        set_type_state(type *tp, type_state state);
133
134 /* Returns NULL for all non atomic types. */
135 ir_mode*    get_type_mode(type *tp);
136 /* Only has an effect on primitive and enumeration types */
137 void        set_type_mode(type *tp, ir_mode* m);
138
139 int         get_type_size(type *tp);
140 /* For primitive, enumeration, pointer and method types the size
141    is always fixed. This call is legal but has no effect. */
142 void        set_type_size(type *tp, int size);
143
144
145 unsigned long get_type_visited(type *tp);
146 void          set_type_visited(type *tp, unsigned long num);
147 /* Sets visited field in type to type_visited. */
148 void          mark_type_visited(type *tp);
149
150 void*         get_type_link(type *tp);
151 void          set_type_link(type *tp, void *l);
152
153 /**
154  *
155  *   visited flag to traverse the type information
156  *   Increase this flag by one before traversing the type information.
157  *   Mark type nodes as visited by set_type_visited(type, type_visited).
158  *   Check whether node was already visited by comparing get_type_visited(type)
159  *   and type_visited.
160  *   Or use the function to walk all types.
161  * @see  typewalk
162  */
163 extern unsigned long type_visited;
164 void          set_master_type_visited(unsigned long val);
165 unsigned long get_master_type_visited();
166 void          inc_master_type_visited();
167
168 /**
169  *
170  *   Checks whether a pointer points to a type.
171  *   @param thing - a pointer
172  *   @return true if the thing is a type, else false
173  *
174  */
175 int is_type            (void *thing);
176
177 /**
178  *
179  *   Checks whether two types are structural equal.
180  *   @param two pointer types
181  *   @return true if the types are equal, else false.
182  *   @return Types are equal if
183  *    - they are the same type kind
184  *    - they have the same name
185  *    - they have the same mode (if applicable)
186  *    - they have the same type_state and, ev., the same size
187  *    - they are class types and have
188  *      - the same members (see same_entity in entity.h)
189  *      - the same supertypes -- the C-pointers are compared --> no recursive call.
190  *      - the same number of subtypes.  Subtypes are not compared,
191  *        as this could cause a cyclic test.
192  *      - the same peculiarity
193  *    - they are structure types and have the same members
194  *    - they are method types and have
195  *      - the same parameter types
196  *      - the same result types
197  *    - they are union types and have the same members
198  *    - they are array types and have
199  *      - the same number of dimensions
200  *      - the same dimension bounds
201  *      - the same dimension order
202  *      - the same element type
203  *    - they are enumeration types and have the same enumerator names
204  *    - they are pointer types and have the identical points_to type
205  *      (i.e., the same C-struct to represent the type, type_id is skipped.
206  *       This is to avoid endless recursions; with pointer types circlic
207  *       type graphs are possible.)
208  *
209  *
210  */
211 bool equal_type(type *tpy1, type *typ2);
212
213 /**
214  *
215  *   Checks whether two types are structural comparable.
216  *   @param two pointer type
217  *   @return true if type st is smaller than type lt, i.e. whenever
218  *   @return lt is expected a st can be used.
219  *   @return This is true if
220  *    - they are the same type kind
221  *    - mode(st) < mode (lt)  (if applicable)
222  *    - they are class types and st is (transitive) subtype of lt,
223  *    - they are structure types and
224  *       - the members of st have exactly one counterpart in lt with the same name,
225  *       - the counterpart has a bigger type.
226  *    - they are method types and have
227  *      - the same number of parameter and result types,
228  *      - the parameter types of st are smaller than those of lt,
229  *      - the result types of st are smaller than those of lt
230  *    - they are union types and have the members of st have exactly one
231  *      @return counterpart in lt and the type is smaller
232  *    - they are array types and have
233  *      - the same number of dimensions
234  *      - all bounds of lt are bound of st
235  *      - the same dimension order
236  *      - the same element type
237  *      @return or
238  *      - the element type of st is smaller than that of lt
239  *      - the element types have the same size and fixed layout.
240  *    - they are enumeration types and have the same enumerator names
241  *    - they are pointer types and have the points_to type of st is
242  *      @return smaller than the points_to type of lt.
243  *
244  */
245 bool smaller_type (type *st, type *lt);
246
247 /**
248  *  Representation of a class type.
249  *  If the type opcode is set to type_class the type represents class
250  *  types.  A list of fields and methods is associated with a class.
251  *  Further a class can inherit from and bequest to other classes.
252  *  @@@ value class???
253  *  @param The following attributes are private to this type kind.
254  *  @param member     All entities belonging to this class.  This are methode entities
255  *             which have type_method or fields that can have any of the
256  *             following type kinds: type_class, type_struct, type_union,
257  *             type_array, type_enumeration, type_pointer, type_primitive.
258  *
259  *  @param subtypes   A list of direct subclasses.
260  *
261  *  @param supertypes A list of direct superclasses.
262  *
263  *  @param These are dynamic lists that can be grown with an "add_" function,
264  *  @param but not shrinked.
265  *
266  *  @param peculiarity The peculiarity of this class.  If the class is of peculiarity
267  *             "description" it only is a description of requirememts to a class,
268  *             as, e.g., a Java interface.  The class will never be allocated.
269  *             Peculiatity inherited is only possible for entities.  An entity
270  *             is of peculiarity inherited if the compiler generated the entity
271  *             to explicitly resolve inheritance.  An inherited method entity has
272  *             no value for irg.
273  *             Values: description, existent, inherited.  Default: existent.
274  *
275  */
276 /* create a new class type */
277 type   *new_type_class (ident *name);
278 type   *new_d_type_class (ident *name, dbg_info *db);
279
280 /** manipulate private fields of class type  **/
281 /* Adds the entity as member of the class.  */
282 void    add_class_member   (type *clss, entity *member);
283 /* Returns the number of members of this class. */
284 int     get_class_n_members (type *clss);
285 /* Returns the member at position pos, 0 <= pos < n_member */
286 entity *get_class_member   (type *clss, int pos);
287 /* Overwrites the member at position pos, 0 <= pos < n_member with
288    the passed entity. */
289 void    set_class_member   (type *clss, entity *member, int pos);
290 /* Replaces complete member list in class type by the list passed.  Copies the
291    list passed. This function is necessary to reduce the number of members.
292    members is an array of entities, num the size of this array.  Sets all
293    owners of the members passed to clss. */
294 void    set_class_members  (type *clss, entity *members[], int arity);
295 /* Finds member in the list of members and removes it.
296    Shrinks the member list, so iterate from the end!!!
297    Does not deallocate the entity.  */
298 void    remove_class_member(type *clss, entity *member);
299
300
301 /* Adds subtype as subtype to clss.
302    Checks whether clss is a supertype of subtype.  If not
303    adds also clss as supertype to subtype.  */
304 void    add_class_subtype   (type *clss, type *subtype);
305 /* Returns the number of subtypes */
306 int     get_class_n_subtypes (type *clss);
307 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
308 type   *get_class_subtype   (type *clss, int pos);
309 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype.  Does not
310    set the corresponding supertype relation for subtype: this might
311    be a different position! */
312 void    set_class_subtype   (type *clss, type *subtype, int pos);
313 /* Finds subtype in the list of subtypes and removes it  */
314 void    remove_class_subtype(type *clss, type *subtype);
315
316
317 /* Adds supertype as supertype to class.
318    Checks whether clss is a subtype of supertype.  If not
319    adds also clss as subtype to supertype.  */
320 void    add_class_supertype   (type *clss, type *supertype);
321 /* Returns the number of supertypes */
322 int     get_class_n_supertypes (type *clss);
323 /* Gets the supertype at position pos,  0 <= pos < n_supertype. */
324 type   *get_class_supertype   (type *clss, int pos);
325 /* Sets the supertype at postition pos, 0 <= pos < n_subtype.  Does not
326    set the corresponding subtype relation for supertype: this might
327    be a different position! */
328 void    set_class_supertype   (type *clss, type *supertype, int pos);
329 /* Finds supertype in the list of supertypes and removes it */
330 void    remove_class_supertype(type *clss, type *supertype);
331
332 /* This enumeration flags the peculiarity of entities and types. */
333 typedef enum peculiarity {
334   description,     /* Represents only a description.  The entity/type is never
335                       allocated, no code/data exists for this entity/type. */
336   inherited,       /* Describes explicitly that other entities are
337                       inherited to the owner of this entity.
338                       Overwrites must refer to at least one other
339                       entity.  If this is a method entity there exists
340                       no irg for this entity, only for one of the
341                       overwritten ones. */
342   existent         /* The entity/type (can) exist. */
343 } peculiarity;
344
345 /* The peculiarity of the class.  The enumeration peculiarity is defined
346    in entity.h */
347 INLINE peculiarity get_class_peculiarity (type *clss);
348 INLINE void        set_class_peculiarity (type *clss, peculiarity pec);
349
350 /* Set and get a class' dfn --
351    @@@ This is an undocumented field, subject to change! */
352 void set_class_dfn (type *clss, int dfn);
353 int  get_class_dfn (type *clss);
354
355 /* typecheck */
356 bool is_class_type(type *clss);
357 /* Returns true if low is subclass of high. */
358 bool is_subclass_of(type *low, type *high);
359
360 /**
361  *  Representation of a struct type.
362  *  Type_strct represents aggregate types that consist of a list
363  *  of fields.
364  *  @param member   All entities belonging to this class.  This are the fields
365  *           that can have any of the following types:  type_class,
366  *           type_struct, type_union, type_array, type_enumeration,
367  *           type_pointer, type_primitive.
368  *           This is a dynamic list that can be grown with an "add_" function,
369  *           but not shrinked.
370  *           This is a dynamic list that can be grown with an "add_" function,
371  *           but not shrinked.
372  */
373 /* create a new type struct */
374 type   *new_type_struct (ident *name);
375 type   *new_d_type_struct (ident *name, dbg_info* db);
376
377 /* manipulate private fields of struct */
378 void    add_struct_member   (type *strct, entity *member);
379 int     get_struct_n_members (type *strct);
380 entity *get_struct_member   (type *strct, int pos);
381 void    set_struct_member   (type *strct, int pos, entity *member);
382 /* Finds member in the list of memberss and removees it */
383 void    remove_struct_member (type *strct, entity *member);
384
385 /* typecheck */
386 bool    is_struct_type(type *strct);
387
388 /**
389  *  Representation of a method type.
390  *  A method type represents a method, function or procedure type.
391  *  It contains a list of the parameter and result types, as these
392  *  are part of the type description.  These lists should not
393  *  be changed by a optimization, as a change creates a new method
394  *  type.  Therefore optimizations should allocated new method types.
395  *  The set_ routines are only for construction by a frontend.
396  *  @param n_params    Number of parameters to the procedure.
397  *              A procedure in FIRM has only call by value parameters.
398  *
399  *  @param param_type  A list with the types of parameters.  This list is ordered.
400  *              The nth type in this list corresponds to the nth element
401  *              in the parameter tuple that is a result of the start node.
402  *              (See ircons.h for more information.)
403  *
404  *  @param n_res       The number of results of the method.  In general, procedures
405  *              have zero results, functions one.
406  *
407  *  @param res_type    A list with the types of parameters.  This list is ordered.
408  *              The nth type in this list corresponds to the nth input to
409  *              Return nodes.  (See ircons.h for more information.)
410  */
411
412 /* Create a new method type.
413    N_param is the number of parameters, n_res the number of results.
414    The arrays for the parameter and result types are not initialized by
415    the constructor. */
416 type *new_type_method (ident *name, int n_param, int n_res);
417 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
418
419 /* manipulate private fields of method. */
420 int   get_method_n_params  (type *method);
421 type *get_method_param_type(type *method, int pos);
422 void  set_method_param_type(type *method, int pos, type* tp);
423
424 int   get_method_n_ress   (type *method);
425 type *get_method_res_type(type *method, int pos);
426 void  set_method_res_type(type *method, int pos, type* tp);
427
428 /*
429  * this enum flags the variadicity of methods (methods with a
430  * variable amount of arguments (e.g. C's printf). Default is
431  * non_variadic.
432  */
433 typedef enum variadicity {
434   non_variadic,
435   variadic
436 } variadicity;
437
438 variadicity get_method_variadicity(type *method);
439 void set_method_variadicity(type *method, variadicity vari);
440
441 /* typecheck */
442 bool  is_method_type     (type *method);
443
444 /**
445  *   Representation of a union type.
446  *   The union type represents union types.
447  *   @param n_types        Number of unioned types.
448  *   @param members        Entities for unioned types.  Fixed length array.
449  *                  This is a dynamic list that can be grown with an "add_" function,
450  *                  but not shrinked.
451  */
452 /* create a new type union  */
453 type   *new_type_union (ident *name);
454 type   *new_d_type_union (ident *name, dbg_info* db);
455
456 /* manipulate private fields of struct */
457 int     get_union_n_members      (type *uni);
458 void    add_union_member (type *uni, entity *member);
459 entity *get_union_member (type *uni, int pos);
460 void    set_union_member (type *uni, int pos, entity *member);
461 /* Finds member in the list of members and removes it. */
462 void    remove_union_member (type *uni, entity *member);
463
464 /* typecheck */
465 bool    is_union_type          (type *uni);
466
467 /**
468  *   Representation of an array type.
469  *   The array type represents rectangular multi dimensional arrays.
470  *   The constants representing the bounds must be allocated to
471  *   get_const_code_irg() by setting current_ir_graph accordingly.
472  *   @param n_dimensions     Number of array dimensions.
473  *   @param *lower_bound     Lower bounds of dimensions.  Usually all 0.
474  *   @param *upper_bound     Upper bounds or dimensions.
475  *   @param *element_type    The type of the array elements.
476  *   @param *element_ent     An entity for the array elements to be used for
477  *                    element selection with Sel.
478  *                    @@@ Do we need several entities?  One might want
479  *                    to select a dimension and not a single element in
480  *                    case of multidim arrays.
481  */
482 /* create a new type array --
483    Sets n_dimension to dimension and all dimension entries to NULL.
484    Initializes order to the order of the dimensions.
485    The entity for array elements is built automatically.
486    Set dimension sizes after call to constructor with set_* routines. */
487 type *new_type_array         (ident *name, int n_dimensions,
488                               type *element_type);
489 type *new_d_type_array         (ident *name, int n_dimensions,
490                               type *element_type, dbg_info* db);
491
492 /* manipulate private fields of array type */
493 int   get_array_n_dimensions (type *array);
494 /* Allocates Const nodes of mode_I for the array dimensions */
495 void  set_array_bounds_int   (type *array, int dimension, int lower_bound,
496                                                           int upper_bound);
497 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
498                                                           ir_node *upper_bound);
499 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
500 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound);
501 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
502 void  set_array_upper_bound_int (type *array, int dimension, int lower_bound);
503 ir_node * get_array_lower_bound  (type *array, int dimension);
504 ir_node * get_array_upper_bound  (type *array, int dimension);
505
506 void set_array_order (type *array, int dimension, int order);
507 int  get_array_order (type *array, int dimension);
508
509 void  set_array_element_type (type *array, type *tp);
510 type *get_array_element_type (type *array);
511
512 void  set_array_element_entity (type *array, entity *ent);
513 entity *get_array_element_entity (type *array);
514
515 /* typecheck */
516 bool   is_array_type         (type *array);
517
518 /**
519  *  Representation of an enumeration type.
520  *  Enumeration types need not necessarily be represented explicitly
521  *  by Firm types, as the frontend can lower them to integer constants as
522  *  well.  For debugging purposes or similar tasks this information is useful.
523  *   @param *enum           The target values representing the constants used to
524  *                   represent individual enumerations.
525  *   @param *enum_nameid    Idents containing the source program name of the enumeration
526  *                   constants
527  *
528 */
529 /* create a new type enumeration -- set the enumerators independently */
530 type   *new_type_enumeration    (ident *name, int n_enums);
531 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db);
532
533 /* manipulate fields of enumeration type. */
534 int     get_enumeration_n_enums (type *enumeration);
535
536 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
537 tarval *get_enumeration_enum    (type *enumeration, int pos);
538
539 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
540 ident  *get_enumeration_nameid  (type *enumeration, int pos);
541 const char *get_enumeration_name(type *enumeration, int pos);
542
543 /* typecheck */
544 bool    is_enumeration_type     (type *enumeration);
545
546 /**
547  *   Representation of a pointer type.
548  *   Pointer types.
549  *   @param points_to       The type of the entity this pointer points to.
550  */
551 /* Create a new type pointer */
552 type *new_type_pointer           (ident *name, type *points_to);
553 type *new_d_type_pointer           (ident *name, type *points_to, dbg_info* db);
554
555 /* manipulate fields of type_pointer */
556 void  set_pointer_points_to_type (type *pointer, type *tp);
557 type *get_pointer_points_to_type (type *pointer);
558
559 /* typecheck */
560 bool  is_pointer_type            (type *pointer);
561
562 /**
563  *   Representation of a primitive type.
564  *   Primitive types are types that represent indivisible data values that
565  *   map directly to modes.  They don't have a private attribute.  The
566  *   important information they carry is held in the common mode field.
567 */
568 /* create a new type primitive */
569 type *new_type_primitive (ident *name, ir_mode *mode);
570 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
571
572 /* typecheck */
573 bool  is_primitive_type  (type *primitive);
574
575
576
577 /**
578  *
579  *   Checks whether a type is atomic.
580  *   @param tp - any type
581  *   @return true if type is primitive, pointer or enumeration
582  *
583  */
584 int is_atomic_type(type *tp);
585
586 /**
587  *
588  *   Checks whether a type is compound.
589  *   @param tp - any type
590  *   @return true if the type is class, structure, union or array type.
591  *
592  */
593 int is_compound_type(type *tp);
594
595 # endif /* _TYPE_H_ */