Skip to content

Commit 3ac6efd

Browse files
authored
[NFC] Minor refactorings for resolver (#2457)
1 parent 707fc7a commit 3ac6efd

File tree

1 file changed

+83
-37
lines changed

1 file changed

+83
-37
lines changed

src/resolver.ts

+83-37
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ export class Resolver extends DiagnosticEmitter {
293293
// Handle special built-in types
294294
if (isSimpleType) {
295295
let text = nameNode.identifier.text;
296-
if (text == CommonNames.native) return this.resolveBuiltinNativeType(node, ctxElement, ctxTypes, reportMode);
297-
if (text == CommonNames.indexof) return this.resolveBuiltinIndexofType(node, ctxElement, ctxTypes, reportMode);
298-
if (text == CommonNames.valueof) return this.resolveBuiltinValueofType(node, ctxElement, ctxTypes, reportMode);
296+
if (text == CommonNames.native) return this.resolveBuiltinNativeType(node, ctxElement, ctxTypes, reportMode);
297+
if (text == CommonNames.indexof) return this.resolveBuiltinIndexofType(node, ctxElement, ctxTypes, reportMode);
298+
if (text == CommonNames.valueof) return this.resolveBuiltinValueofType(node, ctxElement, ctxTypes, reportMode);
299299
if (text == CommonNames.returnof) return this.resolveBuiltinReturnTypeType(node, ctxElement, ctxTypes, reportMode);
300-
if (text == CommonNames.nonnull) return this.resolveBuiltinNotNullableType(node, ctxElement, ctxTypes, reportMode);
300+
if (text == CommonNames.nonnull) return this.resolveBuiltinNotNullableType(node, ctxElement, ctxTypes, reportMode);
301301
}
302302

303303
// Resolve normally
@@ -447,17 +447,17 @@ export class Resolver extends DiagnosticEmitter {
447447
switch (typeArgument.kind) {
448448
case TypeKind.I8:
449449
case TypeKind.I16:
450-
case TypeKind.I32: return Type.i32;
450+
case TypeKind.I32: return Type.i32;
451451
case TypeKind.ISIZE: if (!this.program.options.isWasm64) return Type.i32;
452-
case TypeKind.I64: return Type.i64;
452+
case TypeKind.I64: return Type.i64;
453453
case TypeKind.U8:
454454
case TypeKind.U16:
455455
case TypeKind.U32:
456456
case TypeKind.BOOL: return Type.u32;
457457
case TypeKind.USIZE: if (!this.program.options.isWasm64) return Type.u32;
458-
case TypeKind.U64: return Type.u64;
459-
case TypeKind.F32: return Type.f32;
460-
case TypeKind.F64: return Type.f64;
458+
case TypeKind.U64: return Type.u64;
459+
case TypeKind.F32: return Type.f32;
460+
case TypeKind.F64: return Type.f64;
461461
case TypeKind.V128: return Type.v128;
462462
case TypeKind.VOID: return Type.void;
463463
default: assert(false);
@@ -491,12 +491,13 @@ export class Resolver extends DiagnosticEmitter {
491491
}
492492
var overload = classReference.lookupOverload(OperatorKind.INDEXED_GET);
493493
if (overload) {
494+
let parameterTypes = overload.signature.parameterTypes;
494495
if (overload.is(CommonFlags.STATIC)) {
495-
assert(overload.signature.parameterTypes.length == 2);
496-
return overload.signature.parameterTypes[1];
496+
assert(parameterTypes.length == 2);
497+
return parameterTypes[1];
497498
} else {
498-
assert(overload.signature.parameterTypes.length == 1);
499-
return overload.signature.parameterTypes[0];
499+
assert(parameterTypes.length == 1);
500+
return parameterTypes[0];
500501
}
501502
}
502503
if (reportMode == ReportMode.REPORT) {
@@ -631,8 +632,9 @@ export class Resolver extends DiagnosticEmitter {
631632
/** How to proceed with eventual diagnostics. */
632633
reportMode: ReportMode = ReportMode.REPORT
633634
): Type[] | null {
634-
var minParameterCount = 0;
635-
var maxParameterCount = 0;
635+
var
636+
minParameterCount = 0,
637+
maxParameterCount = 0;
636638
for (let i = 0, k = typeParameters.length; i < k; ++i) {
637639
if (!typeParameters[i].defaultType) ++minParameterCount;
638640
++maxParameterCount;
@@ -730,7 +732,9 @@ export class Resolver extends DiagnosticEmitter {
730732

731733
// infer types with generic components while updating contextual types
732734
for (let i = 0; i < numParameters; ++i) {
733-
let argumentExpression = i < numArguments ? argumentNodes[i] : parameterNodes[i].initializer;
735+
let argumentExpression = i < numArguments
736+
? argumentNodes[i]
737+
: parameterNodes[i].initializer;
734738
if (!argumentExpression) {
735739
// optional but not have initializer should be handled in the other place
736740
if (parameterNodes[i].parameterKind == ParameterKind.OPTIONAL) {
@@ -748,7 +752,15 @@ export class Resolver extends DiagnosticEmitter {
748752
let typeNode = parameterNodes[i].type;
749753
if (typeNode.hasGenericComponent(typeParameterNodes)) {
750754
let type = this.resolveExpression(argumentExpression, ctxFlow, Type.auto, ReportMode.SWALLOW);
751-
if (type) this.propagateInferredGenericTypes(typeNode, type, prototype, contextualTypeArguments, typeParameterNames);
755+
if (type) {
756+
this.propagateInferredGenericTypes(
757+
typeNode,
758+
type,
759+
prototype,
760+
contextualTypeArguments,
761+
typeParameterNames
762+
);
763+
}
752764
}
753765
}
754766

@@ -766,13 +778,19 @@ export class Resolver extends DiagnosticEmitter {
766778
let defaultType = typeParameterNode.defaultType;
767779
if (defaultType) {
768780
// Default parameters are resolved in context of the called function, not the calling function
781+
let parent = prototype.parent;
769782
let defaultTypeContextualTypeArguments: Map<string, Type> | null = null;
770-
if (prototype.parent.kind == ElementKind.CLASS) {
771-
defaultTypeContextualTypeArguments = (<Class>prototype.parent).contextualTypeArguments;
772-
} else if (prototype.parent.kind == ElementKind.FUNCTION) {
773-
defaultTypeContextualTypeArguments = (<Function>prototype.parent).contextualTypeArguments;
783+
if (parent.kind == ElementKind.CLASS) {
784+
defaultTypeContextualTypeArguments = (<Class>parent).contextualTypeArguments;
785+
} else if (parent.kind == ElementKind.FUNCTION) {
786+
defaultTypeContextualTypeArguments = (<Function>parent).contextualTypeArguments;
774787
}
775-
let resolvedDefaultType = this.resolveType(defaultType, prototype, defaultTypeContextualTypeArguments, reportMode);
788+
let resolvedDefaultType = this.resolveType(
789+
defaultType,
790+
prototype,
791+
defaultTypeContextualTypeArguments,
792+
reportMode
793+
);
776794
if (!resolvedDefaultType) return null;
777795
resolvedTypeArguments[i] = resolvedDefaultType;
778796
continue;
@@ -825,7 +843,13 @@ export class Resolver extends DiagnosticEmitter {
825843
let typeArguments = classReference.typeArguments;
826844
if (typeArguments && typeArguments.length == typeArgumentNodes.length) {
827845
for (let i = 0, k = typeArguments.length; i < k; ++i) {
828-
this.propagateInferredGenericTypes(typeArgumentNodes[i], typeArguments[i], ctxElement, ctxTypes, typeParameterNames);
846+
this.propagateInferredGenericTypes(
847+
typeArgumentNodes[i],
848+
typeArguments[i],
849+
ctxElement,
850+
ctxTypes,
851+
typeParameterNames
852+
);
829853
}
830854
return;
831855
}
@@ -835,9 +859,10 @@ export class Resolver extends DiagnosticEmitter {
835859
let name = namedTypeNode.name.identifier.text;
836860
if (ctxTypes.has(name)) {
837861
let currentType = assert(ctxTypes.get(name));
838-
if (currentType == Type.auto || (typeParameterNames.has(name) && currentType.isAssignableTo(type))) {
839-
ctxTypes.set(name, type);
840-
}
862+
if (
863+
currentType == Type.auto ||
864+
(typeParameterNames.has(name) && currentType.isAssignableTo(type))
865+
) ctxTypes.set(name, type);
841866
}
842867
}
843868
} else if (node.kind == NodeKind.FUNCTIONTYPE) { // foo<T>(bar: (baz: T) => i32))
@@ -847,15 +872,34 @@ export class Resolver extends DiagnosticEmitter {
847872
if (signatureReference) {
848873
let parameterTypes = signatureReference.parameterTypes;
849874
for (let i = 0, k = min(parameterTypes.length, parameterNodes.length) ; i < k; ++i) {
850-
this.propagateInferredGenericTypes(parameterNodes[i].type, parameterTypes[i], ctxElement, ctxTypes, typeParameterNames);
875+
this.propagateInferredGenericTypes(
876+
parameterNodes[i].type,
877+
parameterTypes[i],
878+
ctxElement,
879+
ctxTypes,
880+
typeParameterNames
881+
);
851882
}
852-
if (signatureReference.returnType != Type.void) {
853-
this.propagateInferredGenericTypes(functionTypeNode.returnType, signatureReference.returnType, ctxElement, ctxTypes, typeParameterNames);
883+
let returnType = signatureReference.returnType;
884+
if (returnType != Type.void) {
885+
this.propagateInferredGenericTypes(
886+
functionTypeNode.returnType,
887+
returnType,
888+
ctxElement,
889+
ctxTypes,
890+
typeParameterNames
891+
);
854892
}
855893
let thisType = signatureReference.thisType;
856894
let explicitThisType = functionTypeNode.explicitThisType;
857895
if (thisType && explicitThisType) {
858-
this.propagateInferredGenericTypes(explicitThisType, thisType, ctxElement, ctxTypes, typeParameterNames);
896+
this.propagateInferredGenericTypes(
897+
explicitThisType,
898+
thisType,
899+
ctxElement,
900+
ctxTypes,
901+
typeParameterNames
902+
);
859903
}
860904
return;
861905
}
@@ -1251,13 +1295,15 @@ export class Resolver extends DiagnosticEmitter {
12511295
/** Resolves a lazily compiled global, i.e. a static class field or annotated `@lazy`. */
12521296
private ensureResolvedLazyGlobal(global: Global, reportMode: ReportMode = ReportMode.REPORT): bool {
12531297
if (global.is(CommonFlags.RESOLVED)) return true;
1254-
var type: Type | null;
12551298
var typeNode = global.typeNode;
1256-
if (typeNode) {
1257-
type = this.resolveType(typeNode, global.parent, null, reportMode);
1258-
} else {
1259-
type = this.resolveExpression(assert(global.initializerNode), global.file.startFunction.flow, Type.auto, reportMode);
1260-
}
1299+
var type = typeNode
1300+
? this.resolveType(typeNode, global.parent, null, reportMode)
1301+
: this.resolveExpression(
1302+
assert(global.initializerNode),
1303+
global.file.startFunction.flow,
1304+
Type.auto,
1305+
reportMode
1306+
);
12611307
if (!type) return false;
12621308
global.setType(type); // also sets resolved
12631309
return true;
@@ -2632,7 +2678,7 @@ export class Resolver extends DiagnosticEmitter {
26322678
if (
26332679
functionType &&
26342680
declaration.arrowKind != ArrowKind.NONE &&
2635-
body && body.kind == NodeKind.EXPRESSION &&
2681+
body && body.kind == NodeKind.EXPRESSION &&
26362682
isTypeOmitted(signature.returnType)
26372683
) {
26382684
// (x) => ret, infer return type accordingt to `ret`

0 commit comments

Comments
 (0)