@@ -246,6 +246,10 @@ struct ASTContext::Implementation {
246246 // Declare cached declarations for each of the known declarations.
247247#define FUNC_DECL (Name, Id ) FuncDecl *Get##Name = nullptr ;
248248#include " swift/AST/KnownDecls.def"
249+
250+ // Declare cached declarations for each of the known declarations.
251+ #define KNOWN_SDK_FUNC_DECL (Module, Name, Id ) FuncDecl *Get##Name = nullptr ;
252+ #include " swift/AST/KnownSDKDecls.def"
249253
250254 // / func <Int, Int) -> Bool
251255 FuncDecl *LessThanIntDecl = nullptr ;
@@ -699,10 +703,10 @@ Identifier ASTContext::getIdentifier(StringRef Str) const {
699703 return Identifier (I->getKeyData ());
700704}
701705
702- void ASTContext::lookupInSwiftModule (
703- StringRef name ,
704- SmallVectorImpl<ValueDecl *> &results) const {
705- ModuleDecl *M = getStdlibModule ();
706+ void ASTContext::lookupInModule (
707+ ModuleDecl *M ,
708+ StringRef name,
709+ SmallVectorImpl<ValueDecl *> &results) const {
706710 if (!M)
707711 return ;
708712
@@ -711,6 +715,12 @@ void ASTContext::lookupInSwiftModule(
711715 M->lookupValue (identifier, NLKind::UnqualifiedLookup, results);
712716}
713717
718+ void ASTContext::lookupInSwiftModule (
719+ StringRef name,
720+ SmallVectorImpl<ValueDecl *> &results) const {
721+ lookupInModule (getStdlibModule (), name, results);
722+ }
723+
714724FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection () const {
715725 if (getImpl ().PlusFunctionOnRangeReplaceableCollection ) {
716726 return getImpl ().PlusFunctionOnRangeReplaceableCollection ;
@@ -1033,16 +1043,24 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
10331043 return nullptr ;
10341044}
10351045
1036- // / Find the implementation for the given "intrinsic" library function.
1046+ // / Find the implementation for the given "intrinsic" library function,
1047+ // / in the passed in module.
10371048static FuncDecl *findLibraryIntrinsic (const ASTContext &ctx,
1049+ ModuleDecl *M,
10381050 StringRef name) {
10391051 SmallVector<ValueDecl *, 1 > results;
1040- ctx.lookupInSwiftModule ( name, results);
1052+ ctx.lookupInModule (M, name, results);
10411053 if (results.size () == 1 )
10421054 return dyn_cast_or_null<FuncDecl>(results.front ());
10431055 return nullptr ;
10441056}
10451057
1058+ // / Find the implementation for the given "intrinsic" library function.
1059+ static FuncDecl *findLibraryIntrinsic (const ASTContext &ctx,
1060+ StringRef name) {
1061+ return findLibraryIntrinsic (ctx, ctx.getStdlibModule (), name);
1062+ }
1063+
10461064// / Returns the type of an intrinsic function if it is not generic, otherwise
10471065// / returns nullptr.
10481066static FunctionType *
@@ -1492,7 +1510,7 @@ ASTContext::associateInfixOperators(PrecedenceGroupDecl *left,
14921510}
14931511
14941512// Find library intrinsic function.
1495- static FuncDecl *findLibraryFunction (const ASTContext &ctx, FuncDecl *&cache,
1513+ static FuncDecl *findLibraryFunction (const ASTContext &ctx, FuncDecl *&cache,
14961514 StringRef name) {
14971515 if (cache) return cache;
14981516
@@ -1501,12 +1519,33 @@ static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
15011519 return cache;
15021520}
15031521
1504- #define FUNC_DECL (Name, Id ) \
1505- FuncDecl *ASTContext::get##Name() const { \
1522+ // Find library intrinsic function in passed in module
1523+ static FuncDecl *findLibraryFunction (const ASTContext &ctx,
1524+ ModuleDecl *M, FuncDecl *&cache,
1525+ StringRef name) {
1526+ if (cache) return cache;
1527+
1528+ // Look for a generic function.
1529+ cache = findLibraryIntrinsic (ctx, M, name);
1530+ return cache;
1531+ }
1532+
1533+ #define FUNC_DECL (Name, Id ) \
1534+ FuncDecl *ASTContext::get##Name() const { \
15061535 return findLibraryFunction (*this , getImpl ().Get ##Name, Id); \
15071536}
15081537#include " swift/AST/KnownDecls.def"
15091538
1539+ #define KNOWN_SDK_FUNC_DECL (Module, Name, Id ) \
1540+ FuncDecl *ASTContext::get##Name() const { \
1541+ if (ModuleDecl *M = getLoadedModule (Id_##Module)) { \
1542+ return findLibraryFunction (*this , M, getImpl ().Get ##Name, Id); \
1543+ } else { \
1544+ return findLibraryFunction (*this , getImpl ().Get ##Name, Id); \
1545+ } \
1546+ }
1547+ #include " swift/AST/KnownSDKDecls.def"
1548+
15101549bool ASTContext::hasOptionalIntrinsics () const {
15111550 return getOptionalDecl () &&
15121551 getOptionalSomeDecl () &&
0 commit comments