2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * File name: ir/tr/type.h
23 * Purpose: Representation of types.
24 * Author: Goetz Lindenmaier
25 * Modified by: Michael Beck
27 * Copyright: (c) 2001-2006 Universität Karlsruhe
34 * Datastructure to hold type information.
36 * This module supplies a datastructure to represent all types
37 * known in the compiled program. This includes types specified
38 * in the program as well as types defined by the language. In the
39 * view of the intermediate representation there is no difference
40 * between these types. Finally it specifies some auxiliary types.
42 * There exist several kinds of types, arranged by the structure of
43 * the type. A type is described by a set of attributes. Some of
44 * these attributes are common to all types, others depend on the
47 * Types are different from the modes defined in irmode: Types are
48 * on the level of the programming language, modes at the level of
49 * the target processor.
53 #ifndef _FIRM_TR_TYPE_H_
54 #define _FIRM_TR_TYPE_H_
56 #include "firm_types.h"
58 #include "firm_common.h"
62 * An abstract data type to represent types.
64 * This is the abstract data type with which any type known in the
65 * compiled program can be represented. This includes types specified
66 * in the program as well as types defined by the language. In the
67 * view of the intermediate representation there is no difference
68 * between these types.
70 * There exist several kinds of types, arranged by the structure of
71 * the type. These are distinguished by a type opcode.
72 * A type is described by a set of attributes. Some of these attributes
73 * are common to all types, others depend on the kind of the type.
75 * The following describes the common attributes. They can only be
76 * accessed by the functions given below.
78 * The common fields are:
80 * - firm_kind: A firm_kind tag containing k_type. This is useful
81 * for dynamically checking whether a node is a type node.
82 * - type_op: A tp_op specifying the kind of the type.
83 * - name: An identifier specifying the name of the type. To be
84 * set by the frontend.
85 * - visibility: The visibility of this type.
86 * - size: The size of the type, i.e. an entity of this type will
87 * occupy size bits in memory. In several cases this is
88 * determined when fixing the layout of this type (class,
89 * struct, union, array, enumeration).
90 * - alignment The alignment of the type, i.e. an entity of this type will
91 * be allocated an an address in memory with this alignment.
92 * In several cases this is determined when fixing the layout
93 * of this type (class, struct, union, array)
94 * - mode: The mode to be used to represent the type on a machine.
95 * - state: The state of the type. The state represents whether the
96 * layout of the type is undefined or fixed (values: layout_undefined
97 * or layout_fixed). Compound types can have an undefined
98 * layout. The layout of the basic types primitive and pointer
99 * is always layout_fixed. If the layout of
100 * compound types is fixed all entities must have an offset
101 * and the size of the type must be set.
102 * A fixed layout for enumeration types means that each enumeration
103 * is associated with an implementation value.
104 * - assoc_type: The associated lowered/upper type.
105 * - visit: A counter for walks of the type information.
106 * - link: A void* to associate some additional information with the type.
108 * These fields can only be accessed via access functions.
110 * Depending on the value of @c type_op, i.e., depending on the kind of the
111 * type the adt contains further attributes. These are documented below.
115 * @link class_type class @endlink, @link struct_type struct @endlink,
116 * @link method_type method @endlink, @link union_type union @endlink,
117 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
118 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
121 * mode maybe not global field??
123 #ifndef _IR_TYPE_TYPEDEF_
124 #define _IR_TYPE_TYPEDEF_
125 typedef struct ir_type ir_type;
128 # include "type_or_entity.h"
130 /** Frees all entities associated with a type.
131 * Does not free the array entity.
132 * Warning: ensure these entities are not referenced anywhere else.
134 void free_type_entities(ir_type *tp);
136 /** Frees the memory used by the type.
138 * Removes the type from the type list. Does not free the entities
139 * belonging to the type, except for the array element entity. Does
140 * not free if tp is "none" or "unknown". Frees entities in value
141 * param subtypes of method types!!! Make sure these are not
142 * referenced any more. Further make sure there is no pointer type
143 * that refers to this type. */
144 void free_type(ir_type *tp);
146 const tp_op*get_type_tpop(const ir_type *tp);
147 ident* get_type_tpop_nameid(const ir_type *tp);
148 const char* get_type_tpop_name(const ir_type *tp);
149 tp_opcode get_type_tpop_code(const ir_type *tp);
151 ident* get_type_ident(const ir_type *tp);
152 void set_type_ident(ir_type *tp, ident* id);
153 const char* get_type_name(const ir_type *tp);
155 /** This enumeration flags the visibility of entities and types.
157 * This is necessary for partial compilation.
158 * We rely on the ordering of the flags.
161 visibility_local, /**< The entity is only visible locally. This is the default for
163 The type is only visible locally. All instances are allocated
164 locally, and no pointer to entities of this type are passed
165 out of this compilation unit. */
166 visibility_external_visible, /**< The entity is visible to other external program parts, but
167 it is defined here. It may not be optimized away. The entity must
169 For types: entities of this type can be accessed externally. No
170 instances of this type are allocated externally. */
171 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
172 must not allocate memory for this entity. The entity must
173 be static_allocated. This can also be an external defined
175 For types: entities of this type are allocated and accessed from
176 external code. Default for types. */
179 /** The visibility of a type.
181 * The visibility of a type indicates, whether entities of this type
182 * are accessed or allocated in external code.
184 * An entity of a type is allocated in external code, if the external
185 * code declares a variable of this type, or dynamically allocates
186 * an entity of this type. If the external code declares a (compound)
187 * type, that contains entities of this type, the visibility also
188 * must be external_allocated.
190 * The visibility must be higher than that of all entities, if the
191 * type is a compound. Here it is questionable, what happens with
192 * static entities. If these are accessed external by direct reference,
193 * (a static call to a method, that is also in the dispatch table)
194 * it should not affect the visibility of the type.
197 * @@@ Do we need a visibility for types?
198 * I change the layout of types radically when doing type splitting.
199 * I need to know, which fields of classes are accessed in the RTS,
200 * e.g., [_length. I may not move [_length to the split part.
201 * The layout though, is a property of the type.
203 * One could also think of changing the mode of a type ...
205 * But, we could also output macros to access the fields, e.g.,
206 * ACCESS_[_length (X) X->length // conventional
207 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
209 * For now I implement this function, that returns the visibility
210 * based on the visibility of the entities of a compound ...
212 * This function returns visibility_external_visible if one or more
213 * entities of a compound type have visibility_external_visible.
214 * Entities of types are never visibility_external_allocated (right?).
215 * Else returns visibility_local.
217 ir_visibility get_type_visibility(const ir_type *tp);
218 void set_type_visibility(ir_type *tp, ir_visibility v);
222 /** The state of the type layout. */
224 layout_undefined, /**< The layout of this type is not defined.
225 Address computation to access fields is not
226 possible, fields must be accessed by Sel
227 nodes. Enumeration constants might be undefined.
228 This is the default value except for
229 pointer, primitive and method types. */
230 layout_fixed /**< The layout is fixed, all component/member entities
231 have an offset assigned. Size of the type is known.
232 Arrays can be accessed by explicit address
233 computation. Enumeration constants must be defined.
234 Default for pointer, primitive and method types. */
237 /** Returns a human readable string for the enum entry. */
238 const char *get_type_state_name(type_state s);
240 /** Returns the type layout state of a type. */
241 type_state get_type_state(const ir_type *tp);
243 /** Sets the type layout state of a type.
245 * For primitives, pointer and method types the layout is always fixed.
246 * This call is legal but has no effect.
248 void set_type_state(ir_type *tp, type_state state);
250 /** Returns the mode of a type.
252 * Returns NULL for all non atomic types.
254 ir_mode* get_type_mode(const ir_type *tp);
256 /** Sets the mode of a type.
258 * Only has an effect on primitive, enumeration and pointer types.
260 void set_type_mode(ir_type *tp, ir_mode* m);
262 /** Returns the size of a type in bytes, returns -1 if the size is NOT
263 * a byte size, i.e. not dividable by 8. */
264 int get_type_size_bytes(const ir_type *tp);
266 /** Returns the size of a type in bits. */
267 int get_type_size_bits(const ir_type *tp);
269 /** Sets the size of a type in bytes.
271 * For primitive, enumeration, pointer and method types the size
272 * is always fixed. This call is legal but has no effect.
274 void set_type_size_bytes(ir_type *tp, int size);
276 /** Sets the size of a type in bits.
278 * For primitive, enumeration, pointer and method types the size
279 * is always fixed. This call is legal but has no effect.
281 void set_type_size_bits(ir_type *tp, int size);
283 /** Returns the alignment of a type in bytes.
285 * Returns -1 if the alignment is NOT
286 * a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
287 int get_type_alignment_bytes(ir_type *tp);
289 /** Returns the alignment of a type in bits.
291 * If the alignment of a type is
292 * not set, it is calculated here according to the following rules:
293 * -#.) if a type has a mode, the alignment is the mode size.
294 * -#.) compound types have the alignment of there biggest member.
295 * -#.) array types have the alignment of there element type.
296 * -#.) method types return 0 here.
297 * -#.) all other types return 8 here (i.e. aligned at byte).
299 int get_type_alignment_bits(ir_type *tp);
301 /** Sets the alignment of a type in bytes. */
302 void set_type_alignment_bytes(ir_type *tp, int size);
304 /** Sets the alignment of a type in bits.
306 * For method types the alignment is always fixed.
307 * This call is legal but has no effect.
309 void set_type_alignment_bits(ir_type *tp, int size);
311 /** Returns the visited count of a type. */
312 unsigned long get_type_visited(const ir_type *tp);
313 /** Sets the visited count of a type to num. */
314 void set_type_visited(ir_type *tp, unsigned long num);
315 /** Sets visited field in type to type_visited. */
316 void mark_type_visited(ir_type *tp);
317 /** Returns non-zero if the type is already visited */
318 int type_visited(const ir_type *tp);
319 /** Returns non-zero if the type is not yet visited */
320 int type_not_visited(const ir_type *tp);
322 /** Returns the associated link field of a type. */
323 void* get_type_link(const ir_type *tp);
324 /** Sets the associated link field of a type. */
325 void set_type_link(ir_type *tp, void *l);
328 * Visited flag to traverse the type information.
330 * Increase this flag by one before traversing the type information
331 * using inc_master_type_visited().
332 * Mark type nodes as visited by mark_type_visited(ir_type).
333 * Check whether node was already visited by type_visited(ir_type)
334 * and type_not_visited(ir_type).
335 * Or use the function to walk all types.
339 void set_master_type_visited(unsigned long val);
340 unsigned long get_master_type_visited(void);
341 void inc_master_type_visited(void);
344 * Checks whether a pointer points to a type.
346 * @param thing an arbitrary pointer
349 * true if the thing is a type, else false
351 int is_type(const void *thing);
354 * Checks whether two types are structurally equal.
356 * @param typ1 the first type
357 * @param typ2 the second type
360 * true if the types are equal, else false.
362 * Types are equal if :
363 * - they are the same type kind
364 * - they have the same name
365 * - they have the same mode (if applicable)
366 * - they have the same type_state and, ev., the same size
367 * - they are class types and have:
368 * - the same members (see same_entity in entity.h)
369 * - the same supertypes -- the C-pointers are compared --> no recursive call.
370 * - the same number of subtypes. Subtypes are not compared,
371 * as this could cause a cyclic test.
372 * - the same peculiarity
373 * - they are structure types and have the same members
374 * - they are method types and have
375 * - the same parameter types
376 * - the same result types
377 * - they are union types and have the same members
378 * - they are array types and have
379 * - the same number of dimensions
380 * - the same dimension bounds
381 * - the same dimension order
382 * - the same element type
383 * - they are enumeration types and have the same enumerator names
384 * - they are pointer types and have the identical points_to type
385 * (i.e., the same C-struct to represent the type, type_id is skipped.
386 * This is to avoid endless recursions; with pointer types cyclic
387 * type graphs are possible.)
389 int equal_type(ir_type *typ1, ir_type *typ2);
392 * Checks whether two types are structural comparable.
394 * @param st pointer type
395 * @param lt pointer type
398 * true if type st is smaller than type lt, i.e. whenever
399 * lt is expected a st can be used.
401 * - they are the same type kind
402 * - mode(st) < mode (lt) (if applicable)
403 * - they are class types and st is (transitive) subtype of lt,
404 * - they are structure types and
405 * - the members of st have exactly one counterpart in lt with the same name,
406 * - the counterpart has a bigger type.
407 * - they are method types and have
408 * - the same number of parameter and result types,
409 * - the parameter types of st are smaller than those of lt,
410 * - the result types of st are smaller than those of lt
411 * - they are union types and have the members of st have exactly one
412 * @return counterpart in lt and the type is smaller
413 * - they are array types and have
414 * - the same number of dimensions
415 * - all bounds of lt are bound of st
416 * - the same dimension order
417 * - the same element type
419 * - the element type of st is smaller than that of lt
420 * - the element types have the same size and fixed layout.
421 * - they are enumeration types and have the same enumerator names
422 * - they are pointer types and have the points_to type of st is
423 * @return smaller than the points_to type of lt.
426 int smaller_type(ir_type *st, ir_type *lt);
429 * @page class_type Representation of a class type
431 * If the type opcode is set to type_class the type represents class
432 * types. A list of fields and methods is associated with a class.
433 * Further a class can inherit from and bequest to other classes.
435 * The following attributes are private to this type kind:
436 * - member: All entities belonging to this class. This are method entities
437 * which have type_method or fields that can have any of the
438 * following type kinds: type_class, type_struct, type_union,
439 * type_array, type_enumeration, type_pointer, type_primitive.
441 * The following two are dynamic lists that can be grown with an "add_" function,
444 * - subtypes: A list of direct subclasses.
446 * - supertypes: A list of direct superclasses.
448 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
449 * "description" it only is a description of requirements to a class,
450 * as, e.g., a Java interface. The class will never be allocated.
451 * Peculiarity inherited is only possible for entities. An entity
452 * is of peculiarity inherited if the compiler generated the entity
453 * to explicitly resolve inheritance. An inherited method entity has
455 * Values: description, existent, inherited. Default: existent.
457 * - type_info: An entity representing the type information of this class.
458 * This entity can be of arbitrari type, Firm did not use it yet.
459 * It allows to express the coupling of a type with an entity
460 * representing this type. This information is useful for lowering
461 * of InstOf and TypeChk nodes. Default: NULL
463 * - vtable_size: The size of this class virtual function table.
466 * - final: A final class is always a leaf in the class hierarchy. Final
467 * classes cannot be super classes of other ones. As this information
468 * can only be computed in whole world compilations, we allow to
469 * set this flag. It is used in optimizations if get_opt_closed_world()
470 * is false. Default: false
472 * - interface: The class represents an interface. This flag can be set to distinguish
473 * between interfaces, abstract classes and other classes that all may
474 * have the peculiarity peculiarity_description. Depending on this flag
475 * the lowering might do different actions. Default: false
477 * - abstract : The class represents an abstract class. This flag can be set to distinguish
478 * between interfaces, abstract classes and other classes that all may
479 * have the peculiarity peculiarity_description. Depending on this flag
480 * the lowering might do different actions. Default: false
483 /** Creates a new class type. */
484 ir_type *new_type_class (ident *name);
486 /** Creates a new class type with debug information. */
487 ir_type *new_d_type_class (ident *name, dbg_info *db);
489 /* --- manipulate private fields of class type --- */
491 /** Adds the entity as member of the class. */
492 void add_class_member (ir_type *clss, ir_entity *member);
494 /** Returns the number of members of this class. */
495 int get_class_n_members (const ir_type *clss);
497 /** Returns the member at position pos, 0 <= pos < n_member */
498 ir_entity *get_class_member (const ir_type *clss, int pos);
500 /** Returns index of mem in clss, -1 if not contained. */
501 int get_class_member_index(const ir_type *clss, ir_entity *mem);
503 /** Finds the member with name 'name'. If several members with the same
504 * name returns one of them. Returns NULL if no member found. */
505 ir_entity *get_class_member_by_name(ir_type *clss, ident *name);
507 /** Overwrites the member at position pos, 0 <= pos < n_member with
508 * the passed entity. */
509 void set_class_member (ir_type *clss, ir_entity *member, int pos);
511 /** Replaces complete member list in class type by the list passed.
513 * Copies the list passed. This function is necessary to reduce the number of members.
514 * members is an array of entities, num the size of this array. Sets all
515 * owners of the members passed to clss. */
516 void set_class_members (ir_type *clss, ir_entity *members[], int arity);
518 /** Finds member in the list of members and removes it.
520 * Shrinks the member list, so iterate from the end!!!
521 * Does not deallocate the entity. */
522 void remove_class_member(ir_type *clss, ir_entity *member);
525 /** Adds subtype as subtype to clss.
527 * Checks whether clss is a supertype of subtype. If not
528 * adds also clss as supertype to subtype. */
529 void add_class_subtype (ir_type *clss, ir_type *subtype);
531 /** Returns the number of subtypes */
532 int get_class_n_subtypes (const ir_type *clss);
534 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
535 ir_type *get_class_subtype (ir_type *clss, int pos);
537 /** Returns the index to access subclass as subtype of class.
539 * If subclass is no direct subtype of class returns -1.
541 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
543 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
545 * Does not set the corresponding supertype relation for subtype: this might
546 * be a different position! */
547 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos);
549 /** Finds subtype in the list of subtypes and removes it */
550 void remove_class_subtype(ir_type *clss, ir_type *subtype);
552 /* Convenience macros */
553 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
554 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
555 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
556 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
557 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
558 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
560 /** Adds supertype as supertype to class.
562 * Checks whether clss is a subtype of supertype. If not
563 * adds also clss as subtype to supertype. */
564 void add_class_supertype (ir_type *clss, ir_type *supertype);
566 /** Returns the number of supertypes */
567 int get_class_n_supertypes (const ir_type *clss);
569 /** Returns the index to access superclass as supertype of class.
571 * If superclass is no direct supertype of class returns -1.
573 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
575 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
576 ir_type *get_class_supertype (ir_type *clss, int pos);
578 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
580 * Does not set the corresponding subtype relation for supertype: this might
581 * be at a different position! */
582 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos);
584 /** Finds supertype in the list of supertypes and removes it */
585 void remove_class_supertype(ir_type *clss, ir_type *supertype);
587 /** Convenience macro */
588 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
589 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
590 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
591 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
592 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
593 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
595 /** Convenience macro */
596 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
597 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
598 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
599 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
600 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
601 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
603 /** This enumeration flags the peculiarity of entities and types. */
605 peculiarity_description, /**< Represents only a description. The entity/type is never
606 allocated, no code/data exists for this entity/type.
607 @@@ eventually rename to descriptive (adjective as the others!)*/
608 peculiarity_inherited, /**< Describes explicitly that other entities are
609 inherited to the owner of this entity.
610 Overwrites must refer to at least one other
611 entity. If this is a method entity there exists
612 no irg for this entity, only for one of the
615 peculiarity_existent /**< The entity/type (can) exist.
616 @@@ eventually rename to 'real' i.e., 'echt'
617 This serves better as opposition to description _and_ inherited.*/
620 /** Returns a human readable string for a peculiarity. */
621 const char *get_peculiarity_name(ir_peculiarity p);
623 /** Returns the peculiarity of the class. */
624 ir_peculiarity get_class_peculiarity (const ir_type *clss);
625 /** Sets the peculiarity of the class. */
626 void set_class_peculiarity (ir_type *clss, ir_peculiarity pec);
628 /** Returns the type info entity of a class. */
629 ir_entity *get_class_type_info(const ir_type *clss);
631 /** Set a type info entity for the class. */
632 void set_class_type_info(ir_type *clss, ir_entity *ent);
634 /** Returns the size of the virtual function table. */
635 unsigned get_class_vtable_size(const ir_type *clss);
637 /** Sets a new size of the virtual function table. */
638 void set_class_vtable_size(ir_type *clss, unsigned size);
640 /** Returns non-zero if a class is final. */
641 int is_class_final(const ir_type *clss);
643 /** Sets the class final flag. */
644 void set_class_final(ir_type *clss, int flag);
646 /** Return non-zero if a class is an interface */
647 int is_class_interface(const ir_type *clss);
649 /** Sets the class interface flag. */
650 void set_class_interface(ir_type *clss, int flag);
652 /** Return non-zero if a class is an abstract class. */
653 int is_class_abstract(const ir_type *clss);
655 /** Sets the class abstract flag. */
656 void set_class_abstract(ir_type *clss, int flag);
658 /** Set and get a class' dfn --
659 @todo This is an undocumented field, subject to change! */
660 void set_class_dfn (ir_type *clss, int dfn);
661 int get_class_dfn (const ir_type *clss);
663 /** Returns true if a type is a class type. */
664 int is_Class_type(const ir_type *clss);
667 * @page struct_type Representation of a struct type
669 * A struct type represents aggregate types that consist of a list
672 * The following attributes are private to this type kind:
673 * - member: All entities belonging to this class. This are the fields
674 * that can have any of the following types: type_class,
675 * type_struct, type_union, type_array, type_enumeration,
676 * type_pointer, type_primitive.
677 * This is a dynamic list that can be grown with an "add_" function,
679 * This is a dynamic list that can be grown with an "add_" function,
682 /** Creates a new type struct */
683 ir_type *new_type_struct (ident *name);
684 /** Creates a new type struct with debug information. */
685 ir_type *new_d_type_struct (ident *name, dbg_info* db);
687 /* --- manipulate private fields of struct --- */
689 /** Adds the entity as member of the struct. */
690 void add_struct_member (ir_type *strct, ir_entity *member);
692 /** Returns the number of members of this struct. */
693 int get_struct_n_members (const ir_type *strct);
695 /** Returns the member at position pos, 0 <= pos < n_member */
696 ir_entity *get_struct_member (const ir_type *strct, int pos);
698 /** Returns index of member in strct, -1 if not contained. */
699 int get_struct_member_index(const ir_type *strct, ir_entity *member);
701 /** Overwrites the member at position pos, 0 <= pos < n_member with
702 the passed entity. */
703 void set_struct_member (ir_type *strct, int pos, ir_entity *member);
705 /** Finds member in the list of members and removes it. */
706 void remove_struct_member (ir_type *strct, ir_entity *member);
708 /** Returns true if a type is a struct type. */
709 int is_Struct_type(const ir_type *strct);
712 * @page method_type Representation of a method type
714 * A method type represents a method, function or procedure type.
715 * It contains a list of the parameter and result types, as these
716 * are part of the type description. These lists should not
717 * be changed by a optimization, as a change creates a new method
718 * type. Therefore optimizations should allocated new method types.
719 * The set_ routines are only for construction by a frontend.
721 * - n_params: Number of parameters to the procedure.
722 * A procedure in FIRM has only call by value parameters.
724 * - param_type: A list with the types of parameters. This list is ordered.
725 * The nth type in this list corresponds to the nth element
726 * in the parameter tuple that is a result of the start node.
727 * (See ircons.h for more information.)
730 * A list of entities (whose owner is a struct private to the
731 * method type) that represent parameters passed by value.
733 * - n_res: The number of results of the method. In general, procedures
734 * have zero results, functions one.
736 * - res_type: A list with the types of parameters. This list is ordered.
737 * The nth type in this list corresponds to the nth input to
738 * Return nodes. (See ircons.h for more information.)
741 * A list of entities (whose owner is a struct private to the
742 * method type) that represent results passed by value.
745 /* These macros define the suffixes for the types and entities used
746 to represent value parameters / results. */
747 #define VALUE_PARAMS_SUFFIX "val_param"
748 #define VALUE_RESS_SUFFIX "val_res"
750 /** Create a new method type.
752 * @param name the name (ident) of this type
753 * @param n_param the number of parameters
754 * @param n_res the number of results
756 * The arrays for the parameter and result types are not initialized by
759 ir_type *new_type_method (ident *name, int n_param, int n_res);
761 /** Create a new method type with debug information.
763 * @param name the name (ident) of this type
764 * @param n_param the number of parameters
765 * @param n_res the number of results
766 * @param db user defined debug information
768 * The arrays for the parameter and result types are not initialized by
771 ir_type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
773 /* -- manipulate private fields of method. -- */
775 /** Returns the number of parameters of this method. */
776 int get_method_n_params (const ir_type *method);
778 /** Returns the type of the parameter at position pos of a method. */
779 ir_type *get_method_param_type(ir_type *method, int pos);
780 /** Sets the type of the parameter at position pos of a method.
781 Also changes the type in the pass-by-value representation by just
782 changing the type of the corresponding entity if the representation is constructed. */
783 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
784 /** Returns an entity that represents the copied value argument. Only necessary
785 for compounds passed by value. This information is constructed only on demand. */
786 ir_entity *get_method_value_param_ent(ir_type *method, int pos);
788 * Returns a type that represents the copied value arguments if one
789 * was allocated, else NULL.
791 ir_type *get_method_value_param_type(const ir_type *method);
792 /** Returns an ident representing the parameters name. Returns NULL if not set.
793 For debug support only. */
794 ident *get_method_param_ident(ir_type *method, int pos);
795 /** Returns a string representing the parameters name. Returns NULL if not set.
796 For debug support only. */
797 const char *get_method_param_name(ir_type *method, int pos);
798 /** Sets an ident representing the parameters name. For debug support only. */
799 void set_method_param_ident(ir_type *method, int pos, ident *id);
801 /** Returns the number of results of a method type. */
802 int get_method_n_ress (const ir_type *method);
803 /** Returns the return type of a method type at position pos. */
804 ir_type *get_method_res_type(ir_type *method, int pos);
805 /** Sets the type of the result at position pos of a method.
806 Also changes the type in the pass-by-value representation by just
807 changing the type of the corresponding entity if the representation is constructed. */
808 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
809 /** Returns an entity that represents the copied value result. Only necessary
810 for compounds passed by value. This information is constructed only on demand. */
811 ir_entity *get_method_value_res_ent(ir_type *method, int pos);
814 * Returns a type that represents the copied value results.
816 ir_type *get_method_value_res_type(const ir_type *method);
819 * This enum flags the variadicity of methods (methods with a
820 * variable amount of arguments (e.g. C's printf). Default is
823 typedef enum variadicity {
824 variadicity_non_variadic, /**< non variadic */
825 variadicity_variadic /**< variadic */
828 /** Returns the null-terminated name of this variadicity. */
829 const char *get_variadicity_name(variadicity vari);
831 /** Returns the variadicity of a method. */
832 variadicity get_method_variadicity(const ir_type *method);
834 /** Sets the variadicity of a method. */
835 void set_method_variadicity(ir_type *method, variadicity vari);
838 * Returns the first variadic parameter index of a type.
839 * If this index was NOT set, the index of the last parameter
840 * of the method type plus one is returned for variadic functions.
841 * Non-variadic function types always return -1 here.
843 int get_method_first_variadic_param_index(const ir_type *method);
846 * Sets the first variadic parameter index. This allows to specify
847 * a complete call type (containing the type of all parameters)
848 * but still have the knowledge, which parameter must be passed as
851 void set_method_first_variadic_param_index(ir_type *method, int index);
854 * Additional method type properties:
855 * Tell about special properties of a method type. Some
856 * of these may be discovered by analyses.
859 mtp_no_property = 0x00000000, /**< no additional properties, default */
860 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
861 its return values solely from its parameters.
862 GCC: __attribute__((const)). */
863 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
864 its return values solely from its parameters and
865 the memory they points to (or global vars).
866 GCC: __attribute__((pure)). */
867 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
869 GCC: __attribute__((noreturn)). */
870 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
871 GCC: __attribute__((nothrow)). */
872 mtp_property_naked = 0x00000010, /**< This method is naked.
873 GCC: __attribute__((naked)). */
874 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
875 GCC: __attribute__((malloc)). */
876 mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
877 a lowering phase will remove all calls to it. */
878 mtp_property_runtime = 0x00000080, /**< This method represents a runtime routine. */
879 mtp_property_inherited = (1<<31) /**< Internal. Used only in irg's, means property is
880 inherited from type. */
881 } mtp_additional_property;
883 /** Returns the mask of the additional graph properties. */
884 unsigned get_method_additional_properties(const ir_type *method);
886 /** Sets the mask of the additional graph properties. */
887 void set_method_additional_properties(ir_type *method, unsigned property_mask);
889 /** Sets one additional graph property. */
890 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
893 * Calling conventions: lower 24 bits are the number of register parameters,
894 * upper 8 encode the calling conventions.
897 cc_reg_param = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
898 This flag may be set as default on some architectures. */
899 cc_last_on_top = 0x02000000, /**< The last non-register parameter is transmitted on top of
900 the stack. This is equivalent to the pascal
901 calling convention. If this flag is not set, the first
902 non-register parameter is used (stdcall or cdecl
903 calling convention) */
904 cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
905 function calls (stdcall). */
906 cc_this_call = 0x08000000, /**< The first parameter is a this pointer and is transmitted
909 cc_bits = (0xFF << 24) /**< the calling convention bits */
910 } calling_convention;
912 /* some often used cases: made as defines because firmjni cannot handle two
913 equal enum values. */
915 /** cdecl calling convention */
916 #define cc_cdecl_set (0)
917 /** stdcall calling convention */
918 #define cc_stdcall_set cc_callee_clear_stk
919 /** fastcall calling convention */
920 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
922 /** Returns the default calling convention for method types. */
923 unsigned get_default_cc_mask(void);
926 * check for the CDECL calling convention
928 #define IS_CDECL(cc_mask) (((cc_mask) & cc_bits) == cc_cdecl_set)
931 * check for the STDCALL calling convention
933 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_bits) == cc_stdcall_set)
936 * check for the FASTCALL calling convention
938 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_bits) == cc_fastcall_set)
941 * Sets the CDECL convention bits.
943 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_bits) | cc_cdecl_set)
946 * Set. the STDCALL convention bits.
948 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_stdcall_set)
951 * Sets the FASTCALL convention bits.
953 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
955 /** Returns the calling convention of an entities graph. */
956 unsigned get_method_calling_convention(const ir_type *method);
958 /** Sets the calling convention of an entities graph. */
959 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
961 /** Returns the number of registers parameters, 0 means default. */
962 unsigned get_method_n_regparams(ir_type *method);
964 /** Sets the number of registers parameters, 0 means default. */
965 void set_method_n_regparams(ir_type *method, unsigned n_regs);
967 /** Returns true if a type is a method type. */
968 int is_Method_type (const ir_type *method);
971 * @page union_type Representation of a union (variant) type.
973 * The union type represents union types. Note that this representation
974 * resembles the C union type. For tagged variant types like in Pascal or Modula
975 * a combination of a struct and a union type must be used.
977 * - n_types: Number of unioned types.
978 * - members: Entities for unioned types. Fixed length array.
979 * This is a dynamic list that can be grown with an "add_" function,
982 /** Creates a new type union. */
983 ir_type *new_type_union (ident *name);
985 /** Creates a new type union with debug information. */
986 ir_type *new_d_type_union (ident *name, dbg_info* db);
988 /* --- manipulate private fields of struct --- */
990 /** Returns the number of unioned types of this union */
991 int get_union_n_members (const ir_type *uni);
993 /** Adds a new entity to a union type */
994 void add_union_member (ir_type *uni, ir_entity *member);
996 /** Returns the entity at position pos of a union */
997 ir_entity *get_union_member (const ir_type *uni, int pos);
999 /** Returns index of member in uni, -1 if not contained. */
1000 int get_union_member_index(const ir_type *uni, ir_entity *member);
1002 /** Overwrites a entity at position pos in a union type. */
1003 void set_union_member (ir_type *uni, int pos, ir_entity *member);
1005 /** Finds member in the list of members and removes it. */
1006 void remove_union_member (ir_type *uni, ir_entity *member);
1008 /** Returns true if a type is a union type. */
1009 int is_Union_type (const ir_type *uni);
1012 * @page array_type Representation of an array type
1014 * The array type represents rectangular multi dimensional arrays.
1015 * The constants representing the bounds must be allocated to
1016 * get_const_code_irg() by setting current_ir_graph accordingly.
1018 * - n_dimensions: Number of array dimensions.
1019 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
1020 * - *upper_bound: Upper bounds or dimensions.
1021 * - *element_type: The type of the array elements.
1022 * - *element_ent: An entity for the array elements to be used for
1023 * element selection with Sel.
1025 * Do we need several entities? One might want
1026 * to select a dimension and not a single element in case of multi
1027 * dimensional arrays.
1030 /** Create a new type array.
1032 * Sets n_dimension to dimension and all dimension entries to NULL.
1033 * Initializes order to the order of the dimensions.
1034 * The entity for array elements is built automatically.
1035 * Set dimension sizes after call to constructor with set_* routines.
1037 ir_type *new_type_array (ident *name, int n_dimensions,
1038 ir_type *element_type);
1040 /** Create a new type array with debug information.
1042 * Sets n_dimension to dimension and all dimension entries to NULL.
1043 * Initializes order to the order of the dimensions.
1044 * The entity for array elements is built automatically.
1045 * Set dimension sizes after call to constructor with set_* routines.
1046 * A legal array type must have at least one dimension set.
1048 ir_type *new_d_type_array (ident *name, int n_dimensions,
1049 ir_type *element_type, dbg_info* db);
1051 /* --- manipulate private fields of array type --- */
1053 /** Returns the number of array dimensions of this type. */
1054 int get_array_n_dimensions (const ir_type *array);
1057 * Allocates Const nodes of mode_Is for one array dimension.
1058 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
1060 void set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1063 * Sets the bounds for one array dimension.
1064 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
1066 void set_array_bounds (ir_type *array, int dimension, ir_node *lower_bound,
1067 ir_node *upper_bound);
1068 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
1069 void set_array_lower_bound (ir_type *array, int dimension, ir_node *lower_bound);
1071 /** Allocates Const nodes of mode_Is for the lower bound of an array
1072 dimension, i.e. [lower,upper[ */
1073 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
1075 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
1076 void set_array_upper_bound (ir_type *array, int dimension, ir_node *upper_bound);
1078 /** Allocates Const nodes of mode_Is for the upper bound of an array
1079 dimension, i.e. [lower,upper[. */
1080 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
1082 /** Returns true if lower bound != Unknown. */
1083 int has_array_lower_bound (const ir_type *array, int dimension);
1084 /** Returns the lower bound of an array. */
1085 ir_node * get_array_lower_bound (const ir_type *array, int dimension);
1086 /** Works only if bound is Const node with tarval that can be converted to long. */
1087 long get_array_lower_bound_int (const ir_type *array, int dimension);
1088 /** returns true if lower bound != Unknown */
1089 int has_array_upper_bound (const ir_type *array, int dimension);
1090 /** Returns the upper bound of an array. */
1091 ir_node * get_array_upper_bound (const ir_type *array, int dimension);
1092 /** Works only if bound is Const node with tarval that can be converted to long. */
1093 long get_array_upper_bound_int (const ir_type *array, int dimension);
1095 /** Sets an array dimension to a specific order. */
1096 void set_array_order (ir_type *array, int dimension, int order);
1098 /** Returns the order of an array dimension. */
1099 int get_array_order (const ir_type *array, int dimension);
1101 /** Find the array dimension that is placed at order order. */
1102 int find_array_dimension(const ir_type *array, int order);
1104 /** Sets the array element type. */
1105 void set_array_element_type (ir_type *array, ir_type* tp);
1107 /** Gets the array element type. */
1108 ir_type *get_array_element_type (ir_type *array);
1110 /** Sets the array element entity. */
1111 void set_array_element_entity (ir_type *array, ir_entity *ent);
1113 /** Get the array element entity. */
1114 ir_entity *get_array_element_entity (const ir_type *array);
1116 /** Returns true if a type is an array type. */
1117 int is_Array_type(const ir_type *array);
1120 * @page enumeration_type Representation of an enumeration type
1122 * Enumeration types need not necessarily be represented explicitly
1123 * by Firm types, as the frontend can lower them to integer constants as
1124 * well. For debugging purposes or similar tasks this information is useful.
1125 * The type state layout_fixed is set, if all enumeration constant have
1126 * there tarvals assigned. Until then
1128 * - *const: The target values representing the constants used to
1129 * represent individual enumerations.
1132 #ifndef _IR_ENUM_CONST_TYPEDEF_
1133 #define _IR_ENUM_CONST_TYPEDEF_
1134 typedef struct ir_enum_const ir_enum_const;
1137 /** Create a new type enumeration -- set the enumerators independently. */
1138 ir_type *new_type_enumeration(ident *name, int n_enums);
1140 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1141 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db);
1143 /* --- manipulate fields of enumeration type. --- */
1145 /** Set an enumeration constant to a enumeration type at a given position. */
1146 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con);
1148 /** Returns the number of enumeration values of this enumeration */
1149 int get_enumeration_n_enums(const ir_type *enumeration);
1151 /** Returns the enumeration constant at a given position. */
1152 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos);
1154 /** Returns the enumeration type owner of an enumeration constant. */
1155 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst);
1157 /** Sets the enumeration constant value. */
1158 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con);
1160 /** Returns the enumeration constant value. */
1161 tarval *get_enumeration_value(const ir_enum_const *enum_cnst);
1163 /** Assign an ident to an enumeration constant. */
1164 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id);
1166 /** Returns the assigned ident of an enumeration constant. */
1167 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst);
1169 /** Returns the assigned name of an enumeration constant. */
1170 const char *get_enumeration_name(const ir_enum_const *enum_cnst);
1172 /** Returns true if a type is a enumeration type. */
1173 int is_Enumeration_type(const ir_type *enumeration);
1176 * @page pointer_type Representation of a pointer type
1178 * The mode of the pointer type must be a reference mode.
1181 * - points_to: The type of the entity this pointer points to.
1184 /** Creates a new type pointer. */
1185 ir_type *new_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode);
1187 /** Creates a new type pointer with debug information. */
1188 ir_type *new_d_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
1190 /* --- manipulate fields of type_pointer --- */
1192 /** Sets the type to which a pointer points to. */
1193 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
1195 /** Returns the type to which a pointer points to. */
1196 ir_type *get_pointer_points_to_type (ir_type *pointer);
1198 /** Returns true if a type is a pointer type. */
1199 int is_Pointer_type (const ir_type *pointer);
1201 /** Returns the first pointer type that has as points_to tp.
1202 * Not efficient: O(#types).
1203 * If not found returns firm_unknown_type. */
1204 ir_type *find_pointer_type_to_type (ir_type *tp);
1207 * @page primitive_type Representation of a primitive type
1209 * Primitive types are types that represent atomic data values that
1210 * map directly to modes. They don't have private attributes. The
1211 * important information they carry is held in the common mode field.
1213 /** Creates a new primitive type. */
1214 ir_type *new_type_primitive(ident *name, ir_mode *mode);
1216 /** Creates a new primitive type with debug information. */
1217 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info* db);
1219 /** Returns true if a type is a primitive type. */
1220 int is_Primitive_type(const ir_type *primitive);
1224 * @page none_type The None type
1226 * This type is an auxiliary type dedicated to support type analyses.
1228 * The none type represents that there is no type. The type can be used to
1229 * initialize fields of type* that actually can not contain a type or that
1230 * are initialized for an analysis. There exists exactly one type none.
1231 * This type is not on the type list in ir_prog. It is
1232 * allocated when initializing the type module.
1234 * The following values are set:
1236 * - name: "type_none"
1237 * - state: layout_fixed
1240 /** A variable that contains the only none type. */
1241 extern ir_type *firm_none_type;
1243 /** Returns the none type. */
1244 ir_type *get_none_type(void);
1247 * @page unknown_type The Unknown type
1249 * This type is an auxiliary type dedicated to support type analyses.
1251 * The unknown type represents that there could be a type, but it is not
1252 * known. This type can be used to initialize fields before an analysis (not known
1253 * yet) or to represent the top of a lattice (could not be determined). There exists
1254 * exactly one type unknown. This type is not on the type list in ir_prog. It is
1255 * allocated when initializing the type module.
1257 * The following values are set:
1259 * - name: "type_unknown"
1260 * - state: layout_fixed
1263 /** A variable that contains the only unknown type. */
1264 extern ir_type *firm_unknown_type;
1266 /** Returns the unknown type. */
1267 ir_type *get_unknown_type(void);
1271 * Checks whether a type is atomic.
1272 * @param tp any type
1273 * @return true if type is primitive, pointer or enumeration
1275 int is_atomic_type(const ir_type *tp);
1277 /* --- Support for compound types --- */
1280 * Gets the number of elements in a Firm compound type.
1282 * This is just a comfortability function, because structs and
1283 * classes can often be treated be the same code, but they have
1284 * different access functions to their members.
1286 * @param tp The type (must be struct, union or class).
1288 * @return Number of members in the compound type.
1290 int get_compound_n_members(const ir_type *tp);
1293 * Gets the member of a Firm compound type at position pos.
1295 * @param tp The type (must be struct, union or class).
1296 * @param pos The number of the member.
1298 * @return The member entity at position pos.
1300 * @see get_compound_n_members() for justification of existence.
1302 ir_entity *get_compound_member(const ir_type *tp, int pos);
1304 /** Returns index of member in tp, -1 if not contained. */
1305 int get_compound_member_index(const ir_type *tp, ir_entity *member);
1308 * Checks whether a type is a compound type.
1310 * @param tp - any type
1312 * @return true if the type is class, structure, union or array type.
1314 int is_compound_type(const ir_type *tp);
1317 * Checks, whether a type is a frame type.
1319 int is_frame_type(const ir_type *tp);
1322 * Checks, whether a type is a value parameter type.
1324 int is_value_param_type(const ir_type *tp);
1327 * Checks, whether a type is a lowered type.
1329 int is_lowered_type(const ir_type *tp);
1332 * Makes a new frame type. Frame types are class types,
1333 * so all class access functions work.
1334 * Frame types are not in the global list of types.
1336 ir_type *new_type_frame(ident *name);
1339 * Sets a lowered type for a type. This sets both associations
1340 * and marks lowered_type as a "lowered" one.
1342 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
1345 * Gets the lowered/unlowered type of a type or NULL if this type
1346 * has no lowered/unlowered one.
1348 ir_type *get_associated_type(const ir_type *tp);
1351 * Allocate an area of size bytes aligned at alignment
1352 * at the start or the end of a frame type.
1353 * The frame type must already have a fixed layout.
1355 * @param frame_type a frame type
1356 * @param size the size of the entity
1357 * @param alignment the alignment of the entity
1358 * @param at_start if true, put the area at the frame type's start, else at end
1360 * @return the entity representing the area
1362 ir_entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start);
1364 /*-----------------------------------------------------------------*/
1366 /*-----------------------------------------------------------------*/
1369 * Outputs a unique number for this type if libfirm is compiled for
1370 * debugging, (configure with --enable-debug) else returns the address
1371 * of the type cast to long.
1373 long get_type_nr(const ir_type *tp);
1375 #endif /* _FIRM_TR_TYPE_H_ */