Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types

Type     ::= BaseType ('[' ArrayLen ']')?
BaseType ::= ScalarType | ident            -- ident names a declared struct
ScalarType ::= 'bool' | 'byte' | 'Address'
             | 'int'  | 'int8'  | 'int16'  | 'int32'  | 'int64'
             | 'uint' | 'uint8' | 'uint16' | 'uint32' | 'uint64'
ArrayLen ::= int_lit | ident               -- ident names a declared constant

Every variable, parameter, carrier, struct field and constant has a type written this way. Fiducia has no pointer types and no void type.

Scalars

TypeMeaning
int8, int16, int32, int64Signed integers of that many bits.
uint8, uint16, uint32, uint64Unsigned integers of that many bits.
intint64. The type of an integer literal with nothing to give it another.
uintuint64.
byteuint8, spelled uint8_t in the generated C.
booltrue or false; not an integer.
AddressA virtual or physical address, as wide as the platform’s. Arithmetic is defined on it.

int, uint and byte are other names for int64, uint64 and uint8: the same types, not conversions of them. The data layer still rejects int and uint in a struct field, since they name no fixed size there; use the sized types.

Address is the type of a mapping’s m.vaddr and m.paddr (see Imports and Mappings).

Structs and arrays

A struct is declared in the .fis’s data layer and is visible to every program by name. A fixed array T[N] holds N elements of one type; N is an integer literal or a declared constant, and a type has at most one array suffix: T[N], never T[N][M].

const LEN: uint = 4;

struct Header {
    kind:  uint8;
    seq:   uint8;
    flags: uint16;
}

struct Frame {
    head: Header;
    data: byte[4];
}
done:  bool;
raw:   byte;
level: int8;
count: uint;
frame: Frame;
codes: uint16[LEN];

sizeof(T) is the size of any type in bytes, a constant: sizeof(Frame), sizeof(uint16[LEN]). A struct’s fields are laid out as C lays them out, in declaration order and padded, unless the struct is @attr(packed) (see Data Layer).

A struct or array variable is assigned whole. A field of a struct value is read with x.f (frame.head.kind); a field cannot be assigned to, and there is no indexing of an array element in a .fi. A channel’s slot type is a struct or a scalar, never an array (see Channel Declarations).

Conversion

There are no implicit conversions. Both operands of an arithmetic, bitwise or comparison operator have one type, and a literal takes the type of the operand beside it. An assignment must match the type of the variable it assigns exactly: a = raw is an error for an int a and a byte raw.

The one conversion is the cast e as T:

done  = step > 0;
level = step as int8;
count = step as uint + 1;
raw   = frame.head.kind as byte;
  • The target T is an integer type or Address; the operand is an integer or an address. A bool, a struct and an array can be neither.
  • as binds tighter than the binary operators and looser than the unary ones, so step as uint + 1 is (step as uint) + 1 (see Expressions).
  • A narrowing cast of a constant is not a constant overflow: 300 as uint8 type-checks as a uint8, where a bare 300 would not fit.
  • Unary - is defined only on signed types; ! only on bool.