Skip to content

Commit 9da2e86

Browse files
committed
Collapse infrastructure around DefaultTypeRequest
This can no longer live attached to the request.
1 parent eb79f3e commit 9da2e86

File tree

3 files changed

+38
-70
lines changed

3 files changed

+38
-70
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,20 +507,6 @@ class DefaultTypeRequest
507507
bool isCached() const { return true; }
508508
Optional<Type> getCachedResult() const;
509509
void cacheResult(Type value) const;
510-
511-
private:
512-
KnownProtocolKind getKnownProtocolKind() const {
513-
return std::get<0>(getStorage());
514-
}
515-
const DeclContext *getDeclContext() const {
516-
return std::get<1>(getStorage());
517-
}
518-
519-
static const char *getTypeName(KnownProtocolKind);
520-
static bool getPerformLocalLookup(KnownProtocolKind);
521-
TypeChecker &getTypeChecker() const;
522-
SourceFile *getSourceFile() const;
523-
Type &getCache() const;
524510
};
525511

526512
/// Retrieve information about a property wrapper type.

lib/AST/TypeCheckRequests.cpp

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -508,49 +508,20 @@ void swift::simple_display(llvm::raw_ostream &out,
508508
// DefaultTypeRequest caching.
509509
//----------------------------------------------------------------------------//
510510

511-
SourceFile *DefaultTypeRequest::getSourceFile() const {
512-
return getDeclContext()->getParentSourceFile();
513-
}
514-
515-
Type &DefaultTypeRequest::getCache() const {
516-
return getDeclContext()->getASTContext().getDefaultTypeRequestCache(
517-
getSourceFile(), getKnownProtocolKind());
518-
}
519-
520511
Optional<Type> DefaultTypeRequest::getCachedResult() const {
521-
auto const &cachedType = getCache();
512+
auto *DC = std::get<1>(getStorage());
513+
auto knownProtocolKind = std::get<0>(getStorage());
514+
const auto &cachedType = DC->getASTContext().getDefaultTypeRequestCache(
515+
DC->getParentSourceFile(), knownProtocolKind);
522516
return cachedType ? Optional<Type>(cachedType) : None;
523517
}
524518

525-
void DefaultTypeRequest::cacheResult(Type value) const { getCache() = value; }
526-
527-
const char *
528-
DefaultTypeRequest::getTypeName(const KnownProtocolKind knownProtocolKind) {
529-
switch (knownProtocolKind) {
530-
531-
// clang-format off
532-
# define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup) \
533-
case KnownProtocolKind::Id: return typeName;
534-
# include "swift/AST/KnownProtocols.def"
535-
# undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
536-
//clang-format on
537-
538-
default: return nullptr;
539-
}
540-
}
541-
542-
bool DefaultTypeRequest::getPerformLocalLookup(const KnownProtocolKind knownProtocolKind) {
543-
switch (knownProtocolKind) {
544-
545-
// clang-format off
546-
# define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup) \
547-
case KnownProtocolKind::Id: return performLocalLookup;
548-
# include "swift/AST/KnownProtocols.def"
549-
# undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
550-
//clang-format on
551-
552-
default: return false;
553-
}
519+
void DefaultTypeRequest::cacheResult(Type value) const {
520+
auto *DC = std::get<1>(getStorage());
521+
auto knownProtocolKind = std::get<0>(getStorage());
522+
auto &cacheEntry = DC->getASTContext().getDefaultTypeRequestCache(
523+
DC->getParentSourceFile(), knownProtocolKind);
524+
cacheEntry = value;
554525
}
555526

556527
bool PropertyWrapperTypeInfoRequest::isCached() const {

lib/Sema/TypeCheckExpr.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -663,39 +663,54 @@ static Optional<KnownProtocolKind>
663663
getKnownProtocolKindIfAny(const ProtocolDecl *protocol) {
664664
TypeChecker &tc = TypeChecker::createForContext(protocol->getASTContext());
665665

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
673671

674672
return None;
675673
}
676674

677675
Type TypeChecker::getDefaultType(ProtocolDecl *protocol, DeclContext *dc) {
678676
if (auto knownProtocolKindIfAny = getKnownProtocolKindIfAny(protocol)) {
679-
Type t = evaluateOrDefault(
677+
return evaluateOrDefault(
680678
Context.evaluator,
681679
DefaultTypeRequest{knownProtocolKindIfAny.getValue(), dc}, nullptr);
682-
return t;
683680
}
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+
}
685696
}
686697

687698
llvm::Expected<Type>
688699
swift::DefaultTypeRequest::evaluate(Evaluator &evaluator,
689700
KnownProtocolKind knownProtocolKind,
690701
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);
692706
if (!name)
693707
return nullptr;
694708

695-
TypeChecker &tc = getTypeChecker();
709+
// FIXME: Creating a whole type checker just to do lookup is unnecessary.
710+
TypeChecker &tc = TypeChecker::createForContext(dc->getASTContext());
696711

697712
Type type;
698-
if (getPerformLocalLookup(knownProtocolKind))
713+
if (performLocalLookup)
699714
type = lookupDefaultLiteralType(tc, dc, name);
700715

701716
if (!type)
@@ -710,10 +725,6 @@ swift::DefaultTypeRequest::evaluate(Evaluator &evaluator,
710725
return type;
711726
}
712727

713-
TypeChecker &DefaultTypeRequest::getTypeChecker() const {
714-
return TypeChecker::createForContext(getDeclContext()->getASTContext());
715-
}
716-
717728
Expr *TypeChecker::foldSequence(SequenceExpr *expr, DeclContext *dc) {
718729
ArrayRef<Expr*> Elts = expr->getElements();
719730
assert(Elts.size() > 1 && "inadequate number of elements in sequence");

0 commit comments

Comments
 (0)