diff --git a/include/swift/AST/ASTTypeIDZone.def b/include/swift/AST/ASTTypeIDZone.def index 7add2900c8d3a..5a44076e1cddf 100644 --- a/include/swift/AST/ASTTypeIDZone.def +++ b/include/swift/AST/ASTTypeIDZone.def @@ -48,6 +48,7 @@ SWIFT_TYPEID_NAMED(CodeCompletionCallbacksFactory *, CodeCompletionCallbacksFactory) SWIFT_TYPEID_NAMED(ConstructorDecl *, ConstructorDecl) SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr) +SWIFT_TYPEID_NAMED(ConstValueTypeInfo *, ConstValueTypeInfo) SWIFT_TYPEID_NAMED(Decl *, Decl) SWIFT_TYPEID_NAMED(EnumDecl *, EnumDecl) SWIFT_TYPEID_NAMED(FuncDecl *, FuncDecl) diff --git a/include/swift/AST/ASTTypeIDs.h b/include/swift/AST/ASTTypeIDs.h index 0f19111c2582d..c93b9b9c97638 100644 --- a/include/swift/AST/ASTTypeIDs.h +++ b/include/swift/AST/ASTTypeIDs.h @@ -82,6 +82,7 @@ enum class ImplicitMemberAction : uint8_t; struct FingerprintAndMembers; class Identifier; class BodyAndFingerprint; +struct ConstValueTypeInfo; // Define the AST type zone (zone 1) #define SWIFT_TYPEID_ZONE AST diff --git a/include/swift/AST/ConstTypeInfo.h b/include/swift/AST/ConstTypeInfo.h index 2f66c145afab0..734eaa6784ce3 100644 --- a/include/swift/AST/ConstTypeInfo.h +++ b/include/swift/AST/ConstTypeInfo.h @@ -21,7 +21,6 @@ namespace swift { class NominalTypeDecl; class VarDecl; class Type; -} // namespace swift /// Representation of a compile-time-known value, for example /// in a type property initializer expression @@ -43,7 +42,7 @@ class CompileTimeValue { class RawLiteralValue : public CompileTimeValue { public: RawLiteralValue(std::string Value) - : CompileTimeValue(ValueKind::RawLiteral), Value(Value) {} + : CompileTimeValue(ValueKind::RawLiteral), Value(Value) {} std::string getValue() const { return Value; } @@ -70,8 +69,8 @@ struct FunctionParameter { class InitCallValue : public CompileTimeValue { public: InitCallValue(std::string Name, std::vector Parameters) - : CompileTimeValue(ValueKind::InitCall), Name(Name), - Parameters(Parameters) {} + : CompileTimeValue(ValueKind::InitCall), Name(Name), + Parameters(Parameters) {} static bool classof(const CompileTimeValue *T) { return T->getKind() == ValueKind::InitCall; @@ -127,5 +126,5 @@ struct ConstValueTypeInfo { swift::NominalTypeDecl *TypeDecl; std::vector Properties; }; - +} // namespace swift #endif diff --git a/include/swift/AST/TypeCheckRequests.h b/include/swift/AST/TypeCheckRequests.h index aeafcbaf85807..7bf75495d33fe 100644 --- a/include/swift/AST/TypeCheckRequests.h +++ b/include/swift/AST/TypeCheckRequests.h @@ -18,7 +18,6 @@ #include "swift/AST/ActorIsolation.h" #include "swift/AST/AnyFunctionRef.h" -#include "swift/AST/ConstTypeInfo.h" #include "swift/AST/ASTNode.h" #include "swift/AST/ASTTypeIDs.h" #include "swift/AST/Effects.h" @@ -714,26 +713,6 @@ class PropertyWrapperTypeInfoRequest bool isCached() const { return true; } }; -/// Retrieve information about compile-time-known -class ConstantValueInfoRequest - : public SimpleRequest { -public: - using SimpleRequest::SimpleRequest; - -private: - friend SimpleRequest; - - // Evaluation. - ConstValueTypeInfo - evaluate(Evaluator &eval, NominalTypeDecl *nominal) const; - -public: - // Caching - bool isCached() const { return true; } -}; - /// Request the nominal type declaration to which the given custom attribute /// refers. class AttachedPropertyWrappersRequest : diff --git a/include/swift/AST/TypeCheckerTypeIDZone.def b/include/swift/AST/TypeCheckerTypeIDZone.def index fe64147b0f72e..a02591d45f4eb 100644 --- a/include/swift/AST/TypeCheckerTypeIDZone.def +++ b/include/swift/AST/TypeCheckerTypeIDZone.def @@ -260,9 +260,6 @@ SWIFT_REQUEST(TypeChecker, PropertyWrapperMutabilityRequest, SWIFT_REQUEST(TypeChecker, PropertyWrapperTypeInfoRequest, PropertyWrapperTypeInfo(NominalTypeDecl *), Cached, NoLocationInfo) -SWIFT_REQUEST(TypeChecker, ConstantValueInfoRequest, - ConstValueTypeInfo(NominalTypeDecl *), Cached, - NoLocationInfo) SWIFT_REQUEST(TypeChecker, ProtocolRequiresClassRequest, bool(ProtocolDecl *), SeparatelyCached, NoLocationInfo) SWIFT_REQUEST(TypeChecker, PrimaryAssociatedTypesRequest, diff --git a/include/swift/Basic/Statistics.def b/include/swift/Basic/Statistics.def index 4f7d9d2ceb734..b0f272732acf3 100644 --- a/include/swift/Basic/Statistics.def +++ b/include/swift/Basic/Statistics.def @@ -261,6 +261,7 @@ FRONTEND_STATISTIC(Sema, NumUnloadedLazyIterableDeclContexts) #include "swift/Sema/IDETypeCheckingRequestIDZone.def" #include "swift/IDE/IDERequestIDZone.def" #include "swift/ClangImporter/ClangImporterTypeIDZone.def" +#include "swift/ConstExtract/ConstExtractTypeIDZone.def" #undef SWIFT_REQUEST #define SWIFT_REQUEST(ZONE, NAME, Sig, Caching, LocOptions) FRONTEND_STATISTIC(SILGen, NAME) diff --git a/include/swift/Basic/TypeIDZones.def b/include/swift/Basic/TypeIDZones.def index 08ee2c883e4e0..e1e1ddf458a26 100644 --- a/include/swift/Basic/TypeIDZones.def +++ b/include/swift/Basic/TypeIDZones.def @@ -35,5 +35,7 @@ SWIFT_TYPEID_ZONE(IDE, 137) SWIFT_TYPEID_ZONE(ClangImporter, 139) +SWIFT_TYPEID_ZONE(ConstExtract, 140) + // N.B. This is not a formal zone and exists solely to support the unit tests. SWIFT_TYPEID_ZONE(ArithmeticEvaluator, 255) diff --git a/include/swift/ConstExtract/ConstExtractRequests.h b/include/swift/ConstExtract/ConstExtractRequests.h new file mode 100644 index 0000000000000..6550722327bed --- /dev/null +++ b/include/swift/ConstExtract/ConstExtractRequests.h @@ -0,0 +1,79 @@ +//===------- ConstExtractRequests.h - Extraction Requests ------*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// +// +// This file defines const-extraction requests. +// +//===----------------------------------------------------------------------===// +#ifndef SWIFT_CONST_EXTRACT_REQUESTS_H +#define SWIFT_CONST_EXTRACT_REQUESTS_H + +#include "swift/AST/SimpleRequest.h" +#include "swift/AST/ASTTypeIDs.h" +#include "swift/AST/ConstTypeInfo.h" +#include "swift/AST/EvaluatorDependencies.h" +#include "swift/AST/FileUnit.h" +#include "swift/AST/Identifier.h" +#include "swift/AST/NameLookup.h" +#include "swift/Basic/Statistic.h" +#include "llvm/ADT/Hashing.h" +#include "llvm/ADT/TinyPtrVector.h" + +namespace swift { + +class Decl; +class DeclName; +class EnumDecl; + +/// Retrieve information about compile-time-known values +class ConstantValueInfoRequest + : public SimpleRequest { +public: + using SimpleRequest::SimpleRequest; + +private: + friend SimpleRequest; + + // Evaluation. + ConstValueTypeInfo + evaluate(Evaluator &eval, NominalTypeDecl *nominal) const; + +public: + // Caching + bool isCached() const { return true; } +}; + +#define SWIFT_TYPEID_ZONE ConstExtract +#define SWIFT_TYPEID_HEADER "swift/ConstExtract/ConstExtractTypeIDZone.def" +#include "swift/Basic/DefineTypeIDZone.h" +#undef SWIFT_TYPEID_ZONE +#undef SWIFT_TYPEID_HEADER + +// Set up reporting of evaluated requests. +template +void reportEvaluatedRequest(UnifiedStatsReporter &stats, + const Request &request); + +#define SWIFT_REQUEST(Zone, RequestType, Sig, Caching, LocOptions) \ + template <> \ + inline void reportEvaluatedRequest(UnifiedStatsReporter &stats, \ + const RequestType &request) { \ + ++stats.getFrontendCounters().RequestType; \ + } +#include "swift/ConstExtract/ConstExtractTypeIDZone.def" +#undef SWIFT_REQUEST + +} // end namespace swift + +#endif // SWIFT_CONST_EXTRACT_REQUESTS_H + diff --git a/include/swift/ConstExtract/ConstExtractTypeIDZone.def b/include/swift/ConstExtract/ConstExtractTypeIDZone.def new file mode 100644 index 0000000000000..92691ed3143eb --- /dev/null +++ b/include/swift/ConstExtract/ConstExtractTypeIDZone.def @@ -0,0 +1,20 @@ +//===--- ConstExtractTypeIDZone.def ----------------------------*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// +// +// This definition file describes the types in the const extract +// TypeID zone, for use with the TypeID template. +// +//===----------------------------------------------------------------------===// + +SWIFT_REQUEST(ConstExtract, ConstantValueInfoRequest, + ConstValueTypeInfo(NominalTypeDecl *), Cached, + NoLocationInfo) diff --git a/include/swift/Subsystems.h b/include/swift/Subsystems.h index f5ceb5f4f05ba..12fee6c0ad0fa 100644 --- a/include/swift/Subsystems.h +++ b/include/swift/Subsystems.h @@ -386,6 +386,9 @@ namespace swift { /// after forming the ASTContext. void registerClangImporterRequestFunctions(Evaluator &evaluator); + /// Register constant value extraction request functons with the evaluator. + void registerConstExtractRequestFunctions(Evaluator &evaluator); + /// Register SILOptimizer passes necessary for IRGen. void registerIRGenSILTransforms(ASTContext &ctx); diff --git a/lib/ConstExtract/ConstExtract.cpp b/lib/ConstExtract/ConstExtract.cpp index a9d7549dbaf03..a5391910a97f3 100644 --- a/lib/ConstExtract/ConstExtract.cpp +++ b/lib/ConstExtract/ConstExtract.cpp @@ -10,7 +10,9 @@ // //===----------------------------------------------------------------------===// +#include "swift/Basic/TypeID.h" #include "swift/ConstExtract/ConstExtract.h" +#include "swift/ConstExtract/ConstExtractRequests.h" #include "swift/AST/ASTContext.h" #include "swift/AST/ASTWalker.h" #include "swift/AST/Decl.h" @@ -23,6 +25,7 @@ #include "llvm/Support/JSON.h" #include "llvm/Support/YAMLParser.h" #include "llvm/Support/YAMLTraits.h" +#include "swift/Subsystems.h" #include #include @@ -239,3 +242,22 @@ bool writeAsJSONToFile(const std::vector &ConstValueInfos, } } // namespace swift + +#define SWIFT_TYPEID_ZONE ConstExtract +#define SWIFT_TYPEID_HEADER "swift/ConstExtract/ConstExtractTypeIDZone.def" +#include "swift/Basic/ImplementTypeIDZone.h" +#undef SWIFT_TYPEID_ZONE +#undef SWIFT_TYPEID_HEADER + +// Define request evaluation functions for each of the name lookup requests. +static AbstractRequestFunction *constExtractRequestFunctions[] = { +#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \ + reinterpret_cast(&Name::evaluateRequest), +#include "swift/ConstExtract/ConstExtractTypeIDZone.def" +#undef SWIFT_REQUEST +}; + +void swift::registerConstExtractRequestFunctions(Evaluator &evaluator) { + evaluator.registerRequestFunctions(Zone::ConstExtract, + constExtractRequestFunctions); +} diff --git a/lib/Frontend/Frontend.cpp b/lib/Frontend/Frontend.cpp index 205ec027b0b8a..f858d84a0ab96 100644 --- a/lib/Frontend/Frontend.cpp +++ b/lib/Frontend/Frontend.cpp @@ -239,6 +239,7 @@ bool CompilerInstance::setUpASTContextIfNeeded() { registerParseRequestFunctions(Context->evaluator); registerTypeCheckerRequestFunctions(Context->evaluator); registerClangImporterRequestFunctions(Context->evaluator); + registerConstExtractRequestFunctions(Context->evaluator); registerSILGenRequestFunctions(Context->evaluator); registerSILOptimizerRequestFunctions(Context->evaluator); registerTBDGenRequestFunctions(Context->evaluator); diff --git a/lib/IDETool/CompletionInstance.cpp b/lib/IDETool/CompletionInstance.cpp index 13591aeb05951..b8fb8bb00655d 100644 --- a/lib/IDETool/CompletionInstance.cpp +++ b/lib/IDETool/CompletionInstance.cpp @@ -247,6 +247,7 @@ bool CompletionInstance::performCachedOperationIfPossible( registerIDERequestFunctions(tmpCtx->evaluator); registerTypeCheckerRequestFunctions(tmpCtx->evaluator); registerClangImporterRequestFunctions(tmpCtx->evaluator); + registerConstExtractRequestFunctions(tmpCtx->evaluator); registerSILGenRequestFunctions(tmpCtx->evaluator); ModuleDecl *tmpM = ModuleDecl::create(Identifier(), *tmpCtx); SourceFile *tmpSF = new (*tmpCtx)