[cfe-dev] new design of RecursiveASTVisitor

Jordy Rose jediknil at belkadan.com
Sat Jun 5 16:20:28 PDT 2010


Having just written a small tree-walker myself, I want to ask what
returning "false" means. There's lots of ways you might want to "stop" in
the middle of a traversal:

1. Stop traversing altogether. This is what the original proposal says, if
I'm reading it correctly.

2. Stop walking the class hierarchy, but still traverse the children. This
doesn't seem very useful -- a client could probably get around this without
any real trouble.

3. Skip this node's children and move on to the next node. Would have to
override TraverseFoo() for this right now, but not so hard since you just
call WalkUpFromFoo() and don't look at the children.

4. Skip the rest of the /parent/'s children. This is definitely trickier.

5. Jump an arbitrary number of levels back up (i.e. #4 recursively).
Probably not very common, but powerful enough to implement #3, #4, and #1,
with each one wrapped in simple API.


Do you (or does anyone) think there are enough uses of #3, #4, or #5 to
warrant implementing something like this? A nice way to do it could be a
simple push()/pop() system -- when you say abort, it pop()s back to the
last saved node. Alternately, you could return a special value that says
how far back to pop() (such as a particular Stmt*), with special constants
for "Continue" and "Abort".

Overall, though, I like the separation of responsibility. I'm wondering a
little bit about Abramo's point of pre- and post- visiting -- sometimes you
might need the children first before you can finish the parent. (For
example, the type of an expression depends on its children...not that we'd
be using RecursiveASTVisitor to figure out types.)

Jordy



More information about the cfe-dev mailing list