aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/include/llvm/Support/ScopedPrinter.h
blob: 171d5b82c0ee5b78dd6c2a73768f89115205b1d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===-- ScopedPrinter.h ----------------------------------------*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
#define LLVM_SUPPORT_SCOPEDPRINTER_H

#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {

template <typename T> struct EnumEntry {
  StringRef Name;
  // While Name suffices in most of the cases, in certain cases
  // GNU style and LLVM style of ELFDumper do not
  // display same string for same enum. The AltName if initialized appropriately
  // will hold the string that GNU style emits.
  // Example:
  // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to
  // "Advanced Micro Devices X86-64" on GNU style
  StringRef AltName;
  T Value;
  constexpr EnumEntry(StringRef N, StringRef A, T V)
      : Name(N), AltName(A), Value(V) {}
  constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
};

struct HexNumber {
  // To avoid sign-extension we have to explicitly cast to the appropriate
  // unsigned type. The overloads are here so that every type that is implicitly
  // convertible to an integer (including enums and endian helpers) can be used
  // without requiring type traits or call-site changes.
  HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
  HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
  HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
  HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
  HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
  HexNumber(signed long long Value)
      : Value(static_cast<unsigned long long>(Value)) {}
  HexNumber(unsigned char Value) : Value(Value) {}
  HexNumber(unsigned short Value) : Value(Value) {}
  HexNumber(unsigned int Value) : Value(Value) {}
  HexNumber(unsigned long Value) : Value(Value) {}
  HexNumber(unsigned long long Value) : Value(Value) {}
  uint64_t Value;
};

struct FlagEntry {
  FlagEntry(StringRef Name, char Value)
      : Name(Name), Value(static_cast<unsigned char>(Value)) {}
  FlagEntry(StringRef Name, signed char Value)
      : Name(Name), Value(static_cast<unsigned char>(Value)) {}
  FlagEntry(StringRef Name, signed short Value)
      : Name(Name), Value(static_cast<unsigned short>(Value)) {}
  FlagEntry(StringRef Name, signed int Value)
      : Name(Name), Value(static_cast<unsigned int>(Value)) {}
  FlagEntry(StringRef Name, signed long Value)
      : Name(Name), Value(static_cast<unsigned long>(Value)) {}
  FlagEntry(StringRef Name, signed long long Value)
      : Name(Name), Value(static_cast<unsigned long long>(Value)) {}
  FlagEntry(StringRef Name, unsigned char Value) : Name(Name), Value(Value) {}
  FlagEntry(StringRef Name, unsigned short Value) : Name(Name), Value(Value) {}
  FlagEntry(StringRef Name, unsigned int Value) : Name(Name), Value(Value) {}
  FlagEntry(StringRef Name, unsigned long Value) : Name(Name), Value(Value) {}
  FlagEntry(StringRef Name, unsigned long long Value)
      : Name(Name), Value(Value) {}
  StringRef Name;
  uint64_t Value;
};

raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value);
std::string to_hexString(uint64_t Value, bool UpperCase = true);

template <class T> std::string to_string(const T &Value) {
  std::string number;
  raw_string_ostream stream(number);
  stream << Value;
  return stream.str();
}

template <typename T, typename TEnum>
std::string enumToString(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
  for (const EnumEntry<TEnum> &EnumItem : EnumValues)
    if (EnumItem.Value == Value)
      return std::string(EnumItem.AltName);
  return to_hexString(Value, false);
}

class ScopedPrinter {
public:
  enum class ScopedPrinterKind {
    Base,
    JSON,
  };

  ScopedPrinter(raw_ostream &OS,
                ScopedPrinterKind Kind = ScopedPrinterKind::Base)
      : OS(OS), IndentLevel(0), Kind(Kind) {}

  ScopedPrinterKind getKind() const { return Kind; }

  static bool classof(const ScopedPrinter *SP) {
    return SP->getKind() == ScopedPrinterKind::Base;
  }

  virtual ~ScopedPrinter() = default;

  void flush() { OS.flush(); }

  void indent(int Levels = 1) { IndentLevel += Levels; }

  void unindent(int Levels = 1) {
    IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
  }

  void resetIndent() { IndentLevel = 0; }

  int getIndentLevel() { return IndentLevel; }

  void setPrefix(StringRef P) { Prefix = P; }

  void printIndent() {
    OS << Prefix;
    for (int i = 0; i < IndentLevel; ++i)
      OS << "  ";
  }

  template <typename T> HexNumber hex(T Value) { return HexNumber(Value); }

  template <typename T, typename TEnum>
  void printEnum(StringRef Label, T Value,
                 ArrayRef<EnumEntry<TEnum>> EnumValues) {
    StringRef Name;
    bool Found = false;
    for (const auto &EnumItem : EnumValues) {
      if (EnumItem.Value == Value) {
        Name = EnumItem.Name;
        Found = true;
        break;
      }
    }

    if (Found)
      printHex(Label, Name, Value);
    else
      printHex(Label, Value);
  }

  template <typename T, typename TFlag>
  void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags,
                  TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
                  TFlag EnumMask3 = {}) {
    SmallVector<FlagEntry, 10> SetFlags;

    for (const auto &Flag : Flags) {
      if (Flag.Value == 0)
        continue;

      TFlag EnumMask{};
      if (Flag.Value & EnumMask1)
        EnumMask = EnumMask1;
      else if (Flag.Value & EnumMask2)
        EnumMask = EnumMask2;
      else if (Flag.Value & EnumMask3)
        EnumMask = EnumMask3;
      bool IsEnum = (Flag.Value & EnumMask) != 0;
      if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
          (IsEnum && (Value & EnumMask) == Flag.Value)) {
        SetFlags.emplace_back(Flag.Name, Flag.Value);
      }
    }

    llvm::sort(SetFlags, &flagName);
    printFlagsImpl(Label, hex(Value), SetFlags);
  }

  template <typename T> void printFlags(StringRef Label, T Value) {
    SmallVector<HexNumber, 10> SetFlags;
    uint64_t Flag = 1;
    uint64_t Curr = Value;
    while (Curr > 0) {
      if (Curr & 1)
        SetFlags.emplace_back(Flag);
      Curr >>= 1;
      Flag <<= 1;
    }
    printFlagsImpl(Label, hex(Value), SetFlags);
  }

  virtual void printNumber(StringRef Label, uint64_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, uint32_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, uint16_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, uint8_t Value) {
    startLine() << Label << ": " << unsigned(Value) << "\n";
  }

  virtual void printNumber(StringRef Label, int64_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, int32_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, int16_t Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printNumber(StringRef Label, int8_t Value) {
    startLine() << Label << ": " << int(Value) << "\n";
  }

  virtual void printNumber(StringRef Label, const APSInt &Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  template <typename T>
  void printNumber(StringRef Label, StringRef Str, T Value) {
    printNumberImpl(Label, Str, to_string(Value));
  }

  virtual void printBoolean(StringRef Label, bool Value) {
    startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
  }

  template <typename... T> void printVersion(StringRef Label, T... Version) {
    startLine() << Label << ": ";
    printVersionInternal(Version...);
    getOStream() << "\n";
  }

  template <typename T>
  void printList(StringRef Label, const ArrayRef<T> List) {
    SmallVector<std::string, 10> StringList;
    for (const auto &Item : List)
      StringList.emplace_back(to_string(Item));
    printList(Label, StringList);
  }

  virtual void printList(StringRef Label, const ArrayRef<bool> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<std::string> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<uint64_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<uint32_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<uint16_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<uint8_t> List) {
    SmallVector<unsigned> NumberList;
    for (const uint8_t &Item : List)
      NumberList.emplace_back(Item);
    printListImpl(Label, NumberList);
  }

  virtual void printList(StringRef Label, const ArrayRef<int64_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<int32_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<int16_t> List) {
    printListImpl(Label, List);
  }

  virtual void printList(StringRef Label, const ArrayRef<int8_t> List) {
    SmallVector<int> NumberList;
    for (const int8_t &Item : List)
      NumberList.emplace_back(Item);
    printListImpl(Label, NumberList);
  }

  virtual void printList(StringRef Label, const ArrayRef<APSInt> List) {
    printListImpl(Label, List);
  }

  template <typename T, typename U>
  void printList(StringRef Label, const T &List, const U &Printer) {
    startLine() << Label << ": [";
    ListSeparator LS;
    for (const auto &Item : List) {
      OS << LS;
      Printer(OS, Item);
    }
    OS << "]\n";
  }

  template <typename T> void printHexList(StringRef Label, const T &List) {
    SmallVector<HexNumber> HexList;
    for (const auto &Item : List)
      HexList.emplace_back(Item);
    printHexListImpl(Label, HexList);
  }

  template <typename T> void printHex(StringRef Label, T Value) {
    printHexImpl(Label, hex(Value));
  }

  template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
    printHexImpl(Label, Str, hex(Value));
  }

  template <typename T>
  void printSymbolOffset(StringRef Label, StringRef Symbol, T Value) {
    printSymbolOffsetImpl(Label, Symbol, hex(Value));
  }

  virtual void printString(StringRef Value) { startLine() << Value << "\n"; }

  virtual void printString(StringRef Label, StringRef Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
    printBinaryImpl(Label, Str, Value, false);
  }

  void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
    auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
                          Value.size());
    printBinaryImpl(Label, Str, V, false);
  }

  void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
    printBinaryImpl(Label, StringRef(), Value, false);
  }

  void printBinary(StringRef Label, ArrayRef<char> Value) {
    auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
                          Value.size());
    printBinaryImpl(Label, StringRef(), V, false);
  }

  void printBinary(StringRef Label, StringRef Value) {
    auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
                          Value.size());
    printBinaryImpl(Label, StringRef(), V, false);
  }

  void printBinaryBlock(StringRef Label, ArrayRef<uint8_t> Value,
                        uint32_t StartOffset) {
    printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
  }

  void printBinaryBlock(StringRef Label, ArrayRef<uint8_t> Value) {
    printBinaryImpl(Label, StringRef(), Value, true);
  }

  void printBinaryBlock(StringRef Label, StringRef Value) {
    auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
                          Value.size());
    printBinaryImpl(Label, StringRef(), V, true);
  }

  template <typename T> void printObject(StringRef Label, const T &Value) {
    printString(Label, to_string(Value));
  }

  virtual void objectBegin() { scopedBegin('{'); }

  virtual void objectBegin(StringRef Label) { scopedBegin(Label, '{'); }

  virtual void objectEnd() { scopedEnd('}'); }

  virtual void arrayBegin() { scopedBegin('['); }

  virtual void arrayBegin(StringRef Label) { scopedBegin(Label, '['); }

  virtual void arrayEnd() { scopedEnd(']'); }

  virtual raw_ostream &startLine() {
    printIndent();
    return OS;
  }

  virtual raw_ostream &getOStream() { return OS; }

private:
  template <typename T> void printVersionInternal(T Value) {
    getOStream() << Value;
  }

  template <typename S, typename T, typename... TArgs>
  void printVersionInternal(S Value, T Value2, TArgs... Args) {
    getOStream() << Value << ".";
    printVersionInternal(Value2, Args...);
  }

  static bool flagName(const FlagEntry &LHS, const FlagEntry &RHS) {
    return LHS.Name < RHS.Name;
  }

  virtual void printBinaryImpl(StringRef Label, StringRef Str,
                               ArrayRef<uint8_t> Value, bool Block,
                               uint32_t StartOffset = 0);

  virtual void printFlagsImpl(StringRef Label, HexNumber Value,
                              ArrayRef<FlagEntry> Flags) {
    startLine() << Label << " [ (" << Value << ")\n";
    for (const auto &Flag : Flags)
      startLine() << "  " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
    startLine() << "]\n";
  }

  virtual void printFlagsImpl(StringRef Label, HexNumber Value,
                              ArrayRef<HexNumber> Flags) {
    startLine() << Label << " [ (" << Value << ")\n";
    for (const auto &Flag : Flags)
      startLine() << "  " << Flag << '\n';
    startLine() << "]\n";
  }

  template <typename T> void printListImpl(StringRef Label, const T List) {
    startLine() << Label << ": [";
    ListSeparator LS;
    for (const auto &Item : List)
      OS << LS << Item;
    OS << "]\n";
  }

  virtual void printHexListImpl(StringRef Label,
                                const ArrayRef<HexNumber> List) {
    startLine() << Label << ": [";
    ListSeparator LS;
    for (const auto &Item : List)
      OS << LS << hex(Item);
    OS << "]\n";
  }

  virtual void printHexImpl(StringRef Label, HexNumber Value) {
    startLine() << Label << ": " << Value << "\n";
  }

  virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
    startLine() << Label << ": " << Str << " (" << Value << ")\n";
  }

  virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
                                     HexNumber Value) {
    startLine() << Label << ": " << Symbol << '+' << Value << '\n';
  }

  virtual void printNumberImpl(StringRef Label, StringRef Str,
                               StringRef Value) {
    startLine() << Label << ": " << Str << " (" << Value << ")\n";
  }

  void scopedBegin(char Symbol) {
    startLine() << Symbol << '\n';
    indent();
  }

  void scopedBegin(StringRef Label, char Symbol) {
    startLine() << Label;
    if (!Label.empty())
      OS << ' ';
    OS << Symbol << '\n';
    indent();
  }

  void scopedEnd(char Symbol) {
    unindent();
    startLine() << Symbol << '\n';
  }

  raw_ostream &OS;
  int IndentLevel;
  StringRef Prefix;
  ScopedPrinterKind Kind;
};

template <>
inline void
ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
                                              support::ulittle16_t Value) {
  startLine() << Label << ": " << hex(Value) << "\n";
}

struct DelimitedScope;

class JSONScopedPrinter : public ScopedPrinter {
private:
  enum class Scope {
    Array,
    Object,
  };

  enum class ScopeKind {
    NoAttribute,
    Attribute,
    NestedAttribute,
  };

  struct ScopeContext {
    Scope Context;
    ScopeKind Kind;
    ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
        : Context(Context), Kind(Kind) {}
  };

  SmallVector<ScopeContext, 8> ScopeHistory;
  json::OStream JOS;
  std::unique_ptr<DelimitedScope> OuterScope;

public:
  JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint = false,
                    std::unique_ptr<DelimitedScope> &&OuterScope =
                        std::unique_ptr<DelimitedScope>{});

  static bool classof(const ScopedPrinter *SP) {
    return SP->getKind() == ScopedPrinter::ScopedPrinterKind::JSON;
  }

  void printNumber(StringRef Label, uint64_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, uint32_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, uint16_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, uint8_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, int64_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, int32_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, int16_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, int8_t Value) override {
    JOS.attribute(Label, Value);
  }

  void printNumber(StringRef Label, const APSInt &Value) override {
    JOS.attributeBegin(Label);
    printAPSInt(Value);
    JOS.attributeEnd();
  }

  void printBoolean(StringRef Label, bool Value) override {
    JOS.attribute(Label, Value);
  }

  void printList(StringRef Label, const ArrayRef<bool> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<std::string> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<uint64_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<uint32_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<uint16_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<uint8_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<int64_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<int32_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<int16_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<int8_t> List) override {
    printListImpl(Label, List);
  }

  void printList(StringRef Label, const ArrayRef<APSInt> List) override {
    JOS.attributeArray(Label, [&]() {
      for (const APSInt &Item : List) {
        printAPSInt(Item);
      }
    });
  }

  void printString(StringRef Value) override { JOS.value(Value); }

  void printString(StringRef Label, StringRef Value) override {
    JOS.attribute(Label, Value);
  }

  void objectBegin() override {
    scopedBegin({Scope::Object, ScopeKind::NoAttribute});
  }

  void objectBegin(StringRef Label) override {
    scopedBegin(Label, Scope::Object);
  }

  void objectEnd() override { scopedEnd(); }

  void arrayBegin() override {
    scopedBegin({Scope::Array, ScopeKind::NoAttribute});
  }

  void arrayBegin(StringRef Label) override {
    scopedBegin(Label, Scope::Array);
  }

  void arrayEnd() override { scopedEnd(); }

private:
  // Output HexNumbers as decimals so that they're easier to parse.
  uint64_t hexNumberToInt(HexNumber Hex) { return Hex.Value; }

  void printAPSInt(const APSInt &Value) {
    JOS.rawValueBegin() << Value;
    JOS.rawValueEnd();
  }

  void printFlagsImpl(StringRef Label, HexNumber Value,
                      ArrayRef<FlagEntry> Flags) override {
    JOS.attributeObject(Label, [&]() {
      JOS.attribute("RawFlags", hexNumberToInt(Value));
      JOS.attributeArray("Flags", [&]() {
        for (const FlagEntry &Flag : Flags) {
          JOS.objectBegin();
          JOS.attribute("Name", Flag.Name);
          JOS.attribute("Value", Flag.Value);
          JOS.objectEnd();
        }
      });
    });
  }

  void printFlagsImpl(StringRef Label, HexNumber Value,
                      ArrayRef<HexNumber> Flags) override {
    JOS.attributeObject(Label, [&]() {
      JOS.attribute("RawFlags", hexNumberToInt(Value));
      JOS.attributeArray("Flags", [&]() {
        for (const HexNumber &Flag : Flags) {
          JOS.value(Flag.Value);
        }
      });
    });
  }

  template <typename T> void printListImpl(StringRef Label, const T &List) {
    JOS.attributeArray(Label, [&]() {
      for (const auto &Item : List)
        JOS.value(Item);
    });
  }

  void printHexListImpl(StringRef Label,
                        const ArrayRef<HexNumber> List) override {
    JOS.attributeArray(Label, [&]() {
      for (const HexNumber &Item : List) {
        JOS.value(hexNumberToInt(Item));
      }
    });
  }

  void printHexImpl(StringRef Label, HexNumber Value) override {
    JOS.attribute(Label, hexNumberToInt(Value));
  }

  void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) override {
    JOS.attributeObject(Label, [&]() {
      JOS.attribute("Value", Str);
      JOS.attribute("RawValue", hexNumberToInt(Value));
    });
  }

  void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
                             HexNumber Value) override {
    JOS.attributeObject(Label, [&]() {
      JOS.attribute("SymName", Symbol);
      JOS.attribute("Offset", hexNumberToInt(Value));
    });
  }

  void printNumberImpl(StringRef Label, StringRef Str,
                       StringRef Value) override {
    JOS.attributeObject(Label, [&]() {
      JOS.attribute("Value", Str);
      JOS.attributeBegin("RawValue");
      JOS.rawValueBegin() << Value;
      JOS.rawValueEnd();
      JOS.attributeEnd();
    });
  }

  void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
                       bool Block, uint32_t StartOffset = 0) override {
    JOS.attributeObject(Label, [&]() {
      if (!Str.empty())
        JOS.attribute("Value", Str);
      JOS.attribute("Offset", StartOffset);
      JOS.attributeArray("Bytes", [&]() {
        for (uint8_t Val : Value)
          JOS.value(Val);
      });
    });
  }

  void scopedBegin(ScopeContext ScopeCtx) {
    if (ScopeCtx.Context == Scope::Object)
      JOS.objectBegin();
    else if (ScopeCtx.Context == Scope::Array)
      JOS.arrayBegin();
    ScopeHistory.push_back(ScopeCtx);
  }

  void scopedBegin(StringRef Label, Scope Ctx) {
    ScopeKind Kind = ScopeKind::Attribute;
    if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
      JOS.objectBegin();
      Kind = ScopeKind::NestedAttribute;
    }
    JOS.attributeBegin(Label);
    scopedBegin({Ctx, Kind});
  }

  void scopedEnd() {
    ScopeContext ScopeCtx = ScopeHistory.back();
    if (ScopeCtx.Context == Scope::Object)
      JOS.objectEnd();
    else if (ScopeCtx.Context == Scope::Array)
      JOS.arrayEnd();
    if (ScopeCtx.Kind == ScopeKind::Attribute ||
        ScopeCtx.Kind == ScopeKind::NestedAttribute)
      JOS.attributeEnd();
    if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
      JOS.objectEnd();
    ScopeHistory.pop_back();
  }
};

struct DelimitedScope {
  DelimitedScope(ScopedPrinter &W) : W(&W) {}
  DelimitedScope() : W(nullptr) {}
  virtual ~DelimitedScope() = default;
  virtual void setPrinter(ScopedPrinter &W) = 0;
  ScopedPrinter *W;
};

struct DictScope : DelimitedScope {
  explicit DictScope() = default;
  explicit DictScope(ScopedPrinter &W) : DelimitedScope(W) { W.objectBegin(); }

  DictScope(ScopedPrinter &W, StringRef N) : DelimitedScope(W) {
    W.objectBegin(N);
  }

  void setPrinter(ScopedPrinter &W) override {
    this->W = &W;
    W.objectBegin();
  }

  ~DictScope() {
    if (W)
      W->objectEnd();
  }
};

struct ListScope : DelimitedScope {
  explicit ListScope() = default;
  explicit ListScope(ScopedPrinter &W) : DelimitedScope(W) { W.arrayBegin(); }

  ListScope(ScopedPrinter &W, StringRef N) : DelimitedScope(W) {
    W.arrayBegin(N);
  }

  void setPrinter(ScopedPrinter &W) override {
    this->W = &W;
    W.arrayBegin();
  }

  ~ListScope() {
    if (W)
      W->arrayEnd();
  }
};

} // namespace llvm

#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif