Skip to content

Commit 1878ff0

Browse files
committed
ASTBridging: Bridge swift::TypeAttrKind directly
1 parent 9198f05 commit 1878ff0

File tree

4 files changed

+89
-102
lines changed

4 files changed

+89
-102
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,19 +2532,15 @@ class BridgedTypeOrCustomAttr {
25322532
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCustomAttr castToCustomAttr() const;
25332533
};
25342534

2535-
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
2536-
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind {
2537-
#define TYPE_ATTR(_, CLASS) BridgedTypeAttrKind##CLASS,
2538-
#include "swift/AST/TypeAttr.def"
2539-
BridgedTypeAttrKindNone,
2540-
};
2535+
BRIDGED_OPTIONAL(swift::TypeAttrKind, TypeAttrKind)
25412536

2542-
SWIFT_NAME("BridgedTypeAttrKind.init(from:)")
2543-
BridgedTypeAttrKind BridgedTypeAttrKind_fromString(BridgedStringRef cStr);
2537+
SWIFT_NAME("BridgedOptionalTypeAttrKind.init(from:)")
2538+
BridgedOptionalTypeAttrKind
2539+
BridgedOptionalTypeAttrKind_fromString(BridgedStringRef cStr);
25442540

25452541
SWIFT_NAME("BridgedTypeAttribute.createSimple(_:kind:atLoc:nameLoc:)")
25462542
BridgedTypeAttribute BridgedTypeAttribute_createSimple(
2547-
BridgedASTContext cContext, BridgedTypeAttrKind cKind,
2543+
BridgedASTContext cContext, swift::TypeAttrKind kind,
25482544
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
25492545

25502546
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedIsolatedTypeAttrIsolationKind {

include/swift/AST/AttrKind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ enum : unsigned {
149149
};
150150

151151
// Define enumerators for each type attribute, e.g. TypeAttrKind::Weak.
152-
enum class TypeAttrKind {
152+
enum class ENUM_EXTENSIBILITY_ATTR(closed) TypeAttrKind {
153153
#define TYPE_ATTR(_, CLASS) CLASS,
154154
#define LAST_TYPE_ATTR(CLASS) Last_TypeAttr = CLASS,
155155
#include "swift/AST/TypeAttr.def"

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -33,36 +33,19 @@ using namespace swift;
3333
}
3434
#include "swift/AST/TypeAttr.def"
3535

36-
BridgedTypeAttrKind BridgedTypeAttrKind_fromString(BridgedStringRef cStr) {
36+
BridgedOptionalTypeAttrKind
37+
BridgedOptionalTypeAttrKind_fromString(BridgedStringRef cStr) {
3738
auto optKind = TypeAttribute::getAttrKindFromString(cStr.unbridged());
38-
if (!optKind)
39-
return BridgedTypeAttrKindNone;
40-
switch (*optKind) {
41-
#define TYPE_ATTR(_, CLASS) \
42-
case TypeAttrKind::CLASS: \
43-
return BridgedTypeAttrKind##CLASS;
44-
#include "swift/AST/TypeAttr.def"
45-
}
46-
}
47-
48-
static std::optional<TypeAttrKind> unbridged(BridgedTypeAttrKind kind) {
49-
switch (kind) {
50-
#define TYPE_ATTR(_, CLASS) \
51-
case BridgedTypeAttrKind##CLASS: \
52-
return TypeAttrKind::CLASS;
53-
#include "swift/AST/TypeAttr.def"
54-
case BridgedTypeAttrKindNone:
55-
return std::nullopt;
39+
if (!optKind) {
40+
return BridgedOptionalTypeAttrKind();
5641
}
57-
llvm_unreachable("unhandled enum value");
42+
return *optKind;
5843
}
5944

6045
BridgedTypeAttribute BridgedTypeAttribute_createSimple(
61-
BridgedASTContext cContext, BridgedTypeAttrKind cKind,
46+
BridgedASTContext cContext, swift::TypeAttrKind kind,
6247
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc) {
63-
auto optKind = unbridged(cKind);
64-
assert(optKind && "creating attribute of invalid kind?");
65-
return TypeAttribute::createSimple(cContext.unbridged(), *optKind,
48+
return TypeAttribute::createSimple(cContext.unbridged(), kind,
6649
cAtLoc.unbridged(), cNameLoc.unbridged());
6750
}
6851

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -36,90 +36,98 @@ extension ASTGenVisitor {
3636
func generateTypeAttribute(attribute node: AttributeSyntax) -> BridgedTypeOrCustomAttr? {
3737
if let identTy = node.attributeName.as(IdentifierTypeSyntax.self) {
3838
let attrName = identTy.name.rawText
39-
let attrKind = BridgedTypeAttrKind(from: attrName.bridged)
39+
let attrKind: swift.TypeAttrKind?
40+
do {
41+
let bridgedOptional = BridgedOptionalTypeAttrKind(from: attrName.bridged)
42+
attrKind = if bridgedOptional.hasValue {
43+
bridgedOptional.value
44+
} else {
45+
nil
46+
}
47+
}
4048

4149
switch attrKind {
4250
// Simple type attributes.
43-
case .autoclosure,
44-
.addressable,
45-
.concurrent,
46-
.escaping,
47-
.noEscape,
48-
.noDerivative,
49-
.async,
50-
.sendable,
51-
.retroactive,
52-
.unchecked,
53-
.unsafe,
54-
.preconcurrency,
55-
.local,
56-
.noMetadata,
57-
.nonisolated,
58-
.packGuaranteed,
59-
.packInout,
60-
.packOut,
61-
.packOwned,
62-
.pseudogeneric,
63-
.yields,
64-
.yieldMany,
65-
.yieldOnce,
66-
.yieldOnce2,
67-
.thin,
68-
.thick,
69-
.unimplementable:
70-
return self.generateSimpleTypeAttr(attribute: node, kind: attrKind)
51+
case .Autoclosure,
52+
.Addressable,
53+
.Concurrent,
54+
.Escaping,
55+
.NoEscape,
56+
.NoDerivative,
57+
.Async,
58+
.Sendable,
59+
.Retroactive,
60+
.Unchecked,
61+
.Unsafe,
62+
.Preconcurrency,
63+
.Local,
64+
.NoMetadata,
65+
.Nonisolated,
66+
.PackGuaranteed,
67+
.PackInout,
68+
.PackOut,
69+
.PackOwned,
70+
.Pseudogeneric,
71+
.Yields,
72+
.YieldMany,
73+
.YieldOnce,
74+
.YieldOnce2,
75+
.Thin,
76+
.Thick,
77+
.Unimplementable:
78+
return self.generateSimpleTypeAttr(attribute: node, kind: attrKind!)
7179
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
7280

73-
case .convention:
81+
case .Convention:
7482
return (self.generateConventionTypeAttr(attribute: node)?.asTypeAttribute)
7583
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
76-
case .differentiable:
84+
case .Differentiable:
7785
return (self.generateDifferentiableTypeAttr(attribute: node)?.asTypeAttribute)
7886
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
79-
case .opaqueReturnTypeOf:
87+
case .OpaqueReturnTypeOf:
8088
return (self.generateOpaqueReturnTypeOfTypeAttr(attribute: node)?.asTypeAttribute)
8189
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
82-
case .isolated:
90+
case .Isolated:
8391
return (self.generateIsolatedTypeAttr(attribute: node)?.asTypeAttribute)
8492
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
8593

8694
// SIL type attributes are not supported.
87-
case .autoreleased,
88-
.blockStorage,
89-
.box,
90-
.calleeGuaranteed,
91-
.calleeOwned,
92-
.capturesGenerics,
93-
.direct,
94-
.dynamicSelf,
95-
.error,
96-
.errorIndirect,
97-
.errorUnowned,
98-
.guaranteed,
99-
.in,
100-
.inConstant,
101-
.inGuaranteed,
102-
.inCXX,
103-
.inout,
104-
.inoutAliasable,
105-
.moveOnly,
106-
.objCMetatype,
107-
.opened,
108-
.out,
109-
.owned,
110-
.packElement,
111-
.silIsolated,
112-
.silUnmanaged,
113-
.silUnowned,
114-
.silWeak,
115-
.silSending,
116-
.silImplicitLeadingParam,
117-
.unownedInnerPointer:
95+
case .Autoreleased,
96+
.BlockStorage,
97+
.Box,
98+
.CalleeGuaranteed,
99+
.CalleeOwned,
100+
.CapturesGenerics,
101+
.Direct,
102+
.DynamicSelf,
103+
.Error,
104+
.ErrorIndirect,
105+
.ErrorUnowned,
106+
.Guaranteed,
107+
.In,
108+
.InConstant,
109+
.InGuaranteed,
110+
.InCXX,
111+
.Inout,
112+
.InoutAliasable,
113+
.MoveOnly,
114+
.ObjCMetatype,
115+
.Opened,
116+
.Out,
117+
.Owned,
118+
.PackElement,
119+
.SILIsolated,
120+
.SILUnmanaged,
121+
.SILUnowned,
122+
.SILWeak,
123+
.SILSending,
124+
.SILImplicitLeadingParam,
125+
.UnownedInnerPointer:
118126
// TODO: Diagnose or fallback to CustomAttr?
119127
fatalError("SIL type attributes are not supported")
120128
break;
121129

122-
case .none:
130+
case nil:
123131
// Not a builtin type attribute. Fall back to CustomAttr
124132
break;
125133
}
@@ -131,7 +139,7 @@ extension ASTGenVisitor {
131139
return nil
132140
}
133141

134-
func generateSimpleTypeAttr(attribute node: AttributeSyntax, kind: BridgedTypeAttrKind) -> BridgedTypeAttribute? {
142+
func generateSimpleTypeAttr(attribute node: AttributeSyntax, kind: swift.TypeAttrKind) -> BridgedTypeAttribute? {
135143
// TODO: Diagnose extraneous arguments.
136144
return BridgedTypeAttribute.createSimple(
137145
self.ctx,

0 commit comments

Comments
 (0)