This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++: Implement code transformation in parser or tree


Sohail Somani wrote:
> On Fri, 2006-11-10 at 19:46 -0800, Andrew Pinski wrote:
>> On Fri, 2006-11-10 at 15:23 -0800, Sohail Somani wrote:
>>>> Do you need new class types, or just an anonymous FUNCTION_DECL?
>>> Hi Mark, thanks for your reply.
>>>
>>> In general it would be a new class. If the lambda function looks like:
>>>
>>> void myfunc()
>>> {
>>>
>>> int a;
>>>
>>> ...<>(int i1,int i2) extern (a) {a=i1+i2}...
>>>
>>> }
>>>
>>> That would be a new class with an int reference (initialized to a) and
>>> operator()(int,int).
>>>
>>> Does that clarify?
>> Can lambda functions like this escape myfunc?  If not then using the
>> nested function mechanism that is already in GCC seems like a good
>> thing.  In fact I think of lambda functions as nested functions.
> 
> Yes they can in fact. So the object can outlive the scope.

As I understand the lambda proposal, the lambda function may not refer
to things that have gone out of scope.  It can use *references* that
have gone out of scope, but only if the referent is still in scope.
Since the way that something like:

  int i;
  void f() {
    int &a = i;
    ...<>() { return a; } ...
  }

should be implemented is with a lambda-local copy of "a" (rather than a
pointer to "a"), this is OK.

So, I do think that nested functions would be a natural implementation
in GCC, since they already provide access to a containing function's
stack frame.  You could also use the anonymous-class approach that you
suggested, but, as the lambda proposal mentions, using a nested function
may result in better code.  I suspect that what you want is a class (to
meet the requirements on ret_type, etc.) whose operator() is marked as a
nested function for GCC, in the event -- and *only* in event -- that the
lambda function refers to variables with non-static storage duration.

Also, it appears to me that there is something missing from N1958: there
is no discussion about what happens when you apply typeid to a lambda
function, or otherwise use it in a context that requires type_info.
(Can you throw it as an exception, for example?)  Can you capture its
type with typeof()?  Can you declare a function with a paramter of type
pointer-to-lambda-function?  Is this discussed, or am I missing something?

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]