Files

111 lines
3.5 KiB
C++
Raw Permalink Normal View History

//===- lib/Core/Reader.cpp ------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Reader.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"
#include "llvm/ADT/StringRef.h"
2014-06-13 17:20:48 +00:00
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <memory>
2014-03-13 16:20:38 +00:00
namespace lld {
YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
void Registry::add(std::unique_ptr<Reader> reader) {
_readers.push_back(std::move(reader));
}
void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
_yamlHandlers.push_back(std::move(handler));
}
2015-04-24 18:51:30 +00:00
ErrorOr<std::unique_ptr<File>>
Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
// Get file magic.
StringRef content(mb->getBufferStart(), mb->getBufferSize());
llvm::sys::fs::file_magic fileType = llvm::sys::fs::identify_magic(content);
// Ask each registered reader if it can handle this file type or extension.
for (const std::unique_ptr<Reader> &reader : _readers) {
if (!reader->canParse(fileType, mb->getMemBufferRef()))
continue;
2015-04-24 18:51:30 +00:00
return reader->loadFile(std::move(mb), *this);
}
2013-12-20 07:48:29 +00:00
// No Reader could parse this file.
2014-06-13 17:20:48 +00:00
return make_error_code(llvm::errc::executable_format_error);
}
static const Registry::KindStrings kindStrings[] = {
2014-03-26 16:37:13 +00:00
{Reference::kindLayoutAfter, "layout-after"},
{Reference::kindAssociate, "associate"},
2014-03-26 16:37:13 +00:00
LLD_KIND_STRING_END};
Registry::Registry() {
2013-12-20 07:48:29 +00:00
addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
kindStrings);
}
2014-01-27 03:09:26 +00:00
bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
const lld::File *&file) const {
2014-06-04 08:39:56 +00:00
for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
if (h->handledDocTag(io, file))
return true;
return false;
}
2013-12-20 07:48:29 +00:00
void Registry::addKindTable(Reference::KindNamespace ns,
Reference::KindArch arch,
const KindStrings array[]) {
2013-12-20 07:48:29 +00:00
KindEntry entry = { ns, arch, array };
_kindEntries.push_back(entry);
}
2013-12-20 07:48:29 +00:00
bool Registry::referenceKindFromString(StringRef inputStr,
Reference::KindNamespace &ns,
2013-12-20 07:48:29 +00:00
Reference::KindArch &arch,
Reference::KindValue &value) const {
for (const KindEntry &entry : _kindEntries) {
2013-12-20 07:48:29 +00:00
for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
if (!inputStr.equals(pair->name))
continue;
ns = entry.ns;
arch = entry.arch;
value = pair->value;
return true;
}
}
return false;
}
2013-12-20 07:48:29 +00:00
bool Registry::referenceKindToString(Reference::KindNamespace ns,
Reference::KindArch arch,
Reference::KindValue value,
StringRef &str) const {
for (const KindEntry &entry : _kindEntries) {
if (entry.ns != ns)
continue;
if (entry.arch != arch)
continue;
2013-12-20 07:48:29 +00:00
for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
if (pair->value != value)
continue;
str = pair->name;
return true;
}
}
return false;
}
2013-03-14 16:09:49 +00:00
} // end namespace lld