Skip to content

Commit 0fc80dc

Browse files
CodaFiDougGregor
authored andcommitted
Refactor Macro Context Lookup Into a Request
1 parent 394d064 commit 0fc80dc

File tree

7 files changed

+84
-93
lines changed

7 files changed

+84
-93
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ namespace swift {
124124
class DiagnosticEngine;
125125
struct RawComment;
126126
class DocComment;
127-
class StructDecl;
128127
class SILBoxType;
129128
class SILTransform;
130129
class TypeAliasDecl;
@@ -1436,14 +1435,6 @@ class ASTContext final {
14361435
/// The declared interface type of Builtin.TheTupleType.
14371436
BuiltinTupleType *getBuiltinTupleType();
14381437

1439-
/// Look up a macro's evaluation context by name.
1440-
///
1441-
/// If no such macro or evaluation context is found under this name, this
1442-
/// method returns \c nullptr.
1443-
StructDecl *getOrCreateASTGenMacroContext(
1444-
StringRef macroName,
1445-
llvm::function_ref<StructDecl *(StringRef)> createMacro);
1446-
14471438
private:
14481439
friend Decl;
14491440

include/swift/AST/TypeCheckRequests.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,40 @@ class SynthesizeHasSymbolQueryRequest
37543754
bool isCached() const { return true; }
37553755
};
37563756

3757+
3758+
/// Retrieves the evaluation context of a macro with the given name.
3759+
///
3760+
/// The macro evaluation context is a user-defined generic signature and return
3761+
/// type that serves as the "interface type" of references to the macro. The
3762+
/// current implementation takes those pieces of syntax from the macro itself,
3763+
/// then inserts them into a Swift struct that looks like
3764+
///
3765+
/// \code
3766+
/// struct __MacroEvaluationContext\(macro.genericSignature) {
3767+
/// typealias SignatureType = \(macro.signature)
3768+
/// }
3769+
/// \endcode
3770+
///
3771+
/// So that we can use all of Swift's native name lookup and type resolution
3772+
/// facilities to map the parsed signature type back into a semantic \c Type
3773+
/// AST and a set of requiremnets.
3774+
class MacroContextRequest
3775+
: public SimpleRequest<MacroContextRequest,
3776+
StructDecl *(std::string, ModuleDecl *),
3777+
RequestFlags::Cached> {
3778+
public:
3779+
using SimpleRequest::SimpleRequest;
3780+
3781+
private:
3782+
friend SimpleRequest;
3783+
3784+
StructDecl *evaluate(Evaluator &evaluator,
3785+
std::string macroName, ModuleDecl *mod) const;
3786+
3787+
public:
3788+
bool isCached() const { return true; }
3789+
};
3790+
37573791
void simple_display(llvm::raw_ostream &out, ASTNode node);
37583792
void simple_display(llvm::raw_ostream &out, Type value);
37593793
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,6 @@ SWIFT_REQUEST(TypeChecker, GetTypeWrapperInitializer,
443443
SWIFT_REQUEST(TypeChecker, SynthesizeHasSymbolQueryRequest,
444444
FuncDecl *(ValueDecl *),
445445
Cached, NoLocationInfo)
446+
SWIFT_REQUEST(TypeChecker, MacroContextRequest,
447+
StructDecl *(std::string, ModuleDecl *),
448+
Cached, NoLocationInfo)

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,6 @@ struct ASTContext::Implementation {
394394
/// is populated if the body is reparsed from other source buffers.
395395
llvm::DenseMap<const AbstractFunctionDecl *, SourceRange> OriginalBodySourceRanges;
396396

397-
/// A mapping of all the registered ASTGen macros keyed by name.
398-
llvm::StringMap<StructDecl *> ASTGenMacros;
399-
400397
/// Structure that captures data that is segregated into different
401398
/// arenas.
402399
struct Arena {
@@ -6035,17 +6032,3 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
60356032

60366033
return result;
60376034
}
6038-
6039-
StructDecl *ASTContext::getOrCreateASTGenMacroContext(
6040-
StringRef macroName,
6041-
llvm::function_ref<StructDecl *(StringRef)> createMacro) {
6042-
auto &impl = getImpl();
6043-
auto found = impl.ASTGenMacros.find(macroName);
6044-
if (found != impl.ASTGenMacros.end()) {
6045-
return found->second;
6046-
} else {
6047-
auto macroAndContext = createMacro(macroName);
6048-
impl.ASTGenMacros.insert({macroName, macroAndContext});
6049-
return macroAndContext;
6050-
}
6051-
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,8 @@ ConstraintSystem::getTypeOfMemberReference(
24742474
#if SWIFT_SWIFT_PARSER
24752475
Type ConstraintSystem::getTypeOfMacroReference(StringRef macroName,
24762476
Expr *anchor) {
2477-
auto macroCtx = swift::macro_context::lookup(macroName, DC);
2477+
auto req = MacroContextRequest{macroName.str(), DC->getParentModule()};
2478+
auto *macroCtx = evaluateOrDefault(getASTContext().evaluator, req, nullptr);
24782479
if (!macroCtx)
24792480
return Type();
24802481

lib/Sema/TypeCheckMacros.cpp

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTContext.h"
2020
#include "swift/AST/Expr.h"
2121
#include "swift/AST/SourceFile.h"
22+
#include "swift/AST/TypeCheckRequests.h"
2223
#include "swift/Basic/SourceManager.h"
2324
#include "swift/Parse/Lexer.h"
2425
#include "swift/Parse/Parser.h"
@@ -42,52 +43,52 @@ extern "C" void
4243
swift_ASTGen_getMacroTypeSignature(void *macro, const char **genericSignature,
4344
ptrdiff_t *genericSignatureLength);
4445

46+
StructDecl *MacroContextRequest::evaluate(Evaluator &evaluator,
47+
std::string macroName,
48+
ModuleDecl *mod) const {
4549
#if SWIFT_SWIFT_PARSER
50+
auto &ctx = mod->getASTContext();
51+
auto *macro = swift_ASTGen_lookupMacro(macroName.c_str());
52+
if (!macro)
53+
return nullptr;
54+
55+
const char *evaluatedSource;
56+
ptrdiff_t evaluatedSourceLength;
57+
swift_ASTGen_getMacroTypeSignature(macro, &evaluatedSource,
58+
&evaluatedSourceLength);
4659

47-
StructDecl *
48-
swift::macro_context::lookup(StringRef macroName, DeclContext *DC) {
49-
auto &ctx = DC->getASTContext();
50-
51-
return ctx.getOrCreateASTGenMacroContext(
52-
macroName, [&](StringRef macroName) -> StructDecl * {
53-
auto *macro = swift_ASTGen_lookupMacro(macroName.str().c_str());
54-
if (!macro)
55-
return nullptr;
56-
57-
const char *evaluatedSource;
58-
ptrdiff_t evaluatedSourceLength;
59-
swift_ASTGen_getMacroTypeSignature(macro, &evaluatedSource,
60-
&evaluatedSourceLength);
61-
62-
// Create a new source buffer with the contents of the macro's
63-
// signature.
64-
SourceManager &sourceMgr = ctx.SourceMgr;
65-
std::string bufferName;
66-
{
67-
llvm::raw_string_ostream out(bufferName);
68-
out << "Macro signature of #" << macroName;
69-
}
70-
auto macroBuffer = llvm::MemoryBuffer::getMemBuffer(
71-
StringRef(evaluatedSource, evaluatedSourceLength), bufferName);
72-
unsigned macroBufferID =
73-
sourceMgr.addNewSourceBuffer(std::move(macroBuffer));
74-
auto macroSourceFile = new (ctx) SourceFile(
75-
*DC->getParentModule(), SourceFileKind::Library, macroBufferID);
76-
// Make sure implicit imports are resolved in this file.
77-
performImportResolution(*macroSourceFile);
78-
79-
auto *start = sourceMgr.getLocForBufferStart(macroBufferID)
80-
.getOpaquePointerValue();
81-
void *context = nullptr;
82-
swift_ASTGen_getMacroEvaluationContext(
83-
(const void *)start, (void *)(DeclContext *)macroSourceFile,
84-
(void *)&ctx, macro, &context);
85-
86-
ctx.addCleanup([macro]() { swift_ASTGen_destroyMacro(macro); });
87-
return dyn_cast<StructDecl>((Decl *)context);
88-
});
60+
// Create a new source buffer with the contents of the macro's
61+
// signature.
62+
SourceManager &sourceMgr = ctx.SourceMgr;
63+
std::string bufferName;
64+
{
65+
llvm::raw_string_ostream out(bufferName);
66+
out << "Macro signature of #" << macroName;
67+
}
68+
auto macroBuffer = llvm::MemoryBuffer::getMemBuffer(
69+
StringRef(evaluatedSource, evaluatedSourceLength), bufferName);
70+
unsigned macroBufferID =
71+
sourceMgr.addNewSourceBuffer(std::move(macroBuffer));
72+
auto macroSourceFile = new (ctx) SourceFile(
73+
*mod, SourceFileKind::Library, macroBufferID);
74+
// Make sure implicit imports are resolved in this file.
75+
performImportResolution(*macroSourceFile);
76+
77+
auto *start = sourceMgr.getLocForBufferStart(macroBufferID)
78+
.getOpaquePointerValue();
79+
void *context = nullptr;
80+
swift_ASTGen_getMacroEvaluationContext(
81+
(const void *)start, (void *)(DeclContext *)macroSourceFile,
82+
(void *)&ctx, macro, &context);
83+
84+
ctx.addCleanup([macro]() { swift_ASTGen_destroyMacro(macro); });
85+
return dyn_cast<StructDecl>((Decl *)context);
86+
#else
87+
return nullptr;
88+
#endif // SWIFT_SWIFT_PARSER
8989
}
9090

91+
#if SWIFT_SWIFT_PARSER
9192
Expr *swift::expandMacroExpr(
9293
DeclContext *dc, Expr *expr, StringRef macroName, Type expandedType
9394
) {

lib/Sema/TypeCheckMacros.h

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,6 @@ class TypeRepr;
2626

2727
#if SWIFT_SWIFT_PARSER
2828

29-
namespace macro_context {
30-
/// Retrieves the evaluation context of a macro with the given name.
31-
///
32-
/// The macro evaluation context is a user-defined generic signature and return
33-
/// type that serves as the "interface type" of references to the macro. The
34-
/// current implementation takes those pieces of syntax from the macro itself,
35-
/// then inserts them into a Swift struct that looks like
36-
///
37-
/// \code
38-
/// struct __MacroEvaluationContext\(macro.genericSignature) {
39-
/// typealias SignatureType = \(macro.signature)
40-
/// }
41-
/// \endcode
42-
///
43-
/// So that we can use all of Swift's native name lookup and type resolution
44-
/// facilities to map the parsed signature type back into a semantic \c Type
45-
/// AST and a set of requiremnets.
46-
///
47-
/// \param macroName The name of the macro to look up.
48-
/// \param useDC The decl context of the use of this macro.
49-
StructDecl *lookup(StringRef macroName, DeclContext *useDC);
50-
}
51-
5229
/// Expands the given macro expression and type-check the result with
5330
/// the given expanded type.
5431
///
@@ -57,8 +34,9 @@ StructDecl *lookup(StringRef macroName, DeclContext *useDC);
5734
Expr *expandMacroExpr(
5835
DeclContext *dc, Expr *expr, StringRef macroName, Type expandedType);
5936

37+
#endif // SWIFT_SWIFT_PARSER
38+
6039
} // end namespace swift
6140

62-
#endif // SWIFT_SWIFT_PARSER
6341
#endif /* SWIFT_SEMA_TYPECHECKMACROS_H */
6442

0 commit comments

Comments
 (0)