harec

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

expr.h (7131B)


      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 			bool bounds_checked;
     66 		};
     67 		struct {
     68 			struct expression *_struct;
     69 			const struct struct_field *field;
     70 		};
     71 		struct {
     72 			struct expression *tuple;
     73 			const struct type_tuple *tvalue;
     74 			size_t tindex;
     75 		};
     76 	};
     77 };
     78 
     79 enum alloc_kind {
     80 	ALLOC_OBJECT,	// alloc(42)
     81 	ALLOC_WITH_CAP,	// alloc([], 42)
     82 	ALLOC_WITH_LEN,	// alloc([0...], 42)
     83 	ALLOC_COPY,	// alloc(x...);
     84 };
     85 
     86 struct expression_alloc {
     87 	enum alloc_kind kind;
     88 	struct expression *init;
     89 	struct expression *cap;
     90 };
     91 
     92 struct expression_append {
     93 	struct expression *object;
     94 	struct expression *value;
     95 	struct expression *length;
     96 	bool is_static, is_multi;
     97 };
     98 
     99 struct expression_assert {
    100 	struct expression *cond;
    101 	struct expression *message;
    102 	bool is_static;
    103 };
    104 
    105 enum binarithm_operator {
    106 	BIN_BAND,	// &
    107 	BIN_BOR,	// |
    108 	BIN_DIV,	// /
    109 	BIN_GREATER,	// >
    110 	BIN_GREATEREQ,	// >=
    111 	BIN_LAND,	// &&
    112 	BIN_LEQUAL,	// ==
    113 	BIN_LESS,	// <
    114 	BIN_LESSEQ,	// <=
    115 	BIN_LOR,	// ||
    116 	BIN_LSHIFT,	// <<
    117 	BIN_LXOR,	// ^^
    118 	BIN_MINUS,	// -
    119 	BIN_MODULO,	// %
    120 	BIN_NEQUAL,	// !=
    121 	BIN_PLUS,	// +
    122 	BIN_RSHIFT,	// >>
    123 	BIN_TIMES,	// *
    124 	BIN_BXOR,	// ^
    125 };
    126 
    127 struct expression_assign {
    128 	enum binarithm_operator op;
    129 	struct expression *object, *value;
    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_ALIGN,
    269 	M_LEN,
    270 	M_SIZE,
    271 	M_OFFSET,
    272 };
    273 
    274 struct expression_measure {
    275 	enum measure_operator op;
    276 	union {
    277 		struct expression *value;
    278 		struct dimensions dimensions;
    279 		// TODO: Field selection
    280 	};
    281 };
    282 
    283 struct expression_propagate {
    284 	struct expression *value;
    285 	bool abort;
    286 };
    287 
    288 struct expression_return {
    289 	struct expression *value;
    290 };
    291 
    292 struct expression_slice {
    293 	struct expression *object;
    294 	struct expression *start, *end;
    295 	bool bounds_checked;
    296 };
    297 
    298 struct case_option {
    299 	struct expression *value;
    300 	struct case_option *next;
    301 };
    302 
    303 struct switch_case {
    304 	struct case_option *options; // NULL for default case
    305 	struct expression *value;
    306 	struct switch_case *next;
    307 };
    308 
    309 struct expression_switch {
    310 	struct expression *value;
    311 	struct switch_case *cases;
    312 };
    313 
    314 struct expr_struct_field {
    315 	const struct struct_field *field;
    316 	struct expression *value;
    317 	struct expr_struct_field *next;
    318 };
    319 
    320 struct expression_struct {
    321 	struct expr_struct_field *fields;
    322 	bool autofill;
    323 };
    324 
    325 struct expression_tuple {
    326 	struct expression *value;
    327 	struct expression_tuple *next;
    328 };
    329 
    330 enum unarithm_operator {
    331 	UN_ADDRESS,	// &
    332 	UN_BNOT,	// ~
    333 	UN_DEREF,	// *
    334 	UN_LNOT,	// !
    335 	UN_MINUS,	// -
    336 	UN_PLUS,	// +
    337 };
    338 
    339 struct expression_unarithm {
    340 	enum unarithm_operator op;
    341 	struct expression *operand;
    342 };
    343 
    344 struct expression_vaarg {
    345 	struct expression *ap;
    346 };
    347 
    348 struct expression {
    349 	const struct type *result;
    350 	enum expr_type type;
    351 	bool terminates;
    352 	struct location loc; // For fixed aborts
    353 	union {
    354 		struct expression_access access;
    355 		struct expression_alloc alloc;
    356 		struct expression_append append; // and insert
    357 		struct expression_assert assert;
    358 		struct expression_assign assign;
    359 		struct expression_binarithm binarithm;
    360 		struct expression_binding binding;
    361 		struct expression_call call;
    362 		struct expression_cast cast;
    363 		struct expression_compound compound;
    364 		struct expression_constant constant;
    365 		struct expression_defer defer;
    366 		struct expression_delete delete;
    367 		struct expression_control control;
    368 		struct expression_for _for;
    369 		struct expression_free free;
    370 		struct expression_if _if;
    371 		struct expression_match match;
    372 		struct expression_measure measure;
    373 		struct expression_propagate propagate;
    374 		struct expression_return _return;
    375 		struct expression_switch _switch;
    376 		struct expression_struct _struct;
    377 		struct expression_slice slice;
    378 		struct expression_tuple tuple;
    379 		struct expression_unarithm unarithm;
    380 		struct expression_vaarg vaarg;
    381 		void *user;
    382 	};
    383 };
    384 
    385 #endif