Skip to content

Commit 842a8f1

Browse files
committed
ASTBridging: Bridge swift::AccessorKind directly
1 parent 8e1dfa1 commit 842a8f1

23 files changed

+115
-101
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// Pure bridging mode does not permit including any C++/llvm/swift headers.
2020
// See also the comments for `BRIDGING_MODE` in the top-level CMakeLists.txt file.
2121
//
22+
23+
#include "swift/AST/AccessorKind.h"
2224
#include "swift/AST/DiagnosticKind.h"
2325
#include "swift/Basic/BasicBridging.h"
2426

@@ -524,13 +526,6 @@ struct BridgedPatternBindingEntry {
524526
BridgedNullablePatternBindingInitializer initContext;
525527
};
526528

527-
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessorKind {
528-
#define ACCESSOR(ID) BridgedAccessorKind##ID,
529-
#include "swift/AST/AccessorKinds.def"
530-
};
531-
532-
swift::AccessorKind unbridged(BridgedAccessorKind kind);
533-
534529
//===----------------------------------------------------------------------===//
535530
// MARK: Diagnostic Engine
536531
//===----------------------------------------------------------------------===//
@@ -971,7 +966,7 @@ BridgedDerivativeAttr BridgedDerivativeAttr_createParsed(
971966
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
972967
BridgedSourceRange cRange, BridgedNullableTypeRepr cBaseType,
973968
BridgedDeclNameRef cOriginalName, BridgedDeclNameLoc cOriginalNameLoc,
974-
BridgedAccessorKind cAccessorKind, BridgedArrayRef cParams);
969+
swift::AccessorKind AccessorKind, BridgedArrayRef cParams);
975970

976971
SWIFT_NAME("BridgedDerivativeAttr.createParsed(_:atLoc:range:baseType:"
977972
"originalName:originalNameLoc:params:)")
@@ -1450,7 +1445,7 @@ SWIFT_NAME("BridgedAccessorDecl.createParsed(_:declContext:kind:storage:"
14501445
"throwsSpecifierLoc:thrownType:)")
14511446
BridgedAccessorDecl BridgedAccessorDecl_createParsed(
14521447
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1453-
BridgedAccessorKind cKind, BridgedAbstractStorageDecl cStorage,
1448+
swift::AccessorKind Kind, BridgedAbstractStorageDecl cStorage,
14541449
BridgedSourceLoc cDeclLoc, BridgedSourceLoc cAccessorKeywordLoc,
14551450
BridgedNullableParameterList cParamList, BridgedSourceLoc cAsyncLoc,
14561451
BridgedSourceLoc cThrowsLoc, BridgedNullableTypeRepr cThrownType);

include/swift/AST/AccessorKind.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===-- AST/AccessorKind.h --------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
/// This enum lives in its own header so that it can be included in
15+
/// `ASTBridging.h`.
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_AST_ACCESSOR_KIND_H
20+
#define SWIFT_AST_ACCESSOR_KIND_H
21+
22+
/// Be *very* careful with what you include here! See include caveats in
23+
/// `ASTBridging.h`.
24+
#include "swift/Basic/Compiler.h"
25+
#include "swift/Basic/SwiftBridging.h"
26+
27+
namespace swift {
28+
29+
// Note that the values of these enums line up with %select values in
30+
// diagnostics.
31+
enum class ENUM_EXTENSIBILITY_ATTR(closed) AccessorKind {
32+
#define ACCESSOR(ID, KEYWORD) ID SWIFT_NAME(#KEYWORD),
33+
#define LAST_ACCESSOR(ID) Last = ID
34+
#include "swift/AST/AccessorKinds.def"
35+
#undef ACCESSOR
36+
#undef LAST_ACCESSOR
37+
};
38+
39+
} // namespace swift
40+
41+
#endif // SWIFT_AST_ACCESSOR_KIND_H

include/swift/AST/AccessorKinds.def

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020
#error must define either ACCESSOR or ACCESSOR_KEYWORD
2121
#endif
2222

23-
/// ACCESSOR(ID)
23+
/// ACCESSOR(ID, KEYWORD)
2424
/// There is an accessor with the enumerator value AccessorKind::ID.
2525
#if !defined(ACCESSOR) && defined(ACCESSOR_KEYWORD)
26-
#define ACCESSOR(ID)
26+
#define ACCESSOR(ID, KEYWORD)
2727
#endif
2828

2929
/// SINGLETON_ACCESSOR(ID, KEYWORD)
3030
/// The given accessor has only one matching accessor keyword.
3131
///
32-
/// Defaults to ACCESSOR(ID) or ACCESSOR_KEYWORD(KEYWORD), depending on which
33-
/// is defined.
32+
/// Defaults to ACCESSOR(ID, KEYWORD) or ACCESSOR_KEYWORD(KEYWORD), depending
33+
/// on which is defined.
3434
#ifndef SINGLETON_ACCESSOR
3535
#if defined(ACCESSOR_KEYWORD)
3636
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR_KEYWORD(KEYWORD)
3737
#else
38-
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR(ID)
38+
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR(ID, KEYWORD)
3939
#endif
4040
#endif
4141

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8628,7 +8628,7 @@ class AccessorDecl final : public FuncDecl {
86288628
switch (getAccessorKind()) {
86298629
#define OBSERVING_ACCESSOR(ID, KEYWORD) \
86308630
case AccessorKind::ID: return true;
8631-
#define ACCESSOR(ID) \
8631+
#define ACCESSOR(ID, KEYWORD) \
86328632
case AccessorKind::ID: return false;
86338633
#include "swift/AST/AccessorKinds.def"
86348634
}
@@ -8651,7 +8651,7 @@ class AccessorDecl final : public FuncDecl {
86518651
switch (getAccessorKind()) {
86528652
#define COROUTINE_ACCESSOR(ID, KEYWORD) \
86538653
case AccessorKind::ID: return true;
8654-
#define ACCESSOR(ID) \
8654+
#define ACCESSOR(ID, KEYWORD) \
86558655
case AccessorKind::ID: return false;
86568656
#include "swift/AST/AccessorKinds.def"
86578657
}

include/swift/AST/StorageImpl.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_STORAGEIMPL_H
1919
#define SWIFT_STORAGEIMPL_H
2020

21+
#include "swift/AST/AccessorKind.h"
2122
#include "swift/Basic/Range.h"
2223
#include "llvm/ADT/StringRef.h"
2324

@@ -47,16 +48,6 @@ enum class OpaqueReadOwnership : uint8_t {
4748
OwnedOrBorrowed
4849
};
4950

50-
// Note that the values of these enums line up with %select values in
51-
// diagnostics.
52-
enum class AccessorKind {
53-
#define ACCESSOR(ID) ID,
54-
#define LAST_ACCESSOR(ID) Last = ID
55-
#include "swift/AST/AccessorKinds.def"
56-
#undef ACCESSOR
57-
#undef LAST_ACCESSOR
58-
};
59-
6051
inline bool requiresFeatureCoroutineAccessors(AccessorKind kind) {
6152
switch (kind) {
6253
case AccessorKind::Read2:
@@ -144,7 +135,7 @@ static inline IntRange<AccessorKind> allAccessorKinds() {
144135
static inline llvm::StringRef accessorKindName(AccessorKind ak) {
145136
switch(ak) {
146137

147-
#define ACCESSOR(ID) ID
138+
#define ACCESSOR(ID, KEYWORD) ID
148139
#define SINGLETON_ACCESSOR(ID, KEYWORD) \
149140
case AccessorKind::ID: \
150141
return #KEYWORD;

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
752752
AccessorKind unknownKind = (AccessorKind)((uint8_t)(AccessorKind::Last) + 1);
753753
Info.AccKind = llvm::StringSwitch<AccessorKind>(
754754
GetScalarString(Pair.getValue()))
755-
#define ACCESSOR(ID)
755+
#define ACCESSOR(ID, KEYWORD)
756756
#define SINGLETON_ACCESSOR(ID, KEYWORD) .Case(#KEYWORD, AccessorKind::ID)
757757
#include "swift/AST/AccessorKinds.def"
758758
.Default(unknownKind);
@@ -1165,8 +1165,9 @@ static StringRef getSimpleName(ValueDecl *VD) {
11651165
}
11661166
if (auto *AD = dyn_cast<AccessorDecl>(VD)) {
11671167
switch(AD->getAccessorKind()) {
1168-
#define ACCESSOR(ID) \
1169-
case AccessorKind::ID: return #ID;
1168+
#define ACCESSOR(ID, KEYWORD) \
1169+
case AccessorKind::ID: \
1170+
return #ID;
11701171
#include "swift/AST/AccessorKinds.def"
11711172
}
11721173
}
@@ -2267,7 +2268,7 @@ struct ScalarEnumerationTraits<DeclKind> {
22672268
template<>
22682269
struct ScalarEnumerationTraits<AccessorKind> {
22692270
static void enumeration(Output &out, AccessorKind &value) {
2270-
#define ACCESSOR(ID)
2271+
#define ACCESSOR(ID, KEYWORD)
22712272
#define SINGLETON_ACCESSOR(ID, KEYWORD) \
22722273
out.enumCase(value, #KEYWORD, AccessorKind::ID);
22732274
#include "swift/AST/AccessorKinds.def"

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6748,7 +6748,7 @@ void GenericEnvironment::dump() const {
67486748

67496749
StringRef swift::getAccessorKindString(AccessorKind value) {
67506750
switch (value) {
6751-
#define ACCESSOR(ID)
6751+
#define ACCESSOR(ID, KEYWORD)
67526752
#define SINGLETON_ACCESSOR(ID, KEYWORD) \
67536753
case AccessorKind::ID: return #KEYWORD;
67546754
#include "swift/AST/AccessorKinds.def"

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,7 @@ BridgedDerivativeAttr BridgedDerivativeAttr_createParsedImpl(
267267
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
268268
BridgedSourceRange cRange, BridgedNullableTypeRepr cBaseType,
269269
BridgedDeclNameRef cOriginalName, BridgedDeclNameLoc cOriginalNameLoc,
270-
std::optional<BridgedAccessorKind> cAccessorKind, BridgedArrayRef cParams) {
271-
std::optional<AccessorKind> accessorKind;
272-
if (cAccessorKind)
273-
accessorKind = unbridged(*cAccessorKind);
270+
std::optional<swift::AccessorKind> AccessorKind, BridgedArrayRef cParams) {
274271
SmallVector<ParsedAutoDiffParameter, 2> params;
275272
for (auto &elem : cParams.unbridged<BridgedParsedAutoDiffParameter>())
276273
params.push_back(elem.unbridged());
@@ -280,18 +277,18 @@ BridgedDerivativeAttr BridgedDerivativeAttr_createParsedImpl(
280277
cRange.unbridged(), cBaseType.unbridged(),
281278
DeclNameRefWithLoc{cOriginalName.unbridged(),
282279
cOriginalNameLoc.unbridged(),
283-
accessorKind},
280+
AccessorKind},
284281
params);
285282
}
286283

287284
BridgedDerivativeAttr BridgedDerivativeAttr_createParsed(
288285
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
289286
BridgedSourceRange cRange, BridgedNullableTypeRepr cBaseType,
290287
BridgedDeclNameRef cOriginalName, BridgedDeclNameLoc cOriginalNameLoc,
291-
BridgedAccessorKind cAccessorKind, BridgedArrayRef cParams) {
288+
swift::AccessorKind AccessorKind, BridgedArrayRef cParams) {
292289
return BridgedDerivativeAttr_createParsedImpl(
293290
cContext, cAtLoc, cRange, cBaseType, cOriginalName, cOriginalNameLoc,
294-
cAccessorKind, cParams);
291+
AccessorKind, cParams);
295292
}
296293

297294
BridgedDerivativeAttr BridgedDerivativeAttr_createParsed(

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ static StaticSpellingKind unbridged(BridgedStaticSpelling kind) {
129129
return static_cast<StaticSpellingKind>(kind);
130130
}
131131

132-
AccessorKind unbridged(BridgedAccessorKind kind) {
133-
return static_cast<AccessorKind>(kind);
134-
}
135-
136132
void BridgedDecl_attachParsedAttrs(BridgedDecl decl,
137133
BridgedDeclAttributes attrs) {
138134
decl.unbridged()->attachParsedAttrs(attrs.unbridged());
@@ -148,15 +144,15 @@ void BridgedDecl_forEachDeclToHoist(BridgedDecl cDecl,
148144

149145
BridgedAccessorDecl BridgedAccessorDecl_createParsed(
150146
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
151-
BridgedAccessorKind cKind, BridgedAbstractStorageDecl cStorage,
147+
swift::AccessorKind Kind, BridgedAbstractStorageDecl cStorage,
152148
BridgedSourceLoc cDeclLoc, BridgedSourceLoc cAccessorKeywordLoc,
153149
BridgedNullableParameterList cParamList, BridgedSourceLoc cAsyncLoc,
154150
BridgedSourceLoc cThrowsLoc, BridgedNullableTypeRepr cThrownType) {
155151
return AccessorDecl::createParsed(
156-
cContext.unbridged(), unbridged(cKind), cStorage.unbridged(),
157-
cDeclLoc.unbridged(), cAccessorKeywordLoc.unbridged(),
158-
cParamList.unbridged(), cAsyncLoc.unbridged(), cThrowsLoc.unbridged(),
159-
cThrownType.unbridged(), cDeclContext.unbridged());
152+
cContext.unbridged(), Kind, cStorage.unbridged(), cDeclLoc.unbridged(),
153+
cAccessorKeywordLoc.unbridged(), cParamList.unbridged(),
154+
cAsyncLoc.unbridged(), cThrowsLoc.unbridged(), cThrownType.unbridged(),
155+
cDeclContext.unbridged());
160156
}
161157

162158
static VarDecl::Introducer unbridged(BridgedVarDeclIntroducer introducer) {

lib/AST/Decl.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,8 +3375,7 @@ bool AbstractStorageDecl::requiresOpaqueAccessor(AccessorKind kind) const {
33753375

33763376
// Other accessors are never part of the opaque-accessors set.
33773377
#define OPAQUE_ACCESSOR(ID, KEYWORD)
3378-
#define ACCESSOR(ID) \
3379-
case AccessorKind::ID:
3378+
#define ACCESSOR(ID, KEYWORD) case AccessorKind::ID:
33803379
#include "swift/AST/AccessorKinds.def"
33813380
return false;
33823381
}
@@ -7616,7 +7615,7 @@ void AbstractStorageDecl::setAccessors(SourceLoc lbraceLoc,
76167615
// Compute the number of opaque accessors.
76177616
const size_t NumOpaqueAccessors =
76187617
0
7619-
#define ACCESSOR(ID)
7618+
#define ACCESSOR(ID, KEYWORD)
76207619
#define OPAQUE_ACCESSOR(ID, KEYWORD) \
76217620
+ 1
76227621
#include "swift/AST/AccessorKinds.def"
@@ -7639,14 +7638,14 @@ AbstractStorageDecl::AccessorRecord::create(ASTContext &ctx,
76397638
// Make sure that we have enough space to add implicit opaque accessors later.
76407639
size_t numMissingOpaque = NumOpaqueAccessors;
76417640
{
7642-
#define ACCESSOR(ID)
7641+
#define ACCESSOR(ID, KEYWORD)
76437642
#define OPAQUE_ACCESSOR(ID, KEYWORD) \
76447643
bool has##ID = false;
76457644
#include "swift/AST/AccessorKinds.def"
76467645
for (auto accessor : accessors) {
76477646
switch (accessor->getAccessorKind()) {
7648-
#define ACCESSOR(ID) \
7649-
case AccessorKind::ID: \
7647+
#define ACCESSOR(ID, KEYWORD) \
7648+
case AccessorKind::ID: \
76507649
continue;
76517650
#define OPAQUE_ACCESSOR(ID, KEYWORD) \
76527651
case AccessorKind::ID: \
@@ -12207,10 +12206,10 @@ StringRef swift::getAccessorLabel(AccessorKind kind) {
1220712206
switch (kind) {
1220812207
#define SINGLETON_ACCESSOR(ID, KEYWORD) \
1220912208
case AccessorKind::ID: return #KEYWORD;
12210-
#define ACCESSOR(ID)
12211-
#include "swift/AST/AccessorKinds.def"
12212-
}
12213-
llvm_unreachable("bad accessor kind");
12209+
#define ACCESSOR(ID, KEYWORD)
12210+
#include "swift/AST/AccessorKinds.def"
12211+
}
12212+
llvm_unreachable("bad accessor kind");
1221412213
}
1221512214

1221612215
void swift::simple_display(llvm::raw_ostream &out, AccessorKind kind) {

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ extension ASTGenVisitor {
643643
return nil
644644
}
645645

646-
let accessorKind: BridgedAccessorKind?
646+
let accessorKind: swift.AccessorKind?
647647
if let accessorToken = args.accessorSpecifier {
648648
accessorKind = self.generate(accessorSpecifier: accessorToken)
649649
} else {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ extension ASTGenVisitor {
375375
// MARK: - AbstractStorageDecl
376376

377377
extension ASTGenVisitor {
378-
func generate(accessorSpecifier specifier: TokenSyntax) -> BridgedAccessorKind? {
378+
func generate(accessorSpecifier specifier: TokenSyntax) -> swift.AccessorKind? {
379379
switch specifier.keywordKind {
380380
case .get:
381381
return .get
@@ -386,21 +386,21 @@ extension ASTGenVisitor {
386386
case .willSet:
387387
return .willSet
388388
case .unsafeAddress:
389-
return .address
389+
return .unsafeAddress
390390
case .unsafeMutableAddress:
391-
return .mutableAddress
391+
return .unsafeMutableAddress
392392
case ._read:
393-
return .read
393+
return ._read
394394
case ._modify:
395-
return .modify
395+
return ._modify
396396
case .`init`:
397397
return .`init`
398398
case .read:
399399
precondition(ctx.langOptsHasFeature(.CoroutineAccessors), "(compiler bug) 'read' accessor should only be parsed with 'CoroutineAccessors' feature")
400-
return .read2
400+
return .read
401401
case .modify:
402402
precondition(ctx.langOptsHasFeature(.CoroutineAccessors), "(compiler bug) 'modify' accessor should only be parsed with 'CoroutineAccessors' feature")
403-
return .modify2
403+
return .modify
404404
default:
405405
self.diagnose(.unknownAccessorSpecifier(specifier))
406406
return nil

lib/DriverTool/swift_api_digester_main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
852852
auto *LSub = dyn_cast<SDKNodeDeclSubscript>(Left);
853853
auto *RSub = dyn_cast<SDKNodeDeclSubscript>(Right);
854854
SequentialNodeMatcher(LSub->getChildren(), RSub->getChildren(), *this).match();
855-
#define ACCESSOR(ID) \
856-
singleMatch(LSub->getAccessor(AccessorKind::ID), \
855+
#define ACCESSOR(ID, KEYWORD) \
856+
singleMatch(LSub->getAccessor(AccessorKind::ID), \
857857
RSub->getAccessor(AccessorKind::ID), *this);
858858
#include "swift/AST/AccessorKinds.def"
859859
break;
@@ -863,8 +863,8 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
863863
auto *RVar = dyn_cast<SDKNodeDeclVar>(Right);
864864
// Match property type.
865865
singleMatch(LVar->getType(), RVar->getType(), *this);
866-
#define ACCESSOR(ID) \
867-
singleMatch(LVar->getAccessor(AccessorKind::ID), \
866+
#define ACCESSOR(ID, KEYWORD) \
867+
singleMatch(LVar->getAccessor(AccessorKind::ID), \
868868
RVar->getAccessor(AccessorKind::ID), *this);
869869
#include "swift/AST/AccessorKinds.def"
870870
break;

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,8 +1853,7 @@ namespace {
18531853
accessor->getStorage());
18541854

18551855
#define OBJC_ACCESSOR(ID, KEYWORD)
1856-
#define ACCESSOR(ID) \
1857-
case AccessorKind::ID:
1856+
#define ACCESSOR(ID, KEYWORD) case AccessorKind::ID:
18581857
case AccessorKind::DistributedGet:
18591858
#include "swift/AST/AccessorKinds.def"
18601859
llvm_unreachable("shouldn't be trying to build this accessor");

0 commit comments

Comments
 (0)