Types can be created in several ways:
fundamental types can be accessed using gccjit.Context.get_type():
int_type = ctxt.get_type(gccjit.TypeKind.INT)
See gccjit.TypeKind for the available types.
You can get int types of specific sizes (in bytes) using gccjit.Context.get_int_type():
int_type = ctxt.get_int_type(4, is_signed=True)
derived types can be accessed by calling methods on an existing type:
const_int_star = int_type.get_const().get_pointer()
int_const_star = int_type.get_pointer().get_const()
by creating structures (see below).
Given type T get type T*.
Return type: | gccjit.Type |
---|
Given type T get type const T.
Return type: | gccjit.Type |
---|
Given type T get type volatile T.
Return type: | gccjit.Type |
---|
Look up one of the standard types (see gccjit.TypeKind):
int_type = ctxt.get_type(gccjit.TypeKind.INT)
Parameters: | type_enum (gccjit.TypeKind) – Which type to lookup |
---|
C’s “void” type.
C’s “void *”.
C++’s bool type; also C99’s “_Bool” type, aka “bool” if using stdbool.h.
C’s “char” (of some signedness) and the variants where the signedness is specified.
C’s “short” (signed) and “unsigned short”.
C’s “int” (signed) and “unsigned int”:
int_type = ctxt.get_type(gccjit.TypeKind.INT)
C’s “long” (signed) and “unsigned long”.
C99’s “long long” (signed) and “unsigned long long”.
Floating-point types
C type: (const char *):
const_char_p = ctxt.get_type(gccjit.TypeKind.CONST_CHAR_PTR)
The C “size_t” type.
C type: (FILE *)
Look up an integet type of the given size:
int_type = ctxt.get_int_type(4, is_signed=True)
You can model C struct types by creating gccjit.Struct and gccjit.Field instances, in either order:
by creating the fields, then the structure. For example, to model:
struct coord {double x; double y; };
you could call:
field_x = ctxt.new_field(double_type, b'x')
field_y = ctxt.new_field(double_type, b'y')
coord = ctxt.new_struct(b'coord', [field_x, field_y])
(see gccjit.Context.new_field() and gccjit.Context.new_struct()), or
by creating the structure, then populating it with fields, typically to allow modelling self-referential structs such as:
struct node { int m_hash; struct node *m_next; };
like this:
node = ctxt.new_struct(b'node')
node_ptr = node.get_pointer()
field_hash = ctxt.new_field(int_type, b'm_hash')
field_next = ctxt.new_field(node_ptr, b'm_next')
node.set_fields([field_hash, field_next])