Skip to content

Commit ecb58a1

Browse files
authored
Merge pull request swiftlang#30439 from CodaFi/swift-is-an-untyped-languge-now-i-dont-make-the-rules
[NFC] Break TypeChecker Down Into a Namespace
2 parents 91a5ceb + 44ec753 commit ecb58a1

28 files changed

+781
-868
lines changed

include/swift/AST/ASTContext.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ namespace swift {
105105
class SourceManager;
106106
class ValueDecl;
107107
class DiagnosticEngine;
108-
class TypeChecker;
109108
class TypeCheckerDebugConsumer;
110109
struct RawComment;
111110
class DocComment;
@@ -333,6 +332,32 @@ class ASTContext final {
333332
llvm::BumpPtrAllocator &
334333
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;
335334

335+
private:
336+
bool SemanticQueriesEnabled = false;
337+
338+
public:
339+
/// Returns \c true if legacy semantic AST queries are enabled.
340+
///
341+
/// The request evaluator generally subsumes the use of this bit. However,
342+
/// there are clients - mostly SourceKit - that rely on the fact that this bit
343+
/// being \c false causes some property wrapper requests to return null
344+
/// sentinel values. These clients should be migrated off of this interface
345+
/// to syntactic requests as soon as possible.
346+
///
347+
/// rdar://60516325
348+
bool areLegacySemanticQueriesEnabled() const {
349+
return SemanticQueriesEnabled;
350+
}
351+
352+
/// Enable "semantic queries".
353+
///
354+
/// Setting this bit tells property wrapper requests to return a semantic
355+
/// value. It does not otherwise affect compiler behavior and should be
356+
/// removed as soon as possible.
357+
void setLegacySemanticQueriesEnabled() {
358+
SemanticQueriesEnabled = true;
359+
}
360+
336361
public:
337362
/// Allocate - Allocate memory from the ASTContext bump pointer.
338363
void *Allocate(unsigned long bytes, unsigned alignment,
@@ -442,18 +467,7 @@ class ASTContext final {
442467
/// Set a new stats reporter.
443468
void setStatsReporter(UnifiedStatsReporter *stats);
444469

445-
private:
446-
friend class TypeChecker;
447-
448-
void installGlobalTypeChecker(TypeChecker *TC);
449470
public:
450-
/// Returns if semantic AST queries are enabled. This generally means module
451-
/// loading and name lookup can take place.
452-
bool areSemanticQueriesEnabled() const;
453-
454-
/// Retrieve the global \c TypeChecker instance associated with this context.
455-
TypeChecker *getLegacyGlobalTypeChecker() const;
456-
457471
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
458472
/// specified string.
459473
Identifier getIdentifier(StringRef Str) const;

include/swift/AST/Decl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,7 +3597,6 @@ class EnumDecl final : public NominalTypeDecl {
35973597

35983598
friend class EnumRawValuesRequest;
35993599
friend class EnumRawTypeRequest;
3600-
friend class TypeChecker;
36013600

36023601
public:
36033602
EnumDecl(SourceLoc EnumLoc, Identifier Name, SourceLoc NameLoc,
@@ -3883,7 +3882,6 @@ class ClassDecl final : public NominalTypeDecl {
38833882
friend class EmittedMembersRequest;
38843883
friend class HasMissingDesignatedInitializersRequest;
38853884
friend class InheritsSuperclassInitializersRequest;
3886-
friend class TypeChecker;
38873885

38883886
public:
38893887
ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
@@ -4258,7 +4256,6 @@ class ProtocolDecl final : public NominalTypeDecl {
42584256
friend class ProtocolRequiresClassRequest;
42594257
friend class ExistentialConformsToSelfRequest;
42604258
friend class ExistentialTypeSupportedRequest;
4261-
friend class TypeChecker;
42624259

42634260
public:
42644261
ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc, SourceLoc NameLoc,

include/swift/AST/SourceFile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ class SourceFile final : public FileUnit {
267267
/// A set of synthesized declarations that need to be type checked.
268268
llvm::SmallVector<Decl *, 8> SynthesizedDecls;
269269

270+
/// The list of functions defined in this file whose bodies have yet to be
271+
/// typechecked. They must be held in this list instead of eagerly validated
272+
/// because their bodies may force us to perform semantic checks of arbitrary
273+
/// complexity, and we currently cannot handle those checks in isolation. E.g.
274+
/// we cannot, in general, perform witness matching on singular requirements
275+
/// unless the entire conformance has been evaluated.
276+
std::vector<AbstractFunctionDecl *> DelayedFunctions;
277+
270278
/// We might perform type checking on the same source file more than once,
271279
/// if its the main file or a REPL instance, so keep track of the last
272280
/// checked synthesized declaration to avoid duplicating work.

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ class MangleLocalTypeDeclRequest :
498498
};
499499

500500
void simple_display(llvm::raw_ostream &out, const KnownProtocolKind);
501-
class TypeChecker;
502501

503502
// Find the type in the cache or look it up
504503
class DefaultTypeRequest

include/swift/Sema/IDETypeChecking.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ namespace swift {
4343
class SubscriptDecl;
4444
class TopLevelCodeDecl;
4545
class Type;
46-
class TypeChecker;
4746
class ValueDecl;
4847
struct PrintOptions;
4948

include/swift/Subsystems.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ namespace swift {
6666
class SyntaxParsingCache;
6767
class Token;
6868
class TopLevelContext;
69-
class TypeChecker;
7069
class TypeCheckerOptions;
7170
struct TypeLoc;
7271
class UnifiedStatsReporter;
@@ -141,12 +140,6 @@ namespace swift {
141140
/// lib/Sema/PCMacro.cpp for a description of the calls inserted.
142141
void performPCMacro(SourceFile &SF);
143142

144-
/// Creates a type checker instance on the given AST context, if it
145-
/// doesn't already have one.
146-
///
147-
/// \returns a reference to the type checker instance.
148-
TypeChecker &createTypeChecker(ASTContext &Ctx);
149-
150143
/// Bind all 'extension' visible from \p SF to the extended nominal.
151144
void bindExtensions(SourceFile &SF);
152145

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ struct ASTContext::Implementation {
155155
/// The set of cleanups to be called when the ASTContext is destroyed.
156156
std::vector<std::function<void(void)>> Cleanups;
157157

158-
/// A global type checker instance..
159-
TypeChecker *Checker = nullptr;
160-
161158
// FIXME: This is a StringMap rather than a StringSet because StringSet
162159
// doesn't allow passing in a pre-existing allocator.
163160
llvm::StringMap<Identifier::Aligner, llvm::BumpPtrAllocator&>
@@ -632,19 +629,6 @@ RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {
632629
return getImpl().TheSyntaxArena;
633630
}
634631

635-
bool ASTContext::areSemanticQueriesEnabled() const {
636-
return getLegacyGlobalTypeChecker() != nullptr;
637-
}
638-
639-
TypeChecker *ASTContext::getLegacyGlobalTypeChecker() const {
640-
return getImpl().Checker;
641-
}
642-
643-
void ASTContext::installGlobalTypeChecker(TypeChecker *TC) {
644-
assert(!getImpl().Checker);
645-
getImpl().Checker = TC;
646-
}
647-
648632
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
649633
/// specified string.
650634
Identifier ASTContext::getIdentifier(StringRef Str) const {

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,7 +2998,7 @@ bool ValueDecl::isRecursiveValidation() const {
29982998
Type ValueDecl::getInterfaceType() const {
29992999
auto &ctx = getASTContext();
30003000

3001-
assert(ctx.areSemanticQueriesEnabled());
3001+
assert(ctx.areLegacySemanticQueriesEnabled());
30023002

30033003
if (auto type =
30043004
evaluateOrDefault(ctx.evaluator,
@@ -5813,7 +5813,7 @@ StaticSpellingKind AbstractStorageDecl::getCorrectStaticSpelling() const {
58135813

58145814
llvm::TinyPtrVector<CustomAttr *> VarDecl::getAttachedPropertyWrappers() const {
58155815
auto &ctx = getASTContext();
5816-
if (!ctx.areSemanticQueriesEnabled()) {
5816+
if (!ctx.areLegacySemanticQueriesEnabled()) {
58175817
return { };
58185818
}
58195819

@@ -6786,7 +6786,7 @@ ObjCSelector
67866786
AbstractFunctionDecl::getObjCSelector(DeclName preferredName,
67876787
bool skipIsObjCResolution) const {
67886788
// FIXME: Forces computation of the Objective-C selector.
6789-
if (getASTContext().areSemanticQueriesEnabled() && !skipIsObjCResolution)
6789+
if (getASTContext().areLegacySemanticQueriesEnabled() && !skipIsObjCResolution)
67906790
(void)isObjC();
67916791

67926792
// If there is an @objc attribute with a name, use that name.

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
16221622
tracker->addUsedMember({current, member.getBaseName()},isLookupCascading);
16231623

16241624
// Make sure we've resolved property wrappers, if we need them.
1625-
if (ctx.areSemanticQueriesEnabled()) {
1625+
if (ctx.areLegacySemanticQueriesEnabled()) {
16261626
installPropertyWrapperMembersIfNeeded(current, member);
16271627
}
16281628

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
240240
if (setUpModuleLoaders())
241241
return true;
242242

243-
createTypeChecker(*Context);
243+
Context->setLegacySemanticQueriesEnabled();
244244
return false;
245245
}
246246

0 commit comments

Comments
 (0)