Files
swift-mirror/stdlib/core/IntegerArithmetic.swift.gyb
Chris Lattner 3ad9c58c18 Disallow (with a nice fixit) the infix modifier on func decls, it is just validated
and ignored anyway.  It is still required on operator decls though.


Swift SVN r19978
2014-07-15 16:52:04 +00:00

121 lines
3.7 KiB
Swift

//===--- IntegerArithmeticType.swift.gyb ---------------------*- 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
//
//===----------------------------------------------------------------------===//
%# Ignore the following admonition; it applies to the resulting .swift file only
// Automatically Generated From IntegerArithmeticType.swift.gyb. Do Not
// Edit Directly!
% integerBinaryOps = [
% (x[:-1], x[-1]) for x in
% 'add+ subtract- multiply* divide/ remainder%'.split()]
public protocol _IntegerArithmeticType {
% for name,_ in integerBinaryOps:
class func ${name}WithOverflow(lhs: Self, _ rhs: Self) -> (Self, overflow: Bool)
% end
}
public protocol IntegerArithmeticType : _IntegerArithmeticType, 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 public
func ${op} <T: _IntegerArithmeticType>(lhs: T, rhs: T) -> T {
return _overflowChecked(T.${name}WithOverflow(lhs, rhs))
}
@transparent public
func &${op} <T: _IntegerArithmeticType>(lhs: T, rhs: T) -> T {
return T.${name}WithOverflow(lhs, rhs).0
}
@transparent public
func ${op}= <T: _IntegerArithmeticType>(inout lhs: T, rhs: T) {
lhs = lhs ${op} rhs
}
% end
//===--- SignedNumberType -------------------------------------------------===//
// 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 SignedNumberType.
public protocol _SignedNumberType : Comparable, IntegerLiteralConvertible {
// Subtraction is a requirement for SignedNumberType
func - (lhs: Self, rhs: Self) -> Self
}
// SignedNumberType itself contains only operator requirements having
// default implementations on the base protocol.
public protocol SignedNumberType : _SignedNumberType {
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 SignedNumberType can provide their own
// implementations.
@transparent public
prefix func - <T : _SignedNumberType>(x: T) -> T {
return 0 - x
}
// Unary +
@transparent public
prefix func + <T: _SignedNumberType>(x: T) -> T {
return x
}
//===--- abs(x) -----------------------------------------------------------===//
public struct _Abs {}
internal func _abs<Args>(args: Args) -> (_Abs, Args) {
return (_Abs(), args)
}
// Do not use this operator directly; call abs(x) instead
@transparent public
func ~> <T : _SignedNumberType>(x:T,_:(_Abs, ())) -> T {
return x < 0 ? -x : x
}
public protocol AbsoluteValuable : SignedNumberType {
class func abs(_:Self) -> Self
}
// Do not use this operator directly; call abs(x) instead
@transparent public
func ~> <T : AbsoluteValuable>(x:T,_:(_Abs, ())) -> T {
return T.abs(x)
}
// Absolute value. Concrete instances of SignedNumberType can specialize
// this function by conforming to AbsoluteValuable.
@transparent public
func abs<T : SignedNumberType>(x: T) -> T {
return x~>_abs()
}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: