[lld] r234450 - Separate atom_collection type into two different types. NFC.

Rui Ueyama ruiu at google.com
Wed Apr 8 16:02:12 PDT 2015


Author: ruiu
Date: Wed Apr  8 18:02:11 2015
New Revision: 234450

URL: http://llvm.org/viewvc/llvm-project?rev=234450&view=rev
Log:
Separate atom_collection type into two different types. NFC.

atom_collection is basically a wrapper for std::vector. The class
provides begin and end member functions, so that it "hides" the
other member functions provided by std::vector. However, you can
still directly access _atoms member since the member is not
protected.

We cannot simply make the member private because we need that member
when we are constructing atom vectors.

This patch splits atom_collection into two types: std::vector<Atom *>
and AtomRange. When we are constructing atom vectors, we use the
former class. We return instances of the latter class from File
objects so that callers cannot add or remove atoms from the lists.

std::vector<Atom *> is automatically converted to AtomRange.

Modified:
    lld/trunk/include/lld/Core/File.h
    lld/trunk/include/lld/Core/Simple.h
    lld/trunk/lib/ReaderWriter/ELF/DynamicFile.h
    lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.hpp
    lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
    lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp

Modified: lld/trunk/include/lld/Core/File.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/File.h?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/File.h (original)
+++ lld/trunk/include/lld/Core/File.h Wed Apr  8 18:02:11 2015
@@ -91,28 +91,25 @@ public:
     return _allocator;
   }
 
-  template <typename T>
-  using atom_iterator = typename std::vector<const T *>::iterator;
+  /// The range type for the atoms. It's backed by a std::vector, but hides
+  /// its member functions so that you can only call begin or end.
+  template <typename T> class AtomRange {
+    typedef std::vector<const T *> VectorT;
 
-  template <typename T>
-  using const_atom_iterator = typename std::vector<const T *>::const_iterator;
-
-  /// \brief Different object file readers may instantiate and manage atoms with
-  /// different data structures.  This class is a collection abstraction.
-  /// Each concrete File instance must implement these atom_collection
-  /// methods to enable clients to interate the File's atoms.
-  template <typename T> class atom_collection {
   public:
-    atom_iterator<T> begin() { return _atoms.begin(); }
-    atom_iterator<T> end() { return _atoms.end(); }
-    const_atom_iterator<T> begin() const { return _atoms.begin(); }
-    const_atom_iterator<T> end() const { return _atoms.end(); }
-    uint64_t size() const { return _atoms.size(); }
-    bool empty() const { return _atoms.empty(); }
+    AtomRange(std::vector<const T *> v) : _v(v) {}
+    typename VectorT::const_iterator begin() const { return _v.begin(); }
+    typename VectorT::const_iterator end() const { return _v.end(); }
+    typename VectorT::iterator begin() { return _v.begin(); }
+    typename VectorT::iterator end() { return _v.end(); }
 
-    std::vector<const T *> _atoms;
+  private:
+    VectorT &_v;
   };
 
+  /// The type of atom mutable container.
+  template <typename T> using atom_collection = std::vector<const T *>;
+
   /// \brief Must be implemented to return the atom_collection object for
   /// all DefinedAtoms in this File.
   virtual const atom_collection<DefinedAtom> &defined() const = 0;

Modified: lld/trunk/include/lld/Core/Simple.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Simple.h?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Simple.h (original)
+++ lld/trunk/include/lld/Core/Simple.h Wed Apr  8 18:02:11 2015
@@ -30,27 +30,27 @@ class SimpleFile : public File {
 public:
   SimpleFile(StringRef path) : File(path, kindObject) {}
 
-  void addAtom(const DefinedAtom &a) { _defined._atoms.push_back(&a); }
-  void addAtom(const UndefinedAtom &a) { _undefined._atoms.push_back(&a); }
-  void addAtom(const SharedLibraryAtom &a) { _shared._atoms.push_back(&a); }
-  void addAtom(const AbsoluteAtom &a) { _absolute._atoms.push_back(&a); }
+  void addAtom(const DefinedAtom &a) { _defined.push_back(&a); }
+  void addAtom(const UndefinedAtom &a) { _undefined.push_back(&a); }
+  void addAtom(const SharedLibraryAtom &a) { _shared.push_back(&a); }
+  void addAtom(const AbsoluteAtom &a) { _absolute.push_back(&a); }
 
   void addAtom(const Atom &atom) {
     if (auto *p = dyn_cast<DefinedAtom>(&atom)) {
-      _defined._atoms.push_back(p);
+      _defined.push_back(p);
     } else if (auto *p = dyn_cast<UndefinedAtom>(&atom)) {
-      _undefined._atoms.push_back(p);
+      _undefined.push_back(p);
     } else if (auto *p = dyn_cast<SharedLibraryAtom>(&atom)) {
-      _shared._atoms.push_back(p);
+      _shared.push_back(p);
     } else if (auto *p = dyn_cast<AbsoluteAtom>(&atom)) {
-      _absolute._atoms.push_back(p);
+      _absolute.push_back(p);
     } else {
       llvm_unreachable("atom has unknown definition kind");
     }
   }
 
   void removeDefinedAtomsIf(std::function<bool(const DefinedAtom *)> pred) {
-    auto &atoms = _defined._atoms;
+    auto &atoms = _defined;
     auto newEnd = std::remove_if(atoms.begin(), atoms.end(), pred);
     atoms.erase(newEnd, atoms.end());
   }
@@ -72,7 +72,7 @@ public:
   }
 
   typedef range<std::vector<const DefinedAtom *>::iterator> DefinedAtomRange;
-  DefinedAtomRange definedAtoms() { return make_range(_defined._atoms); }
+  DefinedAtomRange definedAtoms() { return make_range(_defined); }
 
 private:
   atom_collection<DefinedAtom> _defined;

Modified: lld/trunk/lib/ReaderWriter/ELF/DynamicFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/DynamicFile.h?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/DynamicFile.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/DynamicFile.h Wed Apr  8 18:02:11 2015
@@ -87,7 +87,7 @@ protected:
         // Create an undefined atom.
         if (!name->empty()) {
           auto *newAtom = new (_alloc) ELFUndefinedAtom<ELFT>(*this, *name, &*i);
-          _undefinedAtoms._atoms.push_back(newAtom);
+          _undefinedAtoms.push_back(newAtom);
         }
         continue;
       }

Modified: lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.hpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.hpp?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.hpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.hpp Wed Apr  8 18:02:11 2015
@@ -97,7 +97,7 @@ public:
 
   File *find(StringRef sym, bool dataSymbolOnly) override {
     if (sym.equals("___dso_handle") || sym.equals(_machHeaderSymbolName)) {
-      _definedAtoms._atoms.push_back(new (allocator()) MachODefinedAtom(
+      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
           *this, sym, DefinedAtom::scopeLinkageUnit,
           DefinedAtom::typeMachHeader, DefinedAtom::mergeNo, false, false,
           ArrayRef<uint8_t>(), DefinedAtom::Alignment(4096)));

Modified: lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp (original)
+++ lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp Wed Apr  8 18:02:11 2015
@@ -407,7 +407,7 @@ private:
     auto *ivar = reinterpret_cast<const IvarsT *>(base + chunk->fileOffset);
     for (size_t i = 0; i < chunk->elementCount; ++i)
       vec[i] = new (_alloc) AtomT(*this, ivar++);
-    result._atoms = std::move(vec);
+    result = std::move(vec);
     return make_error_code(NativeReaderError::success);
   }
 
@@ -544,22 +544,22 @@ private:
     for (uint32_t i=0; i < chunk->elementCount; ++i) {
       const uint32_t index = targetIndexes[i];
       if (index < _definedAtoms.size()) {
-        this->_targetsTable[i] = _definedAtoms._atoms[index];
+        this->_targetsTable[i] = _definedAtoms[index];
         continue;
       }
       const uint32_t undefIndex = index - _definedAtoms.size();
       if (undefIndex < _undefinedAtoms.size()) {
-        this->_targetsTable[i] = _undefinedAtoms._atoms[index];
+        this->_targetsTable[i] = _undefinedAtoms[index];
         continue;
       }
       const uint32_t slIndex = undefIndex - _undefinedAtoms.size();
       if (slIndex < _sharedLibraryAtoms.size()) {
-        this->_targetsTable[i] = _sharedLibraryAtoms._atoms[slIndex];
+        this->_targetsTable[i] = _sharedLibraryAtoms[slIndex];
         continue;
       }
       const uint32_t abIndex = slIndex - _sharedLibraryAtoms.size();
       if (abIndex < _absoluteAtoms.size()) {
-        this->_targetsTable[i] = _absoluteAtoms._atoms[abIndex];
+        this->_targetsTable[i] = _absoluteAtoms[abIndex];
         continue;
       }
      return make_error_code(NativeReaderError::file_malformed);
@@ -718,7 +718,7 @@ inline const lld::File &NativeDefinedAto
 inline uint64_t NativeDefinedAtomV1::ordinal() const {
   const uint8_t* p = reinterpret_cast<const uint8_t*>(_ivarData);
   auto *start = reinterpret_cast<const NativeDefinedAtomV1 *>(
-      _file->_definedAtoms._atoms[0]);
+      _file->_definedAtoms[0]);
   const uint8_t *startp = reinterpret_cast<const uint8_t *>(start->_ivarData);
   return p - startp;
 }

Modified: lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h Wed Apr  8 18:02:11 2015
@@ -230,7 +230,7 @@ private:
 
     if (_ctx->hasEntry()) {
       StringRef entrySym = _ctx->allocate(getEntry());
-      _undefinedAtoms._atoms.push_back(
+      _undefinedAtoms.push_back(
           new (_alloc) SimpleUndefinedAtom(*this, entrySym));
       _ctx->setHasEntry(true);
       _ctx->setEntrySymbolName(entrySym);

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Wed Apr  8 18:02:11 2015
@@ -103,7 +103,7 @@ public:
   void beforeLink() override;
 
   void addUndefinedSymbol(StringRef sym) {
-    _undefinedAtoms._atoms.push_back(new (_alloc) COFFUndefinedAtom(*this, sym));
+    _undefinedAtoms.push_back(new (_alloc) COFFUndefinedAtom(*this, sym));
   }
 
   AliasAtom *createAlias(StringRef name, const DefinedAtom *target, int cnt);
@@ -329,11 +329,11 @@ std::error_code FileCOFF::doParse() {
   if (std::error_code ec = readSymbolTable(symbols))
     return ec;
 
-  createAbsoluteAtoms(symbols, _absoluteAtoms._atoms);
+  createAbsoluteAtoms(symbols, _absoluteAtoms);
   if (std::error_code ec =
-      createUndefinedAtoms(symbols, _undefinedAtoms._atoms))
+      createUndefinedAtoms(symbols, _undefinedAtoms))
     return ec;
-  if (std::error_code ec = createDefinedSymbols(symbols, _definedAtoms._atoms))
+  if (std::error_code ec = createDefinedSymbols(symbols, _definedAtoms))
     return ec;
   if (std::error_code ec = addRelocationReferenceToAtoms())
     return ec;
@@ -862,7 +862,7 @@ void FileCOFF::createAlternateNameAtoms(
       aliases.push_back(createAlias(alias, atom, cnt++));
   }
   for (AliasAtom *alias : aliases)
-    _definedAtoms._atoms.push_back(alias);
+    _definedAtoms.push_back(alias);
 }
 
 // Interpret the contents of .drectve section. If exists, the section contains
@@ -997,7 +997,7 @@ std::error_code FileCOFF::maybeCreateSXD
       handlerFunc, 0));
   }
 
-  _definedAtoms._atoms.push_back(atom);
+  _definedAtoms.push_back(atom);
   return std::error_code();
 }
 

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp Wed Apr  8 18:02:11 2015
@@ -305,14 +305,14 @@ private:
                                                     StringRef dllName) {
     auto *atom = new (_alloc)
         COFFSharedLibraryAtom(*this, hint, symbolName, importName, dllName);
-    _sharedLibraryAtoms._atoms.push_back(atom);
+    _sharedLibraryAtoms.push_back(atom);
     return atom;
   }
 
   void addFuncAtom(StringRef symbolName, StringRef dllName,
                    const COFFSharedLibraryAtom *impAtom) {
     auto *atom = new (_alloc) FuncAtom(*this, symbolName, impAtom, _machine);
-    _definedAtoms._atoms.push_back(atom);
+    _definedAtoms.push_back(atom);
   }
 
   atom_collection<DefinedAtom> _definedAtoms;

Modified: lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp?rev=234450&r1=234449&r2=234450&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp Wed Apr  8 18:02:11 2015
@@ -215,7 +215,15 @@ private:
   NameToAtom _groupMap;
 };
 
-template <typename T> using AtomList = lld::File::atom_collection<T>;
+/// Mapping of Atoms.
+template <typename T> class AtomList {
+  typedef lld::File::atom_collection<T> Ty;
+
+public:
+  typename Ty::iterator begin() { return _atoms.begin(); }
+  typename Ty::iterator end() { return _atoms.end(); }
+  Ty _atoms;
+};
 
 /// Mapping of kind: field in yaml files.
 enum FileKinds {
@@ -633,17 +641,17 @@ template <> struct MappingTraits<const l
     const lld::File *denormalize(IO &io);
 
     const atom_collection<lld::DefinedAtom> &defined() const override {
-      return _definedAtoms;
+      return _definedAtoms._atoms;
     }
     const atom_collection<lld::UndefinedAtom> &undefined() const override {
-      return _undefinedAtoms;
+      return _undefinedAtoms._atoms;
     }
     virtual const atom_collection<lld::SharedLibraryAtom> &
     sharedLibrary() const override {
-      return _sharedLibraryAtoms;
+      return _sharedLibraryAtoms._atoms;
     }
     const atom_collection<lld::AbsoluteAtom> &absolute() const override {
-      return _absoluteAtoms;
+      return _absoluteAtoms._atoms;
     }
 
     // Allocate a new copy of this string in _storage, so the strings





More information about the llvm-commits mailing list