harec

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

ast.h (8543B)


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