1 //===- YAML.h ---------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_OBJECTYAML_YAML_H
10 #define LLVM_OBJECTYAML_YAML_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/YAMLTraits.h"
23 /// Specialized YAMLIO scalar type for representing a binary blob.
25 /// A typical use case would be to represent the content of a section in a
27 /// This class has custom YAMLIO traits for convenient reading and writing.
28 /// It renders as a string of hex digits in a YAML file.
29 /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
30 /// require the quotation marks, so for simplicity when outputting they are
32 /// When reading, any string whose content is an even number of hex digits
34 /// For example, all of the following are acceptable:
35 /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
37 /// A significant advantage of using this class is that it never allocates
38 /// temporary strings or buffers for any of its functionality.
44 /// Foo: DEADBEEFCAFEBABE
47 /// Could be modeled in YAMLIO by the struct:
49 /// struct FooHolder {
55 /// struct MappingTraits<FooHolder> {
56 /// static void mapping(IO &IO, FooHolder &FH) {
57 /// IO.mapRequired("Foo", FH.Foo);
60 /// } // end namespace yaml
61 /// } // end namespace llvm
64 friend bool operator==(const BinaryRef
&LHS
, const BinaryRef
&RHS
);
66 /// Either raw binary data, or a string of hex bytes (must always
67 /// be an even number of characters).
68 ArrayRef
<uint8_t> Data
;
70 /// Discriminator between the two states of the `Data` member.
71 bool DataIsHexString
= true;
74 BinaryRef() = default;
75 BinaryRef(ArrayRef
<uint8_t> Data
) : Data(Data
), DataIsHexString(false) {}
76 BinaryRef(StringRef Data
) : Data(arrayRefFromStringRef(Data
)) {}
78 /// The number of bytes that are represented by this BinaryRef.
79 /// This is the number of bytes that writeAsBinary() will write.
80 ArrayRef
<uint8_t>::size_type
binary_size() const {
82 return Data
.size() / 2;
86 /// Write the contents (regardless of whether it is binary or a
87 /// hex string) as binary to the given raw_ostream.
88 void writeAsBinary(raw_ostream
&OS
) const;
90 /// Write the contents (regardless of whether it is binary or a
91 /// hex string) as hex to the given raw_ostream.
93 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
94 void writeAsHex(raw_ostream
&OS
) const;
97 inline bool operator==(const BinaryRef
&LHS
, const BinaryRef
&RHS
) {
98 // Special case for default constructed BinaryRef.
99 if (LHS
.Data
.empty() && RHS
.Data
.empty())
102 return LHS
.DataIsHexString
== RHS
.DataIsHexString
&& LHS
.Data
== RHS
.Data
;
105 template <> struct ScalarTraits
<BinaryRef
> {
106 static void output(const BinaryRef
&, void *, raw_ostream
&);
107 static StringRef
input(StringRef
, void *, BinaryRef
&);
108 static QuotingType
mustQuote(StringRef S
) { return needsQuotes(S
); }
111 } // end namespace yaml
113 } // end namespace llvm
115 #endif // LLVM_OBJECTYAML_YAML_H