[cfe-dev] prototyping c++ language extensions

Douglas Gregor dgregor at apple.com
Tue Apr 27 09:09:30 PDT 2010

On Apr 27, 2010, at 8:52 AM, Charllls Alquarra wrote:

>> Clang is probably the best environment available to try such a thing. Of
>> course, with Clang, you would create a direct compiler instead of a
>> source-to-source translator. (My own plans for compile-time reflection will
>> also be prototyped in Clang.)
> wouldn't it possible to have a default/identity/trivial code generator that produces readable c++ code?

Translating a complete AST into parseable, readable C++ code is actually quite complicated. Clang may eventually get there (I hope it does!), but there is a lot of work to make that happen.

> The reason i think this is important is than in languages like java annotations change the code at compile time but doesn't necessarily makes that code available for important stuff like debugging for instance. A source-to-source translator would allow an user to check the result of its compile-time metaprograms as c++ code instead of some target that is hard to debug/understand,

Source-to-source translation is a double-edged sword. Yes, you can see the transformed code to figure out what it does, and that is helpful to understand or debug the transformation. That might be helpful to some people to understand/debug their extensions, although I personally have found it to be a hindrance. 

On the other hand, you *always* see the transformed code, even when you don't care about it. If your extensions end up becoming popular, your users will not want to see the transformed code, and the source-to-source translation step is going to get in their way.

So, I don't recommend going the source-to-source translation route. That said...

> i've seen some comments on this mailing list that mention source-to-source translators but i'm not sure if they are for extending clang or rather their own projects

Clang has a Rewriter library that simplifies the task of rewriting parts of the source code without changing any of the surrounding code. For source-to-source translation that involves localized changes (e.g., replace some new language construct with some boilerplate code, add some attributes, etc.), the Rewriter is the best way to go. Check out the Objective-C rewriter to see how to use it.

	- Doug

More information about the cfe-dev mailing list