Skip to content

Commit daa8aac

Browse files
authored
Merge pull request #37740 from ktoso/wip-lookupInModule
2 parents b443118 + 8f77d03 commit daa8aac

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

include/swift/AST/ASTContext.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ class ASTContext final {
521521
FuncDecl *get##Name() const;
522522
#include "swift/AST/KnownDecls.def"
523523

524+
// Declare accessors for the known declarations.
525+
#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) \
526+
FuncDecl *get##Name() const;
527+
#include "swift/AST/KnownSDKDecls.def"
528+
524529
/// Get the '+' function on two RangeReplaceableCollection.
525530
FuncDecl *getPlusFunctionOnRangeReplaceableCollection() const;
526531

@@ -592,6 +597,11 @@ class ASTContext final {
592597
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
593598
FuncDecl *getIsOSVersionAtLeastDecl() const;
594599

600+
/// Look for the declaration with the given name within the
601+
/// passed in module.
602+
void lookupInModule(ModuleDecl *M, StringRef name,
603+
SmallVectorImpl<ValueDecl *> &results) const;
604+
595605
/// Look for the declaration with the given name within the
596606
/// Swift module.
597607
void lookupInSwiftModule(StringRef name,

include/swift/AST/KnownSDKDecls.def

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===--- KnownSDKDecls.def - Compiler decl metaprogramming --*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines macros used for macro-metaprogramming with compiler-known
14+
// declarations, in modules other than the standard library.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef KNOWN_SDK_FUNC_DECL
19+
# define KNOWN_SDK_FUNC_DECL(Module, Name, Id)
20+
#endif
21+
22+
23+
#undef KNOWN_SDK_FUNC_DECL
24+

lib/AST/ASTContext.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
714724
FuncDecl *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.
10371048
static 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.
10481066
static 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+
15101549
bool ASTContext::hasOptionalIntrinsics() const {
15111550
return getOptionalDecl() &&
15121551
getOptionalSomeDecl() &&

lib/SILGen/SILGen.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,23 @@ static FuncDecl *diagnoseMissingIntrinsic(SILGenModule &sgm,
181181

182182
#define FUNC_DECL(NAME, ID) \
183183
FuncDecl *SILGenModule::get##NAME(SILLocation loc) { \
184-
if (auto fn = getASTContext().get##NAME()) \
184+
if (auto fn = getASTContext().get##NAME()) \
185185
return fn; \
186186
return diagnoseMissingIntrinsic(*this, loc, ID); \
187187
}
188188
#include "swift/AST/KnownDecls.def"
189189

190+
#define KNOWN_SDK_FUNC_DECL(MODULE, NAME, ID) \
191+
FuncDecl *SILGenModule::get##NAME(SILLocation loc) { \
192+
if (ModuleDecl *M = getASTContext().getLoadedModule( \
193+
getASTContext().Id_##MODULE)) { \
194+
if (auto fn = getASTContext().get##NAME()) \
195+
return fn; \
196+
} \
197+
return diagnoseMissingIntrinsic(*this, loc, ID); \
198+
}
199+
#include "swift/AST/KnownSDKDecls.def"
200+
190201
ProtocolDecl *SILGenModule::getObjectiveCBridgeable(SILLocation loc) {
191202
if (ObjectiveCBridgeable)
192203
return *ObjectiveCBridgeable;

lib/SILGen/SILGen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
453453
#define FUNC_DECL(NAME, ID) \
454454
FuncDecl *get##NAME(SILLocation loc);
455455
#include "swift/AST/KnownDecls.def"
456+
457+
#define KNOWN_SDK_FUNC_DECL(MODULE, NAME, ID) \
458+
FuncDecl *get##NAME(SILLocation loc);
459+
#include "swift/AST/KnownSDKDecls.def"
456460

457461
/// Retrieve the _ObjectiveCBridgeable protocol definition.
458462
ProtocolDecl *getObjectiveCBridgeable(SILLocation loc);

0 commit comments

Comments
 (0)