C++ -> D converter mentioned in AMA

Daniel Murphy yebblies at nospamgmail.com
Fri Oct 4 03:21:22 PDT 2013


"Jacob Carlborg" <doob at me.com> wrote in message 
news:l2lqik$1vdt$1 at digitalmars.com...
> On 2013-10-04 08:37, Szymon Gatner wrote:
>
>> Well, that is nothing Clang can't handle. The subset is what I was
>> asking for -  there has to be something that tool handles correctly, 
>> right?
>
> Of course Clang will be able to lex and parse it. But how should it be 
> translated?
>
> void foo (int a
> #if BAR
> ,
> int b
> #endif
> )
> { ... }
>
> You cannot do the exact same thing in D:
>
> void foo (int a
> version (BAR)
> {
>     ,
>     int b
> }
> )
> { ... }
>
> Doing these crazy things are only possible with a preprocessor.
>
> Then you need to duplicate the function, use a string mixin or something 
> else that's ugly.
>
> We can take a simpler example:
>
> #if _WIN32
> void foo (int);
> #elif __APPLE__
> void foo (long long);
> #elif linux
> void foo (long long);
> #endif
>
> Translating this manually it would look like this:
>
> version (Windows)
>     void foo (int);
> else version (OSX)
>     void foo (long);
> else version (linux)
>     void foo (long);
>
> But how could this be translated automatically? In this case you would 
> want to have all the above preprocessor macros enabled, at the same time. 
> Or somehow run it multiple times with different macros enabled and merge 
> them.
>
> I don't know how the preprocessor API looks like in Clang. If you could 
> search for hard coded identifiers or something similar.
>
> -- 
> /Jacob Carlborg

I deal with this by not running a preprocessor.  The #if directives are 
parsed as if they're real C++ constructs, and this means everything inside 
(and around) them must be valid C++ code.

With this constraint, translating them to static if/version and doing all 
versions simultaneously becomes possible. 




More information about the Digitalmars-d mailing list