Skip to content

Commit aee9578

Browse files
committed
[Sema] Make getDisallowedOriginKind usable to other source files
1 parent 86c7857 commit aee9578

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,47 +1454,40 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
14541454
}
14551455
};
14561456

1457+
/// Returns the kind of origin, implementation-only import or SPI declaration,
1458+
/// that restricts exporting \p decl from the given file and context.
1459+
///
1460+
/// Local variant to swift::getDisallowedOriginKind for downgrade to warnings.
1461+
DisallowedOriginKind
1462+
getDisallowedOriginKind(const Decl *decl,
1463+
const SourceFile &userSF,
1464+
const Decl *userContext,
1465+
DowngradeToWarning &downgradeToWarning) {
1466+
downgradeToWarning = DowngradeToWarning::No;
1467+
ModuleDecl *M = decl->getModuleContext();
1468+
if (userSF.isImportedImplementationOnly(M)) {
1469+
// Temporarily downgrade implementation-only exportability in SPI to
1470+
// a warning.
1471+
if (userContext->isSPI())
1472+
downgradeToWarning = DowngradeToWarning::Yes;
1473+
1474+
// Implementation-only imported, cannot be reexported.
1475+
return DisallowedOriginKind::ImplementationOnly;
1476+
} else if (decl->isSPI() && !userContext->isSPI()) {
1477+
// SPI can only be exported in SPI.
1478+
return userContext->getModuleContext() == M ?
1479+
DisallowedOriginKind::SPILocal :
1480+
DisallowedOriginKind::SPIImported;
1481+
}
1482+
1483+
return DisallowedOriginKind::None;
1484+
};
1485+
14571486
// Diagnose public APIs exposing types that are either imported as
14581487
// implementation-only or declared as SPI.
14591488
class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
14601489
class Diagnoser;
14611490

1462-
// Problematic origin of an exported type.
1463-
//
1464-
// This enum must be kept in sync with
1465-
// diag::decl_from_hidden_module and
1466-
// diag::conformance_from_implementation_only_module.
1467-
enum class DisallowedOriginKind : uint8_t {
1468-
ImplementationOnly,
1469-
SPIImported,
1470-
SPILocal,
1471-
None
1472-
};
1473-
1474-
// If there's an exportability problem with \p typeDecl, get its origin kind.
1475-
static DisallowedOriginKind getDisallowedOriginKind(
1476-
const TypeDecl *typeDecl, const SourceFile &SF, const Decl *context,
1477-
DowngradeToWarning &downgradeToWarning) {
1478-
downgradeToWarning = DowngradeToWarning::No;
1479-
ModuleDecl *M = typeDecl->getModuleContext();
1480-
if (SF.isImportedImplementationOnly(M)) {
1481-
// Temporarily downgrade implementation-only exportability in SPI to
1482-
// a warning.
1483-
if (context->isSPI())
1484-
downgradeToWarning = DowngradeToWarning::Yes;
1485-
1486-
// Implementation-only imported, cannot be reexported.
1487-
return DisallowedOriginKind::ImplementationOnly;
1488-
} else if (typeDecl->isSPI() && !context->isSPI()) {
1489-
// SPI can only be exported in SPI.
1490-
return context->getModuleContext() == M ?
1491-
DisallowedOriginKind::SPILocal :
1492-
DisallowedOriginKind::SPIImported;
1493-
}
1494-
1495-
return DisallowedOriginKind::None;
1496-
};
1497-
14981491
void checkTypeImpl(
14991492
Type type, const TypeRepr *typeRepr, const SourceFile &SF,
15001493
const Decl *context,
@@ -2067,6 +2060,13 @@ static void checkExtensionGenericParamAccess(const ExtensionDecl *ED) {
20672060
ED, ED, desiredAccessScope, userSpecifiedAccess);
20682061
}
20692062

2063+
DisallowedOriginKind swift::getDisallowedOriginKind(const Decl *decl,
2064+
const SourceFile &userSF,
2065+
const Decl *declContext) {
2066+
auto downgradeToWarning = DowngradeToWarning::No;
2067+
return getDisallowedOriginKind(decl, userSF, declContext, downgradeToWarning);
2068+
}
2069+
20702070
void swift::checkAccessControl(Decl *D) {
20712071
if (isa<ValueDecl>(D) || isa<PatternBindingDecl>(D)) {
20722072
AccessControlChecker().visit(D);

lib/Sema/TypeCheckAccess.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
#ifndef TYPECHECKACCESS_H
1818
#define TYPECHECKACCESS_H
1919

20+
#include <cstdint>
21+
2022
namespace swift {
2123

2224
class Decl;
25+
class SourceFile;
2326

2427
/// Performs access-related checks for \p D.
2528
///
@@ -28,6 +31,24 @@ class Decl;
2831
/// itself. Related checks may also be performed.
2932
void checkAccessControl(Decl *D);
3033

34+
// Problematic origin of an exported type.
35+
//
36+
// This enum must be kept in sync with
37+
// diag::decl_from_hidden_module and
38+
// diag::conformance_from_implementation_only_module.
39+
enum class DisallowedOriginKind : uint8_t {
40+
ImplementationOnly,
41+
SPIImported,
42+
SPILocal,
43+
None
44+
};
45+
46+
/// Returns the kind of origin, implementation-only import or SPI declaration,
47+
/// that restricts exporting \p decl from the given file and context.
48+
DisallowedOriginKind getDisallowedOriginKind(const Decl *decl,
49+
const SourceFile &userSF,
50+
const Decl *userContext);
51+
3152
} // end namespace swift
3253

3354
#endif

0 commit comments

Comments
 (0)