Skip to content

Commit a5a79f2

Browse files
authored
Merge pull request #36721 from CodaFi/resoulver
[NFC] Move Some Type Resolution Methods from TypeChecker to TypeResolution
2 parents 2e9fb5a + 2c5166d commit a5a79f2

File tree

4 files changed

+81
-95
lines changed

4 files changed

+81
-95
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,9 @@ Type ConstraintSystem::openUnboundGenericType(
702702
// pointing at a generic TypeAliasDecl here. If we find a way to
703703
// handle generic TypeAliases elsewhere, this can just become a
704704
// call to BoundGenericType::get().
705-
return TypeChecker::applyUnboundGenericArguments(
706-
decl, parentTy, SourceLoc(),
707-
TypeResolution::forContextual(DC, None, /*unboundTyOpener*/ nullptr,
708-
/*placeholderHandler*/ nullptr),
709-
arguments);
705+
return TypeResolution::forContextual(DC, None, /*unboundTyOpener*/ nullptr,
706+
/*placeholderHandler*/ nullptr)
707+
.applyUnboundGenericArguments(decl, parentTy, SourceLoc(), arguments);
710708
}
711709

712710
static void checkNestedTypeConstraints(ConstraintSystem &cs, Type type,
@@ -1291,12 +1289,12 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
12911289
// Unqualified reference to a type.
12921290
if (auto typeDecl = dyn_cast<TypeDecl>(value)) {
12931291
// Resolve the reference to this type declaration in our current context.
1294-
auto type = TypeChecker::resolveTypeInContext(
1295-
typeDecl, nullptr,
1292+
auto type =
12961293
TypeResolution::forContextual(useDC, TypeResolverContext::InExpression,
12971294
/*unboundTyOpener*/ nullptr,
1298-
/*placeholderHandler*/ nullptr),
1299-
/*isSpecialized=*/false);
1295+
/*placeholderHandler*/ nullptr)
1296+
.resolveTypeInContext(typeDecl, /*foundDC*/ nullptr,
1297+
/*isSpecialized=*/false);
13001298

13011299
checkNestedTypeConstraints(*this, type, locator);
13021300

lib/Sema/TypeCheckType.cpp

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,15 @@ TypeChecker::getDynamicBridgedThroughObjCClass(DeclContext *dc,
419419
return dc->getASTContext().getBridgedToObjC(dc, valueType);
420420
}
421421

422-
Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
423-
TypeResolution resolution,
424-
bool isSpecialized) {
425-
auto fromDC = resolution.getDeclContext();
422+
Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
423+
DeclContext *foundDC,
424+
bool isSpecialized) const {
425+
auto fromDC = getDeclContext();
426426
ASTContext &ctx = fromDC->getASTContext();
427427

428428
// If we found a generic parameter, map to the archetype if there is one.
429429
if (auto genericParam = dyn_cast<GenericTypeParamDecl>(typeDecl)) {
430-
return resolution.mapTypeIntoContext(
431-
genericParam->getDeclaredInterfaceType());
430+
return mapTypeIntoContext(genericParam->getDeclaredInterfaceType());
432431
}
433432

434433
if (!isSpecialized) {
@@ -440,13 +439,12 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
440439
parentDC = parentDC->getParent()) {
441440
auto *parentNominal = parentDC->getSelfNominalTypeDecl();
442441
if (parentNominal == nominalType)
443-
return resolution.mapTypeIntoContext(
444-
parentDC->getDeclaredInterfaceType());
442+
return mapTypeIntoContext(parentDC->getDeclaredInterfaceType());
445443
if (isa<ExtensionDecl>(parentDC)) {
446444
auto *extendedType = parentNominal;
447445
while (extendedType != nullptr) {
448446
if (extendedType == nominalType)
449-
return resolution.mapTypeIntoContext(
447+
return mapTypeIntoContext(
450448
extendedType->getDeclaredInterfaceType());
451449
extendedType = extendedType->getParent()->getSelfNominalTypeDecl();
452450
}
@@ -466,11 +464,11 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
466464
if (auto *ugAliasDecl =
467465
dyn_cast<TypeAliasDecl>(unboundGeneric->getAnyGeneric())) {
468466
if (ugAliasDecl == aliasDecl) {
469-
if (resolution.getStage() == TypeResolutionStage::Structural &&
467+
if (getStage() == TypeResolutionStage::Structural &&
470468
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
471469
return aliasDecl->getStructuralType();
472470
}
473-
return resolution.mapTypeIntoContext(
471+
return mapTypeIntoContext(
474472
aliasDecl->getDeclaredInterfaceType());
475473
}
476474

@@ -481,12 +479,11 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
481479
if (auto *aliasType =
482480
dyn_cast<TypeAliasType>(extendedType.getPointer())) {
483481
if (aliasType->getDecl() == aliasDecl) {
484-
if (resolution.getStage() == TypeResolutionStage::Structural &&
482+
if (getStage() == TypeResolutionStage::Structural &&
485483
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
486484
return aliasDecl->getStructuralType();
487485
}
488-
return resolution.mapTypeIntoContext(
489-
aliasDecl->getDeclaredInterfaceType());
486+
return mapTypeIntoContext(aliasDecl->getDeclaredInterfaceType());
490487
}
491488
extendedType = aliasType->getParent();
492489
continue;
@@ -507,12 +504,11 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
507504
return aliasDecl->getUnboundGenericType();
508505

509506
// Otherwise, return the appropriate type.
510-
if (resolution.getStage() == TypeResolutionStage::Structural &&
507+
if (getStage() == TypeResolutionStage::Structural &&
511508
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
512509
return aliasDecl->getStructuralType();
513510
}
514-
return resolution.mapTypeIntoContext(
515-
aliasDecl->getDeclaredInterfaceType());
511+
return mapTypeIntoContext(aliasDecl->getDeclaredInterfaceType());
516512
}
517513

518514
// When a nominal type used outside its context, return the unbound
@@ -539,12 +535,11 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
539535
if (!foundDC->getDeclaredInterfaceType())
540536
return ErrorType::get(ctx);
541537

542-
selfType =
543-
resolution.mapTypeIntoContext(foundDC->getDeclaredInterfaceType());
538+
selfType = mapTypeIntoContext(foundDC->getDeclaredInterfaceType());
544539
} else {
545540
// Otherwise, we want the protocol 'Self' type for
546541
// substituting into alias types and associated types.
547-
selfType = resolution.mapTypeIntoContext(foundDC->getSelfInterfaceType());
542+
selfType = mapTypeIntoContext(foundDC->getSelfInterfaceType());
548543

549544
if (selfType->is<GenericTypeParamType>()) {
550545
if (typeDecl->getDeclContext()->getSelfProtocolDecl()) {
@@ -559,9 +554,9 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
559554
// because we use the Sequence.SubSequence default instead of
560555
// the Collection.SubSequence default, even when the conforming
561556
// type wants to conform to Collection.
562-
if (resolution.getStage() == TypeResolutionStage::Structural) {
563-
return resolution.resolveSelfAssociatedType(selfType, foundDC,
564-
typeDecl->getName());
557+
if (getStage() == TypeResolutionStage::Structural) {
558+
return resolveSelfAssociatedType(selfType, foundDC,
559+
typeDecl->getName());
565560
} else if (auto assocType = dyn_cast<AssociatedTypeDecl>(typeDecl)) {
566561
typeDecl = assocType->getAssociatedTypeAnchor();
567562
}
@@ -587,8 +582,8 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
587582
}
588583

589584
// Finally, substitute the base type into the member type.
590-
return substMemberTypeWithBase(fromDC->getParentModule(), typeDecl, selfType,
591-
resolution.usesArchetypes());
585+
return TypeChecker::substMemberTypeWithBase(
586+
fromDC->getParentModule(), typeDecl, selfType, usesArchetypes());
592587
}
593588

594589
static TypeResolutionOptions
@@ -685,7 +680,8 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
685680
/// If the type is itself not generic, this does nothing.
686681
///
687682
/// This function emits diagnostics about an invalid type or the wrong number
688-
/// of generic arguments, whereas applyUnboundGenericArguments requires this
683+
/// of generic arguments, whereas
684+
/// \c TypeResolution::applyUnboundGenericArguments requires this
689685
/// to be in a correct and valid form.
690686
///
691687
/// \param type The generic type to which to apply arguments.
@@ -697,7 +693,7 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
697693
/// \returns A BoundGenericType bound to the given arguments, or null on
698694
/// error.
699695
///
700-
/// \see applyUnboundGenericArguments
696+
/// \see TypeResolution::applyUnboundGenericArguments
701697
static Type applyGenericArguments(Type type, TypeResolution resolution,
702698
GenericParamList *silParams,
703699
ComponentIdentTypeRepr *comp) {
@@ -831,8 +827,8 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
831827
args.push_back(substTy);
832828
}
833829

834-
const auto result = TypeChecker::applyUnboundGenericArguments(
835-
decl, unboundType->getParent(), loc, resolution, args);
830+
const auto result = resolution.applyUnboundGenericArguments(
831+
decl, unboundType->getParent(), loc, args);
836832

837833
// Migration hack.
838834
bool isMutablePointer;
@@ -888,10 +884,9 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
888884
}
889885

890886
/// Apply generic arguments to the given type.
891-
Type TypeChecker::applyUnboundGenericArguments(GenericTypeDecl *decl,
892-
Type parentTy, SourceLoc loc,
893-
TypeResolution resolution,
894-
ArrayRef<Type> genericArgs) {
887+
Type TypeResolution::applyUnboundGenericArguments(
888+
GenericTypeDecl *decl, Type parentTy, SourceLoc loc,
889+
ArrayRef<Type> genericArgs) const {
895890
assert(genericArgs.size() == decl->getGenericParams()->size() &&
896891
"invalid arguments, use applyGenericArguments for diagnostic emitting");
897892

@@ -931,9 +926,7 @@ Type TypeChecker::applyUnboundGenericArguments(GenericTypeDecl *decl,
931926
auto genericSig = genericEnv->getGenericSignature();
932927
for (auto gp : genericSig->getGenericParams()) {
933928
subs[gp->getCanonicalType()->castTo<GenericTypeParamType>()] =
934-
(resolution.usesArchetypes()
935-
? genericEnv->mapTypeIntoContext(gp)
936-
: gp);
929+
(usesArchetypes() ? genericEnv->mapTypeIntoContext(gp) : gp);
937930
}
938931
}
939932

@@ -957,21 +950,19 @@ Type TypeChecker::applyUnboundGenericArguments(GenericTypeDecl *decl,
957950

958951
// Check the generic arguments against the requirements of the declaration's
959952
// generic signature.
960-
auto dc = resolution.getDeclContext();
961-
auto *module = dc->getParentModule();
953+
auto *module = getDeclContext()->getParentModule();
962954

963-
if (!skipRequirementsCheck &&
964-
resolution.getStage() > TypeResolutionStage::Structural) {
965-
auto result = checkGenericArguments(
966-
dc, loc, noteLoc,
967-
UnboundGenericType::get(decl, parentTy, dc->getASTContext()),
955+
if (!skipRequirementsCheck && getStage() > TypeResolutionStage::Structural) {
956+
auto result = TypeChecker::checkGenericArguments(
957+
getDeclContext(), loc, noteLoc,
958+
UnboundGenericType::get(decl, parentTy, getASTContext()),
968959
genericSig->getGenericParams(), genericSig->getRequirements(),
969960
QueryTypeSubstitutionMap{subs});
970961

971962
switch (result) {
972963
case RequirementCheckResult::Failure:
973964
case RequirementCheckResult::SubstitutionFailure:
974-
return ErrorType::get(dc->getASTContext());
965+
return ErrorType::get(getASTContext());
975966
case RequirementCheckResult::Success:
976967
break;
977968
}
@@ -1071,8 +1062,8 @@ static Type resolveTypeDecl(TypeDecl *typeDecl, DeclContext *foundDC,
10711062
ComponentIdentTypeRepr *comp) {
10721063
// Resolve the type declaration to a specific type. How this occurs
10731064
// depends on the current context and where the type was found.
1074-
Type type = TypeChecker::resolveTypeInContext(
1075-
typeDecl, foundDC, resolution, isa<GenericIdentTypeRepr>(comp));
1065+
Type type = resolution.resolveTypeInContext(typeDecl, foundDC,
1066+
isa<GenericIdentTypeRepr>(comp));
10761067

10771068
if (type->hasError() && foundDC &&
10781069
(isa<AssociatedTypeDecl>(typeDecl) || isa<TypeAliasDecl>(typeDecl))) {
@@ -3527,9 +3518,8 @@ TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
35273518
return ErrorType::get(getASTContext());
35283519
}
35293520

3530-
if (!TypeChecker::applyUnboundGenericArguments(
3531-
dictDecl, nullptr, repr->getStartLoc(), resolution,
3532-
{keyTy, valueTy})) {
3521+
if (!resolution.applyUnboundGenericArguments(
3522+
dictDecl, nullptr, repr->getStartLoc(), {keyTy, valueTy})) {
35333523
assert(getASTContext().Diags.hadAnyError());
35343524
return ErrorType::get(getASTContext());
35353525
}

lib/Sema/TypeCheckType.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,41 @@ class TypeResolution {
407407
/// Determine whether the given two types are equivalent within this
408408
/// type resolution context.
409409
bool areSameType(Type type1, Type type2) const;
410+
411+
/// Resolve a reference to the given type declaration within a particular
412+
/// context.
413+
///
414+
/// This routine aids unqualified name lookup for types by performing the
415+
/// resolution necessary to rectify the declaration found by name lookup with
416+
/// the declaration context from which name lookup started.
417+
///
418+
/// \param typeDecl The type declaration found by name lookup.
419+
/// \param foundDC The declaration context this type reference was found in.
420+
/// \param isSpecialized Whether the type will have generic arguments applied.
421+
///
422+
/// \returns the resolved type.
423+
Type resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
424+
bool isSpecialized) const;
425+
426+
/// Apply generic arguments to the unbound generic type represented by the
427+
/// given declaration and parent type.
428+
///
429+
/// This function requires the correct number of generic arguments,
430+
/// whereas applyGenericArguments emits diagnostics in those cases.
431+
///
432+
/// \param decl The declaration that the resulting bound generic type
433+
/// shall reference.
434+
/// \param parentTy The parent type.
435+
/// \param loc The source location for diagnostic reporting.
436+
/// \param genericArgs The list of generic arguments to apply.
437+
///
438+
/// \returns A BoundGenericType bound to the given arguments, or null on
439+
/// error.
440+
///
441+
/// \see applyGenericArguments
442+
Type applyUnboundGenericArguments(GenericTypeDecl *decl, Type parentTy,
443+
SourceLoc loc,
444+
ArrayRef<Type> genericArgs) const;
410445
};
411446

412447
} // end namespace swift

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class GenericSignatureBuilder;
4343
class NominalTypeDecl;
4444
class NormalProtocolConformance;
4545
class RootProtocolConformance;
46-
class TypeResolution;
4746
class TypeResolutionOptions;
4847
class TypoCorrectionResults;
4948
class ExprPattern;
@@ -264,42 +263,6 @@ void checkUnsupportedProtocolType(ASTContext &ctx,
264263
void checkUnsupportedProtocolType(ASTContext &ctx,
265264
GenericParamList *genericParams);
266265

267-
/// Resolve a reference to the given type declaration within a particular
268-
/// context.
269-
///
270-
/// This routine aids unqualified name lookup for types by performing the
271-
/// resolution necessary to rectify the declaration found by name lookup with
272-
/// the declaration context from which name lookup started.
273-
///
274-
/// \param typeDecl The type declaration found by name lookup.
275-
/// \param isSpecialized Whether the type will have generic arguments applied.
276-
/// \param resolution The resolution to perform.
277-
///
278-
/// \returns the resolved type.
279-
Type resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
280-
TypeResolution resolution, bool isSpecialized);
281-
282-
/// Apply generic arguments to the unbound generic type represented by the
283-
/// given declaration and parent type.
284-
///
285-
/// This function requires the correct number of generic arguments,
286-
/// whereas applyGenericArguments emits diagnostics in those cases.
287-
///
288-
/// \param decl The declaration that the resulting bound generic type
289-
/// shall reference.
290-
/// \param parentTy The parent type.
291-
/// \param loc The source location for diagnostic reporting.
292-
/// \param resolution The type resolution.
293-
/// \param genericArgs The list of generic arguments to apply.
294-
///
295-
/// \returns A BoundGenericType bound to the given arguments, or null on
296-
/// error.
297-
///
298-
/// \see applyGenericArguments
299-
Type applyUnboundGenericArguments(GenericTypeDecl *decl, Type parentTy,
300-
SourceLoc loc, TypeResolution resolution,
301-
ArrayRef<Type> genericArgs);
302-
303266
/// Substitute the given base type into the type of the given nested type,
304267
/// producing the effective type that the nested type will have.
305268
///

0 commit comments

Comments
 (0)