Skip to content

Commit d5b74a2

Browse files
committed
NFC: Refactor conveniences for filtering out unavailable decls.
Introduce `AvailableDuringLoweringDeclFilter` which can be composed with `OptionalTransformRange` to implement iterators that filter out unavailable decls.
1 parent 774105b commit d5b74a2

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

include/swift/AST/Decl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,17 @@ void *allocateMemoryForDecl(AllocatorTy &allocator, size_t baseSize,
12881288
return mem;
12891289
}
12901290

1291+
/// A convenience for filtering out decls that are not available during
1292+
/// lowering.
1293+
struct AvailableDuringLoweringDeclFilter {
1294+
AvailableDuringLoweringDeclFilter() {}
1295+
llvm::Optional<Decl *> operator()(Decl *D) const {
1296+
if (D->isAvailableDuringLowering())
1297+
return D;
1298+
return llvm::None;
1299+
}
1300+
};
1301+
12911302
// A private class for forcing exact field layout.
12921303
class alignas(8) _GenericContext {
12931304
// Not really public. See GenericContext.

include/swift/AST/DeclContext.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ namespace swift {
8484
class SerializedTopLevelCodeDecl;
8585
class StructDecl;
8686
class AccessorDecl;
87+
struct AvailableDuringLoweringDeclFilter;
8788

8889
namespace serialization {
8990
using DeclID = llvm::PointerEmbeddedInt<unsigned, 31>;
@@ -865,12 +866,11 @@ class IterableDeclContext {
865866
/// on.
866867
DeclRange getMembers() const;
867868

868-
/// Retrieve the current set of members in this context, without triggering the
869-
/// creation of new members via code synthesis, macro expansion, etc.
869+
/// Retrieve the current set of members in this context, without triggering
870+
/// the creation of new members via code synthesis, macro expansion, etc.
870871
///
871872
/// This operation should only be used in narrow places where any side-effect
872-
/// producing operations have been done earlier. For the most part, this means that
873-
/// it should only be used in the implementation of
873+
/// producing operations have been done earlier.
874874
DeclRange getCurrentMembers() const;
875875

876876
/// Get the members that were syntactically present in the source code,
@@ -884,6 +884,15 @@ class IterableDeclContext {
884884
/// The resulting list of members will be stable across translation units.
885885
ArrayRef<Decl *> getABIMembers() const;
886886

887+
using DeclsForLowering =
888+
OptionalTransformRange<ArrayRef<Decl *>,
889+
AvailableDuringLoweringDeclFilter>;
890+
891+
/// Get all of the members within this context that should be included when
892+
/// lowering to SIL/IR, including any implicitly-synthesized members. The
893+
/// decls returned by \c getABIMembers() are a superset of these decls.
894+
DeclsForLowering getMembersForLowering() const;
895+
887896
/// Get all of the members within this context, including any
888897
/// implicitly-synthesized members.
889898
///

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,16 +1079,6 @@ namespace Lowering {
10791079
/// Objective-C runtime, i.e., +alloc and -dealloc.
10801080
LLVM_LIBRARY_VISIBILITY bool usesObjCAllocator(ClassDecl *theClass);
10811081
} // namespace Lowering
1082-
1083-
/// Apply the given function to each ABI member of \c D skipping the members
1084-
/// that should be skipped according to \c shouldSkipLowering()
1085-
template <typename F>
1086-
void forEachMemberToLower(IterableDeclContext *D, F &&f) {
1087-
for (auto *member : D->getABIMembers()) {
1088-
if (member->isAvailableDuringLowering())
1089-
f(member);
1090-
}
1091-
}
10921082
} // namespace swift
10931083

10941084
#endif

include/swift/SIL/SILVTableVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ template <class T> class SILVTableVisitor {
141141
if (!theClass->hasKnownSwiftImplementation())
142142
return;
143143

144-
forEachMemberToLower(theClass, [&](Decl *member) {
144+
for (Decl *member : theClass->getMembersForLowering()) {
145145
maybeAddMember(member);
146-
});
146+
}
147147
}
148148
};
149149

lib/AST/DeclContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ ArrayRef<Decl *> IterableDeclContext::getABIMembers() const {
930930
ArrayRef<Decl *>());
931931
}
932932

933+
IterableDeclContext::DeclsForLowering
934+
IterableDeclContext::getMembersForLowering() const {
935+
return DeclsForLowering(getABIMembers(), AvailableDuringLoweringDeclFilter());
936+
}
937+
933938
ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
934939
ASTContext &ctx = getASTContext();
935940
return evaluateOrDefault(

lib/SILGen/SILGenType.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,9 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
10981098
void emitType() {
10991099
SGM.emitLazyConformancesForType(theType);
11001100

1101-
forEachMemberToLower(theType, [&](Decl *member) {
1101+
for (Decl *member : theType->getMembersForLowering()) {
11021102
visit(member);
1103-
});
1103+
}
11041104

11051105
// Build a vtable if this is a class.
11061106
if (auto theClass = dyn_cast<ClassDecl>(theType)) {
@@ -1276,9 +1276,9 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
12761276
// @_objcImplementation extension, but we don't actually need to do any of
12771277
// the stuff that it currently does.
12781278

1279-
forEachMemberToLower(e, [&](Decl *member) {
1279+
for (Decl *member : e->getMembersForLowering()) {
12801280
visit(member);
1281-
});
1281+
}
12821282

12831283
// If this is a main-interface @_objcImplementation extension and the class
12841284
// has a synthesized destructor, emit it now.

0 commit comments

Comments
 (0)