mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Before this commit, RandomAccessIndex was a refinement of NumericOperations, which forced it to support inappropriate operations such as multiplication. Many obvious random-access index types can't support multiplication (e.g. a StridedIndex adapter, which moves its underlying index by N positions for each increment). Along the way: * the addition and subtraction operations on RandomAccessIndex were renamed to advancedBy and distanceTo, which prevents nasty ambiguities when a type conforms to both RandomAccessIndex and Integer, and allows Index DistanceTypes to actually be signed integers even when the Index is unsigned. * Before this commit, using internal interfaces, it was possible to request static checking without also getting dynamic checks when static checking is impossible. Now the relationship between static and dynamic checking is built into the core protocols. * NumericOperations.swift was moved into IntegerArithmetic.swift.gyb, correcting missing operators by generating them programmatically and in preparation for renaming the protocol to something more appropriate Fixes <rdar://problem/16246927> RandomAccessIndex is over-constrained and possibly: <rdar://problem/15605729> Make all operators generic over protocols (in particular, get NumericOperations done) Swift SVN r14854
80 lines
2.2 KiB
Swift
80 lines
2.2 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 _NumericOperations {
|
|
% for name,_ in integerBinaryOps:
|
|
class func unchecked${name}(lhs: Self, rhs: Self) -> (Self, Bool)
|
|
% end
|
|
}
|
|
|
|
protocol NumericOperations : _NumericOperations, 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: _NumericOperations>(lhs: T, rhs: T) -> T {
|
|
return overflowChecked(T.unchecked${name}(lhs, rhs))
|
|
}
|
|
|
|
@transparent
|
|
func &${op} <T: _NumericOperations>(lhs: T, rhs: T) -> T {
|
|
return T.unchecked${name}(lhs, rhs).0
|
|
}
|
|
|
|
@assignment @transparent
|
|
func ${op}= <T: _NumericOperations>(inout lhs: T, rhs: T) {
|
|
lhs = lhs ${op} rhs
|
|
}
|
|
% end
|
|
|
|
protocol SignedNumber {
|
|
class func uncheckedNegate(_: Self) -> (Self, Bool)
|
|
class func uncheckedAbs(_: Self) -> (Self, Bool)
|
|
func isNegative() -> Bool
|
|
}
|
|
|
|
@transparent
|
|
func abs <T: SignedNumber>(x: T) -> T {
|
|
return overflowChecked(T.uncheckedAbs(x))
|
|
}
|
|
|
|
@prefix @transparent
|
|
func - <T: SignedNumber>(x: T) -> T {
|
|
return overflowChecked(T.uncheckedNegate(x))
|
|
}
|
|
|
|
@prefix @transparent
|
|
func + <T: SignedNumber>(x: T) -> T {
|
|
return x
|
|
}
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|