renamed all type 'type' to new ir_type
[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 _IR_TYPE_TYPEDEF_
107 #define _IR_TYPE_TYPEDEF_
108 typedef struct ir_type ir_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(ir_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(ir_type *tp);
128
129 const tp_op*get_type_tpop(const ir_type *tp);
130 ident*      get_type_tpop_nameid(const ir_type *tp);
131 const char* get_type_tpop_name(const ir_type *tp);
132 tp_opcode   get_type_tpop_code(const ir_type *tp);
133
134 ident*      get_type_ident(const ir_type *tp);
135 void        set_type_ident(ir_type *tp, ident* id);
136 const char* get_type_name(const ir_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 ir_type *tp);
201 void       set_type_visibility (ir_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 ir_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(ir_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 ir_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(ir_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 ir_type *tp);
247
248 /** Returns the size of a type in bits. */
249 int         get_type_size_bits(const ir_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(ir_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(ir_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(ir_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(ir_type *tp);
282
283 /** Sets the alignment of a type in bytes. */
284 void        set_type_alignment_bytes(ir_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(ir_type *tp, int size);
292
293 unsigned long get_type_visited(const ir_type *tp);
294 void          set_type_visited(ir_type *tp, unsigned long num);
295 /* Sets visited field in type to type_visited. */
296 void          mark_type_visited(ir_type *tp);
297 int           type_visited(const ir_type *tp);
298 int           type_not_visited(const ir_type *tp);
299
300 /** Returns the associated link field of a type. */
301 void*         get_type_link(const ir_type *tp);
302 /** Sets the associated link field of a type. */
303 void          set_type_link(ir_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(ir_type).
311  * Check whether node was already visited by type_visited(ir_type)
312  * and type_not_visited(ir_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(ir_type *typ1, ir_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 (ir_type *st, ir_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 ir_type *new_type_class (ident *name);
439
440 /** Creates a new class type with debug information. */
441 ir_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   (ir_type *clss, entity *member);
447
448 /** Returns the number of members of this class. */
449 int     get_class_n_members (const ir_type *clss);
450
451 /** Returns the member at position pos, 0 <= pos < n_member */
452 entity *get_class_member   (const ir_type *clss, int pos);
453
454 /** Returns index of mem in clss, -1 if not contained. */
455 int     get_class_member_index(ir_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(ir_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   (ir_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  (ir_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(ir_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   (ir_type *clss, ir_type *subtype);
484
485 /** Returns the number of subtypes */
486 int     get_class_n_subtypes (const ir_type *clss);
487
488 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
489 ir_type *get_class_subtype   (ir_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(ir_type *clss, const ir_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   (ir_type *clss, ir_type *subtype, int pos);
502
503 /** Finds subtype in the list of subtypes and removes it  */
504 void    remove_class_subtype(ir_type *clss, ir_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   (ir_type *clss, ir_type *supertype);
519
520 /** Returns the number of supertypes */
521 int     get_class_n_supertypes (const ir_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(ir_type *clss, ir_type *super_clss);
528
529 /** Gets the supertype at position pos,  0 <= pos < n_supertype. */
530 ir_type *get_class_supertype   (ir_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   (ir_type *clss, ir_type *supertype, int pos);
537
538 /** Finds supertype in the list of supertypes and removes it */
539 void    remove_class_supertype(ir_type *clss, ir_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 ir_type *clss);
577 /** Sets the peculiarity of the class. */
578 void        set_class_peculiarity (ir_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 (ir_type *clss, int dfn);
583 int  get_class_dfn (const ir_type *clss);
584
585 /** Returns true if a type is a class type. */
586 int is_Class_type(const ir_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 ir_type *new_type_struct (ident *name);
605 /** Creates a new type struct with debug information. */
606 ir_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   (ir_type *strct, entity *member);
612
613 /** Returns the number of members of this struct. */
614 int     get_struct_n_members (const ir_type *strct);
615
616 /** Returns the member at position pos, 0 <= pos < n_member */
617 entity *get_struct_member   (const ir_type *strct, int pos);
618
619 /** Returns index of member in strct, -1 if not contained. */
620 int     get_struct_member_index(ir_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   (ir_type *strct, int pos, entity *member);
625
626 /** Finds member in the list of members and removes it. */
627 void    remove_struct_member (ir_type *strct, entity *member);
628
629 /** Returns true if a type is a struct type. */
630 int     is_Struct_type(const ir_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 ir_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 ir_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 ir_type *method);
698
699 /** Returns the type of the parameter at position pos of a method. */
700 ir_type *get_method_param_type(ir_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(ir_type *method, int pos, ir_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(ir_type *method, int pos);
708 /**
709  * Returns a type that represents the copied value arguments.
710  */
711 ir_type *get_method_value_param_type(const ir_type *method);
712
713 /** Returns the number of results of a method type. */
714 int   get_method_n_ress   (const ir_type *method);
715 /** Returns the return type of a method type at position pos. */
716 ir_type *get_method_res_type(ir_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(ir_type *method, int pos, ir_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(ir_type *method, int pos);
724
725 /**
726  * Returns a type that represents the copied value results.
727  */
728 ir_type *get_method_value_res_type(const ir_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 ir_type *method);
745
746 /** Sets the variadicity of a method. */
747 void set_method_variadicity(ir_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 ir_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(ir_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_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
789                                          a lowering phase will remove all calls to it. */
790   mtp_property_inherited = 0x40000000  /**< used only in irg's, means property is inherited
791                                          from type. */
792 } mtp_additional_property;
793
794 /** Returns the mask of the additional graph properties. */
795 unsigned get_method_additional_properties(const ir_type *method);
796
797 /** Sets the mask of the additional graph properties. */
798 void set_method_additional_properties(ir_type *method, unsigned property_mask);
799
800 /** Sets one additional graph property. */
801 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
802
803 /**
804  * calling conventions: lower 24 bits are the number of register parameters,
805  * upper 8 encode the calling conventions
806  */
807 typedef enum {
808   cc_reg_param        = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
809                                          This flag may be set as default on some architectures. */
810   cc_last_on_top      = 0x02000000, /**< The last non-register parameter is transmitted on top of
811                                              the stack. This is equivalent to the stdcall or pascal
812                                              calling convention. If this flag is not set, the first
813                                              non-register parameter is used (cdecl calling convention) */
814   cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
815                                          function calls (stdcall). */
816   cc_this_call        = 0x08000000, /**< The first parameter is a this pointer and is transmitted
817                                          in a special way. */
818
819
820   cc_bits             = (0xFF << 24)  /**< the calling convention bits */
821 } calling_convention;
822
823 /* some often used cases: made as defines for firmjni */
824 /** cdecl calling convention */
825 #define cc_cdecl_set    (0)
826 /** stdcall calling convention */
827 #define cc_stdcall_set  cc_callee_clear_stk
828 /** fastcall calling convention */
829 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
830
831 /** return the default calling convention for method types */
832 unsigned get_default_cc_mask(void);
833
834 /**
835  * check for the CDECL calling convention
836  */
837 #define IS_CDECL(cc_mask)     (((cc_mask) & cc_bits) == cc_cdecl_set)
838
839 /**
840  * check for the STDCALL calling convention
841  */
842 #define IS_STDCALL(cc_mask)   (((cc_mask) & cc_bits) == cc_stdcall_set)
843
844 /**
845  * check for the FASTCALL calling convention
846  */
847 #define IS_FASTCALL(cc_mask)  (((cc_mask) & cc_bits) == cc_fastcall_set)
848
849 /**
850  * set the CDECL convention bits
851  */
852 #define SET_CDECL(cc_mask)    (((cc_mask) & ~cc_bits) | cc_cdecl_set)
853
854 /**
855  * set the STDCALL convention bits
856  */
857 #define SET_STDCALL(cc_mask)  (((cc_mask) & ~cc_bits) | cc_stdcall_set)
858
859 /**
860  * set the FASTCALL convention bits
861  */
862 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
863
864 /** Returns the calling convention of an entities graph. */
865 unsigned get_method_calling_convention(const ir_type *method);
866
867 /** Sets the calling convention of an entities graph. */
868 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
869
870 /** Returns the number of registers parameters, 0 means default. */
871 unsigned get_method_n_regparams(ir_type *method);
872
873 /** Sets the number of registers parameters, 0 means default. */
874 void set_method_n_regparams(ir_type *method, unsigned n_regs);
875
876 /** Returns true if a type is a method type. */
877 int   is_Method_type     (const ir_type *method);
878
879 /**
880  *   @page union_type   Representation of a union (variant) type.
881  *
882  *   The union type represents union types.
883  *   - n_types:     Number of unioned types.
884  *   - members:     Entities for unioned types.  Fixed length array.
885  *                  This is a dynamic list that can be grown with an "add_" function,
886  *                  but not shrinked.
887  */
888 /** Creates a new type union. */
889 ir_type   *new_type_union (ident *name);
890
891 /** Creates a new type union with debug information. */
892 ir_type   *new_d_type_union (ident *name, dbg_info* db);
893
894 /* --- manipulate private fields of struct --- */
895
896 /** Returns the number of unioned types of this union */
897 int     get_union_n_members      (const ir_type *uni);
898
899 /** Adds a new entity to a union type */
900 void    add_union_member (ir_type *uni, entity *member);
901
902 /** Returns the entity at position pos of a union */
903 entity *get_union_member (const ir_type *uni, int pos);
904
905 /** Overwrites a entity at position pos in a union type. */
906 void    set_union_member (ir_type *uni, int pos, entity *member);
907
908 /** Finds member in the list of members and removes it. */
909 void    remove_union_member (ir_type *uni, entity *member);
910
911 /** Returns true if a type is a union type. */
912 int     is_Union_type          (const ir_type *uni);
913
914 /**
915  * @page array_type Representation of an array type
916  *
917  * The array type represents rectangular multi dimensional arrays.
918  * The constants representing the bounds must be allocated to
919  * get_const_code_irg() by setting current_ir_graph accordingly.
920  *
921  * - n_dimensions:    Number of array dimensions.
922  * - *lower_bound:    Lower bounds of dimensions.  Usually all 0.
923  * - *upper_bound:    Upper bounds or dimensions.
924  * - *element_type:   The type of the array elements.
925  * - *element_ent:    An entity for the array elements to be used for
926  *                      element selection with Sel.
927  * @todo
928  *   Do we need several entities?  One might want
929  *   to select a dimension and not a single element in case of multi
930  *   dimensional arrays.
931  */
932
933 /** Create a new type array.
934  *
935  * Sets n_dimension to dimension and all dimension entries to NULL.
936  * Initializes order to the order of the dimensions.
937  * The entity for array elements is built automatically.
938  * Set dimension sizes after call to constructor with set_* routines.
939  */
940 ir_type *new_type_array         (ident *name, int n_dimensions,
941                   ir_type *element_type);
942
943 /** Create a new type array with debug information.
944  *
945  * Sets n_dimension to dimension and all dimension entries to NULL.
946  * Initializes order to the order of the dimensions.
947  * The entity for array elements is built automatically.
948  * Set dimension sizes after call to constructor with set_* routines.
949  * A legal array type must have at least one dimension set.
950  */
951 ir_type *new_d_type_array         (ident *name, int n_dimensions,
952                   ir_type *element_type, dbg_info* db);
953
954 /* --- manipulate private fields of array type --- */
955
956 /** Returns the number of array dimensions of this type. */
957 int   get_array_n_dimensions (const ir_type *array);
958
959 /**
960  * Allocates Const nodes of mode_I for one array dimension.
961  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
962  */
963 void  set_array_bounds_int   (ir_type *array, int dimension, int lower_bound,
964                                                           int upper_bound);
965 /**
966  * Sets the bounds for one array dimension.
967  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
968  */
969 void  set_array_bounds       (ir_type *array, int dimension, ir_node *lower_bound,
970                                                           ir_node *upper_bound);
971 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
972 void  set_array_lower_bound  (ir_type *array, int dimension, ir_node *lower_bound);
973
974 /** Allocates Const nodes of mode_I for the lower bound of an array
975     dimension, i.e. [lower,upper[ */
976 void  set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
977
978 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
979 void  set_array_upper_bound  (ir_type *array, int dimension, ir_node *upper_bound);
980
981 /** Allocates Const nodes of mode_I for the upper bound of an array
982     dimension, i.e. [lower,upper[. */
983 void  set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
984
985 /** Returns true if lower bound != Unknown. */
986 int       has_array_lower_bound     (const ir_type *array, int dimension);
987 /** Returns the lower bound of an array. */
988 ir_node * get_array_lower_bound     (const ir_type *array, int dimension);
989 /** Works only if bound is Const node with tarval that can be converted to long. */
990 long      get_array_lower_bound_int (const ir_type *array, int dimension);
991 /** returns true if lower bound != Unknown */
992 int       has_array_upper_bound     (const ir_type *array, int dimension);
993 /** Returns the upper bound of an array. */
994 ir_node * get_array_upper_bound     (const ir_type *array, int dimension);
995 /** Works only if bound is Const node with tarval that can be converted to long. */
996 long      get_array_upper_bound_int (const ir_type *array, int dimension);
997
998 /** Sets an array dimension to a specific order. */
999 void set_array_order (ir_type *array, int dimension, int order);
1000
1001 /** Returns the order of an array dimension. */
1002 int  get_array_order (const ir_type *array, int dimension);
1003
1004 /** Find the array dimension that is placed at order ord. */
1005 int find_array_dimension(const ir_type *array, int order);
1006
1007 /** Sets the array element type. */
1008 void  set_array_element_type (ir_type *array, ir_type* tp);
1009
1010 /** Gets the array element type. */
1011 ir_type *get_array_element_type (ir_type *array);
1012
1013 /** Sets the array element entity. */
1014 void  set_array_element_entity (ir_type *array, entity *ent);
1015
1016 /** Get the array element entity. */
1017 entity *get_array_element_entity (const ir_type *array);
1018
1019 /** Returns true if a type is an array type. */
1020 int    is_Array_type(const ir_type *array);
1021
1022 /**
1023  * @page enumeration_type   Representation of an enumeration type
1024  *
1025  * Enumeration types need not necessarily be represented explicitly
1026  * by Firm types, as the frontend can lower them to integer constants as
1027  * well.  For debugging purposes or similar tasks this information is useful.
1028  *
1029  * - *enum:         The target values representing the constants used to
1030  *                  represent individual enumerations.
1031  * - *enum_nameid:  Idents containing the source program name of the enumeration
1032  *                  constants
1033  */
1034 /** Create a new type enumeration -- set the enumerators independently. */
1035 ir_type   *new_type_enumeration    (ident *name, int n_enums);
1036
1037 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1038 ir_type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db);
1039
1040 /* --- manipulate fields of enumeration type. --- */
1041
1042 /** Returns the number of enumeration values of this enumeration */
1043 int     get_enumeration_n_enums (const ir_type *enumeration);
1044
1045 /** Sets the enumeration value at a given position. */
1046 void    set_enumeration_enum    (ir_type *enumeration, int pos, tarval *con);
1047
1048 /** Returns the enumeration value at a given position. */
1049 tarval *get_enumeration_enum    (const ir_type *enumeration, int pos);
1050
1051 /** Assign an ident to an enumeration value at a given position. */
1052 void    set_enumeration_nameid  (ir_type *enumeration, int pos, ident *id);
1053
1054 /** Returns the assigned ident of an enumeration value at a given position. */
1055 ident  *get_enumeration_nameid  (const ir_type *enumeration, int pos);
1056
1057 /** Returns the assigned name of an enumeration value at a given position. */
1058 const char *get_enumeration_name(const ir_type *enumeration, int pos);
1059
1060 /** Returns true if a type is a enumeration type. */
1061 int     is_Enumeration_type     (const ir_type *enumeration);
1062
1063 /**
1064  * @page pointer_type   Representation of a pointer type
1065  *
1066  * The mode of the pointer type must be a reference mode.
1067  *
1068  * Pointer types:
1069  * - points_to:      The type of the entity this pointer points to.
1070  */
1071
1072 /** Creates a new type pointer. */
1073 ir_type *new_type_pointer           (ident *name, ir_type *points_to, ir_mode *ptr_mode);
1074
1075 /** Creates a new type pointer with debug information. */
1076 ir_type *new_d_type_pointer         (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
1077
1078 /* --- manipulate fields of type_pointer --- */
1079
1080 /** Sets the type to which a pointer points to. */
1081 void  set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
1082
1083 /** Returns the type to which a pointer points to. */
1084 ir_type *get_pointer_points_to_type (ir_type *pointer);
1085
1086 /** Returns true if a type is a pointer type. */
1087 int   is_Pointer_type            (const ir_type *pointer);
1088
1089 /** Returns the first pointer type that has as points_to tp.
1090  *  Not efficient: O(#types).
1091  *  If not found returns unknown_type. */
1092 ir_type *find_pointer_type_to_type (ir_type *tp);
1093
1094 /**
1095  * @page primitive_type Representation of a primitive type
1096  *
1097  * Primitive types are types that represent indivisible data values that
1098  * map directly to modes.  They don't have a private attribute.  The
1099  * important information they carry is held in the common mode field.
1100 */
1101 /** Creates a new primitive type. */
1102 ir_type *new_type_primitive (ident *name, ir_mode *mode);
1103
1104 /** Creates a new primitive type with debug information. */
1105 ir_type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1106
1107 /** Returns true if a type is a primitive type. */
1108 int  is_Primitive_type  (const ir_type *primitive);
1109
1110
1111 /**
1112  * @page none_type
1113  *
1114  *  This type is an auxiliary type dedicated to support type analyses.
1115  *
1116  *  The none type represents that there is no type.  The type can be used to
1117  *  initialize fields of type* that actually can not contain a type or that
1118  *  are initialized for an analysis. There exists exactly one type none.
1119  *  This type is not on the type list in ir_prog. It is
1120  *  allocated when initializing the type module.
1121  *
1122  *  The following values are set:
1123  *    - mode:  mode_BAD
1124  *    - name:  "type_none"
1125  *    - state: layout_fixed
1126  *    - size:  0
1127  */
1128 /** A variable that contains the only none type. */
1129 extern type *firm_none_type;
1130 /** Returns the none type */
1131 ir_type *get_none_type(void);
1132
1133 /**
1134  * @page unknown_type
1135  *
1136  *  This type is an auxiliary type dedicated to support type analyses.
1137  *
1138  *  The unknown type represents that there could be a type, but it is not
1139  *  known.  This type can be used to initialize fields before an analysis (not known
1140  *  yet) or to represent the top of a lattice (could not be determined).  There exists
1141  *  exactly one type unknown. This type is not on the type list in ir_prog.  It is
1142  *  allocated when initializing the type module.
1143  *
1144  *  The following values are set:
1145  *    - mode:  mode_ANY
1146  *    - name:  "type_unknown"
1147  *    - state: layout_fixed
1148  *    - size:  0
1149  */
1150 /** A variable that contains the only unknown type. */
1151 extern type *firm_unknown_type;
1152 /** Returns the unknown type */
1153 ir_type *get_unknown_type(void);
1154
1155
1156 /**
1157  *  Checks whether a type is atomic.
1158  *  @param tp   any type
1159  *  @return true if type is primitive, pointer or enumeration
1160  */
1161 int is_atomic_type(const ir_type *tp);
1162
1163 /* --- Support for compound types --- */
1164
1165 /**
1166  * Gets the number of elements in a firm compound type.
1167  *
1168  * This is just a comfortability function, because structs and
1169  * classes can often be treated be the same code, but they have
1170  * different access functions to their members.
1171  *
1172  * @param tp  The type (must be struct, union or class).
1173  *
1174  * @return Number of members in the compound type.
1175  */
1176 int get_compound_n_members(const ir_type *tp);
1177
1178 /**
1179  * Gets the member of a firm compound type at position pos.
1180  *
1181  * @param tp  The type (must be struct, union or class).
1182  * @param pos The number of the member.
1183  *
1184  * @return The member entity at position pos.
1185  *
1186  * @see get_compound_n_members() for justification of existence.
1187  */
1188 entity *get_compound_member(const ir_type *tp, int pos);
1189
1190 /**
1191  * Checks whether a type is compound.
1192  *
1193  * @param tp - any type
1194  *
1195  * @return true if the type is class, structure, union or array type.
1196  */
1197 int is_compound_type(const ir_type *tp);
1198
1199 /**
1200  * Checks, whether a type is a frame type
1201  */
1202 int is_frame_type(const ir_type *tp);
1203
1204 /**
1205  * Makes a new frame type. Frame types are class types,
1206  * so all class access functions work.
1207  * Frame types are not in the global list of types.
1208  */
1209 ir_type   *new_type_frame(ident *name);
1210
1211 /*-----------------------------------------------------------------*/
1212 /** Debug aides                                                   **/
1213 /*-----------------------------------------------------------------*/
1214
1215 /**
1216  *  Outputs a unique number for this type if libfirm is compiled for
1217  *  debugging, (configure with --enable-debug) else returns the address
1218  *  of the type cast to long.
1219  */
1220 long get_type_nr(const ir_type *tp);
1221
1222
1223 # endif /* _TYPE_H_ */