mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Introduce the new BooleanLiteralConvertible protocol for Boolean literals. Take "true" and "false" as real keywords (which is most of the reason for the testsuite churn). Make Bool BooleanLiteralConvertible and the default Boolean literal type, and ObjCBool BooleanLiteralConvertible. Fixes <rdar://problem/17405310> and the recent regression that made ObjCBool not work with true/false. Swift SVN r19728
123 lines
3.2 KiB
Swift
123 lines
3.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// Bool Datatype and Supporting Operators
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bool is the standard way to reason about truth values.
|
|
public struct Bool {
|
|
var value: Builtin.Int1
|
|
|
|
/// Default-initialize Boolean value to `false`.
|
|
@transparent public
|
|
init() { value = Builtin.trunc_Word_Int1(0.value) }
|
|
|
|
@transparent public
|
|
init(_ v : Builtin.Int1) { value = v }
|
|
}
|
|
|
|
extension Bool : _BuiltinBooleanLiteralConvertible, BooleanLiteralConvertible {
|
|
@transparent
|
|
public static func _convertFromBuiltinBooleanLiteral(value: Builtin.Int1)
|
|
-> Bool {
|
|
return Bool(value)
|
|
}
|
|
|
|
@transparent
|
|
public static func convertFromBooleanLiteral(value: Bool) -> Bool {
|
|
return value
|
|
}
|
|
}
|
|
|
|
extension Bool : LogicValue {
|
|
@transparent public func _getBuiltinLogicValue() -> Builtin.Int1 {
|
|
return value
|
|
}
|
|
|
|
@transparent public func getLogicValue() -> Bool { return self }
|
|
|
|
// Bool can be constructed from LogicValue
|
|
public init(_ v : LogicValue) {
|
|
self = v.getLogicValue()
|
|
}
|
|
}
|
|
|
|
extension Bool : Printable {
|
|
public var description: String {
|
|
return self ? "true" : "false"
|
|
}
|
|
}
|
|
|
|
// This is a magic entrypoint known to the compiler.
|
|
@transparent func _getBool(v: Builtin.Int1) -> Bool { return Bool(v) }
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Standard Operators
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unary bitwise complement.
|
|
@prefix @transparent public
|
|
func ~(a: Bool) -> Bool {
|
|
return a ^ true
|
|
}
|
|
|
|
// Unary logical complement.
|
|
@prefix @transparent public
|
|
func !(a: Bool) -> Bool {
|
|
return ~a
|
|
}
|
|
|
|
@transparent public
|
|
func ==(lhs: Bool, rhs: Bool) -> Bool {
|
|
return Bool(Builtin.cmp_eq_Int1(lhs.value, rhs.value))
|
|
}
|
|
|
|
@transparent
|
|
extension Bool : Equatable, Hashable {
|
|
public var hashValue: Int {
|
|
return self ? 1 : 0
|
|
}
|
|
}
|
|
|
|
// Bitwise 'and'.
|
|
@transparent public func & (lhs: Bool, rhs: Bool) -> Bool {
|
|
return Bool(Builtin.and_Int1(lhs.value, rhs.value))
|
|
}
|
|
|
|
// Bitwise 'xor'.
|
|
@transparent public func ^ (lhs: Bool, rhs: Bool) -> Bool {
|
|
return Bool(Builtin.xor_Int1(lhs.value, rhs.value))
|
|
}
|
|
|
|
// Bitwise 'or'.
|
|
@transparent public func | (lhs: Bool, rhs: Bool) -> Bool {
|
|
return Bool(Builtin.or_Int1(lhs.value, rhs.value))
|
|
}
|
|
|
|
// Compound assignment (with bitwise and)
|
|
@assignment @transparent public
|
|
func &= (inout lhs: Bool, rhs: Bool) {
|
|
lhs = lhs & rhs
|
|
}
|
|
|
|
// Compound assignment (with bitwise or)
|
|
@assignment @transparent public
|
|
func |= (inout lhs: Bool, rhs: Bool) {
|
|
lhs = lhs | rhs
|
|
}
|
|
|
|
// Compound assignment (with bitwise xor)
|
|
@assignment @transparent public
|
|
func ^= (inout lhs: Bool, rhs: Bool) {
|
|
lhs = lhs ^ rhs
|
|
}
|
|
|