Skip to content

Commit f88be05

Browse files
authored
Merge pull request #24240 from compnerd/bridge-to-terabithia
WIP: bridge BOOL to Bool
2 parents 57844d3 + 83b2904 commit f88be05

File tree

19 files changed

+220
-39
lines changed

19 files changed

+220
-39
lines changed

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ IDENTIFIER(type)
110110
IDENTIFIER(Value)
111111
IDENTIFIER(value)
112112
IDENTIFIER_WITH_NAME(value_, "_value")
113+
IDENTIFIER(WinSDK)
113114
IDENTIFIER(with)
114115
IDENTIFIER(withArguments)
115116
IDENTIFIER(withKeywordArguments)

include/swift/SIL/BridgedTypes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ BRIDGING_KNOWN_TYPE(Swift, Error)
5353
BRIDGE_TYPE(ObjectiveC, ObjCBool, Swift, Bool, false)
5454
BRIDGE_TYPE(Darwin, DarwinBoolean, Swift, Bool, false)
5555

56+
BRIDGING_KNOWN_TYPE(WinSDK, WindowsBool)
57+
BRIDGE_TYPE(WinSDK, WindowsBool, Swift, Bool, false)
58+
5659
#undef BRIDGING_KNOWN_TYPE
5760
#undef BRIDGE_TYPE
5861

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,12 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
39463946
addTrivial(getIdentifier("DarwinBoolean"), darwin);
39473947
}
39483948

3949+
if (auto winsdk = getLoadedModule(Id_WinSDK)) {
3950+
// NOTE: WindowsBool is odd because it is bridged to Bool in APIs, but can
3951+
// also be trivially bridged.
3952+
addTrivial(getIdentifier("WindowsBool"), winsdk);
3953+
}
3954+
39493955
if (auto objectiveC = getLoadedModule(Id_ObjectiveC)) {
39503956
addTrivial(Id_Selector, objectiveC, true);
39513957

@@ -4003,6 +4009,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
40034009
}
40044010
};
40054011
conditionallyAddTrivial(nominal, getIdentifier("DarwinBoolean") , Id_Darwin);
4012+
conditionallyAddTrivial(nominal, getIdentifier("WindowsBool"), Id_WinSDK);
40064013
conditionallyAddTrivial(nominal, Id_Selector, Id_ObjectiveC, true);
40074014
conditionallyAddTrivial(nominal, getIdentifier("ObjCBool"), Id_ObjectiveC);
40084015
conditionallyAddTrivial(nominal, getSwiftId(KnownFoundationEntity::NSZone), Id_ObjectiveC, true);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,16 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
226226
StringRef SwiftTypeName;
227227
bool CanBeMissing;
228228

229+
229230
do {
230231
#define MAP_TYPE(C_TYPE_NAME, C_TYPE_KIND, C_TYPE_BITWIDTH, \
231232
SWIFT_MODULE_NAME, SWIFT_TYPE_NAME, \
232233
CAN_BE_MISSING, C_NAME_MAPPING) \
233234
if (Name.str() == C_TYPE_NAME) { \
234235
CTypeKind = MappedCTypeKind::C_TYPE_KIND; \
235236
Bitwidth = C_TYPE_BITWIDTH; \
236-
if (StringRef(SWIFT_MODULE_NAME) == StringRef(STDLIB_NAME)) \
237-
IsSwiftModule = true; \
238-
else { \
239-
IsSwiftModule = false; \
240-
SwiftModuleName = SWIFT_MODULE_NAME; \
241-
} \
237+
SwiftModuleName = SWIFT_MODULE_NAME; \
238+
IsSwiftModule = SwiftModuleName == STDLIB_NAME; \
242239
SwiftTypeName = SWIFT_TYPE_NAME; \
243240
CanBeMissing = CAN_BE_MISSING; \
244241
NameMapping = MappedTypeNameKind::C_NAME_MAPPING; \
@@ -249,6 +246,58 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
249246
}
250247
#include "MappedTypes.def"
251248

249+
// We handle `BOOL` as a special case because the selection here is more
250+
// complicated as the type alias exists on multiple platforms as different
251+
// types. It appears in an Objective-C context where it is a `signed char`
252+
// and appears in Windows as an `int`. Furthermore, you can actually have
253+
// the two interoperate, which requires a further bit of logic to
254+
// disambiguate the type aliasing behaviour. To complicate things, the two
255+
// aliases bridge to different types - `ObjCBool` for Objective-C and
256+
// `WindowsBool` for Windows's `BOOL` type.
257+
if (Name.str() == "BOOL") {
258+
auto &CASTContext = Impl.getClangASTContext();
259+
auto &SwiftASTContext = Impl.SwiftContext;
260+
261+
// Default to Objective-C `BOOL`
262+
CTypeKind = MappedCTypeKind::ObjCBool;
263+
if (CASTContext.getTargetInfo().getTriple().isOSWindows()) {
264+
// On Windows fall back to Windows `BOOL`
265+
CTypeKind = MappedCTypeKind::SignedInt;
266+
// If Objective-C interop is enabled, and we match the Objective-C
267+
// `BOOL` type, then switch back to `ObjCBool`.
268+
if (SwiftASTContext.LangOpts.EnableObjCInterop &&
269+
CASTContext.hasSameType(D->getUnderlyingType(),
270+
CASTContext.ObjCBuiltinBoolTy))
271+
CTypeKind = MappedCTypeKind::ObjCBool;
272+
}
273+
274+
if (CTypeKind == MappedCTypeKind::ObjCBool) {
275+
Bitwidth = 8;
276+
SwiftModuleName = StringRef("ObjectiveC");
277+
IsSwiftModule = false;
278+
SwiftTypeName = "ObjCBool";
279+
NameMapping = MappedTypeNameKind::DoNothing;
280+
CanBeMissing = false;
281+
assert(verifyNameMapping(MappedTypeNameKind::DoNothing,
282+
"BOOL", "ObjCBool") &&
283+
"MappedTypes.def: Identical names must use DoNothing");
284+
} else {
285+
assert(CTypeKind == MappedCTypeKind::SignedInt &&
286+
"expected Windows `BOOL` desugared to `int`");
287+
Bitwidth = 32;
288+
SwiftModuleName = StringRef("WinSDK");
289+
IsSwiftModule = false;
290+
SwiftTypeName = "WindowsBool";
291+
NameMapping = MappedTypeNameKind::DoNothing;
292+
CanBeMissing = true;
293+
assert(verifyNameMapping(MappedTypeNameKind::DoNothing,
294+
"BOOL", "WindowsBool") &&
295+
"MappedTypes.def: Identical names must use DoNothing");
296+
}
297+
298+
break;
299+
}
300+
252301
// We did not find this type, thus it is not mapped.
253302
return std::make_pair(Type(), "");
254303
} while (0);

lib/ClangImporter/MappedTypes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ MAP_TYPE("__swift_shims_dispatch_data_t", ObjCId, 0, "Dispatch", "dispatch_data_
143143
true, DoNothing)
144144

145145
// Objective-C types.
146-
MAP_TYPE("BOOL", ObjCBool, 8, "ObjectiveC", "ObjCBool", false, DoNothing)
147146
MAP_TYPE("SEL", ObjCSel, 0, "ObjectiveC", "Selector", false, DoNothing)
148147
MAP_STDLIB_TYPE("Class", ObjCClass, 0, "AnyClass", false, DoNothing)
149148
MAP_STDLIB_TYPE(

lib/IRGen/GenClangType.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ clang::CanQualType GenClangType::visitStructType(CanStructType type) {
220220
CHECK_NAMED_TYPE(swiftDecl->getASTContext().getSwiftName(
221221
KnownFoundationEntity::NSZone),
222222
ctx.VoidPtrTy);
223+
CHECK_NAMED_TYPE("WindowsBool", ctx.IntTy);
223224
CHECK_NAMED_TYPE("ObjCBool", ctx.ObjCBuiltinBoolTy);
224225
CHECK_NAMED_TYPE("Selector", getClangSelectorType(ctx));
225226
CHECK_NAMED_TYPE("UnsafeRawPointer", ctx.VoidPtrTy);

lib/SIL/Bridging.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
140140
return t;
141141
if (builtinTy->getKind() == clang::BuiltinType::UChar)
142142
return getDarwinBooleanType();
143+
if (builtinTy->getKind() == clang::BuiltinType::Int)
144+
return getWindowsBoolType();
143145
assert(builtinTy->getKind() == clang::BuiltinType::SChar);
144146
return getObjCBoolType();
145147
}

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ GET_BRIDGING_FN(ObjectiveC, REQUIRED, Bool, REQUIRED, ObjCBool)
168168
GET_BRIDGING_FN(ObjectiveC, REQUIRED, ObjCBool, REQUIRED, Bool)
169169
GET_BRIDGING_FN(Foundation, OPTIONAL, NSError, REQUIRED, Error)
170170
GET_BRIDGING_FN(Foundation, REQUIRED, Error, REQUIRED, NSError)
171+
GET_BRIDGING_FN(WinSDK, REQUIRED, Bool, REQUIRED, WindowsBool)
172+
GET_BRIDGING_FN(WinSDK, REQUIRED, WindowsBool, REQUIRED, Bool)
171173

172174
#undef GET_BRIDGING_FN
173175
#undef REQUIRED

lib/SILGen/SILGen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
118118
Optional<SILDeclRef> DarwinBooleanToBoolFn;
119119
Optional<SILDeclRef> NSErrorToErrorFn;
120120
Optional<SILDeclRef> ErrorToNSErrorFn;
121+
Optional<SILDeclRef> BoolToWindowsBoolFn;
122+
Optional<SILDeclRef> WindowsBoolToBoolFn;
121123

122124
Optional<ProtocolDecl*> PointerProtocol;
123125

@@ -367,6 +369,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
367369
SILDeclRef getObjCBoolToBoolFn();
368370
SILDeclRef getBoolToDarwinBooleanFn();
369371
SILDeclRef getDarwinBooleanToBoolFn();
372+
SILDeclRef getBoolToWindowsBoolFn();
373+
SILDeclRef getWindowsBoolToBoolFn();
370374
SILDeclRef getNSErrorToErrorFn();
371375
SILDeclRef getErrorToNSErrorFn();
372376

lib/SILGen/SILGenBridging.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ static ManagedValue emitBridgeBoolToDarwinBoolean(SILGenFunction &SGF,
268268
return SGF.emitManagedRValueWithCleanup(result);
269269
}
270270

271+
static ManagedValue emitBridgeBoolToWindowsBool(SILGenFunction &SGF,
272+
SILLocation L, ManagedValue b) {
273+
// func _convertToWindowsBool(Bool) -> WindowsBool
274+
SILValue F = SGF.emitGlobalFunctionRef(L, SGF.SGM.getBoolToWindowsBoolFn());
275+
SILValue R = SGF.B.createApply(L, F, {}, b.forward(SGF), false);
276+
return SGF.emitManagedRValueWithCleanup(R);
277+
}
278+
271279
static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &SGF,
272280
SILLocation loc,
273281
ManagedValue foreignBool,
@@ -1002,7 +1010,7 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,
10021010
return SGF.emitOptionalToOptional(loc, v, loweredNativeTy, helper, C);
10031011
}
10041012

1005-
// Bridge Bool to ObjCBool or DarwinBoolean when requested.
1013+
// Bridge ObjCBool, DarwinBoolean, WindowsBool to Bool when requested.
10061014
if (nativeType == SGF.SGM.Types.getBoolType()) {
10071015
if (bridgedType == SGF.SGM.Types.getObjCBoolType()) {
10081016
return emitBridgeForeignBoolToBool(SGF, loc, v,
@@ -1012,6 +1020,10 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,
10121020
return emitBridgeForeignBoolToBool(SGF, loc, v,
10131021
SGF.SGM.getDarwinBooleanToBoolFn());
10141022
}
1023+
if (bridgedType == SGF.SGM.Types.getWindowsBoolType()) {
1024+
return emitBridgeForeignBoolToBool(SGF, loc, v,
1025+
SGF.SGM.getWindowsBoolToBoolFn());
1026+
}
10151027
}
10161028

10171029
// Bridge Objective-C to thick metatypes.

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ class _InterruptibleSleep {
517517
var completed: Bool = false
518518

519519
init() {
520-
self.event = CreateEventW(nil, TRUE, FALSE, nil)
520+
self.event = CreateEventW(nil, true, false, nil)
521521
precondition(self.event != nil)
522522
}
523523

@@ -536,8 +536,8 @@ class _InterruptibleSleep {
536536

537537
func wake() {
538538
guard completed == false else { return }
539-
let result: BOOL = SetEvent(self.event)
540-
precondition(result == TRUE)
539+
let result: Bool = SetEvent(self.event)
540+
precondition(result == true)
541541
}
542542
}
543543
#else

stdlib/private/SwiftPrivate/IO.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ public struct _FDOutputStream : TextOutputStream {
175175
var dwOffset: DWORD = 0
176176
while dwOffset < dwLength {
177177
var dwBytesWritten: DWORD = 0
178-
if WriteFile(handle,
179-
UnsafeRawPointer(buffer.baseAddress! + Int(dwOffset)),
180-
dwLength - dwOffset, &dwBytesWritten, nil) == FALSE {
178+
if !WriteFile(handle,
179+
UnsafeRawPointer(buffer.baseAddress! + Int(dwOffset)),
180+
dwLength - dwOffset, &dwBytesWritten, nil) {
181181
fatalError("WriteFile() failed")
182182
}
183183
dwOffset += dwBytesWritten

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,27 @@ public func spawnChild(_ args: [String])
5959

6060
var saAttributes: SECURITY_ATTRIBUTES = SECURITY_ATTRIBUTES()
6161
saAttributes.nLength = DWORD(MemoryLayout<SECURITY_ATTRIBUTES>.size)
62-
saAttributes.bInheritHandle = TRUE
62+
saAttributes.bInheritHandle = true
6363
saAttributes.lpSecurityDescriptor = nil
6464

65-
if CreatePipe(&_stdin.read, &_stdin.write, &saAttributes, 0) == FALSE {
65+
if !CreatePipe(&_stdin.read, &_stdin.write, &saAttributes, 0) {
6666
fatalError("CreatePipe() failed")
6767
}
68-
if SetHandleInformation(_stdin.write, HANDLE_FLAG_INHERIT, 0) == FALSE {
68+
if !SetHandleInformation(_stdin.write, HANDLE_FLAG_INHERIT, 0) {
6969
fatalError("SetHandleInformation() failed")
7070
}
7171

72-
if CreatePipe(&_stdout.read, &_stdout.write, &saAttributes, 0) == FALSE {
72+
if !CreatePipe(&_stdout.read, &_stdout.write, &saAttributes, 0) {
7373
fatalError("CreatePipe() failed")
7474
}
75-
if SetHandleInformation(_stdout.read, HANDLE_FLAG_INHERIT, 0) == FALSE {
75+
if !SetHandleInformation(_stdout.read, HANDLE_FLAG_INHERIT, 0) {
7676
fatalError("SetHandleInformation() failed")
7777
}
7878

79-
if CreatePipe(&_stderr.read, &_stderr.write, &saAttributes, 0) == FALSE {
79+
if !CreatePipe(&_stderr.read, &_stderr.write, &saAttributes, 0) {
8080
fatalError("CreatePipe() failed")
8181
}
82-
if SetHandleInformation(_stderr.read, HANDLE_FLAG_INHERIT, 0) == FALSE {
82+
if !SetHandleInformation(_stderr.read, HANDLE_FLAG_INHERIT, 0) {
8383
fatalError("SetHandleInformation() failed")
8484
}
8585

@@ -98,21 +98,21 @@ public func spawnChild(_ args: [String])
9898
let command: String =
9999
([CommandLine.arguments[0]] + args).joined(separator: " ")
100100
command.withCString(encodedAs: UTF16.self) { cString in
101-
if CreateProcessW(nil, UnsafeMutablePointer<WCHAR>(mutating: cString),
102-
nil, nil, TRUE, 0, nil, nil,
103-
&siStartupInfo, &piProcessInfo) == FALSE {
101+
if !CreateProcessW(nil, UnsafeMutablePointer<WCHAR>(mutating: cString),
102+
nil, nil, true, 0, nil, nil,
103+
&siStartupInfo, &piProcessInfo) {
104104
let dwError: DWORD = GetLastError()
105105
fatalError("CreateProcessW() failed \(dwError)")
106106
}
107107
}
108108

109-
if CloseHandle(_stdin.read) == FALSE {
109+
if !CloseHandle(_stdin.read) {
110110
fatalError("CloseHandle() failed")
111111
}
112-
if CloseHandle(_stdout.write) == FALSE {
112+
if !CloseHandle(_stdout.write) {
113113
fatalError("CloseHandle() failed")
114114
}
115-
if CloseHandle(_stderr.write) == FALSE {
115+
if !CloseHandle(_stderr.write) {
116116
fatalError("CloseHandle() failed")
117117
}
118118

@@ -132,7 +132,7 @@ public func waitProcess(_ process: HANDLE) -> ProcessTerminationStatus {
132132
}
133133

134134
var status: DWORD = 0
135-
if GetExitCodeProcess(process, &status) == FALSE {
135+
if !GetExitCodeProcess(process, &status) {
136136
fatalError("GetExitCodeProcess() failed")
137137
}
138138

stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public func _stdlib_thread_barrier_wait(
122122
if barrier.pointee.numThreadsWaiting < barrier.pointee.count {
123123
// Put the thread to sleep.
124124
#if os(Windows)
125-
if SleepConditionVariableSRW(barrier.pointee.cond!, barrier.pointee.mutex!,
126-
INFINITE, 0) == 0 {
125+
if !SleepConditionVariableSRW(barrier.pointee.cond!, barrier.pointee.mutex!,
126+
INFINITE, 0) {
127127
return -1
128128
}
129129
ReleaseSRWLockExclusive(barrier.pointee.mutex!)

0 commit comments

Comments
 (0)