Mangle static variables inside Objective-C methods in Objective-C++. We currently mangle them the same way as gcc does.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91042 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index 2d86709..d156ba5 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -106,6 +106,8 @@
   void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
   void mangleQualifiers(Qualifiers Quals);
 
+  void mangleObjCMethodName(const ObjCMethodDecl *MD);
+  
   // Declare manglers for every type class.
 #define ABSTRACT_TYPE(CLASS, PARENT)
 #define NON_CANONICAL_TYPE(CLASS, PARENT)
@@ -325,7 +327,7 @@
     return;
   }
 
-  if (isa<FunctionDecl>(DC)) {
+  if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
     mangleLocalName(ND);
     return;
   }
@@ -539,7 +541,12 @@
   //              := Z <function encoding> E s [<discriminator>]
   // <discriminator> := _ <non-negative number>
   Out << 'Z';
-  mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
+  
+  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND->getDeclContext()))
+    mangleObjCMethodName(MD);
+  else  
+    mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
+
   Out << 'E';
   mangleSourceName(ND->getIdentifier());
 }
@@ -550,7 +557,6 @@
   //           ::= <template-param>
   //           ::= # empty
   //           ::= <substitution>
-  // FIXME: We only handle mangling of namespaces and classes at the moment.
 
   while (isa<LinkageSpecDecl>(DC))
     DC = DC->getParent();
@@ -703,6 +709,21 @@
   // FIXME: For now, just drop all extension qualifiers on the floor.
 }
 
+void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
+  llvm::SmallString<64> Name;
+  llvm::raw_svector_ostream OS(Name);
+  
+  const ObjCContainerDecl *CD = 
+    dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
+  assert (CD && "Missing container decl in GetNameForMethod");
+  OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
+  if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
+    OS << '(' << CID->getNameAsString() << ')';
+  OS << ' ' << MD->getSelector().getAsString() << ']';
+  
+  Out << OS.str().size() << OS.str();
+}
+
 void CXXNameMangler::mangleType(QualType T) {
   // Only operate on the canonical type!
   T = Context.getASTContext().getCanonicalType(T);