[cfe-dev] RE : AST Transformation

Konstantin Tokarev annulen at yandex.ru
Mon Apr 4 09:43:47 PDT 2011



04.04.2011, 20:15, "David Chisnall" <csdavec at swan.ac.uk>:
> 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).

Thank you for explanation. However, isn't it possible to chain ASTConsumers?

E.g. we do all transformations in our ASTConsumer, than walk again through the tree and
call HandleTopLevelDecl, HandleTagDeclDefinition, HandleTranslationUnit, etc. of other
ASTConsumers just like AST is being created this moment.

Also, if I understand properly, there's another way to effectively "transform" AST - inherit from
Sema and change the way nodes are created (however it may not be enough information on
this step)

-- 
Regards,
Konstantin



More information about the cfe-dev mailing list