Structure and union types are defined with the TYPE_STRUCT
and
TYPE_UNION
operators and use the struct_type
derived type
class. Both of these types include a name and a list of fields. The
difference between them is that the fields of a union are all stored at
the same offset so that only one field may be used at a time, while the
fields of a structure are not allowed to overlap. The name
method returns the name of a structure or union type, which should be
distinct from the names of other types within the symbol table where it
is defined. The name is automatically entered in the lexicon
(see section Lexicon) when it is set with the set_name
method.
The struct_type
class contains a field to record the total size
of the type in bits. The set_size
method is used to assign to
this field. Because structure and union types may be used as array
elements, they must be able to tile an array without violating any
alignment restrictions. This means that extra padding may need to be
added at the end of a structure or union. Consequently, the total size
of the type may be greater than the sizes of the fields.
The names, types, and offsets of the fields are stored in
dynamically-allocated arrays within a struct_type
node. The
set_num_fields
method determines the number of fields and thus
the size of these arrays. The number of fields may be changed at any
time. If necessary, additional space will be allocated. The
num_fields
method returns the current number of fields. The
field_name
, field_type
, and offset
methods retrieve
the field names, types, and offsets, and the set_field_name
,
set_field_type
, and set_offset
methods change their
values. The fields are numbered beginning with zero. For structures
the fields must be in order of increasing offsets. The field offsets
for union types should all be zero. The field names are automatically
entered in the lexicon (see section Lexicon).
The find_field_by_name
method can be used to search for a field
with a particular name. If successful, it returns the index of the
field; otherwise, it returns a value of -1
. The
find_field_by_offset
method can also be used for structure types
to find the field at a certain offset. If the specified offset is not
exactly at the beginning of a field, find_field_by_offset
returns
the index of the field containing it and saves the offset within that
field in the left
parameter.