harec

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

expr.h (7091B)


      1 #ifndef HAREC_EXPR_H
      2 #define HAREC_EXPR_H
      3 #include <stdint.h>
      4 #include "identifier.h"
      5 #include "lex.h"
      6 #include "types.h"
      7 
      8 struct scope;
      9 struct scope_object;
     10 
     11 enum expr_type {
     12 	EXPR_ACCESS,
     13 	EXPR_ALLOC,
     14 	EXPR_APPEND,
     15 	EXPR_ASSERT,
     16 	EXPR_ASSIGN,
     17 	EXPR_BINARITHM,
     18 	EXPR_BINDING,
     19 	EXPR_BREAK,
     20 	EXPR_CALL,
     21 	EXPR_CAST,
     22 	EXPR_COMPOUND,
     23 	EXPR_CONSTANT,
     24 	EXPR_CONTINUE,
     25 	EXPR_DEFER,
     26 	EXPR_DELETE,
     27 	EXPR_FOR,
     28 	EXPR_FREE,
     29 	EXPR_IF,
     30 	EXPR_INSERT,
     31 	EXPR_MATCH,
     32 	EXPR_MEASURE,
     33 	EXPR_PROPAGATE,
     34 	EXPR_RETURN,
     35 	EXPR_SLICE,
     36 	EXPR_STRUCT,
     37 	EXPR_SWITCH,
     38 	EXPR_TUPLE,
     39 	EXPR_UNARITHM,
     40 	EXPR_VAARG,
     41 	EXPR_VAEND,
     42 	EXPR_VASTART,
     43 	EXPR_YIELD,
     44 };
     45 
     46 struct expressions {
     47 	struct expression *expr;
     48 	struct expressions *next;
     49 };
     50 
     51 enum access_type {
     52 	ACCESS_IDENTIFIER,
     53 	ACCESS_INDEX,
     54 	ACCESS_FIELD,
     55 	ACCESS_TUPLE,
     56 };
     57 
     58 struct expression_access {
     59 	enum access_type type;
     60 	union {
     61 		const struct scope_object *object;
     62 		struct {
     63 			struct expression *array;
     64 			struct expression *index;
     65 		};
     66 		struct {
     67 			struct expression *_struct;
     68 			const struct struct_field *field;
     69 		};
     70 		struct {
     71 			struct expression *tuple;
     72 			const struct type_tuple *tvalue;
     73 			size_t tindex;
     74 		};
     75 	};
     76 };
     77 
     78 enum alloc_kind {
     79 	ALLOC_OBJECT,	// alloc(42)
     80 	ALLOC_WITH_CAP,	// alloc([], 42)
     81 	ALLOC_WITH_LEN,	// alloc([0...], 42)
     82 	ALLOC_COPY,	// alloc(x...);
     83 };
     84 
     85 struct expression_alloc {
     86 	enum alloc_kind kind;
     87 	struct expression *init;
     88 	struct expression *cap;
     89 };
     90 
     91 struct expression_append {
     92 	struct expression *object;
     93 	struct expression *value;
     94 	struct expression *length;
     95 	bool is_static, is_multi;
     96 };
     97 
     98 struct expression_assert {
     99 	struct expression *cond;
    100 	struct expression *message;
    101 	bool is_static;
    102 };
    103 
    104 enum binarithm_operator {
    105 	BIN_BAND,	// &
    106 	BIN_BOR,	// |
    107 	BIN_DIV,	// /
    108 	BIN_GREATER,	// >
    109 	BIN_GREATEREQ,	// >=
    110 	BIN_LAND,	// &&
    111 	BIN_LEQUAL,	// ==
    112 	BIN_LESS,	// <
    113 	BIN_LESSEQ,	// <=
    114 	BIN_LOR,	// ||
    115 	BIN_LSHIFT,	// <<
    116 	BIN_LXOR,	// ^^
    117 	BIN_MINUS,	// -
    118 	BIN_MODULO,	// %
    119 	BIN_NEQUAL,	// !=
    120 	BIN_PLUS,	// +
    121 	BIN_RSHIFT,	// >>
    122 	BIN_TIMES,	// *
    123 	BIN_BXOR,	// ^
    124 };
    125 
    126 struct expression_assign {
    127 	enum binarithm_operator op;
    128 	struct expression *object, *value;
    129 	bool indirect;
    130 };
    131 
    132 struct expression_binarithm {
    133 	enum binarithm_operator op;
    134 	struct expression *lvalue, *rvalue;
    135 };
    136 
    137 struct binding_unpack {
    138 	const struct scope_object *object;
    139 	size_t offset;
    140 	struct binding_unpack *next;
    141 };
    142 
    143 struct expression_binding {
    144 	const struct scope_object *object;
    145 	struct binding_unpack *unpack;
    146 	struct expression *initializer;
    147 	struct expression_binding *next;
    148 };
    149 
    150 enum cast_kind {
    151 	C_CAST,
    152 	C_ASSERTION,
    153 	C_TEST,
    154 };
    155 
    156 struct expression_cast {
    157 	enum cast_kind kind;
    158 	const struct type *secondary;
    159 	struct expression *value;
    160 	bool lowered;
    161 };
    162 
    163 struct call_argument {
    164 	bool variadic;
    165 	struct expression *value;
    166 	struct call_argument *next;
    167 };
    168 
    169 struct expression_call {
    170 	struct expression *lvalue;
    171 	struct call_argument *args;
    172 };
    173 
    174 struct expression_compound {
    175 	char *label;
    176 	struct scope *scope;
    177 	struct expressions exprs;
    178 };
    179 
    180 struct array_constant {
    181 	struct expression *value;
    182 	struct array_constant *next;
    183 };
    184 
    185 // Invariant: these are sorted by field offset
    186 struct struct_constant {
    187 	const struct struct_field *field;
    188 	struct expression *value;
    189 	struct struct_constant *next;
    190 };
    191 
    192 struct tuple_constant {
    193 	const struct type_tuple *field;
    194 	struct expression *value;
    195 	struct tuple_constant *next;
    196 };
    197 
    198 struct tagged_constant {
    199 	const struct type *tag;
    200 	struct expression *value;
    201 };
    202 
    203 struct expression_constant {
    204 	// If non-null, ival is an offset from this object's address
    205 	const struct scope_object *object;
    206 	union {
    207 		bool bval;
    208 		double fval;
    209 		intmax_t ival;
    210 		uintmax_t uval;
    211 		uint32_t rune;
    212 		struct {
    213 			size_t len;
    214 			char *value;
    215 		} string;
    216 		struct array_constant *array;
    217 		struct struct_constant *_struct;
    218 		struct tuple_constant *tuple;
    219 		struct tagged_constant tagged;
    220 	};
    221 };
    222 
    223 struct expression_control {
    224 	char *label;
    225 	const struct scope *scope;
    226 	struct expression *value; // Only set for yield
    227 };
    228 
    229 struct expression_defer {
    230 	struct expression *deferred;
    231 };
    232 
    233 struct expression_delete {
    234 	struct expression *expr;
    235 	bool is_static;
    236 };
    237 
    238 struct expression_for {
    239 	struct scope *scope;
    240 	struct expression *bindings;
    241 	struct expression *cond;
    242 	struct expression *afterthought;
    243 	struct expression *body;
    244 };
    245 
    246 struct expression_free {
    247 	struct expression *expr;
    248 };
    249 
    250 struct expression_if {
    251 	struct expression *cond;
    252 	struct expression *true_branch, *false_branch;
    253 };
    254 
    255 struct match_case {
    256 	const struct scope_object *object;	// NULL if not bound
    257 	const struct type *type;		// NULL if default
    258 	struct expression *value;
    259 	struct match_case *next;
    260 };
    261 
    262 struct expression_match {
    263 	struct expression *value;
    264 	struct match_case *cases;
    265 };
    266 
    267 enum measure_operator {
    268 	M_LEN,
    269 	M_SIZE,
    270 	M_OFFSET,
    271 };
    272 
    273 struct expression_measure {
    274 	enum measure_operator op;
    275 	union {
    276 		struct expression *value;
    277 		struct dimensions dimensions;
    278 		// TODO: Field selection
    279 	};
    280 };
    281 
    282 struct expression_propagate {
    283 	struct expression *value;
    284 	bool abort;
    285 };
    286 
    287 struct expression_return {
    288 	struct expression *value;
    289 };
    290 
    291 struct expression_slice {
    292 	struct expression *object;
    293 	struct expression *start, *end;
    294 };
    295 
    296 struct case_option {
    297 	struct expression *value;
    298 	struct case_option *next;
    299 };
    300 
    301 struct switch_case {
    302 	struct case_option *options; // NULL for default case
    303 	struct expression *value;
    304 	struct switch_case *next;
    305 };
    306 
    307 struct expression_switch {
    308 	struct expression *value;
    309 	struct switch_case *cases;
    310 };
    311 
    312 struct expr_struct_field {
    313 	const struct struct_field *field;
    314 	struct expression *value;
    315 	struct expr_struct_field *next;
    316 };
    317 
    318 struct expression_struct {
    319 	struct expr_struct_field *fields;
    320 	bool autofill;
    321 };
    322 
    323 struct expression_tuple {
    324 	struct expression *value;
    325 	struct expression_tuple *next;
    326 };
    327 
    328 enum unarithm_operator {
    329 	UN_ADDRESS,	// &
    330 	UN_BNOT,	// ~
    331 	UN_DEREF,	// *
    332 	UN_LNOT,	// !
    333 	UN_MINUS,	// -
    334 	UN_PLUS,	// +
    335 };
    336 
    337 struct expression_unarithm {
    338 	enum unarithm_operator op;
    339 	struct expression *operand;
    340 };
    341 
    342 struct expression_vaarg {
    343 	struct expression *ap;
    344 };
    345 
    346 struct expression {
    347 	const struct type *result;
    348 	enum expr_type type;
    349 	bool terminates;
    350 	struct location loc; // For fixed aborts
    351 	union {
    352 		struct expression_access access;
    353 		struct expression_alloc alloc;
    354 		struct expression_append append; // and insert
    355 		struct expression_assert assert;
    356 		struct expression_assign assign;
    357 		struct expression_binarithm binarithm;
    358 		struct expression_binding binding;
    359 		struct expression_call call;
    360 		struct expression_cast cast;
    361 		struct expression_compound compound;
    362 		struct expression_constant constant;
    363 		struct expression_defer defer;
    364 		struct expression_delete delete;
    365 		struct expression_control control;
    366 		struct expression_for _for;
    367 		struct expression_free free;
    368 		struct expression_if _if;
    369 		struct expression_match match;
    370 		struct expression_measure measure;
    371 		struct expression_propagate propagate;
    372 		struct expression_return _return;
    373 		struct expression_switch _switch;
    374 		struct expression_struct _struct;
    375 		struct expression_slice slice;
    376 		struct expression_tuple tuple;
    377 		struct expression_unarithm unarithm;
    378 		struct expression_vaarg vaarg;
    379 		void *user;
    380 	};
    381 };
    382 
    383 #endif