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_READOBJ_STREAMWRITER_H
11 #define LLVM_READOBJ_STREAMWRITER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/SmallVector.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 (size_t i = 0; i < EnumValues.size(); ++i) {
85 if (EnumValues[i].Value == Value) {
86 Name = EnumValues[i].Name;
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 EnumMask = TFlag(0)) {
102 typedef EnumEntry<TFlag> FlagEntry;
103 typedef SmallVector<FlagEntry, 10> FlagVector;
106 for (typename ArrayRef<FlagEntry>::const_iterator I = Flags.begin(),
107 E = Flags.end(); I != E; ++I) {
111 bool IsEnum = (I->Value & EnumMask) != 0;
112 if ((!IsEnum && (Value & I->Value) == I->Value) ||
113 (IsEnum && (Value & EnumMask) == I->Value)) {
114 SetFlags.push_back(*I);
118 std::sort(SetFlags.begin(), SetFlags.end(), &flagName<TFlag>);
120 startLine() << Label << " [ (" << hex(Value) << ")\n";
121 for (typename FlagVector::const_iterator I = SetFlags.begin(),
124 startLine() << " " << I->Name << " (" << hex(I->Value) << ")\n";
126 startLine() << "]\n";
130 void printFlags(StringRef Label, T Value) {
131 startLine() << Label << " [ (" << hex(Value) << ")\n";
133 uint64_t Curr = Value;
136 startLine() << " " << hex(Flag) << "\n";
140 startLine() << "]\n";
143 void printNumber(StringRef Label, uint64_t Value) {
144 startLine() << Label << ": " << Value << "\n";
147 void printNumber(StringRef Label, uint32_t Value) {
148 startLine() << Label << ": " << Value << "\n";
151 void printNumber(StringRef Label, uint16_t Value) {
152 startLine() << Label << ": " << Value << "\n";
155 void printNumber(StringRef Label, uint8_t Value) {
156 startLine() << Label << ": " << unsigned(Value) << "\n";
159 void printNumber(StringRef Label, int64_t Value) {
160 startLine() << Label << ": " << Value << "\n";
163 void printNumber(StringRef Label, int32_t Value) {
164 startLine() << Label << ": " << Value << "\n";
167 void printNumber(StringRef Label, int16_t Value) {
168 startLine() << Label << ": " << Value << "\n";
171 void printNumber(StringRef Label, int8_t Value) {
172 startLine() << Label << ": " << int(Value) << "\n";
176 void printHex(StringRef Label, T Value) {
177 startLine() << Label << ": " << hex(Value) << "\n";
181 void printHex(StringRef Label, StringRef Str, T Value) {
182 startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n";
185 void printString(StringRef Label, StringRef Value) {
186 startLine() << Label << ": " << Value << "\n";
189 void printString(StringRef Label, const std::string &Value) {
190 startLine() << Label << ": " << Value << "\n";
194 void printNumber(StringRef Label, StringRef Str, T Value) {
195 startLine() << Label << ": " << Str << " (" << Value << ")\n";
198 void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
199 printBinaryImpl(Label, Str, Value, false);
202 void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
203 ArrayRef<uint8_t> V(reinterpret_cast<const uint8_t*>(Value.data()),
205 printBinaryImpl(Label, Str, V, false);
208 void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
209 printBinaryImpl(Label, StringRef(), Value, false);
212 void printBinary(StringRef Label, ArrayRef<char> Value) {
213 ArrayRef<uint8_t> V(reinterpret_cast<const uint8_t*>(Value.data()),
215 printBinaryImpl(Label, StringRef(), V, false);
218 void printBinary(StringRef Label, StringRef Value) {
219 ArrayRef<uint8_t> V(reinterpret_cast<const uint8_t*>(Value.data()),
221 printBinaryImpl(Label, StringRef(), V, false);
224 void printBinaryBlock(StringRef Label, StringRef Value) {
225 ArrayRef<uint8_t> V(reinterpret_cast<const uint8_t*>(Value.data()),
227 printBinaryImpl(Label, StringRef(), V, true);
230 raw_ostream& startLine() {
235 raw_ostream& getOStream() {
241 static bool flagName(const EnumEntry<T>& lhs, const EnumEntry<T>& rhs) {
242 return lhs.Name < rhs.Name;
245 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
253 DictScope(StreamWriter& W, StringRef N) : W(W) {
254 W.startLine() << N << " {\n";
260 W.startLine() << "}\n";
267 ListScope(StreamWriter& W, StringRef N) : W(W) {
268 W.startLine() << N << " [\n";
274 W.startLine() << "]\n";