Skip to content

Commit d6d2318

Browse files
committed
[Macros] Handle macro expansion for function-like macros.
1 parent af37fc0 commit d6d2318

File tree

8 files changed

+71
-29
lines changed

8 files changed

+71
-29
lines changed

include/swift/AST/Expr.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,20 +6013,23 @@ class TypeJoinExpr final : public Expr,
60136013

60146014
class MacroExpansionExpr final : public Expr {
60156015
private:
6016-
Expr *Macro;
6017-
Expr *Rewritten;
6018-
ArgumentList *ArgList;
60196016
SourceLoc PoundLoc;
6017+
DeclNameRef MacroName;
6018+
DeclNameLoc MacroNameLoc;
6019+
ArgumentList *ArgList;
6020+
Expr *Rewritten;
60206021

60216022
public:
6022-
explicit MacroExpansionExpr(SourceLoc poundLoc, Expr *macro,
6023+
explicit MacroExpansionExpr(SourceLoc poundLoc, DeclNameRef macroName,
6024+
DeclNameLoc macroNameLoc,
60236025
ArgumentList *argList, bool isImplicit = false,
60246026
Type ty = Type())
6025-
: Expr(ExprKind::MacroExpansion, isImplicit, ty), Macro(macro),
6026-
Rewritten(nullptr), ArgList(argList), PoundLoc(poundLoc) {}
6027+
: Expr(ExprKind::MacroExpansion, isImplicit, ty), PoundLoc(poundLoc),
6028+
MacroName(macroName), MacroNameLoc(macroNameLoc), ArgList(argList),
6029+
Rewritten(nullptr) { }
60276030

6028-
Expr *getMacro() const { return Macro; }
6029-
void setMacro(Expr *macro) { Macro = macro; }
6031+
DeclNameRef getMacroName() const { return MacroName; }
6032+
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
60306033

60316034
Expr *getRewritten() const { return Rewritten; }
60326035
void setRewritten(Expr *rewritten) { Rewritten = rewritten; }
@@ -6038,7 +6041,7 @@ class MacroExpansionExpr final : public Expr {
60386041

60396042
SourceRange getSourceRange() const {
60406043
return SourceRange(
6041-
PoundLoc, ArgList ? ArgList->getEndLoc() : Macro->getEndLoc());
6044+
PoundLoc, ArgList ? ArgList->getEndLoc() : MacroNameLoc.getEndLoc());
60426045
}
60436046

60446047
static bool classof(const Expr *E) {

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,8 +2976,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
29762976

29772977
void visitMacroExpansionExpr(MacroExpansionExpr *E) {
29782978
printCommon(E, "macro_expansion_expr");
2979-
OS << '\n';
2980-
printRec(E->getMacro());
2979+
PrintWithColorRAII(OS, IdentifierColor) << " name=" << E->getMacroName();
29812980
if (E->getArgs()) {
29822981
OS << '\n';
29832982
printArgumentList(E->getArgs());

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
12471247
}
12481248

12491249
Expr *visitMacroExpansionExpr(MacroExpansionExpr *E) {
1250-
auto *macro = doIt(E->getMacro());
1251-
if (!macro) return nullptr;
12521250
Expr *rewritten = nullptr;
12531251
if (E->getRewritten()) {
12541252
rewritten = doIt(E->getRewritten());
@@ -1259,7 +1257,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
12591257
args = doIt(E->getArgs());
12601258
if (!args) return nullptr;
12611259
}
1262-
E->setMacro(macro);
12631260
E->setRewritten(rewritten);
12641261
E->setArgs(args);
12651262
return E;

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,8 +3643,6 @@ ParserResult<Expr> Parser::parseExprMacroExpansion(bool isExprBasic) {
36433643
DeclNameOptions());
36443644
if (!macroNameRef)
36453645
return makeParserError();
3646-
auto *macroExpr = new (Context) UnresolvedDeclRefExpr(
3647-
macroNameRef, DeclRefKind::Ordinary, macroNameLoc);
36483646

36493647
ArgumentList *argList = nullptr;
36503648
if (Tok.isFollowingLParen()) {
@@ -3669,7 +3667,8 @@ ParserResult<Expr> Parser::parseExprMacroExpansion(bool isExprBasic) {
36693667
}
36703668

36713669
return makeParserResult(
3672-
new (Context) MacroExpansionExpr(poundLoc, macroExpr, argList));
3670+
new (Context) MacroExpansionExpr(
3671+
poundLoc, macroNameRef, macroNameLoc, argList));
36733672
}
36743673

36753674
/// parseExprCollection - Parse a collection literal expression.

lib/Sema/CSApply.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,9 +2977,12 @@ namespace {
29772977
auto kind = MagicIdentifierLiteralExpr::getKindString(expr->getKind())
29782978
.drop_front();
29792979
auto expandedType = solution.simplifyType(solution.getType(expr));
2980+
cs.setType(expr, expandedType);
2981+
29802982
if (auto newExpr = expandMacroExpr(dc, expr, kind, expandedType)) {
29812983
auto expansion = new (ctx) MacroExpansionExpr(
2982-
expr->getStartLoc(), expr, nullptr, /*isImplicit=*/true,
2984+
expr->getStartLoc(), DeclNameRef(ctx.getIdentifier(kind)),
2985+
DeclNameLoc(expr->getLoc()), nullptr, /*isImplicit=*/true,
29832986
expandedType);
29842987
expansion->setRewritten(newExpr);
29852988
cs.cacheExprTypes(expansion);
@@ -5385,7 +5388,22 @@ namespace {
53855388
}
53865389

53875390
Expr *visitMacroExpansionExpr(MacroExpansionExpr *E) {
5388-
// TODO: Expand macro.
5391+
#if SWIFT_SWIFT_PARSER
5392+
auto &ctx = cs.getASTContext();
5393+
if (ctx.LangOpts.hasFeature(Feature::Macros)) {
5394+
auto macroIdent = E->getMacroName().getBaseIdentifier();
5395+
auto expandedType = solution.simplifyType(solution.getType(E));
5396+
cs.setType(E, expandedType);
5397+
if (auto newExpr = expandMacroExpr(
5398+
dc, E, macroIdent.str(), expandedType)) {
5399+
E->setRewritten(newExpr);
5400+
cs.cacheExprTypes(E);
5401+
return E;
5402+
}
5403+
5404+
// Fall through to use old implementation.
5405+
}
5406+
#endif
53895407
return E;
53905408
}
53915409

lib/Sema/CSGen.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3631,12 +3631,31 @@ namespace {
36313631
#if SWIFT_SWIFT_PARSER
36323632
auto &ctx = CS.getASTContext();
36333633
if (ctx.LangOpts.hasFeature(Feature::Macros)) {
3634-
auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(expr->getMacro());
3635-
if (!UDRE)
3636-
return Type();
3637-
3638-
auto macroIdent = UDRE->getName().getBaseIdentifier();
3639-
return CS.getTypeOfMacroReference(macroIdent.str(), expr);
3634+
auto macroIdent = expr->getMacroName().getBaseIdentifier();
3635+
auto refType = CS.getTypeOfMacroReference(macroIdent.str(), expr);
3636+
if (expr->getArgs()) {
3637+
CS.associateArgumentList(CS.getConstraintLocator(expr), expr->getArgs());
3638+
// FIXME: Do we have object-like vs. function-like macros?
3639+
if (auto fnType = dyn_cast<FunctionType>(refType.getPointer())) {
3640+
SmallVector<AnyFunctionType::Param, 8> params;
3641+
getMatchingParams(expr->getArgs(), params);
3642+
3643+
Type resultType = CS.createTypeVariable(
3644+
CS.getConstraintLocator(expr, ConstraintLocator::FunctionResult),
3645+
TVO_CanBindToNoEscape);
3646+
3647+
CS.addConstraint(
3648+
ConstraintKind::ApplicableFunction,
3649+
FunctionType::get(params, resultType),
3650+
fnType,
3651+
CS.getConstraintLocator(
3652+
expr, ConstraintLocator::ApplyFunction));
3653+
3654+
return resultType;
3655+
}
3656+
}
3657+
3658+
return refType;
36403659
}
36413660
#endif
36423661
return Type();

test/Macros/builtin_macros.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
// REQUIRES: OS=macosx
33
// REQUIRES: asserts
44

5+
56
// CHECK: macro_expansion_expr implicit type='String'
6-
// CHECK-NEXT: magic_identifier_literal_expr{{.*}}kind=#function
77
// CHECK-NEXT: string_literal_expr{{.*}}Macro expansion of #function in{{.*}}value="MacrosTest"
88
print(#function)
99

1010
func f(a: Int, b: Int) {
1111
print(#function, #line, #column)
1212
// CHECK: macro_expansion_expr implicit type='String'
13-
// CHECK-NEXT: magic_identifier_literal_expr{{.*}}kind=#function
1413
// CHECK-NEXT: string_literal_expr{{.*}}Macro expansion of #function in{{.*}}value="f(a:b:)"
1514

1615
// CHECK: macro_expansion_expr implicit type='Int'
17-
// CHECK-NEXT: magic_identifier_literal_expr{{.*}}kind=#line
1816
// CHECK-NEXT: integer_literal_expr{{.*}}Macro expansion of #line in{{.*}}value=11
1917

2018
// CHECK: macro_expansion_expr implicit type='Int'
21-
// CHECK-NEXT: magic_identifier_literal_expr{{.*}}kind=#column
2219
// CHECK-NEXT: integer_literal_expr{{.*}}Macro expansion of #column in{{.*}}value=27
2320
}

test/Macros/macros.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -enable-experimental-feature Macros -dump-ast %s -module-name MacrosTest 2>&1 | %FileCheck %s
2+
// REQUIRES: OS=macosx
3+
// REQUIRES: asserts
4+
5+
let a = 1, b = 1
6+
let s = #stringify(a + b)
7+
// CHECK: macro_expansion_expr type='(Int, String)'{{.*}}name=stringify
8+
// CHECK-NEXT: argument_list
9+
// CHECK: tuple_expr type='(Int, String)' location=Macro expansion of #stringify
10+

0 commit comments

Comments
 (0)