Skip to content

Commit a6372e9

Browse files
authored
Merge pull request #61618 from apple/egorzhdan/scs-reapply-enums
Revert "Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift""
2 parents 510c9c9 + 94ec683 commit a6372e9

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
@@ -440,7 +440,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
440440
return walkDownUses(ofAddress: pta, path: path.with(knownType: nil))
441441
case let bi as BuiltinInst:
442442
switch bi.id {
443-
case .destroyArray:
443+
case .DestroyArray:
444444
// If it's not the array base pointer operand -> bail. Though, that shouldn't happen
445445
// because the other operands (metatype, count) shouldn't be visited anyway.
446446
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
@@ -369,19 +369,10 @@ final public class LoadUnownedInst : SingleValueInstruction, UnaryInstruction {}
369369
final public class LoadBorrowInst : SingleValueInstruction, UnaryInstruction {}
370370

371371
final public class BuiltinInst : SingleValueInstruction {
372-
// TODO: find a way to directly reuse the BuiltinValueKind enum
373-
public enum ID {
374-
case none
375-
case destroyArray
376-
case stackAlloc
377-
}
372+
public typealias ID = swift.BuiltinValueKind
378373

379374
public var id: ID {
380-
switch BuiltinInst_getID(bridged) {
381-
case DestroyArrayBuiltin: return .destroyArray
382-
case StackAllocBuiltin: return .stackAlloc
383-
default: return .none
384-
}
375+
return BuiltinInst_getID(bridged)
385376
}
386377
}
387378

@@ -592,34 +583,12 @@ final public class BridgeObjectToRefInst : SingleValueInstruction,
592583
final public class BridgeObjectToWordInst : SingleValueInstruction,
593584
UnaryInstruction {}
594585

595-
public enum AccessKind {
596-
case initialize
597-
case read
598-
case modify
599-
case deinitialize
600-
}
601-
602-
extension BridgedAccessKind {
603-
var kind: AccessKind {
604-
switch self {
605-
case AccessKind_Init:
606-
return .initialize
607-
case AccessKind_Read:
608-
return .read
609-
case AccessKind_Modify:
610-
return .modify
611-
case AccessKind_Deinit:
612-
return .deinitialize
613-
default:
614-
fatalError("unsupported access kind")
615-
}
616-
}
617-
}
586+
public typealias AccessKind = swift.SILAccessKind
618587

619588

620589
// TODO: add support for begin_unpaired_access
621590
final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {
622-
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged).kind }
591+
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged) }
623592

624593
public var isStatic: Bool { BeginAccessInst_isStatic(bridged) != 0 }
625594
}

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, NoReflectionChildren {
2020
public struct Entry : CustomStringConvertible, NoReflectionChildren {
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
@@ -186,4 +186,10 @@
186186
#define SWIFT_IMPORT_REFERENCE
187187
#endif
188188

189+
#if __has_attribute(enum_extensibility)
190+
#define ENUM_EXTENSIBILITY_ATTR(arg) __attribute__((enum_extensibility(arg)))
191+
#else
192+
#define ENUM_EXTENSIBILITY_ATTR(arg)
193+
#endif
194+
189195
#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;
@@ -182,13 +177,6 @@ typedef enum {
182177
EffectKind_releaseNone,
183178
} BridgedEffectAttributeKind;
184179

185-
typedef enum {
186-
AccessKind_Init,
187-
AccessKind_Read,
188-
AccessKind_Modify,
189-
AccessKind_Deinit
190-
} BridgedAccessKind;
191-
192180
typedef enum {
193181
Ownership_Unowned,
194182
Ownership_Owned,
@@ -210,12 +198,6 @@ typedef enum {
210198

211199
// AST bridging
212200

213-
typedef enum {
214-
UnknownBuiltin = 0,
215-
#define BUILTIN(Id, Name, Attrs) Id##Builtin,
216-
#include "swift/AST/Builtins.def"
217-
} BridgedBuiltinID;
218-
219201
struct BridgedEffectInfo {
220202
SwiftInt argumentIndex;
221203
bool isDerived;
@@ -297,7 +279,7 @@ BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table);
297279
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table);
298280
BridgedArrayRef SILDefaultWitnessTable_getEntries(BridgedDefaultWitnessTable table);
299281
std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry);
300-
SILWitnessTableEntryKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
282+
swift::SILWitnessTable::WitnessKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
301283
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry);
302284

303285
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
@@ -378,7 +360,7 @@ BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
378360

379361
llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi);
380362
SwiftInt LoadInst_getLoadOwnership(BridgedInstruction load);
381-
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
363+
swift::BuiltinValueKind BuiltinInst_getID(BridgedInstruction bi);
382364
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp);
383365
SwiftInt IndexAddrInst_needsStackProtection(BridgedInstruction ia);
384366
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
@@ -409,7 +391,7 @@ BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
409391
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
410392
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
411393
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
412-
BridgedAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
394+
swift::SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
413395
SwiftInt BeginAccessInst_isStatic(BridgedInstruction beginAccess);
414396
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr);
415397
SwiftInt CopyAddrInst_isInitializationOfDest(BridgedInstruction copyAddr);

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
}

0 commit comments

Comments
 (0)