1 //===-- StreamWriter.h ----------------------------------------------------===//
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 #ifndef LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H
11 #define LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm::support;
33 // To avoid sign-extension we have to explicitly cast to the appropriate
34 // unsigned type. The overloads are here so that every type that is implicitly
35 // convertible to an integer (including enums and endian helpers) can be used
36 // without requiring type traits or call-site changes.
37 HexNumber(int8_t Value) : Value(static_cast<uint8_t >(Value)) { }
38 HexNumber(int16_t Value) : Value(static_cast<uint16_t>(Value)) { }
39 HexNumber(int32_t Value) : Value(static_cast<uint32_t>(Value)) { }
40 HexNumber(int64_t Value) : Value(static_cast<uint64_t>(Value)) { }
41 HexNumber(uint8_t Value) : Value(Value) { }
42 HexNumber(uint16_t Value) : Value(Value) { }
43 HexNumber(uint32_t Value) : Value(Value) { }
44 HexNumber(uint64_t Value) : Value(Value) { }
48 raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value);
52 StreamWriter(raw_ostream &OS)
61 void indent(int Levels = 1) {
62 IndentLevel += Levels;
65 void unindent(int Levels = 1) {
66 IndentLevel = std::max(0, IndentLevel - Levels);
70 for (int i = 0; i < IndentLevel; ++i)
75 HexNumber hex(T Value) {
76 return HexNumber(Value);
79 template<typename T, typename TEnum>
80 void printEnum(StringRef Label, T Value,
81 ArrayRef<EnumEntry<TEnum> > EnumValues) {
84 for (const auto &EnumItem : EnumValues) {
85 if (EnumItem.Value == Value) {
93 startLine() << Label << ": " << Name << " (" << hex(Value) << ")\n";
95 startLine() << Label << ": " << hex(Value) << "\n";
99 template <typename T, typename TFlag>
100 void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags,
101 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
102 TFlag EnumMask3 = {}) {
103 typedef EnumEntry<TFlag> FlagEntry;
104 typedef SmallVector<FlagEntry, 10> FlagVector;
107 for (const auto &Flag : Flags) {
112 if (Flag.Value & EnumMask1)
113 EnumMask = EnumMask1;
114 else if (Flag.Value & EnumMask2)
115 EnumMask = EnumMask2;
116 else if (Flag.Value & EnumMask3)
117 EnumMask = EnumMask3;
118 bool IsEnum = (Flag.Value & EnumMask) != 0;
119 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
120 (IsEnum && (Value & EnumMask) == Flag.Value)) {
121 SetFlags.push_back(Flag);
125 std::sort(SetFlags.begin(), SetFlags.end(), &flagName<TFlag>);
127 startLine() << Label << " [ (" << hex(Value) << ")\n";
128 for (const auto &Flag : SetFlags) {
129 startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
131 startLine() << "]\n";
135 void printFlags(StringRef Label, T Value) {
136 startLine() << Label << " [ (" << hex(Value) << ")\n";
138 uint64_t Curr = Value;
141 startLine() << " " << hex(Flag) << "\n";
145 startLine() << "]\n";
148 void printNumber(StringRef Label, uint64_t Value) {
149 startLine() << Label << ": " << Value << "\n";
152 void printNumber(StringRef Label, uint32_t Value) {
153 startLine() << Label << ": " << Value << "\n";
156 void printNumber(StringRef Label, uint16_t Value) {
157 startLine() << Label << ": " << Value << "\n";
160 void printNumber(StringRef Label, uint8_t Value) {
161 startLine() << Label << ": " << unsigned(Value) << "\n";
164 void printNumber(StringRef Label, int64_t Value) {
165 startLine() << Label << ": " << Value << "\n";
168 void printNumber(StringRef Label, int32_t Value) {
169 startLine() << Label << ": " << Value << "\n";
172 void printNumber(StringRef Label, int16_t Value) {
173 startLine() << Label << ": " << Value << "\n";
176 void printNumber(StringRef Label, int8_t Value) {
177 startLine() << Label << ": " << int(Value) << "\n";
180 void printBoolean(StringRef Label, bool Value) {
181 startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
184 template <typename T>
185 void printList(StringRef Label, const T &List) {
186 startLine() << Label << ": [";
188 for (const auto &Item : List) {
198 void printHex(StringRef Label, T Value) {
199 startLine() << Label << ": " << hex(Value) << "\n";
203 void printHex(StringRef Label, StringRef Str, T Value) {
204 startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n";
207 void printString(StringRef Label, StringRef Value) {
208 startLine() << Label << ": " << Value << "\n";
211 void printString(StringRef Label, const std::string &Value) {
212 startLine() << Label << ": " << Value << "\n";
216 void printNumber(StringRef Label, StringRef Str, T Value) {
217 startLine() << Label << ": " << Str << " (" << Value << ")\n";
220 void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
221 printBinaryImpl(Label, Str, Value, false);
224 void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
225 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
227 printBinaryImpl(Label, Str, V, false);
230 void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
231 printBinaryImpl(Label, StringRef(), Value, false);
234 void printBinary(StringRef Label, ArrayRef<char> Value) {
235 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
237 printBinaryImpl(Label, StringRef(), V, false);
240 void printBinary(StringRef Label, StringRef Value) {
241 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
243 printBinaryImpl(Label, StringRef(), V, false);
246 void printBinaryBlock(StringRef Label, StringRef Value) {
247 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
249 printBinaryImpl(Label, StringRef(), V, true);
252 raw_ostream& startLine() {
257 raw_ostream& getOStream() {
263 static bool flagName(const EnumEntry<T>& lhs, const EnumEntry<T>& rhs) {
264 return lhs.Name < rhs.Name;
267 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
275 DictScope(StreamWriter& W, StringRef N) : W(W) {
276 W.startLine() << N << " {\n";
282 W.startLine() << "}\n";
289 ListScope(StreamWriter& W, StringRef N) : W(W) {
290 W.startLine() << N << " [\n";
296 W.startLine() << "]\n";