Files
swift-mirror/stdlib/core/IntegerArithmetic.swift.gyb
Dave Abrahams 6d1095f44e Protocol names end in "Type," "ible," or "able"
Mechanically add "Type" to the end of any protocol names that don't end
in "Type," "ible," or "able."  Also, drop "Type" from the end of any
associated type names, except for those of the *LiteralConvertible
protocols.

There are obvious improvements to make in some of these names, which can
be handled with separate commits.

Fixes <rdar://problem/17165920> Protocols `Integer` etc should get
uglier names.

Swift SVN r19883
2014-07-12 17:29:57 +00:00

121 lines
3.8 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
}
@assignment @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
@infix 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.
@prefix @transparent public
func - <T : _SignedNumberType>(x: T) -> T {
return 0 - x
}
// Unary +
@prefix @transparent public
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: