AST generation API?

Is there a component/API in Clang, separate from the parser, for
generating valid ASTs?

If you are *extremely* careful, you can create ASTs directly with the AST library, but there isn't much to help you get it right. To actually build semantically valid ASTs (including introducing implicit conversions and such), you would need to call into Sema's entry points yourself.

If not, has anyone given thought to what
that might look like?

Not really. One could certainly imagine creating a set of wrapper classes that allow one to form AST nodes using expression syntax, so that you write simple C++ expressions that evaluate into the appropriate Sema calls. I'm sure that people wanting to do AST manipulations would love to have such a library.

  - Doug

One thing that we are definitely interested in having is an AST verifier akin to LLVM's IR verifier: something which would explicitly check and enforce Clang's AST invariants. This would make it much easier to do AST manipulations by both (1) documenting the invariants and (2) catching obvious problems where ASTs fail to conform.

John.

Hi,

One thing that we are definitely interested in having is an AST verifier akin to LLVM's IR verifier: something which would explicitly check and enforce Clang's AST invariants. This would make it much easier to do AST manipulations by both (1) documenting the invariants and (2) catching obvious problems where ASTs fail to conform.

   +1
   That would be really fantastic tool. We do AST transformations
and we definitely need a tool like that.
   What we do is getting the newly (generated/moved) AST node
and try to perform semantic checks on it, taking into account
its (new) place. For simple verifications we use Sema::Check* interfaces.
Some of them are quite useful like Sema::CheckVariableDeclaration.

   The question is can we reuse the existing functionality?

John.

Cheers,
Vassil