annotate include/llvm/ObjectYAML/YAML.h @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
1 //===- YAML.h ---------------------------------------------------*- C++ -*-===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
6 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
8
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
9 #ifndef LLVM_OBJECTYAML_YAML_H
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
10 #define LLVM_OBJECTYAML_YAML_H
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
11
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
12 #include "llvm/ADT/ArrayRef.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
13 #include "llvm/ADT/StringRef.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
14 #include "llvm/Support/YAMLTraits.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
15 #include <cstdint>
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
16
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
17 namespace llvm {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
18
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
19 class raw_ostream;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
20
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
21 namespace yaml {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
22
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
23 /// Specialized YAMLIO scalar type for representing a binary blob.
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
24 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
25 /// A typical use case would be to represent the content of a section in a
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
26 /// binary file.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
27 /// This class has custom YAMLIO traits for convenient reading and writing.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
28 /// It renders as a string of hex digits in a YAML file.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
29 /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
30 /// require the quotation marks, so for simplicity when outputting they are
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
31 /// omitted).
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
32 /// When reading, any string whose content is an even number of hex digits
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
33 /// will be accepted.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
34 /// For example, all of the following are acceptable:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
35 /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
36 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
37 /// A significant advantage of using this class is that it never allocates
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
38 /// temporary strings or buffers for any of its functionality.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
39 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
40 /// Example:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
41 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
42 /// The YAML mapping:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
43 /// \code
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
44 /// Foo: DEADBEEFCAFEBABE
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
45 /// \endcode
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
46 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
47 /// Could be modeled in YAMLIO by the struct:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
48 /// \code
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
49 /// struct FooHolder {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
50 /// BinaryRef Foo;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
51 /// };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
52 /// namespace llvm {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
53 /// namespace yaml {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
54 /// template <>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
55 /// struct MappingTraits<FooHolder> {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
56 /// static void mapping(IO &IO, FooHolder &FH) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
57 /// IO.mapRequired("Foo", FH.Foo);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
58 /// }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
59 /// };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
60 /// } // end namespace yaml
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
61 /// } // end namespace llvm
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
62 /// \endcode
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
63 class BinaryRef {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
64 friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
65
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
66 /// Either raw binary data, or a string of hex bytes (must always
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
67 /// be an even number of characters).
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
68 ArrayRef<uint8_t> Data;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
69
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
70 /// Discriminator between the two states of the `Data` member.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
71 bool DataIsHexString = true;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
72
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
73 public:
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
74 BinaryRef() = default;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
75 BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
76 BinaryRef(StringRef Data) : Data(arrayRefFromStringRef(Data)) {}
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
77
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
78 /// The number of bytes that are represented by this BinaryRef.
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
79 /// This is the number of bytes that writeAsBinary() will write.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
80 ArrayRef<uint8_t>::size_type binary_size() const {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
81 if (DataIsHexString)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
82 return Data.size() / 2;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
83 return Data.size();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
84 }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
85
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
86 /// Write the contents (regardless of whether it is binary or a
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
87 /// hex string) as binary to the given raw_ostream.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
88 void writeAsBinary(raw_ostream &OS) const;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
89
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
90 /// Write the contents (regardless of whether it is binary or a
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
91 /// hex string) as hex to the given raw_ostream.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
92 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
93 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
94 void writeAsHex(raw_ostream &OS) const;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
95 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
96
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
97 inline bool operator==(const BinaryRef &LHS, const BinaryRef &RHS) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
98 // Special case for default constructed BinaryRef.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
99 if (LHS.Data.empty() && RHS.Data.empty())
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
100 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
101
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
102 return LHS.DataIsHexString == RHS.DataIsHexString && LHS.Data == RHS.Data;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
103 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
104
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
105 template <> struct ScalarTraits<BinaryRef> {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
106 static void output(const BinaryRef &, void *, raw_ostream &);
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
107 static StringRef input(StringRef, void *, BinaryRef &);
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
108 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
109 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
110
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
111 } // end namespace yaml
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
112
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
113 } // end namespace llvm
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
114
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
115 #endif // LLVM_OBJECTYAML_YAML_H