Skip to content

Commit 36c3a81

Browse files
authored
Merge pull request #64761 from DougGregor/freestanding-macro-lookup-and-single-type-check
2 parents 4a68116 + 583ca47 commit 36c3a81

14 files changed

+214
-93
lines changed

include/swift/AST/ASTScope.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,32 @@ class MacroDefinitionScope final : public ASTScopeImpl {
12371237
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
12381238
};
12391239

1240+
class MacroExpansionDeclScope final : public ASTScopeImpl {
1241+
public:
1242+
MacroExpansionDecl *const decl;
1243+
1244+
MacroExpansionDeclScope(MacroExpansionDecl *e) : decl(e) {}
1245+
virtual ~MacroExpansionDeclScope() {}
1246+
1247+
protected:
1248+
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1249+
1250+
private:
1251+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
1252+
1253+
public:
1254+
std::string getClassName() const override;
1255+
SourceRange
1256+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1257+
1258+
protected:
1259+
void printSpecifics(llvm::raw_ostream &out) const override;
1260+
1261+
public:
1262+
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
1263+
Decl *getDecl() const { return decl; }
1264+
};
1265+
12401266
class AbstractStmtScope : public ASTScopeImpl {
12411267
public:
12421268
SourceRange

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/ASTScope.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
152152
DEFINE_GET_CLASS_NAME(EnumElementScope)
153153
DEFINE_GET_CLASS_NAME(MacroDeclScope)
154154
DEFINE_GET_CLASS_NAME(MacroDefinitionScope)
155+
DEFINE_GET_CLASS_NAME(MacroExpansionDeclScope)
155156
DEFINE_GET_CLASS_NAME(IfStmtScope)
156157
DEFINE_GET_CLASS_NAME(WhileStmtScope)
157158
DEFINE_GET_CLASS_NAME(GuardStmtScope)

lib/AST/ASTScopeCreation.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ class NodeAdder
317317
VISIT_AND_IGNORE(PoundDiagnosticDecl)
318318
VISIT_AND_IGNORE(MissingDecl)
319319
VISIT_AND_IGNORE(MissingMemberDecl)
320-
VISIT_AND_IGNORE(MacroExpansionDecl)
321320

322321
// Only members of the active clause are in scope, and those
323322
// are visited separately.
@@ -353,6 +352,7 @@ class NodeAdder
353352
VISIT_AND_CREATE(CaseStmt, CaseStmtScope)
354353
VISIT_AND_CREATE(AbstractFunctionDecl, AbstractFunctionDeclScope)
355354
VISIT_AND_CREATE(MacroDecl, MacroDeclScope)
355+
VISIT_AND_CREATE(MacroExpansionDecl, MacroExpansionDeclScope)
356356

357357
#undef VISIT_AND_CREATE
358358

@@ -724,6 +724,7 @@ NO_NEW_INSERTION_POINT(RepeatWhileScope)
724724
NO_NEW_INSERTION_POINT(SubscriptDeclScope)
725725
NO_NEW_INSERTION_POINT(MacroDeclScope)
726726
NO_NEW_INSERTION_POINT(MacroDefinitionScope)
727+
NO_NEW_INSERTION_POINT(MacroExpansionDeclScope)
727728
NO_NEW_INSERTION_POINT(SwitchStmtScope)
728729
NO_NEW_INSERTION_POINT(WhileStmtScope)
729730

@@ -1131,6 +1132,16 @@ MacroDefinitionScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
11311132
scopeCreator.addToScopeTree(ASTNode(definition), this);
11321133
}
11331134

1135+
void MacroExpansionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1136+
ScopeCreator &scopeCreator) {
1137+
// FIXME: If we get attributes on macro expansions, visit them here.
1138+
if (auto argList = decl->getArgs()) {
1139+
for (const auto &arg : *argList) {
1140+
scopeCreator.addExprToScopeTree(arg.getExpr(), this);
1141+
}
1142+
}
1143+
}
1144+
11341145
void CaptureListScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
11351146
ScopeCreator &scopeCreator) {
11361147
auto *closureExpr = expr->getClosureBody();

lib/AST/ASTScopePrinting.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ void MacroDeclScope::printSpecifics(llvm::raw_ostream &out) const {
182182
decl->dumpRef(out);
183183
}
184184

185+
void MacroExpansionDeclScope::printSpecifics(llvm::raw_ostream &out) const {
186+
out << decl->getMacroName();
187+
}
188+
185189
void ConditionalClausePatternUseScope::printSpecifics(
186190
llvm::raw_ostream &out) const {
187191
sec.getPattern()->print(out);

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ SourceRange MacroDefinitionScope::getSourceRangeOfThisASTNode(
148148
return definition->getSourceRange();
149149
}
150150

151+
SourceRange MacroExpansionDeclScope::getSourceRangeOfThisASTNode(
152+
const bool omitAssertions) const {
153+
return decl->getSourceRangeIncludingAttrs();
154+
}
155+
151156
SourceRange
152157
EnumElementScope::getSourceRangeOfThisASTNode(const bool omitAssertions) const {
153158
return decl->getSourceRange();

lib/AST/Decl.cpp

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

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

1035210359
SourceRange MacroExpansionDecl::getSourceRange() const {
1035310360
SourceLoc endLoc;
10354-
if (auto argsEndList = ArgList->getEndLoc())
10361+
if (auto argsEndList = info->ArgList->getEndLoc())
1035510362
endLoc = argsEndList;
10356-
else if (RightAngleLoc.isValid())
10357-
endLoc = RightAngleLoc;
10363+
else if (info->RightAngleLoc.isValid())
10364+
endLoc = info->RightAngleLoc;
1035810365
else
10359-
endLoc = MacroNameLoc.getEndLoc();
10366+
endLoc = info->MacroNameLoc.getEndLoc();
1036010367

10361-
return SourceRange(PoundLoc, endLoc);
10368+
return SourceRange(info->SigilLoc, endLoc);
1036210369
}
1036310370

1036410371
unsigned MacroExpansionDecl::getDiscriminator() const {

0 commit comments

Comments
 (0)