-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
1751 lines (1588 loc) · 50.5 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 "inc.h"
/*******************
* create AST Node *
*******************/
// http://port70.net/~nsz/c/c99/n1256.html#6.3.2.1p1
static int is_lvalue(Node node) {
switch (node->kind) {
case A_VAR:
case A_DEFERENCE:
case A_ARRAY_SUBSCRIPTING:
return 1;
case A_MEMBER_SELECTION:
return is_lvalue(node->structure);
case A_IDENT:
return is_lvalue(node->ref);
default:
return 0;
}
}
static int is_modifiable_lvalue(Node node, int ignore_qual) {
if (node->kind == A_IDENT)
node = node->ref;
Type t = ignore_qual ? unqual(node->type) : node->type;
return is_lvalue(node) && !is_const(t) && !is_array(t) &&
!is_struct_with_const_member(t);
}
static Node mknode(int kind, Token token) {
Node n = calloc(1, sizeof(struct node));
n->kind = kind;
n->token = token;
return n;
}
static Node mkicons(int intvalue) {
Node n = mknode(A_NUM, NULL);
n->type = inttype;
n->intvalue = intvalue;
return n;
}
static Node mkiconsstr(Token token) {
Node n = mknode(A_NUM, token);
char* suffix;
n->intvalue = strtoull(token->name, &suffix, 0);
if (errno == ERANGE)
errorat(token, "integer constant to large");
int is_oct_or_hex = (*token->name == '0') &&
((token->name[1] >= '0' && token->name[1] <= '7') ||
(token->name[1] == 'x' || token->name[1] == 'X'));
int is_none = strlen(suffix) == 0;
int is_u = !strcmp(suffix, "u") || !strcmp(suffix, "U");
int is_l = !strcmp(suffix, "l") || !strcmp(suffix, "L");
is_l |= !strcmp(suffix, "ll") || !strcmp(suffix, "LL");
int is_ul = !strcmp(suffix, "ul") || !strcmp(suffix, "UL") ||
!strcmp(suffix, "Ul") || !strcmp(suffix, "uL") ||
!strcmp(suffix, "lu") || !strcmp(suffix, "LU") ||
!strcmp(suffix, "Lu") || !strcmp(suffix, "lU");
is_ul |= !strcmp(suffix, "ull") || !strcmp(suffix, "ULL") ||
!strcmp(suffix, "Ull") || !strcmp(suffix, "uLL") ||
!strcmp(suffix, "llu") || !strcmp(suffix, "LLU") ||
!strcmp(suffix, "LLu") || !strcmp(suffix, "llU");
if (is_none) {
if (n->intvalue <= INT_MAX) {
n->type = inttype;
return n;
}
if (is_oct_or_hex && n->intvalue <= UINT_MAX) {
n->type = uinttype;
return n;
}
if (n->intvalue <= LONG_MAX) {
n->type = longtype;
return n;
}
if (is_oct_or_hex && n->intvalue <= ULONG_MAX) {
n->type = ulongtype;
return n;
}
errorat(token, "integer constant too large");
} else if (is_ul) {
if (n->intvalue <= ULONG_MAX) {
n->type = ulongtype;
return n;
}
assert(0);
} else if (is_u) {
if (n->intvalue <= UINT_MAX) {
n->type = uinttype;
return n;
}
if (n->intvalue <= ULONG_MAX) {
n->type = ulongtype;
return n;
}
} else if (is_l) {
if (n->intvalue <= LONG_MAX) {
n->type = longtype;
return n;
}
if (is_oct_or_hex && n->intvalue <= ULONG_MAX) {
n->type = ulongtype;
return n;
}
errorat(token, "integer constant too large");
}
errorat(token, "invalid integer suffix %s", suffix);
assert(0);
}
static Node mkaux(int kind, Node body) {
Node n = mknode(kind, NULL);
n->body = body;
return n;
}
static Node mkcvs(Type t, Node body) {
if (body->type == t)
return body;
Node n = mkaux(A_CONVERSION, body);
n->type = t;
return n;
}
static Node mkcast(Node n, Type ty) {
if (ty == voidtype)
return mknode(A_NOOP, NULL);
if (!is_scalar(n->type) || !is_scalar(ty))
errorat(n->token, "scalar type required for cast operator");
// a cast does not yield an lvalue, always create a conversion node
n = mkaux(A_CONVERSION, n);
n->type = ty;
return n;
}
// usual arithmetic conversions for binary expressions
static Type usual_arithmetic_conversions(Node node) {
Type t = usual_arithmetic_type(node->left->type, node->right->type);
node->left = mkcvs(t, node->left);
node->right = mkcvs(t, node->right);
return t;
}
static Node unqual_array_to_ptr(Node n) {
n = mkcvs(unqual(n->type), n);
if (is_array(n->type))
n = mkcvs(array_to_ptr(n->type), n);
return n;
}
static Node mkunary(int kind, Node left, Token token) {
Node n = mknode(kind, token);
n->left = (kind == A_ADDRESS_OF || kind == A_POSTFIX_INC ||
kind == A_POSTFIX_DEC || kind == A_SIZE_OF)
? left
: unqual_array_to_ptr(left);
// http://port70.net/~nsz/c/c99/n1256.html#6.5.3.2
if (kind == A_ADDRESS_OF) {
if (left->kind == A_DEFERENCE)
return left->left;
if (is_lvalue(left)) {
n->type = ptr_type(left->type);
return n;
}
errorat(n->token, "invalid operand of unary '&' (lvalue required)");
}
if (kind == A_DEFERENCE) {
if (left->kind == A_ADDRESS_OF)
return left->left;
if (is_ptr(left->type)) {
n->type = deref_type(left->type);
return n;
}
errorat(n->token, "invalid operand of unary '*' (have '%s')",
left->type->str);
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.3.3
if (kind == A_MINUS || kind == A_PLUS) {
if (is_arithmetic(n->left->type)) {
n->left = mkcvs(integral_promote(n->left->type), n->left);
n->type = n->left->type;
return n;
}
errorat(n->token, "invalid operand of unary +/- (have '%s')",
n->left->type->str);
}
if (kind == A_B_NOT) {
if (is_integer(n->left->type)) {
n->left = mkcvs(integral_promote(n->left->type), n->left);
n->type = n->left->type;
return n;
}
errorat(n->token, "invalid operand of unary '~' (have '%s')",
n->left->type->str);
}
if (kind == A_L_NOT) {
if (is_scalar(n->left->type)) {
n->type = inttype;
return n;
}
errorat(n->token, "invalid operand of unary '!' (have '%s')",
n->left->type->str);
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.2.4
if (kind == A_POSTFIX_INC || kind == A_POSTFIX_DEC) {
if (!is_modifiable_lvalue(n->left, 0))
errorat(n->token, "invalid operand of postfix ++/-- (lvalue required)");
if (!(is_arithmetic(n->left->type) || is_ptr(n->left->type)))
errorat(n->token, "invalid operand of postfix ++/-- (have '%s')",
n->left->type->str);
n->left = mkcvs(unqual(n->left->type), left);
n->type = n->left->type;
return mkcvs(integral_promote(n->type), n);
}
if (kind == A_SIZE_OF) {
Node s = mkicons(unqual(n->left->type)->size);
s->type = uinttype;
return s;
}
assert(0);
}
static Node mkbinary(int kind, Node left, Node right, Token token) {
Node n = mknode(kind, token);
n->left =
(kind == A_ASSIGN || kind == A_INIT) ? left : unqual_array_to_ptr(left);
n->right = unqual_array_to_ptr(right);
// http://port70.net/~nsz/c/c99/n1256.html#6.5.16
if (kind == A_ASSIGN || kind == A_INIT) {
if (!is_modifiable_lvalue(n->left, kind == A_INIT))
errorat(n->left->token,
"invalid left operand of assignment(modifiable lvalue required)");
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
} else if (is_ptr(n->left->type) && n->right->kind == A_NUM &&
n->right->intvalue == 0) {
// http://port70.net/~nsz/c/c99/n1256.html#6.3.2.3p3
} else if (is_ptr(n->left->type) && is_ptr(n->right->type)) {
if (kind == A_ASSIGN) {
if (!is_const(deref_type(n->left->type)) &&
is_const(deref_type(n->right->type)))
errorat(n->token, "assign discards const");
}
if (!is_compatible_type(unqual(n->left->type), n->right->type))
errorat(n->token, "assign with incompatible type");
} else if (is_struct_or_union(n->left->type) &&
is_struct_or_union(n->right->type)) {
if (!is_compatible_type(unqual(n->left->type), n->right->type))
errorat(n->token, "assign with incompatible type");
} else
errorat(n->token, "assign with incompatible type");
n->type = unqual(n->left->type);
n->right = mkcvs(n->type, n->right);
if (kind == A_INIT) {
n->kind = A_ASSIGN;
n = mkaux(A_EXPR_STAT, n);
}
return n;
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.17
if (kind == A_COMMA) {
n->type = n->right->type;
return n;
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.14
// http://port70.net/~nsz/c/c99/n1256.html#6.5.13
if (kind == A_L_OR || kind == A_L_AND) {
if (is_scalar(n->left->type) && is_scalar(n->right->type)) {
n->type = inttype;
return n;
}
errorat(n->token, "invalid operands of logical or/and (scalar required)");
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.10
// http://port70.net/~nsz/c/c99/n1256.html#6.5.11
// http://port70.net/~nsz/c/c99/n1256.html#6.5.12
if (kind == A_B_AND || kind == A_B_INCLUSIVEOR || kind == A_B_EXCLUSIVEOR) {
if (is_integer(n->left->type) && is_integer(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
errorat(n->token, "invalid operand of bitwise operator, scalar required");
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.9
// http://port70.net/~nsz/c/c99/n1256.html#6.5.8
if (kind >= A_EQ && kind <= A_GE) {
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
usual_arithmetic_conversions(n);
n->type = inttype;
return n;
}
if (kind >= A_EQ && kind <= A_NE) {
if ((is_ptr(n->left->type) &&
(n->right->kind == A_NUM && n->right->intvalue == 0)) ||
(is_ptr(n->right->type) &&
(n->left->kind == A_NUM && n->left->intvalue == 0))) {
n->type = inttype;
return n;
}
}
if (is_ptr(n->left->type) && is_ptr(n->right->type)) {
if (!is_compatible_type(n->left->type, n->right->type))
errorat(n->token, "operand have incompatible type");
n->type = inttype;
return n;
}
errorat(n->token, "invalid operand");
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.7
if (kind == A_LEFT_SHIFT || kind == A_RIGHT_SHIFT) {
if (is_integer(n->left->type) && is_integer(n->right->type)) {
n->type = integral_promote(n->left->type);
n->left = mkcvs(n->type, n->left);
n->right = mkcvs(n->right->type, n->right);
return n;
}
errorat(n->token, "invalid operand (integer required)");
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.6
if (kind == A_ADD) {
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
if (is_integer(n->left->type) && is_ptr(n->right->type)) {
n->left =
mkbinary(A_MUL, n->left, mkicons(n->right->type->base->size), NULL);
n->type = n->right->type;
return n;
}
if (is_ptr(n->left->type) && is_integer(n->right->type)) {
n->right =
mkbinary(A_MUL, n->right, mkicons(n->left->type->base->size), NULL);
n->type = n->left->type;
return n;
}
errorat(n->token, "invalid operands to binary +");
}
if (kind == A_SUB) {
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
if (is_ptr(n->left->type) && is_integer(n->right->type)) {
n->right =
mkbinary(A_MUL, n->right, mkicons(n->left->type->base->size), NULL);
n->type = n->left->type;
return n;
}
if (is_ptr(n->left->type) && is_ptr(n->right->type)) {
if (!is_compatible_type(n->left->type, n->right->type))
errorat(n->token,
"invalid operands to binary -, incompatiable pointer");
n->type = inttype;
return mkbinary(A_DIV, n, mkicons(n->left->type->base->size), NULL);
}
errorat(n->token, "invalid operands to binary -");
}
// http://port70.net/~nsz/c/c99/n1256.html#6.5.5
if (kind == A_MUL || kind == A_DIV) {
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
errorat(n->token, "invalid operands to binary (arighmetic required)");
}
if (kind == A_MOD) {
if (is_integer(n->left->type) && is_integer(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
errorat(n->token, "invalid operands to binary (integer required)");
}
assert(0);
}
static Node mkternary(Node cond, Node left, Node right, Token token) {
Node n = mknode(A_TERNARY, token);
n->cond = unqual_array_to_ptr(cond);
n->left = unqual_array_to_ptr(left);
n->right = unqual_array_to_ptr(right);
if (is_arithmetic(n->left->type) && is_arithmetic(n->right->type)) {
n->type = usual_arithmetic_conversions(n);
return n;
}
if (is_ptr(n->left->type) && is_ptr(n->right->type)) {
if (!is_compatible_type(n->left->type, n->right->type))
errorat(n->token, "pointer type mismatch in ternary");
n->type = n->left->type;
if (is_array(n->type))
n->type = array_to_ptr(n->type);
return n;
}
assert(0);
}
static Node mkvar(Token token, Type ty) {
Node n = mknode(A_VAR, token);
n->name = token->name;
n->type = ty;
n->token = token;
n->is_global = current_func ? 0 : 1;
install_symbol(n, SCOPE_INNER);
return n;
}
static Node mkfunc(Token token, Type ty) {
Node n = find_symbol(token, A_FUNCTION, SCOPE_FILE);
if (n) {
if (!is_compatible_type(n->type, ty)) {
infoat(token, "previous:");
errorat(n->token, "conflicting types for function %s", token->name);
}
n->token = token;
n->type =
n->body ? composite_type(n->type, ty) : composite_type(ty, n->type);
return n;
}
n = mknode(A_FUNCTION, token);
n->name = token->name;
n->type = ty;
install_symbol(n, SCOPE_FILE);
return n;
}
static Node mkstrlit(Token token) {
Node n = mknode(A_STRING_LITERAL, token);
n->string_value = token->name;
n->type = array_type(chartype, strlen(n->string_value) + 1);
install_symbol(n, SCOPE_FILE);
return n;
}
static Node mktag(Token tok, Type ty) {
Node n = find_symbol(tok, A_TAG, SCOPE_INNER);
if (n) {
if (is_enum(ty)) {
infoat(n->token, "originally defined here:");
errorat(tok, "redeclaration tag");
} else if (!ty->member) {
} else if (!n->type->member) {
n->token = tok;
update_struct_or_union_type(n->type, ty->member);
} else {
infoat(n->token, "originally defined here:");
errorat(tok, "redeclaration tag");
}
} else {
n = mknode(A_TAG, tok);
n->name = tok->name;
n->type = ty;
install_symbol(n, SCOPE_INNER);
}
return n;
}
// tag for struct/union/enum share namesapce
// http://port70.net/~nsz/c/c99/n1256.html#6.2.3
static Node find_tag(Token tok, int type_kind) {
Node n = find_symbol(tok, A_TAG, SCOPE_ALL);
if (n && unqual(n->type)->kind != type_kind) {
infoat(n->token, "originally defined as:");
errorat(tok, "conflict tag");
}
return n;
}
static Node mkenumconst(Token tok, int value) {
Node n = mknode(A_ENUM_CONST, tok);
n->name = tok->name;
n->type = inttype;
n->intvalue = value;
install_symbol(n, SCOPE_INNER);
return n;
}
static Node mktypedef(Token tok, Type ty) {
Node n = mknode(A_TYPEDEF, tok);
n->name = tok->name;
n->type = ty;
install_symbol(n, SCOPE_INNER);
return n;
}
static int match_specifier_typedef() {
if (match_specifier())
return 1;
if (!match(TK_IDENT))
return 0;
Node n = find_symbol(token(), A_ANY, SCOPE_ALL);
return n && n->kind == A_TYPEDEF;
}
/**********************
* double linked list *
**********************/
// make a circular doubly linked list node
// if body is NULL, make a dummy head node
static Node mklist(Node body) {
Node n = mkaux(A_DLIST, body);
n->next = n->prev = n;
return n;
}
// insert node to the end of list and return head
static Node list_insert(Node head, Node node) {
head->prev->next = node;
node->prev = head->prev;
node->next = head;
head->prev = node;
return head;
}
int list_length(Node head) {
int l = 0;
for (Node n = head->next; n != head; n = n->next)
l++;
return l;
}
/*****************************
* recursive parse procedure *
*****************************/
// trans_unit: { func_def | declaration }*
// ======================= Function DEF =======================
// func_def: declaration_specifiers declarator comp_stat
// ======================= Declaration =======================
// declaration: declaration_specifiers
// init_declarator { ',' init_declarator }* ';'
// declaration_specifiers: { type_specifier | type-qualifier }*
// type_qualifier: { 'const' | 'volatile' | 'restrict' }
// type_specifier: { 'void' | 'char' | 'int' | 'short' |
// 'long' | 'unsigned'|'signed'
// struct_union_specifier | enum_specifier }
// struct_union_specifier: {'struct' | 'union'} identifier?
// { '{' struct_declaration_list '}' }?
// struct_declaration_list: { {type_qualifier|type_specifier}+
// decalrator {',' declarator} ';' }+
// enum_specifier: 'enum' identifier? '{' enumerator_list ','? '}'
// enumerator_list: enumerator { ',' enumerator }*
// enumerator: enumeration-constant { '=' expression }?
// enumeration-constant: identifier
// init_declarator: declarator { '=' expression }? -> func_def
// declarator: pointer? direct_declarator suffix_declarator*
// pointer: { '*' { type_qualifier }* }*
// direct_declarator: '(' declarator ')' | identifier | Empty
// suffix_declarator: array_declarator | function_declarator
// array_declarator: '[' expression ']'
// function_declarator: '(' { parameter_list }* ')'
// parameter_list: parameter_declaration {',' parameter_declaration}*
// parameter_declaration: declaration_specifiers declarator
// type_name: declaration_specifiers declarator
// ======================= Statement =======================
// statement: expr_stat | comp_stat
// selection-stat | iteration-stat | jump_stat
// selection-stat: if_stat
// if_stat: 'if' '(' expr_stat ')' statement { 'else' statement }
// iteration-stat: while_stat | dowhile_stat | for_stat
// while_stat: 'while' '(' expr_stat ')' statement
// dowhile_stat: 'do' statement 'while' '(' expression ')' ';'
// for_stat: 'for' '(' {expression}; {expression}; {expression}')'
// statement
// jump_stat: break ';' | continue ';' | 'return' expression? ';'
// comp_stat: '{' {declaration}* {stat}* '}'
// expr_stat: {expression}? ';'
// ======================= Expression =======================
// expression: comma_expr
// comma_expr: assign_expr { ', ' assign_expr }*
// assign_expr: conditional_expr { '=' assign_expr }
// conditional_expr: logical_or_expr { '?' expression : conditional_expr }?
// logical_or_expr: logical_and_expr { '||' logical_and_expr }*
// logical_and_expr: equality_expr { '&&' inclusive_or_expr }*
// inclusive_or_expr: exclusive_or_expr { '|' exclusive_or_expr }*
// exclusive_or_expr: bitwise_and_expr { '^' bitwise_and_expr }*
// bitwise_and_expr: equality_expr { '&' equality_expr}*
// equality_expr: relational_expr { '==' | '!=' relational_expr }*
// relational_expr: sum_expr { '>' | '<' | '>=' | '<=' shift_expr}*
// shift_expr: sum_expr { '<<' | '>>' sum_expr }*
// sum_expr: mul_expr { '+'|'-' mul_expr }*
// mul_expr: cast_expr { '*'|'/' | '%' cast_expr }*
// cast_expr: {'(' type-name ')'}* unary_expr
// unary_expr: { {'*'|'&'|'+'|'-'|'~'|'!'|'} cast_expr |
// {'++' | '--' } * postfix_expr |
// sizeof '(' type_name ')' |
// sizeof unary_expr }
// postfix_expr: primary_expr {arg_list|array_subscripting|'++'|'--' }*
// primary_expr: identifier | number | string-literal | '('expression ')'
// arg_list: '(' expression { ',' expression } ')'
// array_subscripting: '[' expression ']'
static void trans_unit();
static Node declaration();
static Type declaration_specifiers(int* is_typedef);
static Type struct_union_specifier();
static Member struct_declaration_list();
static Type enum_specifier();
static Node init_declarator(Type ty, int is_typedef);
static Type declarator(Type ty, Token* tok);
static Type pointer(Type ty);
static Type direct_declarator(Type ty, Token* tok);
static Type array_declarator(Type base);
static Type function_declarator(Type ty);
static Type type_name();
static Node function(Node f);
static Node statement(int reuse_scope);
static Node comp_stat(int reuse_scope);
static Node if_stat();
static Node while_stat();
static Node dowhile_stat();
static Node for_stat();
static Node expr_stat();
static Node expression();
static Node comma_expr();
static Node assign_expr();
static Node conditional_expr();
static Node logical_or_expr();
static Node logical_and_expr();
static Node inclusive_or_expr();
static Node exclusive_or_expr();
static Node bitwise_and_expr();
static Node eq_expr();
static Node rel_expr();
static Node shift_expr();
static Node sum_expr();
static Node mul_expr();
static Node cast_expr();
static Node unary_expr();
static Node postfix_expr();
static Node primary_expr();
static Node func_call(Node n);
static Node array_subscripting(Node n);
static Node member_selection(Node n);
static Node ordinary(Node n);
static void trans_unit() {
while (token())
declaration();
}
// for struct type declaration without declarator
// make tag/type(return NULL)
// for typedef
// make type(return NULL)
// for local declaration(called by comp_stat/for_stat)
// make local variable(return node list for init assign)
// for global declaration(called by trans_unit)
// make global variable(set it init_value member, return NULL)
// make function node(return the function node)
// for function definition(called by trans_unit)
// make function node && fill body(return NULL)
static Node declaration() {
Token tok;
int is_typedef;
Type ty = declaration_specifiers(&is_typedef);
if ((tok = consume(TK_SIMI))) {
if (!is_struct_or_union(ty) && !is_enum(ty))
errorat(tok, "declare nothing");
return NULL;
}
Node n = init_declarator(ty, is_typedef);
if (n && n->kind == A_FUNCTION) {
if (match(TK_OPENING_BRACES)) {
function(n);
return NULL;
}
n = NULL;
}
struct node head = {0};
Node last = &head;
last->next = n;
while (last->next)
last = last->next;
while (!consume(TK_SIMI)) {
expect(TK_COMMA);
n = init_declarator(ty, is_typedef);
last->next = (n && n->kind == A_FUNCTION) ? NULL : n;
while (last->next)
last = last->next;
}
return head.next;
}
#define get_specifier(specs, spec) (specs & spec)
#define set_specifier(specs, spec) (specs |= spec)
enum {
// type specifiers
SPEC_VOID = 1 << 0,
SPEC_CHAR = 1 << 1,
SPEC_SHORT = 1 << 2,
SPEC_INT = 1 << 3,
SPEC_LONG1 = 1 << 4,
SPEC_LONG2 = 1 << 5,
SPEC_SIGNED = 1 << 6,
SPEC_UNSIGNED = 1 << 7,
SPEC_VOLATILE = 1 << 8,
SPEC_CONST = 1 << 9,
SPEC_RESTRICT = 1 << 10
};
static Type declaration_specifiers(int* is_typedef) {
Token tok;
Type struct_or_union_type = NULL;
Type enum_type = NULL;
Type typedef_type = NULL;
int specifiers = 0;
if (is_typedef)
*is_typedef = 0;
for (;;) {
if ((tok = consume(TK_VOID))) {
if (get_specifier(specifiers, SPEC_VOID))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_VOID);
} else if ((tok = consume(TK_CHAR))) {
if (get_specifier(specifiers, SPEC_CHAR))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_CHAR);
} else if ((tok = consume(TK_SHORT))) {
if (get_specifier(specifiers, SPEC_SHORT))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_SHORT);
} else if ((tok = consume(TK_INT))) {
if (get_specifier(specifiers, SPEC_INT))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_INT);
} else if ((tok = consume(TK_LONG))) {
if (get_specifier(specifiers, SPEC_LONG1)) {
if (get_specifier(specifiers, SPEC_LONG2))
errorat(tok, "two or more data types in declaration specifiers");
else
set_specifier(specifiers, SPEC_LONG2);
} else {
set_specifier(specifiers, SPEC_LONG1);
}
} else if ((tok = consume(TK_UNSIGNED))) {
if (get_specifier(specifiers, SPEC_UNSIGNED))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_UNSIGNED);
} else if ((tok = consume(TK_SIGNED))) {
if (get_specifier(specifiers, SPEC_SIGNED))
errorat(tok, "two or more data types in declaration specifiers");
set_specifier(specifiers, SPEC_SIGNED);
} else if ((tok = consume(TK_CONST))) {
set_specifier(specifiers, SPEC_CONST);
} else if ((tok = consume(TK_VOLATILE))) {
set_specifier(specifiers, SPEC_VOLATILE);
} else if ((tok = consume(TK_RESTRICT))) {
set_specifier(specifiers, SPEC_RESTRICT);
} else if ((tok = match(TK_STRUCT)) || (tok = match(TK_UNION))) {
if (struct_or_union_type)
errorat(tok, "two or more data types in declaration specifiers");
struct_or_union_type = struct_union_specifier();
} else if ((tok = match(TK_ENUM))) {
if (enum_type)
errorat(tok, "two or more data types in declaration specifiers");
enum_type = enum_specifier();
} else if ((tok = consume(TK_TYPEDEF))) {
if (!is_typedef)
errorat(tok, "unexpected typedef");
if (*is_typedef)
errorat(tok, "duplicate typedef");
*is_typedef = 1;
} else if (match(TK_IDENT)) {
Node n = find_symbol(token(), A_ANY, SCOPE_ALL);
if (typedef_type || !n || n->kind != A_TYPEDEF)
break;
expect(TK_IDENT);
typedef_type = n->type;
} else
break;
}
// http://port70.net/~nsz/c/c99/n1256.html#6.7.2
Type ty;
int type_spec = specifiers & ((1 << 8) - 1);
switch (type_spec) {
case SPEC_VOID:
ty = voidtype;
break;
case SPEC_CHAR:
case SPEC_CHAR | SPEC_SIGNED:
ty = chartype;
break;
case SPEC_CHAR | SPEC_UNSIGNED:
ty = uchartype;
break;
case SPEC_SHORT:
case SPEC_SIGNED | SPEC_SHORT:
case SPEC_SHORT | SPEC_INT:
case SPEC_SIGNED | SPEC_SHORT | SPEC_INT:
ty = shorttype;
break;
case SPEC_UNSIGNED | SPEC_SHORT:
case SPEC_UNSIGNED | SPEC_SHORT | SPEC_INT:
ty = ushorttype;
break;
case SPEC_INT:
case SPEC_SIGNED:
case SPEC_INT | SPEC_SIGNED:
ty = inttype;
break;
case SPEC_UNSIGNED:
case SPEC_UNSIGNED | SPEC_INT:
ty = uinttype;
break;
case SPEC_LONG1:
case SPEC_SIGNED | SPEC_LONG1:
case SPEC_LONG1 | SPEC_INT:
case SPEC_SIGNED | SPEC_LONG1 | SPEC_INT:
ty = longtype;
break;
case SPEC_UNSIGNED | SPEC_LONG1:
case SPEC_UNSIGNED | SPEC_LONG1 | SPEC_INT:
ty = ulongtype;
break;
case SPEC_LONG1 | SPEC_LONG2:
case SPEC_SIGNED | SPEC_LONG1 | SPEC_LONG2:
case SPEC_LONG1 | SPEC_LONG2 | SPEC_INT:
case SPEC_SIGNED | SPEC_LONG1 | SPEC_LONG2 | SPEC_INT:
ty = longtype;
break;
case SPEC_UNSIGNED | SPEC_LONG1 | SPEC_LONG2:
case SPEC_UNSIGNED | SPEC_LONG1 | SPEC_LONG2 | SPEC_INT:
ty = ulongtype;
break;
default:
ty = NULL;
}
if (ty && struct_or_union_type)
errorat(tok, "two or more data types in declaration specifiers");
ty = ty ? ty : struct_or_union_type;
if (ty && enum_type)
errorat(tok, "two or more data types in declaration specifiers");
ty = ty ? ty : enum_type;
if (ty && typedef_type)
errorat(tok, "two or more data types in declaration specifiers");
ty = ty ? ty : typedef_type;
if (!ty)
errorat(token(), "no data type in declaration specifiers");
// cache nothing in register, so all variable is VOLATILE?
if (get_specifier(specifiers, SPEC_CONST))
ty = const_type(ty);
if (get_specifier(specifiers, SPEC_RESTRICT))
errorat(tok, "not implemented: restrict");
return ty;
}
static Member mkmember(Type type, Token tok) {
Member p = calloc(1, sizeof(struct member));
p->type = type;
p->name = tok->name;
p->token = tok;
return p;
}
static void link_member(Member head, Member m) {
while (head->next) {
if (m->name && head->next->name == m->name)
errorat(token(), "redefinition of member %s", m->name);
head = head->next;
}
head->next = m;
}
static Member struct_declaration_list() {
Token tok;
struct member head = {0};
if (consume(TK_OPENING_BRACES)) {
while (!(tok = consume(TK_CLOSING_BRACES))) {
Type ty = declaration_specifiers(NULL);
Token mem_name = NULL;
Type mem_ty = declarator(ty, &mem_name);
if (!mem_name)
errorat(token(), "empty member name");
link_member(&head, mkmember(mem_ty, mem_name));
while (!consume(TK_SIMI)) {
expect(TK_COMMA);
mem_name = NULL;
mem_ty = declarator(ty, &mem_name);
if (!mem_name)
errorat(token(), "empty member name");
link_member(&head, mkmember(mem_ty, mem_name));
}
}
if (!head.next)
errorat(tok, "empty struct");
}
return head.next;
}
static Type struct_union_specifier() {
int ty_kind = consume(TK_STRUCT) ? TY_STRUCT : (expect(TK_UNION), TY_UNION);
Token tok = consume(TK_IDENT);
if (tok) {
char buff[4096];
sprintf(buff, ".tag.%s", tok->name);
tok->name = string(buff);
}
Member member = struct_declaration_list();
if (member) {
Type ty = struct_or_union_type(member, tok ? tok->name : NULL, ty_kind);
if (tok)
mktag(tok, ty);
return ty;
}
Node tag = find_tag(tok, ty_kind);
if (!tag)
tag = mktag(tok, struct_or_union_type(NULL, tok->name, ty_kind));
return tag->type;
}
static void enumerator_list() {
Token tok;
expect(TK_OPENING_BRACES);
int value = -1;
while (!consume(TK_CLOSING_BRACES)) {
tok = expect(TK_IDENT);
if (consume(TK_EQUAL))
value = mkiconsstr(consume(TK_NUM))->intvalue;
else
value++;
mkenumconst(tok, value);
if (!match(TK_CLOSING_BRACES))
expect(TK_COMMA);
}
}
static Type enum_specifier() {
expect(TK_ENUM);
Token tok = consume(TK_IDENT);
if (tok) {
char buff[4096];
sprintf(buff, ".tag.%s", tok->name);
tok->name = string(buff);
}
if (match(TK_OPENING_BRACES)) {
enumerator_list();
Type ty = enum_type(tok->name);
if (tok)
mktag(tok, ty);
return ty;
}
return find_tag(tok, TY_ENUM)->type;
}
// returned value for different kind of declarotor
// function: function node in global
// local variable: nodes for init assign
// global variable: NULL
static Node init_declarator(Type ty, int is_typedef) {
Token tok;
ty = declarator(ty, &tok);
if (!tok)
errorat(token(), "identifier name required in declarator");
if (is_typedef) {
mktypedef(tok, ty);