[cfe-dev] Adding a Node to the AST?

Larisse Voufo lvoufo at cs.indiana.edu
Tue Nov 2 10:30:06 PDT 2010


I had to verify that it really worked for me before I did reply...
Here are the steps:

1 - Define the new node accordingly in the file
*CLANG_DIR*/include/clang/Basic/DeclNodes.td
e.g. *def Concept : DDecl<Template>, DeclContext;*

2 - Implement a class for the corresponding declaration,
specifying *Concept* as the declaration's kind.
e.g.
*class ConceptDecl : public TemplateDecl, public DeclContext {
*
*...
*
*ConceptDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
*
*                                TemplateParameterList *ParamList,  ...)
*
*: TemplateDecl(Concept, DC, L, Id, ParamList), DeclContext(Concept), ... {
}
...
*
*}*

3 - Verify that the appropriate Visit*() and Visit*Decl() methods
are implemented for every class that inherits from the DeclVisitor Class.
e.g. ASTImporter, DeclPrinter, ASTReaderDecl, ASTWriterDecl,
RecursiveASTVisitor, etc...

At the very least, allow the declaration to adopt the default behavior...
Foremost, update your included libraries appropriately (making sure that *
ConceptDecl* is visible within those files).

If anything was forgotten, the generated clang compilation errors should
indicate which file needs to be updated...

4 - There a few other minor hick-ups which one should be able to figure out
from observing other implementations, such as
adding block of codes such as these to the implementation of the ConceptDecl
class.

*        // Implement isa/cast/dyncast/etc.
        static bool classof(const Decl *D) { return
classofKind(D->getKind()); }
        static bool classof(const ConceptDecl *D) { return true; }
        static bool classofKind(Kind K) { return K == Concept; }
        static DeclContext *castToDeclContext(const ConceptDecl *D) {
            return static_cast<DeclContext *>(const_cast<ConceptDecl*>(D));
        }
        static ConceptDecl *castFromDeclContext(const DeclContext *DC) {
            return static_cast<ConceptDecl *>(const_cast<DeclContext*>(DC));
        }*


There may other other stuff I missed. But then again, I'm very new to
this... So, any comment/question can only help.

Cheers,

-- Larisse.


On Fri, Oct 29, 2010 at 9:15 AM, Jacob Carlborg <doob at me.com> wrote:

> I think it would be good if you could share
> your solution if someone else having the same problem.
>
> On 2010-10-29 14:03, Larisse Voufo wrote:
> > Please, never mind this. I figured it out.
> >
> > On Thu, Oct 28, 2010 at 6:49 PM, Larisse Voufo
> > <lvoufo at cs.indiana.edu
> > <mailto:lvoufo at cs.indiana.edu>> wrote:
> >
> >     By the way, here the exact line (bolded) of the generated
> >     DeclNode.inc at which it is complaining:
> >     -------------------------------------------
> >     DECL_CONTEXT(TranslationUnit)
> >     DECL_CONTEXT(Namespace)
> >     *DECL_CONTEXT(Concept)*
> >     DECL_CONTEXT(ObjCMethod)
> >     DECL_CONTEXT(LinkageSpec)
> >     -------------------------------------------
> >
> >
> >
> >     On Thu, Oct 28, 2010 at 6:46 PM, Larisse Voufo
> >     <lvoufo at cs.indiana.edu
> >     <mailto:lvoufo at cs.indiana.edu>> wrote:
> >
> >         Hi Folks,
> >
> >         If anyone can quickly answer this question for me, it'd
> >         definitely help  by a great deal.
> >         I am trying to add a node into the AST, but it seems there is at
> >         least one important parameter I'm overlooking.
> >
> >         I figured out that I was supposed to modify the DeclNodes.td
> >         file by inserting the following at some appropriate spot:
> >         ----------------------------------------
> >         def Concept : DDecl<Named, 1>, DeclContext;
> >         ----------------------------------------
> >
> >         However, this action is breaking my builds, and I'm not sure how
> >         to fix it.
> >
> >         --------------------------------------------------------
> >         llvm[2]: Compiling ParseConceptDecl.cpp for Debug+Asserts build
> >         In file included from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclBase.h:1187,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/Decl.h:18,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/ASTContext.h:21,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/Stmt.h:25,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/Expr.h:18,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclCXX.h:18,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclTemplate.h:17,
> >                           from
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclConcept.h:13,
> >                           from ParseConceptDecl.cpp:88:
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclNodes.inc:415:
> >         error: expected ‘,’ or ‘...’ before ‘*’ token
> >
> /Users/lvoufo/llvm/tools/clang/lib/Parse/../../include/clang/AST/DeclNodes.inc:415:
> >         error: ISO C++ forbids declaration of ‘ConceptDecl’ with no type
> >         make[2]: ***
> >
> [/Users/lvoufo/llvm/tools/clang/lib/Parse/Debug+Asserts/ParseConceptDecl.o]
> >         Error 1
> >         make[1]: *** [Parse/.makeall] Error 2
> >         make: *** [all] Error 1
> >         --------------------------------------------------------
> >
> >         What am I missing?
> >
> >         Thanks,
> >
> >         -- Larisse.
> >
> >
> >
> >
> >
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
> --
> /Jacob Carlborg
>
> _______________________________________________
> 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/20101102/0e328c5d/attachment.html>


More information about the cfe-dev mailing list