Skip to content

Commit 21669b3

Browse files
author
Dave Abrahams
committed
[stdlib] Add "Mutable" to [Autoreleasing]UnsafePointer
UnsafePointer becomes UnsafeMutablePointer AutoreleasingUnsafePointer becomes AutoreleasingUnsafeMutablePointer Swift SVN r20316
1 parent 6867ce2 commit 21669b3

File tree

107 files changed

+717
-650
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+717
-650
lines changed

docs/proposals/C Pointer Argument Interop.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ arguments:
1313

1414
- Const pointer arguments ``const int *``, including const pointers to ObjC
1515
classes ``NSFoo * const *``, can be used as "in" array arguments,
16-
as ``inout`` scalar arguments, or as ``UnsafePointer`` arguments.
16+
as ``inout`` scalar arguments, or as ``UnsafeMutablePointer`` arguments.
1717
- Non-const pointer arguments to C types, ``int *``, can be used as ``inout``
18-
array or scalar arguments, or as ``UnsafePointer`` arguments.
18+
array or scalar arguments, or as ``UnsafeMutablePointer`` arguments.
1919
- Non-const pointer arguments to ObjC class types, ``NSFoo **``, can be used as
2020
``inout`` scalar arguments or passed ``nil``. (They cannot be used as
21-
array arguments or as ``UnsafePointer`` arguments.)
21+
array arguments or as ``UnsafeMutablePointer`` arguments.)
2222
- ``const void *`` and ``void *`` pointers can be used in the same ways as
2323
pointers to any C type (but not ObjC types).
2424

@@ -162,7 +162,7 @@ Pointer Return Values
162162
---------------------
163163

164164
This proposal does not address the handling of return values, which should still
165-
be imported into Swift as ``UnsafePointer`` values.
165+
be imported into Swift as ``UnsafeMutablePointer`` values.
166166

167167

168168
Library Features
@@ -178,17 +178,17 @@ their implicit conversions. The necessary types are as follows:
178178
- ``CConstPointer<T>`` is the imported representation of a ``const T *``
179179
argument. It is implicitly convertible from ``inout T`` by inout address
180180
conversion and from ``Array<T>`` by immutable interior pointer
181-
conversion. It is also implicitly convertible to and from ``UnsafePointer<T>``
181+
conversion. It is also implicitly convertible to and from ``UnsafeMutablePointer<T>``
182182
by normal conversion.
183183
- ``CMutablePointer<T>`` is the imported representation of a ``T *``
184184
argument for a POD C type ``T``. It is implicitly convertible from
185185
``inout T`` by inout address conversion and from ``inout Array<T>`` by mutating
186186
interior pointer conversion. It is also implicitly convertible to and from
187-
``UnsafePointer<T>`` by normal conversion.
187+
``UnsafeMutablePointer<T>`` by normal conversion.
188188
- ``ObjCInOut<T>`` is the imported representation of a ``T **``
189189
argument for an ObjC class type ``T``. It is implicitly convertible from
190190
``inout T`` by inout writeback conversion and is implicitly convertible
191-
from ``nil``. It cannot be converted from an array or to ``UnsafePointer``.
191+
from ``nil``. It cannot be converted from an array or to ``UnsafeMutablePointer``.
192192

193193
New Language Features
194194
=====================

docs/proposals/C Pointer Interop Language Model.rst

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ We have a pretty good user model for C pointer interop now, but the language
44
model still needs improvement. Building the user model on top of implicit
55
conversions has a number of undesirable side effects. We end up with a mess of
66
pointer types—the intended user-facing, one-word pointer types
7-
``UnsafePointer`` and ``COpaquePointer``, which expose a full pointer-ish API
7+
``UnsafeMutablePointer`` and ``COpaquePointer``, which expose a full pointer-ish API
88
and are naturally ABI-compatible with C pointers; and the bridging pointer
99
types, ``ObjCMutablePointer``, ``CMutablePointer``, ``CConstPointer``,
1010
``CMutableVoidPointer``, and ``CConstVoidPointer``, which have no real API yet
@@ -25,18 +25,18 @@ The Pointer Types
2525
In the standard library, we provide three pointer types:
2626

2727
- ``ConstUnsafePointer<T>``, corresponding to ``T const *`` in C and ARC,
28-
- ``UnsafePointer<T>``, corresponding to ``T *`` in C, and ``T* __strong *`` in
28+
- ``UnsafeMutablePointer<T>``, corresponding to ``T *`` in C, and ``T* __strong *`` in
2929
ARC for class types, and
30-
- ``AutoreleasingUnsafePointer<T>`` (for all ``T: AnyObject``), corresponding
30+
- ``AutoreleasingUnsafeMutablePointer<T>`` (for all ``T: AnyObject``), corresponding
3131
to ``T* __autoreleasing *`` in ARC.
3232

3333
These types are all one word, have no ownership semantics, and share a common
3434
interface. ``ConstUnsafePointer`` does not expose operations for storing to the
35-
referenced memory. ``UnsafePointer`` and ``AutoreleasingUnsafePointer`` differ
36-
in store behavior: ``UnsafePointer`` assumes that the pointed-to reference has
35+
referenced memory. ``UnsafeMutablePointer`` and ``AutoreleasingUnsafeMutablePointer`` differ
36+
in store behavior: ``UnsafeMutablePointer`` assumes that the pointed-to reference has
3737
ownership semantics, so ``ptr.initialize(x)`` consumes a reference to ``x``,
3838
and ``ptr.assign(x)`` releases the originally stored value before storing the
39-
new value. ``AutoreleasingUnsafePointer`` assumes that the pointed-to
39+
new value. ``AutoreleasingUnsafeMutablePointer`` assumes that the pointed-to
4040
reference does not have ownership semantics, so values are autoreleased before
4141
being stored by either initialize() or assign(), and no release is done on
4242
reassignment. Loading from any of the three kinds of pointer does a strong
@@ -47,31 +47,31 @@ Conversion behavior for pointer arguments
4747

4848
The user model for pointer arguments becomes an inherent capability of function applications. The rules are:
4949

50-
UnsafePointer<T>
51-
----------------
50+
UnsafeMutablePointer<T>
51+
-----------------------
5252

53-
When a function is declared as taking an ``UnsafePointer<T>`` argument, it can
53+
When a function is declared as taking an ``UnsafeMutablePointer<T>`` argument, it can
5454
accept any of the following:
5555

5656
- ``nil``, which is passed as a null pointer,
57-
- an ``UnsafePointer<T>`` value, which is passed verbatim,
57+
- an ``UnsafeMutablePointer<T>`` value, which is passed verbatim,
5858
- an inout expression whose operand is a stored lvalue of type ``T``, which is
5959
passed as the address of the lvalue, or
6060
- an inout ``Array<T>`` value, which is passed as a pointer to the start of the
6161
array, and lifetime-extended for the duration of the callee.
6262

6363
As a special case, when a function is declared as taking an
64-
``UnsafePointer<Void>`` argument, it can accept the same operands as
65-
``UnsafePointer<T>`` for any type T.
64+
``UnsafeMutablePointer<Void>`` argument, it can accept the same operands as
65+
``UnsafeMutablePointer<T>`` for any type T.
6666

6767
So if you have a function declared::
6868

69-
func foo(x: UnsafePointer<Float>)
69+
func foo(x: UnsafeMutablePointer<Float>)
7070

7171
You can call it as any of::
7272

7373
var x: Float = 0.0
74-
var p: UnsafePointer<Float> = nil
74+
var p: UnsafeMutablePointer<Float> = nil
7575
var a: Float[] = [1.0, 2.0, 3.0]
7676
foo(nil)
7777
foo(p)
@@ -80,12 +80,12 @@ You can call it as any of::
8080

8181
And if you have a function declared::
8282

83-
func bar(x: UnsafePointer<Void>)
83+
func bar(x: UnsafeMutablePointer<Void>)
8484

8585
You can call it as any of::
8686

8787
var x: Float = 0.0, y: Int = 0
88-
var p: UnsafePointer<Float> = nil, q: UnsafePointer<Int> = nil
88+
var p: UnsafeMutablePointer<Float> = nil, q: UnsafeMutablePointer<Int> = nil
8989
var a: Float[] = [1.0, 2.0, 3.0], b: Int = [1, 2, 3]
9090
bar(nil)
9191
bar(p)
@@ -95,14 +95,14 @@ You can call it as any of::
9595
bar(&a)
9696
bar(&b)
9797

98-
AutoreleasingUnsafePointer<T>
99-
-----------------------------
98+
AutoreleasingUnsafeMutablePointer<T>
99+
------------------------------------
100100

101-
When a function is declared as taking an ``AutoreleasingUnsafePointer<T>``, it
101+
When a function is declared as taking an ``AutoreleasingUnsafeMutablePointer<T>``, it
102102
can accept any of the following:
103103

104104
- nil, which is passed as a null pointer,
105-
- an ``AutoreleasingUnsafePointer<T>`` value, which is passed verbatim, or
105+
- an ``AutoreleasingUnsafeMutablePointer<T>`` value, which is passed verbatim, or
106106
- an inout expression, whose operand is primitive-copied to a temporary
107107
nonowning buffer. The address of that buffer is passed to the callee, and on
108108
return, the value in the buffer is loaded, retained, and reassigned into the
@@ -112,25 +112,25 @@ Note that the above list does not include arrays, since implicit autoreleasing-t
112112

113113
So if you have a function declared::
114114

115-
func bas(x: AutoreleasingUnsafePointer<NSBas?>)
115+
func bas(x: AutoreleasingUnsafeMutablePointer<NSBas?>)
116116

117117
You can call it as any of::
118118

119119
var x: NSBas? = nil
120-
var p: AutoreleasingUnsafePointer<NSBas?> = nil
120+
var p: AutoreleasingUnsafeMutablePointer<NSBas?> = nil
121121
bas(nil)
122122
bas(p)
123123
bas(&x)
124124

125125
ConstUnsafePointer<T>
126126
---------------------
127127

128-
When a function is declared as taking an ``UnsafePointer<T>`` argument, it can
128+
When a function is declared as taking an ``UnsafeMutablePointer<T>`` argument, it can
129129
accept any of the following:
130130

131131
- nil, which is passed as a null pointer,
132-
- an ``UnsafePointer<T>``, ``ConstUnsafePointer<T>``, or
133-
``AutoreleasingUnsafePointer<T>`` value, which is converted to
132+
- an ``UnsafeMutablePointer<T>``, ``ConstUnsafePointer<T>``, or
133+
``AutoreleasingUnsafeMutablePointer<T>`` value, which is converted to
134134
``ConstUnsafePointer<T>`` if necessary and passed verbatim,
135135
- an inout expression whose operand is an lvalue of type ``T``, which is passed
136136
as the address of (the potentially temporary writeback buffer of) the lvalue,

docs/proposals/C Pointer Interop.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ arguments:
1313

1414
- Const pointer arguments ``const int *``, including const pointers to ObjC
1515
classes ``NSFoo * const *``, can be used as "in" array arguments,
16-
as ``inout`` scalar arguments, or as ``UnsafePointer`` arguments.
16+
as ``inout`` scalar arguments, or as ``UnsafeMutablePointer`` arguments.
1717
- Non-const pointer arguments to C types, ``int *``, can be used as ``inout``
18-
array or scalar arguments, or as ``UnsafePointer`` arguments.
18+
array or scalar arguments, or as ``UnsafeMutablePointer`` arguments.
1919
- Non-const pointer arguments to ObjC class types, ``NSFoo **``, can be used as
2020
``inout`` scalar arguments or passed ``nil``. (They cannot be used as
21-
array arguments or as ``UnsafePointer`` arguments.)
21+
array arguments or as ``UnsafeMutablePointer`` arguments.)
2222
- ``const void *`` and ``void *`` pointers can be used in the same ways as
2323
pointers to any C type (but not ObjC types).
2424

@@ -105,7 +105,7 @@ Pointer Return Values
105105
---------------------
106106

107107
This proposal does not address the handling of return values, which should still
108-
be imported into Swift as ``UnsafePointer`` values.
108+
be imported into Swift as ``UnsafeMutablePointer`` values.
109109

110110

111111
Library Features
@@ -122,13 +122,13 @@ types are as follows:
122122
- ``CConstPointer<T>`` is the imported representation of a ``const T *``
123123
argument. It is implicitly convertible from ``inout T`` by inout address
124124
conversion and from ``Array<T>`` by immutable interior pointer
125-
conversion. It is also implicitly convertible to and from ``UnsafePointer<T>``
125+
conversion. It is also implicitly convertible to and from ``UnsafeMutablePointer<T>``
126126
by normal conversion.
127127
- ``CMutablePointer<T>`` is the imported representation of a ``T *``
128128
argument for a POD C type ``T``. It is implicitly convertible from
129129
``inout T`` by inout address conversion and from ``inout Array<T>`` by mutating
130130
interior pointer conversion. It is also implicitly convertible to and from
131-
``UnsafePointer<T>`` by normal conversion.
131+
``UnsafeMutablePointer<T>`` by normal conversion.
132132
- ``CConstVoidPointer`` and ``CMutableVoidPointer`` are the imported
133133
representations of ``const void *`` and ``void *`` respectively.
134134
They are implicitly convertible by normal conversion from, respectively,
@@ -137,7 +137,7 @@ types are as follows:
137137
- ``ObjCInOut<T>`` is the imported representation of a ``T **``
138138
argument for an ObjC class type ``T``. It is implicitly convertible from
139139
``inout T`` by inout writeback conversion and is implicitly convertible
140-
from ``nil``. It cannot be converted from an array or to ``UnsafePointer``.
140+
from ``nil``. It cannot be converted from an array or to ``UnsafeMutablePointer``.
141141

142142
New Language Features
143143
=====================

docs/proposals/DocumentationComments.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ through C interfaces.
255255
Proposed syntax::
256256

257257
/// \param [out] extraResult Set to zero if...
258-
func foo(extraResult: UnsafePointer<Int>) -> Int {
258+
func foo(extraResult: UnsafeMutablePointer<Int>) -> Int {
259259
}
260260

261261
[P4] documentation for parameters of closures that are function parameters

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ class ASTContext {
373373
EnumElementDecl *getOptionalSomeDecl(OptionalTypeKind kind) const;
374374
EnumElementDecl *getOptionalNoneDecl(OptionalTypeKind kind) const;
375375

376-
/// Retrieve the declaration of Swift.UnsafePointer<T>.
377-
NominalTypeDecl *getUnsafePointerDecl() const;
376+
/// Retrieve the declaration of Swift.UnsafeMutablePointer<T>.
377+
NominalTypeDecl *getUnsafeMutablePointerDecl() const;
378378

379379
/// Retrieve the declaration of Swift.ConstUnsafePointer<T>.
380380
NominalTypeDecl *getConstUnsafePointerDecl() const;
381381

382-
/// Retrieve the declaration of Swift.AutoreleasingUnsafePointer<T>.
383-
NominalTypeDecl *getAutoreleasingUnsafePointerDecl() const;
382+
/// Retrieve the declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.
383+
NominalTypeDecl *getAutoreleasingUnsafeMutablePointerDecl() const;
384384

385385
/// Retrieve the declaration of Swift.CFunctionPointer<T>.
386386
NominalTypeDecl *getCFunctionPointerDecl() const;
@@ -422,7 +422,7 @@ class ASTContext {
422422
bool hasOptionalIntrinsics(LazyResolver *resolver) const;
423423

424424
/// Check whether the standary library provides all the correct
425-
/// intrinsic support for UnsafePointer<T> function arguments.
425+
/// intrinsic support for UnsafeMutablePointer<T> function arguments.
426426
///
427427
/// If this is true, the methods to getConvert*ToPointerArgument
428428
bool hasPointerArgumentIntrinsics(LazyResolver *resolver) const;

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,9 @@ enum { NumOptionalTypeKinds = 2 };
24282428

24292429
// Kinds of pointer types.
24302430
enum PointerTypeKind : unsigned {
2431-
PTK_UnsafePointer,
2431+
PTK_UnsafeMutablePointer,
24322432
PTK_ConstUnsafePointer,
2433-
PTK_AutoreleasingUnsafePointer,
2433+
PTK_AutoreleasingUnsafeMutablePointer,
24342434
};
24352435

24362436
/// An implicitly created member decl, used when importing a Clang enum type.

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ ERROR(optional_intrinsics_not_found,sema_tct,none,
13061306
"Optional<T>", ())
13071307
ERROR(pointer_argument_intrinsics_not_found,sema_tct,none,
13081308
"broken standard library: cannot find intrinsic operations on "
1309-
"UnsafePointer<T>", ())
1309+
"UnsafeMutablePointer<T>", ())
13101310
ERROR(bool_intrinsics_not_found,sema_tct,none,
13111311
"broken standard library: cannot find intrinsic operations on Bool", ())
13121312
ERROR(unchecked_not_optional_type,sema_tct,none,

include/swift/AST/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ class alignas(8) TypeBase {
417417
/// its list of protocols.
418418
void getAnyExistentialTypeProtocols(SmallVectorImpl<ProtocolDecl *> &protocols);
419419

420-
/// Determines the element type of a known *UnsafePointer variant, or returns
421-
/// null if the type is not a pointer.
420+
/// Determines the element type of a known *UnsafeMutablePointer
421+
/// variant, or returns null if the type is not a pointer.
422422
Type getAnyPointerElementType(PointerTypeKind &PTK);
423423
Type getAnyPointerElementType() {
424424
PointerTypeKind Ignore;

include/swift/Runtime/Reflection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extern "C" Mirror
3636
swift_reflectAny(OpaqueValue *value, const Metadata *T);
3737

3838
/// func unsafeReflect<T>(owner: Builtin.NativeObject,
39-
/// x: UnsafePointer<T>) -> Mirror
39+
/// x: UnsafeMutablePointer<T>) -> Mirror
4040
///
4141
/// Produce a mirror for any value. If the value's type conforms to Reflectable,
4242
/// invoke its getMirror() method; otherwise, fall back to an implementation

lib/AST/ASTContext.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ struct ASTContext::Implementation {
8383
/// The declaration of Swift.ImplicitlyUnwrappedOptional<T>.None.
8484
EnumElementDecl *ImplicitlyUnwrappedOptionalNoneDecl = nullptr;
8585

86-
/// The declaration of Swift.UnsafePointer<T>.
87-
NominalTypeDecl *UnsafePointerDecl = nullptr;
86+
/// The declaration of Swift.UnsafeMutablePointer<T>.
87+
NominalTypeDecl *UnsafeMutablePointerDecl = nullptr;
8888

8989
/// The declaration of Swift.ConstUnsafePointer<T>.
9090
NominalTypeDecl *ConstUnsafePointerDecl = nullptr;
9191

92-
/// The declaration of Swift.AutoreleasingUnsafePointer<T>.
93-
NominalTypeDecl *AutoreleasingUnsafePointerDecl = nullptr;
92+
/// The declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.
93+
NominalTypeDecl *AutoreleasingUnsafeMutablePointerDecl = nullptr;
9494

9595
/// The declaration of Swift.CFunctionPointer<T -> U>.
9696
NominalTypeDecl *CFunctionPointerDecl = nullptr;
@@ -533,11 +533,12 @@ EnumElementDecl *ASTContext::getImplicitlyUnwrappedOptionalNoneDecl() const {
533533
return Impl.ImplicitlyUnwrappedOptionalNoneDecl;
534534
}
535535

536-
NominalTypeDecl *ASTContext::getUnsafePointerDecl() const {
537-
if (!Impl.UnsafePointerDecl)
538-
Impl.UnsafePointerDecl = findSyntaxSugarImpl(*this, "UnsafePointer");
536+
NominalTypeDecl *ASTContext::getUnsafeMutablePointerDecl() const {
537+
if (!Impl.UnsafeMutablePointerDecl)
538+
Impl.UnsafeMutablePointerDecl = findSyntaxSugarImpl(
539+
*this, "UnsafeMutablePointer");
539540

540-
return Impl.UnsafePointerDecl;
541+
return Impl.UnsafeMutablePointerDecl;
541542
}
542543

543544
NominalTypeDecl *ASTContext::getConstUnsafePointerDecl() const {
@@ -548,12 +549,12 @@ NominalTypeDecl *ASTContext::getConstUnsafePointerDecl() const {
548549
return Impl.ConstUnsafePointerDecl;
549550
}
550551

551-
NominalTypeDecl *ASTContext::getAutoreleasingUnsafePointerDecl() const {
552-
if (!Impl.AutoreleasingUnsafePointerDecl)
553-
Impl.AutoreleasingUnsafePointerDecl
554-
= findSyntaxSugarImpl(*this, "AutoreleasingUnsafePointer");
552+
NominalTypeDecl *ASTContext::getAutoreleasingUnsafeMutablePointerDecl() const {
553+
if (!Impl.AutoreleasingUnsafeMutablePointerDecl)
554+
Impl.AutoreleasingUnsafeMutablePointerDecl
555+
= findSyntaxSugarImpl(*this, "AutoreleasingUnsafeMutablePointer");
555556

556-
return Impl.AutoreleasingUnsafePointerDecl;
557+
return Impl.AutoreleasingUnsafeMutablePointerDecl;
557558
}
558559

559560
NominalTypeDecl *ASTContext::getCFunctionPointerDecl() const {
@@ -884,9 +885,9 @@ bool ASTContext::hasOptionalIntrinsics(LazyResolver *resolver) const {
884885
}
885886

886887
bool ASTContext::hasPointerArgumentIntrinsics(LazyResolver *resolver) const {
887-
return getUnsafePointerDecl()
888+
return getUnsafeMutablePointerDecl()
888889
&& getConstUnsafePointerDecl()
889-
&& getAutoreleasingUnsafePointerDecl()
890+
&& getAutoreleasingUnsafeMutablePointerDecl()
890891
&& getConvertPointerToPointerArgument(resolver)
891892
&& getConvertMutableArrayToPointerArgument(resolver)
892893
&& getConvertConstArrayToPointerArgument(resolver)

0 commit comments

Comments
 (0)