SymbolVendor: Move compile unit handling into the SymbolFile class

Summary:
SymbolFile classes are responsible for creating CompileUnit instances
and they already need to have a notion of the id<->CompileUnit mapping
(because of APIs like ParseCompileUnitAtIndex). However, the
SymbolVendor has remained as the thing responsible for caching created
units (which the SymbolFiles were calling via convoluted constructs like
"m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(...)").

This patch moves the responsibility of caching the units into the
SymbolFile class. It does this by moving the implementation of
SymbolVendor::{GetNumCompileUnits,GetCompileUnitAtIndex} into the
equivalent SymbolFile functions. The SymbolVendor functions become just
a passthrough much like the rest of SymbolVendor.

The original implementations of SymbolFile::GetNumCompileUnits is moved
to "CalculateNumCompileUnits", and are made protected, as the "Get"
function is the external api of the class.
SymbolFile::ParseCompileUnitAtIndex is made protected for the same
reason.

This is the first step in removing the SymbolVendor indirection, as
proposed in
<http://lists.llvm.org/pipermail/lldb-dev/2019-June/015071.html>. After
removing all interesting logic from the SymbolVendor class, I'll proceed
with removing the indirection itself.

Reviewers: clayborg, jingham, JDevlieghere

Subscribers: jdoerfert, lldb-commits

Differential Revision: https://reviews.llvm.org/D65089

llvm-svn: 366791
diff --git a/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp b/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
index 5e275e3..42a5e85 100644
--- a/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
+++ b/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
@@ -187,7 +187,7 @@
   return CompileUnits | Functions | LineTables;
 }
 
-uint32_t SymbolFileBreakpad::GetNumCompileUnits() {
+uint32_t SymbolFileBreakpad::CalculateNumCompileUnits() {
   ParseCUData();
   return m_cu_data->GetSize();
 }
@@ -218,7 +218,7 @@
                                              eLanguageTypeUnknown,
                                              /*is_optimized*/ eLazyBoolNo);
 
-  GetSymbolVendor().SetCompileUnitAtIndex(index, cu_sp);
+  SetCompileUnitAtIndex(index, cu_sp);
   return cu_sp;
 }
 
@@ -260,7 +260,7 @@
   if (idx == UINT32_MAX)
     return 0;
 
-  sc.comp_unit = GetSymbolVendor().GetCompileUnitAtIndex(idx).get();
+  sc.comp_unit = GetCompileUnitAtIndex(idx).get();
   SymbolContextItem result = eSymbolContextCompUnit;
   if (resolve_scope & eSymbolContextLineEntry) {
     if (sc.comp_unit->GetLineTable()->FindLineEntryByAddress(so_addr,
@@ -280,7 +280,7 @@
 
   uint32_t old_size = sc_list.GetSize();
   for (size_t i = 0, size = GetNumCompileUnits(); i < size; ++i) {
-    CompileUnit &cu = *GetSymbolVendor().GetCompileUnitAtIndex(i);
+    CompileUnit &cu = *GetCompileUnitAtIndex(i);
     cu.ResolveSymbolContext(file_spec, line, check_inlines,
                             /*exact*/ false, resolve_scope, sc_list);
   }
@@ -522,10 +522,6 @@
   return plan_sp;
 }
 
-SymbolVendor &SymbolFileBreakpad::GetSymbolVendor() {
-  return *m_obj_file->GetModule()->GetSymbolVendor();
-}
-
 addr_t SymbolFileBreakpad::GetBaseFileAddress() {
   return m_obj_file->GetModule()
       ->GetObjectFile()
diff --git a/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h b/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
index 8a0b764..6803636 100644
--- a/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
+++ b/lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
@@ -46,10 +46,6 @@
 
   // Compile Unit function calls
 
-  uint32_t GetNumCompileUnits() override;
-
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override {
     return lldb::eLanguageTypeUnknown;
   }
@@ -196,7 +192,9 @@
 
   };
 
-  SymbolVendor &GetSymbolVendor();
+  uint32_t CalculateNumCompileUnits() override;
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
+
   lldb::addr_t GetBaseFileAddress();
   void ParseFileRecords();
   void ParseCUData();
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index fb5a94f..3b6d8da 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -677,8 +677,7 @@
 
           dwarf_cu.SetUserData(cu_sp.get());
 
-          m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
-              dwarf_cu.GetID(), cu_sp);
+          SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
         }
       }
     }
@@ -715,7 +714,7 @@
   return m_lldb_cu_to_dwarf_unit[cu_idx];
 }
 
-uint32_t SymbolFileDWARF::GetNumCompileUnits() {
+uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
   DWARFDebugInfo *info = DebugInfo();
   if (!info)
     return 0;
@@ -3713,7 +3712,10 @@
 
 uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
 
-void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); }
+void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
+  SymbolFile::Dump(s);
+  m_index->Dump(s);
+}
 
 void SymbolFileDWARF::DumpClangAST(Stream &s) {
   TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
index 018af47..6daa060 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
@@ -95,10 +95,6 @@
 
   // Compile Unit function calls
 
-  uint32_t GetNumCompileUnits() override;
-
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType
   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
 
@@ -331,6 +327,10 @@
   bool DeclContextMatchesThisSymbolFile(
       const lldb_private::CompilerDeclContext *decl_ctx);
 
+  uint32_t CalculateNumCompileUnits() override;
+
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
+
   virtual DWARFUnit *
   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
index 8ec64db..e44ff5f 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
@@ -562,7 +562,7 @@
   return 0;
 }
 
-uint32_t SymbolFileDWARFDebugMap::GetNumCompileUnits() {
+uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() {
   InitOSO();
   return m_compile_unit_infos.size();
 }
@@ -585,9 +585,8 @@
                 eLanguageTypeUnknown, eLazyBoolCalculate);
 
         if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
-          // Let our symbol vendor know about this compile unit
-          m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
-              cu_idx, m_compile_unit_infos[cu_idx].compile_unit_sp);
+          SetCompileUnitAtIndex(cu_idx,
+                                m_compile_unit_infos[cu_idx].compile_unit_sp);
         }
       }
     }
@@ -1284,8 +1283,7 @@
                  cu_sp.get());
         } else {
           m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
-          m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
-              cu_idx, cu_sp);
+          SetCompileUnitAtIndex(cu_idx, cu_sp);
         }
       }
     }
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h
index afc6142..2cdebaf 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h
@@ -44,9 +44,6 @@
   void InitializeObject() override;
 
   // Compile Unit function calls
-  uint32_t GetNumCompileUnits() override;
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType
   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
 
@@ -174,6 +171,9 @@
   // Protected Member Functions
   void InitOSO();
 
+  uint32_t CalculateNumCompileUnits() override;
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
+
   static uint32_t GetOSOIndexFromUserID(lldb::user_id_t uid) {
     return (uint32_t)((uid >> 32ull) - 1ull);
   }
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
index c9fb1dc..9585b35 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
@@ -329,7 +329,7 @@
   m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index);
 }
 
-uint32_t SymbolFileNativePDB::GetNumCompileUnits() {
+uint32_t SymbolFileNativePDB::CalculateNumCompileUnits() {
   const DbiModuleList &modules = m_index->dbi().modules();
   uint32_t count = modules.getModuleCount();
   if (count == 0)
@@ -433,8 +433,7 @@
       std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs,
                                     toOpaqueUid(cci.m_id), lang, optimized);
 
-  m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
-      cci.m_id.modi, cu_sp);
+  SetCompileUnitAtIndex(cci.m_id.modi, cu_sp);
   return cu_sp;
 }
 
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
index 20daff2..88bd5de 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
@@ -68,13 +68,9 @@
 
   // Compile Unit function calls
 
-  uint32_t GetNumCompileUnits() override;
-
   void
   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
 
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType
   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
 
@@ -157,6 +153,9 @@
   void DumpClangAST(Stream &s) override;
 
 private:
+  uint32_t CalculateNumCompileUnits() override;
+
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
 
   size_t FindTypesByName(llvm::StringRef name, uint32_t max_matches,
                          TypeMap &types);
diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
index 17dfcda..c6c25fd 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -123,8 +123,7 @@
 }
 
 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
-    : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
-      m_cached_compile_unit_count(0) {}
+    : SymbolFile(object_file), m_session_up(), m_global_scope_up() {}
 
 SymbolFilePDB::~SymbolFilePDB() {}
 
@@ -191,33 +190,30 @@
   lldbassert(m_global_scope_up.get());
 }
 
-uint32_t SymbolFilePDB::GetNumCompileUnits() {
-  if (m_cached_compile_unit_count == 0) {
-    auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
-    if (!compilands)
-      return 0;
+uint32_t SymbolFilePDB::CalculateNumCompileUnits() {
+  auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
+  if (!compilands)
+    return 0;
 
-    // The linker could link *.dll (compiland language = LINK), or import
-    // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
-    // found as a child of the global scope (PDB executable). Usually, such
-    // compilands contain `thunk` symbols in which we are not interested for
-    // now. However we still count them in the compiland list. If we perform
-    // any compiland related activity, like finding symbols through
-    // llvm::pdb::IPDBSession methods, such compilands will all be searched
-    // automatically no matter whether we include them or not.
-    m_cached_compile_unit_count = compilands->getChildCount();
+  // The linker could link *.dll (compiland language = LINK), or import
+  // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
+  // found as a child of the global scope (PDB executable). Usually, such
+  // compilands contain `thunk` symbols in which we are not interested for
+  // now. However we still count them in the compiland list. If we perform
+  // any compiland related activity, like finding symbols through
+  // llvm::pdb::IPDBSession methods, such compilands will all be searched
+  // automatically no matter whether we include them or not.
+  uint32_t compile_unit_count = compilands->getChildCount();
 
-    // The linker can inject an additional "dummy" compilation unit into the
-    // PDB. Ignore this special compile unit for our purposes, if it is there.
-    // It is always the last one.
-    auto last_compiland_up =
-        compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
-    lldbassert(last_compiland_up.get());
-    std::string name = last_compiland_up->getName();
-    if (name == "* Linker *")
-      --m_cached_compile_unit_count;
-  }
-  return m_cached_compile_unit_count;
+  // The linker can inject an additional "dummy" compilation unit into the
+  // PDB. Ignore this special compile unit for our purposes, if it is there.
+  // It is always the last one.
+  auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1);
+  lldbassert(last_compiland_up.get());
+  std::string name = last_compiland_up->getName();
+  if (name == "* Linker *")
+    --compile_unit_count;
+  return compile_unit_count;
 }
 
 void SymbolFilePDB::GetCompileUnitIndex(
@@ -1698,8 +1694,7 @@
   if (index == UINT32_MAX)
     GetCompileUnitIndex(*compiland_up, index);
   lldbassert(index != UINT32_MAX);
-  m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
-                                                                    cu_sp);
+  SetCompileUnitAtIndex(index, cu_sp);
   return cu_sp;
 }
 
diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
index ba3099a..13bff40 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
+++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
@@ -48,10 +48,6 @@
 
   // Compile Unit function calls
 
-  uint32_t GetNumCompileUnits() override;
-
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType
   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
 
@@ -173,6 +169,10 @@
   };
   using SecContribsMap = std::map<uint32_t, std::vector<SecContribInfo>>;
 
+  uint32_t CalculateNumCompileUnits() override;
+
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
+
   lldb::CompUnitSP ParseCompileUnitForUID(uint32_t id,
                                           uint32_t index = UINT32_MAX);
 
@@ -245,7 +245,6 @@
   std::vector<lldb::TypeSP> m_builtin_types;
   std::unique_ptr<llvm::pdb::IPDBSession> m_session_up;
   std::unique_ptr<llvm::pdb::PDBSymbolExe> m_global_scope_up;
-  uint32_t m_cached_compile_unit_count;
 
   lldb_private::UniqueCStringMap<uint32_t> m_func_full_names;
   lldb_private::UniqueCStringMap<uint32_t> m_func_base_names;
diff --git a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
index a1b21e5..ae0499d 100644
--- a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
+++ b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
@@ -104,7 +104,7 @@
   return abilities;
 }
 
-uint32_t SymbolFileSymtab::GetNumCompileUnits() {
+uint32_t SymbolFileSymtab::CalculateNumCompileUnits() {
   // If we don't have any source file symbols we will just have one compile
   // unit for the entire object file
   if (m_source_indexes.empty())
diff --git a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.h b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.h
index bc9a531..98a8e00 100644
--- a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.h
+++ b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.h
@@ -37,10 +37,6 @@
   uint32_t CalculateAbilities() override;
 
   // Compile Unit function calls
-  uint32_t GetNumCompileUnits() override;
-
-  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
-
   lldb::LanguageType
   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
 
@@ -85,6 +81,10 @@
   uint32_t GetPluginVersion() override;
 
 protected:
+  uint32_t CalculateNumCompileUnits() override;
+
+  lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
+
   typedef std::map<lldb_private::ConstString, lldb::TypeSP> TypeMap;
 
   lldb_private::Symtab::IndexCollection m_source_indexes;