-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.h
625 lines (493 loc) · 12.1 KB
/
parse.h
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#ifndef PARSE_H
#define PARSE_H
#include "alloc.h"
#include "lex.h"
#include "log.h"
#include "util.h"
/* Well known types - keywords (`int`, `unsigned`, etc) */
enum well_known_ty {
// parser.c relies on the unsigned variant being the signed variant +1
// ir.c relies on the sizes being ascending
WELL_KNOWN_TY_SIGNED_CHAR,
WELL_KNOWN_TY_UNSIGNED_CHAR,
WELL_KNOWN_TY_SIGNED_SHORT,
WELL_KNOWN_TY_UNSIGNED_SHORT,
WELL_KNOWN_TY_SIGNED_INT,
WELL_KNOWN_TY_UNSIGNED_INT,
WELL_KNOWN_TY_SIGNED_LONG,
WELL_KNOWN_TY_UNSIGNED_LONG,
WELL_KNOWN_TY_SIGNED_LONG_LONG,
WELL_KNOWN_TY_UNSIGNED_LONG_LONG,
};
#define WKT_MAKE_SIGNED(wkt) ((wkt) & ~1)
#define WKT_MAKE_UNSIGNED(wkt) ((wkt) | 1)
#define WKT_IS_SIGNED(wkt) (((wkt) & 1) == 0)
/* Type refs - `<enum|struct|union> <identifier`, `<typedef-name>`, or
* `<keyword>` */
struct ast_ty_pointer {
struct ast_tyref *underlying;
};
enum ast_ty_array_ty {
AST_TY_ARRAY_TY_UNKNOWN_SIZE,
AST_TY_ARRAY_TY_KNOWN_SIZE,
};
struct ast_ty_array {
enum ast_ty_array_ty ty;
struct ast_tyref *element;
union {
size_t size;
};
};
struct ast_struct_field {
const char *name;
struct ast_tyref *var_ty;
};
struct ast_ty_struct {
struct ast_struct_field *field_var_tys;
size_t num_field_var_tys;
};
struct ast_ty_union {
struct ast_struct_field *field_var_tys;
size_t num_field_var_tys;
};
struct ast_ty_func {
struct ast_tyref *ret_var_ty;
struct ast_tyref *param_var_tys;
size_t num_param_var_tys;
};
enum ast_tyref_ty {
/* Used for variables that were used without declaration and similar. Usually
an error */
AST_TYREF_TY_UNKNOWN,
AST_TYREF_TY_VOID,
AST_TYREF_TY_WELL_KNOWN,
AST_TYREF_TY_FUNC,
AST_TYREF_TY_POINTER,
AST_TYREF_TY_ARRAY,
AST_TYREF_TY_VARIADIC,
AST_TYREF_TY_STRUCT,
AST_TYREF_TY_UNION,
// AST_TYREF_TY_TYPEDEF_NAME,
// AST_TYREF_TY_ENUM,
};
// enum ast_type_storage_class_flags {
// AST_TYPE_STORAGE_CLASS_FLAG_NONE = 0,
// AST_TYPE_STORAGE_CLASS_FLAG_CONST = 1,
// AST_TYPE_STORAGE_CLASS_FLAG_VOLATILE = 2,
// };
enum ast_type_qualifier_flags {
AST_TYPE_QUALIFIER_FLAG_NONE = 0,
AST_TYPE_QUALIFIER_FLAG_CONST = 1,
AST_TYPE_QUALIFIER_FLAG_VOLATILE = 2,
};
// enum ast_type_specifiers_flags {
// AST_TYPE_QUALIFIER_FLAG_NONE,
// AST_TYPE_QUALIFIER_FLAG_VOID,
// AST_TYPE_QUALIFIER_FLAG_CHAR,
// AST_TYPE_QUALIFIER_FLAG_SHORT,
// AST_TYPE_QUALIFIER_FLAG_INT,
// AST_TYPE_QUALIFIER_FLAG_LONG,
// };
struct ast_tyref {
enum ast_tyref_ty ty;
enum ast_type_qualifier_flags type_qualifiers;
union {
enum well_known_ty well_known;
struct ast_ty_pointer pointer;
struct ast_ty_array array;
struct ast_ty_func func;
struct ast_ty_struct struct_ty;
struct ast_ty_union union_ty;
};
};
struct ast_arglist {
struct ast_expr *args;
size_t num_args;
};
/* Variable references */
struct ast_var {
struct token identifier;
int scope;
struct ast_tyref var_ty;
};
struct ast_param {
struct ast_tyref var_ty;
struct ast_var var;
};
struct ast_paramlist {
struct ast_param *params;
size_t num_params;
};
struct ast_funcsig {
struct ast_tyref var_ty;
struct token name;
struct ast_paramlist param_list;
};
/* Constant values (literals) */
struct ast_cnst {
struct ast_tyref cnst_ty;
union {
unsigned long long int_value;
const char *str_value;
};
};
/* Binary expressions - `a <OP> b` */
struct ast_expr;
enum ast_unary_op_ty {
AST_UNARY_OP_TY_PREFIX_INC,
AST_UNARY_OP_TY_PREFIX_DEC,
AST_UNARY_OP_TY_POSTFIX_INC,
AST_UNARY_OP_TY_POSTFIX_DEC,
AST_UNARY_OP_TY_PLUS,
AST_UNARY_OP_TY_MINUS,
AST_UNARY_OP_TY_LOGICAL_NOT,
AST_UNARY_OP_TY_NOT,
AST_UNARY_OP_TY_INDIRECTION,
AST_UNARY_OP_TY_SIZEOF,
AST_UNARY_OP_TY_ADDRESSOF,
AST_UNARY_OP_TY_ALIGNOF,
AST_UNARY_OP_TY_CAST,
};
struct ast_cast {
struct ast_tyref cast_ty;
};
struct ast_unary_op {
enum ast_unary_op_ty ty;
struct ast_tyref var_ty;
struct ast_expr *expr;
union {
struct ast_cast cast;
};
};
enum ast_binary_op_ty {
AST_BINARY_OP_TY_EQ,
AST_BINARY_OP_TY_NEQ,
AST_BINARY_OP_TY_GT,
AST_BINARY_OP_TY_GTEQ,
AST_BINARY_OP_TY_LT,
AST_BINARY_OP_TY_LTEQ,
AST_BINARY_OP_TY_OR,
AST_BINARY_OP_TY_XOR,
AST_BINARY_OP_TY_AND,
AST_BINARY_OP_TY_LSHIFT,
AST_BINARY_OP_TY_RSHIFT,
AST_BINARY_OP_TY_ADD,
AST_BINARY_OP_TY_SUB,
AST_BINARY_OP_TY_MUL,
AST_BINARY_OP_TY_DIV,
AST_BINARY_OP_TY_QUOT
};
struct ast_binary_op {
enum ast_binary_op_ty ty;
struct ast_tyref var_ty;
struct ast_expr *lhs;
struct ast_expr *rhs;
};
struct ast_call {
struct ast_tyref var_ty;
struct ast_expr *target;
struct ast_arglist arg_list;
};
/* Compound expr - comma seperated expressions with well-defined order of
* execution */
struct ast_compoundexpr {
struct ast_expr *exprs;
size_t num_exprs;
};
/* atom - expression which is entirely isolated and not affected by other
* operators in how it is parsed */
struct ast_initlist {
struct ast_expr *exprs;
size_t num_exprs;
};
// Assignments - anything of form `<lvalue> = <lvalue | rvalue>` (so `<lvalue>
// = <expr>`)
enum ast_assg_ty {
AST_ASSG_TY_SIMPLEASSG,
AST_ASSG_TY_COMPOUNDASSG // e.g +=
};
struct ast_assg_compound_assg {
// the type of the intermediate
// e.g `int a = 0; a += 10l`
// is equiv to `int a = 0; a = a + 10l`
// so the resulting type is `int` but the intermediate type is `long`
struct ast_tyref intermediate_var_ty;
enum ast_binary_op_ty binary_op_ty;
};
struct ast_assg {
enum ast_assg_ty ty;
struct ast_tyref var_ty;
struct ast_expr *assignee;
struct ast_expr *expr;
union {
struct ast_assg_compound_assg compound_assg;
};
};
struct ast_arrayaccess {
// The lhs will always be an array/pointer type
// rhs may be integral type
struct ast_expr *lhs;
struct ast_expr *rhs;
};
struct ast_memberaccess {
struct ast_expr *lhs;
struct token member;
};
struct ast_pointeraccess {
struct ast_expr *lhs;
struct token member;
};
enum ast_sizeof_ty {
AST_SIZEOF_TY_TYPE,
AST_SIZEOF_TY_EXPR,
};
struct ast_sizeof {
enum ast_sizeof_ty ty;
union {
struct ast_expr *expr;
struct ast_tyref ty_ref;
};
};
struct ast_alignof {
struct ast_tyref ty_ref;
};
/* Expressions - divided into `lvalue` (can be on left hand side of assignment)
* and `rvalue` (not an lvalue) */
enum ast_expr_ty {
AST_EXPR_TY_CALL,
AST_EXPR_TY_UNARY_OP,
AST_EXPR_TY_BINARY_OP,
AST_EXPR_TY_ARRAYACCESS,
AST_EXPR_TY_MEMBERACCESS,
AST_EXPR_TY_POINTERACCESS,
AST_EXPR_TY_INIT_LIST, // brace list
AST_EXPR_TY_ASSG, // while assignments are of the form `lvalue = rvalue`,
// they themselves evaluate to an rvalue (unlike in C++)
AST_EXPR_TY_VAR,
AST_EXPR_TY_CNST,
AST_EXPR_TY_COMPOUNDEXPR,
AST_EXPR_TY_SIZEOF,
AST_EXPR_TY_ALIGNOF,
};
struct ast_expr {
enum ast_expr_ty ty;
struct ast_tyref var_ty;
union {
struct ast_sizeof size_of;
struct ast_alignof align_of;
struct ast_var var;
struct ast_cnst cnst;
struct ast_compoundexpr
compound_expr; // compound assignments are *never* lvalues in C
struct ast_unary_op unary_op;
struct ast_binary_op binary_op;
struct ast_assg assg;
struct ast_call call;
struct ast_arrayaccess array_access;
struct ast_memberaccess member_access;
struct ast_pointeraccess pointer_access;
struct ast_initlist init_list;
};
};
/* Variable declarations - `<typename> <comma seperated list of declarations>`
* where each declaration is `<name>` or `<name> = <expr>` */
enum ast_vardecl_ty {
AST_VARDECL_TY_DECL,
AST_VARDECL_TY_DECL_WITH_ASSG,
};
struct ast_vardecl {
enum ast_vardecl_ty ty;
struct ast_var var;
// int scope;
union {
struct ast_expr assg_expr;
};
};
struct ast_vardecllist {
struct ast_tyref var_ty;
struct ast_vardecl *decls;
size_t num_decls;
};
/* Jump statements - `return`, `break`, `continue`, `goto` */
struct ast_returnstmt {
struct ast_tyref var_ty;
struct ast_expr *expr;
};
enum ast_jumpstmt_ty {
// AST_JUMPSTMT_TY_GOTO,
// AST_JUMPSTMT_TY_BREAK,
// AST_JUMPSTMT_TY_CONTINUE,
AST_JUMPSTMT_TY_RETURN
};
struct ast_jumpstmt {
enum ast_jumpstmt_ty ty;
union {
struct ast_returnstmt return_stmt;
};
};
/* Statements - either declaration, labelled, expression, compound, jump,
* iteration, or selection */
enum ast_stmt_ty {
AST_STMT_TY_NULL,
AST_STMT_TY_VAR_DECL_LIST,
// AST_STMT_TY_LABELLED,
AST_STMT_TY_EXPR,
AST_STMT_TY_COMPOUND,
AST_STMT_TY_JUMP,
AST_STMT_TY_ITER,
AST_STMT_TY_SELECT,
};
struct ast_ifstmt {
struct ast_expr condition;
struct ast_stmt *body;
};
struct ast_ifelsestmt {
struct ast_expr condition;
struct ast_stmt *body;
struct ast_stmt *else_body;
};
struct ast_switchstmt {
struct ast_expr ctrl_var;
// TODO:
};
enum ast_selectstmt_ty {
AST_SELECTSTMT_TY_IF,
AST_SELECTSTMT_TY_IF_ELSE,
AST_SELECTSTMT_TY_SWITCH,
};
struct ast_selectstmt {
enum ast_selectstmt_ty ty;
union {
struct ast_ifstmt if_stmt;
struct ast_ifelsestmt if_else_stmt;
struct ast_switchstmt switch_stmt;
};
};
struct ast_stmt;
struct ast_compoundstmt {
struct ast_stmt *stmts;
size_t num_stmts;
};
struct ast_whilestmt {
struct ast_expr cond;
struct ast_stmt *body;
};
struct ast_dowhilestmt {
struct ast_expr cond;
struct ast_stmt *body;
};
struct ast_declorexpr {
struct ast_vardecllist *decl;
struct ast_expr *expr;
};
struct ast_forstmt {
struct ast_declorexpr *init;
struct ast_expr *cond;
struct ast_expr *iter;
struct ast_stmt *body;
};
enum ast_iterstmt_ty {
AST_ITERSTMT_TY_WHILE,
AST_ITERSTMT_TY_DO_WHILE,
AST_ITERSTMT_TY_FOR,
};
struct ast_iterstmt {
enum ast_iterstmt_ty ty;
union {
struct ast_whilestmt while_stmt;
struct ast_dowhilestmt do_while_stmt;
struct ast_forstmt for_stmt;
};
};
struct ast_stmt {
enum ast_stmt_ty ty;
union {
struct ast_vardecllist var_decl_list;
struct ast_expr expr;
struct ast_compoundstmt compound;
struct ast_jumpstmt jump;
struct ast_selectstmt select;
struct ast_iterstmt iter;
};
};
/* Struct definitions and declarations */
struct ast_field {
struct ast_tyref var_ty;
struct token identifier;
};
struct ast_fieldlist {
size_t num_fields;
struct ast_field *fields;
};
enum ast_type_ty {
AST_TYPE_TY_UNION,
AST_TYPE_TY_STRUCT,
};
struct ast_typedecl {
enum ast_type_ty ty;
struct token name;
};
struct ast_typedef {
enum ast_type_ty ty;
struct token name;
size_t num_field_lists;
struct ast_fieldlist *field_lists;
};
/* Enum definitions */
enum ast_enumcnst_ty {
AST_ENUMCNST_TY_EXPLICIT_VALUE,
AST_ENUMCNST_TY_IMPLICIT_VALUE,
};
struct ast_enumcnst {
struct token identifier;
enum ast_enumcnst_ty ty;
union {
unsigned long long value;
};
};
struct ast_enumdef {
struct token *name;
size_t num_enum_cnsts;
struct ast_enumcnst *enum_cnsts;
};
/* Function definitions and declarations */
struct ast_funcdef {
struct ast_funcsig sig;
struct ast_compoundstmt body;
};
struct ast_funcdecl {
struct ast_funcsig sig;
};
/* Translation unit (top level) */
struct ast_translationunit {
struct ast_typedef *type_defs;
size_t num_type_defs;
struct ast_typedecl *type_decls;
size_t num_type_decls;
struct ast_funcdef *func_defs;
size_t num_func_defs;
struct ast_funcdecl *func_decls;
size_t num_func_decls;
struct ast_enumdef *enum_defs;
size_t num_enum_defs;
};
struct parser;
enum parser_create_result {
PARSER_CREATE_RESULT_SUCCESS,
PARSER_CREATE_RESULT_FAILURE
};
struct parse_result {
struct ast_translationunit translation_unit;
};
enum parser_create_result parser_create(const char *program,
struct parser **parser);
struct parse_result parse(struct parser *parser);
void parser_free(struct parser **parser);
const char *identifier_str(struct parser *parser, const struct token *token);
struct ast_tyref tyref_make_pointer(struct parser *parser,
const struct ast_tyref *var_ty);
struct ast_tyref tyref_get_underlying(struct parser *parser,
const struct ast_tyref *ty_ref);
void debug_print_ast(struct parser *parser,
struct ast_translationunit *translation_unit);
#endif