<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 30, 2016, at 3:27 PM, Reid Kleckner <<a href="mailto:rnk@google.com" class="">rnk@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">So, I always push for leading upper case variable names. Over time we've gotten to the point where the majority of the code that I read in clang follows this convention. You seem to be pushing the other direction. What do you think we should be doing?</div></div></blockquote><div><br class=""></div>IRGen has always been inconsistent about this, and my goal in IRGen is to make it consistently use leading lowercase.  I have always been up-front about this goal; the pervasive LLVM style where literally everything is uppercased is, as you say, dumb and unreadable, as well as creating massive spurious conflicts between types and variables.  IRGen already has enough readability problems from working with different layers with a ton of similarly-named types and concepts (and trust me, it's somehow worse in Swift's IRGen), as well as the monotonous verbosity of having a thing and lowering it to a different thing, over and over and over; it really does not need any extra disadvantages.  So while I won't reformat existing code for no reason, when I do find myself rewriting it substantially, I usually try to make that function use lowercase, at least for spelled-out names.</div><div><br class=""></div><div>If someone wrote a tool to mass-reformat IRGen to use consistent uppercase, I would complain — we'd be enforcing a convention that it seems everyone who contributes to LLVM has independently decided is bad but which nobody has the energy to fight for (but who would they be fighting?) — but in the end I'd apply it and endeavor to follow the rule consistently thenceforth.  But since significant amounts of IRGen do not use that convention already, I am not going to follow it.</div><div><br class=""></div><div>John.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div>I'll admit the LLVM naming conventions are dumb, but I just don't care anymore, and would rather have it all look the same.<div class=""><a href="http://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly" class="">http://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly</a></div><div class=""><br class=""></div><div class="">I only mention it because you're modifying code that was clearly in the leading uppercase camp to leading lower case, so you weren't fitting into an existing style common to some local source file.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Nov 30, 2016 at 12:19 PM, John McCall via cfe-commits <span dir="ltr" class=""><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank" class="">cfe-commits@lists.llvm.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rjmccall<br class="">
Date: Wed Nov 30 14:19:46 2016<br class="">
New Revision: 288269<br class="">
<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=288269&view=rev" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-<wbr class="">project?rev=288269&view=rev</a><br class="">
Log:<br class="">
Finish adopting ConstantInitBuilder in CGObjCGNU.  NFC.<br class="">
<br class="">
Modified:<br class="">
    cfe/trunk/lib/CodeGen/<wbr class="">CGObjCGNU.cpp<br class="">
    cfe/trunk/lib/CodeGen/<wbr class="">ConstantBuilder.h<br class="">
<br class="">
Modified: cfe/trunk/lib/CodeGen/<wbr class="">CGObjCGNU.cpp<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCGNU.cpp?rev=288269&r1=288268&r2=288269&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-<wbr class="">project/cfe/trunk/lib/CodeGen/<wbr class="">CGObjCGNU.cpp?rev=288269&r1=<wbr class="">288268&r2=288269&view=diff</a><br class="">
==============================<wbr class="">==============================<wbr class="">==================<br class="">
--- cfe/trunk/lib/CodeGen/<wbr class="">CGObjCGNU.cpp (original)<br class="">
+++ cfe/trunk/lib/CodeGen/<wbr class="">CGObjCGNU.cpp Wed Nov 30 14:19:46 2016<br class="">
@@ -164,9 +164,8 @@ protected:<br class="">
   /// Helper function that generates a constant string and returns a pointer to<br class="">
   /// the start of the string.  The result of this function can be used anywhere<br class="">
   /// where the C code specifies const char*.<br class="">
-  llvm::Constant *MakeConstantString(const std::string &Str,<br class="">
-                                     const std::string &Name="") {<br class="">
-    ConstantAddress Array = CGM.GetAddrOfConstantCString(<wbr class="">Str, Name.c_str());<br class="">
+  llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") {<br class="">
+    ConstantAddress Array = CGM.GetAddrOfConstantCString(<wbr class="">Str, Name);<br class="">
     return llvm::ConstantExpr::<wbr class="">getGetElementPtr(Array.<wbr class="">getElementType(),<br class="">
                                                 Array.getPointer(), Zeros);<br class="">
   }<br class="">
@@ -1530,23 +1529,24 @@ GenerateMethodList(StringRef ClassName,<br class="">
   MethodList.addInt(Int32Ty, MethodTypes.size());<br class="">
<br class="">
   // Get the method structure type.<br class="">
-  llvm::StructType *ObjCMethodTy = llvm::StructType::get(<br class="">
-    PtrToInt8Ty, // Really a selector, but the runtime creates it us.<br class="">
-    PtrToInt8Ty, // Method types<br class="">
-    IMPTy, //Method pointer<br class="">
-    nullptr);<br class="">
+  llvm::StructType *ObjCMethodTy =<br class="">
+    llvm::StructType::get(CGM.<wbr class="">getLLVMContext(), {<br class="">
+      PtrToInt8Ty, // Really a selector, but the runtime creates it us.<br class="">
+      PtrToInt8Ty, // Method types<br class="">
+      IMPTy        // Method pointer<br class="">
+    });<br class="">
   auto Methods = MethodList.beginArray();<br class="">
   for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {<br class="">
-    llvm::Constant *Method =<br class="">
+    llvm::Constant *FnPtr =<br class="">
       TheModule.getFunction(<wbr class="">SymbolNameForMethod(ClassName, CategoryName,<br class="">
                                                 MethodSels[i],<br class="">
                                                 isClassMethodList));<br class="">
-    assert(Method && "Can't generate metadata for method that doesn't exist");<br class="">
-    llvm::Constant *C = MakeConstantString(MethodSels[<wbr class="">i].getAsString());<br class="">
-    Method = llvm::ConstantExpr::<wbr class="">getBitCast(Method,<br class="">
-        IMPTy);<br class="">
-    Methods.add(<br class="">
-        llvm::ConstantStruct::get(<wbr class="">ObjCMethodTy, {C, MethodTypes[i], Method}));<br class="">
+    assert(FnPtr && "Can't generate metadata for method that doesn't exist");<br class="">
+    auto Method = Methods.beginStruct(<wbr class="">ObjCMethodTy);<br class="">
+    Method.add(MakeConstantString(<wbr class="">MethodSels[i].getAsString()));<br class="">
+    Method.add(MethodTypes[i]);<br class="">
+    Method.addBitCast(FnPtr, IMPTy);<br class="">
+    Method.finishAndAddTo(Methods)<wbr class="">;<br class="">
   }<br class="">
   Methods.finishAndAddTo(<wbr class="">MethodList);<br class="">
<br class="">
@@ -1644,7 +1644,7 @@ llvm::Constant *CGObjCGNU::GenerateClass<br class="">
   // Fill in the structure<br class="">
<br class="">
   // isa<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<wbr class="">MetaClass, PtrToInt8Ty));<br class="">
+  Elements.addBitCast(MetaClass, PtrToInt8Ty);<br class="">
   // super_class<br class="">
   Elements.add(SuperClass);<br class="">
   // name<br class="">
@@ -1673,7 +1673,7 @@ llvm::Constant *CGObjCGNU::GenerateClass<br class="">
   // sibling_class<br class="">
   Elements.add(NULLPtr);<br class="">
   // protocols<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<wbr class="">Protocols, PtrTy));<br class="">
+  Elements.addBitCast(Protocols, PtrTy);<br class="">
   // gc_object_type<br class="">
   Elements.add(NULLPtr);<br class="">
   // abi_version<br class="">
@@ -1746,9 +1746,7 @@ CGObjCGNU::<wbr class="">GenerateProtocolList(ArrayRef<br class="">
     } else {<br class="">
       protocol = value->getValue();<br class="">
     }<br class="">
-    llvm::Constant *Ptr = llvm::ConstantExpr::<wbr class="">getBitCast(protocol,<br class="">
-                                                           PtrToInt8Ty);<br class="">
-    Elements.add(Ptr);<br class="">
+    Elements.addBitCast(protocol, PtrToInt8Ty);<br class="">
   }<br class="">
   Elements.finishAndAddTo(<wbr class="">ProtocolList);<br class="">
   return ProtocolList.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">protocol_list",<br class="">
@@ -1959,11 +1957,11 @@ void CGObjCGNU::<wbr class="">GenerateProtocolHolderCa<br class="">
   Elements.add(<wbr class="">MakeConstantString(<wbr class="">CategoryName));<br class="">
   Elements.add(<wbr class="">MakeConstantString(ClassName))<wbr class="">;<br class="">
   // Instance method list<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<wbr class="">GenerateMethodList(<br class="">
-          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));<br class="">
+  Elements.addBitCast(<wbr class="">GenerateMethodList(<br class="">
+          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy);<br class="">
   // Class method list<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<wbr class="">GenerateMethodList(<br class="">
-          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));<br class="">
+  Elements.addBitCast(<wbr class="">GenerateMethodList(<br class="">
+          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy);<br class="">
<br class="">
   // Protocol list<br class="">
   ConstantInitBuilder ProtocolListBuilder(CGM);<br class="">
@@ -1973,15 +1971,13 @@ void CGObjCGNU::<wbr class="">GenerateProtocolHolderCa<br class="">
   auto ProtocolElements = ProtocolList.beginArray(PtrTy)<wbr class="">;<br class="">
   for (auto iter = ExistingProtocols.begin(), endIter = ExistingProtocols.end();<br class="">
        iter != endIter ; iter++) {<br class="">
-    llvm::Constant *Ptr = llvm::ConstantExpr::<wbr class="">getBitCast(iter->getValue(),<br class="">
-            PtrTy);<br class="">
-    ProtocolElements.add(Ptr);<br class="">
+    ProtocolElements.addBitCast(<wbr class="">iter->getValue(), PtrTy);<br class="">
   }<br class="">
   ProtocolElements.<wbr class="">finishAndAddTo(ProtocolList);<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
+  Elements.addBitCast(<br class="">
                    ProtocolList.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">protocol_list",<br class="">
                                                       CGM.getPointerAlign()),<br class="">
-                   PtrTy));<br class="">
+                   PtrTy);<br class="">
   Categories.push_back(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
         Elements.<wbr class="">finishAndCreateGlobal("", CGM.getPointerAlign()),<br class="">
         PtrTy));<br class="">
@@ -2066,18 +2062,17 @@ void CGObjCGNU::GenerateCategory(<wbr class="">const O<br class="">
   Elements.add(<wbr class="">MakeConstantString(<wbr class="">CategoryName));<br class="">
   Elements.add(<wbr class="">MakeConstantString(ClassName))<wbr class="">;<br class="">
   // Instance method list<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
+  Elements.addBitCast(<br class="">
           GenerateMethodList(ClassName, CategoryName, InstanceMethodSels,<br class="">
                              InstanceMethodTypes, false),<br class="">
-          PtrTy));<br class="">
+          PtrTy);<br class="">
   // Class method list<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
-                 GenerateMethodList(ClassName, CategoryName,<br class="">
-                                    ClassMethodSels, ClassMethodTypes, true),<br class="">
-                  PtrTy));<br class="">
+  Elements.addBitCast(<br class="">
+          GenerateMethodList(ClassName, CategoryName, ClassMethodSels,<br class="">
+                             ClassMethodTypes, true),<br class="">
+          PtrTy);<br class="">
   // Protocol list<br class="">
-  Elements.add(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
-                 GenerateProtocolList(<wbr class="">Protocols), PtrTy));<br class="">
+  Elements.addBitCast(<wbr class="">GenerateProtocolList(<wbr class="">Protocols), PtrTy);<br class="">
   Categories.push_back(llvm::<wbr class="">ConstantExpr::getBitCast(<br class="">
         Elements.<wbr class="">finishAndCreateGlobal("", CGM.getPointerAlign()),<br class="">
         PtrTy));<br class="">
@@ -2406,173 +2401,179 @@ llvm::Function *CGObjCGNU::ModuleInitFun<br class="">
   // Add all referenced protocols to a category.<br class="">
   GenerateProtocolHolderCategory<wbr class="">();<br class="">
<br class="">
-  llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(<br class="">
-          SelectorTy->getElementType());<br class="">
-  llvm::Type *SelStructPtrTy = SelectorTy;<br class="">
-  if (!SelStructTy) {<br class="">
-    SelStructTy = llvm::StructType::get(<wbr class="">PtrToInt8Ty, PtrToInt8Ty, nullptr);<br class="">
-    SelStructPtrTy = llvm::PointerType::getUnqual(<wbr class="">SelStructTy);<br class="">
+  llvm::StructType *selStructTy =<br class="">
+    dyn_cast<llvm::StructType>(<wbr class="">SelectorTy->getElementType());<br class="">
+  llvm::Type *selStructPtrTy = SelectorTy;<br class="">
+  if (!selStructTy) {<br class="">
+    selStructTy = llvm::StructType::get(CGM.<wbr class="">getLLVMContext(),<br class="">
+                                        { PtrToInt8Ty, PtrToInt8Ty });<br class="">
+    selStructPtrTy = llvm::PointerType::getUnqual(<wbr class="">selStructTy);<br class="">
   }<br class="">
<br class="">
-  std::vector<llvm::Constant*> Elements;<br class="">
-  llvm::Constant *Statics = NULLPtr;<br class="">
   // Generate statics list:<br class="">
+  llvm::Constant *statics = NULLPtr;<br class="">
   if (!ConstantStrings.empty()) {<br class="">
-    llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(<wbr class="">PtrToInt8Ty,<br class="">
-        ConstantStrings.size() + 1);<br class="">
-    ConstantStrings.push_back(<wbr class="">NULLPtr);<br class="">
-<br class="">
-    StringRef StringClass = CGM.getLangOpts().<wbr class="">ObjCConstantStringClass;<br class="">
-<br class="">
-    if (StringClass.empty()) StringClass = "NXConstantString";<br class="">
-<br class="">
-    Elements.push_back(<wbr class="">MakeConstantString(<wbr class="">StringClass,<br class="">
-                                          ".objc_static_class_name"));<br class="">
-    Elements.push_back(llvm::<wbr class="">ConstantArray::get(<wbr class="">StaticsArrayTy,<br class="">
-                                                ConstantStrings));<br class="">
-    Statics = MakeGlobal(llvm::<wbr class="">ConstantStruct::getAnon(<wbr class="">Elements),<br class="">
-                         CGM.getPointerAlign(), ".objc_statics");<br class="">
-    llvm::Type *StaticsListPtrTy = Statics->getType();<br class="">
-    llvm::ArrayType *StaticsListArrayTy =<br class="">
-      llvm::ArrayType::get(<wbr class="">StaticsListPtrTy, 2);<br class="">
-    Elements.clear();<br class="">
-    Elements.push_back(Statics);<br class="">
-    Elements.push_back(llvm::<wbr class="">Constant::getNullValue(<wbr class="">StaticsListPtrTy));<br class="">
-    Statics = MakeGlobal(llvm::<wbr class="">ConstantArray::get(<wbr class="">StaticsListArrayTy, Elements),<br class="">
-                         CGM.getPointerAlign(), ".objc_statics_ptr");<br class="">
-    Statics = llvm::ConstantExpr::<wbr class="">getBitCast(Statics, PtrTy);<br class="">
-  }<br class="">
-  // Array of classes, categories, and constant objects<br class="">
-  llvm::ArrayType *ClassListTy = llvm::ArrayType::get(<wbr class="">PtrToInt8Ty,<br class="">
-      Classes.size() + Categories.size()  + 2);<br class="">
-  llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,<br class="">
-                                                     llvm::Type::getInt16Ty(<wbr class="">VMContext),<br class="">
-                                                     llvm::Type::getInt16Ty(<wbr class="">VMContext),<br class="">
-                                                     ClassListTy, nullptr);<br class="">
+    llvm::GlobalVariable *fileStatics = [&] {<br class="">
+      ConstantInitBuilder builder(CGM);<br class="">
+      auto staticsStruct = builder.beginStruct();<br class="">
+<br class="">
+      StringRef stringClass = CGM.getLangOpts().<wbr class="">ObjCConstantStringClass;<br class="">
+      if (stringClass.empty()) stringClass = "NXConstantString";<br class="">
+      staticsStruct.add(<wbr class="">MakeConstantString(<wbr class="">stringClass,<br class="">
+                                           ".objc_static_class_name"));<br class="">
+<br class="">
+      auto array = staticsStruct.beginArray();<br class="">
+      array.addAll(ConstantStrings);<br class="">
+      array.add(NULLPtr);<br class="">
+      array.finishAndAddTo(<wbr class="">staticsStruct);<br class="">
+<br class="">
+      return staticsStruct.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">statics",<br class="">
+                                                 CGM.getPointerAlign());<br class="">
+    }();<br class="">
+<br class="">
+    ConstantInitBuilder builder(CGM);<br class="">
+    auto allStaticsArray = builder.beginArray(<wbr class="">fileStatics->getType());<br class="">
+    allStaticsArray.add(<wbr class="">fileStatics);<br class="">
+    allStaticsArray.<wbr class="">addNullPointer(fileStatics-><wbr class="">getType());<br class="">
+<br class="">
+    statics = allStaticsArray.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">statics_ptr",<br class="">
+                                                    CGM.getPointerAlign());<br class="">
+    statics = llvm::ConstantExpr::<wbr class="">getBitCast(statics, PtrTy);<br class="">
+  }<br class="">
+<br class="">
+  // Array of classes, categories, and constant objects.<br class="">
+<br class="">
+  SmallVector<llvm::GlobalAlias*<wbr class="">, 16> selectorAliases;<br class="">
+  unsigned selectorCount;<br class="">
<br class="">
-  Elements.clear();<br class="">
   // Pointer to an array of selectors used in this module.<br class="">
-  ConstantInitBuilder SelectorBuilder(CGM);<br class="">
-  auto Selectors = SelectorBuilder.beginArray(<wbr class="">SelStructTy);<br class="">
-  std::vector<llvm::GlobalAlias*<wbr class="">> SelectorAliases;<br class="">
-  for (SelectorMap::iterator iter = SelectorTable.begin(),<br class="">
-      iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {<br class="">
-<br class="">
-    std::string SelNameStr = iter->first.getAsString();<br class="">
-    llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");<br class="">
-<br class="">
-    SmallVectorImpl<TypedSelector> &Types = iter->second;<br class="">
-    for (SmallVectorImpl<<wbr class="">TypedSelector>::iterator i = Types.begin(),<br class="">
-        e = Types.end() ; i!=e ; i++) {<br class="">
-<br class="">
-      llvm::Constant *SelectorTypeEncoding = NULLPtr;<br class="">
-      if (!i->first.empty())<br class="">
-        SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");<br class="">
-<br class="">
-      auto SelStruct = Selectors.beginStruct(<wbr class="">SelStructTy);<br class="">
-      SelStruct.add(SelName);<br class="">
-      SelStruct.add(<wbr class="">SelectorTypeEncoding);<br class="">
-      SelStruct.finishAndAddTo(<wbr class="">Selectors);<br class="">
-<br class="">
-      // Store the selector alias for later replacement<br class="">
-      SelectorAliases.push_back(i-><wbr class="">second);<br class="">
-    }<br class="">
-  }<br class="">
-  unsigned SelectorCount = Selectors.size();<br class="">
-  // NULL-terminate the selector list.  This should not actually be required,<br class="">
-  // because the selector list has a length field.  Unfortunately, the GCC<br class="">
-  // runtime decides to ignore the length field and expects a NULL terminator,<br class="">
-  // and GCC cooperates with this by always setting the length to 0.<br class="">
-  {<br class="">
-    auto SelStruct = Selectors.beginStruct(<wbr class="">SelStructTy);<br class="">
-    SelStruct.add(NULLPtr);<br class="">
-    SelStruct.add(NULLPtr);<br class="">
-    SelStruct.finishAndAddTo(<wbr class="">Selectors);<br class="">
-  }<br class="">
+  llvm::GlobalVariable *selectorList = [&] {<br class="">
+    ConstantInitBuilder builder(CGM);<br class="">
+    auto selectors = builder.beginArray(<wbr class="">selStructTy);<br class="">
+    for (auto &entry : SelectorTable) {<br class="">
+<br class="">
+      std::string selNameStr = entry.first.getAsString();<br class="">
+      llvm::Constant *selName = ExportUniqueString(selNameStr, ".objc_sel_name");<br class="">
+<br class="">
+      for (TypedSelector &sel : entry.second) {<br class="">
+        llvm::Constant *selectorTypeEncoding = NULLPtr;<br class="">
+        if (!sel.first.empty())<br class="">
+          selectorTypeEncoding =<br class="">
+            MakeConstantString(sel.first, ".objc_sel_types");<br class="">
+<br class="">
+        auto selStruct = selectors.beginStruct(<wbr class="">selStructTy);<br class="">
+        selStruct.add(selName);<br class="">
+        selStruct.add(<wbr class="">selectorTypeEncoding);<br class="">
+        selStruct.finishAndAddTo(<wbr class="">selectors);<br class="">
+<br class="">
+        // Store the selector alias for later replacement<br class="">
+        selectorAliases.push_back(sel.<wbr class="">second);<br class="">
+      }<br class="">
+    }<br class="">
<br class="">
-  // Number of static selectors<br class="">
-  Elements.push_back(llvm::<wbr class="">ConstantInt::get(LongTy, SelectorCount));<br class="">
-  llvm::GlobalVariable *SelectorList =<br class="">
-      Selectors.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">selector_list",<br class="">
-                                      CGM.getPointerAlign());<br class="">
-  Elements.push_back(llvm::<wbr class="">ConstantExpr::getBitCast(<wbr class="">SelectorList,<br class="">
-    SelStructPtrTy));<br class="">
+    // Remember the number of entries in the selector table.<br class="">
+    selectorCount = selectors.size();<br class="">
<br class="">
-  // Now that all of the static selectors exist, create pointers to them.<br class="">
-  for (unsigned int i=0 ; i<SelectorCount ; i++) {<br class="">
+    // NULL-terminate the selector list.  This should not actually be required,<br class="">
+    // because the selector list has a length field.  Unfortunately, the GCC<br class="">
+    // runtime decides to ignore the length field and expects a NULL terminator,<br class="">
+    // and GCC cooperates with this by always setting the length to 0.<br class="">
+    auto selStruct = selectors.beginStruct(<wbr class="">selStructTy);<br class="">
+    selStruct.add(NULLPtr);<br class="">
+    selStruct.add(NULLPtr);<br class="">
+    selStruct.finishAndAddTo(<wbr class="">selectors);<br class="">
<br class="">
-    llvm::Constant *Idxs[] = {Zeros[0],<br class="">
-      llvm::ConstantInt::get(<wbr class="">Int32Ty, i), Zeros[0]};<br class="">
+    return selectors.<wbr class="">finishAndCreateGlobal(".objc_<wbr class="">selector_list",<br class="">
+                                           CGM.getPointerAlign());<br class="">
+  }();<br class="">
+<br class="">
+  // Now that all of the static selectors exist, create pointers to them.<br class="">
+  for (unsigned i = 0; i < selectorCount; ++i) {<br class="">
+    llvm::Constant *idxs[] = {<br class="">
+      Zeros[0],<br class="">
+      llvm::ConstantInt::get(<wbr class="">Int32Ty, i)<br class="">
+    };<br class="">
     // FIXME: We're generating redundant loads and stores here!<br class="">
-    llvm::Constant *SelPtr = llvm::ConstantExpr::<wbr class="">getGetElementPtr(<br class="">
-        SelectorList->getValueType(), SelectorList, makeArrayRef(Idxs, 2));<br class="">
+    llvm::Constant *selPtr = llvm::ConstantExpr::<wbr class="">getGetElementPtr(<br class="">
+        selectorList->getValueType(), selectorList, idxs);<br class="">
     // If selectors are defined as an opaque type, cast the pointer to this<br class="">
     // type.<br class="">
-    SelPtr = llvm::ConstantExpr::<wbr class="">getBitCast(SelPtr, SelectorTy);<br class="">
-    SelectorAliases[i]-><wbr class="">replaceAllUsesWith(SelPtr);<br class="">
-    SelectorAliases[i]-><wbr class="">eraseFromParent();<br class="">
-  }<br class="">
-<br class="">
-  // Number of classes defined.<br class="">
-  Elements.push_back(llvm::<wbr class="">ConstantInt::get(llvm::Type::<wbr class="">getInt16Ty(VMContext),<br class="">
-        Classes.size()));<br class="">
-  // Number of categories defined<br class="">
-  Elements.push_back(llvm::<wbr class="">ConstantInt::get(llvm::Type::<wbr class="">getInt16Ty(VMContext),<br class="">
-        Categories.size()));<br class="">
-  // Create an array of classes, then categories, then static object instances<br class="">
-  Classes.insert(Classes.end(), Categories.begin(), Categories.end());<br class="">
-  //  NULL-terminated list of static object instances (mainly constant strings)<br class="">
-  Classes.push_back(Statics);<br class="">
-  Classes.push_back(NULLPtr);<br class="">
-  llvm::Constant *ClassList = llvm::ConstantArray::get(<wbr class="">ClassListTy, Classes);<br class="">
-  Elements.push_back(ClassList);<br class="">
-  // Construct the symbol table<br class="">
-  llvm::Constant *SymTab =<br class="">
-    MakeGlobal(llvm::<wbr class="">ConstantStruct::get(SymTabTy, Elements),<br class="">
-               CGM.getPointerAlign());<br class="">
+    selPtr = llvm::ConstantExpr::<wbr class="">getBitCast(selPtr, SelectorTy);<br class="">
+    selectorAliases[i]-><wbr class="">replaceAllUsesWith(selPtr);<br class="">
+    selectorAliases[i]-><wbr class="">eraseFromParent();<br class="">
+  }<br class="">
+<br class="">
+  llvm::GlobalVariable *symtab = [&] {<br class="">
+    ConstantInitBuilder builder(CGM);<br class="">
+    auto symtab = builder.beginStruct();<br class="">
+<br class="">
+    // Number of static selectors<br class="">
+    symtab.addInt(LongTy, selectorCount);<br class="">
+<br class="">
+    symtab.addBitCast(<wbr class="">selectorList, selStructPtrTy);<br class="">
+<br class="">
+    // Number of classes defined.<br class="">
+    symtab.addInt(CGM.Int16Ty, Classes.size());<br class="">
+    // Number of categories defined<br class="">
+    symtab.addInt(CGM.Int16Ty, Categories.size());<br class="">
+<br class="">
+    // Create an array of classes, then categories, then static object instances<br class="">
+    auto classList = symtab.beginArray(PtrToInt8Ty)<wbr class="">;<br class="">
+    classList.addAll(Classes);<br class="">
+    classList.addAll(Categories);<br class="">
+    //  NULL-terminated list of static object instances (mainly constant strings)<br class="">
+    classList.add(statics);<br class="">
+    classList.add(NULLPtr);<br class="">
+    classList.finishAndAddTo(<wbr class="">symtab);<br class="">
+<br class="">
+    // Construct the symbol table.<br class="">
+    return symtab.finishAndCreateGlobal("<wbr class="">", CGM.getPointerAlign());<br class="">
+  }();<br class="">
<br class="">
   // The symbol table is contained in a module which has some version-checking<br class="">
   // constants<br class="">
-  llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,<br class="">
-      PtrToInt8Ty, llvm::PointerType::getUnqual(<wbr class="">SymTabTy),<br class="">
-      (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr);<br class="">
-  Elements.clear();<br class="">
-  // Runtime version, used for ABI compatibility checking.<br class="">
-  Elements.push_back(llvm::<wbr class="">ConstantInt::get(LongTy, RuntimeVersion));<br class="">
-  // sizeof(ModuleTy)<br class="">
-  llvm::DataLayout td(&TheModule);<br class="">
-  Elements.push_back(<br class="">
-    llvm::ConstantInt::get(LongTy,<br class="">
-                           td.getTypeSizeInBits(ModuleTy) /<br class="">
-                             CGM.getContext().getCharWidth(<wbr class="">)));<br class="">
-<br class="">
-  // The path to the source file where this module was declared<br class="">
-  SourceManager &SM = CGM.getContext().<wbr class="">getSourceManager();<br class="">
-  const FileEntry *mainFile = SM.getFileEntryForID(SM.<wbr class="">getMainFileID());<br class="">
-  std::string path =<br class="">
+  llvm::Constant *module = [&] {<br class="">
+    llvm::Type *moduleEltTys[] = {<br class="">
+      LongTy, LongTy, PtrToInt8Ty, symtab->getType(), IntTy<br class="">
+    };<br class="">
+    llvm::StructType *moduleTy =<br class="">
+      llvm::StructType::get(CGM.<wbr class="">getLLVMContext(),<br class="">
+         makeArrayRef(moduleEltTys).<wbr class="">drop_back(unsigned(<wbr class="">RuntimeVersion < 10)));<br class="">
+<br class="">
+    ConstantInitBuilder builder(CGM);<br class="">
+    auto module = builder.beginStruct(moduleTy);<br class="">
+    // Runtime version, used for ABI compatibility checking.<br class="">
+    module.addInt(LongTy, RuntimeVersion);<br class="">
+    // sizeof(ModuleTy)<br class="">
+    module.addInt(LongTy, CGM.getDataLayout().<wbr class="">getTypeStoreSize(moduleTy));<br class="">
+<br class="">
+    // The path to the source file where this module was declared<br class="">
+    SourceManager &SM = CGM.getContext().<wbr class="">getSourceManager();<br class="">
+    const FileEntry *mainFile = SM.getFileEntryForID(SM.<wbr class="">getMainFileID());<br class="">
+    std::string path =<br class="">
       (Twine(mainFile->getDir()-><wbr class="">getName()) + "/" + mainFile->getName()).str();<br class="">
-  Elements.push_back(<wbr class="">MakeConstantString(path, ".objc_source_file_name"));<br class="">
-  Elements.push_back(SymTab);<br class="">
+    module.add(MakeConstantString(<wbr class="">path, ".objc_source_file_name"));<br class="">
+    module.add(symtab);<br class="">
<br class="">
-  if (RuntimeVersion >= 10)<br class="">
-    switch (CGM.getLangOpts().getGC()) {<br class="">
+    if (RuntimeVersion >= 10) {<br class="">
+      switch (CGM.getLangOpts().getGC()) {<br class="">
       case LangOptions::GCOnly:<br class="">
-        Elements.push_back(llvm::<wbr class="">ConstantInt::get(IntTy, 2));<br class="">
+        module.addInt(IntTy, 2);<br class="">
         break;<br class="">
       case LangOptions::NonGC:<br class="">
         if (CGM.getLangOpts().<wbr class="">ObjCAutoRefCount)<br class="">
-          Elements.push_back(llvm::<wbr class="">ConstantInt::get(IntTy, 1));<br class="">
+          module.addInt(IntTy, 1);<br class="">
         else<br class="">
-          Elements.push_back(llvm::<wbr class="">ConstantInt::get(IntTy, 0));<br class="">
+          module.addInt(IntTy, 0);<br class="">
         break;<br class="">
       case LangOptions::HybridGC:<br class="">
-          Elements.push_back(llvm::<wbr class="">ConstantInt::get(IntTy, 1));<br class="">
+        module.addInt(IntTy, 1);<br class="">
         break;<br class="">
+      }<br class="">
     }<br class="">
<br class="">
-  llvm::Value *Module =<br class="">
-    MakeGlobal(llvm::<wbr class="">ConstantStruct::get(ModuleTy, Elements),<br class="">
-               CGM.getPointerAlign());<br class="">
+    return module.finishAndCreateGlobal("<wbr class="">", CGM.getPointerAlign());<br class="">
+  }();<br class="">
<br class="">
   // Create the load function calling the runtime entry point with the module<br class="">
   // structure<br class="">
@@ -2586,10 +2587,9 @@ llvm::Function *CGObjCGNU::ModuleInitFun<br class="">
   Builder.SetInsertPoint(<wbr class="">EntryBB);<br class="">
<br class="">
   llvm::FunctionType *FT =<br class="">
-    llvm::FunctionType::get(<wbr class="">Builder.getVoidTy(),<br class="">
-                            llvm::PointerType::getUnqual(<wbr class="">ModuleTy), true);<br class="">
+    llvm::FunctionType::get(<wbr class="">Builder.getVoidTy(), module->getType(), true);<br class="">
   llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");<br class="">
-  Builder.CreateCall(Register, Module);<br class="">
+  Builder.CreateCall(Register, module);<br class="">
<br class="">
   if (!ClassAliases.empty()) {<br class="">
     llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};<br class="">
<br class="">
Modified: cfe/trunk/lib/CodeGen/<wbr class="">ConstantBuilder.h<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/ConstantBuilder.h?rev=288269&r1=288268&r2=288269&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-<wbr class="">project/cfe/trunk/lib/CodeGen/<wbr class="">ConstantBuilder.h?rev=288269&<wbr class="">r1=288268&r2=288269&view=diff</a><br class="">
==============================<wbr class="">==============================<wbr class="">==================<br class="">
--- cfe/trunk/lib/CodeGen/<wbr class="">ConstantBuilder.h (original)<br class="">
+++ cfe/trunk/lib/CodeGen/<wbr class="">ConstantBuilder.h Wed Nov 30 14:19:46 2016<br class="">
@@ -155,6 +155,13 @@ public:<br class="">
       add(llvm::ConstantExpr::<wbr class="">getBitCast(value, type));<br class="">
     }<br class="">
<br class="">
+    /// Add a bunch of new values to this initializer.<br class="">
+    void addAll(ArrayRef<llvm::Constant *> values) {<br class="">
+      assert(!Finished && "cannot add more values after finishing builder");<br class="">
+      assert(!Frozen && "cannot add values while subbuilder is active");<br class="">
+      Builder.Buffer.append(values.<wbr class="">begin(), values.end());<br class="">
+    }<br class="">
+<br class="">
     /// An opaque class to hold the abstract position of a placeholder.<br class="">
     class PlaceholderPosition {<br class="">
       size_t Index;<br class="">
<br class="">
<br class="">
______________________________<wbr class="">_________________<br class="">
cfe-commits mailing list<br class="">
<a href="mailto:cfe-commits@lists.llvm.org" class="">cfe-commits@lists.llvm.org</a><br class="">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/<wbr class="">mailman/listinfo/cfe-commits</a><br class="">
</blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>