Skip to content

Commit 09287a1

Browse files
committed
[Macros] Separate determining whether a custom attribute is a macro and
type checking macro attribute arguments into two different requests.
1 parent 8221409 commit 09287a1

File tree

8 files changed

+78
-60
lines changed

8 files changed

+78
-60
lines changed

include/swift/AST/MacroDeclaration.h

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

69-
class MacroDecl;
7069
class CustomAttr;
7170

7271
/// Perform lookup to determine whether the given custom attribute refers to
73-
/// a macro declaration, and return that macro declaration.
74-
///
75-
/// \Returns \c None if the custom attribute name does not match any macro
76-
/// declarations, \c nullptr if the macro reference has errors in the argument
77-
/// list, or a resolved macro declaration for a valid macro attribute.
78-
Optional<MacroDecl *>
79-
findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc);
72+
/// a macro declaration, and populate the \c macros vector with the lookup
73+
/// results that are attached macros.
74+
void findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc,
75+
llvm::TinyPtrVector<ValueDecl *> &macros);
8076

8177
class MacroIntroducedDeclName {
8278
public:

include/swift/AST/TypeCheckRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,24 @@ class CheckRedeclarationRequest
31283128
evaluator::SideEffect) const;
31293129
};
31303130

3131+
/// Resolve a given custom attribute to an attached macro declaration.
3132+
class ResolveAttachedMacroRequest
3133+
: public SimpleRequest<ResolveAttachedMacroRequest,
3134+
MacroDecl *(CustomAttr *, DeclContext *),
3135+
RequestFlags::Cached> {
3136+
public:
3137+
using SimpleRequest::SimpleRequest;
3138+
3139+
private:
3140+
friend SimpleRequest;
3141+
3142+
MacroDecl *
3143+
evaluate(Evaluator &evaluator, CustomAttr *attr, DeclContext *dc) const;
3144+
3145+
public:
3146+
bool isCached() const { return true; }
3147+
};
3148+
31313149
class ResolveTypeEraserTypeRequest
31323150
: public SimpleRequest<ResolveTypeEraserTypeRequest,
31333151
Type (ProtocolDecl *, TypeEraserAttr *),

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ SWIFT_REQUEST(TypeChecker, PreCheckResultBuilderRequest,
340340
SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
341341
evaluator::SideEffect(NominalTypeDecl *, ImplicitMemberAction),
342342
Uncached, NoLocationInfo)
343+
SWIFT_REQUEST(TypeChecker, ResolveAttachedMacroRequest,
344+
MacroDecl *(CustomAttr *, DeclContext *),
345+
Cached, NoLocationInfo)
343346
SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,
344347
Type(ProtocolDecl *, TypeEraserAttr *),
345348
SeparatelyCached, NoLocationInfo)

lib/AST/Attr.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,15 +2342,12 @@ bool CustomAttr::isAttachedMacro(const Decl *decl) const {
23422342
auto &ctx = decl->getASTContext();
23432343
auto *dc = decl->getInnermostDeclContext();
23442344

2345-
auto attrDecl = evaluateOrDefault(
2345+
auto *macroDecl = evaluateOrDefault(
23462346
ctx.evaluator,
2347-
CustomAttrDeclRequest{const_cast<CustomAttr *>(this), dc},
2347+
ResolveAttachedMacroRequest{const_cast<CustomAttr *>(this), dc},
23482348
nullptr);
23492349

2350-
if (!attrDecl)
2351-
return false;
2352-
2353-
return attrDecl.dyn_cast<MacroDecl *>();
2350+
return macroDecl != nullptr;
23542351
}
23552352

23562353
DeclarationAttr::DeclarationAttr(SourceLoc atLoc, SourceRange range,

lib/AST/NameLookup.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,14 +3041,40 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
30413041
parsedGenericParams->getRAngleLoc());
30423042
}
30433043

3044+
void swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc,
3045+
llvm::TinyPtrVector<ValueDecl *> &macros) {
3046+
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
3047+
if (!identTypeRepr)
3048+
return;
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+
auto moduleScopeDC = dc->getModuleScopeContext();
3054+
ASTContext &ctx = moduleScopeDC->getASTContext();
3055+
UnqualifiedLookupDescriptor descriptor(
3056+
identTypeRepr->getNameRef(), moduleScopeDC
3057+
);
3058+
auto lookup = evaluateOrDefault(
3059+
ctx.evaluator, UnqualifiedLookupRequest{descriptor}, {});
3060+
for (const auto &result : lookup.allResults()) {
3061+
// Only keep attached macros, which can be spelled as custom attributes.
3062+
if (auto macro = dyn_cast<MacroDecl>(result.getValueDecl()))
3063+
if (isAttachedMacro(macro->getMacroRoles()))
3064+
macros.push_back(macro);
3065+
}
3066+
}
3067+
30443068
MacroOrNominalTypeDecl
30453069
CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
30463070
CustomAttr *attr, DeclContext *dc) const {
30473071
// Look for names at module scope, so we don't trigger name lookup for
30483072
// nested scopes. At this point, we're looking to see whether there are
30493073
// any suitable macros.
3050-
if (auto macro = findMacroForCustomAttr(attr, dc))
3051-
return macro.getValue();
3074+
llvm::TinyPtrVector<ValueDecl *> macros;
3075+
findMacroForCustomAttr(attr, dc, macros);
3076+
if (!macros.empty())
3077+
return nullptr;
30523078

30533079
// Find the types referenced by the custom attribute.
30543080
auto &ctx = dc->getASTContext();

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3558,19 +3558,20 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
35583558
auto found = evaluateOrDefault(
35593559
Ctx.evaluator, CustomAttrDeclRequest{attr, dc}, nullptr);
35603560

3561-
// FIXME: deal with macros.
35623561
NominalTypeDecl *nominal = nullptr;
35633562
if (found) {
3564-
// FIXME: Do full checking of the macro arguments here by turning it into
3565-
// a macro expansion expression (?).
3566-
if (found.is<MacroDecl *>())
3567-
return;
3568-
35693563
nominal = found.dyn_cast<NominalTypeDecl *>();
35703564
}
35713565

3572-
// Diagnose errors.
35733566
if (!found) {
3567+
// Try resolving an attached macro attribute.
3568+
auto *macro = evaluateOrDefault(
3569+
Ctx.evaluator, ResolveAttachedMacroRequest{attr, dc}, nullptr);
3570+
if (macro || !attr->isValid())
3571+
return;
3572+
3573+
// Diagnose errors.
3574+
35743575
auto typeRepr = attr->getTypeRepr();
35753576

35763577
auto type = TypeResolution::forInterface(dc, TypeResolverContext::CustomAttr,

lib/Sema/TypeCheckMacros.cpp

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,14 @@ bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
318318
auto parentAttrs = parentDecl->getSemanticAttrs();
319319
for (auto customAttrConst: parentAttrs.getAttributes<CustomAttr>()) {
320320
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
321-
auto customAttrDecl = evaluateOrDefault(
321+
auto *macroDecl = evaluateOrDefault(
322322
evaluator,
323-
CustomAttrDeclRequest{
323+
ResolveAttachedMacroRequest{
324324
customAttr,
325325
parentDecl->getInnermostDeclContext()
326326
},
327327
nullptr);
328328

329-
if (!customAttrDecl)
330-
continue;
331-
332-
auto macroDecl = customAttrDecl.dyn_cast<MacroDecl *>();
333329
if (!macroDecl)
334330
continue;
335331

@@ -364,10 +360,9 @@ static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro) {
364360
} else if (auto *macroAttr = sourceFile->getAttachedMacroAttribute()) {
365361
auto *decl = expansion.dyn_cast<Decl *>();
366362
auto &ctx = decl->getASTContext();
367-
auto attrDecl = evaluateOrDefault(ctx.evaluator,
368-
CustomAttrDeclRequest{macroAttr, decl->getDeclContext()},
363+
auto *macroDecl = evaluateOrDefault(ctx.evaluator,
364+
ResolveAttachedMacroRequest{macroAttr, decl->getDeclContext()},
369365
nullptr);
370-
auto *macroDecl = attrDecl.dyn_cast<MacroDecl *>();
371366
if (!macroDecl)
372367
return false;
373368

@@ -1083,32 +1078,16 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10831078
return addedAttributes;
10841079
}
10851080

1086-
Optional<MacroDecl *>
1087-
swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
1088-
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
1089-
if (!identTypeRepr)
1090-
return None;
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.
1081+
MacroDecl *
1082+
ResolveAttachedMacroRequest::evaluate(Evaluator &evaluator,
1083+
CustomAttr *attr,
1084+
DeclContext *dc) const {
1085+
auto &ctx = dc->getASTContext();
10951086
llvm::TinyPtrVector<ValueDecl *> 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-
}
1087+
findMacroForCustomAttr(attr, dc, macros);
11091088

11101089
if (macros.empty())
1111-
return None;
1090+
return nullptr;
11121091

11131092
// Extract macro arguments from the attribute, or create an empty list.
11141093
ArgumentList *attrArgs;
@@ -1121,6 +1100,7 @@ swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
11211100
// Form an `OverloadedDeclRefExpr` with the filtered lookup result above
11221101
// to ensure @freestanding macros are not considered in overload resolution.
11231102
FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
1103+
auto *identTypeRepr = dyn_cast<IdentTypeRepr>(attr->getTypeRepr());
11241104
auto macroRefExpr = new (ctx) OverloadedDeclRefExpr(
11251105
macros, identTypeRepr->getNameLoc(), functionRefKind,
11261106
/*implicit*/true);

lib/Sema/TypeCheckStorage.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,17 +3443,14 @@ StorageImplInfoRequest::evaluate(Evaluator &evaluator,
34433443
// Check for an accessor macro.
34443444
for (auto customAttrConst : storage->getSemanticAttrs().getAttributes<CustomAttr>()) {
34453445
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
3446-
auto decl = evaluateOrDefault(
3446+
auto *macro = evaluateOrDefault(
34473447
evaluator,
3448-
CustomAttrDeclRequest{
3448+
ResolveAttachedMacroRequest{
34493449
customAttr,
34503450
storage->getInnermostDeclContext()
34513451
},
34523452
nullptr);
3453-
if (!decl)
3454-
continue;
34553453

3456-
auto macro = decl.dyn_cast<MacroDecl *>();
34573454
if (!macro)
34583455
continue;
34593456

0 commit comments

Comments
 (0)