Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Finding ICPPClassType for this from the AST

On 14-01-08 12:22 PM, Nathan Ridge wrote:
> Do you mean "class definition" rather than "function definition"? If so, that is
> what I was going to suggest for case 2, though I think you can do it more easily 
> by calling CPPVisitor.getContainingScope() for the name, and then walking up 
> to parent scopes until you find a class scope.
> 
> For case 3, I think you want to get the name 'T' and get the binding for it to
> get the class type. Alternatively, if getEvaluation() doesn't return an
> EvalMemberAccess, it probably returns an EvalID (off the top of my head,
> I haven't checked), and the class should be the EvalID's "name owner".

Thanks for the reply, using the CPPVisitor and the IScope works well in all my cases.  That is what I'll use for my
solution.

The following is irrelevant because your solution is better.  But for documentation, I did mean the function definition
but it was pretty difficult to get to it.  Here is the ugly code that I tested in my debugger:

IASTFunctionCallExpression fncall;
ICPPClassType clsType
= (
  (CPPMethod)
    (
      (IASTFunctionDefinition)
        fncall.getParent().getParent().getParent()).getDeclarator().getName().resolveBinding()
).getOwner();

Once I got past my mental block of thinking that I couldn't access bindings, I was able to short it to:

IASTExpression fnName = fncall.getFunctionNameExpression();
if(fnName instanceof IASTIdExpression) {
  IASTName n = ((IASTIdExpression) fnName).getName();
  if (n == null)
    return null;

  for(IBinding binding = n.resolveBinding(); binding != null; binding = binding.getOwner())
    if (binding instanceof ICPPClassType)
      return (ICPPClassType) binding;
}

The problem is that this still has special cases while your IScope method does not.

Thanks,
-Andrew



Back to the top