mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
assert() and fatalError() These functions are meant to be used in user code. They are enabled in debug mode and disabled in release or fast mode. _precondition() and _preconditionFailure() These functions are meant to be used in library code to check preconditions at the api boundry. They are enabled in debug mode (with a verbose message) and release mode (trap). In fast mode they are disabled. _debugPrecondition() and _debugPreconditionFailure() These functions are meant to be used in library code to check preconditions that are not neccesarily comprehensive for safety (UnsafePointer can be null or an invalid pointer but we can't check both). They are enabled only in debug mode. _sanityCheck() and _fatalError() These are meant to be used for internal consistency checks. They are only enabled when the library is build with -DSWIFT_STDLIB_INTERNAL_CHECKS=ON. I modified the code in the standard library to the best of my judgement. rdar://16477198 Swift SVN r18212
122 lines
3.6 KiB
Swift
122 lines
3.6 KiB
Swift
%# -*- mode: swift -*-
|
|
//===--- IntegerArithmetic.swift.gyb --------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
%# Ignore the following admonition; it applies to the resulting .swift file only
|
|
|
|
// Automatically Generated From IntegerArithmetic.swift.gyb. Do Not
|
|
// Edit Directly!
|
|
|
|
% integerBinaryOps = [
|
|
% (x[:-1], x[-1]) for x in
|
|
% 'Add+ Subtract- Multiply* Divide/ Modulus%'.split()]
|
|
|
|
protocol _IntegerArithmetic {
|
|
% for name,_ in integerBinaryOps:
|
|
class func unchecked${name}(lhs: Self, _ rhs: Self) -> (Self, Bool)
|
|
% end
|
|
}
|
|
|
|
protocol IntegerArithmetic : _IntegerArithmetic, Comparable {
|
|
// Checked arithmetic functions. Specific implementations in
|
|
// FixedPoint.swift.gyb support static checking for integer types.
|
|
% for _,op in integerBinaryOps:
|
|
func ${op} (lhs: Self, rhs: Self) -> Self
|
|
% end
|
|
|
|
// Explicitly convert to IntMax, trapping on overflow
|
|
func toIntMax() -> IntMax
|
|
}
|
|
|
|
% for name,op in integerBinaryOps:
|
|
@transparent
|
|
func ${op} <T: _IntegerArithmetic>(lhs: T, rhs: T) -> T {
|
|
return _overflowChecked(T.unchecked${name}(lhs, rhs))
|
|
}
|
|
|
|
@transparent
|
|
func &${op} <T: _IntegerArithmetic>(lhs: T, rhs: T) -> T {
|
|
return T.unchecked${name}(lhs, rhs).0
|
|
}
|
|
|
|
@assignment @transparent
|
|
func ${op}= <T: _IntegerArithmetic>(inout lhs: T, rhs: T) {
|
|
lhs = lhs ${op} rhs
|
|
}
|
|
% end
|
|
|
|
//===--- SignedNumber -----------------------------------------------------===//
|
|
// A numeric type that supports abs(x), +x and -x
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Base protocol containing all the non-defaulted implentations. This
|
|
// protocol should be presented to users as part of SignedNumber.
|
|
protocol _SignedNumber : Comparable, IntegerLiteralConvertible {
|
|
// Subtraction is a requirement for SignedNumber
|
|
@infix func - (lhs: Self, rhs: Self) -> Self
|
|
}
|
|
|
|
// SignedNumber itself contains only operator requirements having
|
|
// default implementations on the base protocol.
|
|
protocol SignedNumber : _SignedNumber {
|
|
@prefix func - (x: Self) -> Self
|
|
|
|
// Do not use this operator directly; call abs(x) instead
|
|
func ~> (_:Self,_:(_Abs, ())) -> Self
|
|
}
|
|
|
|
// Unary negation in terms of subtraction. This is a default
|
|
// implementation; models of SignedNumber can provide their own
|
|
// implementations.
|
|
@prefix @transparent
|
|
func - <T : _SignedNumber>(x: T) -> T {
|
|
return 0 - x
|
|
}
|
|
|
|
// Unary +
|
|
@prefix @transparent
|
|
func + <T: _SignedNumber>(x: T) -> T {
|
|
return x
|
|
}
|
|
|
|
//===--- abs(x) -----------------------------------------------------------===//
|
|
struct _Abs {}
|
|
func _abs<Args>(args: Args) -> (_Abs, Args) {
|
|
return (_Abs(), args)
|
|
}
|
|
|
|
// Do not use this operator directly; call abs(x) instead
|
|
@transparent
|
|
func ~> <T : _SignedNumber>(x:T,_:(_Abs, ())) -> T {
|
|
return x < 0 ? -x : x
|
|
}
|
|
|
|
protocol AbsoluteValuable : SignedNumber {
|
|
class func abs(_:Self) -> Self
|
|
}
|
|
|
|
// Do not use this operator directly; call abs(x) instead
|
|
@transparent
|
|
func ~> <T : AbsoluteValuable>(x:T,_:(_Abs, ())) -> T {
|
|
return T.abs(x)
|
|
}
|
|
|
|
// Absolute value. Concrete instances of SignedNumber can specialize
|
|
// this function by conforming to AbsoluteValuable.
|
|
@transparent
|
|
func abs<T : SignedNumber>(x: T) -> T {
|
|
return x~>_abs()
|
|
}
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|