Looking up names after parsing

I'm looking for the "right" way to resolve a name wrt an already parsed AST.

The lookup context would be the same we have at end of translation unit.

E.g. suppose my translation unit content is the following:

#include <vector>

namespace ns {
std::vector<int> v;

Now, after translation unit parsing has been completed I need to get:

a) Type* from string "std::vector<int>" (or NULL if this is not parsable)

b) NamedDecl* from string "ns::v" (or NULL if this is not parsable)

What's the Right(TM) way to do that using clang library?

At least for this last case, there is a DeclContext for the translation unit, and all DeclContext’s have a ‘lookup’ method. For the more complex cases, I would look at how Sema and other parts of Clang use the lookup method and their surrounding code.

However, I suspect the real challenge here is parsing “ns::v”. That isn’t a single name lookup, that as two, with the parser for nested-name-specifiers building on the first to issue the second. Doing each name lookup will be easy, but the rules for parsing a NNS are moderately complex. How far do you want to go? ;]

I do think it would be very handy to have a well defined set of routines to build a Clang parser for you that assumes a particular parse context (relative to an AST), and parses a string of text into a new AST-fragment. Tools like LLDB clearly need this so some form of it may already exist. Getting it into Clang’s Parser library would be very useful I think.