Skip to content

Commit 7c50f95

Browse files
committed
[Macros] Use MacroExpansionExpr when resolving attached and freestanding macros.
1 parent 4665053 commit 7c50f95

File tree

11 files changed

+59
-40
lines changed

11 files changed

+59
-40
lines changed

include/swift/AST/Expr.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6051,16 +6051,19 @@ class TypeJoinExpr final : public Expr,
60516051
}
60526052
};
60536053

6054+
/// An invocation of a macro expansion, spelled with `#` for freestanding
6055+
/// macros or `@` for attached macros.
60546056
class MacroExpansionExpr final : public Expr {
60556057
private:
60566058
DeclContext *DC;
6057-
SourceLoc PoundLoc;
6059+
SourceLoc SigilLoc;
60586060
DeclNameRef MacroName;
60596061
DeclNameLoc MacroNameLoc;
60606062
SourceLoc LeftAngleLoc, RightAngleLoc;
60616063
ArrayRef<TypeRepr *> GenericArgs;
60626064
ArgumentList *ArgList;
60636065
Expr *Rewritten;
6066+
MacroRoles Roles;
60646067

60656068
/// The referenced macro.
60666069
ConcreteDeclRef macroRef;
@@ -6069,21 +6072,22 @@ class MacroExpansionExpr final : public Expr {
60696072
enum : unsigned { InvalidDiscriminator = 0xFFFF };
60706073

60716074
explicit MacroExpansionExpr(DeclContext *dc,
6072-
SourceLoc poundLoc, DeclNameRef macroName,
6075+
SourceLoc sigilLoc, DeclNameRef macroName,
60736076
DeclNameLoc macroNameLoc,
60746077
SourceLoc leftAngleLoc,
60756078
ArrayRef<TypeRepr *> genericArgs,
60766079
SourceLoc rightAngleLoc,
60776080
ArgumentList *argList,
6081+
MacroRoles roles,
60786082
bool isImplicit = false,
60796083
Type ty = Type())
60806084
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6081-
DC(dc), PoundLoc(poundLoc),
6085+
DC(dc), SigilLoc(sigilLoc),
60826086
MacroName(macroName), MacroNameLoc(macroNameLoc),
60836087
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
60846088
GenericArgs(genericArgs),
60856089
ArgList(argList),
6086-
Rewritten(nullptr) {
6090+
Rewritten(nullptr), Roles(roles) {
60876091
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
60886092
}
60896093

@@ -6102,7 +6106,9 @@ class MacroExpansionExpr final : public Expr {
61026106
ArgumentList *getArgs() const { return ArgList; }
61036107
void setArgs(ArgumentList *newArgs) { ArgList = newArgs; }
61046108

6105-
SourceLoc getLoc() const { return PoundLoc; }
6109+
MacroRoles getMacroRoles() const { return Roles; }
6110+
6111+
SourceLoc getLoc() const { return SigilLoc; }
61066112

61076113
ConcreteDeclRef getMacroRef() const { return macroRef; }
61086114
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }

include/swift/AST/TypeCheckRequests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,7 @@ class UnresolvedMacroReference {
31513151
return pointer.getOpaqueValue();
31523152
}
31533153

3154+
SourceLoc getSigilLoc() const;
31543155
DeclNameRef getMacroName() const;
31553156
DeclNameLoc getMacroNameLoc() const;
31563157
SourceRange getGenericArgsRange() const;

lib/AST/Expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,14 +2538,14 @@ TypeJoinExpr *TypeJoinExpr::create(ASTContext &ctx, DeclRefExpr *var,
25382538

25392539
SourceRange MacroExpansionExpr::getSourceRange() const {
25402540
SourceLoc endLoc;
2541-
if (ArgList)
2541+
if (ArgList && !ArgList->isImplicit())
25422542
endLoc = ArgList->getEndLoc();
25432543
else if (RightAngleLoc.isValid())
25442544
endLoc = RightAngleLoc;
25452545
else
25462546
endLoc = MacroNameLoc.getEndLoc();
25472547

2548-
return SourceRange(PoundLoc, endLoc);
2548+
return SourceRange(SigilLoc, endLoc);
25492549
}
25502550

25512551
unsigned MacroExpansionExpr::getDiscriminator() const {

lib/AST/TypeCheckRequests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,16 @@ DeclNameRef UnresolvedMacroReference::getMacroName() const {
16761676
llvm_unreachable("Unhandled case");
16771677
}
16781678

1679+
SourceLoc UnresolvedMacroReference::getSigilLoc() const {
1680+
if (auto *med = pointer.dyn_cast<MacroExpansionDecl *>())
1681+
return med->getPoundLoc();
1682+
if (auto *mee = pointer.dyn_cast<MacroExpansionExpr *>())
1683+
return mee->getLoc();
1684+
if (auto *attr = pointer.dyn_cast<CustomAttr *>())
1685+
return attr->getRangeWithAt().Start;
1686+
llvm_unreachable("Unhandled case");
1687+
}
1688+
16791689
DeclNameLoc UnresolvedMacroReference::getMacroNameLoc() const {
16801690
if (auto *med = pointer.dyn_cast<MacroExpansionDecl *>())
16811691
return med->getMacroLoc();

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3368,7 +3368,8 @@ ParserResult<Expr> Parser::parseExprMacroExpansion(bool isExprBasic) {
33683368
status,
33693369
new (Context) MacroExpansionExpr(
33703370
CurDeclContext, poundLoc, macroNameRef, macroNameLoc, leftAngleLoc,
3371-
Context.AllocateCopy(genericArgs), rightAngleLoc, argList));
3371+
Context.AllocateCopy(genericArgs), rightAngleLoc, argList,
3372+
MacroRole::Expression));
33723373
}
33733374

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

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,7 +2985,7 @@ namespace {
29852985
auto expansion = new (ctx) MacroExpansionExpr(
29862986
dc, expr->getStartLoc(), DeclNameRef(macro->getName()),
29872987
DeclNameLoc(expr->getLoc()), SourceLoc(), { }, SourceLoc(),
2988-
nullptr, /*isImplicit=*/true, expandedType);
2988+
nullptr, MacroRole::Expression, /*isImplicit=*/true, expandedType);
29892989
expansion->setMacroRef(macroRef);
29902990
expansion->setRewritten(newExpr);
29912991
cs.cacheExprTypes(expansion);
@@ -5406,7 +5406,8 @@ namespace {
54065406
ConcreteDeclRef macroRef = resolveConcreteDeclRef(macro, locator);
54075407
E->setMacroRef(macroRef);
54085408

5409-
if (!cs.Options.contains(ConstraintSystemFlags::DisableMacroExpansions)) {
5409+
if (E->getMacroRoles().contains(MacroRole::Expression) &&
5410+
!cs.Options.contains(ConstraintSystemFlags::DisableMacroExpansions)) {
54105411
if (auto newExpr = expandMacroExpr(dc, E, macroRef, expandedType)) {
54115412
E->setRewritten(newExpr);
54125413
}

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,8 @@ namespace {
12111211

12121212
auto macroIdent = ctx.getIdentifier(kind);
12131213
auto macros = lookupMacros(
1214-
macroIdent, expr->getLoc(), FunctionRefKind::Unapplied);
1214+
macroIdent, expr->getLoc(), FunctionRefKind::Unapplied,
1215+
MacroRole::Expression);
12151216
if (!macros.empty()) {
12161217
// Introduce an overload set for the macro reference.
12171218
auto locator = CS.getConstraintLocator(expr);
@@ -3775,10 +3776,11 @@ namespace {
37753776
/// Lookup all macros with the given macro name.
37763777
SmallVector<OverloadChoice, 1>
37773778
lookupMacros(Identifier macroName, SourceLoc loc,
3778-
FunctionRefKind functionRefKind) {
3779+
FunctionRefKind functionRefKind,
3780+
MacroRoles roles) {
37793781
SmallVector<OverloadChoice, 1> choices;
37803782
auto results = TypeChecker::lookupMacros(
3781-
CurDC, DeclNameRef(macroName), loc, MacroRole::Expression);
3783+
CurDC, DeclNameRef(macroName), loc, roles);
37823784
for (const auto &result : results) {
37833785
OverloadChoice choice = OverloadChoice(Type(), result, functionRefKind);
37843786
choices.push_back(choice);
@@ -3809,7 +3811,7 @@ namespace {
38093811
: FunctionRefKind::Unapplied;
38103812
auto macros = lookupMacros(
38113813
macroIdent, expr->getMacroNameLoc().getBaseNameLoc(),
3812-
functionRefKind);
3814+
functionRefKind, expr->getMacroRoles());
38133815
if (macros.empty()) {
38143816
ctx.Diags.diagnose(expr->getMacroNameLoc(), diag::macro_undefined,
38153817
macroIdent)

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3590,7 +3590,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
35903590
if (auto macro = dyn_cast<MacroDecl>(decl)) {
35913591
// Macro can only be used in an expansion. If we end up here, it's
35923592
// because we found a macro but are missing the leading '#'.
3593-
if (!(locator->isForMacroExpansion() || locator->getAnchor().isImplicit())) {
3593+
if (!locator->isForMacroExpansion()) {
35943594
// Record a fix here
35953595
(void)recordFix(MacroMissingPound::create(*this, macro, locator));
35963596
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,36 +1258,34 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
12581258
return nullptr;
12591259

12601260
// Extract macro arguments, or create an empty list.
1261-
auto *args = macroRef.getArgs();
1262-
if (!args)
1263-
args = ArgumentList::createImplicit(ctx, {});
1264-
1265-
// Form an `OverloadedDeclRefExpr` with the filtered lookup result above
1266-
// to ensure @freestanding macros are not considered in overload resolution.
1267-
FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
1268-
SmallVector<ValueDecl *> valueDecls;
1269-
for (auto *macro : foundMacros)
1270-
valueDecls.push_back(macro);
1271-
Expr *callee = new (ctx) OverloadedDeclRefExpr(
1272-
valueDecls, macroRef.getMacroNameLoc(), functionRefKind,
1273-
/*implicit*/true);
1274-
auto genArgs = macroRef.getGenericArgs();
1275-
if (!genArgs.empty()) {
1276-
auto genArgsRange = macroRef.getGenericArgsRange();
1277-
callee = UnresolvedSpecializeExpr::create(
1278-
ctx, callee, genArgsRange.Start, genArgs, genArgsRange.End);
1261+
auto *argList = macroRef.getArgs();
1262+
if (!argList)
1263+
argList = ArgumentList::createImplicit(ctx, {});
1264+
1265+
// If we already have a MacroExpansionExpr, use that. Otherwise,
1266+
// create one.
1267+
MacroExpansionExpr *macroExpansion;
1268+
if (auto *expr = macroRef.getExpr()) {
1269+
macroExpansion = expr;
1270+
} else {
1271+
SourceRange genericArgsRange = macroRef.getGenericArgsRange();
1272+
macroExpansion = new (ctx) MacroExpansionExpr(
1273+
dc, macroRef.getSigilLoc(), macroRef.getMacroName(),
1274+
macroRef.getMacroNameLoc(), genericArgsRange.Start,
1275+
macroRef.getGenericArgs(), genericArgsRange.End,
1276+
argList, roles);
12791277
}
1280-
auto *call = CallExpr::createImplicit(ctx, callee, args);
12811278

1282-
Expr *result = call;
1279+
Expr *result = macroExpansion;
12831280
TypeChecker::typeCheckExpression(result, dc);
12841281

1285-
if (auto *fn = dyn_cast<DeclRefExpr>(call->getFn()))
1286-
if (auto *macro = dyn_cast<MacroDecl>(fn->getDecl()))
1287-
return macro;
1282+
auto macroDeclRef = macroExpansion->getMacroRef();
1283+
if (auto *macroDecl = dyn_cast_or_null<MacroDecl>(macroDeclRef.getDecl()))
1284+
return macroDecl;
12881285

12891286
// If we couldn't resolve a macro decl, the attribute is invalid.
12901287
if (auto *attr = macroRef.getAttr())
12911288
attr->setInvalid();
1289+
12921290
return nullptr;
12931291
}

test/Macros/attached_macros_diags.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct SkipNestedType {
4040
}
4141

4242
struct TestMacroArgs {
43-
@m1("extra arg") struct Args1 {} // expected-error{{argument passed to call that takes no arguments}}
43+
@m1("extra arg") struct Args1 {} // expected-error{{argument passed to macro expansion that takes no arguments}}
4444

4545
@m2(10) struct Args2 {}
4646

0 commit comments

Comments
 (0)