Skip to content

Commit 6cb79a5

Browse files
committed
[Macros] Share storage between linked MacroExpansion(Decl|Expr).
Sometimes we build a `MacroExpansionDecl` from a `MacroExpansionExpr`. Sometimes we do it the other way. In both cases, we risk the two copies of must-by-shared data (macro arguments, resolved macro reference, etc.) getting out-of-sync. Instead, share the storage between the two representations when we create one from the other, so that they cannot get out-of-sync. This allows us to eliminate the extremely-dodgy `cacheOutput` call earlier. (cherry picked from commit 583ca47)
1 parent 517c194 commit 6cb79a5

File tree

6 files changed

+110
-84
lines changed

6 files changed

+110
-84
lines changed

include/swift/AST/Decl.h

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8538,8 +8538,13 @@ class MacroDecl : public GenericContext, public ValueDecl {
85388538
using Decl::getASTContext;
85398539
};
85408540

8541-
class MacroExpansionDecl : public Decl {
8542-
SourceLoc PoundLoc;
8541+
/// Information about a macro expansion that is common between macro
8542+
/// expansion declarations and expressions.
8543+
///
8544+
/// Instances of these types will be shared among paired macro expansion
8545+
/// declaration/expression nodes.
8546+
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
8547+
SourceLoc SigilLoc;
85438548
DeclNameRef MacroName;
85448549
DeclNameLoc MacroNameLoc;
85458550
SourceLoc LeftAngleLoc, RightAngleLoc;
@@ -8549,33 +8554,53 @@ class MacroExpansionDecl : public Decl {
85498554
/// The referenced macro.
85508555
ConcreteDeclRef macroRef;
85518556

8557+
MacroExpansionInfo(SourceLoc sigilLoc,
8558+
DeclNameRef macroName,
8559+
DeclNameLoc macroNameLoc,
8560+
SourceLoc leftAngleLoc, SourceLoc rightAngleLoc,
8561+
ArrayRef<TypeRepr *> genericArgs,
8562+
ArgumentList *argList)
8563+
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
8564+
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8565+
GenericArgs(genericArgs), ArgList(argList) { }
8566+
};
8567+
8568+
class MacroExpansionDecl : public Decl {
8569+
MacroExpansionInfo *info;
8570+
85528571
public:
85538572
enum : unsigned { InvalidDiscriminator = 0xFFFF };
85548573

8574+
MacroExpansionDecl(DeclContext *dc, MacroExpansionInfo *info);
8575+
85558576
MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
85568577
DeclNameLoc macroLoc,
85578578
SourceLoc leftAngleLoc,
85588579
ArrayRef<TypeRepr *> genericArgs,
85598580
SourceLoc rightAngleLoc,
85608581
ArgumentList *args);
85618582

8562-
ArrayRef<TypeRepr *> getGenericArgs() const { return GenericArgs; }
8583+
ArrayRef<TypeRepr *> getGenericArgs() const {
8584+
return info->GenericArgs;
8585+
}
85638586

85648587
SourceRange getGenericArgsRange() const {
8565-
return SourceRange(LeftAngleLoc, RightAngleLoc);
8588+
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
85668589
}
85678590

85688591
SourceRange getSourceRange() const;
8569-
SourceLoc getLocFromSource() const { return PoundLoc; }
8570-
SourceLoc getPoundLoc() const { return PoundLoc; }
8571-
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
8572-
DeclNameRef getMacroName() const { return MacroName; }
8573-
ArgumentList *getArgs() const { return ArgList; }
8574-
void setArgs(ArgumentList *args) { ArgList = args; }
8592+
SourceLoc getLocFromSource() const { return info->SigilLoc; }
8593+
SourceLoc getPoundLoc() const { return info->SigilLoc; }
8594+
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
8595+
DeclNameRef getMacroName() const { return info->MacroName; }
8596+
ArgumentList *getArgs() const { return info->ArgList; }
8597+
void setArgs(ArgumentList *args) { info->ArgList = args; }
85758598
using ExprOrStmtExpansionCallback = llvm::function_ref<void(ASTNode)>;
85768599
void forEachExpandedExprOrStmt(ExprOrStmtExpansionCallback) const;
8577-
ConcreteDeclRef getMacroRef() const { return macroRef; }
8578-
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
8600+
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
8601+
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
8602+
8603+
MacroExpansionInfo *getExpansionInfo() const { return info; }
85798604

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

include/swift/AST/Expr.h

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6189,22 +6189,24 @@ class TypeJoinExpr final : public Expr,
61896189
class MacroExpansionExpr final : public Expr {
61906190
private:
61916191
DeclContext *DC;
6192-
SourceLoc SigilLoc;
6193-
DeclNameRef MacroName;
6194-
DeclNameLoc MacroNameLoc;
6195-
SourceLoc LeftAngleLoc, RightAngleLoc;
6196-
ArrayRef<TypeRepr *> GenericArgs;
6197-
ArgumentList *ArgList;
6192+
MacroExpansionInfo *info;
61986193
Expr *Rewritten;
61996194
MacroRoles Roles;
62006195
MacroExpansionDecl *SubstituteDecl;
62016196

6202-
/// The referenced macro.
6203-
ConcreteDeclRef macroRef;
6204-
62056197
public:
62066198
enum : unsigned { InvalidDiscriminator = 0xFFFF };
62076199

6200+
explicit MacroExpansionExpr(DeclContext *dc, MacroExpansionInfo *info,
6201+
MacroRoles roles,
6202+
bool isImplicit = false,
6203+
Type ty = Type())
6204+
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6205+
DC(dc), info(info), Rewritten(nullptr), Roles(roles),
6206+
SubstituteDecl(nullptr) {
6207+
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
6208+
}
6209+
62086210
explicit MacroExpansionExpr(DeclContext *dc,
62096211
SourceLoc sigilLoc, DeclNameRef macroName,
62106212
DeclNameLoc macroNameLoc,
@@ -6214,45 +6216,29 @@ class MacroExpansionExpr final : public Expr {
62146216
ArgumentList *argList,
62156217
MacroRoles roles,
62166218
bool isImplicit = false,
6217-
Type ty = Type())
6218-
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6219-
DC(dc), SigilLoc(sigilLoc),
6220-
MacroName(macroName), MacroNameLoc(macroNameLoc),
6221-
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
6222-
GenericArgs(genericArgs),
6223-
Rewritten(nullptr), Roles(roles), SubstituteDecl(nullptr) {
6224-
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
6219+
Type ty = Type());
62256220

6226-
// Macro expansions always have an argument list. If one is not provided, create
6227-
// an implicit one.
6228-
if (argList) {
6229-
ArgList = argList;
6230-
} else {
6231-
ArgList = ArgumentList::createImplicit(dc->getASTContext(), {});
6232-
}
6233-
}
6234-
6235-
DeclNameRef getMacroName() const { return MacroName; }
6236-
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
6221+
DeclNameRef getMacroName() const { return info->MacroName; }
6222+
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
62376223

62386224
Expr *getRewritten() const { return Rewritten; }
62396225
void setRewritten(Expr *rewritten) { Rewritten = rewritten; }
62406226

6241-
ArrayRef<TypeRepr *> getGenericArgs() const { return GenericArgs; }
6227+
ArrayRef<TypeRepr *> getGenericArgs() const { return info->GenericArgs; }
62426228

62436229
SourceRange getGenericArgsRange() const {
6244-
return SourceRange(LeftAngleLoc, RightAngleLoc);
6230+
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
62456231
}
62466232

6247-
ArgumentList *getArgs() const { return ArgList; }
6248-
void setArgs(ArgumentList *newArgs) { ArgList = newArgs; }
6233+
ArgumentList *getArgs() const { return info->ArgList; }
6234+
void setArgs(ArgumentList *newArgs) { info->ArgList = newArgs; }
62496235

62506236
MacroRoles getMacroRoles() const { return Roles; }
62516237

6252-
SourceLoc getLoc() const { return SigilLoc; }
6238+
SourceLoc getLoc() const { return info->SigilLoc; }
62536239

6254-
ConcreteDeclRef getMacroRef() const { return macroRef; }
6255-
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
6240+
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
6241+
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
62566242

62576243
DeclContext *getDeclContext() const { return DC; }
62586244
void setDeclContext(DeclContext *dc) { DC = dc; }
@@ -6275,11 +6261,12 @@ class MacroExpansionExpr final : public Expr {
62756261
Bits.MacroExpansionExpr.Discriminator = discriminator;
62766262
}
62776263

6264+
MacroExpansionInfo *getExpansionInfo() const { return info; }
6265+
62786266
SourceRange getSourceRange() const;
62796267

6280-
MacroExpansionDecl *createSubstituteDecl() const;
6268+
MacroExpansionDecl *createSubstituteDecl();
62816269
MacroExpansionDecl *getSubstituteDecl() const;
6282-
void setSubstituteDecl(MacroExpansionDecl *decl);
62836270

62846271
static bool classof(const Expr *E) {
62856272
return E->getKind() == ExprKind::MacroExpansion;

lib/AST/Decl.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10334,30 +10334,37 @@ MacroDefinition MacroDefinition::forExpanded(
1033410334
ctx.AllocateCopy(replacements)};
1033510335
}
1033610336

10337+
MacroExpansionDecl::MacroExpansionDecl(
10338+
DeclContext *dc, MacroExpansionInfo *info
10339+
) : Decl(DeclKind::MacroExpansion, dc), info(info) {
10340+
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
10341+
}
10342+
1033710343
MacroExpansionDecl::MacroExpansionDecl(
1033810344
DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
1033910345
DeclNameLoc macroLoc, SourceLoc leftAngleLoc,
1034010346
ArrayRef<TypeRepr *> genericArgs, SourceLoc rightAngleLoc,
10341-
ArgumentList *args)
10342-
: Decl(DeclKind::MacroExpansion, dc), PoundLoc(poundLoc),
10343-
MacroName(macro), MacroNameLoc(macroLoc),
10344-
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
10345-
GenericArgs(genericArgs),
10346-
ArgList(args ? args
10347-
: ArgumentList::createImplicit(dc->getASTContext(), {})) {
10347+
ArgumentList *args
10348+
) : Decl(DeclKind::MacroExpansion, dc) {
10349+
ASTContext &ctx = dc->getASTContext();
10350+
info = new (ctx) MacroExpansionInfo{
10351+
poundLoc, macro, macroLoc,
10352+
leftAngleLoc, rightAngleLoc, genericArgs,
10353+
args ? args : ArgumentList::createImplicit(ctx, {})
10354+
};
1034810355
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
1034910356
}
1035010357

1035110358
SourceRange MacroExpansionDecl::getSourceRange() const {
1035210359
SourceLoc endLoc;
10353-
if (auto argsEndList = ArgList->getEndLoc())
10360+
if (auto argsEndList = info->ArgList->getEndLoc())
1035410361
endLoc = argsEndList;
10355-
else if (RightAngleLoc.isValid())
10356-
endLoc = RightAngleLoc;
10362+
else if (info->RightAngleLoc.isValid())
10363+
endLoc = info->RightAngleLoc;
1035710364
else
10358-
endLoc = MacroNameLoc.getEndLoc();
10365+
endLoc = info->MacroNameLoc.getEndLoc();
1035910366

10360-
return SourceRange(PoundLoc, endLoc);
10367+
return SourceRange(info->SigilLoc, endLoc);
1036110368
}
1036210369

1036310370
unsigned MacroExpansionDecl::getDiscriminator() const {

lib/AST/Expr.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,16 +2716,34 @@ TypeJoinExpr::forBranchesOfSingleValueStmtExpr(ASTContext &ctx, Type joinType,
27162716
return createImpl(ctx, joinType.getPointer(), /*elements*/ {}, arena, SVE);
27172717
}
27182718

2719+
MacroExpansionExpr::MacroExpansionExpr(
2720+
DeclContext *dc, SourceLoc sigilLoc, DeclNameRef macroName,
2721+
DeclNameLoc macroNameLoc, SourceLoc leftAngleLoc,
2722+
ArrayRef<TypeRepr *> genericArgs, SourceLoc rightAngleLoc,
2723+
ArgumentList *argList, MacroRoles roles, bool isImplicit,
2724+
Type ty
2725+
) : Expr(ExprKind::MacroExpansion, isImplicit, ty), DC(dc),
2726+
Rewritten(nullptr), Roles(roles), SubstituteDecl(nullptr) {
2727+
ASTContext &ctx = dc->getASTContext();
2728+
info = new (ctx) MacroExpansionInfo{
2729+
sigilLoc, macroName, macroNameLoc,
2730+
leftAngleLoc, rightAngleLoc, genericArgs,
2731+
argList ? argList : ArgumentList::createImplicit(ctx, {})
2732+
};
2733+
2734+
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
2735+
}
2736+
27192737
SourceRange MacroExpansionExpr::getSourceRange() const {
27202738
SourceLoc endLoc;
2721-
if (ArgList && !ArgList->isImplicit())
2722-
endLoc = ArgList->getEndLoc();
2723-
else if (RightAngleLoc.isValid())
2724-
endLoc = RightAngleLoc;
2739+
if (info->ArgList && !info->ArgList->isImplicit())
2740+
endLoc = info->ArgList->getEndLoc();
2741+
else if (info->RightAngleLoc.isValid())
2742+
endLoc = info->RightAngleLoc;
27252743
else
2726-
endLoc = MacroNameLoc.getEndLoc();
2744+
endLoc = info->MacroNameLoc.getEndLoc();
27272745

2728-
return SourceRange(SigilLoc, endLoc);
2746+
return SourceRange(info->SigilLoc, endLoc);
27292747
}
27302748

27312749
unsigned MacroExpansionExpr::getDiscriminator() const {
@@ -2745,23 +2763,18 @@ unsigned MacroExpansionExpr::getDiscriminator() const {
27452763
return getRawDiscriminator();
27462764
}
27472765

2748-
MacroExpansionDecl *MacroExpansionExpr::createSubstituteDecl() const {
2766+
MacroExpansionDecl *MacroExpansionExpr::createSubstituteDecl() {
27492767
auto dc = DC;
27502768
if (auto *tlcd = dyn_cast_or_null<TopLevelCodeDecl>(dc->getAsDecl()))
27512769
dc = tlcd->getDeclContext();
2752-
return new (DC->getASTContext()) MacroExpansionDecl(
2753-
dc, SigilLoc, MacroName, MacroNameLoc, LeftAngleLoc,
2754-
GenericArgs, RightAngleLoc, ArgList);
2770+
SubstituteDecl = new (DC->getASTContext()) MacroExpansionDecl(dc, info);
2771+
return SubstituteDecl;
27552772
}
27562773

27572774
MacroExpansionDecl *MacroExpansionExpr::getSubstituteDecl() const {
27582775
return SubstituteDecl;
27592776
}
27602777

2761-
void MacroExpansionExpr::setSubstituteDecl(MacroExpansionDecl *decl) {
2762-
SubstituteDecl = decl;
2763-
}
2764-
27652778
void swift::simple_display(llvm::raw_ostream &out, const ClosureExpr *CE) {
27662779
if (!CE) {
27672780
out << "(null)";

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5398,15 +5398,6 @@ namespace {
53985398
auto &ctx = cs.getASTContext();
53995399
if (!E->getSubstituteDecl()) {
54005400
auto *med = E->createSubstituteDecl();
5401-
5402-
// Cache the result of ResolveMacroRequest for this new declaration;
5403-
// we don't want to compute it again.
5404-
ctx.evaluator.cacheOutput(
5405-
ResolveMacroRequest{UnresolvedMacroReference{med},
5406-
med->getDeclContext()},
5407-
ConcreteDeclRef(macroRef));
5408-
5409-
E->setSubstituteDecl(med);
54105401
TypeChecker::typeCheckDecl(med);
54115402
}
54125403
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,9 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
14811481
MacroExpansionExpr *macroExpansion;
14821482
if (auto *expr = macroRef.getExpr()) {
14831483
macroExpansion = expr;
1484+
} else if (auto *decl = macroRef.getDecl()) {
1485+
macroExpansion = new (ctx) MacroExpansionExpr(
1486+
dc, decl->getExpansionInfo(), roles);
14841487
} else {
14851488
SourceRange genericArgsRange = macroRef.getGenericArgsRange();
14861489
macroExpansion = new (ctx) MacroExpansionExpr(

0 commit comments

Comments
 (0)