forked from AssemblyScript/assemblyscript
/
resolver.ts
1892 lines (1810 loc) Β· 63 KB
/
resolver.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
/**
* Resolve infrastructure to obtain types and elements.
* @module resolver
*//***/
import {
DiagnosticEmitter,
DiagnosticCode
} from "./diagnostics";
import {
Program,
ElementKind,
OperatorKind,
Element,
Class,
ClassPrototype,
Function,
FunctionPrototype,
VariableLikeElement,
Property,
PropertyPrototype,
Field,
FieldPrototype,
Global,
TypeDefinition
} from "./program";
import {
FlowFlags,
Flow
} from "./flow";
import {
FunctionTypeNode,
ParameterKind,
TypeNode,
NodeKind,
NamedTypeNode,
TypeName,
TypeParameterNode,
Node,
Range,
IdentifierExpression,
CallExpression,
ElementAccessExpression,
PropertyAccessExpression,
LiteralExpression,
LiteralKind,
ParenthesizedExpression,
AssertionExpression,
Expression,
IntegerLiteralExpression,
UnaryPrefixExpression,
UnaryPostfixExpression,
AssertionKind,
BinaryExpression,
ThisExpression,
SuperExpression,
isTypeOmitted
} from "./ast";
import {
Type,
Signature,
typesToString,
TypeKind,
TypeFlags
} from "./types";
import {
CommonFlags,
CommonSymbols
} from "./common";
import {
makeMap,
isPowerOf2
} from "./util";
import {
Token
} from "./tokenizer";
import {
BuiltinSymbols
} from "./builtins";
/** Indicates whether errors are reported or not. */
export enum ReportMode {
/** Report errors. */
REPORT,
/** Swallow errors. */
SWALLOW
}
/** Provides tools to resolve types and expressions. */
export class Resolver extends DiagnosticEmitter {
/** The program this resolver belongs to. */
program: Program;
/** Target expression of the previously resolved property or element access. */
currentThisExpression: Expression | null = null;
/** Element expression of the previously resolved element access. */
currentElementExpression : Expression | null = null;
/** Constructs the resolver for the specified program. */
constructor(
/** The program to construct a resolver for. */
program: Program
) {
super(program.diagnostics);
this.program = program;
}
/** Resolves a {@link CommonTypeNode} to a concrete {@link Type}. */
resolveType(
/** The type to resolve. */
node: TypeNode,
/** Relative context. */
context: Element,
/** Type arguments inherited through context, i.e. `T`. */
contextualTypeArguments: Map<string,Type> | null = null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
// handle signature
if (node.kind == NodeKind.FUNCTIONTYPE) {
let explicitThisType = (<FunctionTypeNode>node).explicitThisType;
let thisType: Type | null = null;
if (explicitThisType) {
thisType = this.resolveType(
explicitThisType,
context,
contextualTypeArguments,
reportMode
);
if (!thisType) return null;
}
let parameterNodes = (<FunctionTypeNode>node).parameters;
let numParameters = parameterNodes.length;
let parameterTypes = new Array<Type>(numParameters);
let parameterNames = new Array<string>(numParameters);
let requiredParameters = 0;
let hasRest = false;
for (let i = 0; i < numParameters; ++i) {
let parameterNode = parameterNodes[i];
switch (parameterNode.parameterKind) {
case ParameterKind.DEFAULT: {
requiredParameters = i + 1;
break;
}
case ParameterKind.REST: {
assert(i == numParameters);
hasRest = true;
break;
}
}
let parameterTypeNode = parameterNode.type;
if (isTypeOmitted(parameterTypeNode)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_expected,
parameterTypeNode.range
);
}
return null;
}
let parameterType = this.resolveType(
parameterTypeNode,
context,
contextualTypeArguments,
reportMode
);
if (!parameterType) return null;
parameterTypes[i] = parameterType;
parameterNames[i] = parameterNode.name.text;
}
let returnTypeNode = (<FunctionTypeNode>node).returnType;
if (isTypeOmitted(returnTypeNode)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_expected,
returnTypeNode.range
);
}
return null;
}
let returnType: Type | null;
if (returnTypeNode) {
returnType = this.resolveType(
returnTypeNode,
context,
contextualTypeArguments,
reportMode
);
if (!returnType) return null;
} else {
returnType = Type.void;
}
let signature = new Signature(parameterTypes, returnType, thisType);
signature.parameterNames = parameterNames;
signature.requiredParameters = requiredParameters;
signature.hasRest = hasRest;
return node.isNullable ? signature.type.asNullable() : signature.type;
}
// now dealing with TypeNode
assert(node.kind == NodeKind.NAMEDTYPE);
var typeNode = <NamedTypeNode>node;
var typeName = typeNode.name;
var typeArgumentNodes = typeNode.typeArguments;
var isSimpleType = !typeName.next;
// look up in contextual type arguments if a simple type
if (isSimpleType) {
if (contextualTypeArguments && contextualTypeArguments.has(typeName.identifier.text)) {
let type = contextualTypeArguments.get(typeName.identifier.text)!;
if (typeArgumentNodes !== null && typeArgumentNodes.length) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_0_is_not_generic,
node.range, type.toString()
);
}
}
if (node.isNullable) {
if (!type.is(TypeFlags.REFERENCE)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Basic_type_0_cannot_be_nullable,
node.range, type.toString()
);
}
}
return type.asNullable();
}
return type;
}
}
// look up in context
var element = this.resolveTypeName(typeName, context, reportMode);
if (!element) return null;
// use shadow type if present (i.e. namespace sharing a type)
if (element.shadowType) {
element = element.shadowType;
} else {
// handle enums (become i32)
if (element.kind == ElementKind.ENUM) {
if (typeArgumentNodes !== null && typeArgumentNodes.length) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_0_is_not_generic,
node.range, element.internalName
);
}
}
if (node.isNullable) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Basic_type_0_cannot_be_nullable,
node.range, element.name
);
}
}
return Type.i32;
}
// handle classes
if (element.kind == ElementKind.CLASS_PROTOTYPE) {
let instance = this.resolveClassInclTypeArguments(
<ClassPrototype>element,
typeArgumentNodes,
context,
makeMap<string,Type>(contextualTypeArguments), // don't inherit
node
); // reports
if (!instance) return null;
return node.isNullable ? instance.type.asNullable() : instance.type;
}
}
// handle type definitions
if (element.kind == ElementKind.TYPEDEFINITION) {
// shortcut already resolved (mostly builtins)
if (element.is(CommonFlags.RESOLVED)) {
if (typeArgumentNodes !== null && typeArgumentNodes.length) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_0_is_not_generic,
node.range, element.internalName
);
}
}
let type = (<TypeDefinition>element).type;
if (node.isNullable) {
if (!type.is(TypeFlags.REFERENCE)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Basic_type_0_cannot_be_nullable,
typeNode.name.range, typeName.identifier.text
);
}
} else {
return type.asNullable();
}
}
return type;
}
// handle built-in types
if (isSimpleType) {
switch (typeName.identifier.symbol) {
case CommonSymbols.native: return this.resolveBuiltinNativeType(typeNode, context, contextualTypeArguments, reportMode);
case CommonSymbols.indexof: return this.resolveBuiltinIndexofType(typeNode, context, contextualTypeArguments, reportMode);
case CommonSymbols.valueof: return this.resolveBuiltinValueofType(typeNode, context, contextualTypeArguments, reportMode);
}
}
// resolve normally
let typeParameterNodes = (<TypeDefinition>element).typeParameterNodes;
let typeArguments: Type[] | null = null;
if (typeParameterNodes) {
typeArguments = this.resolveTypeArguments(
typeParameterNodes,
typeArgumentNodes,
context,
contextualTypeArguments = makeMap(contextualTypeArguments), // inherit
node,
reportMode
);
if (!typeArguments) return null;
} else if (typeArgumentNodes && typeArgumentNodes.length) {
this.error(
DiagnosticCode.Type_0_is_not_generic,
typeNode.range, typeName.identifier.text
);
// recoverable
}
let type = this.resolveType(
(<TypeDefinition>element).typeNode,
element,
contextualTypeArguments,
reportMode
);
if (!type) return null;
if (node.isNullable) {
if (!type.is(TypeFlags.REFERENCE)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Basic_type_0_cannot_be_nullable,
typeNode.name.range, typeName.identifier.text
);
}
} else {
return type.asNullable();
}
}
return type;
}
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Cannot_find_name_0,
typeNode.name.range, typeName.identifier.text
);
}
return null;
}
private resolveBuiltinNativeType(
/** The type to resolve. */
typeNode: NamedTypeNode,
/** Relative context. */
context: Element,
/** Type arguments inherited through context, i.e. `T`. */
contextualTypeArguments: Map<string,Type> | null = null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = typeNode.typeArguments;
if (!(typeArgumentNodes && typeArgumentNodes.length == 1)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
typeNode.range, "1", (typeArgumentNodes ? typeArgumentNodes.length : 1).toString(10)
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], context, contextualTypeArguments, reportMode);
if (!typeArgument) return null;
switch (typeArgument.kind) {
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.I32: return Type.i32;
case TypeKind.ISIZE: if (!this.program.options.isWasm64) return Type.i32;
case TypeKind.I64: return Type.i64;
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.U32:
case TypeKind.BOOL: return Type.u32;
case TypeKind.USIZE: if (!this.program.options.isWasm64) return Type.u32;
case TypeKind.U64: return Type.u64;
case TypeKind.F32: return Type.f32;
case TypeKind.F64: return Type.f64;
case TypeKind.V128: return Type.v128;
case TypeKind.VOID: return Type.void;
default: assert(false);
}
return null;
}
private resolveBuiltinIndexofType(
/** The type to resolve. */
typeNode: NamedTypeNode,
/** Relative context. */
context: Element,
/** Type arguments inherited through context, i.e. `T`. */
contextualTypeArguments: Map<string,Type> | null = null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = typeNode.typeArguments;
if (!(typeArgumentNodes && typeArgumentNodes.length == 1)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
typeNode.range, "1", (typeArgumentNodes ? typeArgumentNodes.length : 1).toString(10)
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], context, contextualTypeArguments, reportMode);
if (!typeArgument) return null;
var classReference = typeArgument.classReference;
if (!classReference) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
);
}
return null;
}
var overload = classReference.lookupOverload(OperatorKind.INDEXED_GET);
if (overload) {
if (overload.is(CommonFlags.STATIC)) {
assert(overload.signature.parameterTypes.length == 2);
return overload.signature.parameterTypes[1];
} else {
assert(overload.signature.parameterTypes.length == 1);
return overload.signature.parameterTypes[0];
}
}
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
);
}
return null;
}
private resolveBuiltinValueofType(
/** The type to resolve. */
typeNode: NamedTypeNode,
/** Relative context. */
context: Element,
/** Type arguments inherited through context, i.e. `T`. */
contextualTypeArguments: Map<string,Type> | null = null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = typeNode.typeArguments;
if (!(typeArgumentNodes && typeArgumentNodes.length == 1)) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
typeNode.range, "1", (typeArgumentNodes ? typeArgumentNodes.length : 1).toString(10)
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], context, contextualTypeArguments, reportMode);
if (!typeArgument) return null;
var classReference = typeArgument.classReference;
if (!classReference) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
);
}
return null;
}
var overload = classReference.lookupOverload(OperatorKind.INDEXED_GET);
if (overload) return overload.signature.returnType;
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
);
}
return null;
}
/** Resolves a type name to the program element it refers to. */
resolveTypeName(
/** The type name to resolve. */
typeName: TypeName,
/** Relative context. */
context: Element,
/** How to proceed with eventualy diagnostics. */
reportMode = ReportMode.REPORT
): Element | null {
var element = context.lookup(typeName.identifier.text);
if (!element) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Cannot_find_name_0,
typeName.range, typeName.identifier.text
);
}
return null;
}
var prev = typeName;
var next = typeName.next;
while (next) {
if (!(element = element.lookupInSelf(next.identifier.text))) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
next.range, next.identifier.text, prev.identifier.text
);
}
return null;
}
prev = next;
next = next.next;
}
return element;
}
/** Resolves an array of type arguments to concrete types. */
resolveTypeArguments(
/** Actual type parameter nodes. */
typeParameters: TypeParameterNode[],
/** Type arguments provided. */
typeArgumentNodes: TypeNode[] | null,
/** Relative context. */
context: Element,
/** Type arguments inherited through context, i.e. `T`. */
contextualTypeArguments: Map<string,Type> = makeMap<string,Type>(),
/** Alternative report node in case of empty type arguments. */
alternativeReportNode: Node | null = null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type[] | null {
var minParameterCount = 0;
var maxParameterCount = 0;
for (let i = 0, k = typeParameters.length; i < k; ++i) {
if (!typeParameters[i].defaultType) ++minParameterCount;
++maxParameterCount;
}
var argumentCount = typeArgumentNodes ? typeArgumentNodes.length : 0;
if (argumentCount < minParameterCount || argumentCount > maxParameterCount) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
argumentCount
? Range.join(
(<NamedTypeNode[]>typeArgumentNodes)[0].range,
(<NamedTypeNode[]>typeArgumentNodes)[argumentCount - 1].range
)
: assert(alternativeReportNode).range,
(argumentCount < minParameterCount ? minParameterCount : maxParameterCount).toString(10),
argumentCount.toString(10)
);
return null;
}
var typeArguments = new Array<Type>(maxParameterCount);
for (let i = 0; i < maxParameterCount; ++i) {
let type = i < argumentCount
? this.resolveType( // reports
(<NamedTypeNode[]>typeArgumentNodes)[i],
context,
contextualTypeArguments,
reportMode
)
: this.resolveType( // reports
assert(typeParameters[i].defaultType),
context,
contextualTypeArguments,
reportMode
);
if (!type) return null;
// TODO: check extendsType
contextualTypeArguments.set(typeParameters[i].name.text, type);
typeArguments[i] = type;
}
return typeArguments;
}
/** Resolves an identifier to the program element it refers to. */
resolveIdentifier(
/** The expression to resolve. */
identifier: IdentifierExpression,
/** Optional flow to search for scoped locals. */
flow: Flow | null,
/** Optional context to search. */
context: Element | null,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Element | null {
var name = identifier.text;
var element: Element | null;
if (flow) {
if (element = flow.lookup(name)) {
this.currentThisExpression = null;
this.currentElementExpression = null;
return element;
}
}
if (context) {
if (element = context.lookup(name)) {
this.currentThisExpression = null;
this.currentElementExpression = null;
return element;
}
}
if (element = this.program.lookupGlobal(name)) {
this.currentThisExpression = null;
this.currentElementExpression = null;
return element;
}
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Cannot_find_name_0,
identifier.range, name
);
}
return null;
}
/** Resolves a lazily compiled global, i.e. a static class field. */
ensureResolvedLazyGlobal(global: Global, reportMode: ReportMode = ReportMode.REPORT): bool {
if (global.is(CommonFlags.RESOLVED)) return true;
var typeNode = global.typeNode;
if (!typeNode) return false;
var type = this.resolveType( // reports
typeNode,
global.parent,
null,
reportMode
);
if (!type) return false;
global.setType(type);
return true;
}
/** Resolves a property access expression to the program element it refers to. */
resolvePropertyAccessExpression(
/** The expression to resolve. */
propertyAccess: PropertyAccessExpression,
/** Current flow. */
flow: Flow,
/** Current contextual type. */
contextualType: Type,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Element | null {
var targetExpression = propertyAccess.expression;
var target = this.resolveExpression(targetExpression, flow, contextualType, reportMode); // reports
if (!target) return null;
var propertyName = propertyAccess.property.text;
// Resolve variable-likes to their class type first
switch (target.kind) {
case ElementKind.GLOBAL: if (!this.ensureResolvedLazyGlobal(<Global>target, reportMode)) return null;
case ElementKind.LOCAL:
case ElementKind.FIELD: {
let type = (<VariableLikeElement>target).type; assert(type != Type.void);
let classReference = type.classReference;
if (!classReference) {
let typeClasses = this.program.typeClasses;
if (!type.is(TypeFlags.REFERENCE) && typeClasses.has(type.kind)) {
classReference = typeClasses.get(type.kind)!;
} else {
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
propertyAccess.property.range, propertyName, (<VariableLikeElement>target).type.toString()
);
return null;
}
}
target = classReference;
break;
}
case ElementKind.PROPERTY_PROTOTYPE: { // static
let getterInstance = this.resolveFunction( // reports
assert((<PropertyPrototype>target).getterPrototype), // must have a getter
null,
makeMap<string,Type>(),
reportMode
);
if (!getterInstance) return null;
let classReference = getterInstance.signature.returnType.classReference;
if (!classReference) {
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
propertyAccess.property.range, propertyName, getterInstance.signature.returnType.toString()
);
return null;
}
target = classReference;
break;
}
case ElementKind.PROPERTY: { // instance
let getterInstance = assert((<Property>target).getterInstance); // must have a getter
let classReference = getterInstance.signature.returnType.classReference;
if (!classReference) {
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
propertyAccess.property.range, propertyName, getterInstance.signature.returnType.toString()
);
return null;
}
target = classReference;
break;
}
case ElementKind.CLASS: { // property access on element access?
let elementExpression = this.currentElementExpression;
if (elementExpression) {
// let arrayType = this.program.determineBuiltinArrayType(<Class>target);
// if (!arrayType) {
let indexedGet = (<Class>target).lookupOverload(OperatorKind.INDEXED_GET);
if (!indexedGet) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
elementExpression.range, (<Class>target).internalName
);
return null;
}
let arrayType = indexedGet.signature.returnType;
// }
let classReference = arrayType.classReference;
if (!classReference) {
let typeClasses = this.program.typeClasses;
if (!arrayType.is(TypeFlags.REFERENCE) && typeClasses.has(arrayType.kind)) {
classReference = typeClasses.get(arrayType.kind)!;
} else {
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
propertyAccess.property.range, propertyName, arrayType.toString()
);
return null;
}
}
target = classReference;
}
break;
}
case ElementKind.FUNCTION_PROTOTYPE: { // function Symbol() + type Symbol = _Symbol
let shadowType = target.shadowType;
if (shadowType) {
if (!shadowType.is(CommonFlags.RESOLVED)) {
let resolvedType = this.resolveType(shadowType.typeNode, shadowType.parent, null, reportMode);
if (resolvedType) shadowType.setType(resolvedType);
}
let classReference = shadowType.type.classReference;
if (classReference) target = classReference.prototype;
break;
}
}
}
// Look up the member within
switch (target.kind) {
case ElementKind.CLASS_PROTOTYPE:
case ElementKind.CLASS: {
do {
let members = target.members;
if (members && members.has(propertyName)) {
this.currentThisExpression = targetExpression;
this.currentElementExpression = null;
return members.get(propertyName)!; // instance FIELD, static GLOBAL, FUNCTION_PROTOTYPE...
}
// traverse inherited static members on the base prototype if target is a class prototype
if (target.kind == ElementKind.CLASS_PROTOTYPE) {
if ((<ClassPrototype>target).basePrototype) {
target = <ClassPrototype>(<ClassPrototype>target).basePrototype;
} else {
break;
}
// traverse inherited instance members on the base class if target is a class instance
} else if (target.kind == ElementKind.CLASS) {
if ((<Class>target).base) {
target = <Class>(<Class>target).base;
} else {
break;
}
} else {
break;
}
} while (true);
break;
}
default: { // enums or other namespace-like elements
let members = target.members;
if (members && members.has(propertyName)) {
this.currentThisExpression = targetExpression;
this.currentElementExpression = null;
return members.get(propertyName)!; // static ENUMVALUE, static GLOBAL, static FUNCTION_PROTOTYPE...
}
break;
}
}
this.error(
DiagnosticCode.Property_0_does_not_exist_on_type_1,
propertyAccess.property.range, propertyName, target.internalName
);
return null;
}
/** Resolves an element access expression to the program element it refers to. */
resolveElementAccessExpression(
/** The expression to resolve. */
elementAccess: ElementAccessExpression,
/** Current flow. */
flow: Flow,
/** Current contextual type. */
contextualType: Type,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Element | null {
var targetExpression = elementAccess.expression;
var target = this.resolveExpression(targetExpression, flow, contextualType, reportMode); // reports
if (!target) return null;
switch (target.kind) {
case ElementKind.GLOBAL: if (!this.ensureResolvedLazyGlobal(<Global>target, reportMode)) return null;
case ElementKind.LOCAL:
case ElementKind.FIELD: {
let type = (<VariableLikeElement>target).type;
if (target = type.classReference) {
this.currentThisExpression = targetExpression;
this.currentElementExpression = elementAccess.elementExpression;
return target;
}
break;
}
case ElementKind.CLASS: {
// let arrayType = this.program.determineBuiltinArrayType(<Class>target);
// if (!arrayType) {
let indexedGet = (<Class>target).lookupOverload(OperatorKind.INDEXED_GET);
if (!indexedGet) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
elementAccess.range, (<Class>target).internalName
);
}
return null;
}
let arrayType = indexedGet.signature.returnType;
// }
if (targetExpression.kind == NodeKind.ELEMENTACCESS) { // nested element access
if (target = arrayType.classReference) {
this.currentThisExpression = targetExpression;
this.currentElementExpression = elementAccess.elementExpression;
return target;
}
return null;
}
this.currentThisExpression = targetExpression;
this.currentElementExpression = elementAccess.elementExpression;
return target;
}
}
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Operation_not_supported,
targetExpression.range
);
}
return null;
}
/** Determines the final type of an integer literal given the specified contextual type. */
determineIntegerLiteralType(
/** Integer literal value. */
intValue: I64,
/** Current contextual type. */
contextualType: Type
): Type {
if (!contextualType.is(TypeFlags.REFERENCE)) {
// compile to contextualType if matching
switch (contextualType.kind) {
case TypeKind.I8: {
if (i64_is_i8(intValue)) return Type.i8;
break;
}
case TypeKind.U8: {
if (i64_is_u8(intValue)) return Type.u8;
break;
}
case TypeKind.I16: {
if (i64_is_i16(intValue)) return Type.i16;
break;
}
case TypeKind.U16: {
if (i64_is_u16(intValue)) return Type.u16;
break;
}
case TypeKind.I32: {
if (i64_is_i32(intValue)) return Type.i32;
break;
}
case TypeKind.U32: {
if (i64_is_u32(intValue)) return Type.u32;
break;
}
case TypeKind.BOOL: {
if (i64_is_bool(intValue)) return Type.bool;
break;
}
case TypeKind.ISIZE: {
if (!this.program.options.isWasm64) {
if (i64_is_i32(intValue)) return Type.isize32;
break;
}
return Type.isize64;
}
case TypeKind.USIZE: {
if (!this.program.options.isWasm64) {
if (i64_is_u32(intValue)) return Type.usize32;
break;
}
return Type.usize64;
}
case TypeKind.I64: return Type.i64;
case TypeKind.U64: return Type.u64;
case TypeKind.F32: return Type.f32;
case TypeKind.F64: return Type.f64;
case TypeKind.VOID: break; // best fitting below
default: assert(false);
}
}
// otherwise compile to best fitting native type
if (i64_is_i32(intValue)) return Type.i32;
if (i64_is_u32(intValue)) return Type.u32;
return Type.i64;
}
/** Resolves any expression to the program element it refers to. */
resolveExpression(
/** The expression to resolve. */
expression: Expression,
/** Current flow. */
flow: Flow,
/** Current contextual type. */
contextualType: Type = Type.void,
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Element | null {
while (expression.kind == NodeKind.PARENTHESIZED) { // simply skip
expression = (<ParenthesizedExpression>expression).expression;
}
switch (expression.kind) {
case NodeKind.ASSERTION: {
return this.resolveAssertionExpression(
<AssertionExpression>expression,
flow, contextualType, reportMode
);
}
case NodeKind.UNARYPREFIX: {
return this.resolveUnaryPrefixExpression(
<UnaryPrefixExpression>expression,
flow, contextualType, reportMode
);
}
case NodeKind.UNARYPOSTFIX: {
return this.resolveUnaryPostfixExpression(
<UnaryPostfixExpression>expression,
flow, contextualType, reportMode
);
}
case NodeKind.BINARY: {
return this.resolveBinaryExpression(
<BinaryExpression>expression,
flow, contextualType, reportMode
);
}