1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
#ifndef HARE_AST_H
#define HARE_AST_H
#include <stdbool.h>
#include <stdint.h>
#include "expr.h"
#include "identifier.h"
#include "lex.h"
#include "types.h"
struct ast_type;
enum ast_import_mode {
IMPORT_NORMAL, // use foo::bar;
IMPORT_ALIAS, // use foo = bar::baz;
IMPORT_MEMBERS, // use foo::{bar, baz};
IMPORT_WILDCARD, // use foo::bar::*;
};
struct ast_import_members {
struct location loc;
struct ident *name;
struct ast_import_members *next;
};
struct ast_imports {
enum ast_import_mode mode;
struct ident *ident;
union {
const char *alias;
struct ast_import_members *members;
};
struct ast_imports *next;
};
struct ast_array_type {
struct ast_expression *length; // NULL for unbounded arrays
struct ast_type *members;
bool contextual;
};
struct ast_slice_type {
struct ast_type *members;
};
struct ast_enum_field {
struct location loc;
struct ident *name;
struct ast_expression *value;
struct ast_enum_field *next;
};
struct ast_enum_type {
enum type_storage storage;
struct ast_enum_field *values;
};
struct ast_function_parameters {
struct location loc;
struct ident *name;
struct ast_type *type;
struct ast_expression *default_value;
struct ast_function_parameters *next;
};
struct ast_function_type {
struct ast_type *result;
struct ast_function_parameters *params;
enum variadism variadism;
};
struct ast_pointer_type {
struct ast_type *referent;
bool nullable;
};
struct ast_tagged_union_type {
struct ast_type *type;
bool unwrap;
struct ast_tagged_union_type *next;
};
struct ast_tuple_type {
struct ast_type *type;
struct ast_tuple_type *next;
};
struct ast_struct_union_field {
struct ast_struct_union_field *next;
const char *name; // null if embed, may be "_"
struct ast_type *type;
};
struct ast_struct_union_type {
struct ast_struct_union_field fields;
bool packed;
};
struct ast_type {
struct location loc;
enum type_storage storage;
union {
struct ast_array_type array;
struct ast_type *error;
struct ast_function_type func;
struct ast_pointer_type pointer;
struct ast_slice_type slice;
struct ast_struct_union_type struct_union;
struct ast_tagged_union_type tagged;
struct ast_tuple_type tuple;
struct {
struct ident *alias;
// Only valid for enums
struct ast_enum_type _enum;
};
};
};
struct ast_types {
const struct ast_type *type;
struct ast_types *next;
};
struct ast_expression_list {
struct ast_expression *expr;
struct ast_expression_list *next;
};
struct ast_expression_access {
enum access_type type;
union {
struct ident *ident;
struct {
struct ast_expression *array;
struct ast_expression *index;
};
struct {
struct ast_expression *_struct;
const char *field;
};
struct {
struct ast_expression *tuple;
struct ast_expression *value;
};
};
};
struct ast_expression_alloc {
enum alloc_kind kind;
struct ast_expression *init;
struct ast_expression *cap;
};
struct ast_expression_append {
struct ast_expression *object;
struct ast_expression *value;
struct ast_expression *length;
bool is_static, is_multi;
};
struct ast_expression_assert {
struct ast_expression *cond;
struct ast_expression *message;
bool is_static;
};
struct ast_expression_assign {
enum binarithm_operator op;
// object == NULL for discarding assignment (`_ = foo`)
struct ast_expression *object, *value;
};
struct ast_expression_binarithm {
enum binarithm_operator op;
struct ast_expression *lvalue, *rvalue;
};
struct ast_binding_names {
struct ident *name; // NULL for _
struct ast_binding_names *next;
};
struct ast_expression_binding {
// more than one name means tuple unpacking,
// otherwise it's a regular binding
struct ast_binding_names names;
struct ast_type *type;
bool is_static;
struct ast_expression *initializer;
struct ast_expression_binding *next;
};
struct ast_expression_call {
struct ast_expression *lvalue;
struct ast_expression_list *args;
bool variadic; // last argument is a variadic argument list
};
struct ast_expression_cast {
enum cast_kind kind;
struct ast_expression *value;
struct ast_type *type;
};
struct ast_expression_literal {
enum type_storage storage;
union {
int64_t ival;
uint64_t uval;
double fval;
uint32_t rune;
bool bval;
struct {
size_t len;
char *value;
} string;
struct {
struct ast_expression_list *exprs;
bool expand;
} array;
};
};
struct ast_expression_control {
const char *label; // Never set for return.
struct ast_expression *value; // Never set for continue
};
struct ast_expression_defer {
struct ast_expression *deferred;
};
struct ast_expression_delete {
struct ast_expression *expr;
bool is_static;
};
struct ast_expression_for {
enum for_kind kind;
const char *label;
struct ast_expression *bindings;
struct ast_expression *cond;
struct ast_expression *afterthought;
struct ast_expression *body;
struct ast_expression *else_branch;
};
struct ast_expression_free {
struct ast_expression *expr;
};
struct ast_expression_if {
struct ast_expression *cond;
struct ast_expression *true_branch, *false_branch;
};
struct ast_expression_compound {
const char *label;
struct location label_loc;
struct ast_expression_list list;
};
struct ast_match_case {
struct ident *name; // May be null
struct ast_type *type;
struct ast_expression_list exprs;
struct ast_match_case *next;
};
struct ast_expression_match {
const char *label;
struct ast_expression *value;
struct ast_match_case *cases;
};
enum measure_operator {
M_ALIGN,
M_LEN,
M_SIZE,
M_OFFSET,
};
struct ast_expression_measure {
enum measure_operator op;
union {
struct ast_expression *value;
struct ast_type *type;
// TODO: Field selection
};
};
struct ast_expression_propagate {
struct ast_expression *value;
bool abort;
};
struct ast_expression_slice {
struct ast_expression *object;
struct ast_expression *start, *end;
};
struct ast_case_option {
struct ast_expression *value;
struct ast_case_option *next;
};
struct ast_switch_case {
struct ast_case_option *options; // NULL for *
struct ast_expression_list exprs;
struct ast_switch_case *next;
};
struct ast_expression_switch {
const char *label;
struct ast_expression *value;
struct ast_switch_case *cases;
};
struct ast_field_value {
const char *name;
struct ast_type *type;
struct ast_expression *initializer;
struct ast_field_value *next;
};
struct ast_expression_struct {
bool autofill;
bool undefined;
struct ident *type;
struct ast_field_value *fields;
};
struct ast_expression_tuple {
struct ast_expression *expr;
struct ast_expression_tuple *next;
};
struct ast_expression_unarithm {
enum unarithm_operator op;
struct ast_expression *operand;
};
struct ast_expression_vaarg {
struct ast_expression *ap;
struct ast_type *type;
};
struct ast_expression {
struct location loc;
enum expr_type type;
union {
struct ast_expression_access access;
struct ast_expression_alloc alloc;
struct ast_expression_append append; // also insert
struct ast_expression_assert assert;
struct ast_expression_assign assign;
struct ast_expression_binarithm binarithm;
struct ast_expression_binding binding;
struct ast_expression_call call;
struct ast_expression_cast cast;
struct ast_expression_compound compound;
struct ast_expression_control control;
struct ast_expression_defer defer;
struct ast_expression_delete delete;
struct ast_expression_for _for;
struct ast_expression_free free;
struct ast_expression_if _if;
struct ast_expression_literal literal;
struct ast_expression_match match;
struct ast_expression_measure measure;
struct ast_expression_propagate propagate;
struct ast_expression_slice slice;
struct ast_expression_struct _struct;
struct ast_expression_switch _switch;
struct ast_expression_tuple tuple;
struct ast_expression_unarithm unarithm;
struct ast_expression_vaarg vaarg;
};
};
struct ast_global_decl {
const char *symbol;
bool threadlocal;
struct ident *ident;
struct ast_type *type;
struct ast_expression *init;
};
struct ast_type_decl {
struct ident *ident;
struct ast_type *type;
};
enum func_decl_flags {
FN_FINI = 1 << 0,
FN_INIT = 1 << 1,
FN_TEST = 1 << 2,
};
struct ast_function_decl {
const char *symbol;
struct ident *ident;
struct ast_function_type prototype;
struct ast_expression *body;
enum func_decl_flags flags;
};
enum ast_decl_type {
ADECL_FUNC,
ADECL_TYPE,
ADECL_GLOBAL,
ADECL_CONST,
ADECL_ASSERT,
};
struct ast_decl {
struct location loc;
enum ast_decl_type decl_type;
bool exported;
union {
struct ast_global_decl global;
struct ast_global_decl constant;
struct ast_type_decl type;
struct ast_function_decl function;
struct ast_expression_assert assert;
};
};
struct ast_decls {
struct ast_decl decl;
struct ast_decls *next;
};
struct ast_subunit {
struct ast_imports *imports;
struct ast_decls *decls;
struct ast_subunit *next;
};
struct ast_unit {
struct ident *ns;
struct ast_subunit subunits;
};
#endif
|