-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathparse.c
3985 lines (3457 loc) · 107 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 "rvcc.h"
// 局部变量,全局变量,typedef,enum常量的域
typedef struct {
Obj *Var; // 对应的变量
Type *Typedef; // 别名
Type *EnumTy; // 枚举的类型
int EnumVal; // 枚举的值
} VarScope;
// 表示一个块域
typedef struct Scope Scope;
struct Scope {
Scope *Next; // 指向上一级的域
// C有两个域:变量(或类型别名)域,结构体(或联合体,枚举)标签域
HashMap Vars; // 指向当前域内的变量
HashMap Tags; // 指向当前域内的结构体标签
};
// 变量属性
typedef struct {
bool IsTypedef; // 是否为类型别名
bool IsStatic; // 是否为文件域内
bool IsExtern; // 是否为外部变量
bool IsInline; // 是否为内联
bool IsTLS; // 是否为线程局部存储,Thread Local Storage
int Align; // 对齐量
} VarAttr;
// 可变的初始化器。此处为树状结构。
// 因为初始化器可以是嵌套的,
// 类似于 int x[2][2] = {{1, 2}, {3, 4}} ,
typedef struct Initializer Initializer;
struct Initializer {
Initializer *Next; // 下一个
Type *Ty; // 原始类型
Token *Tok; // 终结符
bool IsFlexible; // 可调整的,表示需要重新构造
// 如果不是聚合类型,并且有一个初始化器,Expr 有对应的初始化表达式。
Node *Expr;
// 如果是聚合类型(如数组或结构体),Children有子节点的初始化器
Initializer **Children;
// 联合体中只有一个成员能被初始化,此处用来标记是哪个成员被初始化
Member *Mem;
};
// 指派初始化,用于局部变量的初始化器
typedef struct InitDesig InitDesig;
struct InitDesig {
InitDesig *Next; // 下一个
int Idx; // 数组中的索引
Member *Mem; // 成员变量
Obj *Var; // 对应的变量
};
// 在解析时,全部的变量实例都被累加到这个列表里。
Obj *Locals; // 局部变量
Obj *Globals; // 全局变量
// 所有的域的链表
static Scope *Scp = &(Scope){};
// 指向当前正在解析的函数
static Obj *CurrentFn;
// 当前函数内的goto和标签列表
static Node *Gotos;
static Node *Labels;
// 当前goto跳转的目标
static char *BrkLabel;
// 当前continue跳转的目标
static char *ContLabel;
// 如果我们正在解析switch语句,则指向表示switch的节点。
// 否则为空。
static Node *CurrentSwitch;
// 内建的Alloca函数
static Obj *BuiltinAlloca;
// program = (typedef | functionDefinition | globalVariable)*
// functionDefinition = declspec declarator "{" compoundStmt*
// declspec = ("void" | "_Bool" | char" | "short" | "int" | "long"
// | "typedef" | "static" | "extern" | "inline"
// | "_Thread_local" | "__thread"
// | "_Alignas" ("(" typeName | constExpr ")")
// | "signed" | "unsigned"
// | structDecl | unionDecl | typedefName
// | enumSpecifier | typeofSpecifier
// | "const" | "volatile" | "auto" | "register" | "restrict"
// | "__restrict" | "__restrict__" | "_Noreturn")+
// enumSpecifier = ident? "{" enumList? "}"
// | ident ("{" enumList? "}")?
// enumList = ident ("=" constExpr)? ("," ident ("=" constExpr)?)* ","?
// declarator = pointers ("(" ident ")" | "(" declarator ")" | ident) typeSuffix
// pointers = ("*" ("const" | "volatile" | "restrict")*)*
// typeSuffix = "(" funcParams | "[" arrayDimensions | ε
// arrayDimensions = ("static" | "restrict")* constExpr? "]" typeSuffix
// funcParams = ("void" | param ("," param)* ("," "...")?)? ")"
// param = declspec declarator
// compoundStmt = (typedef | declaration | stmt)* "}"
// declaration = declspec (declarator ("=" initializer)?
// ("," declarator ("=" initializer)?)*)? ";"
// initializer = stringInitializer | arrayInitializer | structInitializer
// | unionInitializer |assign
// stringInitializer = stringLiteral
// arrayInitializer = arrayInitializer1 | arrayInitializer2
// arrayInitializer1 = "{" initializer ("," initializer)* ","? "}"
// arrayIntializer2 = initializer ("," initializer)* ","?
// structInitializer = structInitializer1 | structInitializer2
// structInitializer1 = "{" initializer ("," initializer)* ","? "}"
// structIntializer2 = initializer ("," initializer)* ","?
// unionInitializer = "{" initializer "}"
// stmt = "return" expr? ";"
// | "if" "(" expr ")" stmt ("else" stmt)?
// | "switch" "(" expr ")" stmt
// | "case" constExpr ("..." constExpr)? ":" stmt
// | "default" ":" stmt
// | "for" "(" exprStmt expr? ";" expr? ")" stmt
// | "while" "(" expr ")" stmt
// | "do" stmt "while" "(" expr ")" ";"
// | asmStmt
// | "goto" (ident | "*" expr) ";"
// | "break" ";"
// | "continue" ";"
// | ident ":" stmt
// | "{" compoundStmt
// | exprStmt
// asmStmt = "asm" ("volatile" | "inline")* "(" stringLiteral ")"
// exprStmt = expr? ";"
// expr = assign ("," expr)?
// assign = conditional (assignOp assign)?
// conditional = logOr ("?" expr? ":" conditional)?
// logOr = logAnd ("||" logAnd)*
// logAnd = bitOr ("&&" bitOr)*
// bitOr = bitXor ("|" bitXor)*
// bitXor = bitAnd ("^" bitAnd)*
// bitAnd = equality ("&" equality)*
// assignOp = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^="
// | "<<=" | ">>="
// equality = relational ("==" relational | "!=" relational)*
// relational = shift ("<" shift | "<=" shift | ">" shift | ">=" shift)*
// shift = add ("<<" add | ">>" add)*
// add = mul ("+" mul | "-" mul)*
// mul = cast ("*" cast | "/" cast | "%" cast)*
// cast = "(" typeName ")" cast | unary
// unary = ("+" | "-" | "*" | "&" | "!" | "~") cast
// | ("++" | "--") unary
// | "&&" ident
// | postfix
// structMembers = (declspec declarator ("," declarator)* ";")*
// structDecl = structUnionDecl
// unionDecl = structUnionDecl
// structUnionDecl = attribute? ident? ("{" structMembers)?
// attribute = ("__attribute__" "(" "(" ("packed")
// | ("aligned" "(" N ")") ")" ")")*
// postfix = "(" typeName ")" "{" initializerList "}"
// = ident "(" funcArgs ")" postfixTail*
// | primary postfixTail*
//
// postfixTail = "[" expr "]"
// | "(" funcArgs ")"
// | "." ident
// | "->" ident
// | "++"
// | "--"
// primary = "(" "{" stmt+ "}" ")"
// | "(" expr ")"
// | "sizeof" "(" typeName ")"
// | "sizeof" unary
// | "_Alignof" "(" typeName ")"
// | "_Alignof" unary
// | "_Generic" genericSelection
// | "__builtin_types_compatible_p" "(" typeName, typeName, ")"
// | ident
// | str
// | num
// genericSelection = "(" assign "," genericAssoc ("," genericAssoc)* ")"
// genericAssoc = typeName ":" assign
// | "default" ":" assign
// typeName = declspec abstractDeclarator
// abstractDeclarator = pointers ("(" abstractDeclarator ")")? typeSuffix
// funcall = (assign ("," assign)*)? ")"
static bool isTypename(Token *Tok);
static Type *declspec(Token **Rest, Token *Tok, VarAttr *Attr);
static Type *typename(Token **Rest, Token *Tok);
static Type *enumSpecifier(Token **Rest, Token *Tok);
static Type *typeofSpecifier(Token **Rest, Token *Tok);
static Type *typeSuffix(Token **Rest, Token *Tok, Type *Ty);
static Type *declarator(Token **Rest, Token *Tok, Type *Ty);
static Node *declaration(Token **Rest, Token *Tok, Type *BaseTy, VarAttr *Attr);
static void arrayInitializer2(Token **Rest, Token *Tok, Initializer *Init,
int I);
static void structInitializer2(Token **Rest, Token *Tok, Initializer *Init,
Member *Mem);
static void initializer2(Token **Rest, Token *Tok, Initializer *Init);
static Initializer *initializer(Token **Rest, Token *Tok, Type *Ty,
Type **NewTy);
static Node *LVarInitializer(Token **Rest, Token *Tok, Obj *Var);
static void GVarInitializer(Token **Rest, Token *Tok, Obj *Var);
static Node *compoundStmt(Token **Rest, Token *Tok);
static Node *stmt(Token **Rest, Token *Tok);
static Node *exprStmt(Token **Rest, Token *Tok);
static Node *expr(Token **Rest, Token *Tok);
static int64_t eval(Node *Nd);
static int64_t eval2(Node *Nd, char ***Label);
static int64_t evalRVal(Node *Nd, char ***Label);
static double evalDouble(Node *Nd);
static bool isConstExpr(Node *Nd);
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 *newAdd(Node *LHS, Node *RHS, Token *Tok);
static Node *newSub(Node *LHS, Node *RHS, Token *Tok);
static Node *mul(Token **Rest, Token *Tok);
static Node *cast(Token **Rest, Token *Tok);
static Member *getStructMember(Type *Ty, Token *Tok);
static Type *structDecl(Token **Rest, Token *Tok);
static Type *unionDecl(Token **Rest, Token *Tok);
static Node *unary(Token **Rest, Token *Tok);
static Node *postfix(Token **Rest, Token *Tok);
static Node *funCall(Token **Rest, Token *Tok, Node *Nd);
static Node *primary(Token **Rest, Token *Tok);
static Token *parseTypedef(Token *Tok, Type *BaseTy);
static bool isFunction(Token *Tok);
static Token *function(Token *Tok, Type *BaseTy, VarAttr *Attr);
static Token *globalVariable(Token *Tok, Type *Basety, VarAttr *Attr);
// 向下对齐值
// N % Align != 0 , 即 N 未对齐时, AlignDown(N) = AlignTo(N) - Align
// N % Align == 0 , 即 N 已对齐时, AlignDown(N) = AlignTo(N)
static int alignDown(int N, int Align) { return alignTo(N - Align + 1, Align); }
// 进入域
static void enterScope(void) {
Scope *S = calloc(1, sizeof(Scope));
// 后来的在链表头部
// 类似于栈的结构,栈顶对应最近的域
S->Next = Scp;
Scp = S;
}
// 结束当前域
static void leaveScope(void) { Scp = Scp->Next; }
// 通过名称,查找一个变量
static VarScope *findVar(Token *Tok) {
// 此处越先匹配的域,越深层
for (Scope *S = Scp; S; S = S->Next) {
// 遍历域内的所有变量
VarScope *S2 = hashmapGet2(&S->Vars, Tok->Loc, Tok->Len);
if (S2)
return S2;
}
return NULL;
}
// 通过Token查找标签
static Type *findTag(Token *Tok) {
for (Scope *S = Scp; S; S = S->Next) {
Type *Ty = hashmapGet2(&S->Tags, Tok->Loc, Tok->Len);
if (Ty)
return Ty;
}
return NULL;
}
// 新建一个节点
static Node *newNode(NodeKind Kind, Token *Tok) {
Node *Nd = calloc(1, sizeof(Node));
Nd->Kind = Kind;
Nd->Tok = Tok;
return Nd;
}
// 新建一个单叉树
static Node *newUnary(NodeKind Kind, Node *Expr, Token *Tok) {
Node *Nd = newNode(Kind, Tok);
Nd->LHS = Expr;
return Nd;
}
// 新建一个二叉树节点
static Node *newBinary(NodeKind Kind, Node *LHS, Node *RHS, Token *Tok) {
Node *Nd = newNode(Kind, Tok);
Nd->LHS = LHS;
Nd->RHS = RHS;
return Nd;
}
// 新建一个数字节点
static Node *newNum(int64_t Val, Token *Tok) {
Node *Nd = newNode(ND_NUM, Tok);
Nd->Val = Val;
return Nd;
}
// 新建一个长整型节点
static Node *newLong(int64_t Val, Token *Tok) {
Node *Nd = newNode(ND_NUM, Tok);
Nd->Val = Val;
Nd->Ty = TyLong;
return Nd;
}
// 新建一个无符号长整型节点
static Node *newULong(long Val, Token *Tok) {
Node *node = newNode(ND_NUM, Tok);
node->Val = Val;
node->Ty = TyULong;
return node;
}
// 新变量
static Node *newVarNode(Obj *Var, Token *Tok) {
Node *Nd = newNode(ND_VAR, Tok);
Nd->Var = Var;
return Nd;
}
// VLA指针
static Node *newVLAPtr(Obj *Var, Token *Tok) {
Node *Nd = newNode(ND_VLA_PTR, Tok);
Nd->Var = Var;
return Nd;
}
// 新转换
Node *newCast(Node *Expr, Type *Ty) {
addType(Expr);
Node *Nd = calloc(1, sizeof(Node));
Nd->Kind = ND_CAST;
Nd->Tok = Expr->Tok;
Nd->LHS = Expr;
Nd->Ty = copyType(Ty);
return Nd;
}
// 将变量存入当前的域中
static VarScope *pushScope(char *Name) {
VarScope *S = calloc(1, sizeof(VarScope));
hashmapPut(&Scp->Vars, Name, S);
return S;
}
// 新建初始化器
static Initializer *newInitializer(Type *Ty, bool IsFlexible) {
Initializer *Init = calloc(1, sizeof(Initializer));
// 存储原始类型
Init->Ty = Ty;
// 处理数组类型
if (Ty->Kind == TY_ARRAY) {
// 判断是否需要调整数组元素数并且数组不完整
if (IsFlexible && Ty->Size < 0) {
// 设置初始化器为可调整的,之后进行完数组元素数的计算后,再构造初始化器
Init->IsFlexible = true;
return Init;
}
// 为数组的最外层的每个元素分配空间
Init->Children = calloc(Ty->ArrayLen, sizeof(Initializer *));
// 遍历解析数组最外层的每个元素
for (int I = 0; I < Ty->ArrayLen; ++I)
Init->Children[I] = newInitializer(Ty->Base, false);
}
// 处理结构体和联合体
if (Ty->Kind == TY_STRUCT || Ty->Kind == TY_UNION) {
// 计算结构体成员的数量
int Len = 0;
for (Member *Mem = Ty->Mems; Mem; Mem = Mem->Next)
++Len;
// 初始化器的子项
Init->Children = calloc(Len, sizeof(Initializer *));
// 遍历子项进行赋值
for (Member *Mem = Ty->Mems; Mem; Mem = Mem->Next) {
// 判断结构体是否是灵活的,同时成员也是灵活的并且是最后一个
// 在这里直接构造,避免对于灵活数组的解析
if (IsFlexible && Ty->IsFlexible && !Mem->Next) {
Initializer *Child = calloc(1, sizeof(Initializer));
Child->Ty = Mem->Ty;
Child->IsFlexible = true;
Init->Children[Mem->Idx] = Child;
} else {
// 对非灵活子项进行赋值
Init->Children[Mem->Idx] = newInitializer(Mem->Ty, false);
}
}
return Init;
}
return Init;
}
// 新建变量
static Obj *newVar(char *Name, Type *Ty) {
Obj *Var = calloc(1, sizeof(Obj));
Var->Name = Name;
Var->Ty = Ty;
// 设置变量默认的对齐量为类型的对齐量
Var->Align = Ty->Align;
pushScope(Name)->Var = Var;
return Var;
}
// 在链表中新增一个局部变量
static Obj *newLVar(char *Name, Type *Ty) {
Obj *Var = newVar(Name, Ty);
Var->IsLocal = true;
// 将变量插入头部
Var->Next = Locals;
Locals = Var;
return Var;
}
// 在链表中新增一个全局变量
static Obj *newGVar(char *Name, Type *Ty) {
Obj *Var = newVar(Name, Ty);
Var->Next = Globals;
// static全局变量
Var->IsStatic = true;
// 存在定义
Var->IsDefinition = true;
Globals = Var;
return Var;
}
// 新增唯一名称
static char *newUniqueName(void) {
static int Id = 0;
return format(".L..%d", Id++);
}
// 新增匿名全局变量
static Obj *newAnonGVar(Type *Ty) { return newGVar(newUniqueName(), Ty); }
// 新增字符串字面量
static Obj *newStringLiteral(char *Str, Type *Ty) {
Obj *Var = newAnonGVar(Ty);
Var->InitData = Str;
return Var;
}
// 获取标识符
static char *getIdent(Token *Tok) {
if (Tok->Kind != TK_IDENT)
errorTok(Tok, "expected an identifier");
return strndup(Tok->Loc, Tok->Len);
}
// 查找类型别名
static Type *findTypedef(Token *Tok) {
// 类型别名是个标识符
if (Tok->Kind == TK_IDENT) {
// 查找是否存在于变量域内
VarScope *S = findVar(Tok);
if (S)
return S->Typedef;
}
return NULL;
}
static void pushTagScope(Token *Tok, Type *Ty) {
hashmapPut2(&Scp->Tags, Tok->Loc, Tok->Len, Ty);
}
// declspec = ("void" | "_Bool" | char" | "short" | "int" | "long"
// | "typedef" | "static" | "extern" | "inline"
// | "_Thread_local" | "__thread"
// | "_Alignas" ("(" typeName | constExpr ")")
// | "signed" | "unsigned"
// | structDecl | unionDecl | typedefName
// | enumSpecifier | typeofSpecifier
// | "const" | "volatile" | "auto" | "register" | "restrict"
// | "__restrict" | "__restrict__" | "_Noreturn")+
// declarator specifier
static Type *declspec(Token **Rest, Token *Tok, VarAttr *Attr) {
// 类型的组合,被表示为例如:LONG+LONG=1<<9
// 可知long int和int long是等价的。
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 = TyInt;
int Counter = 0; // 记录类型相加的数值
bool IsAtomic = false; // 标记是否为原子的
// 遍历所有类型名的Tok
while (isTypename(Tok)) {
// 处理typedef等关键字
if (equal(Tok, "typedef") || equal(Tok, "static") || equal(Tok, "extern") ||
equal(Tok, "inline") || equal(Tok, "_Thread_local") ||
equal(Tok, "__thread")) {
if (!Attr)
errorTok(Tok, "storage class specifier is not allowed in this context");
if (equal(Tok, "typedef"))
Attr->IsTypedef = true;
else if (equal(Tok, "static"))
Attr->IsStatic = true;
else if (equal(Tok, "extern"))
Attr->IsExtern = true;
else if (equal(Tok, "inline"))
Attr->IsInline = true;
else
Attr->IsTLS = true;
// typedef不应与static/extern/inline/__thread/_Thread_local一起使用
if (Attr->IsTypedef &&
(Attr->IsStatic || Attr->IsExtern || Attr->IsInline || Attr->IsTLS))
errorTok(Tok, "typedef and static/extern/inline/__thread/_Thread_local "
"may not be used together");
Tok = Tok->Next;
continue;
}
// 识别这些关键字并忽略
if (consume(&Tok, Tok, "const") || consume(&Tok, Tok, "volatile") ||
consume(&Tok, Tok, "auto") || consume(&Tok, Tok, "register") ||
consume(&Tok, Tok, "restrict") || consume(&Tok, Tok, "__restrict") ||
consume(&Tok, Tok, "__restrict__") || consume(&Tok, Tok, "_Noreturn"))
continue;
// 匹配是否为原子的
if (equal(Tok, "_Atomic")) {
Tok = Tok->Next;
if (equal(Tok, "(")) {
Ty = typename(&Tok, Tok->Next);
Tok = skip(Tok, ")");
}
IsAtomic = true;
continue;
}
// _Alignas "(" typeName | constExpr ")"
if (equal(Tok, "_Alignas")) {
// 不存在变量属性时,无法设置对齐值
if (!Attr)
errorTok(Tok, "_Alignas is not allowed in this context");
Tok = skip(Tok->Next, "(");
// 判断是类型名,或者常量表达式
if (isTypename(Tok))
Attr->Align = typename(&Tok, Tok)->Align;
else
Attr->Align = constExpr(&Tok, Tok);
Tok = skip(Tok, ")");
continue;
}
// 处理用户定义的类型
Type *Ty2 = findTypedef(Tok);
if (equal(Tok, "struct") || equal(Tok, "union") || equal(Tok, "enum") ||
equal(Tok, "typeof") || Ty2) {
if (Counter)
break;
if (equal(Tok, "struct")) {
Ty = structDecl(&Tok, Tok->Next);
} else if (equal(Tok, "union")) {
Ty = unionDecl(&Tok, Tok->Next);
} else if (equal(Tok, "enum")) {
Ty = enumSpecifier(&Tok, Tok->Next);
} else if (equal(Tok, "typeof")) {
Ty = typeofSpecifier(&Tok, Tok->Next);
} else {
// 将类型设为类型别名指向的类型
Ty = Ty2;
Tok = Tok->Next;
}
Counter += OTHER;
continue;
}
// 对于出现的类型名加入Counter
// 每一步的Counter都需要有合法值
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
unreachable();
// 根据Counter值映射到对应的Type
switch (Counter) {
case VOID:
Ty = TyVoid;
break;
case BOOL:
Ty = TyBool;
break;
case SIGNED + CHAR:
Ty = TyChar;
break;
// RISCV当中char是无符号类型的
case CHAR:
case UNSIGNED + CHAR:
Ty = TyUChar;
break;
case SHORT:
case SHORT + INT:
case SIGNED + SHORT:
case SIGNED + SHORT + INT:
Ty = TyShort;
break;
case UNSIGNED + SHORT:
case UNSIGNED + SHORT + INT:
Ty = TyUShort;
break;
case INT:
case SIGNED:
case SIGNED + INT:
Ty = TyInt;
break;
case UNSIGNED:
case UNSIGNED + INT:
Ty = TyUInt;
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 = TyLong;
break;
case UNSIGNED + LONG:
case UNSIGNED + LONG + INT:
case UNSIGNED + LONG + LONG:
case UNSIGNED + LONG + LONG + INT:
Ty = TyULong;
break;
case FLOAT:
Ty = TyFloat;
break;
case DOUBLE:
Ty = TyDouble;
break;
case LONG + DOUBLE:
Ty = TyLDouble;
break;
default:
errorTok(Tok, "invalid type");
}
Tok = Tok->Next;
} // while (isTypename(Tok))
if (IsAtomic) {
Ty = copyType(Ty);
// 类型被标记为原子的
Ty->IsAtomic = true;
}
*Rest = Tok;
return Ty;
}
// funcParams = ("void" | param ("," param)* ("," "...")?)? ")"
// param = declspec declarator
static Type *funcParams(Token **Rest, Token *Tok, Type *Ty) {
// "void"
if (equal(Tok, "void") && equal(Tok->Next, ")")) {
*Rest = Tok->Next->Next;
return funcType(Ty);
}
Type Head = {};
Type *Cur = &Head;
bool IsVariadic = false;
while (!equal(Tok, ")")) {
// funcParams = param ("," param)*
// param = declspec declarator
if (Cur != &Head)
Tok = skip(Tok, ",");
// ("," "...")?
if (equal(Tok, "...")) {
IsVariadic = true;
Tok = Tok->Next;
skip(Tok, ")");
break;
}
Type *Ty2 = declspec(&Tok, Tok, NULL);
Ty2 = declarator(&Tok, Tok, Ty2);
// 存储名称
Token *Name = Ty2->Name;
// T类型的数组或函数被转换为T*
if (Ty2->Kind == TY_ARRAY) {
Ty2 = pointerTo(Ty2->Base);
Ty2->Name = Name;
} else if (Ty2->Kind == TY_FUNC) {
Ty2 = pointerTo(Ty2);
Ty2->Name = Name;
}
// 将类型复制到形参链表一份
Cur->Next = copyType(Ty2);
Cur = Cur->Next;
}
// 设置空参函数调用为可变的
if (Cur == &Head)
IsVariadic = true;
// 封装一个函数节点
Ty = funcType(Ty);
// 传递形参
Ty->Params = Head.Next;
// 传递可变参数
Ty->IsVariadic = IsVariadic;
*Rest = Tok->Next;
return Ty;
}
// 数组维数
// arrayDimensions = ("static" | "restrict")* constExpr? "]" typeSuffix
static Type *arrayDimensions(Token **Rest, Token *Tok, Type *Ty) {
// ("static" | "restrict")*
while (equal(Tok, "static") || equal(Tok, "restrict"))
Tok = Tok->Next;
// "]" 无数组维数的 "[]"
if (equal(Tok, "]")) {
Ty = typeSuffix(Rest, Tok->Next, Ty);
return arrayOf(Ty, -1);
}
// 有数组维数的情况
Node *Expr = conditional(&Tok, Tok);
Tok = skip(Tok, "]");
Ty = typeSuffix(Rest, Tok, Ty);
// 处理可变长度数组
if (Ty->Kind == TY_VLA || !isConstExpr(Expr))
return VLAOf(Ty, Expr);
// 处理固定长度数组
return arrayOf(Ty, eval(Expr));
}
// typeSuffix = "(" funcParams | "[" arrayDimensions | ε
static Type *typeSuffix(Token **Rest, Token *Tok, Type *Ty) {
// "(" funcParams
if (equal(Tok, "("))
return funcParams(Rest, Tok->Next, Ty);
// "[" arrayDimensions
if (equal(Tok, "["))
return arrayDimensions(Rest, Tok->Next, Ty);
*Rest = Tok;
return Ty;
}
// pointers = ("*" ("const" | "volatile" | "restrict")*)*
static Type *pointers(Token **Rest, Token *Tok, Type *Ty) {
// "*"*
// 构建所有的(多重)指针
while (consume(&Tok, Tok, "*")) {
Ty = pointerTo(Ty);
// 识别这些关键字并忽略
while (equal(Tok, "const") || equal(Tok, "volatile") ||
equal(Tok, "restrict") || equal(Tok, "__restrict") ||
equal(Tok, "__restrict__"))
Tok = Tok->Next;
}
*Rest = Tok;
return Ty;
}
// declarator = pointers ("(" ident ")" | "(" declarator ")" | ident) typeSuffix
static Type *declarator(Token **Rest, Token *Tok, Type *Ty) {
// pointers
Ty = pointers(&Tok, Tok, Ty);
// "(" declarator ")"
if (equal(Tok, "(")) {
// 记录"("的位置
Token *Start = Tok;
Type Dummy = {};
// 使Tok前进到")"后面的位置
declarator(&Tok, Start->Next, &Dummy);
Tok = skip(Tok, ")");
// 获取到括号后面的类型后缀,Ty为解析完的类型,Rest指向分号
Ty = typeSuffix(Rest, Tok, Ty);
// 解析Ty整体作为Base去构造,返回Type的值
return declarator(&Tok, Start->Next, Ty);
}
// 默认名称为空
Token *Name = NULL;
// 名称位置指向类型后的区域
Token *NamePos = Tok;
// 存在名字则赋值
if (Tok->Kind == TK_IDENT) {
Name = Tok;
Tok = Tok->Next;
}
// typeSuffix
Ty = typeSuffix(Rest, Tok, Ty);
// ident
// 变量名 或 函数名
Ty->Name = Name;
Ty->NamePos = NamePos;
return Ty;
}
// abstractDeclarator = pointers ("(" abstractDeclarator ")")? typeSuffix
static Type *abstractDeclarator(Token **Rest, Token *Tok, Type *Ty) {
// pointers
Ty = pointers(&Tok, Tok, Ty);
// ("(" abstractDeclarator ")")?
if (equal(Tok, "(")) {
Token *Start = Tok;
Type Dummy = {};
// 使Tok前进到")"后面的位置
abstractDeclarator(&Tok, Start->Next, &Dummy);
Tok = skip(Tok, ")");
// 获取到括号后面的类型后缀,Ty为解析完的类型,Rest指向分号
Ty = typeSuffix(Rest, Tok, Ty);
// 解析Ty整体作为Base去构造,返回Type的值
return abstractDeclarator(&Tok, Start->Next, Ty);
}
// typeSuffix
return typeSuffix(Rest, Tok, Ty);
}
// typeName = declspec abstractDeclarator
// 获取类型的相关信息
static Type *typename(Token **Rest, Token *Tok) {
// declspec
Type *Ty = declspec(&Tok, Tok, NULL);
// abstractDeclarator
return abstractDeclarator(Rest, Tok, Ty);
}
// 判断是否终结符匹配到了结尾
static bool isEnd(Token *Tok) {
// "}" | ",}"
return equal(Tok, "}") || (equal(Tok, ",") && equal(Tok->Next, "}"));
}
// 消耗掉结尾的终结符
// "}" | ",}"
static bool consumeEnd(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;
}
// 获取枚举类型信息
// enumSpecifier = ident? "{" enumList? "}"
// | ident ("{" enumList? "}")?
// enumList = ident ("=" constExpr)? ("," ident ("=" constExpr)?)* ","?
static Type *enumSpecifier(Token **Rest, Token *Tok) {
Type *Ty = enumType();
// 读取标签
// ident?
Token *Tag = NULL;
if (Tok->Kind == TK_IDENT) {
Tag = Tok;
Tok = Tok->Next;
}
// 处理没有{}的情况
if (Tag && !equal(Tok, "{")) {
Type *Ty = findTag(Tag);
if (!Ty)
errorTok(Tag, "unknown enum type");
if (Ty->Kind != TY_ENUM)
errorTok(Tag, "not an enum tag");
*Rest = Tok;
return Ty;
}
// "{" enumList? "}"
Tok = skip(Tok, "{");
// enumList
// 读取枚举列表
int I = 0; // 第几个枚举常量
int Val = 0; // 枚举常量的值
while (!consumeEnd(Rest, Tok)) {
if (I++ > 0)
Tok = skip(Tok, ",");
char *Name = getIdent(Tok);
Tok = Tok->Next;
// 判断是否存在赋值
if (equal(Tok, "="))
Val = constExpr(&Tok, Tok->Next);
// 存入枚举常量
VarScope *S = pushScope(Name);
S->EnumTy = Ty;
S->EnumVal = Val++;
}
if (Tag)
pushTagScope(Tag, Ty);
return Ty;
}
// typeofSpecifier = "(" (expr | typename) ")"
// typeof 获取对应的类型
static Type *typeofSpecifier(Token **Rest, Token *Tok) {
// "("
Tok = skip(Tok, "(");
Type *Ty;
if (isTypename(Tok)) {
// typename
// 匹配到相应的类型
Ty = typename(&Tok, Tok);
} else {
// expr
// 计算表达式,然后获取表达式的类型
Node *Nd = expr(&Tok, Tok);
addType(Nd);
Ty = Nd->Ty;
}
// ")"
*Rest = skip(Tok, ")");
// 将获取的类型进行返回
return Ty;
}