Skip to content

Commit af639e5

Browse files
committed
[AST] RuntimeMetadata: Move getRuntimeDiscoverableAttrs to Decl
Reflection metadata attributes could be attached to a `ValueDecl` and `ExtensionDecl`.
1 parent ec3534b commit af639e5

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
859859
return Attrs;
860860
}
861861

862+
/// Retrieve runtime discoverable attributes (if any) associated
863+
/// with this declaration.
864+
ArrayRef<CustomAttr *> getRuntimeDiscoverableAttrs() const;
865+
862866
/// Returns the semantic attributes attached to this declaration,
863867
/// including attributes that are generated as the result of member
864868
/// attribute macro expansion.
@@ -2851,9 +2855,6 @@ class ValueDecl : public Decl {
28512855
GenericParameterReferenceInfo findExistentialSelfReferences(
28522856
Type baseTy, bool treatNonResultCovariantSelfAsInvariant) const;
28532857

2854-
/// Retrieve runtime discoverable attributes (if any) associated
2855-
/// with this declaration.
2856-
ArrayRef<CustomAttr *> getRuntimeDiscoverableAttrs() const;
28572858
/// Retrieve a nominal type declaration backing given runtime discoverable
28582859
/// attribute.
28592860
///

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,15 +3926,15 @@ class ExternalMacroDefinitionRequest
39263926

39273927
class GetRuntimeDiscoverableAttributes
39283928
: public SimpleRequest<GetRuntimeDiscoverableAttributes,
3929-
ArrayRef<CustomAttr *>(ValueDecl *),
3929+
ArrayRef<CustomAttr *>(Decl *),
39303930
RequestFlags::Cached> {
39313931
public:
39323932
using SimpleRequest::SimpleRequest;
39333933

39343934
private:
39353935
friend SimpleRequest;
39363936

3937-
ArrayRef<CustomAttr *> evaluate(Evaluator &evaluator, ValueDecl *decl) const;
3937+
ArrayRef<CustomAttr *> evaluate(Evaluator &evaluator, Decl *decl) const;
39383938

39393939
public:
39403940
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ SWIFT_REQUEST(TypeChecker, SynthesizeRuntimeMetadataAttrGeneratorBody,
468468
BraceStmt *(CustomAttr *, ValueDecl *),
469469
Cached, NoLocationInfo)
470470
SWIFT_REQUEST(TypeChecker, GetRuntimeDiscoverableAttributes,
471-
ArrayRef<CustomAttr *>(ValueDecl *),
471+
ArrayRef<CustomAttr *>(Decl *),
472472
Cached, NoLocationInfo)
473473
SWIFT_REQUEST(TypeChecker, LocalDiscriminatorsRequest,
474474
unsigned(DeclContext *),

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,11 @@ bool isNonSendableExtension(const Decl *D) {
18831883
bool ShouldPrintChecker::shouldPrint(const Decl *D,
18841884
const PrintOptions &Options) {
18851885
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
1886+
// Always print unavilable extensions that carry reflection
1887+
// metadata attributes.
1888+
if (!ED->getRuntimeDiscoverableAttrs().empty())
1889+
return true;
1890+
18861891
if (Options.printExtensionContentAsMembers(ED))
18871892
return false;
18881893
}

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9888,8 +9888,8 @@ ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
98889888
return nominal;
98899889
}
98909890

9891-
ArrayRef<CustomAttr *> ValueDecl::getRuntimeDiscoverableAttrs() const {
9892-
auto *mutableSelf = const_cast<ValueDecl *>(this);
9891+
ArrayRef<CustomAttr *> Decl::getRuntimeDiscoverableAttrs() const {
9892+
auto *mutableSelf = const_cast<Decl *>(this);
98939893
return evaluateOrDefault(getASTContext().evaluator,
98949894
GetRuntimeDiscoverableAttributes{mutableSelf},
98959895
nullptr);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7463,7 +7463,6 @@ static void forEachCustomAttribute(
74637463
auto *nominal = evaluateOrDefault(
74647464
ctx.evaluator,
74657465
CustomAttrNominalRequest{mutableAttr, decl->getDeclContext()}, nullptr);
7466-
74677466
if (!nominal)
74687467
continue;
74697468

@@ -7474,15 +7473,15 @@ static void forEachCustomAttribute(
74747473

74757474
ArrayRef<CustomAttr *>
74767475
GetRuntimeDiscoverableAttributes::evaluate(Evaluator &evaluator,
7477-
ValueDecl *decl) const {
7476+
Decl *decl) const {
74787477
auto &ctx = decl->getASTContext();
74797478

74807479
llvm::SmallMapVector<NominalTypeDecl *, CustomAttr *, 4> attrs;
74817480

74827481
enum class GatheringMode { Direct, Inference };
74837482

74847483
auto gatherRuntimeAttrsOnDecl =
7485-
[&](ValueDecl *decl,
7484+
[&](Decl *decl,
74867485
llvm::SmallMapVector<NominalTypeDecl *, CustomAttr *, 4> &attrs,
74877486
GatheringMode mode) {
74887487
forEachCustomAttribute<RuntimeMetadataAttr>(
@@ -7514,6 +7513,19 @@ GetRuntimeDiscoverableAttributes::evaluate(Evaluator &evaluator,
75147513
return copy;
75157514
};
75167515

7516+
// Gather reflection metadata attributes only if this extension is:
7517+
// - unavailable;
7518+
// - unconstrained;
7519+
// - declared in the same module as the extended type.
7520+
if (auto *ED = dyn_cast<ExtensionDecl>(decl)) {
7521+
if (!AvailableAttr::isUnavailable(ED))
7522+
return copyAttrs(attrs);
7523+
7524+
if (ED->isConstrainedExtension() ||
7525+
ED->getParentModule() != decl->getDeclContext()->getParentModule())
7526+
return copyAttrs(attrs);
7527+
}
7528+
75177529
// First, gather all of the runtime attributes directly on the decl.
75187530
gatherRuntimeAttrsOnDecl(decl, attrs, GatheringMode::Direct);
75197531

0 commit comments

Comments
 (0)