[cfe-dev] Question of RecursiveASTVisitor class

Manuel Klimek klimek at google.com
Mon Nov 3 00:40:56 PST 2014


On Mon Nov 03 2014 at 4:00:27 AM Xiaohui Chen <xchen422 at uwo.ca> wrote:

>  Dear all:
>
> My name is Xiaohui Chen, a computer science student in UWO in canada and i
> am using Clang as the frontend of my project, but i am confusing of the
> following statements.
> PS: i am a newbie.
>
> These tasks are done by three groups of methods, respectively:00089 ///   1. TraverseDecl(Decl *x) does task #1.  It is the entry point00090 ///      for traversing an AST rooted at x.  This method simply00091 ///      dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo00092 ///      is the dynamic type of *x, which calls WalkUpFromFoo(x) and00093 ///      then recursively visits the child nodes of x.00094 ///      TraverseStmt(Stmt *x) and TraverseType(QualType x) work00095 ///      similarly.00096 ///   2. WalkUpFromFoo(Foo *x) does task #2.  It does not try to visit00097 ///      any child node of x.  Instead, it first calls WalkUpFromBar(x)00098 ///      where Bar is the direct parent class of Foo (unless Foo has00099 ///      no parent), and then calls VisitFoo(x) (see the next list item).00100 ///   3. VisitFoo(Foo *x) does task #3.00101 ///00102 /// These three method groups are tiered (Traverse* > WalkUpFrom* >00103 /// Visit*).  A method (e.g. Traverse*) may call methods from the same00104 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).00105 /// It may not call methods from a higher tier.
>
> According to the above statement, the calling relationship between these
> functions are organized in this way in general:
>
> Traversal*()
> {
>         .......
>         WalkUpFrom*();
>         .......
> }
>
> WalkUpFrom*()
> {
>          .......
>          Visit*();
>          .......
>
> }
>
> am i right?
>
> For this statement:
>
> 00096 ///   2. WalkUpFromFoo(Foo *x) does task #2.  It does not try to visit00097 ///      any child node of x.  Instead, it first calls WalkUpFromBar(x)00098 ///      where Bar is the direct parent class of Foo (unless Foo has00099 ///      no parent), and then calls VisitFoo(x) (see the next list item).
>
> what do you mean by saying "the direct parent class of Foo"?
>

For example, if you are currently traversing a CXXRecordDecl, the direct
parent class is the RecordDecl (
http://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html)


> why i need to visit the parent class before i visit the current class?
>

You don't need to visit anything - the RAV is doing the visitation for you.
You only need to know those details if you plan to change the traversal.


> what is the purpose? Could you please give me a short example?
>

This way you can implement the Visit(RecordDecl*) method and get a call to
it if the real type is a CXXRecordDecl.


>
> Thank you in advance!
>
> Sincerely
> xiaohui
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141103/9c2f1d54/attachment.html>


More information about the cfe-dev mailing list