[cfe-commits] r91893 - in /cfe/trunk: lib/CodeGen/Mangle.cpp test/CodeGenCXX/mangle.cpp test/CodeGenCXX/member-functions.cpp

Anders Carlsson andersca at mac.com
Mon Dec 21 22:36:32 PST 2009


Author: andersca
Date: Tue Dec 22 00:36:32 2009
New Revision: 91893

URL: http://llvm.org/viewvc/llvm-project?rev=91893&view=rev
Log:
Make sure that we mangle overloaded operators that are member functions correctly, giving them the correct arity.

With this seemingly insignificant fix, we are now able to build and link clang using clang itself! (LLVM still has to be built with gcc for the time being).

Modified:
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/test/CodeGenCXX/mangle.cpp
    cfe/trunk/test/CodeGenCXX/member-functions.cpp

Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=91893&r1=91892&r2=91893&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Tue Dec 22 00:36:32 2009
@@ -480,10 +480,17 @@
     mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
     break;
 
-  case DeclarationName::CXXOperatorName:
-    mangleOperatorName(Name.getCXXOverloadedOperator(),
-                       cast<FunctionDecl>(ND)->getNumParams());
+  case DeclarationName::CXXOperatorName: {
+    unsigned Arity = cast<FunctionDecl>(ND)->getNumParams();
+    
+    // If we have a C++ member function, we need to include the 'this' pointer.
+    // FIXME: This does not make sense for operators that are static, but their
+    // names stay the same regardless of the arity (operator new for instance).
+    if (isa<CXXMethodDecl>(ND))
+      Arity++;
+    mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
     break;
+  }
 
   case DeclarationName::CXXLiteralOperatorName:
     // FIXME: This mangling is not yet official.
@@ -611,16 +618,24 @@
   case OO_Array_Delete: Out << "da"; break;
   //              ::= ps        # + (unary)
   //              ::= pl        # +
-  case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
+  case OO_Plus: 
+    assert((Arity == 1 || Arity == 2) && "Invalid arity!");
+    Out << (Arity == 1? "ps" : "pl"); break;
   //              ::= ng        # - (unary)
   //              ::= mi        # -
-  case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
+  case OO_Minus: 
+    assert((Arity == 1 || Arity == 2) && "Invalid arity!");
+    Out << (Arity == 1? "ng" : "mi"); break;
   //              ::= ad        # & (unary)
   //              ::= an        # &
-  case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
+  case OO_Amp: 
+    assert((Arity == 1 || Arity == 2) && "Invalid arity!");
+    Out << (Arity == 1? "ad" : "an"); break;
   //              ::= de        # * (unary)
   //              ::= ml        # *
-  case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
+  case OO_Star: 
+    assert((Arity == 1 || Arity == 2) && "Invalid arity!");
+    Out << (Arity == 1? "de" : "ml"); break;
   //              ::= co        # ~
   case OO_Tilde: Out << "co"; break;
   //              ::= dv        # /

Modified: cfe/trunk/test/CodeGenCXX/mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/mangle.cpp?rev=91893&r1=91892&r2=91893&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/mangle.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/mangle.cpp Tue Dec 22 00:36:32 2009
@@ -141,7 +141,7 @@
 // PR5017
 extern "C" {
 struct Debug {
- const Debug& operator<< (unsigned a) const { }
+  const Debug& operator<< (unsigned a) const { return *this; }
 };
 Debug dbg;
 // CHECK: @_ZNK5DebuglsEj
@@ -270,5 +270,23 @@
 // CHECK: define void @_ZN11Expressions2f4ILb1EEEvPAquT_Li1ELi2E_i
 template <bool b> void f4(int (*)[b ? 1 : 2]) { };
 template void f4<true>(int (*)[1]);
-
 }
+
+struct Ops {
+  Ops& operator+(const Ops&);
+  Ops& operator-(const Ops&);
+  Ops& operator&(const Ops&);
+  Ops& operator*(const Ops&);
+  
+  void *v;
+};
+
+// CHECK: define %struct.Ops* @_ZN3OpsplERKS_
+Ops& Ops::operator+(const Ops&) { return *this; }
+// CHECK: define %struct.Ops* @_ZN3OpsmiERKS_
+Ops& Ops::operator-(const Ops&) { return *this; }
+// CHECK: define %struct.Ops* @_ZN3OpsanERKS_
+Ops& Ops::operator&(const Ops&) { return *this; }
+// CHECK: define %struct.Ops* @_ZN3OpsmlERKS_
+Ops& Ops::operator*(const Ops&) { return *this; }
+

Modified: cfe/trunk/test/CodeGenCXX/member-functions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/member-functions.cpp?rev=91893&r1=91892&r2=91893&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/member-functions.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/member-functions.cpp Tue Dec 22 00:36:32 2009
@@ -58,6 +58,6 @@
 void test3() {
   T t1, t2;
   
-  // RUN: grep "call i64 @_ZN1TpsERKS_" %t
+  // RUN: grep "call i64 @_ZN1TplERKS_" %t
   T result = t1 + t2;
 }





More information about the cfe-commits mailing list