Skip to content

Commit 9cc0e02

Browse files
committed
Switch back to parsing top-level MacroExpansionExpr.
Use a "substitute decl" in `MacroExpansionExpr` to represent top-level or local macro expansions that can be interpreted as a declaration macro or code item macro expansion.
1 parent fa7a5c1 commit 9cc0e02

31 files changed

+262
-234
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8473,9 +8473,9 @@ class MacroExpansionDecl : public Decl {
84738473
ArgumentList *getArgs() const { return ArgList; }
84748474
void setArgs(ArgumentList *args) { ArgList = args; }
84758475
ArrayRef<ASTNode> getRewritten() const;
8476+
ArrayRef<ASTNode> getFlattenedExpansion() const;
84768477
ConcreteDeclRef getMacroRef() const { return macroRef; }
84778478
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
8478-
MacroExpansionExpr *createExpr() const;
84798479

84808480
/// Returns a discriminator which determines this macro expansion's index
84818481
/// in the sequence of macro expansions within the current function.

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -832,13 +832,6 @@ class IterableDeclContext {
832832
/// The resulting list of members will be stable across translation units.
833833
ArrayRef<Decl *> getAllMembers() const;
834834

835-
/// Get all of the members within this context, including any
836-
/// implicitly-synthesized members and macro expansions. All macro expansions
837-
/// will be recursively flattened.
838-
///
839-
/// The resulting list of members will be stable across translation units.
840-
ArrayRef<Decl *> getExpandedAllMembers() const;
841-
842835
/// Retrieve the set of members in this context without loading any from the
843836
/// associated lazy loader; this should only be used as part of implementing
844837
/// abstractions on top of member loading, such as a name lookup table.

include/swift/AST/Expr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6156,6 +6156,7 @@ class MacroExpansionExpr final : public Expr {
61566156
ArgumentList *ArgList;
61576157
Expr *Rewritten;
61586158
MacroRoles Roles;
6159+
MacroExpansionDecl *SubstituteDecl;
61596160

61606161
/// The referenced macro.
61616162
ConcreteDeclRef macroRef;
@@ -6178,7 +6179,7 @@ class MacroExpansionExpr final : public Expr {
61786179
MacroName(macroName), MacroNameLoc(macroNameLoc),
61796180
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
61806181
GenericArgs(genericArgs),
6181-
Rewritten(nullptr), Roles(roles) {
6182+
Rewritten(nullptr), Roles(roles), SubstituteDecl(nullptr) {
61826183
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
61836184

61846185
// Macro expansions always have an argument list. If one is not provided, create
@@ -6235,6 +6236,10 @@ class MacroExpansionExpr final : public Expr {
62356236

62366237
SourceRange getSourceRange() const;
62376238

6239+
MacroExpansionDecl *createSubstituteDecl() const;
6240+
MacroExpansionDecl *getSubstituteDecl() const;
6241+
void setSubstituteDecl(MacroExpansionDecl *decl);
6242+
62386243
static bool classof(const Expr *E) {
62396244
return E->getKind() == ExprKind::MacroExpansion;
62406245
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,24 +1813,6 @@ class AllMembersRequest :
18131813
bool isCached() const { return true; }
18141814
};
18151815

1816-
class ExpandedAllMembersRequest :
1817-
public SimpleRequest<ExpandedAllMembersRequest,
1818-
ArrayRef<Decl *>(IterableDeclContext *),
1819-
RequestFlags::Cached> {
1820-
public:
1821-
using SimpleRequest::SimpleRequest;
1822-
1823-
private:
1824-
friend SimpleRequest;
1825-
1826-
// Evaluation.
1827-
ArrayRef<Decl *>
1828-
evaluate(Evaluator &evaluator, IterableDeclContext *idc) const;
1829-
1830-
public:
1831-
bool isCached() const { return true; }
1832-
};
1833-
18341816
class IsImplicitlyUnwrappedOptionalRequest :
18351817
public SimpleRequest<IsImplicitlyUnwrappedOptionalRequest,
18361818
bool(ValueDecl *),
@@ -3846,6 +3828,23 @@ class ExpandMacroExpansionDeclRequest
38463828
bool isCached() const { return true; }
38473829
};
38483830

3831+
class RecursivelyExpandMacroExpansionDeclRequest
3832+
: public SimpleRequest<RecursivelyExpandMacroExpansionDeclRequest,
3833+
ArrayRef<ASTNode>(MacroExpansionDecl *),
3834+
RequestFlags::Cached> {
3835+
public:
3836+
using SimpleRequest::SimpleRequest;
3837+
3838+
private:
3839+
friend SimpleRequest;
3840+
3841+
ArrayRef<ASTNode>
3842+
evaluate(Evaluator &evaluator, MacroExpansionDecl *med) const;
3843+
3844+
public:
3845+
bool isCached() const { return true; }
3846+
};
3847+
38493848
/// Expand all accessor macros attached to the given declaration.
38503849
///
38513850
/// Produces the set of macro expansion buffer IDs.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ SWIFT_REQUEST(TypeChecker, ABIMembersRequest,
8383
ArrayRef<Decl *>(IterableDeclContext *), Cached, NoLocationInfo)
8484
SWIFT_REQUEST(TypeChecker, AllMembersRequest,
8585
ArrayRef<Decl *>(IterableDeclContext *), Cached, NoLocationInfo)
86-
SWIFT_REQUEST(TypeChecker, ExpandedAllMembersRequest,
87-
ArrayRef<Decl *>(IterableDeclContext *), Cached, NoLocationInfo)
8886
SWIFT_REQUEST(TypeChecker, SpecializeAttrTargetDeclRequest,
8987
ValueDecl *(const ValueDecl *, SpecializeAttr *),
9088
Cached, NoLocationInfo)
@@ -433,6 +431,9 @@ SWIFT_REQUEST(TypeChecker, ExternalMacroDefinitionRequest,
433431
SWIFT_REQUEST(TypeChecker, ExpandMacroExpansionDeclRequest,
434432
ArrayRef<ASTNode> (MacroExpansionDecl *),
435433
Cached, NoLocationInfo)
434+
SWIFT_REQUEST(TypeChecker, RecursivelyExpandMacroExpansionDeclRequest,
435+
ArrayRef<ASTNode> (MacroExpansionDecl *),
436+
Cached, NoLocationInfo)
436437
SWIFT_REQUEST(TypeChecker, ExpandMemberAttributeMacros,
437438
ArrayRef<unsigned>(Decl *),
438439
Cached, NoLocationInfo)

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,6 @@ class Verifier : public ASTWalker {
357357
llvm_unreachable("not all cases handled!");
358358
}
359359

360-
// Don't eagerly expand macros.
361-
bool shouldWalkIntoMacroExpansions() override {
362-
return false;
363-
}
364-
365360
PreWalkAction walkToDeclPre(Decl *D) override {
366361
switch (D->getKind()) {
367362
#define DISPATCH(ID) return dispatchVisitPre(static_cast<ID##Decl*>(D))

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13191319
}
13201320
E->setRewritten(rewritten);
13211321
}
1322+
if (auto *decl = E->getSubstituteDecl())
1323+
if (doIt(decl))
1324+
return nullptr;
13221325
E->setArgs(args);
13231326
return E;
13241327
}

lib/AST/Decl.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,24 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
398398
}
399399
}
400400

401+
// For a macro expansion decl, its auxiliary decls are part of its expansion.
402+
if (auto *med = dyn_cast<MacroExpansionDecl>(this)) {
403+
for (auto node : med->getRewritten()) {
404+
if (auto *expr = node.dyn_cast<Expr *>()) {
405+
if (auto *mee = dyn_cast<MacroExpansionExpr>(expr)) {
406+
auto *med = mee->getSubstituteDecl();
407+
if (!med) {
408+
med = mee->createSubstituteDecl();
409+
mee->setSubstituteDecl(med);
410+
}
411+
callback(med);
412+
}
413+
} else if (auto *decl = node.dyn_cast<Decl *>()) {
414+
callback(decl);
415+
}
416+
}
417+
}
418+
401419
// FIXME: fold VarDecl::visitAuxiliaryDecls into this.
402420
}
403421

@@ -9968,13 +9986,6 @@ SourceRange MacroExpansionDecl::getSourceRange() const {
99689986
return SourceRange(PoundLoc, endLoc);
99699987
}
99709988

9971-
MacroExpansionExpr *MacroExpansionDecl::createExpr() const {
9972-
return new (getASTContext()) MacroExpansionExpr(
9973-
getDeclContext(), PoundLoc, MacroName, MacroNameLoc, LeftAngleLoc,
9974-
GenericArgs, RightAngleLoc, ArgList,
9975-
MacroRoles(MacroRole::Expression) | MacroRole::Declaration);
9976-
}
9977-
99789989
unsigned MacroExpansionDecl::getDiscriminator() const {
99799990
if (getRawDiscriminator() != InvalidDiscriminator)
99809991
return getRawDiscriminator();
@@ -9999,6 +10010,13 @@ ArrayRef<ASTNode> MacroExpansionDecl::getRewritten() const {
999910010
ExpandMacroExpansionDeclRequest{mutableThis}, {});
1000010011
}
1000110012

10013+
ArrayRef<ASTNode> MacroExpansionDecl::getFlattenedExpansion() const {
10014+
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
10015+
return evaluateOrDefault(
10016+
getASTContext().evaluator,
10017+
RecursivelyExpandMacroExpansionDeclRequest{mutableThis}, {});
10018+
}
10019+
1000210020
NominalTypeDecl *
1000310021
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
1000410022
auto &ctx = getASTContext();

lib/AST/DeclContext.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -889,14 +889,6 @@ ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
889889
ArrayRef<Decl *>());
890890
}
891891

892-
ArrayRef<Decl *> IterableDeclContext::getExpandedAllMembers() const {
893-
ASTContext &ctx = getASTContext();
894-
return evaluateOrDefault(
895-
ctx.evaluator,
896-
ExpandedAllMembersRequest{const_cast<IterableDeclContext *>(this)},
897-
ArrayRef<Decl *>());
898-
}
899-
900892
void IterableDeclContext::addMemberPreservingSourceOrder(Decl *member) {
901893
auto &SM = getASTContext().SourceMgr;
902894

lib/AST/Expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,23 @@ unsigned MacroExpansionExpr::getDiscriminator() const {
27092709
return getRawDiscriminator();
27102710
}
27112711

2712+
MacroExpansionDecl *MacroExpansionExpr::createSubstituteDecl() const {
2713+
auto dc = getDeclContext();
2714+
if (auto *tlcd = dyn_cast_or_null<TopLevelCodeDecl>(dc->getAsDecl()))
2715+
dc = tlcd->getDeclContext();
2716+
return new (DC->getASTContext()) MacroExpansionDecl(
2717+
getDeclContext(), SigilLoc, MacroName, MacroNameLoc, LeftAngleLoc,
2718+
GenericArgs, RightAngleLoc, ArgList);
2719+
}
2720+
2721+
MacroExpansionDecl *MacroExpansionExpr::getSubstituteDecl() const {
2722+
return SubstituteDecl;
2723+
}
2724+
2725+
void MacroExpansionExpr::setSubstituteDecl(MacroExpansionDecl *decl) {
2726+
SubstituteDecl = decl;
2727+
}
2728+
27122729
void swift::simple_display(llvm::raw_ostream &out, const ClosureExpr *CE) {
27132730
if (!CE) {
27142731
out << "(null)";

lib/AST/Module.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
263263

264264
else if (auto *MED = dyn_cast<MacroExpansionDecl>(D))
265265
DelayedMacroExpansions.push_back(MED);
266+
267+
// Top-level macro expansion expressions can actually produce declarations,
268+
// when they resolve to a substitute macro expansion decl.
269+
else if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
270+
for (auto node : TLCD->getBody()->getElements()) {
271+
if (auto *E = node.dyn_cast<Expr *>()) {
272+
if (auto *MEE = dyn_cast<MacroExpansionExpr>(E)) {
273+
auto *MED = MEE->getSubstituteDecl();
274+
if (!MED) {
275+
MED = MEE->createSubstituteDecl();
276+
MEE->setSubstituteDecl(MED);
277+
}
278+
DelayedMacroExpansions.push_back(MED);
279+
}
280+
}
281+
}
282+
}
266283
}
267284
}
268285

@@ -339,13 +356,18 @@ SourceLookupCache::SourceLookupCache(const ModuleDecl &M) {
339356
}
340357

341358
void SourceLookupCache::addDelayedMacroExpansions() {
342-
for (unsigned i = 0; i != DelayedMacroExpansions.size(); ++i) {
343-
auto *MED = DelayedMacroExpansions[i];
344-
for (auto node : MED->getRewritten())
359+
while (!DelayedMacroExpansions.empty()) {
360+
auto *MED = DelayedMacroExpansions.pop_back_val();
361+
for (auto node : MED->getRewritten()) {
345362
if (auto *decl = node.dyn_cast<Decl *>())
346363
addToUnqualifiedLookupCache(TinyPtrVector<Decl *>{decl}, false);
364+
else if (auto *expr = node.dyn_cast<Expr *>()) {
365+
if (auto *mee = dyn_cast<MacroExpansionExpr>(expr))
366+
if (auto *decl = mee->getSubstituteDecl())
367+
addToUnqualifiedLookupCache(TinyPtrVector<Decl *>{decl}, false);
368+
}
369+
}
347370
}
348-
DelayedMacroExpansions.clear();
349371
}
350372

351373
void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
@@ -3788,19 +3810,15 @@ void FileUnit::getExpandedTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
37883810
SmallVector<Decl *, 32> nonExpandedDecls;
37893811
nonExpandedDecls.reserve(results.capacity());
37903812
getTopLevelDecls(nonExpandedDecls);
3791-
3792-
llvm::function_ref<void(Decl *)> process;
3793-
process = [&](Decl *decl) {
3794-
if (auto *MED = dyn_cast<MacroExpansionDecl>(decl)) {
3795-
for (auto node : MED->getRewritten())
3796-
if (auto *expandedDecl = node.dyn_cast<Decl *>())
3797-
process(expandedDecl);
3813+
for (auto *decl : nonExpandedDecls) {
3814+
if (auto *med = dyn_cast<MacroExpansionDecl>(decl)) {
3815+
for (auto node : med->getFlattenedExpansion())
3816+
if (auto *decl = node.dyn_cast<Decl *>())
3817+
results.push_back(decl);
37983818
} else {
37993819
results.push_back(decl);
38003820
}
3801-
};
3802-
for (auto *decl : nonExpandedDecls)
3803-
process(decl);
3821+
}
38043822
}
38053823

38063824
void FileUnit::dumpDisplayDecls() const {

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,6 @@ MemberLookupTable::MemberLookupTable(ASTContext &ctx) {
13361336
}
13371337

13381338
void MemberLookupTable::addMember(Decl *member) {
1339-
13401339
// Only value declarations matter.
13411340
auto vd = dyn_cast<ValueDecl>(member);
13421341
if (!vd)
@@ -1515,7 +1514,7 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15151514
DeclName name,
15161515
NominalTypeDecl *dc) {
15171516
auto expandAndPopulate = [&](MacroExpansionDecl *med) {
1518-
for (auto node : med->getRewritten())
1517+
for (auto node : med->getFlattenedExpansion())
15191518
if (auto *decl = node.dyn_cast<Decl *>())
15201519
table.addMember(decl);
15211520
};

lib/AST/TypeCheckRequests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,10 +1743,7 @@ ArgumentList *UnresolvedMacroReference::getArgs() const {
17431743
}
17441744

17451745
MacroRoles UnresolvedMacroReference::getMacroRoles() const {
1746-
if (pointer.is<MacroExpansionExpr *>())
1747-
return MacroRole::Expression;
1748-
1749-
if (pointer.is<MacroExpansionDecl *>())
1746+
if (pointer.is<MacroExpansionExpr *>() || pointer.is<MacroExpansionDecl *>())
17501747
return getFreestandingMacroRoles();
17511748

17521749
if (pointer.is<CustomAttr *>())

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,7 +4682,6 @@ bool swift::isKeywordPossibleDeclStart(const Token &Tok) {
46824682
case tok::kw_subscript:
46834683
case tok::kw_typealias:
46844684
case tok::kw_var:
4685-
case tok::pound:
46864685
case tok::pound_if:
46874686
case tok::pound_warning:
46884687
case tok::pound_error:
@@ -4794,10 +4793,6 @@ bool Parser::isStartOfSwiftDecl(bool allowPoundIfAttributes) {
47944793
return isStartOfSwiftDecl(allowPoundIfAttributes);
47954794
}
47964795

4797-
// Macro expansion starts with a pound and a custom identifier.
4798-
if (Tok.is(tok::pound) && peekToken().is(tok::identifier))
4799-
return true;
4800-
48014796
// Skip a #if that contains only attributes in all branches. These will be
48024797
// parsed as attributes of a declaration, not as separate declarations.
48034798
if (Tok.is(tok::pound_if) && allowPoundIfAttributes) {
@@ -9593,9 +9588,6 @@ Parser::parseDeclMacroExpansion(ParseDeclOptions flags,
95939588
}
95949589
}
95959590

9596-
if (!argList)
9597-
argList = ArgumentList::createImplicit(Context, {});
9598-
95999591
return makeParserResult(
96009592
status,
96019593
new (Context) MacroExpansionDecl(

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3391,7 +3391,9 @@ ParserResult<Expr> Parser::parseExprMacroExpansion(bool isExprBasic) {
33913391
new (Context) MacroExpansionExpr(
33923392
CurDeclContext, poundLoc, macroNameRef, macroNameLoc, leftAngleLoc,
33933393
Context.AllocateCopy(genericArgs), rightAngleLoc, argList,
3394-
MacroRole::Expression));
3394+
CurDeclContext->isTypeContext()
3395+
? MacroRole::Declaration
3396+
: getFreestandingMacroRoles()));
33953397
}
33963398

33973399
/// parseExprCollection - Parse a collection literal expression.

lib/Parse/ParseRequests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
173173

174174
switch (generatedInfo->kind) {
175175
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
176+
if (parser.CurDeclContext->isTypeContext()) {
177+
parser.parseExpandedMemberList(items);
178+
} else {
179+
parser.parseTopLevelItems(items);
180+
}
181+
break;
182+
176183
case GeneratedSourceInfo::ExpressionMacroExpansion:
177184
case GeneratedSourceInfo::ReplacedFunctionBody:
178185
case GeneratedSourceInfo::PrettyPrinted: {

0 commit comments

Comments
 (0)