lookup from AST

Hi,

I need to access reflection gathered from some C++ code, e.g. which classes are defined, what data members does class "MyClass" have. My naive approach was to parse the source and query the resulting ASTContext using Sema, i.e. lookup "MyClass" and walk its decls. But Sema is a non-public API, which makes me wonder whether this is the right approach.

Is it?

Cheers, Axel.

Hi Axel,

Sema is considered a transient object (whose 'output' is a well-formed AST).

The design is oriented toward creating an ASTConsumer. This 'callback' interface, combined with ASTContext's "get*" functions (and the respective AST's) should do the trick.

Did you try this? If we are missing some functionality in the AST, let us know (it is entirely possible that there is some logic that needs to move from Sema->AST).

snaroff

Hi,

the consumer is great when I want to create a map / copy of the AST data. But I really would like to keep the AST data inside the ASTContext, and only query it, e.g. starting form a type name. If I understand the cunsumer correctly I would have to consume the AST and then walk my way down the scopes, for every lookup - even though that's already (much better :slight_smile: implemented in Sema...

Cheers, Axel.

DeclContext::lookup will perform name lookup into a specific context (class, namespace, etc.), but it only looks in that context---it doesn't perform full C++ qualified name lookup.

The real name-lookup routines live in Sema, but there are multiple clients that could benefit from moving these routines into the AST. For qualified name lookup, it's "just" a bunch of refactoring. For unqualified name lookup, it's a bit tougher, since we don't actually maintain Scope information in the AST.

  - Doug

Hi Doug,

thanks for your help!

(class, namespace, etc.)

Should I create an IdentifierTable and put that single name (and its declaring contexts) that I want to look up in there? Or is there a simpler way of creating a DeclarationName from a string?

> but it only looks in that context---it doesn't
> perform full C++ qualified name lookup.

The real name-lookup routines live in Sema, but there are multiple clients that could benefit from moving these routines into the AST. For qualified name lookup, it's "just" a bunch of refactoring.

Qualified is all I need, so having those available outside of Sema would be nice. Should I give it a try and suggest a patch? If so, should I simply replace DeclContext::lookup by what Sema::LookupQualifiedName currently does? I believe given qualified lookup in DeclContext we wouldn't need DeclContext::lookup anymore.

Cheers, Axel.

Hi,

progress, but not enough:

Should I create an IdentifierTable and put that single name (and its declaring contexts) that I want to look up in there? Or is there a simpler way of creating a DeclarationName from a string?

of course this is the wrong approach; I should first lex the string. But building a full blown preprocessor every time I need to lex a string like "nsp::class" looks a bit heavy. Is there a shortcut? Is it Preprocessor::CreateString()?

And the offer below still stands :wink:

Cheers, Axel.

If you have an ASTContext, you should be able to get to the identifier table it refers to. Based on that, you can use Table->get("foo") to get a uniqued identifierinfo*.

-Chris