mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Macros] Separate determining whether a custom attribute is a macro and
type checking macro attribute arguments into two different requests.
This commit is contained in:
@@ -3041,14 +3041,40 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
|
||||
parsedGenericParams->getRAngleLoc());
|
||||
}
|
||||
|
||||
void swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc,
|
||||
llvm::TinyPtrVector<ValueDecl *> ¯os) {
|
||||
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
|
||||
if (!identTypeRepr)
|
||||
return;
|
||||
|
||||
// Look for macros at module scope. They can only occur at module scope, and
|
||||
// we need to be sure not to trigger name lookup into type contexts along
|
||||
// the way.
|
||||
auto moduleScopeDC = dc->getModuleScopeContext();
|
||||
ASTContext &ctx = moduleScopeDC->getASTContext();
|
||||
UnqualifiedLookupDescriptor descriptor(
|
||||
identTypeRepr->getNameRef(), moduleScopeDC
|
||||
);
|
||||
auto lookup = evaluateOrDefault(
|
||||
ctx.evaluator, UnqualifiedLookupRequest{descriptor}, {});
|
||||
for (const auto &result : lookup.allResults()) {
|
||||
// Only keep attached macros, which can be spelled as custom attributes.
|
||||
if (auto macro = dyn_cast<MacroDecl>(result.getValueDecl()))
|
||||
if (isAttachedMacro(macro->getMacroRoles()))
|
||||
macros.push_back(macro);
|
||||
}
|
||||
}
|
||||
|
||||
MacroOrNominalTypeDecl
|
||||
CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
|
||||
CustomAttr *attr, DeclContext *dc) const {
|
||||
// Look for names at module scope, so we don't trigger name lookup for
|
||||
// nested scopes. At this point, we're looking to see whether there are
|
||||
// any suitable macros.
|
||||
if (auto macro = findMacroForCustomAttr(attr, dc))
|
||||
return macro.getValue();
|
||||
llvm::TinyPtrVector<ValueDecl *> macros;
|
||||
findMacroForCustomAttr(attr, dc, macros);
|
||||
if (!macros.empty())
|
||||
return nullptr;
|
||||
|
||||
// Find the types referenced by the custom attribute.
|
||||
auto &ctx = dc->getASTContext();
|
||||
|
||||
Reference in New Issue
Block a user