From 90434238b7edfc806437f1d40742a60ff92fbf6a Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Tue, 29 Mar 2011 08:08:18 +0000 Subject: [PATCH] Fix a bug in how we were resolving the address of overloaded functions when the resolution took place due to a single template specialization being named with an explicit template argument list. In this case, the "resolution" doesn't take into account the target type at all, and therefore can take place for functions, static member functions, and *non-static* member functions. The latter weren't being properly checked and their proper form enforced in this scenario. We now do so. The result of this last form slipping through was some confusing logic in IsStandardConversion handling of these resolved address-of expressions which eventually exploded in an assert. Simplify this logic a bit and add some more aggressive asserts to catch improperly formed expressions getting into this routine. Finally add systematic testing of member functions, both static and non-static, in the various forms they can take. One of these is essentially PR9563, and this commit fixes the crash in that PR. However, the diagnostics for this are still pretty terrible. We at least are now accepting the correct constructs and rejecting the invalid ones rather than accepting invalid or crashing as before. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128456 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaOverload.cpp | 55 +++++++++++----- .../p2-resolve-single-template-id.cpp | 66 ++++++++++++++++++- 2 files changed, 103 insertions(+), 18 deletions(-) diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 6fa78a9a46..8125ce1086 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -1058,27 +1058,33 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, // otherwise, only a boolean conversion is standard if (!ToType->isBooleanType()) return false; - - } - - if (CXXMethodDecl *Method = dyn_cast(Fn)) { - if (!Method->isStatic()) { - const Type *ClassType - = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); - FromType = S.Context.getMemberPointerType(FromType, ClassType); - } } - // If the "from" expression takes the address of the overloaded - // function, update the type of the resulting expression accordingly. - if (FromType->getAs()) - if (UnaryOperator *UnOp = dyn_cast(From->IgnoreParens())) - if (UnOp->getOpcode() == UO_AddrOf) - FromType = S.Context.getPointerType(FromType); + // Check if the "from" expression is taking the address of an overloaded + // function and recompute the FromType accordingly. Take advantage of the + // fact that non-static member functions *must* have such an address-of + // expression. + CXXMethodDecl *Method = dyn_cast(Fn); + if (Method && !Method->isStatic()) { + assert(isa(From->IgnoreParens()) && + "Non-unary operator on non-static member address"); + assert(cast(From->IgnoreParens())->getOpcode() + == UO_AddrOf && + "Non-address-of operator on non-static member address"); + const Type *ClassType + = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); + FromType = S.Context.getMemberPointerType(FromType, ClassType); + } else if (UnaryOperator *UnOp + = dyn_cast(From->IgnoreParens())) { + assert(UnOp->getOpcode() == UO_AddrOf && + "Non-address-of operator for overloaded function expression"); + FromType = S.Context.getPointerType(FromType); + } // Check that we've computed the proper type after overload resolution. - assert(S.Context.hasSameType(FromType, - S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); + assert(S.Context.hasSameType( + FromType, + S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); } else { return false; } @@ -7174,6 +7180,21 @@ public: DeclAccessPair dap; if( FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( OvlExpr, false, &dap) ) { + + if (CXXMethodDecl *Method = dyn_cast(Fn)) { + if (!Method->isStatic()) { + // If the target type is a non-function type and the function + // found is a non-static member function, pretend as if that was + // the target, it's the only possible type to end up with. + TargetTypeIsNonStaticMemberFunction = true; + + // And skip adding the function if its not in the proper form. + // We'll diagnose this due to an empty set of functions. + if (!OvlExprInfo.HasFormOfMemberPointer) + return; + } + } + Matches.push_back(std::make_pair(dap,Fn)); } } diff --git a/test/CXX/over/over.over/p2-resolve-single-template-id.cpp b/test/CXX/over/over.over/p2-resolve-single-template-id.cpp index 64871edcbc..f4a5e8f633 100644 --- a/test/CXX/over/over.over/p2-resolve-single-template-id.cpp +++ b/test/CXX/over/over.over/p2-resolve-single-template-id.cpp @@ -123,4 +123,68 @@ int main() } - +namespace member_pointers { + struct S { + template bool f(T) { return false; } + template static bool g(T) { return false; } + + template bool h(T) { return false; } + template static bool h(int) { return false; } + }; + + void test(S s) { + if (S::f) return; // expected-error {{call to non-static member function without an object argument}} + if (S::f) return; // expected-error {{call to non-static member function without an object argument}} + if (&S::f) return; + if (&S::f) return; + if (s.f) return; // expected-error {{contextually convertible}} + if (s.f) return; // expected-error {{contextually convertible}} + if (&s.f) return; // expected-error {{contextually convertible}} + if (&s.f) return; // expected-error {{contextually convertible}} + + if (S::g) return; + if (S::g) return; + if (&S::g) return; + if (&S::g) return; + if (s.g) return; + if (s.g) return; + if (&s.g) return; + if (&s.g) return; + + if (S::h<42>) return; + if (S::h) return; // expected-error {{contextually convertible}} + if (&S::h<42>) return; + if (&S::h) return; + if (s.h<42>) return; + if (s.h) return; // expected-error {{contextually convertible}} + if (&s.h<42>) return; + if (&s.h) return; // expected-error {{contextually convertible}} + + { bool b = S::f; } // expected-error {{call to non-static member function without an object argument}} + { bool b = S::f; } // expected-error {{call to non-static member function without an object argument}} + { bool b = &S::f; } + { bool b = &S::f; } + { bool b = s.f; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + { bool b = s.f; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + { bool b = &s.f; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + { bool b = &s.f; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + + { bool b = S::g; } + { bool b = S::g; } + { bool b = &S::g; } + { bool b = &S::g; } + { bool b = s.g; } + { bool b = s.g; } + { bool b = &s.g; } + { bool b = &s.g; } + + { bool b = S::h<42>; } + { bool b = S::h; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + { bool b = &S::h<42>; } + { bool b = &S::h; } + { bool b = s.h<42>; } + { bool b = s.h; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + { bool b = &s.h<42>; } + { bool b = &s.h; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}} + } +} -- 2.40.0