Files
swift-mirror/stdlib/core/Bool.swift
Jordan Rose cca27d02a0 Tag everything in the standard library with accessibility attributes.
Keep calm: remember that the standard library has many more public exports
than the average target, and that this contains ALL of them at once.
I also deliberately tried to tag nearly every top-level decl, even if that
was just to explicitly mark things @internal, to make sure I didn't miss
something.

This does export more than we might want to, mostly for protocol conformance
reasons, along with our simple-but-limiting typealias rule. I tried to also
mark things private where possible, but it's really going to be up to the
standard library owners to get this right. This is also only validated
against top-level access control; I haven't fully tested against member-level
access control yet, and none of our semantic restrictions are in place.

Along the way I also noticed bits of stdlib cruft; to keep this patch
understandable, I didn't change any of them.

Swift SVN r19145
2014-06-24 21:32:18 +00:00

136 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
init(_ v : Builtin.Int1) { value = v }
@public static var false : Bool {
@transparent
get {
return Bool()
}
}
@public static var true : Bool {
@transparent
get {
return Bool(Builtin.trunc_Word_Int1(1.value))
}
}
}
@public var true : Bool {
@transparent
get {
return Bool.true
}
}
@public var false : Bool {
@transparent
get {
return Bool.false
}
}
extension Bool : LogicValue {
@transparent 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
}