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(char Value) : Value(static_cast<unsigned char>(Value)) { }
38 HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) { }
39 HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) { }
40 HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) { }
41 HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) { }
42 HexNumber(signed long long Value) : Value(static_cast<unsigned long long>(Value)) { }
43 HexNumber(unsigned char Value) : Value(Value) { }
44 HexNumber(unsigned short Value) : Value(Value) { }
45 HexNumber(unsigned int Value) : Value(Value) { }
46 HexNumber(unsigned long Value) : Value(Value) { }
47 HexNumber(unsigned long long Value) : Value(Value) { }
51 raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value);
55 StreamWriter(raw_ostream &OS)
64 void indent(int Levels = 1) {
65 IndentLevel += Levels;
68 void unindent(int Levels = 1) {
69 IndentLevel = std::max(0, IndentLevel - Levels);
73 for (int i = 0; i < IndentLevel; ++i)
78 HexNumber hex(T Value) {
79 return HexNumber(Value);
82 template<typename T, typename TEnum>
83 void printEnum(StringRef Label, T Value,
84 ArrayRef<EnumEntry<TEnum> > EnumValues) {
87 for (const auto &EnumItem : EnumValues) {
88 if (EnumItem.Value == Value) {
96 startLine() << Label << ": " << Name << " (" << hex(Value) << ")\n";
98 startLine() << Label << ": " << hex(Value) << "\n";
102 template <typename T, typename TFlag>
103 void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags,
104 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
105 TFlag EnumMask3 = {}) {
106 typedef EnumEntry<TFlag> FlagEntry;
107 typedef SmallVector<FlagEntry, 10> FlagVector;
110 for (const auto &Flag : Flags) {
115 if (Flag.Value & EnumMask1)
116 EnumMask = EnumMask1;
117 else if (Flag.Value & EnumMask2)
118 EnumMask = EnumMask2;
119 else if (Flag.Value & EnumMask3)
120 EnumMask = EnumMask3;
121 bool IsEnum = (Flag.Value & EnumMask) != 0;
122 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
123 (IsEnum && (Value & EnumMask) == Flag.Value)) {
124 SetFlags.push_back(Flag);
128 std::sort(SetFlags.begin(), SetFlags.end(), &flagName<TFlag>);
130 startLine() << Label << " [ (" << hex(Value) << ")\n";
131 for (const auto &Flag : SetFlags) {
132 startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
134 startLine() << "]\n";
138 void printFlags(StringRef Label, T Value) {
139 startLine() << Label << " [ (" << hex(Value) << ")\n";
141 uint64_t Curr = Value;
144 startLine() << " " << hex(Flag) << "\n";
148 startLine() << "]\n";
151 void printNumber(StringRef Label, uint64_t Value) {
152 startLine() << Label << ": " << Value << "\n";
155 void printNumber(StringRef Label, uint32_t Value) {
156 startLine() << Label << ": " << Value << "\n";
159 void printNumber(StringRef Label, uint16_t Value) {
160 startLine() << Label << ": " << Value << "\n";
163 void printNumber(StringRef Label, uint8_t Value) {
164 startLine() << Label << ": " << unsigned(Value) << "\n";
167 void printNumber(StringRef Label, int64_t Value) {
168 startLine() << Label << ": " << Value << "\n";
171 void printNumber(StringRef Label, int32_t Value) {
172 startLine() << Label << ": " << Value << "\n";
175 void printNumber(StringRef Label, int16_t Value) {
176 startLine() << Label << ": " << Value << "\n";
179 void printNumber(StringRef Label, int8_t Value) {
180 startLine() << Label << ": " << int(Value) << "\n";
183 void printBoolean(StringRef Label, bool Value) {
184 startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
187 template <typename T>
188 void printList(StringRef Label, const T &List) {
189 startLine() << Label << ": [";
191 for (const auto &Item : List) {
200 template <typename T>
201 void printHexList(StringRef Label, const T &List) {
202 startLine() << Label << ": [";
204 for (const auto &Item : List) {
214 void printHex(StringRef Label, T Value) {
215 startLine() << Label << ": " << hex(Value) << "\n";
219 void printHex(StringRef Label, StringRef Str, T Value) {
220 startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n";
223 void printString(StringRef Label, StringRef Value) {
224 startLine() << Label << ": " << Value << "\n";
227 void printString(StringRef Label, const std::string &Value) {
228 startLine() << Label << ": " << Value << "\n";
232 void printNumber(StringRef Label, StringRef Str, T Value) {
233 startLine() << Label << ": " << Str << " (" << Value << ")\n";
236 void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
237 printBinaryImpl(Label, Str, Value, false);
240 void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
241 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
243 printBinaryImpl(Label, Str, V, false);
246 void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
247 printBinaryImpl(Label, StringRef(), Value, false);
250 void printBinary(StringRef Label, ArrayRef<char> Value) {
251 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
253 printBinaryImpl(Label, StringRef(), V, false);
256 void printBinary(StringRef Label, StringRef Value) {
257 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
259 printBinaryImpl(Label, StringRef(), V, false);
262 void printBinaryBlock(StringRef Label, StringRef Value) {
263 auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
265 printBinaryImpl(Label, StringRef(), V, true);
268 raw_ostream& startLine() {
273 raw_ostream& getOStream() {
279 static bool flagName(const EnumEntry<T>& lhs, const EnumEntry<T>& rhs) {
280 return lhs.Name < rhs.Name;
283 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
291 DictScope(StreamWriter& W, StringRef N) : W(W) {
292 W.startLine() << N << " {\n";
298 W.startLine() << "}\n";
305 ListScope(StreamWriter& W, StringRef N) : W(W) {
306 W.startLine() << N << " [\n";
312 W.startLine() << "]\n";