Thanks everyone for the responses, sounds like my best bet is to subclass RecursiveASTVisitor. I'm curious, sounds like Clang worked well to translate Objective-C to Javascript, since Javascript is similar to Actionscript, any gotchas to look out for? Yes I agree multiple inheritance would be a nightmare, I'm going to cheat by only converting C++ code that doesn't use it.<br>
<br><div class="gmail_quote">On Wed, Mar 28, 2012 at 9:21 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com">dgregor@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
On Mar 28, 2012, at 3:26 AM, David Chisnall wrote:<br>
<br>
> On 28 Mar 2012, at 03:37, Tim Stowell wrote:<br>
><br>
>> I've recently compiled Clang and LLVM on Windows. My goal is to use it to translate from C++ to another language (specifically Actionscript), but I'm not sure the best way to go about this. For example, if I invoke clang with the -ast-print "pretty print" option, it looks like Clang can get a faithful representation of the original code from its internal AST. Do I need to somehow mimic that code so I get a pretty print in my new language? Or should I walk the AST tree? Thanks for any help!<br>

><br>
> Yes, that's likely to be the best approach - it's what I did to translate Objective-C to JavaScript.  You will probably want to create a clang plugin that subclasses RecursiveASTVisitor.  This should walk the AST and emit code for your target language.<br>

><br>
> You could use the rewriter, but I would strongly recommend against it.  For example, when translating a class with multiple inheritance into ActionScript you will need to create a class that composes them and then turn pointer casts into some fairly complex logic for accessing the relevant class (upcasts, in particular, are going to be especially horrible - no idea how you plan on implementing these) so trying to do simple rewriting is going to involve a lot more pain than simply writing out a new program from the AST.<br>

<br>
<br>
</div></div>Right. The rewriter is good when you're just changing from one dialect to another within the same base language---Objective-C to C, C99 to C90, etc.---and want to preserve comments and code structure for all of the common parts. If the two languages are very different (C++ to Actionscript), you should either be transforming IR or walking the AST.<br>

<br>
        - Doug<br>
</blockquote></div><br>