[cfe-dev] lookup of a variable

Douglas Gregor dgregor at apple.com
Wed Sep 1 07:18:24 PDT 2010


On Sep 1, 2010, at 3:08 AM, Jan Bierbaum wrote:

> Hi!
> 
> 
> Given the qualified identifier of a variable (as a pure string like
> "A::B::foo"), an ASTContext and a Sema how can I

If you're actually coming from a string, you should consider watching what the parser does when parsing A::B::foo as an id-expression.

> 1) verify a variable with the given name exists within the current
> translation unit?

Sema::LookupQualifiedName can do this.

> 2) get its type?

Once you have a VarDecl, just use it's getType() member function.

> 3) the value it was initialized to, if any?

VarDecl::getInit().

> How about the same issue with an unqualified identifier and a
> Decl/DeclContext to start the search from?
> 
> I found 'Sema::Lookup*Name' and it looks like a good start. But to use
> that I need a 'LookupResult' which in turn needs a 'DeclarationNameInfo'
> that yet again needs a 'SourceLocation'. And I do not have that but only
> an ID...

LookupResult is where the results go. If you don't care about location information, just synthesize the DeclarationNameInfo with an empty SourceLocation().

> Can I use these Sema lookups at all? All examples I saw use them from
> within other Sema methods.


Yes, these are the lookup functions you want. LookupName is unqualified lookup from a scope, LookupQualifiedName is qualified name lookup into a DeclContext.

	- Doug



More information about the cfe-dev mailing list