[cfe-dev] Written name of conversion operator
Douglas Gregor
dgregor at apple.com
Fri Jun 4 13:52:27 PDT 2010
On Jun 4, 2010, at 2:35 AM, Abramo Bagnara wrote:
> Il 04/06/2010 09:56, John McCall ha scritto:
>> On Jun 3, 2010, at 11:54 PM, Abramo Bagnara wrote:
>>> We have attempted to extract from AST the name of operator written in
>>> the source but we have failed:
>>>
>>> - using the DeclarationName we get the "wrong" name each time the name
>>> is not the one of the canonical type
>>>
>>> - using the TypeSourceInfo of CXXConversionDecl should be always correct
>>> for operator declaration, but it fails on operator call if the name
>>> specified in the call is different from the one in the declaration
>>>
>>> Is there a know way to obtain the written name?
>>
>> Unfortunately, no, and I'm not sure there's a straightforward way to make this possible, either. Possibly some extra storage on a DeclRefExpr / UnresolvedExpr?
>
> ... and also in the three kinds of MemberExpr :-(
>
> I'd like to propose you a whole different approach: non canonical
> DeclarationName.
>
> The equality comparison would become:
>
> /// operator== - Determine whether the specified names are identical..
> friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
> return LHS.getCanonical() == RHS.getCanonical();
> }
This could work, and perhaps by adding appropriate specializations we could get DenseMaps to work. However, I don't think it's quite the right approach, because it only solves half of the problem: we'll be able to reproduce the spelling of the operator name from a non-canonical DeclarationName, but we won't have source location information for type named by the conversion function.
I have a different idea of how to address this issue, for which I had previously filed this bug report:
http://llvm.org/bugs/show_bug.cgi?id=6357
Essentially, it suggests adding a DeclarationNameLoc type that, similarly to TypeLoc, contains information about the exact spelling of and the source locations for a declaration name. DeclarationNameLoc should remain small (IdentifierInfo* + SourceLocation for the common case), and use separate storage when we need to store extra information (e.g., a conversion operator would refer to the TypeSourceInfo * of the conversion type as written).
There would have to be a way---probably via the ASTContext---to extract a DeclarationName from a DeclarationNameLoc, for name-lookup purposes.
We would use DeclarationNameLoc within the ASTs (MemberExpr, DeclRefExpr, etc.) in lieu of DeclarationName.
> 2) only c++ conversion operator id has a canonical field so
> getCanonical() implementation uses a conditional on name kind
FWIW, C++ constructors and destructors should also get source information.
- Doug
More information about the cfe-dev
mailing list