Back-navigation of the Syntax Tree!

Dear all,
I need to make some kind of manipulation of a syntax tree in Clang.

Actually what I want to do is just move some statement (mainly the entire line... until the ';' which enclose a statement I want to move). Apparently this kind of work is more complex than what I thought.

At the beginning I perform a simple data-flow analysis to determine the positions where the statement can be placed (and that's is quite trivial). It can be easily done by using a visitor of the AST, but for example when I found, let's say a DeclRefExpr I'd like to have the possibility to access to the statement that encloses the var reference.
Practically, what I need is the possibility from a statement to access to the PARENT! I know that I can manually save a reference to the parent node before entering into a node and that will solve my problem but it is ugly! :slight_smile: Is this possible with the current API to access the parent of a node? I searched a lot and there is no way to get this information.

regards Simone

Hi Simone,

Check out ParentMap (and it's various clients).

snaroff

Simone Pellegrini wrote:

Practically, what I need is the possibility from a statement to access
to the PARENT! I know that I can manually save a reference to the parent
node before entering into a node and that will solve my problem but it
is ugly! :slight_smile: Is this possible with the current API to access the parent
of a node? I searched a lot and there is no way to get this information.
  

Your search result is correct. AST nodes do not save their parents, so
there is no way of getting the parent of a node save by iterating the
tree and looking.

The reason is that we try to keep the AST nodes as small as possible,
and an additional pointer in every node that is not needed by the most
common use cases (code generation, analysis, and the rewriter - the
users in the Clang repository) makes a very big difference.

Sebastian

Sebastian Redl wrote:

Your search result is correct. AST nodes do not save their parents, so
there is no way of getting the parent of a node save by iterating the
tree and looking.
  
Apparently I'm talking out of my ass. Well, good to know that there is a
solution available.

Sebastian

Sebastian Redl wrote:

Your search result is correct. AST nodes do not save their parents, so
there is no way of getting the parent of a node save by iterating the
tree and looking.

Apparently I'm talking out of my ass. Well, good to know that there is a
solution available.

Everything you said was actually true!

The surface area of the API has gotten quite large...don't beat yourself up for not knowing every API:-)

snaroff

Anyway,
the ParentMap definitely solved my problem! And actually the implementation/idea is very nice!
you CLANG guys kick ass! :stuck_out_tongue:

regards, Simone

steve naroff wrote: