Skip to content

Commit 81943b2

Browse files
authored
[Macros] Clean up MacroExpansionDecl (swiftlang#63486)
- Remove the `Rewritten` field and `setRewritten()`. Make `getRewritten()` invoke the request. - Rename fields `Macro` and `MacroLoc` to `MacroName` and `MacroNameLoc` respectively as well as their getters to match those in `MacroExpansionExpr`.
1 parent 205313b commit 81943b2

File tree

10 files changed

+32
-29
lines changed

10 files changed

+32
-29
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8467,12 +8467,11 @@ class MacroDecl : public GenericContext, public ValueDecl {
84678467

84688468
class MacroExpansionDecl : public Decl {
84698469
SourceLoc PoundLoc;
8470-
DeclNameRef Macro;
8471-
DeclNameLoc MacroLoc;
8470+
DeclNameRef MacroName;
8471+
DeclNameLoc MacroNameLoc;
84728472
SourceLoc LeftAngleLoc, RightAngleLoc;
84738473
ArrayRef<TypeRepr *> GenericArgs;
84748474
ArgumentList *ArgList;
8475-
ArrayRef<Decl *> Rewritten;
84768475

84778476
/// The referenced macro.
84788477
ConcreteDeclRef macroRef;
@@ -8487,9 +8486,9 @@ class MacroExpansionDecl : public Decl {
84878486
SourceLoc rightAngleLoc,
84888487
ArgumentList *args)
84898488
: Decl(DeclKind::MacroExpansion, dc), PoundLoc(poundLoc),
8490-
Macro(macro), MacroLoc(macroLoc),
8489+
MacroName(macro), MacroNameLoc(macroLoc),
84918490
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8492-
GenericArgs(genericArgs), ArgList(args), Rewritten({}) {
8491+
GenericArgs(genericArgs), ArgList(args) {
84938492
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
84948493
}
84958494

@@ -8502,11 +8501,10 @@ class MacroExpansionDecl : public Decl {
85028501
SourceRange getSourceRange() const;
85038502
SourceLoc getLocFromSource() const { return PoundLoc; }
85048503
SourceLoc getPoundLoc() const { return PoundLoc; }
8505-
DeclNameLoc getMacroLoc() const { return MacroLoc; }
8506-
DeclNameRef getMacro() const { return Macro; }
8504+
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
8505+
DeclNameRef getMacroName() const { return MacroName; }
85078506
ArgumentList *getArgs() const { return ArgList; }
8508-
ArrayRef<Decl *> getRewritten() const { return Rewritten; }
8509-
void setRewritten(ArrayRef<Decl *> rewritten) { Rewritten = rewritten; }
8507+
ArrayRef<Decl *> getRewritten() const;
85108508
ConcreteDeclRef getMacroRef() const { return macroRef; }
85118509
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
85128510

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ namespace {
13211321

13221322
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
13231323
printCommon(MED, "macro_expansion_decl ");
1324-
OS << MED->getMacro();
1324+
OS << MED->getMacroName();
13251325
if (MED->getArgs()) {
13261326
OS << '\n';
13271327
OS.indent(Indent + 2);

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
33863386
appendMacroExpansionContext(
33873387
expansion->getLoc(), expansion->getDeclContext());
33883388
appendMacroExpansionOperator(
3389-
expansion->getMacro().getBaseName().userFacingName(),
3389+
expansion->getMacroName().getBaseName().userFacingName(),
33903390
MacroRole::Declaration,
33913391
expansion->getDiscriminator());
33923392
return;
@@ -3748,7 +3748,7 @@ void ASTMangler::appendMacroExpansionContext(
37483748
} else {
37493749
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
37503750
outerExpansionLoc = decl->getLoc();
3751-
baseName = decl->getMacro().getBaseName();
3751+
baseName = decl->getMacroName().getBaseName();
37523752
discriminator = decl->getDiscriminator();
37533753
role = MacroRole::Declaration;
37543754
}
@@ -3854,7 +3854,7 @@ std::string ASTMangler::mangleMacroExpansion(
38543854
beginMangling();
38553855
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
38563856
appendMacroExpansionOperator(
3857-
expansion->getMacro().getBaseName().userFacingName(),
3857+
expansion->getMacroName().getBaseName().userFacingName(),
38583858
MacroRole::Declaration,
38593859
expansion->getDiscriminator());
38603860
return finalize();

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4600,7 +4600,7 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
46004600
}
46014601

46024602
void PrintAST::visitMacroExpansionDecl(MacroExpansionDecl *decl) {
4603-
Printer << '#' << decl->getMacro();
4603+
Printer << '#' << decl->getMacroName();
46044604
if (decl->getArgs()) {
46054605
Printer << '(';
46064606
auto args = decl->getArgs()->getOriginalArgs();

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,7 @@ class Verifier : public ASTWalker {
24362436
auto dc = getCanonicalDeclContext(expansion->getDeclContext());
24372437
MacroExpansionDiscriminatorKey key{
24382438
dc,
2439-
expansion->getMacro().getBaseName().getIdentifier()
2439+
expansion->getMacroName().getBaseName().getIdentifier()
24402440
};
24412441
auto &discriminatorSet = MacroExpansionDiscriminators[key];
24422442
unsigned discriminator = expansion->getDiscriminator();

lib/AST/Decl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9894,7 +9894,7 @@ SourceRange MacroExpansionDecl::getSourceRange() const {
98949894
else if (RightAngleLoc.isValid())
98959895
endLoc = RightAngleLoc;
98969896
else
9897-
endLoc = MacroLoc.getEndLoc();
9897+
endLoc = MacroNameLoc.getEndLoc();
98989898

98999899
return SourceRange(PoundLoc, endLoc);
99009900
}
@@ -9910,12 +9910,19 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
99109910
MacroDiscriminatorContext::getParentOf(mutableThis);
99119911
mutableThis->setDiscriminator(
99129912
ctx.getNextMacroDiscriminator(
9913-
discriminatorContext, getMacro().getBaseName()));
9913+
discriminatorContext, getMacroName().getBaseName()));
99149914

99159915
assert(getRawDiscriminator() != InvalidDiscriminator);
99169916
return getRawDiscriminator();
99179917
}
99189918

9919+
ArrayRef<Decl *> MacroExpansionDecl::getRewritten() const {
9920+
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
9921+
return evaluateOrDefault(
9922+
getASTContext().evaluator,
9923+
ExpandMacroExpansionDeclRequest{mutableThis}, {});
9924+
}
9925+
99199926
NominalTypeDecl *
99209927
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
99219928
auto &ctx = getASTContext();

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
13281328
} else {
13291329
auto expansionDecl =
13301330
cast<MacroExpansionDecl>(expansionNode.get<Decl *>());
1331-
macroName = expansionDecl->getMacro().getFullName();
1331+
macroName = expansionDecl->getMacroName().getFullName();
13321332
}
13331333

13341334
Diagnostic expansionNote(diag::in_macro_expansion, macroName);

lib/AST/TypeCheckRequests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ void swift::simple_display(
16641664

16651665
DeclNameRef UnresolvedMacroReference::getMacroName() const {
16661666
if (auto *med = pointer.dyn_cast<MacroExpansionDecl *>())
1667-
return med->getMacro();
1667+
return med->getMacroName();
16681668
if (auto *mee = pointer.dyn_cast<MacroExpansionExpr *>())
16691669
return mee->getMacroName();
16701670
if (auto *attr = pointer.dyn_cast<CustomAttr *>()) {
@@ -1688,7 +1688,7 @@ SourceLoc UnresolvedMacroReference::getSigilLoc() const {
16881688

16891689
DeclNameLoc UnresolvedMacroReference::getMacroNameLoc() const {
16901690
if (auto *med = pointer.dyn_cast<MacroExpansionDecl *>())
1691-
return med->getMacroLoc();
1691+
return med->getMacroNameLoc();
16921692
if (auto *mee = pointer.dyn_cast<MacroExpansionExpr *>())
16931693
return mee->getMacroNameLoc();
16941694
if (auto *attr = pointer.dyn_cast<CustomAttr *>()) {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,11 +3738,11 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37383738
auto &ctx = MED->getASTContext();
37393739
auto *dc = MED->getDeclContext();
37403740
auto foundMacros = TypeChecker::lookupMacros(
3741-
MED->getDeclContext(), MED->getMacro(),
3741+
MED->getDeclContext(), MED->getMacroName(),
37423742
MED->getLoc(), MacroRole::Declaration);
37433743
if (foundMacros.empty()) {
3744-
MED->diagnose(diag::macro_undefined, MED->getMacro().getBaseIdentifier())
3745-
.highlight(MED->getMacroLoc().getSourceRange());
3744+
MED->diagnose(diag::macro_undefined, MED->getMacroName().getBaseIdentifier())
3745+
.highlight(MED->getMacroNameLoc().getSourceRange());
37463746
return {};
37473747
}
37483748
// Resolve macro candidates.
@@ -3754,8 +3754,8 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37543754
}
37553755
else {
37563756
if (foundMacros.size() > 1) {
3757-
MED->diagnose(diag::ambiguous_decl_ref, MED->getMacro())
3758-
.highlight(MED->getMacroLoc().getSourceRange());
3757+
MED->diagnose(diag::ambiguous_decl_ref, MED->getMacroName())
3758+
.highlight(MED->getMacroNameLoc().getSourceRange());
37593759
for (auto *candidate : foundMacros)
37603760
candidate->diagnose(diag::found_candidate);
37613761
return {};
@@ -3770,7 +3770,5 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37703770
SmallVector<Decl *, 2> expandedTemporary;
37713771
if (!expandFreestandingDeclarationMacro(MED, expandedTemporary))
37723772
return {};
3773-
auto expanded = ctx.AllocateCopy(expandedTemporary);
3774-
MED->setRewritten(expanded);
3775-
return expanded;
3773+
return ctx.AllocateCopy(expandedTemporary);
37763774
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro,
374374
expansion.dyn_cast<Decl *>())) {
375375
// FIXME: Update once MacroExpansionDecl has a proper macro reference
376376
// in it.
377-
if (expansionDecl->getMacro().getFullName() == macro->getName())
377+
if (expansionDecl->getMacroName().getFullName() == macro->getName())
378378
return true;
379379
} else if (auto *macroAttr = sourceFile->getAttachedMacroAttribute()) {
380380
auto *decl = expansion.dyn_cast<Decl *>();

0 commit comments

Comments
 (0)