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 /// \brief Specialized YAMLIO scalar type for representing a binary blob.
26 /// A typical use case would be to represent the content of a section in a
28 /// This class has custom YAMLIO traits for convenient reading and writing.
29 /// It renders as a string of hex digits in a YAML file.
30 /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
31 /// require the quotation marks, so for simplicity when outputting they are
33 /// When reading, any string whose content is an even number of hex digits
35 /// For example, all of the following are acceptable:
36 /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
38 /// A significant advantage of using this class is that it never allocates
39 /// temporary strings or buffers for any of its functionality.
45 /// Foo: DEADBEEFCAFEBABE
48 /// Could be modeled in YAMLIO by the struct:
50 /// struct FooHolder {
56 /// struct MappingTraits<FooHolder> {
57 /// static void mapping(IO &IO, FooHolder &FH) {
58 /// IO.mapRequired("Foo", FH.Foo);
61 /// } // end namespace yaml
62 /// } // end namespace llvm
65 /// \brief Either raw binary data, or a string of hex bytes (must always
66 /// be an even number of characters).
67 ArrayRef<uint8_t> Data;
68 /// \brief Discriminator between the two states of the `Data` member.
72 BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
73 BinaryRef(StringRef Data)
74 : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
75 DataIsHexString(true) {}
76 BinaryRef() : DataIsHexString(true) {}
77 /// \brief The number of bytes that are represented by this BinaryRef.
78 /// This is the number of bytes that writeAsBinary() will write.
79 ArrayRef<uint8_t>::size_type binary_size() const {
81 return Data.size() / 2;
84 bool operator==(const BinaryRef &RHS) {
85 // Special case for default constructed BinaryRef.
86 if (RHS.Data.empty() && Data.empty())
89 return RHS.DataIsHexString == DataIsHexString && RHS.Data == Data;
91 /// \brief Write the contents (regardless of whether it is binary or a
92 /// hex string) as binary to the given raw_ostream.
93 void writeAsBinary(raw_ostream &OS) const;
94 /// \brief Write the contents (regardless of whether it is binary or a
95 /// hex string) as hex to the given raw_ostream.
97 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
98 void writeAsHex(raw_ostream &OS) const;
105 template <> struct ScalarTraits<object::yaml::BinaryRef> {
106 static void output(const object::yaml::BinaryRef &, void *,
107 llvm::raw_ostream &);
108 static StringRef input(StringRef, void *, object::yaml::BinaryRef &);