-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathast_builder.c2
More file actions
1094 lines (981 loc) · 39.3 KB
/
ast_builder.c2
File metadata and controls
1094 lines (981 loc) · 39.3 KB
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
/* Copyright 2022-2026 Bas van den Berg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module ast_builder;
import ast local;
import ast_context local;
import attr_handler;
import attr local;
import component;
import diagnostics;
import number_radix local;
import src_loc local;
import string_pool;
import stdlib local;
public type Builder struct @(opaque) {
Context* context; // no ownership
diagnostics.Diags* diags; // no ownership
string_pool.Pool* auxPool; // no ownership
component.Component* comp; // no ownership
Module* mod; // no ownership
AST* ast;
u32 ast_idx;
u32 c2_name;
u32 main_name;
bool is_interface;
bool is_private;
Attr[8] attrs;
u32 num_attrs;
attr_handler.Handler* attr_handler; // no ownership
}
public fn Builder* create(Context* context,
diagnostics.Diags* diags,
string_pool.Pool* auxPool,
u32 c2_name,
u32 main_name,
attr_handler.Handler* attr_handler_)
{
Builder* b = calloc(1, sizeof(Builder));
b.context = context;
b.diags = diags;
b.auxPool = auxPool;
b.c2_name = c2_name;
b.main_name = main_name;
b.attr_handler = attr_handler_;
return b;
}
public fn void Builder.free(Builder* b) {
free(b);
}
public fn void Builder.setComponent(Builder* b, component.Component* comp) {
b.comp = comp;
b.mod = nil;
b.is_interface = !comp.hasSources();
b.is_private = comp.isExternalSourceLib(); // all non-exported modules created here will be private
}
public fn void Builder.actOnModule(Builder* b,
u32 mod_name,
SrcLoc mod_loc,
u32 filename,
bool is_interface,
bool is_generated) {
assert(b.comp);
if (mod_name == b.c2_name && !is_interface) {
b.diags.error(mod_loc, "module name 'c2' is reserved");
stdlib.exit(-1);
}
if (mod_name == b.main_name) {
b.diags.error(mod_loc, "module name 'main' is reserved");
stdlib.exit(-1);
}
b.mod = b.comp.getOrAddModule(mod_name, b.is_private);
b.ast = b.mod.add(b.auxPool, filename, is_interface, is_generated);
b.ast_idx = b.ast.getIdx();
// NOTE: make special ImportDecl to add own symbols
ImportDecl* i = ImportDecl.create(b.context, mod_name, mod_loc, 0, 0, b.ast_idx, true);
Decl* d = (Decl*)i;
d.setUsed();
d.setChecked();
i.setDest(b.mod);
d.setType(QualType.create((Type*)b.mod.getType()));
b.ast.addImport(i);
}
public fn void Builder.actOnImport(Builder* b,
u32 mod_name,
SrcLoc mod_loc,
u32 alias_name,
SrcLoc alias_loc,
bool islocal) {
if (b.ast.getNameIdx() == mod_name) {
if (mod_loc) {
// reject self imports, no error for implicit imports (eg: varargs)
b.diags.error(mod_loc, "cannot import own module '%s'", idx2name(mod_name));
}
return;
}
ImportDecl* old = b.ast.findImport(mod_name);
if (old) {
if (mod_loc) {
// reject duplicate imports, no error for implicit imports
b.diags.error(mod_loc, "duplicate import of module '%s'", idx2name(mod_name));
b.diags.note(old.asDecl().getLoc(), "previous import is here");
}
return;
}
if (alias_name) {
if (alias_name == mod_name) {
b.diags.error(alias_loc, "alias name is same as module name");
return;
}
// Note: other clashes are done by Scope later
}
ImportDecl* d = ImportDecl.create(b.context,
mod_name,
mod_loc,
alias_name,
alias_loc,
b.ast_idx,
islocal);
b.ast.addImport(d);
}
public fn Decl* Builder.actOnAliasType(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* ref) {
is_public |= b.is_interface;
AliasTypeDecl* d = AliasTypeDecl.create(b.context, name, loc, is_public, b.ast_idx, ref);
b.ast.addTypeDecl(d.asDecl());
Decl* dd = (Decl*)d;
if (b.is_interface) dd.setExternal();
b.addSymbol(name, dd);
return dd;
}
public fn Decl* Builder.actOnFunctionTypeDecl(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* rtype,
VarDecl** params,
u32 num_params,
bool is_variadic)
{
is_public |= b.is_interface;
FunctionDecl* fd = FunctionDecl.create(b.context,
name,
loc,
is_public,
b.ast_idx,
rtype,
nil,
params,
num_params,
is_variadic,
DefKind.Type);
FunctionTypeDecl* d = FunctionTypeDecl.create(b.context, fd);
b.ast.addTypeDecl(d.asDecl());
Decl* dd = (Decl*)d;
if (b.is_interface) {
dd.setExternal();
fd.asDecl().setExternal();
}
b.addSymbol(name, dd);
return dd;
}
public fn Decl* Builder.actOnFunctionType(Builder* b,
const TypeRefHolder* rtype,
VarDecl** params,
u32 num_params,
bool is_variadic,
DefKind def_kind)
{
FunctionDecl* fd = FunctionDecl.create(b.context,
0, 0, false,
b.ast_idx,
rtype,
nil,
params,
num_params,
is_variadic,
def_kind);
return (Decl*)fd;
}
public fn StructTypeDecl* Builder.actOnStructType(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
bool is_struct,
bool is_global,
Decl** members,
u32 num_members)
{
is_public |= b.is_interface;
StructTypeDecl* d = StructTypeDecl.create(b.context,
name,
loc,
is_public,
b.ast_idx,
is_struct,
is_global,
members,
num_members);
if (is_global) {
b.ast.addTypeDecl(d.asDecl());
b.addSymbol(name, d.asDecl());
}
if (b.is_interface) d.asDecl().setExternal();
return d;
}
public fn VarDecl* Builder.actOnStructMember(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* ref,
Expr* bitfield)
{
is_public |= b.is_interface;
return VarDecl.createStructMember(b.context,
name,
loc,
is_public,
ref,
b.ast_idx,
bitfield);
}
public fn FieldInitInfo* Builder.actOnFieldInitInfo(Builder* b, StructTypeDecl* std) {
return std.createFieldInfo(b.context);
}
public fn Decl* Builder.actOnGlobalVarDecl(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
TypeRefHolder* ref,
SrcLoc assignLoc,
bool has_embed,
Expr* initValue)
{
is_public |= b.is_interface;
VarDecl* vd = VarDecl.create(b.context,
VarDeclKind.GlobalVar,
name,
loc,
is_public,
ref,
b.ast_idx,
assignLoc,
has_embed,
initValue);
Decl* d = vd.asDecl();
b.ast.addVarDecl(d);
b.addSymbol(name, d);
if (b.is_interface) d.setExternal();
return d;
}
public fn VarDecl* Builder.actOnFunctionParam(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* ref,
SrcLoc assignLoc,
Expr* initValue)
{
is_public |= b.is_interface;
return VarDecl.create(b.context,
VarDeclKind.FunctionParam,
name,
loc,
is_public,
ref,
b.ast_idx,
assignLoc,
false,
initValue);
}
public fn Stmt* Builder.actOnDeclStmt(Builder* b, VarDecl** decl, u32 count)
{
return (Stmt*)DeclStmt.create(b.context, decl, count);
}
public fn VarDecl* Builder.actOnVarDecl(Builder* b,
u32 name,
SrcLoc loc,
const TypeRefHolder* ref,
SrcLoc assignLoc,
Expr* initValue,
bool has_local,
bool has_init_call)
{
VarDecl* d = VarDecl.create(b.context,
VarDeclKind.LocalVar,
name,
loc,
false,
ref,
b.ast_idx,
assignLoc,
false,
initValue);
d.setLocal(has_local);
d.setInitCall(has_init_call);
return d;
}
fn void Builder.storeAttr(Builder* b, Decl* d, const Attr* a) {
d.setHasAttr();
b.ast.storeAttr(d, a);
}
public fn void Builder.actOnArrayValue(Builder* b,
u32 name,
SrcLoc loc,
Expr* initValue)
{
ArrayValue* avd = ArrayValue.create(b.context, name, loc, initValue);
b.ast.addArrayValue(avd);
}
fn void Builder.actOnFunctionAttr(Builder* b, Decl* d, const Attr* a) {
FunctionDecl* fd = (FunctionDecl*)d;
switch (a.kind) {
case Export:
d.setAttrExport();
break;
case Unused:
d.setAttrUnused();
break;
case UnusedParams:
fd.setAttrUnusedParams();
break;
case Section:
b.storeAttr(d, a);
break;
case NoReturn:
fd.setAttrNoReturn();
break;
case Inline:
fd.setAttrInline();
break;
case Weak:
if (!d.isPublic()) b.diags.error(a.loc, "weak declarations must be public");
fd.setAttrWeak();
break;
case CName:
case CDef:
b.storeAttr(d, a);
break;
case Constructor:
fd.setAttrConstructor();
break;
case Destructor:
fd.setAttrDestructor();
break;
case Pure:
fd.setAttrPure();
break;
case Deprecated:
fd.setAttrDeprecated();
b.storeAttr(d, a);
break;
default:
b.diags.error(a.loc, "attribute '%s' is not applicable to functions", a.kind2name());
break;
}
}
fn void Builder.actOnStructAttr(Builder* b, Decl* d, const Attr* a) {
StructTypeDecl* std = (StructTypeDecl*)d;
switch (a.kind) {
case Export:
d.setAttrExport();
break;
case Packed:
std.setPacked();
break;
case Unused:
d.setAttrUnused();
break;
case Section:
b.diags.error(a.loc, "attribute '%s' cannot be applied to type declarations",
a.kind2name());
break;
case Aligned:
std.setAttrAlignment(a.value.number);
break;
case Opaque:
if (!d.isPublic()) {
b.diags.error(a.loc, "opaque declaration must be public");
}
std.setOpaque();
break;
case CName:
b.storeAttr(d, a);
break;
case NoTypeDef:
if (b.is_interface) {
std.setAttrNoTypeDef();
} else {
b.diags.error(a.loc, "attribute '%s' can only be used in interfaces",
a.kind2name());
}
break;
default:
b.diags.error(a.loc, "attribute '%s' is not applicable to structs",
a.kind2name());
break;
}
}
fn bool Builder.actOnTypeAttr(Builder* b, Decl* d, const Attr* a) {
switch (a.kind) {
case Export:
d.setAttrExport();
break;
case Packed:
b.diags.error(a.loc, "attribute '%s' can only be applied to struct/union types",
a.kind2name());
return false;
case Unused:
d.setAttrUnused();
break;
case Opaque:
b.diags.error(a.loc, "attribute '%s' can only be applied to struct/union types",
a.kind2name());
return false;
case CName:
b.storeAttr(d, a);
break;
default:
b.diags.error(a.loc, "attribute '%s' is not applicable to Enum/Alias types",
a.kind2name());
return false;
}
return true;
}
fn void Builder.actOnVarAttr(Builder* b, Decl* d, const Attr* a) {
VarDecl* vd = (VarDecl*)d;
if (vd.getKind() == FunctionParam) {
b.actOnParamAttr(vd, a);
return;
}
switch (a.kind) {
case Export:
d.setAttrExport();
break;
case Unused:
d.setAttrUnused();
break;
case Section:
b.storeAttr(d, a);
break;
case Aligned:
b.storeAttr(d, a);
break;
case Weak:
if (!d.isPublic()) b.diags.error(a.loc, "weak declarations must be public");
vd.setAttrWeak();
break;
case CName:
case CDef:
b.storeAttr(d, a);
break;
case Embed:
b.storeAttr(d, a);
break;
default:
b.diags.error(a.loc, "attribute '%s' is not applicable to variables",
a.kind2name());
break;
}
}
public fn bool Builder.hasOpaqueAttr(Builder* b) {
for (u32 i=0; i<b.num_attrs; i++) {
const Attr* a = &b.attrs[i];
if (b.attrs[i].kind == Opaque) return true;
}
return false;
}
public fn bool Builder.hasEmbedAttr(Builder* b) {
for (u32 i=0; i<b.num_attrs; i++) {
const Attr* a = &b.attrs[i];
if (b.attrs[i].kind == Embed) return true;
}
return false;
}
fn bool Builder.actOnParamAttr(Builder* b, VarDecl* d, const Attr* a) {
switch (a.kind) {
case PrintfFormat:
d.setFormatAttr(Printf);
return true;
case ScanfFormat:
d.setFormatAttr(Scanf);
return true;
case AutoFile:
if (d.hasAutoAttr()) break;
d.setAttrAutoFile();
return true;
case AutoLine:
if (d.hasAutoAttr()) break;
d.setAttrAutoLine();
return true;
case AutoFunc:
if (d.hasAutoAttr()) break;
d.setAttrAutoFunc();
return true;
default:
b.diags.error(a.loc, "attribute '%s' cannot be applied to function parameters",
ast.idx2name(a.name));
return false;
}
b.diags.error(a.loc, "invalid combination of attributes");
return false;
}
public fn bool Builder.actOnAttr(Builder* b, const Attr* a) {
switch (a.kind) {
case CName:
case CDef:
if (!b.is_interface) {
b.diags.error(a.loc, "attribute '%s' can only be used in interface files", a.kind2name());
return false;
}
break;
default:
break;
}
if (a.kind != Unknown) {
switch (a.checkArgument()) {
case NoArg:
b.diags.error(a.loc, "attribute '%s' has no argument", a.kind2name());
return false;
case Arg:
b.diags.error(a.loc, "attribute '%s' needs an argument", a.kind2name());
return false;
case Number:
b.diags.error(a.value.loc, "attribute '%s' needs a number argument", a.kind2name());
return false;
case String:
b.diags.error(a.value.loc, "attribute '%s' needs a string argument", a.kind2name());
return false;
case Power2:
b.diags.error(a.value.loc, "requested alignment is not a power of 2");
return false;
case Ok:
break;
}
}
// always store attribute and apply later in applyAttributes()
if (b.num_attrs == elemsof(b.attrs)) {
b.diags.error(a.loc, "too many attributes");
return false;
}
b.attrs[b.num_attrs] = *a;
b.num_attrs++;
return true;
}
public fn void Builder.clearAttributes(Builder* b) {
b.num_attrs = 0;
}
fn void Builder.applyAttribute(Builder* b, Decl* d, const Attr* a) {
DeclKind dk = d.getKind();
switch (dk) {
case Function:
b.actOnFunctionAttr(d, a);
break;
case StructType:
b.actOnStructAttr(d, a);
break;
case EnumType:
b.actOnTypeAttr(d, a);
break;
case FunctionType:
// store in inner type also (for needed for type decl, other for when used)
// check result of first, otherwise we get the same error twice
if (!b.actOnTypeAttr(d, a)) return;
FunctionTypeDecl* ftd = (FunctionTypeDecl*)d;
b.applyAttribute((Decl*)ftd.getDecl(), a);
break;
case AliasType:
b.actOnTypeAttr(d, a);
break;
case Variable:
b.actOnVarAttr(d, a);
break;
default:
assert(0);
return;
}
}
public fn void Builder.applyAttributes(Builder* b, Decl* d, u32 count) {
assert(d);
assert(count <= b.num_attrs);
for (u32 i = 0; i < count; i++) {
const Attr* a = &b.attrs[b.num_attrs - count + i];
if (a.kind == Unknown) {
if (!b.attr_handler.handle(d, a)) b.storeAttr(d, a);
} else {
b.applyAttribute(d, a);
}
}
b.num_attrs -= count;
}
public fn QualType Builder.actOnBuiltinType(Builder*, BuiltinKind kind) {
return getBuiltinQT(kind);
}
public fn QualType Builder.actOnVoidType(Builder*) {
return getVoidQT();
}
public fn QualType Builder.actOnPointerType(Builder*, QualType inner) {
QualType ptr = QualType.create(ast.getPointerType(inner));
// canonical can be either self or a pointer to elem's canonical type
QualType canon = inner.getCanonicalType();
if (inner.getTypeOrNil() == canon.getTypeOrNil()) {
canon = ptr;
} else {
canon = QualType.create(ast.getPointerType(canon));
canon.setCanonicalType(canon);
}
ptr.setCanonicalType(canon);
return ptr;
}
public fn QualType Builder.actOnArrayType(Builder* b,
QualType elem,
bool has_size,
u32 size,
bool is_enum_index,
QualType index_type) {
ArrayType* t = ArrayType.create(b.context, elem, has_size, size, is_enum_index, index_type);
QualType a = QualType.create((Type*)t);
// canonical can be either self or a pointer to elem's canonical type
QualType canon = elem.getCanonicalType();
if (elem.getTypeOrNil() == canon.getTypeOrNil()) {
canon = a;
} else {
ArrayType* t2 = ArrayType.create(b.context, canon, has_size, size, is_enum_index, index_type);
// Note: keep same quals here, even if canonical type may be a PointerType!
canon = QualType.create((Type*)t2);
}
a.setCanonicalType(canon);
return a;
}
public fn QualType Builder.actOnIncrementalArrayType(Builder* b, QualType elem) {
ArrayType* t = ArrayType.createIncremental(b.context, elem);
QualType a = QualType.create((Type*)t);
// canonical can be either self or a pointer to elem's canonical type
QualType canon = elem.getCanonicalType();
if (elem.getTypeOrNil() == canon.getTypeOrNil()) {
canon = a;
} else {
ArrayType* t2 = ArrayType.createIncremental(b.context, canon);
// Note: keep same quals here, even if canonical type may be a PointerType!
canon = QualType.create((Type*)t2);
}
a.setCanonicalType(canon);
return a;
}
public fn FunctionDecl* Builder.actOnFunctionDecl(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* rtype,
const Ref* prefix,
VarDecl** params,
u32 num_params,
bool is_variadic,
bool is_local)
{
is_public |= b.is_interface & !is_local;
FunctionDecl* f = FunctionDecl.create(b.context,
name,
loc,
is_public,
b.ast_idx,
rtype,
prefix,
params,
num_params,
is_variadic,
is_local ? DefKind.Local : DefKind.Global);
b.ast.addFunc(f);
if (!is_local) {
if (!prefix) b.addSymbol(name, f.asDecl());
if (b.is_interface) f.asDecl().setExternal();
}
return f;
}
public fn FunctionDecl* Builder.actOnTemplateFunctionDecl(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
const TypeRefHolder* rtype,
u32 template_name,
SrcLoc template_loc,
VarDecl** params,
u32 num_params,
bool is_variadic)
{
if (b.is_interface) b.diags.error(loc, "template functions are not allow in interfaces");
is_public |= b.is_interface;
FunctionDecl* f = FunctionDecl.createTemplate(b.context,
name,
loc,
is_public,
b.ast_idx,
rtype,
template_name,
template_loc,
params,
num_params,
is_variadic);
b.ast.addFunc(f);
b.addSymbol(name, f.asDecl());
if (b.is_interface) f.asDecl().setExternal();
return f;
}
public fn void Builder.actOnFunctionBody(Builder*, FunctionDecl* f, CompoundStmt* body) {
f.setBody(body);
}
public fn EnumConstantDecl* Builder.actOnEnumConstant(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
Expr* init_expr) {
return EnumConstantDecl.create(b.context, name, loc, is_public, b.ast_idx, init_expr);
}
public fn Decl* Builder.actOnEnumType(Builder* b,
u32 name,
SrcLoc loc,
bool is_public,
bool is_incr,
QualType implType,
EnumConstantDecl** constants,
u32 num_constants)
{
is_public |= b.is_interface;
EnumTypeDecl* d = EnumTypeDecl.create(b.context,
name,
loc,
is_public,
b.ast_idx,
implType,
is_incr,
constants,
num_constants);
b.ast.addTypeDecl(d.asDecl());
b.addSymbol(name, d.asDecl());
return (Decl*)d;
}
public fn Stmt* Builder.actOnAsmStmt(Builder* b,
SrcLoc loc,
bool is_basic,
bool is_volatile,
u32 num_outputs,
u32 num_inputs,
const u32* names, // can be nil
ExprList* constraints,
ExprList* exprs,
ExprList* clobbers,
Expr* asm_string)
{
return (Stmt*)AsmStmt.create(b.context,
loc,
is_basic, is_volatile,
num_outputs, num_inputs,
names,
constraints,
exprs,
clobbers,
asm_string);
}
public fn CompoundStmt* Builder.actOnCompoundStmt(Builder* b,
SrcLoc endLoc,
Stmt** stmts,
u32 count) {
return CompoundStmt.create(b.context, endLoc, stmts, count);
}
public fn Stmt* Builder.actOnReturnStmt(Builder* b, SrcLoc loc, Expr* ret) {
return (Stmt*)ReturnStmt.create(b.context, loc, ret);
}
public fn Stmt* Builder.actOnIfStmt(Builder* b, SrcLoc loc, Stmt* cond, Stmt* then, Stmt* else_stmt) {
return (Stmt*)IfStmt.create(b.context, loc, cond, then, else_stmt);
}
public fn Stmt* Builder.actOnWhileStmt(Builder* b, SrcLoc loc, Stmt* cond, Stmt* then) {
return (Stmt*)WhileStmt.create(b.context, loc, cond, then);
}
public fn Stmt* Builder.actOnForStmt(Builder* b,
SrcLoc loc,
Stmt* init,
Expr* cond,
Expr* incr,
Stmt* body) {
return (Stmt*)ForStmt.create(b.context, loc, init, cond, incr, body);
}
public fn Stmt* Builder.actOnSwitchStmt(Builder* b,
SrcLoc loc,
Stmt* cond,
SwitchCase** cases, u32 num_cases,
bool has_default) {
return (Stmt*)SwitchStmt.create(b.context, loc, cond, cases, num_cases, has_default);
}
public fn SwitchCase* Builder.actOnCase(Builder* b,
SrcLoc loc,
bool is_default,
Expr** conds, u32 num_conds,
Stmt** stmts, u32 num_stmts) {
return SwitchCase.create(b.context, loc, is_default,
conds, num_conds, stmts, num_stmts);
}
public fn Stmt* Builder.actOnAssertStmt(Builder* b, SrcLoc loc, Expr* inner) {
return (Stmt*)AssertStmt.create(b.context, loc, inner);
}
public fn Stmt* Builder.actOnBreakStmt(Builder* b, SrcLoc loc) {
return (Stmt*)BreakStmt.create(b.context, loc);
}
public fn Stmt* Builder.actOnContinueStmt(Builder* b, SrcLoc loc) {
return (Stmt*)ContinueStmt.create(b.context, loc);
}
public fn Stmt* Builder.actOnFallthroughStmt(Builder* b, SrcLoc loc) {
return (Stmt*)FallthroughStmt.create(b.context, loc);
}
public fn Stmt* Builder.actOnLabelStmt(Builder* b, u32 name, SrcLoc loc, Stmt* stmt) {
return (Stmt*)LabelStmt.create(b.context, name, loc, stmt);
}
public fn Stmt* Builder.actOnGotoStmt(Builder* b, u32 name, SrcLoc loc) {
return (Stmt*)GotoStmt.create(b.context, name, loc);
}
public fn IdentifierExpr* Builder.actOnIdentifier(Builder* b, SrcLoc loc, u32 name) {
return IdentifierExpr.create(b.context, loc, name);
}
public fn Expr* Builder.actOnIntegerLiteral(Builder* b, SrcLoc loc, u32 src_len, u64 value, Radix radix, BuiltinKind kind) {
return (Expr*)IntegerLiteral.create(b.context, loc, src_len, value, radix, kind);
}
public fn Expr* Builder.actOnFloatLiteral(Builder* b, SrcLoc loc, u32 src_len, f64 value, Radix radix, BuiltinKind kind) {
return (Expr*)FloatLiteral.create(b.context, loc, src_len, value, radix, kind);
}
public fn Expr* Builder.actOnCharLiteral(Builder* b, SrcLoc loc, u32 src_len, u8 value, Radix radix) {
return (Expr*)CharLiteral.create(b.context, loc, src_len, value, radix);
}
public fn Expr* Builder.actOnStringLiteral(Builder* b, SrcLoc loc, u32 src_len, u32 value, u32 len) {
return (Expr*)StringLiteral.create(b.context, loc, src_len, value, len);
}
public fn Expr* Builder.actOnNilExpr(Builder* b, SrcLoc loc) {
return (Expr*)NilExpr.create(b.context, loc);
}
public fn Expr* Builder.actOnParenExpr(Builder* b, SrcLoc loc, u32 src_len, Expr* inner) {
return (Expr*)ParenExpr.create(b.context, loc, src_len, inner);
}
public fn Expr* Builder.actOnUnaryOperator(Builder* b, SrcLoc loc, UnaryOpcode opcode, Expr* inner) {
return (Expr*)UnaryOperator.create(b.context, loc, opcode, inner);
}
public fn Expr* Builder.actOnBinaryOperator(Builder* b,
SrcLoc loc,
BinaryOpcode opcode,
Expr* lhs,
Expr* rhs) {
return (Expr*)BinaryOperator.create(b.context, loc, opcode, lhs, rhs);
}
public fn Expr* Builder.actOnConditionalOperator(Builder* b,
SrcLoc questionLoc,
SrcLoc colonLoc,
Expr* cond,
Expr* lhs,
Expr* rhs) {
return (Expr*)ConditionalOperator.create(b.context,
questionLoc,
colonLoc,
cond,
lhs,
rhs);
}
public fn Expr* Builder.actOnBooleanConstant(Builder* b, SrcLoc loc, bool value) {
return (Expr*)BooleanLiteral.create(b.context, loc, value);
}
public fn Expr* Builder.actOnBuiltinExpr(Builder* b,
SrcLoc loc, u32 src_len,
Expr* inner,
BuiltinExprKind kind) {
return (Expr*)BuiltinExpr.create(b.context, loc, src_len, inner, kind);
}
public fn Expr* Builder.actOnOffsetOfExpr(Builder* b,
SrcLoc loc, u32 src_len,
Expr* structExpr,
Expr* member) {
return (Expr*)BuiltinExpr.createOffsetOf(b.context, loc, src_len, structExpr, member);
}
public fn Expr* Builder.actOnToContainerExpr(Builder* b,
SrcLoc loc, u32 src_len,
Expr* structExpr,
Expr* member,
Expr* pointer) {
return (Expr*)BuiltinExpr.createToContainer(b.context,
loc, src_len,
structExpr,
member,
pointer);
}
public fn Expr* Builder.actOnTypeExpr(Builder* b, SrcLoc loc, u32 src_len, const TypeRefHolder* ref) {
return (Expr*)TypeExpr.create(b.context, loc, src_len, ref);
}
public fn Expr* Builder.actOnBitOffsetExpr(Builder* b, SrcLoc loc, Expr* lhs, Expr* rhs) {
return (Expr*)BitOffsetExpr.create(b.context, loc, lhs, rhs);