1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for constructing a dwarf compile unit.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfUnit.h"
14 #include "AddressPool.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCDwarf.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include <cassert>
33 #include <cstdint>
34 #include <string>
35 #include <utility>
36 
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "dwarfdebug"
40 
41 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP,
42                                        DwarfCompileUnit &CU, DIELoc &DIE)
43     : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
44 
45 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
46   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op);
47 }
48 
49 void DIEDwarfExpression::emitSigned(int64_t Value) {
50   CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value);
51 }
52 
53 void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
54   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value);
55 }
56 
57 void DIEDwarfExpression::emitData1(uint8_t Value) {
58   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value);
59 }
60 
61 void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
62   CU.addBaseTypeRef(getActiveDIE(), Idx);
63 }
64 
65 void DIEDwarfExpression::enableTemporaryBuffer() {
66   assert(!IsBuffering && "Already buffering?");
67   IsBuffering = true;
68 }
69 
70 void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
71 
72 unsigned DIEDwarfExpression::getTemporaryBufferSize() {
73   return TmpDIE.computeSize(AP.getDwarfFormParams());
74 }
75 
76 void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); }
77 
78 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
79                                          llvm::Register MachineReg) {
80   return MachineReg == TRI.getFrameRegister(*AP.MF);
81 }
82 
83 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
84                      AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
85     : DIEUnit(UnitTag), CUNode(Node), Asm(A), DD(DW), DU(DWU) {}
86 
87 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
88                              DwarfDebug *DW, DwarfFile *DWU,
89                              MCDwarfDwoLineTable *SplitLineTable)
90     : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
91       SplitLineTable(SplitLineTable) {
92 }
93 
94 DwarfUnit::~DwarfUnit() {
95   for (DIEBlock *B : DIEBlocks)
96     B->~DIEBlock();
97   for (DIELoc *L : DIELocs)
98     L->~DIELoc();
99 }
100 
101 int64_t DwarfUnit::getDefaultLowerBound() const {
102   switch (getLanguage()) {
103   default:
104     break;
105 
106   // The languages below have valid values in all DWARF versions.
107   case dwarf::DW_LANG_C:
108   case dwarf::DW_LANG_C89:
109   case dwarf::DW_LANG_C_plus_plus:
110     return 0;
111 
112   case dwarf::DW_LANG_Fortran77:
113   case dwarf::DW_LANG_Fortran90:
114     return 1;
115 
116   // The languages below have valid values only if the DWARF version >= 3.
117   case dwarf::DW_LANG_C99:
118   case dwarf::DW_LANG_ObjC:
119   case dwarf::DW_LANG_ObjC_plus_plus:
120     if (DD->getDwarfVersion() >= 3)
121       return 0;
122     break;
123 
124   case dwarf::DW_LANG_Fortran95:
125     if (DD->getDwarfVersion() >= 3)
126       return 1;
127     break;
128 
129   // Starting with DWARF v4, all defined languages have valid values.
130   case dwarf::DW_LANG_D:
131   case dwarf::DW_LANG_Java:
132   case dwarf::DW_LANG_Python:
133   case dwarf::DW_LANG_UPC:
134     if (DD->getDwarfVersion() >= 4)
135       return 0;
136     break;
137 
138   case dwarf::DW_LANG_Ada83:
139   case dwarf::DW_LANG_Ada95:
140   case dwarf::DW_LANG_Cobol74:
141   case dwarf::DW_LANG_Cobol85:
142   case dwarf::DW_LANG_Modula2:
143   case dwarf::DW_LANG_Pascal83:
144   case dwarf::DW_LANG_PLI:
145     if (DD->getDwarfVersion() >= 4)
146       return 1;
147     break;
148 
149   // The languages below are new in DWARF v5.
150   case dwarf::DW_LANG_BLISS:
151   case dwarf::DW_LANG_C11:
152   case dwarf::DW_LANG_C_plus_plus_03:
153   case dwarf::DW_LANG_C_plus_plus_11:
154   case dwarf::DW_LANG_C_plus_plus_14:
155   case dwarf::DW_LANG_Dylan:
156   case dwarf::DW_LANG_Go:
157   case dwarf::DW_LANG_Haskell:
158   case dwarf::DW_LANG_OCaml:
159   case dwarf::DW_LANG_OpenCL:
160   case dwarf::DW_LANG_RenderScript:
161   case dwarf::DW_LANG_Rust:
162   case dwarf::DW_LANG_Swift:
163     if (DD->getDwarfVersion() >= 5)
164       return 0;
165     break;
166 
167   case dwarf::DW_LANG_Fortran03:
168   case dwarf::DW_LANG_Fortran08:
169   case dwarf::DW_LANG_Julia:
170   case dwarf::DW_LANG_Modula3:
171     if (DD->getDwarfVersion() >= 5)
172       return 1;
173     break;
174   }
175 
176   return -1;
177 }
178 
179 /// Check whether the DIE for this MDNode can be shared across CUs.
180 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
181   // When the MDNode can be part of the type system, the DIE can be shared
182   // across CUs.
183   // Combining type units and cross-CU DIE sharing is lower value (since
184   // cross-CU DIE sharing is used in LTO and removes type redundancy at that
185   // level already) but may be implementable for some value in projects
186   // building multiple independent libraries with LTO and then linking those
187   // together.
188   if (isDwoUnit() && !DD->shareAcrossDWOCUs())
189     return false;
190   return (isa<DIType>(D) ||
191           (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
192          !DD->generateTypeUnits();
193 }
194 
195 DIE *DwarfUnit::getDIE(const DINode *D) const {
196   if (isShareableAcrossCUs(D))
197     return DU->getDIE(D);
198   return MDNodeToDieMap.lookup(D);
199 }
200 
201 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
202   if (isShareableAcrossCUs(Desc)) {
203     DU->insertDIE(Desc, D);
204     return;
205   }
206   MDNodeToDieMap.insert(std::make_pair(Desc, D));
207 }
208 
209 void DwarfUnit::insertDIE(DIE *D) {
210   MDNodeToDieMap.insert(std::make_pair(nullptr, D));
211 }
212 
213 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
214   if (DD->getDwarfVersion() >= 4)
215     addAttribute(Die, Attribute, dwarf::DW_FORM_flag_present, DIEInteger(1));
216   else
217     addAttribute(Die, Attribute, dwarf::DW_FORM_flag, DIEInteger(1));
218 }
219 
220 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
221                         std::optional<dwarf::Form> Form, uint64_t Integer) {
222   if (!Form)
223     Form = DIEInteger::BestForm(false, Integer);
224   assert(Form != dwarf::DW_FORM_implicit_const &&
225          "DW_FORM_implicit_const is used only for signed integers");
226   addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
227 }
228 
229 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
230                         uint64_t Integer) {
231   addUInt(Block, (dwarf::Attribute)0, Form, Integer);
232 }
233 
234 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
235                         std::optional<dwarf::Form> Form, int64_t Integer) {
236   if (!Form)
237     Form = DIEInteger::BestForm(true, Integer);
238   addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
239 }
240 
241 void DwarfUnit::addSInt(DIELoc &Die, std::optional<dwarf::Form> Form,
242                         int64_t Integer) {
243   addSInt(Die, (dwarf::Attribute)0, Form, Integer);
244 }
245 
246 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
247                           StringRef String) {
248   if (CUNode->isDebugDirectivesOnly())
249     return;
250 
251   if (DD->useInlineStrings()) {
252     addAttribute(Die, Attribute, dwarf::DW_FORM_string,
253                  new (DIEValueAllocator)
254                      DIEInlineString(String, DIEValueAllocator));
255     return;
256   }
257   dwarf::Form IxForm =
258       isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
259 
260   auto StringPoolEntry =
261       useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
262           ? DU->getStringPool().getIndexedEntry(*Asm, String)
263           : DU->getStringPool().getEntry(*Asm, String);
264 
265   // For DWARF v5 and beyond, use the smallest strx? form possible.
266   if (useSegmentedStringOffsetsTable()) {
267     IxForm = dwarf::DW_FORM_strx1;
268     unsigned Index = StringPoolEntry.getIndex();
269     if (Index > 0xffffff)
270       IxForm = dwarf::DW_FORM_strx4;
271     else if (Index > 0xffff)
272       IxForm = dwarf::DW_FORM_strx3;
273     else if (Index > 0xff)
274       IxForm = dwarf::DW_FORM_strx2;
275   }
276   addAttribute(Die, Attribute, IxForm, DIEString(StringPoolEntry));
277 }
278 
279 void DwarfUnit::addLabel(DIEValueList &Die, dwarf::Attribute Attribute,
280                          dwarf::Form Form, const MCSymbol *Label) {
281   addAttribute(Die, Attribute, Form, DIELabel(Label));
282 }
283 
284 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
285   addLabel(Die, (dwarf::Attribute)0, Form, Label);
286 }
287 
288 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
289                                  uint64_t Integer) {
290   addUInt(Die, Attribute, DD->getDwarfSectionOffsetForm(), Integer);
291 }
292 
293 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
294   if (!SplitLineTable)
295     return getCU().getOrCreateSourceID(File);
296   if (!UsedLineTable) {
297     UsedLineTable = true;
298     // This is a split type unit that needs a line table.
299     addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
300   }
301   return SplitLineTable->getFile(
302       File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
303       Asm->OutContext.getDwarfVersion(), File->getSource());
304 }
305 
306 void DwarfUnit::addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label) {
307   bool UseAddrOffsetFormOrExpressions =
308       DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
309 
310   const MCSymbol *Base = nullptr;
311   if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
312     Base = DD->getSectionLabel(&Label->getSection());
313 
314   uint32_t Index = DD->getAddressPool().getIndex(Base ? Base : Label);
315 
316   if (DD->getDwarfVersion() >= 5) {
317     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
318     addUInt(Die, dwarf::DW_FORM_addrx, Index);
319   } else {
320     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
321     addUInt(Die, dwarf::DW_FORM_GNU_addr_index, Index);
322   }
323 
324   if (Base && Base != Label) {
325     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_const4u);
326     addLabelDelta(Die, (dwarf::Attribute)0, Label, Base);
327     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
328   }
329 }
330 
331 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
332   if (DD->getDwarfVersion() >= 5) {
333     addPoolOpAddress(Die, Sym);
334     return;
335   }
336 
337   if (DD->useSplitDwarf()) {
338     addPoolOpAddress(Die, Sym);
339     return;
340   }
341 
342   addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
343   addLabel(Die, dwarf::DW_FORM_addr, Sym);
344 }
345 
346 void DwarfUnit::addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,
347                               const MCSymbol *Hi, const MCSymbol *Lo) {
348   addAttribute(Die, Attribute, dwarf::DW_FORM_data4,
349                new (DIEValueAllocator) DIEDelta(Hi, Lo));
350 }
351 
352 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
353   addDIEEntry(Die, Attribute, DIEEntry(Entry));
354 }
355 
356 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
357   // Flag the type unit reference as a declaration so that if it contains
358   // members (implicit special members, static data member definitions, member
359   // declarations for definitions in this CU, etc) consumers don't get confused
360   // and think this is a full definition.
361   addFlag(Die, dwarf::DW_AT_declaration);
362 
363   addAttribute(Die, dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
364                DIEInteger(Signature));
365 }
366 
367 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
368                             DIEEntry Entry) {
369   const DIEUnit *CU = Die.getUnit();
370   const DIEUnit *EntryCU = Entry.getEntry().getUnit();
371   if (!CU)
372     // We assume that Die belongs to this CU, if it is not linked to any CU yet.
373     CU = getUnitDie().getUnit();
374   if (!EntryCU)
375     EntryCU = getUnitDie().getUnit();
376   assert(EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() ||
377          !static_cast<const DwarfUnit*>(CU)->isDwoUnit());
378   addAttribute(Die, Attribute,
379                EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
380                Entry);
381 }
382 
383 DIE &DwarfUnit::createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N) {
384   DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, Tag));
385   if (N)
386     insertDIE(N, &Die);
387   return Die;
388 }
389 
390 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
391   Loc->computeSize(Asm->getDwarfFormParams());
392   DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
393   addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
394 }
395 
396 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
397                          DIEBlock *Block) {
398   Block->computeSize(Asm->getDwarfFormParams());
399   DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
400   addAttribute(Die, Attribute, Form, Block);
401 }
402 
403 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
404                          DIEBlock *Block) {
405   addBlock(Die, Attribute, Block->BestForm(), Block);
406 }
407 
408 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
409   if (Line == 0)
410     return;
411 
412   unsigned FileID = getOrCreateSourceID(File);
413   addUInt(Die, dwarf::DW_AT_decl_file, std::nullopt, FileID);
414   addUInt(Die, dwarf::DW_AT_decl_line, std::nullopt, Line);
415 }
416 
417 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
418   assert(V);
419 
420   addSourceLine(Die, V->getLine(), V->getFile());
421 }
422 
423 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
424   assert(G);
425 
426   addSourceLine(Die, G->getLine(), G->getFile());
427 }
428 
429 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
430   assert(SP);
431 
432   addSourceLine(Die, SP->getLine(), SP->getFile());
433 }
434 
435 void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) {
436   assert(L);
437 
438   addSourceLine(Die, L->getLine(), L->getFile());
439 }
440 
441 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
442   assert(Ty);
443 
444   addSourceLine(Die, Ty->getLine(), Ty->getFile());
445 }
446 
447 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
448   assert(Ty);
449 
450   addSourceLine(Die, Ty->getLine(), Ty->getFile());
451 }
452 
453 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
454   // Pass this down to addConstantValue as an unsigned bag of bits.
455   addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
456 }
457 
458 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
459                                  const DIType *Ty) {
460   addConstantValue(Die, CI->getValue(), Ty);
461 }
462 
463 void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
464   addConstantValue(Die, DD->isUnsignedDIType(Ty), Val);
465 }
466 
467 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
468   // FIXME: This is a bit conservative/simple - it emits negative values always
469   // sign extended to 64 bits rather than minimizing the number of bytes.
470   addUInt(Die, dwarf::DW_AT_const_value,
471           Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
472 }
473 
474 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
475   addConstantValue(Die, Val, DD->isUnsignedDIType(Ty));
476 }
477 
478 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
479   unsigned CIBitWidth = Val.getBitWidth();
480   if (CIBitWidth <= 64) {
481     addConstantValue(Die, Unsigned,
482                      Unsigned ? Val.getZExtValue() : Val.getSExtValue());
483     return;
484   }
485 
486   DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
487 
488   // Get the raw data form of the large APInt.
489   const uint64_t *Ptr64 = Val.getRawData();
490 
491   int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
492   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
493 
494   // Output the constant to DWARF one byte at a time.
495   for (int i = 0; i < NumBytes; i++) {
496     uint8_t c;
497     if (LittleEndian)
498       c = Ptr64[i / 8] >> (8 * (i & 7));
499     else
500       c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
501     addUInt(*Block, dwarf::DW_FORM_data1, c);
502   }
503 
504   addBlock(Die, dwarf::DW_AT_const_value, Block);
505 }
506 
507 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
508   if (!LinkageName.empty())
509     addString(Die,
510               DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
511                                          : dwarf::DW_AT_MIPS_linkage_name,
512               GlobalValue::dropLLVMManglingEscape(LinkageName));
513 }
514 
515 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
516   // Add template parameters.
517   for (const auto *Element : TParams) {
518     if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
519       constructTemplateTypeParameterDIE(Buffer, TTP);
520     else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
521       constructTemplateValueParameterDIE(Buffer, TVP);
522   }
523 }
524 
525 /// Add thrown types.
526 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
527   for (const auto *Ty : ThrownTypes) {
528     DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
529     addType(TT, cast<DIType>(Ty));
530   }
531 }
532 
533 void DwarfUnit::addAccess(DIE &Die, DINode::DIFlags Flags) {
534   if ((Flags & DINode::FlagAccessibility) == DINode::FlagProtected)
535     addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
536             dwarf::DW_ACCESS_protected);
537   else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPrivate)
538     addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
539             dwarf::DW_ACCESS_private);
540   else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPublic)
541     addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
542             dwarf::DW_ACCESS_public);
543 }
544 
545 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
546   if (!Context || isa<DIFile>(Context) || isa<DICompileUnit>(Context))
547     return &getUnitDie();
548   if (auto *T = dyn_cast<DIType>(Context))
549     return getOrCreateTypeDIE(T);
550   if (auto *NS = dyn_cast<DINamespace>(Context))
551     return getOrCreateNameSpace(NS);
552   if (auto *SP = dyn_cast<DISubprogram>(Context))
553     return getOrCreateSubprogramDIE(SP);
554   if (auto *M = dyn_cast<DIModule>(Context))
555     return getOrCreateModule(M);
556   return getDIE(Context);
557 }
558 
559 DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) {
560   auto *Context = Ty->getScope();
561   DIE *ContextDIE = getOrCreateContextDIE(Context);
562 
563   if (DIE *TyDIE = getDIE(Ty))
564     return TyDIE;
565 
566   // Create new type.
567   DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
568 
569   constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
570 
571   updateAcceleratorTables(Context, Ty, TyDIE);
572   return &TyDIE;
573 }
574 
575 DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
576                               const DIType *Ty) {
577   // Create new type.
578   DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty);
579 
580   updateAcceleratorTables(Context, Ty, TyDIE);
581 
582   if (auto *BT = dyn_cast<DIBasicType>(Ty))
583     constructTypeDIE(TyDIE, BT);
584   else if (auto *ST = dyn_cast<DIStringType>(Ty))
585     constructTypeDIE(TyDIE, ST);
586   else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
587     constructTypeDIE(TyDIE, STy);
588   else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
589     if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
590         (Ty->getRawName() || CTy->getRawIdentifier())) {
591       // Skip updating the accelerator tables since this is not the full type.
592       if (MDString *TypeId = CTy->getRawIdentifier())
593         DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
594       else
595         finishNonUnitTypeDIE(TyDIE, CTy);
596       return &TyDIE;
597     }
598     constructTypeDIE(TyDIE, CTy);
599   } else {
600     constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
601   }
602 
603   return &TyDIE;
604 }
605 
606 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
607   if (!TyNode)
608     return nullptr;
609 
610   auto *Ty = cast<DIType>(TyNode);
611 
612   // DW_TAG_restrict_type is not supported in DWARF2
613   if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
614     return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
615 
616   // DW_TAG_atomic_type is not supported in DWARF < 5
617   if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
618     return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
619 
620   // Construct the context before querying for the existence of the DIE in case
621   // such construction creates the DIE.
622   auto *Context = Ty->getScope();
623   DIE *ContextDIE = getOrCreateContextDIE(Context);
624   assert(ContextDIE);
625 
626   if (DIE *TyDIE = getDIE(Ty))
627     return TyDIE;
628 
629   return static_cast<DwarfUnit *>(ContextDIE->getUnit())
630       ->createTypeDIE(Context, *ContextDIE, Ty);
631 }
632 
633 void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
634                                         const DIType *Ty, const DIE &TyDIE) {
635   if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
636     bool IsImplementation = false;
637     if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
638       // A runtime language of 0 actually means C/C++ and that any
639       // non-negative value is some version of Objective-C/C++.
640       IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
641     }
642     unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
643     DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags);
644 
645     if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
646         isa<DINamespace>(Context) || isa<DICommonBlock>(Context))
647       addGlobalType(Ty, TyDIE, Context);
648   }
649 }
650 
651 void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
652                         dwarf::Attribute Attribute) {
653   assert(Ty && "Trying to add a type that doesn't exist?");
654   addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
655 }
656 
657 std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
658   if (!Context)
659     return "";
660 
661   // FIXME: Decide whether to implement this for non-C++ languages.
662   if (!dwarf::isCPlusPlus((dwarf::SourceLanguage)getLanguage()))
663     return "";
664 
665   std::string CS;
666   SmallVector<const DIScope *, 1> Parents;
667   while (!isa<DICompileUnit>(Context)) {
668     Parents.push_back(Context);
669     if (const DIScope *S = Context->getScope())
670       Context = S;
671     else
672       // Structure, etc types will have a NULL context if they're at the top
673       // level.
674       break;
675   }
676 
677   // Reverse iterate over our list to go from the outermost construct to the
678   // innermost.
679   for (const DIScope *Ctx : llvm::reverse(Parents)) {
680     StringRef Name = Ctx->getName();
681     if (Name.empty() && isa<DINamespace>(Ctx))
682       Name = "(anonymous namespace)";
683     if (!Name.empty()) {
684       CS += Name;
685       CS += "::";
686     }
687   }
688   return CS;
689 }
690 
691 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
692   // Get core information.
693   StringRef Name = BTy->getName();
694   // Add name if not anonymous or intermediate type.
695   if (!Name.empty())
696     addString(Buffer, dwarf::DW_AT_name, Name);
697 
698   // An unspecified type only has a name attribute.
699   if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
700     return;
701 
702   if (BTy->getTag() != dwarf::DW_TAG_string_type)
703     addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
704             BTy->getEncoding());
705 
706   uint64_t Size = BTy->getSizeInBits() >> 3;
707   addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
708 
709   if (BTy->isBigEndian())
710     addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_big);
711   else if (BTy->isLittleEndian())
712     addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_little);
713 }
714 
715 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
716   // Get core information.
717   StringRef Name = STy->getName();
718   // Add name if not anonymous or intermediate type.
719   if (!Name.empty())
720     addString(Buffer, dwarf::DW_AT_name, Name);
721 
722   if (DIVariable *Var = STy->getStringLength()) {
723     if (auto *VarDIE = getDIE(Var))
724       addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
725   } else if (DIExpression *Expr = STy->getStringLengthExp()) {
726     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
727     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
728     // This is to describe the memory location of the
729     // length of a Fortran deferred length string, so
730     // lock it down as such.
731     DwarfExpr.setMemoryLocationKind();
732     DwarfExpr.addExpression(Expr);
733     addBlock(Buffer, dwarf::DW_AT_string_length, DwarfExpr.finalize());
734   } else {
735     uint64_t Size = STy->getSizeInBits() >> 3;
736     addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
737   }
738 
739   if (DIExpression *Expr = STy->getStringLocationExp()) {
740     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
741     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
742     // This is to describe the memory location of the
743     // string, so lock it down as such.
744     DwarfExpr.setMemoryLocationKind();
745     DwarfExpr.addExpression(Expr);
746     addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
747   }
748 
749   if (STy->getEncoding()) {
750     // For eventual Unicode support.
751     addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
752             STy->getEncoding());
753   }
754 }
755 
756 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
757   // Get core information.
758   StringRef Name = DTy->getName();
759   uint64_t Size = DTy->getSizeInBits() >> 3;
760   uint16_t Tag = Buffer.getTag();
761 
762   // Map to main type, void will not have a type.
763   const DIType *FromTy = DTy->getBaseType();
764   if (FromTy)
765     addType(Buffer, FromTy);
766 
767   // Add name if not anonymous or intermediate type.
768   if (!Name.empty())
769     addString(Buffer, dwarf::DW_AT_name, Name);
770 
771   addAnnotation(Buffer, DTy->getAnnotations());
772 
773   // If alignment is specified for a typedef , create and insert DW_AT_alignment
774   // attribute in DW_TAG_typedef DIE.
775   if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
776     uint32_t AlignInBytes = DTy->getAlignInBytes();
777     if (AlignInBytes > 0)
778       addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
779               AlignInBytes);
780   }
781 
782   // Add size if non-zero (derived types might be zero-sized.)
783   if (Size && Tag != dwarf::DW_TAG_pointer_type
784            && Tag != dwarf::DW_TAG_ptr_to_member_type
785            && Tag != dwarf::DW_TAG_reference_type
786            && Tag != dwarf::DW_TAG_rvalue_reference_type)
787     addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
788 
789   if (Tag == dwarf::DW_TAG_ptr_to_member_type)
790     addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
791                 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType()));
792 
793   addAccess(Buffer, DTy->getFlags());
794 
795   // Add source line info if available and TyDesc is not a forward declaration.
796   if (!DTy->isForwardDecl())
797     addSourceLine(Buffer, DTy);
798 
799   // If DWARF address space value is other than None, add it.  The IR
800   // verifier checks that DWARF address space only exists for pointer
801   // or reference types.
802   if (DTy->getDWARFAddressSpace())
803     addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
804             *DTy->getDWARFAddressSpace());
805 }
806 
807 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
808   for (unsigned i = 1, N = Args.size(); i < N; ++i) {
809     const DIType *Ty = Args[i];
810     if (!Ty) {
811       assert(i == N-1 && "Unspecified parameter must be the last argument");
812       createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
813     } else {
814       DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
815       addType(Arg, Ty);
816       if (Ty->isArtificial())
817         addFlag(Arg, dwarf::DW_AT_artificial);
818     }
819   }
820 }
821 
822 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
823   // Add return type.  A void return won't have a type.
824   auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
825   if (Elements.size())
826     if (auto RTy = Elements[0])
827       addType(Buffer, RTy);
828 
829   bool isPrototyped = true;
830   if (Elements.size() == 2 && !Elements[1])
831     isPrototyped = false;
832 
833   constructSubprogramArguments(Buffer, Elements);
834 
835   // Add prototype flag if we're dealing with a C language and the function has
836   // been prototyped.
837   if (isPrototyped && dwarf::isC((dwarf::SourceLanguage)getLanguage()))
838     addFlag(Buffer, dwarf::DW_AT_prototyped);
839 
840   // Add a DW_AT_calling_convention if this has an explicit convention.
841   if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
842     addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
843             CTy->getCC());
844 
845   if (CTy->isLValueReference())
846     addFlag(Buffer, dwarf::DW_AT_reference);
847 
848   if (CTy->isRValueReference())
849     addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
850 }
851 
852 void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
853   if (!Annotations)
854     return;
855 
856   for (const Metadata *Annotation : Annotations->operands()) {
857     const MDNode *MD = cast<MDNode>(Annotation);
858     const MDString *Name = cast<MDString>(MD->getOperand(0));
859     const auto &Value = MD->getOperand(1);
860 
861     DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
862     addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
863     if (const auto *Data = dyn_cast<MDString>(Value))
864       addString(AnnotationDie, dwarf::DW_AT_const_value, Data->getString());
865     else if (const auto *Data = dyn_cast<ConstantAsMetadata>(Value))
866       addConstantValue(AnnotationDie, Data->getValue()->getUniqueInteger(),
867                        /*Unsigned=*/true);
868     else
869       assert(false && "Unsupported annotation value type");
870   }
871 }
872 
873 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
874   // Add name if not anonymous or intermediate type.
875   StringRef Name = CTy->getName();
876 
877   uint64_t Size = CTy->getSizeInBits() >> 3;
878   uint16_t Tag = Buffer.getTag();
879 
880   switch (Tag) {
881   case dwarf::DW_TAG_array_type:
882     constructArrayTypeDIE(Buffer, CTy);
883     break;
884   case dwarf::DW_TAG_enumeration_type:
885     constructEnumTypeDIE(Buffer, CTy);
886     break;
887   case dwarf::DW_TAG_variant_part:
888   case dwarf::DW_TAG_structure_type:
889   case dwarf::DW_TAG_union_type:
890   case dwarf::DW_TAG_class_type:
891   case dwarf::DW_TAG_namelist: {
892     // Emit the discriminator for a variant part.
893     DIDerivedType *Discriminator = nullptr;
894     if (Tag == dwarf::DW_TAG_variant_part) {
895       Discriminator = CTy->getDiscriminator();
896       if (Discriminator) {
897         // DWARF says:
898         //    If the variant part has a discriminant, the discriminant is
899         //    represented by a separate debugging information entry which is
900         //    a child of the variant part entry.
901         DIE &DiscMember = constructMemberDIE(Buffer, Discriminator);
902         addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember);
903       }
904     }
905 
906     // Add template parameters to a class, structure or union types.
907     if (Tag == dwarf::DW_TAG_class_type ||
908         Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
909       addTemplateParams(Buffer, CTy->getTemplateParams());
910 
911     // Add elements to structure type.
912     DINodeArray Elements = CTy->getElements();
913     for (const auto *Element : Elements) {
914       if (!Element)
915         continue;
916       if (auto *SP = dyn_cast<DISubprogram>(Element))
917         getOrCreateSubprogramDIE(SP);
918       else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
919         if (DDTy->getTag() == dwarf::DW_TAG_friend) {
920           DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
921           addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend);
922         } else if (DDTy->isStaticMember()) {
923           getOrCreateStaticMemberDIE(DDTy);
924         } else if (Tag == dwarf::DW_TAG_variant_part) {
925           // When emitting a variant part, wrap each member in
926           // DW_TAG_variant.
927           DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
928           if (const ConstantInt *CI =
929               dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) {
930             if (DD->isUnsignedDIType(Discriminator->getBaseType()))
931               addUInt(Variant, dwarf::DW_AT_discr_value, std::nullopt,
932                       CI->getZExtValue());
933             else
934               addSInt(Variant, dwarf::DW_AT_discr_value, std::nullopt,
935                       CI->getSExtValue());
936           }
937           constructMemberDIE(Variant, DDTy);
938         } else {
939           constructMemberDIE(Buffer, DDTy);
940         }
941       } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
942         DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
943         StringRef PropertyName = Property->getName();
944         addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
945         if (Property->getType())
946           addType(ElemDie, Property->getType());
947         addSourceLine(ElemDie, Property);
948         StringRef GetterName = Property->getGetterName();
949         if (!GetterName.empty())
950           addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
951         StringRef SetterName = Property->getSetterName();
952         if (!SetterName.empty())
953           addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
954         if (unsigned PropertyAttributes = Property->getAttributes())
955           addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, std::nullopt,
956                   PropertyAttributes);
957       } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
958         if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
959           DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
960           constructTypeDIE(VariantPart, Composite);
961         }
962       } else if (Tag == dwarf::DW_TAG_namelist) {
963         auto *Var = dyn_cast<DINode>(Element);
964         auto *VarDIE = getDIE(Var);
965         if (VarDIE) {
966           DIE &ItemDie = createAndAddDIE(dwarf::DW_TAG_namelist_item, Buffer);
967           addDIEEntry(ItemDie, dwarf::DW_AT_namelist_item, *VarDIE);
968         }
969       }
970     }
971 
972     if (CTy->isAppleBlockExtension())
973       addFlag(Buffer, dwarf::DW_AT_APPLE_block);
974 
975     if (CTy->getExportSymbols())
976       addFlag(Buffer, dwarf::DW_AT_export_symbols);
977 
978     // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
979     // inside C++ composite types to point to the base class with the vtable.
980     // Rust uses DW_AT_containing_type to link a vtable to the type
981     // for which it was created.
982     if (auto *ContainingType = CTy->getVTableHolder())
983       addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
984                   *getOrCreateTypeDIE(ContainingType));
985 
986     if (CTy->isObjcClassComplete())
987       addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
988 
989     // Add the type's non-standard calling convention.
990     // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
991     if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
992       uint8_t CC = 0;
993       if (CTy->isTypePassByValue())
994         CC = dwarf::DW_CC_pass_by_value;
995       else if (CTy->isTypePassByReference())
996         CC = dwarf::DW_CC_pass_by_reference;
997       if (CC)
998         addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
999                 CC);
1000     }
1001     break;
1002   }
1003   default:
1004     break;
1005   }
1006 
1007   // Add name if not anonymous or intermediate type.
1008   if (!Name.empty())
1009     addString(Buffer, dwarf::DW_AT_name, Name);
1010 
1011   addAnnotation(Buffer, CTy->getAnnotations());
1012 
1013   if (Tag == dwarf::DW_TAG_enumeration_type ||
1014       Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1015       Tag == dwarf::DW_TAG_union_type) {
1016     // Add size if non-zero (derived types might be zero-sized.)
1017     // Ignore the size if it's a non-enum forward decl.
1018     // TODO: Do we care about size for enum forward declarations?
1019     if (Size &&
1020         (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
1021       addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
1022     else if (!CTy->isForwardDecl())
1023       // Add zero size if it is not a forward declaration.
1024       addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, 0);
1025 
1026     // If we're a forward decl, say so.
1027     if (CTy->isForwardDecl())
1028       addFlag(Buffer, dwarf::DW_AT_declaration);
1029 
1030     // Add accessibility info if available.
1031     addAccess(Buffer, CTy->getFlags());
1032 
1033     // Add source line info if available.
1034     if (!CTy->isForwardDecl())
1035       addSourceLine(Buffer, CTy);
1036 
1037     // No harm in adding the runtime language to the declaration.
1038     unsigned RLang = CTy->getRuntimeLang();
1039     if (RLang)
1040       addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1041               RLang);
1042 
1043     // Add align info if available.
1044     if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1045       addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1046               AlignInBytes);
1047   }
1048 }
1049 
1050 void DwarfUnit::constructTemplateTypeParameterDIE(
1051     DIE &Buffer, const DITemplateTypeParameter *TP) {
1052   DIE &ParamDIE =
1053       createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1054   // Add the type if it exists, it could be void and therefore no type.
1055   if (TP->getType())
1056     addType(ParamDIE, TP->getType());
1057   if (!TP->getName().empty())
1058     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1059   if (TP->isDefault() && isCompatibleWithVersion(5))
1060     addFlag(ParamDIE, dwarf::DW_AT_default_value);
1061 }
1062 
1063 void DwarfUnit::constructTemplateValueParameterDIE(
1064     DIE &Buffer, const DITemplateValueParameter *VP) {
1065   DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1066 
1067   // Add the type if there is one, template template and template parameter
1068   // packs will not have a type.
1069   if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1070     addType(ParamDIE, VP->getType());
1071   if (!VP->getName().empty())
1072     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1073   if (VP->isDefault() && isCompatibleWithVersion(5))
1074     addFlag(ParamDIE, dwarf::DW_AT_default_value);
1075   if (Metadata *Val = VP->getValue()) {
1076     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1077       addConstantValue(ParamDIE, CI, VP->getType());
1078     else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1079       // We cannot describe the location of dllimport'd entities: the
1080       // computation of their address requires loads from the IAT.
1081       if (!GV->hasDLLImportStorageClass()) {
1082         // For declaration non-type template parameters (such as global values
1083         // and functions)
1084         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1085         addOpAddress(*Loc, Asm->getSymbol(GV));
1086         // Emit DW_OP_stack_value to use the address as the immediate value of
1087         // the parameter, rather than a pointer to it.
1088         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1089         addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1090       }
1091     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1092       assert(isa<MDString>(Val));
1093       addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1094                 cast<MDString>(Val)->getString());
1095     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1096       addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1097     }
1098   }
1099 }
1100 
1101 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1102   // Construct the context before querying for the existence of the DIE in case
1103   // such construction creates the DIE.
1104   DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1105 
1106   if (DIE *NDie = getDIE(NS))
1107     return NDie;
1108   DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1109 
1110   StringRef Name = NS->getName();
1111   if (!Name.empty())
1112     addString(NDie, dwarf::DW_AT_name, NS->getName());
1113   else
1114     Name = "(anonymous namespace)";
1115   DD->addAccelNamespace(*CUNode, Name, NDie);
1116   addGlobalName(Name, NDie, NS->getScope());
1117   if (NS->getExportSymbols())
1118     addFlag(NDie, dwarf::DW_AT_export_symbols);
1119   return &NDie;
1120 }
1121 
1122 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1123   // Construct the context before querying for the existence of the DIE in case
1124   // such construction creates the DIE.
1125   DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1126 
1127   if (DIE *MDie = getDIE(M))
1128     return MDie;
1129   DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1130 
1131   if (!M->getName().empty()) {
1132     addString(MDie, dwarf::DW_AT_name, M->getName());
1133     addGlobalName(M->getName(), MDie, M->getScope());
1134   }
1135   if (!M->getConfigurationMacros().empty())
1136     addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1137               M->getConfigurationMacros());
1138   if (!M->getIncludePath().empty())
1139     addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1140   if (!M->getAPINotesFile().empty())
1141     addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile());
1142   if (M->getFile())
1143     addUInt(MDie, dwarf::DW_AT_decl_file, std::nullopt,
1144             getOrCreateSourceID(M->getFile()));
1145   if (M->getLineNo())
1146     addUInt(MDie, dwarf::DW_AT_decl_line, std::nullopt, M->getLineNo());
1147   if (M->getIsDecl())
1148     addFlag(MDie, dwarf::DW_AT_declaration);
1149 
1150   return &MDie;
1151 }
1152 
1153 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1154   // Construct the context before querying for the existence of the DIE in case
1155   // such construction creates the DIE (as is the case for member function
1156   // declarations).
1157   DIE *ContextDIE =
1158       Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope());
1159 
1160   if (DIE *SPDie = getDIE(SP))
1161     return SPDie;
1162 
1163   if (auto *SPDecl = SP->getDeclaration()) {
1164     if (!Minimal) {
1165       // Add subprogram definitions to the CU die directly.
1166       ContextDIE = &getUnitDie();
1167       // Build the decl now to ensure it precedes the definition.
1168       getOrCreateSubprogramDIE(SPDecl);
1169     }
1170   }
1171 
1172   // DW_TAG_inlined_subroutine may refer to this DIE.
1173   DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1174 
1175   // Stop here and fill this in later, depending on whether or not this
1176   // subprogram turns out to have inlined instances or not.
1177   if (SP->isDefinition())
1178     return &SPDie;
1179 
1180   static_cast<DwarfUnit *>(SPDie.getUnit())
1181       ->applySubprogramAttributes(SP, SPDie);
1182   return &SPDie;
1183 }
1184 
1185 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1186                                                     DIE &SPDie, bool Minimal) {
1187   DIE *DeclDie = nullptr;
1188   StringRef DeclLinkageName;
1189   if (auto *SPDecl = SP->getDeclaration()) {
1190     if (!Minimal) {
1191       DITypeRefArray DeclArgs, DefinitionArgs;
1192       DeclArgs = SPDecl->getType()->getTypeArray();
1193       DefinitionArgs = SP->getType()->getTypeArray();
1194 
1195       if (DeclArgs.size() && DefinitionArgs.size())
1196         if (DefinitionArgs[0] != nullptr && DeclArgs[0] != DefinitionArgs[0])
1197           addType(SPDie, DefinitionArgs[0]);
1198 
1199       DeclDie = getDIE(SPDecl);
1200       assert(DeclDie && "This DIE should've already been constructed when the "
1201                         "definition DIE was created in "
1202                         "getOrCreateSubprogramDIE");
1203       // Look at the Decl's linkage name only if we emitted it.
1204       if (DD->useAllLinkageNames())
1205         DeclLinkageName = SPDecl->getLinkageName();
1206       unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1207       unsigned DefID = getOrCreateSourceID(SP->getFile());
1208       if (DeclID != DefID)
1209         addUInt(SPDie, dwarf::DW_AT_decl_file, std::nullopt, DefID);
1210 
1211       if (SP->getLine() != SPDecl->getLine())
1212         addUInt(SPDie, dwarf::DW_AT_decl_line, std::nullopt, SP->getLine());
1213     }
1214   }
1215 
1216   // Add function template parameters.
1217   addTemplateParams(SPDie, SP->getTemplateParams());
1218 
1219   // Add the linkage name if we have one and it isn't in the Decl.
1220   StringRef LinkageName = SP->getLinkageName();
1221   assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1222           LinkageName == DeclLinkageName) &&
1223          "decl has a linkage name and it is different");
1224   if (DeclLinkageName.empty() &&
1225       // Always emit it for abstract subprograms.
1226       (DD->useAllLinkageNames() || DU->getAbstractScopeDIEs().lookup(SP)))
1227     addLinkageName(SPDie, LinkageName);
1228 
1229   if (!DeclDie)
1230     return false;
1231 
1232   // Refer to the function declaration where all the other attributes will be
1233   // found.
1234   addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1235   return true;
1236 }
1237 
1238 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1239                                           bool SkipSPAttributes) {
1240   // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1241   // and its source location.
1242   bool SkipSPSourceLocation = SkipSPAttributes &&
1243                               !CUNode->getDebugInfoForProfiling();
1244   if (!SkipSPSourceLocation)
1245     if (applySubprogramDefinitionAttributes(SP, SPDie, SkipSPAttributes))
1246       return;
1247 
1248   // Constructors and operators for anonymous aggregates do not have names.
1249   if (!SP->getName().empty())
1250     addString(SPDie, dwarf::DW_AT_name, SP->getName());
1251 
1252   addAnnotation(SPDie, SP->getAnnotations());
1253 
1254   if (!SkipSPSourceLocation)
1255     addSourceLine(SPDie, SP);
1256 
1257   // Skip the rest of the attributes under -gmlt to save space.
1258   if (SkipSPAttributes)
1259     return;
1260 
1261   // Add the prototype if we have a prototype and we have a C like
1262   // language.
1263   if (SP->isPrototyped() && dwarf::isC((dwarf::SourceLanguage)getLanguage()))
1264     addFlag(SPDie, dwarf::DW_AT_prototyped);
1265 
1266   if (SP->isObjCDirect())
1267     addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct);
1268 
1269   unsigned CC = 0;
1270   DITypeRefArray Args;
1271   if (const DISubroutineType *SPTy = SP->getType()) {
1272     Args = SPTy->getTypeArray();
1273     CC = SPTy->getCC();
1274   }
1275 
1276   // Add a DW_AT_calling_convention if this has an explicit convention.
1277   if (CC && CC != dwarf::DW_CC_normal)
1278     addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1279 
1280   // Add a return type. If this is a type like a C/C++ void type we don't add a
1281   // return type.
1282   if (Args.size())
1283     if (auto Ty = Args[0])
1284       addType(SPDie, Ty);
1285 
1286   unsigned VK = SP->getVirtuality();
1287   if (VK) {
1288     addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1289     if (SP->getVirtualIndex() != -1u) {
1290       DIELoc *Block = getDIELoc();
1291       addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1292       addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1293       addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1294     }
1295     ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType()));
1296   }
1297 
1298   if (!SP->isDefinition()) {
1299     addFlag(SPDie, dwarf::DW_AT_declaration);
1300 
1301     // Add arguments. Do not add arguments for subprogram definition. They will
1302     // be handled while processing variables.
1303     constructSubprogramArguments(SPDie, Args);
1304   }
1305 
1306   addThrownTypes(SPDie, SP->getThrownTypes());
1307 
1308   if (SP->isArtificial())
1309     addFlag(SPDie, dwarf::DW_AT_artificial);
1310 
1311   if (!SP->isLocalToUnit())
1312     addFlag(SPDie, dwarf::DW_AT_external);
1313 
1314   if (DD->useAppleExtensionAttributes()) {
1315     if (SP->isOptimized())
1316       addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1317 
1318     if (unsigned isa = Asm->getISAEncoding())
1319       addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1320   }
1321 
1322   if (SP->isLValueReference())
1323     addFlag(SPDie, dwarf::DW_AT_reference);
1324 
1325   if (SP->isRValueReference())
1326     addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1327 
1328   if (SP->isNoReturn())
1329     addFlag(SPDie, dwarf::DW_AT_noreturn);
1330 
1331   addAccess(SPDie, SP->getFlags());
1332 
1333   if (SP->isExplicit())
1334     addFlag(SPDie, dwarf::DW_AT_explicit);
1335 
1336   if (SP->isMainSubprogram())
1337     addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1338   if (SP->isPure())
1339     addFlag(SPDie, dwarf::DW_AT_pure);
1340   if (SP->isElemental())
1341     addFlag(SPDie, dwarf::DW_AT_elemental);
1342   if (SP->isRecursive())
1343     addFlag(SPDie, dwarf::DW_AT_recursive);
1344 
1345   if (!SP->getTargetFuncName().empty())
1346     addString(SPDie, dwarf::DW_AT_trampoline, SP->getTargetFuncName());
1347 
1348   if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1349     addFlag(SPDie, dwarf::DW_AT_deleted);
1350 }
1351 
1352 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1353                                      DIE *IndexTy) {
1354   DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1355   addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1356 
1357   // The LowerBound value defines the lower bounds which is typically zero for
1358   // C/C++. The Count value is the number of elements.  Values are 64 bit. If
1359   // Count == -1 then the array is unbounded and we do not emit
1360   // DW_AT_lower_bound and DW_AT_count attributes.
1361   int64_t DefaultLowerBound = getDefaultLowerBound();
1362 
1363   auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1364                                DISubrange::BoundType Bound) -> void {
1365     if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1366       if (auto *VarDIE = getDIE(BV))
1367         addDIEEntry(DW_Subrange, Attr, *VarDIE);
1368     } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1369       DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1370       DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1371       DwarfExpr.setMemoryLocationKind();
1372       DwarfExpr.addExpression(BE);
1373       addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
1374     } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
1375       if (Attr == dwarf::DW_AT_count) {
1376         if (BI->getSExtValue() != -1)
1377           addUInt(DW_Subrange, Attr, std::nullopt, BI->getSExtValue());
1378       } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1379                  BI->getSExtValue() != DefaultLowerBound)
1380         addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1381     }
1382   };
1383 
1384   AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1385 
1386   AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1387 
1388   AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1389 
1390   AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1391 }
1392 
1393 void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1394                                             const DIGenericSubrange *GSR,
1395                                             DIE *IndexTy) {
1396   DIE &DwGenericSubrange =
1397       createAndAddDIE(dwarf::DW_TAG_generic_subrange, Buffer);
1398   addDIEEntry(DwGenericSubrange, dwarf::DW_AT_type, *IndexTy);
1399 
1400   int64_t DefaultLowerBound = getDefaultLowerBound();
1401 
1402   auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1403                                DIGenericSubrange::BoundType Bound) -> void {
1404     if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1405       if (auto *VarDIE = getDIE(BV))
1406         addDIEEntry(DwGenericSubrange, Attr, *VarDIE);
1407     } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1408       if (BE->isConstant() &&
1409           DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1410               *BE->isConstant()) {
1411         if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1412             static_cast<int64_t>(BE->getElement(1)) != DefaultLowerBound)
1413           addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
1414                   BE->getElement(1));
1415       } else {
1416         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1417         DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1418         DwarfExpr.setMemoryLocationKind();
1419         DwarfExpr.addExpression(BE);
1420         addBlock(DwGenericSubrange, Attr, DwarfExpr.finalize());
1421       }
1422     }
1423   };
1424 
1425   AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1426   AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1427   AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1428   AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1429 }
1430 
1431 DIE *DwarfUnit::getIndexTyDie() {
1432   if (IndexTyDie)
1433     return IndexTyDie;
1434   // Construct an integer type to use for indexes.
1435   IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1436   StringRef Name = "__ARRAY_SIZE_TYPE__";
1437   addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1438   addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, std::nullopt, sizeof(int64_t));
1439   addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1440           dwarf::getArrayIndexTypeEncoding(
1441               (dwarf::SourceLanguage)getLanguage()));
1442   DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0);
1443   return IndexTyDie;
1444 }
1445 
1446 /// Returns true if the vector's size differs from the sum of sizes of elements
1447 /// the user specified.  This can occur if the vector has been rounded up to
1448 /// fit memory alignment constraints.
1449 static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1450   assert(CTy && CTy->isVector() && "Composite type is not a vector");
1451   const uint64_t ActualSize = CTy->getSizeInBits();
1452 
1453   // Obtain the size of each element in the vector.
1454   DIType *BaseTy = CTy->getBaseType();
1455   assert(BaseTy && "Unknown vector element type.");
1456   const uint64_t ElementSize = BaseTy->getSizeInBits();
1457 
1458   // Locate the number of elements in the vector.
1459   const DINodeArray Elements = CTy->getElements();
1460   assert(Elements.size() == 1 &&
1461          Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1462          "Invalid vector element array, expected one element of type subrange");
1463   const auto Subrange = cast<DISubrange>(Elements[0]);
1464   const auto NumVecElements =
1465       Subrange->getCount()
1466           ? cast<ConstantInt *>(Subrange->getCount())->getSExtValue()
1467           : 0;
1468 
1469   // Ensure we found the element count and that the actual size is wide
1470   // enough to contain the requested size.
1471   assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1472   return ActualSize != (NumVecElements * ElementSize);
1473 }
1474 
1475 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1476   if (CTy->isVector()) {
1477     addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1478     if (hasVectorBeenPadded(CTy))
1479       addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt,
1480               CTy->getSizeInBits() / CHAR_BIT);
1481   }
1482 
1483   if (DIVariable *Var = CTy->getDataLocation()) {
1484     if (auto *VarDIE = getDIE(Var))
1485       addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
1486   } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1487     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1488     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1489     DwarfExpr.setMemoryLocationKind();
1490     DwarfExpr.addExpression(Expr);
1491     addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
1492   }
1493 
1494   if (DIVariable *Var = CTy->getAssociated()) {
1495     if (auto *VarDIE = getDIE(Var))
1496       addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
1497   } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1498     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1499     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1500     DwarfExpr.setMemoryLocationKind();
1501     DwarfExpr.addExpression(Expr);
1502     addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize());
1503   }
1504 
1505   if (DIVariable *Var = CTy->getAllocated()) {
1506     if (auto *VarDIE = getDIE(Var))
1507       addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
1508   } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1509     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1510     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1511     DwarfExpr.setMemoryLocationKind();
1512     DwarfExpr.addExpression(Expr);
1513     addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize());
1514   }
1515 
1516   if (auto *RankConst = CTy->getRankConst()) {
1517     addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
1518             RankConst->getSExtValue());
1519   } else if (auto *RankExpr = CTy->getRankExp()) {
1520     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1521     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1522     DwarfExpr.setMemoryLocationKind();
1523     DwarfExpr.addExpression(RankExpr);
1524     addBlock(Buffer, dwarf::DW_AT_rank, DwarfExpr.finalize());
1525   }
1526 
1527   // Emit the element type.
1528   addType(Buffer, CTy->getBaseType());
1529 
1530   // Get an anonymous type for index type.
1531   // FIXME: This type should be passed down from the front end
1532   // as different languages may have different sizes for indexes.
1533   DIE *IdxTy = getIndexTyDie();
1534 
1535   // Add subranges to array type.
1536   DINodeArray Elements = CTy->getElements();
1537   for (DINode *E : Elements) {
1538     // FIXME: Should this really be such a loose cast?
1539     if (auto *Element = dyn_cast_or_null<DINode>(E)) {
1540       if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1541         constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1542       else if (Element->getTag() == dwarf::DW_TAG_generic_subrange)
1543         constructGenericSubrangeDIE(Buffer, cast<DIGenericSubrange>(Element),
1544                                     IdxTy);
1545     }
1546   }
1547 }
1548 
1549 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1550   const DIType *DTy = CTy->getBaseType();
1551   bool IsUnsigned = DTy && DD->isUnsignedDIType(DTy);
1552   if (DTy) {
1553     if (DD->getDwarfVersion() >= 3)
1554       addType(Buffer, DTy);
1555     if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1556       addFlag(Buffer, dwarf::DW_AT_enum_class);
1557   }
1558 
1559   auto *Context = CTy->getScope();
1560   bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
1561       isa<DINamespace>(Context) || isa<DICommonBlock>(Context);
1562   DINodeArray Elements = CTy->getElements();
1563 
1564   // Add enumerators to enumeration type.
1565   for (const DINode *E : Elements) {
1566     auto *Enum = dyn_cast_or_null<DIEnumerator>(E);
1567     if (Enum) {
1568       DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1569       StringRef Name = Enum->getName();
1570       addString(Enumerator, dwarf::DW_AT_name, Name);
1571       addConstantValue(Enumerator, Enum->getValue(), IsUnsigned);
1572       if (IndexEnumerators)
1573         addGlobalName(Name, Enumerator, Context);
1574     }
1575   }
1576 }
1577 
1578 void DwarfUnit::constructContainingTypeDIEs() {
1579   for (auto &P : ContainingTypeMap) {
1580     DIE &SPDie = *P.first;
1581     const DINode *D = P.second;
1582     if (!D)
1583       continue;
1584     DIE *NDie = getDIE(D);
1585     if (!NDie)
1586       continue;
1587     addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1588   }
1589 }
1590 
1591 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1592   DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1593   StringRef Name = DT->getName();
1594   if (!Name.empty())
1595     addString(MemberDie, dwarf::DW_AT_name, Name);
1596 
1597   addAnnotation(MemberDie, DT->getAnnotations());
1598 
1599   if (DIType *Resolved = DT->getBaseType())
1600     addType(MemberDie, Resolved);
1601 
1602   addSourceLine(MemberDie, DT);
1603 
1604   if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1605 
1606     // For C++, virtual base classes are not at fixed offset. Use following
1607     // expression to extract appropriate offset from vtable.
1608     // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1609 
1610     DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1611     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1612     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1613     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1614     addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1615     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1616     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1617     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1618 
1619     addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1620   } else {
1621     uint64_t Size = DT->getSizeInBits();
1622     uint64_t FieldSize = DD->getBaseTypeSize(DT);
1623     uint32_t AlignInBytes = DT->getAlignInBytes();
1624     uint64_t OffsetInBytes;
1625 
1626     bool IsBitfield = DT->isBitField();
1627     if (IsBitfield) {
1628       // Handle bitfield, assume bytes are 8 bits.
1629       if (DD->useDWARF2Bitfields())
1630         addUInt(MemberDie, dwarf::DW_AT_byte_size, std::nullopt, FieldSize / 8);
1631       addUInt(MemberDie, dwarf::DW_AT_bit_size, std::nullopt, Size);
1632 
1633       uint64_t Offset = DT->getOffsetInBits();
1634       // We can't use DT->getAlignInBits() here: AlignInBits for member type
1635       // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1636       // which can't be done with bitfields. Thus we use FieldSize here.
1637       uint32_t AlignInBits = FieldSize;
1638       uint32_t AlignMask = ~(AlignInBits - 1);
1639       // The bits from the start of the storage unit to the start of the field.
1640       uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1641       // The byte offset of the field's aligned storage unit inside the struct.
1642       OffsetInBytes = (Offset - StartBitOffset) / 8;
1643 
1644       if (DD->useDWARF2Bitfields()) {
1645         uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1646         uint64_t FieldOffset = (HiMark - FieldSize);
1647         Offset -= FieldOffset;
1648 
1649         // Maybe we need to work from the other end.
1650         if (Asm->getDataLayout().isLittleEndian())
1651           Offset = FieldSize - (Offset + Size);
1652 
1653         addUInt(MemberDie, dwarf::DW_AT_bit_offset, std::nullopt, Offset);
1654         OffsetInBytes = FieldOffset >> 3;
1655       } else {
1656         addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, std::nullopt, Offset);
1657       }
1658     } else {
1659       // This is not a bitfield.
1660       OffsetInBytes = DT->getOffsetInBits() / 8;
1661       if (AlignInBytes)
1662         addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1663                 AlignInBytes);
1664     }
1665 
1666     if (DD->getDwarfVersion() <= 2) {
1667       DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1668       addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1669       addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1670       addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1671     } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
1672       // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1673       // interpreted as location-list pointers. Interpreting constants as
1674       // pointers is not expected, so we use DW_FORM_udata to encode the
1675       // constants here.
1676       if (DD->getDwarfVersion() == 3)
1677         addUInt(MemberDie, dwarf::DW_AT_data_member_location,
1678                 dwarf::DW_FORM_udata, OffsetInBytes);
1679       else
1680         addUInt(MemberDie, dwarf::DW_AT_data_member_location, std::nullopt,
1681                 OffsetInBytes);
1682     }
1683   }
1684 
1685   addAccess(MemberDie, DT->getFlags());
1686 
1687   if (DT->isVirtual())
1688     addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1689             dwarf::DW_VIRTUALITY_virtual);
1690 
1691   // Objective-C properties.
1692   if (DINode *PNode = DT->getObjCProperty())
1693     if (DIE *PDie = getDIE(PNode))
1694       addAttribute(MemberDie, dwarf::DW_AT_APPLE_property,
1695                    dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1696 
1697   if (DT->isArtificial())
1698     addFlag(MemberDie, dwarf::DW_AT_artificial);
1699 
1700   return MemberDie;
1701 }
1702 
1703 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1704   if (!DT)
1705     return nullptr;
1706 
1707   // Construct the context before querying for the existence of the DIE in case
1708   // such construction creates the DIE.
1709   DIE *ContextDIE = getOrCreateContextDIE(DT->getScope());
1710   assert(dwarf::isType(ContextDIE->getTag()) &&
1711          "Static member should belong to a type.");
1712 
1713   if (DIE *StaticMemberDIE = getDIE(DT))
1714     return StaticMemberDIE;
1715 
1716   DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1717 
1718   const DIType *Ty = DT->getBaseType();
1719 
1720   addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1721   addType(StaticMemberDIE, Ty);
1722   addSourceLine(StaticMemberDIE, DT);
1723   addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1724   addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1725 
1726   // FIXME: We could omit private if the parent is a class_type, and
1727   // public if the parent is something else.
1728   addAccess(StaticMemberDIE, DT->getFlags());
1729 
1730   if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1731     addConstantValue(StaticMemberDIE, CI, Ty);
1732   if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1733     addConstantFPValue(StaticMemberDIE, CFP);
1734 
1735   if (uint32_t AlignInBytes = DT->getAlignInBytes())
1736     addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1737             AlignInBytes);
1738 
1739   return &StaticMemberDIE;
1740 }
1741 
1742 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1743   // Emit size of content not including length itself
1744   if (!DD->useSectionsAsReferences())
1745     EndLabel = Asm->emitDwarfUnitLength(
1746         isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
1747   else
1748     Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
1749                              "Length of Unit");
1750 
1751   Asm->OutStreamer->AddComment("DWARF version number");
1752   unsigned Version = DD->getDwarfVersion();
1753   Asm->emitInt16(Version);
1754 
1755   // DWARF v5 reorders the address size and adds a unit type.
1756   if (Version >= 5) {
1757     Asm->OutStreamer->AddComment("DWARF Unit Type");
1758     Asm->emitInt8(UT);
1759     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1760     Asm->emitInt8(Asm->MAI->getCodePointerSize());
1761   }
1762 
1763   // We share one abbreviations table across all units so it's always at the
1764   // start of the section. Use a relocatable offset where needed to ensure
1765   // linking doesn't invalidate that offset.
1766   Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1767   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1768   if (UseOffsets)
1769     Asm->emitDwarfLengthOrOffset(0);
1770   else
1771     Asm->emitDwarfSymbolReference(
1772         TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1773 
1774   if (Version <= 4) {
1775     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1776     Asm->emitInt8(Asm->MAI->getCodePointerSize());
1777   }
1778 }
1779 
1780 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1781   DwarfUnit::emitCommonHeader(UseOffsets,
1782                               DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1783                                                   : dwarf::DW_UT_type);
1784   Asm->OutStreamer->AddComment("Type Signature");
1785   Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature));
1786   Asm->OutStreamer->AddComment("Type DIE Offset");
1787   // In a skeleton type unit there is no type DIE so emit a zero offset.
1788   Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0);
1789 }
1790 
1791 void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1792                                 const MCSymbol *Hi, const MCSymbol *Lo) {
1793   addAttribute(Die, Attribute, DD->getDwarfSectionOffsetForm(),
1794                new (DIEValueAllocator) DIEDelta(Hi, Lo));
1795 }
1796 
1797 void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1798                                 const MCSymbol *Label, const MCSymbol *Sec) {
1799   if (Asm->doesDwarfUseRelocationsAcrossSections())
1800     addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
1801   else
1802     addSectionDelta(Die, Attribute, Label, Sec);
1803 }
1804 
1805 bool DwarfTypeUnit::isDwoUnit() const {
1806   // Since there are no skeleton type units, all type units are dwo type units
1807   // when split DWARF is being used.
1808   return DD->useSplitDwarf();
1809 }
1810 
1811 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1812                                   const DIScope *Context) {
1813   getCU().addGlobalNameForTypeUnit(Name, Context);
1814 }
1815 
1816 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1817                                   const DIScope *Context) {
1818   getCU().addGlobalTypeUnitType(Ty, Context);
1819 }
1820 
1821 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1822   if (!Asm->doesDwarfUseRelocationsAcrossSections())
1823     return nullptr;
1824   if (isDwoUnit())
1825     return nullptr;
1826   return getSection()->getBeginSymbol();
1827 }
1828 
1829 void DwarfUnit::addStringOffsetsStart() {
1830   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1831   addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
1832                   DU->getStringOffsetsStartSym(),
1833                   TLOF.getDwarfStrOffSection()->getBeginSymbol());
1834 }
1835 
1836 void DwarfUnit::addRnglistsBase() {
1837   assert(DD->getDwarfVersion() >= 5 &&
1838          "DW_AT_rnglists_base requires DWARF version 5 or later");
1839   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1840   addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
1841                   DU->getRnglistsTableBaseSym(),
1842                   TLOF.getDwarfRnglistsSection()->getBeginSymbol());
1843 }
1844 
1845 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1846   DD->getAddressPool().resetUsedFlag(true);
1847 }
1848 
1849 bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
1850   return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
1851 }
1852