Skip to content

Commit 40b3321

Browse files
committed
[Macros] Fix name lookup of macro parameters in the definition.
This enables macros to be defined in terms of other macros.
1 parent 9292231 commit 40b3321

File tree

7 files changed

+57
-2
lines changed

7 files changed

+57
-2
lines changed

include/swift/AST/ASTScope.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,26 @@ class MacroDeclScope final : public ASTScopeImpl {
12151215

12161216
protected:
12171217
NullablePtr<const GenericParamList> genericParams() const override;
1218+
bool lookupLocalsOrMembers(DeclConsumer) const override;
1219+
};
1220+
1221+
/// The scope introduced for the definition of a macro, which follows the `=`.
1222+
class MacroDefinitionScope final : public ASTScopeImpl {
1223+
public:
1224+
Expr *const definition;
1225+
1226+
MacroDefinitionScope(Expr *definition) : definition(definition) {}
1227+
1228+
virtual ~MacroDefinitionScope() {}
1229+
SourceRange
1230+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1231+
std::string getClassName() const override;
1232+
1233+
private:
1234+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
1235+
1236+
protected:
1237+
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
12181238
};
12191239

12201240
class AbstractStmtScope : public ASTScopeImpl {

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ DEFINE_GET_CLASS_NAME(DifferentiableAttributeScope)
151151
DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
152152
DEFINE_GET_CLASS_NAME(EnumElementScope)
153153
DEFINE_GET_CLASS_NAME(MacroDeclScope)
154+
DEFINE_GET_CLASS_NAME(MacroDefinitionScope)
154155
DEFINE_GET_CLASS_NAME(IfStmtScope)
155156
DEFINE_GET_CLASS_NAME(WhileStmtScope)
156157
DEFINE_GET_CLASS_NAME(GuardStmtScope)

lib/AST/ASTScopeCreation.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ NO_NEW_INSERTION_POINT(IfStmtScope)
723723
NO_NEW_INSERTION_POINT(RepeatWhileScope)
724724
NO_NEW_INSERTION_POINT(SubscriptDeclScope)
725725
NO_NEW_INSERTION_POINT(MacroDeclScope)
726+
NO_NEW_INSERTION_POINT(MacroDefinitionScope)
726727
NO_NEW_INSERTION_POINT(SwitchStmtScope)
727728
NO_NEW_INSERTION_POINT(WhileStmtScope)
728729

@@ -1115,9 +1116,19 @@ void MacroDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
11151116
auto *leaf = scopeCreator.addNestedGenericParamScopesToTree(
11161117
decl, getPotentiallyOpaqueGenericParams(decl), this);
11171118
if (decl->parameterList) {
1118-
scopeCreator.constructExpandAndInsert<ParameterListScope>(
1119+
leaf = scopeCreator.constructExpandAndInsert<ParameterListScope>(
11191120
leaf, decl->parameterList, nullptr);
11201121
}
1122+
if (auto def = decl->definition) {
1123+
scopeCreator
1124+
.constructExpandAndInsert<MacroDefinitionScope>(leaf, def);
1125+
}
1126+
}
1127+
1128+
void
1129+
MacroDefinitionScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1130+
ScopeCreator &scopeCreator) {
1131+
scopeCreator.addToScopeTree(ASTNode(definition), this);
11211132
}
11221133

11231134
void CaptureListScope::expandAScopeThatDoesNotCreateANewInsertionPoint(

lib/AST/ASTScopeLookup.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ NullablePtr<const GenericParamList> MacroDeclScope::genericParams() const {
260260
return decl->getParsedGenericParams();
261261
}
262262

263+
bool MacroDeclScope::lookupLocalsOrMembers(
264+
DeclConsumer consumer) const {
265+
if (auto *paramList = decl->parameterList) {
266+
for (auto *paramDecl : *paramList)
267+
if (consumer.consume({paramDecl}))
268+
return true;
269+
}
270+
271+
return false;
272+
}
273+
263274
#pragma mark lookInMyGenericParameters
264275

265276
std::pair<bool, NullablePtr<const GenericParamList>>

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ SourceRange MacroDeclScope::getSourceRangeOfThisASTNode(
143143
return decl->getSourceRangeIncludingAttrs();
144144
}
145145

146+
SourceRange MacroDefinitionScope::getSourceRangeOfThisASTNode(
147+
const bool omitAssertions) const {
148+
return definition->getSourceRange();
149+
}
150+
146151
SourceRange
147152
EnumElementScope::getSourceRangeOfThisASTNode(const bool omitAssertions) const {
148153
return decl->getSourceRange();

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class Verifier : public ASTWalker {
477477
// For @objc enums, we serialize the pre-type-checked integer
478478
// literal raw values, and thus when they are deserialized
479479
// they do not have a type on them.
480-
if (!isa<IntegerLiteralExpr>(E)) {
480+
if (!isa<IntegerLiteralExpr>(E) && !isa<MacroExpansionExpr>(E)) {
481481
Out << "expression has no type\n";
482482
E->dump(Out);
483483
abort();

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
422422
return true;
423423
}
424424

425+
if (auto def = MD->definition) {
426+
if (auto newDef = doIt(def))
427+
MD->definition = newDef;
428+
else
429+
return true;
430+
}
431+
425432
// Visit trailing requirements
426433
if (WalkGenerics && visitTrailingRequirements(MD))
427434
return true;

0 commit comments

Comments
 (0)