Source code output

John Colvin john.loughran.colvin at gmail.com
Wed Jul 17 08:49:32 PDT 2013


On Wednesday, 17 July 2013 at 05:29:26 UTC, H. S. Teoh wrote:
> On Wed, Jul 17, 2013 at 05:27:34AM +0200, JS wrote:
>> With heavy ctfe code generation usage is it possible to have 
>> the d
>> compiler output the source code after all mixin templates have 
>> been
>> "used"? This way it is easier to visually check for errors in 
>> the
>> generated code.
>> 
>> I imagine one could use pragma in a "special way" to do this 
>> but I
>> was hoping for something more direct.
>
> Yeah I've been looking for this too. Once you have templates 
> and mixins
> nested deeply enough, it can be near impossible to figure out 
> just
> exactly *what* is being compiled at the end of it all.
>
> Though I suppose the common approach is to generate a string
> representing the code, and then using mixin(str) to compile it; 
> this
> lets you use pragma(msg) to output str and thereby see what 
> exactly is
> being compiled. But this may not be possible in some cases.
>
>
> T

I would love to have .codeof that can be applied to as many 
things as possible.

Or __traits(code, ...)

Or __traits(code, expansion_depth, ...)

So as to be able to drill down in to the code of something to the 
necessary level but no further.


Btw, this isn't only useful for printing out, it would be a 
powerful compile-time tool in general:

E.g. given a function
void foo(int a, int b)
{ body }

it would then be possible to do:

void fooCompileTimeCurried(int a)(int b)
{
     mixin(stripSig(__traits(code, 1, foo)));
}

to get an optimised version of foo for a == some_ct_value


More information about the Digitalmars-d-learn mailing list