Skip to content

Commit b7f3697

Browse files
author
David Ungar
authored
Merge pull request #21907 from davidungar/A-defaultTypeReq-1-15-10
Use Request to getDefaultType & move cache to SourceFile.
2 parents 49bea70 + 66503ea commit b7f3697

13 files changed

+247
-147
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,9 @@ class ASTContext final {
942942
return !LangOpts.EnableAccessControl;
943943
}
944944

945+
/// Each kind and SourceFile has its own cache for a Type.
946+
Type &getDefaultTypeRequestCache(SourceFile *, KnownProtocolKind);
947+
945948
private:
946949
friend Decl;
947950
Optional<RawComment> getRawComment(const Decl *D);

include/swift/AST/DeclContext.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
#include "swift/AST/ResilienceExpansion.h"
2525
#include "swift/AST/TypeAlignments.h"
2626
#include "swift/Basic/LLVM.h"
27-
#include "swift/Basic/SourceLoc.h"
2827
#include "swift/Basic/STLExtras.h"
28+
#include "swift/Basic/SourceLoc.h"
2929
#include "llvm/ADT/PointerEmbeddedInt.h"
3030
#include "llvm/ADT/PointerIntPair.h"
3131
#include "llvm/ADT/PointerUnion.h"
32+
#include "llvm/Support/raw_ostream.h"
33+
34+
#include <type_traits>
3235

3336
namespace llvm {
3437
class raw_ostream;
@@ -586,7 +589,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
586589
bool walkContext(ASTWalker &Walker);
587590

588591
void dumpContext() const;
589-
unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0) const;
592+
unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0,
593+
bool onlyAPartialLine = false) const;
590594

591595
// Only allow allocation of DeclContext using the allocator in ASTContext.
592596
void *operator new(size_t Bytes, ASTContext &C,
@@ -776,7 +780,18 @@ class IterableDeclContext {
776780
/// member is an invisible addition.
777781
void addMemberSilently(Decl *member, Decl *hint = nullptr) const;
778782
};
779-
783+
784+
/// Define simple_display for DeclContexts but not for subclasses in order to
785+
/// avoid ambiguities with Decl* arguments.
786+
template <typename ParamT, typename = typename std::enable_if<
787+
std::is_same<ParamT, DeclContext>::value>::type>
788+
void simple_display(llvm::raw_ostream &out, const ParamT *dc) {
789+
if (dc)
790+
dc->printContext(out, 0, true);
791+
else
792+
out << "(null)";
793+
}
794+
780795
} // end namespace swift
781796

782797
namespace llvm {

include/swift/AST/KnownProtocols.def

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
#define PROTOCOL_WITH_NAME(Id, Name)
2525
#endif
2626

27-
/// \def EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name)
27+
/// \def EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup)
28+
/// \param typeName supplies the name used for type lookup,
29+
/// \param performLocalLookup specifies whether to first look in the local context.
2830
#ifndef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
29-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name) \
31+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup) \
3032
PROTOCOL_WITH_NAME(Id, Name)
3133
#endif
3234

@@ -41,10 +43,17 @@
4143

4244
#define PROTOCOL(name) PROTOCOL_WITH_NAME(name, #name)
4345
#define PROTOCOL_(name) PROTOCOL_WITH_NAME(name, "_" #name)
44-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL(name) \
45-
EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, #name)
46-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_(name) \
47-
EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, "_" #name)
46+
47+
/// \param typeName supplies the name used for type lookup,
48+
/// \param performLocalLookup specifies whether to first look in the local context.
49+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL(name, typeName, performLocalLookup) \
50+
EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, #name, typeName, performLocalLookup)
51+
52+
/// \param typeName supplies the name used for type lookup,
53+
/// \param performLocalLookup specifies whether to first look in the local context.
54+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_(name, typeName, performLocalLookup) \
55+
EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, "_" #name, typeName, performLocalLookup)
56+
4857
#define BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_(name) \
4958
BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, "_" #name)
5059

@@ -72,20 +81,20 @@ PROTOCOL_(DestructorSafeContainer)
7281

7382
PROTOCOL(StringInterpolationProtocol)
7483

75-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral)
76-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByBooleanLiteral)
77-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByDictionaryLiteral)
78-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByExtendedGraphemeClusterLiteral)
79-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByFloatLiteral)
80-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByIntegerLiteral)
81-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByStringInterpolation)
82-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByStringLiteral)
83-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByNilLiteral)
84-
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByUnicodeScalarLiteral)
85-
86-
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByColorLiteral)
87-
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByImageLiteral)
88-
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByFileReferenceLiteral)
84+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral, "Array", false)
85+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByBooleanLiteral, "BooleanLiteralType", true)
86+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByDictionaryLiteral, "Dictionary", false)
87+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByExtendedGraphemeClusterLiteral, "ExtendedGraphemeClusterType", true)
88+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByFloatLiteral, "FloatLiteralType", true)
89+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByIntegerLiteral, "IntegerLiteralType", true)
90+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByStringInterpolation, "StringLiteralType", true)
91+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByStringLiteral, "StringLiteralType", true)
92+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByNilLiteral, nullptr, false)
93+
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByUnicodeScalarLiteral, "UnicodeScalarType", true)
94+
95+
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByColorLiteral, "_ColorLiteralType", true)
96+
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByImageLiteral, "_ImageLiteralType", true)
97+
EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByFileReferenceLiteral, "_FileReferenceLiteralType", true)
8998

9099
BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByBuiltinBooleanLiteral)
91100
BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByBuiltinExtendedGraphemeClusterLiteral)

include/swift/AST/TypeCheckRequests.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,48 @@ class USRGenerationRequest :
322322
bool isCached() const { return true; }
323323
};
324324

325+
void simple_display(llvm::raw_ostream &out, const KnownProtocolKind);
326+
class TypeChecker;
327+
328+
// Find the type in the cache or look it up
329+
class DefaultTypeRequest
330+
: public SimpleRequest<DefaultTypeRequest, CacheKind::SeparatelyCached,
331+
Type, KnownProtocolKind, const DeclContext *> {
332+
public:
333+
using SimpleRequest::SimpleRequest;
334+
335+
private:
336+
friend SimpleRequest;
337+
338+
// Evaluation.
339+
llvm::Expected<Type> evaluate(Evaluator &eval, KnownProtocolKind,
340+
const DeclContext *) const;
341+
342+
public:
343+
// Cycle handling
344+
void diagnoseCycle(DiagnosticEngine &diags) const;
345+
void noteCycleStep(DiagnosticEngine &diags) const;
346+
347+
// Caching
348+
bool isCached() const { return true; }
349+
Optional<Type> getCachedResult() const;
350+
void cacheResult(Type value) const;
351+
352+
private:
353+
KnownProtocolKind getKnownProtocolKind() const {
354+
return std::get<0>(getStorage());
355+
}
356+
const DeclContext *getDeclContext() const {
357+
return std::get<1>(getStorage());
358+
}
359+
360+
static const char *getTypeName(KnownProtocolKind);
361+
static bool getPerformLocalLookup(KnownProtocolKind);
362+
TypeChecker &getTypeChecker() const;
363+
SourceFile *getSourceFile() const;
364+
Type &getCache() const;
365+
};
366+
325367
/// The zone number for the type checker.
326368
#define SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE 10
327369

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ SWIFT_TYPEID(IsObjCRequest)
2222
SWIFT_TYPEID(IsDynamicRequest)
2323
SWIFT_TYPEID(RequirementRequest)
2424
SWIFT_TYPEID(USRGenerationRequest)
25+
SWIFT_TYPEID(DefaultTypeRequest)

lib/AST/ASTContext.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
291291
ProtocolConformanceRef>
292292
DefaultAssociatedConformanceWitnesses;
293293

294+
/// Caches of default types for DefaultTypeRequest.
295+
/// Used to be instance variables in the TypeChecker.
296+
/// There is a logically separate cache for each SourceFile and
297+
/// KnownProtocolKind.
298+
llvm::DenseMap<SourceFile *, std::array<Type, NumKnownProtocols>>
299+
DefaultTypeRequestCaches;
300+
294301
/// Structure that captures data that is segregated into different
295302
/// arenas.
296303
struct Arena {
@@ -5075,4 +5082,7 @@ LayoutConstraint LayoutConstraint::getLayoutConstraint(LayoutConstraintKind Kind
50755082
return LayoutConstraint(New);
50765083
}
50775084

5078-
5085+
Type &ASTContext::getDefaultTypeRequestCache(SourceFile *SF,
5086+
KnownProtocolKind kind) {
5087+
return getImpl().DefaultTypeRequestCaches[SF][size_t(kind)];
5088+
}

lib/AST/DeclContext.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,12 @@ static unsigned getLineNumber(DCType *DC) {
525525
return ctx.SourceMgr.getLineAndColumn(loc).first;
526526
}
527527

528-
unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
528+
unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
529+
const bool onlyAPartialLine) const {
529530
unsigned Depth = 0;
530-
if (auto *P = getParent())
531-
Depth = P->printContext(OS, indent);
531+
if (!onlyAPartialLine)
532+
if (auto *P = getParent())
533+
Depth = P->printContext(OS, indent);
532534

533535
const char *Kind;
534536
switch (getContextKind()) {
@@ -658,7 +660,8 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
658660
}
659661
}
660662

661-
OS << "\n";
663+
if (!onlyAPartialLine)
664+
OS << "\n";
662665
return Depth + 1;
663666
}
664667

lib/AST/TypeCheckRequests.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/AST/ASTContext.h"
1414
#include "swift/AST/Decl.h"
1515
#include "swift/AST/DiagnosticsCommon.h"
16+
#include "swift/AST/Module.h"
1617
#include "swift/AST/TypeLoc.h"
1718
#include "swift/AST/TypeRepr.h"
1819
#include "swift/AST/Types.h"
@@ -429,3 +430,69 @@ void USRGenerationRequest::noteCycleStep(DiagnosticEngine &diags) const {
429430
auto &d = std::get<0>(storage);
430431
diags.diagnose(d, diag::circular_reference);
431432
}
433+
434+
//----------------------------------------------------------------------------//
435+
// DefaultTypeRequest.
436+
//----------------------------------------------------------------------------//
437+
438+
void swift::simple_display(llvm::raw_ostream &out,
439+
const KnownProtocolKind kind) {
440+
out << getProtocolName(kind);
441+
}
442+
443+
void DefaultTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
444+
diags.diagnose(SourceLoc(), diag::circular_reference);
445+
}
446+
447+
void DefaultTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
448+
diags.diagnose(SourceLoc(), diag::circular_reference_through);
449+
}
450+
451+
//----------------------------------------------------------------------------//
452+
// DefaultTypeRequest caching.
453+
//----------------------------------------------------------------------------//
454+
455+
SourceFile *DefaultTypeRequest::getSourceFile() const {
456+
return getDeclContext()->getParentSourceFile();
457+
}
458+
459+
Type &DefaultTypeRequest::getCache() const {
460+
return getDeclContext()->getASTContext().getDefaultTypeRequestCache(
461+
getSourceFile(), getKnownProtocolKind());
462+
}
463+
464+
Optional<Type> DefaultTypeRequest::getCachedResult() const {
465+
auto const &cachedType = getCache();
466+
return cachedType ? Optional<Type>(cachedType) : None;
467+
}
468+
469+
void DefaultTypeRequest::cacheResult(Type value) const { getCache() = value; }
470+
471+
const char *
472+
DefaultTypeRequest::getTypeName(const KnownProtocolKind knownProtocolKind) {
473+
switch (knownProtocolKind) {
474+
475+
// clang-format off
476+
# define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup) \
477+
case KnownProtocolKind::Id: return typeName;
478+
# include "swift/AST/KnownProtocols.def"
479+
# undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
480+
//clang-format on
481+
482+
default: return nullptr;
483+
}
484+
}
485+
486+
bool DefaultTypeRequest::getPerformLocalLookup(const KnownProtocolKind knownProtocolKind) {
487+
switch (knownProtocolKind) {
488+
489+
// clang-format off
490+
# define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, typeName, performLocalLookup) \
491+
case KnownProtocolKind::Id: return performLocalLookup;
492+
# include "swift/AST/KnownProtocols.def"
493+
# undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
494+
//clang-format on
495+
496+
default: return false;
497+
}
498+
}

lib/Sema/CSDiag.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,9 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
21132113
// Only try to insert a converting construction if the protocol is a
21142114
// literal protocol and not some other known protocol.
21152115
switch (kind) {
2116-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _) \
2117-
case KnownProtocolKind::name: break;
2116+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _, __, ___) \
2117+
case KnownProtocolKind::name: \
2118+
break;
21182119
#define PROTOCOL_WITH_NAME(name, _) \
21192120
case KnownProtocolKind::name: return false;
21202121
#include "swift/AST/KnownProtocols.def"
@@ -2136,8 +2137,9 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
21362137
// Only try to insert a converting construction if the protocol is a
21372138
// literal protocol and not some other known protocol.
21382139
switch (kind) {
2139-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _) \
2140-
case KnownProtocolKind::name: break;
2140+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _, __, ___) \
2141+
case KnownProtocolKind::name: \
2142+
break;
21412143
#define PROTOCOL_WITH_NAME(name, _) \
21422144
case KnownProtocolKind::name: return false;
21432145
#include "swift/AST/KnownProtocols.def"

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
304304
switch (kind) {
305305
#define PROTOCOL_WITH_NAME(Id, Name) \
306306
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
307-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name)
307+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
308308
#include "swift/AST/KnownProtocols.def"
309309

310310
case KnownProtocolKind::ExpressibleByArrayLiteral: index = 0; break;
@@ -334,7 +334,7 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
334334
switch (kind) {
335335
#define PROTOCOL_WITH_NAME(Id, Name) \
336336
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
337-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name)
337+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
338338
#include "swift/AST/KnownProtocols.def"
339339

340340
case KnownProtocolKind::ExpressibleByArrayLiteral:

0 commit comments

Comments
 (0)