[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:
Holly Borla
2023-01-22 16:49:30 -08:00
parent 8221409931
commit 09287a1bd6
8 changed files with 78 additions and 60 deletions

View File

@@ -3041,14 +3041,40 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
parsedGenericParams->getRAngleLoc());
}
void swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc,
llvm::TinyPtrVector<ValueDecl *> &macros) {
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();