Skip to content

Commit 0ca5b8a

Browse files
committed
[NFC] Move findMacroForCustomAttr to TypeCheckMacros.cpp.
1 parent ac86aed commit 0ca5b8a

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ bool macroIntroducedNameRequiresArgument(MacroIntroducedDeclNameKind kind);
6666
StringRef getMacroIntroducedDeclNameString(
6767
MacroIntroducedDeclNameKind kind);
6868

69+
class MacroDecl;
70+
class CustomAttr;
71+
72+
/// Perform lookup to determine whether the given custom attribute refers to
73+
/// a macro declaration, and return that macro declaration.
74+
MacroDecl *findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc);
75+
6976
class MacroIntroducedDeclName {
7077
public:
7178
using Kind = MacroIntroducedDeclNameKind;

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/ImportCache.h"
2727
#include "swift/AST/Initializer.h"
2828
#include "swift/AST/LazyResolver.h"
29+
#include "swift/AST/MacroDeclaration.h"
2930
#include "swift/AST/ModuleNameLookup.h"
3031
#include "swift/AST/NameLookupRequests.h"
3132
#include "swift/AST/ParameterList.h"
@@ -3040,48 +3041,6 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
30403041
parsedGenericParams->getRAngleLoc());
30413042
}
30423043

3043-
/// Perform lookup to determine whether the given custom attribute refers to
3044-
/// a macro declaration, and return that macro declaration.
3045-
static MacroDecl *findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
3046-
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
3047-
if (!identTypeRepr)
3048-
return nullptr;
3049-
3050-
// Look for macros at module scope. They can only occur at module scope, and
3051-
// we need to be sure not to trigger name lookup into type contexts along
3052-
// the way.
3053-
llvm::TinyPtrVector<MacroDecl *> macros;
3054-
auto moduleScopeDC = dc->getModuleScopeContext();
3055-
ASTContext &ctx = moduleScopeDC->getASTContext();
3056-
UnqualifiedLookupDescriptor descriptor(
3057-
identTypeRepr->getNameRef(), moduleScopeDC
3058-
);
3059-
auto lookup = evaluateOrDefault(
3060-
ctx.evaluator, UnqualifiedLookupRequest{descriptor}, {});
3061-
for (const auto &result : lookup.allResults()) {
3062-
// Only keep attached macros, which can be spelled as custom attributes.
3063-
if (auto macro = dyn_cast<MacroDecl>(result.getValueDecl()))
3064-
if (isAttachedMacro(macro->getMacroRoles()))
3065-
macros.push_back(macro);
3066-
}
3067-
3068-
if (macros.empty())
3069-
return nullptr;
3070-
3071-
if (macros.size() > 1) {
3072-
ctx.Diags.diagnose(attr->getLocation(), diag::ambiguous_macro_reference,
3073-
identTypeRepr->getNameRef().getFullName());
3074-
3075-
for (auto macro : macros) {
3076-
macro->diagnose(
3077-
diag::kind_declname_declared_here, macro->getDescriptiveKind(),
3078-
macro->getName());
3079-
}
3080-
}
3081-
3082-
return macros.front();
3083-
}
3084-
30853044
MacroOrNominalTypeDecl
30863045
CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
30873046
CustomAttr *attr, DeclContext *dc) const {

lib/Sema/TypeCheckMacros.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,3 +1082,44 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10821082

10831083
return addedAttributes;
10841084
}
1085+
1086+
MacroDecl *
1087+
swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
1088+
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
1089+
if (!identTypeRepr)
1090+
return nullptr;
1091+
1092+
// Look for macros at module scope. They can only occur at module scope, and
1093+
// we need to be sure not to trigger name lookup into type contexts along
1094+
// the way.
1095+
llvm::TinyPtrVector<MacroDecl *> macros;
1096+
auto moduleScopeDC = dc->getModuleScopeContext();
1097+
ASTContext &ctx = moduleScopeDC->getASTContext();
1098+
UnqualifiedLookupDescriptor descriptor(
1099+
identTypeRepr->getNameRef(), moduleScopeDC
1100+
);
1101+
auto lookup = evaluateOrDefault(
1102+
ctx.evaluator, UnqualifiedLookupRequest{descriptor}, {});
1103+
for (const auto &result : lookup.allResults()) {
1104+
// Only keep attached macros, which can be spelled as custom attributes.
1105+
if (auto macro = dyn_cast<MacroDecl>(result.getValueDecl()))
1106+
if (isAttachedMacro(macro->getMacroRoles()))
1107+
macros.push_back(macro);
1108+
}
1109+
1110+
if (macros.empty())
1111+
return nullptr;
1112+
1113+
if (macros.size() > 1) {
1114+
ctx.Diags.diagnose(attr->getLocation(), diag::ambiguous_macro_reference,
1115+
identTypeRepr->getNameRef().getFullName());
1116+
1117+
for (auto macro : macros) {
1118+
macro->diagnose(
1119+
diag::kind_declname_declared_here, macro->getDescriptiveKind(),
1120+
macro->getName());
1121+
}
1122+
}
1123+
1124+
return macros.front();
1125+
}

0 commit comments

Comments
 (0)