2013-06-05 02:32:26 +00:00
|
|
|
//===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares utility classes for handling the YAML representation of
|
|
|
|
// object files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_OBJECT_YAML_H
|
|
|
|
#define LLVM_OBJECT_YAML_H
|
|
|
|
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
/// In an object file this is just a binary blob. In an yaml file it is an hex
|
|
|
|
/// string. Using this avoid having to allocate temporary strings.
|
|
|
|
class BinaryRef {
|
2013-06-05 23:32:23 +00:00
|
|
|
/// \brief Either raw binary data, or a string of hex bytes (must always
|
|
|
|
/// be an even number of characters).
|
2013-06-05 02:32:26 +00:00
|
|
|
ArrayRef<uint8_t> Data;
|
2013-06-05 23:32:31 +00:00
|
|
|
/// \brief Discriminator between the two states of the `Data` member.
|
|
|
|
bool DataIsHexString;
|
2013-06-05 02:32:26 +00:00
|
|
|
|
|
|
|
public:
|
2013-06-05 23:32:31 +00:00
|
|
|
BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
|
2013-06-05 02:32:26 +00:00
|
|
|
BinaryRef(StringRef Data)
|
|
|
|
: Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
|
2013-06-05 23:32:31 +00:00
|
|
|
DataIsHexString(true) {}
|
|
|
|
BinaryRef() : DataIsHexString(true) {}
|
2013-06-05 02:32:26 +00:00
|
|
|
StringRef getHex() const {
|
2013-06-05 23:32:31 +00:00
|
|
|
assert(DataIsHexString);
|
2013-06-05 02:32:26 +00:00
|
|
|
return StringRef(reinterpret_cast<const char *>(Data.data()), Data.size());
|
|
|
|
}
|
|
|
|
ArrayRef<uint8_t> getBinary() const {
|
2013-06-05 23:32:31 +00:00
|
|
|
assert(!DataIsHexString);
|
2013-06-05 02:32:26 +00:00
|
|
|
return Data;
|
|
|
|
}
|
2013-06-05 23:32:27 +00:00
|
|
|
/// \brief The number of bytes that are represented by this BinaryRef.
|
|
|
|
/// This is the number of bytes that writeAsBinary() will write.
|
|
|
|
ArrayRef<uint8_t>::size_type binary_size() const {
|
2013-06-05 23:32:31 +00:00
|
|
|
if (DataIsHexString)
|
2013-06-05 23:32:27 +00:00
|
|
|
return Data.size() / 2;
|
|
|
|
return Data.size();
|
|
|
|
}
|
2013-06-05 03:20:13 +00:00
|
|
|
bool operator==(const BinaryRef &Ref) {
|
|
|
|
// Special case for default constructed BinaryRef.
|
|
|
|
if (Ref.Data.empty() && Data.empty())
|
|
|
|
return true;
|
|
|
|
|
2013-06-05 23:32:31 +00:00
|
|
|
return Ref.DataIsHexString == DataIsHexString && Ref.Data == Data;
|
2013-06-05 03:20:13 +00:00
|
|
|
}
|
2013-06-05 22:59:00 +00:00
|
|
|
/// \brief Write the contents (regardless of whether it is binary or a
|
|
|
|
/// hex string) as binary to the given raw_ostream.
|
|
|
|
void writeAsBinary(raw_ostream &OS) const;
|
2013-06-05 02:32:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace yaml {
|
|
|
|
template <> struct ScalarTraits<object::yaml::BinaryRef> {
|
|
|
|
static void output(const object::yaml::BinaryRef &, void *,
|
|
|
|
llvm::raw_ostream &);
|
|
|
|
static StringRef input(StringRef, void *, object::yaml::BinaryRef &);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|