Skip to content

Commit 48893ea

Browse files
committed
[Sema] Introduce utility service ValueDecl::getImportAccessFrom
1 parent 41e371b commit 48893ea

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,16 @@ class ValueDecl : public Decl {
27832783
bool forConformance = false,
27842784
bool allowUsableFromInline = false) const;
27852785

2786+
using ImportAccessLevel = llvm::Optional<AttributedImport<ImportedModule>>;
2787+
2788+
/// Returns the import that may restrict the access to this decl
2789+
/// from \p useDC.
2790+
///
2791+
/// If this decl and \p useDC are from the same module it returns
2792+
/// \c llvm::None. If there are many imports, it returns the most
2793+
/// permissive one.
2794+
ImportAccessLevel getImportAccessFrom(const DeclContext *useDC) const;
2795+
27862796
/// Returns whether this declaration should be treated as \c open from
27872797
/// \p useDC. This is very similar to #getFormalAccess, but takes
27882798
/// \c \@testable into account.

lib/AST/Decl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,16 @@ bool ValueDecl::isAccessibleFrom(const DeclContext *useDC,
43264326
[&]() { return getFormalAccess(); });
43274327
}
43284328

4329+
ImportAccessLevel ValueDecl::getImportAccessFrom(const DeclContext *useDC) const {
4330+
ModuleDecl *Mod = getModuleContext();
4331+
if (useDC && useDC->getParentModule() != Mod) {
4332+
if (auto useSF = useDC->getParentSourceFile()) {
4333+
return useSF->getImportAccessLevel(Mod);
4334+
}
4335+
}
4336+
return llvm::None;
4337+
}
4338+
43294339
bool AbstractStorageDecl::isSetterAccessibleFrom(const DeclContext *DC,
43304340
bool forConformance) const {
43314341
assert(isSettable(DC));

0 commit comments

Comments
 (0)