From: Anders Carlsson Date: Wed, 27 May 2009 04:18:27 +0000 (+0000) Subject: Handle operator call expressions where the callee is a member function. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0f294632f36459174199b77699e339715244b5ab;p=clang Handle operator call expressions where the callee is a member function. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72458 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index 0257494977..94d69d846c 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -124,6 +124,25 @@ RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { CE->arg_begin(), CE->arg_end()); } +RValue +CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, + const CXXMethodDecl *MD) { + assert(MD->isInstance() && + "Trying to emit a member call expr on a static method!"); + + + const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); + const llvm::Type *Ty = + CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), + FPT->isVariadic()); + llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); + + llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); + + return EmitCXXMemberCall(MD, Callee, This, + E->arg_begin() + 1, E->arg_end()); +} + llvm::Value *CodeGenFunction::LoadCXXThis() { assert(isa(CurFuncDecl) && "Must be in a C++ member function decl to load 'this'"); diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 45e19f139a..ff61d5ba1c 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1136,6 +1136,11 @@ RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { } } + if (const CXXOperatorCallExpr *CE = dyn_cast(E)) { + if (const CXXMethodDecl *MD = dyn_cast_or_null(TargetDecl)) + return EmitCXXOperatorMemberCallExpr(CE, MD); + } + llvm::Value *Callee = EmitScalarExpr(E->getCallee()); return EmitCall(Callee, E->getCallee()->getType(), E->arg_begin(), E->arg_end(), TargetDecl); diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 28c557c156..7d13071ece 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -686,6 +686,9 @@ public: CallExpr::const_arg_iterator ArgEnd); RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E); + RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, + const CXXMethodDecl *MD); + RValue EmitBuiltinExpr(const FunctionDecl *FD, unsigned BuiltinID, const CallExpr *E); diff --git a/test/CodeGenCXX/member-functions.cpp b/test/CodeGenCXX/member-functions.cpp index 02c7e2c36d..3f40453e98 100644 --- a/test/CodeGenCXX/member-functions.cpp +++ b/test/CodeGenCXX/member-functions.cpp @@ -37,7 +37,7 @@ struct S { static void f(); }; -// RUN: grep "define void @_ZN1S1fEv" %t +// RUN: grep "define void @_ZN1S1fEv" %t && void S::f() { } @@ -50,3 +50,14 @@ void test2() { S::g(); } + +struct T { + T operator+(const T&); +}; + +void test3() { + T t1, t2; + + // RUN: grep "call void @_ZN1TpsERK1T" %t + T result = t1 + t2; +}