Next: Pretty Printing, Previous: Values From Inferior, Up: Python API

##### 23.2.2.5 Types In Python

gdb represents types from the inferior using the class `gdb.Type`.

The following type-related functions are available in the `gdb` module:

— Function: lookup_type name [block]

> This function looks up a type by name. name is the name of the type to look up. It must be a string.

> Ordinarily, this function will return an instance of `gdb.Type`. If the named type cannot be found, it will throw an exception.

An instance of `Type` has the following attributes:

— Instance Variable of Type: code

> The type code for this type. The type code will be one of the `TYPE_CODE_` constants defined below.

— Instance Variable of Type: sizeof

> The size of this type, in target `char` units. Usually, a target's `char` type will be an 8-bit byte. However, on some unusual platforms, this type may have a different size.

— Instance Variable of Type: tag

> The tag name for this type. The tag name is the name after `struct`, `union`, or `enum` in C and C`++`; not all languages have this concept. If this type has no tag name, then `None` is returned.

The following methods are provided:

— Method on Type: fields

> For structure and union types, this method returns the fields. Range types have two fields, the minimum and maximum values. Enum types have one field per enum constant. Function and method types have one field per parameter. The base types of C`++` classes are also represented as fields. If the type has no fields, or does not fit into one of these categories, an empty sequence will be returned.

> Each field is an object, with some pre-defined attributes:

> `bitpos`
>   This attribute is not available for `static` fields (as in C`++` or Java). For non-`static` fields, the value is the bit position of the field.
> `name`
>   The name of the field, or `None` for anonymous fields.
> `artificial`
>   This is `True` if the field is artificial, usually meaning that it was provided by the compiler and not the user. This attribute is always provided, and is `False` if the field is not artificial.
> `bitsize`
>   If the field is packed, or is a bitfield, then this will have a non-zero value, which is the size of the field in bits. Otherwise, this will be zero; in this case the field's size is given by its type.
> `type`
>   The type of the field. This is usually an instance of `Type`, but it can be `None` in some situations.

— Method on Type: const

> Return a new `gdb.Type` object which represents a `const`-qualified variant of this type.

— Method on Type: volatile

> Return a new `gdb.Type` object which represents a `volatile`-qualified variant of this type.

— Method on Type: unqualified

> Return a new `gdb.Type` object which represents an unqualified variant of this type. That is, the result is neither `const` nor `volatile`.

— Method on Type: reference

> Return a new `gdb.Type` object which represents a reference to this type.

— Method on Type: strip_typedefs

> Return a new `gdb.Type` that represents the real type, after removing all layers of typedefs.

— Method on Type: target

> Return a new `gdb.Type` object which represents the target type of this type.

> For a pointer type, the target type is the type of the pointed-to object. For an array type (meaning C-like arrays), the target type is the type of the elements of the array. For a function or method type, the target type is the type of the return value. For a complex type, the target type is the type of the elements. For a typedef, the target type is the aliased type.

> If the type does not have a target, this method will throw an exception.

— Method on Type: template_argument n

> If this `gdb.Type` is an instantiation of a template, this will return a new `gdb.Type` which represents the type of the nth template argument.

> If this `gdb.Type` is not a template type, this will throw an exception. Ordinarily, only C`++` code will have template types.

> name is searched for globally.

Each type has a code, which indicates what category this type falls into. The available type categories are represented by constants defined in the `gdb` module:

`TYPE_CODE_PTR`
  The type is a pointer.

`TYPE_CODE_ARRAY`
  The type is an array.

`TYPE_CODE_STRUCT`
  The type is a structure.

`TYPE_CODE_UNION`
  The type is a union.

`TYPE_CODE_ENUM`
  The type is an enum.

`TYPE_CODE_FLAGS`
  A bit flags type, used for things such as status registers.

`TYPE_CODE_FUNC`
  The type is a function.

`TYPE_CODE_INT`
  The type is an integer type.

`TYPE_CODE_FLT`
  A floating point type.

`TYPE_CODE_VOID`
  The special type `void`.

`TYPE_CODE_SET`
  A Pascal set type.

`TYPE_CODE_RANGE`
  A range type, that is, an integer type with bounds.

`TYPE_CODE_STRING`
  A string type. Note that this is only used for certain languages with language-defined string types; C strings are not represented this way.

`TYPE_CODE_BITSTRING`
  A string of bits.

`TYPE_CODE_ERROR`
  An unknown or erroneous type.

`TYPE_CODE_METHOD`
  A method type, as found in C`++` or Java.

`TYPE_CODE_METHODPTR`
  A pointer-to-member-function.

`TYPE_CODE_MEMBERPTR`
  A pointer-to-member.

`TYPE_CODE_REF`
  A reference type.

`TYPE_CODE_CHAR`
  A character type.

`TYPE_CODE_BOOL`
  A boolean type.

`TYPE_CODE_COMPLEX`
  A complex float type.

`TYPE_CODE_TYPEDEF`
  A typedef to some other type.

`TYPE_CODE_NAMESPACE`
  A C`++` namespace.

`TYPE_CODE_DECFLOAT`
  A decimal floating point type.

`TYPE_CODE_INTERNAL_FUNCTION`
  A function internal to gdb. This is the type used to represent convenience functions.