Skip to content

Commit c87d7ae

Browse files
authored
Merge pull request #61153 from apple/egorzhdan/scs-enum
[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift
2 parents e979dc2 + 5f2a641 commit c87d7ae

File tree

11 files changed

+39
-116
lines changed

11 files changed

+39
-116
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/FunctionUses.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ struct FunctionUses {
134134

135135
for witnessTable in context.witnessTables {
136136
for entry in witnessTable.entries {
137-
if entry.kind == .method, let f = entry.methodFunction {
137+
if entry.kind == .Method, let f = entry.methodFunction {
138138
markUnknown(f)
139139
}
140140
}
141141
}
142142

143143
for witnessTable in context.defaultWitnessTables {
144144
for entry in witnessTable.entries {
145-
if entry.kind == .method, let f = entry.methodFunction {
145+
if entry.kind == .Method, let f = entry.methodFunction {
146146
markUnknown(f)
147147
}
148148
}

SwiftCompilerSources/Sources/Optimizer/ModulePasses/StackProtection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private struct StackProtectionOptimization {
124124
mustFixStackNesting: inout Bool, _ context: PassContext) {
125125

126126
// `withUnsafeTemporaryAllocation(of:capacity:_:)` is compiled to a `builtin "stackAlloc"`.
127-
if let bi = instruction as? BuiltinInst, bi.id == .stackAlloc {
127+
if let bi = instruction as? BuiltinInst, bi.id == .StackAlloc {
128128
function.setNeedsStackProtection(context)
129129
return
130130
}
@@ -332,7 +332,7 @@ private struct StackProtectionOptimization {
332332
/// Moves the value of a `beginAccess` to a temporary stack location, if possible.
333333
private func moveToTemporary(scope beginAccess: BeginAccessInst, mustFixStackNesting: inout Bool,
334334
_ context: PassContext) {
335-
if beginAccess.accessKind != .modify {
335+
if beginAccess.accessKind != .Modify {
336336
// We can only move from a `modify` access.
337337
// Also, read-only accesses shouldn't be subject to buffer overflows (because
338338
// no one should ever write to such a storage).

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
422422
return walkDownUses(ofAddress: pta, path: path.with(knownType: nil))
423423
case let bi as BuiltinInst:
424424
switch bi.id {
425-
case .destroyArray:
425+
case .DestroyArray:
426426
// If it's not the array base pointer operand -> bail. Though, that shouldn't happen
427427
// because the other operands (metatype, count) shouldn't be visited anyway.
428428
if operand.index != 1 { return isEscaping }

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,10 @@ final public class LoadUnownedInst : SingleValueInstruction, UnaryInstruction {}
319319
final public class LoadBorrowInst : SingleValueInstruction, UnaryInstruction {}
320320

321321
final public class BuiltinInst : SingleValueInstruction {
322-
// TODO: find a way to directly reuse the BuiltinValueKind enum
323-
public enum ID {
324-
case none
325-
case destroyArray
326-
case stackAlloc
327-
}
322+
public typealias ID = swift.BuiltinValueKind
328323

329324
public var id: ID {
330-
switch BuiltinInst_getID(bridged) {
331-
case DestroyArrayBuiltin: return .destroyArray
332-
case StackAllocBuiltin: return .stackAlloc
333-
default: return .none
334-
}
325+
return BuiltinInst_getID(bridged)
335326
}
336327
}
337328

@@ -542,34 +533,12 @@ final public class BridgeObjectToRefInst : SingleValueInstruction,
542533
final public class BridgeObjectToWordInst : SingleValueInstruction,
543534
UnaryInstruction {}
544535

545-
public enum AccessKind {
546-
case initialize
547-
case read
548-
case modify
549-
case deinitialize
550-
}
551-
552-
extension BridgedAccessKind {
553-
var kind: AccessKind {
554-
switch self {
555-
case AccessKind_Init:
556-
return .initialize
557-
case AccessKind_Read:
558-
return .read
559-
case AccessKind_Modify:
560-
return .modify
561-
case AccessKind_Deinit:
562-
return .deinitialize
563-
default:
564-
fatalError("unsupported access kind")
565-
}
566-
}
567-
}
536+
public typealias AccessKind = swift.SILAccessKind
568537

569538

570539
// TODO: add support for begin_unpaired_access
571540
final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {
572-
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged).kind }
541+
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged) }
573542
}
574543

575544
public protocol ScopedInstruction {

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,14 @@ public struct WitnessTable : CustomStringConvertible, CustomReflectable {
2020
public struct Entry : CustomStringConvertible, CustomReflectable {
2121
fileprivate let bridged: BridgedWitnessTableEntry
2222

23-
public enum Kind {
24-
case invalid
25-
case method
26-
case associatedType
27-
case associatedTypeProtocol
28-
case baseProtocol
29-
}
23+
public typealias Kind = swift.SILWitnessTable.WitnessKind
3024

3125
public var kind: Kind {
32-
switch SILWitnessTableEntry_getKind(bridged) {
33-
case SILWitnessTableEntry_Invalid: return .invalid
34-
case SILWitnessTableEntry_Method: return .method
35-
case SILWitnessTableEntry_AssociatedType: return .associatedType
36-
case SILWitnessTableEntry_AssociatedTypeProtocol: return .associatedTypeProtocol
37-
case SILWitnessTableEntry_BaseProtocol: return .baseProtocol
38-
default:
39-
fatalError("unknown witness table kind")
40-
}
26+
return SILWitnessTableEntry_getKind(bridged)
4127
}
4228

4329
public var methodFunction: Function? {
44-
assert(kind == .method)
30+
assert(kind == .Method)
4531
return SILWitnessTableEntry_getMethodFunction(bridged).function
4632
}
4733

include/swift/AST/ASTBridging.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2525
// Diagnostic Engine
2626
//===----------------------------------------------------------------------===//
2727

28-
// TODO: Move this to somewhere common header.
29-
#if __has_attribute(enum_extensibility)
30-
#define ENUM_EXTENSIBILITY_ATTR(arg) __attribute__((enum_extensibility(arg)))
31-
#else
32-
#define ENUM_EXTENSIBILITY_ATTR(arg)
33-
#endif
34-
3528
// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined
3629
// in 'DiagnosticList.cpp'.
3730
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {

include/swift/Basic/Compiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,10 @@
187187
#define SWIFT_IMPORT_REFERENCE
188188
#endif
189189

190+
#if __has_attribute(enum_extensibility)
191+
#define ENUM_EXTENSIBILITY_ATTR(arg) __attribute__((enum_extensibility(arg)))
192+
#else
193+
#define ENUM_EXTENSIBILITY_ATTR(arg)
194+
#endif
195+
190196
#endif // SWIFT_BASIC_COMPILER_H

include/swift/SIL/SILBridging.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
#include "swift/Basic/BasicBridging.h"
1717
#include "swift/Basic/BridgedSwiftObject.h"
18+
#include "swift/AST/Builtins.h"
1819
#include "swift/AST/SubstitutionMap.h"
20+
#include "swift/SIL/SILInstruction.h"
1921
#include "swift/SIL/SILLocation.h"
22+
#include "swift/SIL/SILWitnessTable.h"
2023
#include <stdbool.h>
2124
#include <stddef.h>
2225
#include <string>
@@ -94,14 +97,6 @@ typedef struct {
9497
const void * _Nonnull ptr;
9598
} BridgedWitnessTableEntry;
9699

97-
typedef enum {
98-
SILWitnessTableEntry_Invalid,
99-
SILWitnessTableEntry_Method,
100-
SILWitnessTableEntry_AssociatedType,
101-
SILWitnessTableEntry_AssociatedTypeProtocol,
102-
SILWitnessTableEntry_BaseProtocol
103-
} SILWitnessTableEntryKind;
104-
105100
typedef struct {
106101
SwiftObject obj;
107102
} BridgedFunction;
@@ -175,13 +170,6 @@ typedef enum {
175170
MayHaveSideEffectsBehavior
176171
} BridgedMemoryBehavior;
177172

178-
typedef enum {
179-
AccessKind_Init,
180-
AccessKind_Read,
181-
AccessKind_Modify,
182-
AccessKind_Deinit
183-
} BridgedAccessKind;
184-
185173
typedef enum {
186174
Ownership_Unowned,
187175
Ownership_Owned,
@@ -203,12 +191,6 @@ typedef enum {
203191

204192
// AST bridging
205193

206-
typedef enum {
207-
UnknownBuiltin = 0,
208-
#define BUILTIN(Id, Name, Attrs) Id##Builtin,
209-
#include "swift/AST/Builtins.def"
210-
} BridgedBuiltinID;
211-
212194
struct BridgedEffectInfo {
213195
SwiftInt argumentIndex;
214196
bool isDerived;
@@ -276,7 +258,7 @@ BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table);
276258
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table);
277259
BridgedArrayRef SILDefaultWitnessTable_getEntries(BridgedDefaultWitnessTable table);
278260
std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry);
279-
SILWitnessTableEntryKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
261+
swift::SILWitnessTable::WitnessKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
280262
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry);
281263

282264
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
@@ -352,7 +334,7 @@ BridgedMultiValueResult
352334
BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
353335

354336
llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi);
355-
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
337+
swift::BuiltinValueKind BuiltinInst_getID(BridgedInstruction bi);
356338
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp);
357339
SwiftInt IndexAddrInst_needsStackProtection(BridgedInstruction ia);
358340
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
@@ -383,7 +365,7 @@ BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
383365
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
384366
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
385367
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
386-
BridgedAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
368+
swift::SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
387369
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr);
388370
SwiftInt CopyAddrInst_isInitializationOfDest(BridgedInstruction copyAddr);
389371
void RefCountingInst_setIsAtomic(BridgedInstruction rc, bool isAtomic);

include/swift/SIL/SILWitnessTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
9090
AssociatedType,
9191
AssociatedTypeProtocol,
9292
BaseProtocol
93-
};
93+
} ENUM_EXTENSIBILITY_ATTR(open);
9494

9595
/// A witness table entry.
9696
class Entry {

include/swift/module.modulemap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ module BasicBridging {
77
}
88

99
module ASTBridging {
10+
header "AST/AnyFunctionRef.h"
1011
header "AST/ASTBridging.h"
12+
header "AST/Builtins.h"
1113
header "AST/DiagnosticEngine.h"
1214
header "AST/DiagnosticConsumer.h"
15+
header "AST/ForeignAsyncConvention.h"
16+
header "AST/ForeignErrorConvention.h"
1317
header "AST/SubstitutionMap.h"
18+
19+
textual header "AST/Builtins.def"
20+
1421
requires cplusplus
1522
export *
1623
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -653,19 +653,9 @@ std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry
653653
return str;
654654
}
655655

656-
SILWitnessTableEntryKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry) {
657-
switch (castToWitnessTableEntry(entry)->getKind()) {
658-
case SILWitnessTable::Invalid:
659-
return SILWitnessTableEntry_Invalid;
660-
case SILWitnessTable::Method:
661-
return SILWitnessTableEntry_Method;
662-
case SILWitnessTable::AssociatedType:
663-
return SILWitnessTableEntry_AssociatedType;
664-
case SILWitnessTable::AssociatedTypeProtocol:
665-
return SILWitnessTableEntry_AssociatedTypeProtocol;
666-
case SILWitnessTable::BaseProtocol:
667-
return SILWitnessTableEntry_BaseProtocol;
668-
}
656+
SILWitnessTable::WitnessKind
657+
SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry) {
658+
return castToWitnessTableEntry(entry)->getKind();
669659
}
670660

671661
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry) {
@@ -751,8 +741,8 @@ llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi) {
751741
return castToInst<CondFailInst>(cfi)->getMessage();
752742
}
753743

754-
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi) {
755-
return (BridgedBuiltinID)castToInst<BuiltinInst>(bi)->getBuiltinInfo().ID;
744+
BuiltinValueKind BuiltinInst_getID(BridgedInstruction bi) {
745+
return castToInst<BuiltinInst>(bi)->getBuiltinInfo().ID;
756746
}
757747

758748
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp) {
@@ -877,18 +867,8 @@ SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store) {
877867
return (SwiftInt)castToInst<StoreInst>(store)->getOwnershipQualifier();
878868
}
879869

880-
BridgedAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess) {
881-
auto kind = castToInst<BeginAccessInst>(beginAccess)->getAccessKind();
882-
switch (kind) {
883-
case SILAccessKind::Init:
884-
return BridgedAccessKind::AccessKind_Init;
885-
case SILAccessKind::Read:
886-
return BridgedAccessKind::AccessKind_Read;
887-
case SILAccessKind::Modify:
888-
return BridgedAccessKind::AccessKind_Modify;
889-
case SILAccessKind::Deinit:
890-
return BridgedAccessKind::AccessKind_Deinit;
891-
}
870+
SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess) {
871+
return castToInst<BeginAccessInst>(beginAccess)->getAccessKind();
892872
}
893873

894874
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr) {

0 commit comments

Comments
 (0)