r181000 - Correctly emit certain implicit references to 'self' even within

John McCall rjmccall at apple.com
Fri May 3 10:07:37 PDT 2013


On May 3, 2013, at 9:14 AM, Jordan Rose <jordan_rose at apple.com> wrote:
> On May 3, 2013, at 0:33 , John McCall <rjmccall at apple.com> wrote:
>> Modified: cfe/trunk/lib/AST/DeclBase.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=181000&r1=180999&r2=181000&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/AST/DeclBase.cpp (original)
>> +++ cfe/trunk/lib/AST/DeclBase.cpp Fri May  3 02:33:41 2013
>> @@ -704,21 +704,37 @@ void Decl::CheckAccessDeclContext() cons
>> #endif
>> }
>> 
>> -DeclContext *Decl::getNonClosureContext() {
>> -  return getDeclContext()->getNonClosureAncestor();
>> -}
>> +static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
>> +static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
>> 
>> -DeclContext *DeclContext::getNonClosureAncestor() {
>> -  DeclContext *DC = this;
>> +/// Starting at a given context (a Decl or DeclContext), look for a
>> +/// code context that is not a closure (a lambda, block, etc.).
>> +template <class T> static Decl *getNonClosureContext(T *D) {
>> +  if (getKind(D) == Decl::CXXMethod) {
>> +    CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
>> +    if (MD->getParent()->isLambda() &&
>> +        MD->getOverloadedOperator() == OO_Call)
>> +      return getNonClosureContext(MD->getParent()->getParent());
>> +    return MD;
> 
> Can you explain this? I get that lambdas look like CXXMethodDecls, but what's wrong with dyn_cast here? I see that other kinds of methods will be caught by the FunctionDecl below, but honestly this seems like a silly reason to add complexity—not just in the check, but in needing the template and the random other free functions to make it work.

The template is there so that it works over both Decls and DeclContexts, since generically converting between them is not free.  You're right that the direct kind check is a micro-optimization that forces a two other free functions, but I don't actually feel bad about it.

> Also, checking the overloaded operator is probably ever so slightly cheaper to do before checking the parent.

Oh, you're right, because isLambda is slightly more complicated than I was guessing.  I'll fix it.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130503/2338db88/attachment.html>


More information about the cfe-commits mailing list