This file is indexed.

/usr/share/doc/clang-3.9-doc/html/BlockLanguageSpec.html is in clang-3.9-doc 1:3.9.1-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Language Specification for Blocks &#8212; Clang 3.9 documentation</title>
    
    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '3.9',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Clang 3.9 documentation" href="index.html" />
    <link rel="up" title="Clang Language Extensions" href="LanguageExtensions.html" />
    <link rel="next" title="Block Implementation Specification" href="Block-ABI-Apple.html" />
    <link rel="prev" title="Objective-C Literals" href="ObjectiveCLiterals.html" /> 
  </head>
  <body role="document">
      <div class="header" role="banner"><h1 class="heading"><a href="index.html">
          <span>Clang 3.9 documentation</span></a></h1>
        <h2 class="heading"><span>Language Specification for Blocks</span></h2>
      </div>
      <div class="topnav" role="navigation" aria-label="top navigation">
      
        <p>
        «&#160;&#160;<a href="ObjectiveCLiterals.html">Objective-C Literals</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        &#160;&#160;::&#160;&#160;
        <a href="Block-ABI-Apple.html">Block Implementation Specification</a>&#160;&#160;»
        </p>

      </div>
      <div class="content">
        
        
  <div class="section" id="language-specification-for-blocks">
<h1>Language Specification for Blocks<a class="headerlink" href="#language-specification-for-blocks" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#revisions" id="id1">Revisions</a></li>
<li><a class="reference internal" href="#overview" id="id2">Overview</a></li>
<li><a class="reference internal" href="#the-block-type" id="id3">The Block Type</a></li>
<li><a class="reference internal" href="#block-variable-declarations" id="id4">Block Variable Declarations</a></li>
<li><a class="reference internal" href="#block-literal-expressions" id="id5">Block Literal Expressions</a></li>
<li><a class="reference internal" href="#the-invoke-operator" id="id6">The Invoke Operator</a></li>
<li><a class="reference internal" href="#the-copy-and-release-operations" id="id7">The Copy and Release Operations</a></li>
<li><a class="reference internal" href="#the-block-storage-qualifier" id="id8">The <code class="docutils literal"><span class="pre">__block</span></code> Storage Qualifier</a></li>
<li><a class="reference internal" href="#control-flow" id="id9">Control Flow</a></li>
<li><a class="reference internal" href="#objective-c-extensions" id="id10">Objective-C Extensions</a></li>
<li><a class="reference internal" href="#c-extensions" id="id11">C++ Extensions</a></li>
</ul>
</div>
<div class="section" id="revisions">
<h2><a class="toc-backref" href="#id1">Revisions</a><a class="headerlink" href="#revisions" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li>2008/2/25 &#8212; created</li>
<li>2008/7/28 &#8212; revised, <code class="docutils literal"><span class="pre">__block</span></code> syntax</li>
<li>2008/8/13 &#8212; revised, Block globals</li>
<li>2008/8/21 &#8212; revised, C++ elaboration</li>
<li>2008/11/1 &#8212; revised, <code class="docutils literal"><span class="pre">__weak</span></code> support</li>
<li>2009/1/12 &#8212; revised, explicit return types</li>
<li>2009/2/10 &#8212; revised, <code class="docutils literal"><span class="pre">__block</span></code> objects need retain</li>
</ul>
</div>
<div class="section" id="overview">
<h2><a class="toc-backref" href="#id2">Overview</a><a class="headerlink" href="#overview" title="Permalink to this headline"></a></h2>
<p>A new derived type is introduced to C and, by extension, Objective-C,
C++, and Objective-C++</p>
</div>
<div class="section" id="the-block-type">
<h2><a class="toc-backref" href="#id3">The Block Type</a><a class="headerlink" href="#the-block-type" title="Permalink to this headline"></a></h2>
<p>Like function types, the <span class="block-term">Block type</span> is a pair consisting
of a result value type and a list of parameter types very similar to a
function type. Blocks are intended to be used much like functions with
the key distinction being that in addition to executable code they
also contain various variable bindings to automatic (stack) or managed
(heap) memory.</p>
<p>The abstract declarator,</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="p">(</span><span class="o">^</span><span class="p">)(</span><span class="kt">char</span><span class="p">,</span> <span class="kt">float</span><span class="p">)</span>
</pre></div>
</div>
<p>describes a reference to a Block that, when invoked, takes two
parameters, the first of type char and the second of type float, and
returns a value of type int.  The Block referenced is of opaque data
that may reside in automatic (stack) memory, global memory, or heap
memory.</p>
</div>
<div class="section" id="block-variable-declarations">
<h2><a class="toc-backref" href="#id4">Block Variable Declarations</a><a class="headerlink" href="#block-variable-declarations" title="Permalink to this headline"></a></h2>
<p>A <span class="block-term">variable with Block type</span> is declared using function
pointer style notation substituting <code class="docutils literal"><span class="pre">^</span></code> for <code class="docutils literal"><span class="pre">*</span></code>. The following are
valid Block variable declarations:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="p">(</span><span class="o">^</span><span class="n">blockReturningVoidWithVoidArgument</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="kt">int</span> <span class="p">(</span><span class="o">^</span><span class="n">blockReturningIntWithIntAndCharArguments</span><span class="p">)(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">char</span><span class="p">);</span>
<span class="kt">void</span> <span class="p">(</span><span class="o">^</span><span class="n">arrayOfTenBlocksReturningVoidWithIntArgument</span><span class="p">[</span><span class="mi">10</span><span class="p">])(</span><span class="kt">int</span><span class="p">);</span>
</pre></div>
</div>
<p>Variadic <code class="docutils literal"><span class="pre">...</span></code> arguments are supported. [variadic.c] A Block that
takes no arguments must specify void in the argument list [voidarg.c].
An empty parameter list does not represent, as K&amp;R provide, an
unspecified argument list.  Note: both gcc and clang support K&amp;R style
as a convenience.</p>
<p>A Block reference may be cast to a pointer of arbitrary type and vice
versa. [cast.c] A Block reference may not be dereferenced via the
pointer dereference operator <code class="docutils literal"><span class="pre">*</span></code>, and thus a Block&#8217;s size may not be
computed at compile time. [sizeof.c]</p>
</div>
<div class="section" id="block-literal-expressions">
<h2><a class="toc-backref" href="#id5">Block Literal Expressions</a><a class="headerlink" href="#block-literal-expressions" title="Permalink to this headline"></a></h2>
<p>A <span class="block-term">Block literal expression</span> produces a reference to a
Block. It is introduced by the use of the <code class="docutils literal"><span class="pre">^</span></code> token as a unary
operator.</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">Block_literal_expression</span> <span class="o">::=</span>   <span class="o">^</span> <span class="n">block_decl</span> <span class="n">compound_statement_body</span>
<span class="n">block_decl</span> <span class="o">::=</span>
<span class="n">block_decl</span> <span class="o">::=</span> <span class="n">parameter_list</span>
<span class="n">block_decl</span> <span class="o">::=</span> <span class="n">type_expression</span>
</pre></div>
</div>
<p>where type expression is extended to allow <code class="docutils literal"><span class="pre">^</span></code> as a Block reference
(pointer) where <code class="docutils literal"><span class="pre">*</span></code> is allowed as a function reference (pointer).</p>
<p>The following Block literal:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="o">^</span> <span class="kt">void</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">&quot;hello world</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span> <span class="p">}</span>
</pre></div>
</div>
<p>produces a reference to a Block with no arguments with no return value.</p>
<p>The return type is optional and is inferred from the return
statements. If the return statements return a value, they all must
return a value of the same type. If there is no value returned the
inferred type of the Block is void; otherwise it is the type of the
return statement value.</p>
<p>If the return type is omitted and the argument list is <code class="docutils literal"><span class="pre">(</span> <span class="pre">void</span> <span class="pre">)</span></code>,
the <code class="docutils literal"><span class="pre">(</span> <span class="pre">void</span> <span class="pre">)</span></code> argument list may also be omitted.</p>
<p>So:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="o">^</span> <span class="p">(</span> <span class="kt">void</span> <span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">&quot;hello world</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span> <span class="p">}</span>
</pre></div>
</div>
<p>and:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="o">^</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">&quot;hello world</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span> <span class="p">}</span>
</pre></div>
</div>
<p>are exactly equivalent constructs for the same expression.</p>
<p>The type_expression extends C expression parsing to accommodate Block
reference declarations as it accommodates function pointer
declarations.</p>
<p>Given:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="nf">int</span> <span class="p">(</span><span class="o">*</span><span class="n">pointerToFunctionThatReturnsIntWithCharArg</span><span class="p">)(</span><span class="kt">char</span><span class="p">);</span>
<span class="n">pointerToFunctionThatReturnsIntWithCharArg</span> <span class="n">functionPointer</span><span class="p">;</span>
<span class="o">^</span> <span class="n">pointerToFunctionThatReturnsIntWithCharArg</span> <span class="p">(</span><span class="kt">float</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">functionPointer</span><span class="p">;</span> <span class="p">}</span>
</pre></div>
</div>
<p>and:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="o">^</span> <span class="kt">int</span> <span class="p">((</span><span class="o">*</span><span class="p">)(</span><span class="kt">float</span> <span class="n">x</span><span class="p">))(</span><span class="kt">char</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">functionPointer</span><span class="p">;</span> <span class="p">}</span>
</pre></div>
</div>
<p>are equivalent expressions, as is:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="o">^</span><span class="p">(</span><span class="kt">float</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">functionPointer</span><span class="p">;</span> <span class="p">}</span>
</pre></div>
</div>
<p>[returnfunctionptr.c]</p>
<p>The compound statement body establishes a new lexical scope within
that of its parent. Variables used within the scope of the compound
statement are bound to the Block in the normal manner with the
exception of those in automatic (stack) storage. Thus one may access
functions and global variables as one would expect, as well as static
local variables. [testme]</p>
<p>Local automatic (stack) variables referenced within the compound
statement of a Block are imported and captured by the Block as const
copies. The capture (binding) is performed at the time of the Block
literal expression evaluation.</p>
<p>The compiler is not required to capture a variable if it can prove
that no references to the variable will actually be evaluated.
Programmers can force a variable to be captured by referencing it in a
statement at the beginning of the Block, like so:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">foo</span><span class="p">;</span>
</pre></div>
</div>
<p>This matters when capturing the variable has side-effects, as it can
in Objective-C or C++.</p>
<p>The lifetime of variables declared in a Block is that of a function;
each activation frame contains a new copy of variables declared within
the local scope of the Block. Such variable declarations should be
allowed anywhere [testme] rather than only when C99 parsing is
requested, including for statements. [testme]</p>
<p>Block literal expressions may occur within Block literal expressions
(nest) and all variables captured by any nested blocks are implicitly
also captured in the scopes of their enclosing Blocks.</p>
<p>A Block literal expression may be used as the initialization value for
Block variables at global or local static scope.</p>
</div>
<div class="section" id="the-invoke-operator">
<h2><a class="toc-backref" href="#id6">The Invoke Operator</a><a class="headerlink" href="#the-invoke-operator" title="Permalink to this headline"></a></h2>
<p>Blocks are <span class="block-term">invoked</span> using function call syntax with a
list of expression parameters of types corresponding to the
declaration and returning a result type also according to the
declaration. Given:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="p">(</span><span class="o">^</span><span class="n">x</span><span class="p">)(</span><span class="kt">char</span><span class="p">);</span>
<span class="kt">void</span> <span class="p">(</span><span class="o">^</span><span class="n">z</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="kt">int</span> <span class="p">(</span><span class="o">^</span><span class="p">(</span><span class="o">*</span><span class="n">y</span><span class="p">))(</span><span class="kt">char</span><span class="p">)</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">;</span>
</pre></div>
</div>
<p>the following are all legal Block invocations:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">x</span><span class="p">(</span><span class="sc">&#39;a&#39;</span><span class="p">);</span>
<span class="p">(</span><span class="o">*</span><span class="n">y</span><span class="p">)(</span><span class="sc">&#39;a&#39;</span><span class="p">);</span>
<span class="p">(</span><span class="nb">true</span> <span class="o">?</span> <span class="nl">x</span> <span class="p">:</span> <span class="o">*</span><span class="n">y</span><span class="p">)(</span><span class="sc">&#39;a&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-copy-and-release-operations">
<h2><a class="toc-backref" href="#id7">The Copy and Release Operations</a><a class="headerlink" href="#the-copy-and-release-operations" title="Permalink to this headline"></a></h2>
<p>The compiler and runtime provide <span class="block-term">copy</span> and
<span class="block-term">release</span> operations for Block references that create and,
in matched use, release allocated storage for referenced Blocks.</p>
<p>The copy operation <code class="docutils literal"><span class="pre">Block_copy()</span></code> is styled as a function that takes
an arbitrary Block reference and returns a Block reference of the same
type. The release operation, <code class="docutils literal"><span class="pre">Block_release()</span></code>, is styled as a
function that takes an arbitrary Block reference and, if dynamically
matched to a Block copy operation, allows recovery of the referenced
allocated memory.</p>
</div>
<div class="section" id="the-block-storage-qualifier">
<h2><a class="toc-backref" href="#id8">The <code class="docutils literal"><span class="pre">__block</span></code> Storage Qualifier</a><a class="headerlink" href="#the-block-storage-qualifier" title="Permalink to this headline"></a></h2>
<p>In addition to the new Block type we also introduce a new storage
qualifier, <span class="block-term">__block</span>, for local variables. [testme: a
__block declaration within a block literal] The <code class="docutils literal"><span class="pre">__block</span></code> storage
qualifier is mutually exclusive to the existing local storage
qualifiers auto, register, and static. [testme] Variables qualified by
<code class="docutils literal"><span class="pre">__block</span></code> act as if they were in allocated storage and this storage
is automatically recovered after last use of said variable.  An
implementation may choose an optimization where the storage is
initially automatic and only &#8220;moved&#8221; to allocated (heap) storage upon
a Block_copy of a referencing Block.  Such variables may be mutated as
normal variables are.</p>
<p>In the case where a <code class="docutils literal"><span class="pre">__block</span></code> variable is a Block one must assume
that the <code class="docutils literal"><span class="pre">__block</span></code> variable resides in allocated storage and as such
is assumed to reference a Block that is also in allocated storage
(that it is the result of a <code class="docutils literal"><span class="pre">Block_copy</span></code> operation).  Despite this
there is no provision to do a <code class="docutils literal"><span class="pre">Block_copy</span></code> or a <code class="docutils literal"><span class="pre">Block_release</span></code> if
an implementation provides initial automatic storage for Blocks.  This
is due to the inherent race condition of potentially several threads
trying to update the shared variable and the need for synchronization
around disposing of older values and copying new ones.  Such
synchronization is beyond the scope of this language specification.</p>
</div>
<div class="section" id="control-flow">
<h2><a class="toc-backref" href="#id9">Control Flow</a><a class="headerlink" href="#control-flow" title="Permalink to this headline"></a></h2>
<p>The compound statement of a Block is treated much like a function body
with respect to control flow in that goto, break, and continue do not
escape the Block.  Exceptions are treated <em>normally</em> in that when
thrown they pop stack frames until a catch clause is found.</p>
</div>
<div class="section" id="objective-c-extensions">
<h2><a class="toc-backref" href="#id10">Objective-C Extensions</a><a class="headerlink" href="#objective-c-extensions" title="Permalink to this headline"></a></h2>
<p>Objective-C extends the definition of a Block reference type to be
that also of id.  A variable or expression of Block type may be
messaged or used as a parameter wherever an id may be. The converse is
also true. Block references may thus appear as properties and are
subject to the assign, retain, and copy attribute logic that is
reserved for objects.</p>
<p>All Blocks are constructed to be Objective-C objects regardless of
whether the Objective-C runtime is operational in the program or
not. Blocks using automatic (stack) memory are objects and may be
messaged, although they may not be assigned into <code class="docutils literal"><span class="pre">__weak</span></code> locations
if garbage collection is enabled.</p>
<p>Within a Block literal expression within a method definition
references to instance variables are also imported into the lexical
scope of the compound statement. These variables are implicitly
qualified as references from self, and so self is imported as a const
copy. The net effect is that instance variables can be mutated.</p>
<p>The <span class="block-term">Block_copy</span> operator retains all objects held in
variables of automatic storage referenced within the Block expression
(or form strong references if running under garbage collection).
Object variables of <code class="docutils literal"><span class="pre">__block</span></code> storage type are assumed to hold
normal pointers with no provision for retain and release messages.</p>
<p>Foundation defines (and supplies) <code class="docutils literal"><span class="pre">-copy</span></code> and <code class="docutils literal"><span class="pre">-release</span></code> methods for
Blocks.</p>
<p>In the Objective-C and Objective-C++ languages, we allow the
<code class="docutils literal"><span class="pre">__weak</span></code> specifier for <code class="docutils literal"><span class="pre">__block</span></code> variables of object type.  If
garbage collection is not enabled, this qualifier causes these
variables to be kept without retain messages being sent. This
knowingly leads to dangling pointers if the Block (or a copy) outlives
the lifetime of this object.</p>
<p>In garbage collected environments, the <code class="docutils literal"><span class="pre">__weak</span></code> variable is set to
nil when the object it references is collected, as long as the
<code class="docutils literal"><span class="pre">__block</span></code> variable resides in the heap (either by default or via
<code class="docutils literal"><span class="pre">Block_copy()</span></code>).  The initial Apple implementation does in fact
start <code class="docutils literal"><span class="pre">__block</span></code> variables on the stack and migrate them to the heap
only as a result of a <code class="docutils literal"><span class="pre">Block_copy()</span></code> operation.</p>
<p>It is a runtime error to attempt to assign a reference to a
stack-based Block into any storage marked <code class="docutils literal"><span class="pre">__weak</span></code>, including
<code class="docutils literal"><span class="pre">__weak</span></code> <code class="docutils literal"><span class="pre">__block</span></code> variables.</p>
</div>
<div class="section" id="c-extensions">
<h2><a class="toc-backref" href="#id11">C++ Extensions</a><a class="headerlink" href="#c-extensions" title="Permalink to this headline"></a></h2>
<p>Block literal expressions within functions are extended to allow const
use of C++ objects, pointers, or references held in automatic storage.</p>
<p>As usual, within the block, references to captured variables become
const-qualified, as if they were references to members of a const
object.  Note that this does not change the type of a variable of
reference type.</p>
<p>For example, given a class Foo:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">Foo</span> <span class="n">foo</span><span class="p">;</span>
<span class="n">Foo</span> <span class="o">&amp;</span><span class="n">fooRef</span> <span class="o">=</span> <span class="n">foo</span><span class="p">;</span>
<span class="n">Foo</span> <span class="o">*</span><span class="n">fooPtr</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">foo</span><span class="p">;</span>
</pre></div>
</div>
<p>A Block that referenced these variables would import the variables as
const variations:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">const</span> <span class="n">Foo</span> <span class="n">block_foo</span> <span class="o">=</span> <span class="n">foo</span><span class="p">;</span>
<span class="n">Foo</span> <span class="o">&amp;</span><span class="n">block_fooRef</span> <span class="o">=</span> <span class="n">fooRef</span><span class="p">;</span>
<span class="n">Foo</span> <span class="o">*</span><span class="k">const</span> <span class="n">block_fooPtr</span> <span class="o">=</span> <span class="n">fooPtr</span><span class="p">;</span>
</pre></div>
</div>
<p>Captured variables are copied into the Block at the instant of
evaluating the Block literal expression.  They are also copied when
calling <code class="docutils literal"><span class="pre">Block_copy()</span></code> on a Block allocated on the stack.  In both
cases, they are copied as if the variable were const-qualified, and
it&#8217;s an error if there&#8217;s no such constructor.</p>
<p>Captured variables in Blocks on the stack are destroyed when control
leaves the compound statement that contains the Block literal
expression.  Captured variables in Blocks on the heap are destroyed
when the reference count of the Block drops to zero.</p>
<p>Variables declared as residing in <code class="docutils literal"><span class="pre">__block</span></code> storage may be initially
allocated in the heap or may first appear on the stack and be copied
to the heap as a result of a <code class="docutils literal"><span class="pre">Block_copy()</span></code> operation. When copied
from the stack, <code class="docutils literal"><span class="pre">__block</span></code> variables are copied using their normal
qualification (i.e. without adding const).  In C++11, <code class="docutils literal"><span class="pre">__block</span></code>
variables are copied as x-values if that is possible, then as l-values
if not; if both fail, it&#8217;s an error.  The destructor for any initial
stack-based version is called at the variable&#8217;s normal end of scope.</p>
<p>References to <code class="docutils literal"><span class="pre">this</span></code>, as well as references to non-static members of
any enclosing class, are evaluated by capturing <code class="docutils literal"><span class="pre">this</span></code> just like a
normal variable of C pointer type.</p>
<p>Member variables that are Blocks may not be overloaded by the types of
their arguments.</p>
</div>
</div>


      </div>
      <div class="bottomnav" role="navigation" aria-label="bottom navigation">
      
        <p>
        «&#160;&#160;<a href="ObjectiveCLiterals.html">Objective-C Literals</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        &#160;&#160;::&#160;&#160;
        <a href="Block-ABI-Apple.html">Block Implementation Specification</a>&#160;&#160;»
        </p>

      </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2007-2017, The Clang Team.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
    </div>
  </body>
</html>