@@ -663,39 +663,54 @@ static Optional<KnownProtocolKind>
663
663
getKnownProtocolKindIfAny (const ProtocolDecl *protocol) {
664
664
TypeChecker &tc = TypeChecker::createForContext (protocol->getASTContext ());
665
665
666
- // clang-format off
667
- #define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME (Id, _, __, ___ ) \
668
- if (protocol == tc.getProtocol (SourceLoc (), KnownProtocolKind::Id)) \
669
- return KnownProtocolKind::Id;
670
- #include " swift/AST/KnownProtocols.def"
671
- #undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
672
- // clang-format on
666
+ #define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME (Id, _, __, ___ ) \
667
+ if (protocol == tc.getProtocol (SourceLoc (), KnownProtocolKind::Id)) \
668
+ return KnownProtocolKind::Id;
669
+ #include " swift/AST/KnownProtocols.def"
670
+ #undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
673
671
674
672
return None;
675
673
}
676
674
677
675
Type TypeChecker::getDefaultType (ProtocolDecl *protocol, DeclContext *dc) {
678
676
if (auto knownProtocolKindIfAny = getKnownProtocolKindIfAny (protocol)) {
679
- Type t = evaluateOrDefault (
677
+ return evaluateOrDefault (
680
678
Context.evaluator ,
681
679
DefaultTypeRequest{knownProtocolKindIfAny.getValue (), dc}, nullptr );
682
- return t;
683
680
}
684
- return nullptr ;
681
+ return Type ();
682
+ }
683
+
684
+ static std::pair<const char *, bool > lookupDefaultTypeInfoForKnownProtocol (
685
+ const KnownProtocolKind knownProtocolKind) {
686
+ switch (knownProtocolKind) {
687
+ #define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME (Id, Name, typeName, \
688
+ performLocalLookup) \
689
+ case KnownProtocolKind::Id: \
690
+ return {typeName, performLocalLookup};
691
+ #include " swift/AST/KnownProtocols.def"
692
+ #undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
693
+ default :
694
+ return {nullptr , false };
695
+ }
685
696
}
686
697
687
698
llvm::Expected<Type>
688
699
swift::DefaultTypeRequest::evaluate (Evaluator &evaluator,
689
700
KnownProtocolKind knownProtocolKind,
690
701
const DeclContext *dc) const {
691
- const char *const name = getTypeName (knownProtocolKind);
702
+ const char *name;
703
+ bool performLocalLookup;
704
+ std::tie (name, performLocalLookup) =
705
+ lookupDefaultTypeInfoForKnownProtocol (knownProtocolKind);
692
706
if (!name)
693
707
return nullptr ;
694
708
695
- TypeChecker &tc = getTypeChecker ();
709
+ // FIXME: Creating a whole type checker just to do lookup is unnecessary.
710
+ TypeChecker &tc = TypeChecker::createForContext (dc->getASTContext ());
696
711
697
712
Type type;
698
- if (getPerformLocalLookup (knownProtocolKind) )
713
+ if (performLocalLookup )
699
714
type = lookupDefaultLiteralType (tc, dc, name);
700
715
701
716
if (!type)
@@ -710,10 +725,6 @@ swift::DefaultTypeRequest::evaluate(Evaluator &evaluator,
710
725
return type;
711
726
}
712
727
713
- TypeChecker &DefaultTypeRequest::getTypeChecker () const {
714
- return TypeChecker::createForContext (getDeclContext ()->getASTContext ());
715
- }
716
-
717
728
Expr *TypeChecker::foldSequence (SequenceExpr *expr, DeclContext *dc) {
718
729
ArrayRef<Expr*> Elts = expr->getElements ();
719
730
assert (Elts.size () > 1 && " inadequate number of elements in sequence" );
0 commit comments