renamed reducible -> is_reducible
[libfirm] / ir / tr / type.h
1 /**
2  * @file type.h
3  *
4  * Project:     libFIRM                                                   <br>
5  * File name:   ir/tr/type.h                                              <br>
6  * Purpose:     Representation of types.                                  <br>
7  * Author:      Goetz Lindenmaier                                         <br>
8  * Modified by:                                                           <br>
9  * Created:                                                               <br>
10  * Copyright:   (c) 2001-2003 Universität Karlsruhe                       <br>
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE. <br>
12  * CVS-ID:      $Id$
13  *
14  *
15  *  Datastructure to hold type information.
16  *
17  *  This module supplies a datastructure to represent all types
18  *  known in the compiled program.  This includes types specified
19  *  in the program as well as types defined by the language.  In the
20  *  view of the intermediate representation there is no difference
21  *  between these types.  Finally it specifies some auxiliary types.
22  *
23  *  There exist several kinds of types, arranged by the structure of
24  *  the type.  A type is described by a set of attributes.  Some of
25  *  these attributes are common to all types, others depend on the
26  *  kind of the type.
27  *
28  *  Types are different from the modes defined in irmode:  Types are
29  *  on the level of the programming language, modes at the level of
30  *  the target processor.
31  *
32  *  @see  tpop.h
33  */
34
35 # ifndef _TYPE_H_
36 # define _TYPE_H_
37
38 # include "firm_types.h"
39 # include "tpop.h"
40 # include "firm_common.h"
41 # include "ident.h"
42 # include "irmode.h"
43 # include "dbginfo.h"
44 # include "tr_inheritance.h"
45
46 /**
47  *  An abstract data type to represent types.
48  *
49  *  This is the abstract data type with which any type known in the
50  *  compiled program can be represented.  This includes types specified
51  *  in the program as well as types defined by the language.  In the
52  *  view of the intermediate representation there is no difference
53  *  between these types.
54  *
55  *  There exist several kinds of types, arranged by the structure of
56  *  the type.  These are distinguished by a type opcode.
57  *  A type is described by a set of attributes.  Some of these attributes
58  *  are common to all types, others depend on the kind of the type.
59  *
60  *  The following describes the common attributes.  They can only be
61  *  accessed by the functions given below.
62  *
63  *  The common fields are:
64  *
65  *  - firm_kind: A firm_kind tag containing k_type.  This is useful
66  *               for dynamically checking whether a node is a type node.
67  *  - type_op:   A tp_op specifying the kind of the type.
68  *  - mode:      The mode to be used to represent the type on a machine.
69  *  - name:      An identifier specifying the name of the type.  To be
70  *               set by the frontend.
71  *  - size:      The size of the type, i.e. an entity of this type will
72  *               occupy size bits in memory.  In several cases this is
73  *               determined when fixing the layout of this type (class,
74  *               struct, union, array, enumeration).
75  *  - alignment  The alignment of the type, i.e. an entity of this type will
76  *               be allocated an an address in memory with this alignment.
77  *               In several cases this is determined when fixing the layout
78  *               of this type (class, struct, union, array)
79  *  - 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  *  - visit:     A counter for walks of the type information.
89  *  - link:      A void* to associate some additional information with the type.
90  *
91  *  These fields can only be accessed via access functions.
92  *
93  *  Depending on the value of @c type_op, i.e., depending on the kind of the
94  *  type the adt contains further attributes.  These are documented below.
95  *
96  *  @see
97  *
98  *  @link class_type class @endlink, @link struct_type struct @endlink,
99  *  @link method_type method @endlink, @link union_type union @endlink,
100  *  @link array_type array @endlink, @link enumeration_type enumeration @endlink,
101  *  @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
102  *
103  *  @todo
104  *      mode maybe not global field??
105  */
106 #ifndef _TYPE_TYPEDEF_
107 #define _TYPE_TYPEDEF_
108 typedef struct type type;
109 #endif
110
111 # include "type_or_entity.h"
112
113 /** frees all entities associated with a type.
114     Does not free array entity.
115     Warning: make sure these entities are not referenced anywhere else.
116 */
117 void        free_type_entities(type *tp);
118
119 /** Frees the memory used by the type.
120  *
121  * Removes the type from the type list. Does not free the entities
122  * belonging to the type, except for the array element entity.  Does
123  * not free if tp is "none" or "unknown".  Frees entities in value
124  * param subtypes of method types!!! Make sure these are not
125  * referenced any more.  Further make sure there is no pointer type
126  * that refers to this type.                           */
127 void        free_type(type *tp);
128
129 const tp_op*get_type_tpop(const type *tp);
130 ident*      get_type_tpop_nameid(const type *tp);
131 const char* get_type_tpop_name(const type *tp);
132 tp_opcode   get_type_tpop_code(const type *tp);
133
134 ident*      get_type_ident(const type *tp);
135 void        set_type_ident(type *tp, ident* id);
136 const char* get_type_name(const type *tp);
137
138 /** This enumeration flags the visibility of entities and types.
139  *
140  * This is necessary for partial compilation.
141  * We rely on the ordering of the flags.
142  */
143 typedef enum {
144   visibility_local,              /**< The entity is only visible locally.  This is the default for
145                                       entities.
146                                       The type is only visible locally.  All instances are allocated
147                                       locally, and no pointer to entities of this type are passed
148                                       out of this compilation unit. */
149   visibility_external_visible,   /**< The entity is visible to other external program parts, but
150                                       it is defined here.  It may not be optimized away.  The entity must
151                                       be static_allocated.
152                                       For types:  entities of this type can be accessed externally.  No
153                                       instances of this type are allocated externally.  */
154   visibility_external_allocated  /**< The entity is defined and allocated externally.  This compilation
155                                       must not allocate memory for this entity. The entity must
156                                       be static_allocated.  This can also be an external defined
157                                       method.
158                                       For types:  entities of this type are allocated and accessed from
159                                       external code.  Default for types.  */
160 } visibility;
161
162 /** The visibility of a type.
163  *
164  *  The visibility of a type indicates, whether entities of this type
165  *  are accessed or allocated in external code.
166  *
167  *  An entity of a type is allocated in external code, if the external
168  *  code declares a variable of this type, or dynamically allocates
169  *  an entity of this type.  If the external code declares a (compound)
170  *  type, that contains entities of this type, the visibility also
171  *  must be external_allocated.
172  *
173  *  The visibility must be higher than that of all entities, if the
174  *  type is a compound.  Here it is questionable, what happens with
175  *  static entities.  If these are accessed external by direct reference,
176  *  (a static call to a method, that is also in the dispatch table)
177  *  it should not affect the visibility of the type.
178  *
179  *
180  * @@@ Do we need a visibility for types?
181  * I change the layout of types radically when doing type splitting.
182  * I need to know, which fields of classes are accessed in the RTS,
183  * e.g., [_length.  I may not move [_length to the split part.
184  * The layout though, is a property of the type.
185  *
186  * One could also think of changing the mode of a type ...
187  *
188  * But, we could also output macros to access the fields, e.g.,
189  *  ACCESS_[_length (X)   X->length              // conventional
190  *  ACCESS_[_length (X)   X->_split_ref->length  // with type splitting
191  *
192  * For now I implement this function, that returns the visibility
193  * based on the visibility of the entities of a compound ...
194  *
195  * This function returns visibility_external_visible if one or more
196  * entities of a compound type have visibility_external_visible.
197  * Entities of types are never visibility_external_allocated (right?).
198  * Else returns visibility_local.
199  */
200 visibility get_type_visibility (const type *tp);
201 void       set_type_visibility (type *tp, visibility v);
202
203
204
205 /** The state of the type layout. */
206 typedef enum {
207   layout_undefined,    /**< The layout of this type is not defined.
208               Address computation to access fields is not
209               possible, fields must be accessed by Sel
210               nodes.  This is the default value except for
211               pointer, primitive and method types. */
212   layout_fixed         /**< The layout is fixed, all component/member entities
213               have an offset assigned.  Size of the type is known.
214               Arrays can be accessed by explicit address
215               computation. Default for pointer, primitive and method
216               types.  */
217 } type_state;
218
219 /** Returns a human readable string for the enum entry. */
220 const char *get_type_state_name(type_state s);
221
222 /** Returns the type layout state of a type. */
223 type_state  get_type_state(const type *tp);
224
225 /** Sets the type layout state of a type.
226  *
227  * For primitives, pointer and method types the layout is always fixed.
228  * This call is legal but has no effect.
229  */
230 void        set_type_state(type *tp, type_state state);
231
232 /** Returns the mode of a type.
233  *
234  * Returns NULL for all non atomic types.
235  */
236 ir_mode*    get_type_mode(const type *tp);
237
238 /** Sets the mode of a type.
239  *
240  * Only has an effect on primitive, enumeration and pointer types.
241  */
242 void        set_type_mode(type *tp, ir_mode* m);
243
244 /** Returns the size of a type in bytes, returns -1 if the size is NOT
245  *  a byte size, i.e. not dividable by 8. */
246 int         get_type_size_bytes(const type *tp);
247
248 /** Returns the size of a type in bits. */
249 int         get_type_size_bits(const type *tp);
250
251 /** Sets the size of a type in bytes.
252  *
253  * For primitive, enumeration, pointer and method types the size
254  * is always fixed. This call is legal but has no effect.
255  */
256 void        set_type_size_bytes(type *tp, int size);
257
258 /** Sets the size of a type in bits.
259  *
260  * For primitive, enumeration, pointer and method types the size
261  * is always fixed. This call is legal but has no effect.
262  */
263 void        set_type_size_bits(type *tp, int size);
264
265 /** Returns the alignment of a type in bytes.
266  *
267  *  Returns -1 if the alignment is NOT
268  *  a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
269 int         get_type_alignment_bytes(type *tp);
270
271 /** Returns the alignment of a type in bits.
272  *
273  *  If the alignment of a type is
274  *  not set, it is calculated here according to the following rules:
275  *  -#.) if a type has a mode, the alignment is the mode size.
276  *  -#.) compound types have the alignment of there biggest member.
277  *  -#.) array types have the alignment of there element type.
278  *  -#.) method types return 0 here.
279  *  -#.) all other types return 8 here (i.e. aligned at byte).
280  */
281 int         get_type_alignment_bits(type *tp);
282
283 /** Sets the alignment of a type in bytes. */
284 void        set_type_alignment_bytes(type *tp, int size);
285
286 /** Sets the alignment of a type in bits.
287  *
288  * For method types the alignment is always fixed.
289  * This call is legal but has no effect.
290  */
291 void        set_type_alignment_bits(type *tp, int size);
292
293 unsigned long get_type_visited(const type *tp);
294 void          set_type_visited(type *tp, unsigned long num);
295 /* Sets visited field in type to type_visited. */
296 void          mark_type_visited(type *tp);
297 int           type_visited(const type *tp);
298 int           type_not_visited(const type *tp);
299
300 /** Returns the associated link field of a type. */
301 void*         get_type_link(const type *tp);
302 /** Sets the associated link field of a type. */
303 void          set_type_link(type *tp, void *l);
304
305 /**
306  * Visited flag to traverse the type information.
307  *
308  * Increase this flag by one before traversing the type information
309  * using inc_master_type_visited().
310  * Mark type nodes as visited by mark_type_visited(type).
311  * Check whether node was already visited by type_visited(type)
312  * and type_not_visited(type).
313  * Or use the function to walk all types.
314  *
315  * @see  typewalk
316  */
317 void          set_master_type_visited(unsigned long val);
318 unsigned long get_master_type_visited(void);
319 void          inc_master_type_visited(void);
320
321 /**
322  * Checks whether a pointer points to a type.
323  *
324  * @param thing     an arbitrary pointer
325  *
326  * @return
327  *     true if the thing is a type, else false
328  */
329 int is_type            (const void *thing);
330
331 /**
332  *   Checks whether two types are structurally equal.
333  *
334  *   @param typ1  the first type
335  *   @param typ2  the second type
336  *
337  *   @return
338  *    true if the types are equal, else false.
339  *
340  *   Types are equal if :
341  *    - they are the same type kind
342  *    - they have the same name
343  *    - they have the same mode (if applicable)
344  *    - they have the same type_state and, ev., the same size
345  *    - they are class types and have:
346  *      - the same members (see same_entity in entity.h)
347  *      - the same supertypes -- the C-pointers are compared --> no recursive call.
348  *      - the same number of subtypes.  Subtypes are not compared,
349  *        as this could cause a cyclic test.
350  *      - the same peculiarity
351  *    - they are structure types and have the same members
352  *    - they are method types and have
353  *      - the same parameter types
354  *      - the same result types
355  *    - they are union types and have the same members
356  *    - they are array types and have
357  *      - the same number of dimensions
358  *      - the same dimension bounds
359  *      - the same dimension order
360  *      - the same element type
361  *    - they are enumeration types and have the same enumerator names
362  *    - they are pointer types and have the identical points_to type
363  *      (i.e., the same C-struct to represent the type, type_id is skipped.
364  *       This is to avoid endless recursions; with pointer types cyclic
365  *       type graphs are possible.)
366  */
367 int equal_type(type *typ1, type *typ2);
368
369 /**
370  *   Checks whether two types are structural comparable.
371  *
372  *   @param st pointer type
373  *   @param lt pointer type
374  *
375  *   @return
376  *    true if type st is smaller than type lt, i.e. whenever
377  *    lt is expected a st can be used.
378  *    This is true if
379  *    - they are the same type kind
380  *    - mode(st) < mode (lt)  (if applicable)
381  *    - they are class types and st is (transitive) subtype of lt,
382  *    - they are structure types and
383  *       - the members of st have exactly one counterpart in lt with the same name,
384  *       - the counterpart has a bigger type.
385  *    - they are method types and have
386  *      - the same number of parameter and result types,
387  *      - the parameter types of st are smaller than those of lt,
388  *      - the result types of st are smaller than those of lt
389  *    - they are union types and have the members of st have exactly one
390  *      @return counterpart in lt and the type is smaller
391  *    - they are array types and have
392  *      - the same number of dimensions
393  *      - all bounds of lt are bound of st
394  *      - the same dimension order
395  *      - the same element type
396  *      @return or
397  *      - the element type of st is smaller than that of lt
398  *      - the element types have the same size and fixed layout.
399  *    - they are enumeration types and have the same enumerator names
400  *    - they are pointer types and have the points_to type of st is
401  *      @return smaller than the points_to type of lt.
402  *
403  */
404 int smaller_type (type *st, type *lt);
405
406 /**
407  *  @page class_type    Representation of a class type
408  *
409  *  If the type opcode is set to type_class the type represents class
410  *  types.  A list of fields and methods is associated with a class.
411  *  Further a class can inherit from and bequest to other classes.
412  *  @@@ value class???
413  *  The following attributes are private to this type kind:
414  *  - member:     All entities belonging to this class.  This are method entities
415  *                which have type_method or fields that can have any of the
416  *                following type kinds: type_class, type_struct, type_union,
417  *                type_array, type_enumeration, type_pointer, type_primitive.
418  *
419  *  The following two are dynamic lists that can be grown with an "add_" function,
420  *  but not shrinked:
421  *
422  *  - subtypes:   A list of direct subclasses.
423  *
424  *  - supertypes: A list of direct superclasses.
425  *
426  *  - peculiarity: The peculiarity of this class.  If the class is of peculiarity
427  *                 "description" it only is a description of requirements to a class,
428  *                 as, e.g., a Java interface.  The class will never be allocated.
429  *                 Peculiarity inherited is only possible for entities.  An entity
430  *                 is of peculiarity inherited if the compiler generated the entity
431  *                 to explicitly resolve inheritance.  An inherited method entity has
432  *                 no value for irg.
433  *                 Values: description, existent, inherited.  Default: existent.
434  *
435  */
436
437 /** Creates a new class type. */
438 type   *new_type_class (ident *name);
439
440 /** Creates a new class type with debug information. */
441 type   *new_d_type_class (ident *name, dbg_info *db);
442
443 /* --- manipulate private fields of class type  --- */
444
445 /** Adds the entity as member of the class.  */
446 void    add_class_member   (type *clss, entity *member);
447
448 /** Returns the number of members of this class. */
449 int     get_class_n_members (const type *clss);
450
451 /** Returns the member at position pos, 0 <= pos < n_member */
452 entity *get_class_member   (const type *clss, int pos);
453
454 /** Returns index of mem in clss, -1 if not contained. */
455 int     get_class_member_index(type *clss, entity *mem);
456
457 /** Finds the member with name 'name'. If several members with the same
458  *  name returns one of them.  Returns NULL if no member found. */
459 entity *get_class_member_by_name(type *clss, ident *name);
460
461 /** Overwrites the member at position pos, 0 <= pos < n_member with
462  *  the passed entity. */
463 void    set_class_member   (type *clss, entity *member, int pos);
464
465 /** Replaces complete member list in class type by the list passed.
466  *
467  *  Copies the list passed. This function is necessary to reduce the number of members.
468  *  members is an array of entities, num the size of this array.  Sets all
469  *  owners of the members passed to clss. */
470 void    set_class_members  (type *clss, entity *members[], int arity);
471
472 /** Finds member in the list of members and removes it.
473  *
474  *  Shrinks the member list, so iterate from the end!!!
475  *  Does not deallocate the entity.  */
476 void    remove_class_member(type *clss, entity *member);
477
478
479 /** Adds subtype as subtype to clss.
480  *
481  *  Checks whether clss is a supertype of subtype.  If not
482  *  adds also clss as supertype to subtype.  */
483 void    add_class_subtype   (type *clss, type *subtype);
484
485 /** Returns the number of subtypes */
486 int     get_class_n_subtypes (const type *clss);
487
488 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
489 type   *get_class_subtype   (type *clss, int pos);
490
491 /** Returns the index to access subclass as subtype of class.
492  *
493  *  If subclass is no direct subtype of class returns -1.
494  */
495 int get_class_subtype_index(type *clss, const type *subclass);
496
497 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
498  *
499  *  Does not set the corresponding supertype relation for subtype: this might
500  *  be a different position! */
501 void    set_class_subtype   (type *clss, type *subtype, int pos);
502
503 /** Finds subtype in the list of subtypes and removes it  */
504 void    remove_class_subtype(type *clss, type *subtype);
505
506 /* Convenience macros */
507 #define add_class_derived_type(clss, drvtype)       add_class_subtype(clss, drvtype)
508 #define get_class_n_derived_types(clss)             get_class_n_subtypes(clss)
509 #define get_class_derived_type(clss, pos)           get_class_subtype(clss, pos)
510 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
511 #define set_class_derived_type(clss, drvtype, pos)  set_class_subtype(clss, drvtype, pos)
512 #define remove_class_derived_type(clss, drvtype)    remove_class_subtype(clss, drvtype)
513
514 /** Adds supertype as supertype to class.
515  *
516  *  Checks whether clss is a subtype of supertype.  If not
517  *  adds also clss as subtype to supertype.  */
518 void    add_class_supertype   (type *clss, type *supertype);
519
520 /** Returns the number of supertypes */
521 int     get_class_n_supertypes (const type *clss);
522
523 /** Returns the index to access superclass as supertype of class.
524  *
525  *  If superclass is no direct supertype of class returns -1.
526  */
527 int     get_class_supertype_index(type *clss, type *super_clss);
528
529 /** Gets the supertype at position pos,  0 <= pos < n_supertype. */
530 type   *get_class_supertype   (type *clss, int pos);
531
532 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
533  *
534  *  Does not set the corresponding subtype relation for supertype: this might
535  *  be at a different position! */
536 void    set_class_supertype   (type *clss, type *supertype, int pos);
537
538 /** Finds supertype in the list of supertypes and removes it */
539 void    remove_class_supertype(type *clss, type *supertype);
540
541 /** Convenience macro */
542 #define add_class_base_type(clss, basetype)  add_class_supertype(clss, basetype)
543 #define get_class_n_base_types(clss)  get_class_n_supertypes(clss)
544 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
545 #define get_class_base_type(clss, pos)  get_class_supertype(clss, pos)
546 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
547 #define remove_class_base_type(clss, basetype)  remove_class_supertype(clss, basetype)
548
549 /** Convenience macro */
550 #define add_class_base_type(clss, basetype)        add_class_supertype(clss, basetype)
551 #define get_class_n_base_types(clss)               get_class_n_supertypes(clss)
552 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
553 #define get_class_base_type(clss, pos)             get_class_supertype(clss, pos)
554 #define set_class_base_type(clss, basetype, pos)   set_class_supertype(clss, basetype, pos)
555 #define remove_class_base_type(clss, basetype)     remove_class_supertype(clss, basetype)
556
557 /** This enumeration flags the peculiarity of entities and types. */
558 typedef enum peculiarity {
559   peculiarity_description,     /**< Represents only a description.  The entity/type is never
560                             allocated, no code/data exists for this entity/type.
561                         @@@ eventually rename to descriptive (adjective as the others!)*/
562   peculiarity_inherited,       /**< Describes explicitly that other entities are
563                             inherited to the owner of this entity.
564                             Overwrites must refer to at least one other
565                             entity.  If this is a method entity there exists
566                             no irg for this entity, only for one of the
567                             overwritten ones.
568                         Only for entity. */
569   peculiarity_existent         /**< The entity/type (can) exist.
570                     @@@ eventually rename to 'real' i.e., 'echt'
571                         This serves better as opposition to description _and_ inherited.*/
572 } peculiarity;
573 const char *get_peculiarity_string(peculiarity p);
574
575 /** Returns the peculiarity of the class. */
576 peculiarity get_class_peculiarity (const type *clss);
577 /** Sets the peculiarity of the class. */
578 void        set_class_peculiarity (type *clss, peculiarity pec);
579
580 /* Set and get a class' dfn --
581    @todo This is an undocumented field, subject to change! */
582 void set_class_dfn (type *clss, int dfn);
583 int  get_class_dfn (const type *clss);
584
585 /** Returns true if a type is a class type. */
586 int is_Class_type(const type *clss);
587
588 /**
589  *  @page struct_type   Representation of a struct type
590  *
591  *  Type_strct represents aggregate types that consist of a list
592  *  of fields.
593  *  The following attributes are private to this type kind:
594  *  - member:  All entities belonging to this class.  This are the fields
595  *             that can have any of the following types:  type_class,
596  *             type_struct, type_union, type_array, type_enumeration,
597  *             type_pointer, type_primitive.
598  *             This is a dynamic list that can be grown with an "add_" function,
599  *             but not shrinked.
600  *             This is a dynamic list that can be grown with an "add_" function,
601  *             but not shrinked.
602  */
603 /** Creates a new type struct */
604 type   *new_type_struct (ident *name);
605 /** Creates a new type struct with debug information. */
606 type   *new_d_type_struct (ident *name, dbg_info* db);
607
608 /* --- manipulate private fields of struct --- */
609
610 /** Adds the entity as member of the struct.  */
611 void    add_struct_member   (type *strct, entity *member);
612
613 /** Returns the number of members of this struct. */
614 int     get_struct_n_members (const type *strct);
615
616 /** Returns the member at position pos, 0 <= pos < n_member */
617 entity *get_struct_member   (const type *strct, int pos);
618
619 /** Returns index of member in strct, -1 if not contained. */
620 int     get_struct_member_index(type *strct, entity *member);
621
622 /** Overwrites the member at position pos, 0 <= pos < n_member with
623    the passed entity. */
624 void    set_struct_member   (type *strct, int pos, entity *member);
625
626 /** Finds member in the list of members and removes it. */
627 void    remove_struct_member (type *strct, entity *member);
628
629 /** Returns true if a type is a struct type. */
630 int     is_Struct_type(const type *strct);
631
632 /**
633  * @page method_type    Representation of a method type
634  *
635  * A method type represents a method, function or procedure type.
636  * It contains a list of the parameter and result types, as these
637  * are part of the type description.  These lists should not
638  * be changed by a optimization, as a change creates a new method
639  * type.  Therefore optimizations should allocated new method types.
640  * The set_ routines are only for construction by a frontend.
641  *
642  * - n_params:   Number of parameters to the procedure.
643  *               A procedure in FIRM has only call by value parameters.
644  *
645  * - param_type: A list with the types of parameters.  This list is ordered.
646  *               The nth type in this list corresponds to the nth element
647  *               in the parameter tuple that is a result of the start node.
648  *               (See ircons.h for more information.)
649  *
650  * - value_param_ents
651  *               A list of entities (whose owner is a struct private to the
652  *               method type) that represent parameters passed by value.
653  *
654  * - n_res:      The number of results of the method.  In general, procedures
655  *               have zero results, functions one.
656  *
657  * - res_type:   A list with the types of parameters.  This list is ordered.
658  *               The nth type in this list corresponds to the nth input to
659  *               Return nodes.  (See ircons.h for more information.)
660  *
661  * - value_res_ents
662  *               A list of entities (whose owner is a struct private to the
663  *               method type) that represent results passed by value.
664  */
665
666 /* These macros define the suffixes for the types and entities used
667    to represent value parameters / results. */
668 #define VALUE_PARAMS_SUFFIX  "val_param"
669 #define VALUE_RESS_SUFFIX    "val_res"
670
671 /** Create a new method type.
672  *
673  * @param name      the name (ident) of this type
674  * @param n_param   the number of parameters
675  * @param n_res     the number of results
676  *
677  * The arrays for the parameter and result types are not initialized by
678  * the constructor.
679  */
680 type *new_type_method (ident *name, int n_param, int n_res);
681
682 /** Create a new method type with debug information.
683  *
684  * @param name      the name (ident) of this type
685  * @param n_param   the number of parameters
686  * @param n_res     the number of results
687  * @param db        user defined debug information
688  *
689  * The arrays for the parameter and result types are not initialized by
690  * the constructor.
691  */
692 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
693
694 /* -- manipulate private fields of method. -- */
695
696 /** Returns the number of parameters of this method. */
697 int   get_method_n_params  (const type *method);
698
699 /** Returns the type of the parameter at position pos of a method. */
700 type *get_method_param_type(type *method, int pos);
701 /** Sets the type of the parameter at position pos of a method.
702     Also changes the type in the pass-by-value representation by just
703     changing the type of the corresponding entity if the representation is constructed. */
704 void  set_method_param_type(type *method, int pos, type* tp);
705 /** Returns an entity that represents the copied value argument.  Only necessary
706    for compounds passed by value. This information is constructed only on demand. */
707 entity *get_method_value_param_ent(type *method, int pos);
708 /**
709  * Returns a type that represents the copied value arguments.
710  */
711 type *get_method_value_param_type(const type *method);
712
713 /** Returns the number of results of a method type. */
714 int   get_method_n_ress   (const type *method);
715 /** Returns the return type of a method type at position pos. */
716 type *get_method_res_type(type *method, int pos);
717 /** Sets the type of the result at position pos of a method.
718     Also changes the type in the pass-by-value representation by just
719     changing the type of the corresponding entity if the representation is constructed. */
720 void  set_method_res_type(type *method, int pos, type* tp);
721 /** Returns an entity that represents the copied value result.  Only necessary
722    for compounds passed by value. This information is constructed only on demand. */
723 entity *get_method_value_res_ent(type *method, int pos);
724
725 /**
726  * Returns a type that represents the copied value results.
727  */
728 type *get_method_value_res_type(const type *method);
729
730 /**
731  * This enum flags the variadicity of methods (methods with a
732  * variable amount of arguments (e.g. C's printf). Default is
733  * non_variadic.
734  */
735 typedef enum variadicity {
736   variadicity_non_variadic, /**< non variadic */
737   variadicity_variadic      /**< variadic */
738 } variadicity;
739
740 /** Returns the null-terminated name of this variadicity. */
741 const char *get_variadicity_name(variadicity vari);
742
743 /** Returns the variadicity of a method. */
744 variadicity get_method_variadicity(const type *method);
745
746 /** Sets the variadicity of a method. */
747 void set_method_variadicity(type *method, variadicity vari);
748
749 /**
750  * Returns the first variadic parameter index of a type.
751  * If this index was NOT set, the index of the last parameter
752  * of the method type plus one is returned for variadic functions.
753  * Non-variadic function types always return -1 here.
754  */
755 int get_method_first_variadic_param_index(const type *method);
756
757 /**
758  * Sets the first variadic parameter index. This allows to specify
759  * a complete call type (containing the type of all parameters)
760  * but still have the knowledge, which parameter must be passed as
761  * variadic one.
762  */
763 void set_method_first_variadic_param_index(type *method, int index);
764
765 /**
766  * additional method type properties:
767  *  Tell about special properties of a method type. Some
768  *  of these may be discovered by analyses.
769  */
770 typedef enum {
771   mtp_no_property        = 0x00000000, /**< no additional properties, default */
772   mtp_property_const     = 0x00000001, /**< This method did not access memory and calculates
773                                          its return values solely from its parameters.
774                                          GCC: __attribute__((const)). */
775   mtp_property_pure      = 0x00000002, /**< This method did NOT write to memory and calculates
776                                          its return values solely form its parameters and
777                                          the memory they points to (or global vars).
778                                          GCC: __attribute__((pure)). */
779   mtp_property_noreturn  = 0x00000004, /**< This method did not return due to an aborting system
780                                          call.
781                                          GCC: __attribute__((noreturn)). */
782   mtp_property_nothrow   = 0x00000008, /**< This method cannot throw an exception.
783                                          GCC: __attribute__((nothrow)). */
784   mtp_property_naked     = 0x00000010, /**< This method is naked.
785                                          GCC: __attribute__((naked)). */
786   mtp_property_malloc    = 0x00000020, /**< This method returns newly allocate memory.
787                                          GCC: __attribute__((malloc)). */
788   mtp_property_inherited = 0x40000000  /**< used only in irg's, means property is inherited
789                                          from type. */
790 } mtp_additional_property;
791
792 /** Returns the mask of the additional graph properties. */
793 unsigned get_method_additional_properties(const type *method);
794
795 /** Sets the mask of the additional graph properties. */
796 void set_method_additional_properties(type *method, unsigned property_mask);
797
798 /** Sets one additional graph property. */
799 void set_method_additional_property(type *method, mtp_additional_property flag);
800
801 /**
802  * calling conventions
803  */
804 typedef enum {
805   cc_reg_param        = 0x00000001, /**< Transmit parameters in registers, else the stack is used.
806                                              This flag may be set as default on some architectures. */
807   cc_last_on_top      = 0x00000002, /**< The last non-register parameter is transmitted on top of
808                                              the stack. This is equivalent to the stdcall or pascal
809                                              calling convention. If this flag is not set, the first
810                                              non-register parameter is used (cdecl calling convention) */
811   cc_callee_clear_stk = 0x00000004, /**< The callee clears the stack. This forbids variadic
812                                              function calls (stdcall). */
813   cc_this_call        = 0x00000008  /**< The first parameter is a this pointer and is transmitted
814                                         in a special way. */
815 } calling_convention;
816
817 /** return the default calling convention for method types */
818 unsigned get_default_cc_mask(void);
819
820 /**
821  * check for the CDECL calling convention
822  */
823 #define IS_CDECL(cc_mask)     (((cc_mask) & (cc_callee_clear_stk|cc_last_on_top)) == 0)
824
825 /**
826  * check for the STDCALL calling convention
827  */
828 #define IS_STDCALL(cc_mask)   (((cc_mask) & (cc_callee_clear_stk|cc_last_on_top)) == cc_callee_clear_stk)
829
830 /**
831  * add the CDECL convention bits
832  */
833 #define SET_CDECL(cc_mask)    ((cc_mask) & ~(cc_callee_clear_stk|cc_last_on_top))
834
835 /**
836  * add the STDCALL convention bits
837  */
838 #define SET_STDCALL(cc_mask)  (((cc_mask) & ~cc_last_on_top) | cc_callee_clear_stk)
839
840 /** Returns the calling convention of an entities graph. */
841 unsigned get_method_calling_convention(const type *method);
842
843 /** Sets the calling convention of an entities graph. */
844 void set_method_calling_convention(type *method, unsigned cc_mask);
845
846 /** Returns true if a type is a method type. */
847 int   is_Method_type     (const type *method);
848
849 /**
850  *   @page union_type   Representation of a union (variant) type.
851  *
852  *   The union type represents union types.
853  *   - n_types:     Number of unioned types.
854  *   - members:     Entities for unioned types.  Fixed length array.
855  *                  This is a dynamic list that can be grown with an "add_" function,
856  *                  but not shrinked.
857  */
858 /** Creates a new type union. */
859 type   *new_type_union (ident *name);
860
861 /** Creates a new type union with debug information. */
862 type   *new_d_type_union (ident *name, dbg_info* db);
863
864 /* --- manipulate private fields of struct --- */
865
866 /** Returns the number of unioned types of this union */
867 int     get_union_n_members      (const type *uni);
868
869 /** Adds a new entity to a union type */
870 void    add_union_member (type *uni, entity *member);
871
872 /** Returns the entity at position pos of a union */
873 entity *get_union_member (const type *uni, int pos);
874
875 /** Overwrites a entity at position pos in a union type. */
876 void    set_union_member (type *uni, int pos, entity *member);
877
878 /** Finds member in the list of members and removes it. */
879 void    remove_union_member (type *uni, entity *member);
880
881 /** Returns true if a type is a union type. */
882 int     is_Union_type          (const type *uni);
883
884 /**
885  * @page array_type Representation of an array type
886  *
887  * The array type represents rectangular multi dimensional arrays.
888  * The constants representing the bounds must be allocated to
889  * get_const_code_irg() by setting current_ir_graph accordingly.
890  *
891  * - n_dimensions:    Number of array dimensions.
892  * - *lower_bound:    Lower bounds of dimensions.  Usually all 0.
893  * - *upper_bound:    Upper bounds or dimensions.
894  * - *element_type:   The type of the array elements.
895  * - *element_ent:    An entity for the array elements to be used for
896  *                      element selection with Sel.
897  * @todo
898  *   Do we need several entities?  One might want
899  *   to select a dimension and not a single element in case of multi
900  *   dimensional arrays.
901  */
902
903 /** Create a new type array.
904  *
905  * Sets n_dimension to dimension and all dimension entries to NULL.
906  * Initializes order to the order of the dimensions.
907  * The entity for array elements is built automatically.
908  * Set dimension sizes after call to constructor with set_* routines.
909  */
910 type *new_type_array         (ident *name, int n_dimensions,
911                   type *element_type);
912
913 /** Create a new type array with debug information.
914  *
915  * Sets n_dimension to dimension and all dimension entries to NULL.
916  * Initializes order to the order of the dimensions.
917  * The entity for array elements is built automatically.
918  * Set dimension sizes after call to constructor with set_* routines.
919  * A legal array type must have at least one dimension set.
920  */
921 type *new_d_type_array         (ident *name, int n_dimensions,
922                   type *element_type, dbg_info* db);
923
924 /* --- manipulate private fields of array type --- */
925
926 /** Returns the number of array dimensions of this type. */
927 int   get_array_n_dimensions (const type *array);
928
929 /**
930  * Allocates Const nodes of mode_I for one array dimension.
931  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
932  */
933 void  set_array_bounds_int   (type *array, int dimension, int lower_bound,
934                                                           int upper_bound);
935 /**
936  * Sets the bounds for one array dimension.
937  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
938  */
939 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
940                                                           ir_node *upper_bound);
941 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
942 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
943
944 /** Allocates Const nodes of mode_I for the lower bound of an array
945     dimension, i.e. [lower,upper[ */
946 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound);
947
948 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
949 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
950
951 /** Allocates Const nodes of mode_I for the upper bound of an array
952     dimension, i.e. [lower,upper[. */
953 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound);
954
955 /** Returns true if lower bound != Unknown. */
956 int       has_array_lower_bound     (const type *array, int dimension);
957 /** Returns the lower bound of an array. */
958 ir_node * get_array_lower_bound     (const type *array, int dimension);
959 /** Works only if bound is Const node with tarval that can be converted to long. */
960 long      get_array_lower_bound_int (const type *array, int dimension);
961 /** returns true if lower bound != Unknown */
962 int       has_array_upper_bound     (const type *array, int dimension);
963 /** Returns the upper bound of an array. */
964 ir_node * get_array_upper_bound     (const type *array, int dimension);
965 /** Works only if bound is Const node with tarval that can be converted to long. */
966 long      get_array_upper_bound_int (const type *array, int dimension);
967
968 /** Sets an array dimension to a specific order. */
969 void set_array_order (type *array, int dimension, int order);
970
971 /** Returns the order of an array dimension. */
972 int  get_array_order (const type *array, int dimension);
973
974 /** Find the array dimension that is placed at order ord. */
975 int find_array_dimension(const type *array, int order);
976
977 /** Sets the array element type. */
978 void  set_array_element_type (type *array, type *tp);
979
980 /** Gets the array element type. */
981 type *get_array_element_type (type *array);
982
983 /** Sets the array element entity. */
984 void  set_array_element_entity (type *array, entity *ent);
985
986 /** Get the array element entity. */
987 entity *get_array_element_entity (const type *array);
988
989 /** Returns true if a type is an array type. */
990 int    is_Array_type(const type *array);
991
992 /**
993  * @page enumeration_type   Representation of an enumeration type
994  *
995  * Enumeration types need not necessarily be represented explicitly
996  * by Firm types, as the frontend can lower them to integer constants as
997  * well.  For debugging purposes or similar tasks this information is useful.
998  *
999  * - *enum:         The target values representing the constants used to
1000  *                  represent individual enumerations.
1001  * - *enum_nameid:  Idents containing the source program name of the enumeration
1002  *                  constants
1003  */
1004 /** Create a new type enumeration -- set the enumerators independently. */
1005 type   *new_type_enumeration    (ident *name, int n_enums);
1006
1007 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1008 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db);
1009
1010 /* --- manipulate fields of enumeration type. --- */
1011
1012 /** Returns the number of enumeration values of this enumeration */
1013 int     get_enumeration_n_enums (const type *enumeration);
1014
1015 /** Sets the enumeration value at a given position. */
1016 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
1017
1018 /** Returns the enumeration value at a given position. */
1019 tarval *get_enumeration_enum    (const type *enumeration, int pos);
1020
1021 /** Assign an ident to an enumeration value at a given position. */
1022 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
1023
1024 /** Returns the assigned ident of an enumeration value at a given position. */
1025 ident  *get_enumeration_nameid  (const type *enumeration, int pos);
1026
1027 /** Returns the assigned name of an enumeration value at a given position. */
1028 const char *get_enumeration_name(const type *enumeration, int pos);
1029
1030 /** Returns true if a type is a enumeration type. */
1031 int     is_Enumeration_type     (const type *enumeration);
1032
1033 /**
1034  * @page pointer_type   Representation of a pointer type
1035  *
1036  * The mode of the pointer type must be a reference mode.
1037  *
1038  * Pointer types:
1039  * - points_to:      The type of the entity this pointer points to.
1040  */
1041
1042 /** Creates a new type pointer. */
1043 type *new_type_pointer           (ident *name, type *points_to, ir_mode *ptr_mode);
1044
1045 /** Creates a new type pointer with debug information. */
1046 type *new_d_type_pointer         (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db);
1047
1048 /* --- manipulate fields of type_pointer --- */
1049
1050 /** Sets the type to which a pointer points to. */
1051 void  set_pointer_points_to_type (type *pointer, type *tp);
1052
1053 /** Returns the type to which a pointer points to. */
1054 type *get_pointer_points_to_type (type *pointer);
1055
1056 /** Returns true if a type is a pointer type. */
1057 int   is_Pointer_type            (const type *pointer);
1058
1059 /** Returns the first pointer type that has as points_to tp.
1060  *  Not efficient: O(#types).
1061  *  If not found returns unknown_type. */
1062 type *find_pointer_type_to_type (type *tp);
1063
1064 /**
1065  * @page primitive_type Representation of a primitive type
1066  *
1067  * Primitive types are types that represent indivisible data values that
1068  * map directly to modes.  They don't have a private attribute.  The
1069  * important information they carry is held in the common mode field.
1070 */
1071 /** Creates a new primitive type. */
1072 type *new_type_primitive (ident *name, ir_mode *mode);
1073
1074 /** Creates a new primitive type with debug information. */
1075 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1076
1077 /** Returns true if a type is a primitive type. */
1078 int  is_Primitive_type  (const type *primitive);
1079
1080
1081 /**
1082  * @page none_type
1083  *
1084  *  This type is an auxiliary type dedicated to support type analyses.
1085  *
1086  *  The none type represents that there is no type.  The type can be used to
1087  *  initialize fields of type* that actually can not contain a type or that
1088  *  are initialized for an analysis. There exists exactly one type none.
1089  *  This type is not on the type list in ir_prog. It is
1090  *  allocated when initializing the type module.
1091  *
1092  *  The following values are set:
1093  *    - mode:  mode_BAD
1094  *    - name:  "type_none"
1095  *    - state: layout_fixed
1096  *    - size:  0
1097  */
1098 /** A variable that contains the only none type. */
1099 extern type *firm_none_type;
1100 /** Returns the none type */
1101 type *get_none_type(void);
1102
1103 /**
1104  * @page unknown_type
1105  *
1106  *  This type is an auxiliary type dedicated to support type analyses.
1107  *
1108  *  The unknown type represents that there could be a type, but it is not
1109  *  known.  This type can be used to initialize fields before an analysis (not known
1110  *  yet) or to represent the top of a lattice (could not be determined).  There exists
1111  *  exactly one type unknown. This type is not on the type list in ir_prog.  It is
1112  *  allocated when initializing the type module.
1113  *
1114  *  The following values are set:
1115  *    - mode:  mode_ANY
1116  *    - name:  "type_unknown"
1117  *    - state: layout_fixed
1118  *    - size:  0
1119  */
1120 /** A variable that contains the only unknown type. */
1121 extern type *firm_unknown_type;
1122 /** Returns the unknown type */
1123 type *get_unknown_type(void);
1124
1125
1126 /**
1127  *  Checks whether a type is atomic.
1128  *  @param tp   any type
1129  *  @return true if type is primitive, pointer or enumeration
1130  */
1131 int is_atomic_type(const type *tp);
1132
1133 /* --- Support for compound types --- */
1134
1135 /**
1136  * Gets the number of elements in a firm compound type.
1137  *
1138  * This is just a comfortability function, because structs and
1139  * classes can often be treated be the same code, but they have
1140  * different access functions to their members.
1141  *
1142  * @param tp  The type (must be struct, union or class).
1143  *
1144  * @return Number of members in the compound type.
1145  */
1146 int get_compound_n_members(const type *tp);
1147
1148 /**
1149  * Gets the member of a firm compound type at position pos.
1150  *
1151  * @param tp  The type (must be struct, union or class).
1152  * @param pos The number of the member.
1153  *
1154  * @return The member entity at position pos.
1155  *
1156  * @see get_compound_n_members() for justification of existence.
1157  */
1158 entity *get_compound_member(const type *tp, int pos);
1159
1160 /**
1161  * Checks whether a type is compound.
1162  *
1163  * @param tp - any type
1164  *
1165  * @return true if the type is class, structure, union or array type.
1166  */
1167 int is_compound_type(const type *tp);
1168
1169 /**
1170  * Checks, whether a type is a frame type
1171  */
1172 int is_frame_type(const type *tp);
1173
1174 /**
1175  * Makes a new frame type. Frame types are class types,
1176  * so all class access functions work.
1177  * Frame types are not in the global list of types.
1178  */
1179 type   *new_type_frame(ident *name);
1180
1181 /*-----------------------------------------------------------------*/
1182 /** Debug aides                                                   **/
1183 /*-----------------------------------------------------------------*/
1184
1185 /**
1186  *  Outputs a unique number for this type if libfirm is compiled for
1187  *  debugging, (configure with --enable-debug) else returns the address
1188  *  of the type cast to long.
1189  */
1190 long get_type_nr(const type *tp);
1191
1192
1193 # endif /* _TYPE_H_ */