objective-c modern translator: Refactor intername ivar names to
one place and use it throughout. Also, change ivar name to avoid
name collisions. // rdar://11079366



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153093 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Rewrite/RewriteModernObjC.cpp b/lib/Rewrite/RewriteModernObjC.cpp
index d3d8622..aee89f1 100644
--- a/lib/Rewrite/RewriteModernObjC.cpp
+++ b/lib/Rewrite/RewriteModernObjC.cpp
@@ -3171,6 +3171,14 @@
     llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct");
 }
 
+static void WriteInternalIvarName(ObjCInterfaceDecl *IDecl,
+                                  ObjCIvarDecl *IvarDecl, std::string &Result) {
+  Result += "OBJC_IVAR_$_";
+  Result += IDecl->getName();
+  Result += "$";
+  Result += IvarDecl->getName();
+}
+
 /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which
 /// have been referenced in an ivar access expression.
 void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
@@ -3193,9 +3201,9 @@
       if (CDecl->getImplementation())
         Result += "__declspec(dllexport) ";
     }
-    Result += "extern unsigned long OBJC_IVAR_$_";
-    Result += CDecl->getName(); Result += "_";
-    Result += IvarDecl->getName(); Result += ";";
+    Result += "extern unsigned long ";
+    WriteInternalIvarName(CDecl, IvarDecl, Result);
+    Result += ";";
   }
 }
 
@@ -5746,8 +5754,7 @@
 static void Write_IvarOffsetVar(ASTContext *Context,
                                 std::string &Result, 
                                 ArrayRef<ObjCIvarDecl *> Ivars, 
-                                StringRef VarName, 
-                                StringRef ClassName) {
+                                ObjCInterfaceDecl *CDecl) {
   // FIXME. visibilty of offset symbols may have to be set; for Darwin
   // this is what happens:
   /**
@@ -5771,10 +5778,7 @@
       Result += "unsigned long int "; 
     else
       Result += "__declspec(dllexport) unsigned long int ";
-    
-    Result += VarName;
-    Result += ClassName; Result += "_";
-    Result += IvarDecl->getName(); 
+    WriteInternalIvarName(CDecl, IvarDecl, Result);
     Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))";
     Result += " = ";
     if (IvarDecl->isBitField()) {
@@ -5784,7 +5788,7 @@
     }
     else {
       Result += "__OFFSETOFIVAR__(struct ";
-      Result += ClassName;
+      Result += CDecl->getNameAsString();
       Result += "_IMPL, "; 
       Result += IvarDecl->getName(); Result += ");\n";
     }
@@ -5795,14 +5799,14 @@
                                            ASTContext *Context, std::string &Result,
                                            ArrayRef<ObjCIvarDecl *> Ivars,
                                            StringRef VarName,
-                                           StringRef ClassName) {
+                                           ObjCInterfaceDecl *CDecl) {
   if (Ivars.size() > 0) {
-    Write_IvarOffsetVar(Context, Result, Ivars, "OBJC_IVAR_$_", ClassName);
+    Write_IvarOffsetVar(Context, Result, Ivars, CDecl);
     
     Result += "\nstatic ";
     Write__ivar_list_t_TypeDecl(Result, Ivars.size());
     Result += " "; Result += VarName;
-    Result += ClassName;
+    Result += CDecl->getNameAsString();
     Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
     Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n";
     Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n";
@@ -5812,9 +5816,8 @@
         Result += "\t{{";
       else
         Result += "\t {";
-      
-      Result += "(unsigned long int *)&OBJC_IVAR_$_";
-      Result += ClassName; Result += "_"; Result += IvarDecl->getName();
+      Result += "(unsigned long int *)&";
+      WriteInternalIvarName(CDecl, IvarDecl, Result);
       Result += ", ";
       
       Result += "\""; Result += IvarDecl->getName(); Result += "\", ";
@@ -6088,7 +6091,7 @@
   
   Write__ivar_list_t_initializer(*this, Context, Result, IVars, 
                                  "_OBJC_$_INSTANCE_VARIABLES_",
-                                 CDecl->getNameAsString());
+                                 CDecl);
   
   // Build _objc_method_list for class's instance methods if needed
   SmallVector<ObjCMethodDecl *, 32>
@@ -6523,10 +6526,9 @@
       assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
       
       // Build name of symbol holding ivar offset.
-      std::string IvarOffsetName = "OBJC_IVAR_$_";
-      IvarOffsetName += clsDeclared->getIdentifier()->getName();
-      IvarOffsetName += "_";
-      IvarOffsetName += D->getName();
+      std::string IvarOffsetName;
+      WriteInternalIvarName(clsDeclared, D, IvarOffsetName);
+      
       ReferencedIvars[clsDeclared].insert(D);
       
       // cast offset to "char *".
diff --git a/test/Rewriter/objc-modern-ivar-receiver-1.mm b/test/Rewriter/objc-modern-ivar-receiver-1.mm
index 537530b..5ffcdd4 100644
--- a/test/Rewriter/objc-modern-ivar-receiver-1.mm
+++ b/test/Rewriter/objc-modern-ivar-receiver-1.mm
@@ -25,4 +25,4 @@
 }
 @end
 
-// CHECK: id obj = (*(id *)((char *)newInv + OBJC_IVAR_$_NSInvocation__container));
+// CHECK: id obj = (*(id *)((char *)newInv + OBJC_IVAR_$_NSInvocation$_container));
diff --git a/test/Rewriter/rewrite-modern-block-ivar-call.mm b/test/Rewriter/rewrite-modern-block-ivar-call.mm
index 0fe2d54..1bafbdd 100644
--- a/test/Rewriter/rewrite-modern-block-ivar-call.mm
+++ b/test/Rewriter/rewrite-modern-block-ivar-call.mm
@@ -14,4 +14,4 @@
 }
 @end
 
-// CHECK: ((void (*)(struct __block_impl *))((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo__block)))->FuncPtr)((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo__block)));
+// CHECK: ((void (*)(struct __block_impl *))((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo$_block)))->FuncPtr)((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo$_block)));
diff --git a/test/Rewriter/rewrite-modern-nested-ivar.mm b/test/Rewriter/rewrite-modern-nested-ivar.mm
index e2b5ea2..13e31cd 100644
--- a/test/Rewriter/rewrite-modern-nested-ivar.mm
+++ b/test/Rewriter/rewrite-modern-nested-ivar.mm
@@ -30,4 +30,4 @@
   }
 @end
 
-// CHECK: (*(NSURLResponse **)((char *)(*(NSURLResponse **)((char *)(*(NSCachedURLResponseInternal **)((char *)self + OBJC_IVAR_$_NSCachedURLResponse__internal)) + OBJC_IVAR_$_NSCachedURLResponseInternal_response)) + OBJC_IVAR_$_NSURLResponse_InnerResponse)) = 0;
+// CHECK: (*(NSURLResponse **)((char *)(*(NSURLResponse **)((char *)(*(NSCachedURLResponseInternal **)((char *)self + OBJC_IVAR_$_NSCachedURLResponse$_internal)) + OBJC_IVAR_$_NSCachedURLResponseInternal$response)) + OBJC_IVAR_$_NSURLResponse$InnerResponse)) = 0;
diff --git a/test/Rewriter/rewrite-modern-struct-ivar.mm b/test/Rewriter/rewrite-modern-struct-ivar.mm
index 5ab02d4..09c02df 100644
--- a/test/Rewriter/rewrite-modern-struct-ivar.mm
+++ b/test/Rewriter/rewrite-modern-struct-ivar.mm
@@ -21,4 +21,4 @@
 - (struct S) dMeth{ return struct_ivar; }
 @end
   
-// CHECK: return (*(struct S *)((char *)self + OBJC_IVAR_$_I_struct_ivar));
+// CHECK: return (*(struct S *)((char *)self + OBJC_IVAR_$_I$struct_ivar));