Skip to content

Commit 547d85e

Browse files
authored
Revert "[Macros] Unify macro resolution under ResolveMacroRequest" (#63289)
1 parent 8997f7e commit 547d85e

File tree

10 files changed

+97
-200
lines changed

10 files changed

+97
-200
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ enum class MacroRole: uint32_t {
5454
/// The contexts in which a particular macro declaration can be used.
5555
using MacroRoles = OptionSet<MacroRole>;
5656

57-
void simple_display(llvm::raw_ostream &out, MacroRoles roles);
58-
bool operator==(MacroRoles lhs, MacroRoles rhs);
59-
llvm::hash_code hash_value(MacroRoles roles);
60-
6157
/// Retrieve the string form of the given macro role, as written on the
6258
/// corresponding attribute.
6359
StringRef getMacroRoleString(MacroRole role);
@@ -66,14 +62,10 @@ StringRef getMacroRoleString(MacroRole role);
6662
/// written in the source code with the `#` syntax.
6763
bool isFreestandingMacro(MacroRoles contexts);
6864

69-
MacroRoles getFreestandingMacroRoles();
70-
7165
/// Whether a macro with the given set of macro contexts is attached, i.e.,
7266
/// written in the source code as an attribute with the `@` syntax.
7367
bool isAttachedMacro(MacroRoles contexts);
7468

75-
MacroRoles getAttachedMacroRoles();
76-
7769
enum class MacroIntroducedDeclNameKind {
7870
Named,
7971
Overloaded,

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,53 +3128,10 @@ class CheckRedeclarationRequest
31283128
evaluator::SideEffect) const;
31293129
};
31303130

3131-
class UnresolvedMacroReference {
3132-
private:
3133-
llvm::PointerUnion<MacroExpansionDecl *, MacroExpansionExpr *, CustomAttr *>
3134-
pointer;
3135-
3136-
public:
3137-
UnresolvedMacroReference(MacroExpansionDecl *decl) : pointer(decl) {}
3138-
UnresolvedMacroReference(MacroExpansionExpr *expr) : pointer(expr) {}
3139-
UnresolvedMacroReference(CustomAttr *attr) : pointer(attr) {}
3140-
3141-
MacroExpansionDecl *getDecl() const {
3142-
return pointer.dyn_cast<MacroExpansionDecl *>();
3143-
}
3144-
MacroExpansionExpr *getExpr() const {
3145-
return pointer.dyn_cast<MacroExpansionExpr *>();
3146-
}
3147-
CustomAttr *getAttr() const {
3148-
return pointer.dyn_cast<CustomAttr *>();
3149-
}
3150-
void *getOpaqueValue() const {
3151-
return pointer.getOpaqueValue();
3152-
}
3153-
3154-
DeclNameRef getMacroName() const;
3155-
DeclNameLoc getMacroNameLoc() const;
3156-
SourceRange getGenericArgsRange() const;
3157-
ArrayRef<TypeRepr *> getGenericArgs() const;
3158-
ArgumentList *getArgs() const;
3159-
3160-
friend bool operator==(const UnresolvedMacroReference &lhs,
3161-
const UnresolvedMacroReference &rhs) {
3162-
return lhs.getOpaqueValue() == rhs.getOpaqueValue();
3163-
}
3164-
3165-
friend llvm::hash_code hash_value(const UnresolvedMacroReference &ref) {
3166-
return reinterpret_cast<ptrdiff_t>(ref.pointer.getOpaqueValue());
3167-
}
3168-
};
3169-
3170-
void simple_display(llvm::raw_ostream &out,
3171-
const UnresolvedMacroReference &ref);
3172-
31733131
/// Resolve a given custom attribute to an attached macro declaration.
3174-
class ResolveMacroRequest
3175-
: public SimpleRequest<ResolveMacroRequest,
3176-
MacroDecl *(UnresolvedMacroReference, MacroRoles,
3177-
DeclContext *),
3132+
class ResolveAttachedMacroRequest
3133+
: public SimpleRequest<ResolveAttachedMacroRequest,
3134+
MacroDecl *(CustomAttr *, DeclContext *),
31783135
RequestFlags::Cached> {
31793136
public:
31803137
using SimpleRequest::SimpleRequest;
@@ -3183,8 +3140,7 @@ class ResolveMacroRequest
31833140
friend SimpleRequest;
31843141

31853142
MacroDecl *
3186-
evaluate(Evaluator &evaluator, UnresolvedMacroReference macroRef,
3187-
MacroRoles roles, DeclContext *dc) const;
3143+
evaluate(Evaluator &evaluator, CustomAttr *attr, DeclContext *dc) const;
31883144

31893145
public:
31903146
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ SWIFT_REQUEST(TypeChecker, PreCheckResultBuilderRequest,
340340
SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
341341
evaluator::SideEffect(NominalTypeDecl *, ImplicitMemberAction),
342342
Uncached, NoLocationInfo)
343-
SWIFT_REQUEST(TypeChecker, ResolveMacroRequest,
343+
SWIFT_REQUEST(TypeChecker, ResolveAttachedMacroRequest,
344344
MacroDecl *(CustomAttr *, DeclContext *),
345345
Cached, NoLocationInfo)
346346
SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,

lib/AST/Attr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,8 +2356,7 @@ bool CustomAttr::isAttachedMacro(const Decl *decl) const {
23562356

23572357
auto *macroDecl = evaluateOrDefault(
23582358
ctx.evaluator,
2359-
ResolveMacroRequest{const_cast<CustomAttr *>(this),
2360-
getAttachedMacroRoles(), dc},
2359+
ResolveAttachedMacroRequest{const_cast<CustomAttr *>(this), dc},
23612360
nullptr);
23622361

23632362
return macroDecl != nullptr;

lib/AST/Decl.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ void Decl::forEachAttachedMacro(MacroRole role,
384384
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
385385
auto *macroDecl = evaluateOrDefault(
386386
ctx.evaluator,
387-
ResolveMacroRequest{customAttr, getAttachedMacroRoles(), dc},
387+
ResolveAttachedMacroRequest{customAttr, dc},
388388
nullptr);
389389

390390
if (!macroDecl)
@@ -9777,18 +9777,10 @@ bool swift::isFreestandingMacro(MacroRoles contexts) {
97779777
return bool(contexts & freestandingMacroRoles);
97789778
}
97799779

9780-
MacroRoles swift::getFreestandingMacroRoles() {
9781-
return freestandingMacroRoles;
9782-
}
9783-
97849780
bool swift::isAttachedMacro(MacroRoles contexts) {
97859781
return bool(contexts & attachedMacroRoles);
97869782
}
97879783

9788-
MacroRoles swift::getAttachedMacroRoles() {
9789-
return attachedMacroRoles;
9790-
}
9791-
97929784
MacroDecl::MacroDecl(
97939785
SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
97949786
GenericParamList *genericParams,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,8 +3570,7 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
35703570
if (!nominal) {
35713571
// Try resolving an attached macro attribute.
35723572
auto *macro = evaluateOrDefault(
3573-
Ctx.evaluator, ResolveMacroRequest{attr, getAttachedMacroRoles(), dc},
3574-
nullptr);
3573+
Ctx.evaluator, ResolveAttachedMacroRequest{attr, dc}, nullptr);
35753574
if (macro || !attr->isValid())
35763575
return;
35773576

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,16 +1602,14 @@ TypeChecker::lookupPrecedenceGroup(DeclContext *dc, Identifier name,
16021602

16031603
SmallVector<MacroDecl *, 1>
16041604
TypeChecker::lookupMacros(DeclContext *dc, DeclNameRef macroName,
1605-
SourceLoc loc, MacroRoles roles) {
1605+
SourceLoc loc, MacroRoles contexts) {
1606+
auto result = lookupUnqualified(dc, DeclNameRef(macroName), loc,
1607+
(defaultUnqualifiedLookupOptions |
1608+
NameLookupFlags::IncludeOuterResults));
16061609
SmallVector<MacroDecl *, 1> choices;
1607-
auto moduleScopeDC = dc->getModuleScopeContext();
1608-
ASTContext &ctx = moduleScopeDC->getASTContext();
1609-
UnqualifiedLookupDescriptor descriptor(macroName, moduleScopeDC);
1610-
auto lookup = evaluateOrDefault(
1611-
ctx.evaluator, UnqualifiedLookupRequest{descriptor}, {});
1612-
for (const auto &found : lookup.allResults())
1610+
for (const auto &found : result.allResults())
16131611
if (auto macro = dyn_cast<MacroDecl>(found.getValueDecl()))
1614-
if (roles.contains(macro->getMacroRoles()))
1612+
if (contexts.contains(macro->getMacroRoles()))
16151613
choices.push_back(macro);
16161614
return choices;
16171615
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,12 +3685,9 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
36853685
}
36863686
// Resolve macro candidates.
36873687
MacroDecl *macro;
3688-
if (auto *args = MED->getArgs()) {
3689-
macro = evaluateOrDefault(
3690-
ctx.evaluator, ResolveMacroRequest{MED, MacroRole::Declaration, dc},
3691-
nullptr);
3692-
}
3693-
else {
3688+
// Non-call declaration macros cannot be overloaded.
3689+
auto *args = MED->getArgs();
3690+
if (!args) {
36943691
if (foundMacros.size() > 1) {
36953692
MED->diagnose(diag::ambiguous_decl_ref, MED->getMacro())
36963693
.highlight(MED->getMacroLoc().getSourceRange());
@@ -3700,8 +3697,63 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37003697
}
37013698
macro = foundMacros.front();
37023699
}
3703-
if (!macro)
3704-
return {};
3700+
// Call-like macros need to be resolved.
3701+
else {
3702+
using namespace constraints;
3703+
ConstraintSystem cs(dc, ConstraintSystemOptions());
3704+
// Type-check macro arguments.
3705+
for (auto *arg : args->getArgExprs())
3706+
cs.setType(arg, TypeChecker::typeCheckExpression(arg, dc));
3707+
auto choices = map<SmallVector<OverloadChoice, 1>>(
3708+
foundMacros,
3709+
[](MacroDecl *md) {
3710+
return OverloadChoice(Type(), md, FunctionRefKind::SingleApply);
3711+
});
3712+
auto locator = cs.getConstraintLocator(MED);
3713+
auto macroRefType = Type(cs.createTypeVariable(locator, 0));
3714+
cs.addOverloadSet(macroRefType, choices, dc, locator);
3715+
if (MED->getGenericArgsRange().isValid()) {
3716+
// FIXME: Deal with generic args.
3717+
MED->diagnose(diag::macro_unsupported);
3718+
return {};
3719+
}
3720+
auto getMatchingParams = [&](
3721+
ArgumentList *argList,
3722+
SmallVectorImpl<AnyFunctionType::Param> &result) {
3723+
for (auto arg : *argList) {
3724+
ParameterTypeFlags flags;
3725+
auto ty = cs.getType(arg.getExpr()); if (arg.isInOut()) {
3726+
ty = ty->getInOutObjectType();
3727+
flags = flags.withInOut(true);
3728+
}
3729+
if (arg.isConst()) {
3730+
flags = flags.withCompileTimeConst(true);
3731+
}
3732+
result.emplace_back(ty, arg.getLabel(), flags);
3733+
}
3734+
};
3735+
SmallVector<AnyFunctionType::Param, 8> params;
3736+
getMatchingParams(args, params);
3737+
cs.associateArgumentList(locator, args);
3738+
cs.addConstraint(
3739+
ConstraintKind::ApplicableFunction,
3740+
FunctionType::get(params, ctx.getVoidType()),
3741+
macroRefType,
3742+
cs.getConstraintLocator(MED, ConstraintLocator::ApplyFunction));
3743+
// Solve.
3744+
auto solution = cs.solveSingle();
3745+
if (!solution) {
3746+
MED->diagnose(diag::no_overloads_match_exactly_in_call,
3747+
/*reference|call*/false, DescriptiveDeclKind::Macro,
3748+
false, MED->getMacro().getBaseName())
3749+
.highlight(MED->getMacroLoc().getSourceRange());
3750+
for (auto *candidate : foundMacros)
3751+
candidate->diagnose(diag::found_candidate);
3752+
return {};
3753+
}
3754+
auto choice = solution->getOverloadChoice(locator).choice;
3755+
macro = cast<MacroDecl>(choice.getDecl());
3756+
}
37053757
MED->setMacroRef(macro);
37063758

37073759
// Expand the macro.

0 commit comments

Comments
 (0)