xref: /minix3/external/bsd/llvm/dist/clang/docs/AutomaticReferenceCounting.rst (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc.. FIXME: move to the stylesheet or Sphinx plugin
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc.. raw:: html
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc  <style>
6*f4a2713aSLionel Sambuc    .arc-term { font-style: italic; font-weight: bold; }
7*f4a2713aSLionel Sambuc    .revision { font-style: italic; }
8*f4a2713aSLionel Sambuc    .when-revised { font-weight: bold; font-style: normal; }
9*f4a2713aSLionel Sambuc
10*f4a2713aSLionel Sambuc    /*
11*f4a2713aSLionel Sambuc     * Automatic numbering is described in this article:
12*f4a2713aSLionel Sambuc     * http://dev.opera.com/articles/view/automatic-numbering-with-css-counters/
13*f4a2713aSLionel Sambuc     */
14*f4a2713aSLionel Sambuc    /*
15*f4a2713aSLionel Sambuc     * Automatic numbering for the TOC.
16*f4a2713aSLionel Sambuc     * This is wrong from the semantics point of view, since it is an ordered
17*f4a2713aSLionel Sambuc     * list, but uses "ul" tag.
18*f4a2713aSLionel Sambuc     */
19*f4a2713aSLionel Sambuc    div#contents.contents.local ul {
20*f4a2713aSLionel Sambuc      counter-reset: toc-section;
21*f4a2713aSLionel Sambuc      list-style-type: none;
22*f4a2713aSLionel Sambuc    }
23*f4a2713aSLionel Sambuc    div#contents.contents.local ul li {
24*f4a2713aSLionel Sambuc      counter-increment: toc-section;
25*f4a2713aSLionel Sambuc      background: none; // Remove bullets
26*f4a2713aSLionel Sambuc    }
27*f4a2713aSLionel Sambuc    div#contents.contents.local ul li a.reference:before {
28*f4a2713aSLionel Sambuc      content: counters(toc-section, ".") " ";
29*f4a2713aSLionel Sambuc    }
30*f4a2713aSLionel Sambuc
31*f4a2713aSLionel Sambuc    /* Automatic numbering for the body. */
32*f4a2713aSLionel Sambuc    body {
33*f4a2713aSLionel Sambuc      counter-reset: section subsection subsubsection;
34*f4a2713aSLionel Sambuc    }
35*f4a2713aSLionel Sambuc    .section h2 {
36*f4a2713aSLionel Sambuc      counter-reset: subsection subsubsection;
37*f4a2713aSLionel Sambuc      counter-increment: section;
38*f4a2713aSLionel Sambuc    }
39*f4a2713aSLionel Sambuc    .section h2 a.toc-backref:before {
40*f4a2713aSLionel Sambuc      content: counter(section) " ";
41*f4a2713aSLionel Sambuc    }
42*f4a2713aSLionel Sambuc    .section h3 {
43*f4a2713aSLionel Sambuc      counter-reset: subsubsection;
44*f4a2713aSLionel Sambuc      counter-increment: subsection;
45*f4a2713aSLionel Sambuc    }
46*f4a2713aSLionel Sambuc    .section h3 a.toc-backref:before {
47*f4a2713aSLionel Sambuc      content: counter(section) "." counter(subsection) " ";
48*f4a2713aSLionel Sambuc    }
49*f4a2713aSLionel Sambuc    .section h4 {
50*f4a2713aSLionel Sambuc      counter-increment: subsubsection;
51*f4a2713aSLionel Sambuc    }
52*f4a2713aSLionel Sambuc    .section h4 a.toc-backref:before {
53*f4a2713aSLionel Sambuc      content: counter(section) "." counter(subsection) "." counter(subsubsection) " ";
54*f4a2713aSLionel Sambuc    }
55*f4a2713aSLionel Sambuc  </style>
56*f4a2713aSLionel Sambuc
57*f4a2713aSLionel Sambuc.. role:: arc-term
58*f4a2713aSLionel Sambuc.. role:: revision
59*f4a2713aSLionel Sambuc.. role:: when-revised
60*f4a2713aSLionel Sambuc
61*f4a2713aSLionel Sambuc==============================================
62*f4a2713aSLionel SambucObjective-C Automatic Reference Counting (ARC)
63*f4a2713aSLionel Sambuc==============================================
64*f4a2713aSLionel Sambuc
65*f4a2713aSLionel Sambuc.. contents::
66*f4a2713aSLionel Sambuc   :local:
67*f4a2713aSLionel Sambuc
68*f4a2713aSLionel Sambuc.. _arc.meta:
69*f4a2713aSLionel Sambuc
70*f4a2713aSLionel SambucAbout this document
71*f4a2713aSLionel Sambuc===================
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc.. _arc.meta.purpose:
74*f4a2713aSLionel Sambuc
75*f4a2713aSLionel SambucPurpose
76*f4a2713aSLionel Sambuc-------
77*f4a2713aSLionel Sambuc
78*f4a2713aSLionel SambucThe first and primary purpose of this document is to serve as a complete
79*f4a2713aSLionel Sambuctechnical specification of Automatic Reference Counting.  Given a core
80*f4a2713aSLionel SambucObjective-C compiler and runtime, it should be possible to write a compiler and
81*f4a2713aSLionel Sambucruntime which implements these new semantics.
82*f4a2713aSLionel Sambuc
83*f4a2713aSLionel SambucThe secondary purpose is to act as a rationale for why ARC was designed in this
84*f4a2713aSLionel Sambucway.  This should remain tightly focused on the technical design and should not
85*f4a2713aSLionel Sambucstray into marketing speculation.
86*f4a2713aSLionel Sambuc
87*f4a2713aSLionel Sambuc.. _arc.meta.background:
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel SambucBackground
90*f4a2713aSLionel Sambuc----------
91*f4a2713aSLionel Sambuc
92*f4a2713aSLionel SambucThis document assumes a basic familiarity with C.
93*f4a2713aSLionel Sambuc
94*f4a2713aSLionel Sambuc:arc-term:`Blocks` are a C language extension for creating anonymous functions.
95*f4a2713aSLionel SambucUsers interact with and transfer block objects using :arc-term:`block
96*f4a2713aSLionel Sambucpointers`, which are represented like a normal pointer.  A block may capture
97*f4a2713aSLionel Sambucvalues from local variables; when this occurs, memory must be dynamically
98*f4a2713aSLionel Sambucallocated.  The initial allocation is done on the stack, but the runtime
99*f4a2713aSLionel Sambucprovides a ``Block_copy`` function which, given a block pointer, either copies
100*f4a2713aSLionel Sambucthe underlying block object to the heap, setting its reference count to 1 and
101*f4a2713aSLionel Sambucreturning the new block pointer, or (if the block object is already on the
102*f4a2713aSLionel Sambucheap) increases its reference count by 1.  The paired function is
103*f4a2713aSLionel Sambuc``Block_release``, which decreases the reference count by 1 and destroys the
104*f4a2713aSLionel Sambucobject if the count reaches zero and is on the heap.
105*f4a2713aSLionel Sambuc
106*f4a2713aSLionel SambucObjective-C is a set of language extensions, significant enough to be
107*f4a2713aSLionel Sambucconsidered a different language.  It is a strict superset of C.  The extensions
108*f4a2713aSLionel Sambuccan also be imposed on C++, producing a language called Objective-C++.  The
109*f4a2713aSLionel Sambucprimary feature is a single-inheritance object system; we briefly describe the
110*f4a2713aSLionel Sambucmodern dialect.
111*f4a2713aSLionel Sambuc
112*f4a2713aSLionel SambucObjective-C defines a new type kind, collectively called the :arc-term:`object
113*f4a2713aSLionel Sambucpointer types`.  This kind has two notable builtin members, ``id`` and
114*f4a2713aSLionel Sambuc``Class``; ``id`` is the final supertype of all object pointers.  The validity
115*f4a2713aSLionel Sambucof conversions between object pointer types is not checked at runtime.  Users
116*f4a2713aSLionel Sambucmay define :arc-term:`classes`; each class is a type, and the pointer to that
117*f4a2713aSLionel Sambuctype is an object pointer type.  A class may have a superclass; its pointer
118*f4a2713aSLionel Sambuctype is a subtype of its superclass's pointer type.  A class has a set of
119*f4a2713aSLionel Sambuc:arc-term:`ivars`, fields which appear on all instances of that class.  For
120*f4a2713aSLionel Sambucevery class *T* there's an associated metaclass; it has no fields, its
121*f4a2713aSLionel Sambucsuperclass is the metaclass of *T*'s superclass, and its metaclass is a global
122*f4a2713aSLionel Sambucclass.  Every class has a global object whose class is the class's metaclass;
123*f4a2713aSLionel Sambucmetaclasses have no associated type, so pointers to this object have type
124*f4a2713aSLionel Sambuc``Class``.
125*f4a2713aSLionel Sambuc
126*f4a2713aSLionel SambucA class declaration (``@interface``) declares a set of :arc-term:`methods`.  A
127*f4a2713aSLionel Sambucmethod has a return type, a list of argument types, and a :arc-term:`selector`:
128*f4a2713aSLionel Sambuca name like ``foo:bar:baz:``, where the number of colons corresponds to the
129*f4a2713aSLionel Sambucnumber of formal arguments.  A method may be an instance method, in which case
130*f4a2713aSLionel Sambucit can be invoked on objects of the class, or a class method, in which case it
131*f4a2713aSLionel Sambuccan be invoked on objects of the metaclass.  A method may be invoked by
132*f4a2713aSLionel Sambucproviding an object (called the :arc-term:`receiver`) and a list of formal
133*f4a2713aSLionel Sambucarguments interspersed with the selector, like so:
134*f4a2713aSLionel Sambuc
135*f4a2713aSLionel Sambuc.. code-block:: objc
136*f4a2713aSLionel Sambuc
137*f4a2713aSLionel Sambuc  [receiver foo: fooArg bar: barArg baz: bazArg]
138*f4a2713aSLionel Sambuc
139*f4a2713aSLionel SambucThis looks in the dynamic class of the receiver for a method with this name,
140*f4a2713aSLionel Sambucthen in that class's superclass, etc., until it finds something it can execute.
141*f4a2713aSLionel SambucThe receiver "expression" may also be the name of a class, in which case the
142*f4a2713aSLionel Sambucactual receiver is the class object for that class, or (within method
143*f4a2713aSLionel Sambucdefinitions) it may be ``super``, in which case the lookup algorithm starts
144*f4a2713aSLionel Sambucwith the static superclass instead of the dynamic class.  The actual methods
145*f4a2713aSLionel Sambucdynamically found in a class are not those declared in the ``@interface``, but
146*f4a2713aSLionel Sambucthose defined in a separate ``@implementation`` declaration; however, when
147*f4a2713aSLionel Sambuccompiling a call, typechecking is done based on the methods declared in the
148*f4a2713aSLionel Sambuc``@interface``.
149*f4a2713aSLionel Sambuc
150*f4a2713aSLionel SambucMethod declarations may also be grouped into :arc-term:`protocols`, which are not
151*f4a2713aSLionel Sambucinherently associated with any class, but which classes may claim to follow.
152*f4a2713aSLionel SambucObject pointer types may be qualified with additional protocols that the object
153*f4a2713aSLionel Sambucis known to support.
154*f4a2713aSLionel Sambuc
155*f4a2713aSLionel Sambuc:arc-term:`Class extensions` are collections of ivars and methods, designed to
156*f4a2713aSLionel Sambucallow a class's ``@interface`` to be split across multiple files; however,
157*f4a2713aSLionel Sambucthere is still a primary implementation file which must see the
158*f4a2713aSLionel Sambuc``@interface``\ s of all class extensions.  :arc-term:`Categories` allow
159*f4a2713aSLionel Sambucmethods (but not ivars) to be declared *post hoc* on an arbitrary class; the
160*f4a2713aSLionel Sambucmethods in the category's ``@implementation`` will be dynamically added to that
161*f4a2713aSLionel Sambucclass's method tables which the category is loaded at runtime, replacing those
162*f4a2713aSLionel Sambucmethods in case of a collision.
163*f4a2713aSLionel Sambuc
164*f4a2713aSLionel SambucIn the standard environment, objects are allocated on the heap, and their
165*f4a2713aSLionel Sambuclifetime is manually managed using a reference count.  This is done using two
166*f4a2713aSLionel Sambucinstance methods which all classes are expected to implement: ``retain``
167*f4a2713aSLionel Sambucincreases the object's reference count by 1, whereas ``release`` decreases it
168*f4a2713aSLionel Sambucby 1 and calls the instance method ``dealloc`` if the count reaches 0.  To
169*f4a2713aSLionel Sambucsimplify certain operations, there is also an :arc-term:`autorelease pool`, a
170*f4a2713aSLionel Sambucthread-local list of objects to call ``release`` on later; an object can be
171*f4a2713aSLionel Sambucadded to this pool by calling ``autorelease`` on it.
172*f4a2713aSLionel Sambuc
173*f4a2713aSLionel SambucBlock pointers may be converted to type ``id``; block objects are laid out in a
174*f4a2713aSLionel Sambucway that makes them compatible with Objective-C objects.  There is a builtin
175*f4a2713aSLionel Sambucclass that all block objects are considered to be objects of; this class
176*f4a2713aSLionel Sambucimplements ``retain`` by adjusting the reference count, not by calling
177*f4a2713aSLionel Sambuc``Block_copy``.
178*f4a2713aSLionel Sambuc
179*f4a2713aSLionel Sambuc.. _arc.meta.evolution:
180*f4a2713aSLionel Sambuc
181*f4a2713aSLionel SambucEvolution
182*f4a2713aSLionel Sambuc---------
183*f4a2713aSLionel Sambuc
184*f4a2713aSLionel SambucARC is under continual evolution, and this document must be updated as the
185*f4a2713aSLionel Sambuclanguage progresses.
186*f4a2713aSLionel Sambuc
187*f4a2713aSLionel SambucIf a change increases the expressiveness of the language, for example by
188*f4a2713aSLionel Sambuclifting a restriction or by adding new syntax, the change will be annotated
189*f4a2713aSLionel Sambucwith a revision marker, like so:
190*f4a2713aSLionel Sambuc
191*f4a2713aSLionel Sambuc  ARC applies to Objective-C pointer types, block pointer types, and
192*f4a2713aSLionel Sambuc  :when-revised:`[beginning Apple 8.0, LLVM 3.8]` :revision:`BPTRs declared
193*f4a2713aSLionel Sambuc  within` ``extern "BCPL"`` blocks.
194*f4a2713aSLionel Sambuc
195*f4a2713aSLionel SambucFor now, it is sensible to version this document by the releases of its sole
196*f4a2713aSLionel Sambucimplementation (and its host project), clang.  "LLVM X.Y" refers to an
197*f4a2713aSLionel Sambucopen-source release of clang from the LLVM project.  "Apple X.Y" refers to an
198*f4a2713aSLionel SambucApple-provided release of the Apple LLVM Compiler.  Other organizations that
199*f4a2713aSLionel Sambucprepare their own, separately-versioned clang releases and wish to maintain
200*f4a2713aSLionel Sambucsimilar information in this document should send requests to cfe-dev.
201*f4a2713aSLionel Sambuc
202*f4a2713aSLionel SambucIf a change decreases the expressiveness of the language, for example by
203*f4a2713aSLionel Sambucimposing a new restriction, this should be taken as an oversight in the
204*f4a2713aSLionel Sambucoriginal specification and something to be avoided in all versions.  Such
205*f4a2713aSLionel Sambucchanges are generally to be avoided.
206*f4a2713aSLionel Sambuc
207*f4a2713aSLionel Sambuc.. _arc.general:
208*f4a2713aSLionel Sambuc
209*f4a2713aSLionel SambucGeneral
210*f4a2713aSLionel Sambuc=======
211*f4a2713aSLionel Sambuc
212*f4a2713aSLionel SambucAutomatic Reference Counting implements automatic memory management for
213*f4a2713aSLionel SambucObjective-C objects and blocks, freeing the programmer from the need to
214*f4a2713aSLionel Sambucexplicitly insert retains and releases.  It does not provide a cycle collector;
215*f4a2713aSLionel Sambucusers must explicitly manage the lifetime of their objects, breaking cycles
216*f4a2713aSLionel Sambucmanually or with weak or unsafe references.
217*f4a2713aSLionel Sambuc
218*f4a2713aSLionel SambucARC may be explicitly enabled with the compiler flag ``-fobjc-arc``.  It may
219*f4a2713aSLionel Sambucalso be explicitly disabled with the compiler flag ``-fno-objc-arc``.  The last
220*f4a2713aSLionel Sambucof these two flags appearing on the compile line "wins".
221*f4a2713aSLionel Sambuc
222*f4a2713aSLionel SambucIf ARC is enabled, ``__has_feature(objc_arc)`` will expand to 1 in the
223*f4a2713aSLionel Sambucpreprocessor.  For more information about ``__has_feature``, see the
224*f4a2713aSLionel Sambuc:ref:`language extensions <langext-__has_feature-__has_extension>` document.
225*f4a2713aSLionel Sambuc
226*f4a2713aSLionel Sambuc.. _arc.objects:
227*f4a2713aSLionel Sambuc
228*f4a2713aSLionel SambucRetainable object pointers
229*f4a2713aSLionel Sambuc==========================
230*f4a2713aSLionel Sambuc
231*f4a2713aSLionel SambucThis section describes retainable object pointers, their basic operations, and
232*f4a2713aSLionel Sambucthe restrictions imposed on their use under ARC.  Note in particular that it
233*f4a2713aSLionel Sambuccovers the rules for pointer *values* (patterns of bits indicating the location
234*f4a2713aSLionel Sambucof a pointed-to object), not pointer *objects* (locations in memory which store
235*f4a2713aSLionel Sambucpointer values).  The rules for objects are covered in the next section.
236*f4a2713aSLionel Sambuc
237*f4a2713aSLionel SambucA :arc-term:`retainable object pointer` (or "retainable pointer") is a value of
238*f4a2713aSLionel Sambuca :arc-term:`retainable object pointer type` ("retainable type").  There are
239*f4a2713aSLionel Sambucthree kinds of retainable object pointer types:
240*f4a2713aSLionel Sambuc
241*f4a2713aSLionel Sambuc* block pointers (formed by applying the caret (``^``) declarator sigil to a
242*f4a2713aSLionel Sambuc  function type)
243*f4a2713aSLionel Sambuc* Objective-C object pointers (``id``, ``Class``, ``NSFoo*``, etc.)
244*f4a2713aSLionel Sambuc* typedefs marked with ``__attribute__((NSObject))``
245*f4a2713aSLionel Sambuc
246*f4a2713aSLionel SambucOther pointer types, such as ``int*`` and ``CFStringRef``, are not subject to
247*f4a2713aSLionel SambucARC's semantics and restrictions.
248*f4a2713aSLionel Sambuc
249*f4a2713aSLionel Sambuc.. admonition:: Rationale
250*f4a2713aSLionel Sambuc
251*f4a2713aSLionel Sambuc  We are not at liberty to require all code to be recompiled with ARC;
252*f4a2713aSLionel Sambuc  therefore, ARC must interoperate with Objective-C code which manages retains
253*f4a2713aSLionel Sambuc  and releases manually.  In general, there are three requirements in order for
254*f4a2713aSLionel Sambuc  a compiler-supported reference-count system to provide reliable
255*f4a2713aSLionel Sambuc  interoperation:
256*f4a2713aSLionel Sambuc
257*f4a2713aSLionel Sambuc  * The type system must reliably identify which objects are to be managed.  An
258*f4a2713aSLionel Sambuc    ``int*`` might be a pointer to a ``malloc``'ed array, or it might be an
259*f4a2713aSLionel Sambuc    interior pointer to such an array, or it might point to some field or local
260*f4a2713aSLionel Sambuc    variable.  In contrast, values of the retainable object pointer types are
261*f4a2713aSLionel Sambuc    never interior.
262*f4a2713aSLionel Sambuc
263*f4a2713aSLionel Sambuc  * The type system must reliably indicate how to manage objects of a type.
264*f4a2713aSLionel Sambuc    This usually means that the type must imply a procedure for incrementing
265*f4a2713aSLionel Sambuc    and decrementing retain counts.  Supporting single-ownership objects
266*f4a2713aSLionel Sambuc    requires a lot more explicit mediation in the language.
267*f4a2713aSLionel Sambuc
268*f4a2713aSLionel Sambuc  * There must be reliable conventions for whether and when "ownership" is
269*f4a2713aSLionel Sambuc    passed between caller and callee, for both arguments and return values.
270*f4a2713aSLionel Sambuc    Objective-C methods follow such a convention very reliably, at least for
271*f4a2713aSLionel Sambuc    system libraries on Mac OS X, and functions always pass objects at +0.  The
272*f4a2713aSLionel Sambuc    C-based APIs for Core Foundation objects, on the other hand, have much more
273*f4a2713aSLionel Sambuc    varied transfer semantics.
274*f4a2713aSLionel Sambuc
275*f4a2713aSLionel SambucThe use of ``__attribute__((NSObject))`` typedefs is not recommended.  If it's
276*f4a2713aSLionel Sambucabsolutely necessary to use this attribute, be very explicit about using the
277*f4a2713aSLionel Sambuctypedef, and do not assume that it will be preserved by language features like
278*f4a2713aSLionel Sambuc``__typeof`` and C++ template argument substitution.
279*f4a2713aSLionel Sambuc
280*f4a2713aSLionel Sambuc.. admonition:: Rationale
281*f4a2713aSLionel Sambuc
282*f4a2713aSLionel Sambuc  Any compiler operation which incidentally strips type "sugar" from a type
283*f4a2713aSLionel Sambuc  will yield a type without the attribute, which may result in unexpected
284*f4a2713aSLionel Sambuc  behavior.
285*f4a2713aSLionel Sambuc
286*f4a2713aSLionel Sambuc.. _arc.objects.retains:
287*f4a2713aSLionel Sambuc
288*f4a2713aSLionel SambucRetain count semantics
289*f4a2713aSLionel Sambuc----------------------
290*f4a2713aSLionel Sambuc
291*f4a2713aSLionel SambucA retainable object pointer is either a :arc-term:`null pointer` or a pointer
292*f4a2713aSLionel Sambucto a valid object.  Furthermore, if it has block pointer type and is not
293*f4a2713aSLionel Sambuc``null`` then it must actually be a pointer to a block object, and if it has
294*f4a2713aSLionel Sambuc``Class`` type (possibly protocol-qualified) then it must actually be a pointer
295*f4a2713aSLionel Sambucto a class object.  Otherwise ARC does not enforce the Objective-C type system
296*f4a2713aSLionel Sambucas long as the implementing methods follow the signature of the static type.
297*f4a2713aSLionel SambucIt is undefined behavior if ARC is exposed to an invalid pointer.
298*f4a2713aSLionel Sambuc
299*f4a2713aSLionel SambucFor ARC's purposes, a valid object is one with "well-behaved" retaining
300*f4a2713aSLionel Sambucoperations.  Specifically, the object must be laid out such that the
301*f4a2713aSLionel SambucObjective-C message send machinery can successfully send it the following
302*f4a2713aSLionel Sambucmessages:
303*f4a2713aSLionel Sambuc
304*f4a2713aSLionel Sambuc* ``retain``, taking no arguments and returning a pointer to the object.
305*f4a2713aSLionel Sambuc* ``release``, taking no arguments and returning ``void``.
306*f4a2713aSLionel Sambuc* ``autorelease``, taking no arguments and returning a pointer to the object.
307*f4a2713aSLionel Sambuc
308*f4a2713aSLionel SambucThe behavior of these methods is constrained in the following ways.  The term
309*f4a2713aSLionel Sambuc:arc-term:`high-level semantics` is an intentionally vague term; the intent is
310*f4a2713aSLionel Sambucthat programmers must implement these methods in a way such that the compiler,
311*f4a2713aSLionel Sambucmodifying code in ways it deems safe according to these constraints, will not
312*f4a2713aSLionel Sambucviolate their requirements.  For example, if the user puts logging statements
313*f4a2713aSLionel Sambucin ``retain``, they should not be surprised if those statements are executed
314*f4a2713aSLionel Sambucmore or less often depending on optimization settings.  These constraints are
315*f4a2713aSLionel Sambucnot exhaustive of the optimization opportunities: values held in local
316*f4a2713aSLionel Sambucvariables are subject to additional restrictions, described later in this
317*f4a2713aSLionel Sambucdocument.
318*f4a2713aSLionel Sambuc
319*f4a2713aSLionel SambucIt is undefined behavior if a computation history featuring a send of
320*f4a2713aSLionel Sambuc``retain`` followed by a send of ``release`` to the same object, with no
321*f4a2713aSLionel Sambucintervening ``release`` on that object, is not equivalent under the high-level
322*f4a2713aSLionel Sambucsemantics to a computation history in which these sends are removed.  Note that
323*f4a2713aSLionel Sambucthis implies that these methods may not raise exceptions.
324*f4a2713aSLionel Sambuc
325*f4a2713aSLionel SambucIt is undefined behavior if a computation history features any use whatsoever
326*f4a2713aSLionel Sambucof an object following the completion of a send of ``release`` that is not
327*f4a2713aSLionel Sambucpreceded by a send of ``retain`` to the same object.
328*f4a2713aSLionel Sambuc
329*f4a2713aSLionel SambucThe behavior of ``autorelease`` must be equivalent to sending ``release`` when
330*f4a2713aSLionel Sambucone of the autorelease pools currently in scope is popped.  It may not throw an
331*f4a2713aSLionel Sambucexception.
332*f4a2713aSLionel Sambuc
333*f4a2713aSLionel SambucWhen the semantics call for performing one of these operations on a retainable
334*f4a2713aSLionel Sambucobject pointer, if that pointer is ``null`` then the effect is a no-op.
335*f4a2713aSLionel Sambuc
336*f4a2713aSLionel SambucAll of the semantics described in this document are subject to additional
337*f4a2713aSLionel Sambuc:ref:`optimization rules <arc.optimization>` which permit the removal or
338*f4a2713aSLionel Sambucoptimization of operations based on local knowledge of data flow.  The
339*f4a2713aSLionel Sambucsemantics describe the high-level behaviors that the compiler implements, not
340*f4a2713aSLionel Sambucan exact sequence of operations that a program will be compiled into.
341*f4a2713aSLionel Sambuc
342*f4a2713aSLionel Sambuc.. _arc.objects.operands:
343*f4a2713aSLionel Sambuc
344*f4a2713aSLionel SambucRetainable object pointers as operands and arguments
345*f4a2713aSLionel Sambuc----------------------------------------------------
346*f4a2713aSLionel Sambuc
347*f4a2713aSLionel SambucIn general, ARC does not perform retain or release operations when simply using
348*f4a2713aSLionel Sambuca retainable object pointer as an operand within an expression.  This includes:
349*f4a2713aSLionel Sambuc
350*f4a2713aSLionel Sambuc* loading a retainable pointer from an object with non-weak :ref:`ownership
351*f4a2713aSLionel Sambuc  <arc.ownership>`,
352*f4a2713aSLionel Sambuc* passing a retainable pointer as an argument to a function or method, and
353*f4a2713aSLionel Sambuc* receiving a retainable pointer as the result of a function or method call.
354*f4a2713aSLionel Sambuc
355*f4a2713aSLionel Sambuc.. admonition:: Rationale
356*f4a2713aSLionel Sambuc
357*f4a2713aSLionel Sambuc  While this might seem uncontroversial, it is actually unsafe when multiple
358*f4a2713aSLionel Sambuc  expressions are evaluated in "parallel", as with binary operators and calls,
359*f4a2713aSLionel Sambuc  because (for example) one expression might load from an object while another
360*f4a2713aSLionel Sambuc  writes to it.  However, C and C++ already call this undefined behavior
361*f4a2713aSLionel Sambuc  because the evaluations are unsequenced, and ARC simply exploits that here to
362*f4a2713aSLionel Sambuc  avoid needing to retain arguments across a large number of calls.
363*f4a2713aSLionel Sambuc
364*f4a2713aSLionel SambucThe remainder of this section describes exceptions to these rules, how those
365*f4a2713aSLionel Sambucexceptions are detected, and what those exceptions imply semantically.
366*f4a2713aSLionel Sambuc
367*f4a2713aSLionel Sambuc.. _arc.objects.operands.consumed:
368*f4a2713aSLionel Sambuc
369*f4a2713aSLionel SambucConsumed parameters
370*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
371*f4a2713aSLionel Sambuc
372*f4a2713aSLionel SambucA function or method parameter of retainable object pointer type may be marked
373*f4a2713aSLionel Sambucas :arc-term:`consumed`, signifying that the callee expects to take ownership
374*f4a2713aSLionel Sambucof a +1 retain count.  This is done by adding the ``ns_consumed`` attribute to
375*f4a2713aSLionel Sambucthe parameter declaration, like so:
376*f4a2713aSLionel Sambuc
377*f4a2713aSLionel Sambuc.. code-block:: objc
378*f4a2713aSLionel Sambuc
379*f4a2713aSLionel Sambuc  void foo(__attribute((ns_consumed)) id x);
380*f4a2713aSLionel Sambuc  - (void) foo: (id) __attribute((ns_consumed)) x;
381*f4a2713aSLionel Sambuc
382*f4a2713aSLionel SambucThis attribute is part of the type of the function or method, not the type of
383*f4a2713aSLionel Sambucthe parameter.  It controls only how the argument is passed and received.
384*f4a2713aSLionel Sambuc
385*f4a2713aSLionel SambucWhen passing such an argument, ARC retains the argument prior to making the
386*f4a2713aSLionel Sambuccall.
387*f4a2713aSLionel Sambuc
388*f4a2713aSLionel SambucWhen receiving such an argument, ARC releases the argument at the end of the
389*f4a2713aSLionel Sambucfunction, subject to the usual optimizations for local values.
390*f4a2713aSLionel Sambuc
391*f4a2713aSLionel Sambuc.. admonition:: Rationale
392*f4a2713aSLionel Sambuc
393*f4a2713aSLionel Sambuc  This formalizes direct transfers of ownership from a caller to a callee.  The
394*f4a2713aSLionel Sambuc  most common scenario here is passing the ``self`` parameter to ``init``, but
395*f4a2713aSLionel Sambuc  it is useful to generalize.  Typically, local optimization will remove any
396*f4a2713aSLionel Sambuc  extra retains and releases: on the caller side the retain will be merged with
397*f4a2713aSLionel Sambuc  a +1 source, and on the callee side the release will be rolled into the
398*f4a2713aSLionel Sambuc  initialization of the parameter.
399*f4a2713aSLionel Sambuc
400*f4a2713aSLionel SambucThe implicit ``self`` parameter of a method may be marked as consumed by adding
401*f4a2713aSLionel Sambuc``__attribute__((ns_consumes_self))`` to the method declaration.  Methods in
402*f4a2713aSLionel Sambucthe ``init`` :ref:`family <arc.method-families>` are treated as if they were
403*f4a2713aSLionel Sambucimplicitly marked with this attribute.
404*f4a2713aSLionel Sambuc
405*f4a2713aSLionel SambucIt is undefined behavior if an Objective-C message send to a method with
406*f4a2713aSLionel Sambuc``ns_consumed`` parameters (other than self) is made with a null receiver.  It
407*f4a2713aSLionel Sambucis undefined behavior if the method to which an Objective-C message send
408*f4a2713aSLionel Sambucstatically resolves to has a different set of ``ns_consumed`` parameters than
409*f4a2713aSLionel Sambucthe method it dynamically resolves to.  It is undefined behavior if a block or
410*f4a2713aSLionel Sambucfunction call is made through a static type with a different set of
411*f4a2713aSLionel Sambuc``ns_consumed`` parameters than the implementation of the called block or
412*f4a2713aSLionel Sambucfunction.
413*f4a2713aSLionel Sambuc
414*f4a2713aSLionel Sambuc.. admonition:: Rationale
415*f4a2713aSLionel Sambuc
416*f4a2713aSLionel Sambuc  Consumed parameters with null receiver are a guaranteed leak.  Mismatches
417*f4a2713aSLionel Sambuc  with consumed parameters will cause over-retains or over-releases, depending
418*f4a2713aSLionel Sambuc  on the direction.  The rule about function calls is really just an
419*f4a2713aSLionel Sambuc  application of the existing C/C++ rule about calling functions through an
420*f4a2713aSLionel Sambuc  incompatible function type, but it's useful to state it explicitly.
421*f4a2713aSLionel Sambuc
422*f4a2713aSLionel Sambuc.. _arc.object.operands.retained-return-values:
423*f4a2713aSLionel Sambuc
424*f4a2713aSLionel SambucRetained return values
425*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
426*f4a2713aSLionel Sambuc
427*f4a2713aSLionel SambucA function or method which returns a retainable object pointer type may be
428*f4a2713aSLionel Sambucmarked as returning a retained value, signifying that the caller expects to take
429*f4a2713aSLionel Sambucownership of a +1 retain count.  This is done by adding the
430*f4a2713aSLionel Sambuc``ns_returns_retained`` attribute to the function or method declaration, like
431*f4a2713aSLionel Sambucso:
432*f4a2713aSLionel Sambuc
433*f4a2713aSLionel Sambuc.. code-block:: objc
434*f4a2713aSLionel Sambuc
435*f4a2713aSLionel Sambuc  id foo(void) __attribute((ns_returns_retained));
436*f4a2713aSLionel Sambuc  - (id) foo __attribute((ns_returns_retained));
437*f4a2713aSLionel Sambuc
438*f4a2713aSLionel SambucThis attribute is part of the type of the function or method.
439*f4a2713aSLionel Sambuc
440*f4a2713aSLionel SambucWhen returning from such a function or method, ARC retains the value at the
441*f4a2713aSLionel Sambucpoint of evaluation of the return statement, before leaving all local scopes.
442*f4a2713aSLionel Sambuc
443*f4a2713aSLionel SambucWhen receiving a return result from such a function or method, ARC releases the
444*f4a2713aSLionel Sambucvalue at the end of the full-expression it is contained within, subject to the
445*f4a2713aSLionel Sambucusual optimizations for local values.
446*f4a2713aSLionel Sambuc
447*f4a2713aSLionel Sambuc.. admonition:: Rationale
448*f4a2713aSLionel Sambuc
449*f4a2713aSLionel Sambuc  This formalizes direct transfers of ownership from a callee to a caller.  The
450*f4a2713aSLionel Sambuc  most common scenario this models is the retained return from ``init``,
451*f4a2713aSLionel Sambuc  ``alloc``, ``new``, and ``copy`` methods, but there are other cases in the
452*f4a2713aSLionel Sambuc  frameworks.  After optimization there are typically no extra retains and
453*f4a2713aSLionel Sambuc  releases required.
454*f4a2713aSLionel Sambuc
455*f4a2713aSLionel SambucMethods in the ``alloc``, ``copy``, ``init``, ``mutableCopy``, and ``new``
456*f4a2713aSLionel Sambuc:ref:`families <arc.method-families>` are implicitly marked
457*f4a2713aSLionel Sambuc``__attribute__((ns_returns_retained))``.  This may be suppressed by explicitly
458*f4a2713aSLionel Sambucmarking the method ``__attribute__((ns_returns_not_retained))``.
459*f4a2713aSLionel Sambuc
460*f4a2713aSLionel SambucIt is undefined behavior if the method to which an Objective-C message send
461*f4a2713aSLionel Sambucstatically resolves has different retain semantics on its result from the
462*f4a2713aSLionel Sambucmethod it dynamically resolves to.  It is undefined behavior if a block or
463*f4a2713aSLionel Sambucfunction call is made through a static type with different retain semantics on
464*f4a2713aSLionel Sambucits result from the implementation of the called block or function.
465*f4a2713aSLionel Sambuc
466*f4a2713aSLionel Sambuc.. admonition:: Rationale
467*f4a2713aSLionel Sambuc
468*f4a2713aSLionel Sambuc  Mismatches with returned results will cause over-retains or over-releases,
469*f4a2713aSLionel Sambuc  depending on the direction.  Again, the rule about function calls is really
470*f4a2713aSLionel Sambuc  just an application of the existing C/C++ rule about calling functions
471*f4a2713aSLionel Sambuc  through an incompatible function type.
472*f4a2713aSLionel Sambuc
473*f4a2713aSLionel Sambuc.. _arc.objects.operands.unretained-returns:
474*f4a2713aSLionel Sambuc
475*f4a2713aSLionel SambucUnretained return values
476*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
477*f4a2713aSLionel Sambuc
478*f4a2713aSLionel SambucA method or function which returns a retainable object type but does not return
479*f4a2713aSLionel Sambuca retained value must ensure that the object is still valid across the return
480*f4a2713aSLionel Sambucboundary.
481*f4a2713aSLionel Sambuc
482*f4a2713aSLionel SambucWhen returning from such a function or method, ARC retains the value at the
483*f4a2713aSLionel Sambucpoint of evaluation of the return statement, then leaves all local scopes, and
484*f4a2713aSLionel Sambucthen balances out the retain while ensuring that the value lives across the
485*f4a2713aSLionel Sambuccall boundary.  In the worst case, this may involve an ``autorelease``, but
486*f4a2713aSLionel Sambuccallers must not assume that the value is actually in the autorelease pool.
487*f4a2713aSLionel Sambuc
488*f4a2713aSLionel SambucARC performs no extra mandatory work on the caller side, although it may elect
489*f4a2713aSLionel Sambucto do something to shorten the lifetime of the returned value.
490*f4a2713aSLionel Sambuc
491*f4a2713aSLionel Sambuc.. admonition:: Rationale
492*f4a2713aSLionel Sambuc
493*f4a2713aSLionel Sambuc  It is common in non-ARC code to not return an autoreleased value; therefore
494*f4a2713aSLionel Sambuc  the convention does not force either path.  It is convenient to not be
495*f4a2713aSLionel Sambuc  required to do unnecessary retains and autoreleases; this permits
496*f4a2713aSLionel Sambuc  optimizations such as eliding retain/autoreleases when it can be shown that
497*f4a2713aSLionel Sambuc  the original pointer will still be valid at the point of return.
498*f4a2713aSLionel Sambuc
499*f4a2713aSLionel SambucA method or function may be marked with
500*f4a2713aSLionel Sambuc``__attribute__((ns_returns_autoreleased))`` to indicate that it returns a
501*f4a2713aSLionel Sambucpointer which is guaranteed to be valid at least as long as the innermost
502*f4a2713aSLionel Sambucautorelease pool.  There are no additional semantics enforced in the definition
503*f4a2713aSLionel Sambucof such a method; it merely enables optimizations in callers.
504*f4a2713aSLionel Sambuc
505*f4a2713aSLionel Sambuc.. _arc.objects.operands.casts:
506*f4a2713aSLionel Sambuc
507*f4a2713aSLionel SambucBridged casts
508*f4a2713aSLionel Sambuc^^^^^^^^^^^^^
509*f4a2713aSLionel Sambuc
510*f4a2713aSLionel SambucA :arc-term:`bridged cast` is a C-style cast annotated with one of three
511*f4a2713aSLionel Sambuckeywords:
512*f4a2713aSLionel Sambuc
513*f4a2713aSLionel Sambuc* ``(__bridge T) op`` casts the operand to the destination type ``T``.  If
514*f4a2713aSLionel Sambuc  ``T`` is a retainable object pointer type, then ``op`` must have a
515*f4a2713aSLionel Sambuc  non-retainable pointer type.  If ``T`` is a non-retainable pointer type,
516*f4a2713aSLionel Sambuc  then ``op`` must have a retainable object pointer type.  Otherwise the cast
517*f4a2713aSLionel Sambuc  is ill-formed.  There is no transfer of ownership, and ARC inserts no retain
518*f4a2713aSLionel Sambuc  operations.
519*f4a2713aSLionel Sambuc* ``(__bridge_retained T) op`` casts the operand, which must have retainable
520*f4a2713aSLionel Sambuc  object pointer type, to the destination type, which must be a non-retainable
521*f4a2713aSLionel Sambuc  pointer type.  ARC retains the value, subject to the usual optimizations on
522*f4a2713aSLionel Sambuc  local values, and the recipient is responsible for balancing that +1.
523*f4a2713aSLionel Sambuc* ``(__bridge_transfer T) op`` casts the operand, which must have
524*f4a2713aSLionel Sambuc  non-retainable pointer type, to the destination type, which must be a
525*f4a2713aSLionel Sambuc  retainable object pointer type.  ARC will release the value at the end of
526*f4a2713aSLionel Sambuc  the enclosing full-expression, subject to the usual optimizations on local
527*f4a2713aSLionel Sambuc  values.
528*f4a2713aSLionel Sambuc
529*f4a2713aSLionel SambucThese casts are required in order to transfer objects in and out of ARC
530*f4a2713aSLionel Sambuccontrol; see the rationale in the section on :ref:`conversion of retainable
531*f4a2713aSLionel Sambucobject pointers <arc.objects.restrictions.conversion>`.
532*f4a2713aSLionel Sambuc
533*f4a2713aSLionel SambucUsing a ``__bridge_retained`` or ``__bridge_transfer`` cast purely to convince
534*f4a2713aSLionel SambucARC to emit an unbalanced retain or release, respectively, is poor form.
535*f4a2713aSLionel Sambuc
536*f4a2713aSLionel Sambuc.. _arc.objects.restrictions:
537*f4a2713aSLionel Sambuc
538*f4a2713aSLionel SambucRestrictions
539*f4a2713aSLionel Sambuc------------
540*f4a2713aSLionel Sambuc
541*f4a2713aSLionel Sambuc.. _arc.objects.restrictions.conversion:
542*f4a2713aSLionel Sambuc
543*f4a2713aSLionel SambucConversion of retainable object pointers
544*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
545*f4a2713aSLionel Sambuc
546*f4a2713aSLionel SambucIn general, a program which attempts to implicitly or explicitly convert a
547*f4a2713aSLionel Sambucvalue of retainable object pointer type to any non-retainable type, or
548*f4a2713aSLionel Sambucvice-versa, is ill-formed.  For example, an Objective-C object pointer shall
549*f4a2713aSLionel Sambucnot be converted to ``void*``.  As an exception, cast to ``intptr_t`` is
550*f4a2713aSLionel Sambucallowed because such casts are not transferring ownership.  The :ref:`bridged
551*f4a2713aSLionel Sambuccasts <arc.objects.operands.casts>` may be used to perform these conversions
552*f4a2713aSLionel Sambucwhere necessary.
553*f4a2713aSLionel Sambuc
554*f4a2713aSLionel Sambuc.. admonition:: Rationale
555*f4a2713aSLionel Sambuc
556*f4a2713aSLionel Sambuc  We cannot ensure the correct management of the lifetime of objects if they
557*f4a2713aSLionel Sambuc  may be freely passed around as unmanaged types.  The bridged casts are
558*f4a2713aSLionel Sambuc  provided so that the programmer may explicitly describe whether the cast
559*f4a2713aSLionel Sambuc  transfers control into or out of ARC.
560*f4a2713aSLionel Sambuc
561*f4a2713aSLionel SambucHowever, the following exceptions apply.
562*f4a2713aSLionel Sambuc
563*f4a2713aSLionel Sambuc.. _arc.objects.restrictions.conversion.with.known.semantics:
564*f4a2713aSLionel Sambuc
565*f4a2713aSLionel SambucConversion to retainable object pointer type of expressions with known semantics
566*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
567*f4a2713aSLionel Sambuc
568*f4a2713aSLionel Sambuc:when-revised:`[beginning Apple 4.0, LLVM 3.1]`
569*f4a2713aSLionel Sambuc:revision:`These exceptions have been greatly expanded; they previously applied
570*f4a2713aSLionel Sambuconly to a much-reduced subset which is difficult to categorize but which
571*f4a2713aSLionel Sambucincluded null pointers, message sends (under the given rules), and the various
572*f4a2713aSLionel Sambucglobal constants.`
573*f4a2713aSLionel Sambuc
574*f4a2713aSLionel SambucAn unbridged conversion to a retainable object pointer type from a type other
575*f4a2713aSLionel Sambucthan a retainable object pointer type is ill-formed, as discussed above, unless
576*f4a2713aSLionel Sambucthe operand of the cast has a syntactic form which is known retained, known
577*f4a2713aSLionel Sambucunretained, or known retain-agnostic.
578*f4a2713aSLionel Sambuc
579*f4a2713aSLionel SambucAn expression is :arc-term:`known retain-agnostic` if it is:
580*f4a2713aSLionel Sambuc
581*f4a2713aSLionel Sambuc* an Objective-C string literal,
582*f4a2713aSLionel Sambuc* a load from a ``const`` system global variable of :ref:`C retainable pointer
583*f4a2713aSLionel Sambuc  type <arc.misc.c-retainable>`, or
584*f4a2713aSLionel Sambuc* a null pointer constant.
585*f4a2713aSLionel Sambuc
586*f4a2713aSLionel SambucAn expression is :arc-term:`known unretained` if it is an rvalue of :ref:`C
587*f4a2713aSLionel Sambucretainable pointer type <arc.misc.c-retainable>` and it is:
588*f4a2713aSLionel Sambuc
589*f4a2713aSLionel Sambuc* a direct call to a function, and either that function has the
590*f4a2713aSLionel Sambuc  ``cf_returns_not_retained`` attribute or it is an :ref:`audited
591*f4a2713aSLionel Sambuc  <arc.misc.c-retainable.audit>` function that does not have the
592*f4a2713aSLionel Sambuc  ``cf_returns_retained`` attribute and does not follow the create/copy naming
593*f4a2713aSLionel Sambuc  convention,
594*f4a2713aSLionel Sambuc* a message send, and the declared method either has the
595*f4a2713aSLionel Sambuc  ``cf_returns_not_retained`` attribute or it has neither the
596*f4a2713aSLionel Sambuc  ``cf_returns_retained`` attribute nor a :ref:`selector family
597*f4a2713aSLionel Sambuc  <arc.method-families>` that implies a retained result.
598*f4a2713aSLionel Sambuc
599*f4a2713aSLionel SambucAn expression is :arc-term:`known retained` if it is an rvalue of :ref:`C
600*f4a2713aSLionel Sambucretainable pointer type <arc.misc.c-retainable>` and it is:
601*f4a2713aSLionel Sambuc
602*f4a2713aSLionel Sambuc* a message send, and the declared method either has the
603*f4a2713aSLionel Sambuc  ``cf_returns_retained`` attribute, or it does not have the
604*f4a2713aSLionel Sambuc  ``cf_returns_not_retained`` attribute but it does have a :ref:`selector
605*f4a2713aSLionel Sambuc  family <arc.method-families>` that implies a retained result.
606*f4a2713aSLionel Sambuc
607*f4a2713aSLionel SambucFurthermore:
608*f4a2713aSLionel Sambuc
609*f4a2713aSLionel Sambuc* a comma expression is classified according to its right-hand side,
610*f4a2713aSLionel Sambuc* a statement expression is classified according to its result expression, if
611*f4a2713aSLionel Sambuc  it has one,
612*f4a2713aSLionel Sambuc* an lvalue-to-rvalue conversion applied to an Objective-C property lvalue is
613*f4a2713aSLionel Sambuc  classified according to the underlying message send, and
614*f4a2713aSLionel Sambuc* a conditional operator is classified according to its second and third
615*f4a2713aSLionel Sambuc  operands, if they agree in classification, or else the other if one is known
616*f4a2713aSLionel Sambuc  retain-agnostic.
617*f4a2713aSLionel Sambuc
618*f4a2713aSLionel SambucIf the cast operand is known retained, the conversion is treated as a
619*f4a2713aSLionel Sambuc``__bridge_transfer`` cast.  If the cast operand is known unretained or known
620*f4a2713aSLionel Sambucretain-agnostic, the conversion is treated as a ``__bridge`` cast.
621*f4a2713aSLionel Sambuc
622*f4a2713aSLionel Sambuc.. admonition:: Rationale
623*f4a2713aSLionel Sambuc
624*f4a2713aSLionel Sambuc  Bridging casts are annoying.  Absent the ability to completely automate the
625*f4a2713aSLionel Sambuc  management of CF objects, however, we are left with relatively poor attempts
626*f4a2713aSLionel Sambuc  to reduce the need for a glut of explicit bridges.  Hence these rules.
627*f4a2713aSLionel Sambuc
628*f4a2713aSLionel Sambuc  We've so far consciously refrained from implicitly turning retained CF
629*f4a2713aSLionel Sambuc  results from function calls into ``__bridge_transfer`` casts.  The worry is
630*f4a2713aSLionel Sambuc  that some code patterns  ---  for example, creating a CF value, assigning it
631*f4a2713aSLionel Sambuc  to an ObjC-typed local, and then calling ``CFRelease`` when done  ---  are a
632*f4a2713aSLionel Sambuc  bit too likely to be accidentally accepted, leading to mysterious behavior.
633*f4a2713aSLionel Sambuc
634*f4a2713aSLionel Sambuc.. _arc.objects.restrictions.conversion-exception-contextual:
635*f4a2713aSLionel Sambuc
636*f4a2713aSLionel SambucConversion from retainable object pointer type in certain contexts
637*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
638*f4a2713aSLionel Sambuc
639*f4a2713aSLionel Sambuc:when-revised:`[beginning Apple 4.0, LLVM 3.1]`
640*f4a2713aSLionel Sambuc
641*f4a2713aSLionel SambucIf an expression of retainable object pointer type is explicitly cast to a
642*f4a2713aSLionel Sambuc:ref:`C retainable pointer type <arc.misc.c-retainable>`, the program is
643*f4a2713aSLionel Sambucill-formed as discussed above unless the result is immediately used:
644*f4a2713aSLionel Sambuc
645*f4a2713aSLionel Sambuc* to initialize a parameter in an Objective-C message send where the parameter
646*f4a2713aSLionel Sambuc  is not marked with the ``cf_consumed`` attribute, or
647*f4a2713aSLionel Sambuc* to initialize a parameter in a direct call to an
648*f4a2713aSLionel Sambuc  :ref:`audited <arc.misc.c-retainable.audit>` function where the parameter is
649*f4a2713aSLionel Sambuc  not marked with the ``cf_consumed`` attribute.
650*f4a2713aSLionel Sambuc
651*f4a2713aSLionel Sambuc.. admonition:: Rationale
652*f4a2713aSLionel Sambuc
653*f4a2713aSLionel Sambuc  Consumed parameters are left out because ARC would naturally balance them
654*f4a2713aSLionel Sambuc  with a retain, which was judged too treacherous.  This is in part because
655*f4a2713aSLionel Sambuc  several of the most common consuming functions are in the ``Release`` family,
656*f4a2713aSLionel Sambuc  and it would be quite unfortunate for explicit releases to be silently
657*f4a2713aSLionel Sambuc  balanced out in this way.
658*f4a2713aSLionel Sambuc
659*f4a2713aSLionel Sambuc.. _arc.ownership:
660*f4a2713aSLionel Sambuc
661*f4a2713aSLionel SambucOwnership qualification
662*f4a2713aSLionel Sambuc=======================
663*f4a2713aSLionel Sambuc
664*f4a2713aSLionel SambucThis section describes the behavior of *objects* of retainable object pointer
665*f4a2713aSLionel Sambuctype; that is, locations in memory which store retainable object pointers.
666*f4a2713aSLionel Sambuc
667*f4a2713aSLionel SambucA type is a :arc-term:`retainable object owner type` if it is a retainable
668*f4a2713aSLionel Sambucobject pointer type or an array type whose element type is a retainable object
669*f4a2713aSLionel Sambucowner type.
670*f4a2713aSLionel Sambuc
671*f4a2713aSLionel SambucAn :arc-term:`ownership qualifier` is a type qualifier which applies only to
672*f4a2713aSLionel Sambucretainable object owner types.  An array type is ownership-qualified according
673*f4a2713aSLionel Sambucto its element type, and adding an ownership qualifier to an array type so
674*f4a2713aSLionel Sambucqualifies its element type.
675*f4a2713aSLionel Sambuc
676*f4a2713aSLionel SambucA program is ill-formed if it attempts to apply an ownership qualifier to a
677*f4a2713aSLionel Sambuctype which is already ownership-qualified, even if it is the same qualifier.
678*f4a2713aSLionel SambucThere is a single exception to this rule: an ownership qualifier may be applied
679*f4a2713aSLionel Sambucto a substituted template type parameter, which overrides the ownership
680*f4a2713aSLionel Sambucqualifier provided by the template argument.
681*f4a2713aSLionel Sambuc
682*f4a2713aSLionel SambucWhen forming a function type, the result type is adjusted so that any
683*f4a2713aSLionel Sambuctop-level ownership qualifier is deleted.
684*f4a2713aSLionel Sambuc
685*f4a2713aSLionel SambucExcept as described under the :ref:`inference rules <arc.ownership.inference>`,
686*f4a2713aSLionel Sambuca program is ill-formed if it attempts to form a pointer or reference type to a
687*f4a2713aSLionel Sambucretainable object owner type which lacks an ownership qualifier.
688*f4a2713aSLionel Sambuc
689*f4a2713aSLionel Sambuc.. admonition:: Rationale
690*f4a2713aSLionel Sambuc
691*f4a2713aSLionel Sambuc  These rules, together with the inference rules, ensure that all objects and
692*f4a2713aSLionel Sambuc  lvalues of retainable object pointer type have an ownership qualifier.  The
693*f4a2713aSLionel Sambuc  ability to override an ownership qualifier during template substitution is
694*f4a2713aSLionel Sambuc  required to counteract the :ref:`inference of __strong for template type
695*f4a2713aSLionel Sambuc  arguments <arc.ownership.inference.template.arguments>`.  Ownership qualifiers
696*f4a2713aSLionel Sambuc  on return types are dropped because they serve no purpose there except to
697*f4a2713aSLionel Sambuc  cause spurious problems with overloading and templates.
698*f4a2713aSLionel Sambuc
699*f4a2713aSLionel SambucThere are four ownership qualifiers:
700*f4a2713aSLionel Sambuc
701*f4a2713aSLionel Sambuc* ``__autoreleasing``
702*f4a2713aSLionel Sambuc* ``__strong``
703*f4a2713aSLionel Sambuc* ``__unsafe_unretained``
704*f4a2713aSLionel Sambuc* ``__weak``
705*f4a2713aSLionel Sambuc
706*f4a2713aSLionel SambucA type is :arc-term:`nontrivially ownership-qualified` if it is qualified with
707*f4a2713aSLionel Sambuc``__autoreleasing``, ``__strong``, or ``__weak``.
708*f4a2713aSLionel Sambuc
709*f4a2713aSLionel Sambuc.. _arc.ownership.spelling:
710*f4a2713aSLionel Sambuc
711*f4a2713aSLionel SambucSpelling
712*f4a2713aSLionel Sambuc--------
713*f4a2713aSLionel Sambuc
714*f4a2713aSLionel SambucThe names of the ownership qualifiers are reserved for the implementation.  A
715*f4a2713aSLionel Sambucprogram may not assume that they are or are not implemented with macros, or
716*f4a2713aSLionel Sambucwhat those macros expand to.
717*f4a2713aSLionel Sambuc
718*f4a2713aSLionel SambucAn ownership qualifier may be written anywhere that any other type qualifier
719*f4a2713aSLionel Sambucmay be written.
720*f4a2713aSLionel Sambuc
721*f4a2713aSLionel SambucIf an ownership qualifier appears in the *declaration-specifiers*, the
722*f4a2713aSLionel Sambucfollowing rules apply:
723*f4a2713aSLionel Sambuc
724*f4a2713aSLionel Sambuc* if the type specifier is a retainable object owner type, the qualifier
725*f4a2713aSLionel Sambuc  initially applies to that type;
726*f4a2713aSLionel Sambuc
727*f4a2713aSLionel Sambuc* otherwise, if the outermost non-array declarator is a pointer
728*f4a2713aSLionel Sambuc  or block pointer declarator, the qualifier initially applies to
729*f4a2713aSLionel Sambuc  that type;
730*f4a2713aSLionel Sambuc
731*f4a2713aSLionel Sambuc* otherwise the program is ill-formed.
732*f4a2713aSLionel Sambuc
733*f4a2713aSLionel Sambuc* If the qualifier is so applied at a position in the declaration
734*f4a2713aSLionel Sambuc  where the next-innermost declarator is a function declarator, and
735*f4a2713aSLionel Sambuc  there is an block declarator within that function declarator, then
736*f4a2713aSLionel Sambuc  the qualifier applies instead to that block declarator and this rule
737*f4a2713aSLionel Sambuc  is considered afresh beginning from the new position.
738*f4a2713aSLionel Sambuc
739*f4a2713aSLionel SambucIf an ownership qualifier appears on the declarator name, or on the declared
740*f4a2713aSLionel Sambucobject, it is applied to the innermost pointer or block-pointer type.
741*f4a2713aSLionel Sambuc
742*f4a2713aSLionel SambucIf an ownership qualifier appears anywhere else in a declarator, it applies to
743*f4a2713aSLionel Sambucthe type there.
744*f4a2713aSLionel Sambuc
745*f4a2713aSLionel Sambuc.. admonition:: Rationale
746*f4a2713aSLionel Sambuc
747*f4a2713aSLionel Sambuc  Ownership qualifiers are like ``const`` and ``volatile`` in the sense
748*f4a2713aSLionel Sambuc  that they may sensibly apply at multiple distinct positions within a
749*f4a2713aSLionel Sambuc  declarator.  However, unlike those qualifiers, there are many
750*f4a2713aSLionel Sambuc  situations where they are not meaningful, and so we make an effort
751*f4a2713aSLionel Sambuc  to "move" the qualifier to a place where it will be meaningful.  The
752*f4a2713aSLionel Sambuc  general goal is to allow the programmer to write, say, ``__strong``
753*f4a2713aSLionel Sambuc  before the entire declaration and have it apply in the leftmost
754*f4a2713aSLionel Sambuc  sensible place.
755*f4a2713aSLionel Sambuc
756*f4a2713aSLionel Sambuc.. _arc.ownership.spelling.property:
757*f4a2713aSLionel Sambuc
758*f4a2713aSLionel SambucProperty declarations
759*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
760*f4a2713aSLionel Sambuc
761*f4a2713aSLionel SambucA property of retainable object pointer type may have ownership.  If the
762*f4a2713aSLionel Sambucproperty's type is ownership-qualified, then the property has that ownership.
763*f4a2713aSLionel SambucIf the property has one of the following modifiers, then the property has the
764*f4a2713aSLionel Sambuccorresponding ownership.  A property is ill-formed if it has conflicting
765*f4a2713aSLionel Sambucsources of ownership, or if it has redundant ownership modifiers, or if it has
766*f4a2713aSLionel Sambuc``__autoreleasing`` ownership.
767*f4a2713aSLionel Sambuc
768*f4a2713aSLionel Sambuc* ``assign`` implies ``__unsafe_unretained`` ownership.
769*f4a2713aSLionel Sambuc* ``copy`` implies ``__strong`` ownership, as well as the usual behavior of
770*f4a2713aSLionel Sambuc  copy semantics on the setter.
771*f4a2713aSLionel Sambuc* ``retain`` implies ``__strong`` ownership.
772*f4a2713aSLionel Sambuc* ``strong`` implies ``__strong`` ownership.
773*f4a2713aSLionel Sambuc* ``unsafe_unretained`` implies ``__unsafe_unretained`` ownership.
774*f4a2713aSLionel Sambuc* ``weak`` implies ``__weak`` ownership.
775*f4a2713aSLionel Sambuc
776*f4a2713aSLionel SambucWith the exception of ``weak``, these modifiers are available in non-ARC
777*f4a2713aSLionel Sambucmodes.
778*f4a2713aSLionel Sambuc
779*f4a2713aSLionel SambucA property's specified ownership is preserved in its metadata, but otherwise
780*f4a2713aSLionel Sambucthe meaning is purely conventional unless the property is synthesized.  If a
781*f4a2713aSLionel Sambucproperty is synthesized, then the :arc-term:`associated instance variable` is
782*f4a2713aSLionel Sambucthe instance variable which is named, possibly implicitly, by the
783*f4a2713aSLionel Sambuc``@synthesize`` declaration.  If the associated instance variable already
784*f4a2713aSLionel Sambucexists, then its ownership qualification must equal the ownership of the
785*f4a2713aSLionel Sambucproperty; otherwise, the instance variable is created with that ownership
786*f4a2713aSLionel Sambucqualification.
787*f4a2713aSLionel Sambuc
788*f4a2713aSLionel SambucA property of retainable object pointer type which is synthesized without a
789*f4a2713aSLionel Sambucsource of ownership has the ownership of its associated instance variable, if it
790*f4a2713aSLionel Sambucalready exists; otherwise, :when-revised:`[beginning Apple 3.1, LLVM 3.1]`
791*f4a2713aSLionel Sambuc:revision:`its ownership is implicitly` ``strong``.  Prior to this revision, it
792*f4a2713aSLionel Sambucwas ill-formed to synthesize such a property.
793*f4a2713aSLionel Sambuc
794*f4a2713aSLionel Sambuc.. admonition:: Rationale
795*f4a2713aSLionel Sambuc
796*f4a2713aSLionel Sambuc  Using ``strong`` by default is safe and consistent with the generic ARC rule
797*f4a2713aSLionel Sambuc  about :ref:`inferring ownership <arc.ownership.inference.variables>`.  It is,
798*f4a2713aSLionel Sambuc  unfortunately, inconsistent with the non-ARC rule which states that such
799*f4a2713aSLionel Sambuc  properties are implicitly ``assign``.  However, that rule is clearly
800*f4a2713aSLionel Sambuc  untenable in ARC, since it leads to default-unsafe code.  The main merit to
801*f4a2713aSLionel Sambuc  banning the properties is to avoid confusion with non-ARC practice, which did
802*f4a2713aSLionel Sambuc  not ultimately strike us as sufficient to justify requiring extra syntax and
803*f4a2713aSLionel Sambuc  (more importantly) forcing novices to understand ownership rules just to
804*f4a2713aSLionel Sambuc  declare a property when the default is so reasonable.  Changing the rule away
805*f4a2713aSLionel Sambuc  from non-ARC practice was acceptable because we had conservatively banned the
806*f4a2713aSLionel Sambuc  synthesis in order to give ourselves exactly this leeway.
807*f4a2713aSLionel Sambuc
808*f4a2713aSLionel SambucApplying ``__attribute__((NSObject))`` to a property not of retainable object
809*f4a2713aSLionel Sambucpointer type has the same behavior it does outside of ARC: it requires the
810*f4a2713aSLionel Sambucproperty type to be some sort of pointer and permits the use of modifiers other
811*f4a2713aSLionel Sambucthan ``assign``.  These modifiers only affect the synthesized getter and
812*f4a2713aSLionel Sambucsetter; direct accesses to the ivar (even if synthesized) still have primitive
813*f4a2713aSLionel Sambucsemantics, and the value in the ivar will not be automatically released during
814*f4a2713aSLionel Sambucdeallocation.
815*f4a2713aSLionel Sambuc
816*f4a2713aSLionel Sambuc.. _arc.ownership.semantics:
817*f4a2713aSLionel Sambuc
818*f4a2713aSLionel SambucSemantics
819*f4a2713aSLionel Sambuc---------
820*f4a2713aSLionel Sambuc
821*f4a2713aSLionel SambucThere are five :arc-term:`managed operations` which may be performed on an
822*f4a2713aSLionel Sambucobject of retainable object pointer type.  Each qualifier specifies different
823*f4a2713aSLionel Sambucsemantics for each of these operations.  It is still undefined behavior to
824*f4a2713aSLionel Sambucaccess an object outside of its lifetime.
825*f4a2713aSLionel Sambuc
826*f4a2713aSLionel SambucA load or store with "primitive semantics" has the same semantics as the
827*f4a2713aSLionel Sambucrespective operation would have on an ``void*`` lvalue with the same alignment
828*f4a2713aSLionel Sambucand non-ownership qualification.
829*f4a2713aSLionel Sambuc
830*f4a2713aSLionel Sambuc:arc-term:`Reading` occurs when performing a lvalue-to-rvalue conversion on an
831*f4a2713aSLionel Sambucobject lvalue.
832*f4a2713aSLionel Sambuc
833*f4a2713aSLionel Sambuc* For ``__weak`` objects, the current pointee is retained and then released at
834*f4a2713aSLionel Sambuc  the end of the current full-expression.  This must execute atomically with
835*f4a2713aSLionel Sambuc  respect to assignments and to the final release of the pointee.
836*f4a2713aSLionel Sambuc* For all other objects, the lvalue is loaded with primitive semantics.
837*f4a2713aSLionel Sambuc
838*f4a2713aSLionel Sambuc:arc-term:`Assignment` occurs when evaluating an assignment operator.  The
839*f4a2713aSLionel Sambucsemantics vary based on the qualification:
840*f4a2713aSLionel Sambuc
841*f4a2713aSLionel Sambuc* For ``__strong`` objects, the new pointee is first retained; second, the
842*f4a2713aSLionel Sambuc  lvalue is loaded with primitive semantics; third, the new pointee is stored
843*f4a2713aSLionel Sambuc  into the lvalue with primitive semantics; and finally, the old pointee is
844*f4a2713aSLionel Sambuc  released.  This is not performed atomically; external synchronization must be
845*f4a2713aSLionel Sambuc  used to make this safe in the face of concurrent loads and stores.
846*f4a2713aSLionel Sambuc* For ``__weak`` objects, the lvalue is updated to point to the new pointee,
847*f4a2713aSLionel Sambuc  unless the new pointee is an object currently undergoing deallocation, in
848*f4a2713aSLionel Sambuc  which case the lvalue is updated to a null pointer.  This must execute
849*f4a2713aSLionel Sambuc  atomically with respect to other assignments to the object, to reads from the
850*f4a2713aSLionel Sambuc  object, and to the final release of the new pointee.
851*f4a2713aSLionel Sambuc* For ``__unsafe_unretained`` objects, the new pointee is stored into the
852*f4a2713aSLionel Sambuc  lvalue using primitive semantics.
853*f4a2713aSLionel Sambuc* For ``__autoreleasing`` objects, the new pointee is retained, autoreleased,
854*f4a2713aSLionel Sambuc  and stored into the lvalue using primitive semantics.
855*f4a2713aSLionel Sambuc
856*f4a2713aSLionel Sambuc:arc-term:`Initialization` occurs when an object's lifetime begins, which
857*f4a2713aSLionel Sambucdepends on its storage duration.  Initialization proceeds in two stages:
858*f4a2713aSLionel Sambuc
859*f4a2713aSLionel Sambuc#. First, a null pointer is stored into the lvalue using primitive semantics.
860*f4a2713aSLionel Sambuc   This step is skipped if the object is ``__unsafe_unretained``.
861*f4a2713aSLionel Sambuc#. Second, if the object has an initializer, that expression is evaluated and
862*f4a2713aSLionel Sambuc   then assigned into the object using the usual assignment semantics.
863*f4a2713aSLionel Sambuc
864*f4a2713aSLionel Sambuc:arc-term:`Destruction` occurs when an object's lifetime ends.  In all cases it
865*f4a2713aSLionel Sambucis semantically equivalent to assigning a null pointer to the object, with the
866*f4a2713aSLionel Sambucproviso that of course the object cannot be legally read after the object's
867*f4a2713aSLionel Sambuclifetime ends.
868*f4a2713aSLionel Sambuc
869*f4a2713aSLionel Sambuc:arc-term:`Moving` occurs in specific situations where an lvalue is "moved
870*f4a2713aSLionel Sambucfrom", meaning that its current pointee will be used but the object may be left
871*f4a2713aSLionel Sambucin a different (but still valid) state.  This arises with ``__block`` variables
872*f4a2713aSLionel Sambucand rvalue references in C++.  For ``__strong`` lvalues, moving is equivalent
873*f4a2713aSLionel Sambucto loading the lvalue with primitive semantics, writing a null pointer to it
874*f4a2713aSLionel Sambucwith primitive semantics, and then releasing the result of the load at the end
875*f4a2713aSLionel Sambucof the current full-expression.  For all other lvalues, moving is equivalent to
876*f4a2713aSLionel Sambucreading the object.
877*f4a2713aSLionel Sambuc
878*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions:
879*f4a2713aSLionel Sambuc
880*f4a2713aSLionel SambucRestrictions
881*f4a2713aSLionel Sambuc------------
882*f4a2713aSLionel Sambuc
883*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions.weak:
884*f4a2713aSLionel Sambuc
885*f4a2713aSLionel SambucWeak-unavailable types
886*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
887*f4a2713aSLionel Sambuc
888*f4a2713aSLionel SambucIt is explicitly permitted for Objective-C classes to not support ``__weak``
889*f4a2713aSLionel Sambucreferences.  It is undefined behavior to perform an operation with weak
890*f4a2713aSLionel Sambucassignment semantics with a pointer to an Objective-C object whose class does
891*f4a2713aSLionel Sambucnot support ``__weak`` references.
892*f4a2713aSLionel Sambuc
893*f4a2713aSLionel Sambuc.. admonition:: Rationale
894*f4a2713aSLionel Sambuc
895*f4a2713aSLionel Sambuc  Historically, it has been possible for a class to provide its own
896*f4a2713aSLionel Sambuc  reference-count implementation by overriding ``retain``, ``release``, etc.
897*f4a2713aSLionel Sambuc  However, weak references to an object require coordination with its class's
898*f4a2713aSLionel Sambuc  reference-count implementation because, among other things, weak loads and
899*f4a2713aSLionel Sambuc  stores must be atomic with respect to the final release.  Therefore, existing
900*f4a2713aSLionel Sambuc  custom reference-count implementations will generally not support weak
901*f4a2713aSLionel Sambuc  references without additional effort.  This is unavoidable without breaking
902*f4a2713aSLionel Sambuc  binary compatibility.
903*f4a2713aSLionel Sambuc
904*f4a2713aSLionel SambucA class may indicate that it does not support weak references by providing the
905*f4a2713aSLionel Sambuc``objc_arc_weak_unavailable`` attribute on the class's interface declaration.  A
906*f4a2713aSLionel Sambucretainable object pointer type is **weak-unavailable** if
907*f4a2713aSLionel Sambucis a pointer to an (optionally protocol-qualified) Objective-C class ``T`` where
908*f4a2713aSLionel Sambuc``T`` or one of its superclasses has the ``objc_arc_weak_unavailable``
909*f4a2713aSLionel Sambucattribute.  A program is ill-formed if it applies the ``__weak`` ownership
910*f4a2713aSLionel Sambucqualifier to a weak-unavailable type or if the value operand of a weak
911*f4a2713aSLionel Sambucassignment operation has a weak-unavailable type.
912*f4a2713aSLionel Sambuc
913*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions.autoreleasing:
914*f4a2713aSLionel Sambuc
915*f4a2713aSLionel SambucStorage duration of ``__autoreleasing`` objects
916*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
917*f4a2713aSLionel Sambuc
918*f4a2713aSLionel SambucA program is ill-formed if it declares an ``__autoreleasing`` object of
919*f4a2713aSLionel Sambucnon-automatic storage duration.  A program is ill-formed if it captures an
920*f4a2713aSLionel Sambuc``__autoreleasing`` object in a block or, unless by reference, in a C++11
921*f4a2713aSLionel Sambuclambda.
922*f4a2713aSLionel Sambuc
923*f4a2713aSLionel Sambuc.. admonition:: Rationale
924*f4a2713aSLionel Sambuc
925*f4a2713aSLionel Sambuc  Autorelease pools are tied to the current thread and scope by their nature.
926*f4a2713aSLionel Sambuc  While it is possible to have temporary objects whose instance variables are
927*f4a2713aSLionel Sambuc  filled with autoreleased objects, there is no way that ARC can provide any
928*f4a2713aSLionel Sambuc  sort of safety guarantee there.
929*f4a2713aSLionel Sambuc
930*f4a2713aSLionel SambucIt is undefined behavior if a non-null pointer is assigned to an
931*f4a2713aSLionel Sambuc``__autoreleasing`` object while an autorelease pool is in scope and then that
932*f4a2713aSLionel Sambucobject is read after the autorelease pool's scope is left.
933*f4a2713aSLionel Sambuc
934*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions.conversion.indirect:
935*f4a2713aSLionel Sambuc
936*f4a2713aSLionel SambucConversion of pointers to ownership-qualified types
937*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
938*f4a2713aSLionel Sambuc
939*f4a2713aSLionel SambucA program is ill-formed if an expression of type ``T*`` is converted,
940*f4a2713aSLionel Sambucexplicitly or implicitly, to the type ``U*``, where ``T`` and ``U`` have
941*f4a2713aSLionel Sambucdifferent ownership qualification, unless:
942*f4a2713aSLionel Sambuc
943*f4a2713aSLionel Sambuc* ``T`` is qualified with ``__strong``, ``__autoreleasing``, or
944*f4a2713aSLionel Sambuc  ``__unsafe_unretained``, and ``U`` is qualified with both ``const`` and
945*f4a2713aSLionel Sambuc  ``__unsafe_unretained``; or
946*f4a2713aSLionel Sambuc* either ``T`` or ``U`` is ``cv void``, where ``cv`` is an optional sequence
947*f4a2713aSLionel Sambuc  of non-ownership qualifiers; or
948*f4a2713aSLionel Sambuc* the conversion is requested with a ``reinterpret_cast`` in Objective-C++; or
949*f4a2713aSLionel Sambuc* the conversion is a well-formed :ref:`pass-by-writeback
950*f4a2713aSLionel Sambuc  <arc.ownership.restrictions.pass_by_writeback>`.
951*f4a2713aSLionel Sambuc
952*f4a2713aSLionel SambucThe analogous rule applies to ``T&`` and ``U&`` in Objective-C++.
953*f4a2713aSLionel Sambuc
954*f4a2713aSLionel Sambuc.. admonition:: Rationale
955*f4a2713aSLionel Sambuc
956*f4a2713aSLionel Sambuc  These rules provide a reasonable level of type-safety for indirect pointers,
957*f4a2713aSLionel Sambuc  as long as the underlying memory is not deallocated.  The conversion to
958*f4a2713aSLionel Sambuc  ``const __unsafe_unretained`` is permitted because the semantics of reads are
959*f4a2713aSLionel Sambuc  equivalent across all these ownership semantics, and that's a very useful and
960*f4a2713aSLionel Sambuc  common pattern.  The interconversion with ``void*`` is useful for allocating
961*f4a2713aSLionel Sambuc  memory or otherwise escaping the type system, but use it carefully.
962*f4a2713aSLionel Sambuc  ``reinterpret_cast`` is considered to be an obvious enough sign of taking
963*f4a2713aSLionel Sambuc  responsibility for any problems.
964*f4a2713aSLionel Sambuc
965*f4a2713aSLionel SambucIt is undefined behavior to access an ownership-qualified object through an
966*f4a2713aSLionel Sambuclvalue of a differently-qualified type, except that any non-``__weak`` object
967*f4a2713aSLionel Sambucmay be read through an ``__unsafe_unretained`` lvalue.
968*f4a2713aSLionel Sambuc
969*f4a2713aSLionel SambucIt is undefined behavior if a managed operation is performed on a ``__strong``
970*f4a2713aSLionel Sambucor ``__weak`` object without a guarantee that it contains a primitive zero
971*f4a2713aSLionel Sambucbit-pattern, or if the storage for such an object is freed or reused without the
972*f4a2713aSLionel Sambucobject being first assigned a null pointer.
973*f4a2713aSLionel Sambuc
974*f4a2713aSLionel Sambuc.. admonition:: Rationale
975*f4a2713aSLionel Sambuc
976*f4a2713aSLionel Sambuc  ARC cannot differentiate between an assignment operator which is intended to
977*f4a2713aSLionel Sambuc  "initialize" dynamic memory and one which is intended to potentially replace
978*f4a2713aSLionel Sambuc  a value.  Therefore the object's pointer must be valid before letting ARC at
979*f4a2713aSLionel Sambuc  it.  Similarly, C and Objective-C do not provide any language hooks for
980*f4a2713aSLionel Sambuc  destroying objects held in dynamic memory, so it is the programmer's
981*f4a2713aSLionel Sambuc  responsibility to avoid leaks (``__strong`` objects) and consistency errors
982*f4a2713aSLionel Sambuc  (``__weak`` objects).
983*f4a2713aSLionel Sambuc
984*f4a2713aSLionel SambucThese requirements are followed automatically in Objective-C++ when creating
985*f4a2713aSLionel Sambucobjects of retainable object owner type with ``new`` or ``new[]`` and destroying
986*f4a2713aSLionel Sambucthem with ``delete``, ``delete[]``, or a pseudo-destructor expression.  Note
987*f4a2713aSLionel Sambucthat arrays of nontrivially-ownership-qualified type are not ABI compatible with
988*f4a2713aSLionel Sambucnon-ARC code because the element type is non-POD: such arrays that are
989*f4a2713aSLionel Sambuc``new[]``'d in ARC translation units cannot be ``delete[]``'d in non-ARC
990*f4a2713aSLionel Sambuctranslation units and vice-versa.
991*f4a2713aSLionel Sambuc
992*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions.pass_by_writeback:
993*f4a2713aSLionel Sambuc
994*f4a2713aSLionel SambucPassing to an out parameter by writeback
995*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
996*f4a2713aSLionel Sambuc
997*f4a2713aSLionel SambucIf the argument passed to a parameter of type ``T __autoreleasing *`` has type
998*f4a2713aSLionel Sambuc``U oq *``, where ``oq`` is an ownership qualifier, then the argument is a
999*f4a2713aSLionel Sambuccandidate for :arc-term:`pass-by-writeback`` if:
1000*f4a2713aSLionel Sambuc
1001*f4a2713aSLionel Sambuc* ``oq`` is ``__strong`` or ``__weak``, and
1002*f4a2713aSLionel Sambuc* it would be legal to initialize a ``T __strong *`` with a ``U __strong *``.
1003*f4a2713aSLionel Sambuc
1004*f4a2713aSLionel SambucFor purposes of overload resolution, an implicit conversion sequence requiring
1005*f4a2713aSLionel Sambuca pass-by-writeback is always worse than an implicit conversion sequence not
1006*f4a2713aSLionel Sambucrequiring a pass-by-writeback.
1007*f4a2713aSLionel Sambuc
1008*f4a2713aSLionel SambucThe pass-by-writeback is ill-formed if the argument expression does not have a
1009*f4a2713aSLionel Sambuclegal form:
1010*f4a2713aSLionel Sambuc
1011*f4a2713aSLionel Sambuc* ``&var``, where ``var`` is a scalar variable of automatic storage duration
1012*f4a2713aSLionel Sambuc  with retainable object pointer type
1013*f4a2713aSLionel Sambuc* a conditional expression where the second and third operands are both legal
1014*f4a2713aSLionel Sambuc  forms
1015*f4a2713aSLionel Sambuc* a cast whose operand is a legal form
1016*f4a2713aSLionel Sambuc* a null pointer constant
1017*f4a2713aSLionel Sambuc
1018*f4a2713aSLionel Sambuc.. admonition:: Rationale
1019*f4a2713aSLionel Sambuc
1020*f4a2713aSLionel Sambuc  The restriction in the form of the argument serves two purposes.  First, it
1021*f4a2713aSLionel Sambuc  makes it impossible to pass the address of an array to the argument, which
1022*f4a2713aSLionel Sambuc  serves to protect against an otherwise serious risk of mis-inferring an
1023*f4a2713aSLionel Sambuc  "array" argument as an out-parameter.  Second, it makes it much less likely
1024*f4a2713aSLionel Sambuc  that the user will see confusing aliasing problems due to the implementation,
1025*f4a2713aSLionel Sambuc  below, where their store to the writeback temporary is not immediately seen
1026*f4a2713aSLionel Sambuc  in the original argument variable.
1027*f4a2713aSLionel Sambuc
1028*f4a2713aSLionel SambucA pass-by-writeback is evaluated as follows:
1029*f4a2713aSLionel Sambuc
1030*f4a2713aSLionel Sambuc#. The argument is evaluated to yield a pointer ``p`` of type ``U oq *``.
1031*f4a2713aSLionel Sambuc#. If ``p`` is a null pointer, then a null pointer is passed as the argument,
1032*f4a2713aSLionel Sambuc   and no further work is required for the pass-by-writeback.
1033*f4a2713aSLionel Sambuc#. Otherwise, a temporary of type ``T __autoreleasing`` is created and
1034*f4a2713aSLionel Sambuc   initialized to a null pointer.
1035*f4a2713aSLionel Sambuc#. If the parameter is not an Objective-C method parameter marked ``out``,
1036*f4a2713aSLionel Sambuc   then ``*p`` is read, and the result is written into the temporary with
1037*f4a2713aSLionel Sambuc   primitive semantics.
1038*f4a2713aSLionel Sambuc#. The address of the temporary is passed as the argument to the actual call.
1039*f4a2713aSLionel Sambuc#. After the call completes, the temporary is loaded with primitive
1040*f4a2713aSLionel Sambuc   semantics, and that value is assigned into ``*p``.
1041*f4a2713aSLionel Sambuc
1042*f4a2713aSLionel Sambuc.. admonition:: Rationale
1043*f4a2713aSLionel Sambuc
1044*f4a2713aSLionel Sambuc  This is all admittedly convoluted.  In an ideal world, we would see that a
1045*f4a2713aSLionel Sambuc  local variable is being passed to an out-parameter and retroactively modify
1046*f4a2713aSLionel Sambuc  its type to be ``__autoreleasing`` rather than ``__strong``.  This would be
1047*f4a2713aSLionel Sambuc  remarkably difficult and not always well-founded under the C type system.
1048*f4a2713aSLionel Sambuc  However, it was judged unacceptably invasive to require programmers to write
1049*f4a2713aSLionel Sambuc  ``__autoreleasing`` on all the variables they intend to use for
1050*f4a2713aSLionel Sambuc  out-parameters.  This was the least bad solution.
1051*f4a2713aSLionel Sambuc
1052*f4a2713aSLionel Sambuc.. _arc.ownership.restrictions.records:
1053*f4a2713aSLionel Sambuc
1054*f4a2713aSLionel SambucOwnership-qualified fields of structs and unions
1055*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1056*f4a2713aSLionel Sambuc
1057*f4a2713aSLionel SambucA program is ill-formed if it declares a member of a C struct or union to have
1058*f4a2713aSLionel Sambuca nontrivially ownership-qualified type.
1059*f4a2713aSLionel Sambuc
1060*f4a2713aSLionel Sambuc.. admonition:: Rationale
1061*f4a2713aSLionel Sambuc
1062*f4a2713aSLionel Sambuc  The resulting type would be non-POD in the C++ sense, but C does not give us
1063*f4a2713aSLionel Sambuc  very good language tools for managing the lifetime of aggregates, so it is
1064*f4a2713aSLionel Sambuc  more convenient to simply forbid them.  It is still possible to manage this
1065*f4a2713aSLionel Sambuc  with a ``void*`` or an ``__unsafe_unretained`` object.
1066*f4a2713aSLionel Sambuc
1067*f4a2713aSLionel SambucThis restriction does not apply in Objective-C++.  However, nontrivally
1068*f4a2713aSLionel Sambucownership-qualified types are considered non-POD: in C++11 terms, they are not
1069*f4a2713aSLionel Sambuctrivially default constructible, copy constructible, move constructible, copy
1070*f4a2713aSLionel Sambucassignable, move assignable, or destructible.  It is a violation of C++'s One
1071*f4a2713aSLionel SambucDefinition Rule to use a class outside of ARC that, under ARC, would have a
1072*f4a2713aSLionel Sambucnontrivially ownership-qualified member.
1073*f4a2713aSLionel Sambuc
1074*f4a2713aSLionel Sambuc.. admonition:: Rationale
1075*f4a2713aSLionel Sambuc
1076*f4a2713aSLionel Sambuc  Unlike in C, we can express all the necessary ARC semantics for
1077*f4a2713aSLionel Sambuc  ownership-qualified subobjects as suboperations of the (default) special
1078*f4a2713aSLionel Sambuc  member functions for the class.  These functions then become non-trivial.
1079*f4a2713aSLionel Sambuc  This has the non-obvious result that the class will have a non-trivial copy
1080*f4a2713aSLionel Sambuc  constructor and non-trivial destructor; if this would not normally be true
1081*f4a2713aSLionel Sambuc  outside of ARC, objects of the type will be passed and returned in an
1082*f4a2713aSLionel Sambuc  ABI-incompatible manner.
1083*f4a2713aSLionel Sambuc
1084*f4a2713aSLionel Sambuc.. _arc.ownership.inference:
1085*f4a2713aSLionel Sambuc
1086*f4a2713aSLionel SambucOwnership inference
1087*f4a2713aSLionel Sambuc-------------------
1088*f4a2713aSLionel Sambuc
1089*f4a2713aSLionel Sambuc.. _arc.ownership.inference.variables:
1090*f4a2713aSLionel Sambuc
1091*f4a2713aSLionel SambucObjects
1092*f4a2713aSLionel Sambuc^^^^^^^
1093*f4a2713aSLionel Sambuc
1094*f4a2713aSLionel SambucIf an object is declared with retainable object owner type, but without an
1095*f4a2713aSLionel Sambucexplicit ownership qualifier, its type is implicitly adjusted to have
1096*f4a2713aSLionel Sambuc``__strong`` qualification.
1097*f4a2713aSLionel Sambuc
1098*f4a2713aSLionel SambucAs a special case, if the object's base type is ``Class`` (possibly
1099*f4a2713aSLionel Sambucprotocol-qualified), the type is adjusted to have ``__unsafe_unretained``
1100*f4a2713aSLionel Sambucqualification instead.
1101*f4a2713aSLionel Sambuc
1102*f4a2713aSLionel Sambuc.. _arc.ownership.inference.indirect_parameters:
1103*f4a2713aSLionel Sambuc
1104*f4a2713aSLionel SambucIndirect parameters
1105*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
1106*f4a2713aSLionel Sambuc
1107*f4a2713aSLionel SambucIf a function or method parameter has type ``T*``, where ``T`` is an
1108*f4a2713aSLionel Sambucownership-unqualified retainable object pointer type, then:
1109*f4a2713aSLionel Sambuc
1110*f4a2713aSLionel Sambuc* if ``T`` is ``const``-qualified or ``Class``, then it is implicitly
1111*f4a2713aSLionel Sambuc  qualified with ``__unsafe_unretained``;
1112*f4a2713aSLionel Sambuc* otherwise, it is implicitly qualified with ``__autoreleasing``.
1113*f4a2713aSLionel Sambuc
1114*f4a2713aSLionel Sambuc.. admonition:: Rationale
1115*f4a2713aSLionel Sambuc
1116*f4a2713aSLionel Sambuc  ``__autoreleasing`` exists mostly for this case, the Cocoa convention for
1117*f4a2713aSLionel Sambuc  out-parameters.  Since a pointer to ``const`` is obviously not an
1118*f4a2713aSLionel Sambuc  out-parameter, we instead use a type more useful for passing arrays.  If the
1119*f4a2713aSLionel Sambuc  user instead intends to pass in a *mutable* array, inferring
1120*f4a2713aSLionel Sambuc  ``__autoreleasing`` is the wrong thing to do; this directs some of the
1121*f4a2713aSLionel Sambuc  caution in the following rules about writeback.
1122*f4a2713aSLionel Sambuc
1123*f4a2713aSLionel SambucSuch a type written anywhere else would be ill-formed by the general rule
1124*f4a2713aSLionel Sambucrequiring ownership qualifiers.
1125*f4a2713aSLionel Sambuc
1126*f4a2713aSLionel SambucThis rule does not apply in Objective-C++ if a parameter's type is dependent in
1127*f4a2713aSLionel Sambuca template pattern and is only *instantiated* to a type which would be a
1128*f4a2713aSLionel Sambucpointer to an unqualified retainable object pointer type.  Such code is still
1129*f4a2713aSLionel Sambucill-formed.
1130*f4a2713aSLionel Sambuc
1131*f4a2713aSLionel Sambuc.. admonition:: Rationale
1132*f4a2713aSLionel Sambuc
1133*f4a2713aSLionel Sambuc  The convention is very unlikely to be intentional in template code.
1134*f4a2713aSLionel Sambuc
1135*f4a2713aSLionel Sambuc.. _arc.ownership.inference.template.arguments:
1136*f4a2713aSLionel Sambuc
1137*f4a2713aSLionel SambucTemplate arguments
1138*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^
1139*f4a2713aSLionel Sambuc
1140*f4a2713aSLionel SambucIf a template argument for a template type parameter is an retainable object
1141*f4a2713aSLionel Sambucowner type that does not have an explicit ownership qualifier, it is adjusted
1142*f4a2713aSLionel Sambucto have ``__strong`` qualification.  This adjustment occurs regardless of
1143*f4a2713aSLionel Sambucwhether the template argument was deduced or explicitly specified.
1144*f4a2713aSLionel Sambuc
1145*f4a2713aSLionel Sambuc.. admonition:: Rationale
1146*f4a2713aSLionel Sambuc
1147*f4a2713aSLionel Sambuc  ``__strong`` is a useful default for containers (e.g., ``std::vector<id>``),
1148*f4a2713aSLionel Sambuc  which would otherwise require explicit qualification.  Moreover, unqualified
1149*f4a2713aSLionel Sambuc  retainable object pointer types are unlikely to be useful within templates,
1150*f4a2713aSLionel Sambuc  since they generally need to have a qualifier applied to the before being
1151*f4a2713aSLionel Sambuc  used.
1152*f4a2713aSLionel Sambuc
1153*f4a2713aSLionel Sambuc.. _arc.method-families:
1154*f4a2713aSLionel Sambuc
1155*f4a2713aSLionel SambucMethod families
1156*f4a2713aSLionel Sambuc===============
1157*f4a2713aSLionel Sambuc
1158*f4a2713aSLionel SambucAn Objective-C method may fall into a :arc-term:`method family`, which is a
1159*f4a2713aSLionel Sambucconventional set of behaviors ascribed to it by the Cocoa conventions.
1160*f4a2713aSLionel Sambuc
1161*f4a2713aSLionel SambucA method is in a certain method family if:
1162*f4a2713aSLionel Sambuc
1163*f4a2713aSLionel Sambuc* it has a ``objc_method_family`` attribute placing it in that family; or if
1164*f4a2713aSLionel Sambuc  not that,
1165*f4a2713aSLionel Sambuc* it does not have an ``objc_method_family`` attribute placing it in a
1166*f4a2713aSLionel Sambuc  different or no family, and
1167*f4a2713aSLionel Sambuc* its selector falls into the corresponding selector family, and
1168*f4a2713aSLionel Sambuc* its signature obeys the added restrictions of the method family.
1169*f4a2713aSLionel Sambuc
1170*f4a2713aSLionel SambucA selector is in a certain selector family if, ignoring any leading
1171*f4a2713aSLionel Sambucunderscores, the first component of the selector either consists entirely of
1172*f4a2713aSLionel Sambucthe name of the method family or it begins with that name followed by a
1173*f4a2713aSLionel Sambuccharacter other than a lowercase letter.  For example, ``_perform:with:`` and
1174*f4a2713aSLionel Sambuc``performWith:`` would fall into the ``perform`` family (if we recognized one),
1175*f4a2713aSLionel Sambucbut ``performing:with`` would not.
1176*f4a2713aSLionel Sambuc
1177*f4a2713aSLionel SambucThe families and their added restrictions are:
1178*f4a2713aSLionel Sambuc
1179*f4a2713aSLionel Sambuc* ``alloc`` methods must return a retainable object pointer type.
1180*f4a2713aSLionel Sambuc* ``copy`` methods must return a retainable object pointer type.
1181*f4a2713aSLionel Sambuc* ``mutableCopy`` methods must return a retainable object pointer type.
1182*f4a2713aSLionel Sambuc* ``new`` methods must return a retainable object pointer type.
1183*f4a2713aSLionel Sambuc* ``init`` methods must be instance methods and must return an Objective-C
1184*f4a2713aSLionel Sambuc  pointer type.  Additionally, a program is ill-formed if it declares or
1185*f4a2713aSLionel Sambuc  contains a call to an ``init`` method whose return type is neither ``id`` nor
1186*f4a2713aSLionel Sambuc  a pointer to a super-class or sub-class of the declaring class (if the method
1187*f4a2713aSLionel Sambuc  was declared on a class) or the static receiver type of the call (if it was
1188*f4a2713aSLionel Sambuc  declared on a protocol).
1189*f4a2713aSLionel Sambuc
1190*f4a2713aSLionel Sambuc  .. admonition:: Rationale
1191*f4a2713aSLionel Sambuc
1192*f4a2713aSLionel Sambuc    There are a fair number of existing methods with ``init``-like selectors
1193*f4a2713aSLionel Sambuc    which nonetheless don't follow the ``init`` conventions.  Typically these
1194*f4a2713aSLionel Sambuc    are either accidental naming collisions or helper methods called during
1195*f4a2713aSLionel Sambuc    initialization.  Because of the peculiar retain/release behavior of
1196*f4a2713aSLionel Sambuc    ``init`` methods, it's very important not to treat these methods as
1197*f4a2713aSLionel Sambuc    ``init`` methods if they aren't meant to be.  It was felt that implicitly
1198*f4a2713aSLionel Sambuc    defining these methods out of the family based on the exact relationship
1199*f4a2713aSLionel Sambuc    between the return type and the declaring class would be much too subtle
1200*f4a2713aSLionel Sambuc    and fragile.  Therefore we identify a small number of legitimate-seeming
1201*f4a2713aSLionel Sambuc    return types and call everything else an error.  This serves the secondary
1202*f4a2713aSLionel Sambuc    purpose of encouraging programmers not to accidentally give methods names
1203*f4a2713aSLionel Sambuc    in the ``init`` family.
1204*f4a2713aSLionel Sambuc
1205*f4a2713aSLionel Sambuc    Note that a method with an ``init``-family selector which returns a
1206*f4a2713aSLionel Sambuc    non-Objective-C type (e.g. ``void``) is perfectly well-formed; it simply
1207*f4a2713aSLionel Sambuc    isn't in the ``init`` family.
1208*f4a2713aSLionel Sambuc
1209*f4a2713aSLionel SambucA program is ill-formed if a method's declarations, implementations, and
1210*f4a2713aSLionel Sambucoverrides do not all have the same method family.
1211*f4a2713aSLionel Sambuc
1212*f4a2713aSLionel Sambuc.. _arc.family.attribute:
1213*f4a2713aSLionel Sambuc
1214*f4a2713aSLionel SambucExplicit method family control
1215*f4a2713aSLionel Sambuc------------------------------
1216*f4a2713aSLionel Sambuc
1217*f4a2713aSLionel SambucA method may be annotated with the ``objc_method_family`` attribute to
1218*f4a2713aSLionel Sambucprecisely control which method family it belongs to.  If a method in an
1219*f4a2713aSLionel Sambuc``@implementation`` does not have this attribute, but there is a method
1220*f4a2713aSLionel Sambucdeclared in the corresponding ``@interface`` that does, then the attribute is
1221*f4a2713aSLionel Sambuccopied to the declaration in the ``@implementation``.  The attribute is
1222*f4a2713aSLionel Sambucavailable outside of ARC, and may be tested for with the preprocessor query
1223*f4a2713aSLionel Sambuc``__has_attribute(objc_method_family)``.
1224*f4a2713aSLionel Sambuc
1225*f4a2713aSLionel SambucThe attribute is spelled
1226*f4a2713aSLionel Sambuc``__attribute__((objc_method_family(`` *family* ``)))``.  If *family* is
1227*f4a2713aSLionel Sambuc``none``, the method has no family, even if it would otherwise be considered to
1228*f4a2713aSLionel Sambuchave one based on its selector and type.  Otherwise, *family* must be one of
1229*f4a2713aSLionel Sambuc``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``, in which case the
1230*f4a2713aSLionel Sambucmethod is considered to belong to the corresponding family regardless of its
1231*f4a2713aSLionel Sambucselector.  It is an error if a method that is explicitly added to a family in
1232*f4a2713aSLionel Sambucthis way does not meet the requirements of the family other than the selector
1233*f4a2713aSLionel Sambucnaming convention.
1234*f4a2713aSLionel Sambuc
1235*f4a2713aSLionel Sambuc.. admonition:: Rationale
1236*f4a2713aSLionel Sambuc
1237*f4a2713aSLionel Sambuc  The rules codified in this document describe the standard conventions of
1238*f4a2713aSLionel Sambuc  Objective-C.  However, as these conventions have not heretofore been enforced
1239*f4a2713aSLionel Sambuc  by an unforgiving mechanical system, they are only imperfectly kept,
1240*f4a2713aSLionel Sambuc  especially as they haven't always even been precisely defined.  While it is
1241*f4a2713aSLionel Sambuc  possible to define low-level ownership semantics with attributes like
1242*f4a2713aSLionel Sambuc  ``ns_returns_retained``, this attribute allows the user to communicate
1243*f4a2713aSLionel Sambuc  semantic intent, which is of use both to ARC (which, e.g., treats calls to
1244*f4a2713aSLionel Sambuc  ``init`` specially) and the static analyzer.
1245*f4a2713aSLionel Sambuc
1246*f4a2713aSLionel Sambuc.. _arc.family.semantics:
1247*f4a2713aSLionel Sambuc
1248*f4a2713aSLionel SambucSemantics of method families
1249*f4a2713aSLionel Sambuc----------------------------
1250*f4a2713aSLionel Sambuc
1251*f4a2713aSLionel SambucA method's membership in a method family may imply non-standard semantics for
1252*f4a2713aSLionel Sambucits parameters and return type.
1253*f4a2713aSLionel Sambuc
1254*f4a2713aSLionel SambucMethods in the ``alloc``, ``copy``, ``mutableCopy``, and ``new`` families ---
1255*f4a2713aSLionel Sambucthat is, methods in all the currently-defined families except ``init`` ---
1256*f4a2713aSLionel Sambucimplicitly :ref:`return a retained object
1257*f4a2713aSLionel Sambuc<arc.object.operands.retained-return-values>` as if they were annotated with
1258*f4a2713aSLionel Sambucthe ``ns_returns_retained`` attribute.  This can be overridden by annotating
1259*f4a2713aSLionel Sambucthe method with either of the ``ns_returns_autoreleased`` or
1260*f4a2713aSLionel Sambuc``ns_returns_not_retained`` attributes.
1261*f4a2713aSLionel Sambuc
1262*f4a2713aSLionel SambucProperties also follow same naming rules as methods.  This means that those in
1263*f4a2713aSLionel Sambucthe ``alloc``, ``copy``, ``mutableCopy``, and ``new`` families provide access
1264*f4a2713aSLionel Sambucto :ref:`retained objects <arc.object.operands.retained-return-values>`.  This
1265*f4a2713aSLionel Sambuccan be overridden by annotating the property with ``ns_returns_not_retained``
1266*f4a2713aSLionel Sambucattribute.
1267*f4a2713aSLionel Sambuc
1268*f4a2713aSLionel Sambuc.. _arc.family.semantics.init:
1269*f4a2713aSLionel Sambuc
1270*f4a2713aSLionel SambucSemantics of ``init``
1271*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
1272*f4a2713aSLionel Sambuc
1273*f4a2713aSLionel SambucMethods in the ``init`` family implicitly :ref:`consume
1274*f4a2713aSLionel Sambuc<arc.objects.operands.consumed>` their ``self`` parameter and :ref:`return a
1275*f4a2713aSLionel Sambucretained object <arc.object.operands.retained-return-values>`.  Neither of
1276*f4a2713aSLionel Sambucthese properties can be altered through attributes.
1277*f4a2713aSLionel Sambuc
1278*f4a2713aSLionel SambucA call to an ``init`` method with a receiver that is either ``self`` (possibly
1279*f4a2713aSLionel Sambucparenthesized or casted) or ``super`` is called a :arc-term:`delegate init
1280*f4a2713aSLionel Sambuccall`.  It is an error for a delegate init call to be made except from an
1281*f4a2713aSLionel Sambuc``init`` method, and excluding blocks within such methods.
1282*f4a2713aSLionel Sambuc
1283*f4a2713aSLionel SambucAs an exception to the :ref:`usual rule <arc.misc.self>`, the variable ``self``
1284*f4a2713aSLionel Sambucis mutable in an ``init`` method and has the usual semantics for a ``__strong``
1285*f4a2713aSLionel Sambucvariable.  However, it is undefined behavior and the program is ill-formed, no
1286*f4a2713aSLionel Sambucdiagnostic required, if an ``init`` method attempts to use the previous value
1287*f4a2713aSLionel Sambucof ``self`` after the completion of a delegate init call.  It is conventional,
1288*f4a2713aSLionel Sambucbut not required, for an ``init`` method to return ``self``.
1289*f4a2713aSLionel Sambuc
1290*f4a2713aSLionel SambucIt is undefined behavior for a program to cause two or more calls to ``init``
1291*f4a2713aSLionel Sambucmethods on the same object, except that each ``init`` method invocation may
1292*f4a2713aSLionel Sambucperform at most one delegate init call.
1293*f4a2713aSLionel Sambuc
1294*f4a2713aSLionel Sambuc.. _arc.family.semantics.result_type:
1295*f4a2713aSLionel Sambuc
1296*f4a2713aSLionel SambucRelated result types
1297*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
1298*f4a2713aSLionel Sambuc
1299*f4a2713aSLionel SambucCertain methods are candidates to have :arc-term:`related result types`:
1300*f4a2713aSLionel Sambuc
1301*f4a2713aSLionel Sambuc* class methods in the ``alloc`` and ``new`` method families
1302*f4a2713aSLionel Sambuc* instance methods in the ``init`` family
1303*f4a2713aSLionel Sambuc* the instance method ``self``
1304*f4a2713aSLionel Sambuc* outside of ARC, the instance methods ``retain`` and ``autorelease``
1305*f4a2713aSLionel Sambuc
1306*f4a2713aSLionel SambucIf the formal result type of such a method is ``id`` or protocol-qualified
1307*f4a2713aSLionel Sambuc``id``, or a type equal to the declaring class or a superclass, then it is said
1308*f4a2713aSLionel Sambucto have a related result type.  In this case, when invoked in an explicit
1309*f4a2713aSLionel Sambucmessage send, it is assumed to return a type related to the type of the
1310*f4a2713aSLionel Sambucreceiver:
1311*f4a2713aSLionel Sambuc
1312*f4a2713aSLionel Sambuc* if it is a class method, and the receiver is a class name ``T``, the message
1313*f4a2713aSLionel Sambuc  send expression has type ``T*``; otherwise
1314*f4a2713aSLionel Sambuc* if it is an instance method, and the receiver has type ``T``, the message
1315*f4a2713aSLionel Sambuc  send expression has type ``T``; otherwise
1316*f4a2713aSLionel Sambuc* the message send expression has the normal result type of the method.
1317*f4a2713aSLionel Sambuc
1318*f4a2713aSLionel SambucThis is a new rule of the Objective-C language and applies outside of ARC.
1319*f4a2713aSLionel Sambuc
1320*f4a2713aSLionel Sambuc.. admonition:: Rationale
1321*f4a2713aSLionel Sambuc
1322*f4a2713aSLionel Sambuc  ARC's automatic code emission is more prone than most code to signature
1323*f4a2713aSLionel Sambuc  errors, i.e. errors where a call was emitted against one method signature,
1324*f4a2713aSLionel Sambuc  but the implementing method has an incompatible signature.  Having more
1325*f4a2713aSLionel Sambuc  precise type information helps drastically lower this risk, as well as
1326*f4a2713aSLionel Sambuc  catching a number of latent bugs.
1327*f4a2713aSLionel Sambuc
1328*f4a2713aSLionel Sambuc.. _arc.optimization:
1329*f4a2713aSLionel Sambuc
1330*f4a2713aSLionel SambucOptimization
1331*f4a2713aSLionel Sambuc============
1332*f4a2713aSLionel Sambuc
1333*f4a2713aSLionel SambucWithin this section, the word :arc-term:`function` will be used to
1334*f4a2713aSLionel Sambucrefer to any structured unit of code, be it a C function, an
1335*f4a2713aSLionel SambucObjective-C method, or a block.
1336*f4a2713aSLionel Sambuc
1337*f4a2713aSLionel SambucThis specification describes ARC as performing specific ``retain`` and
1338*f4a2713aSLionel Sambuc``release`` operations on retainable object pointers at specific
1339*f4a2713aSLionel Sambucpoints during the execution of a program.  These operations make up a
1340*f4a2713aSLionel Sambucnon-contiguous subsequence of the computation history of the program.
1341*f4a2713aSLionel SambucThe portion of this sequence for a particular retainable object
1342*f4a2713aSLionel Sambucpointer for which a specific function execution is directly
1343*f4a2713aSLionel Sambucresponsible is the :arc-term:`formal local retain history` of the
1344*f4a2713aSLionel Sambucobject pointer.  The corresponding actual sequence executed is the
1345*f4a2713aSLionel Sambuc`dynamic local retain history`.
1346*f4a2713aSLionel Sambuc
1347*f4a2713aSLionel SambucHowever, under certain circumstances, ARC is permitted to re-order and
1348*f4a2713aSLionel Sambuceliminate operations in a manner which may alter the overall
1349*f4a2713aSLionel Sambuccomputation history beyond what is permitted by the general "as if"
1350*f4a2713aSLionel Sambucrule of C/C++ and the :ref:`restrictions <arc.objects.retains>` on
1351*f4a2713aSLionel Sambucthe implementation of ``retain`` and ``release``.
1352*f4a2713aSLionel Sambuc
1353*f4a2713aSLionel Sambuc.. admonition:: Rationale
1354*f4a2713aSLionel Sambuc
1355*f4a2713aSLionel Sambuc  Specifically, ARC is sometimes permitted to optimize ``release``
1356*f4a2713aSLionel Sambuc  operations in ways which might cause an object to be deallocated
1357*f4a2713aSLionel Sambuc  before it would otherwise be.  Without this, it would be almost
1358*f4a2713aSLionel Sambuc  impossible to eliminate any ``retain``/``release`` pairs.  For
1359*f4a2713aSLionel Sambuc  example, consider the following code:
1360*f4a2713aSLionel Sambuc
1361*f4a2713aSLionel Sambuc  .. code-block:: objc
1362*f4a2713aSLionel Sambuc
1363*f4a2713aSLionel Sambuc    id x = _ivar;
1364*f4a2713aSLionel Sambuc    [x foo];
1365*f4a2713aSLionel Sambuc
1366*f4a2713aSLionel Sambuc  If we were not permitted in any event to shorten the lifetime of the
1367*f4a2713aSLionel Sambuc  object in ``x``, then we would not be able to eliminate this retain
1368*f4a2713aSLionel Sambuc  and release unless we could prove that the message send could not
1369*f4a2713aSLionel Sambuc  modify ``_ivar`` (or deallocate ``self``).  Since message sends are
1370*f4a2713aSLionel Sambuc  opaque to the optimizer, this is not possible, and so ARC's hands
1371*f4a2713aSLionel Sambuc  would be almost completely tied.
1372*f4a2713aSLionel Sambuc
1373*f4a2713aSLionel SambucARC makes no guarantees about the execution of a computation history
1374*f4a2713aSLionel Sambucwhich contains undefined behavior.  In particular, ARC makes no
1375*f4a2713aSLionel Sambucguarantees in the presence of race conditions.
1376*f4a2713aSLionel Sambuc
1377*f4a2713aSLionel SambucARC may assume that any retainable object pointers it receives or
1378*f4a2713aSLionel Sambucgenerates are instantaneously valid from that point until a point
1379*f4a2713aSLionel Sambucwhich, by the concurrency model of the host language, happens-after
1380*f4a2713aSLionel Sambucthe generation of the pointer and happens-before a release of that
1381*f4a2713aSLionel Sambucobject (possibly via an aliasing pointer or indirectly due to
1382*f4a2713aSLionel Sambucdestruction of a different object).
1383*f4a2713aSLionel Sambuc
1384*f4a2713aSLionel Sambuc.. admonition:: Rationale
1385*f4a2713aSLionel Sambuc
1386*f4a2713aSLionel Sambuc  There is very little point in trying to guarantee correctness in the
1387*f4a2713aSLionel Sambuc  presence of race conditions.  ARC does not have a stack-scanning
1388*f4a2713aSLionel Sambuc  garbage collector, and guaranteeing the atomicity of every load and
1389*f4a2713aSLionel Sambuc  store operation would be prohibitive and preclude a vast amount of
1390*f4a2713aSLionel Sambuc  optimization.
1391*f4a2713aSLionel Sambuc
1392*f4a2713aSLionel SambucARC may assume that non-ARC code engages in sensible balancing
1393*f4a2713aSLionel Sambucbehavior and does not rely on exact or minimum retain count values
1394*f4a2713aSLionel Sambucexcept as guaranteed by ``__strong`` object invariants or +1 transfer
1395*f4a2713aSLionel Sambucconventions.  For example, if an object is provably double-retained
1396*f4a2713aSLionel Sambucand double-released, ARC may eliminate the inner retain and release;
1397*f4a2713aSLionel Sambucit does not need to guard against code which performs an unbalanced
1398*f4a2713aSLionel Sambucrelease followed by a "balancing" retain.
1399*f4a2713aSLionel Sambuc
1400*f4a2713aSLionel Sambuc.. _arc.optimization.liveness:
1401*f4a2713aSLionel Sambuc
1402*f4a2713aSLionel SambucObject liveness
1403*f4a2713aSLionel Sambuc---------------
1404*f4a2713aSLionel Sambuc
1405*f4a2713aSLionel SambucARC may not allow a retainable object ``X`` to be deallocated at a
1406*f4a2713aSLionel Sambuctime ``T`` in a computation history if:
1407*f4a2713aSLionel Sambuc
1408*f4a2713aSLionel Sambuc* ``X`` is the value stored in a ``__strong`` object ``S`` with
1409*f4a2713aSLionel Sambuc  :ref:`precise lifetime semantics <arc.optimization.precise>`, or
1410*f4a2713aSLionel Sambuc
1411*f4a2713aSLionel Sambuc* ``X`` is the value stored in a ``__strong`` object ``S`` with
1412*f4a2713aSLionel Sambuc  imprecise lifetime semantics and, at some point after ``T`` but
1413*f4a2713aSLionel Sambuc  before the next store to ``S``, the computation history features a
1414*f4a2713aSLionel Sambuc  load from ``S`` and in some way depends on the value loaded, or
1415*f4a2713aSLionel Sambuc
1416*f4a2713aSLionel Sambuc* ``X`` is a value described as being released at the end of the
1417*f4a2713aSLionel Sambuc  current full-expression and, at some point after ``T`` but before
1418*f4a2713aSLionel Sambuc  the end of the full-expression, the computation history depends
1419*f4a2713aSLionel Sambuc  on that value.
1420*f4a2713aSLionel Sambuc
1421*f4a2713aSLionel Sambuc.. admonition:: Rationale
1422*f4a2713aSLionel Sambuc
1423*f4a2713aSLionel Sambuc  The intent of the second rule is to say that objects held in normal
1424*f4a2713aSLionel Sambuc  ``__strong`` local variables may be released as soon as the value in
1425*f4a2713aSLionel Sambuc  the variable is no longer being used: either the variable stops
1426*f4a2713aSLionel Sambuc  being used completely or a new value is stored in the variable.
1427*f4a2713aSLionel Sambuc
1428*f4a2713aSLionel Sambuc  The intent of the third rule is to say that return values may be
1429*f4a2713aSLionel Sambuc  released after they've been used.
1430*f4a2713aSLionel Sambuc
1431*f4a2713aSLionel SambucA computation history depends on a pointer value ``P`` if it:
1432*f4a2713aSLionel Sambuc
1433*f4a2713aSLionel Sambuc* performs a pointer comparison with ``P``,
1434*f4a2713aSLionel Sambuc* loads from ``P``,
1435*f4a2713aSLionel Sambuc* stores to ``P``,
1436*f4a2713aSLionel Sambuc* depends on a pointer value ``Q`` derived via pointer arithmetic
1437*f4a2713aSLionel Sambuc  from ``P`` (including an instance-variable or field access), or
1438*f4a2713aSLionel Sambuc* depends on a pointer value ``Q`` loaded from ``P``.
1439*f4a2713aSLionel Sambuc
1440*f4a2713aSLionel SambucDependency applies only to values derived directly or indirectly from
1441*f4a2713aSLionel Sambuca particular expression result and does not occur merely because a
1442*f4a2713aSLionel Sambucseparate pointer value dynamically aliases ``P``.  Furthermore, this
1443*f4a2713aSLionel Sambucdependency is not carried by values that are stored to objects.
1444*f4a2713aSLionel Sambuc
1445*f4a2713aSLionel Sambuc.. admonition:: Rationale
1446*f4a2713aSLionel Sambuc
1447*f4a2713aSLionel Sambuc  The restrictions on dependency are intended to make this analysis
1448*f4a2713aSLionel Sambuc  feasible by an optimizer with only incomplete information about a
1449*f4a2713aSLionel Sambuc  program.  Essentially, dependence is carried to "obvious" uses of a
1450*f4a2713aSLionel Sambuc  pointer.  Merely passing a pointer argument to a function does not
1451*f4a2713aSLionel Sambuc  itself cause dependence, but since generally the optimizer will not
1452*f4a2713aSLionel Sambuc  be able to prove that the function doesn't depend on that parameter,
1453*f4a2713aSLionel Sambuc  it will be forced to conservatively assume it does.
1454*f4a2713aSLionel Sambuc
1455*f4a2713aSLionel Sambuc  Dependency propagates to values loaded from a pointer because those
1456*f4a2713aSLionel Sambuc  values might be invalidated by deallocating the object.  For
1457*f4a2713aSLionel Sambuc  example, given the code ``__strong id x = p->ivar;``, ARC must not
1458*f4a2713aSLionel Sambuc  move the release of ``p`` to between the load of ``p->ivar`` and the
1459*f4a2713aSLionel Sambuc  retain of that value for storing into ``x``.
1460*f4a2713aSLionel Sambuc
1461*f4a2713aSLionel Sambuc  Dependency does not propagate through stores of dependent pointer
1462*f4a2713aSLionel Sambuc  values because doing so would allow dependency to outlive the
1463*f4a2713aSLionel Sambuc  full-expression which produced the original value.  For example, the
1464*f4a2713aSLionel Sambuc  address of an instance variable could be written to some global
1465*f4a2713aSLionel Sambuc  location and then freely accessed during the lifetime of the local,
1466*f4a2713aSLionel Sambuc  or a function could return an inner pointer of an object and store
1467*f4a2713aSLionel Sambuc  it to a local.  These cases would be potentially impossible to
1468*f4a2713aSLionel Sambuc  reason about and so would basically prevent any optimizations based
1469*f4a2713aSLionel Sambuc  on imprecise lifetime.  There are also uncommon enough to make it
1470*f4a2713aSLionel Sambuc  reasonable to require the precise-lifetime annotation if someone
1471*f4a2713aSLionel Sambuc  really wants to rely on them.
1472*f4a2713aSLionel Sambuc
1473*f4a2713aSLionel Sambuc  Dependency does propagate through return values of pointer type.
1474*f4a2713aSLionel Sambuc  The compelling source of need for this rule is a property accessor
1475*f4a2713aSLionel Sambuc  which returns an un-autoreleased result; the calling function must
1476*f4a2713aSLionel Sambuc  have the chance to operate on the value, e.g. to retain it, before
1477*f4a2713aSLionel Sambuc  ARC releases the original pointer.  Note again, however, that
1478*f4a2713aSLionel Sambuc  dependence does not survive a store, so ARC does not guarantee the
1479*f4a2713aSLionel Sambuc  continued validity of the return value past the end of the
1480*f4a2713aSLionel Sambuc  full-expression.
1481*f4a2713aSLionel Sambuc
1482*f4a2713aSLionel Sambuc.. _arc.optimization.object_lifetime:
1483*f4a2713aSLionel Sambuc
1484*f4a2713aSLionel SambucNo object lifetime extension
1485*f4a2713aSLionel Sambuc----------------------------
1486*f4a2713aSLionel Sambuc
1487*f4a2713aSLionel SambucIf, in the formal computation history of the program, an object ``X``
1488*f4a2713aSLionel Sambuchas been deallocated by the time of an observable side-effect, then
1489*f4a2713aSLionel SambucARC must cause ``X`` to be deallocated by no later than the occurrence
1490*f4a2713aSLionel Sambucof that side-effect, except as influenced by the re-ordering of the
1491*f4a2713aSLionel Sambucdestruction of objects.
1492*f4a2713aSLionel Sambuc
1493*f4a2713aSLionel Sambuc.. admonition:: Rationale
1494*f4a2713aSLionel Sambuc
1495*f4a2713aSLionel Sambuc  This rule is intended to prohibit ARC from observably extending the
1496*f4a2713aSLionel Sambuc  lifetime of a retainable object, other than as specified in this
1497*f4a2713aSLionel Sambuc  document.  Together with the rule limiting the transformation of
1498*f4a2713aSLionel Sambuc  releases, this rule requires ARC to eliminate retains and release
1499*f4a2713aSLionel Sambuc  only in pairs.
1500*f4a2713aSLionel Sambuc
1501*f4a2713aSLionel Sambuc  ARC's power to reorder the destruction of objects is critical to its
1502*f4a2713aSLionel Sambuc  ability to do any optimization, for essentially the same reason that
1503*f4a2713aSLionel Sambuc  it must retain the power to decrease the lifetime of an object.
1504*f4a2713aSLionel Sambuc  Unfortunately, while it's generally poor style for the destruction
1505*f4a2713aSLionel Sambuc  of objects to have arbitrary side-effects, it's certainly possible.
1506*f4a2713aSLionel Sambuc  Hence the caveat.
1507*f4a2713aSLionel Sambuc
1508*f4a2713aSLionel Sambuc.. _arc.optimization.precise:
1509*f4a2713aSLionel Sambuc
1510*f4a2713aSLionel SambucPrecise lifetime semantics
1511*f4a2713aSLionel Sambuc--------------------------
1512*f4a2713aSLionel Sambuc
1513*f4a2713aSLionel SambucIn general, ARC maintains an invariant that a retainable object pointer held in
1514*f4a2713aSLionel Sambuca ``__strong`` object will be retained for the full formal lifetime of the
1515*f4a2713aSLionel Sambucobject.  Objects subject to this invariant have :arc-term:`precise lifetime
1516*f4a2713aSLionel Sambucsemantics`.
1517*f4a2713aSLionel Sambuc
1518*f4a2713aSLionel SambucBy default, local variables of automatic storage duration do not have precise
1519*f4a2713aSLionel Sambuclifetime semantics.  Such objects are simply strong references which hold
1520*f4a2713aSLionel Sambucvalues of retainable object pointer type, and these values are still fully
1521*f4a2713aSLionel Sambucsubject to the optimizations on values under local control.
1522*f4a2713aSLionel Sambuc
1523*f4a2713aSLionel Sambuc.. admonition:: Rationale
1524*f4a2713aSLionel Sambuc
1525*f4a2713aSLionel Sambuc  Applying these precise-lifetime semantics strictly would be prohibitive.
1526*f4a2713aSLionel Sambuc  Many useful optimizations that might theoretically decrease the lifetime of
1527*f4a2713aSLionel Sambuc  an object would be rendered impossible.  Essentially, it promises too much.
1528*f4a2713aSLionel Sambuc
1529*f4a2713aSLionel SambucA local variable of retainable object owner type and automatic storage duration
1530*f4a2713aSLionel Sambucmay be annotated with the ``objc_precise_lifetime`` attribute to indicate that
1531*f4a2713aSLionel Sambucit should be considered to be an object with precise lifetime semantics.
1532*f4a2713aSLionel Sambuc
1533*f4a2713aSLionel Sambuc.. admonition:: Rationale
1534*f4a2713aSLionel Sambuc
1535*f4a2713aSLionel Sambuc  Nonetheless, it is sometimes useful to be able to force an object to be
1536*f4a2713aSLionel Sambuc  released at a precise time, even if that object does not appear to be used.
1537*f4a2713aSLionel Sambuc  This is likely to be uncommon enough that the syntactic weight of explicitly
1538*f4a2713aSLionel Sambuc  requesting these semantics will not be burdensome, and may even make the code
1539*f4a2713aSLionel Sambuc  clearer.
1540*f4a2713aSLionel Sambuc
1541*f4a2713aSLionel Sambuc.. _arc.misc:
1542*f4a2713aSLionel Sambuc
1543*f4a2713aSLionel SambucMiscellaneous
1544*f4a2713aSLionel Sambuc=============
1545*f4a2713aSLionel Sambuc
1546*f4a2713aSLionel Sambuc.. _arc.misc.special_methods:
1547*f4a2713aSLionel Sambuc
1548*f4a2713aSLionel SambucSpecial methods
1549*f4a2713aSLionel Sambuc---------------
1550*f4a2713aSLionel Sambuc
1551*f4a2713aSLionel Sambuc.. _arc.misc.special_methods.retain:
1552*f4a2713aSLionel Sambuc
1553*f4a2713aSLionel SambucMemory management methods
1554*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^
1555*f4a2713aSLionel Sambuc
1556*f4a2713aSLionel SambucA program is ill-formed if it contains a method definition, message send, or
1557*f4a2713aSLionel Sambuc``@selector`` expression for any of the following selectors:
1558*f4a2713aSLionel Sambuc
1559*f4a2713aSLionel Sambuc* ``autorelease``
1560*f4a2713aSLionel Sambuc* ``release``
1561*f4a2713aSLionel Sambuc* ``retain``
1562*f4a2713aSLionel Sambuc* ``retainCount``
1563*f4a2713aSLionel Sambuc
1564*f4a2713aSLionel Sambuc.. admonition:: Rationale
1565*f4a2713aSLionel Sambuc
1566*f4a2713aSLionel Sambuc  ``retainCount`` is banned because ARC robs it of consistent semantics.  The
1567*f4a2713aSLionel Sambuc  others were banned after weighing three options for how to deal with message
1568*f4a2713aSLionel Sambuc  sends:
1569*f4a2713aSLionel Sambuc
1570*f4a2713aSLionel Sambuc  **Honoring** them would work out very poorly if a programmer naively or
1571*f4a2713aSLionel Sambuc  accidentally tried to incorporate code written for manual retain/release code
1572*f4a2713aSLionel Sambuc  into an ARC program.  At best, such code would do twice as much work as
1573*f4a2713aSLionel Sambuc  necessary; quite frequently, however, ARC and the explicit code would both
1574*f4a2713aSLionel Sambuc  try to balance the same retain, leading to crashes.  The cost is losing the
1575*f4a2713aSLionel Sambuc  ability to perform "unrooted" retains, i.e. retains not logically
1576*f4a2713aSLionel Sambuc  corresponding to a strong reference in the object graph.
1577*f4a2713aSLionel Sambuc
1578*f4a2713aSLionel Sambuc  **Ignoring** them would badly violate user expectations about their code.
1579*f4a2713aSLionel Sambuc  While it *would* make it easier to develop code simultaneously for ARC and
1580*f4a2713aSLionel Sambuc  non-ARC, there is very little reason to do so except for certain library
1581*f4a2713aSLionel Sambuc  developers.  ARC and non-ARC translation units share an execution model and
1582*f4a2713aSLionel Sambuc  can seamlessly interoperate.  Within a translation unit, a developer who
1583*f4a2713aSLionel Sambuc  faithfully maintains their code in non-ARC mode is suffering all the
1584*f4a2713aSLionel Sambuc  restrictions of ARC for zero benefit, while a developer who isn't testing the
1585*f4a2713aSLionel Sambuc  non-ARC mode is likely to be unpleasantly surprised if they try to go back to
1586*f4a2713aSLionel Sambuc  it.
1587*f4a2713aSLionel Sambuc
1588*f4a2713aSLionel Sambuc  **Banning** them has the disadvantage of making it very awkward to migrate
1589*f4a2713aSLionel Sambuc  existing code to ARC.  The best answer to that, given a number of other
1590*f4a2713aSLionel Sambuc  changes and restrictions in ARC, is to provide a specialized tool to assist
1591*f4a2713aSLionel Sambuc  users in that migration.
1592*f4a2713aSLionel Sambuc
1593*f4a2713aSLionel Sambuc  Implementing these methods was banned because they are too integral to the
1594*f4a2713aSLionel Sambuc  semantics of ARC; many tricks which worked tolerably under manual reference
1595*f4a2713aSLionel Sambuc  counting will misbehave if ARC performs an ephemeral extra retain or two.  If
1596*f4a2713aSLionel Sambuc  absolutely required, it is still possible to implement them in non-ARC code,
1597*f4a2713aSLionel Sambuc  for example in a category; the implementations must obey the :ref:`semantics
1598*f4a2713aSLionel Sambuc  <arc.objects.retains>` laid out elsewhere in this document.
1599*f4a2713aSLionel Sambuc
1600*f4a2713aSLionel Sambuc.. _arc.misc.special_methods.dealloc:
1601*f4a2713aSLionel Sambuc
1602*f4a2713aSLionel Sambuc``dealloc``
1603*f4a2713aSLionel Sambuc^^^^^^^^^^^
1604*f4a2713aSLionel Sambuc
1605*f4a2713aSLionel SambucA program is ill-formed if it contains a message send or ``@selector``
1606*f4a2713aSLionel Sambucexpression for the selector ``dealloc``.
1607*f4a2713aSLionel Sambuc
1608*f4a2713aSLionel Sambuc.. admonition:: Rationale
1609*f4a2713aSLionel Sambuc
1610*f4a2713aSLionel Sambuc  There are no legitimate reasons to call ``dealloc`` directly.
1611*f4a2713aSLionel Sambuc
1612*f4a2713aSLionel SambucA class may provide a method definition for an instance method named
1613*f4a2713aSLionel Sambuc``dealloc``.  This method will be called after the final ``release`` of the
1614*f4a2713aSLionel Sambucobject but before it is deallocated or any of its instance variables are
1615*f4a2713aSLionel Sambucdestroyed.  The superclass's implementation of ``dealloc`` will be called
1616*f4a2713aSLionel Sambucautomatically when the method returns.
1617*f4a2713aSLionel Sambuc
1618*f4a2713aSLionel Sambuc.. admonition:: Rationale
1619*f4a2713aSLionel Sambuc
1620*f4a2713aSLionel Sambuc  Even though ARC destroys instance variables automatically, there are still
1621*f4a2713aSLionel Sambuc  legitimate reasons to write a ``dealloc`` method, such as freeing
1622*f4a2713aSLionel Sambuc  non-retainable resources.  Failing to call ``[super dealloc]`` in such a
1623*f4a2713aSLionel Sambuc  method is nearly always a bug.  Sometimes, the object is simply trying to
1624*f4a2713aSLionel Sambuc  prevent itself from being destroyed, but ``dealloc`` is really far too late
1625*f4a2713aSLionel Sambuc  for the object to be raising such objections.  Somewhat more legitimately, an
1626*f4a2713aSLionel Sambuc  object may have been pool-allocated and should not be deallocated with
1627*f4a2713aSLionel Sambuc  ``free``; for now, this can only be supported with a ``dealloc``
1628*f4a2713aSLionel Sambuc  implementation outside of ARC.  Such an implementation must be very careful
1629*f4a2713aSLionel Sambuc  to do all the other work that ``NSObject``'s ``dealloc`` would, which is
1630*f4a2713aSLionel Sambuc  outside the scope of this document to describe.
1631*f4a2713aSLionel Sambuc
1632*f4a2713aSLionel SambucThe instance variables for an ARC-compiled class will be destroyed at some
1633*f4a2713aSLionel Sambucpoint after control enters the ``dealloc`` method for the root class of the
1634*f4a2713aSLionel Sambucclass.  The ordering of the destruction of instance variables is unspecified,
1635*f4a2713aSLionel Sambucboth within a single class and between subclasses and superclasses.
1636*f4a2713aSLionel Sambuc
1637*f4a2713aSLionel Sambuc.. admonition:: Rationale
1638*f4a2713aSLionel Sambuc
1639*f4a2713aSLionel Sambuc  The traditional, non-ARC pattern for destroying instance variables is to
1640*f4a2713aSLionel Sambuc  destroy them immediately before calling ``[super dealloc]``.  Unfortunately,
1641*f4a2713aSLionel Sambuc  message sends from the superclass are quite capable of reaching methods in
1642*f4a2713aSLionel Sambuc  the subclass, and those methods may well read or write to those instance
1643*f4a2713aSLionel Sambuc  variables.  Making such message sends from dealloc is generally discouraged,
1644*f4a2713aSLionel Sambuc  since the subclass may well rely on other invariants that were broken during
1645*f4a2713aSLionel Sambuc  ``dealloc``, but it's not so inescapably dangerous that we felt comfortable
1646*f4a2713aSLionel Sambuc  calling it undefined behavior.  Therefore we chose to delay destroying the
1647*f4a2713aSLionel Sambuc  instance variables to a point at which message sends are clearly disallowed:
1648*f4a2713aSLionel Sambuc  the point at which the root class's deallocation routines take over.
1649*f4a2713aSLionel Sambuc
1650*f4a2713aSLionel Sambuc  In most code, the difference is not observable.  It can, however, be observed
1651*f4a2713aSLionel Sambuc  if an instance variable holds a strong reference to an object whose
1652*f4a2713aSLionel Sambuc  deallocation will trigger a side-effect which must be carefully ordered with
1653*f4a2713aSLionel Sambuc  respect to the destruction of the super class.  Such code violates the design
1654*f4a2713aSLionel Sambuc  principle that semantically important behavior should be explicit.  A simple
1655*f4a2713aSLionel Sambuc  fix is to clear the instance variable manually during ``dealloc``; a more
1656*f4a2713aSLionel Sambuc  holistic solution is to move semantically important side-effects out of
1657*f4a2713aSLionel Sambuc  ``dealloc`` and into a separate teardown phase which can rely on working with
1658*f4a2713aSLionel Sambuc  well-formed objects.
1659*f4a2713aSLionel Sambuc
1660*f4a2713aSLionel Sambuc.. _arc.misc.autoreleasepool:
1661*f4a2713aSLionel Sambuc
1662*f4a2713aSLionel Sambuc``@autoreleasepool``
1663*f4a2713aSLionel Sambuc--------------------
1664*f4a2713aSLionel Sambuc
1665*f4a2713aSLionel SambucTo simplify the use of autorelease pools, and to bring them under the control
1666*f4a2713aSLionel Sambucof the compiler, a new kind of statement is available in Objective-C.  It is
1667*f4a2713aSLionel Sambucwritten ``@autoreleasepool`` followed by a *compound-statement*, i.e.  by a new
1668*f4a2713aSLionel Sambucscope delimited by curly braces.  Upon entry to this block, the current state
1669*f4a2713aSLionel Sambucof the autorelease pool is captured.  When the block is exited normally,
1670*f4a2713aSLionel Sambucwhether by fallthrough or directed control flow (such as ``return`` or
1671*f4a2713aSLionel Sambuc``break``), the autorelease pool is restored to the saved state, releasing all
1672*f4a2713aSLionel Sambucthe objects in it.  When the block is exited with an exception, the pool is not
1673*f4a2713aSLionel Sambucdrained.
1674*f4a2713aSLionel Sambuc
1675*f4a2713aSLionel Sambuc``@autoreleasepool`` may be used in non-ARC translation units, with equivalent
1676*f4a2713aSLionel Sambucsemantics.
1677*f4a2713aSLionel Sambuc
1678*f4a2713aSLionel SambucA program is ill-formed if it refers to the ``NSAutoreleasePool`` class.
1679*f4a2713aSLionel Sambuc
1680*f4a2713aSLionel Sambuc.. admonition:: Rationale
1681*f4a2713aSLionel Sambuc
1682*f4a2713aSLionel Sambuc  Autorelease pools are clearly important for the compiler to reason about, but
1683*f4a2713aSLionel Sambuc  it is far too much to expect the compiler to accurately reason about control
1684*f4a2713aSLionel Sambuc  dependencies between two calls.  It is also very easy to accidentally forget
1685*f4a2713aSLionel Sambuc  to drain an autorelease pool when using the manual API, and this can
1686*f4a2713aSLionel Sambuc  significantly inflate the process's high-water-mark.  The introduction of a
1687*f4a2713aSLionel Sambuc  new scope is unfortunate but basically required for sane interaction with the
1688*f4a2713aSLionel Sambuc  rest of the language.  Not draining the pool during an unwind is apparently
1689*f4a2713aSLionel Sambuc  required by the Objective-C exceptions implementation.
1690*f4a2713aSLionel Sambuc
1691*f4a2713aSLionel Sambuc.. _arc.misc.self:
1692*f4a2713aSLionel Sambuc
1693*f4a2713aSLionel Sambuc``self``
1694*f4a2713aSLionel Sambuc--------
1695*f4a2713aSLionel Sambuc
1696*f4a2713aSLionel SambucThe ``self`` parameter variable of an Objective-C method is never actually
1697*f4a2713aSLionel Sambucretained by the implementation.  It is undefined behavior, or at least
1698*f4a2713aSLionel Sambucdangerous, to cause an object to be deallocated during a message send to that
1699*f4a2713aSLionel Sambucobject.
1700*f4a2713aSLionel Sambuc
1701*f4a2713aSLionel SambucTo make this safe, for Objective-C instance methods ``self`` is implicitly
1702*f4a2713aSLionel Sambuc``const`` unless the method is in the :ref:`init family
1703*f4a2713aSLionel Sambuc<arc.family.semantics.init>`.  Further, ``self`` is **always** implicitly
1704*f4a2713aSLionel Sambuc``const`` within a class method.
1705*f4a2713aSLionel Sambuc
1706*f4a2713aSLionel Sambuc.. admonition:: Rationale
1707*f4a2713aSLionel Sambuc
1708*f4a2713aSLionel Sambuc  The cost of retaining ``self`` in all methods was found to be prohibitive, as
1709*f4a2713aSLionel Sambuc  it tends to be live across calls, preventing the optimizer from proving that
1710*f4a2713aSLionel Sambuc  the retain and release are unnecessary --- for good reason, as it's quite
1711*f4a2713aSLionel Sambuc  possible in theory to cause an object to be deallocated during its execution
1712*f4a2713aSLionel Sambuc  without this retain and release.  Since it's extremely uncommon to actually
1713*f4a2713aSLionel Sambuc  do so, even unintentionally, and since there's no natural way for the
1714*f4a2713aSLionel Sambuc  programmer to remove this retain/release pair otherwise (as there is for
1715*f4a2713aSLionel Sambuc  other parameters by, say, making the variable ``__unsafe_unretained``), we
1716*f4a2713aSLionel Sambuc  chose to make this optimizing assumption and shift some amount of risk to the
1717*f4a2713aSLionel Sambuc  user.
1718*f4a2713aSLionel Sambuc
1719*f4a2713aSLionel Sambuc.. _arc.misc.enumeration:
1720*f4a2713aSLionel Sambuc
1721*f4a2713aSLionel SambucFast enumeration iteration variables
1722*f4a2713aSLionel Sambuc------------------------------------
1723*f4a2713aSLionel Sambuc
1724*f4a2713aSLionel SambucIf a variable is declared in the condition of an Objective-C fast enumeration
1725*f4a2713aSLionel Sambucloop, and the variable has no explicit ownership qualifier, then it is
1726*f4a2713aSLionel Sambucqualified with ``const __strong`` and objects encountered during the
1727*f4a2713aSLionel Sambucenumeration are not actually retained.
1728*f4a2713aSLionel Sambuc
1729*f4a2713aSLionel Sambuc.. admonition:: Rationale
1730*f4a2713aSLionel Sambuc
1731*f4a2713aSLionel Sambuc  This is an optimization made possible because fast enumeration loops promise
1732*f4a2713aSLionel Sambuc  to keep the objects retained during enumeration, and the collection itself
1733*f4a2713aSLionel Sambuc  cannot be synchronously modified.  It can be overridden by explicitly
1734*f4a2713aSLionel Sambuc  qualifying the variable with ``__strong``, which will make the variable
1735*f4a2713aSLionel Sambuc  mutable again and cause the loop to retain the objects it encounters.
1736*f4a2713aSLionel Sambuc
1737*f4a2713aSLionel Sambuc.. _arc.misc.blocks:
1738*f4a2713aSLionel Sambuc
1739*f4a2713aSLionel SambucBlocks
1740*f4a2713aSLionel Sambuc------
1741*f4a2713aSLionel Sambuc
1742*f4a2713aSLionel SambucThe implicit ``const`` capture variables created when evaluating a block
1743*f4a2713aSLionel Sambucliteral expression have the same ownership semantics as the local variables
1744*f4a2713aSLionel Sambucthey capture.  The capture is performed by reading from the captured variable
1745*f4a2713aSLionel Sambucand initializing the capture variable with that value; the capture variable is
1746*f4a2713aSLionel Sambucdestroyed when the block literal is, i.e. at the end of the enclosing scope.
1747*f4a2713aSLionel Sambuc
1748*f4a2713aSLionel SambucThe :ref:`inference <arc.ownership.inference>` rules apply equally to
1749*f4a2713aSLionel Sambuc``__block`` variables, which is a shift in semantics from non-ARC, where
1750*f4a2713aSLionel Sambuc``__block`` variables did not implicitly retain during capture.
1751*f4a2713aSLionel Sambuc
1752*f4a2713aSLionel Sambuc``__block`` variables of retainable object owner type are moved off the stack
1753*f4a2713aSLionel Sambucby initializing the heap copy with the result of moving from the stack copy.
1754*f4a2713aSLionel Sambuc
1755*f4a2713aSLionel SambucWith the exception of retains done as part of initializing a ``__strong``
1756*f4a2713aSLionel Sambucparameter variable or reading a ``__weak`` variable, whenever these semantics
1757*f4a2713aSLionel Sambuccall for retaining a value of block-pointer type, it has the effect of a
1758*f4a2713aSLionel Sambuc``Block_copy``.  The optimizer may remove such copies when it sees that the
1759*f4a2713aSLionel Sambucresult is used only as an argument to a call.
1760*f4a2713aSLionel Sambuc
1761*f4a2713aSLionel Sambuc.. _arc.misc.exceptions:
1762*f4a2713aSLionel Sambuc
1763*f4a2713aSLionel SambucExceptions
1764*f4a2713aSLionel Sambuc----------
1765*f4a2713aSLionel Sambuc
1766*f4a2713aSLionel SambucBy default in Objective C, ARC is not exception-safe for normal releases:
1767*f4a2713aSLionel Sambuc
1768*f4a2713aSLionel Sambuc* It does not end the lifetime of ``__strong`` variables when their scopes are
1769*f4a2713aSLionel Sambuc  abnormally terminated by an exception.
1770*f4a2713aSLionel Sambuc* It does not perform releases which would occur at the end of a
1771*f4a2713aSLionel Sambuc  full-expression if that full-expression throws an exception.
1772*f4a2713aSLionel Sambuc
1773*f4a2713aSLionel SambucA program may be compiled with the option ``-fobjc-arc-exceptions`` in order to
1774*f4a2713aSLionel Sambucenable these, or with the option ``-fno-objc-arc-exceptions`` to explicitly
1775*f4a2713aSLionel Sambucdisable them, with the last such argument "winning".
1776*f4a2713aSLionel Sambuc
1777*f4a2713aSLionel Sambuc.. admonition:: Rationale
1778*f4a2713aSLionel Sambuc
1779*f4a2713aSLionel Sambuc  The standard Cocoa convention is that exceptions signal programmer error and
1780*f4a2713aSLionel Sambuc  are not intended to be recovered from.  Making code exceptions-safe by
1781*f4a2713aSLionel Sambuc  default would impose severe runtime and code size penalties on code that
1782*f4a2713aSLionel Sambuc  typically does not actually care about exceptions safety.  Therefore,
1783*f4a2713aSLionel Sambuc  ARC-generated code leaks by default on exceptions, which is just fine if the
1784*f4a2713aSLionel Sambuc  process is going to be immediately terminated anyway.  Programs which do care
1785*f4a2713aSLionel Sambuc  about recovering from exceptions should enable the option.
1786*f4a2713aSLionel Sambuc
1787*f4a2713aSLionel SambucIn Objective-C++, ``-fobjc-arc-exceptions`` is enabled by default.
1788*f4a2713aSLionel Sambuc
1789*f4a2713aSLionel Sambuc.. admonition:: Rationale
1790*f4a2713aSLionel Sambuc
1791*f4a2713aSLionel Sambuc  C++ already introduces pervasive exceptions-cleanup code of the sort that ARC
1792*f4a2713aSLionel Sambuc  introduces.  C++ programmers who have not already disabled exceptions are
1793*f4a2713aSLionel Sambuc  much more likely to actual require exception-safety.
1794*f4a2713aSLionel Sambuc
1795*f4a2713aSLionel SambucARC does end the lifetimes of ``__weak`` objects when an exception terminates
1796*f4a2713aSLionel Sambuctheir scope unless exceptions are disabled in the compiler.
1797*f4a2713aSLionel Sambuc
1798*f4a2713aSLionel Sambuc.. admonition:: Rationale
1799*f4a2713aSLionel Sambuc
1800*f4a2713aSLionel Sambuc  The consequence of a local ``__weak`` object not being destroyed is very
1801*f4a2713aSLionel Sambuc  likely to be corruption of the Objective-C runtime, so we want to be safer
1802*f4a2713aSLionel Sambuc  here.  Of course, potentially massive leaks are about as likely to take down
1803*f4a2713aSLionel Sambuc  the process as this corruption is if the program does try to recover from
1804*f4a2713aSLionel Sambuc  exceptions.
1805*f4a2713aSLionel Sambuc
1806*f4a2713aSLionel Sambuc.. _arc.misc.interior:
1807*f4a2713aSLionel Sambuc
1808*f4a2713aSLionel SambucInterior pointers
1809*f4a2713aSLionel Sambuc-----------------
1810*f4a2713aSLionel Sambuc
1811*f4a2713aSLionel SambucAn Objective-C method returning a non-retainable pointer may be annotated with
1812*f4a2713aSLionel Sambucthe ``objc_returns_inner_pointer`` attribute to indicate that it returns a
1813*f4a2713aSLionel Sambuchandle to the internal data of an object, and that this reference will be
1814*f4a2713aSLionel Sambucinvalidated if the object is destroyed.  When such a message is sent to an
1815*f4a2713aSLionel Sambucobject, the object's lifetime will be extended until at least the earliest of:
1816*f4a2713aSLionel Sambuc
1817*f4a2713aSLionel Sambuc* the last use of the returned pointer, or any pointer derived from it, in the
1818*f4a2713aSLionel Sambuc  calling function or
1819*f4a2713aSLionel Sambuc* the autorelease pool is restored to a previous state.
1820*f4a2713aSLionel Sambuc
1821*f4a2713aSLionel Sambuc.. admonition:: Rationale
1822*f4a2713aSLionel Sambuc
1823*f4a2713aSLionel Sambuc  Rationale: not all memory and resources are managed with reference counts; it
1824*f4a2713aSLionel Sambuc  is common for objects to manage private resources in their own, private way.
1825*f4a2713aSLionel Sambuc  Typically these resources are completely encapsulated within the object, but
1826*f4a2713aSLionel Sambuc  some classes offer their users direct access for efficiency.  If ARC is not
1827*f4a2713aSLionel Sambuc  aware of methods that return such "interior" pointers, its optimizations can
1828*f4a2713aSLionel Sambuc  cause the owning object to be reclaimed too soon.  This attribute informs ARC
1829*f4a2713aSLionel Sambuc  that it must tread lightly.
1830*f4a2713aSLionel Sambuc
1831*f4a2713aSLionel Sambuc  The extension rules are somewhat intentionally vague.  The autorelease pool
1832*f4a2713aSLionel Sambuc  limit is there to permit a simple implementation to simply retain and
1833*f4a2713aSLionel Sambuc  autorelease the receiver.  The other limit permits some amount of
1834*f4a2713aSLionel Sambuc  optimization.  The phrase "derived from" is intended to encompass the results
1835*f4a2713aSLionel Sambuc  both of pointer transformations, such as casts and arithmetic, and of loading
1836*f4a2713aSLionel Sambuc  from such derived pointers; furthermore, it applies whether or not such
1837*f4a2713aSLionel Sambuc  derivations are applied directly in the calling code or by other utility code
1838*f4a2713aSLionel Sambuc  (for example, the C library routine ``strchr``).  However, the implementation
1839*f4a2713aSLionel Sambuc  never need account for uses after a return from the code which calls the
1840*f4a2713aSLionel Sambuc  method returning an interior pointer.
1841*f4a2713aSLionel Sambuc
1842*f4a2713aSLionel SambucAs an exception, no extension is required if the receiver is loaded directly
1843*f4a2713aSLionel Sambucfrom a ``__strong`` object with :ref:`precise lifetime semantics
1844*f4a2713aSLionel Sambuc<arc.optimization.precise>`.
1845*f4a2713aSLionel Sambuc
1846*f4a2713aSLionel Sambuc.. admonition:: Rationale
1847*f4a2713aSLionel Sambuc
1848*f4a2713aSLionel Sambuc  Implicit autoreleases carry the risk of significantly inflating memory use,
1849*f4a2713aSLionel Sambuc  so it's important to provide users a way of avoiding these autoreleases.
1850*f4a2713aSLionel Sambuc  Tying this to precise lifetime semantics is ideal, as for local variables
1851*f4a2713aSLionel Sambuc  this requires a very explicit annotation, which allows ARC to trust the user
1852*f4a2713aSLionel Sambuc  with good cheer.
1853*f4a2713aSLionel Sambuc
1854*f4a2713aSLionel Sambuc.. _arc.misc.c-retainable:
1855*f4a2713aSLionel Sambuc
1856*f4a2713aSLionel SambucC retainable pointer types
1857*f4a2713aSLionel Sambuc--------------------------
1858*f4a2713aSLionel Sambuc
1859*f4a2713aSLionel SambucA type is a :arc-term:`C retainable pointer type` if it is a pointer to
1860*f4a2713aSLionel Sambuc(possibly qualified) ``void`` or a pointer to a (possibly qualifier) ``struct``
1861*f4a2713aSLionel Sambucor ``class`` type.
1862*f4a2713aSLionel Sambuc
1863*f4a2713aSLionel Sambuc.. admonition:: Rationale
1864*f4a2713aSLionel Sambuc
1865*f4a2713aSLionel Sambuc  ARC does not manage pointers of CoreFoundation type (or any of the related
1866*f4a2713aSLionel Sambuc  families of retainable C pointers which interoperate with Objective-C for
1867*f4a2713aSLionel Sambuc  retain/release operation).  In fact, ARC does not even know how to
1868*f4a2713aSLionel Sambuc  distinguish these types from arbitrary C pointer types.  The intent of this
1869*f4a2713aSLionel Sambuc  concept is to filter out some obviously non-object types while leaving a hook
1870*f4a2713aSLionel Sambuc  for later tightening if a means of exhaustively marking CF types is made
1871*f4a2713aSLionel Sambuc  available.
1872*f4a2713aSLionel Sambuc
1873*f4a2713aSLionel Sambuc.. _arc.misc.c-retainable.audit:
1874*f4a2713aSLionel Sambuc
1875*f4a2713aSLionel SambucAuditing of C retainable pointer interfaces
1876*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1877*f4a2713aSLionel Sambuc
1878*f4a2713aSLionel Sambuc:when-revised:`[beginning Apple 4.0, LLVM 3.1]`
1879*f4a2713aSLionel Sambuc
1880*f4a2713aSLionel SambucA C function may be marked with the ``cf_audited_transfer`` attribute to
1881*f4a2713aSLionel Sambucexpress that, except as otherwise marked with attributes, it obeys the
1882*f4a2713aSLionel Sambucparameter (consuming vs. non-consuming) and return (retained vs. non-retained)
1883*f4a2713aSLionel Sambucconventions for a C function of its name, namely:
1884*f4a2713aSLionel Sambuc
1885*f4a2713aSLionel Sambuc* A parameter of C retainable pointer type is assumed to not be consumed
1886*f4a2713aSLionel Sambuc  unless it is marked with the ``cf_consumed`` attribute, and
1887*f4a2713aSLionel Sambuc* A result of C retainable pointer type is assumed to not be returned retained
1888*f4a2713aSLionel Sambuc  unless the function is either marked ``cf_returns_retained`` or it follows
1889*f4a2713aSLionel Sambuc  the create/copy naming convention and is not marked
1890*f4a2713aSLionel Sambuc  ``cf_returns_not_retained``.
1891*f4a2713aSLionel Sambuc
1892*f4a2713aSLionel SambucA function obeys the :arc-term:`create/copy` naming convention if its name
1893*f4a2713aSLionel Sambuccontains as a substring:
1894*f4a2713aSLionel Sambuc
1895*f4a2713aSLionel Sambuc* either "Create" or "Copy" not followed by a lowercase letter, or
1896*f4a2713aSLionel Sambuc* either "create" or "copy" not followed by a lowercase letter and
1897*f4a2713aSLionel Sambuc  not preceded by any letter, whether uppercase or lowercase.
1898*f4a2713aSLionel Sambuc
1899*f4a2713aSLionel SambucA second attribute, ``cf_unknown_transfer``, signifies that a function's
1900*f4a2713aSLionel Sambuctransfer semantics cannot be accurately captured using any of these
1901*f4a2713aSLionel Sambucannotations.  A program is ill-formed if it annotates the same function with
1902*f4a2713aSLionel Sambucboth ``cf_audited_transfer`` and ``cf_unknown_transfer``.
1903*f4a2713aSLionel Sambuc
1904*f4a2713aSLionel SambucA pragma is provided to facilitate the mass annotation of interfaces:
1905*f4a2713aSLionel Sambuc
1906*f4a2713aSLionel Sambuc.. code-block:: objc
1907*f4a2713aSLionel Sambuc
1908*f4a2713aSLionel Sambuc  #pragma clang arc_cf_code_audited begin
1909*f4a2713aSLionel Sambuc  ...
1910*f4a2713aSLionel Sambuc  #pragma clang arc_cf_code_audited end
1911*f4a2713aSLionel Sambuc
1912*f4a2713aSLionel SambucAll C functions declared within the extent of this pragma are treated as if
1913*f4a2713aSLionel Sambucannotated with the ``cf_audited_transfer`` attribute unless they otherwise have
1914*f4a2713aSLionel Sambucthe ``cf_unknown_transfer`` attribute.  The pragma is accepted in all language
1915*f4a2713aSLionel Sambucmodes.  A program is ill-formed if it attempts to change files, whether by
1916*f4a2713aSLionel Sambucincluding a file or ending the current file, within the extent of this pragma.
1917*f4a2713aSLionel Sambuc
1918*f4a2713aSLionel SambucIt is possible to test for all the features in this section with
1919*f4a2713aSLionel Sambuc``__has_feature(arc_cf_code_audited)``.
1920*f4a2713aSLionel Sambuc
1921*f4a2713aSLionel Sambuc.. admonition:: Rationale
1922*f4a2713aSLionel Sambuc
1923*f4a2713aSLionel Sambuc  A significant inconvenience in ARC programming is the necessity of
1924*f4a2713aSLionel Sambuc  interacting with APIs based around C retainable pointers.  These features are
1925*f4a2713aSLionel Sambuc  designed to make it relatively easy for API authors to quickly review and
1926*f4a2713aSLionel Sambuc  annotate their interfaces, in turn improving the fidelity of tools such as
1927*f4a2713aSLionel Sambuc  the static analyzer and ARC.  The single-file restriction on the pragma is
1928*f4a2713aSLionel Sambuc  designed to eliminate the risk of accidentally annotating some other header's
1929*f4a2713aSLionel Sambuc  interfaces.
1930*f4a2713aSLionel Sambuc
1931*f4a2713aSLionel Sambuc.. _arc.runtime:
1932*f4a2713aSLionel Sambuc
1933*f4a2713aSLionel SambucRuntime support
1934*f4a2713aSLionel Sambuc===============
1935*f4a2713aSLionel Sambuc
1936*f4a2713aSLionel SambucThis section describes the interaction between the ARC runtime and the code
1937*f4a2713aSLionel Sambucgenerated by the ARC compiler.  This is not part of the ARC language
1938*f4a2713aSLionel Sambucspecification; instead, it is effectively a language-specific ABI supplement,
1939*f4a2713aSLionel Sambucakin to the "Itanium" generic ABI for C++.
1940*f4a2713aSLionel Sambuc
1941*f4a2713aSLionel SambucOwnership qualification does not alter the storage requirements for objects,
1942*f4a2713aSLionel Sambucexcept that it is undefined behavior if a ``__weak`` object is inadequately
1943*f4a2713aSLionel Sambucaligned for an object of type ``id``.  The other qualifiers may be used on
1944*f4a2713aSLionel Sambucexplicitly under-aligned memory.
1945*f4a2713aSLionel Sambuc
1946*f4a2713aSLionel SambucThe runtime tracks ``__weak`` objects which holds non-null values.  It is
1947*f4a2713aSLionel Sambucundefined behavior to direct modify a ``__weak`` object which is being tracked
1948*f4a2713aSLionel Sambucby the runtime except through an
1949*f4a2713aSLionel Sambuc:ref:`objc_storeWeak <arc.runtime.objc_storeWeak>`,
1950*f4a2713aSLionel Sambuc:ref:`objc_destroyWeak <arc.runtime.objc_destroyWeak>`, or
1951*f4a2713aSLionel Sambuc:ref:`objc_moveWeak <arc.runtime.objc_moveWeak>` call.
1952*f4a2713aSLionel Sambuc
1953*f4a2713aSLionel SambucThe runtime must provide a number of new entrypoints which the compiler may
1954*f4a2713aSLionel Sambucemit, which are described in the remainder of this section.
1955*f4a2713aSLionel Sambuc
1956*f4a2713aSLionel Sambuc.. admonition:: Rationale
1957*f4a2713aSLionel Sambuc
1958*f4a2713aSLionel Sambuc  Several of these functions are semantically equivalent to a message send; we
1959*f4a2713aSLionel Sambuc  emit calls to C functions instead because:
1960*f4a2713aSLionel Sambuc
1961*f4a2713aSLionel Sambuc  * the machine code to do so is significantly smaller,
1962*f4a2713aSLionel Sambuc  * it is much easier to recognize the C functions in the ARC optimizer, and
1963*f4a2713aSLionel Sambuc  * a sufficient sophisticated runtime may be able to avoid the message send in
1964*f4a2713aSLionel Sambuc    common cases.
1965*f4a2713aSLionel Sambuc
1966*f4a2713aSLionel Sambuc  Several other of these functions are "fused" operations which can be
1967*f4a2713aSLionel Sambuc  described entirely in terms of other operations.  We use the fused operations
1968*f4a2713aSLionel Sambuc  primarily as a code-size optimization, although in some cases there is also a
1969*f4a2713aSLionel Sambuc  real potential for avoiding redundant operations in the runtime.
1970*f4a2713aSLionel Sambuc
1971*f4a2713aSLionel Sambuc.. _arc.runtime.objc_autorelease:
1972*f4a2713aSLionel Sambuc
1973*f4a2713aSLionel Sambuc``id objc_autorelease(id value);``
1974*f4a2713aSLionel Sambuc----------------------------------
1975*f4a2713aSLionel Sambuc
1976*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
1977*f4a2713aSLionel Sambuc
1978*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it adds the object
1979*f4a2713aSLionel Sambucto the innermost autorelease pool exactly as if the object had been sent the
1980*f4a2713aSLionel Sambuc``autorelease`` message.
1981*f4a2713aSLionel Sambuc
1982*f4a2713aSLionel SambucAlways returns ``value``.
1983*f4a2713aSLionel Sambuc
1984*f4a2713aSLionel Sambuc.. _arc.runtime.objc_autoreleasePoolPop:
1985*f4a2713aSLionel Sambuc
1986*f4a2713aSLionel Sambuc``void objc_autoreleasePoolPop(void *pool);``
1987*f4a2713aSLionel Sambuc---------------------------------------------
1988*f4a2713aSLionel Sambuc
1989*f4a2713aSLionel Sambuc*Precondition:* ``pool`` is the result of a previous call to
1990*f4a2713aSLionel Sambuc:ref:`objc_autoreleasePoolPush <arc.runtime.objc_autoreleasePoolPush>` on the
1991*f4a2713aSLionel Sambuccurrent thread, where neither ``pool`` nor any enclosing pool have previously
1992*f4a2713aSLionel Sambucbeen popped.
1993*f4a2713aSLionel Sambuc
1994*f4a2713aSLionel SambucReleases all the objects added to the given autorelease pool and any
1995*f4a2713aSLionel Sambucautorelease pools it encloses, then sets the current autorelease pool to the
1996*f4a2713aSLionel Sambucpool directly enclosing ``pool``.
1997*f4a2713aSLionel Sambuc
1998*f4a2713aSLionel Sambuc.. _arc.runtime.objc_autoreleasePoolPush:
1999*f4a2713aSLionel Sambuc
2000*f4a2713aSLionel Sambuc``void *objc_autoreleasePoolPush(void);``
2001*f4a2713aSLionel Sambuc-----------------------------------------
2002*f4a2713aSLionel Sambuc
2003*f4a2713aSLionel SambucCreates a new autorelease pool that is enclosed by the current pool, makes that
2004*f4a2713aSLionel Sambucthe current pool, and returns an opaque "handle" to it.
2005*f4a2713aSLionel Sambuc
2006*f4a2713aSLionel Sambuc.. admonition:: Rationale
2007*f4a2713aSLionel Sambuc
2008*f4a2713aSLionel Sambuc  While the interface is described as an explicit hierarchy of pools, the rules
2009*f4a2713aSLionel Sambuc  allow the implementation to just keep a stack of objects, using the stack
2010*f4a2713aSLionel Sambuc  depth as the opaque pool handle.
2011*f4a2713aSLionel Sambuc
2012*f4a2713aSLionel Sambuc.. _arc.runtime.objc_autoreleaseReturnValue:
2013*f4a2713aSLionel Sambuc
2014*f4a2713aSLionel Sambuc``id objc_autoreleaseReturnValue(id value);``
2015*f4a2713aSLionel Sambuc---------------------------------------------
2016*f4a2713aSLionel Sambuc
2017*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2018*f4a2713aSLionel Sambuc
2019*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it makes a best
2020*f4a2713aSLionel Sambuceffort to hand off ownership of a retain count on the object to a call to
2021*f4a2713aSLionel Sambuc:ref:`objc_retainAutoreleasedReturnValue
2022*f4a2713aSLionel Sambuc<arc.runtime.objc_retainAutoreleasedReturnValue>` for the same object in an
2023*f4a2713aSLionel Sambucenclosing call frame.  If this is not possible, the object is autoreleased as
2024*f4a2713aSLionel Sambucabove.
2025*f4a2713aSLionel Sambuc
2026*f4a2713aSLionel SambucAlways returns ``value``.
2027*f4a2713aSLionel Sambuc
2028*f4a2713aSLionel Sambuc.. _arc.runtime.objc_copyWeak:
2029*f4a2713aSLionel Sambuc
2030*f4a2713aSLionel Sambuc``void objc_copyWeak(id *dest, id *src);``
2031*f4a2713aSLionel Sambuc------------------------------------------
2032*f4a2713aSLionel Sambuc
2033*f4a2713aSLionel Sambuc*Precondition:* ``src`` is a valid pointer which either contains a null pointer
2034*f4a2713aSLionel Sambucor has been registered as a ``__weak`` object.  ``dest`` is a valid pointer
2035*f4a2713aSLionel Sambucwhich has not been registered as a ``__weak`` object.
2036*f4a2713aSLionel Sambuc
2037*f4a2713aSLionel Sambuc``dest`` is initialized to be equivalent to ``src``, potentially registering it
2038*f4a2713aSLionel Sambucwith the runtime.  Equivalent to the following code:
2039*f4a2713aSLionel Sambuc
2040*f4a2713aSLionel Sambuc.. code-block:: objc
2041*f4a2713aSLionel Sambuc
2042*f4a2713aSLionel Sambuc  void objc_copyWeak(id *dest, id *src) {
2043*f4a2713aSLionel Sambuc    objc_release(objc_initWeak(dest, objc_loadWeakRetained(src)));
2044*f4a2713aSLionel Sambuc  }
2045*f4a2713aSLionel Sambuc
2046*f4a2713aSLionel SambucMust be atomic with respect to calls to ``objc_storeWeak`` on ``src``.
2047*f4a2713aSLionel Sambuc
2048*f4a2713aSLionel Sambuc.. _arc.runtime.objc_destroyWeak:
2049*f4a2713aSLionel Sambuc
2050*f4a2713aSLionel Sambuc``void objc_destroyWeak(id *object);``
2051*f4a2713aSLionel Sambuc--------------------------------------
2052*f4a2713aSLionel Sambuc
2053*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer which either contains a null
2054*f4a2713aSLionel Sambucpointer or has been registered as a ``__weak`` object.
2055*f4a2713aSLionel Sambuc
2056*f4a2713aSLionel Sambuc``object`` is unregistered as a weak object, if it ever was.  The current value
2057*f4a2713aSLionel Sambucof ``object`` is left unspecified; otherwise, equivalent to the following code:
2058*f4a2713aSLionel Sambuc
2059*f4a2713aSLionel Sambuc.. code-block:: objc
2060*f4a2713aSLionel Sambuc
2061*f4a2713aSLionel Sambuc  void objc_destroyWeak(id *object) {
2062*f4a2713aSLionel Sambuc    objc_storeWeak(object, nil);
2063*f4a2713aSLionel Sambuc  }
2064*f4a2713aSLionel Sambuc
2065*f4a2713aSLionel SambucDoes not need to be atomic with respect to calls to ``objc_storeWeak`` on
2066*f4a2713aSLionel Sambuc``object``.
2067*f4a2713aSLionel Sambuc
2068*f4a2713aSLionel Sambuc.. _arc.runtime.objc_initWeak:
2069*f4a2713aSLionel Sambuc
2070*f4a2713aSLionel Sambuc``id objc_initWeak(id *object, id value);``
2071*f4a2713aSLionel Sambuc-------------------------------------------
2072*f4a2713aSLionel Sambuc
2073*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer which has not been registered as
2074*f4a2713aSLionel Sambuca ``__weak`` object.  ``value`` is null or a pointer to a valid object.
2075*f4a2713aSLionel Sambuc
2076*f4a2713aSLionel SambucIf ``value`` is a null pointer or the object to which it points has begun
2077*f4a2713aSLionel Sambucdeallocation, ``object`` is zero-initialized.  Otherwise, ``object`` is
2078*f4a2713aSLionel Sambucregistered as a ``__weak`` object pointing to ``value``.  Equivalent to the
2079*f4a2713aSLionel Sambucfollowing code:
2080*f4a2713aSLionel Sambuc
2081*f4a2713aSLionel Sambuc.. code-block:: objc
2082*f4a2713aSLionel Sambuc
2083*f4a2713aSLionel Sambuc  id objc_initWeak(id *object, id value) {
2084*f4a2713aSLionel Sambuc    *object = nil;
2085*f4a2713aSLionel Sambuc    return objc_storeWeak(object, value);
2086*f4a2713aSLionel Sambuc  }
2087*f4a2713aSLionel Sambuc
2088*f4a2713aSLionel SambucReturns the value of ``object`` after the call.
2089*f4a2713aSLionel Sambuc
2090*f4a2713aSLionel SambucDoes not need to be atomic with respect to calls to ``objc_storeWeak`` on
2091*f4a2713aSLionel Sambuc``object``.
2092*f4a2713aSLionel Sambuc
2093*f4a2713aSLionel Sambuc.. _arc.runtime.objc_loadWeak:
2094*f4a2713aSLionel Sambuc
2095*f4a2713aSLionel Sambuc``id objc_loadWeak(id *object);``
2096*f4a2713aSLionel Sambuc---------------------------------
2097*f4a2713aSLionel Sambuc
2098*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer which either contains a null
2099*f4a2713aSLionel Sambucpointer or has been registered as a ``__weak`` object.
2100*f4a2713aSLionel Sambuc
2101*f4a2713aSLionel SambucIf ``object`` is registered as a ``__weak`` object, and the last value stored
2102*f4a2713aSLionel Sambucinto ``object`` has not yet been deallocated or begun deallocation, retains and
2103*f4a2713aSLionel Sambucautoreleases that value and returns it.  Otherwise returns null.  Equivalent to
2104*f4a2713aSLionel Sambucthe following code:
2105*f4a2713aSLionel Sambuc
2106*f4a2713aSLionel Sambuc.. code-block:: objc
2107*f4a2713aSLionel Sambuc
2108*f4a2713aSLionel Sambuc  id objc_loadWeak(id *object) {
2109*f4a2713aSLionel Sambuc    return objc_autorelease(objc_loadWeakRetained(object));
2110*f4a2713aSLionel Sambuc  }
2111*f4a2713aSLionel Sambuc
2112*f4a2713aSLionel SambucMust be atomic with respect to calls to ``objc_storeWeak`` on ``object``.
2113*f4a2713aSLionel Sambuc
2114*f4a2713aSLionel Sambuc.. admonition:: Rationale
2115*f4a2713aSLionel Sambuc
2116*f4a2713aSLionel Sambuc  Loading weak references would be inherently prone to race conditions without
2117*f4a2713aSLionel Sambuc  the retain.
2118*f4a2713aSLionel Sambuc
2119*f4a2713aSLionel Sambuc.. _arc.runtime.objc_loadWeakRetained:
2120*f4a2713aSLionel Sambuc
2121*f4a2713aSLionel Sambuc``id objc_loadWeakRetained(id *object);``
2122*f4a2713aSLionel Sambuc-----------------------------------------
2123*f4a2713aSLionel Sambuc
2124*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer which either contains a null
2125*f4a2713aSLionel Sambucpointer or has been registered as a ``__weak`` object.
2126*f4a2713aSLionel Sambuc
2127*f4a2713aSLionel SambucIf ``object`` is registered as a ``__weak`` object, and the last value stored
2128*f4a2713aSLionel Sambucinto ``object`` has not yet been deallocated or begun deallocation, retains
2129*f4a2713aSLionel Sambucthat value and returns it.  Otherwise returns null.
2130*f4a2713aSLionel Sambuc
2131*f4a2713aSLionel SambucMust be atomic with respect to calls to ``objc_storeWeak`` on ``object``.
2132*f4a2713aSLionel Sambuc
2133*f4a2713aSLionel Sambuc.. _arc.runtime.objc_moveWeak:
2134*f4a2713aSLionel Sambuc
2135*f4a2713aSLionel Sambuc``void objc_moveWeak(id *dest, id *src);``
2136*f4a2713aSLionel Sambuc------------------------------------------
2137*f4a2713aSLionel Sambuc
2138*f4a2713aSLionel Sambuc*Precondition:* ``src`` is a valid pointer which either contains a null pointer
2139*f4a2713aSLionel Sambucor has been registered as a ``__weak`` object.  ``dest`` is a valid pointer
2140*f4a2713aSLionel Sambucwhich has not been registered as a ``__weak`` object.
2141*f4a2713aSLionel Sambuc
2142*f4a2713aSLionel Sambuc``dest`` is initialized to be equivalent to ``src``, potentially registering it
2143*f4a2713aSLionel Sambucwith the runtime.  ``src`` may then be left in its original state, in which
2144*f4a2713aSLionel Sambuccase this call is equivalent to :ref:`objc_copyWeak
2145*f4a2713aSLionel Sambuc<arc.runtime.objc_copyWeak>`, or it may be left as null.
2146*f4a2713aSLionel Sambuc
2147*f4a2713aSLionel SambucMust be atomic with respect to calls to ``objc_storeWeak`` on ``src``.
2148*f4a2713aSLionel Sambuc
2149*f4a2713aSLionel Sambuc.. _arc.runtime.objc_release:
2150*f4a2713aSLionel Sambuc
2151*f4a2713aSLionel Sambuc``void objc_release(id value);``
2152*f4a2713aSLionel Sambuc--------------------------------
2153*f4a2713aSLionel Sambuc
2154*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2155*f4a2713aSLionel Sambuc
2156*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it performs a
2157*f4a2713aSLionel Sambucrelease operation exactly as if the object had been sent the ``release``
2158*f4a2713aSLionel Sambucmessage.
2159*f4a2713aSLionel Sambuc
2160*f4a2713aSLionel Sambuc.. _arc.runtime.objc_retain:
2161*f4a2713aSLionel Sambuc
2162*f4a2713aSLionel Sambuc``id objc_retain(id value);``
2163*f4a2713aSLionel Sambuc-----------------------------
2164*f4a2713aSLionel Sambuc
2165*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2166*f4a2713aSLionel Sambuc
2167*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it performs a retain
2168*f4a2713aSLionel Sambucoperation exactly as if the object had been sent the ``retain`` message.
2169*f4a2713aSLionel Sambuc
2170*f4a2713aSLionel SambucAlways returns ``value``.
2171*f4a2713aSLionel Sambuc
2172*f4a2713aSLionel Sambuc.. _arc.runtime.objc_retainAutorelease:
2173*f4a2713aSLionel Sambuc
2174*f4a2713aSLionel Sambuc``id objc_retainAutorelease(id value);``
2175*f4a2713aSLionel Sambuc----------------------------------------
2176*f4a2713aSLionel Sambuc
2177*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2178*f4a2713aSLionel Sambuc
2179*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it performs a retain
2180*f4a2713aSLionel Sambucoperation followed by an autorelease operation.  Equivalent to the following
2181*f4a2713aSLionel Sambuccode:
2182*f4a2713aSLionel Sambuc
2183*f4a2713aSLionel Sambuc.. code-block:: objc
2184*f4a2713aSLionel Sambuc
2185*f4a2713aSLionel Sambuc  id objc_retainAutorelease(id value) {
2186*f4a2713aSLionel Sambuc    return objc_autorelease(objc_retain(value));
2187*f4a2713aSLionel Sambuc  }
2188*f4a2713aSLionel Sambuc
2189*f4a2713aSLionel SambucAlways returns ``value``.
2190*f4a2713aSLionel Sambuc
2191*f4a2713aSLionel Sambuc.. _arc.runtime.objc_retainAutoreleaseReturnValue:
2192*f4a2713aSLionel Sambuc
2193*f4a2713aSLionel Sambuc``id objc_retainAutoreleaseReturnValue(id value);``
2194*f4a2713aSLionel Sambuc---------------------------------------------------
2195*f4a2713aSLionel Sambuc
2196*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2197*f4a2713aSLionel Sambuc
2198*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it performs a retain
2199*f4a2713aSLionel Sambucoperation followed by the operation described in
2200*f4a2713aSLionel Sambuc:ref:`objc_autoreleaseReturnValue <arc.runtime.objc_autoreleaseReturnValue>`.
2201*f4a2713aSLionel SambucEquivalent to the following code:
2202*f4a2713aSLionel Sambuc
2203*f4a2713aSLionel Sambuc.. code-block:: objc
2204*f4a2713aSLionel Sambuc
2205*f4a2713aSLionel Sambuc  id objc_retainAutoreleaseReturnValue(id value) {
2206*f4a2713aSLionel Sambuc    return objc_autoreleaseReturnValue(objc_retain(value));
2207*f4a2713aSLionel Sambuc  }
2208*f4a2713aSLionel Sambuc
2209*f4a2713aSLionel SambucAlways returns ``value``.
2210*f4a2713aSLionel Sambuc
2211*f4a2713aSLionel Sambuc.. _arc.runtime.objc_retainAutoreleasedReturnValue:
2212*f4a2713aSLionel Sambuc
2213*f4a2713aSLionel Sambuc``id objc_retainAutoreleasedReturnValue(id value);``
2214*f4a2713aSLionel Sambuc----------------------------------------------------
2215*f4a2713aSLionel Sambuc
2216*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid object.
2217*f4a2713aSLionel Sambuc
2218*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, it attempts to
2219*f4a2713aSLionel Sambucaccept a hand off of a retain count from a call to
2220*f4a2713aSLionel Sambuc:ref:`objc_autoreleaseReturnValue <arc.runtime.objc_autoreleaseReturnValue>` on
2221*f4a2713aSLionel Sambuc``value`` in a recently-called function or something it calls.  If that fails,
2222*f4a2713aSLionel Sambucit performs a retain operation exactly like :ref:`objc_retain
2223*f4a2713aSLionel Sambuc<arc.runtime.objc_retain>`.
2224*f4a2713aSLionel Sambuc
2225*f4a2713aSLionel SambucAlways returns ``value``.
2226*f4a2713aSLionel Sambuc
2227*f4a2713aSLionel Sambuc.. _arc.runtime.objc_retainBlock:
2228*f4a2713aSLionel Sambuc
2229*f4a2713aSLionel Sambuc``id objc_retainBlock(id value);``
2230*f4a2713aSLionel Sambuc----------------------------------
2231*f4a2713aSLionel Sambuc
2232*f4a2713aSLionel Sambuc*Precondition:* ``value`` is null or a pointer to a valid block object.
2233*f4a2713aSLionel Sambuc
2234*f4a2713aSLionel SambucIf ``value`` is null, this call has no effect.  Otherwise, if the block pointed
2235*f4a2713aSLionel Sambucto by ``value`` is still on the stack, it is copied to the heap and the address
2236*f4a2713aSLionel Sambucof the copy is returned.  Otherwise a retain operation is performed on the
2237*f4a2713aSLionel Sambucblock exactly as if it had been sent the ``retain`` message.
2238*f4a2713aSLionel Sambuc
2239*f4a2713aSLionel Sambuc.. _arc.runtime.objc_storeStrong:
2240*f4a2713aSLionel Sambuc
2241*f4a2713aSLionel Sambuc``id objc_storeStrong(id *object, id value);``
2242*f4a2713aSLionel Sambuc----------------------------------------------
2243*f4a2713aSLionel Sambuc
2244*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer to a ``__strong`` object which is
2245*f4a2713aSLionel Sambucadequately aligned for a pointer.  ``value`` is null or a pointer to a valid
2246*f4a2713aSLionel Sambucobject.
2247*f4a2713aSLionel Sambuc
2248*f4a2713aSLionel SambucPerforms the complete sequence for assigning to a ``__strong`` object of
2249*f4a2713aSLionel Sambucnon-block type [*]_.  Equivalent to the following code:
2250*f4a2713aSLionel Sambuc
2251*f4a2713aSLionel Sambuc.. code-block:: objc
2252*f4a2713aSLionel Sambuc
2253*f4a2713aSLionel Sambuc  id objc_storeStrong(id *object, id value) {
2254*f4a2713aSLionel Sambuc    value = [value retain];
2255*f4a2713aSLionel Sambuc    id oldValue = *object;
2256*f4a2713aSLionel Sambuc    *object = value;
2257*f4a2713aSLionel Sambuc    [oldValue release];
2258*f4a2713aSLionel Sambuc    return value;
2259*f4a2713aSLionel Sambuc  }
2260*f4a2713aSLionel Sambuc
2261*f4a2713aSLionel SambucAlways returns ``value``.
2262*f4a2713aSLionel Sambuc
2263*f4a2713aSLionel Sambuc.. [*] This does not imply that a ``__strong`` object of block type is an
2264*f4a2713aSLionel Sambuc   invalid argument to this function. Rather it implies that an ``objc_retain``
2265*f4a2713aSLionel Sambuc   and not an ``objc_retainBlock`` operation will be emitted if the argument is
2266*f4a2713aSLionel Sambuc   a block.
2267*f4a2713aSLionel Sambuc
2268*f4a2713aSLionel Sambuc.. _arc.runtime.objc_storeWeak:
2269*f4a2713aSLionel Sambuc
2270*f4a2713aSLionel Sambuc``id objc_storeWeak(id *object, id value);``
2271*f4a2713aSLionel Sambuc--------------------------------------------
2272*f4a2713aSLionel Sambuc
2273*f4a2713aSLionel Sambuc*Precondition:* ``object`` is a valid pointer which either contains a null
2274*f4a2713aSLionel Sambucpointer or has been registered as a ``__weak`` object.  ``value`` is null or a
2275*f4a2713aSLionel Sambucpointer to a valid object.
2276*f4a2713aSLionel Sambuc
2277*f4a2713aSLionel SambucIf ``value`` is a null pointer or the object to which it points has begun
2278*f4a2713aSLionel Sambucdeallocation, ``object`` is assigned null and unregistered as a ``__weak``
2279*f4a2713aSLionel Sambucobject.  Otherwise, ``object`` is registered as a ``__weak`` object or has its
2280*f4a2713aSLionel Sambucregistration updated to point to ``value``.
2281*f4a2713aSLionel Sambuc
2282*f4a2713aSLionel SambucReturns the value of ``object`` after the call.
2283*f4a2713aSLionel Sambuc
2284