types.ha (2726B)
1 // License: MPL-2.0 2 // (c) 2021 Alexey Yerin <yyp@disroot.org> 3 // (c) 2021 Drew DeVault <sir@cmpwn.com> 4 // (c) 2021 Ember Sawady <ecs@d2evs.net> 5 use hare::ast; 6 7 // A type alias. 8 export type alias = struct { 9 id: ast::ident, 10 // null for forward referenced types 11 secondary: const nullable *_type, 12 }; 13 14 // An array type, e.g. [10]int 15 export type array = struct { 16 // [[SIZE_UNDEFINED]] for [*]type 17 length: size, 18 member: const *_type, 19 }; 20 21 // A built-in primitive type (int, bool, str, etc). 22 export type builtin = enum u8 { 23 // Keep me consistent with ast::builtin 24 25 BOOL, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NULL, 26 RUNE, SIZE, STR, U16, U32, U64, U8, UINT, UINTPTR, 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 // Indicats if a [[func]] has the @noreturn attribute 43 export type func_flag = enum uint { 44 NONE = 0, 45 NORETURN = 1 << 0, 46 }; 47 48 // A function type, e.g. fn(x: int, y: int) int 49 export type func = struct { 50 result: const *_type, 51 variadism: variadism, 52 flags: func_flag, 53 params: []const *_type, 54 }; 55 56 // Flags which apply to a pointer type. 57 export type pointer_flag = enum u8 { 58 // Keep me consistent with ast::pointer_flag 59 60 NONE = 0, 61 NULLABLE = 1 << 0, 62 }; 63 64 // *int 65 export type pointer = struct { 66 referent: const *_type, 67 flags: pointer_flag, 68 }; 69 70 // []int 71 export type slice = const *_type; 72 73 // Indicates if a [[_struct]] was declared as a struct or union type. 74 export type struct_union = enum { 75 STRUCT, 76 UNION, 77 }; 78 79 // struct { ... } or union { ... } 80 // 81 // Note that embedded anonymous structs will have been merged into their parent 82 // type. 83 export type _struct = struct { 84 kind: struct_union, 85 fields: []struct_field, 86 }; 87 88 // A single struct field. 89 export type struct_field = struct { 90 // "" for an anonymous field 91 name: str, 92 offs: size, 93 _type: const *_type, 94 }; 95 96 // A tagged union type, e.g. (int | uint | void). 97 export type tagged = []const *_type; 98 99 // A tuple type, e.g. (a, b, c) 100 export type tuple = []tuple_value; 101 102 // A single value of a tuple type. 103 export type tuple_value = struct { 104 offs: size, 105 _type: const *_type, 106 }; 107 108 // Flags for a Hare type. 109 export type flag = enum u8 { 110 // Keep me consistent with ast::type_flag 111 112 NONE = 0, 113 CONST = 1 << 0, 114 ERROR = 1 << 1, 115 }; 116 117 // The sz field of [[_type]] is set to this value to indicate that the size of 118 // the type is undefined. 119 export def SIZE_UNDEFINED: size = -1: size; 120 121 // A Hare type. 122 export type _type = struct { 123 flags: flag, 124 repr: (alias | array | builtin | _enum | func 125 | pointer | slice | _struct | tagged | tuple), 126 id: u32, 127 sz: size, 128 _align: size, 129 };