types.h (5541B)
1 #ifndef HARE_TYPES_H 2 #define HARE_TYPES_H 3 #include <stdbool.h> 4 #include <stdint.h> 5 #include "identifier.h" 6 7 enum type_storage { 8 // Built-in types 9 // The order of these is important 10 STORAGE_BOOL, 11 STORAGE_CHAR, 12 STORAGE_F32, 13 STORAGE_F64, 14 STORAGE_I16, 15 STORAGE_I32, 16 STORAGE_I64, 17 STORAGE_I8, 18 STORAGE_INT, 19 STORAGE_NULL, 20 STORAGE_RUNE, 21 STORAGE_SIZE, 22 STORAGE_STRING, 23 STORAGE_U16, 24 STORAGE_U32, 25 STORAGE_U64, 26 STORAGE_U8, 27 STORAGE_UINT, 28 STORAGE_UINTPTR, 29 STORAGE_VOID, 30 // Other types 31 STORAGE_ALIAS, 32 STORAGE_ARRAY, 33 STORAGE_ENUM, 34 STORAGE_FUNCTION, 35 STORAGE_POINTER, 36 STORAGE_SLICE, 37 STORAGE_STRUCT, 38 STORAGE_TAGGED, 39 STORAGE_TUPLE, 40 STORAGE_UNION, 41 STORAGE_VALIST, 42 STORAGE_FCONST, 43 STORAGE_ICONST, 44 STORAGE_RCONST, 45 // For internal use only 46 STORAGE_ERROR, 47 }; 48 49 struct type; 50 51 #define SIZE_UNDEFINED ((size_t)-1) 52 #define ALIGN_UNDEFINED ((size_t)-1) 53 54 struct type_alias { 55 struct identifier ident; 56 struct identifier name; 57 const struct type *type; 58 bool exported; // Used to make sure unexported aliases aren't emitted 59 }; 60 61 struct type_array { 62 size_t length; // SIZE_UNDEFINED for [*] and slices 63 const struct type *members; 64 bool expandable; 65 }; 66 67 struct type_enum { 68 struct scope *values; 69 }; 70 71 enum variadism { 72 VARIADISM_NONE, 73 VARIADISM_C, 74 VARIADISM_HARE, 75 }; 76 77 enum function_flags { 78 FN_NORETURN = 1 << 0, 79 }; 80 81 struct type_func_param { 82 const struct type *type; 83 struct type_func_param *next; 84 }; 85 86 struct type_func { 87 const struct type *result; 88 enum variadism variadism; 89 struct type_func_param *params; 90 unsigned int flags; // enum function_flags 91 }; 92 93 struct type_const { 94 intmax_t min, max; 95 uint32_t id; 96 const struct type ***refs; 97 size_t nrefs; 98 size_t zrefs; 99 }; 100 101 enum pointer_flags { 102 PTR_NULLABLE = 1 << 0, 103 }; 104 105 struct type_pointer { 106 const struct type *referent; 107 unsigned int flags; 108 }; 109 110 struct struct_field { 111 char *name; 112 const struct type *type; 113 size_t offset; 114 size_t size; 115 struct struct_field *next; 116 }; 117 118 struct type_struct_union { 119 struct struct_field *fields; 120 // c_compat is false if explicit offsets are used, or if the type is a 121 // union. The latter is actually still C-compatible, but this is an 122 // implementation detail which changes the way the QBE IL is generated, 123 // and in the case of unions, the altered behavior for explicit-offset 124 // structs is also correct for all cases of unions. 125 bool c_compat; 126 bool packed; 127 }; 128 129 struct type_tuple { 130 const struct type *type; 131 size_t offset; 132 struct type_tuple *next; 133 }; 134 135 struct type_tagged_union { 136 const struct type *type; 137 struct type_tagged_union *next; 138 }; 139 140 enum type_flags { 141 TYPE_CONST = 1 << 0, 142 TYPE_ERROR = 1 << 1, 143 }; 144 145 struct type { 146 enum type_storage storage; 147 uint32_t id; 148 unsigned int flags; 149 size_t size, align; 150 union { 151 struct { 152 struct type_alias alias; 153 struct type_enum _enum; 154 }; 155 struct type_array array; 156 struct type_func func; 157 struct type_const _const; 158 struct type_pointer pointer; 159 struct type_struct_union struct_union; 160 struct type_tagged_union tagged; 161 struct type_tuple tuple; 162 }; 163 }; 164 165 struct dimensions { 166 size_t size; 167 size_t align; 168 }; 169 170 const struct type *type_dereference(const struct type *type); 171 const struct type *type_dealias(const struct type *type); 172 const struct struct_field *type_get_field( 173 const struct type *type, const char *name); 174 const struct type_tuple *type_get_value( 175 const struct type *type, uintmax_t index); 176 177 const struct type *tagged_select_subtype( 178 const struct type *tagged, const struct type *subtype, bool strip); 179 bool tagged_subset_compat(const struct type *to, const struct type *from); 180 181 const char *type_storage_unparse(enum type_storage storage); 182 bool type_is_signed(const struct type *type); 183 bool type_is_integer(const struct type *type); 184 bool type_is_numeric(const struct type *type); 185 bool type_is_float(const struct type *type); 186 bool type_is_constant(const struct type *type); 187 bool type_has_error(const struct type *type); 188 189 uint32_t type_hash(const struct type *type); 190 191 const struct type *promote_const(const struct type *a, const struct type *b); 192 bool type_is_assignable(const struct type *to, const struct type *from); 193 const struct type *type_is_castable(const struct type *to, const struct type *from); 194 bool type_is_complete(const struct type *type); 195 196 const struct type *type_create_const(enum type_storage storage, 197 intmax_t min, intmax_t max); 198 const struct type *lower_const(const struct type *old, const struct type *new); 199 void const_refer(const struct type *type, const struct type **ref); 200 201 void builtin_types_init(const char *target); 202 203 // Built-in type singletons 204 extern struct type 205 // Primitive 206 builtin_type_bool, 207 builtin_type_char, 208 builtin_type_error, 209 builtin_type_f32, 210 builtin_type_f64, 211 builtin_type_i8, 212 builtin_type_i16, 213 builtin_type_i32, 214 builtin_type_i64, 215 builtin_type_int, 216 builtin_type_u8, 217 builtin_type_u16, 218 builtin_type_u32, 219 builtin_type_u64, 220 builtin_type_uint, 221 builtin_type_uintptr, 222 builtin_type_null, 223 builtin_type_rune, 224 builtin_type_size, 225 builtin_type_void, 226 // Const primitives 227 builtin_type_const_bool, 228 builtin_type_const_char, 229 builtin_type_const_f32, 230 builtin_type_const_f64, 231 builtin_type_const_i8, 232 builtin_type_const_i16, 233 builtin_type_const_i32, 234 builtin_type_const_i64, 235 builtin_type_const_int, 236 builtin_type_const_u8, 237 builtin_type_const_u16, 238 builtin_type_const_u32, 239 builtin_type_const_u64, 240 builtin_type_const_uint, 241 builtin_type_const_uintptr, 242 builtin_type_const_rune, 243 builtin_type_const_size, 244 builtin_type_const_void, 245 // etc 246 builtin_type_ptr_const_char, 247 builtin_type_str, 248 builtin_type_const_str, 249 builtin_type_valist; 250 251 #endif