Skip to content

Commit 3f60a1d

Browse files
authored
Merge pull request #64803 from DougGregor/freestanding-macro-lookup-and-single-type-check-5.9
2 parents 45c26cd + 6cb79a5 commit 3f60a1d

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
@@ -8542,8 +8542,13 @@ class MacroDecl : public GenericContext, public ValueDecl {
85428542
using Decl::getASTContext;
85438543
};
85448544

8545-
class MacroExpansionDecl : public Decl {
8546-
SourceLoc PoundLoc;
8545+
/// Information about a macro expansion that is common between macro
8546+
/// expansion declarations and expressions.
8547+
///
8548+
/// Instances of these types will be shared among paired macro expansion
8549+
/// declaration/expression nodes.
8550+
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
8551+
SourceLoc SigilLoc;
85478552
DeclNameRef MacroName;
85488553
DeclNameLoc MacroNameLoc;
85498554
SourceLoc LeftAngleLoc, RightAngleLoc;
@@ -8553,33 +8558,53 @@ class MacroExpansionDecl : public Decl {
85538558
/// The referenced macro.
85548559
ConcreteDeclRef macroRef;
85558560

8561+
MacroExpansionInfo(SourceLoc sigilLoc,
8562+
DeclNameRef macroName,
8563+
DeclNameLoc macroNameLoc,
8564+
SourceLoc leftAngleLoc, SourceLoc rightAngleLoc,
8565+
ArrayRef<TypeRepr *> genericArgs,
8566+
ArgumentList *argList)
8567+
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
8568+
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8569+
GenericArgs(genericArgs), ArgList(argList) { }
8570+
};
8571+
8572+
class MacroExpansionDecl : public Decl {
8573+
MacroExpansionInfo *info;
8574+
85568575
public:
85578576
enum : unsigned { InvalidDiscriminator = 0xFFFF };
85588577

8578+
MacroExpansionDecl(DeclContext *dc, MacroExpansionInfo *info);
8579+
85598580
MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
85608581
DeclNameLoc macroLoc,
85618582
SourceLoc leftAngleLoc,
85628583
ArrayRef<TypeRepr *> genericArgs,
85638584
SourceLoc rightAngleLoc,
85648585
ArgumentList *args);
85658586

8566-
ArrayRef<TypeRepr *> getGenericArgs() const { return GenericArgs; }
8587+
ArrayRef<TypeRepr *> getGenericArgs() const {
8588+
return info->GenericArgs;
8589+
}
85678590

85688591
SourceRange getGenericArgsRange() const {
8569-
return SourceRange(LeftAngleLoc, RightAngleLoc);
8592+
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
85708593
}
85718594

85728595
SourceRange getSourceRange() const;
8573-
SourceLoc getLocFromSource() const { return PoundLoc; }
8574-
SourceLoc getPoundLoc() const { return PoundLoc; }
8575-
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
8576-
DeclNameRef getMacroName() const { return MacroName; }
8577-
ArgumentList *getArgs() const { return ArgList; }
8578-
void setArgs(ArgumentList *args) { ArgList = args; }
8596+
SourceLoc getLocFromSource() const { return info->SigilLoc; }
8597+
SourceLoc getPoundLoc() const { return info->SigilLoc; }
8598+
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
8599+
DeclNameRef getMacroName() const { return info->MacroName; }
8600+
ArgumentList *getArgs() const { return info->ArgList; }
8601+
void setArgs(ArgumentList *args) { info->ArgList = args; }
85798602
using ExprOrStmtExpansionCallback = llvm::function_ref<void(ASTNode)>;
85808603
void forEachExpandedExprOrStmt(ExprOrStmtExpansionCallback) const;
8581-
ConcreteDeclRef getMacroRef() const { return macroRef; }
8582-
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
8604+
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
8605+
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
8606+
8607+
MacroExpansionInfo *getExpansionInfo() const { return info; }
85838608

85848609
/// Returns a discriminator which determines this macro expansion's index
85858610
/// 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
@@ -10336,30 +10336,37 @@ MacroDefinition MacroDefinition::forExpanded(
1033610336
ctx.AllocateCopy(replacements)};
1033710337
}
1033810338

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

1035310360
SourceRange MacroExpansionDecl::getSourceRange() const {
1035410361
SourceLoc endLoc;
10355-
if (auto argsEndList = ArgList->getEndLoc())
10362+
if (auto argsEndList = info->ArgList->getEndLoc())
1035610363
endLoc = argsEndList;
10357-
else if (RightAngleLoc.isValid())
10358-
endLoc = RightAngleLoc;
10364+
else if (info->RightAngleLoc.isValid())
10365+
endLoc = info->RightAngleLoc;
1035910366
else
10360-
endLoc = MacroNameLoc.getEndLoc();
10367+
endLoc = info->MacroNameLoc.getEndLoc();
1036110368

10362-
return SourceRange(PoundLoc, endLoc);
10369+
return SourceRange(info->SigilLoc, endLoc);
1036310370
}
1036410371

1036510372
unsigned MacroExpansionDecl::getDiscriminator() const {

0 commit comments

Comments
 (0)