Skip to content

Commit 69431f0

Browse files
authored
Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift"
1 parent d015505 commit 69431f0

File tree

11 files changed

+116
-39
lines changed

11 files changed

+116
-39
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: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,19 @@ final public class LoadUnownedInst : SingleValueInstruction, UnaryInstruction {}
319319
final public class LoadBorrowInst : SingleValueInstruction, UnaryInstruction {}
320320

321321
final public class BuiltinInst : SingleValueInstruction {
322-
public typealias ID = swift.BuiltinValueKind
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+
}
323328

324329
public var id: ID {
325-
return BuiltinInst_getID(bridged)
330+
switch BuiltinInst_getID(bridged) {
331+
case DestroyArrayBuiltin: return .destroyArray
332+
case StackAllocBuiltin: return .stackAlloc
333+
default: return .none
334+
}
326335
}
327336
}
328337

@@ -533,12 +542,34 @@ final public class BridgeObjectToRefInst : SingleValueInstruction,
533542
final public class BridgeObjectToWordInst : SingleValueInstruction,
534543
UnaryInstruction {}
535544

536-
public typealias AccessKind = swift.SILAccessKind
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+
}
537568

538569

539570
// TODO: add support for begin_unpaired_access
540571
final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {
541-
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged) }
572+
public var accessKind: AccessKind { BeginAccessInst_getAccessKind(bridged).kind }
542573
}
543574

544575
public protocol ScopedInstruction {

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

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

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

2531
public var kind: Kind {
26-
return SILWitnessTableEntry_getKind(bridged)
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+
}
2741
}
2842

2943
public var methodFunction: Function? {
30-
assert(kind == .Method)
44+
assert(kind == .method)
3145
return SILWitnessTableEntry_getMethodFunction(bridged).function
3246
}
3347

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ 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+
2835
// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined
2936
// in 'DiagnosticList.cpp'.
3037
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {

include/swift/Basic/Compiler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,4 @@
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-
196190
#endif // SWIFT_BASIC_COMPILER_H

include/swift/SIL/SILBridging.h

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

1616
#include "swift/Basic/BasicBridging.h"
1717
#include "swift/Basic/BridgedSwiftObject.h"
18-
#include "swift/AST/Builtins.h"
1918
#include "swift/AST/SubstitutionMap.h"
20-
#include "swift/SIL/SILInstruction.h"
2119
#include "swift/SIL/SILLocation.h"
22-
#include "swift/SIL/SILWitnessTable.h"
2320
#include <stdbool.h>
2421
#include <stddef.h>
2522
#include <string>
@@ -97,6 +94,14 @@ typedef struct {
9794
const void * _Nonnull ptr;
9895
} BridgedWitnessTableEntry;
9996

97+
typedef enum {
98+
SILWitnessTableEntry_Invalid,
99+
SILWitnessTableEntry_Method,
100+
SILWitnessTableEntry_AssociatedType,
101+
SILWitnessTableEntry_AssociatedTypeProtocol,
102+
SILWitnessTableEntry_BaseProtocol
103+
} SILWitnessTableEntryKind;
104+
100105
typedef struct {
101106
SwiftObject obj;
102107
} BridgedFunction;
@@ -170,6 +175,13 @@ typedef enum {
170175
MayHaveSideEffectsBehavior
171176
} BridgedMemoryBehavior;
172177

178+
typedef enum {
179+
AccessKind_Init,
180+
AccessKind_Read,
181+
AccessKind_Modify,
182+
AccessKind_Deinit
183+
} BridgedAccessKind;
184+
173185
typedef enum {
174186
Ownership_Unowned,
175187
Ownership_Owned,
@@ -191,6 +203,12 @@ typedef enum {
191203

192204
// AST bridging
193205

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

264282
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
@@ -334,7 +352,7 @@ BridgedMultiValueResult
334352
BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
335353

336354
llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi);
337-
swift::BuiltinValueKind BuiltinInst_getID(BridgedInstruction bi);
355+
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
338356
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp);
339357
SwiftInt IndexAddrInst_needsStackProtection(BridgedInstruction ia);
340358
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
@@ -365,7 +383,7 @@ BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
365383
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
366384
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
367385
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
368-
swift::SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
386+
BridgedAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess);
369387
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr);
370388
SwiftInt CopyAddrInst_isInitializationOfDest(BridgedInstruction copyAddr);
371389
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-
} ENUM_EXTENSIBILITY_ATTR(open);
93+
};
9494

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

include/swift/module.modulemap

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

99
module ASTBridging {
10-
header "AST/AnyFunctionRef.h"
1110
header "AST/ASTBridging.h"
12-
header "AST/Builtins.h"
1311
header "AST/DiagnosticEngine.h"
1412
header "AST/DiagnosticConsumer.h"
15-
header "AST/ForeignAsyncConvention.h"
16-
header "AST/ForeignErrorConvention.h"
1713
header "AST/SubstitutionMap.h"
18-
19-
textual header "AST/Builtins.def"
20-
2114
requires cplusplus
2215
export *
2316
}

lib/SIL/Utils/SILBridging.cpp

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

656-
SILWitnessTable::WitnessKind
657-
SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry) {
658-
return castToWitnessTableEntry(entry)->getKind();
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+
}
659669
}
660670

661671
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry) {
@@ -741,8 +751,8 @@ llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi) {
741751
return castToInst<CondFailInst>(cfi)->getMessage();
742752
}
743753

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

748758
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp) {
@@ -867,8 +877,18 @@ SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store) {
867877
return (SwiftInt)castToInst<StoreInst>(store)->getOwnershipQualifier();
868878
}
869879

870-
SILAccessKind BeginAccessInst_getAccessKind(BridgedInstruction beginAccess) {
871-
return castToInst<BeginAccessInst>(beginAccess)->getAccessKind();
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+
}
872892
}
873893

874894
SwiftInt CopyAddrInst_isTakeOfSrc(BridgedInstruction copyAddr) {

0 commit comments

Comments
 (0)