Home | History | Annotate | Download | only in mcld
      1 //===- Module.h -----------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Module contains the intermediate representation (LDIR) of MCLinker.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef MCLD_MODULE_H
     14 #define MCLD_MODULE_H
     15 #ifdef ENABLE_UNITTEST
     16 #include <gtest.h>
     17 #endif
     18 
     19 #include <vector>
     20 #include <string>
     21 
     22 #include <llvm/ADT/ilist.h>
     23 
     24 #include <mcld/InputTree.h>
     25 #include <mcld/ADT/HashTable.h>
     26 #include <mcld/ADT/HashEntry.h>
     27 #include <mcld/Support/GCFactoryListTraits.h>
     28 #include <mcld/Fragment/Fragment.h>
     29 #include <mcld/LD/NamePool.h>
     30 #include <mcld/LD/SectionSymbolSet.h>
     31 #include <mcld/MC/SymbolCategory.h>
     32 #include <mcld/MC/MCLDInput.h>
     33 
     34 namespace mcld {
     35 
     36 class LDSection;
     37 class LDSymbol;
     38 
     39 /** \class Module
     40  *  \brief Module provides the intermediate representation for linking.
     41  */
     42 class Module
     43 {
     44 public:
     45   typedef std::vector<Input*> ObjectList;
     46   typedef ObjectList::iterator obj_iterator;
     47   typedef ObjectList::const_iterator const_obj_iterator;
     48 
     49   typedef std::vector<Input*> LibraryList;
     50   typedef LibraryList::iterator lib_iterator;
     51   typedef LibraryList::const_iterator const_lib_iterator;
     52 
     53   typedef InputTree::iterator input_iterator;
     54   typedef InputTree::const_iterator const_input_iterator;
     55 
     56   typedef std::vector<LDSection*> SectionTable;
     57   typedef SectionTable::iterator iterator;
     58   typedef SectionTable::const_iterator const_iterator;
     59 
     60   typedef SymbolCategory SymbolTable;
     61   typedef SymbolTable::iterator sym_iterator;
     62   typedef SymbolTable::const_iterator const_sym_iterator;
     63 
     64 public:
     65   Module();
     66 
     67   Module(const std::string& pName);
     68 
     69   ~Module();
     70 
     71   // -----  name  ----- //
     72   const std::string& name() const { return m_Name; }
     73 
     74   void setName(const std::string& pName) { m_Name = pName; }
     75 
     76   // -----  link-in objects ----- //
     77   const ObjectList& getObjectList() const { return m_ObjectList; }
     78   ObjectList&       getObjectList()       { return m_ObjectList; }
     79 
     80   const_obj_iterator obj_begin() const { return m_ObjectList.begin(); }
     81   obj_iterator       obj_begin()       { return m_ObjectList.begin(); }
     82   const_obj_iterator obj_end  () const { return m_ObjectList.end();   }
     83   obj_iterator       obj_end  ()       { return m_ObjectList.end();   }
     84 
     85   // -----  link-in libraries  ----- //
     86   const LibraryList& getLibraryList() const { return m_LibraryList; }
     87   LibraryList&       getLibraryList()       { return m_LibraryList; }
     88 
     89   const_lib_iterator lib_begin() const { return m_LibraryList.begin(); }
     90   lib_iterator       lib_begin()       { return m_LibraryList.begin(); }
     91   const_lib_iterator lib_end  () const { return m_LibraryList.end();   }
     92   lib_iterator       lib_end  ()       { return m_LibraryList.end();   }
     93 
     94   // -----  link-in inputs  ----- //
     95   const InputTree& getInputTree() const { return m_MainTree; }
     96   InputTree&       getInputTree()       { return m_MainTree; }
     97 
     98   const_input_iterator input_begin() const { return m_MainTree.begin(); }
     99   input_iterator       input_begin()       { return m_MainTree.begin(); }
    100   const_input_iterator input_end  () const { return m_MainTree.end();   }
    101   input_iterator       input_end  ()       { return m_MainTree.end();   }
    102 
    103 /// @}
    104 /// @name Section Accessors
    105 /// @{
    106 
    107   // -----  sections  ----- //
    108   const SectionTable& getSectionTable() const { return m_SectionTable; }
    109   SectionTable&       getSectionTable()       { return m_SectionTable; }
    110 
    111   iterator         begin()       { return m_SectionTable.begin(); }
    112   const_iterator   begin() const { return m_SectionTable.begin(); }
    113   iterator         end  ()       { return m_SectionTable.end();   }
    114   const_iterator   end  () const { return m_SectionTable.end();   }
    115   LDSection*       front()       { return m_SectionTable.front(); }
    116   const LDSection* front() const { return m_SectionTable.front(); }
    117   LDSection*       back ()       { return m_SectionTable.back();  }
    118   const LDSection* back () const { return m_SectionTable.back();  }
    119   size_t           size () const { return m_SectionTable.size();  }
    120   bool             empty() const { return m_SectionTable.empty(); }
    121 
    122   LDSection*       getSection(const std::string& pName);
    123   const LDSection* getSection(const std::string& pName) const;
    124 
    125   LDSymbol*       getSectionSymbol(const LDSection* pSection);
    126   const LDSymbol* getSectionSymbol(const LDSection* pSection) const;
    127 
    128 /// @}
    129 /// @name Symbol Accessors
    130 /// @{
    131 
    132   // -----  symbols  ----- //
    133   const SymbolTable& getSymbolTable() const { return m_SymbolTable; }
    134   SymbolTable&       getSymbolTable()       { return m_SymbolTable; }
    135 
    136   sym_iterator       sym_begin()       { return m_SymbolTable.begin();         }
    137   const_sym_iterator sym_begin() const { return m_SymbolTable.begin();         }
    138   sym_iterator       sym_end  ()       { return m_SymbolTable.end();           }
    139   const_sym_iterator sym_end  () const { return m_SymbolTable.end();           }
    140   size_t             sym_size () const { return m_SymbolTable.numOfSymbols();  }
    141 
    142   // ----- section symbols ----- //
    143   const SectionSymbolSet& getSectionSymbolSet() const
    144   { return m_SectSymbolSet; }
    145   SectionSymbolSet&       getSectionSymbolSet()
    146   { return m_SectSymbolSet; }
    147 
    148   // -----  names  ----- //
    149   const NamePool& getNamePool() const { return m_NamePool; }
    150   NamePool&       getNamePool()       { return m_NamePool; }
    151 
    152 private:
    153   std::string m_Name;
    154   ObjectList m_ObjectList;
    155   LibraryList m_LibraryList;
    156   InputTree m_MainTree;
    157   SectionTable m_SectionTable;
    158   SymbolTable m_SymbolTable;
    159   NamePool m_NamePool;
    160   SectionSymbolSet m_SectSymbolSet;
    161 };
    162 
    163 } // namespace of mcld
    164 
    165 #endif
    166 
    167