Skip to content

Commit 454dd00

Browse files
committed
[sil] Add support for adding _semantics attributes to nominal types.
This will make it easier to prototype diagnostics on specifically marked nominal types. My intended usage would be to have a way to emit diagnostics if specific instances of the nominal type are ever not on the stack.
1 parent 2bd55f6 commit 454dd00

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ DECL_ATTR(inline, Inline,
202202
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
203203
20)
204204
DECL_ATTR(_semantics, Semantics,
205-
OnAbstractFunction | OnSubscript |
205+
OnAbstractFunction | OnSubscript | OnNominalType |
206206
AllowMultipleAttributes | UserInaccessible |
207207
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
208208
21)

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,18 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
34383438
/// with placeholders for unimportable stored properties.
34393439
ArrayRef<Decl *> getStoredPropertiesAndMissingMemberPlaceholders() const;
34403440

3441+
/// Return the range of semantics attributes attached to this NominalTypeDecl.
3442+
auto getSemanticsAttrs() const
3443+
-> decltype(getAttrs().getAttributes<SemanticsAttr>()) {
3444+
return getAttrs().getAttributes<SemanticsAttr>();
3445+
}
3446+
3447+
bool hasSemanticsAttr(StringRef attrValue) const {
3448+
return llvm::any_of(getSemanticsAttrs(), [&](const SemanticsAttr *attr) {
3449+
return attrValue.equals(attr->Value);
3450+
});
3451+
}
3452+
34413453
// Implement isa/cast/dyncast/etc.
34423454
static bool classof(const Decl *D) {
34433455
return D->getKind() >= DeclKind::First_NominalTypeDecl &&

test/attr/attr_semantics.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ func func_with_nested_semantics_2() {
1919
func exit(_ code : UInt32) -> Void
2020
exit(0)
2121
}
22+
23+
@_semantics("struct")
24+
struct StructWithSemantics {}
25+
26+
@_semantics("class")
27+
class ClassWithSemantics {}
28+
29+
@_semantics("enum")
30+
enum EnumWithSemantics {}
31+
32+
@_semantics("struct1")
33+
@_semantics("struct2")
34+
struct StructWithDuplicateSemantics {}

0 commit comments

Comments
 (0)