Files
swift-mirror/stdlib/public/SDK/GLKit/GLKit.swift.gyb
Doug Gregor 793b3326af Implement the new rules for argument label defaults.
The rule changes are as follows:
  * All functions (introduced with the 'func' keyword) have argument
  labels for arguments beyond the first, by default. Methods are no
  longer special in this regard.
  * The presence of a default argument no longer implies an argument
  label.

The actual changes to the parser and printer are fairly simple; the
rest of the noise is updating the standard library, overlays, tests,
etc.

With the standard library, this change is intended to be API neutral:
I've added/removed #'s and _'s as appropriate to keep the user
interface the same. If we want to separately consider using argument
labels for more free functions now that the defaults in the language
have shifted, we can tackle that separately.

Fixes rdar://problem/17218256.

Swift SVN r27704
2015-04-24 19:03:30 +00:00

126 lines
3.5 KiB
Swift

//===----------------------------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
// GLKit overlays for Swift
//===----------------------------------------------------------------------===//
@exported import GLKit // Clang module
// The GLKit headers provide a fairly complete set of types and operations
// that Swift's importer is now able to present in Swift. However, Swift
// still doesn't know yet how to natively expose the elements of these types.
// This overlay generates Swift accessors for the GLKit matrix and vector
// types.
%{
# Each element of the array is a tuple of the element labels and the minimum
# vector length at which to apply them.
vectorElementNames = [
(['x', 'y', 'z', 'w'], 2),
(['s', 't', 'p', 'q'], 2),
(['r', 'g', 'b', 'a'], 3),
]
}%
// Do dirty pointer manipulations to index an opaque struct like an array.
@inline(__always)
public func _indexHomogeneousValue<TTT, T>(aggregate: UnsafePointer<TTT>,
_ index: Int) -> T {
return UnsafePointer<T>(aggregate)[index]
}
%{
def defineSubscript(Type, limit):
return """
public subscript(i: Int) -> Float {{
@inline(__always)
get {{
_precondition(i >= 0, "Negative {0} index out of range")
_precondition(i < {1}, "{0} index out of range")
// We can't derive an UnsafePointer from a let binding. Lame.
var clone = self
return _indexHomogeneousValue(&clone, i)
}}
}}
""".format(Type, limit)
}%
% for size in [2, 3, 4]:
extension GLKMatrix${size} {
public typealias _Tuple = (${ ', '.join(['Float'] * (size * size)) })
public var _tuple: _Tuple {
@inline(__always) get { return unsafeBitCast(self, _Tuple.self) }
}
% for i in xrange(0, size):
% for j in xrange(0, size):
public var m${i}${j}: Float {
@inline(__always) get { return _tuple.${i * size + j} }
}
% end
% end
${ defineSubscript("GLKMatrix" + str(size), size * size) }
}
extension GLKVector${size} {
public typealias _Tuple = (${ ', '.join(['Float'] * size) })
public var _tuple: _Tuple {
@inline(__always) get { return unsafeBitCast(self, _Tuple.self) }
}
% for (names, minSize) in vectorElementNames:
% for i in xrange(0, size if size >= minSize else 0):
public var ${names[i]}: Float {
@inline(__always) get { return _tuple.${i} }
}
% end
% end
${ defineSubscript("GLKVector" + str(size), size) }
}
% end
extension GLKQuaternion {
public typealias _Tuple = (Float, Float, Float, Float)
public var _tuple: _Tuple {
@inline(__always) get { return unsafeBitCast(self, _Tuple.self) }
}
public var v: GLKVector3 {
@inline(__always) get {
let (i, j, k, _) = _tuple
return GLKVector3Make(i, j, k)
}
}
public var s: Float {
@inline(__always) get { return _tuple.3 }
}
public var x: Float {
@inline(__always) get { return _tuple.0 }
}
public var y: Float {
@inline(__always) get { return _tuple.1 }
}
public var z: Float {
@inline(__always) get { return _tuple.2 }
}
public var w: Float {
@inline(__always) get { return _tuple.3 }
}
${ defineSubscript("GLKQuaternion", 4) }
}