9#ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
10#define LLVM_SUPPORT_SCOPEDPRINTER_H
59 template <
typename EnumT,
typename = std::enable_if_t<std::is_enum_v<EnumT>>>
85 template <
typename EnumT,
typename = std::enable_if_t<std::is_enum_v<EnumT>>>
102template <
typename T,
typename TEnum>
105 if (EnumItem.Value ==
Value)
106 return std::string(EnumItem.AltName);
107 return utohexstr(
Value,
true);
113template <
typename T,
typename TEnum>
116 if (EnumItem.Value ==
Value)
117 return EnumItem.AltName;
130 :
OS(
OS), Kind(Kind) {}
135 return SP->getKind() == ScopedPrinterKind::Base;
142 void indent(
int Levels = 1) { IndentLevel += Levels; }
145 IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
156 for (
int i = 0; i < IndentLevel; ++i)
162 template <
typename T,
typename TEnum>
167 for (
const auto &EnumItem : EnumValues) {
168 if (EnumItem.Value ==
Value) {
169 Name = EnumItem.Name;
178 printHex(Label,
Value);
181 template <
typename T,
typename TFlag>
183 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
184 TFlag EnumMask3 = {}, ArrayRef<FlagEntry> ExtraFlags = {}) {
185 SmallVector<FlagEntry, 10> SetFlags(ExtraFlags);
187 for (
const auto &Flag : Flags) {
188 if (
Flag.Value == TFlag{})
192 if ((
Flag.Value & EnumMask1) != TFlag{})
193 EnumMask = EnumMask1;
194 else if ((
Flag.Value & EnumMask2) != TFlag{})
195 EnumMask = EnumMask2;
196 else if ((
Flag.Value & EnumMask3) != TFlag{})
197 EnumMask = EnumMask3;
198 bool IsEnum = (
Flag.Value & EnumMask) != TFlag{};
199 if ((!IsEnum && (Value &
Flag.Value) ==
Flag.Value) ||
200 (IsEnum && (
Value & EnumMask) ==
Flag.Value)) {
201 SetFlags.emplace_back(
Flag.Name,
Flag.Value);
206 printFlagsImpl(Label, hex(Value), SetFlags);
219 printFlagsImpl(Label, hex(
Value), SetFlags);
223 startLine() << Label <<
": " <<
static_cast<int>(
Value) <<
"\n";
227 startLine() << Label <<
": " <<
static_cast<int>(
Value) <<
"\n";
231 startLine() << Label <<
": " <<
static_cast<unsigned>(
Value) <<
"\n";
235 startLine() << Label <<
": " <<
Value <<
"\n";
239 startLine() << Label <<
": " <<
Value <<
"\n";
243 startLine() << Label <<
": " <<
Value <<
"\n";
247 startLine() << Label <<
": " <<
Value <<
"\n";
251 startLine() << Label <<
": " <<
Value <<
"\n";
255 startLine() << Label <<
": " <<
Value <<
"\n";
259 startLine() << Label <<
": " <<
Value <<
"\n";
263 startLine() << Label <<
": " <<
Value <<
"\n";
267 startLine() << Label <<
": " <<
Value <<
"\n";
271 startLine() << Label <<
": " <<
format(
"%5.1f",
Value) <<
"\n";
275 startLine() << Label <<
": " <<
format(
"%5.1f",
Value) <<
"\n";
278 template <
typename T>
284 startLine() << Label <<
": " << (
Value ?
"Yes" :
"No") <<
'\n';
288 startLine() << Label <<
": ";
289 printVersionInternal(Version...);
290 getOStream() <<
"\n";
293 template <
typename T>
296 for (
const auto &Item :
List)
298 printList(Label, StringList);
302 printListImpl(Label,
List);
306 printListImpl(Label,
List);
310 printListImpl(Label,
List);
314 printListImpl(Label,
List);
318 printListImpl(Label,
List);
325 printListImpl(Label, NumberList);
329 printListImpl(Label,
List);
333 printListImpl(Label,
List);
337 printListImpl(Label,
List);
342 for (
const int8_t &Item :
List)
344 printListImpl(Label, NumberList);
348 printListImpl(Label,
List);
351 template <
typename T,
typename U>
353 startLine() << Label <<
": [";
355 for (
const auto &Item :
List) {
364 for (
const auto &Item :
List)
366 printHexListImpl(Label, HexList);
370 printHexImpl(Label, hex(
Value));
374 printHexImpl(Label, Str, hex(
Value));
377 template <
typename T>
379 printSymbolOffsetImpl(Label, Symbol, hex(
Value));
385 startLine() << Label <<
": " <<
Value <<
"\n";
389 printStringEscapedImpl(Label,
Value);
393 printBinaryImpl(Label, Str,
Value,
false);
399 printBinaryImpl(Label, Str, V,
false);
409 printBinaryImpl(Label,
StringRef(), V,
false);
415 printBinaryImpl(Label,
StringRef(), V,
false);
430 printBinaryImpl(Label,
StringRef(), V,
true);
457 template <
typename T>
void printVersionInternal(
T Value) {
458 getOStream() <<
Value;
461 template <
typename S,
typename T,
typename... TArgs>
462 void printVersionInternal(S Value,
T Value2, TArgs... Args) {
463 getOStream() << Value <<
".";
464 printVersionInternal(Value2, Args...);
467 static bool flagName(
const FlagEntry &
LHS,
const FlagEntry &
RHS) {
468 return LHS.Name <
RHS.Name;
471 virtual void printBinaryImpl(StringRef Label, StringRef Str,
472 ArrayRef<uint8_t> Value,
bool Block,
475 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
476 ArrayRef<FlagEntry> Flags) {
477 startLine() <<
Label <<
" [ (" <<
Value <<
")\n";
478 for (
const auto &Flag : Flags)
479 startLine() <<
" " <<
Flag.Name <<
" (" << hex(
Flag.Value) <<
")\n";
480 startLine() <<
"]\n";
483 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
484 ArrayRef<HexNumber> Flags) {
485 startLine() <<
Label <<
" [ (" <<
Value <<
")\n";
486 for (
const auto &Flag : Flags)
487 startLine() <<
" " <<
Flag <<
'\n';
488 startLine() <<
"]\n";
491 template <
typename T>
void printListImpl(StringRef Label,
const T List) {
492 startLine() <<
Label <<
": [";
494 for (
const auto &Item : List)
499 virtual void printHexListImpl(StringRef Label,
500 const ArrayRef<HexNumber> List) {
501 startLine() <<
Label <<
": [";
503 for (
const auto &Item : List)
504 OS <<
LS << hex(Item);
508 virtual void printHexImpl(StringRef Label, HexNumber Value) {
509 startLine() <<
Label <<
": " <<
Value <<
"\n";
512 virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
513 startLine() <<
Label <<
": " << Str <<
" (" <<
Value <<
")\n";
516 virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
521 virtual void printNumberImpl(StringRef Label, StringRef Str,
523 startLine() <<
Label <<
": " << Str <<
" (" <<
Value <<
")\n";
526 virtual void printStringEscapedImpl(StringRef Label, StringRef Value) {
527 startLine() <<
Label <<
": ";
528 OS.write_escaped(Value);
532 void scopedBegin(
char Symbol) {
533 startLine() <<
Symbol <<
'\n';
537 void scopedBegin(StringRef Label,
char Symbol) {
538 startLine() <<
Label;
545 void scopedEnd(
char Symbol) {
547 startLine() <<
Symbol <<
'\n';
553 ScopedPrinterKind
Kind;
558ScopedPrinter::printHex<support::ulittle16_t>(
StringRef Label,
560 startLine() << Label <<
": " << hex(
Value) <<
"\n";
578 enum class ScopeKind {
584 struct ScopeContext {
587 ScopeContext(Scope
Context, ScopeKind Kind = ScopeKind::NoAttribute)
593 std::unique_ptr<DelimitedScope> OuterScope;
597 std::unique_ptr<DelimitedScope> &&OuterScope =
598 std::unique_ptr<DelimitedScope>{});
667 printListImpl(Label,
List);
671 printListImpl(Label,
List);
675 printListImpl(Label,
List);
679 printListImpl(Label,
List);
683 printListImpl(Label,
List);
687 printListImpl(Label,
List);
691 printListImpl(Label,
List);
695 printListImpl(Label,
List);
699 printListImpl(Label,
List);
703 printListImpl(Label,
List);
721 scopedBegin({Scope::Object, ScopeKind::NoAttribute});
725 scopedBegin(Label, Scope::Object);
731 scopedBegin({Scope::Array, ScopeKind::NoAttribute});
735 scopedBegin(Label, Scope::Array);
744 void printAPSInt(
const APSInt &Value) {
749 void printFlagsImpl(StringRef Label, HexNumber Value,
750 ArrayRef<FlagEntry> Flags)
override {
752 JOS.
attribute(
"Value", hexNumberToInt(Value));
754 for (
const FlagEntry &Flag : Flags) {
764 void printFlagsImpl(StringRef Label, HexNumber Value,
765 ArrayRef<HexNumber> Flags)
override {
767 JOS.
attribute(
"Value", hexNumberToInt(Value));
769 for (
const HexNumber &Flag : Flags) {
776 template <
typename T>
void printListImpl(StringRef Label,
const T &
List) {
778 for (
const auto &Item :
List)
783 void printHexListImpl(StringRef Label,
784 const ArrayRef<HexNumber>
List)
override {
786 for (
const HexNumber &Item :
List) {
787 JOS.
value(hexNumberToInt(Item));
792 void printHexImpl(StringRef Label, HexNumber Value)
override {
793 JOS.
attribute(Label, hexNumberToInt(Value));
796 void printHexImpl(StringRef Label, StringRef Str, HexNumber Value)
override {
799 JOS.
attribute(
"Value", hexNumberToInt(Value));
803 void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
804 HexNumber Value)
override {
807 JOS.
attribute(
"Offset", hexNumberToInt(Value));
811 void printNumberImpl(StringRef Label, StringRef Str,
812 StringRef Value)
override {
822 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
829 for (uint8_t Val : Value)
835 void scopedBegin(ScopeContext ScopeCtx) {
836 if (ScopeCtx.Context == Scope::Object)
838 else if (ScopeCtx.Context == Scope::Array)
843 void scopedBegin(StringRef Label, Scope Ctx) {
844 ScopeKind
Kind = ScopeKind::Attribute;
845 if (ScopeHistory.
empty() || ScopeHistory.
back().Context != Scope::Object) {
847 Kind = ScopeKind::NestedAttribute;
850 scopedBegin({Ctx,
Kind});
854 ScopeContext ScopeCtx = ScopeHistory.
back();
855 if (ScopeCtx.Context == Scope::Object)
857 else if (ScopeCtx.Context == Scope::Array)
859 if (ScopeCtx.Kind == ScopeKind::Attribute ||
860 ScopeCtx.Kind == ScopeKind::NestedAttribute)
862 if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
dxil pretty DXIL Metadata Pretty Printer
This file supports working with JSON data.
This file defines the SmallVector class.
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
void printNumber(StringRef Label, signed char Value) override
void printNumber(StringRef Label, double Value) override
void printList(StringRef Label, const ArrayRef< std::string > List) override
void printNumber(StringRef Label, float Value) override
void printNumber(StringRef Label, short Value) override
void printNumber(StringRef Label, long long Value) override
void printList(StringRef Label, const ArrayRef< uint8_t > List) override
void printList(StringRef Label, const ArrayRef< int32_t > List) override
void printNumber(StringRef Label, char Value) override
void printBoolean(StringRef Label, bool Value) override
void printNumber(StringRef Label, unsigned char Value) override
void printList(StringRef Label, const ArrayRef< int64_t > List) override
void printNumber(StringRef Label, int Value) override
void printList(StringRef Label, const ArrayRef< bool > List) override
void arrayBegin() override
void printList(StringRef Label, const ArrayRef< APSInt > List) override
void printList(StringRef Label, const ArrayRef< uint64_t > List) override
void printList(StringRef Label, const ArrayRef< uint16_t > List) override
void printNumber(StringRef Label, long Value) override
void printList(StringRef Label, const ArrayRef< int16_t > List) override
void objectEnd() override
static bool classof(const ScopedPrinter *SP)
void printNumber(StringRef Label, const APSInt &Value) override
void printList(StringRef Label, const ArrayRef< uint32_t > List) override
void printNumber(StringRef Label, unsigned int Value) override
void arrayBegin(StringRef Label) override
void printString(StringRef Value) override
void printNumber(StringRef Label, unsigned short Value) override
void objectBegin(StringRef Label) override
void printNumber(StringRef Label, unsigned long Value) override
void printString(StringRef Label, StringRef Value) override
void printList(StringRef Label, const ArrayRef< int8_t > List) override
void printNumber(StringRef Label, unsigned long long Value) override
void objectBegin() override
virtual void printList(StringRef Label, const ArrayRef< int32_t > List)
void printHexList(StringRef Label, const T &List)
virtual void printList(StringRef Label, const ArrayRef< uint32_t > List)
void printBinary(StringRef Label, StringRef Str, ArrayRef< char > Value)
virtual void printString(StringRef Value)
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value, uint32_t StartOffset)
void printStringEscaped(StringRef Label, StringRef Value)
void printBinaryBlock(StringRef Label, StringRef Value)
virtual void printNumber(StringRef Label, unsigned short Value)
virtual void printNumber(StringRef Label, long Value)
virtual void printList(StringRef Label, const ArrayRef< int64_t > List)
virtual void printNumber(StringRef Label, unsigned int Value)
void indent(int Levels=1)
virtual void printList(StringRef Label, const ArrayRef< int16_t > List)
virtual void printNumber(StringRef Label, long long Value)
void printFlags(StringRef Label, T Value)
virtual void printList(StringRef Label, const ArrayRef< std::string > List)
void unindent(int Levels=1)
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value)
virtual void arrayBegin(StringRef Label)
void printEnum(StringRef Label, T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
virtual void printList(StringRef Label, const ArrayRef< bool > List)
virtual void printList(StringRef Label, const ArrayRef< APSInt > List)
void printVersion(StringRef Label, T... Version)
ScopedPrinterKind getKind() const
virtual void printNumber(StringRef Label, const APSInt &Value)
ScopedPrinter(raw_ostream &OS, ScopedPrinterKind Kind=ScopedPrinterKind::Base)
void printList(StringRef Label, const T &List, const U &Printer)
virtual void printNumber(StringRef Label, short Value)
void printBinary(StringRef Label, StringRef Value)
virtual raw_ostream & getOStream()
virtual void printList(StringRef Label, const ArrayRef< int8_t > List)
static bool classof(const ScopedPrinter *SP)
virtual void printNumber(StringRef Label, unsigned long Value)
virtual raw_ostream & startLine()
virtual void printNumber(StringRef Label, char Value)
virtual void printNumber(StringRef Label, int Value)
void printBinary(StringRef Label, StringRef Str, ArrayRef< uint8_t > Value)
virtual void printList(StringRef Label, const ArrayRef< uint64_t > List)
virtual void printNumber(StringRef Label, float Value)
void printHex(StringRef Label, T Value)
virtual void objectBegin(StringRef Label)
void setPrefix(StringRef P)
void printFlags(StringRef Label, T Value, ArrayRef< EnumEntry< TFlag > > Flags, TFlag EnumMask1={}, TFlag EnumMask2={}, TFlag EnumMask3={}, ArrayRef< FlagEntry > ExtraFlags={})
virtual ~ScopedPrinter()=default
virtual void arrayBegin()
virtual void printBoolean(StringRef Label, bool Value)
void printHex(StringRef Label, StringRef Str, T Value)
void printNumber(StringRef Label, StringRef Str, T Value)
virtual void objectBegin()
virtual void printNumber(StringRef Label, signed char Value)
virtual void printNumber(StringRef Label, unsigned char Value)
virtual void printNumber(StringRef Label, unsigned long long Value)
virtual void printList(StringRef Label, const ArrayRef< uint16_t > List)
virtual void printString(StringRef Label, StringRef Value)
virtual void printList(StringRef Label, const ArrayRef< uint8_t > List)
void printSymbolOffset(StringRef Label, StringRef Symbol, T Value)
virtual void printNumber(StringRef Label, double Value)
void printBinary(StringRef Label, ArrayRef< char > Value)
void printList(StringRef Label, const ArrayRef< T > List)
void printBinary(StringRef Label, ArrayRef< uint8_t > Value)
void printObject(StringRef Label, const T &Value)
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
LLVM Value Representation.
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
void attributeObject(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an object with attributes from the Block.
LLVM_ABI void attributeBegin(llvm::StringRef Key)
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
LLVM_ABI void arrayBegin()
LLVM_ABI void objectBegin()
LLVM_ABI raw_ostream & rawValueBegin()
void attributeArray(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an array with elements from the Block.
LLVM_ABI void attributeEnd()
LLVM_ABI void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
LLVM_ABI void rawValueEnd()
LLVM_ABI void objectEnd()
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
This is an optimization pass for GlobalISel generic memory operations.
std::string enumToString(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
void sort(IteratorTy Start, IteratorTy End)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
const char * to_string(ThinOrFullLTOPhase Phase)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
StringRef enumToStringRef(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
Retrieves the Value's enum name.
Implement std::hash so that hash_code can be used in STL containers.
virtual void setPrinter(ScopedPrinter &W)=0
DelimitedScope(ScopedPrinter &W)
virtual ~DelimitedScope()=default
DictScope(ScopedPrinter &W)
DictScope(ScopedPrinter &W, StringRef N)
void setPrinter(ScopedPrinter &W) override
constexpr EnumEntry(StringRef N, StringRef A, T V)
constexpr EnumEntry(StringRef N, T V)
FlagEntry(StringRef Name, signed long Value)
FlagEntry(StringRef Name, signed int Value)
FlagEntry(StringRef Name, unsigned short Value)
FlagEntry(StringRef Name, unsigned long long Value)
FlagEntry(StringRef Name, unsigned long Value)
FlagEntry(StringRef Name, signed long long Value)
FlagEntry(StringRef Name, char Value)
FlagEntry(StringRef Name, signed char Value)
FlagEntry(StringRef Name, signed short Value)
FlagEntry(StringRef Name, EnumT Value)
FlagEntry(StringRef Name, unsigned int Value)
FlagEntry(StringRef Name, unsigned char Value)
HexNumber(unsigned char Value)
HexNumber(signed int Value)
HexNumber(signed long long Value)
HexNumber(signed long Value)
HexNumber(unsigned int Value)
HexNumber(unsigned long long Value)
HexNumber(signed char Value)
HexNumber(signed short Value)
HexNumber(unsigned long Value)
HexNumber(unsigned short Value)
ListScope(ScopedPrinter &W)
void setPrinter(ScopedPrinter &W) override
ListScope(ScopedPrinter &W, StringRef N)