aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/DWARFLinker/Parallel/SyntheticTypeNameBuilder.cpp
blob: 1554946c2c047b2485ad4def32c84f8b1fff00b3 (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
//===- SyntheticTypeNameBuilder.cpp ---------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "SyntheticTypeNameBuilder.h"
#include "DWARFLinkerCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
#include "llvm/Support/ScopedPrinter.h"

using namespace llvm;
using namespace dwarf_linker;
using namespace dwarf_linker::parallel;

Error SyntheticTypeNameBuilder::assignName(
    UnitEntryPairTy InputUnitEntryPair,
    std::optional<std::pair<size_t, size_t>> ChildIndex) {
  [[maybe_unused]] const CompileUnit::DIEInfo &Info =
      InputUnitEntryPair.CU->getDIEInfo(InputUnitEntryPair.DieEntry);
  assert(Info.needToPlaceInTypeTable() &&
         "Cann't assign name for non-type DIE");

  if (InputUnitEntryPair.CU->getDieTypeEntry(InputUnitEntryPair.DieEntry) !=
      nullptr)
    return Error::success();

  SyntheticName.resize(0);
  RecursionDepth = 0;
  return addDIETypeName(InputUnitEntryPair, ChildIndex, true);
}

void SyntheticTypeNameBuilder::addArrayDimension(
    UnitEntryPairTy InputUnitEntryPair) {
  for (const DWARFDebugInfoEntry *CurChild =
           InputUnitEntryPair.CU->getFirstChildEntry(
               InputUnitEntryPair.DieEntry);
       CurChild && CurChild->getAbbreviationDeclarationPtr();
       CurChild = InputUnitEntryPair.CU->getSiblingEntry(CurChild)) {
    if (CurChild->getTag() == dwarf::DW_TAG_subrange_type ||
        CurChild->getTag() == dwarf::DW_TAG_generic_subrange) {
      SyntheticName += "[";
      if (std::optional<DWARFFormValue> Val =
              InputUnitEntryPair.CU->find(CurChild, dwarf::DW_AT_count)) {
        if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant()) {
          SyntheticName += std::to_string(*ConstVal);
        } else if (std::optional<int64_t> ConstVal =
                       Val->getAsSignedConstant()) {
          SyntheticName += std::to_string(*ConstVal);
        }
      }

      SyntheticName += "]";
    }
  }
}

static dwarf::Attribute TypeAttr[] = {dwarf::DW_AT_type};
Error SyntheticTypeNameBuilder::addSignature(UnitEntryPairTy InputUnitEntryPair,
                                             bool addTemplateParameters) {
  // Add entry type.
  if (Error Err = addReferencedODRDies(InputUnitEntryPair, false, TypeAttr))
    return Err;
  SyntheticName += ':';

  SmallVector<const DWARFDebugInfoEntry *, 10> TemplateParameters;
  SmallVector<const DWARFDebugInfoEntry *, 20> FunctionParameters;
  for (const DWARFDebugInfoEntry *CurChild =
           InputUnitEntryPair.CU->getFirstChildEntry(
               InputUnitEntryPair.DieEntry);
       CurChild && CurChild->getAbbreviationDeclarationPtr();
       CurChild = InputUnitEntryPair.CU->getSiblingEntry(CurChild)) {
    dwarf::Tag ChildTag = CurChild->getTag();
    if (addTemplateParameters &&
        (ChildTag == dwarf::DW_TAG_template_type_parameter ||
         ChildTag == dwarf::DW_TAG_template_value_parameter))
      TemplateParameters.push_back(CurChild);
    else if (ChildTag == dwarf::DW_TAG_formal_parameter ||
             ChildTag == dwarf::DW_TAG_unspecified_parameters)
      FunctionParameters.push_back(CurChild);
    else if (addTemplateParameters &&
             ChildTag == dwarf::DW_TAG_GNU_template_parameter_pack) {
      for (const DWARFDebugInfoEntry *CurGNUChild =
               InputUnitEntryPair.CU->getFirstChildEntry(CurChild);
           CurGNUChild && CurGNUChild->getAbbreviationDeclarationPtr();
           CurGNUChild = InputUnitEntryPair.CU->getSiblingEntry(CurGNUChild))
        TemplateParameters.push_back(CurGNUChild);
    } else if (ChildTag == dwarf::DW_TAG_GNU_formal_parameter_pack) {
      for (const DWARFDebugInfoEntry *CurGNUChild =
               InputUnitEntryPair.CU->getFirstChildEntry(CurChild);
           CurGNUChild && CurGNUChild->getAbbreviationDeclarationPtr();
           CurGNUChild = InputUnitEntryPair.CU->getSiblingEntry(CurGNUChild))
        FunctionParameters.push_back(CurGNUChild);
    }
  }

  // Add parameters.
  if (Error Err = addParamNames(*InputUnitEntryPair.CU, FunctionParameters))
    return Err;

  // Add template parameters.
  if (Error Err =
          addTemplateParamNames(*InputUnitEntryPair.CU, TemplateParameters))
    return Err;

  return Error::success();
}

Error SyntheticTypeNameBuilder::addParamNames(
    CompileUnit &CU,
    SmallVector<const DWARFDebugInfoEntry *, 20> &FunctionParameters) {
  SyntheticName += '(';
  for (const DWARFDebugInfoEntry *FunctionParameter : FunctionParameters) {
    if (SyntheticName.back() != '(')
      SyntheticName += ", ";
    if (dwarf::toUnsigned(CU.find(FunctionParameter, dwarf::DW_AT_artificial),
                          0))
      SyntheticName += "^";
    if (Error Err = addReferencedODRDies(
            UnitEntryPairTy{&CU, FunctionParameter}, false, TypeAttr))
      return Err;
  }
  SyntheticName += ')';
  return Error::success();
}

Error SyntheticTypeNameBuilder::addTemplateParamNames(
    CompileUnit &CU,
    SmallVector<const DWARFDebugInfoEntry *, 10> &TemplateParameters) {
  if (!TemplateParameters.empty()) {
    SyntheticName += '<';
    for (const DWARFDebugInfoEntry *Parameter : TemplateParameters) {
      if (SyntheticName.back() != '<')
        SyntheticName += ", ";

      if (Parameter->getTag() == dwarf::DW_TAG_template_value_parameter) {
        if (std::optional<DWARFFormValue> Val =
                CU.find(Parameter, dwarf::DW_AT_const_value)) {
          if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant())
            SyntheticName += std::to_string(*ConstVal);
          else if (std::optional<int64_t> ConstVal = Val->getAsSignedConstant())
            SyntheticName += std::to_string(*ConstVal);
        }
      }

      if (Error Err = addReferencedODRDies(UnitEntryPairTy{&CU, Parameter},
                                           false, TypeAttr))
        return Err;
    }
    SyntheticName += '>';
  }
  return Error::success();
}

void SyntheticTypeNameBuilder::addOrderedName(
    std::pair<size_t, size_t> ChildIdx) {
  std::string Name;
  llvm::raw_string_ostream stream(Name);
  stream << format_hex_no_prefix(ChildIdx.first, ChildIdx.second);
  SyntheticName += Name;
}

// Examine DIE and return type deduplication candidate: some DIEs could not be
// deduplicated, namespace may refer to another namespace.
static std::optional<UnitEntryPairTy>
getTypeDeduplicationCandidate(UnitEntryPairTy UnitEntryPair) {
  switch (UnitEntryPair.DieEntry->getTag()) {
  case dwarf::DW_TAG_null:
  case dwarf::DW_TAG_compile_unit:
  case dwarf::DW_TAG_partial_unit:
  case dwarf::DW_TAG_type_unit:
  case dwarf::DW_TAG_skeleton_unit: {
    return std::nullopt;
  }
  case dwarf::DW_TAG_namespace: {
    // Check if current namespace refers another.
    if (UnitEntryPair.CU->find(UnitEntryPair.DieEntry, dwarf::DW_AT_extension))
      UnitEntryPair = UnitEntryPair.getNamespaceOrigin();

    // Content of anonimous namespaces should not be deduplicated.
    if (!UnitEntryPair.CU->find(UnitEntryPair.DieEntry, dwarf::DW_AT_name))
      llvm_unreachable("Cann't deduplicate anonimous namespace");

    return UnitEntryPair;
  }
  default:
    return UnitEntryPair;
  }
}

Error SyntheticTypeNameBuilder::addParentName(
    UnitEntryPairTy &InputUnitEntryPair) {
  std::optional<UnitEntryPairTy> UnitEntryPair = InputUnitEntryPair.getParent();
  if (!UnitEntryPair)
    return Error::success();

  UnitEntryPair = getTypeDeduplicationCandidate(*UnitEntryPair);
  if (!UnitEntryPair)
    return Error::success();

  if (TypeEntry *ImmediateParentName =
          UnitEntryPair->CU->getDieTypeEntry(UnitEntryPair->DieEntry)) {
    SyntheticName += ImmediateParentName->getKey();
    SyntheticName += ".";
    return Error::success();
  }

  // Collect parent entries.
  SmallVector<UnitEntryPairTy, 10> Parents;
  do {
    Parents.push_back(*UnitEntryPair);

    UnitEntryPair = UnitEntryPair->getParent();
    if (!UnitEntryPair)
      break;

    UnitEntryPair = getTypeDeduplicationCandidate(*UnitEntryPair);
    if (!UnitEntryPair)
      break;

  } while (!UnitEntryPair->CU->getDieTypeEntry(UnitEntryPair->DieEntry));

  // Assign name for each parent entry.
  size_t NameStart = SyntheticName.size();
  for (UnitEntryPairTy Parent : reverse(Parents)) {
    SyntheticName.resize(NameStart);
    if (Error Err = addDIETypeName(Parent, std::nullopt, true))
      return Err;
  }

  // Add parents delimiter.
  SyntheticName += ".";
  return Error::success();
}

void SyntheticTypeNameBuilder::addDieNameFromDeclFileAndDeclLine(
    UnitEntryPairTy &InputUnitEntryPair, bool &HasDeclFileName) {
  if (std::optional<DWARFFormValue> DeclFileVal = InputUnitEntryPair.CU->find(
          InputUnitEntryPair.DieEntry, dwarf::DW_AT_decl_file)) {
    if (std::optional<DWARFFormValue> DeclLineVal = InputUnitEntryPair.CU->find(
            InputUnitEntryPair.DieEntry, dwarf::DW_AT_decl_line)) {
      if (std::optional<std::pair<StringRef, StringRef>> DirAndFilename =
              InputUnitEntryPair.CU->getDirAndFilenameFromLineTable(
                  *DeclFileVal)) {
        SyntheticName += DirAndFilename->first;
        SyntheticName += DirAndFilename->second;

        if (std::optional<uint64_t> DeclLineIntVal =
                dwarf::toUnsigned(*DeclLineVal)) {
          SyntheticName += " ";
          SyntheticName += utohexstr(*DeclLineIntVal);
        }

        HasDeclFileName = true;
      }
    }
  }
}

void SyntheticTypeNameBuilder::addValueName(UnitEntryPairTy InputUnitEntryPair,
                                            dwarf::Attribute Attr) {
  if (std::optional<DWARFFormValue> Val =
          InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry, Attr)) {
    if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant()) {
      SyntheticName += " ";
      SyntheticName += std::to_string(*ConstVal);
    } else if (std::optional<int64_t> ConstVal = Val->getAsSignedConstant()) {
      SyntheticName += " ";
      SyntheticName += std::to_string(*ConstVal);
    }
  }
}

Error SyntheticTypeNameBuilder::addReferencedODRDies(
    UnitEntryPairTy InputUnitEntryPair, bool AssignNameToTypeDescriptor,
    ArrayRef<dwarf::Attribute> ODRAttrs) {
  bool FirstIteration = true;
  for (dwarf::Attribute Attr : ODRAttrs) {
    if (std::optional<DWARFFormValue> AttrValue =
            InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry, Attr)) {
      std::optional<UnitEntryPairTy> RefDie =
          InputUnitEntryPair.CU->resolveDIEReference(
              *AttrValue, ResolveInterCUReferencesMode::Resolve);

      if (!RefDie)
        continue;

      if (!RefDie->DieEntry)
        return createStringError(std::errc::invalid_argument,
                                 "Cann't resolve DIE reference");

      if (!FirstIteration)
        SyntheticName += ",";

      RecursionDepth++;
      if (RecursionDepth > 1000)
        return createStringError(
            std::errc::invalid_argument,
            "Cann't parse input DWARF. Recursive dependence.");

      if (Error Err =
              addDIETypeName(*RefDie, std::nullopt, AssignNameToTypeDescriptor))
        return Err;
      RecursionDepth--;
      FirstIteration = false;
    }
  }

  return Error::success();
}

Error SyntheticTypeNameBuilder::addTypeName(UnitEntryPairTy InputUnitEntryPair,
                                            bool AddParentNames) {
  bool HasLinkageName = false;
  bool HasShortName = false;
  bool HasTemplatesInShortName = false;
  bool HasDeclFileName = false;

  // Try to get name from the DIE.
  if (std::optional<DWARFFormValue> Val = InputUnitEntryPair.CU->find(
          InputUnitEntryPair.DieEntry,
          {dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_AT_linkage_name})) {
    // Firstly check for linkage name.
    SyntheticName += dwarf::toStringRef(Val);
    HasLinkageName = true;
  } else if (std::optional<DWARFFormValue> Val = InputUnitEntryPair.CU->find(
                 InputUnitEntryPair.DieEntry, dwarf::DW_AT_name)) {
    // Then check for short name.
    StringRef Name = dwarf::toStringRef(Val);
    SyntheticName += Name;

    HasShortName = true;
    HasTemplatesInShortName =
        Name.ends_with(">") && Name.count("<") != 0 && !Name.ends_with("<=>");
  } else {
    // Finally check for declaration attributes.
    addDieNameFromDeclFileAndDeclLine(InputUnitEntryPair, HasDeclFileName);
  }

  // Add additional name parts for some DIEs.
  switch (InputUnitEntryPair.DieEntry->getTag()) {
  case dwarf::DW_TAG_union_type:
  case dwarf::DW_TAG_interface_type:
  case dwarf::DW_TAG_class_type:
  case dwarf::DW_TAG_structure_type:
  case dwarf::DW_TAG_subroutine_type:
  case dwarf::DW_TAG_subprogram: {
    if (InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry,
                                    dwarf::DW_AT_artificial))
      SyntheticName += "^";

    // No need to add signature information for linkage name,
    // also no need to add template parameters name if short name already
    // includes them.
    if (!HasLinkageName)
      if (Error Err =
              addSignature(InputUnitEntryPair, !HasTemplatesInShortName))
        return Err;
  } break;
  case dwarf::DW_TAG_coarray_type:
  case dwarf::DW_TAG_array_type: {
    addArrayDimension(InputUnitEntryPair);
  } break;
  case dwarf::DW_TAG_subrange_type: {
    addValueName(InputUnitEntryPair, dwarf::DW_AT_count);
  } break;
  case dwarf::DW_TAG_template_value_parameter: {
    if (!HasTemplatesInShortName) {
      // TODO add support for DW_AT_location
      addValueName(InputUnitEntryPair, dwarf::DW_AT_const_value);
    }
  } break;
  default: {
    // Nothing to do.
  } break;
  }

  // If name for the DIE is not determined yet add referenced types to the name.
  if (!HasLinkageName && !HasShortName && !HasDeclFileName) {
    if (InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry,
                                    getODRAttributes()))
      if (Error Err = addReferencedODRDies(InputUnitEntryPair, AddParentNames,
                                           getODRAttributes()))
        return Err;
  }

  return Error::success();
}

Error SyntheticTypeNameBuilder::addDIETypeName(
    UnitEntryPairTy InputUnitEntryPair,
    std::optional<std::pair<size_t, size_t>> ChildIndex,
    bool AssignNameToTypeDescriptor) {
  std::optional<UnitEntryPairTy> UnitEntryPair =
      getTypeDeduplicationCandidate(InputUnitEntryPair);
  if (!UnitEntryPair)
    return Error::success();

  TypeEntry *TypeEntryPtr =
      InputUnitEntryPair.CU->getDieTypeEntry(InputUnitEntryPair.DieEntry);
  // Check if DIE already has a name.
  if (!TypeEntryPtr) {
    size_t NameStart = SyntheticName.size();
    if (AssignNameToTypeDescriptor) {
      if (Error Err = addParentName(*UnitEntryPair))
        return Err;
    }
    addTypePrefix(UnitEntryPair->DieEntry);

    if (ChildIndex) {
      addOrderedName(*ChildIndex);
    } else {
      if (Error Err = addTypeName(*UnitEntryPair, AssignNameToTypeDescriptor))
        return Err;
    }

    if (AssignNameToTypeDescriptor) {
      // Add built name to the DIE.
      TypeEntryPtr = TypePoolRef.insert(SyntheticName.substr(NameStart));
      InputUnitEntryPair.CU->setDieTypeEntry(InputUnitEntryPair.DieEntry,
                                             TypeEntryPtr);
    }
  } else
    SyntheticName += TypeEntryPtr->getKey();

  return Error::success();
}

void SyntheticTypeNameBuilder::addTypePrefix(
    const DWARFDebugInfoEntry *DieEntry) {
  switch (DieEntry->getTag()) {
  case dwarf::DW_TAG_base_type: {
    SyntheticName += "{0}";
  } break;
  case dwarf::DW_TAG_namespace: {
    SyntheticName += "{1}";
  } break;
  case dwarf::DW_TAG_formal_parameter: {
    SyntheticName += "{2}";
  } break;
  // dwarf::DW_TAG_unspecified_parameters have the same prefix as before.
  case dwarf::DW_TAG_unspecified_parameters: {
    SyntheticName += "{2}";
  } break;
  case dwarf::DW_TAG_template_type_parameter: {
    SyntheticName += "{3}";
  } break;
  // dwarf::DW_TAG_template_value_parameter have the same prefix as before.
  case dwarf::DW_TAG_template_value_parameter: {
    SyntheticName += "{3}";
  } break;
  case dwarf::DW_TAG_GNU_formal_parameter_pack: {
    SyntheticName += "{4}";
  } break;
  case dwarf::DW_TAG_GNU_template_parameter_pack: {
    SyntheticName += "{5}";
  } break;
  case dwarf::DW_TAG_inheritance: {
    SyntheticName += "{6}";
  } break;
  case dwarf::DW_TAG_array_type: {
    SyntheticName += "{7}";
  } break;
  case dwarf::DW_TAG_class_type: {
    SyntheticName += "{8}";
  } break;
  case dwarf::DW_TAG_enumeration_type: {
    SyntheticName += "{9}";
  } break;
  case dwarf::DW_TAG_imported_declaration: {
    SyntheticName += "{A}";
  } break;
  case dwarf::DW_TAG_member: {
    SyntheticName += "{B}";
  } break;
  case dwarf::DW_TAG_pointer_type: {
    SyntheticName += "{C}";
  } break;
  case dwarf::DW_TAG_reference_type: {
    SyntheticName += "{D}";
  } break;
  case dwarf::DW_TAG_string_type: {
    SyntheticName += "{E}";
  } break;
  case dwarf::DW_TAG_structure_type: {
    SyntheticName += "{F}";
  } break;
  case dwarf::DW_TAG_subroutine_type: {
    SyntheticName += "{G}";
  } break;
  case dwarf::DW_TAG_typedef: {
    SyntheticName += "{H}";
  } break;
  case dwarf::DW_TAG_union_type: {
    SyntheticName += "{I}";
  } break;
  case dwarf::DW_TAG_variant: {
    SyntheticName += "{J}";
  } break;
  case dwarf::DW_TAG_inlined_subroutine: {
    SyntheticName += "{K}";
  } break;
  case dwarf::DW_TAG_module: {
    SyntheticName += "{L}";
  } break;
  case dwarf::DW_TAG_ptr_to_member_type: {
    SyntheticName += "{M}";
  } break;
  case dwarf::DW_TAG_set_type: {
    SyntheticName += "{N}";
  } break;
  case dwarf::DW_TAG_subrange_type: {
    SyntheticName += "{O}";
  } break;
  case dwarf::DW_TAG_with_stmt: {
    SyntheticName += "{P}";
  } break;
  case dwarf::DW_TAG_access_declaration: {
    SyntheticName += "{Q}";
  } break;
  case dwarf::DW_TAG_catch_block: {
    SyntheticName += "{R}";
  } break;
  case dwarf::DW_TAG_const_type: {
    SyntheticName += "{S}";
  } break;
  case dwarf::DW_TAG_constant: {
    SyntheticName += "{T}";
  } break;
  case dwarf::DW_TAG_enumerator: {
    SyntheticName += "{U}";
  } break;
  case dwarf::DW_TAG_file_type: {
    SyntheticName += "{V}";
  } break;
  case dwarf::DW_TAG_friend: {
    SyntheticName += "{W}";
  } break;
  case dwarf::DW_TAG_namelist: {
    SyntheticName += "{X}";
  } break;
  case dwarf::DW_TAG_namelist_item: {
    SyntheticName += "{Y}";
  } break;
  case dwarf::DW_TAG_packed_type: {
    SyntheticName += "{Z}";
  } break;
  case dwarf::DW_TAG_subprogram: {
    SyntheticName += "{a}";
  } break;
  case dwarf::DW_TAG_thrown_type: {
    SyntheticName += "{b}";
  } break;
  case dwarf::DW_TAG_variant_part: {
    SyntheticName += "{c}";
  } break;
  case dwarf::DW_TAG_variable: {
    SyntheticName += "{d}";
  } break;
  case dwarf::DW_TAG_volatile_type: {
    SyntheticName += "{e}";
  } break;
  case dwarf::DW_TAG_dwarf_procedure: {
    SyntheticName += "{f}";
  } break;
  case dwarf::DW_TAG_restrict_type: {
    SyntheticName += "{g}";
  } break;
  case dwarf::DW_TAG_interface_type: {
    SyntheticName += "{h}";
  } break;
  case dwarf::DW_TAG_imported_module: {
    SyntheticName += "{i}";
  } break;
  case dwarf::DW_TAG_unspecified_type: {
    SyntheticName += "{j}";
  } break;
  case dwarf::DW_TAG_imported_unit: {
    SyntheticName += "{k}";
  } break;
  case dwarf::DW_TAG_condition: {
    SyntheticName += "{l}";
  } break;
  case dwarf::DW_TAG_shared_type: {
    SyntheticName += "{m}";
  } break;
  case dwarf::DW_TAG_rvalue_reference_type: {
    SyntheticName += "{n}";
  } break;
  case dwarf::DW_TAG_template_alias: {
    SyntheticName += "{o}";
  } break;
  case dwarf::DW_TAG_coarray_type: {
    SyntheticName += "{p}";
  } break;
  case dwarf::DW_TAG_generic_subrange: {
    SyntheticName += "{q}";
  } break;
  case dwarf::DW_TAG_dynamic_type: {
    SyntheticName += "{r}";
  } break;
  case dwarf::DW_TAG_atomic_type: {
    SyntheticName += "{s}";
  } break;
  case dwarf::DW_TAG_call_site: {
    SyntheticName += "{t}";
  } break;
  case dwarf::DW_TAG_call_site_parameter: {
    SyntheticName += "{u}";
  } break;
  case dwarf::DW_TAG_immutable_type: {
    SyntheticName += "{v}";
  } break;
  case dwarf::DW_TAG_entry_point: {
    SyntheticName += "{w}";
  } break;
  case dwarf::DW_TAG_label: {
    SyntheticName += "{x}";
  } break;
  case dwarf::DW_TAG_lexical_block: {
    SyntheticName += "{y}";
  } break;
  case dwarf::DW_TAG_common_block: {
    SyntheticName += "{z}";
  } break;
  case dwarf::DW_TAG_common_inclusion: {
    SyntheticName += "{|}";
  } break;
  case dwarf::DW_TAG_try_block: {
    SyntheticName += "{~}";
  } break;

  case dwarf::DW_TAG_null: {
    llvm_unreachable("No type prefix for DW_TAG_null");
  } break;
  case dwarf::DW_TAG_compile_unit: {
    llvm_unreachable("No type prefix for DW_TAG_compile_unit");
  } break;
  case dwarf::DW_TAG_partial_unit: {
    llvm_unreachable("No type prefix for DW_TAG_partial_unit");
  } break;
  case dwarf::DW_TAG_type_unit: {
    llvm_unreachable("No type prefix for DW_TAG_type_unit");
  } break;
  case dwarf::DW_TAG_skeleton_unit: {
    llvm_unreachable("No type prefix for DW_TAG_skeleton_unit");
  } break;

  default: {
    SyntheticName += "{~~";
    SyntheticName += utohexstr(DieEntry->getTag());
    SyntheticName += "}";
  } break;
  }
}

OrderedChildrenIndexAssigner::OrderedChildrenIndexAssigner(
    CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry) {
  switch (DieEntry->getTag()) {
  case dwarf::DW_TAG_array_type:
  case dwarf::DW_TAG_coarray_type:
  case dwarf::DW_TAG_class_type:
  case dwarf::DW_TAG_common_block:
  case dwarf::DW_TAG_lexical_block:
  case dwarf::DW_TAG_structure_type:
  case dwarf::DW_TAG_subprogram:
  case dwarf::DW_TAG_subroutine_type:
  case dwarf::DW_TAG_union_type:
  case dwarf::DW_TAG_GNU_template_template_param:
  case dwarf::DW_TAG_GNU_formal_parameter_pack: {
    NeedCountChildren = true;
  } break;
  case dwarf::DW_TAG_enumeration_type: {
    // TODO : do we need to add condition
    NeedCountChildren = true;
  } break;
  default: {
    // Nothing to do.
  }
  }

  // Calculate maximal index value
  if (NeedCountChildren) {
    for (const DWARFDebugInfoEntry *CurChild = CU.getFirstChildEntry(DieEntry);
         CurChild && CurChild->getAbbreviationDeclarationPtr();
         CurChild = CU.getSiblingEntry(CurChild)) {
      std::optional<size_t> ArrayIndex = tagToArrayIndex(CU, CurChild);
      if (!ArrayIndex)
        continue;

      assert((*ArrayIndex < ChildIndexesWidth.size()) &&
             "Wrong index for ChildIndexesWidth");
      ChildIndexesWidth[*ArrayIndex]++;
    }

    // Calculate index field width(number of digits in hexadecimal
    // representation).
    for (size_t &Width : ChildIndexesWidth) {
      size_t digitsCounter = 1;
      size_t NumToCompare = 15;

      while (NumToCompare < Width) {
        NumToCompare <<= 4;
        digitsCounter++;
      }

      Width = digitsCounter;
    }
  }
}

std::optional<size_t> OrderedChildrenIndexAssigner::tagToArrayIndex(
    CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry) {
  if (!NeedCountChildren)
    return std::nullopt;

  switch (DieEntry->getTag()) {
  case dwarf::DW_TAG_unspecified_parameters:
  case dwarf::DW_TAG_formal_parameter:
    return 0;
  case dwarf::DW_TAG_template_value_parameter:
  case dwarf::DW_TAG_template_type_parameter:
    return 1;
  case dwarf::DW_TAG_enumeration_type:
    if (std::optional<uint32_t> ParentIdx = DieEntry->getParentIdx()) {
      if (*ParentIdx && CU.getDebugInfoEntry(*ParentIdx)->getTag() ==
                            dwarf::DW_TAG_array_type)
        return 2;
    }
    return std::nullopt;
  case dwarf::DW_TAG_subrange_type:
    return 3;
  case dwarf::DW_TAG_generic_subrange:
    return 4;
  case dwarf::DW_TAG_enumerator:
    return 5;
  case dwarf::DW_TAG_namelist_item:
    return 6;
  case dwarf::DW_TAG_member:
    return 7;
  default:
    return std::nullopt;
  };
}

std::optional<std::pair<size_t, size_t>>
OrderedChildrenIndexAssigner::getChildIndex(
    CompileUnit &CU, const DWARFDebugInfoEntry *ChildDieEntry) {
  std::optional<size_t> ArrayIndex = tagToArrayIndex(CU, ChildDieEntry);
  if (!ArrayIndex)
    return std::nullopt;

  assert((*ArrayIndex < OrderedChildIdxs.size()) &&
         "Wrong index for ChildIndexesWidth");
  assert(ChildIndexesWidth[*ArrayIndex] < 16 &&
         "Index width exceeds 16 digits.");

  std::pair<size_t, size_t> Result = std::make_pair(
      OrderedChildIdxs[*ArrayIndex], ChildIndexesWidth[*ArrayIndex]);
  OrderedChildIdxs[*ArrayIndex]++;
  return Result;
}