Add a pointer to a source file to SymbolBody.

Previously, each subclass of SymbolBody had a pointer to a source
file from which it was created. So, there was no single way to get
a source file for a symbol. We had getSourceFile<ELFT>(), but the
function was a bit inconvenient as it's a template.

This patch makes SymbolBody have a pointer to a source file.
If a symbol is not created from a file, the pointer has a nullptr.

llvm-svn: 275701
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 0c3a116..7c9c748 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -142,18 +142,6 @@
   return false;
 }
 
-template <class ELFT> InputFile *SymbolBody::getSourceFile() {
-  if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
-    return S->Section ? S->Section->getFile() : nullptr;
-  if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
-    return S->File;
-  if (auto *S = dyn_cast<DefinedBitcode>(this))
-    return S->File;
-  if (auto *S = dyn_cast<Undefined>(this))
-    return S->File;
-  return nullptr;
-}
-
 template <class ELFT>
 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
   typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
@@ -207,17 +195,25 @@
 
 DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
                                BitcodeFile *F)
-    : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
+    : Defined(DefinedBitcodeKind, Name, StOther, Type) {
+  this->File = F;
+}
 
 bool DefinedBitcode::classof(const SymbolBody *S) {
   return S->kind() == DefinedBitcodeKind;
 }
 
-Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
-    : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
+Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
+                     InputFile *File)
+    : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
+  this->File = File;
+}
 
-Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
-    : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
+Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
+                     InputFile *File)
+    : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
+  this->File = File;
+}
 
 template <typename ELFT>
 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
@@ -230,24 +226,35 @@
     : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
       Alignment(Alignment), Size(Size) {}
 
-std::unique_ptr<InputFile> Lazy::getFile() {
+std::unique_ptr<InputFile> Lazy::fetch() {
   if (auto *S = dyn_cast<LazyArchive>(this))
-    return S->getFile();
-  return cast<LazyObject>(this)->getFile();
+    return S->fetch();
+  return cast<LazyObject>(this)->fetch();
 }
 
-std::unique_ptr<InputFile> LazyArchive::getFile() {
-  MemoryBufferRef MBRef = File.getMember(&Sym);
+LazyArchive::LazyArchive(ArchiveFile &File,
+                         const llvm::object::Archive::Symbol S, uint8_t Type)
+    : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
+  this->File = &File;
+}
+
+LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
+    : Lazy(LazyObjectKind, Name, Type) {
+  this->File = &File;
+}
+
+std::unique_ptr<InputFile> LazyArchive::fetch() {
+  MemoryBufferRef MBRef = file()->getMember(&Sym);
 
   // getMember returns an empty buffer if the member was already
   // read from the library.
   if (MBRef.getBuffer().empty())
     return std::unique_ptr<InputFile>(nullptr);
-  return createObjectFile(MBRef, File.getName());
+  return createObjectFile(MBRef, file()->getName());
 }
 
-std::unique_ptr<InputFile> LazyObject::getFile() {
-  MemoryBufferRef MBRef = File.getBuffer();
+std::unique_ptr<InputFile> LazyObject::fetch() {
+  MemoryBufferRef MBRef = file()->getBuffer();
   if (MBRef.getBuffer().empty())
     return std::unique_ptr<InputFile>(nullptr);
   return createObjectFile(MBRef);
@@ -264,11 +271,6 @@
 template bool SymbolBody::hasThunk<ELF64LE>() const;
 template bool SymbolBody::hasThunk<ELF64BE>() const;
 
-template InputFile *SymbolBody::template getSourceFile<ELF32LE>();
-template InputFile *SymbolBody::template getSourceFile<ELF32BE>();
-template InputFile *SymbolBody::template getSourceFile<ELF64LE>();
-template InputFile *SymbolBody::template getSourceFile<ELF64BE>();
-
 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;