-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
2335 lines (1940 loc) · 59.4 KB
/
parse.c
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "alloycc.h"
//
// Parser
//
// Scope for local or global vars (incl. string literal),
// typedefs, or enum constants
typedef struct VarScope VarScope;
struct VarScope {
VarScope *next;
char *name;
int depth;
Var *var;
Type *type_def;
Type *enum_ty;
int enum_val;
};
// variable attributes (e.g. typedef, extern, etc)
typedef struct {
bool is_typedef;
bool is_static;
bool is_extern;
int align;
} VarAttr;
// Scope for struct, union or enum tags
typedef struct TagScope TagScope;
struct TagScope {
TagScope *next;
char *name;
int depth;
Type *ty;
};
// represents a variable initializer
// this struct has a tree structure since initializers can be nested:
// (e.g. int x[2][2] = {{1, 2}, {3, 4}})
typedef struct Initializer Initializer;
struct Initializer {
Type *ty;
Token *tok;
// for leaf nodes, len == 0 and expr is the initializer expression
// otherwise children has chile nodes of length `len`
int len;
Node *expr;
// children might contain null pointers, in which case, corresponding
// members have to be initialized by zeroes.
Initializer **children;
};
// For local variable initializer
typedef struct InitDesg InitDesg;
struct InitDesg {
InitDesg *next;
int idx;
Member *member;
Var *var;
};
Var *locals;
Var *globals;
// C has two block scopes: one for variables/typedefs and
// the other for struct/union/enum tags.
static VarScope *var_scope;
static TagScope *tag_scope;
static int scope_depth;
// Points to the function object the parser is currently parsing.
static Var *current_fn;
// Points to a node representing a switch if we are parsing
// a switch statement. Otherwise, NULL.
static Node *current_switch;
static bool is_typename(Token *tok);
static Type *typespec(Token **rest, Token *tok, VarAttr *attr);
static Type *typename(Token **rest, Token *tok);
static void register_enum_list(Token **rest, Token *tok, Type *ty);
static Type *enum_specifier(Token **rest, Token *tok);
static Type *type_suffix(Token **rest, Token *tok, Type *ty);
static Type *declarator(Token **rest, Token *tok, Type *base);
static Node *declaration(Token **rest, Token *tok);
static Initializer *initializer(Token **rest, Token *tok, Type *ty);
static void gvar_initializer(Token **rest, Token *tok, Var *var);
static Node *lvar_initializer(Token **rest, Token *tok, Var *var);
static Function *funcdef(Token **rest, Token *tok);
static Type *func_params(Token **rest, Token *tok, Type *ty);
static Type *struct_decl(Token **rest, Token *tok);
static Type *union_decl(Token **rest, Token *tok);
static Node *block_stmt(Token **rest, Token *tok);
static Node *stmt(Token **rest, Token *tok);
static Node *if_stmt(Token **rest, Token *tok);
static Node *while_stmt(Token **rest, Token *tok);
static Node *do_stmt(Token **rest, Token *tok);
static Node *for_stmt(Token **rest, Token *tok);
static Node *goto_stmt(Token **rest, Token *tok);
static Node *switch_stmt(Token **rest, Token *tok);
static Node *case_labeled_stmt(Token **rest, Token *tok);
static Node *default_labeled_stmt(Token **rest, Token *tok);
static Node *labeled_stmt(Token **rest, Token *tok);
static Node *return_stmt(Token **rest, Token *tok);
static Node *expr_stmt(Token **rest, Token *tok);
static Node *expr(Token **rest, Token *tok);
static long eval(Node *node);
static long eval2(Node *node, Var **var);
static double eval_double(Node *node);
static Node *assign(Token **rest, Token *tok);
static Node *conditional(Token **rest, Token *tok);
static Node *logor(Token **rest, Token *tok);
static Node *logand(Token **rest, Token *tok);
static Node *bitor(Token **rest, Token *tok);
static Node *bitxor(Token **rest, Token *tok);
static Node *bitand(Token **rest, Token *tok);
static Node *equality(Token **rest, Token *tok);
static Node *relational(Token **rest, Token *tok);
static Node *shift(Token **rest, Token *tok);
static Node *add(Token **rest, Token *tok);
static Node *mul(Token **rest, Token *tok);
static Node *cast(Token **rest, Token *tok);
static Node *unary(Token **rest, Token *tok);
static Node *postfix(Token **rest, Token *tok);
static Node *primary(Token **rest, Token *tok);
static Node *funcall(Token **rest, Token *tok, Node *fn);
static void enter_scope() {
scope_depth++;
}
static void leave_scope() {
scope_depth--;
// remove deeper-scoped vars from the end of the chain
while (var_scope && var_scope->depth > scope_depth)
var_scope = var_scope->next;
while (tag_scope && tag_scope->depth > scope_depth)
tag_scope = tag_scope->next;
}
static VarScope *push_scope(char *name) {
VarScope *sc = calloc(1, sizeof(VarScope));
sc->name = name;
sc->depth = scope_depth;
sc->next = var_scope;
var_scope = sc;
return sc;
}
static TagScope *push_tag_scope(char *name, Type *ty) {
TagScope *sc = calloc(1, sizeof(TagScope));
sc->name = name;
sc->ty = ty;
sc->depth = scope_depth;
sc->next = tag_scope;
tag_scope = sc;
return sc;
}
static Program *new_program() {
Program *prog = calloc(1, sizeof(Program));
return prog;
}
static Function *new_function(Type *ty, VarAttr *attr) {
Function *func = calloc(1, sizeof(Function));
func->name = get_identifier(ty->ident);
func->is_static = attr->is_static;
func->is_variadic= ty->is_variadic;
// TODO: consider return type: func->return_ty = ty;
return func;
}
static Node *new_node(NodeKind kind, Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->token = tok;
return node;
}
static Var *new_var(char *name, Type *ty) {
Var *var = calloc(1, sizeof(Var));
var->name = name;
var->ty = ty;
var->align = ty->align;
return var;
}
static Member *new_member(char *name, Type *ty) {
Member *mem = calloc(1, sizeof(Member));
mem->name = name;
mem->ty= ty;
return mem;
}
static Initializer *new_init(Type *ty, int len, Node *expr, Token *tok) {
Initializer *init = calloc(1, sizeof(Initializer));
init->ty = ty;
init->tok = tok;
init->len = len;
init->expr = expr;
if (len)
init->children = calloc(len, sizeof(Initializer *));
return init;
}
static Var *new_lvar(char *name, Type *ty) {
Var *var = new_var(name, ty);
var->is_local = true;
var->next = locals;
locals = var;
push_scope(name)->var = var;
return var;
}
static Var *new_gvar(char *name, Type *ty, bool is_static, bool emit) {
Var *var = new_var(name, ty);
var->is_local = false;
var->is_static = is_static;
if (emit) {
var->next = globals;
globals = var;
}
push_scope(name)->var = var;
return var;
}
// finds a variable or a typedef by name
static VarScope *lookup_var(char *name) {
for (VarScope *sc = var_scope; sc; sc = sc->next) {
if (!strcmp(sc->name, name))
return sc;
}
return NULL;
}
static Type *lookup_typedef(Token *tok) {
if (tok->kind == TK_IDENT) {
VarScope *sc = lookup_var(get_identifier(tok));
if (sc)
return sc->type_def;
}
return NULL;
}
static TagScope *lookup_tag(char *name) {
for (TagScope *sc = tag_scope; sc; sc = sc->next) {
if (!strcmp(sc->name, name))
return sc;
}
return NULL;
}
static char *new_gvar_name() {
static int counter = 0;
char *buf = malloc(20);
sprintf(buf, ".L.data.%d", counter++);
return buf;
}
static Var *new_string_literal(char *s, int len) {
Type *ty = array_of(ty_char, len);
Var *var = new_gvar(new_gvar_name(), ty, true, true);
var->init_data = s;
return var;
}
static Node *new_node_unary(NodeKind kind, Node *lhs, Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = lhs;
return node;
}
static Node *new_node_binary(NodeKind kind, Node *lhs, Node *rhs, Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Node *new_node_var(Var *var, Token *tok) {
Node *node = new_node(ND_VAR, tok);
node->var = var;
return node;
}
static Node *new_node_num(long val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->val = val;
return node;
}
static Node *new_node_fnum(double fval, int n, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->fval = fval;
return node;
}
static Node *new_node_num_ulong(long val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->val = val;
node->ty = ty_ulong;
return node;
}
static Node *new_node_add(Node *lhs, Node *rhs, Token *tok) {
generate_type(lhs);
generate_type(rhs);
// number + number
if (is_numeric(lhs->ty) && is_numeric(rhs->ty))
return new_node_binary(ND_ADD, lhs, rhs, tok);
// ptr + ptr (illegal)
if (is_pointer_like(lhs->ty) && is_pointer_like(rhs->ty))
error_tok(tok, "invalid operands");
// number + ptr (canonicalize)
if (is_integer(lhs->ty) && is_pointer_like(rhs->ty)) {
Node *t = lhs;
lhs = rhs;
rhs = t;
}
// ptr + number (multiplied by base size of ptr)
if (is_pointer_like(lhs->ty) && is_integer(rhs->ty)) {
rhs = new_node_binary(ND_MUL, rhs, new_node_num(size_of(lhs->ty->base), tok), tok);
return new_node_binary(ND_ADD, lhs, rhs, tok);
}
error_tok(tok, "invalid operands");
}
static Node *new_node_sub(Node *lhs, Node *rhs, Token *tok) {
generate_type(lhs);
generate_type(rhs);
// number - number
if (is_numeric(lhs->ty) && is_numeric(rhs->ty))
return new_node_binary(ND_SUB, lhs, rhs, tok);
// ptr - number (multiplied by base size of ptr)
if (is_pointer_like(lhs->ty) && is_integer(rhs->ty)) {
rhs = new_node_binary(ND_MUL, rhs, new_node_num(size_of(lhs->ty->base), tok), tok);
return new_node_binary(ND_SUB, lhs, rhs, tok);
}
// ptr - ptr: returns how many elements are between the two
if (is_pointer_like(lhs->ty) && is_pointer_like(rhs->ty)) {
Node *node = new_node_binary(ND_SUB, lhs, rhs, tok);
return new_node_binary(ND_DIV, node, new_node_num(size_of(lhs->ty->base), tok), tok);
}
// number - ptr (illegal)
error_tok(tok, "invalid operands");
}
Node *new_node_cast(Node *expr, Type *ty) {
generate_type(expr);
Node *node = new_node_unary(ND_CAST, expr, expr->token);
node->ty = copy_ty(ty);
return node;
}
// create a lvar node for "__func__" that refers to the name of the current function
static void add_func_ident(char *funcname) {
Var *var = new_string_literal(funcname, strlen(funcname) + 1);
push_scope("__func__")->var = var;
}
// program = (funcdef | global-var)*
Program *parse(Token *tok) {
// add built-if functon types
new_gvar("__builtin_va_start", func_returning(ty_void), true, false);
// read source code until EOF
Function head = {0};
Function *cur = &head;
globals = NULL;
while (tok->kind != TK_EOF) {
Token *start = tok;
VarAttr attr = {0};
Type *basety = typespec(&tok, tok, &attr);
if (consume(&tok, tok, ";"))
continue;
Type *ty = declarator(&tok, tok, basety);
// typedef
// "typedef" basety foo[3], *bar, ..
if (attr.is_typedef) {
for(;;) {
if (!ty->ident)
error_tok(ty->name_pos, "typedef name omitted");
push_scope(get_identifier(ty->ident))->type_def =ty;
if (consume(&tok, tok, ";"))
break;
tok = skip(tok, ",");
ty = declarator(&tok, tok, basety);
}
continue;
}
// function
if (ty->kind == TY_FUNC) {
current_fn = new_gvar(get_identifier(ty->ident), ty, attr.is_static, false);
if (!consume(&tok, tok, ";"))
cur = cur->next = funcdef(&tok, start);
continue;
}
// global variable = typespec declarator ("," declarator)* ";"
for (;;) {
if (!ty->ident)
error_tok(ty->name_pos, "variable name omitted");
Var *var = new_gvar(get_identifier(ty->ident), ty, attr.is_static, !attr.is_extern);
if (attr.align)
var->align = attr.align;
if (consume(&tok, tok, "="))
gvar_initializer(&tok, tok, var);
if (consume(&tok, tok, ";"))
break;
tok = skip(tok, ",");
ty = declarator(&tok, tok, basety);
}
}
Program *prog = new_program();
prog->globals = globals;
prog->fns = head.next;
return prog;
}
// typespec = typename typename*
// typename = "void" | "_Bool" | "char" | "int" | "short" | "long" |
// "struct" struct_dec | "union" union-decll
static Type *typespec(Token **rest, Token *tok, VarAttr *attr) {
enum {
VOID = 1 << 0,
BOOL = 1 << 2,
CHAR = 1 << 4,
SHORT = 1 << 6,
INT = 1 << 8,
LONG = 1 << 10,
FLOAT = 1 << 12,
DOUBLE = 1 << 14,
OTHER = 1 << 16,
SIGNED = 1 << 17,
UNSIGNED = 1 << 18,
};
Type *ty = ty_int;
int counter = 0;
bool is_const = false;
while (is_typename(tok)) {
// handle storage class specifiers
if (equal(tok, "typedef") || equal(tok, "static") || equal(tok, "extern")) {
if (!attr)
error_tok(tok, "storage class specifier is not allowed in this context");
if (consume(&tok, tok, "typedef"))
attr->is_typedef = true;
else if(consume(&tok, tok, "static"))
attr->is_static = true;
else if(consume(&tok, tok, "extern"))
attr->is_extern = true;
else
error_tok(tok, "internal error");
if (attr->is_typedef + attr->is_static + attr->is_extern > 1)
error_tok(tok, "typedef and static may not be used together");
continue;
}
if (consume(&tok, tok, "const")) {
is_const = true;
continue;
}
if (consume(&tok, tok, "volatile")) {
continue;
}
if (equal(tok, "_Alignas")) {
if (!attr)
error_tok(tok, "_Alignas is not allowed in this context");
tok = skip(tok->next, "(");
if (is_typename(tok))
attr->align = typename(&tok, tok)->align;
else
attr->align = const_expr(&tok, tok);
tok = skip(tok, ")");
continue;
}
// Handle user-defined tyees
Type *ty2 = lookup_typedef(tok);
if (equal(tok, "struct") || equal(tok, "union") || equal(tok, "enum") || ty2) {
if (counter)
break;
if (equal(tok, "struct")) {
ty = struct_decl(&tok, tok->next);
} else if (equal(tok, "union")) {
ty = union_decl(&tok, tok->next);
} else if (equal(tok, "enum")) {
ty = enum_specifier(&tok, tok->next);
} else {
ty = ty2;
expect_ident(&tok, tok);
}
counter += OTHER;
continue;
}
// Handle built-in types.
if (equal(tok, "void"))
counter += VOID;
else if (equal(tok, "_Bool"))
counter += BOOL;
else if (equal(tok, "char"))
counter += CHAR;
else if (equal(tok, "short"))
counter += SHORT;
else if (equal(tok, "int"))
counter += INT;
else if (equal(tok, "long"))
counter += LONG;
else if (equal(tok, "float"))
counter += FLOAT;
else if (equal(tok, "double"))
counter += DOUBLE;
else if (equal(tok, "signed"))
counter |= SIGNED;
else if (equal(tok, "unsigned"))
counter |= UNSIGNED;
else
error_tok(tok, "internal error");
switch (counter) {
case VOID:
ty = ty_void;
break;
case BOOL:
ty = ty_bool;
break;
case CHAR:
case SIGNED + CHAR:
ty = ty_char;
break;
case UNSIGNED + CHAR:
ty = ty_uchar;
break;
case SHORT:
case SHORT + INT:
case SIGNED + SHORT:
case SIGNED + SHORT + INT:
ty = ty_short;
break;
case UNSIGNED + SHORT:
case UNSIGNED + SHORT + INT:
ty = ty_ushort;
break;
case INT:
case SIGNED:
case SIGNED + INT:
ty = ty_int;
break;
case UNSIGNED:
case UNSIGNED + INT:
ty = ty_uint;
break;
case LONG:
case LONG + INT:
case LONG + LONG:
case LONG + LONG + INT:
case SIGNED + LONG:
case SIGNED + LONG + INT:
case SIGNED + LONG + LONG:
case SIGNED + LONG + LONG + INT:
ty = ty_long;
break;
case UNSIGNED + LONG:
case UNSIGNED + LONG + INT:
case UNSIGNED + LONG + LONG:
case UNSIGNED + LONG + LONG + INT:
ty = ty_ulong;
break;
case FLOAT:
ty = ty_float;
break;
case DOUBLE:
case LONG + DOUBLE:
ty = ty_double;
break;
default:
error_tok(tok, "invalid type");
}
tok = tok->next;
}
if (is_const) {
ty = copy_ty(ty);
ty->is_const = true;
}
*rest = tok;
return ty;
}
// array-dimensions = "[" const-expr? "]" type-suffix
static Type *array_dimensions(Token **rest, Token *tok, Type *ty) {
tok = skip(tok, "[");
if (equal(tok, "]")) {
ty = type_suffix(rest, tok->next, ty);
ty = array_of(ty, 0);
ty->is_incomplete = true;
return ty;
}
int sz = const_expr(&tok, tok);
tok = skip(tok, "]");
ty = type_suffix(rest, tok, ty); // first, define rightmost sub-array's size
return array_of(ty, sz); // this array composes of sz length of subarrays above
}
// type-suffix = "(" func-params ")"
// | array-dimensions
// | ε
static Type *type_suffix(Token **rest, Token *tok, Type *ty) {
if (consume(&tok, tok, "(")) {
ty = func_params(&tok, tok, ty);
tok = skip(tok, ")");
*rest = tok;
return ty;
}
if (equal(tok, "["))
return array_dimensions(rest, tok, ty);
*rest = tok;
return ty;
}
// pointers = ("*" "const"*)*
static Type *pointers(Token **rest, Token *tok, Type *ty) {
while (consume(&tok, tok, "*")) {
ty = pointer_to(ty);
while (equal(tok, "const") || equal(tok, "volatile")) {
if (equal(tok, "const"))
ty->is_const = true;
tok = tok->next;
}
}
*rest = tok;
return ty;
}
// declarator = pointers ("(" declarator ")" | ident?) type-suffix
static Type *declarator(Token **rest, Token *tok, Type *ty) {
ty = pointers(&tok, tok, ty);
if (consume(&tok, tok, "(")) {
Type *placeholder = calloc(1, sizeof(Type));
Type *new_ty = declarator(&tok, tok, placeholder);
tok = skip(tok, ")");
*placeholder = *type_suffix(&tok, tok, ty);
*rest = tok;
return new_ty;
}
Token *name_pos = tok;
Token *ident = NULL;
if (tok->kind == TK_IDENT) {
ident = tok;
tok = tok->next;
}
ty = type_suffix(rest, tok, ty);
ty->ident = ident;
ty->name_pos = name_pos;
return ty;
}
// abstract-declarator = pinters ("(" abstract-declarator ")")? type-suffix
static Type *abstract_declarator(Token **rest, Token *tok, Type *ty) {
ty = pointers(&tok, tok, ty);
if (consume(&tok, tok, "(")) {
Type *placeholder = calloc(1, sizeof(Type));
Type *new_ty = abstract_declarator(&tok, tok, placeholder);
tok = skip(tok, ")");
*placeholder = *type_suffix(rest, tok, ty);
return new_ty;
}
return type_suffix(rest, tok, ty);
}
// type-name = typespec abstract-declarator
static Type *typename(Token **rest, Token *tok) {
Type *ty = typespec(&tok, tok, NULL);
return abstract_declarator(rest, tok, ty);
}
static bool is_end(Token *tok) {
return equal(tok, "}") || (equal(tok, ",") && equal(tok->next, "}"));
}
static bool consume_end(Token **rest, Token *tok) {
if (equal(tok, "}")) {
*rest = tok->next;
return true;
}
if (equal(tok, ",") && equal(tok->next, "}")) {
*rest = tok->next->next;
return true;
}
return false;
}
// enum-list = ident ("=" const-expr)? ("," ident ("=" const-expr)?)* ","?
static void register_enum_list(Token **rest, Token *tok, Type *ty) {
int i = 0;
int val = 0;
tok = skip(tok, "{");
while (!consume_end(rest, tok)) {
if (i++ > 0)
tok = skip(tok, ",");
char *tag_name = expect_ident(&tok, tok);
if (equal(tok, "="))
val = const_expr(&tok, tok->next);
VarScope *sc = push_scope(tag_name);
sc->enum_ty = ty;
sc->enum_val = val++;
}
}
// enum-specifier = ident? "{" enum-list? "}"
// | ident ("{" enum-list? "}")?
static Type *enum_specifier(Token **rest, Token *tok) {
char *tag_name = NULL;
Token *start = tok;
// read a struct tag;
if (tok->kind == TK_IDENT)
tag_name = expect_ident(&tok, tok);
if (tag_name && !equal(tok, "{")) {
TagScope *sc = lookup_tag(tag_name);
if (!sc)
error_tok(start, "unknown enum type");
if (sc->ty->kind != TY_ENUM)
error_tok(start, "not an enum tag");
*rest = tok;
return sc->ty;
}
Type *ty = enum_type();
register_enum_list(&tok, tok, ty);
// Register the struct type if name is given
if (tag_name)
push_tag_scope(tag_name, ty);
*rest = tok;
return ty;
}
// declaration = typespec (declarator ( = expr)? ( "," declarator ( = expr)? )* )? ";"
static Node *declaration(Token **rest, Token *tok) {
Node head = {};
Node *cur = &head;
Token *start_decl = tok;
VarAttr attr = {0};
Type *basety = typespec(&tok, tok, &attr);
int cnt = 0;
while(!consume(&tok, tok, ";")) {
if (cnt++ > 0)
tok = skip(tok, ",");
Token *start = tok;
Type *ty = declarator(&tok, tok, basety);
if (!ty->ident)
error_tok(ty->name_pos, "variable declared void");
if (ty->kind == TY_VOID)
error_tok(start, "variable declared void");
if (attr.is_typedef) {
push_scope(get_identifier(ty->ident))->type_def = ty;
continue;
}
if (attr.is_static) {
// static local variable
Var *var = new_gvar(new_gvar_name(), ty, true, true);
push_scope(get_identifier(ty->ident))->var = var;
if (equal(tok, "="))
gvar_initializer(&tok, tok->next, var);
continue;
}
Var *var = new_lvar(get_identifier(ty->ident), ty);
if (attr.align)
var->align = attr.align;
if (consume(&tok, tok, "=")) {
Node *expr = lvar_initializer(&tok, tok, var);
cur = cur->next = new_node_unary(ND_EXPR_STMT, expr, tok);
}
}
Node *blk = new_node(ND_BLOCK, start_decl);
blk->body = head.next;
*rest = tok;
return blk;
}
static Token *skip_excess_elements(Token *tok) {
while(!consume_end(&tok, tok)) {
tok = skip(tok, ",");
if (equal(tok, "{"))
tok = skip_excess_elements(tok->next);
else
assign(&tok, tok);
}
return tok;
}
static Token *skip_end(Token *tok) {
if (consume_end(&tok, tok))
return tok;
warn_tok(tok, "excess elements in initializer");
return skip_excess_elements(tok);
}
// string-initializer = string-literal
static Initializer *string_initializer(Token **rest, Token *tok, Type *ty) {
if (ty->is_incomplete) {
ty->size = tok->cont_len;
ty->array_len = tok->cont_len;
ty->is_incomplete = false;
}
Initializer *init = new_init(ty, ty->array_len, NULL, tok);
int len = (ty->array_len < tok->cont_len) ? ty->array_len : tok->cont_len;
for (int i = 0; i < len; i++) {
Node *expr = new_node_num(tok->contents[i], tok);
init->children[i] = new_init(ty->base, 0, expr, tok);
}
*rest = tok->next;
return init;
}
// array-initializer = "{" initializer ("," initializer)* ","? "}"
// | initializer ("," initializer)* ","?
static Initializer *array_initializer(Token **rest, Token *tok, Type *ty) {
bool has_paren = consume(&tok, tok, "{");
if (ty->is_incomplete) {
int i = 0;
for (Token *tok2 = tok; !is_end(tok2); i++) {
if (i > 0)
tok2 = skip(tok2, ",");
initializer(&tok2, tok2, ty->base);
}
ty->size = size_of(ty->base) * i;
ty->array_len= i;
ty->is_incomplete = false;
}
Initializer *init = new_init(ty, ty->array_len, NULL, tok);
for (int i = 0; i < ty->array_len && !is_end(tok); i++) {
if (i > 0)
tok = skip(tok, ",");
init->children[i] = initializer(&tok, tok, ty->base);
}
if (has_paren)
tok = skip_end(tok);
*rest = tok;
return init;
}
// struct-initializer = "{" initializer ("," initializer)* ","? "}"
// | initializer ("," initializer)* ","?
static Initializer *struct_initializer(Token **rest, Token *tok, Type *ty) {
if (!equal(tok, "{")) {
Token *tok2;
Node *expr = assign(&tok2, tok);
generate_type(expr);
if (expr->ty->kind == TY_STRUCT) {
Initializer *init = new_init(ty, 0, expr, tok);
*rest = tok2;
return init;
}
}
int len = 0;
for (Member *mem = ty->members; mem; mem = mem->next)
len++;
Initializer *init = new_init(ty, len, NULL, tok);
bool has_paren = consume(&tok, tok, "{");
int i = 0;
for (Member *mem = ty->members; mem && !is_end(tok); mem = mem->next, i++) {
if (i > 0)
tok = skip(tok, ",");
init->children[i] = initializer(&tok, tok, mem->ty);
}
if (has_paren)
tok = skip_end(tok);
*rest = tok;
return init;
}
// initializer = string-initializer | array-initializer | struct-initializer
// | "{" assign "}" | assign
static Initializer *initializer(Token **rest, Token *tok, Type *ty) {
if (ty->kind == TY_ARRAY && ty->base->kind == TY_CHAR && tok->kind == TK_STR)
return string_initializer(rest, tok, ty);
if (ty->kind == TY_ARRAY)
return array_initializer(rest, tok, ty);
if (ty->kind == TY_STRUCT)
return struct_initializer(rest, tok, ty);
Token *start = tok;
bool has_paren = consume(&tok, tok, "{");
Initializer *init = new_init(ty, 0, assign(&tok, tok), start);
if (has_paren)
tok = skip_end(tok);
*rest = tok;
return init;
}
Node *init_desg_expr(InitDesg *desg, Token *tok) {
if (desg->var)
return new_node_var(desg->var, tok);
if (desg->member) {
Node *node = new_node_unary(ND_MEMBER, init_desg_expr(desg->next, tok), tok);
node->member = desg->member;
return node;
}
Node *lhs = init_desg_expr(desg->next, tok);
Node *rhs = new_node_num(desg->idx, tok);
return new_node_unary(ND_DEREF, new_node_add(lhs, rhs, tok), tok);
}