[cfe-dev] About BlockDecl

Zhongxing Xu xuzhongxing at gmail.com
Thu Dec 3 23:25:15 PST 2009


Thanks. That's clear.

2009/12/4 Ted Kremenek <kremenek at apple.com>:
>
> On Dec 3, 2009, at 6:49 PM, Zhongxing Xu wrote:
>
>> Hi,
>>
>> I have two questions about BlockDecl.
>>
>> 1. Is it possible to distinguish the following decls in AST:
>>
>> ^ void (void) {  };
>>
>> ^ (void) {};
>>
>> ^{};
>
> Hmm.  It doesn't look like BlockDecl currently doesn't store enough source information to distinguish these cases, although I may be wrong.
>
>> 2. When is BlockDeclRefExpr used?
>
> It's used within a block to refer to a "captured" variable from outside the block.  From SemaExpr.cpp:
>
>  // If the identifier reference is inside a block, and it refers to a value
>  // that is outside the block, create a BlockDeclRefExpr instead of a
>  // DeclRefExpr.  This ensures the value is treated as a copy-in snapshot when
>  // the block is formed.
>  //
>
> For example:
>
> void foo(void) {
>  int x = 0;
>  ^{ x + 1; }();
> }
>
> The 'x' within the block would be BlockDeclRefExpr, as it refers to a "captured" variable outside the block.  By default, captured variables are literally copied when the block is created, but if a __block annotation precedes the declaration, the variable is captured by reference.  e.g.:
>
> void bar(void) {
>  __block int x = 0;
>  ^{ x = x + 1; }();
>  }
>
> Here 'x' in bar is literally modified by the block.




More information about the cfe-dev mailing list