1*67e74705SXin Li 2*67e74705SXin Li.. role:: block-term 3*67e74705SXin Li 4*67e74705SXin Li================================= 5*67e74705SXin LiLanguage Specification for Blocks 6*67e74705SXin Li================================= 7*67e74705SXin Li 8*67e74705SXin Li.. contents:: 9*67e74705SXin Li :local: 10*67e74705SXin Li 11*67e74705SXin LiRevisions 12*67e74705SXin Li========= 13*67e74705SXin Li 14*67e74705SXin Li- 2008/2/25 --- created 15*67e74705SXin Li- 2008/7/28 --- revised, ``__block`` syntax 16*67e74705SXin Li- 2008/8/13 --- revised, Block globals 17*67e74705SXin Li- 2008/8/21 --- revised, C++ elaboration 18*67e74705SXin Li- 2008/11/1 --- revised, ``__weak`` support 19*67e74705SXin Li- 2009/1/12 --- revised, explicit return types 20*67e74705SXin Li- 2009/2/10 --- revised, ``__block`` objects need retain 21*67e74705SXin Li 22*67e74705SXin LiOverview 23*67e74705SXin Li======== 24*67e74705SXin Li 25*67e74705SXin LiA new derived type is introduced to C and, by extension, Objective-C, 26*67e74705SXin LiC++, and Objective-C++ 27*67e74705SXin Li 28*67e74705SXin LiThe Block Type 29*67e74705SXin Li============== 30*67e74705SXin Li 31*67e74705SXin LiLike function types, the :block-term:`Block type` is a pair consisting 32*67e74705SXin Liof a result value type and a list of parameter types very similar to a 33*67e74705SXin Lifunction type. Blocks are intended to be used much like functions with 34*67e74705SXin Lithe key distinction being that in addition to executable code they 35*67e74705SXin Lialso contain various variable bindings to automatic (stack) or managed 36*67e74705SXin Li(heap) memory. 37*67e74705SXin Li 38*67e74705SXin LiThe abstract declarator, 39*67e74705SXin Li 40*67e74705SXin Li.. code-block:: c 41*67e74705SXin Li 42*67e74705SXin Li int (^)(char, float) 43*67e74705SXin Li 44*67e74705SXin Lidescribes a reference to a Block that, when invoked, takes two 45*67e74705SXin Liparameters, the first of type char and the second of type float, and 46*67e74705SXin Lireturns a value of type int. The Block referenced is of opaque data 47*67e74705SXin Lithat may reside in automatic (stack) memory, global memory, or heap 48*67e74705SXin Limemory. 49*67e74705SXin Li 50*67e74705SXin LiBlock Variable Declarations 51*67e74705SXin Li=========================== 52*67e74705SXin Li 53*67e74705SXin LiA :block-term:`variable with Block type` is declared using function 54*67e74705SXin Lipointer style notation substituting ``^`` for ``*``. The following are 55*67e74705SXin Livalid Block variable declarations: 56*67e74705SXin Li 57*67e74705SXin Li.. code-block:: c 58*67e74705SXin Li 59*67e74705SXin Li void (^blockReturningVoidWithVoidArgument)(void); 60*67e74705SXin Li int (^blockReturningIntWithIntAndCharArguments)(int, char); 61*67e74705SXin Li void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int); 62*67e74705SXin Li 63*67e74705SXin LiVariadic ``...`` arguments are supported. [variadic.c] A Block that 64*67e74705SXin Litakes no arguments must specify void in the argument list [voidarg.c]. 65*67e74705SXin LiAn empty parameter list does not represent, as K&R provide, an 66*67e74705SXin Liunspecified argument list. Note: both gcc and clang support K&R style 67*67e74705SXin Lias a convenience. 68*67e74705SXin Li 69*67e74705SXin LiA Block reference may be cast to a pointer of arbitrary type and vice 70*67e74705SXin Liversa. [cast.c] A Block reference may not be dereferenced via the 71*67e74705SXin Lipointer dereference operator ``*``, and thus a Block's size may not be 72*67e74705SXin Licomputed at compile time. [sizeof.c] 73*67e74705SXin Li 74*67e74705SXin LiBlock Literal Expressions 75*67e74705SXin Li========================= 76*67e74705SXin Li 77*67e74705SXin LiA :block-term:`Block literal expression` produces a reference to a 78*67e74705SXin LiBlock. It is introduced by the use of the ``^`` token as a unary 79*67e74705SXin Lioperator. 80*67e74705SXin Li 81*67e74705SXin Li.. code-block:: c 82*67e74705SXin Li 83*67e74705SXin Li Block_literal_expression ::= ^ block_decl compound_statement_body 84*67e74705SXin Li block_decl ::= 85*67e74705SXin Li block_decl ::= parameter_list 86*67e74705SXin Li block_decl ::= type_expression 87*67e74705SXin Li 88*67e74705SXin Liwhere type expression is extended to allow ``^`` as a Block reference 89*67e74705SXin Li(pointer) where ``*`` is allowed as a function reference (pointer). 90*67e74705SXin Li 91*67e74705SXin LiThe following Block literal: 92*67e74705SXin Li 93*67e74705SXin Li.. code-block:: c 94*67e74705SXin Li 95*67e74705SXin Li ^ void (void) { printf("hello world\n"); } 96*67e74705SXin Li 97*67e74705SXin Liproduces a reference to a Block with no arguments with no return value. 98*67e74705SXin Li 99*67e74705SXin LiThe return type is optional and is inferred from the return 100*67e74705SXin Listatements. If the return statements return a value, they all must 101*67e74705SXin Lireturn a value of the same type. If there is no value returned the 102*67e74705SXin Liinferred type of the Block is void; otherwise it is the type of the 103*67e74705SXin Lireturn statement value. 104*67e74705SXin Li 105*67e74705SXin LiIf the return type is omitted and the argument list is ``( void )``, 106*67e74705SXin Lithe ``( void )`` argument list may also be omitted. 107*67e74705SXin Li 108*67e74705SXin LiSo: 109*67e74705SXin Li 110*67e74705SXin Li.. code-block:: c 111*67e74705SXin Li 112*67e74705SXin Li ^ ( void ) { printf("hello world\n"); } 113*67e74705SXin Li 114*67e74705SXin Liand: 115*67e74705SXin Li 116*67e74705SXin Li.. code-block:: c 117*67e74705SXin Li 118*67e74705SXin Li ^ { printf("hello world\n"); } 119*67e74705SXin Li 120*67e74705SXin Liare exactly equivalent constructs for the same expression. 121*67e74705SXin Li 122*67e74705SXin LiThe type_expression extends C expression parsing to accommodate Block 123*67e74705SXin Lireference declarations as it accommodates function pointer 124*67e74705SXin Lideclarations. 125*67e74705SXin Li 126*67e74705SXin LiGiven: 127*67e74705SXin Li 128*67e74705SXin Li.. code-block:: c 129*67e74705SXin Li 130*67e74705SXin Li typedef int (*pointerToFunctionThatReturnsIntWithCharArg)(char); 131*67e74705SXin Li pointerToFunctionThatReturnsIntWithCharArg functionPointer; 132*67e74705SXin Li ^ pointerToFunctionThatReturnsIntWithCharArg (float x) { return functionPointer; } 133*67e74705SXin Li 134*67e74705SXin Liand: 135*67e74705SXin Li 136*67e74705SXin Li.. code-block:: c 137*67e74705SXin Li 138*67e74705SXin Li ^ int ((*)(float x))(char) { return functionPointer; } 139*67e74705SXin Li 140*67e74705SXin Liare equivalent expressions, as is: 141*67e74705SXin Li 142*67e74705SXin Li.. code-block:: c 143*67e74705SXin Li 144*67e74705SXin Li ^(float x) { return functionPointer; } 145*67e74705SXin Li 146*67e74705SXin Li[returnfunctionptr.c] 147*67e74705SXin Li 148*67e74705SXin LiThe compound statement body establishes a new lexical scope within 149*67e74705SXin Lithat of its parent. Variables used within the scope of the compound 150*67e74705SXin Listatement are bound to the Block in the normal manner with the 151*67e74705SXin Liexception of those in automatic (stack) storage. Thus one may access 152*67e74705SXin Lifunctions and global variables as one would expect, as well as static 153*67e74705SXin Lilocal variables. [testme] 154*67e74705SXin Li 155*67e74705SXin LiLocal automatic (stack) variables referenced within the compound 156*67e74705SXin Listatement of a Block are imported and captured by the Block as const 157*67e74705SXin Licopies. The capture (binding) is performed at the time of the Block 158*67e74705SXin Liliteral expression evaluation. 159*67e74705SXin Li 160*67e74705SXin LiThe compiler is not required to capture a variable if it can prove 161*67e74705SXin Lithat no references to the variable will actually be evaluated. 162*67e74705SXin LiProgrammers can force a variable to be captured by referencing it in a 163*67e74705SXin Listatement at the beginning of the Block, like so: 164*67e74705SXin Li 165*67e74705SXin Li.. code-block:: c 166*67e74705SXin Li 167*67e74705SXin Li (void) foo; 168*67e74705SXin Li 169*67e74705SXin LiThis matters when capturing the variable has side-effects, as it can 170*67e74705SXin Liin Objective-C or C++. 171*67e74705SXin Li 172*67e74705SXin LiThe lifetime of variables declared in a Block is that of a function; 173*67e74705SXin Lieach activation frame contains a new copy of variables declared within 174*67e74705SXin Lithe local scope of the Block. Such variable declarations should be 175*67e74705SXin Liallowed anywhere [testme] rather than only when C99 parsing is 176*67e74705SXin Lirequested, including for statements. [testme] 177*67e74705SXin Li 178*67e74705SXin LiBlock literal expressions may occur within Block literal expressions 179*67e74705SXin Li(nest) and all variables captured by any nested blocks are implicitly 180*67e74705SXin Lialso captured in the scopes of their enclosing Blocks. 181*67e74705SXin Li 182*67e74705SXin LiA Block literal expression may be used as the initialization value for 183*67e74705SXin LiBlock variables at global or local static scope. 184*67e74705SXin Li 185*67e74705SXin LiThe Invoke Operator 186*67e74705SXin Li=================== 187*67e74705SXin Li 188*67e74705SXin LiBlocks are :block-term:`invoked` using function call syntax with a 189*67e74705SXin Lilist of expression parameters of types corresponding to the 190*67e74705SXin Lideclaration and returning a result type also according to the 191*67e74705SXin Lideclaration. Given: 192*67e74705SXin Li 193*67e74705SXin Li.. code-block:: c 194*67e74705SXin Li 195*67e74705SXin Li int (^x)(char); 196*67e74705SXin Li void (^z)(void); 197*67e74705SXin Li int (^(*y))(char) = &x; 198*67e74705SXin Li 199*67e74705SXin Lithe following are all legal Block invocations: 200*67e74705SXin Li 201*67e74705SXin Li.. code-block:: c 202*67e74705SXin Li 203*67e74705SXin Li x('a'); 204*67e74705SXin Li (*y)('a'); 205*67e74705SXin Li (true ? x : *y)('a') 206*67e74705SXin Li 207*67e74705SXin LiThe Copy and Release Operations 208*67e74705SXin Li=============================== 209*67e74705SXin Li 210*67e74705SXin LiThe compiler and runtime provide :block-term:`copy` and 211*67e74705SXin Li:block-term:`release` operations for Block references that create and, 212*67e74705SXin Liin matched use, release allocated storage for referenced Blocks. 213*67e74705SXin Li 214*67e74705SXin LiThe copy operation ``Block_copy()`` is styled as a function that takes 215*67e74705SXin Lian arbitrary Block reference and returns a Block reference of the same 216*67e74705SXin Litype. The release operation, ``Block_release()``, is styled as a 217*67e74705SXin Lifunction that takes an arbitrary Block reference and, if dynamically 218*67e74705SXin Limatched to a Block copy operation, allows recovery of the referenced 219*67e74705SXin Liallocated memory. 220*67e74705SXin Li 221*67e74705SXin Li 222*67e74705SXin LiThe ``__block`` Storage Qualifier 223*67e74705SXin Li================================= 224*67e74705SXin Li 225*67e74705SXin LiIn addition to the new Block type we also introduce a new storage 226*67e74705SXin Liqualifier, :block-term:`__block`, for local variables. [testme: a 227*67e74705SXin Li__block declaration within a block literal] The ``__block`` storage 228*67e74705SXin Liqualifier is mutually exclusive to the existing local storage 229*67e74705SXin Liqualifiers auto, register, and static. [testme] Variables qualified by 230*67e74705SXin Li``__block`` act as if they were in allocated storage and this storage 231*67e74705SXin Liis automatically recovered after last use of said variable. An 232*67e74705SXin Liimplementation may choose an optimization where the storage is 233*67e74705SXin Liinitially automatic and only "moved" to allocated (heap) storage upon 234*67e74705SXin Lia Block_copy of a referencing Block. Such variables may be mutated as 235*67e74705SXin Linormal variables are. 236*67e74705SXin Li 237*67e74705SXin LiIn the case where a ``__block`` variable is a Block one must assume 238*67e74705SXin Lithat the ``__block`` variable resides in allocated storage and as such 239*67e74705SXin Liis assumed to reference a Block that is also in allocated storage 240*67e74705SXin Li(that it is the result of a ``Block_copy`` operation). Despite this 241*67e74705SXin Lithere is no provision to do a ``Block_copy`` or a ``Block_release`` if 242*67e74705SXin Lian implementation provides initial automatic storage for Blocks. This 243*67e74705SXin Liis due to the inherent race condition of potentially several threads 244*67e74705SXin Litrying to update the shared variable and the need for synchronization 245*67e74705SXin Liaround disposing of older values and copying new ones. Such 246*67e74705SXin Lisynchronization is beyond the scope of this language specification. 247*67e74705SXin Li 248*67e74705SXin Li 249*67e74705SXin LiControl Flow 250*67e74705SXin Li============ 251*67e74705SXin Li 252*67e74705SXin LiThe compound statement of a Block is treated much like a function body 253*67e74705SXin Liwith respect to control flow in that goto, break, and continue do not 254*67e74705SXin Liescape the Block. Exceptions are treated *normally* in that when 255*67e74705SXin Lithrown they pop stack frames until a catch clause is found. 256*67e74705SXin Li 257*67e74705SXin Li 258*67e74705SXin LiObjective-C Extensions 259*67e74705SXin Li====================== 260*67e74705SXin Li 261*67e74705SXin LiObjective-C extends the definition of a Block reference type to be 262*67e74705SXin Lithat also of id. A variable or expression of Block type may be 263*67e74705SXin Limessaged or used as a parameter wherever an id may be. The converse is 264*67e74705SXin Lialso true. Block references may thus appear as properties and are 265*67e74705SXin Lisubject to the assign, retain, and copy attribute logic that is 266*67e74705SXin Lireserved for objects. 267*67e74705SXin Li 268*67e74705SXin LiAll Blocks are constructed to be Objective-C objects regardless of 269*67e74705SXin Liwhether the Objective-C runtime is operational in the program or 270*67e74705SXin Linot. Blocks using automatic (stack) memory are objects and may be 271*67e74705SXin Limessaged, although they may not be assigned into ``__weak`` locations 272*67e74705SXin Liif garbage collection is enabled. 273*67e74705SXin Li 274*67e74705SXin LiWithin a Block literal expression within a method definition 275*67e74705SXin Lireferences to instance variables are also imported into the lexical 276*67e74705SXin Liscope of the compound statement. These variables are implicitly 277*67e74705SXin Liqualified as references from self, and so self is imported as a const 278*67e74705SXin Licopy. The net effect is that instance variables can be mutated. 279*67e74705SXin Li 280*67e74705SXin LiThe :block-term:`Block_copy` operator retains all objects held in 281*67e74705SXin Livariables of automatic storage referenced within the Block expression 282*67e74705SXin Li(or form strong references if running under garbage collection). 283*67e74705SXin LiObject variables of ``__block`` storage type are assumed to hold 284*67e74705SXin Linormal pointers with no provision for retain and release messages. 285*67e74705SXin Li 286*67e74705SXin LiFoundation defines (and supplies) ``-copy`` and ``-release`` methods for 287*67e74705SXin LiBlocks. 288*67e74705SXin Li 289*67e74705SXin LiIn the Objective-C and Objective-C++ languages, we allow the 290*67e74705SXin Li``__weak`` specifier for ``__block`` variables of object type. If 291*67e74705SXin Ligarbage collection is not enabled, this qualifier causes these 292*67e74705SXin Livariables to be kept without retain messages being sent. This 293*67e74705SXin Liknowingly leads to dangling pointers if the Block (or a copy) outlives 294*67e74705SXin Lithe lifetime of this object. 295*67e74705SXin Li 296*67e74705SXin LiIn garbage collected environments, the ``__weak`` variable is set to 297*67e74705SXin Linil when the object it references is collected, as long as the 298*67e74705SXin Li``__block`` variable resides in the heap (either by default or via 299*67e74705SXin Li``Block_copy()``). The initial Apple implementation does in fact 300*67e74705SXin Listart ``__block`` variables on the stack and migrate them to the heap 301*67e74705SXin Lionly as a result of a ``Block_copy()`` operation. 302*67e74705SXin Li 303*67e74705SXin LiIt is a runtime error to attempt to assign a reference to a 304*67e74705SXin Listack-based Block into any storage marked ``__weak``, including 305*67e74705SXin Li``__weak`` ``__block`` variables. 306*67e74705SXin Li 307*67e74705SXin Li 308*67e74705SXin LiC++ Extensions 309*67e74705SXin Li============== 310*67e74705SXin Li 311*67e74705SXin LiBlock literal expressions within functions are extended to allow const 312*67e74705SXin Liuse of C++ objects, pointers, or references held in automatic storage. 313*67e74705SXin Li 314*67e74705SXin LiAs usual, within the block, references to captured variables become 315*67e74705SXin Liconst-qualified, as if they were references to members of a const 316*67e74705SXin Liobject. Note that this does not change the type of a variable of 317*67e74705SXin Lireference type. 318*67e74705SXin Li 319*67e74705SXin LiFor example, given a class Foo: 320*67e74705SXin Li 321*67e74705SXin Li.. code-block:: c 322*67e74705SXin Li 323*67e74705SXin Li Foo foo; 324*67e74705SXin Li Foo &fooRef = foo; 325*67e74705SXin Li Foo *fooPtr = &foo; 326*67e74705SXin Li 327*67e74705SXin LiA Block that referenced these variables would import the variables as 328*67e74705SXin Liconst variations: 329*67e74705SXin Li 330*67e74705SXin Li.. code-block:: c 331*67e74705SXin Li 332*67e74705SXin Li const Foo block_foo = foo; 333*67e74705SXin Li Foo &block_fooRef = fooRef; 334*67e74705SXin Li Foo *const block_fooPtr = fooPtr; 335*67e74705SXin Li 336*67e74705SXin LiCaptured variables are copied into the Block at the instant of 337*67e74705SXin Lievaluating the Block literal expression. They are also copied when 338*67e74705SXin Licalling ``Block_copy()`` on a Block allocated on the stack. In both 339*67e74705SXin Licases, they are copied as if the variable were const-qualified, and 340*67e74705SXin Liit's an error if there's no such constructor. 341*67e74705SXin Li 342*67e74705SXin LiCaptured variables in Blocks on the stack are destroyed when control 343*67e74705SXin Lileaves the compound statement that contains the Block literal 344*67e74705SXin Liexpression. Captured variables in Blocks on the heap are destroyed 345*67e74705SXin Liwhen the reference count of the Block drops to zero. 346*67e74705SXin Li 347*67e74705SXin LiVariables declared as residing in ``__block`` storage may be initially 348*67e74705SXin Liallocated in the heap or may first appear on the stack and be copied 349*67e74705SXin Lito the heap as a result of a ``Block_copy()`` operation. When copied 350*67e74705SXin Lifrom the stack, ``__block`` variables are copied using their normal 351*67e74705SXin Liqualification (i.e. without adding const). In C++11, ``__block`` 352*67e74705SXin Livariables are copied as x-values if that is possible, then as l-values 353*67e74705SXin Liif not; if both fail, it's an error. The destructor for any initial 354*67e74705SXin Listack-based version is called at the variable's normal end of scope. 355*67e74705SXin Li 356*67e74705SXin LiReferences to ``this``, as well as references to non-static members of 357*67e74705SXin Liany enclosing class, are evaluated by capturing ``this`` just like a 358*67e74705SXin Linormal variable of C pointer type. 359*67e74705SXin Li 360*67e74705SXin LiMember variables that are Blocks may not be overloaded by the types of 361*67e74705SXin Litheir arguments. 362