[cfe-dev] About BlockDecl
Ted Kremenek
kremenek at apple.com
Thu Dec 3 23:07:17 PST 2009
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