Skip to content

Commit e6622ad

Browse files
committed
Add KnownSDKDecls and findLibraryFunction for other modules
1 parent 6008b32 commit e6622ad

File tree

6 files changed

+111
-10
lines changed

6 files changed

+111
-10
lines changed

include/swift/AST/ASTContext.h

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

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

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

599+
/// Look for the declaration with the given name within the
600+
/// passed in module.
601+
void lookupInModule(ModuleDecl *M, StringRef name,
602+
SmallVectorImpl<ValueDecl *> &results) const;
603+
594604
/// Look for the declaration with the given name within the
595605
/// Swift module.
596606
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
@@ -245,6 +245,10 @@ struct ASTContext::Implementation {
245245
// Declare cached declarations for each of the known declarations.
246246
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
247247
#include "swift/AST/KnownDecls.def"
248+
249+
// Declare cached declarations for each of the known declarations.
250+
#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) FuncDecl *Get##Name = nullptr;
251+
#include "swift/AST/KnownSDKDecls.def"
248252

249253
/// func <Int, Int) -> Bool
250254
FuncDecl *LessThanIntDecl = nullptr;
@@ -694,10 +698,10 @@ Identifier ASTContext::getIdentifier(StringRef Str) const {
694698
return Identifier(I->getKeyData());
695699
}
696700

697-
void ASTContext::lookupInSwiftModule(
698-
StringRef name,
699-
SmallVectorImpl<ValueDecl *> &results) const {
700-
ModuleDecl *M = getStdlibModule();
701+
void ASTContext::lookupInModule(
702+
ModuleDecl *M,
703+
StringRef name,
704+
SmallVectorImpl<ValueDecl *> &results) const {
701705
if (!M)
702706
return;
703707

@@ -706,6 +710,12 @@ void ASTContext::lookupInSwiftModule(
706710
M->lookupValue(identifier, NLKind::UnqualifiedLookup, results);
707711
}
708712

713+
void ASTContext::lookupInSwiftModule(
714+
StringRef name,
715+
SmallVectorImpl<ValueDecl *> &results) const {
716+
lookupInModule(getStdlibModule(), name, results);
717+
}
718+
709719
FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
710720
if (getImpl().PlusFunctionOnRangeReplaceableCollection) {
711721
return getImpl().PlusFunctionOnRangeReplaceableCollection;
@@ -1028,16 +1038,24 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
10281038
return nullptr;
10291039
}
10301040

1031-
/// Find the implementation for the given "intrinsic" library function.
1041+
/// Find the implementation for the given "intrinsic" library function,
1042+
/// in the passed in module.
10321043
static FuncDecl *findLibraryIntrinsic(const ASTContext &ctx,
1044+
ModuleDecl *M,
10331045
StringRef name) {
10341046
SmallVector<ValueDecl *, 1> results;
1035-
ctx.lookupInSwiftModule(name, results);
1047+
ctx.lookupInModule(M, name, results);
10361048
if (results.size() == 1)
10371049
return dyn_cast_or_null<FuncDecl>(results.front());
10381050
return nullptr;
10391051
}
10401052

1053+
/// Find the implementation for the given "intrinsic" library function.
1054+
static FuncDecl *findLibraryIntrinsic(const ASTContext &ctx,
1055+
StringRef name) {
1056+
return findLibraryIntrinsic(ctx, ctx.getStdlibModule(), name);
1057+
}
1058+
10411059
/// Returns the type of an intrinsic function if it is not generic, otherwise
10421060
/// returns nullptr.
10431061
static FunctionType *
@@ -1487,7 +1505,7 @@ ASTContext::associateInfixOperators(PrecedenceGroupDecl *left,
14871505
}
14881506

14891507
// Find library intrinsic function.
1490-
static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
1508+
static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
14911509
StringRef name) {
14921510
if (cache) return cache;
14931511

@@ -1496,12 +1514,33 @@ static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
14961514
return cache;
14971515
}
14981516

1499-
#define FUNC_DECL(Name, Id) \
1500-
FuncDecl *ASTContext::get##Name() const { \
1517+
// Find library intrinsic function in passed in module
1518+
static FuncDecl *findLibraryFunction(const ASTContext &ctx,
1519+
ModuleDecl *M, FuncDecl *&cache,
1520+
StringRef name) {
1521+
if (cache) return cache;
1522+
1523+
// Look for a generic function.
1524+
cache = findLibraryIntrinsic(ctx, M, name);
1525+
return cache;
1526+
}
1527+
1528+
#define FUNC_DECL(Name, Id) \
1529+
FuncDecl *ASTContext::get##Name() const { \
15011530
return findLibraryFunction(*this, getImpl().Get##Name, Id); \
15021531
}
15031532
#include "swift/AST/KnownDecls.def"
15041533

1534+
#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) \
1535+
FuncDecl *ASTContext::get##Name() const { \
1536+
if (ModuleDecl *M = getLoadedModule(Id_##Module)) { \
1537+
return findLibraryFunction(*this, M, getImpl().Get##Name, Id); \
1538+
} else { \
1539+
return findLibraryFunction(*this, getImpl().Get##Name, Id); \
1540+
} \
1541+
}
1542+
#include "swift/AST/KnownSDKDecls.def"
1543+
15051544
bool ASTContext::hasOptionalIntrinsics() const {
15061545
return getOptionalDecl() &&
15071546
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);

test/IRGen/distributed_actor.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -emit-ir %s -swift-version 5 -enable-experimental-distributed | %IRGenFileCheck %s
2+
// REQUIRES: concurrency
3+
// REQUIRES: distributed
4+
5+
import _Distributed
6+
7+
// Type descriptor.
8+
// CHECK-LABEL: @"$s17distributed_actor7MyActorC0B9Transport12_Distributed0dE0_pvpWvd"
9+
10+
@available(SwiftStdlib 5.5, *)
11+
public distributed actor MyActor {
12+
// nothing
13+
}

0 commit comments

Comments
 (0)