hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

types.ha (2546B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use hare::ast;
      5 
      6 // A type alias.
      7 export type alias = struct {
      8 	id: ast::ident,
      9 	// null for forward referenced types
     10 	secondary: const nullable *_type,
     11 };
     12 
     13 // An array type, e.g. [10]int
     14 export type array = struct {
     15 	// [[SIZE_UNDEFINED]] for [*]type
     16 	length: size,
     17 	member: const *_type,
     18 };
     19 
     20 // A built-in primitive type (int, bool, str, etc).
     21 export type builtin = enum u8 {
     22 	// Keep me consistent with ast::builtin
     23 
     24 	BOOL, DONE, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NEVER,
     25 	NULL, OPAQUE, RCONST, RUNE, SIZE, STR, U16, U32, U64, U8, UINT, UINTPTR,
     26 	VALIST, VOID
     27 };
     28 
     29 // An enum type, e.g. enum { FOO = 0 }
     30 export type _enum = struct {
     31 	storage: builtin,
     32 	values: [](str, u64),
     33 };
     34 
     35 // Indicates the variadism of a [[func]]
     36 export type variadism = enum {
     37 	NONE,
     38 	C,
     39 	HARE,
     40 };
     41 
     42 // A function type, e.g. fn(x: int, y: int) int
     43 export type func = struct {
     44 	result: const *_type,
     45 	variadism: variadism,
     46 	params: []const *_type,
     47 };
     48 
     49 // Flags which apply to a pointer type.
     50 export type pointer_flag = enum u8 {
     51 	// Keep me consistent with ast::pointer_flag
     52 
     53 	NONE = 0,
     54 	NULLABLE = 1 << 0,
     55 };
     56 
     57 // *int
     58 export type pointer = struct {
     59 	referent: const *_type,
     60 	flags: pointer_flag,
     61 };
     62 
     63 // []int
     64 export type slice = const *_type;
     65 
     66 // Indicates if a [[_struct]] was declared as a struct or union type.
     67 export type struct_union = enum {
     68 	STRUCT,
     69 	UNION,
     70 };
     71 
     72 // struct { ... } or union { ... }
     73 //
     74 // Note that embedded anonymous structs will have been merged into their parent
     75 // type.
     76 export type _struct = struct {
     77 	kind: struct_union,
     78 	fields: []struct_field,
     79 };
     80 
     81 // A single struct field.
     82 export type struct_field = struct {
     83 	// "" for an anonymous field
     84 	name: str,
     85 	offs: size,
     86 	_type: const *_type,
     87 };
     88 
     89 // A tagged union type, e.g. (int | uint | void).
     90 export type tagged = []const *_type;
     91 
     92 // A tuple type, e.g. (a, b, c)
     93 export type tuple = []tuple_value;
     94 
     95 // A single value of a tuple type.
     96 export type tuple_value = struct {
     97 	offs: size,
     98 	_type: const *_type,
     99 };
    100 
    101 // Flags for a Hare type.
    102 export type flag = enum u8 {
    103 	// Keep me consistent with ast::type_flag
    104 
    105 	NONE = 0,
    106 	CONST = 1 << 0,
    107 	ERROR = 1 << 1,
    108 };
    109 
    110 // The sz field of [[_type]] is set to this value to indicate that the size of
    111 // the type is undefined.
    112 export def SIZE_UNDEFINED: size = -1: size;
    113 
    114 // A Hare type.
    115 export type _type = struct {
    116 	flags: flag,
    117 	repr: (alias | array | builtin | _enum | func
    118 		| pointer | slice | _struct | tagged | tuple),
    119 	id: u32,
    120 	sz: size,
    121 	_align: size,
    122 };