-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.h
754 lines (609 loc) · 15.6 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#ifndef PARSE_H
#define PARSE_H
#include "alloc.h"
#include "lex.h"
#include "log.h"
#include "program.h"
#include "util.h"
/* Type refs - `<enum|struct|union> <identifier`, `<typedef-name>`, or
* `<keyword>` */
enum ast_storage_class_specifier {
AST_STORAGE_CLASS_SPECIFIER_TYPEDEF,
AST_STORAGE_CLASS_SPECIFIER_EXTERN,
AST_STORAGE_CLASS_SPECIFIER_STATIC,
AST_STORAGE_CLASS_SPECIFIER_AUTO,
AST_STORAGE_CLASS_SPECIFIER_REGISTER,
};
enum ast_function_specifier {
AST_FUNCTION_SPECIFIER_INLINE,
};
enum ast_type_qualifier {
AST_TYPE_QUALIFIER_CONST,
AST_TYPE_QUALIFIER_VOLATILE,
AST_TYPE_QUALIFIER_RESTRICT,
};
struct ast_declaration_specifier_list {
size_t num_decl_specifiers;
struct ast_declaration_specifier *decl_specifiers;
};
struct ast_pointer {
struct ast_declaration_specifier_list specifier_list;
};
struct ast_pointer_list {
size_t num_pointers;
struct ast_pointer *pointers;
};
enum ast_direct_declarator_ty {
AST_DIRECT_DECLARATOR_TY_IDENTIFIER,
AST_DIRECT_DECLARATOR_TY_PAREN_DECLARATOR,
AST_DIRECT_DECLARATOR_TY_ARRAY_DECLARATOR,
AST_DIRECT_DECLARATOR_TY_FUNC_DECLARATOR,
};
struct ast_direct_declarator {
enum ast_direct_declarator_ty ty;
union {
struct lex_token identifier;
struct ast_declarator *paren_declarator;
struct ast_array_declarator *array_declarator;
struct ast_func_declarator *func_declarator;
};
};
struct ast_direct_declarator_list {
size_t num_direct_declarators;
struct ast_direct_declarator *direct_declarators;
};
enum ast_declarator_ty {
AST_DECLARATOR_TY_VALUE,
AST_DECLARATOR_TY_POINTER,
};
struct ast_declarator {
struct ast_pointer_list pointer_list;
struct ast_direct_declarator_list direct_declarator_list;
struct ast_expr *bitfield_size;
};
// struct ast_struct_declarator {
// struct ast_declarator declarator;
// struct ast_expr *bitfield_size;
// };
// struct ast_struct_declarator_list {
// size_t num_declarators;
// struct ast_struct_declarator *declarators;
// };
// struct ast_struct_declaration {
// struct ast_declaration_specifier_list decl_specifiers;
// struct ast_struct_declarator_list struct_declarator_list;
// };
struct ast_struct_declaration_list {
size_t num_declarations;
struct ast_declaration *declarations;
};
enum ast_struct_or_union_specifier_ty {
AST_STRUCT_OR_UNION_SPECIFIER_TY_STRUCT,
AST_STRUCT_OR_UNION_SPECIFIER_TY_UNION,
};
struct ast_declaration_list {
size_t num_declarations;
struct ast_declaration *declarations;
};
struct ast_struct_or_union_specifier {
enum ast_struct_or_union_specifier_ty ty;
struct lex_token *identifier;
struct ast_declaration_list *decl_list;
};
struct ast_enumerator {
struct lex_token identifier;
struct ast_expr *value;
};
struct ast_enumerator_list {
size_t num_enumerators;
struct ast_enumerator *enumerators;
};
struct ast_enum_specifier {
struct lex_token *identifier;
struct ast_enumerator_list *enumerator_list;
};
enum ast_type_specifier_ty {
AST_TYPE_SPECIFIER_TY_KW,
AST_TYPE_SPECIFIER_STRUCT_OR_UNION,
AST_TYPE_SPECIFIER_ENUM,
AST_TYPE_SPECIFIER_TYPEDEF_NAME
};
enum ast_type_specifier_kw {
AST_TYPE_SPECIFIER_KW_VOID,
AST_TYPE_SPECIFIER_KW_CHAR,
AST_TYPE_SPECIFIER_KW_SHORT,
AST_TYPE_SPECIFIER_KW_INT,
AST_TYPE_SPECIFIER_KW_LONG,
AST_TYPE_SPECIFIER_KW_FLOAT,
AST_TYPE_SPECIFIER_KW_DOUBLE,
AST_TYPE_SPECIFIER_KW_SIGNED,
AST_TYPE_SPECIFIER_KW_UNSIGNED,
AST_TYPE_SPECIFIER_KW_BOOL,
AST_TYPE_SPECIFIER_KW_COMPLEX,
AST_TYPE_SPECIFIER_KW_HALF,
};
struct ast_type_specifier {
enum ast_type_specifier_ty ty;
union {
enum ast_type_specifier_kw type_specifier_kw;
struct ast_struct_or_union_specifier struct_or_union_specifier;
struct ast_enum_specifier enum_specifier;
struct lex_token typedef_name;
};
};
enum ast_decl_specifier_ty {
AST_DECL_SPECIFIER_TY_STORAGE_CLASS_SPECIFIER,
AST_DECL_SPECIFIER_TY_TYPE_SPECIFIER,
AST_DECL_SPECIFIER_TY_TYPE_QUALIFIER,
AST_DECL_SPECIFIER_TY_FUNCTION_SPECIFIER,
};
struct ast_declaration_specifier {
enum ast_decl_specifier_ty ty;
union {
enum ast_storage_class_specifier storage_class_specifier;
enum ast_type_qualifier type_qualifier;
enum ast_function_specifier function_specifier;
struct ast_type_specifier type_specifier;
};
};
enum ast_array_declarator_ty {
AST_ARRAY_DECLARATOR_TY_STAR,
AST_ARRAY_DECLARATOR_TY_STATIC_SIZED,
AST_ARRAY_DECLARATOR_TY_SIZED,
AST_ARRAY_DECLARATOR_TY_UNSIZED,
};
struct ast_array_declarator {
enum ast_array_declarator_ty ty;
struct ast_declaration_specifier_list specifier_list;
struct ast_expr *size;
};
struct ast_func_declarator {
struct ast_paramlist *param_list;
};
enum ast_direct_abstract_declarator_ty {
AST_DIRECT_ABSTRACT_DECLARATOR_TY_PAREN_DECLARATOR,
AST_DIRECT_ABSTRACT_DECLARATOR_TY_ARRAY_DECLARATOR,
AST_DIRECT_ABSTRACT_DECLARATOR_TY_FUNC_DECLARATOR,
};
struct ast_direct_abstract_declarator {
enum ast_direct_abstract_declarator_ty ty;
union {
struct ast_abstract_declarator *paren_declarator;
struct ast_array_declarator *array_declarator;
struct ast_func_declarator *func_declarator;
};
};
struct ast_direct_abstract_declarator_list {
size_t num_direct_abstract_declarators;
struct ast_direct_abstract_declarator *direct_abstract_declarators;
};
struct ast_abstract_declarator {
struct ast_pointer_list pointer_list;
struct ast_direct_abstract_declarator_list direct_abstract_declarator_list;
};
struct ast_type_name {
struct ast_declaration_specifier_list specifier_list;
struct ast_abstract_declarator abstract_declarator;
};
// TODO: try and parse init lists as expressions to give better error messages
enum ast_init_ty {
AST_INIT_TY_EXPR,
AST_INIT_TY_INIT_LIST,
};
struct ast_arglist {
struct ast_expr *args;
size_t num_args;
};
/* Variable references */
struct ast_var {
struct lex_token identifier;
};
enum ast_param_ty {
AST_PARAM_TY_DECL,
AST_PARAM_TY_ABSTRACT_DECL,
AST_PARAM_TY_VARIADIC,
AST_PARAM_TY_VOID
};
struct ast_param {
enum ast_param_ty ty;
struct ast_declaration_specifier_list specifier_list;
union {
struct ast_declarator declarator;
struct ast_abstract_declarator abstract_declarator;
};
};
struct ast_paramlist {
struct ast_param *params;
size_t num_params;
};
/* Constant values (literals) */
enum ast_cnst_ty {
AST_CNST_TY_SIGNED_INT,
AST_CNST_TY_UNSIGNED_INT,
AST_CNST_TY_SIGNED_LONG,
AST_CNST_TY_UNSIGNED_LONG,
AST_CNST_TY_SIGNED_LONG_LONG,
AST_CNST_TY_UNSIGNED_LONG_LONG,
AST_CNST_TY_FLOAT,
AST_CNST_TY_DOUBLE,
AST_CNST_TY_LONG_DOUBLE,
AST_CNST_TY_CHAR,
AST_CNST_TY_WIDE_CHAR,
AST_CNST_TY_STR_LITERAL,
AST_CNST_TY_WIDE_STR_LITERAL,
};
struct ast_cnst {
enum ast_cnst_ty ty;
union {
unsigned long long int_value;
char *str_value;
long double flt_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_ADDRESSOF,
AST_UNARY_OP_TY_CAST,
};
struct ast_cast {
struct ast_type_name type_name;
};
struct ast_unary_op {
enum ast_unary_op_ty 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_LOGICAL_OR,
AST_BINARY_OP_TY_LOGICAL_AND,
AST_BINARY_OP_TY_OR,
AST_BINARY_OP_TY_AND,
AST_BINARY_OP_TY_XOR,
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_expr *lhs;
struct ast_expr *rhs;
};
struct ast_call {
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 */
enum ast_designator_ty {
AST_DESIGNATOR_TY_FIELD,
AST_DESIGNATOR_TY_INDEX,
};
struct ast_designator {
enum ast_designator_ty ty;
union {
struct lex_token field;
struct ast_expr *index;
};
};
struct ast_designator_list {
size_t num_designators;
struct ast_designator *designators;
};
struct ast_init_list_init {
struct ast_designator_list *designator_list;
struct ast_init *init;
};
struct ast_init_list {
struct ast_init_list_init *inits;
size_t num_inits;
};
// Assignments - anything of form `<lvalue> = <lvalue | rvalue>` (so `<lvalue>
// = <expr>`)
enum ast_assg_ty {
AST_ASSG_TY_BASIC,
AST_ASSG_TY_ADD,
AST_ASSG_TY_SUB,
AST_ASSG_TY_MUL,
AST_ASSG_TY_DIV,
AST_ASSG_TY_QUOT,
AST_ASSG_TY_AND,
AST_ASSG_TY_OR,
AST_ASSG_TY_XOR,
AST_ASSG_TY_LSHIFT,
AST_ASSG_TY_RSHIFT,
};
struct ast_assg {
enum ast_assg_ty ty;
struct ast_expr *assignee;
struct ast_expr *expr;
};
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 lex_token member;
};
struct ast_pointeraccess {
struct ast_expr *lhs;
struct lex_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_type_name type_name;
};
};
struct ast_alignof {
struct ast_type_name type_name;
};
struct ast_ternary {
struct ast_expr *cond;
struct ast_expr *true_expr;
struct ast_expr *false_expr;
};
/* Expressions - divided into `lvalue` (can be on left hand side of assignment)
* and `rvalue` (not an lvalue) */
struct ast_compound_literal {
struct ast_type_name type_name;
struct ast_init_list init_list;
};
enum ast_expr_ty {
AST_EXPR_TY_TERNARY,
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_ASSG,
AST_EXPR_TY_VAR,
AST_EXPR_TY_CNST,
AST_EXPR_TY_COMPOUNDEXPR,
AST_EXPR_TY_SIZEOF,
AST_EXPR_TY_ALIGNOF,
AST_EXPR_TY_COMPOUND_LITERAL,
};
struct ast_expr {
enum ast_expr_ty ty;
union {
struct ast_ternary ternary;
struct ast_sizeof size_of;
struct ast_alignof align_of;
struct ast_var var;
struct ast_cnst cnst;
struct ast_compoundexpr compound_expr;
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_compound_literal compound_literal;
};
};
/* Variable declarations - `<typename> <comma seperated list of declarations>`
* where each declaration is `<name>` or `<name> = <expr>` */
struct ast_init {
enum ast_init_ty ty;
union {
struct ast_expr expr;
struct ast_init_list init_list;
};
};
struct ast_init_declarator {
struct ast_declarator declarator;
struct ast_init *init;
};
struct ast_init_declarator_list {
size_t num_init_declarators;
struct ast_init_declarator *init_declarators;
};
struct ast_declaration {
struct ast_declaration_specifier_list specifier_list;
struct ast_init_declarator_list declarator_list;
};
/* Jump statements - `return`, `break`, `continue`, `goto` */
struct ast_returnstmt {
struct ast_expr *expr;
};
struct ast_gotostmt {
struct lex_token label;
};
enum ast_jumpstmt_ty {
AST_JUMPSTMT_TY_BREAK,
AST_JUMPSTMT_TY_CONTINUE,
AST_JUMPSTMT_TY_GOTO,
AST_JUMPSTMT_TY_RETURN
};
struct ast_jumpstmt {
enum ast_jumpstmt_ty ty;
union {
struct ast_returnstmt return_stmt;
struct ast_gotostmt goto_stmt;
};
};
/* Statements - either declaration, labelled, expression, compound, jump,
* iteration, or selection */
enum ast_labeledstmt_ty {
AST_LABELEDSTMT_TY_LABEL,
AST_LABELEDSTMT_TY_CASE,
AST_LABELEDSTMT_TY_DEFAULT,
};
struct ast_labeledstmt {
enum ast_labeledstmt_ty ty;
struct ast_stmt *stmt;
union {
struct ast_expr cnst;
struct lex_token label;
};
};
struct ast_ifstmt {
struct ast_expr cond;
struct ast_stmt *body;
};
struct ast_ifelsestmt {
struct ast_expr cond;
struct ast_stmt *body;
struct ast_stmt *else_body;
};
struct ast_switchstmt {
struct ast_expr ctrl_expr;
struct ast_stmt *body;
};
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;
};
enum ast_declaration_or_expr_ty {
AST_DECLARATION_OR_EXPR_TY_DECL,
AST_DECLARATION_OR_EXPR_TY_EXPR,
};
struct ast_declaration_or_expr {
enum ast_declaration_or_expr_ty ty;
union {
struct ast_declaration decl;
struct ast_expr expr;
};
};
struct ast_forstmt {
struct ast_declaration_or_expr *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;
};
};
enum ast_stmt_ty {
AST_STMT_TY_NULL,
AST_STMT_TY_DECLARATION,
AST_STMT_TY_LABELED,
AST_STMT_TY_EXPR,
AST_STMT_TY_COMPOUND,
AST_STMT_TY_JUMP,
AST_STMT_TY_ITER,
AST_STMT_TY_SELECT,
};
struct ast_stmt {
enum ast_stmt_ty ty;
union {
struct ast_declaration declaration;
struct ast_expr expr;
struct ast_compoundstmt compound;
struct ast_jumpstmt jump;
struct ast_selectstmt select;
struct ast_iterstmt iter;
struct ast_labeledstmt labeled;
};
};
/* Function definitions and declarations */
struct ast_funcdef {
struct ast_declaration_specifier_list specifier_list;
struct ast_declarator declarator;
struct ast_declaration_list declaration_list;
struct ast_compoundstmt body;
};
enum ast_external_declaration_ty {
AST_EXTERNAL_DECLARATION_TY_DECLARATION,
AST_EXTERNAL_DECLARATION_TY_FUNC_DEF
};
struct ast_external_declaration {
enum ast_external_declaration_ty ty;
union {
struct ast_funcdef func_def;
struct ast_declaration declaration;
};
};
/* Translation unit (top level) */
struct ast_translationunit {
struct ast_external_declaration *external_declarations;
size_t num_external_declarations;
};
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(struct program *program,
struct preproc *preproc,
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 lex_token *token);
void debug_print_ast(struct parser *parser,
struct ast_translationunit *translation_unit);
#endif