Quantcast

About BlockDecl

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

About BlockDecl

Zhongxing Xu
Hi,

I have two questions about BlockDecl.

1. Is it possible to distinguish the following decls in AST:

^ void (void) {  };

^ (void) {};

^{};

2. When is BlockDeclRefExpr used?

Thanks.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About BlockDecl

Ted Kremenek

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.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About BlockDecl

Zhongxing Xu
Thanks. That's clear.

2009/12/4 Ted Kremenek <[hidden email]>:

>
> 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.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Loading...