[lld] r256436 - Use virtual function instead of hand-written type dispatch.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 25 21:51:07 PST 2015


Author: ruiu
Date: Fri Dec 25 23:51:07 2015
New Revision: 256436

URL: http://llvm.org/viewvc/llvm-project?rev=256436&view=rev
Log:
Use virtual function instead of hand-written type dispatch.

OutputSectionBase already has virtual member functions.
This patch makes addSection() a virtual function to remove code
from Writer::createSections().

Modified:
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=256436&r1=256435&r2=256436&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Fri Dec 25 23:51:07 2015
@@ -768,17 +768,18 @@ OutputSection<ELFT>::OutputSection(Strin
     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
 
 template <class ELFT>
-void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
-  Sections.push_back(C);
-  C->OutSec = this;
-  uint32_t Align = C->getAlign();
+void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
+  auto *S = cast<InputSection<ELFT>>(C);
+  Sections.push_back(S);
+  S->OutSec = this;
+  uint32_t Align = S->getAlign();
   if (Align > this->Header.sh_addralign)
     this->Header.sh_addralign = Align;
 
   uintX_t Off = this->Header.sh_size;
   Off = RoundUpToAlignment(Off, Align);
-  C->OutSecOff = Off;
-  Off += C->getSize();
+  S->OutSecOff = Off;
+  Off += S->getSize();
   this->Header.sh_size = Off;
 }
 
@@ -1026,7 +1027,8 @@ EHOutputSection<ELFT>::readEntryLength(A
 }
 
 template <class ELFT>
-void EHOutputSection<ELFT>::addSection(EHInputSection<ELFT> *S) {
+void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
+  auto *S = cast<EHInputSection<ELFT>>(C);
   const Elf_Shdr *RelSec = S->RelocSection;
   if (!RelSec)
     return addSectionAux(
@@ -1109,7 +1111,8 @@ static size_t findNull(StringRef S, size
 }
 
 template <class ELFT>
-void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
+void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
+  auto *S = cast<MergeInputSection<ELFT>>(C);
   S->OutSec = this;
   uint32_t Align = S->getAlign();
   if (Align > this->Header.sh_addralign)
@@ -1413,8 +1416,8 @@ void MipsReginfoOutputSection<ELFT>::wri
 }
 
 template <class ELFT>
-void MipsReginfoOutputSection<ELFT>::addSection(
-    MipsReginfoInputSection<ELFT> *S) {
+void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
+  auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
   GeneralMask |= S->getGeneralMask();
 }
 

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=256436&r1=256435&r2=256436&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Fri Dec 25 23:51:07 2015
@@ -85,6 +85,8 @@ public:
   void writeHeaderTo(Elf_Shdr *SHdr);
   StringRef getName() { return Name; }
 
+  virtual void addSection(InputSectionBase<ELFT> *C) {}
+
   unsigned SectionIndex;
 
   // Returns the size of the section in the output file.
@@ -249,7 +251,7 @@ public:
   typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
   typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
   OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
-  void addSection(InputSection<ELFT> *C);
+  void addSection(InputSectionBase<ELFT> *C) override;
   void writeTo(uint8_t *Buf) override;
 
 private:
@@ -264,7 +266,7 @@ class MergeOutputSection final : public
 
 public:
   MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
-  void addSection(MergeInputSection<ELFT> *S);
+  void addSection(InputSectionBase<ELFT> *S) override;
   void writeTo(uint8_t *Buf) override;
   unsigned getOffset(StringRef Val);
   void finalize() override;
@@ -303,7 +305,7 @@ public:
       llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
           Rels);
 
-  void addSection(EHInputSection<ELFT> *S);
+  void addSection(InputSectionBase<ELFT> *S) override;
 
 private:
   uintX_t readEntryLength(ArrayRef<uint8_t> D);
@@ -424,8 +426,7 @@ class MipsReginfoOutputSection final : p
 public:
   MipsReginfoOutputSection();
   void writeTo(uint8_t *Buf) override;
-
-  void addSection(MipsReginfoInputSection<ELFT> *S);
+  void addSection(InputSectionBase<ELFT> *S) override;
 
 private:
   uint32_t GeneralMask = 0;

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=256436&r1=256435&r2=256436&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Dec 25 23:51:07 2015
@@ -671,24 +671,7 @@ template <class ELFT> void Writer<ELFT>:
         OutputSections.push_back(Sec);
         RegularSections.push_back(Sec);
       }
-      switch (C->SectionKind) {
-      case InputSectionBase<ELFT>::Regular:
-        static_cast<OutputSection<ELFT> *>(Sec)
-            ->addSection(cast<InputSection<ELFT>>(C));
-        break;
-      case InputSectionBase<ELFT>::EHFrame:
-        static_cast<EHOutputSection<ELFT> *>(Sec)
-            ->addSection(cast<EHInputSection<ELFT>>(C));
-        break;
-      case InputSectionBase<ELFT>::Merge:
-        static_cast<MergeOutputSection<ELFT> *>(Sec)
-            ->addSection(cast<MergeInputSection<ELFT>>(C));
-        break;
-      case InputSectionBase<ELFT>::MipsReginfo:
-        static_cast<MipsReginfoOutputSection<ELFT> *>(Sec)
-            ->addSection(cast<MipsReginfoInputSection<ELFT>>(C));
-        break;
-      }
+      Sec->addSection(C);
     }
   }
 




More information about the llvm-commits mailing list