[GSoC] "C++0x Lambda functions implementation" proposal

Hello.
This is the Summer of Code proposal I was talking about in my last message (anybody read?).
I'll soon send it to melange, too.

Title: C++0x Lambda functions implementation

1. Proposal:
The C++ committee recently approved the new C++ 2011 international standard, adding a lot of
new features to the language. The clang compiler supports a part of them, including the most
relevant ones such as variadic templates and r-value references.
However, a lot of other new features still need to be implemented. This proposal is about
implementing the new lambda functions. Although it's not the most important feature of the
new standard, it's already implemented in other compilers such as gcc and VC++,
and I think clang should stay up-to-date.

This project will implement lambda functions in Clang, including other needed C++0x features.
In particular, this will imply the implementation of at least the new "lately-specified return type" syntax.
The project will cover all the aspect of the implementation, from the parser to code generation,
including full compliance with the international standard document and a full set of unit tests.

The implementation design will also need to deal with a similar feature that
clang already implements: the apple blocks extension. As previously discussed
on the mailing list and in the IRC channel, the two features will have to share a fair amount
of architecture to work well together, and the interoperability issues between the two
features need to be well studied (especially regarding object lifetime in Objective-C++).

2. Why I'm interested in this project:
My main academic interest area is in the development of compilers, and for this reason
I've followed and used the LLVM and Clang project for the last two years.
Since I plan to graduate this year, the GSoC timeline fits quite well in my plans and
I would like this project to be my undergraduate thesis work.
I've chosen to implement lambda functions because I'm also very interested in functional
and functional-style programming, and it therefore is my favourite C++0x feature.

3. How the project will be useful for LLVM:
In the next years, competition between C++ compilers will focus on C++ 2011 compliance.
Therefore, I think it will be important to Clang and LLVM to have strong support for this new standard.
Since Visual C++ 2010 already implements lambdas, this feature should be added as soon as possible
(imho) also to increase interoperability.

4. Prior knowledge in compilers and LLVM:
As I said, In my academic life I've been interested primarily in compilers implementation.
For this reason, I'm following the Compilers course at the University as part of
my undergraduate degree in CS, and I've been interested in LLVM and Clang projects for the
last two years.
I've used the LLVM backend for a small personal project last year, and I've sent a couple
of trivial patches to Clang in 2009.

5. Academic, Industry and other experiences:
I'm an undergraduate student of the Computer Science course at the University of Udine, Italy
(http://www.uniud.it)

I've worked a lot with C++ in the last five years, including the development of two complex custom
C++ software systems for a quite big local industry, regarding industrial production and product
testing automation.

In 2009, I've successfully applied to the Google Summer of Code for the KDE project, implementing
KAuth, the authentication and authorization framework used by KDE desktops applications.

6. Contact information
e-mail: nicola.gigante@gmail.com
IRC nickname: gigabytes at FreeNode and OFTC
personal blog: http://www.gigabytes.it (In italian, sorry)

Bye,
Nicola

Hello,

[...]
This project will implement lambda functions in Clang, including other needed C++0x features.
In particular, this will imply the implementation of at least the new "lately-specified return type" syntax.

According to the overview <http://clang.llvm.org/cxx_status.html>, this feature has not been implemented
(or even started) yet. I am currently too tired to be able to parse 8.3.5p2 reliably, but the following
works (well, compiles) in clang revision 128404:

static auto foo(int a, int b) -> decltype(a+b)
{
  return a+b;
}

static auto bar(float a, float b) -> float
{
  return a+b;
}

Bye,
Nicola

Jonathan

Hello,

[...]
This project will implement lambda functions in Clang, including other needed C++0x features.
In particular, this will imply the implementation of at least the new "lately-specified return type" syntax.

According to the overview <http://clang.llvm.org/cxx_status.html>, this feature has not been implemented
(or even started) yet. I am currently too tired to be able to parse 8.3.5p2 reliably, but the following
works (well, compiles) in clang revision 128404:
[...]

Oh, ok. The status page should be updated then. Anybody knows which revision introduced this?
Other thought on the proposal in general?

One one hand, it seems simple: Just transform a lambda into a local struct, turn captured variables into
attributes and initialize them in the local struct's constructor. Finally create an anonymous instance of
this struct and use it in place of the lambda (a bit like it is described here:
<http://stackoverflow.com/questions/3218821/c0x-lambda-overhead>).

(just ignore interaction with ObjC blocks. They are too different from each other)

On the other hand, clang is a really complex system, and C++ is a grotesquely complicated language, so
the integration of lambdas into the compiler will be a lot more difficult than the simple transformation
I outlined above. Therefore I wonder if you will have enough time to complete your implementation,
especially if you're not proficient in clang. OTOH, IMO even a partial, but working and well-documented
implementation still is better than none.

Finally, when I read your first announcement on this list, I was very excited, because although I realized
that other C++0x features such as rvalue references were far more needed than lambdas (they are, after all,
"just" syntactic sugar), they are still useful (but why, oh why couldn't they be polymorphic?).

So I say: Go for it! :slight_smile:

Jonathan

One one hand, it seems simple: Just transform a lambda into a local struct, turn captured variables into
attributes and initialize them in the local struct’s constructor. Finally create an anonymous instance of
this struct and use it in place of the lambda (a bit like it is described here:
<http://stackoverflow.com/questions/3218821/c0x-lambda-overhead>).

Yes, this is the main idea, and also the main attractive power of C++ lambdas when compared
with other languages: there’s no runtime overhead.

(just ignore interaction with ObjC blocks. They are too different from each other)

The semantics of the two are very incompatible, especially regarding object lifetime issues.
However, someone expressed in the past the need for them to be at least partially compatible
(for example, being able to pass a lambda where a block is expected), so I think it is at least
worth thinking about it.

On the other hand, clang is a really complex system, and C++ is a grotesquely complicated language, so
the integration of lambdas into the compiler will be a lot more difficult than the simple transformation
I outlined above. Therefore I wonder if you will have enough time to complete your implementation,
especially if you’re not proficient in clang. OTOH, IMO even a partial, but working and well-documented
implementation still is better than none.

I think that with a fair amount of help from my mentor and the other members of the community I will
be able to complete the job. Also, this will be my thesis work, so I should receive support from my
thesis relator here at the university.

Finally, when I read your first announcement on this list, I was very excited, because although I realized
that other C++0x features such as rvalue references were far more needed than lambdas (they are, after all,
“just” syntactic sugar), they are still useful (but why, oh why couldn’t they be polymorphic?).

So I say: Go for it! :slight_smile:

This positive opinion is very good news for me. Thank you :slight_smile:

Anybody else has comments or suggestions about the proposal? If you think it’s good enough
I’ll submit it to melange in the next days.

Jonathan

Bye,
Nicola

Nicola Gigante wrote:

Hello.
This is the Summer of Code proposal I was talking about in my last message
(anybody read?). I'll soon send it to melange, too.

Title: C++0x Lambda functions implementation

Two months ago someone on the IRC channel said:

  00:43 * alp has a c++0x lambda patch almost ready for review

Not sure what happened to that.

That’s a quite bad news but… nobody saw it, right?

Does anybody know who ‘alp’ is?

I copied him in.

Thanks,