[cfe-dev] Getting the FieldDecl from a CXXDependentScopeMemberExpr

John McCall rjmccall at apple.com
Tue Jun 21 10:36:35 PDT 2011


On Jun 21, 2011, at 10:08 AM, Adrien Chauve wrote:
> I'm using the following code in an ASTConsumer/RecursiveASTVisitor:
> 
>   bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr* expr)
>   {
>        // skip accesses that are not written in the sources
>        if (expr->isImplicitAccess())
>                return true;
> 
>        DeclRefExpr* decl_ref_expr = dyn_cast<DeclRefExpr>(expr->getBase());
>        assert(decl_ref_expr);

Instead of dyn_cast / assert, you should use cast<DeclRefExpr>.

>        ValueDecl* v = decl_ref_expr->getDecl();
>        VarDecl* var_decl = dyn_cast<VarDecl>(v);
>        assert(var_decl);
> 
>        const Type* t = var_decl->getType().getTypePtr();
>        assert(t);
>        assert(t->isDependentType()); // Ok
>        //assert(t->isRecordType());    // Error
> 
>        return true;
>   }
> 
> I'm trying to get the FieldDecl but I have no clue where to go from
> this code...
> The assert(t->isRecordType()) fails and I don't know why (because it's
> a dependent type?).

Yes, because it's a dependent type.

We make CXXDependentScopeMemberExprs in precisely the situations
where we can't resolve the member down to something.  There is no way
to figure out the field here;  it could be totally different fields depending on
how the code is instantiated.  Your code just has to deal with the fact
that you often can't resolve things within a template pattern.

John.



More information about the cfe-dev mailing list