YAML.h revision 639adc59f86d0526dbace4a1ecc4924c6c52e1cd
1//===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares utility classes for handling the YAML representation of 11// object files. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_OBJECT_YAML_H 16#define LLVM_OBJECT_YAML_H 17 18#include "llvm/Support/YAMLTraits.h" 19 20namespace llvm { 21namespace object { 22namespace yaml { 23 24/// In an object file this is just a binary blob. In an yaml file it is an hex 25/// string. Using this avoid having to allocate temporary strings. 26class BinaryRef { 27 ArrayRef<uint8_t> Data; 28 bool isBinary; 29 30public: 31 BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), isBinary(true) {} 32 BinaryRef(StringRef Data) 33 : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()), 34 isBinary(false) {} 35 BinaryRef() : isBinary(false) {} 36 StringRef getHex() const { 37 assert(!isBinary); 38 return StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()); 39 } 40 ArrayRef<uint8_t> getBinary() const { 41 assert(isBinary); 42 return Data; 43 } 44 bool operator==(const BinaryRef &Ref) { 45 // Special case for default constructed BinaryRef. 46 if (Ref.Data.empty() && Data.empty()) 47 return true; 48 49 return Ref.isBinary == isBinary && Ref.Data == Data; 50 } 51 /// \brief Write the contents (regardless of whether it is binary or a 52 /// hex string) as binary to the given raw_ostream. 53 void writeAsBinary(raw_ostream &OS) const; 54}; 55 56} 57} 58 59namespace yaml { 60template <> struct ScalarTraits<object::yaml::BinaryRef> { 61 static void output(const object::yaml::BinaryRef &, void *, 62 llvm::raw_ostream &); 63 static StringRef input(StringRef, void *, object::yaml::BinaryRef &); 64}; 65} 66 67} 68 69#endif 70