Next: Scopes, Previous: Tree overview, Up: Trees
All types have corresponding tree nodes. However, you should not assume that there is exactly one tree node corresponding to each type. There are often multiple nodes corresponding to the same type.
For the most part, different kinds of types have different tree codes.
(For example, pointer types use a POINTER_TYPE code while arrays
use an ARRAY_TYPE code.) However, pointers to member functions
use the RECORD_TYPE code. Therefore, when writing a
switch statement that depends on the code associated with a
particular type, you should take care to handle pointers to member
functions under the RECORD_TYPE case label.
In C++, an array type is not qualified; rather the type of the array
elements is qualified. This situation is reflected in the intermediate
representation. The macros described here will always examine the
qualification of the underlying element type when applied to an array
type. (If the element type is itself an array, then the recursion
continues until a non-array type is found, and the qualification of this
type is examined.) So, for example, CP_TYPE_CONST_P will hold of
the type const int ()[7], denoting an array of seven ints.
The following functions and macros deal with cv-qualification of types:
CP_TYPE_QUALSTYPE_UNQUALIFIED if no qualifiers have been
applied. The TYPE_QUAL_CONST bit is set if the type is
const-qualified. The TYPE_QUAL_VOLATILE bit is set if the
type is volatile-qualified. The TYPE_QUAL_RESTRICT bit is
set if the type is restrict-qualified.
CP_TYPE_CONST_Pconst-qualified.
CP_TYPE_VOLATILE_Pvolatile-qualified.
CP_TYPE_RESTRICT_Prestrict-qualified.
CP_TYPE_CONST_NON_VOLATILE_Pconst-qualified, but
not volatile-qualified; other cv-qualifiers are ignored as
well: only the const-ness is tested.
TYPE_MAIN_VARIANTA few other macros and functions are usable with all types:
TYPE_SIZEINTEGER_CST. For an incomplete type, TYPE_SIZE will be
NULL_TREE.
TYPE_ALIGNint.
TYPE_NAMETYPE_DECL) for
the type. (Note this macro does not return a
IDENTIFIER_NODE, as you might expect, given its name!) You can
look at the DECL_NAME of the TYPE_DECL to obtain the
actual name of the type. The TYPE_NAME will be NULL_TREE
for a type that is not a built-in type, the result of a typedef, or a
named class type.
CP_INTEGRAL_TYPEARITHMETIC_TYPE_PCLASS_TYPE_PTYPE_BUILT_INTYPE_PTRMEM_PTYPE_PTR_PTYPE_PTRFN_PTYPE_PTROB_Pvoid *. You
may use TYPE_PTROBV_P to test for a pointer to object type as
well as void *.
TYPE_CANONICALsame_type_p: if the TYPE_CANONICAL values
of the types are equal, the types are equivalent; otherwise, the types
are not equivalent. The notion of equivalence for canonical types is
the same as the notion of type equivalence in the language itself. For
instance,
When TYPE_CANONICAL is NULL_TREE, there is no canonical
type for the given type node. In this case, comparison between this
type and any other type requires the compiler to perform a deep,
“structural” comparison to see if the two type nodes have the same
form and properties.
The canonical type for a node is always the most fundamental type in
the equivalence class of types. For instance, int is its own
canonical type. A typedef I of int will have int
as its canonical type. Similarly, I* and a typedef IP (defined to I*) will has int* as their canonical
type. When building a new type node, be sure to set
TYPE_CANONICAL to the appropriate canonical type. If the new
type is a compound type (built from other types), and any of those
other types require structural equality, use
SET_TYPE_STRUCTURAL_EQUALITY to ensure that the new type also
requires structural equality. Finally, if for some reason you cannot
guarantee that TYPE_CANONICAL will point to the canonical type,
use SET_TYPE_STRUCTURAL_EQUALITY to make sure that the new
type–and any type constructed based on it–requires structural
equality. If you suspect that the canonical type system is
miscomparing types, pass --param verify-canonical-types=1 to
the compiler or configure with --enable-checking to force the
compiler to verify its canonical-type comparisons against the
structural comparisons; the compiler will then print any warnings if
the canonical types miscompare.
TYPE_STRUCTURAL_EQUALITY_PTYPE_CANONICAL is NULL_TREE.
SET_TYPE_STRUCTURAL_EQUALITYTYPE_CANONICAL to
NULL_TREE.
same_type_ptypedef for the other, or
both are typedefs for the same type. This predicate also holds if
the two trees given as input are simply copies of one another; i.e.,
there is no difference between them at the source level, but, for
whatever reason, a duplicate has been made in the representation. You
should never use == (pointer equality) to compare types; always
use same_type_p instead.
Detailed below are the various kinds of types, and the macros that can be used to access them. Although other kinds of types are used elsewhere in G++, the types described here are the only ones that you will encounter while examining the intermediate representation.
VOID_TYPEvoid type.
INTEGER_TYPEchar,
short, int, long, and long long. This code
is not used for enumeration types, nor for the bool type.
The TYPE_PRECISION is the number of bits used in
the representation, represented as an unsigned int. (Note that
in the general case this is not the same value as TYPE_SIZE;
suppose that there were a 24-bit integer type, but that alignment
requirements for the ABI required 32-bit alignment. Then,
TYPE_SIZE would be an INTEGER_CST for 32, while
TYPE_PRECISION would be 24.) The integer type is unsigned if
TYPE_UNSIGNED holds; otherwise, it is signed.
The TYPE_MIN_VALUE is an INTEGER_CST for the smallest
integer that may be represented by this type. Similarly, the
TYPE_MAX_VALUE is an INTEGER_CST for the largest integer
that may be represented by this type.
REAL_TYPEfloat, double, and long
double types. The number of bits in the floating-point representation
is given by TYPE_PRECISION, as in the INTEGER_TYPE case.
FIXED_POINT_TYPEshort _Fract, _Fract, long
_Fract, long long _Fract, short _Accum, _Accum,
long _Accum, and long long _Accum types. The number of bits
in the fixed-point representation is given by TYPE_PRECISION,
as in the INTEGER_TYPE case. There may be padding bits, fractional
bits and integral bits. The number of fractional bits is given by
TYPE_FBIT, and the number of integral bits is given by TYPE_IBIT.
The fixed-point type is unsigned if TYPE_UNSIGNED holds; otherwise,
it is signed.
The fixed-point type is saturating if TYPE_SATURATING holds; otherwise,
it is not saturating.
COMPLEX_TYPE__complex__ data types. The
TREE_TYPE is the type of the real and imaginary parts.
ENUMERAL_TYPETYPE_PRECISION gives
(as an int), the number of bits used to represent the type. If
there are no negative enumeration constants, TYPE_UNSIGNED will
hold. The minimum and maximum enumeration constants may be obtained
with TYPE_MIN_VALUE and TYPE_MAX_VALUE, respectively; each
of these macros returns an INTEGER_CST.
The actual enumeration constants themselves may be obtained by looking
at the TYPE_VALUES. This macro will return a TREE_LIST,
containing the constants. The TREE_PURPOSE of each node will be
an IDENTIFIER_NODE giving the name of the constant; the
TREE_VALUE will be an INTEGER_CST giving the value
assigned to that constant. These constants will appear in the order in
which they were declared. The TREE_TYPE of each of these
constants will be the type of enumeration type itself.
BOOLEAN_TYPEbool type.
POINTER_TYPETREE_TYPE gives the type to which this type points. If the type
is a pointer to data member type, then TYPE_PTRMEM_P will hold.
For a pointer to data member type of the form ‘T X::*’,
TYPE_PTRMEM_CLASS_TYPE will be the type X, while
TYPE_PTRMEM_POINTED_TO_TYPE will be the type T.
REFERENCE_TYPETREE_TYPE gives the type
to which this type refers.
FUNCTION_TYPETREE_TYPE gives the return type of the function.
The TYPE_ARG_TYPES are a TREE_LIST of the argument types.
The TREE_VALUE of each node in this list is the type of the
corresponding argument; the TREE_PURPOSE is an expression for the
default argument value, if any. If the last node in the list is
void_list_node (a TREE_LIST node whose TREE_VALUE
is the void_type_node), then functions of this type do not take
variable arguments. Otherwise, they do take a variable number of
arguments.
Note that in C (but not in C++) a function declared like void f()
is an unprototyped function taking a variable number of arguments; the
TYPE_ARG_TYPES of such a function will be NULL.
METHOD_TYPEFUNCTION_TYPE, the return type is given by the TREE_TYPE.
The type of *this, i.e., the class of which functions of this
type are a member, is given by the TYPE_METHOD_BASETYPE. The
TYPE_ARG_TYPES is the parameter list, as for a
FUNCTION_TYPE, and includes the this argument.
ARRAY_TYPETREE_TYPE gives the type of
the elements in the array. If the array-bound is present in the type,
the TYPE_DOMAIN is an INTEGER_TYPE whose
TYPE_MIN_VALUE and TYPE_MAX_VALUE will be the lower and
upper bounds of the array, respectively. The TYPE_MIN_VALUE will
always be an INTEGER_CST for zero, while the
TYPE_MAX_VALUE will be one less than the number of elements in
the array, i.e., the highest value which may be used to index an element
in the array.
RECORD_TYPEstruct and class types, as well as
pointers to member functions and similar constructs in other languages.
TYPE_FIELDS contains the items contained in this type, each of
which can be a FIELD_DECL, VAR_DECL, CONST_DECL, or
TYPE_DECL. You may not make any assumptions about the ordering
of the fields in the type or whether one or more of them overlap. If
TYPE_PTRMEMFUNC_P holds, then this type is a pointer-to-member
type. In that case, the TYPE_PTRMEMFUNC_FN_TYPE is a
POINTER_TYPE pointing to a METHOD_TYPE. The
METHOD_TYPE is the type of a function pointed to by the
pointer-to-member function. If TYPE_PTRMEMFUNC_P does not hold,
this type is a class type. For more information, see see Classes.
UNION_TYPEunion types. Similar to RECORD_TYPE
except that all FIELD_DECL nodes in TYPE_FIELD start at
bit position zero.
QUAL_UNION_TYPEUNION_TYPE except that each FIELD_DECL has a
DECL_QUALIFIER field, which contains a boolean expression that
indicates whether the field is present in the object. The type will only
have one field, so each field's DECL_QUALIFIER is only evaluated
if none of the expressions in the previous fields in TYPE_FIELDS
are nonzero. Normally these expressions will reference a field in the
outer object using a PLACEHOLDER_EXPR.
UNKNOWN_TYPEOFFSET_TYPEX::m the TYPE_OFFSET_BASETYPE is X and the
TREE_TYPE is the type of m.
TYPENAME_TYPEtypename T::A. The
TYPE_CONTEXT is T; the TYPE_NAME is an
IDENTIFIER_NODE for A. If the type is specified via a
template-id, then TYPENAME_TYPE_FULLNAME yields a
TEMPLATE_ID_EXPR. The TREE_TYPE is non-NULL if the
node is implicitly generated in support for the implicit typename
extension; in which case the TREE_TYPE is a type node for the
base-class.
TYPEOF_TYPE__typeof__ extension. The
TYPE_FIELDS is the expression the type of which is being
represented.
There are variables whose values represent some of the basic types. These include:
void_type_nodevoid.
integer_type_nodeint.
unsigned_type_node.unsigned int.
char_type_node.char.
same_type_p.