Skip to content

Commit 73e5d66

Browse files
authored
ASTContext: Add a utility to retrieve the '+' function decl for RangeReplaceableCollection in the stdlib. NFC (#11964)
1 parent 3b38edd commit 73e5d66

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class ConstraintCheckerArenaRAII {
170170
};
171171

172172
class SILLayout; // From SIL
173-
174173
/// \brief Describes either a nominal type declaration or an extension
175174
/// declaration.
176175
typedef llvm::PointerUnion<NominalTypeDecl *, ExtensionDecl *>
@@ -438,6 +437,9 @@ class ASTContext {
438437
FuncDecl *get##Name(LazyResolver *resolver) const;
439438
#include "swift/AST/KnownDecls.def"
440439

440+
/// Get the '+' function on two RangeReplaceableCollection.
441+
FuncDecl *getPlusFunctionOnRangeReplaceableCollection() const;
442+
441443
/// Check whether the standard library provides all the correct
442444
/// intrinsic support for Optional<T>.
443445
///

include/swift/AST/KnownStdlibTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ KNOWN_STDLIB_TYPE_DECL(Encoder, ProtocolDecl, 1)
7777
KNOWN_STDLIB_TYPE_DECL(Decoder, ProtocolDecl, 1)
7878
KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
7979
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
80+
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)
8081

8182
#undef KNOWN_STDLIB_TYPE_DECL

lib/AST/ASTContext.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ struct ASTContext::Implementation {
129129
DECL_CLASS *NAME##Decl = nullptr;
130130
#include "swift/AST/KnownStdlibTypes.def"
131131

132+
/// The declaration of '+' function for two RangeReplaceableCollection.
133+
FuncDecl *PlusFunctionOnRangeReplaceableCollection = nullptr;
134+
132135
/// The declaration of Swift.Optional<T>.Some.
133136
EnumElementDecl *OptionalSomeDecl = nullptr;
134137

@@ -545,6 +548,30 @@ static NominalTypeDecl *findStdlibType(const ASTContext &ctx, StringRef name,
545548
return nullptr;
546549
}
547550

551+
FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
552+
if (Impl.PlusFunctionOnRangeReplaceableCollection) {
553+
return Impl.PlusFunctionOnRangeReplaceableCollection;
554+
}
555+
// Find all of the declarations with this name in the Swift module.
556+
SmallVector<ValueDecl *, 1> Results;
557+
lookupInSwiftModule("+", Results);
558+
for (auto Result : Results) {
559+
if (auto *FD = dyn_cast<FuncDecl>(Result)) {
560+
if(!FD->getOperatorDecl())
561+
continue;
562+
for (auto Req: FD->getGenericRequirements()) {
563+
if (Req.getKind() == RequirementKind::Conformance &&
564+
Req.getSecondType()->getNominalOrBoundGenericNominal() ==
565+
getRangeReplaceableCollectionDecl()) {
566+
Impl.PlusFunctionOnRangeReplaceableCollection = FD;
567+
}
568+
}
569+
}
570+
}
571+
return Impl.PlusFunctionOnRangeReplaceableCollection;
572+
}
573+
574+
548575
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
549576
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
550577
if (!Impl.NAME##Decl) \

0 commit comments

Comments
 (0)