forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointer.swift
88 lines (79 loc) · 2.76 KB
/
Pointer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// A stdlib-internal protocol modeled by the intrinsic pointer types,
/// UnsafeMutablePointer, UnsafePointer, and
/// AutoreleasingUnsafeMutablePointer.
public protocol _PointerType {
/// The underlying raw pointer value.
var _rawValue: Builtin.RawPointer { get }
/// Construct a pointer from a raw value.
init(_ _rawValue: Builtin.RawPointer)
}
/// Derive a pointer argument from a convertible pointer type.
@_transparent
@warn_unused_result
public // COMPILER_INTRINSIC
func _convertPointerToPointerArgument<
FromPointer: _PointerType,
ToPointer: _PointerType
>(from: FromPointer) -> ToPointer {
return ToPointer(from._rawValue)
}
/// Derive a pointer argument from the address of an inout parameter.
@_transparent
@warn_unused_result
public // COMPILER_INTRINSIC
func _convertInOutToPointerArgument<
ToPointer: _PointerType
>(from: Builtin.RawPointer) -> ToPointer {
return ToPointer(from)
}
/// Derive a pointer argument from an inout array parameter.
@_transparent
@warn_unused_result
public // COMPILER_INTRINSIC
func _convertMutableArrayToPointerArgument<
FromElement,
ToPointer: _PointerType
>(inout a: [FromElement]) -> (AnyObject?, ToPointer) {
// TODO: Putting a canary at the end of the array in checked builds might
// be a good idea
// Call reserve to force contiguous storage.
a.reserveCapacity(0)
_debugPrecondition(a._baseAddressIfContiguous != nil || a.isEmpty)
return (a._owner, ToPointer(a._baseAddressIfContiguous._rawValue))
}
/// Derive a pointer argument from a value array parameter.
@_transparent
@warn_unused_result
public // COMPILER_INTRINSIC
func _convertConstArrayToPointerArgument<
FromElement,
ToPointer: _PointerType
>(arr: [FromElement]) -> (AnyObject?, ToPointer) {
let (owner, raw) = arr._cPointerArgs()
return (owner, ToPointer(raw))
}
/// Derive a UTF-8 pointer argument from a value string parameter.
@_transparent
@warn_unused_result
public // COMPILER_INTRINSIC
func _convertConstStringToUTF8PointerArgument<
ToPointer: _PointerType
>(str: String) -> (AnyObject?, ToPointer) {
// Convert the UTF-8 representation to a null-terminated array.
var utf8 = Array(str.utf8)
utf8.append(0)
// Extract the owner and pointer from the array.
let (owner, raw) = utf8._cPointerArgs()
return (owner, ToPointer(raw))
}