Skip to content

Commit aaaef34

Browse files
committed
[Sema] Intro @_spiOnly attribute and import filter
Introduce the attribute and basic import filter logic.
1 parent cf8f61d commit aaaef34

File tree

8 files changed

+29
-3
lines changed

8 files changed

+29
-3
lines changed

include/swift/AST/Attr.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,11 @@ SIMPLE_DECL_ATTR(typeWrapper, TypeWrapper,
768768
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
769769
134)
770770

771+
SIMPLE_DECL_ATTR(_spiOnly, SPIOnly,
772+
OnImport | UserInaccessible |
773+
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
774+
135)
775+
771776
// If you're adding a new underscored attribute here, please document it in
772777
// docs/ReferenceGuides/UnderscoredAttributes.md.
773778

include/swift/AST/Import.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ enum class ImportFlags {
8888
WeakLinked = 0x40,
8989

9090
/// Used for DenseMap.
91-
Reserved = 0x80
91+
Reserved = 0x80,
92+
93+
/// The imported module can only be referenced from SPI decls, or
94+
/// implementation details.
95+
SPIOnly = 0x100
9296
};
9397

9498
/// \see ImportFlags

include/swift/AST/Module.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,13 @@ class ModuleDecl
696696
Default = 1 << 1,
697697
/// Include imports declared with `@_implementationOnly`.
698698
ImplementationOnly = 1 << 2,
699-
/// Include imports of SPIs declared with `@_spi`
699+
/// Include imports of SPIs declared with `@_spi`.
700700
SPIAccessControl = 1 << 3,
701+
/// Include imports declared with `@_spiOnly`.
702+
SPIOnly = 1 << 4,
701703
/// Include imports shadowed by a cross-import overlay. Unshadowed imports
702704
/// are included whether or not this flag is specified.
703-
ShadowedByCrossImportOverlay = 1 << 4
705+
ShadowedByCrossImportOverlay = 1 << 5
704706
};
705707
/// \sa getImportedModules
706708
using ImportFilter = OptionSet<ImportFilterKind>;

lib/AST/ImportCache.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ ImportSet &ImportCache::getImportSet(const DeclContext *dc) {
195195
file->getImportedModules(imports,
196196
{ModuleDecl::ImportFilterKind::Default,
197197
ModuleDecl::ImportFilterKind::ImplementationOnly,
198+
ModuleDecl::ImportFilterKind::SPIOnly,
198199
ModuleDecl::ImportFilterKind::SPIAccessControl});
199200
}
200201

@@ -279,6 +280,7 @@ ImportCache::getAllAccessPathsNotShadowedBy(const ModuleDecl *mod,
279280
file->getImportedModules(stack,
280281
{ModuleDecl::ImportFilterKind::Default,
281282
ModuleDecl::ImportFilterKind::ImplementationOnly,
283+
ModuleDecl::ImportFilterKind::SPIOnly,
282284
ModuleDecl::ImportFilterKind::SPIAccessControl});
283285
}
284286

lib/AST/Module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,8 @@ SourceFile::getImportedModules(SmallVectorImpl<ImportedModule> &modules,
15961596
requiredFilter |= ModuleDecl::ImportFilterKind::Exported;
15971597
else if (desc.options.contains(ImportFlags::ImplementationOnly))
15981598
requiredFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
1599+
else if (desc.options.contains(ImportFlags::SPIOnly))
1600+
requiredFilter |= ModuleDecl::ImportFilterKind::SPIOnly;
15991601
else if (desc.options.contains(ImportFlags::SPIAccessControl))
16001602
requiredFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;
16011603
else
@@ -1904,6 +1906,7 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
19041906

19051907
ModuleDecl::ImportFilter topLevelFilter = filter;
19061908
topLevelFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
1909+
topLevelFilter |= ModuleDecl::ImportFilterKind::SPIOnly;
19071910
topLevel->getImportedModules(stack, topLevelFilter);
19081911

19091912
// Make sure the top-level module is first; we want pre-order-ish traversal.
@@ -2600,6 +2603,7 @@ canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const {
26002603
// @_implementationOnly or @_spi.
26012604
ModuleDecl::ImportFilter filter = {
26022605
ModuleDecl::ImportFilterKind::ImplementationOnly,
2606+
ModuleDecl::ImportFilterKind::SPIOnly,
26032607
ModuleDecl::ImportFilterKind::SPIAccessControl
26042608
};
26052609
SmallVector<ImportedModule, 4> results;

lib/Sema/ImportResolution.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ UnboundImport::UnboundImport(ImportDecl *ID)
536536
if (ID->getAttrs().hasAttribute<ImplementationOnlyAttr>())
537537
import.options |= ImportFlags::ImplementationOnly;
538538

539+
if (ID->getAttrs().hasAttribute<SPIOnlyAttr>())
540+
import.options |= ImportFlags::SPIOnly;
541+
539542
if (auto *privateImportAttr =
540543
ID->getAttrs().getAttribute<PrivateImportAttr>()) {
541544
import.options |= ImportFlags::PrivateImport;

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
296296
void visitResultBuilderAttr(ResultBuilderAttr *attr);
297297

298298
void visitImplementationOnlyAttr(ImplementationOnlyAttr *attr);
299+
void visitSPIOnlyAttr(SPIOnlyAttr *attr);
299300
void visitNonEphemeralAttr(NonEphemeralAttr *attr);
300301
void checkOriginalDefinedInAttrs(ArrayRef<OriginallyDefinedInAttr *> Attrs);
301302

@@ -3908,6 +3909,10 @@ AttributeChecker::visitImplementationOnlyAttr(ImplementationOnlyAttr *attr) {
39083909
// it won't necessarily be able to say why.
39093910
}
39103911

3912+
void
3913+
AttributeChecker::visitSPIOnlyAttr(SPIOnlyAttr *attr) {
3914+
}
3915+
39113916
void AttributeChecker::visitTypeSequenceAttr(TypeSequenceAttr *attr) {
39123917
if (!isa<GenericTypeParamDecl>(D)) {
39133918
attr->setInvalid();

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,7 @@ namespace {
15821582
UNINTERESTING_ATTR(Frozen)
15831583
UNINTERESTING_ATTR(HasInitialValue)
15841584
UNINTERESTING_ATTR(ImplementationOnly)
1585+
UNINTERESTING_ATTR(SPIOnly)
15851586
UNINTERESTING_ATTR(Custom)
15861587
UNINTERESTING_ATTR(PropertyWrapper)
15871588
UNINTERESTING_ATTR(TypeWrapper)

0 commit comments

Comments
 (0)