Refine vtable layout for virtual bases and keep better track of
primaries.  WIP.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78950 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp
index 2940f18..24e87f5 100644
--- a/lib/CodeGen/CGCXX.cpp
+++ b/lib/CodeGen/CGCXX.cpp
@@ -617,10 +617,10 @@
   llvm::GlobalVariable::LinkageTypes linktype;
   linktype = llvm::GlobalValue::WeakAnyLinkage;
   std::vector<llvm::Constant *> info;
-  // assert (0 && "FIXME: implement rtti descriptor");
+  // assert(0 && "FIXME: implement rtti descriptor");
   // FIXME: descriptor
   info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
-  // assert (0 && "FIXME: implement rtti ts");
+  // assert(0 && "FIXME: implement rtti ts");
   // FIXME: TS
   info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
 
@@ -666,6 +666,25 @@
   }
 }
 
+void CodeGenFunction::GenerateVtableForVBases(const CXXRecordDecl *RD,
+                                              llvm::Constant *rtti,
+                                         std::vector<llvm::Constant *> &methods,
+                   llvm::SmallSet<const CXXRecordDecl *, 32> &IndirectPrimary) {
+  for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
+         e = RD->bases_end(); i != e; ++i) {
+    const CXXRecordDecl *Base = 
+      cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
+    if (i->isVirtual() && !IndirectPrimary.count(Base)) {
+      // Mark it so we don't output it twice.
+      IndirectPrimary.insert(Base);
+      GenerateVtableForBase(Base, RD, rtti, methods, false, true,
+                            IndirectPrimary);
+    }
+    if (Base->getNumVBases())
+      GenerateVtableForVBases(Base, rtti, methods, IndirectPrimary);
+  }
+}
+
 void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD,
                                             const CXXRecordDecl *Class,
                                             llvm::Constant *rtti,
@@ -776,15 +795,8 @@
                             IndirectPrimary);
   }
 
-  // FIXME: Though complete, this is the wrong order
-  for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(),
-         e = RD->vbases_end(); i != e; ++i) {
-    const CXXRecordDecl *Base = 
-      cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
-    if (!IndirectPrimary.count(Base))
-      GenerateVtableForBase(Base, RD, rtti, methods, false, true,
-                            IndirectPrimary);
-  }
+  // Then come the vtables for all the virtual bases.
+  GenerateVtableForVBases(RD, rtti, methods, IndirectPrimary);
 
   llvm::Constant *C;
   llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h
index 1ea19c6..aa4bcce 100644
--- a/lib/CodeGen/CodeGenFunction.h
+++ b/lib/CodeGen/CodeGenFunction.h
@@ -363,6 +363,10 @@
                       const CXXRecordDecl *RD, llvm::Type *Ptr8Ty);
   void GenerateMethods(std::vector<llvm::Constant *> &methods,
                        const CXXRecordDecl *RD, llvm::Type *Ptr8Ty);
+void GenerateVtableForVBases(const CXXRecordDecl *RD,
+                             llvm::Constant *rtti,
+                             std::vector<llvm::Constant *> &methods,
+                    llvm::SmallSet<const CXXRecordDecl *, 32> &IndirectPrimary);
   void GenerateVtableForBase(const CXXRecordDecl *RD,
                              const CXXRecordDecl *Class,
                              llvm::Constant *rtti,