hare::types
hare::types provides an implementation of the Hare type system. See store to initialize a type store and lookup for looking up Hare types from their hare::ast:: representation.
Index
Types
type _enum = struct {
storage: builtin,
values: [](str, u64),
};
type _struct = struct {
kind: struct_union,
fields: []struct_field,
};
type _type = struct {
flags: flag,
repr: (alias | array | builtin | _enum | func | pointer | slice | _struct | tagged | tuple),
id: u32,
sz: size,
_align: size,
};
type alias = struct {
id: ast::ident,
secondary: const nullable *_type,
};
type arch = struct {
_int: size,
_pointer: size,
_size: size,
valist_size: size,
valist_align: size,
};
type array = struct {
length: size,
member: const *_type,
};
type builtin = enum u8 {
BOOL,
DONE,
F32,
F64,
FCONST,
I16,
I32,
I64,
I8,
ICONST,
INT,
NEVER,
NULL,
OPAQUE,
RCONST,
RUNE,
SIZE,
STR,
U16,
U32,
U64,
U8,
UINT,
UINTPTR,
VALIST,
VOID,
};
type flag = enum u8 {
NONE = 0,
CONST = 1 << 0,
ERROR = 1 << 1,
};
type func = struct {
result: const *_type,
variadism: variadism,
params: []const *_type,
};
type pointer = struct {
referent: const *_type,
flags: pointer_flag,
};
type pointer_flag = enum u8 {
NONE = 0,
NULLABLE = 1 << 0,
};
type resolver = fn(rstate: nullable *opaque, store: *typestore, expr: const *ast::expr) (size | deferred | error);
type slice = const *_type;
type struct_field = struct {
name: str,
offs: size,
_type: const *_type,
};
type struct_union = enum {
STRUCT,
UNION,
};
type tagged = []const *_type;
type tuple = []tuple_value;
type tuple_value = struct {
offs: size,
_type: const *_type,
};
type variadism = enum {
NONE,
C,
HARE,
};
type typestore = struct {
map: [BUCKETS][]_type,
arch: arch,
resolve: nullable *resolver,
rstate: nullable *opaque,
};
Errors
type deferred = !void;
type error = !(noresolver | errors::opaque_);
type noresolver = !void;
Constants
def SIZE_UNDEFINED: size = -1: size;
def BUCKETS: size = 65535;
Globals
const aarch64: arch;
const builtin_bool: _type;
const builtin_done: _type;
const builtin_f32: _type;
const builtin_f64: _type;
const builtin_i16: _type;
const builtin_i32: _type;
const builtin_i64: _type;
const builtin_i8: _type;
const builtin_never: _type;
const builtin_opaque: _type;
const builtin_rune: _type;
const builtin_u16: _type;
const builtin_u32: _type;
const builtin_u64: _type;
const builtin_u8: _type;
const builtin_void: _type;
const riscv64: arch;
const x86_64: arch;
Functions
fn dealias(t: *_type) const *_type;
fn hash(t: *_type) u32;
fn is_float(ty: const *_type) bool;
fn is_integer(ty: const *_type) bool;
fn is_signed(ty: const *_type) bool;
fn lookup(store: *typestore, ty: *ast::_type) (const *_type | deferred | error);
fn lookup_builtin(store: *typestore, _type: ast::builtin_type) const *_type;
fn newalias(store: *typestore, ident: const ast::ident, of: const *_type) const *_type;
fn store(arch: arch, resolver: nullable *resolver, rstate: nullable *opaque) *typestore;
fn store_free(store: *typestore) void;
fn strerror(err: error) const str;
Types
type _enum
type _enum = struct {
storage: builtin,
values: [](str, u64),
};
An enum type, e.g. enum { FOO = 0 }
type _struct
type _struct = struct {
kind: struct_union,
fields: []struct_field,
};
struct { ... } or union { ... }
Note that embedded anonymous structs will have been merged into their parent type.
type _type
type _type = struct {
flags: flag,
repr: (alias | array | builtin | _enum | func | pointer | slice | _struct | tagged | tuple),
id: u32,
sz: size,
_align: size,
};
A Hare type.
type alias
type alias = struct {
id: ast::ident,
secondary: const nullable *_type,
};
A type alias.
type arch
type arch = struct {
_int: size,
_pointer: size,
_size: size,
valist_size: size,
valist_align: size,
};
Configuration for a specific architecture.
type array
type array = struct {
length: size,
member: const *_type,
};
An array type, e.g. [10]int
type builtin
type builtin = enum u8 {
BOOL,
DONE,
F32,
F64,
FCONST,
I16,
I32,
I64,
I8,
ICONST,
INT,
NEVER,
NULL,
OPAQUE,
RCONST,
RUNE,
SIZE,
STR,
U16,
U32,
U64,
U8,
UINT,
UINTPTR,
VALIST,
VOID,
};
A built-in primitive type (int, bool, str, etc).
type flag
type flag = enum u8 {
NONE = 0,
CONST = 1 << 0,
ERROR = 1 << 1,
};
Flags for a Hare type.
type func
type func = struct {
result: const *_type,
variadism: variadism,
params: []const *_type,
};
A function type, e.g. fn(x: int, y: int) int
type pointer
type pointer = struct {
referent: const *_type,
flags: pointer_flag,
};
*int
type pointer_flag
type pointer_flag = enum u8 {
NONE = 0,
NULLABLE = 1 << 0,
};
Flags which apply to a pointer type.
type resolver
type resolver = fn(rstate: nullable *opaque, store: *typestore, expr: const *ast::expr) (size | deferred | error);
A function which evaluates a hare::ast::expr, providing either a size result or an error.
type slice
type slice = const *_type;
[]int
type struct_field
type struct_field = struct {
name: str,
offs: size,
_type: const *_type,
};
A single struct field.
type struct_union
type struct_union = enum {
STRUCT,
UNION,
};
Indicates if a _struct was declared as a struct or union type.
type tagged
type tagged = []const *_type;
A tagged union type, e.g. (int | uint | void).
type tuple
type tuple = []tuple_value;
A tuple type, e.g. (a, b, c)
type tuple_value
type tuple_value = struct {
offs: size,
_type: const *_type,
};
A single value of a tuple type.
type variadism
type variadism = enum {
NONE,
C,
HARE,
};
Indicates the variadism of a func
type typestore
Show undocumented member
type typestore = struct {
map: [BUCKETS][]_type,
arch: arch,
resolve: nullable *resolver,
rstate: nullable *opaque,
};
Errors
type deferred
type deferred = !void;
Returned from lookup when we are unable to resolve this type, but it does not necessarily have an error. This occurs when a type includes an unknown forward reference.
type error
type error = !(noresolver | errors::opaque_);
All possible errors for lookup.
type noresolver
type noresolver = !void;
A resolver function was not provided to store, but was required to look up this type.
Constants
def SIZE_UNDEFINED
def SIZE_UNDEFINED: size = -1: size;
The sz field of _type is set to this value to indicate that the size of the type is undefined.
def BUCKETS
Show undocumented member
def BUCKETS: size = 65535;
Globals
let aarch64
const aarch64: arch;
arch configuration for aarch64.
let builtin_bool
const builtin_bool: _type;
_type representation of bool.
let builtin_done
const builtin_done: _type;
_type representation of done.
let builtin_f32
const builtin_f32: _type;
_type representation of f32.
let builtin_f64
const builtin_f64: _type;
_type representation of f64.
let builtin_i16
const builtin_i16: _type;
_type representation of i16.
let builtin_i32
const builtin_i32: _type;
_type representation of i32.
let builtin_i64
const builtin_i64: _type;
_type representation of i64.
let builtin_i8
const builtin_i8: _type;
_type representation of i8.
let builtin_never
const builtin_never: _type;
_type representation of never.
let builtin_opaque
const builtin_opaque: _type;
_type representation of opaque.
let builtin_rune
const builtin_rune: _type;
_type representation of rune.
let builtin_u16
const builtin_u16: _type;
_type representation of u16.
let builtin_u32
const builtin_u32: _type;
_type representation of u32.
let builtin_u64
const builtin_u64: _type;
_type representation of u64.
let builtin_u8
const builtin_u8: _type;
_type representation of u8.
let builtin_void
const builtin_void: _type;
_type representation of void.
let riscv64
const riscv64: arch;
arch configuration for riscv64.
let x86_64
const x86_64: arch;
arch configuration for x86_64.
Functions
fn dealias
fn dealias(t: *_type) const *_type;
Unwraps a type which may be aliased and returns the underlying type.
fn hash
fn hash(t: *_type) u32;
Returns the hash of a type. These hashes are deterministic and universally unique: different computers will generate the same hash for the same type.
fn is_float
fn is_float(ty: const *_type) bool;
Returns true if the given type is a floating-point type.
fn is_integer
fn is_integer(ty: const *_type) bool;
Returns true if the given type is an integer type.
fn is_signed
fn is_signed(ty: const *_type) bool;
Returns true if the given type is a signed type.
fn lookup
fn lookup(store: *typestore, ty: *ast::_type) (const *_type | deferred | error);
Retrieves a _type for a given hare::ast::_type.
fn lookup_builtin
fn lookup_builtin(store: *typestore, _type: ast::builtin_type) const *_type;
Looks up a built-in type.
fn newalias
fn newalias(store: *typestore, ident: const ast::ident, of: const *_type) const *_type;
Creates a new type alias.
fn store
fn store(arch: arch, resolver: nullable *resolver, rstate: nullable *opaque) *typestore;
Initializes a new type store. Optionally, provide a function which type-checks and evaluates an hare::ast::expr. If a resolver is not provided, looking up types with an expression component (e.g. [2 + 2]int) will return noresolver.
fn store_free
fn store_free(store: *typestore) void;
Frees state associated with a typestore.
fn strerror
fn strerror(err: error) const str;
Convert an error into a human-friendly string.