Skip to content

Commit 1d6a547

Browse files
authored
Merge pull request #63247 from rxwei/resolve-macro-request
2 parents c9ce3a9 + 7d6994a commit 1d6a547

File tree

10 files changed

+200
-97
lines changed

10 files changed

+200
-97
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ 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+
5761
/// Retrieve the string form of the given macro role, as written on the
5862
/// corresponding attribute.
5963
StringRef getMacroRoleString(MacroRole role);
@@ -62,10 +66,14 @@ StringRef getMacroRoleString(MacroRole role);
6266
/// written in the source code with the `#` syntax.
6367
bool isFreestandingMacro(MacroRoles contexts);
6468

69+
MacroRoles getFreestandingMacroRoles();
70+
6571
/// Whether a macro with the given set of macro contexts is attached, i.e.,
6672
/// written in the source code as an attribute with the `@` syntax.
6773
bool isAttachedMacro(MacroRoles contexts);
6874

75+
MacroRoles getAttachedMacroRoles();
76+
6977
enum class MacroIntroducedDeclNameKind {
7078
Named,
7179
Overloaded,

include/swift/AST/TypeCheckRequests.h

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,10 +3128,53 @@ 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+
31313173
/// Resolve a given custom attribute to an attached macro declaration.
3132-
class ResolveAttachedMacroRequest
3133-
: public SimpleRequest<ResolveAttachedMacroRequest,
3134-
MacroDecl *(CustomAttr *, DeclContext *),
3174+
class ResolveMacroRequest
3175+
: public SimpleRequest<ResolveMacroRequest,
3176+
MacroDecl *(UnresolvedMacroReference, MacroRoles,
3177+
DeclContext *),
31353178
RequestFlags::Cached> {
31363179
public:
31373180
using SimpleRequest::SimpleRequest;
@@ -3140,7 +3183,8 @@ class ResolveAttachedMacroRequest
31403183
friend SimpleRequest;
31413184

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

31453189
public:
31463190
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, ResolveAttachedMacroRequest,
343+
SWIFT_REQUEST(TypeChecker, ResolveMacroRequest,
344344
MacroDecl *(CustomAttr *, DeclContext *),
345345
Cached, NoLocationInfo)
346346
SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,

lib/AST/Attr.cpp

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

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

23622363
return macroDecl != nullptr;

lib/AST/Decl.cpp

Lines changed: 9 additions & 1 deletion
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-
ResolveAttachedMacroRequest{customAttr, dc},
387+
ResolveMacroRequest{customAttr, getAttachedMacroRoles(), dc},
388388
nullptr);
389389

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

9780+
MacroRoles swift::getFreestandingMacroRoles() {
9781+
return freestandingMacroRoles;
9782+
}
9783+
97809784
bool swift::isAttachedMacro(MacroRoles contexts) {
97819785
return bool(contexts & attachedMacroRoles);
97829786
}
97839787

9788+
MacroRoles swift::getAttachedMacroRoles() {
9789+
return attachedMacroRoles;
9790+
}
9791+
97849792
MacroDecl::MacroDecl(
97859793
SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
97869794
GenericParamList *genericParams,

lib/Sema/TypeCheckAttr.cpp

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

lib/Sema/TypeCheckDecl.cpp

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

16031603
SmallVector<MacroDecl *, 1>
16041604
TypeChecker::lookupMacros(DeclContext *dc, DeclNameRef macroName,
1605-
SourceLoc loc, MacroRoles contexts) {
1606-
auto result = lookupUnqualified(dc, DeclNameRef(macroName), loc,
1607-
(defaultUnqualifiedLookupOptions |
1608-
NameLookupFlags::IncludeOuterResults));
1605+
SourceLoc loc, MacroRoles roles) {
16091606
SmallVector<MacroDecl *, 1> choices;
1610-
for (const auto &found : result.allResults())
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())
16111613
if (auto macro = dyn_cast<MacroDecl>(found.getValueDecl()))
1612-
if (contexts.contains(macro->getMacroRoles()))
1614+
if (roles.contains(macro->getMacroRoles()))
16131615
choices.push_back(macro);
16141616
return choices;
16151617
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,9 +3685,12 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
36853685
}
36863686
// Resolve macro candidates.
36873687
MacroDecl *macro;
3688-
// Non-call declaration macros cannot be overloaded.
3689-
auto *args = MED->getArgs();
3690-
if (!args) {
3688+
if (auto *args = MED->getArgs()) {
3689+
macro = evaluateOrDefault(
3690+
ctx.evaluator, ResolveMacroRequest{MED, MacroRole::Declaration, dc},
3691+
nullptr);
3692+
}
3693+
else {
36913694
if (foundMacros.size() > 1) {
36923695
MED->diagnose(diag::ambiguous_decl_ref, MED->getMacro())
36933696
.highlight(MED->getMacroLoc().getSourceRange());
@@ -3697,63 +3700,8 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
36973700
}
36983701
macro = foundMacros.front();
36993702
}
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-
}
3703+
if (!macro)
3704+
return {};
37573705
MED->setMacroRef(macro);
37583706

37593707
// Expand the macro.

0 commit comments

Comments
 (0)