[cfe-dev] RE : AST Transformation

David Chisnall csdavec at swan.ac.uk
Mon Apr 4 09:15:03 PDT 2011


On 31 Mar 2011, at 16:35, Nico Weber wrote:

> The AST is designed to be immutable after construction, so "it's
> impossible to transform AST".

Note that this doesn't mean that you can't walk the AST and then construct a new AST that is based on the old one but in some way different.  

A good project for someone interested in AST transforms would be a subclass of RecursiveASTVisitor that generated a copy of the AST by walking each node in turn and adding an equivalent node to a new AST.  Users could then subclass this and use it to generate a new AST that differed from the original in only the specific ways that they desired.  

This would have the same effect as modifying an AST (albeit with a higher memory overhead), but would not impact any of the AST consumers that expect the AST to be immutable (i.e. all of the ones that currently exist, including CodeGen).

David

--
This email complies with ISO 3103



More information about the cfe-dev mailing list