forked from tact-lang/tact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolveDescriptors.ts
2295 lines (2159 loc) · 83.7 KB
/
resolveDescriptors.ts
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
import {
AstConstantDef,
AstFieldDecl,
AstContractInit,
AstNativeFunctionDecl,
AstNode,
AstType,
createAstNode,
idText,
AstId,
eqNames,
AstFunctionDef,
isSelfId,
isSlice,
AstFunctionDecl,
AstConstantDecl,
AstExpression,
AstMapType,
AstTypeId,
AstAsmFunctionDef,
} from "../grammar/ast";
import { traverse } from "../grammar/iterators";
import {
idTextErr,
throwCompilationError,
throwInternalCompilerError,
} from "../errors";
import { CompilerContext, Store, createContextStore } from "../context";
import {
ConstantDescription,
FieldDescription,
FunctionParameter,
FunctionDescription,
InitParameter,
InitDescription,
printTypeRef,
ReceiverSelector,
receiverSelectorName,
TypeDescription,
TypeRef,
typeRefEquals,
} from "./types";
import { getRawAST } from "../grammar/store";
import { cloneNode } from "../grammar/clone";
import { crc16 } from "../utils/crc16";
import { isSubsetOf } from "../utils/isSubsetOf";
import { evalConstantExpression } from "../constEval";
import { resolveABIType, intMapFormats } from "./resolveABITypeRef";
import { enabledExternals } from "../config/features";
import { isRuntimeType } from "./isRuntimeType";
import { GlobalFunctions } from "../abi/global";
import { ItemOrigin } from "../grammar/grammar";
import { getExpType, resolveExpression } from "./resolveExpression";
import { emptyContext } from "./resolveStatements";
import { isAssignable } from "./subtyping";
const store = createContextStore<TypeDescription>();
const staticFunctionsStore = createContextStore<FunctionDescription>();
const staticConstantsStore = createContextStore<ConstantDescription>();
// this function does not handle the case of structs
function verifyMapAsAnnotationsForPrimitiveTypes(
type: AstTypeId,
asAnnotation: AstId | null,
): void {
switch (idText(type)) {
case "Int": {
if (
asAnnotation !== null &&
!Object.keys(intMapFormats).includes(idText(asAnnotation))
) {
throwCompilationError(
'Invalid `as`-annotation for type "Int" type',
asAnnotation.loc,
);
}
return;
}
case "Address":
case "Bool":
case "Cell": {
if (asAnnotation !== null) {
throwCompilationError(
`${idTextErr(type)} type cannot have as-annotation`,
asAnnotation.loc,
);
}
return;
}
default: {
throwInternalCompilerError("Unsupported map type", type.loc);
}
}
}
function verifyMapTypes(
typeId: AstTypeId,
asAnnotation: AstId | null,
allowedTypeNames: string[],
): void {
if (!allowedTypeNames.includes(idText(typeId))) {
throwCompilationError(
"Invalid map type. Check https://docs.tact-lang.org/book/maps#allowed-types",
typeId.loc,
);
}
verifyMapAsAnnotationsForPrimitiveTypes(typeId, asAnnotation);
}
function verifyMapType(mapTy: AstMapType, isValTypeStruct: boolean) {
// optional and other compound key and value types are disallowed at the level of grammar
// check allowed key types
verifyMapTypes(mapTy.keyType, mapTy.keyStorageType, ["Int", "Address"]);
// check allowed value types
if (isValTypeStruct && mapTy.valueStorageType === null) {
return;
}
// the case for struct/message is already checked
verifyMapTypes(mapTy.valueType, mapTy.valueStorageType, [
"Int",
"Address",
"Bool",
"Cell",
]);
}
export const toBounced = (type: string) => `${type}%%BOUNCED%%`;
export function resolveTypeRef(ctx: CompilerContext, type: AstType): TypeRef {
switch (type.kind) {
case "type_id": {
const t = getType(ctx, idText(type));
return {
kind: "ref",
name: t.name,
optional: false,
};
}
case "optional_type": {
if (type.typeArg.kind !== "type_id") {
throwInternalCompilerError(
"Only optional type identifiers are supported now",
type.typeArg.loc,
);
}
const t = getType(ctx, idText(type.typeArg));
return {
kind: "ref",
name: t.name,
optional: true,
};
}
case "map_type": {
const keyTy = getType(ctx, idText(type.keyType));
const valTy = getType(ctx, idText(type.valueType));
verifyMapType(type, valTy.kind === "struct");
return {
kind: "map",
key: keyTy.name,
keyAs:
type.keyStorageType !== null
? idText(type.keyStorageType)
: null,
value: valTy.name,
valueAs:
type.valueStorageType !== null
? idText(type.valueStorageType)
: null,
};
}
case "bounced_message_type": {
const t = getType(ctx, idText(type.messageType));
return {
kind: "ref_bounced",
name: t.name,
};
}
}
}
function buildTypeRef(
type: AstType,
types: Map<string, TypeDescription>,
): TypeRef {
switch (type.kind) {
case "type_id": {
if (!types.has(idText(type))) {
throwCompilationError(
`Type ${idTextErr(type)} not found`,
type.loc,
);
}
return {
kind: "ref",
name: idText(type),
optional: false,
};
}
case "optional_type": {
if (type.typeArg.kind !== "type_id") {
throwInternalCompilerError(
"Only optional type identifiers are supported now",
type.typeArg.loc,
);
}
if (!types.has(idText(type.typeArg))) {
throwCompilationError(
`Type ${idTextErr(type.typeArg)} not found`,
type.loc,
);
}
return {
kind: "ref",
name: idText(type.typeArg),
optional: true,
};
}
case "map_type": {
if (!types.has(idText(type.keyType))) {
throwCompilationError(
`Type ${idTextErr(type.keyType)} not found`,
type.loc,
);
}
if (!types.has(idText(type.valueType))) {
throwCompilationError(
`Type ${idTextErr(type.valueType)} not found`,
type.loc,
);
}
const valTy = types.get(idText(type.valueType))!;
verifyMapType(type, valTy.kind === "struct");
return {
kind: "map",
key: idText(type.keyType),
keyAs:
type.keyStorageType !== null
? idText(type.keyStorageType)
: null,
value: idText(type.valueType),
valueAs:
type.valueStorageType !== null
? idText(type.valueStorageType)
: null,
};
}
case "bounced_message_type": {
return {
kind: "ref_bounced",
name: idText(type.messageType),
};
}
}
}
function uidForName(name: string, types: Map<string, TypeDescription>) {
// Resolve unique typeid from crc16
let uid = crc16(name);
while (Array.from(types.values()).find((v) => v.uid === uid)) {
uid = (uid + 1) % 65536;
}
return uid;
}
export function resolveDescriptors(ctx: CompilerContext) {
const types: Map<string, TypeDescription> = new Map();
const staticFunctions: Map<string, FunctionDescription> = new Map();
const staticConstants: Map<string, ConstantDescription> = new Map();
const ast = getRawAST(ctx);
//
// Register types
//
for (const a of ast.types) {
if (types.has(idText(a.name))) {
throwCompilationError(
`Type "${idText(a.name)}" already exists`,
a.loc,
);
}
const uid = uidForName(idText(a.name), types);
switch (a.kind) {
case "primitive_type_decl":
{
types.set(idText(a.name), {
kind: "primitive_type_decl",
origin: a.loc.origin,
name: idText(a.name),
uid,
fields: [],
traits: [],
header: null,
tlb: null,
signature: null,
functions: new Map(),
receivers: [],
dependsOn: [],
init: null,
ast: a,
interfaces: [],
constants: [],
partialFieldCount: 0,
});
}
break;
case "contract":
{
types.set(idText(a.name), {
kind: "contract",
origin: a.loc.origin,
name: idText(a.name),
uid,
header: null,
tlb: null,
fields: [],
traits: [],
signature: null,
functions: new Map(),
receivers: [],
dependsOn: [],
init: null,
ast: a,
interfaces: a.attributes.map((v) => v.name.value),
constants: [],
partialFieldCount: 0,
});
}
break;
case "struct_decl":
case "message_decl":
{
types.set(idText(a.name), {
kind: "struct",
origin: a.loc.origin,
name: idText(a.name),
uid,
header: null,
tlb: null,
signature: null,
fields: [],
traits: [],
functions: new Map(),
receivers: [],
dependsOn: [],
init: null,
ast: a,
interfaces: [],
constants: [],
partialFieldCount: 0,
});
}
break;
case "trait": {
types.set(idText(a.name), {
kind: "trait",
origin: a.loc.origin,
name: idText(a.name),
uid,
header: null,
tlb: null,
signature: null,
fields: [],
traits: [],
functions: new Map(),
receivers: [],
dependsOn: [],
init: null,
ast: a,
interfaces: a.attributes.map((v) => v.name.value),
constants: [],
partialFieldCount: 0,
});
}
}
}
//
// Resolve fields
//
function buildFieldDescription(
src: AstFieldDecl,
index: number,
): FieldDescription {
const fieldTy = buildTypeRef(src.type, types);
// Check if field is runtime type
if (isRuntimeType(fieldTy)) {
throwCompilationError(
printTypeRef(fieldTy) +
" is a runtime only type and can't be used as field",
src.loc,
);
}
// Resolve abi type
const type = resolveABIType(src);
return {
name: idText(src.name),
type: fieldTy,
index,
as: src.as !== null ? idText(src.as) : null,
default: undefined, // initializer will be evaluated after typechecking
loc: src.loc,
ast: src,
abi: { name: idText(src.name), type },
};
}
function buildConstantDescription(
src: AstConstantDef | AstConstantDecl,
): ConstantDescription {
const constDeclTy = buildTypeRef(src.type, types);
return {
name: idText(src.name),
type: constDeclTy,
value: undefined, // initializer will be evaluated after typechecking
loc: src.loc,
ast: src,
};
}
for (const a of ast.types) {
// Contract
if (a.kind === "contract") {
for (const f of a.declarations) {
if (f.kind === "field_decl") {
if (
types
.get(idText(a.name))!
.fields.find((v) => eqNames(v.name, f.name))
) {
throwCompilationError(
`Field ${idTextErr(f.name)} already exists`,
f.loc,
);
}
if (
types
.get(idText(a.name))!
.constants.find((v) => eqNames(v.name, f.name))
) {
throwCompilationError(
`Constant ${idText(f.name)} already exists`,
f.loc,
);
}
types
.get(idText(a.name))!
.fields.push(
buildFieldDescription(
f,
types.get(idText(a.name))!.fields.length,
),
);
} else if (f.kind === "constant_def") {
if (
types
.get(idText(a.name))!
.fields.find((v) => eqNames(v.name, f.name))
) {
throwCompilationError(
`Field ${idTextErr(f.name)} already exists`,
f.loc,
);
}
if (
types
.get(idText(a.name))!
.constants.find((v) => eqNames(v.name, f.name))
) {
throwCompilationError(
`Constant ${idTextErr(f.name)} already exists`,
f.loc,
);
}
if (f.attributes.find((v) => v.type !== "override")) {
throwCompilationError(
`Constant can be only overridden`,
f.loc,
);
}
types
.get(idText(a.name))!
.constants.push(buildConstantDescription(f));
}
}
}
// Struct
if (a.kind === "struct_decl" || a.kind === "message_decl") {
for (const f of a.fields) {
if (
types
.get(idText(a.name))!
.fields.find((v) => eqNames(v.name, f.name))
) {
throwCompilationError(
`Field ${idTextErr(f.name)} already exists`,
f.loc,
);
}
types
.get(idText(a.name))!
.fields.push(
buildFieldDescription(
f,
types.get(idText(a.name))!.fields.length,
),
);
}
if (a.fields.length === 0 && a.kind === "struct_decl") {
throwCompilationError(
`Struct ${idTextErr(a.name)} must have at least one field`,
a.loc,
);
}
if (a.kind === "message_decl" && a.opcode) {
if (a.opcode.value === 0n) {
throwCompilationError(
`Zero opcodes are reserved for text comments and cannot be used for message structs`,
a.opcode.loc,
);
}
if (a.opcode.value > 0xffff_ffff) {
throwCompilationError(
`Opcode of message ${idTextErr(a.name)} is too large: it must fit into 32 bits`,
a.opcode.loc,
);
}
}
}
// Trait
if (a.kind === "trait") {
for (const traitDecl of a.declarations) {
if (traitDecl.kind === "field_decl") {
if (
types
.get(idText(a.name))!
.fields.find((v) => eqNames(v.name, traitDecl.name))
) {
throwCompilationError(
`Field ${idTextErr(traitDecl.name)} already exists`,
traitDecl.loc,
);
}
if (traitDecl.as) {
throwCompilationError(
`Trait field cannot have serialization specifier`,
traitDecl.loc,
);
}
if (traitDecl.initializer) {
throwCompilationError(
`Trait field cannot have an initializer`,
traitDecl.initializer.loc,
);
}
types
.get(idText(a.name))!
.fields.push(
buildFieldDescription(
traitDecl,
types.get(idText(a.name))!.fields.length,
),
);
} else if (
traitDecl.kind === "constant_def" ||
traitDecl.kind === "constant_decl"
) {
if (
types
.get(idText(a.name))!
.fields.find((v) => eqNames(v.name, traitDecl.name))
) {
throwCompilationError(
`Field ${idTextErr(traitDecl.name)} already exists`,
traitDecl.loc,
);
}
if (
types
.get(idText(a.name))!
.constants.find((v) =>
eqNames(v.name, traitDecl.name),
)
) {
throwCompilationError(
`Constant ${idTextErr(traitDecl.name)} already exists`,
traitDecl.loc,
);
}
if (
traitDecl.attributes.find((v) => v.type === "override")
) {
throwCompilationError(
`Trait constant cannot be overridden`,
traitDecl.loc,
);
}
types
.get(idText(a.name))!
.constants.push(buildConstantDescription(traitDecl));
}
}
}
}
//
// Populate partial serialization info
//
for (const t of types.values()) {
t.partialFieldCount = resolvePartialFields(ctx, t);
}
//
// Resolve contract functions
//
function resolveFunctionDescriptor(
optSelf: TypeRef | null,
a:
| AstFunctionDef
| AstNativeFunctionDecl
| AstFunctionDecl
| AstAsmFunctionDef,
origin: ItemOrigin,
): FunctionDescription {
let self = optSelf;
// Resolve return
let returns: TypeRef = { kind: "void" };
if (a.return) {
returns = buildTypeRef(a.return, types);
}
let params: FunctionParameter[] = [];
for (const r of a.params) {
params.push({
name: r.name,
type: buildTypeRef(r.type, types),
loc: r.loc,
});
}
// Resolve flags
const isGetter = a.attributes.find((a) => a.type === "get");
const isMutating = a.attributes.find((a) => a.type === "mutates");
const isExtends = a.attributes.find((a) => a.type === "extends");
const isVirtual = a.attributes.find((a) => a.type === "virtual");
const isOverride = a.attributes.find((a) => a.type === "override");
const isInline = a.attributes.find((a) => a.type === "inline");
const isAbstract = a.attributes.find((a) => a.type === "abstract");
// Check for native
if (a.kind === "native_function_decl") {
if (isGetter) {
throwCompilationError(
"Native functions cannot be getters",
isGetter.loc,
);
}
if (self) {
throwCompilationError(
"Native functions cannot be declared within a contract",
a.loc,
);
}
if (isVirtual) {
throwCompilationError(
"Native functions cannot be virtual",
isVirtual.loc,
);
}
if (isOverride) {
throwCompilationError(
"Native functions cannot be overridden",
isOverride.loc,
);
}
}
// Check virtual and override
if (isVirtual && isExtends) {
throwCompilationError(
"Extend functions cannot be virtual",
isVirtual.loc,
);
}
if (isOverride && isExtends) {
throwCompilationError(
"Extend functions cannot be overridden",
isOverride.loc,
);
}
if (isAbstract && isExtends) {
throwCompilationError(
"Extend functions cannot be abstract",
isAbstract.loc,
);
}
if (!self && isVirtual) {
throwCompilationError(
"Virtual functions must be defined within a contract or a trait",
isVirtual.loc,
);
}
if (!self && isOverride) {
throwCompilationError(
"Overrides functions must be defined within a contract or a trait",
isOverride.loc,
);
}
if (!self && isAbstract) {
throwCompilationError(
"Abstract functions must be defined within a trait",
isAbstract.loc,
);
}
if (isVirtual && isAbstract) {
throwCompilationError(
"Abstract functions cannot be virtual",
isAbstract.loc,
);
}
if (isVirtual && isOverride) {
throwCompilationError(
"Overrides functions cannot be virtual",
isOverride.loc,
);
}
if (isAbstract && isOverride) {
throwCompilationError(
"Overrides functions cannot be abstract",
isOverride.loc,
);
}
// Check virtual
if (isVirtual) {
if (self?.kind !== "ref") {
throwInternalCompilerError(
"Virtual functions must have a self parameter",
isVirtual.loc,
);
}
const t = types.get(self.name!)!;
if (t.kind !== "trait") {
throwCompilationError(
"Virtual functions must be defined within a trait",
isVirtual.loc,
);
}
}
// Check abstract
if (isAbstract) {
if (self?.kind !== "ref") {
throwInternalCompilerError(
"Abstract functions must have a self parameter",
isAbstract.loc,
);
}
const t = types.get(self.name!)!;
if (t.kind !== "trait") {
throwCompilationError(
"Abstract functions must be defined within a trait",
isAbstract.loc,
);
}
}
if (isOverride) {
if (self?.kind !== "ref") {
throwInternalCompilerError(
"Override functions must have a self parameter",
isOverride.loc,
);
}
const t = types.get(self.name!)!;
if (!["contract", "trait"].includes(t.kind)) {
throwCompilationError(
"Overridden functions must be defined within a contract or a trait",
isOverride.loc,
);
}
}
// Check for common
if (a.kind === "function_def") {
if (isGetter && !self) {
throwCompilationError(
"Getters must be defined within a contract",
isGetter.loc,
);
}
}
// Check for getter
if (isInline && isGetter) {
throwCompilationError("Getters cannot be inline", isInline.loc);
}
// Validate mutating
if (isExtends) {
if (self) {
throwCompilationError(
"Extend functions cannot be defined within a contract",
isExtends.loc,
);
}
if (params.length === 0) {
throwCompilationError(
"Extend functions must have at least one parameter",
isExtends.loc,
);
}
const firstParam = params[0]!;
if (!isSelfId(firstParam.name)) {
throwCompilationError(
'Extend function must have first parameter named "self"',
firstParam.loc,
);
}
if (firstParam.type.kind !== "ref") {
throwCompilationError(
"Extend functions must have a reference type as the first parameter",
firstParam.loc,
);
}
if (!types.has(firstParam.type.name)) {
throwCompilationError(
"Type " + firstParam.type.name + " not found",
firstParam.loc,
);
}
// Update self and remove first parameter
self = firstParam.type;
params = params.slice(1);
}
// Check for mutating and extends
if (isMutating && !isExtends) {
throwCompilationError(
"Mutating functions must be extend functions",
isMutating.loc,
);
}
// Check parameter names
const exNames: Set<string> = new Set();
for (const param of params) {
if (isSelfId(param.name)) {
throwCompilationError(
'Parameter name "self" is reserved',
param.loc,
);
}
if (exNames.has(idText(param.name))) {
throwCompilationError(
`Parameter name ${idTextErr(param.name)} is already used`,
param.loc,
);
}
exNames.add(idText(param.name));
}
// Check for runtime types in getters
if (isGetter) {
for (const param of params) {
if (isRuntimeType(param.type)) {
throwCompilationError(
printTypeRef(param.type) +
" is a runtime-only type and can't be used as a getter parameter",
param.loc,
);
}
}
if (isRuntimeType(returns)) {
throwCompilationError(
printTypeRef(returns) +
" is a runtime-only type and can't be used as getter return type",
a.loc,
);
}
}
// check asm shuffle
if (a.kind === "asm_function_def") {
// check arguments shuffle
if (a.shuffle.args.length !== 0) {
const shuffleArgSet = new Set(
a.shuffle.args.map((id) => idText(id)),
);
if (shuffleArgSet.size !== a.shuffle.args.length) {
throwCompilationError(
"asm argument rearrangement cannot have duplicates",
a.loc,
);
}
const paramSet = new Set(
a.params.map((typedId) => idText(typedId.name)),
);
if (!isSubsetOf(paramSet, shuffleArgSet)) {
throwCompilationError(
"asm argument rearrangement must mention all function parameters",
a.loc,
);
}
if (!isSubsetOf(shuffleArgSet, paramSet)) {
throwCompilationError(
"asm argument rearrangement must mention only function parameters",
a.loc,
);
}
}
// check return shuffle
if (a.shuffle.ret.length !== 0) {
const shuffleRetSet = new Set(
a.shuffle.ret.map((num) => Number(num.value)),
);
if (shuffleRetSet.size !== a.shuffle.ret.length) {
throwCompilationError(
"asm return rearrangement cannot have duplicates",
a.loc,
);
}
let retTupleSize = 0;
switch (returns.kind) {
case "ref":
{
const ty = types.get(returns.name)!;
switch (ty.kind) {
case "struct":
case "contract":
retTupleSize = ty.fields.length;
break;
case "primitive_type_decl":
retTupleSize = 1;
break;
case "trait":
throwInternalCompilerError(
"A trait cannot be returned from a function",
a.loc,
);
}
}
break;
case "null":
case "map":
retTupleSize = 1;
break;
case "ref_bounced":
throwInternalCompilerError(
"A <bounced> type cannot be returned from a function",
a.loc,
);
break;
case "void":
retTupleSize = 0;
break;
}
// mutating functions also return `self` arg (implicitly in Tact, but explicitly in FunC)
retTupleSize += isMutating ? 1 : 0;
const returnValueSet = new Set([...Array(retTupleSize).keys()]);
if (!isSubsetOf(returnValueSet, shuffleRetSet)) {
throwCompilationError(
`asm return rearrangement must mention all return position numbers: [0..${retTupleSize - 1}]`,
a.loc,
);
}
if (!isSubsetOf(shuffleRetSet, returnValueSet)) {
throwCompilationError(
`asm return rearrangement must mention only valid return position numbers: [0..${retTupleSize - 1}]`,
a.loc,
);
}
}
}
// Register function
return {
name: idText(a.name),
self: self,
origin,
params,
returns,
ast: a,
isMutating: !!isMutating || !!optSelf /* && !isGetter */, // Mark all contract functions as mutating