Skip to content

Commit 38360fe

Browse files
committed
ASTBridging: Bridge swift::DeclAttrKind directly
1 parent 7de8827 commit 38360fe

File tree

10 files changed

+281
-287
lines changed

10 files changed

+281
-287
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222

2323
#include "swift/AST/AccessorKind.h"
24+
#include "swift/AST/AttrKind.h"
2425
#include "swift/AST/DiagnosticKind.h"
2526
#include "swift/AST/DiagnosticList.h"
2627
#include "swift/Basic/BasicBridging.h"
@@ -830,14 +831,11 @@ class BridgedParsedAutoDiffParameter {
830831
// MARK: DeclAttributes
831832
//===----------------------------------------------------------------------===//
832833

833-
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedDeclAttrKind {
834-
#define DECL_ATTR(_, CLASS, ...) BridgedDeclAttrKind##CLASS,
835-
#include "swift/AST/DeclAttr.def"
836-
BridgedDeclAttrKindNone,
837-
};
834+
BRIDGED_OPTIONAL(swift::DeclAttrKind, DeclAttrKind)
838835

839-
SWIFT_NAME("BridgedDeclAttrKind.init(from:)")
840-
BridgedDeclAttrKind BridgedDeclAttrKind_fromString(BridgedStringRef cStr);
836+
SWIFT_NAME("BridgedOptionalDeclAttrKind.init(from:)")
837+
BridgedOptionalDeclAttrKind
838+
BridgedOptionalDeclAttrKind_fromString(BridgedStringRef cStr);
841839

842840
struct BridgedDeclAttributes {
843841
BridgedNullableDeclAttribute chain;
@@ -850,18 +848,18 @@ struct BridgedDeclAttributes {
850848
};
851849

852850
SWIFT_NAME("BridgedDeclAttribute.shouldBeRejectedByParser(_:)")
853-
bool BridgedDeclAttribute_shouldBeRejectedByParser(BridgedDeclAttrKind cKind);
851+
bool BridgedDeclAttribute_shouldBeRejectedByParser(swift::DeclAttrKind kind);
854852

855853
SWIFT_NAME("BridgedDeclAttribute.isDeclModifier(_:)")
856-
bool BridgedDeclAttribute_isDeclModifier(BridgedDeclAttrKind cKind);
854+
bool BridgedDeclAttribute_isDeclModifier(swift::DeclAttrKind kind);
857855

858856
SWIFT_NAME("BridgedDeclAttributes.add(self:_:)")
859857
void BridgedDeclAttributes_add(BridgedDeclAttributes *_Nonnull attrs,
860858
BridgedDeclAttribute add);
861859

862860
SWIFT_NAME("BridgedDeclAttribute.createSimple(_:kind:atLoc:nameLoc:)")
863861
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
864-
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
862+
BridgedASTContext cContext, swift::DeclAttrKind kind,
865863
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
866864

867865
SWIFT_NAME("BridgedABIAttr.createParsed(_:atLoc:range:abiDecl:)")

include/swift/AST/Attr.h

Lines changed: 39 additions & 2 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) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 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
@@ -106,7 +106,41 @@ class alignas(1 << AttrAlignInBits) AttributeBase
106106
class DeclAttributes;
107107
enum class DeclKind : uint8_t;
108108

109-
/// Represents one declaration attribute.
109+
enum : unsigned {
110+
NumInlineKindBits =
111+
countBitsUsed(static_cast<unsigned>(InlineKind::Last_InlineKind))
112+
};
113+
114+
enum : unsigned {
115+
NumEffectsKindBits =
116+
countBitsUsed(static_cast<unsigned>(EffectsKind::Last_EffectsKind))
117+
};
118+
119+
enum : unsigned {
120+
NumExposureKindBits =
121+
countBitsUsed(static_cast<unsigned>(ExposureKind::Last_ExposureKind))
122+
};
123+
124+
enum : unsigned {
125+
NumExternKindBits =
126+
countBitsUsed(static_cast<unsigned>(ExternKind::Last_ExternKind))
127+
};
128+
129+
enum : unsigned {
130+
NumNonIsolatedModifierBits = countBitsUsed(
131+
static_cast<unsigned>(NonIsolatedModifier::Last_NonIsolatedModifier))
132+
};
133+
134+
enum : unsigned {
135+
NumInheritActorContextKindBits = countBitsUsed(static_cast<unsigned>(
136+
InheritActorContextModifier::Last_InheritActorContextKind))
137+
};
138+
139+
enum : unsigned { NumDeclAttrKindBits = countBitsUsed(NumDeclAttrKinds - 1) };
140+
141+
enum : unsigned { NumTypeAttrKindBits = countBitsUsed(NumTypeAttrKinds - 1) };
142+
143+
/// Represents one declaration attribute.
110144
class DeclAttribute : public AttributeBase {
111145
friend class DeclAttributes;
112146

@@ -3466,6 +3500,9 @@ class ABIAttr : public DeclAttribute {
34663500
}
34673501
};
34683502

3503+
/// The kind of unary operator, if any.
3504+
enum class UnaryOperatorKind : uint8_t { None, Prefix, Postfix };
3505+
34693506
/// Attributes that may be applied to declarations.
34703507
class DeclAttributes {
34713508
/// Linked list of declaration attributes.

include/swift/AST/AttrKind.h

Lines changed: 19 additions & 48 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) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 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
@@ -17,10 +17,14 @@
1717
#ifndef SWIFT_ATTRKIND_H
1818
#define SWIFT_ATTRKIND_H
1919

20-
#include "swift/Basic/InlineBitfield.h"
21-
#include "swift/Basic/LLVM.h"
22-
#include "swift/Config.h"
23-
#include "llvm/Support/DataTypes.h"
20+
/// This header is included in a bridging header. Be *very* careful with what
21+
/// you include here! See include caveats in `ASTBridging.h`.
22+
#include "swift/Basic/SwiftBridging.h"
23+
#include <stdint.h>
24+
25+
namespace llvm {
26+
class StringRef;
27+
}
2428

2529
namespace swift {
2630

@@ -41,14 +45,8 @@ enum class Associativity : uint8_t {
4145
};
4246

4347
/// Returns the in-source spelling of the given associativity.
44-
StringRef getAssociativitySpelling(Associativity value);
45-
46-
/// The kind of unary operator, if any.
47-
enum class UnaryOperatorKind : uint8_t {
48-
None,
49-
Prefix,
50-
Postfix
51-
};
48+
SWIFT_UNAVAILABLE("Unavailable in Swift")
49+
llvm::StringRef getAssociativitySpelling(Associativity value);
5250

5351
/// Access control levels.
5452
// These are used in diagnostics and with < and similar operations,
@@ -74,18 +72,15 @@ enum class AccessLevel : uint8_t {
7472
};
7573

7674
/// Returns the in-source spelling of the given access level.
77-
StringRef getAccessLevelSpelling(AccessLevel value);
75+
SWIFT_UNAVAILABLE("Unavailable in Swift")
76+
llvm::StringRef getAccessLevelSpelling(AccessLevel value);
7877

7978
enum class InlineKind : uint8_t {
8079
Never = 0,
8180
Always = 1,
8281
Last_InlineKind = Always
8382
};
8483

85-
enum : unsigned { NumInlineKindBits =
86-
countBitsUsed(static_cast<unsigned>(InlineKind::Last_InlineKind)) };
87-
88-
8984
/// This enum represents the possible values of the @_effects attribute.
9085
/// These values are ordered from the strongest guarantee to the weakest,
9186
/// so please do not reorder existing values.
@@ -99,19 +94,9 @@ enum class EffectsKind : uint8_t {
9994
Last_EffectsKind = Unspecified
10095
};
10196

102-
enum : unsigned { NumEffectsKindBits =
103-
countBitsUsed(static_cast<unsigned>(EffectsKind::Last_EffectsKind)) };
104-
10597
/// This enum represents the possible values of the @_expose attribute.
106-
enum class ExposureKind: uint8_t {
107-
Cxx,
108-
Wasm,
109-
Last_ExposureKind = Wasm
110-
};
98+
enum class ExposureKind : uint8_t { Cxx, Wasm, Last_ExposureKind = Wasm };
11199

112-
enum : unsigned { NumExposureKindBits =
113-
countBitsUsed(static_cast<unsigned>(ExposureKind::Last_ExposureKind)) };
114-
115100
/// This enum represents the possible values of the @_extern attribute.
116101
enum class ExternKind: uint8_t {
117102
/// Reference an externally defined C function.
@@ -127,21 +112,13 @@ enum class ExternKind: uint8_t {
127112
Last_ExternKind = Wasm
128113
};
129114

130-
enum : unsigned { NumExternKindBits =
131-
countBitsUsed(static_cast<unsigned>(ExternKind::Last_ExternKind)) };
132-
133115
enum class NonIsolatedModifier : uint8_t {
134116
None = 0,
135117
Unsafe,
136118
NonSending,
137119
Last_NonIsolatedModifier = NonSending
138120
};
139121

140-
enum : unsigned {
141-
NumNonIsolatedModifierBits = countBitsUsed(
142-
static_cast<unsigned>(NonIsolatedModifier::Last_NonIsolatedModifier))
143-
};
144-
145122
enum class InheritActorContextModifier : uint8_t {
146123
/// Inherit the actor execution context if the isolated parameter was
147124
/// captured by the closure, context is nonisolated or isolated to a
@@ -153,22 +130,17 @@ enum class InheritActorContextModifier : uint8_t {
153130
Last_InheritActorContextKind = Always
154131
};
155132

156-
enum : unsigned {
157-
NumInheritActorContextKindBits = countBitsUsed(static_cast<unsigned>(
158-
InheritActorContextModifier::Last_InheritActorContextKind))
159-
};
160-
161-
enum class DeclAttrKind : unsigned {
133+
enum class ENUM_EXTENSIBILITY_ATTR(closed) DeclAttrKind : unsigned {
162134
#define DECL_ATTR(_, CLASS, ...) CLASS,
163135
#define LAST_DECL_ATTR(CLASS) Last_DeclAttr = CLASS,
164136
#include "swift/AST/DeclAttr.def"
165137
};
166138

167-
StringRef getDeclAttrKindID(DeclAttrKind kind);
139+
SWIFT_UNAVAILABLE("Unavailable in Swift")
140+
llvm::StringRef getDeclAttrKindID(DeclAttrKind kind);
168141

169142
enum : unsigned {
170-
NumDeclAttrKinds = static_cast<unsigned>(DeclAttrKind::Last_DeclAttr) + 1,
171-
NumDeclAttrKindBits = countBitsUsed(NumDeclAttrKinds - 1),
143+
NumDeclAttrKinds = static_cast<unsigned>(DeclAttrKind::Last_DeclAttr) + 1
172144
};
173145

174146
// Define enumerators for each type attribute, e.g. TypeAttrKind::Weak.
@@ -179,8 +151,7 @@ enum class TypeAttrKind {
179151
};
180152

181153
enum : unsigned {
182-
NumTypeAttrKinds = static_cast<unsigned>(TypeAttrKind::Last_TypeAttr) + 1,
183-
NumTypeAttrKindBits = countBitsUsed(NumTypeAttrKinds - 1),
154+
NumTypeAttrKinds = static_cast<unsigned>(TypeAttrKind::Last_TypeAttr) + 1
184155
};
185156

186157
} // end namespace swift

include/swift/AST/KnownProtocols.h

Lines changed: 2 additions & 1 deletion
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) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 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
@@ -14,6 +14,7 @@
1414
#define SWIFT_AST_KNOWNPROTOCOLS_H
1515

1616
#include "swift/ABI/InvertibleProtocols.h"
17+
#include "swift/Basic/InlineBitfield.h"
1718
#include "swift/Config.h"
1819

1920
namespace llvm {

include/swift/AST/Type.h

Lines changed: 2 additions & 1 deletion
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) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 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
@@ -26,6 +26,7 @@
2626
#include "swift/Basic/ArrayRefView.h"
2727
#include "swift/Basic/Compiler.h"
2828
#include "swift/Basic/Debug.h"
29+
#include "swift/Basic/InlineBitfield.h"
2930
#include "swift/Basic/LLVM.h"
3031
#include "swift/Basic/OptionSet.h"
3132
#include "llvm/ADT/DenseMap.h"

include/swift/Basic/BasicBridging.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@
5555
#include <vector>
5656
#endif
5757

58-
#if __has_attribute(availability)
59-
#define SWIFT_UNAVAILABLE(msg) \
60-
__attribute__((availability(swift, unavailable, message=msg)))
61-
#else
62-
#define SWIFT_UNAVAILABLE(msg)
63-
#endif
64-
6558
#ifdef PURE_BRIDGING_MODE
6659
// In PURE_BRIDGING_MODE, briding functions are not inlined
6760
#define BRIDGED_INLINE

include/swift/Basic/SwiftBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,12 @@
8989
#define SWIFT_NAME(_name)
9090
#endif
9191

92+
#if __has_attribute(availability)
93+
#define SWIFT_UNAVAILABLE(msg) \
94+
__attribute__((availability(swift, unavailable, message = msg)))
95+
#else
96+
#define SWIFT_UNAVAILABLE(msg)
97+
#endif
98+
9299
#endif // SWIFT_BASIC_SWIFT_BRIDGING_H
93100

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,51 +68,28 @@ ParsedAutoDiffParameter BridgedParsedAutoDiffParameter::unbridged() const {
6868
}
6969
#include "swift/AST/DeclAttr.def"
7070

71-
BridgedDeclAttrKind BridgedDeclAttrKind_fromString(BridgedStringRef cStr) {
71+
BridgedOptionalDeclAttrKind
72+
BridgedOptionalDeclAttrKind_fromString(BridgedStringRef cStr) {
7273
auto optKind = DeclAttribute::getAttrKindFromString(cStr.unbridged());
73-
if (!optKind)
74-
return BridgedDeclAttrKindNone;
75-
switch (*optKind) {
76-
#define DECL_ATTR(_, CLASS, ...) \
77-
case DeclAttrKind::CLASS: \
78-
return BridgedDeclAttrKind##CLASS;
79-
#include "swift/AST/DeclAttr.def"
80-
}
81-
}
82-
83-
std::optional<DeclAttrKind> unbridged(BridgedDeclAttrKind kind) {
84-
switch (kind) {
85-
#define DECL_ATTR(_, CLASS, ...) \
86-
case BridgedDeclAttrKind##CLASS: \
87-
return DeclAttrKind::CLASS;
88-
#include "swift/AST/DeclAttr.def"
89-
case BridgedDeclAttrKindNone:
90-
return std::nullopt;
74+
if (!optKind) {
75+
return BridgedOptionalDeclAttrKind();
9176
}
92-
llvm_unreachable("unhandled enum value");
77+
return *optKind;
9378
}
9479

9580
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
96-
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
81+
BridgedASTContext cContext, swift::DeclAttrKind kind,
9782
BridgedSourceLoc cAtLoc, BridgedSourceLoc cAttrLoc) {
98-
auto optKind = unbridged(cKind);
99-
assert(optKind && "creating attribute of invalid kind?");
100-
return DeclAttribute::createSimple(cContext.unbridged(), *optKind,
83+
return DeclAttribute::createSimple(cContext.unbridged(), kind,
10184
cAtLoc.unbridged(), cAttrLoc.unbridged());
10285
}
10386

104-
bool BridgedDeclAttribute_shouldBeRejectedByParser(BridgedDeclAttrKind cKind) {
105-
auto optKind = unbridged(cKind);
106-
if (!optKind)
107-
return false;
108-
return DeclAttribute::shouldBeRejectedByParser(*optKind);
87+
bool BridgedDeclAttribute_shouldBeRejectedByParser(swift::DeclAttrKind kind) {
88+
return DeclAttribute::shouldBeRejectedByParser(kind);
10989
}
11090

111-
bool BridgedDeclAttribute_isDeclModifier(BridgedDeclAttrKind cKind) {
112-
auto optKind = unbridged(cKind);
113-
if (!optKind)
114-
return false;
115-
return DeclAttribute::isDeclModifier(*optKind);
91+
bool BridgedDeclAttribute_isDeclModifier(swift::DeclAttrKind kind) {
92+
return DeclAttribute::isDeclModifier(kind);
11693
}
11794

11895
void BridgedDeclAttributes_add(BridgedDeclAttributes *cAttrs,

0 commit comments

Comments
 (0)