summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2015-04-08 23:02:11 +0000
committerRui Ueyama <ruiu@google.com>2015-04-08 23:02:11 +0000
commitb0db07bf9049bdc028fcdd306710b86100d68a5f (patch)
tree458e64e4d774ed54456f36ad70bae1cc4109b8b2
parent7a2e5c260c89fe40f0d65e165a83d75a34bc587e (diff)
downloadbcm5719-llvm-b0db07bf9049bdc028fcdd306710b86100d68a5f.tar.gz
bcm5719-llvm-b0db07bf9049bdc028fcdd306710b86100d68a5f.zip
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. llvm-svn: 234450
-rw-r--r--lld/include/lld/Core/File.h33
-rw-r--r--lld/include/lld/Core/Simple.h20
-rw-r--r--lld/lib/ReaderWriter/ELF/DynamicFile.h2
-rw-r--r--lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp2
-rw-r--r--lld/lib/ReaderWriter/Native/ReaderNative.cpp12
-rw-r--r--lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h2
-rw-r--r--lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp12
-rw-r--r--lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp4
-rw-r--r--lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp18
9 files changed, 55 insertions, 50 deletions
diff --git a/lld/include/lld/Core/File.h b/lld/include/lld/Core/File.h
index 4ac03a11da1..21284bc5d6d 100644
--- a/lld/include/lld/Core/File.h
+++ b/lld/include/lld/Core/File.h
@@ -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(); }
-
- std::vector<const T *> _atoms;
+ 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(); }
+
+ 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;
diff --git a/lld/include/lld/Core/Simple.h b/lld/include/lld/Core/Simple.h
index a4999e15f82..b4d7b87e642 100644
--- a/lld/include/lld/Core/Simple.h
+++ b/lld/include/lld/Core/Simple.h
@@ -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;
diff --git a/lld/lib/ReaderWriter/ELF/DynamicFile.h b/lld/lib/ReaderWriter/ELF/DynamicFile.h
index 9b2f2c94d13..6e03b3a76ba 100644
--- a/lld/lib/ReaderWriter/ELF/DynamicFile.h
+++ b/lld/lib/ReaderWriter/ELF/DynamicFile.h
@@ -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;
}
diff --git a/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp b/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp
index 6caa10766be..e3bcac7ba77 100644
--- a/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp
+++ b/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp
@@ -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)));
diff --git a/lld/lib/ReaderWriter/Native/ReaderNative.cpp b/lld/lib/ReaderWriter/Native/ReaderNative.cpp
index 0ded101f8b0..40c6057b869 100644
--- a/lld/lib/ReaderWriter/Native/ReaderNative.cpp
+++ b/lld/lib/ReaderWriter/Native/ReaderNative.cpp
@@ -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 &NativeDefinedAtomV1::file() const {
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;
}
diff --git a/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
index 4bcae11ef8d..1985f9fed92 100644
--- a/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
+++ b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
@@ -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);
diff --git a/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp b/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
index 9a7a5809c90..64adbe139b0 100644
--- a/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
+++ b/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
@@ -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::maybeCreateSXDataAtoms() {
handlerFunc, 0));
}
- _definedAtoms._atoms.push_back(atom);
+ _definedAtoms.push_back(atom);
return std::error_code();
}
diff --git a/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp b/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
index 26bdfaa1460..7c7155cb8b6 100644
--- a/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
+++ b/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
@@ -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;
diff --git a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
index 3bf8a943637..7e14b2c130d 100644
--- a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
+++ b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
@@ -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 lld::File *> {
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
OpenPOWER on IntegriCloud