[SourceKit] Implement macro expansion for (some) attached macros.

Extend the macro-expansion refactoring to work with member and
member-attribute attached macros. These expansions can return several
different changes, e.g., adding new members, sprinkling member
attributes around, and so on.
This commit is contained in:
Doug Gregor
2023-02-10 22:44:25 -08:00
parent 7ea0e3f096
commit 96380624db
6 changed files with 221 additions and 27 deletions

View File

@@ -19,6 +19,7 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeRepr.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Defer.h"
@@ -691,9 +692,23 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
return true;
}
}
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
if (auto *Repr = customAttr->getTypeRepr()) {
if (!Repr->walk(*this))
// If this attribute resolves to a macro, index that.
ASTContext &ctx = D->getASTContext();
ResolveMacroRequest req{const_cast<CustomAttr *>(customAttr),
getAttachedMacroRoles(),
D->getInnermostDeclContext()};
if (auto macroDecl = evaluateOrDefault(ctx.evaluator, req, nullptr)) {
Type macroRefType = macroDecl->getDeclaredInterfaceType();
if (!passReference(
macroDecl, macroRefType, DeclNameLoc(Repr->getStartLoc()),
ReferenceMetaData(SemaReferenceKind::DeclRef, None,
/*isImplicit=*/false,
std::make_pair(customAttr, D))))
return false;
} else if (!Repr->walk(*this))
return false;
}
if (auto *SemaInit = customAttr->getSemanticInit()) {