3 * File name: ir/tr/type.h
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck
8 * Copyright: (c) 2001-2003 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Datastructure to hold type information.
18 * This module supplies a datastructure to represent all types
19 * known in the compiled program. This includes types specified
20 * in the program as well as types defined by the language. In the
21 * view of the intermediate representation there is no difference
22 * between these types. Finally it specifies some auxiliary types.
24 * There exist several kinds of types, arranged by the structure of
25 * the type. A type is described by a set of attributes. Some of
26 * these attributes are common to all types, others depend on the
29 * Types are different from the modes defined in irmode: Types are
30 * on the level of the programming language, modes at the level of
31 * the target processor.
35 #ifndef _FIRM_TR_TYPE_H_
36 #define _FIRM_TR_TYPE_H_
38 #include "firm_types.h"
40 #include "firm_common.h"
44 * An abstract data type to represent types.
46 * This is the abstract data type with which any type known in the
47 * compiled program can be represented. This includes types specified
48 * in the program as well as types defined by the language. In the
49 * view of the intermediate representation there is no difference
50 * between these types.
52 * There exist several kinds of types, arranged by the structure of
53 * the type. These are distinguished by a type opcode.
54 * A type is described by a set of attributes. Some of these attributes
55 * are common to all types, others depend on the kind of the type.
57 * The following describes the common attributes. They can only be
58 * accessed by the functions given below.
60 * The common fields are:
62 * - firm_kind: A firm_kind tag containing k_type. This is useful
63 * for dynamically checking whether a node is a type node.
64 * - type_op: A tp_op specifying the kind of the type.
65 * - mode: The mode to be used to represent the type on a machine.
66 * - name: An identifier specifying the name of the type. To be
67 * set by the frontend.
68 * - size: The size of the type, i.e. an entity of this type will
69 * occupy size bits in memory. In several cases this is
70 * determined when fixing the layout of this type (class,
71 * struct, union, array, enumeration).
72 * - alignment The alignment of the type, i.e. an entity of this type will
73 * be allocated an an address in memory with this alignment.
74 * In several cases this is determined when fixing the layout
75 * of this type (class, struct, union, array)
76 * - state: The state of the type. The state represents whether the
77 * layout of the type is undefined or fixed (values: layout_undefined
78 * or layout_fixed). Compound types can have an undefined
79 * layout. The layout of the basic types primitive and pointer
80 * is always layout_fixed. If the layout of
81 * compound types is fixed all entities must have an offset
82 * and the size of the type must be set.
83 * A fixed layout for enumeration types means that each enumeration
84 * is associated with an implementation value.
85 * - visit: A counter for walks of the type information.
86 * - link: A void* to associate some additional information with the type.
88 * These fields can only be accessed via access functions.
90 * Depending on the value of @c type_op, i.e., depending on the kind of the
91 * type the adt contains further attributes. These are documented below.
95 * @link class_type class @endlink, @link struct_type struct @endlink,
96 * @link method_type method @endlink, @link union_type union @endlink,
97 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
98 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
101 * mode maybe not global field??
103 #ifndef _IR_TYPE_TYPEDEF_
104 #define _IR_TYPE_TYPEDEF_
105 typedef struct ir_type ir_type;
108 # include "type_or_entity.h"
110 /** frees all entities associated with a type.
111 Does not free array entity.
112 Warning: make sure these entities are not referenced anywhere else.
114 void free_type_entities(ir_type *tp);
116 /** Frees the memory used by the type.
118 * Removes the type from the type list. Does not free the entities
119 * belonging to the type, except for the array element entity. Does
120 * not free if tp is "none" or "unknown". Frees entities in value
121 * param subtypes of method types!!! Make sure these are not
122 * referenced any more. Further make sure there is no pointer type
123 * that refers to this type. */
124 void free_type(ir_type *tp);
126 const tp_op*get_type_tpop(const ir_type *tp);
127 ident* get_type_tpop_nameid(const ir_type *tp);
128 const char* get_type_tpop_name(const ir_type *tp);
129 tp_opcode get_type_tpop_code(const ir_type *tp);
131 ident* get_type_ident(const ir_type *tp);
132 void set_type_ident(ir_type *tp, ident* id);
133 const char* get_type_name(const ir_type *tp);
135 /** This enumeration flags the visibility of entities and types.
137 * This is necessary for partial compilation.
138 * We rely on the ordering of the flags.
141 visibility_local, /**< The entity is only visible locally. This is the default for
143 The type is only visible locally. All instances are allocated
144 locally, and no pointer to entities of this type are passed
145 out of this compilation unit. */
146 visibility_external_visible, /**< The entity is visible to other external program parts, but
147 it is defined here. It may not be optimized away. The entity must
149 For types: entities of this type can be accessed externally. No
150 instances of this type are allocated externally. */
151 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
152 must not allocate memory for this entity. The entity must
153 be static_allocated. This can also be an external defined
155 For types: entities of this type are allocated and accessed from
156 external code. Default for types. */
159 /** The visibility of a type.
161 * The visibility of a type indicates, whether entities of this type
162 * are accessed or allocated in external code.
164 * An entity of a type is allocated in external code, if the external
165 * code declares a variable of this type, or dynamically allocates
166 * an entity of this type. If the external code declares a (compound)
167 * type, that contains entities of this type, the visibility also
168 * must be external_allocated.
170 * The visibility must be higher than that of all entities, if the
171 * type is a compound. Here it is questionable, what happens with
172 * static entities. If these are accessed external by direct reference,
173 * (a static call to a method, that is also in the dispatch table)
174 * it should not affect the visibility of the type.
177 * @@@ Do we need a visibility for types?
178 * I change the layout of types radically when doing type splitting.
179 * I need to know, which fields of classes are accessed in the RTS,
180 * e.g., [_length. I may not move [_length to the split part.
181 * The layout though, is a property of the type.
183 * One could also think of changing the mode of a type ...
185 * But, we could also output macros to access the fields, e.g.,
186 * ACCESS_[_length (X) X->length // conventional
187 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
189 * For now I implement this function, that returns the visibility
190 * based on the visibility of the entities of a compound ...
192 * This function returns visibility_external_visible if one or more
193 * entities of a compound type have visibility_external_visible.
194 * Entities of types are never visibility_external_allocated (right?).
195 * Else returns visibility_local.
197 visibility get_type_visibility (const ir_type *tp);
198 void set_type_visibility (ir_type *tp, visibility v);
202 /** The state of the type layout. */
204 layout_undefined, /**< The layout of this type is not defined.
205 Address computation to access fields is not
206 possible, fields must be accessed by Sel
207 nodes. This is the default value except for
208 pointer, primitive and method types. */
209 layout_fixed /**< The layout is fixed, all component/member entities
210 have an offset assigned. Size of the type is known.
211 Arrays can be accessed by explicit address
212 computation. Default for pointer, primitive and method
216 /** Returns a human readable string for the enum entry. */
217 const char *get_type_state_name(type_state s);
219 /** Returns the type layout state of a type. */
220 type_state get_type_state(const ir_type *tp);
222 /** Sets the type layout state of a type.
224 * For primitives, pointer and method types the layout is always fixed.
225 * This call is legal but has no effect.
227 void set_type_state(ir_type *tp, type_state state);
229 /** Returns the mode of a type.
231 * Returns NULL for all non atomic types.
233 ir_mode* get_type_mode(const ir_type *tp);
235 /** Sets the mode of a type.
237 * Only has an effect on primitive, enumeration and pointer types.
239 void set_type_mode(ir_type *tp, ir_mode* m);
241 /** Returns the size of a type in bytes, returns -1 if the size is NOT
242 * a byte size, i.e. not dividable by 8. */
243 int get_type_size_bytes(const ir_type *tp);
245 /** Returns the size of a type in bits. */
246 int get_type_size_bits(const ir_type *tp);
248 /** Sets the size of a type in bytes.
250 * For primitive, enumeration, pointer and method types the size
251 * is always fixed. This call is legal but has no effect.
253 void set_type_size_bytes(ir_type *tp, int size);
255 /** Sets the size of a type in bits.
257 * For primitive, enumeration, pointer and method types the size
258 * is always fixed. This call is legal but has no effect.
260 void set_type_size_bits(ir_type *tp, int size);
262 /** Returns the alignment of a type in bytes.
264 * Returns -1 if the alignment is NOT
265 * a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
266 int get_type_alignment_bytes(ir_type *tp);
268 /** Returns the alignment of a type in bits.
270 * If the alignment of a type is
271 * not set, it is calculated here according to the following rules:
272 * -#.) if a type has a mode, the alignment is the mode size.
273 * -#.) compound types have the alignment of there biggest member.
274 * -#.) array types have the alignment of there element type.
275 * -#.) method types return 0 here.
276 * -#.) all other types return 8 here (i.e. aligned at byte).
278 int get_type_alignment_bits(ir_type *tp);
280 /** Sets the alignment of a type in bytes. */
281 void set_type_alignment_bytes(ir_type *tp, int size);
283 /** Sets the alignment of a type in bits.
285 * For method types the alignment is always fixed.
286 * This call is legal but has no effect.
288 void set_type_alignment_bits(ir_type *tp, int size);
290 unsigned long get_type_visited(const ir_type *tp);
291 void set_type_visited(ir_type *tp, unsigned long num);
292 /* Sets visited field in type to type_visited. */
293 void mark_type_visited(ir_type *tp);
294 int type_visited(const ir_type *tp);
295 int type_not_visited(const ir_type *tp);
297 /** Returns the associated link field of a type. */
298 void* get_type_link(const ir_type *tp);
299 /** Sets the associated link field of a type. */
300 void set_type_link(ir_type *tp, void *l);
303 * Visited flag to traverse the type information.
305 * Increase this flag by one before traversing the type information
306 * using inc_master_type_visited().
307 * Mark type nodes as visited by mark_type_visited(ir_type).
308 * Check whether node was already visited by type_visited(ir_type)
309 * and type_not_visited(ir_type).
310 * Or use the function to walk all types.
314 void set_master_type_visited(unsigned long val);
315 unsigned long get_master_type_visited(void);
316 void inc_master_type_visited(void);
319 * Checks whether a pointer points to a type.
321 * @param thing an arbitrary pointer
324 * true if the thing is a type, else false
326 int is_type (const void *thing);
329 * Checks whether two types are structurally equal.
331 * @param typ1 the first type
332 * @param typ2 the second type
335 * true if the types are equal, else false.
337 * Types are equal if :
338 * - they are the same type kind
339 * - they have the same name
340 * - they have the same mode (if applicable)
341 * - they have the same type_state and, ev., the same size
342 * - they are class types and have:
343 * - the same members (see same_entity in entity.h)
344 * - the same supertypes -- the C-pointers are compared --> no recursive call.
345 * - the same number of subtypes. Subtypes are not compared,
346 * as this could cause a cyclic test.
347 * - the same peculiarity
348 * - they are structure types and have the same members
349 * - they are method types and have
350 * - the same parameter types
351 * - the same result types
352 * - they are union types and have the same members
353 * - they are array types and have
354 * - the same number of dimensions
355 * - the same dimension bounds
356 * - the same dimension order
357 * - the same element type
358 * - they are enumeration types and have the same enumerator names
359 * - they are pointer types and have the identical points_to type
360 * (i.e., the same C-struct to represent the type, type_id is skipped.
361 * This is to avoid endless recursions; with pointer types cyclic
362 * type graphs are possible.)
364 int equal_type(ir_type *typ1, ir_type *typ2);
367 * Checks whether two types are structural comparable.
369 * @param st pointer type
370 * @param lt pointer type
373 * true if type st is smaller than type lt, i.e. whenever
374 * lt is expected a st can be used.
376 * - they are the same type kind
377 * - mode(st) < mode (lt) (if applicable)
378 * - they are class types and st is (transitive) subtype of lt,
379 * - they are structure types and
380 * - the members of st have exactly one counterpart in lt with the same name,
381 * - the counterpart has a bigger type.
382 * - they are method types and have
383 * - the same number of parameter and result types,
384 * - the parameter types of st are smaller than those of lt,
385 * - the result types of st are smaller than those of lt
386 * - they are union types and have the members of st have exactly one
387 * @return counterpart in lt and the type is smaller
388 * - they are array types and have
389 * - the same number of dimensions
390 * - all bounds of lt are bound of st
391 * - the same dimension order
392 * - the same element type
394 * - the element type of st is smaller than that of lt
395 * - the element types have the same size and fixed layout.
396 * - they are enumeration types and have the same enumerator names
397 * - they are pointer types and have the points_to type of st is
398 * @return smaller than the points_to type of lt.
401 int smaller_type (ir_type *st, ir_type *lt);
404 * @page class_type Representation of a class type
406 * If the type opcode is set to type_class the type represents class
407 * types. A list of fields and methods is associated with a class.
408 * Further a class can inherit from and bequest to other classes.
410 * The following attributes are private to this type kind:
411 * - member: All entities belonging to this class. This are method entities
412 * which have type_method or fields that can have any of the
413 * following type kinds: type_class, type_struct, type_union,
414 * type_array, type_enumeration, type_pointer, type_primitive.
416 * The following two are dynamic lists that can be grown with an "add_" function,
419 * - subtypes: A list of direct subclasses.
421 * - supertypes: A list of direct superclasses.
423 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
424 * "description" it only is a description of requirements to a class,
425 * as, e.g., a Java interface. The class will never be allocated.
426 * Peculiarity inherited is only possible for entities. An entity
427 * is of peculiarity inherited if the compiler generated the entity
428 * to explicitly resolve inheritance. An inherited method entity has
430 * Values: description, existent, inherited. Default: existent.
432 * - type_info: An entity representing the type information of this class.
433 * This entity can be of arbitrari type, Firm did not use it yet.
434 * It allows to express the coupling of a type with an entity
435 * representing this type. This information is useful for lowering
436 * of InstOf and TypeChk nodes. Default: NULL
438 * - vtable_size: The size of this class vritual function table.
441 * - final: A final class is always a leaf in the class hierarchy. Final
442 * classes cannot be super classes of other ones. As this information
443 * can only be computed in whole world compilations, we allow to
444 * set this flag. It is used in optimizations if get_opt_closed_world()
445 * is false. Default: false
448 /** Creates a new class type. */
449 ir_type *new_type_class (ident *name);
451 /** Creates a new class type with debug information. */
452 ir_type *new_d_type_class (ident *name, dbg_info *db);
454 /* --- manipulate private fields of class type --- */
456 /** Adds the entity as member of the class. */
457 void add_class_member (ir_type *clss, entity *member);
459 /** Returns the number of members of this class. */
460 int get_class_n_members (const ir_type *clss);
462 /** Returns the member at position pos, 0 <= pos < n_member */
463 entity *get_class_member (const ir_type *clss, int pos);
465 /** Returns index of mem in clss, -1 if not contained. */
466 int get_class_member_index(const ir_type *clss, entity *mem);
468 /** Finds the member with name 'name'. If several members with the same
469 * name returns one of them. Returns NULL if no member found. */
470 entity *get_class_member_by_name(ir_type *clss, ident *name);
472 /** Overwrites the member at position pos, 0 <= pos < n_member with
473 * the passed entity. */
474 void set_class_member (ir_type *clss, entity *member, int pos);
476 /** Replaces complete member list in class type by the list passed.
478 * Copies the list passed. This function is necessary to reduce the number of members.
479 * members is an array of entities, num the size of this array. Sets all
480 * owners of the members passed to clss. */
481 void set_class_members (ir_type *clss, entity *members[], int arity);
483 /** Finds member in the list of members and removes it.
485 * Shrinks the member list, so iterate from the end!!!
486 * Does not deallocate the entity. */
487 void remove_class_member(ir_type *clss, entity *member);
490 /** Adds subtype as subtype to clss.
492 * Checks whether clss is a supertype of subtype. If not
493 * adds also clss as supertype to subtype. */
494 void add_class_subtype (ir_type *clss, ir_type *subtype);
496 /** Returns the number of subtypes */
497 int get_class_n_subtypes (const ir_type *clss);
499 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
500 ir_type *get_class_subtype (ir_type *clss, int pos);
502 /** Returns the index to access subclass as subtype of class.
504 * If subclass is no direct subtype of class returns -1.
506 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
508 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
510 * Does not set the corresponding supertype relation for subtype: this might
511 * be a different position! */
512 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos);
514 /** Finds subtype in the list of subtypes and removes it */
515 void remove_class_subtype(ir_type *clss, ir_type *subtype);
517 /* Convenience macros */
518 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
519 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
520 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
521 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
522 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
523 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
525 /** Adds supertype as supertype to class.
527 * Checks whether clss is a subtype of supertype. If not
528 * adds also clss as subtype to supertype. */
529 void add_class_supertype (ir_type *clss, ir_type *supertype);
531 /** Returns the number of supertypes */
532 int get_class_n_supertypes (const ir_type *clss);
534 /** Returns the index to access superclass as supertype of class.
536 * If superclass is no direct supertype of class returns -1.
538 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
540 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
541 ir_type *get_class_supertype (ir_type *clss, int pos);
543 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
545 * Does not set the corresponding subtype relation for supertype: this might
546 * be at a different position! */
547 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos);
549 /** Finds supertype in the list of supertypes and removes it */
550 void remove_class_supertype(ir_type *clss, ir_type *supertype);
552 /** Convenience macro */
553 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
554 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
555 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
556 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
557 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
558 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
560 /** Convenience macro */
561 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
562 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
563 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
564 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
565 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
566 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
568 /** This enumeration flags the peculiarity of entities and types. */
569 typedef enum peculiarity {
570 peculiarity_description, /**< Represents only a description. The entity/type is never
571 allocated, no code/data exists for this entity/type.
572 @@@ eventually rename to descriptive (adjective as the others!)*/
573 peculiarity_inherited, /**< Describes explicitly that other entities are
574 inherited to the owner of this entity.
575 Overwrites must refer to at least one other
576 entity. If this is a method entity there exists
577 no irg for this entity, only for one of the
580 peculiarity_existent /**< The entity/type (can) exist.
581 @@@ eventually rename to 'real' i.e., 'echt'
582 This serves better as opposition to description _and_ inherited.*/
584 const char *get_peculiarity_string(peculiarity p);
586 /** Returns the peculiarity of the class. */
587 peculiarity get_class_peculiarity (const ir_type *clss);
588 /** Sets the peculiarity of the class. */
589 void set_class_peculiarity (ir_type *clss, peculiarity pec);
591 /** Returns the type info entity of a class. */
592 entity *get_class_type_info(const ir_type *clss);
594 /** Set a type info entity for the class. */
595 void set_class_type_info(ir_type *clss, entity *ent);
597 /** Returns the size of the virtual function table. */
598 unsigned get_class_vtable_size(const ir_type *clss);
600 /** Sets a new size of the virtual function table. */
601 void set_class_vtable_size(ir_type *clss, unsigned size);
603 /** Returns non-zero if a class is final. */
604 int is_class_final(const ir_type *clss);
606 /** Sets if a class is final. */
607 void set_class_final(ir_type *clss, int flag);
609 /* Set and get a class' dfn --
610 @todo This is an undocumented field, subject to change! */
611 void set_class_dfn (ir_type *clss, int dfn);
612 int get_class_dfn (const ir_type *clss);
614 /** Returns true if a type is a class type. */
615 int is_Class_type(const ir_type *clss);
618 * @page struct_type Representation of a struct type
620 * Type_strct represents aggregate types that consist of a list
622 * The following attributes are private to this type kind:
623 * - member: All entities belonging to this class. This are the fields
624 * that can have any of the following types: type_class,
625 * type_struct, type_union, type_array, type_enumeration,
626 * type_pointer, type_primitive.
627 * This is a dynamic list that can be grown with an "add_" function,
629 * This is a dynamic list that can be grown with an "add_" function,
632 /** Creates a new type struct */
633 ir_type *new_type_struct (ident *name);
634 /** Creates a new type struct with debug information. */
635 ir_type *new_d_type_struct (ident *name, dbg_info* db);
637 /* --- manipulate private fields of struct --- */
639 /** Adds the entity as member of the struct. */
640 void add_struct_member (ir_type *strct, entity *member);
642 /** Returns the number of members of this struct. */
643 int get_struct_n_members (const ir_type *strct);
645 /** Returns the member at position pos, 0 <= pos < n_member */
646 entity *get_struct_member (const ir_type *strct, int pos);
648 /** Returns index of member in strct, -1 if not contained. */
649 int get_struct_member_index(const ir_type *strct, entity *member);
651 /** Overwrites the member at position pos, 0 <= pos < n_member with
652 the passed entity. */
653 void set_struct_member (ir_type *strct, int pos, entity *member);
655 /** Finds member in the list of members and removes it. */
656 void remove_struct_member (ir_type *strct, entity *member);
658 /** Returns true if a type is a struct type. */
659 int is_Struct_type(const ir_type *strct);
662 * @page method_type Representation of a method type
664 * A method type represents a method, function or procedure type.
665 * It contains a list of the parameter and result types, as these
666 * are part of the type description. These lists should not
667 * be changed by a optimization, as a change creates a new method
668 * type. Therefore optimizations should allocated new method types.
669 * The set_ routines are only for construction by a frontend.
671 * - n_params: Number of parameters to the procedure.
672 * A procedure in FIRM has only call by value parameters.
674 * - param_type: A list with the types of parameters. This list is ordered.
675 * The nth type in this list corresponds to the nth element
676 * in the parameter tuple that is a result of the start node.
677 * (See ircons.h for more information.)
680 * A list of entities (whose owner is a struct private to the
681 * method type) that represent parameters passed by value.
683 * - n_res: The number of results of the method. In general, procedures
684 * have zero results, functions one.
686 * - res_type: A list with the types of parameters. This list is ordered.
687 * The nth type in this list corresponds to the nth input to
688 * Return nodes. (See ircons.h for more information.)
691 * A list of entities (whose owner is a struct private to the
692 * method type) that represent results passed by value.
695 /* These macros define the suffixes for the types and entities used
696 to represent value parameters / results. */
697 #define VALUE_PARAMS_SUFFIX "val_param"
698 #define VALUE_RESS_SUFFIX "val_res"
700 /** Create a new method type.
702 * @param name the name (ident) of this type
703 * @param n_param the number of parameters
704 * @param n_res the number of results
706 * The arrays for the parameter and result types are not initialized by
709 ir_type *new_type_method (ident *name, int n_param, int n_res);
711 /** Create a new method type with debug information.
713 * @param name the name (ident) of this type
714 * @param n_param the number of parameters
715 * @param n_res the number of results
716 * @param db user defined debug information
718 * The arrays for the parameter and result types are not initialized by
721 ir_type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
723 /* -- manipulate private fields of method. -- */
725 /** Returns the number of parameters of this method. */
726 int get_method_n_params (const ir_type *method);
728 /** Returns the type of the parameter at position pos of a method. */
729 ir_type *get_method_param_type(ir_type *method, int pos);
730 /** Sets the type of the parameter at position pos of a method.
731 Also changes the type in the pass-by-value representation by just
732 changing the type of the corresponding entity if the representation is constructed. */
733 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
734 /** Returns an entity that represents the copied value argument. Only necessary
735 for compounds passed by value. This information is constructed only on demand. */
736 entity *get_method_value_param_ent(ir_type *method, int pos);
738 * Returns a type that represents the copied value arguments.
740 ir_type *get_method_value_param_type(const ir_type *method);
742 /** Returns the number of results of a method type. */
743 int get_method_n_ress (const ir_type *method);
744 /** Returns the return type of a method type at position pos. */
745 ir_type *get_method_res_type(ir_type *method, int pos);
746 /** Sets the type of the result at position pos of a method.
747 Also changes the type in the pass-by-value representation by just
748 changing the type of the corresponding entity if the representation is constructed. */
749 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
750 /** Returns an entity that represents the copied value result. Only necessary
751 for compounds passed by value. This information is constructed only on demand. */
752 entity *get_method_value_res_ent(ir_type *method, int pos);
755 * Returns a type that represents the copied value results.
757 ir_type *get_method_value_res_type(const ir_type *method);
760 * This enum flags the variadicity of methods (methods with a
761 * variable amount of arguments (e.g. C's printf). Default is
764 typedef enum variadicity {
765 variadicity_non_variadic, /**< non variadic */
766 variadicity_variadic /**< variadic */
769 /** Returns the null-terminated name of this variadicity. */
770 const char *get_variadicity_name(variadicity vari);
772 /** Returns the variadicity of a method. */
773 variadicity get_method_variadicity(const ir_type *method);
775 /** Sets the variadicity of a method. */
776 void set_method_variadicity(ir_type *method, variadicity vari);
779 * Returns the first variadic parameter index of a type.
780 * If this index was NOT set, the index of the last parameter
781 * of the method type plus one is returned for variadic functions.
782 * Non-variadic function types always return -1 here.
784 int get_method_first_variadic_param_index(const ir_type *method);
787 * Sets the first variadic parameter index. This allows to specify
788 * a complete call type (containing the type of all parameters)
789 * but still have the knowledge, which parameter must be passed as
792 void set_method_first_variadic_param_index(ir_type *method, int index);
795 * additional method type properties:
796 * Tell about special properties of a method type. Some
797 * of these may be discovered by analyses.
800 mtp_no_property = 0x00000000, /**< no additional properties, default */
801 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
802 its return values solely from its parameters.
803 GCC: __attribute__((const)). */
804 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
805 its return values solely from its parameters and
806 the memory they points to (or global vars).
807 GCC: __attribute__((pure)). */
808 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
810 GCC: __attribute__((noreturn)). */
811 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
812 GCC: __attribute__((nothrow)). */
813 mtp_property_naked = 0x00000010, /**< This method is naked.
814 GCC: __attribute__((naked)). */
815 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
816 GCC: __attribute__((malloc)). */
817 mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
818 a lowering phase will remove all calls to it. */
819 mtp_property_inherited = (1<<31) /**< used only in irg's, means property is inherited
821 } mtp_additional_property;
823 /** Returns the mask of the additional graph properties. */
824 unsigned get_method_additional_properties(const ir_type *method);
826 /** Sets the mask of the additional graph properties. */
827 void set_method_additional_properties(ir_type *method, unsigned property_mask);
829 /** Sets one additional graph property. */
830 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
833 * calling conventions: lower 24 bits are the number of register parameters,
834 * upper 8 encode the calling conventions
837 cc_reg_param = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
838 This flag may be set as default on some architectures. */
839 cc_last_on_top = 0x02000000, /**< The last non-register parameter is transmitted on top of
840 the stack. This is equivalent to the stdcall or pascal
841 calling convention. If this flag is not set, the first
842 non-register parameter is used (cdecl calling convention) */
843 cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
844 function calls (stdcall). */
845 cc_this_call = 0x08000000, /**< The first parameter is a this pointer and is transmitted
849 cc_bits = (0xFF << 24) /**< the calling convention bits */
850 } calling_convention;
852 /* some often used cases: made as defines for firmjni */
853 /** cdecl calling convention */
854 #define cc_cdecl_set (0)
855 /** stdcall calling convention */
856 #define cc_stdcall_set cc_callee_clear_stk
857 /** fastcall calling convention */
858 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
860 /** return the default calling convention for method types */
861 unsigned get_default_cc_mask(void);
864 * check for the CDECL calling convention
866 #define IS_CDECL(cc_mask) (((cc_mask) & cc_bits) == cc_cdecl_set)
869 * check for the STDCALL calling convention
871 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_bits) == cc_stdcall_set)
874 * check for the FASTCALL calling convention
876 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_bits) == cc_fastcall_set)
879 * set the CDECL convention bits
881 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_bits) | cc_cdecl_set)
884 * set the STDCALL convention bits
886 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_stdcall_set)
889 * set the FASTCALL convention bits
891 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
893 /** Returns the calling convention of an entities graph. */
894 unsigned get_method_calling_convention(const ir_type *method);
896 /** Sets the calling convention of an entities graph. */
897 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
899 /** Returns the number of registers parameters, 0 means default. */
900 unsigned get_method_n_regparams(ir_type *method);
902 /** Sets the number of registers parameters, 0 means default. */
903 void set_method_n_regparams(ir_type *method, unsigned n_regs);
905 /** Returns true if a type is a method type. */
906 int is_Method_type (const ir_type *method);
909 * @page union_type Representation of a union (variant) type.
911 * The union type represents union types.
912 * - n_types: Number of unioned types.
913 * - members: Entities for unioned types. Fixed length array.
914 * This is a dynamic list that can be grown with an "add_" function,
917 /** Creates a new type union. */
918 ir_type *new_type_union (ident *name);
920 /** Creates a new type union with debug information. */
921 ir_type *new_d_type_union (ident *name, dbg_info* db);
923 /* --- manipulate private fields of struct --- */
925 /** Returns the number of unioned types of this union */
926 int get_union_n_members (const ir_type *uni);
928 /** Adds a new entity to a union type */
929 void add_union_member (ir_type *uni, entity *member);
931 /** Returns the entity at position pos of a union */
932 entity *get_union_member (const ir_type *uni, int pos);
934 /** Returns index of member in uni, -1 if not contained. */
935 int get_union_member_index(const ir_type *uni, entity *member);
937 /** Overwrites a entity at position pos in a union type. */
938 void set_union_member (ir_type *uni, int pos, entity *member);
940 /** Finds member in the list of members and removes it. */
941 void remove_union_member (ir_type *uni, entity *member);
943 /** Returns true if a type is a union type. */
944 int is_Union_type (const ir_type *uni);
947 * @page array_type Representation of an array type
949 * The array type represents rectangular multi dimensional arrays.
950 * The constants representing the bounds must be allocated to
951 * get_const_code_irg() by setting current_ir_graph accordingly.
953 * - n_dimensions: Number of array dimensions.
954 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
955 * - *upper_bound: Upper bounds or dimensions.
956 * - *element_type: The type of the array elements.
957 * - *element_ent: An entity for the array elements to be used for
958 * element selection with Sel.
960 * Do we need several entities? One might want
961 * to select a dimension and not a single element in case of multi
962 * dimensional arrays.
965 /** Create a new type array.
967 * Sets n_dimension to dimension and all dimension entries to NULL.
968 * Initializes order to the order of the dimensions.
969 * The entity for array elements is built automatically.
970 * Set dimension sizes after call to constructor with set_* routines.
972 ir_type *new_type_array (ident *name, int n_dimensions,
973 ir_type *element_type);
975 /** Create a new type array with debug information.
977 * Sets n_dimension to dimension and all dimension entries to NULL.
978 * Initializes order to the order of the dimensions.
979 * The entity for array elements is built automatically.
980 * Set dimension sizes after call to constructor with set_* routines.
981 * A legal array type must have at least one dimension set.
983 ir_type *new_d_type_array (ident *name, int n_dimensions,
984 ir_type *element_type, dbg_info* db);
986 /* --- manipulate private fields of array type --- */
988 /** Returns the number of array dimensions of this type. */
989 int get_array_n_dimensions (const ir_type *array);
992 * Allocates Const nodes of mode_I for one array dimension.
993 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
995 void set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
998 * Sets the bounds for one array dimension.
999 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
1001 void set_array_bounds (ir_type *array, int dimension, ir_node *lower_bound,
1002 ir_node *upper_bound);
1003 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
1004 void set_array_lower_bound (ir_type *array, int dimension, ir_node *lower_bound);
1006 /** Allocates Const nodes of mode_I for the lower bound of an array
1007 dimension, i.e. [lower,upper[ */
1008 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
1010 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
1011 void set_array_upper_bound (ir_type *array, int dimension, ir_node *upper_bound);
1013 /** Allocates Const nodes of mode_I for the upper bound of an array
1014 dimension, i.e. [lower,upper[. */
1015 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
1017 /** Returns true if lower bound != Unknown. */
1018 int has_array_lower_bound (const ir_type *array, int dimension);
1019 /** Returns the lower bound of an array. */
1020 ir_node * get_array_lower_bound (const ir_type *array, int dimension);
1021 /** Works only if bound is Const node with tarval that can be converted to long. */
1022 long get_array_lower_bound_int (const ir_type *array, int dimension);
1023 /** returns true if lower bound != Unknown */
1024 int has_array_upper_bound (const ir_type *array, int dimension);
1025 /** Returns the upper bound of an array. */
1026 ir_node * get_array_upper_bound (const ir_type *array, int dimension);
1027 /** Works only if bound is Const node with tarval that can be converted to long. */
1028 long get_array_upper_bound_int (const ir_type *array, int dimension);
1030 /** Sets an array dimension to a specific order. */
1031 void set_array_order (ir_type *array, int dimension, int order);
1033 /** Returns the order of an array dimension. */
1034 int get_array_order (const ir_type *array, int dimension);
1036 /** Find the array dimension that is placed at order ord. */
1037 int find_array_dimension(const ir_type *array, int order);
1039 /** Sets the array element type. */
1040 void set_array_element_type (ir_type *array, ir_type* tp);
1042 /** Gets the array element type. */
1043 ir_type *get_array_element_type (ir_type *array);
1045 /** Sets the array element entity. */
1046 void set_array_element_entity (ir_type *array, entity *ent);
1048 /** Get the array element entity. */
1049 entity *get_array_element_entity (const ir_type *array);
1051 /** Returns true if a type is an array type. */
1052 int is_Array_type(const ir_type *array);
1055 * @page enumeration_type Representation of an enumeration type
1057 * Enumeration types need not necessarily be represented explicitly
1058 * by Firm types, as the frontend can lower them to integer constants as
1059 * well. For debugging purposes or similar tasks this information is useful.
1061 * - *enum: The target values representing the constants used to
1062 * represent individual enumerations.
1063 * - *enum_nameid: Idents containing the source program name of the enumeration
1066 /** Create a new type enumeration -- set the enumerators independently. */
1067 ir_type *new_type_enumeration (ident *name, int n_enums);
1069 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1070 ir_type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
1072 /* --- manipulate fields of enumeration type. --- */
1074 /** Returns the number of enumeration values of this enumeration */
1075 int get_enumeration_n_enums (const ir_type *enumeration);
1077 /** Sets the enumeration value at a given position. */
1078 void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con);
1080 /** Returns the enumeration value at a given position. */
1081 tarval *get_enumeration_enum (const ir_type *enumeration, int pos);
1083 /** Assign an ident to an enumeration value at a given position. */
1084 void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id);
1086 /** Returns the assigned ident of an enumeration value at a given position. */
1087 ident *get_enumeration_nameid (const ir_type *enumeration, int pos);
1089 /** Returns the assigned name of an enumeration value at a given position. */
1090 const char *get_enumeration_name(const ir_type *enumeration, int pos);
1092 /** Returns true if a type is a enumeration type. */
1093 int is_Enumeration_type (const ir_type *enumeration);
1096 * @page pointer_type Representation of a pointer type
1098 * The mode of the pointer type must be a reference mode.
1101 * - points_to: The type of the entity this pointer points to.
1104 /** Creates a new type pointer. */
1105 ir_type *new_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode);
1107 /** Creates a new type pointer with debug information. */
1108 ir_type *new_d_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
1110 /* --- manipulate fields of type_pointer --- */
1112 /** Sets the type to which a pointer points to. */
1113 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
1115 /** Returns the type to which a pointer points to. */
1116 ir_type *get_pointer_points_to_type (ir_type *pointer);
1118 /** Returns true if a type is a pointer type. */
1119 int is_Pointer_type (const ir_type *pointer);
1121 /** Returns the first pointer type that has as points_to tp.
1122 * Not efficient: O(#types).
1123 * If not found returns firm_unknown_type. */
1124 ir_type *find_pointer_type_to_type (ir_type *tp);
1127 * @page primitive_type Representation of a primitive type
1129 * Primitive types are types that represent indivisible data values that
1130 * map directly to modes. They don't have a private attribute. The
1131 * important information they carry is held in the common mode field.
1133 /** Creates a new primitive type. */
1134 ir_type *new_type_primitive (ident *name, ir_mode *mode);
1136 /** Creates a new primitive type with debug information. */
1137 ir_type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1139 /** Returns true if a type is a primitive type. */
1140 int is_Primitive_type (const ir_type *primitive);
1144 * @page none_type The None type
1146 * This type is an auxiliary type dedicated to support type analyses.
1148 * The none type represents that there is no type. The type can be used to
1149 * initialize fields of type* that actually can not contain a type or that
1150 * are initialized for an analysis. There exists exactly one type none.
1151 * This type is not on the type list in ir_prog. It is
1152 * allocated when initializing the type module.
1154 * The following values are set:
1156 * - name: "type_none"
1157 * - state: layout_fixed
1160 /** A variable that contains the only none type. */
1161 extern ir_type *firm_none_type;
1162 /** Returns the none type */
1163 ir_type *get_none_type(void);
1166 * @page unknown_type
1168 * This type is an auxiliary type dedicated to support type analyses.
1170 * The unknown type represents that there could be a type, but it is not
1171 * known. This type can be used to initialize fields before an analysis (not known
1172 * yet) or to represent the top of a lattice (could not be determined). There exists
1173 * exactly one type unknown. This type is not on the type list in ir_prog. It is
1174 * allocated when initializing the type module.
1176 * The following values are set:
1178 * - name: "type_unknown"
1179 * - state: layout_fixed
1182 /** A variable that contains the only unknown type. */
1183 extern ir_type *firm_unknown_type;
1184 /** Returns the unknown type */
1185 ir_type *get_unknown_type(void);
1189 * Checks whether a type is atomic.
1190 * @param tp any type
1191 * @return true if type is primitive, pointer or enumeration
1193 int is_atomic_type(const ir_type *tp);
1195 /* --- Support for compound types --- */
1198 * Gets the number of elements in a firm compound type.
1200 * This is just a comfortability function, because structs and
1201 * classes can often be treated be the same code, but they have
1202 * different access functions to their members.
1204 * @param tp The type (must be struct, union or class).
1206 * @return Number of members in the compound type.
1208 int get_compound_n_members(const ir_type *tp);
1211 * Gets the member of a firm compound type at position pos.
1213 * @param tp The type (must be struct, union or class).
1214 * @param pos The number of the member.
1216 * @return The member entity at position pos.
1218 * @see get_compound_n_members() for justification of existence.
1220 entity *get_compound_member(const ir_type *tp, int pos);
1222 /** Returns index of member in tp, -1 if not contained. */
1223 int get_compound_member_index(const ir_type *tp, entity *member);
1226 * Checks whether a type is compound.
1228 * @param tp - any type
1230 * @return true if the type is class, structure, union or array type.
1232 int is_compound_type(const ir_type *tp);
1235 * Checks, whether a type is a frame type.
1237 int is_frame_type(const ir_type *tp);
1240 * Checks, whether a type is a lowered type.
1242 int is_lowered_type(const ir_type *tp);
1245 * Makes a new frame type. Frame types are class types,
1246 * so all class access functions work.
1247 * Frame types are not in the global list of types.
1249 ir_type *new_type_frame(ident *name);
1252 * Sets a lowered type for a type. This sets both associations
1253 * and marks lowered type as a "lowered" one.
1255 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
1258 * Gets the lowered/unlowered type of a type or NULL if this type
1259 * has no lowered/unlowered one.
1261 ir_type *get_associated_type(const ir_type *tp);
1264 * Allocate an area of size bytes aligned at alignment
1265 * at the start or the end of a frame type.
1266 * The frame type must have already an fixed layout.
1268 * @param frame_type a frame type
1269 * @param size the size of the entity
1270 * @param alignment the alignment of the entity
1271 * @param at_start if true, put the area at the frame type's start, else at end
1273 * @return the entity representing the area
1275 entity *frame_alloc_area(type *frame_type, int size, int alignment, int at_start);
1277 /*-----------------------------------------------------------------*/
1279 /*-----------------------------------------------------------------*/
1282 * Outputs a unique number for this type if libfirm is compiled for
1283 * debugging, (configure with --enable-debug) else returns the address
1284 * of the type cast to long.
1286 long get_type_nr(const ir_type *tp);
1288 #endif /* _FIRM_TR_TYPE_H_ */