mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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
122 lines
3.7 KiB
Swift
122 lines
3.7 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()]
|
|
|
|
@public protocol _IntegerArithmetic {
|
|
% for name,_ in integerBinaryOps:
|
|
class func unchecked${name}(lhs: Self, _ rhs: Self) -> (Self, Bool)
|
|
% end
|
|
}
|
|
|
|
@public 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 @public
|
|
func ${op} <T: _IntegerArithmetic>(lhs: T, rhs: T) -> T {
|
|
return _overflowChecked(T.unchecked${name}(lhs, rhs))
|
|
}
|
|
|
|
@transparent @public
|
|
func &${op} <T: _IntegerArithmetic>(lhs: T, rhs: T) -> T {
|
|
return T.unchecked${name}(lhs, rhs).0
|
|
}
|
|
|
|
@assignment @transparent @public
|
|
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.
|
|
@public 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.
|
|
@public 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 @public
|
|
func - <T : _SignedNumber>(x: T) -> T {
|
|
return 0 - x
|
|
}
|
|
|
|
// Unary +
|
|
@prefix @transparent @public
|
|
func + <T: _SignedNumber>(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 : _SignedNumber>(x:T,_:(_Abs, ())) -> T {
|
|
return x < 0 ? -x : x
|
|
}
|
|
|
|
@public protocol AbsoluteValuable : SignedNumber {
|
|
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 SignedNumber can specialize
|
|
// this function by conforming to AbsoluteValuable.
|
|
@transparent @public
|
|
func abs<T : SignedNumber>(x: T) -> T {
|
|
return x~>_abs()
|
|
}
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|