1 //===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares utility classes for handling the YAML representation of
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECT_YAML_H
16 #define LLVM_OBJECT_YAML_H
18 #include "llvm/Support/YAMLTraits.h"
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.
27 /// \brief Either raw binary data, or a string of hex bytes (must always
28 /// be an even number of characters).
29 ArrayRef<uint8_t> Data;
30 /// \brief Discriminator between the two states of the `Data` member.
34 BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
35 BinaryRef(StringRef Data)
36 : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
37 DataIsHexString(true) {}
38 BinaryRef() : DataIsHexString(true) {}
39 /// \brief The number of bytes that are represented by this BinaryRef.
40 /// This is the number of bytes that writeAsBinary() will write.
41 ArrayRef<uint8_t>::size_type binary_size() const {
43 return Data.size() / 2;
46 bool operator==(const BinaryRef &Ref) {
47 // Special case for default constructed BinaryRef.
48 if (Ref.Data.empty() && Data.empty())
51 return Ref.DataIsHexString == DataIsHexString && Ref.Data == Data;
53 /// \brief Write the contents (regardless of whether it is binary or a
54 /// hex string) as binary to the given raw_ostream.
55 void writeAsBinary(raw_ostream &OS) const;
56 /// \brief Write the contents (regardless of whether it is binary or a
57 /// hex string) as hex to the given raw_ostream.
59 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
60 void writeAsHex(raw_ostream &OS) const;
67 template <> struct ScalarTraits<object::yaml::BinaryRef> {
68 static void output(const object::yaml::BinaryRef &, void *,
70 static StringRef input(StringRef, void *, object::yaml::BinaryRef &);