www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - preprocessor pass equivalent?

reply "Jay Norwood" <jayn prismnet.com> writes:
Is there some option, similar to -E gcc option, that would 
generate the analogous listing for D?  What I mean is that I'd 
like to see all the code in version blocks gone, all the mixin 
strings expanded.

It is probably too much to wish for having the infered auto's 
expanded...
Mar 15 2012
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 15.03.2012 12:35, Jay Norwood wrote:
 Is there some option, similar to -E gcc option, that would generate the
 analogous listing for D? What I mean is that I'd like to see all the
 code in version blocks gone, all the mixin strings expanded.

 It is probably too much to wish for having the infered auto's expanded...

Not every auto could be expanded, even simple stuff like: auto add(T1,T2)(T1 t1, T2 t2){ return t1+t2; } Expanding mixins could be useful, yet in certain circumstances I'd rather not see the resulting code :) -- Dmitry Olshansky
Mar 15 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-03-15 09:35, Jay Norwood wrote:
 Is there some option, similar to -E gcc option, that would generate the
 analogous listing for D? What I mean is that I'd like to see all the
 code in version blocks gone, all the mixin strings expanded.

 It is probably too much to wish for having the infered auto's expanded...

The Eclipse plugin, Descent, has a view that does something like this. Although I don't know how well it works for D2. -- /Jacob Carlborg
Mar 15 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-03-15 11:04, Jacob Carlborg wrote:
 On 2012-03-15 09:35, Jay Norwood wrote:
 Is there some option, similar to -E gcc option, that would generate the
 analogous listing for D? What I mean is that I'd like to see all the
 code in version blocks gone, all the mixin strings expanded.

 It is probably too much to wish for having the infered auto's expanded...

The Eclipse plugin, Descent, has a view that does something like this. Although I don't know how well it works for D2.

It expands mixins, string mixins, replaces scope statements with try/catch/finally and other things. It also has a compile time debugger. -- /Jacob Carlborg
Mar 15 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-03-15 14:34, Jay Norwood wrote:
 On Thursday, 15 March 2012 at 10:09:25 UTC, Jacob Carlborg wrote:
 The Eclipse plugin, Descent, has a view that does something like this.
 Although I don't know how well it works for D2.

It expands mixins, string mixins, replaces scope statements with try/catch/finally and other things. It also has a compile time debugger.

Wow, that's pretty impressive. Does it do aliases also?

I think so and showing the actual type for type inference as well.
 I read a little more last night and saw that there is an option for json
 output from dmd, and tried it out. Is that meant to serve the purpose of
 the gccxml program?


 I also found mention of a c++ compiler that someone states can compile
 c++ and output C. Kind of weird, but maybe compiling D to expanded D
 would not be such a stretch.

I think LLVM can do this, convert C++ to C, via its byte code. -- /Jacob Carlborg
Mar 15 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, March 15, 2012 09:35:46 Jay Norwood wrote:
 Is there some option, similar to -E gcc option, that would
 generate the analogous listing for D?  What I mean is that I'd
 like to see all the code in version blocks gone, all the mixin
 strings expanded.
 
 It is probably too much to wish for having the infered auto's
 expanded...

http://d.puremagic.com/issues/show_bug.cgi?id=5051 - Jonathan M Davis
Mar 15 2012
prev sibling next sibling parent "Jay Norwood" <jayn prismnet.com> writes:
On Thursday, 15 March 2012 at 10:09:25 UTC, Jacob Carlborg wrote:
 The Eclipse plugin, Descent, has a view that does something 
 like this.
 Although I don't know how well it works for D2.

It expands mixins, string mixins, replaces scope statements with try/catch/finally and other things. It also has a compile time debugger.

Wow, that's pretty impressive. Does it do aliases also? I read a little more last night and saw that there is an option for json output from dmd, and tried it out. Is that meant to serve the purpose of the gccxml program? I also found mention of a c++ compiler that someone states can compile c++ and output C. Kind of weird, but maybe compiling D to expanded D would not be such a stretch. http://stackoverflow.com/questions/1139793/c-template-preprocessor-tool
Mar 15 2012
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 15 March 2012 at 08:35:48 UTC, Jay Norwood wrote:
 Is there some option, similar to -E gcc option, that would 
 generate the analogous listing for D?

You could add one to the compiler in just a few lines; there's already a function that does it, but it isn't called from anywhere. Open up mars.c, and find this comment: "Do not attempt to generate output files if errors" It is line 1358 in my copy. Right under that if(), add this: for (size_t i = 0; i < modules.dim; i++) { m = modules[i]; m->gensymfile(); } compile your new compiler. BACK UP YOUR FILES because this function overwrites the original .d file! $ cat test10.d void main() { auto a = 0; } # we have to backup because otherwise our original source will be lost! $ cp test10.d test10_original.d # you'll have to pass the paths for phobos and druntime unless you bring in a dmd.conf... $ d/dmd2/src/dmd/dmd -Id/dmd2/src/druntime/import -Id/dmd2/src/phobos -L-Ld/dmd2/linux/lib32/ test10.d $ cat test10.d // Sym file generated from 'test10.d' import object; void main() { int a = 0; return 0; } It gets ugly if you use a lot of features because this outputs the dmd translations - so foreach becomes for and other lowerings. But you can see basically what the compiler is going to generate for final code.
Mar 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Mar 15, 2012 at 02:51:33PM +0100, Adam D. Ruppe wrote:
 On Thursday, 15 March 2012 at 08:35:48 UTC, Jay Norwood wrote:
Is there some option, similar to -E gcc option, that would
generate the analogous listing for D?

You could add one to the compiler in just a few lines; there's already a function that does it, but it isn't called from anywhere.

 $ cat test10.d
 void main() {
    auto a = 0;
 }
 
 # we have to backup because otherwise our original source will be
 lost!
 $ cp test10.d test10_original.d
 
 # you'll have to pass the paths for phobos and druntime unless you
 bring in a dmd.conf...
 $ d/dmd2/src/dmd/dmd -Id/dmd2/src/druntime/import
 -Id/dmd2/src/phobos -L-Ld/dmd2/linux/lib32/    test10.d
 
 $ cat test10.d
 // Sym file generated from 'test10.d'
 import object;
 void main()
 {
 int a = 0;
 return 0;
 }

Whoa! This is cool. I might take a stab at making this a compiler option (and writing the output to stdout instead of overwriting the original source). I think this will be VERY useful when learning the language, and also when debugging a compiler bug. T -- EMACS = Extremely Massive And Cumbersome System
Mar 15 2012
prev sibling next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Thu, 15 Mar 2012 14:34:19 +0100, Jay Norwood <jayn prismnet.com> wrote:

 I also found mention of a c++ compiler that someone states can compile  
 c++ and output C.  Kind of weird, but maybe compiling D to expanded D  
 would not be such a stretch.

That's how early C++ compilers worked. Actually, many compilers work/have worked that way due to the ease of finding a C compiler for just about any given platform.
Mar 15 2012
prev sibling next sibling parent Trass3r <un known.com> writes:
There's a pull request to help with debugging string mixins:
https://github.com/D-Programming-Language/dmd/pull/426
Mar 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Mar 15, 2012 at 02:51:33PM +0100, Adam D. Ruppe wrote:
 On Thursday, 15 March 2012 at 08:35:48 UTC, Jay Norwood wrote:
Is there some option, similar to -E gcc option, that would
generate the analogous listing for D?

You could add one to the compiler in just a few lines; there's already a function that does it, but it isn't called from anywhere.

Alright, so I hacked dmd a little to make this a runtime option: I changed the default symfile extension from .d to .ds so that it won't overwrite the original source files, and added the option -sym to turn it on/off. Check it out: https://github.com/quickfur/dmd/tree/gensymfile Playing around with it a little, I notice that it doesn't output template bodies, even though calls to template functions are left intact. It also doesn't expand [] into opIndex/opIndexAssign, for example. So you can't actually compile the output if you're using templates, AFAICT. I'm guessing that by this point the compiler has stored all templates in a symbol table somewhere, so it only has the bare template declarations left. T -- The peace of mind---from knowing that viruses which exploit Microsoft system vulnerabilities cannot touch Linux---is priceless. -- Frustrated system administrator.
Mar 16 2012
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 17 March 2012 at 00:13:33 UTC, H. S. Teoh wrote:
 Playing around with it a little, I notice that it doesn't output
 template bodies, even though calls to template functions are 
 left intact. It also doesn't expand [] into 
 opIndex/opIndexAssign,

I didn't play with this yet.. but it might not have to do this. On arrays, [] is a primitive, not a function. opIndex should only show up on custom types. For the templates, I'd be surprised if they weren't there somewhere. My D -> JS code works in the same location (after running semantic) and templates just work in there. The instantiations might be in a weird place though.
Mar 16 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Mar 17, 2012 at 01:21:56AM +0100, Adam D. Ruppe wrote:
 On Saturday, 17 March 2012 at 00:13:33 UTC, H. S. Teoh wrote:
Playing around with it a little, I notice that it doesn't output
template bodies, even though calls to template functions are left
intact. It also doesn't expand [] into opIndex/opIndexAssign,

I didn't play with this yet.. but it might not have to do this. On arrays, [] is a primitive, not a function. opIndex should only show up on custom types.

Yes, but I was testing with my custom AA implementation.
 For the templates, I'd be surprised if they weren't there
 somewhere. My D -> JS code works in the same location
 (after running semantic) and templates just work in there.
 
 The instantiations might be in a weird place though.

Could be. I didn't really do much, just copied the code from your previous email as-is. I don't know enough about dmd internals to be able to do much more (though I'll have to get up to speed soon, if my AA implementation ends up going into druntime). T -- IBM = I'll Buy Microsoft!
Mar 16 2012