Skip to content

Commit 94ec683

Browse files
committed
Revert "Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift""
This reverts commit 69431f0
1 parent 8535e80 commit 94ec683

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

336336
final public class BuiltinInst : SingleValueInstruction {
337-
// TODO: find a way to directly reuse the BuiltinValueKind enum
338-
public enum ID {
339-
case none
340-
case destroyArray
341-
case stackAlloc
342-
}
337+
public typealias ID = swift.BuiltinValueKind
343338

344339
public var id: ID {
345-
switch BuiltinInst_getID(bridged) {
346-
case DestroyArrayBuiltin: return .destroyArray
347-
case StackAllocBuiltin: return .stackAlloc
348-
default: return .none
349-
}
340+
return BuiltinInst_getID(bridged)
350341
}
351342
}
352343

@@ -557,34 +548,12 @@ final public class BridgeObjectToRefInst : SingleValueInstruction,
557548
final public class BridgeObjectToWordInst : SingleValueInstruction,
558549
UnaryInstruction {}
559550

560-
public enum AccessKind {
561-
case initialize
562-
case read
563-
case modify
564-
case deinitialize
565-
}
566-
567-
extension BridgedAccessKind {
568-
var kind: AccessKind {
569-
switch self {
570-
case AccessKind_Init:
571-
return .initialize
572-
case AccessKind_Read:
573-
return .read
574-
case AccessKind_Modify:
575-
return .modify
576-
case AccessKind_Deinit:
577-
return .deinitialize
578-
default:
579-
fatalError("unsupported access kind")
580-
}
581-
}
582-
}
551+
public typealias AccessKind = swift.SILAccessKind
583552

584553

585554
// TODO: add support for begin_unpaired_access
586555
final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {
587-
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged).kind }
556+
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged) }
588557

589558
public var isStatic: Bool { BeginAccessInst_isStatic(bridged) != 0 }
590559
}

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
@@ -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;
@@ -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;
@@ -295,7 +277,7 @@ BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table);
295277
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table);
296278
BridgedArrayRef SILDefaultWitnessTable_getEntries(BridgedDefaultWitnessTable table);
297279
std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry);
298-
SILWitnessTableEntryKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
280+
swift::SILWitnessTable::WitnessKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
299281
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry);
300282

301283
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
@@ -375,7 +357,7 @@ BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
375357

376358
llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi);
377359
SwiftInt LoadInst_getLoadOwnership(BridgedInstruction load);
378-
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
360+
swift::BuiltinValueKind BuiltinInst_getID(BridgedInstruction bi);
379361
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp);
380362
SwiftInt IndexAddrInst_needsStackProtection(BridgedInstruction ia);
381363
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
@@ -406,7 +388,7 @@ BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
406388
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
407389
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
408390
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
409-
BridgedAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
391+
swift::SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
410392
SwiftInt BeginAccessInst_isStatic(BridgedInstruction beginAccess);
411393
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr);
412394
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)