Skip to content

Commit cf5888a

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 cf5888a

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-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 function object for filtering out decls that are not available
1292+
/// during lowering.
1293+
template <typename T>
1294+
struct AvailableDuringLoweringDeclFilter {
1295+
llvm::Optional<T *> operator()(T *decl) const {
1296+
if (decl->isAvailableDuringLowering())
1297+
return decl;
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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ namespace swift {
8585
class StructDecl;
8686
class AccessorDecl;
8787

88+
template <typename T>
89+
struct AvailableDuringLoweringDeclFilter;
90+
8891
namespace serialization {
8992
using DeclID = llvm::PointerEmbeddedInt<unsigned, 31>;
9093
}
@@ -865,12 +868,11 @@ class IterableDeclContext {
865868
/// on.
866869
DeclRange getMembers() const;
867870

868-
/// Retrieve the current set of members in this context, without triggering the
869-
/// creation of new members via code synthesis, macro expansion, etc.
871+
/// Retrieve the current set of members in this context, without triggering
872+
/// the creation of new members via code synthesis, macro expansion, etc.
870873
///
871874
/// 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
875+
/// producing operations have been done earlier.
874876
DeclRange getCurrentMembers() const;
875877

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

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

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,12 @@ ArrayRef<Decl *> IterableDeclContext::getABIMembers() const {
930930
ArrayRef<Decl *>());
931931
}
932932

933+
IterableDeclContext::DeclsForLowering
934+
IterableDeclContext::getMembersForLowering() const {
935+
return DeclsForLowering(getABIMembers(),
936+
AvailableDuringLoweringDeclFilter<Decl>());
937+
}
938+
933939
ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
934940
ASTContext &ctx = getASTContext();
935941
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)