harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

ast.h (8636B)


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