[cfe-dev] libclang and MemberRefExpr

Erik Verbruggen erik.verbruggen at me.com
Fri Jun 3 07:47:58 PDT 2011



On 27 May, 2011,at 02:16 AM, Douglas Gregor <dgregor at apple.com> wrote:


On May 25, 2011, at 6:47 PM, Erik Verbruggen wrote:

On 13 May, 2011,at 07:01 PM, Douglas Gregor <dgregor at apple.com> wrote:


On May 13, 2011, at 9:55 AM, Erik Verbruggen wrote:


On 13 May 2011, at 17:56, Douglas Gregor wrote:


On May 13, 2011, at 5:13 AM, Erik Verbruggen wrote:

On 12 May, 2011,at 05:43 PM, Douglas Gregor <dgregor at apple.com> wrote:

However, if the name isn't a simple identifier ("x.operator[]"), then you can still get the location of the start of the member name ("operator"), but when you ask for the range, you'll only get the range of that one token.

Back when we designed libclang, Clang didn't even have the information about where the three tokens of "operator[]" were. Now, we actually have this information via DeclarationNameInfo, so it would make sense to add an API for specifically what you want. Here is a general API that (I think!) could fully solve this problem:

CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, unsigned PieceIndex);

where C is a cursor that references something else (e.g., a member reference, declaration reference, type reference, etc.), and returns the source range covering the reference itself. The two "unsigned" values would be for configurability:

- NameFlags could be bitset with three independent flags: WantQualifier (to ask it to include the nested-name-specifier, e.g., Foo:: in x.Foo::y, in the range), WantTemplateArgs (to ask it to include the explicit template arguments, e.g., <int> in x.f<int>, in the range), and WantSinglePiece (described below).

- WantPiece/PieceIndex is my attempt at handling cases where the name itself isn't contiguous. For example, imagine the expression "a[y]", which ends up referring to an overloaded operator[]. The source range for the full operator name is, effectively, "[y]", since the name has been split into two parts. However, that's not necessarily useful, so WantPiece would indicate that we want a range covering only one piece of the name, where PieceIndex==0 indicates that we want the '[' and PieceIndex==1 indicates that we want the ']'.

So for "operator[]" you would 3 pieces? Or just 1?

Probably just one.

I'm giving it a shot, but I'm running into a couple of problems. For ObjC, ParseObjCMethodDecl does not store the identifier positions, nor the colons. Should I extend the Selector there, or is there another way to get the SourceLocations for those tokens?

Right, this data isn't available yet. We'd need to pass through a separate set of SourceLocations for the identifiers and colons.

I wasn't actually expecting you to tackle selectors, but I think it's great if you're planning to work on those, too. I was mostly concerned that any solution to this problem can still work for selectors.
 
Ok, I will do that in a second iteration.


Then for C++, consider:

struct Struct {
    void func();
    int operator[](int);
};

void f()
{
    Struct inst;
    inst.func(); // 1
    inst[1];  // 2
    inst.operator[](1);  // 3
}

Now (1) and (3) are doable, but (2) is a bit tricky: the SourceLocation for the two pieces of the DeclRefExpr are stored in the CXXOperatorCallExpr node as the lparen/rparen location in the CallExpr parent-class. Should these two locations also be stored in the DeclRefExpr, like in case (3)? Or should we leave them where they are and handle case (2) in the same way as case (1)?

I think that case (2) should be treated the same way as case (1). The fact that these are stored as CXXOperatorCallExprs indicates that the MemberRefExpr is synthesized.
 
So to get the location of the brackets in case (2), you would need to call clang_getCursorReferenceNameRange on the CallExpr and then deduce that the MemberRefExpr is synthesised? Or should clang_getCursorReferenceNameRange check if it is called on a cursor pointing to a DeclRefExpr, walk up the AST nodes to find a CXXOperatorCallExpr, and then get the bracket locations from it? And if this last one is the proper behaviour, how can I get the parent node of an AST node?

Regards,
Erik.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110603/ce73bfa1/attachment.html>


More information about the cfe-dev mailing list