xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGDebugInfo.cpp (revision 37c9eba1644b0f9e1b5d130ccff381122f48aebe)
1 //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
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 coordinates the debug information generation while generating code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGDebugInfo.h"
14 #include "CGBlocks.h"
15 #include "CGCXXABI.h"
16 #include "CGObjCRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "ConstantEmitter.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Attr.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/RecordLayout.h"
30 #include "clang/AST/RecursiveASTVisitor.h"
31 #include "clang/AST/VTableBuilder.h"
32 #include "clang/Basic/CodeGenOptions.h"
33 #include "clang/Basic/SourceManager.h"
34 #include "clang/Basic/Version.h"
35 #include "clang/CodeGen/ModuleBuilder.h"
36 #include "clang/Frontend/FrontendOptions.h"
37 #include "clang/Lex/HeaderSearchOptions.h"
38 #include "clang/Lex/ModuleMap.h"
39 #include "clang/Lex/PreprocessorOptions.h"
40 #include "llvm/ADT/DenseSet.h"
41 #include "llvm/ADT/SmallVector.h"
42 #include "llvm/ADT/StringExtras.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DataLayout.h"
45 #include "llvm/IR/DerivedTypes.h"
46 #include "llvm/IR/Instruction.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/Intrinsics.h"
49 #include "llvm/IR/Metadata.h"
50 #include "llvm/IR/Module.h"
51 #include "llvm/Support/MD5.h"
52 #include "llvm/Support/Path.h"
53 #include "llvm/Support/SHA1.h"
54 #include "llvm/Support/SHA256.h"
55 #include "llvm/Support/TimeProfiler.h"
56 #include <cstdint>
57 #include <optional>
58 using namespace clang;
59 using namespace clang::CodeGen;
60 
61 // TODO: consider deprecating ClArrayBoundsPseudoFn; functionality is subsumed
62 //       by -fsanitize-annotate-debug-info
63 static llvm::cl::opt<bool> ClArrayBoundsPseudoFn(
64     "array-bounds-pseudofn", llvm::cl::Hidden, llvm::cl::Optional,
65     llvm::cl::desc("Emit debug info that places array-bounds instrumentation "
66                    "in an inline function called __ubsan_check_array_bounds."));
67 
68 static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
69   auto TI = Ctx.getTypeInfo(Ty);
70   if (TI.isAlignRequired())
71     return TI.Align;
72 
73   // MaxFieldAlignmentAttr is the attribute added to types
74   // declared after #pragma pack(n).
75   if (auto *Decl = Ty->getAsRecordDecl())
76     if (Decl->hasAttr<MaxFieldAlignmentAttr>())
77       return TI.Align;
78 
79   return 0;
80 }
81 
82 static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
83   return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
84 }
85 
86 static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
87   return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
88 }
89 
90 /// Returns true if \ref VD is a a holding variable (aka a
91 /// VarDecl retrieved using \ref BindingDecl::getHoldingVar).
92 static bool IsDecomposedVarDecl(VarDecl const *VD) {
93   auto const *Init = VD->getInit();
94   if (!Init)
95     return false;
96 
97   auto const *RefExpr =
98       llvm::dyn_cast_or_null<DeclRefExpr>(Init->IgnoreUnlessSpelledInSource());
99   if (!RefExpr)
100     return false;
101 
102   return llvm::dyn_cast_or_null<DecompositionDecl>(RefExpr->getDecl());
103 }
104 
105 /// Returns true if \ref VD is a compiler-generated variable
106 /// and should be treated as artificial for the purposes
107 /// of debug-info generation.
108 static bool IsArtificial(VarDecl const *VD) {
109   // Tuple-like bindings are marked as implicit despite
110   // being spelled out in source. Don't treat them as artificial
111   // variables.
112   if (IsDecomposedVarDecl(VD))
113     return false;
114 
115   return VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
116                               cast<Decl>(VD->getDeclContext())->isImplicit());
117 }
118 
119 CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
120     : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
121       DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
122       DBuilder(CGM.getModule()) {
123   CreateCompileUnit();
124 }
125 
126 CGDebugInfo::~CGDebugInfo() {
127   assert(LexicalBlockStack.empty() &&
128          "Region stack mismatch, stack not empty!");
129 }
130 
131 void CGDebugInfo::addInstSourceAtomMetadata(llvm::Instruction *I,
132                                             uint64_t Group, uint8_t Rank) {
133   if (!I->getDebugLoc() || Group == 0 || !I->getDebugLoc()->getLine())
134     return;
135 
136   // Saturate the 3-bit rank.
137   Rank = std::min<uint8_t>(Rank, 7);
138 
139   const llvm::DebugLoc &DL = I->getDebugLoc();
140 
141   // Each instruction can only be attributed to one source atom (a limitation of
142   // the implementation). If this instruction is already part of a source atom,
143   // pick the group in which it has highest precedence (lowest rank).
144   if (DL->getAtomGroup() && DL->getAtomRank() && DL->getAtomRank() < Rank) {
145     Group = DL->getAtomGroup();
146     Rank = DL->getAtomRank();
147   }
148 
149   // Update the function-local watermark so we don't reuse this number for
150   // another atom.
151   KeyInstructionsInfo.HighestEmittedAtom =
152       std::max(Group, KeyInstructionsInfo.HighestEmittedAtom);
153 
154   // Apply the new DILocation to the instruction.
155   llvm::DILocation *NewDL = llvm::DILocation::get(
156       I->getContext(), DL.getLine(), DL.getCol(), DL.getScope(),
157       DL.getInlinedAt(), DL.isImplicitCode(), Group, Rank);
158   I->setDebugLoc(NewDL);
159 }
160 
161 void CGDebugInfo::addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction,
162                                              llvm::Value *Backup) {
163   addInstToSpecificSourceAtom(KeyInstruction, Backup,
164                               KeyInstructionsInfo.CurrentAtom);
165 }
166 
167 void CGDebugInfo::addInstToSpecificSourceAtom(llvm::Instruction *KeyInstruction,
168                                               llvm::Value *Backup,
169                                               uint64_t Group) {
170   if (!Group || !CGM.getCodeGenOpts().DebugKeyInstructions)
171     return;
172 
173   llvm::DISubprogram *SP = KeyInstruction->getFunction()->getSubprogram();
174   if (!SP || !SP->getKeyInstructionsEnabled())
175     return;
176 
177   addInstSourceAtomMetadata(KeyInstruction, Group, /*Rank=*/1);
178 
179   llvm::Instruction *BackupI =
180       llvm::dyn_cast_or_null<llvm::Instruction>(Backup);
181   if (!BackupI)
182     return;
183 
184   // Add the backup instruction to the group.
185   addInstSourceAtomMetadata(BackupI, Group, /*Rank=*/2);
186 
187   // Look through chains of casts too, as they're probably going to evaporate.
188   // FIXME: And other nops like zero length geps?
189   // FIXME: Should use Cast->isNoopCast()?
190   uint8_t Rank = 3;
191   while (auto *Cast = dyn_cast<llvm::CastInst>(BackupI)) {
192     BackupI = dyn_cast<llvm::Instruction>(Cast->getOperand(0));
193     if (!BackupI)
194       break;
195     addInstSourceAtomMetadata(BackupI, Group, Rank++);
196   }
197 }
198 
199 void CGDebugInfo::completeFunction() {
200   // Reset the atom group number tracker as the numbers are function-local.
201   KeyInstructionsInfo.NextAtom = 1;
202   KeyInstructionsInfo.HighestEmittedAtom = 0;
203   KeyInstructionsInfo.CurrentAtom = 0;
204 }
205 
206 ApplyAtomGroup::ApplyAtomGroup(CGDebugInfo *DI) : DI(DI) {
207   if (!DI)
208     return;
209   OriginalAtom = DI->KeyInstructionsInfo.CurrentAtom;
210   DI->KeyInstructionsInfo.CurrentAtom = DI->KeyInstructionsInfo.NextAtom++;
211 }
212 
213 ApplyAtomGroup::~ApplyAtomGroup() {
214   if (!DI)
215     return;
216 
217   // We may not have used the group number at all.
218   DI->KeyInstructionsInfo.NextAtom =
219       std::min(DI->KeyInstructionsInfo.HighestEmittedAtom + 1,
220                DI->KeyInstructionsInfo.NextAtom);
221 
222   DI->KeyInstructionsInfo.CurrentAtom = OriginalAtom;
223 }
224 
225 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
226                                        SourceLocation TemporaryLocation)
227     : CGF(&CGF) {
228   init(TemporaryLocation);
229 }
230 
231 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
232                                        bool DefaultToEmpty,
233                                        SourceLocation TemporaryLocation)
234     : CGF(&CGF) {
235   init(TemporaryLocation, DefaultToEmpty);
236 }
237 
238 void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
239                               bool DefaultToEmpty) {
240   auto *DI = CGF->getDebugInfo();
241   if (!DI) {
242     CGF = nullptr;
243     return;
244   }
245 
246   OriginalLocation = CGF->Builder.getCurrentDebugLocation();
247 
248   if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
249     return;
250 
251   if (TemporaryLocation.isValid()) {
252     DI->EmitLocation(CGF->Builder, TemporaryLocation);
253     return;
254   }
255 
256   if (DefaultToEmpty) {
257     CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
258     return;
259   }
260 
261   // Construct a location that has a valid scope, but no line info.
262   assert(!DI->LexicalBlockStack.empty());
263   CGF->Builder.SetCurrentDebugLocation(
264       llvm::DILocation::get(DI->LexicalBlockStack.back()->getContext(), 0, 0,
265                             DI->LexicalBlockStack.back(), DI->getInlinedAt()));
266 }
267 
268 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
269     : CGF(&CGF) {
270   init(E->getExprLoc());
271 }
272 
273 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
274     : CGF(&CGF) {
275   if (!CGF.getDebugInfo()) {
276     this->CGF = nullptr;
277     return;
278   }
279   OriginalLocation = CGF.Builder.getCurrentDebugLocation();
280   if (Loc) {
281     // Key Instructions: drop the atom group and rank to avoid accidentally
282     // propagating it around.
283     if (Loc->getAtomGroup())
284       Loc = llvm::DILocation::get(Loc->getContext(), Loc.getLine(),
285                                   Loc->getColumn(), Loc->getScope(),
286                                   Loc->getInlinedAt(), Loc.isImplicitCode());
287     CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
288   }
289 }
290 
291 ApplyDebugLocation::~ApplyDebugLocation() {
292   // Query CGF so the location isn't overwritten when location updates are
293   // temporarily disabled (for C++ default function arguments)
294   if (CGF)
295     CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
296 }
297 
298 ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
299                                                    GlobalDecl InlinedFn)
300     : CGF(&CGF) {
301   if (!CGF.getDebugInfo()) {
302     this->CGF = nullptr;
303     return;
304   }
305   auto &DI = *CGF.getDebugInfo();
306   SavedLocation = DI.getLocation();
307   assert((DI.getInlinedAt() ==
308           CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
309          "CGDebugInfo and IRBuilder are out of sync");
310 
311   DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
312 }
313 
314 ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
315   if (!CGF)
316     return;
317   auto &DI = *CGF->getDebugInfo();
318   DI.EmitInlineFunctionEnd(CGF->Builder);
319   DI.EmitLocation(CGF->Builder, SavedLocation);
320 }
321 
322 void CGDebugInfo::setLocation(SourceLocation Loc) {
323   // If the new location isn't valid return.
324   if (Loc.isInvalid())
325     return;
326 
327   CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
328 
329   // If we've changed files in the middle of a lexical scope go ahead
330   // and create a new lexical scope with file node if it's different
331   // from the one in the scope.
332   if (LexicalBlockStack.empty())
333     return;
334 
335   SourceManager &SM = CGM.getContext().getSourceManager();
336   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
337   PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
338   if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc))
339     return;
340 
341   if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
342     LexicalBlockStack.pop_back();
343     LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
344         LBF->getScope(), getOrCreateFile(CurLoc)));
345   } else if (isa<llvm::DILexicalBlock>(Scope) ||
346              isa<llvm::DISubprogram>(Scope)) {
347     LexicalBlockStack.pop_back();
348     LexicalBlockStack.emplace_back(
349         DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
350   }
351 }
352 
353 llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
354   llvm::DIScope *Mod = getParentModuleOrNull(D);
355   return getContextDescriptor(cast<Decl>(D->getDeclContext()),
356                               Mod ? Mod : TheCU);
357 }
358 
359 llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
360                                                  llvm::DIScope *Default) {
361   if (!Context)
362     return Default;
363 
364   auto I = RegionMap.find(Context);
365   if (I != RegionMap.end()) {
366     llvm::Metadata *V = I->second;
367     return dyn_cast_or_null<llvm::DIScope>(V);
368   }
369 
370   // Check namespace.
371   if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
372     return getOrCreateNamespace(NSDecl);
373 
374   if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
375     if (!RDecl->isDependentType())
376       return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
377                              TheCU->getFile());
378   return Default;
379 }
380 
381 PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
382   PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
383 
384   // If we're emitting codeview, it's important to try to match MSVC's naming so
385   // that visualizers written for MSVC will trigger for our class names. In
386   // particular, we can't have spaces between arguments of standard templates
387   // like basic_string and vector, but we must have spaces between consecutive
388   // angle brackets that close nested template argument lists.
389   if (CGM.getCodeGenOpts().EmitCodeView) {
390     PP.MSVCFormatting = true;
391     PP.SplitTemplateClosers = true;
392   } else {
393     // For DWARF, printing rules are underspecified.
394     // SplitTemplateClosers yields better interop with GCC and GDB (PR46052).
395     PP.SplitTemplateClosers = true;
396   }
397 
398   PP.SuppressInlineNamespace =
399       PrintingPolicy::SuppressInlineNamespaceMode::None;
400   PP.PrintAsCanonical = true;
401   PP.UsePreferredNames = false;
402   PP.AlwaysIncludeTypeForTemplateArgument = true;
403   PP.UseEnumerators = false;
404 
405   // Apply -fdebug-prefix-map.
406   PP.Callbacks = &PrintCB;
407   return PP;
408 }
409 
410 StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
411   return internString(GetName(FD));
412 }
413 
414 StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
415   SmallString<256> MethodName;
416   llvm::raw_svector_ostream OS(MethodName);
417   OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
418   const DeclContext *DC = OMD->getDeclContext();
419   if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
420     OS << OID->getName();
421   } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
422     OS << OID->getName();
423   } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
424     if (OC->IsClassExtension()) {
425       OS << OC->getClassInterface()->getName();
426     } else {
427       OS << OC->getIdentifier()->getNameStart() << '('
428          << OC->getIdentifier()->getNameStart() << ')';
429     }
430   } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
431     OS << OCD->getClassInterface()->getName() << '(' << OCD->getName() << ')';
432   }
433   OS << ' ' << OMD->getSelector().getAsString() << ']';
434 
435   return internString(OS.str());
436 }
437 
438 StringRef CGDebugInfo::getSelectorName(Selector S) {
439   return internString(S.getAsString());
440 }
441 
442 StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
443   if (isa<ClassTemplateSpecializationDecl>(RD)) {
444     // Copy this name on the side and use its reference.
445     return internString(GetName(RD));
446   }
447 
448   // quick optimization to avoid having to intern strings that are already
449   // stored reliably elsewhere
450   if (const IdentifierInfo *II = RD->getIdentifier())
451     return II->getName();
452 
453   // The CodeView printer in LLVM wants to see the names of unnamed types
454   // because they need to have a unique identifier.
455   // These names are used to reconstruct the fully qualified type names.
456   if (CGM.getCodeGenOpts().EmitCodeView) {
457     if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
458       assert(RD->getDeclContext() == D->getDeclContext() &&
459              "Typedef should not be in another decl context!");
460       assert(D->getDeclName().getAsIdentifierInfo() &&
461              "Typedef was not named!");
462       return D->getDeclName().getAsIdentifierInfo()->getName();
463     }
464 
465     if (CGM.getLangOpts().CPlusPlus) {
466       StringRef Name;
467 
468       ASTContext &Context = CGM.getContext();
469       if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
470         // Anonymous types without a name for linkage purposes have their
471         // declarator mangled in if they have one.
472         Name = DD->getName();
473       else if (const TypedefNameDecl *TND =
474                    Context.getTypedefNameForUnnamedTagDecl(RD))
475         // Anonymous types without a name for linkage purposes have their
476         // associate typedef mangled in if they have one.
477         Name = TND->getName();
478 
479       // Give lambdas a display name based on their name mangling.
480       if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
481         if (CXXRD->isLambda())
482           return internString(
483               CGM.getCXXABI().getMangleContext().getLambdaString(CXXRD));
484 
485       if (!Name.empty()) {
486         SmallString<256> UnnamedType("<unnamed-type-");
487         UnnamedType += Name;
488         UnnamedType += '>';
489         return internString(UnnamedType);
490       }
491     }
492   }
493 
494   return StringRef();
495 }
496 
497 std::optional<llvm::DIFile::ChecksumKind>
498 CGDebugInfo::computeChecksum(FileID FID, SmallString<64> &Checksum) const {
499   Checksum.clear();
500 
501   if (!CGM.getCodeGenOpts().EmitCodeView &&
502       CGM.getCodeGenOpts().DwarfVersion < 5)
503     return std::nullopt;
504 
505   SourceManager &SM = CGM.getContext().getSourceManager();
506   std::optional<llvm::MemoryBufferRef> MemBuffer = SM.getBufferOrNone(FID);
507   if (!MemBuffer)
508     return std::nullopt;
509 
510   auto Data = llvm::arrayRefFromStringRef(MemBuffer->getBuffer());
511   switch (CGM.getCodeGenOpts().getDebugSrcHash()) {
512   case clang::CodeGenOptions::DSH_MD5:
513     llvm::toHex(llvm::MD5::hash(Data), /*LowerCase=*/true, Checksum);
514     return llvm::DIFile::CSK_MD5;
515   case clang::CodeGenOptions::DSH_SHA1:
516     llvm::toHex(llvm::SHA1::hash(Data), /*LowerCase=*/true, Checksum);
517     return llvm::DIFile::CSK_SHA1;
518   case clang::CodeGenOptions::DSH_SHA256:
519     llvm::toHex(llvm::SHA256::hash(Data), /*LowerCase=*/true, Checksum);
520     return llvm::DIFile::CSK_SHA256;
521   case clang::CodeGenOptions::DSH_NONE:
522     return std::nullopt;
523   }
524   llvm_unreachable("Unhandled DebugSrcHashKind enum");
525 }
526 
527 std::optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
528                                                 FileID FID) {
529   if (!CGM.getCodeGenOpts().EmbedSource)
530     return std::nullopt;
531 
532   bool SourceInvalid = false;
533   StringRef Source = SM.getBufferData(FID, &SourceInvalid);
534 
535   if (SourceInvalid)
536     return std::nullopt;
537 
538   return Source;
539 }
540 
541 llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
542   SourceManager &SM = CGM.getContext().getSourceManager();
543   StringRef FileName;
544   FileID FID;
545   std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
546 
547   if (Loc.isInvalid()) {
548     // The DIFile used by the CU is distinct from the main source file. Call
549     // createFile() below for canonicalization if the source file was specified
550     // with an absolute path.
551     FileName = TheCU->getFile()->getFilename();
552     CSInfo = TheCU->getFile()->getChecksum();
553   } else {
554     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
555     FileName = PLoc.getFilename();
556 
557     if (FileName.empty()) {
558       FileName = TheCU->getFile()->getFilename();
559     } else {
560       FileName = PLoc.getFilename();
561     }
562     FID = PLoc.getFileID();
563   }
564 
565   // Cache the results.
566   auto It = DIFileCache.find(FileName.data());
567   if (It != DIFileCache.end()) {
568     // Verify that the information still exists.
569     if (llvm::Metadata *V = It->second)
570       return cast<llvm::DIFile>(V);
571   }
572 
573   // Put Checksum at a scope where it will persist past the createFile call.
574   SmallString<64> Checksum;
575   if (!CSInfo) {
576     std::optional<llvm::DIFile::ChecksumKind> CSKind =
577       computeChecksum(FID, Checksum);
578     if (CSKind)
579       CSInfo.emplace(*CSKind, Checksum);
580   }
581   return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
582 }
583 
584 llvm::DIFile *CGDebugInfo::createFile(
585     StringRef FileName,
586     std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,
587     std::optional<StringRef> Source) {
588   StringRef Dir;
589   StringRef File;
590   std::string RemappedFile = remapDIPath(FileName);
591   std::string CurDir = remapDIPath(getCurrentDirname());
592   SmallString<128> DirBuf;
593   SmallString<128> FileBuf;
594   if (llvm::sys::path::is_absolute(RemappedFile)) {
595     // Strip the common prefix (if it is more than just "/" or "C:\") from
596     // current directory and FileName for a more space-efficient encoding.
597     auto FileIt = llvm::sys::path::begin(RemappedFile);
598     auto FileE = llvm::sys::path::end(RemappedFile);
599     auto CurDirIt = llvm::sys::path::begin(CurDir);
600     auto CurDirE = llvm::sys::path::end(CurDir);
601     for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)
602       llvm::sys::path::append(DirBuf, *CurDirIt);
603     if (llvm::sys::path::root_path(DirBuf) == DirBuf) {
604       // Don't strip the common prefix if it is only the root ("/" or "C:\")
605       // since that would make LLVM diagnostic locations confusing.
606       Dir = {};
607       File = RemappedFile;
608     } else {
609       for (; FileIt != FileE; ++FileIt)
610         llvm::sys::path::append(FileBuf, *FileIt);
611       Dir = DirBuf;
612       File = FileBuf;
613     }
614   } else {
615     if (!llvm::sys::path::is_absolute(FileName))
616       Dir = CurDir;
617     File = RemappedFile;
618   }
619   llvm::DIFile *F = DBuilder.createFile(File, Dir, CSInfo, Source);
620   DIFileCache[FileName.data()].reset(F);
621   return F;
622 }
623 
624 std::string CGDebugInfo::remapDIPath(StringRef Path) const {
625   SmallString<256> P = Path;
626   for (auto &[From, To] : llvm::reverse(CGM.getCodeGenOpts().DebugPrefixMap))
627     if (llvm::sys::path::replace_path_prefix(P, From, To))
628       break;
629   return P.str().str();
630 }
631 
632 unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
633   if (Loc.isInvalid())
634     return 0;
635   SourceManager &SM = CGM.getContext().getSourceManager();
636   return SM.getPresumedLoc(Loc).getLine();
637 }
638 
639 unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
640   // We may not want column information at all.
641   if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
642     return 0;
643 
644   // If the location is invalid then use the current column.
645   if (Loc.isInvalid() && CurLoc.isInvalid())
646     return 0;
647   SourceManager &SM = CGM.getContext().getSourceManager();
648   PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
649   return PLoc.isValid() ? PLoc.getColumn() : 0;
650 }
651 
652 StringRef CGDebugInfo::getCurrentDirname() {
653   if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
654     return CGM.getCodeGenOpts().DebugCompilationDir;
655 
656   if (!CWDName.empty())
657     return CWDName;
658   llvm::ErrorOr<std::string> CWD =
659       CGM.getFileSystem()->getCurrentWorkingDirectory();
660   if (!CWD)
661     return StringRef();
662   return CWDName = internString(*CWD);
663 }
664 
665 void CGDebugInfo::CreateCompileUnit() {
666   SmallString<64> Checksum;
667   std::optional<llvm::DIFile::ChecksumKind> CSKind;
668   std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
669 
670   // Should we be asking the SourceManager for the main file name, instead of
671   // accepting it as an argument? This just causes the main file name to
672   // mismatch with source locations and create extra lexical scopes or
673   // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
674   // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
675   // because that's what the SourceManager says)
676 
677   // Get absolute path name.
678   SourceManager &SM = CGM.getContext().getSourceManager();
679   auto &CGO = CGM.getCodeGenOpts();
680   const LangOptions &LO = CGM.getLangOpts();
681   std::string MainFileName = CGO.MainFileName;
682   if (MainFileName.empty())
683     MainFileName = "<stdin>";
684 
685   // The main file name provided via the "-main-file-name" option contains just
686   // the file name itself with no path information. This file name may have had
687   // a relative path, so we look into the actual file entry for the main
688   // file to determine the real absolute path for the file.
689   std::string MainFileDir;
690   if (OptionalFileEntryRef MainFile =
691           SM.getFileEntryRefForID(SM.getMainFileID())) {
692     MainFileDir = std::string(MainFile->getDir().getName());
693     if (!llvm::sys::path::is_absolute(MainFileName)) {
694       llvm::SmallString<1024> MainFileDirSS(MainFileDir);
695       llvm::sys::path::Style Style =
696           LO.UseTargetPathSeparator
697               ? (CGM.getTarget().getTriple().isOSWindows()
698                      ? llvm::sys::path::Style::windows_backslash
699                      : llvm::sys::path::Style::posix)
700               : llvm::sys::path::Style::native;
701       llvm::sys::path::append(MainFileDirSS, Style, MainFileName);
702       MainFileName = std::string(
703           llvm::sys::path::remove_leading_dotslash(MainFileDirSS, Style));
704     }
705     // If the main file name provided is identical to the input file name, and
706     // if the input file is a preprocessed source, use the module name for
707     // debug info. The module name comes from the name specified in the first
708     // linemarker if the input is a preprocessed source. In this case we don't
709     // know the content to compute a checksum.
710     if (MainFile->getName() == MainFileName &&
711         FrontendOptions::getInputKindForExtension(
712             MainFile->getName().rsplit('.').second)
713             .isPreprocessed()) {
714       MainFileName = CGM.getModule().getName().str();
715     } else {
716       CSKind = computeChecksum(SM.getMainFileID(), Checksum);
717     }
718   }
719 
720   llvm::dwarf::SourceLanguage LangTag;
721   if (LO.CPlusPlus) {
722     if (LO.ObjC)
723       LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
724     else if (CGO.DebugStrictDwarf && CGO.DwarfVersion < 5)
725       LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
726     else if (LO.CPlusPlus14)
727       LangTag = llvm::dwarf::DW_LANG_C_plus_plus_14;
728     else if (LO.CPlusPlus11)
729       LangTag = llvm::dwarf::DW_LANG_C_plus_plus_11;
730     else
731       LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
732   } else if (LO.ObjC) {
733     LangTag = llvm::dwarf::DW_LANG_ObjC;
734   } else if (LO.OpenCL && (!CGM.getCodeGenOpts().DebugStrictDwarf ||
735                            CGM.getCodeGenOpts().DwarfVersion >= 5)) {
736     LangTag = llvm::dwarf::DW_LANG_OpenCL;
737   } else if (LO.C11 && !(CGO.DebugStrictDwarf && CGO.DwarfVersion < 5)) {
738       LangTag = llvm::dwarf::DW_LANG_C11;
739   } else if (LO.C99) {
740     LangTag = llvm::dwarf::DW_LANG_C99;
741   } else {
742     LangTag = llvm::dwarf::DW_LANG_C89;
743   }
744 
745   std::string Producer = getClangFullVersion();
746 
747   // Figure out which version of the ObjC runtime we have.
748   unsigned RuntimeVers = 0;
749   if (LO.ObjC)
750     RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
751 
752   llvm::DICompileUnit::DebugEmissionKind EmissionKind;
753   switch (DebugKind) {
754   case llvm::codegenoptions::NoDebugInfo:
755   case llvm::codegenoptions::LocTrackingOnly:
756     EmissionKind = llvm::DICompileUnit::NoDebug;
757     break;
758   case llvm::codegenoptions::DebugLineTablesOnly:
759     EmissionKind = llvm::DICompileUnit::LineTablesOnly;
760     break;
761   case llvm::codegenoptions::DebugDirectivesOnly:
762     EmissionKind = llvm::DICompileUnit::DebugDirectivesOnly;
763     break;
764   case llvm::codegenoptions::DebugInfoConstructor:
765   case llvm::codegenoptions::LimitedDebugInfo:
766   case llvm::codegenoptions::FullDebugInfo:
767   case llvm::codegenoptions::UnusedTypeInfo:
768     EmissionKind = llvm::DICompileUnit::FullDebug;
769     break;
770   }
771 
772   uint64_t DwoId = 0;
773   auto &CGOpts = CGM.getCodeGenOpts();
774   // The DIFile used by the CU is distinct from the main source
775   // file. Its directory part specifies what becomes the
776   // DW_AT_comp_dir (the compilation directory), even if the source
777   // file was specified with an absolute path.
778   if (CSKind)
779     CSInfo.emplace(*CSKind, Checksum);
780   llvm::DIFile *CUFile = DBuilder.createFile(
781       remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo,
782       getSource(SM, SM.getMainFileID()));
783 
784   StringRef Sysroot, SDK;
785   if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB) {
786     Sysroot = CGM.getHeaderSearchOpts().Sysroot;
787     auto B = llvm::sys::path::rbegin(Sysroot);
788     auto E = llvm::sys::path::rend(Sysroot);
789     auto It =
790         std::find_if(B, E, [](auto SDK) { return SDK.ends_with(".sdk"); });
791     if (It != E)
792       SDK = *It;
793   }
794 
795   llvm::DICompileUnit::DebugNameTableKind NameTableKind =
796       static_cast<llvm::DICompileUnit::DebugNameTableKind>(
797           CGOpts.DebugNameTable);
798   if (CGM.getTarget().getTriple().isNVPTX())
799     NameTableKind = llvm::DICompileUnit::DebugNameTableKind::None;
800   else if (CGM.getTarget().getTriple().getVendor() == llvm::Triple::Apple)
801     NameTableKind = llvm::DICompileUnit::DebugNameTableKind::Apple;
802 
803   // Create new compile unit.
804   TheCU = DBuilder.createCompileUnit(
805       LangTag, CUFile, CGOpts.EmitVersionIdentMetadata ? Producer : "",
806       LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
807       CGOpts.DwarfDebugFlags, RuntimeVers, CGOpts.SplitDwarfFile, EmissionKind,
808       DwoId, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
809       NameTableKind, CGOpts.DebugRangesBaseAddress, remapDIPath(Sysroot), SDK);
810 }
811 
812 llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
813   llvm::dwarf::TypeKind Encoding;
814   StringRef BTName;
815   switch (BT->getKind()) {
816 #define BUILTIN_TYPE(Id, SingletonId)
817 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
818 #include "clang/AST/BuiltinTypes.def"
819   case BuiltinType::Dependent:
820     llvm_unreachable("Unexpected builtin type");
821   case BuiltinType::NullPtr:
822     return DBuilder.createNullPtrType();
823   case BuiltinType::Void:
824     return nullptr;
825   case BuiltinType::ObjCClass:
826     if (!ClassTy)
827       ClassTy =
828           DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
829                                      "objc_class", TheCU, TheCU->getFile(), 0);
830     return ClassTy;
831   case BuiltinType::ObjCId: {
832     // typedef struct objc_class *Class;
833     // typedef struct objc_object {
834     //  Class isa;
835     // } *id;
836 
837     if (ObjTy)
838       return ObjTy;
839 
840     if (!ClassTy)
841       ClassTy =
842           DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
843                                      "objc_class", TheCU, TheCU->getFile(), 0);
844 
845     unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
846 
847     auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
848 
849     ObjTy = DBuilder.createStructType(TheCU, "objc_object", TheCU->getFile(), 0,
850                                       (uint64_t)0, 0, llvm::DINode::FlagZero,
851                                       nullptr, llvm::DINodeArray());
852 
853     DBuilder.replaceArrays(
854         ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
855                    ObjTy, "isa", TheCU->getFile(), 0, Size, 0, 0,
856                    llvm::DINode::FlagZero, ISATy)));
857     return ObjTy;
858   }
859   case BuiltinType::ObjCSel: {
860     if (!SelTy)
861       SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
862                                          "objc_selector", TheCU,
863                                          TheCU->getFile(), 0);
864     return SelTy;
865   }
866 
867 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix)                   \
868   case BuiltinType::Id:                                                        \
869     return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t",       \
870                                     SingletonId);
871 #include "clang/Basic/OpenCLImageTypes.def"
872   case BuiltinType::OCLSampler:
873     return getOrCreateStructPtrType("opencl_sampler_t", OCLSamplerDITy);
874   case BuiltinType::OCLEvent:
875     return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
876   case BuiltinType::OCLClkEvent:
877     return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
878   case BuiltinType::OCLQueue:
879     return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
880   case BuiltinType::OCLReserveID:
881     return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
882 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
883   case BuiltinType::Id: \
884     return getOrCreateStructPtrType("opencl_" #ExtType, Id##Ty);
885 #include "clang/Basic/OpenCLExtensionTypes.def"
886 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId)                            \
887   case BuiltinType::Id:                                                        \
888     return getOrCreateStructPtrType(#Name, SingletonId);
889 #include "clang/Basic/HLSLIntangibleTypes.def"
890 
891 #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
892 #include "clang/Basic/AArch64ACLETypes.def"
893     {
894       if (BT->getKind() == BuiltinType::MFloat8) {
895         Encoding = llvm::dwarf::DW_ATE_unsigned_char;
896         BTName = BT->getName(CGM.getLangOpts());
897         // Bit size and offset of the type.
898         uint64_t Size = CGM.getContext().getTypeSize(BT);
899         return DBuilder.createBasicType(BTName, Size, Encoding);
900       }
901       ASTContext::BuiltinVectorTypeInfo Info =
902           // For svcount_t, only the lower 2 bytes are relevant.
903           BT->getKind() == BuiltinType::SveCount
904               ? ASTContext::BuiltinVectorTypeInfo(
905                     CGM.getContext().BoolTy, llvm::ElementCount::getFixed(16),
906                     1)
907               : CGM.getContext().getBuiltinVectorTypeInfo(BT);
908 
909       // A single vector of bytes may not suffice as the representation of
910       // svcount_t tuples because of the gap between the active 16bits of
911       // successive tuple members. Currently no such tuples are defined for
912       // svcount_t, so assert that NumVectors is 1.
913       assert((BT->getKind() != BuiltinType::SveCount || Info.NumVectors == 1) &&
914              "Unsupported number of vectors for svcount_t");
915 
916       // Debuggers can't extract 1bit from a vector, so will display a
917       // bitpattern for predicates instead.
918       unsigned NumElems = Info.EC.getKnownMinValue() * Info.NumVectors;
919       if (Info.ElementType == CGM.getContext().BoolTy) {
920         NumElems /= 8;
921         Info.ElementType = CGM.getContext().UnsignedCharTy;
922       }
923 
924       llvm::Metadata *LowerBound, *UpperBound;
925       LowerBound = llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
926           llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0));
927       if (Info.EC.isScalable()) {
928         unsigned NumElemsPerVG = NumElems / 2;
929         SmallVector<uint64_t, 9> Expr(
930             {llvm::dwarf::DW_OP_constu, NumElemsPerVG, llvm::dwarf::DW_OP_bregx,
931              /* AArch64::VG */ 46, 0, llvm::dwarf::DW_OP_mul,
932              llvm::dwarf::DW_OP_constu, 1, llvm::dwarf::DW_OP_minus});
933         UpperBound = DBuilder.createExpression(Expr);
934       } else
935         UpperBound = llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
936             llvm::Type::getInt64Ty(CGM.getLLVMContext()), NumElems - 1));
937 
938       llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(
939           /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr);
940       llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
941       llvm::DIType *ElemTy =
942           getOrCreateType(Info.ElementType, TheCU->getFile());
943       auto Align = getTypeAlignIfRequired(BT, CGM.getContext());
944       return DBuilder.createVectorType(/*Size*/ 0, Align, ElemTy,
945                                        SubscriptArray);
946     }
947   // It doesn't make sense to generate debug info for PowerPC MMA vector types.
948   // So we return a safe type here to avoid generating an error.
949 #define PPC_VECTOR_TYPE(Name, Id, size) \
950   case BuiltinType::Id:
951 #include "clang/Basic/PPCTypes.def"
952     return CreateType(cast<const BuiltinType>(CGM.getContext().IntTy));
953 
954 #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
955 #include "clang/Basic/RISCVVTypes.def"
956     {
957       ASTContext::BuiltinVectorTypeInfo Info =
958           CGM.getContext().getBuiltinVectorTypeInfo(BT);
959 
960       unsigned ElementCount = Info.EC.getKnownMinValue();
961       unsigned SEW = CGM.getContext().getTypeSize(Info.ElementType);
962 
963       bool Fractional = false;
964       unsigned LMUL;
965       unsigned NFIELDS = Info.NumVectors;
966       unsigned FixedSize = ElementCount * SEW;
967       if (Info.ElementType == CGM.getContext().BoolTy) {
968         // Mask type only occupies one vector register.
969         LMUL = 1;
970       } else if (FixedSize < 64) {
971         // In RVV scalable vector types, we encode 64 bits in the fixed part.
972         Fractional = true;
973         LMUL = 64 / FixedSize;
974       } else {
975         LMUL = FixedSize / 64;
976       }
977 
978       // Element count = (VLENB / SEW) x LMUL x NFIELDS
979       SmallVector<uint64_t, 12> Expr(
980           // The DW_OP_bregx operation has two operands: a register which is
981           // specified by an unsigned LEB128 number, followed by a signed LEB128
982           // offset.
983           {llvm::dwarf::DW_OP_bregx, // Read the contents of a register.
984            4096 + 0xC22,             // RISC-V VLENB CSR register.
985            0, // Offset for DW_OP_bregx. It is dummy here.
986            llvm::dwarf::DW_OP_constu,
987            SEW / 8, // SEW is in bits.
988            llvm::dwarf::DW_OP_div, llvm::dwarf::DW_OP_constu, LMUL});
989       if (Fractional)
990         Expr.push_back(llvm::dwarf::DW_OP_div);
991       else
992         Expr.push_back(llvm::dwarf::DW_OP_mul);
993       // NFIELDS multiplier
994       if (NFIELDS > 1)
995         Expr.append({llvm::dwarf::DW_OP_constu, NFIELDS, llvm::dwarf::DW_OP_mul});
996       // Element max index = count - 1
997       Expr.append({llvm::dwarf::DW_OP_constu, 1, llvm::dwarf::DW_OP_minus});
998 
999       auto *LowerBound =
1000           llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
1001               llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0));
1002       auto *UpperBound = DBuilder.createExpression(Expr);
1003       llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(
1004           /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr);
1005       llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1006       llvm::DIType *ElemTy =
1007           getOrCreateType(Info.ElementType, TheCU->getFile());
1008 
1009       auto Align = getTypeAlignIfRequired(BT, CGM.getContext());
1010       return DBuilder.createVectorType(/*Size=*/0, Align, ElemTy,
1011                                        SubscriptArray);
1012     }
1013 
1014 #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS)                  \
1015   case BuiltinType::Id: {                                                      \
1016     if (!SingletonId)                                                          \
1017       SingletonId =                                                            \
1018           DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,       \
1019                                      MangledName, TheCU, TheCU->getFile(), 0); \
1020     return SingletonId;                                                        \
1021   }
1022 #include "clang/Basic/WebAssemblyReferenceTypes.def"
1023 #define AMDGPU_OPAQUE_PTR_TYPE(Name, Id, SingletonId, Width, Align, AS)        \
1024   case BuiltinType::Id: {                                                      \
1025     if (!SingletonId)                                                          \
1026       SingletonId =                                                            \
1027           DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, \
1028                                      TheCU, TheCU->getFile(), 0);              \
1029     return SingletonId;                                                        \
1030   }
1031 #define AMDGPU_NAMED_BARRIER_TYPE(Name, Id, SingletonId, Width, Align, Scope)  \
1032   case BuiltinType::Id: {                                                      \
1033     if (!SingletonId)                                                          \
1034       SingletonId =                                                            \
1035           DBuilder.createBasicType(Name, Width, llvm::dwarf::DW_ATE_unsigned); \
1036     return SingletonId;                                                        \
1037   }
1038 #include "clang/Basic/AMDGPUTypes.def"
1039   case BuiltinType::UChar:
1040   case BuiltinType::Char_U:
1041     Encoding = llvm::dwarf::DW_ATE_unsigned_char;
1042     break;
1043   case BuiltinType::Char_S:
1044   case BuiltinType::SChar:
1045     Encoding = llvm::dwarf::DW_ATE_signed_char;
1046     break;
1047   case BuiltinType::Char8:
1048   case BuiltinType::Char16:
1049   case BuiltinType::Char32:
1050     Encoding = llvm::dwarf::DW_ATE_UTF;
1051     break;
1052   case BuiltinType::UShort:
1053   case BuiltinType::UInt:
1054   case BuiltinType::UInt128:
1055   case BuiltinType::ULong:
1056   case BuiltinType::WChar_U:
1057   case BuiltinType::ULongLong:
1058     Encoding = llvm::dwarf::DW_ATE_unsigned;
1059     break;
1060   case BuiltinType::Short:
1061   case BuiltinType::Int:
1062   case BuiltinType::Int128:
1063   case BuiltinType::Long:
1064   case BuiltinType::WChar_S:
1065   case BuiltinType::LongLong:
1066     Encoding = llvm::dwarf::DW_ATE_signed;
1067     break;
1068   case BuiltinType::Bool:
1069     Encoding = llvm::dwarf::DW_ATE_boolean;
1070     break;
1071   case BuiltinType::Half:
1072   case BuiltinType::Float:
1073   case BuiltinType::LongDouble:
1074   case BuiltinType::Float16:
1075   case BuiltinType::BFloat16:
1076   case BuiltinType::Float128:
1077   case BuiltinType::Double:
1078   case BuiltinType::Ibm128:
1079     // FIXME: For targets where long double, __ibm128 and __float128 have the
1080     // same size, they are currently indistinguishable in the debugger without
1081     // some special treatment. However, there is currently no consensus on
1082     // encoding and this should be updated once a DWARF encoding exists for
1083     // distinct floating point types of the same size.
1084     Encoding = llvm::dwarf::DW_ATE_float;
1085     break;
1086   case BuiltinType::ShortAccum:
1087   case BuiltinType::Accum:
1088   case BuiltinType::LongAccum:
1089   case BuiltinType::ShortFract:
1090   case BuiltinType::Fract:
1091   case BuiltinType::LongFract:
1092   case BuiltinType::SatShortFract:
1093   case BuiltinType::SatFract:
1094   case BuiltinType::SatLongFract:
1095   case BuiltinType::SatShortAccum:
1096   case BuiltinType::SatAccum:
1097   case BuiltinType::SatLongAccum:
1098     Encoding = llvm::dwarf::DW_ATE_signed_fixed;
1099     break;
1100   case BuiltinType::UShortAccum:
1101   case BuiltinType::UAccum:
1102   case BuiltinType::ULongAccum:
1103   case BuiltinType::UShortFract:
1104   case BuiltinType::UFract:
1105   case BuiltinType::ULongFract:
1106   case BuiltinType::SatUShortAccum:
1107   case BuiltinType::SatUAccum:
1108   case BuiltinType::SatULongAccum:
1109   case BuiltinType::SatUShortFract:
1110   case BuiltinType::SatUFract:
1111   case BuiltinType::SatULongFract:
1112     Encoding = llvm::dwarf::DW_ATE_unsigned_fixed;
1113     break;
1114   }
1115 
1116   BTName = BT->getName(CGM.getLangOpts());
1117   // Bit size and offset of the type.
1118   uint64_t Size = CGM.getContext().getTypeSize(BT);
1119   return DBuilder.createBasicType(BTName, Size, Encoding);
1120 }
1121 
1122 llvm::DIType *CGDebugInfo::CreateType(const BitIntType *Ty) {
1123 
1124   StringRef Name = Ty->isUnsigned() ? "unsigned _BitInt" : "_BitInt";
1125   llvm::dwarf::TypeKind Encoding = Ty->isUnsigned()
1126                                        ? llvm::dwarf::DW_ATE_unsigned
1127                                        : llvm::dwarf::DW_ATE_signed;
1128 
1129   return DBuilder.createBasicType(Name, CGM.getContext().getTypeSize(Ty),
1130                                   Encoding);
1131 }
1132 
1133 llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
1134   // Bit size and offset of the type.
1135   llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
1136   if (Ty->isComplexIntegerType())
1137     Encoding = llvm::dwarf::DW_ATE_lo_user;
1138 
1139   uint64_t Size = CGM.getContext().getTypeSize(Ty);
1140   return DBuilder.createBasicType("complex", Size, Encoding);
1141 }
1142 
1143 static void stripUnusedQualifiers(Qualifiers &Q) {
1144   // Ignore these qualifiers for now.
1145   Q.removeObjCGCAttr();
1146   Q.removeAddressSpace();
1147   Q.removeObjCLifetime();
1148   Q.removeUnaligned();
1149 }
1150 
1151 static llvm::dwarf::Tag getNextQualifier(Qualifiers &Q) {
1152   if (Q.hasConst()) {
1153     Q.removeConst();
1154     return llvm::dwarf::DW_TAG_const_type;
1155   }
1156   if (Q.hasVolatile()) {
1157     Q.removeVolatile();
1158     return llvm::dwarf::DW_TAG_volatile_type;
1159   }
1160   if (Q.hasRestrict()) {
1161     Q.removeRestrict();
1162     return llvm::dwarf::DW_TAG_restrict_type;
1163   }
1164   return (llvm::dwarf::Tag)0;
1165 }
1166 
1167 llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
1168                                                llvm::DIFile *Unit) {
1169   QualifierCollector Qc;
1170   const Type *T = Qc.strip(Ty);
1171 
1172   stripUnusedQualifiers(Qc);
1173 
1174   // We will create one Derived type for one qualifier and recurse to handle any
1175   // additional ones.
1176   llvm::dwarf::Tag Tag = getNextQualifier(Qc);
1177   if (!Tag) {
1178     if (Qc.getPointerAuth()) {
1179       unsigned Key = Qc.getPointerAuth().getKey();
1180       bool IsDiscr = Qc.getPointerAuth().isAddressDiscriminated();
1181       unsigned ExtraDiscr = Qc.getPointerAuth().getExtraDiscriminator();
1182       bool IsaPointer = Qc.getPointerAuth().isIsaPointer();
1183       bool AuthenticatesNullValues =
1184           Qc.getPointerAuth().authenticatesNullValues();
1185       Qc.removePointerAuth();
1186       assert(Qc.empty() && "Unknown type qualifier for debug info");
1187       llvm::DIType *FromTy = getOrCreateType(QualType(T, 0), Unit);
1188       return DBuilder.createPtrAuthQualifiedType(FromTy, Key, IsDiscr,
1189                                                  ExtraDiscr, IsaPointer,
1190                                                  AuthenticatesNullValues);
1191     } else {
1192       assert(Qc.empty() && "Unknown type qualifier for debug info");
1193       return getOrCreateType(QualType(T, 0), Unit);
1194     }
1195   }
1196 
1197   auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
1198 
1199   // No need to fill in the Name, Line, Size, Alignment, Offset in case of
1200   // CVR derived types.
1201   return DBuilder.createQualifiedType(Tag, FromTy);
1202 }
1203 
1204 llvm::DIType *CGDebugInfo::CreateQualifiedType(const FunctionProtoType *F,
1205                                                llvm::DIFile *Unit) {
1206   FunctionProtoType::ExtProtoInfo EPI = F->getExtProtoInfo();
1207   Qualifiers &Q = EPI.TypeQuals;
1208   stripUnusedQualifiers(Q);
1209 
1210   // We will create one Derived type for one qualifier and recurse to handle any
1211   // additional ones.
1212   llvm::dwarf::Tag Tag = getNextQualifier(Q);
1213   if (!Tag) {
1214     assert(Q.empty() && "Unknown type qualifier for debug info");
1215     return nullptr;
1216   }
1217 
1218   auto *FromTy =
1219       getOrCreateType(CGM.getContext().getFunctionType(F->getReturnType(),
1220                                                        F->getParamTypes(), EPI),
1221                       Unit);
1222 
1223   // No need to fill in the Name, Line, Size, Alignment, Offset in case of
1224   // CVR derived types.
1225   return DBuilder.createQualifiedType(Tag, FromTy);
1226 }
1227 
1228 llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
1229                                       llvm::DIFile *Unit) {
1230 
1231   // The frontend treats 'id' as a typedef to an ObjCObjectType,
1232   // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
1233   // debug info, we want to emit 'id' in both cases.
1234   if (Ty->isObjCQualifiedIdType())
1235     return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
1236 
1237   return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
1238                                Ty->getPointeeType(), Unit);
1239 }
1240 
1241 llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
1242                                       llvm::DIFile *Unit) {
1243   return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
1244                                Ty->getPointeeType(), Unit);
1245 }
1246 
1247 /// \return whether a C++ mangling exists for the type defined by TD.
1248 static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
1249   switch (TheCU->getSourceLanguage()) {
1250   case llvm::dwarf::DW_LANG_C_plus_plus:
1251   case llvm::dwarf::DW_LANG_C_plus_plus_11:
1252   case llvm::dwarf::DW_LANG_C_plus_plus_14:
1253     return true;
1254   case llvm::dwarf::DW_LANG_ObjC_plus_plus:
1255     return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
1256   default:
1257     return false;
1258   }
1259 }
1260 
1261 // Determines if the debug info for this tag declaration needs a type
1262 // identifier. The purpose of the unique identifier is to deduplicate type
1263 // information for identical types across TUs. Because of the C++ one definition
1264 // rule (ODR), it is valid to assume that the type is defined the same way in
1265 // every TU and its debug info is equivalent.
1266 //
1267 // C does not have the ODR, and it is common for codebases to contain multiple
1268 // different definitions of a struct with the same name in different TUs.
1269 // Therefore, if the type doesn't have a C++ mangling, don't give it an
1270 // identifer. Type information in C is smaller and simpler than C++ type
1271 // information, so the increase in debug info size is negligible.
1272 //
1273 // If the type is not externally visible, it should be unique to the current TU,
1274 // and should not need an identifier to participate in type deduplication.
1275 // However, when emitting CodeView, the format internally uses these
1276 // unique type name identifers for references between debug info. For example,
1277 // the method of a class in an anonymous namespace uses the identifer to refer
1278 // to its parent class. The Microsoft C++ ABI attempts to provide unique names
1279 // for such types, so when emitting CodeView, always use identifiers for C++
1280 // types. This may create problems when attempting to emit CodeView when the MS
1281 // C++ ABI is not in use.
1282 static bool needsTypeIdentifier(const TagDecl *TD, CodeGenModule &CGM,
1283                                 llvm::DICompileUnit *TheCU) {
1284   // We only add a type identifier for types with C++ name mangling.
1285   if (!hasCXXMangling(TD, TheCU))
1286     return false;
1287 
1288   // Externally visible types with C++ mangling need a type identifier.
1289   if (TD->isExternallyVisible())
1290     return true;
1291 
1292   // CodeView types with C++ mangling need a type identifier.
1293   if (CGM.getCodeGenOpts().EmitCodeView)
1294     return true;
1295 
1296   return false;
1297 }
1298 
1299 // Returns a unique type identifier string if one exists, or an empty string.
1300 static SmallString<256> getTypeIdentifier(const TagType *Ty, CodeGenModule &CGM,
1301                                           llvm::DICompileUnit *TheCU) {
1302   SmallString<256> Identifier;
1303   const TagDecl *TD = Ty->getDecl();
1304 
1305   if (!needsTypeIdentifier(TD, CGM, TheCU))
1306     return Identifier;
1307   if (const auto *RD = dyn_cast<CXXRecordDecl>(TD))
1308     if (RD->getDefinition())
1309       if (RD->isDynamicClass() &&
1310           CGM.getVTableLinkage(RD) == llvm::GlobalValue::ExternalLinkage)
1311         return Identifier;
1312 
1313   // TODO: This is using the RTTI name. Is there a better way to get
1314   // a unique string for a type?
1315   llvm::raw_svector_ostream Out(Identifier);
1316   CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
1317   return Identifier;
1318 }
1319 
1320 /// \return the appropriate DWARF tag for a composite type.
1321 static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
1322   llvm::dwarf::Tag Tag;
1323   if (RD->isStruct() || RD->isInterface())
1324     Tag = llvm::dwarf::DW_TAG_structure_type;
1325   else if (RD->isUnion())
1326     Tag = llvm::dwarf::DW_TAG_union_type;
1327   else {
1328     // FIXME: This could be a struct type giving a default visibility different
1329     // than C++ class type, but needs llvm metadata changes first.
1330     assert(RD->isClass());
1331     Tag = llvm::dwarf::DW_TAG_class_type;
1332   }
1333   return Tag;
1334 }
1335 
1336 llvm::DICompositeType *
1337 CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
1338                                       llvm::DIScope *Ctx) {
1339   const RecordDecl *RD = Ty->getDecl();
1340   if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
1341     return cast<llvm::DICompositeType>(T);
1342   llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
1343   const unsigned Line =
1344       getLineNumber(RD->getLocation().isValid() ? RD->getLocation() : CurLoc);
1345   StringRef RDName = getClassName(RD);
1346 
1347   uint64_t Size = 0;
1348   uint32_t Align = 0;
1349 
1350   const RecordDecl *D = RD->getDefinition();
1351   if (D && D->isCompleteDefinition())
1352     Size = CGM.getContext().getTypeSize(Ty);
1353 
1354   llvm::DINode::DIFlags Flags = llvm::DINode::FlagFwdDecl;
1355 
1356   // Add flag to nontrivial forward declarations. To be consistent with MSVC,
1357   // add the flag if a record has no definition because we don't know whether
1358   // it will be trivial or not.
1359   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1360     if (!CXXRD->hasDefinition() ||
1361         (CXXRD->hasDefinition() && !CXXRD->isTrivial()))
1362       Flags |= llvm::DINode::FlagNonTrivial;
1363 
1364   // Create the type.
1365   SmallString<256> Identifier;
1366   // Don't include a linkage name in line tables only.
1367   if (CGM.getCodeGenOpts().hasReducedDebugInfo())
1368     Identifier = getTypeIdentifier(Ty, CGM, TheCU);
1369   llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
1370       getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, Flags,
1371       Identifier);
1372   if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
1373     if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
1374       DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
1375                              CollectCXXTemplateParams(TSpecial, DefUnit));
1376   ReplaceMap.emplace_back(
1377       std::piecewise_construct, std::make_tuple(Ty),
1378       std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
1379   return RetTy;
1380 }
1381 
1382 llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
1383                                                  const Type *Ty,
1384                                                  QualType PointeeTy,
1385                                                  llvm::DIFile *Unit) {
1386   // Bit size, align and offset of the type.
1387   // Size is always the size of a pointer.
1388   uint64_t Size = CGM.getContext().getTypeSize(Ty);
1389   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
1390   std::optional<unsigned> DWARFAddressSpace =
1391       CGM.getTarget().getDWARFAddressSpace(
1392           CGM.getTypes().getTargetAddressSpace(PointeeTy));
1393 
1394   const BTFTagAttributedType *BTFAttrTy;
1395   if (auto *Atomic = PointeeTy->getAs<AtomicType>())
1396     BTFAttrTy = dyn_cast<BTFTagAttributedType>(Atomic->getValueType());
1397   else
1398     BTFAttrTy = dyn_cast<BTFTagAttributedType>(PointeeTy);
1399   SmallVector<llvm::Metadata *, 4> Annots;
1400   while (BTFAttrTy) {
1401     StringRef Tag = BTFAttrTy->getAttr()->getBTFTypeTag();
1402     if (!Tag.empty()) {
1403       llvm::Metadata *Ops[2] = {
1404           llvm::MDString::get(CGM.getLLVMContext(), StringRef("btf_type_tag")),
1405           llvm::MDString::get(CGM.getLLVMContext(), Tag)};
1406       Annots.insert(Annots.begin(),
1407                     llvm::MDNode::get(CGM.getLLVMContext(), Ops));
1408     }
1409     BTFAttrTy = dyn_cast<BTFTagAttributedType>(BTFAttrTy->getWrappedType());
1410   }
1411 
1412   llvm::DINodeArray Annotations = nullptr;
1413   if (Annots.size() > 0)
1414     Annotations = DBuilder.getOrCreateArray(Annots);
1415 
1416   if (Tag == llvm::dwarf::DW_TAG_reference_type ||
1417       Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
1418     return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
1419                                         Size, Align, DWARFAddressSpace);
1420   else
1421     return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
1422                                       Align, DWARFAddressSpace, StringRef(),
1423                                       Annotations);
1424 }
1425 
1426 llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
1427                                                     llvm::DIType *&Cache) {
1428   if (Cache)
1429     return Cache;
1430   Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
1431                                      TheCU, TheCU->getFile(), 0);
1432   unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1433   Cache = DBuilder.createPointerType(Cache, Size);
1434   return Cache;
1435 }
1436 
1437 uint64_t CGDebugInfo::collectDefaultElementTypesForBlockPointer(
1438     const BlockPointerType *Ty, llvm::DIFile *Unit, llvm::DIDerivedType *DescTy,
1439     unsigned LineNo, SmallVectorImpl<llvm::Metadata *> &EltTys) {
1440   QualType FType;
1441 
1442   // Advanced by calls to CreateMemberType in increments of FType, then
1443   // returned as the overall size of the default elements.
1444   uint64_t FieldOffset = 0;
1445 
1446   // Blocks in OpenCL have unique constraints which make the standard fields
1447   // redundant while requiring size and align fields for enqueue_kernel. See
1448   // initializeForBlockHeader in CGBlocks.cpp
1449   if (CGM.getLangOpts().OpenCL) {
1450     FType = CGM.getContext().IntTy;
1451     EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1452     EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset));
1453   } else {
1454     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1455     EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1456     FType = CGM.getContext().IntTy;
1457     EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1458     EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
1459     FType = CGM.getContext().getPointerType(Ty->getPointeeType());
1460     EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
1461     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1462     uint64_t FieldSize = CGM.getContext().getTypeSize(Ty);
1463     uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty);
1464     EltTys.push_back(DBuilder.createMemberType(
1465         Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign,
1466         FieldOffset, llvm::DINode::FlagZero, DescTy));
1467     FieldOffset += FieldSize;
1468   }
1469 
1470   return FieldOffset;
1471 }
1472 
1473 llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
1474                                       llvm::DIFile *Unit) {
1475   SmallVector<llvm::Metadata *, 8> EltTys;
1476   QualType FType;
1477   uint64_t FieldOffset;
1478   llvm::DINodeArray Elements;
1479 
1480   FieldOffset = 0;
1481   FType = CGM.getContext().UnsignedLongTy;
1482   EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
1483   EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
1484 
1485   Elements = DBuilder.getOrCreateArray(EltTys);
1486   EltTys.clear();
1487 
1488   llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
1489 
1490   auto *EltTy =
1491       DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0,
1492                                 FieldOffset, 0, Flags, nullptr, Elements);
1493 
1494   // Bit size, align and offset of the type.
1495   uint64_t Size = CGM.getContext().getTypeSize(Ty);
1496 
1497   auto *DescTy = DBuilder.createPointerType(EltTy, Size);
1498 
1499   FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy,
1500                                                           0, EltTys);
1501 
1502   Elements = DBuilder.getOrCreateArray(EltTys);
1503 
1504   // The __block_literal_generic structs are marked with a special
1505   // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
1506   // the debugger needs to know about. To allow type uniquing, emit
1507   // them without a name or a location.
1508   EltTy = DBuilder.createStructType(Unit, "", nullptr, 0, FieldOffset, 0,
1509                                     Flags, nullptr, Elements);
1510 
1511   return DBuilder.createPointerType(EltTy, Size);
1512 }
1513 
1514 static llvm::SmallVector<TemplateArgument>
1515 GetTemplateArgs(const TemplateDecl *TD, const TemplateSpecializationType *Ty) {
1516   assert(Ty->isTypeAlias());
1517   // TemplateSpecializationType doesn't know if its template args are
1518   // being substituted into a parameter pack. We can find out if that's
1519   // the case now by inspecting the TypeAliasTemplateDecl template
1520   // parameters. Insert Ty's template args into SpecArgs, bundling args
1521   // passed to a parameter pack into a TemplateArgument::Pack. It also
1522   // doesn't know the value of any defaulted args, so collect those now
1523   // too.
1524   SmallVector<TemplateArgument> SpecArgs;
1525   ArrayRef SubstArgs = Ty->template_arguments();
1526   for (const NamedDecl *Param : TD->getTemplateParameters()->asArray()) {
1527     // If Param is a parameter pack, pack the remaining arguments.
1528     if (Param->isParameterPack()) {
1529       SpecArgs.push_back(TemplateArgument(SubstArgs));
1530       break;
1531     }
1532 
1533     // Skip defaulted args.
1534     // FIXME: Ideally, we wouldn't do this. We can read the default values
1535     // for each parameter. However, defaulted arguments which are dependent
1536     // values or dependent types can't (easily?) be resolved here.
1537     if (SubstArgs.empty()) {
1538       // If SubstArgs is now empty (we're taking from it each iteration) and
1539       // this template parameter isn't a pack, then that should mean we're
1540       // using default values for the remaining template parameters (after
1541       // which there may be an empty pack too which we will ignore).
1542       break;
1543     }
1544 
1545     // Take the next argument.
1546     SpecArgs.push_back(SubstArgs.front());
1547     SubstArgs = SubstArgs.drop_front();
1548   }
1549   return SpecArgs;
1550 }
1551 
1552 llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1553                                       llvm::DIFile *Unit) {
1554   assert(Ty->isTypeAlias());
1555   llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
1556 
1557   const TemplateDecl *TD = Ty->getTemplateName().getAsTemplateDecl();
1558   if (isa<BuiltinTemplateDecl>(TD))
1559     return Src;
1560 
1561   const auto *AliasDecl = cast<TypeAliasTemplateDecl>(TD)->getTemplatedDecl();
1562   if (AliasDecl->hasAttr<NoDebugAttr>())
1563     return Src;
1564 
1565   SmallString<128> NS;
1566   llvm::raw_svector_ostream OS(NS);
1567 
1568   auto PP = getPrintingPolicy();
1569   Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None);
1570 
1571   SourceLocation Loc = AliasDecl->getLocation();
1572 
1573   if (CGM.getCodeGenOpts().DebugTemplateAlias) {
1574     auto ArgVector = ::GetTemplateArgs(TD, Ty);
1575     TemplateArgs Args = {TD->getTemplateParameters(), ArgVector};
1576 
1577     // FIXME: Respect DebugTemplateNameKind::Mangled, e.g. by using GetName.
1578     // Note we can't use GetName without additional work: TypeAliasTemplateDecl
1579     // doesn't have instantiation information, so
1580     // TypeAliasTemplateDecl::getNameForDiagnostic wouldn't have access to the
1581     // template args.
1582     std::string Name;
1583     llvm::raw_string_ostream OS(Name);
1584     TD->getNameForDiagnostic(OS, PP, /*Qualified=*/false);
1585     if (CGM.getCodeGenOpts().getDebugSimpleTemplateNames() !=
1586             llvm::codegenoptions::DebugTemplateNamesKind::Simple ||
1587         !HasReconstitutableArgs(Args.Args))
1588       printTemplateArgumentList(OS, Args.Args, PP);
1589 
1590     llvm::DIDerivedType *AliasTy = DBuilder.createTemplateAlias(
1591         Src, Name, getOrCreateFile(Loc), getLineNumber(Loc),
1592         getDeclContextDescriptor(AliasDecl), CollectTemplateParams(Args, Unit));
1593     return AliasTy;
1594   }
1595 
1596   printTemplateArgumentList(OS, Ty->template_arguments(), PP,
1597                             TD->getTemplateParameters());
1598   return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
1599                                 getLineNumber(Loc),
1600                                 getDeclContextDescriptor(AliasDecl));
1601 }
1602 
1603 /// Convert an AccessSpecifier into the corresponding DINode flag.
1604 /// As an optimization, return 0 if the access specifier equals the
1605 /// default for the containing type.
1606 static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1607                                            const RecordDecl *RD) {
1608   AccessSpecifier Default = clang::AS_none;
1609   if (RD && RD->isClass())
1610     Default = clang::AS_private;
1611   else if (RD && (RD->isStruct() || RD->isUnion()))
1612     Default = clang::AS_public;
1613 
1614   if (Access == Default)
1615     return llvm::DINode::FlagZero;
1616 
1617   switch (Access) {
1618   case clang::AS_private:
1619     return llvm::DINode::FlagPrivate;
1620   case clang::AS_protected:
1621     return llvm::DINode::FlagProtected;
1622   case clang::AS_public:
1623     return llvm::DINode::FlagPublic;
1624   case clang::AS_none:
1625     return llvm::DINode::FlagZero;
1626   }
1627   llvm_unreachable("unexpected access enumerator");
1628 }
1629 
1630 llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1631                                       llvm::DIFile *Unit) {
1632   llvm::DIType *Underlying =
1633       getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
1634 
1635   if (Ty->getDecl()->hasAttr<NoDebugAttr>())
1636     return Underlying;
1637 
1638   // We don't set size information, but do specify where the typedef was
1639   // declared.
1640   SourceLocation Loc = Ty->getDecl()->getLocation();
1641 
1642   uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext());
1643   // Typedefs are derived from some other type.
1644   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl());
1645 
1646   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1647   const DeclContext *DC = Ty->getDecl()->getDeclContext();
1648   if (isa<RecordDecl>(DC))
1649     Flags = getAccessFlag(Ty->getDecl()->getAccess(), cast<RecordDecl>(DC));
1650 
1651   return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(),
1652                                 getOrCreateFile(Loc), getLineNumber(Loc),
1653                                 getDeclContextDescriptor(Ty->getDecl()), Align,
1654                                 Flags, Annotations);
1655 }
1656 
1657 static unsigned getDwarfCC(CallingConv CC) {
1658   switch (CC) {
1659   case CC_C:
1660     // Avoid emitting DW_AT_calling_convention if the C convention was used.
1661     return 0;
1662 
1663   case CC_X86StdCall:
1664     return llvm::dwarf::DW_CC_BORLAND_stdcall;
1665   case CC_X86FastCall:
1666     return llvm::dwarf::DW_CC_BORLAND_msfastcall;
1667   case CC_X86ThisCall:
1668     return llvm::dwarf::DW_CC_BORLAND_thiscall;
1669   case CC_X86VectorCall:
1670     return llvm::dwarf::DW_CC_LLVM_vectorcall;
1671   case CC_X86Pascal:
1672     return llvm::dwarf::DW_CC_BORLAND_pascal;
1673   case CC_Win64:
1674     return llvm::dwarf::DW_CC_LLVM_Win64;
1675   case CC_X86_64SysV:
1676     return llvm::dwarf::DW_CC_LLVM_X86_64SysV;
1677   case CC_AAPCS:
1678   case CC_AArch64VectorCall:
1679   case CC_AArch64SVEPCS:
1680     return llvm::dwarf::DW_CC_LLVM_AAPCS;
1681   case CC_AAPCS_VFP:
1682     return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP;
1683   case CC_IntelOclBicc:
1684     return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
1685   case CC_SpirFunction:
1686     return llvm::dwarf::DW_CC_LLVM_SpirFunction;
1687   case CC_DeviceKernel:
1688     return llvm::dwarf::DW_CC_LLVM_DeviceKernel;
1689   case CC_Swift:
1690     return llvm::dwarf::DW_CC_LLVM_Swift;
1691   case CC_SwiftAsync:
1692     return llvm::dwarf::DW_CC_LLVM_SwiftTail;
1693   case CC_PreserveMost:
1694     return llvm::dwarf::DW_CC_LLVM_PreserveMost;
1695   case CC_PreserveAll:
1696     return llvm::dwarf::DW_CC_LLVM_PreserveAll;
1697   case CC_X86RegCall:
1698     return llvm::dwarf::DW_CC_LLVM_X86RegCall;
1699   case CC_M68kRTD:
1700     return llvm::dwarf::DW_CC_LLVM_M68kRTD;
1701   case CC_PreserveNone:
1702     return llvm::dwarf::DW_CC_LLVM_PreserveNone;
1703   case CC_RISCVVectorCall:
1704     return llvm::dwarf::DW_CC_LLVM_RISCVVectorCall;
1705 #define CC_VLS_CASE(ABI_VLEN) case CC_RISCVVLSCall_##ABI_VLEN:
1706     CC_VLS_CASE(32)
1707     CC_VLS_CASE(64)
1708     CC_VLS_CASE(128)
1709     CC_VLS_CASE(256)
1710     CC_VLS_CASE(512)
1711     CC_VLS_CASE(1024)
1712     CC_VLS_CASE(2048)
1713     CC_VLS_CASE(4096)
1714     CC_VLS_CASE(8192)
1715     CC_VLS_CASE(16384)
1716     CC_VLS_CASE(32768)
1717     CC_VLS_CASE(65536)
1718 #undef CC_VLS_CASE
1719     return llvm::dwarf::DW_CC_LLVM_RISCVVLSCall;
1720   }
1721   return 0;
1722 }
1723 
1724 static llvm::DINode::DIFlags getRefFlags(const FunctionProtoType *Func) {
1725   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1726   if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1727     Flags |= llvm::DINode::FlagLValueReference;
1728   if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1729     Flags |= llvm::DINode::FlagRValueReference;
1730   return Flags;
1731 }
1732 
1733 llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1734                                       llvm::DIFile *Unit) {
1735   const auto *FPT = dyn_cast<FunctionProtoType>(Ty);
1736   if (FPT) {
1737     if (llvm::DIType *QTy = CreateQualifiedType(FPT, Unit))
1738       return QTy;
1739   }
1740 
1741   // Create the type without any qualifiers
1742 
1743   SmallVector<llvm::Metadata *, 16> EltTys;
1744 
1745   // Add the result type at least.
1746   EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
1747 
1748   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1749   // Set up remainder of arguments if there is a prototype.
1750   // otherwise emit it as a variadic function.
1751   if (!FPT) {
1752     EltTys.push_back(DBuilder.createUnspecifiedParameter());
1753   } else {
1754     Flags = getRefFlags(FPT);
1755     for (const QualType &ParamType : FPT->param_types())
1756       EltTys.push_back(getOrCreateType(ParamType, Unit));
1757     if (FPT->isVariadic())
1758       EltTys.push_back(DBuilder.createUnspecifiedParameter());
1759   }
1760 
1761   llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
1762   llvm::DIType *F = DBuilder.createSubroutineType(
1763       EltTypeArray, Flags, getDwarfCC(Ty->getCallConv()));
1764   return F;
1765 }
1766 
1767 llvm::DIDerivedType *
1768 CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1769                                 llvm::DIScope *RecordTy, const RecordDecl *RD) {
1770   StringRef Name = BitFieldDecl->getName();
1771   QualType Ty = BitFieldDecl->getType();
1772   if (BitFieldDecl->hasAttr<PreferredTypeAttr>())
1773     Ty = BitFieldDecl->getAttr<PreferredTypeAttr>()->getType();
1774   SourceLocation Loc = BitFieldDecl->getLocation();
1775   llvm::DIFile *VUnit = getOrCreateFile(Loc);
1776   llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1777 
1778   // Get the location for the field.
1779   llvm::DIFile *File = getOrCreateFile(Loc);
1780   unsigned Line = getLineNumber(Loc);
1781 
1782   const CGBitFieldInfo &BitFieldInfo =
1783       CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1784   uint64_t SizeInBits = BitFieldInfo.Size;
1785   assert(SizeInBits > 0 && "found named 0-width bitfield");
1786   uint64_t StorageOffsetInBits =
1787       CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1788   uint64_t Offset = BitFieldInfo.Offset;
1789   // The bit offsets for big endian machines are reversed for big
1790   // endian target, compensate for that as the DIDerivedType requires
1791   // un-reversed offsets.
1792   if (CGM.getDataLayout().isBigEndian())
1793     Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1794   uint64_t OffsetInBits = StorageOffsetInBits + Offset;
1795   llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
1796   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(BitFieldDecl);
1797   return DBuilder.createBitFieldMemberType(
1798       RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1799       Flags, DebugType, Annotations);
1800 }
1801 
1802 llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded(
1803     const FieldDecl *BitFieldDecl, const llvm::DIDerivedType *BitFieldDI,
1804     llvm::ArrayRef<llvm::Metadata *> PreviousFieldsDI, const RecordDecl *RD) {
1805 
1806   if (!CGM.getTargetCodeGenInfo().shouldEmitDWARFBitFieldSeparators())
1807     return nullptr;
1808 
1809   /*
1810   Add a *single* zero-bitfield separator between two non-zero bitfields
1811   separated by one or more zero-bitfields. This is used to distinguish between
1812   structures such the ones below, where the memory layout is the same, but how
1813   the ABI assigns fields to registers differs.
1814 
1815   struct foo {
1816     int space[4];
1817     char a : 8; // on amdgpu, passed on v4
1818     char b : 8;
1819     char x : 8;
1820     char y : 8;
1821   };
1822   struct bar {
1823     int space[4];
1824     char a : 8; // on amdgpu, passed on v4
1825     char b : 8;
1826     char : 0;
1827     char x : 8; // passed on v5
1828     char y : 8;
1829   };
1830   */
1831   if (PreviousFieldsDI.empty())
1832     return nullptr;
1833 
1834   // If we already emitted metadata for a 0-length bitfield, nothing to do here.
1835   auto *PreviousMDEntry =
1836       PreviousFieldsDI.empty() ? nullptr : PreviousFieldsDI.back();
1837   auto *PreviousMDField =
1838       dyn_cast_or_null<llvm::DIDerivedType>(PreviousMDEntry);
1839   if (!PreviousMDField || !PreviousMDField->isBitField() ||
1840       PreviousMDField->getSizeInBits() == 0)
1841     return nullptr;
1842 
1843   auto PreviousBitfield = RD->field_begin();
1844   std::advance(PreviousBitfield, BitFieldDecl->getFieldIndex() - 1);
1845 
1846   assert(PreviousBitfield->isBitField());
1847 
1848   if (!PreviousBitfield->isZeroLengthBitField())
1849     return nullptr;
1850 
1851   QualType Ty = PreviousBitfield->getType();
1852   SourceLocation Loc = PreviousBitfield->getLocation();
1853   llvm::DIFile *VUnit = getOrCreateFile(Loc);
1854   llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1855   llvm::DIScope *RecordTy = BitFieldDI->getScope();
1856 
1857   llvm::DIFile *File = getOrCreateFile(Loc);
1858   unsigned Line = getLineNumber(Loc);
1859 
1860   uint64_t StorageOffsetInBits =
1861       cast<llvm::ConstantInt>(BitFieldDI->getStorageOffsetInBits())
1862           ->getZExtValue();
1863 
1864   llvm::DINode::DIFlags Flags =
1865       getAccessFlag(PreviousBitfield->getAccess(), RD);
1866   llvm::DINodeArray Annotations =
1867       CollectBTFDeclTagAnnotations(*PreviousBitfield);
1868   return DBuilder.createBitFieldMemberType(
1869       RecordTy, "", File, Line, 0, StorageOffsetInBits, StorageOffsetInBits,
1870       Flags, DebugType, Annotations);
1871 }
1872 
1873 llvm::DIType *CGDebugInfo::createFieldType(
1874     StringRef name, QualType type, SourceLocation loc, AccessSpecifier AS,
1875     uint64_t offsetInBits, uint32_t AlignInBits, llvm::DIFile *tunit,
1876     llvm::DIScope *scope, const RecordDecl *RD, llvm::DINodeArray Annotations) {
1877   llvm::DIType *debugType = getOrCreateType(type, tunit);
1878 
1879   // Get the location for the field.
1880   llvm::DIFile *file = getOrCreateFile(loc);
1881   const unsigned line = getLineNumber(loc.isValid() ? loc : CurLoc);
1882 
1883   uint64_t SizeInBits = 0;
1884   auto Align = AlignInBits;
1885   if (!type->isIncompleteArrayType()) {
1886     TypeInfo TI = CGM.getContext().getTypeInfo(type);
1887     SizeInBits = TI.Width;
1888     if (!Align)
1889       Align = getTypeAlignIfRequired(type, CGM.getContext());
1890   }
1891 
1892   llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
1893   return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align,
1894                                    offsetInBits, flags, debugType, Annotations);
1895 }
1896 
1897 llvm::DISubprogram *
1898 CGDebugInfo::createInlinedSubprogram(StringRef FuncName,
1899                                      llvm::DIFile *FileScope) {
1900   // We are caching the subprogram because we don't want to duplicate
1901   // subprograms with the same message. Note that `SPFlagDefinition` prevents
1902   // subprograms from being uniqued.
1903   llvm::DISubprogram *&SP = InlinedSubprogramMap[FuncName];
1904 
1905   if (!SP) {
1906     llvm::DISubroutineType *DIFnTy = DBuilder.createSubroutineType(nullptr);
1907     SP = DBuilder.createFunction(
1908         /*Scope=*/FileScope, /*Name=*/FuncName, /*LinkageName=*/StringRef(),
1909         /*File=*/FileScope, /*LineNo=*/0, /*Ty=*/DIFnTy,
1910         /*ScopeLine=*/0,
1911         /*Flags=*/llvm::DINode::FlagArtificial,
1912         /*SPFlags=*/llvm::DISubprogram::SPFlagDefinition,
1913         /*TParams=*/nullptr, /*Decl=*/nullptr, /*ThrownTypes=*/nullptr,
1914         /*Annotations=*/nullptr, /*TargetFuncName=*/StringRef(),
1915         /*UseKeyInstructions=*/CGM.getCodeGenOpts().DebugKeyInstructions);
1916   }
1917 
1918   return SP;
1919 }
1920 
1921 void CGDebugInfo::CollectRecordLambdaFields(
1922     const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
1923     llvm::DIType *RecordTy) {
1924   // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1925   // has the name and the location of the variable so we should iterate over
1926   // both concurrently.
1927   const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1928   RecordDecl::field_iterator Field = CXXDecl->field_begin();
1929   unsigned fieldno = 0;
1930   for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
1931                                              E = CXXDecl->captures_end();
1932        I != E; ++I, ++Field, ++fieldno) {
1933     const LambdaCapture &C = *I;
1934     if (C.capturesVariable()) {
1935       SourceLocation Loc = C.getLocation();
1936       assert(!Field->isBitField() && "lambdas don't have bitfield members!");
1937       ValueDecl *V = C.getCapturedVar();
1938       StringRef VName = V->getName();
1939       llvm::DIFile *VUnit = getOrCreateFile(Loc);
1940       auto Align = getDeclAlignIfRequired(V, CGM.getContext());
1941       llvm::DIType *FieldType = createFieldType(
1942           VName, Field->getType(), Loc, Field->getAccess(),
1943           layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
1944       elements.push_back(FieldType);
1945     } else if (C.capturesThis()) {
1946       // TODO: Need to handle 'this' in some way by probably renaming the
1947       // this of the lambda class and having a field member of 'this' or
1948       // by using AT_object_pointer for the function and having that be
1949       // used as 'this' for semantic references.
1950       FieldDecl *f = *Field;
1951       llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
1952       QualType type = f->getType();
1953       StringRef ThisName =
1954           CGM.getCodeGenOpts().EmitCodeView ? "__this" : "this";
1955       llvm::DIType *fieldType = createFieldType(
1956           ThisName, type, f->getLocation(), f->getAccess(),
1957           layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
1958 
1959       elements.push_back(fieldType);
1960     }
1961   }
1962 }
1963 
1964 llvm::DIDerivedType *
1965 CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
1966                                      const RecordDecl *RD) {
1967   // Create the descriptor for the static variable, with or without
1968   // constant initializers.
1969   Var = Var->getCanonicalDecl();
1970   llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1971   llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
1972 
1973   unsigned LineNumber = getLineNumber(Var->getLocation());
1974   StringRef VName = Var->getName();
1975 
1976   // FIXME: to avoid complications with type merging we should
1977   // emit the constant on the definition instead of the declaration.
1978   llvm::Constant *C = nullptr;
1979   if (Var->getInit()) {
1980     const APValue *Value = Var->evaluateValue();
1981     if (Value) {
1982       if (Value->isInt())
1983         C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1984       if (Value->isFloat())
1985         C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1986     }
1987   }
1988 
1989   llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
1990   auto Tag = CGM.getCodeGenOpts().DwarfVersion >= 5
1991                  ? llvm::dwarf::DW_TAG_variable
1992                  : llvm::dwarf::DW_TAG_member;
1993   auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
1994   llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
1995       RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Tag, Align);
1996   StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
1997   return GV;
1998 }
1999 
2000 void CGDebugInfo::CollectRecordNormalField(
2001     const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
2002     SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
2003     const RecordDecl *RD) {
2004   StringRef name = field->getName();
2005   QualType type = field->getType();
2006 
2007   // Ignore unnamed fields unless they're anonymous structs/unions.
2008   if (name.empty() && !type->isRecordType())
2009     return;
2010 
2011   llvm::DIType *FieldType;
2012   if (field->isBitField()) {
2013     llvm::DIDerivedType *BitFieldType;
2014     FieldType = BitFieldType = createBitFieldType(field, RecordTy, RD);
2015     if (llvm::DIType *Separator =
2016             createBitFieldSeparatorIfNeeded(field, BitFieldType, elements, RD))
2017       elements.push_back(Separator);
2018   } else {
2019     auto Align = getDeclAlignIfRequired(field, CGM.getContext());
2020     llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field);
2021     FieldType =
2022         createFieldType(name, type, field->getLocation(), field->getAccess(),
2023                         OffsetInBits, Align, tunit, RecordTy, RD, Annotations);
2024   }
2025 
2026   elements.push_back(FieldType);
2027 }
2028 
2029 void CGDebugInfo::CollectRecordNestedType(
2030     const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
2031   QualType Ty = CGM.getContext().getTypeDeclType(TD);
2032   // Injected class names are not considered nested records.
2033   if (isa<InjectedClassNameType>(Ty))
2034     return;
2035   SourceLocation Loc = TD->getLocation();
2036   if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)))
2037     elements.push_back(nestedType);
2038 }
2039 
2040 void CGDebugInfo::CollectRecordFields(
2041     const RecordDecl *record, llvm::DIFile *tunit,
2042     SmallVectorImpl<llvm::Metadata *> &elements,
2043     llvm::DICompositeType *RecordTy) {
2044   const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
2045 
2046   if (CXXDecl && CXXDecl->isLambda())
2047     CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
2048   else {
2049     const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
2050 
2051     // Field number for non-static fields.
2052     unsigned fieldNo = 0;
2053 
2054     // Static and non-static members should appear in the same order as
2055     // the corresponding declarations in the source program.
2056     for (const auto *I : record->decls())
2057       if (const auto *V = dyn_cast<VarDecl>(I)) {
2058         if (V->hasAttr<NoDebugAttr>())
2059           continue;
2060 
2061         // Skip variable template specializations when emitting CodeView. MSVC
2062         // doesn't emit them.
2063         if (CGM.getCodeGenOpts().EmitCodeView &&
2064             isa<VarTemplateSpecializationDecl>(V))
2065           continue;
2066 
2067         if (isa<VarTemplatePartialSpecializationDecl>(V))
2068           continue;
2069 
2070         // Reuse the existing static member declaration if one exists
2071         auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
2072         if (MI != StaticDataMemberCache.end()) {
2073           assert(MI->second &&
2074                  "Static data member declaration should still exist");
2075           elements.push_back(MI->second);
2076         } else {
2077           auto Field = CreateRecordStaticField(V, RecordTy, record);
2078           elements.push_back(Field);
2079         }
2080       } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
2081         CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
2082                                  elements, RecordTy, record);
2083 
2084         // Bump field number for next field.
2085         ++fieldNo;
2086       } else if (CGM.getCodeGenOpts().EmitCodeView) {
2087         // Debug info for nested types is included in the member list only for
2088         // CodeView.
2089         if (const auto *nestedType = dyn_cast<TypeDecl>(I)) {
2090           // MSVC doesn't generate nested type for anonymous struct/union.
2091           if (isa<RecordDecl>(I) &&
2092               cast<RecordDecl>(I)->isAnonymousStructOrUnion())
2093             continue;
2094           if (!nestedType->isImplicit() &&
2095               nestedType->getDeclContext() == record)
2096             CollectRecordNestedType(nestedType, elements);
2097         }
2098       }
2099   }
2100 }
2101 
2102 llvm::DISubroutineType *
2103 CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
2104                                    llvm::DIFile *Unit) {
2105   const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
2106   if (Method->isStatic())
2107     return cast_or_null<llvm::DISubroutineType>(
2108         getOrCreateType(QualType(Func, 0), Unit));
2109 
2110   QualType ThisType;
2111   if (!Method->hasCXXExplicitFunctionObjectParameter())
2112     ThisType = Method->getThisType();
2113 
2114   return getOrCreateInstanceMethodType(ThisType, Func, Unit);
2115 }
2116 
2117 llvm::DISubroutineType *CGDebugInfo::getOrCreateMethodTypeForDestructor(
2118     const CXXMethodDecl *Method, llvm::DIFile *Unit, QualType FNType) {
2119   const FunctionProtoType *Func = FNType->getAs<FunctionProtoType>();
2120   // skip the first param since it is also this
2121   return getOrCreateInstanceMethodType(Method->getThisType(), Func, Unit, true);
2122 }
2123 
2124 llvm::DISubroutineType *
2125 CGDebugInfo::getOrCreateInstanceMethodType(QualType ThisPtr,
2126                                            const FunctionProtoType *Func,
2127                                            llvm::DIFile *Unit, bool SkipFirst) {
2128   FunctionProtoType::ExtProtoInfo EPI = Func->getExtProtoInfo();
2129   Qualifiers &Qc = EPI.TypeQuals;
2130   Qc.removeConst();
2131   Qc.removeVolatile();
2132   Qc.removeRestrict();
2133   Qc.removeUnaligned();
2134   // Keep the removed qualifiers in sync with
2135   // CreateQualifiedType(const FunctionPrototype*, DIFile *Unit)
2136   // On a 'real' member function type, these qualifiers are carried on the type
2137   // of the first parameter, not as separate DW_TAG_const_type (etc) decorator
2138   // tags around them. (But, in the raw function types with qualifiers, they have
2139   // to use wrapper types.)
2140 
2141   // Add "this" pointer.
2142   const auto *OriginalFunc = cast<llvm::DISubroutineType>(
2143       getOrCreateType(CGM.getContext().getFunctionType(
2144                           Func->getReturnType(), Func->getParamTypes(), EPI),
2145                       Unit));
2146   llvm::DITypeRefArray Args = OriginalFunc->getTypeArray();
2147   assert(Args.size() && "Invalid number of arguments!");
2148 
2149   SmallVector<llvm::Metadata *, 16> Elts;
2150 
2151   // First element is always return type. For 'void' functions it is NULL.
2152   Elts.push_back(Args[0]);
2153 
2154   const bool HasExplicitObjectParameter = ThisPtr.isNull();
2155 
2156   // "this" pointer is always first argument. For explicit "this"
2157   // parameters, it will already be in Args[1].
2158   if (!HasExplicitObjectParameter) {
2159     llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
2160     TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
2161     ThisPtrType =
2162         DBuilder.createObjectPointerType(ThisPtrType, /*Implicit=*/true);
2163     Elts.push_back(ThisPtrType);
2164   }
2165 
2166   // Copy rest of the arguments.
2167   for (unsigned i = (SkipFirst ? 2 : 1), e = Args.size(); i < e; ++i)
2168     Elts.push_back(Args[i]);
2169 
2170   // Attach FlagObjectPointer to the explicit "this" parameter.
2171   if (HasExplicitObjectParameter) {
2172     assert(Elts.size() >= 2 && Args.size() >= 2 &&
2173            "Expected at least return type and object parameter.");
2174     Elts[1] = DBuilder.createObjectPointerType(Args[1], /*Implicit=*/false);
2175   }
2176 
2177   llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
2178 
2179   return DBuilder.createSubroutineType(EltTypeArray, OriginalFunc->getFlags(),
2180                                        getDwarfCC(Func->getCallConv()));
2181 }
2182 
2183 /// isFunctionLocalClass - Return true if CXXRecordDecl is defined
2184 /// inside a function.
2185 static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
2186   if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
2187     return isFunctionLocalClass(NRD);
2188   if (isa<FunctionDecl>(RD->getDeclContext()))
2189     return true;
2190   return false;
2191 }
2192 
2193 llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
2194     const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
2195   bool IsCtorOrDtor =
2196       isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
2197 
2198   StringRef MethodName = getFunctionName(Method);
2199   llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
2200 
2201   // Since a single ctor/dtor corresponds to multiple functions, it doesn't
2202   // make sense to give a single ctor/dtor a linkage name.
2203   StringRef MethodLinkageName;
2204   // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
2205   // property to use here. It may've been intended to model "is non-external
2206   // type" but misses cases of non-function-local but non-external classes such
2207   // as those in anonymous namespaces as well as the reverse - external types
2208   // that are function local, such as those in (non-local) inline functions.
2209   if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
2210     MethodLinkageName = CGM.getMangledName(Method);
2211 
2212   // Get the location for the method.
2213   llvm::DIFile *MethodDefUnit = nullptr;
2214   unsigned MethodLine = 0;
2215   if (!Method->isImplicit()) {
2216     MethodDefUnit = getOrCreateFile(Method->getLocation());
2217     MethodLine = getLineNumber(Method->getLocation());
2218   }
2219 
2220   // Collect virtual method info.
2221   llvm::DIType *ContainingType = nullptr;
2222   unsigned VIndex = 0;
2223   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
2224   llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
2225   int ThisAdjustment = 0;
2226 
2227   if (VTableContextBase::hasVtableSlot(Method)) {
2228     if (Method->isPureVirtual())
2229       SPFlags |= llvm::DISubprogram::SPFlagPureVirtual;
2230     else
2231       SPFlags |= llvm::DISubprogram::SPFlagVirtual;
2232 
2233     if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
2234       // It doesn't make sense to give a virtual destructor a vtable index,
2235       // since a single destructor has two entries in the vtable.
2236       if (!isa<CXXDestructorDecl>(Method))
2237         VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
2238     } else {
2239       // Emit MS ABI vftable information.  There is only one entry for the
2240       // deleting dtor.
2241       const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
2242       GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
2243       MethodVFTableLocation ML =
2244           CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
2245       VIndex = ML.Index;
2246 
2247       // CodeView only records the vftable offset in the class that introduces
2248       // the virtual method. This is possible because, unlike Itanium, the MS
2249       // C++ ABI does not include all virtual methods from non-primary bases in
2250       // the vtable for the most derived class. For example, if C inherits from
2251       // A and B, C's primary vftable will not include B's virtual methods.
2252       if (Method->size_overridden_methods() == 0)
2253         Flags |= llvm::DINode::FlagIntroducedVirtual;
2254 
2255       // The 'this' adjustment accounts for both the virtual and non-virtual
2256       // portions of the adjustment. Presumably the debugger only uses it when
2257       // it knows the dynamic type of an object.
2258       ThisAdjustment = CGM.getCXXABI()
2259                            .getVirtualFunctionPrologueThisAdjustment(GD)
2260                            .getQuantity();
2261     }
2262     ContainingType = RecordTy;
2263   }
2264 
2265   if (Method->getCanonicalDecl()->isDeleted())
2266     SPFlags |= llvm::DISubprogram::SPFlagDeleted;
2267 
2268   if (Method->isNoReturn())
2269     Flags |= llvm::DINode::FlagNoReturn;
2270 
2271   if (Method->isStatic())
2272     Flags |= llvm::DINode::FlagStaticMember;
2273   if (Method->isImplicit())
2274     Flags |= llvm::DINode::FlagArtificial;
2275   Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
2276   if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
2277     if (CXXC->isExplicit())
2278       Flags |= llvm::DINode::FlagExplicit;
2279   } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
2280     if (CXXC->isExplicit())
2281       Flags |= llvm::DINode::FlagExplicit;
2282   }
2283   if (Method->hasPrototype())
2284     Flags |= llvm::DINode::FlagPrototyped;
2285   if (Method->getRefQualifier() == RQ_LValue)
2286     Flags |= llvm::DINode::FlagLValueReference;
2287   if (Method->getRefQualifier() == RQ_RValue)
2288     Flags |= llvm::DINode::FlagRValueReference;
2289   if (!Method->isExternallyVisible())
2290     SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
2291   if (CGM.getLangOpts().Optimize)
2292     SPFlags |= llvm::DISubprogram::SPFlagOptimized;
2293 
2294   // In this debug mode, emit type info for a class when its constructor type
2295   // info is emitted.
2296   if (DebugKind == llvm::codegenoptions::DebugInfoConstructor)
2297     if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method))
2298       completeUnusedClass(*CD->getParent());
2299 
2300   llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
2301   llvm::DISubprogram *SP = DBuilder.createMethod(
2302       RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
2303       MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags,
2304       TParamsArray.get(), /*ThrownTypes*/ nullptr,
2305       CGM.getCodeGenOpts().DebugKeyInstructions);
2306 
2307   SPCache[Method->getCanonicalDecl()].reset(SP);
2308 
2309   return SP;
2310 }
2311 
2312 void CGDebugInfo::CollectCXXMemberFunctions(
2313     const CXXRecordDecl *RD, llvm::DIFile *Unit,
2314     SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
2315 
2316   // Since we want more than just the individual member decls if we
2317   // have templated functions iterate over every declaration to gather
2318   // the functions.
2319   for (const auto *I : RD->decls()) {
2320     const auto *Method = dyn_cast<CXXMethodDecl>(I);
2321     // If the member is implicit, don't add it to the member list. This avoids
2322     // the member being added to type units by LLVM, while still allowing it
2323     // to be emitted into the type declaration/reference inside the compile
2324     // unit.
2325     // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
2326     // FIXME: Handle Using(Shadow?)Decls here to create
2327     // DW_TAG_imported_declarations inside the class for base decls brought into
2328     // derived classes. GDB doesn't seem to notice/leverage these when I tried
2329     // it, so I'm not rushing to fix this. (GCC seems to produce them, if
2330     // referenced)
2331     if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
2332       continue;
2333 
2334     if (Method->getType()->castAs<FunctionProtoType>()->getContainedAutoType())
2335       continue;
2336 
2337     // Reuse the existing member function declaration if it exists.
2338     // It may be associated with the declaration of the type & should be
2339     // reused as we're building the definition.
2340     //
2341     // This situation can arise in the vtable-based debug info reduction where
2342     // implicit members are emitted in a non-vtable TU.
2343     auto MI = SPCache.find(Method->getCanonicalDecl());
2344     EltTys.push_back(MI == SPCache.end()
2345                          ? CreateCXXMemberFunction(Method, Unit, RecordTy)
2346                          : static_cast<llvm::Metadata *>(MI->second));
2347   }
2348 }
2349 
2350 void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
2351                                   SmallVectorImpl<llvm::Metadata *> &EltTys,
2352                                   llvm::DIType *RecordTy) {
2353   llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
2354   CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
2355                      llvm::DINode::FlagZero);
2356 
2357   // If we are generating CodeView debug info, we also need to emit records for
2358   // indirect virtual base classes.
2359   if (CGM.getCodeGenOpts().EmitCodeView) {
2360     CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
2361                        llvm::DINode::FlagIndirectVirtualBase);
2362   }
2363 }
2364 
2365 void CGDebugInfo::CollectCXXBasesAux(
2366     const CXXRecordDecl *RD, llvm::DIFile *Unit,
2367     SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
2368     const CXXRecordDecl::base_class_const_range &Bases,
2369     llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
2370     llvm::DINode::DIFlags StartingFlags) {
2371   const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2372   for (const auto &BI : Bases) {
2373     const auto *Base =
2374         cast<CXXRecordDecl>(BI.getType()->castAs<RecordType>()->getDecl());
2375     if (!SeenTypes.insert(Base).second)
2376       continue;
2377     auto *BaseTy = getOrCreateType(BI.getType(), Unit);
2378     llvm::DINode::DIFlags BFlags = StartingFlags;
2379     uint64_t BaseOffset;
2380     uint32_t VBPtrOffset = 0;
2381 
2382     if (BI.isVirtual()) {
2383       if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
2384         // virtual base offset offset is -ve. The code generator emits dwarf
2385         // expression where it expects +ve number.
2386         BaseOffset = 0 - CGM.getItaniumVTableContext()
2387                              .getVirtualBaseOffsetOffset(RD, Base)
2388                              .getQuantity();
2389       } else {
2390         // In the MS ABI, store the vbtable offset, which is analogous to the
2391         // vbase offset offset in Itanium.
2392         BaseOffset =
2393             4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
2394         VBPtrOffset = CGM.getContext()
2395                           .getASTRecordLayout(RD)
2396                           .getVBPtrOffset()
2397                           .getQuantity();
2398       }
2399       BFlags |= llvm::DINode::FlagVirtual;
2400     } else
2401       BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
2402     // FIXME: Inconsistent units for BaseOffset. It is in bytes when
2403     // BI->isVirtual() and bits when not.
2404 
2405     BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
2406     llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset,
2407                                                    VBPtrOffset, BFlags);
2408     EltTys.push_back(DTy);
2409   }
2410 }
2411 
2412 llvm::DINodeArray
2413 CGDebugInfo::CollectTemplateParams(std::optional<TemplateArgs> OArgs,
2414                                    llvm::DIFile *Unit) {
2415   if (!OArgs)
2416     return llvm::DINodeArray();
2417   TemplateArgs &Args = *OArgs;
2418   SmallVector<llvm::Metadata *, 16> TemplateParams;
2419   for (unsigned i = 0, e = Args.Args.size(); i != e; ++i) {
2420     const TemplateArgument &TA = Args.Args[i];
2421     StringRef Name;
2422     const bool defaultParameter = TA.getIsDefaulted();
2423     if (Args.TList)
2424       Name = Args.TList->getParam(i)->getName();
2425 
2426     switch (TA.getKind()) {
2427     case TemplateArgument::Type: {
2428       llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
2429       TemplateParams.push_back(DBuilder.createTemplateTypeParameter(
2430           TheCU, Name, TTy, defaultParameter));
2431 
2432     } break;
2433     case TemplateArgument::Integral: {
2434       llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
2435       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2436           TheCU, Name, TTy, defaultParameter,
2437           llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
2438     } break;
2439     case TemplateArgument::Declaration: {
2440       const ValueDecl *D = TA.getAsDecl();
2441       QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
2442       llvm::DIType *TTy = getOrCreateType(T, Unit);
2443       llvm::Constant *V = nullptr;
2444       // Skip retrieve the value if that template parameter has cuda device
2445       // attribute, i.e. that value is not available at the host side.
2446       if (!CGM.getLangOpts().CUDA || CGM.getLangOpts().CUDAIsDevice ||
2447           !D->hasAttr<CUDADeviceAttr>()) {
2448         // Variable pointer template parameters have a value that is the address
2449         // of the variable.
2450         if (const auto *VD = dyn_cast<VarDecl>(D))
2451           V = CGM.GetAddrOfGlobalVar(VD);
2452         // Member function pointers have special support for building them,
2453         // though this is currently unsupported in LLVM CodeGen.
2454         else if (const auto *MD = dyn_cast<CXXMethodDecl>(D);
2455                  MD && MD->isImplicitObjectMemberFunction())
2456           V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
2457         else if (const auto *FD = dyn_cast<FunctionDecl>(D))
2458           V = CGM.GetAddrOfFunction(FD);
2459         // Member data pointers have special handling too to compute the fixed
2460         // offset within the object.
2461         else if (const auto *MPT =
2462                      dyn_cast<MemberPointerType>(T.getTypePtr())) {
2463           // These five lines (& possibly the above member function pointer
2464           // handling) might be able to be refactored to use similar code in
2465           // CodeGenModule::getMemberPointerConstant
2466           uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
2467           CharUnits chars =
2468               CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
2469           V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
2470         } else if (const auto *GD = dyn_cast<MSGuidDecl>(D)) {
2471           V = CGM.GetAddrOfMSGuidDecl(GD).getPointer();
2472         } else if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) {
2473           if (T->isRecordType())
2474             V = ConstantEmitter(CGM).emitAbstract(
2475                 SourceLocation(), TPO->getValue(), TPO->getType());
2476           else
2477             V = CGM.GetAddrOfTemplateParamObject(TPO).getPointer();
2478         }
2479         assert(V && "Failed to find template parameter pointer");
2480         V = V->stripPointerCasts();
2481       }
2482       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2483           TheCU, Name, TTy, defaultParameter, cast_or_null<llvm::Constant>(V)));
2484     } break;
2485     case TemplateArgument::NullPtr: {
2486       QualType T = TA.getNullPtrType();
2487       llvm::DIType *TTy = getOrCreateType(T, Unit);
2488       llvm::Constant *V = nullptr;
2489       // Special case member data pointer null values since they're actually -1
2490       // instead of zero.
2491       if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
2492         // But treat member function pointers as simple zero integers because
2493         // it's easier than having a special case in LLVM's CodeGen. If LLVM
2494         // CodeGen grows handling for values of non-null member function
2495         // pointers then perhaps we could remove this special case and rely on
2496         // EmitNullMemberPointer for member function pointers.
2497         if (MPT->isMemberDataPointer())
2498           V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
2499       if (!V)
2500         V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
2501       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2502           TheCU, Name, TTy, defaultParameter, V));
2503     } break;
2504     case TemplateArgument::StructuralValue: {
2505       QualType T = TA.getStructuralValueType();
2506       llvm::DIType *TTy = getOrCreateType(T, Unit);
2507       llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(
2508           SourceLocation(), TA.getAsStructuralValue(), T);
2509       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2510           TheCU, Name, TTy, defaultParameter, V));
2511     } break;
2512     case TemplateArgument::Template: {
2513       std::string QualName;
2514       llvm::raw_string_ostream OS(QualName);
2515       TA.getAsTemplate().getAsTemplateDecl()->printQualifiedName(
2516           OS, getPrintingPolicy());
2517       TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
2518           TheCU, Name, nullptr, QualName, defaultParameter));
2519       break;
2520     }
2521     case TemplateArgument::Pack:
2522       TemplateParams.push_back(DBuilder.createTemplateParameterPack(
2523           TheCU, Name, nullptr,
2524           CollectTemplateParams({{nullptr, TA.getPackAsArray()}}, Unit)));
2525       break;
2526     case TemplateArgument::Expression: {
2527       const Expr *E = TA.getAsExpr();
2528       QualType T = E->getType();
2529       if (E->isGLValue())
2530         T = CGM.getContext().getLValueReferenceType(T);
2531       llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
2532       assert(V && "Expression in template argument isn't constant");
2533       llvm::DIType *TTy = getOrCreateType(T, Unit);
2534       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2535           TheCU, Name, TTy, defaultParameter, V->stripPointerCasts()));
2536     } break;
2537     // And the following should never occur:
2538     case TemplateArgument::TemplateExpansion:
2539     case TemplateArgument::Null:
2540       llvm_unreachable(
2541           "These argument types shouldn't exist in concrete types");
2542     }
2543   }
2544   return DBuilder.getOrCreateArray(TemplateParams);
2545 }
2546 
2547 std::optional<CGDebugInfo::TemplateArgs>
2548 CGDebugInfo::GetTemplateArgs(const FunctionDecl *FD) const {
2549   if (FD->getTemplatedKind() ==
2550       FunctionDecl::TK_FunctionTemplateSpecialization) {
2551     const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
2552                                              ->getTemplate()
2553                                              ->getTemplateParameters();
2554     return {{TList, FD->getTemplateSpecializationArgs()->asArray()}};
2555   }
2556   return std::nullopt;
2557 }
2558 std::optional<CGDebugInfo::TemplateArgs>
2559 CGDebugInfo::GetTemplateArgs(const VarDecl *VD) const {
2560   // Always get the full list of parameters, not just the ones from the
2561   // specialization. A partial specialization may have fewer parameters than
2562   // there are arguments.
2563   auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VD);
2564   if (!TS)
2565     return std::nullopt;
2566   VarTemplateDecl *T = TS->getSpecializedTemplate();
2567   const TemplateParameterList *TList = T->getTemplateParameters();
2568   auto TA = TS->getTemplateArgs().asArray();
2569   return {{TList, TA}};
2570 }
2571 std::optional<CGDebugInfo::TemplateArgs>
2572 CGDebugInfo::GetTemplateArgs(const RecordDecl *RD) const {
2573   if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
2574     // Always get the full list of parameters, not just the ones from the
2575     // specialization. A partial specialization may have fewer parameters than
2576     // there are arguments.
2577     TemplateParameterList *TPList =
2578         TSpecial->getSpecializedTemplate()->getTemplateParameters();
2579     const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
2580     return {{TPList, TAList.asArray()}};
2581   }
2582   return std::nullopt;
2583 }
2584 
2585 llvm::DINodeArray
2586 CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
2587                                            llvm::DIFile *Unit) {
2588   return CollectTemplateParams(GetTemplateArgs(FD), Unit);
2589 }
2590 
2591 llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL,
2592                                                         llvm::DIFile *Unit) {
2593   return CollectTemplateParams(GetTemplateArgs(VL), Unit);
2594 }
2595 
2596 llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(const RecordDecl *RD,
2597                                                         llvm::DIFile *Unit) {
2598   return CollectTemplateParams(GetTemplateArgs(RD), Unit);
2599 }
2600 
2601 llvm::DINodeArray CGDebugInfo::CollectBTFDeclTagAnnotations(const Decl *D) {
2602   if (!D->hasAttr<BTFDeclTagAttr>())
2603     return nullptr;
2604 
2605   SmallVector<llvm::Metadata *, 4> Annotations;
2606   for (const auto *I : D->specific_attrs<BTFDeclTagAttr>()) {
2607     llvm::Metadata *Ops[2] = {
2608         llvm::MDString::get(CGM.getLLVMContext(), StringRef("btf_decl_tag")),
2609         llvm::MDString::get(CGM.getLLVMContext(), I->getBTFDeclTag())};
2610     Annotations.push_back(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
2611   }
2612   return DBuilder.getOrCreateArray(Annotations);
2613 }
2614 
2615 llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
2616   if (VTablePtrType)
2617     return VTablePtrType;
2618 
2619   ASTContext &Context = CGM.getContext();
2620 
2621   /* Function type */
2622   llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
2623   llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
2624   llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
2625   unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
2626   unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
2627   std::optional<unsigned> DWARFAddressSpace =
2628       CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
2629 
2630   llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType(
2631       SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type");
2632   VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
2633   return VTablePtrType;
2634 }
2635 
2636 StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
2637   // Copy the gdb compatible name on the side and use its reference.
2638   return internString("_vptr$", RD->getNameAsString());
2639 }
2640 
2641 // Emit symbol for the debugger that points to the vtable address for
2642 // the given class. The symbol is named as '_vtable$'.
2643 // The debugger does not need to know any details about the contents of the
2644 // vtable as it can work this out using its knowledge of the ABI and the
2645 // existing information in the DWARF. The type is assumed to be 'void *'.
2646 void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable,
2647                                    const CXXRecordDecl *RD) {
2648   if (!CGM.getTarget().getCXXABI().isItaniumFamily() ||
2649       CGM.getTarget().getTriple().isOSBinFormatCOFF())
2650     return;
2651 
2652   ASTContext &Context = CGM.getContext();
2653   StringRef SymbolName = "_vtable$";
2654   SourceLocation Loc;
2655   QualType VoidPtr = Context.getPointerType(Context.VoidTy);
2656 
2657   // We deal with two different contexts:
2658   // - The type for the variable, which is part of the class that has the
2659   //   vtable, is placed in the context of the DICompositeType metadata.
2660   // - The DIGlobalVariable for the vtable is put in the DICompileUnitScope.
2661 
2662   // The created non-member should be mark as 'artificial'. It will be
2663   // placed inside the scope of the C++ class/structure.
2664   llvm::DIScope *DContext = getContextDescriptor(RD, TheCU);
2665   auto *Ctxt = cast<llvm::DICompositeType>(DContext);
2666   llvm::DIFile *Unit = getOrCreateFile(Loc);
2667   llvm::DIType *VTy = getOrCreateType(VoidPtr, Unit);
2668   llvm::DINode::DIFlags Flags = getAccessFlag(AccessSpecifier::AS_private, RD) |
2669                                 llvm::DINode::FlagArtificial;
2670   auto Tag = CGM.getCodeGenOpts().DwarfVersion >= 5
2671                  ? llvm::dwarf::DW_TAG_variable
2672                  : llvm::dwarf::DW_TAG_member;
2673   llvm::DIDerivedType *DT = DBuilder.createStaticMemberType(
2674       Ctxt, SymbolName, Unit, /*LineNumber=*/0, VTy, Flags,
2675       /*Val=*/nullptr, Tag);
2676 
2677   // Use the same vtable pointer to global alignment for the symbol.
2678   unsigned PAlign = CGM.getVtableGlobalVarAlignment();
2679 
2680   // The global variable is in the CU scope, and links back to the type it's
2681   // "within" via the declaration field.
2682   llvm::DIGlobalVariableExpression *GVE =
2683       DBuilder.createGlobalVariableExpression(
2684           TheCU, SymbolName, VTable->getName(), Unit, /*LineNo=*/0,
2685           getOrCreateType(VoidPtr, Unit), VTable->hasLocalLinkage(),
2686           /*isDefined=*/true, nullptr, DT, /*TemplateParameters=*/nullptr,
2687           PAlign);
2688   VTable->addDebugInfo(GVE);
2689 }
2690 
2691 StringRef CGDebugInfo::getDynamicInitializerName(const VarDecl *VD,
2692                                                  DynamicInitKind StubKind,
2693                                                  llvm::Function *InitFn) {
2694   // If we're not emitting codeview, use the mangled name. For Itanium, this is
2695   // arbitrary.
2696   if (!CGM.getCodeGenOpts().EmitCodeView ||
2697       StubKind == DynamicInitKind::GlobalArrayDestructor)
2698     return InitFn->getName();
2699 
2700   // Print the normal qualified name for the variable, then break off the last
2701   // NNS, and add the appropriate other text. Clang always prints the global
2702   // variable name without template arguments, so we can use rsplit("::") and
2703   // then recombine the pieces.
2704   SmallString<128> QualifiedGV;
2705   StringRef Quals;
2706   StringRef GVName;
2707   {
2708     llvm::raw_svector_ostream OS(QualifiedGV);
2709     VD->printQualifiedName(OS, getPrintingPolicy());
2710     std::tie(Quals, GVName) = OS.str().rsplit("::");
2711     if (GVName.empty())
2712       std::swap(Quals, GVName);
2713   }
2714 
2715   SmallString<128> InitName;
2716   llvm::raw_svector_ostream OS(InitName);
2717   if (!Quals.empty())
2718     OS << Quals << "::";
2719 
2720   switch (StubKind) {
2721   case DynamicInitKind::NoStub:
2722   case DynamicInitKind::GlobalArrayDestructor:
2723     llvm_unreachable("not an initializer");
2724   case DynamicInitKind::Initializer:
2725     OS << "`dynamic initializer for '";
2726     break;
2727   case DynamicInitKind::AtExit:
2728     OS << "`dynamic atexit destructor for '";
2729     break;
2730   }
2731 
2732   OS << GVName;
2733 
2734   // Add any template specialization args.
2735   if (const auto *VTpl = dyn_cast<VarTemplateSpecializationDecl>(VD)) {
2736     printTemplateArgumentList(OS, VTpl->getTemplateArgs().asArray(),
2737                               getPrintingPolicy());
2738   }
2739 
2740   OS << '\'';
2741 
2742   return internString(OS.str());
2743 }
2744 
2745 void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
2746                                     SmallVectorImpl<llvm::Metadata *> &EltTys) {
2747   // If this class is not dynamic then there is not any vtable info to collect.
2748   if (!RD->isDynamicClass())
2749     return;
2750 
2751   // Don't emit any vtable shape or vptr info if this class doesn't have an
2752   // extendable vfptr. This can happen if the class doesn't have virtual
2753   // methods, or in the MS ABI if those virtual methods only come from virtually
2754   // inherited bases.
2755   const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2756   if (!RL.hasExtendableVFPtr())
2757     return;
2758 
2759   // CodeView needs to know how large the vtable of every dynamic class is, so
2760   // emit a special named pointer type into the element list. The vptr type
2761   // points to this type as well.
2762   llvm::DIType *VPtrTy = nullptr;
2763   bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
2764                          CGM.getTarget().getCXXABI().isMicrosoft();
2765   if (NeedVTableShape) {
2766     uint64_t PtrWidth =
2767         CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
2768     const VTableLayout &VFTLayout =
2769         CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
2770     unsigned VSlotCount =
2771         VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
2772     unsigned VTableWidth = PtrWidth * VSlotCount;
2773     unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
2774     std::optional<unsigned> DWARFAddressSpace =
2775         CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
2776 
2777     // Create a very wide void* type and insert it directly in the element list.
2778     llvm::DIType *VTableType = DBuilder.createPointerType(
2779         nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type");
2780     EltTys.push_back(VTableType);
2781 
2782     // The vptr is a pointer to this special vtable type.
2783     VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
2784   }
2785 
2786   // If there is a primary base then the artificial vptr member lives there.
2787   if (RL.getPrimaryBase())
2788     return;
2789 
2790   if (!VPtrTy)
2791     VPtrTy = getOrCreateVTablePtrType(Unit);
2792 
2793   unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
2794   llvm::DIType *VPtrMember =
2795       DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
2796                                 llvm::DINode::FlagArtificial, VPtrTy);
2797   EltTys.push_back(VPtrMember);
2798 }
2799 
2800 llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
2801                                                  SourceLocation Loc) {
2802   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
2803   llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
2804   return T;
2805 }
2806 
2807 llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
2808                                                     SourceLocation Loc) {
2809   return getOrCreateStandaloneType(D, Loc);
2810 }
2811 
2812 llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
2813                                                      SourceLocation Loc) {
2814   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
2815   assert(!D.isNull() && "null type");
2816   llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
2817   assert(T && "could not create debug info for type");
2818 
2819   RetainedTypes.push_back(D.getAsOpaquePtr());
2820   return T;
2821 }
2822 
2823 void CGDebugInfo::addHeapAllocSiteMetadata(llvm::CallBase *CI,
2824                                            QualType AllocatedTy,
2825                                            SourceLocation Loc) {
2826   if (CGM.getCodeGenOpts().getDebugInfo() <=
2827       llvm::codegenoptions::DebugLineTablesOnly)
2828     return;
2829   llvm::MDNode *node;
2830   if (AllocatedTy->isVoidType())
2831     node = llvm::MDNode::get(CGM.getLLVMContext(), {});
2832   else
2833     node = getOrCreateType(AllocatedTy, getOrCreateFile(Loc));
2834 
2835   CI->setMetadata("heapallocsite", node);
2836 }
2837 
2838 void CGDebugInfo::completeType(const EnumDecl *ED) {
2839   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
2840     return;
2841   QualType Ty = CGM.getContext().getEnumType(ED);
2842   void *TyPtr = Ty.getAsOpaquePtr();
2843   auto I = TypeCache.find(TyPtr);
2844   if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
2845     return;
2846   llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
2847   assert(!Res->isForwardDecl());
2848   TypeCache[TyPtr].reset(Res);
2849 }
2850 
2851 void CGDebugInfo::completeType(const RecordDecl *RD) {
2852   if (DebugKind > llvm::codegenoptions::LimitedDebugInfo ||
2853       !CGM.getLangOpts().CPlusPlus)
2854     completeRequiredType(RD);
2855 }
2856 
2857 /// Return true if the class or any of its methods are marked dllimport.
2858 static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
2859   if (RD->hasAttr<DLLImportAttr>())
2860     return true;
2861   for (const CXXMethodDecl *MD : RD->methods())
2862     if (MD->hasAttr<DLLImportAttr>())
2863       return true;
2864   return false;
2865 }
2866 
2867 /// Does a type definition exist in an imported clang module?
2868 static bool isDefinedInClangModule(const RecordDecl *RD) {
2869   // Only definitions that where imported from an AST file come from a module.
2870   if (!RD || !RD->isFromASTFile())
2871     return false;
2872   // Anonymous entities cannot be addressed. Treat them as not from module.
2873   if (!RD->isExternallyVisible() && RD->getName().empty())
2874     return false;
2875   if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
2876     if (!CXXDecl->isCompleteDefinition())
2877       return false;
2878     // Check wether RD is a template.
2879     auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
2880     if (TemplateKind != TSK_Undeclared) {
2881       // Unfortunately getOwningModule() isn't accurate enough to find the
2882       // owning module of a ClassTemplateSpecializationDecl that is inside a
2883       // namespace spanning multiple modules.
2884       bool Explicit = false;
2885       if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl))
2886         Explicit = TD->isExplicitInstantiationOrSpecialization();
2887       if (!Explicit && CXXDecl->getEnclosingNamespaceContext())
2888         return false;
2889       // This is a template, check the origin of the first member.
2890       if (CXXDecl->field_begin() == CXXDecl->field_end())
2891         return TemplateKind == TSK_ExplicitInstantiationDeclaration;
2892       if (!CXXDecl->field_begin()->isFromASTFile())
2893         return false;
2894     }
2895   }
2896   return true;
2897 }
2898 
2899 void CGDebugInfo::completeClassData(const RecordDecl *RD) {
2900   if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
2901     if (CXXRD->isDynamicClass() &&
2902         CGM.getVTableLinkage(CXXRD) ==
2903             llvm::GlobalValue::AvailableExternallyLinkage &&
2904         !isClassOrMethodDLLImport(CXXRD))
2905       return;
2906 
2907   if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
2908     return;
2909 
2910   completeClass(RD);
2911 }
2912 
2913 void CGDebugInfo::completeClass(const RecordDecl *RD) {
2914   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
2915     return;
2916   QualType Ty = CGM.getContext().getRecordType(RD);
2917   void *TyPtr = Ty.getAsOpaquePtr();
2918   auto I = TypeCache.find(TyPtr);
2919   if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
2920     return;
2921 
2922   // We want the canonical definition of the structure to not
2923   // be the typedef. Since that would lead to circular typedef
2924   // metadata.
2925   auto [Res, PrefRes] = CreateTypeDefinition(Ty->castAs<RecordType>());
2926   assert(!Res->isForwardDecl());
2927   TypeCache[TyPtr].reset(Res);
2928 }
2929 
2930 static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
2931                                         CXXRecordDecl::method_iterator End) {
2932   for (CXXMethodDecl *MD : llvm::make_range(I, End))
2933     if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
2934       if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
2935           !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
2936         return true;
2937   return false;
2938 }
2939 
2940 static bool canUseCtorHoming(const CXXRecordDecl *RD) {
2941   // Constructor homing can be used for classes that cannnot be constructed
2942   // without emitting code for one of their constructors. This is classes that
2943   // don't have trivial or constexpr constructors, or can be created from
2944   // aggregate initialization. Also skip lambda objects because they don't call
2945   // constructors.
2946 
2947   // Skip this optimization if the class or any of its methods are marked
2948   // dllimport.
2949   if (isClassOrMethodDLLImport(RD))
2950     return false;
2951 
2952   if (RD->isLambda() || RD->isAggregate() ||
2953       RD->hasTrivialDefaultConstructor() ||
2954       RD->hasConstexprNonCopyMoveConstructor())
2955     return false;
2956 
2957   for (const CXXConstructorDecl *Ctor : RD->ctors()) {
2958     if (Ctor->isCopyOrMoveConstructor())
2959       continue;
2960     if (!Ctor->isDeleted())
2961       return true;
2962   }
2963   return false;
2964 }
2965 
2966 static bool shouldOmitDefinition(llvm::codegenoptions::DebugInfoKind DebugKind,
2967                                  bool DebugTypeExtRefs, const RecordDecl *RD,
2968                                  const LangOptions &LangOpts) {
2969   if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
2970     return true;
2971 
2972   if (auto *ES = RD->getASTContext().getExternalSource())
2973     if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
2974       return true;
2975 
2976   // Only emit forward declarations in line tables only to keep debug info size
2977   // small. This only applies to CodeView, since we don't emit types in DWARF
2978   // line tables only.
2979   if (DebugKind == llvm::codegenoptions::DebugLineTablesOnly)
2980     return true;
2981 
2982   if (DebugKind > llvm::codegenoptions::LimitedDebugInfo ||
2983       RD->hasAttr<StandaloneDebugAttr>())
2984     return false;
2985 
2986   if (!LangOpts.CPlusPlus)
2987     return false;
2988 
2989   if (!RD->isCompleteDefinitionRequired())
2990     return true;
2991 
2992   const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
2993 
2994   if (!CXXDecl)
2995     return false;
2996 
2997   // Only emit complete debug info for a dynamic class when its vtable is
2998   // emitted.  However, Microsoft debuggers don't resolve type information
2999   // across DLL boundaries, so skip this optimization if the class or any of its
3000   // methods are marked dllimport. This isn't a complete solution, since objects
3001   // without any dllimport methods can be used in one DLL and constructed in
3002   // another, but it is the current behavior of LimitedDebugInfo.
3003   if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
3004       !isClassOrMethodDLLImport(CXXDecl) && !CXXDecl->hasAttr<MSNoVTableAttr>())
3005     return true;
3006 
3007   TemplateSpecializationKind Spec = TSK_Undeclared;
3008   if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
3009     Spec = SD->getSpecializationKind();
3010 
3011   if (Spec == TSK_ExplicitInstantiationDeclaration &&
3012       hasExplicitMemberDefinition(CXXDecl->method_begin(),
3013                                   CXXDecl->method_end()))
3014     return true;
3015 
3016   // In constructor homing mode, only emit complete debug info for a class
3017   // when its constructor is emitted.
3018   if ((DebugKind == llvm::codegenoptions::DebugInfoConstructor) &&
3019       canUseCtorHoming(CXXDecl))
3020     return true;
3021 
3022   return false;
3023 }
3024 
3025 void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
3026   if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
3027     return;
3028 
3029   QualType Ty = CGM.getContext().getRecordType(RD);
3030   llvm::DIType *T = getTypeOrNull(Ty);
3031   if (T && T->isForwardDecl())
3032     completeClassData(RD);
3033 }
3034 
3035 llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
3036   RecordDecl *RD = Ty->getDecl();
3037   llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
3038   if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
3039                                 CGM.getLangOpts())) {
3040     if (!T)
3041       T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
3042     return T;
3043   }
3044 
3045   auto [Def, Pref] = CreateTypeDefinition(Ty);
3046 
3047   return Pref ? Pref : Def;
3048 }
3049 
3050 llvm::DIType *CGDebugInfo::GetPreferredNameType(const CXXRecordDecl *RD,
3051                                                 llvm::DIFile *Unit) {
3052   if (!RD)
3053     return nullptr;
3054 
3055   auto const *PNA = RD->getAttr<PreferredNameAttr>();
3056   if (!PNA)
3057     return nullptr;
3058 
3059   return getOrCreateType(PNA->getTypedefType(), Unit);
3060 }
3061 
3062 std::pair<llvm::DIType *, llvm::DIType *>
3063 CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
3064   RecordDecl *RD = Ty->getDecl();
3065 
3066   // Get overall information about the record type for the debug info.
3067   llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
3068 
3069   // Records and classes and unions can all be recursive.  To handle them, we
3070   // first generate a debug descriptor for the struct as a forward declaration.
3071   // Then (if it is a definition) we go through and get debug info for all of
3072   // its members.  Finally, we create a descriptor for the complete type (which
3073   // may refer to the forward decl if the struct is recursive) and replace all
3074   // uses of the forward declaration with the final definition.
3075   llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty);
3076 
3077   const RecordDecl *D = RD->getDefinition();
3078   if (!D || !D->isCompleteDefinition())
3079     return {FwdDecl, nullptr};
3080 
3081   if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
3082     CollectContainingType(CXXDecl, FwdDecl);
3083 
3084   // Push the struct on region stack.
3085   LexicalBlockStack.emplace_back(&*FwdDecl);
3086   RegionMap[Ty->getDecl()].reset(FwdDecl);
3087 
3088   // Convert all the elements.
3089   SmallVector<llvm::Metadata *, 16> EltTys;
3090   // what about nested types?
3091 
3092   // Note: The split of CXXDecl information here is intentional, the
3093   // gdb tests will depend on a certain ordering at printout. The debug
3094   // information offsets are still correct if we merge them all together
3095   // though.
3096   const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
3097   if (CXXDecl) {
3098     CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
3099     CollectVTableInfo(CXXDecl, DefUnit, EltTys);
3100   }
3101 
3102   // Collect data fields (including static variables and any initializers).
3103   CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
3104   if (CXXDecl && !CGM.getCodeGenOpts().DebugOmitUnreferencedMethods)
3105     CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
3106 
3107   LexicalBlockStack.pop_back();
3108   RegionMap.erase(Ty->getDecl());
3109 
3110   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
3111   DBuilder.replaceArrays(FwdDecl, Elements);
3112 
3113   if (FwdDecl->isTemporary())
3114     FwdDecl =
3115         llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
3116 
3117   RegionMap[Ty->getDecl()].reset(FwdDecl);
3118 
3119   if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB)
3120     if (auto *PrefDI = GetPreferredNameType(CXXDecl, DefUnit))
3121       return {FwdDecl, PrefDI};
3122 
3123   return {FwdDecl, nullptr};
3124 }
3125 
3126 llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
3127                                       llvm::DIFile *Unit) {
3128   // Ignore protocols.
3129   return getOrCreateType(Ty->getBaseType(), Unit);
3130 }
3131 
3132 llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
3133                                       llvm::DIFile *Unit) {
3134   // Ignore protocols.
3135   SourceLocation Loc = Ty->getDecl()->getLocation();
3136 
3137   // Use Typedefs to represent ObjCTypeParamType.
3138   return DBuilder.createTypedef(
3139       getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
3140       Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
3141       getDeclContextDescriptor(Ty->getDecl()));
3142 }
3143 
3144 /// \return true if Getter has the default name for the property PD.
3145 static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
3146                                  const ObjCMethodDecl *Getter) {
3147   assert(PD);
3148   if (!Getter)
3149     return true;
3150 
3151   assert(Getter->getDeclName().isObjCZeroArgSelector());
3152   return PD->getName() ==
3153          Getter->getDeclName().getObjCSelector().getNameForSlot(0);
3154 }
3155 
3156 /// \return true if Setter has the default name for the property PD.
3157 static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
3158                                  const ObjCMethodDecl *Setter) {
3159   assert(PD);
3160   if (!Setter)
3161     return true;
3162 
3163   assert(Setter->getDeclName().isObjCOneArgSelector());
3164   return SelectorTable::constructSetterName(PD->getName()) ==
3165          Setter->getDeclName().getObjCSelector().getNameForSlot(0);
3166 }
3167 
3168 llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
3169                                       llvm::DIFile *Unit) {
3170   ObjCInterfaceDecl *ID = Ty->getDecl();
3171   if (!ID)
3172     return nullptr;
3173 
3174   auto RuntimeLang =
3175       static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
3176 
3177   // Return a forward declaration if this type was imported from a clang module,
3178   // and this is not the compile unit with the implementation of the type (which
3179   // may contain hidden ivars).
3180   if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
3181       !ID->getImplementation())
3182     return DBuilder.createForwardDecl(
3183         llvm::dwarf::DW_TAG_structure_type, ID->getName(),
3184         getDeclContextDescriptor(ID), Unit, 0, RuntimeLang);
3185 
3186   // Get overall information about the record type for the debug info.
3187   llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
3188   unsigned Line = getLineNumber(ID->getLocation());
3189 
3190   // If this is just a forward declaration return a special forward-declaration
3191   // debug type since we won't be able to lay out the entire type.
3192   ObjCInterfaceDecl *Def = ID->getDefinition();
3193   if (!Def || !Def->getImplementation()) {
3194     llvm::DIScope *Mod = getParentModuleOrNull(ID);
3195     llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
3196         llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
3197         DefUnit, Line, RuntimeLang);
3198     ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
3199     return FwdDecl;
3200   }
3201 
3202   return CreateTypeDefinition(Ty, Unit);
3203 }
3204 
3205 llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
3206                                                   bool CreateSkeletonCU) {
3207   // Use the Module pointer as the key into the cache. This is a
3208   // nullptr if the "Module" is a PCH, which is safe because we don't
3209   // support chained PCH debug info, so there can only be a single PCH.
3210   const Module *M = Mod.getModuleOrNull();
3211   auto ModRef = ModuleCache.find(M);
3212   if (ModRef != ModuleCache.end())
3213     return cast<llvm::DIModule>(ModRef->second);
3214 
3215   // Macro definitions that were defined with "-D" on the command line.
3216   SmallString<128> ConfigMacros;
3217   {
3218     llvm::raw_svector_ostream OS(ConfigMacros);
3219     const auto &PPOpts = CGM.getPreprocessorOpts();
3220     unsigned I = 0;
3221     // Translate the macro definitions back into a command line.
3222     for (auto &M : PPOpts.Macros) {
3223       if (++I > 1)
3224         OS << " ";
3225       const std::string &Macro = M.first;
3226       bool Undef = M.second;
3227       OS << "\"-" << (Undef ? 'U' : 'D');
3228       for (char c : Macro)
3229         switch (c) {
3230         case '\\':
3231           OS << "\\\\";
3232           break;
3233         case '"':
3234           OS << "\\\"";
3235           break;
3236         default:
3237           OS << c;
3238         }
3239       OS << '\"';
3240     }
3241   }
3242 
3243   bool IsRootModule = M ? !M->Parent : true;
3244   // When a module name is specified as -fmodule-name, that module gets a
3245   // clang::Module object, but it won't actually be built or imported; it will
3246   // be textual.
3247   if (CreateSkeletonCU && IsRootModule && Mod.getASTFile().empty() && M)
3248     assert(StringRef(M->Name).starts_with(CGM.getLangOpts().ModuleName) &&
3249            "clang module without ASTFile must be specified by -fmodule-name");
3250 
3251   // Return a StringRef to the remapped Path.
3252   auto RemapPath = [this](StringRef Path) -> std::string {
3253     std::string Remapped = remapDIPath(Path);
3254     StringRef Relative(Remapped);
3255     StringRef CompDir = TheCU->getDirectory();
3256     if (Relative.consume_front(CompDir))
3257       Relative.consume_front(llvm::sys::path::get_separator());
3258 
3259     return Relative.str();
3260   };
3261 
3262   if (CreateSkeletonCU && IsRootModule && !Mod.getASTFile().empty()) {
3263     // PCH files don't have a signature field in the control block,
3264     // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
3265     // We use the lower 64 bits for debug info.
3266 
3267     uint64_t Signature = 0;
3268     if (const auto &ModSig = Mod.getSignature())
3269       Signature = ModSig.truncatedValue();
3270     else
3271       Signature = ~1ULL;
3272 
3273     llvm::DIBuilder DIB(CGM.getModule());
3274     SmallString<0> PCM;
3275     if (!llvm::sys::path::is_absolute(Mod.getASTFile())) {
3276       if (CGM.getHeaderSearchOpts().ModuleFileHomeIsCwd)
3277         PCM = getCurrentDirname();
3278       else
3279         PCM = Mod.getPath();
3280     }
3281     llvm::sys::path::append(PCM, Mod.getASTFile());
3282     DIB.createCompileUnit(
3283         TheCU->getSourceLanguage(),
3284         // TODO: Support "Source" from external AST providers?
3285         DIB.createFile(Mod.getModuleName(), TheCU->getDirectory()),
3286         TheCU->getProducer(), false, StringRef(), 0, RemapPath(PCM),
3287         llvm::DICompileUnit::FullDebug, Signature);
3288     DIB.finalize();
3289   }
3290 
3291   llvm::DIModule *Parent =
3292       IsRootModule ? nullptr
3293                    : getOrCreateModuleRef(ASTSourceDescriptor(*M->Parent),
3294                                           CreateSkeletonCU);
3295   std::string IncludePath = Mod.getPath().str();
3296   llvm::DIModule *DIMod =
3297       DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
3298                             RemapPath(IncludePath));
3299   ModuleCache[M].reset(DIMod);
3300   return DIMod;
3301 }
3302 
3303 llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
3304                                                 llvm::DIFile *Unit) {
3305   ObjCInterfaceDecl *ID = Ty->getDecl();
3306   llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
3307   unsigned Line = getLineNumber(ID->getLocation());
3308   unsigned RuntimeLang = TheCU->getSourceLanguage();
3309 
3310   // Bit size, align and offset of the type.
3311   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3312   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3313 
3314   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
3315   if (ID->getImplementation())
3316     Flags |= llvm::DINode::FlagObjcClassComplete;
3317 
3318   llvm::DIScope *Mod = getParentModuleOrNull(ID);
3319   llvm::DICompositeType *RealDecl = DBuilder.createStructType(
3320       Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
3321       nullptr, llvm::DINodeArray(), RuntimeLang);
3322 
3323   QualType QTy(Ty, 0);
3324   TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
3325 
3326   // Push the struct on region stack.
3327   LexicalBlockStack.emplace_back(RealDecl);
3328   RegionMap[Ty->getDecl()].reset(RealDecl);
3329 
3330   // Convert all the elements.
3331   SmallVector<llvm::Metadata *, 16> EltTys;
3332 
3333   ObjCInterfaceDecl *SClass = ID->getSuperClass();
3334   if (SClass) {
3335     llvm::DIType *SClassTy =
3336         getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
3337     if (!SClassTy)
3338       return nullptr;
3339 
3340     llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
3341                                                       llvm::DINode::FlagZero);
3342     EltTys.push_back(InhTag);
3343   }
3344 
3345   // Create entries for all of the properties.
3346   auto AddProperty = [&](const ObjCPropertyDecl *PD) {
3347     SourceLocation Loc = PD->getLocation();
3348     llvm::DIFile *PUnit = getOrCreateFile(Loc);
3349     unsigned PLine = getLineNumber(Loc);
3350     ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
3351     ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
3352     llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
3353         PD->getName(), PUnit, PLine,
3354         hasDefaultGetterName(PD, Getter) ? ""
3355                                          : getSelectorName(PD->getGetterName()),
3356         hasDefaultSetterName(PD, Setter) ? ""
3357                                          : getSelectorName(PD->getSetterName()),
3358         PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
3359     EltTys.push_back(PropertyNode);
3360   };
3361   {
3362     // Use 'char' for the isClassProperty bit as DenseSet requires space for
3363     // empty/tombstone keys in the data type (and bool is too small for that).
3364     typedef std::pair<char, const IdentifierInfo *> IsClassAndIdent;
3365     /// List of already emitted properties. Two distinct class and instance
3366     /// properties can share the same identifier (but not two instance
3367     /// properties or two class properties).
3368     llvm::DenseSet<IsClassAndIdent> PropertySet;
3369     /// Returns the IsClassAndIdent key for the given property.
3370     auto GetIsClassAndIdent = [](const ObjCPropertyDecl *PD) {
3371       return std::make_pair(PD->isClassProperty(), PD->getIdentifier());
3372     };
3373     for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
3374       for (auto *PD : ClassExt->properties()) {
3375         PropertySet.insert(GetIsClassAndIdent(PD));
3376         AddProperty(PD);
3377       }
3378     for (const auto *PD : ID->properties()) {
3379       // Don't emit duplicate metadata for properties that were already in a
3380       // class extension.
3381       if (!PropertySet.insert(GetIsClassAndIdent(PD)).second)
3382         continue;
3383       AddProperty(PD);
3384     }
3385   }
3386 
3387   const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
3388   unsigned FieldNo = 0;
3389   for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
3390        Field = Field->getNextIvar(), ++FieldNo) {
3391     llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
3392     if (!FieldTy)
3393       return nullptr;
3394 
3395     StringRef FieldName = Field->getName();
3396 
3397     // Ignore unnamed fields.
3398     if (FieldName.empty())
3399       continue;
3400 
3401     // Get the location for the field.
3402     llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
3403     unsigned FieldLine = getLineNumber(Field->getLocation());
3404     QualType FType = Field->getType();
3405     uint64_t FieldSize = 0;
3406     uint32_t FieldAlign = 0;
3407 
3408     if (!FType->isIncompleteArrayType()) {
3409 
3410       // Bit size, align and offset of the type.
3411       FieldSize = Field->isBitField() ? Field->getBitWidthValue()
3412                                       : CGM.getContext().getTypeSize(FType);
3413       FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
3414     }
3415 
3416     uint64_t FieldOffset;
3417     if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
3418       // We don't know the runtime offset of an ivar if we're using the
3419       // non-fragile ABI.  For bitfields, use the bit offset into the first
3420       // byte of storage of the bitfield.  For other fields, use zero.
3421       if (Field->isBitField()) {
3422         FieldOffset =
3423             CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
3424         FieldOffset %= CGM.getContext().getCharWidth();
3425       } else {
3426         FieldOffset = 0;
3427       }
3428     } else {
3429       FieldOffset = RL.getFieldOffset(FieldNo);
3430     }
3431 
3432     llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
3433     if (Field->getAccessControl() == ObjCIvarDecl::Protected)
3434       Flags = llvm::DINode::FlagProtected;
3435     else if (Field->getAccessControl() == ObjCIvarDecl::Private)
3436       Flags = llvm::DINode::FlagPrivate;
3437     else if (Field->getAccessControl() == ObjCIvarDecl::Public)
3438       Flags = llvm::DINode::FlagPublic;
3439 
3440     if (Field->isBitField())
3441       Flags |= llvm::DINode::FlagBitField;
3442 
3443     llvm::MDNode *PropertyNode = nullptr;
3444     if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
3445       if (ObjCPropertyImplDecl *PImpD =
3446               ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
3447         if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
3448           SourceLocation Loc = PD->getLocation();
3449           llvm::DIFile *PUnit = getOrCreateFile(Loc);
3450           unsigned PLine = getLineNumber(Loc);
3451           ObjCMethodDecl *Getter = PImpD->getGetterMethodDecl();
3452           ObjCMethodDecl *Setter = PImpD->getSetterMethodDecl();
3453           PropertyNode = DBuilder.createObjCProperty(
3454               PD->getName(), PUnit, PLine,
3455               hasDefaultGetterName(PD, Getter)
3456                   ? ""
3457                   : getSelectorName(PD->getGetterName()),
3458               hasDefaultSetterName(PD, Setter)
3459                   ? ""
3460                   : getSelectorName(PD->getSetterName()),
3461               PD->getPropertyAttributes(),
3462               getOrCreateType(PD->getType(), PUnit));
3463         }
3464       }
3465     }
3466     FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
3467                                       FieldSize, FieldAlign, FieldOffset, Flags,
3468                                       FieldTy, PropertyNode);
3469     EltTys.push_back(FieldTy);
3470   }
3471 
3472   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
3473   DBuilder.replaceArrays(RealDecl, Elements);
3474 
3475   LexicalBlockStack.pop_back();
3476   return RealDecl;
3477 }
3478 
3479 llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
3480                                       llvm::DIFile *Unit) {
3481   if (Ty->isPackedVectorBoolType(CGM.getContext())) {
3482     // Boolean ext_vector_type(N) are special because their real element type
3483     // (bits of bit size) is not their Clang element type (_Bool of size byte).
3484     // For now, we pretend the boolean vector were actually a vector of bytes
3485     // (where each byte represents 8 bits of the actual vector).
3486     // FIXME Debug info should actually represent this proper as a vector mask
3487     // type.
3488     auto &Ctx = CGM.getContext();
3489     uint64_t Size = CGM.getContext().getTypeSize(Ty);
3490     uint64_t NumVectorBytes = Size / Ctx.getCharWidth();
3491 
3492     // Construct the vector of 'char' type.
3493     QualType CharVecTy =
3494         Ctx.getVectorType(Ctx.CharTy, NumVectorBytes, VectorKind::Generic);
3495     return CreateType(CharVecTy->getAs<VectorType>(), Unit);
3496   }
3497 
3498   llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
3499   int64_t Count = Ty->getNumElements();
3500 
3501   llvm::Metadata *Subscript;
3502   QualType QTy(Ty, 0);
3503   auto SizeExpr = SizeExprCache.find(QTy);
3504   if (SizeExpr != SizeExprCache.end())
3505     Subscript = DBuilder.getOrCreateSubrange(
3506         SizeExpr->getSecond() /*count*/, nullptr /*lowerBound*/,
3507         nullptr /*upperBound*/, nullptr /*stride*/);
3508   else {
3509     auto *CountNode =
3510         llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3511             llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count ? Count : -1));
3512     Subscript = DBuilder.getOrCreateSubrange(
3513         CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3514         nullptr /*stride*/);
3515   }
3516   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
3517 
3518   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3519   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3520 
3521   return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
3522 }
3523 
3524 llvm::DIType *CGDebugInfo::CreateType(const ConstantMatrixType *Ty,
3525                                       llvm::DIFile *Unit) {
3526   // FIXME: Create another debug type for matrices
3527   // For the time being, it treats it like a nested ArrayType.
3528 
3529   llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
3530   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3531   uint32_t Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3532 
3533   // Create ranges for both dimensions.
3534   llvm::SmallVector<llvm::Metadata *, 2> Subscripts;
3535   auto *ColumnCountNode =
3536       llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3537           llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumColumns()));
3538   auto *RowCountNode =
3539       llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3540           llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumRows()));
3541   Subscripts.push_back(DBuilder.getOrCreateSubrange(
3542       ColumnCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3543       nullptr /*stride*/));
3544   Subscripts.push_back(DBuilder.getOrCreateSubrange(
3545       RowCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3546       nullptr /*stride*/));
3547   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
3548   return DBuilder.createArrayType(Size, Align, ElementTy, SubscriptArray);
3549 }
3550 
3551 llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
3552   uint64_t Size;
3553   uint32_t Align;
3554 
3555   // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
3556   if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
3557     Size = 0;
3558     Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
3559                                    CGM.getContext());
3560   } else if (Ty->isIncompleteArrayType()) {
3561     Size = 0;
3562     if (Ty->getElementType()->isIncompleteType())
3563       Align = 0;
3564     else
3565       Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
3566   } else if (Ty->isIncompleteType()) {
3567     Size = 0;
3568     Align = 0;
3569   } else {
3570     // Size and align of the whole array, not the element type.
3571     Size = CGM.getContext().getTypeSize(Ty);
3572     Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3573   }
3574 
3575   // Add the dimensions of the array.  FIXME: This loses CV qualifiers from
3576   // interior arrays, do we care?  Why aren't nested arrays represented the
3577   // obvious/recursive way?
3578   SmallVector<llvm::Metadata *, 8> Subscripts;
3579   QualType EltTy(Ty, 0);
3580   while ((Ty = dyn_cast<ArrayType>(EltTy))) {
3581     // If the number of elements is known, then count is that number. Otherwise,
3582     // it's -1. This allows us to represent a subrange with an array of 0
3583     // elements, like this:
3584     //
3585     //   struct foo {
3586     //     int x[0];
3587     //   };
3588     int64_t Count = -1; // Count == -1 is an unbounded array.
3589     if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
3590       Count = CAT->getZExtSize();
3591     else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
3592       if (Expr *Size = VAT->getSizeExpr()) {
3593         Expr::EvalResult Result;
3594         if (Size->EvaluateAsInt(Result, CGM.getContext()))
3595           Count = Result.Val.getInt().getExtValue();
3596       }
3597     }
3598 
3599     auto SizeNode = SizeExprCache.find(EltTy);
3600     if (SizeNode != SizeExprCache.end())
3601       Subscripts.push_back(DBuilder.getOrCreateSubrange(
3602           SizeNode->getSecond() /*count*/, nullptr /*lowerBound*/,
3603           nullptr /*upperBound*/, nullptr /*stride*/));
3604     else {
3605       auto *CountNode =
3606           llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3607               llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count));
3608       Subscripts.push_back(DBuilder.getOrCreateSubrange(
3609           CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3610           nullptr /*stride*/));
3611     }
3612     EltTy = Ty->getElementType();
3613   }
3614 
3615   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
3616 
3617   return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
3618                                   SubscriptArray);
3619 }
3620 
3621 llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
3622                                       llvm::DIFile *Unit) {
3623   return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
3624                                Ty->getPointeeType(), Unit);
3625 }
3626 
3627 llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
3628                                       llvm::DIFile *Unit) {
3629   llvm::dwarf::Tag Tag = llvm::dwarf::DW_TAG_rvalue_reference_type;
3630   // DW_TAG_rvalue_reference_type was introduced in DWARF 4.
3631   if (CGM.getCodeGenOpts().DebugStrictDwarf &&
3632       CGM.getCodeGenOpts().DwarfVersion < 4)
3633     Tag = llvm::dwarf::DW_TAG_reference_type;
3634 
3635   return CreatePointerLikeType(Tag, Ty, Ty->getPointeeType(), Unit);
3636 }
3637 
3638 llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
3639                                       llvm::DIFile *U) {
3640   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
3641   uint64_t Size = 0;
3642 
3643   if (!Ty->isIncompleteType()) {
3644     Size = CGM.getContext().getTypeSize(Ty);
3645 
3646     // Set the MS inheritance model. There is no flag for the unspecified model.
3647     if (CGM.getTarget().getCXXABI().isMicrosoft()) {
3648       switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
3649       case MSInheritanceModel::Single:
3650         Flags |= llvm::DINode::FlagSingleInheritance;
3651         break;
3652       case MSInheritanceModel::Multiple:
3653         Flags |= llvm::DINode::FlagMultipleInheritance;
3654         break;
3655       case MSInheritanceModel::Virtual:
3656         Flags |= llvm::DINode::FlagVirtualInheritance;
3657         break;
3658       case MSInheritanceModel::Unspecified:
3659         break;
3660       }
3661     }
3662   }
3663 
3664   llvm::DIType *ClassType = getOrCreateType(
3665       QualType(Ty->getMostRecentCXXRecordDecl()->getTypeForDecl(), 0), U);
3666   if (Ty->isMemberDataPointerType())
3667     return DBuilder.createMemberPointerType(
3668         getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
3669         Flags);
3670 
3671   const FunctionProtoType *FPT =
3672       Ty->getPointeeType()->castAs<FunctionProtoType>();
3673   return DBuilder.createMemberPointerType(
3674       getOrCreateInstanceMethodType(
3675           CXXMethodDecl::getThisType(FPT, Ty->getMostRecentCXXRecordDecl()),
3676           FPT, U),
3677       ClassType, Size, /*Align=*/0, Flags);
3678 }
3679 
3680 llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
3681   auto *FromTy = getOrCreateType(Ty->getValueType(), U);
3682   return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
3683 }
3684 
3685 llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) {
3686   return getOrCreateType(Ty->getElementType(), U);
3687 }
3688 
3689 llvm::DIType *CGDebugInfo::CreateType(const HLSLAttributedResourceType *Ty,
3690                                       llvm::DIFile *U) {
3691   return getOrCreateType(Ty->getWrappedType(), U);
3692 }
3693 
3694 llvm::DIType *CGDebugInfo::CreateType(const HLSLInlineSpirvType *Ty,
3695                                       llvm::DIFile *U) {
3696   // Debug information unneeded.
3697   return nullptr;
3698 }
3699 
3700 llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
3701   const EnumDecl *ED = Ty->getDecl();
3702 
3703   uint64_t Size = 0;
3704   uint32_t Align = 0;
3705   if (!ED->getTypeForDecl()->isIncompleteType()) {
3706     Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
3707     Align = getDeclAlignIfRequired(ED, CGM.getContext());
3708   }
3709 
3710   SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
3711 
3712   bool isImportedFromModule =
3713       DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
3714 
3715   // If this is just a forward declaration, construct an appropriately
3716   // marked node and just return it.
3717   if (isImportedFromModule || !ED->getDefinition()) {
3718     // Note that it is possible for enums to be created as part of
3719     // their own declcontext. In this case a FwdDecl will be created
3720     // twice. This doesn't cause a problem because both FwdDecls are
3721     // entered into the ReplaceMap: finalize() will replace the first
3722     // FwdDecl with the second and then replace the second with
3723     // complete type.
3724     llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
3725     llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
3726     llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
3727         llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
3728 
3729     unsigned Line = getLineNumber(ED->getLocation());
3730     StringRef EDName = ED->getName();
3731     llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
3732         llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
3733         0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
3734 
3735     ReplaceMap.emplace_back(
3736         std::piecewise_construct, std::make_tuple(Ty),
3737         std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
3738     return RetTy;
3739   }
3740 
3741   return CreateTypeDefinition(Ty);
3742 }
3743 
3744 llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
3745   const EnumDecl *ED = Ty->getDecl();
3746   uint64_t Size = 0;
3747   uint32_t Align = 0;
3748   if (!ED->getTypeForDecl()->isIncompleteType()) {
3749     Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
3750     Align = getDeclAlignIfRequired(ED, CGM.getContext());
3751   }
3752 
3753   SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
3754 
3755   SmallVector<llvm::Metadata *, 16> Enumerators;
3756   ED = ED->getDefinition();
3757   assert(ED && "An enumeration definition is required");
3758   for (const auto *Enum : ED->enumerators()) {
3759     Enumerators.push_back(
3760         DBuilder.createEnumerator(Enum->getName(), Enum->getInitVal()));
3761   }
3762 
3763   std::optional<EnumExtensibilityAttr::Kind> EnumKind;
3764   if (auto *Attr = ED->getAttr<EnumExtensibilityAttr>())
3765     EnumKind = Attr->getExtensibility();
3766 
3767   // Return a CompositeType for the enum itself.
3768   llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
3769 
3770   llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
3771   unsigned Line = getLineNumber(ED->getLocation());
3772   llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
3773   llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
3774   return DBuilder.createEnumerationType(
3775       EnumContext, ED->getName(), DefUnit, Line, Size, Align, EltArray, ClassTy,
3776       /*RunTimeLang=*/0, Identifier, ED->isScoped(), EnumKind);
3777 }
3778 
3779 llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
3780                                         unsigned MType, SourceLocation LineLoc,
3781                                         StringRef Name, StringRef Value) {
3782   unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
3783   return DBuilder.createMacro(Parent, Line, MType, Name, Value);
3784 }
3785 
3786 llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
3787                                                     SourceLocation LineLoc,
3788                                                     SourceLocation FileLoc) {
3789   llvm::DIFile *FName = getOrCreateFile(FileLoc);
3790   unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
3791   return DBuilder.createTempMacroFile(Parent, Line, FName);
3792 }
3793 
3794 llvm::DILocation *CGDebugInfo::CreateSyntheticInlineAt(llvm::DebugLoc Location,
3795                                                        StringRef FuncName) {
3796   llvm::DISubprogram *SP =
3797       createInlinedSubprogram(FuncName, Location->getFile());
3798   return llvm::DILocation::get(CGM.getLLVMContext(), /*Line=*/0, /*Column=*/0,
3799                                /*Scope=*/SP, /*InlinedAt=*/Location);
3800 }
3801 
3802 llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
3803     llvm::DebugLoc TrapLocation, StringRef Category, StringRef FailureMsg) {
3804   // Create a debug location from `TrapLocation` that adds an artificial inline
3805   // frame.
3806   SmallString<64> FuncName(ClangTrapPrefix);
3807 
3808   FuncName += "$";
3809   FuncName += Category;
3810   FuncName += "$";
3811   FuncName += FailureMsg;
3812 
3813   return CreateSyntheticInlineAt(TrapLocation, FuncName);
3814 }
3815 
3816 static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
3817   Qualifiers Quals;
3818   do {
3819     Qualifiers InnerQuals = T.getLocalQualifiers();
3820     // Qualifiers::operator+() doesn't like it if you add a Qualifier
3821     // that is already there.
3822     Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
3823     Quals += InnerQuals;
3824     QualType LastT = T;
3825     switch (T->getTypeClass()) {
3826     default:
3827       return C.getQualifiedType(T.getTypePtr(), Quals);
3828     case Type::TemplateSpecialization: {
3829       const auto *Spec = cast<TemplateSpecializationType>(T);
3830       if (Spec->isTypeAlias())
3831         return C.getQualifiedType(T.getTypePtr(), Quals);
3832       T = Spec->desugar();
3833       break;
3834     }
3835     case Type::TypeOfExpr:
3836       T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
3837       break;
3838     case Type::TypeOf:
3839       T = cast<TypeOfType>(T)->getUnmodifiedType();
3840       break;
3841     case Type::Decltype:
3842       T = cast<DecltypeType>(T)->getUnderlyingType();
3843       break;
3844     case Type::UnaryTransform:
3845       T = cast<UnaryTransformType>(T)->getUnderlyingType();
3846       break;
3847     case Type::Attributed:
3848       T = cast<AttributedType>(T)->getEquivalentType();
3849       break;
3850     case Type::BTFTagAttributed:
3851       T = cast<BTFTagAttributedType>(T)->getWrappedType();
3852       break;
3853     case Type::CountAttributed:
3854       T = cast<CountAttributedType>(T)->desugar();
3855       break;
3856     case Type::Elaborated:
3857       T = cast<ElaboratedType>(T)->getNamedType();
3858       break;
3859     case Type::Using:
3860       T = cast<UsingType>(T)->getUnderlyingType();
3861       break;
3862     case Type::Paren:
3863       T = cast<ParenType>(T)->getInnerType();
3864       break;
3865     case Type::MacroQualified:
3866       T = cast<MacroQualifiedType>(T)->getUnderlyingType();
3867       break;
3868     case Type::SubstTemplateTypeParm:
3869       T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
3870       break;
3871     case Type::Auto:
3872     case Type::DeducedTemplateSpecialization: {
3873       QualType DT = cast<DeducedType>(T)->getDeducedType();
3874       assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
3875       T = DT;
3876       break;
3877     }
3878     case Type::PackIndexing: {
3879       T = cast<PackIndexingType>(T)->getSelectedType();
3880       break;
3881     }
3882     case Type::Adjusted:
3883     case Type::Decayed:
3884       // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
3885       T = cast<AdjustedType>(T)->getAdjustedType();
3886       break;
3887     }
3888 
3889     assert(T != LastT && "Type unwrapping failed to unwrap!");
3890     (void)LastT;
3891   } while (true);
3892 }
3893 
3894 llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
3895   assert(Ty == UnwrapTypeForDebugInfo(Ty, CGM.getContext()));
3896   auto It = TypeCache.find(Ty.getAsOpaquePtr());
3897   if (It != TypeCache.end()) {
3898     // Verify that the debug info still exists.
3899     if (llvm::Metadata *V = It->second)
3900       return cast<llvm::DIType>(V);
3901   }
3902 
3903   return nullptr;
3904 }
3905 
3906 void CGDebugInfo::completeTemplateDefinition(
3907     const ClassTemplateSpecializationDecl &SD) {
3908   completeUnusedClass(SD);
3909 }
3910 
3911 void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
3912   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly ||
3913       D.isDynamicClass())
3914     return;
3915 
3916   completeClassData(&D);
3917   // In case this type has no member function definitions being emitted, ensure
3918   // it is retained
3919   RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
3920 }
3921 
3922 llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
3923   if (Ty.isNull())
3924     return nullptr;
3925 
3926   llvm::TimeTraceScope TimeScope("DebugType", [&]() {
3927     std::string Name;
3928     llvm::raw_string_ostream OS(Name);
3929     Ty.print(OS, getPrintingPolicy());
3930     return Name;
3931   });
3932 
3933   // Unwrap the type as needed for debug information.
3934   Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
3935 
3936   if (auto *T = getTypeOrNull(Ty))
3937     return T;
3938 
3939   llvm::DIType *Res = CreateTypeNode(Ty, Unit);
3940   void *TyPtr = Ty.getAsOpaquePtr();
3941 
3942   // And update the type cache.
3943   TypeCache[TyPtr].reset(Res);
3944 
3945   return Res;
3946 }
3947 
3948 llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
3949   // A forward declaration inside a module header does not belong to the module.
3950   if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
3951     return nullptr;
3952   if (DebugTypeExtRefs && D->isFromASTFile()) {
3953     // Record a reference to an imported clang module or precompiled header.
3954     auto *Reader = CGM.getContext().getExternalSource();
3955     auto Idx = D->getOwningModuleID();
3956     auto Info = Reader->getSourceDescriptor(Idx);
3957     if (Info)
3958       return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
3959   } else if (ClangModuleMap) {
3960     // We are building a clang module or a precompiled header.
3961     //
3962     // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
3963     // and it wouldn't be necessary to specify the parent scope
3964     // because the type is already unique by definition (it would look
3965     // like the output of -fno-standalone-debug). On the other hand,
3966     // the parent scope helps a consumer to quickly locate the object
3967     // file where the type's definition is located, so it might be
3968     // best to make this behavior a command line or debugger tuning
3969     // option.
3970     if (Module *M = D->getOwningModule()) {
3971       // This is a (sub-)module.
3972       auto Info = ASTSourceDescriptor(*M);
3973       return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
3974     } else {
3975       // This the precompiled header being built.
3976       return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
3977     }
3978   }
3979 
3980   return nullptr;
3981 }
3982 
3983 llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3984   // Handle qualifiers, which recursively handles what they refer to.
3985   if (Ty.hasLocalQualifiers())
3986     return CreateQualifiedType(Ty, Unit);
3987 
3988   // Work out details of type.
3989   switch (Ty->getTypeClass()) {
3990 #define TYPE(Class, Base)
3991 #define ABSTRACT_TYPE(Class, Base)
3992 #define NON_CANONICAL_TYPE(Class, Base)
3993 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
3994 #include "clang/AST/TypeNodes.inc"
3995     llvm_unreachable("Dependent types cannot show up in debug information");
3996 
3997   case Type::ExtVector:
3998   case Type::Vector:
3999     return CreateType(cast<VectorType>(Ty), Unit);
4000   case Type::ConstantMatrix:
4001     return CreateType(cast<ConstantMatrixType>(Ty), Unit);
4002   case Type::ObjCObjectPointer:
4003     return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
4004   case Type::ObjCObject:
4005     return CreateType(cast<ObjCObjectType>(Ty), Unit);
4006   case Type::ObjCTypeParam:
4007     return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
4008   case Type::ObjCInterface:
4009     return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
4010   case Type::Builtin:
4011     return CreateType(cast<BuiltinType>(Ty));
4012   case Type::Complex:
4013     return CreateType(cast<ComplexType>(Ty));
4014   case Type::Pointer:
4015     return CreateType(cast<PointerType>(Ty), Unit);
4016   case Type::BlockPointer:
4017     return CreateType(cast<BlockPointerType>(Ty), Unit);
4018   case Type::Typedef:
4019     return CreateType(cast<TypedefType>(Ty), Unit);
4020   case Type::Record:
4021     return CreateType(cast<RecordType>(Ty));
4022   case Type::Enum:
4023     return CreateEnumType(cast<EnumType>(Ty));
4024   case Type::FunctionProto:
4025   case Type::FunctionNoProto:
4026     return CreateType(cast<FunctionType>(Ty), Unit);
4027   case Type::ConstantArray:
4028   case Type::VariableArray:
4029   case Type::IncompleteArray:
4030   case Type::ArrayParameter:
4031     return CreateType(cast<ArrayType>(Ty), Unit);
4032 
4033   case Type::LValueReference:
4034     return CreateType(cast<LValueReferenceType>(Ty), Unit);
4035   case Type::RValueReference:
4036     return CreateType(cast<RValueReferenceType>(Ty), Unit);
4037 
4038   case Type::MemberPointer:
4039     return CreateType(cast<MemberPointerType>(Ty), Unit);
4040 
4041   case Type::Atomic:
4042     return CreateType(cast<AtomicType>(Ty), Unit);
4043 
4044   case Type::BitInt:
4045     return CreateType(cast<BitIntType>(Ty));
4046   case Type::Pipe:
4047     return CreateType(cast<PipeType>(Ty), Unit);
4048 
4049   case Type::TemplateSpecialization:
4050     return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
4051   case Type::HLSLAttributedResource:
4052     return CreateType(cast<HLSLAttributedResourceType>(Ty), Unit);
4053   case Type::HLSLInlineSpirv:
4054     return CreateType(cast<HLSLInlineSpirvType>(Ty), Unit);
4055 
4056   case Type::CountAttributed:
4057   case Type::Auto:
4058   case Type::Attributed:
4059   case Type::BTFTagAttributed:
4060   case Type::Adjusted:
4061   case Type::Decayed:
4062   case Type::DeducedTemplateSpecialization:
4063   case Type::Elaborated:
4064   case Type::Using:
4065   case Type::Paren:
4066   case Type::MacroQualified:
4067   case Type::SubstTemplateTypeParm:
4068   case Type::TypeOfExpr:
4069   case Type::TypeOf:
4070   case Type::Decltype:
4071   case Type::PackIndexing:
4072   case Type::UnaryTransform:
4073     break;
4074   }
4075 
4076   llvm_unreachable("type should have been unwrapped!");
4077 }
4078 
4079 llvm::DICompositeType *
4080 CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty) {
4081   QualType QTy(Ty, 0);
4082 
4083   auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
4084 
4085   // We may have cached a forward decl when we could have created
4086   // a non-forward decl. Go ahead and create a non-forward decl
4087   // now.
4088   if (T && !T->isForwardDecl())
4089     return T;
4090 
4091   // Otherwise create the type.
4092   llvm::DICompositeType *Res = CreateLimitedType(Ty);
4093 
4094   // Propagate members from the declaration to the definition
4095   // CreateType(const RecordType*) will overwrite this with the members in the
4096   // correct order if the full type is needed.
4097   DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
4098 
4099   // And update the type cache.
4100   TypeCache[QTy.getAsOpaquePtr()].reset(Res);
4101   return Res;
4102 }
4103 
4104 // TODO: Currently used for context chains when limiting debug info.
4105 llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
4106   RecordDecl *RD = Ty->getDecl();
4107 
4108   // Get overall information about the record type for the debug info.
4109   StringRef RDName = getClassName(RD);
4110   const SourceLocation Loc = RD->getLocation();
4111   llvm::DIFile *DefUnit = nullptr;
4112   unsigned Line = 0;
4113   if (Loc.isValid()) {
4114     DefUnit = getOrCreateFile(Loc);
4115     Line = getLineNumber(Loc);
4116   }
4117 
4118   llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
4119 
4120   // If we ended up creating the type during the context chain construction,
4121   // just return that.
4122   auto *T = cast_or_null<llvm::DICompositeType>(
4123       getTypeOrNull(CGM.getContext().getRecordType(RD)));
4124   if (T && (!T->isForwardDecl() || !RD->getDefinition()))
4125     return T;
4126 
4127   // If this is just a forward or incomplete declaration, construct an
4128   // appropriately marked node and just return it.
4129   const RecordDecl *D = RD->getDefinition();
4130   if (!D || !D->isCompleteDefinition())
4131     return getOrCreateRecordFwdDecl(Ty, RDContext);
4132 
4133   uint64_t Size = CGM.getContext().getTypeSize(Ty);
4134   // __attribute__((aligned)) can increase or decrease alignment *except* on a
4135   // struct or struct member, where it only increases  alignment unless 'packed'
4136   // is also specified. To handle this case, the `getTypeAlignIfRequired` needs
4137   // to be used.
4138   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
4139 
4140   SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
4141 
4142   // Explicitly record the calling convention and export symbols for C++
4143   // records.
4144   auto Flags = llvm::DINode::FlagZero;
4145   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4146     if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect)
4147       Flags |= llvm::DINode::FlagTypePassByReference;
4148     else
4149       Flags |= llvm::DINode::FlagTypePassByValue;
4150 
4151     // Record if a C++ record is non-trivial type.
4152     if (!CXXRD->isTrivial())
4153       Flags |= llvm::DINode::FlagNonTrivial;
4154 
4155     // Record exports it symbols to the containing structure.
4156     if (CXXRD->isAnonymousStructOrUnion())
4157         Flags |= llvm::DINode::FlagExportSymbols;
4158 
4159     Flags |= getAccessFlag(CXXRD->getAccess(),
4160                            dyn_cast<CXXRecordDecl>(CXXRD->getDeclContext()));
4161   }
4162 
4163   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
4164   llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
4165       getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
4166       Flags, Identifier, Annotations);
4167 
4168   // Elements of composite types usually have back to the type, creating
4169   // uniquing cycles.  Distinct nodes are more efficient.
4170   switch (RealDecl->getTag()) {
4171   default:
4172     llvm_unreachable("invalid composite type tag");
4173 
4174   case llvm::dwarf::DW_TAG_array_type:
4175   case llvm::dwarf::DW_TAG_enumeration_type:
4176     // Array elements and most enumeration elements don't have back references,
4177     // so they don't tend to be involved in uniquing cycles and there is some
4178     // chance of merging them when linking together two modules.  Only make
4179     // them distinct if they are ODR-uniqued.
4180     if (Identifier.empty())
4181       break;
4182     [[fallthrough]];
4183 
4184   case llvm::dwarf::DW_TAG_structure_type:
4185   case llvm::dwarf::DW_TAG_union_type:
4186   case llvm::dwarf::DW_TAG_class_type:
4187     // Immediately resolve to a distinct node.
4188     RealDecl =
4189         llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
4190     break;
4191   }
4192 
4193   RegionMap[Ty->getDecl()].reset(RealDecl);
4194   TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
4195 
4196   if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
4197     DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
4198                            CollectCXXTemplateParams(TSpecial, DefUnit));
4199   return RealDecl;
4200 }
4201 
4202 void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
4203                                         llvm::DICompositeType *RealDecl) {
4204   // A class's primary base or the class itself contains the vtable.
4205   llvm::DIType *ContainingType = nullptr;
4206   const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
4207   if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
4208     // Seek non-virtual primary base root.
4209     while (true) {
4210       const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
4211       const CXXRecordDecl *PBT = BRL.getPrimaryBase();
4212       if (PBT && !BRL.isPrimaryBaseVirtual())
4213         PBase = PBT;
4214       else
4215         break;
4216     }
4217     ContainingType = getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
4218                                      getOrCreateFile(RD->getLocation()));
4219   } else if (RD->isDynamicClass())
4220     ContainingType = RealDecl;
4221 
4222   DBuilder.replaceVTableHolder(RealDecl, ContainingType);
4223 }
4224 
4225 llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
4226                                             StringRef Name, uint64_t *Offset) {
4227   llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
4228   uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
4229   auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
4230   llvm::DIType *Ty =
4231       DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
4232                                 *Offset, llvm::DINode::FlagZero, FieldTy);
4233   *Offset += FieldSize;
4234   return Ty;
4235 }
4236 
4237 void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
4238                                            StringRef &Name,
4239                                            StringRef &LinkageName,
4240                                            llvm::DIScope *&FDContext,
4241                                            llvm::DINodeArray &TParamsArray,
4242                                            llvm::DINode::DIFlags &Flags) {
4243   const auto *FD = cast<FunctionDecl>(GD.getCanonicalDecl().getDecl());
4244   Name = getFunctionName(FD);
4245   // Use mangled name as linkage name for C/C++ functions.
4246   if (FD->getType()->getAs<FunctionProtoType>())
4247     LinkageName = CGM.getMangledName(GD);
4248   if (FD->hasPrototype())
4249     Flags |= llvm::DINode::FlagPrototyped;
4250   // No need to replicate the linkage name if it isn't different from the
4251   // subprogram name, no need to have it at all unless coverage is enabled or
4252   // debug is set to more than just line tables or extra debug info is needed.
4253   if (LinkageName == Name ||
4254       (CGM.getCodeGenOpts().CoverageNotesFile.empty() &&
4255        CGM.getCodeGenOpts().CoverageDataFile.empty() &&
4256        !CGM.getCodeGenOpts().DebugInfoForProfiling &&
4257        !CGM.getCodeGenOpts().PseudoProbeForProfiling &&
4258        DebugKind <= llvm::codegenoptions::DebugLineTablesOnly))
4259     LinkageName = StringRef();
4260 
4261   // Emit the function scope in line tables only mode (if CodeView) to
4262   // differentiate between function names.
4263   if (CGM.getCodeGenOpts().hasReducedDebugInfo() ||
4264       (DebugKind == llvm::codegenoptions::DebugLineTablesOnly &&
4265        CGM.getCodeGenOpts().EmitCodeView)) {
4266     if (const NamespaceDecl *NSDecl =
4267             dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
4268       FDContext = getOrCreateNamespace(NSDecl);
4269     else if (const RecordDecl *RDecl =
4270                  dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
4271       llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
4272       FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
4273     }
4274   }
4275   if (CGM.getCodeGenOpts().hasReducedDebugInfo()) {
4276     // Check if it is a noreturn-marked function
4277     if (FD->isNoReturn())
4278       Flags |= llvm::DINode::FlagNoReturn;
4279     // Collect template parameters.
4280     TParamsArray = CollectFunctionTemplateParams(FD, Unit);
4281   }
4282 }
4283 
4284 void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
4285                                       unsigned &LineNo, QualType &T,
4286                                       StringRef &Name, StringRef &LinkageName,
4287                                       llvm::MDTuple *&TemplateParameters,
4288                                       llvm::DIScope *&VDContext) {
4289   Unit = getOrCreateFile(VD->getLocation());
4290   LineNo = getLineNumber(VD->getLocation());
4291 
4292   setLocation(VD->getLocation());
4293 
4294   T = VD->getType();
4295   if (T->isIncompleteArrayType()) {
4296     // CodeGen turns int[] into int[1] so we'll do the same here.
4297     llvm::APInt ConstVal(32, 1);
4298     QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
4299 
4300     T = CGM.getContext().getConstantArrayType(ET, ConstVal, nullptr,
4301                                               ArraySizeModifier::Normal, 0);
4302   }
4303 
4304   Name = VD->getName();
4305   if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
4306       !isa<ObjCMethodDecl>(VD->getDeclContext()))
4307     LinkageName = CGM.getMangledName(VD);
4308   if (LinkageName == Name)
4309     LinkageName = StringRef();
4310 
4311   if (isa<VarTemplateSpecializationDecl>(VD)) {
4312     llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VD, &*Unit);
4313     TemplateParameters = parameterNodes.get();
4314   } else {
4315     TemplateParameters = nullptr;
4316   }
4317 
4318   // Since we emit declarations (DW_AT_members) for static members, place the
4319   // definition of those static members in the namespace they were declared in
4320   // in the source code (the lexical decl context).
4321   // FIXME: Generalize this for even non-member global variables where the
4322   // declaration and definition may have different lexical decl contexts, once
4323   // we have support for emitting declarations of (non-member) global variables.
4324   const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
4325                                                    : VD->getDeclContext();
4326   // When a record type contains an in-line initialization of a static data
4327   // member, and the record type is marked as __declspec(dllexport), an implicit
4328   // definition of the member will be created in the record context.  DWARF
4329   // doesn't seem to have a nice way to describe this in a form that consumers
4330   // are likely to understand, so fake the "normal" situation of a definition
4331   // outside the class by putting it in the global scope.
4332   if (DC->isRecord())
4333     DC = CGM.getContext().getTranslationUnitDecl();
4334 
4335   llvm::DIScope *Mod = getParentModuleOrNull(VD);
4336   VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
4337 }
4338 
4339 llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
4340                                                           bool Stub) {
4341   llvm::DINodeArray TParamsArray;
4342   StringRef Name, LinkageName;
4343   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
4344   llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
4345   SourceLocation Loc = GD.getDecl()->getLocation();
4346   llvm::DIFile *Unit = getOrCreateFile(Loc);
4347   llvm::DIScope *DContext = Unit;
4348   unsigned Line = getLineNumber(Loc);
4349   collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray,
4350                            Flags);
4351   auto *FD = cast<FunctionDecl>(GD.getDecl());
4352 
4353   // Build function type.
4354   SmallVector<QualType, 16> ArgTypes;
4355   for (const ParmVarDecl *Parm : FD->parameters())
4356     ArgTypes.push_back(Parm->getType());
4357 
4358   CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
4359   QualType FnType = CGM.getContext().getFunctionType(
4360       FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
4361   if (!FD->isExternallyVisible())
4362     SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
4363   if (CGM.getLangOpts().Optimize)
4364     SPFlags |= llvm::DISubprogram::SPFlagOptimized;
4365 
4366   if (Stub) {
4367     Flags |= getCallSiteRelatedAttrs();
4368     SPFlags |= llvm::DISubprogram::SPFlagDefinition;
4369     return DBuilder.createFunction(
4370         DContext, Name, LinkageName, Unit, Line,
4371         getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
4372         TParamsArray.get(), getFunctionDeclaration(FD), /*ThrownTypes*/ nullptr,
4373         /*Annotations*/ nullptr, /*TargetFuncName*/ "",
4374         CGM.getCodeGenOpts().DebugKeyInstructions);
4375   }
4376 
4377   llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
4378       DContext, Name, LinkageName, Unit, Line,
4379       getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
4380       TParamsArray.get(), getFunctionDeclaration(FD));
4381   const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
4382   FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
4383                                  std::make_tuple(CanonDecl),
4384                                  std::make_tuple(SP));
4385   return SP;
4386 }
4387 
4388 llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
4389   return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
4390 }
4391 
4392 llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) {
4393   return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
4394 }
4395 
4396 llvm::DIGlobalVariable *
4397 CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
4398   QualType T;
4399   StringRef Name, LinkageName;
4400   SourceLocation Loc = VD->getLocation();
4401   llvm::DIFile *Unit = getOrCreateFile(Loc);
4402   llvm::DIScope *DContext = Unit;
4403   unsigned Line = getLineNumber(Loc);
4404   llvm::MDTuple *TemplateParameters = nullptr;
4405 
4406   collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, TemplateParameters,
4407                       DContext);
4408   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
4409   auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
4410       DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
4411       !VD->isExternallyVisible(), nullptr, TemplateParameters, Align);
4412   FwdDeclReplaceMap.emplace_back(
4413       std::piecewise_construct,
4414       std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
4415       std::make_tuple(static_cast<llvm::Metadata *>(GV)));
4416   return GV;
4417 }
4418 
4419 llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
4420   // We only need a declaration (not a definition) of the type - so use whatever
4421   // we would otherwise do to get a type for a pointee. (forward declarations in
4422   // limited debug info, full definitions (if the type definition is available)
4423   // in unlimited debug info)
4424   if (const auto *TD = dyn_cast<TypeDecl>(D))
4425     return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
4426                            getOrCreateFile(TD->getLocation()));
4427   auto I = DeclCache.find(D->getCanonicalDecl());
4428 
4429   if (I != DeclCache.end()) {
4430     auto N = I->second;
4431     if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
4432       return GVE->getVariable();
4433     return cast<llvm::DINode>(N);
4434   }
4435 
4436   // Search imported declaration cache if it is already defined
4437   // as imported declaration.
4438   auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
4439 
4440   if (IE != ImportedDeclCache.end()) {
4441     auto N = IE->second;
4442     if (auto *GVE = dyn_cast_or_null<llvm::DIImportedEntity>(N))
4443       return cast<llvm::DINode>(GVE);
4444     return dyn_cast_or_null<llvm::DINode>(N);
4445   }
4446 
4447   // No definition for now. Emit a forward definition that might be
4448   // merged with a potential upcoming definition.
4449   if (const auto *FD = dyn_cast<FunctionDecl>(D))
4450     return getFunctionForwardDeclaration(FD);
4451   else if (const auto *VD = dyn_cast<VarDecl>(D))
4452     return getGlobalVariableForwardDeclaration(VD);
4453 
4454   return nullptr;
4455 }
4456 
4457 llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
4458   if (!D || DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
4459     return nullptr;
4460 
4461   const auto *FD = dyn_cast<FunctionDecl>(D);
4462   if (!FD)
4463     return nullptr;
4464 
4465   // Setup context.
4466   auto *S = getDeclContextDescriptor(D);
4467 
4468   auto MI = SPCache.find(FD->getCanonicalDecl());
4469   if (MI == SPCache.end()) {
4470     if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
4471       return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
4472                                      cast<llvm::DICompositeType>(S));
4473     }
4474   }
4475   if (MI != SPCache.end()) {
4476     auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
4477     if (SP && !SP->isDefinition())
4478       return SP;
4479   }
4480 
4481   for (auto *NextFD : FD->redecls()) {
4482     auto MI = SPCache.find(NextFD->getCanonicalDecl());
4483     if (MI != SPCache.end()) {
4484       auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
4485       if (SP && !SP->isDefinition())
4486         return SP;
4487     }
4488   }
4489   return nullptr;
4490 }
4491 
4492 llvm::DISubprogram *CGDebugInfo::getObjCMethodDeclaration(
4493     const Decl *D, llvm::DISubroutineType *FnType, unsigned LineNo,
4494     llvm::DINode::DIFlags Flags, llvm::DISubprogram::DISPFlags SPFlags) {
4495   if (!D || DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
4496     return nullptr;
4497 
4498   const auto *OMD = dyn_cast<ObjCMethodDecl>(D);
4499   if (!OMD)
4500     return nullptr;
4501 
4502   if (CGM.getCodeGenOpts().DwarfVersion < 5 && !OMD->isDirectMethod())
4503     return nullptr;
4504 
4505   if (OMD->isDirectMethod())
4506     SPFlags |= llvm::DISubprogram::SPFlagObjCDirect;
4507 
4508   // Starting with DWARF V5 method declarations are emitted as children of
4509   // the interface type.
4510   auto *ID = dyn_cast_or_null<ObjCInterfaceDecl>(D->getDeclContext());
4511   if (!ID)
4512     ID = OMD->getClassInterface();
4513   if (!ID)
4514     return nullptr;
4515   QualType QTy(ID->getTypeForDecl(), 0);
4516   auto It = TypeCache.find(QTy.getAsOpaquePtr());
4517   if (It == TypeCache.end())
4518     return nullptr;
4519   auto *InterfaceType = cast<llvm::DICompositeType>(It->second);
4520   llvm::DISubprogram *FD = DBuilder.createFunction(
4521       InterfaceType, getObjCMethodName(OMD), StringRef(),
4522       InterfaceType->getFile(), LineNo, FnType, LineNo, Flags, SPFlags);
4523   DBuilder.finalizeSubprogram(FD);
4524   ObjCMethodCache[ID].push_back({FD, OMD->isDirectMethod()});
4525   return FD;
4526 }
4527 
4528 // getOrCreateFunctionType - Construct type. If it is a c++ method, include
4529 // implicit parameter "this".
4530 llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
4531                                                              QualType FnType,
4532                                                              llvm::DIFile *F) {
4533   // In CodeView, we emit the function types in line tables only because the
4534   // only way to distinguish between functions is by display name and type.
4535   if (!D || (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly &&
4536              !CGM.getCodeGenOpts().EmitCodeView))
4537     // Create fake but valid subroutine type. Otherwise -verify would fail, and
4538     // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
4539     return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray({}));
4540 
4541   if (const auto *Method = dyn_cast<CXXDestructorDecl>(D)) {
4542     // Read method type from 'FnType' because 'D.getType()' does not cover
4543     // implicit arguments for destructors.
4544     return getOrCreateMethodTypeForDestructor(Method, F, FnType);
4545   }
4546 
4547   if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
4548     return getOrCreateMethodType(Method, F);
4549 
4550   const auto *FTy = FnType->getAs<FunctionType>();
4551   CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
4552 
4553   if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
4554     // Add "self" and "_cmd"
4555     SmallVector<llvm::Metadata *, 16> Elts;
4556 
4557     // First element is always return type. For 'void' functions it is NULL.
4558     QualType ResultTy = OMethod->getReturnType();
4559 
4560     // Replace the instancetype keyword with the actual type.
4561     if (ResultTy == CGM.getContext().getObjCInstanceType())
4562       ResultTy = CGM.getContext().getPointerType(
4563           QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
4564 
4565     Elts.push_back(getOrCreateType(ResultTy, F));
4566     // "self" pointer is always first argument.
4567     QualType SelfDeclTy;
4568     if (auto *SelfDecl = OMethod->getSelfDecl())
4569       SelfDeclTy = SelfDecl->getType();
4570     else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
4571       if (FPT->getNumParams() > 1)
4572         SelfDeclTy = FPT->getParamType(0);
4573     if (!SelfDeclTy.isNull())
4574       Elts.push_back(
4575           CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
4576     // "_cmd" pointer is always second argument.
4577     Elts.push_back(DBuilder.createArtificialType(
4578         getOrCreateType(CGM.getContext().getObjCSelType(), F)));
4579     // Get rest of the arguments.
4580     for (const auto *PI : OMethod->parameters())
4581       Elts.push_back(getOrCreateType(PI->getType(), F));
4582     // Variadic methods need a special marker at the end of the type list.
4583     if (OMethod->isVariadic())
4584       Elts.push_back(DBuilder.createUnspecifiedParameter());
4585 
4586     llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
4587     return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
4588                                          getDwarfCC(CC));
4589   }
4590 
4591   // Handle variadic function types; they need an additional
4592   // unspecified parameter.
4593   if (const auto *FD = dyn_cast<FunctionDecl>(D))
4594     if (FD->isVariadic()) {
4595       SmallVector<llvm::Metadata *, 16> EltTys;
4596       EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
4597       if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
4598         for (QualType ParamType : FPT->param_types())
4599           EltTys.push_back(getOrCreateType(ParamType, F));
4600       EltTys.push_back(DBuilder.createUnspecifiedParameter());
4601       llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
4602       return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
4603                                            getDwarfCC(CC));
4604     }
4605 
4606   return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
4607 }
4608 
4609 QualType
4610 CGDebugInfo::getFunctionType(const FunctionDecl *FD, QualType RetTy,
4611                              const SmallVectorImpl<const VarDecl *> &Args) {
4612   CallingConv CC = CallingConv::CC_C;
4613   if (FD)
4614     if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>())
4615       CC = SrcFnTy->getCallConv();
4616   SmallVector<QualType, 16> ArgTypes;
4617   for (const VarDecl *VD : Args)
4618     ArgTypes.push_back(VD->getType());
4619   return CGM.getContext().getFunctionType(RetTy, ArgTypes,
4620                                           FunctionProtoType::ExtProtoInfo(CC));
4621 }
4622 
4623 void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
4624                                     SourceLocation ScopeLoc, QualType FnType,
4625                                     llvm::Function *Fn, bool CurFuncIsThunk) {
4626   StringRef Name;
4627   StringRef LinkageName;
4628 
4629   FnBeginRegionCount.push_back(LexicalBlockStack.size());
4630 
4631   const Decl *D = GD.getDecl();
4632   bool HasDecl = (D != nullptr);
4633 
4634   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
4635   llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
4636   llvm::DIFile *Unit = getOrCreateFile(Loc);
4637   llvm::DIScope *FDContext = Unit;
4638   llvm::DINodeArray TParamsArray;
4639   bool KeyInstructions = CGM.getCodeGenOpts().DebugKeyInstructions;
4640   if (!HasDecl) {
4641     // Use llvm function name.
4642     LinkageName = Fn->getName();
4643   } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
4644     // If there is a subprogram for this function available then use it.
4645     auto FI = SPCache.find(FD->getCanonicalDecl());
4646     if (FI != SPCache.end()) {
4647       auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
4648       if (SP && SP->isDefinition()) {
4649         LexicalBlockStack.emplace_back(SP);
4650         RegionMap[D].reset(SP);
4651         return;
4652       }
4653     }
4654     collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
4655                              TParamsArray, Flags);
4656     // Disable KIs if this is a coroutine.
4657     KeyInstructions =
4658         KeyInstructions && !isa_and_present<CoroutineBodyStmt>(FD->getBody());
4659   } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
4660     Name = getObjCMethodName(OMD);
4661     Flags |= llvm::DINode::FlagPrototyped;
4662   } else if (isa<VarDecl>(D) &&
4663              GD.getDynamicInitKind() != DynamicInitKind::NoStub) {
4664     // This is a global initializer or atexit destructor for a global variable.
4665     Name = getDynamicInitializerName(cast<VarDecl>(D), GD.getDynamicInitKind(),
4666                                      Fn);
4667   } else {
4668     Name = Fn->getName();
4669 
4670     if (isa<BlockDecl>(D))
4671       LinkageName = Name;
4672 
4673     Flags |= llvm::DINode::FlagPrototyped;
4674   }
4675   Name.consume_front("\01");
4676 
4677   assert((!D || !isa<VarDecl>(D) ||
4678           GD.getDynamicInitKind() != DynamicInitKind::NoStub) &&
4679          "Unexpected DynamicInitKind !");
4680 
4681   if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>() ||
4682       isa<VarDecl>(D) || isa<CapturedDecl>(D)) {
4683     Flags |= llvm::DINode::FlagArtificial;
4684     // Artificial functions should not silently reuse CurLoc.
4685     CurLoc = SourceLocation();
4686   }
4687 
4688   if (CurFuncIsThunk)
4689     Flags |= llvm::DINode::FlagThunk;
4690 
4691   if (Fn->hasLocalLinkage())
4692     SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
4693   if (CGM.getLangOpts().Optimize)
4694     SPFlags |= llvm::DISubprogram::SPFlagOptimized;
4695 
4696   llvm::DINode::DIFlags FlagsForDef = Flags | getCallSiteRelatedAttrs();
4697   llvm::DISubprogram::DISPFlags SPFlagsForDef =
4698       SPFlags | llvm::DISubprogram::SPFlagDefinition;
4699 
4700   const unsigned LineNo = getLineNumber(Loc.isValid() ? Loc : CurLoc);
4701   unsigned ScopeLine = getLineNumber(ScopeLoc);
4702   llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit);
4703   llvm::DISubprogram *Decl = nullptr;
4704   llvm::DINodeArray Annotations = nullptr;
4705   if (D) {
4706     Decl = isa<ObjCMethodDecl>(D)
4707                ? getObjCMethodDeclaration(D, DIFnType, LineNo, Flags, SPFlags)
4708                : getFunctionDeclaration(D);
4709     Annotations = CollectBTFDeclTagAnnotations(D);
4710   }
4711 
4712   // FIXME: The function declaration we're constructing here is mostly reusing
4713   // declarations from CXXMethodDecl and not constructing new ones for arbitrary
4714   // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
4715   // all subprograms instead of the actual context since subprogram definitions
4716   // are emitted as CU level entities by the backend.
4717   llvm::DISubprogram *SP = DBuilder.createFunction(
4718       FDContext, Name, LinkageName, Unit, LineNo, DIFnType, ScopeLine,
4719       FlagsForDef, SPFlagsForDef, TParamsArray.get(), Decl, nullptr,
4720       Annotations, "", KeyInstructions);
4721   Fn->setSubprogram(SP);
4722 
4723   // We might get here with a VarDecl in the case we're generating
4724   // code for the initialization of globals. Do not record these decls
4725   // as they will overwrite the actual VarDecl Decl in the cache.
4726   if (HasDecl && isa<FunctionDecl>(D))
4727     DeclCache[D->getCanonicalDecl()].reset(SP);
4728 
4729   // Push the function onto the lexical block stack.
4730   LexicalBlockStack.emplace_back(SP);
4731 
4732   if (HasDecl)
4733     RegionMap[D].reset(SP);
4734 }
4735 
4736 void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
4737                                    QualType FnType, llvm::Function *Fn) {
4738   StringRef Name;
4739   StringRef LinkageName;
4740 
4741   const Decl *D = GD.getDecl();
4742   if (!D)
4743     return;
4744 
4745   llvm::TimeTraceScope TimeScope("DebugFunction", [&]() {
4746     return GetName(D, true);
4747   });
4748 
4749   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
4750   llvm::DIFile *Unit = getOrCreateFile(Loc);
4751   bool IsDeclForCallSite = Fn ? true : false;
4752   llvm::DIScope *FDContext =
4753       IsDeclForCallSite ? Unit : getDeclContextDescriptor(D);
4754   llvm::DINodeArray TParamsArray;
4755   if (isa<FunctionDecl>(D)) {
4756     // If there is a DISubprogram for this function available then use it.
4757     collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
4758                              TParamsArray, Flags);
4759   } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
4760     Name = getObjCMethodName(OMD);
4761     Flags |= llvm::DINode::FlagPrototyped;
4762   } else {
4763     llvm_unreachable("not a function or ObjC method");
4764   }
4765   Name.consume_front("\01");
4766 
4767   if (D->isImplicit()) {
4768     Flags |= llvm::DINode::FlagArtificial;
4769     // Artificial functions without a location should not silently reuse CurLoc.
4770     if (Loc.isInvalid())
4771       CurLoc = SourceLocation();
4772   }
4773   unsigned LineNo = getLineNumber(Loc);
4774   unsigned ScopeLine = 0;
4775   llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
4776   if (CGM.getLangOpts().Optimize)
4777     SPFlags |= llvm::DISubprogram::SPFlagOptimized;
4778 
4779   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
4780   llvm::DISubroutineType *STy = getOrCreateFunctionType(D, FnType, Unit);
4781   // Key Instructions: Don't set flag on declarations.
4782   assert(~SPFlags & llvm::DISubprogram::SPFlagDefinition);
4783   llvm::DISubprogram *SP = DBuilder.createFunction(
4784       FDContext, Name, LinkageName, Unit, LineNo, STy, ScopeLine, Flags,
4785       SPFlags, TParamsArray.get(), nullptr, nullptr, Annotations,
4786       /*TargetFunctionName*/ "", /*UseKeyInstructions*/ false);
4787 
4788   // Preserve btf_decl_tag attributes for parameters of extern functions
4789   // for BPF target. The parameters created in this loop are attached as
4790   // DISubprogram's retainedNodes in the DIBuilder::finalize() call.
4791   if (IsDeclForCallSite && CGM.getTarget().getTriple().isBPF()) {
4792     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
4793       llvm::DITypeRefArray ParamTypes = STy->getTypeArray();
4794       unsigned ArgNo = 1;
4795       for (ParmVarDecl *PD : FD->parameters()) {
4796         llvm::DINodeArray ParamAnnotations = CollectBTFDeclTagAnnotations(PD);
4797         DBuilder.createParameterVariable(
4798             SP, PD->getName(), ArgNo, Unit, LineNo, ParamTypes[ArgNo], true,
4799             llvm::DINode::FlagZero, ParamAnnotations);
4800         ++ArgNo;
4801       }
4802     }
4803   }
4804 
4805   if (IsDeclForCallSite)
4806     Fn->setSubprogram(SP);
4807 }
4808 
4809 void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
4810                                           QualType CalleeType,
4811                                           const FunctionDecl *CalleeDecl) {
4812   if (!CallOrInvoke)
4813     return;
4814   auto *Func = CallOrInvoke->getCalledFunction();
4815   if (!Func)
4816     return;
4817   if (Func->getSubprogram())
4818     return;
4819 
4820   // Do not emit a declaration subprogram for a function with nodebug
4821   // attribute, or if call site info isn't required.
4822   if (CalleeDecl->hasAttr<NoDebugAttr>() ||
4823       getCallSiteRelatedAttrs() == llvm::DINode::FlagZero)
4824     return;
4825 
4826   // If there is no DISubprogram attached to the function being called,
4827   // create the one describing the function in order to have complete
4828   // call site debug info.
4829   if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined())
4830     EmitFunctionDecl(CalleeDecl, CalleeDecl->getLocation(), CalleeType, Func);
4831 }
4832 
4833 void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
4834   const auto *FD = cast<FunctionDecl>(GD.getDecl());
4835   // If there is a subprogram for this function available then use it.
4836   auto FI = SPCache.find(FD->getCanonicalDecl());
4837   llvm::DISubprogram *SP = nullptr;
4838   if (FI != SPCache.end())
4839     SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
4840   if (!SP || !SP->isDefinition())
4841     SP = getFunctionStub(GD);
4842   FnBeginRegionCount.push_back(LexicalBlockStack.size());
4843   LexicalBlockStack.emplace_back(SP);
4844   setInlinedAt(Builder.getCurrentDebugLocation());
4845   EmitLocation(Builder, FD->getLocation());
4846 }
4847 
4848 void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
4849   assert(CurInlinedAt && "unbalanced inline scope stack");
4850   EmitFunctionEnd(Builder, nullptr);
4851   setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
4852 }
4853 
4854 void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
4855   // Update our current location
4856   setLocation(Loc);
4857 
4858   if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
4859     return;
4860 
4861   llvm::MDNode *Scope = LexicalBlockStack.back();
4862   Builder.SetCurrentDebugLocation(
4863       llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc),
4864                             getColumnNumber(CurLoc), Scope, CurInlinedAt));
4865 }
4866 
4867 void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
4868   llvm::MDNode *Back = nullptr;
4869   if (!LexicalBlockStack.empty())
4870     Back = LexicalBlockStack.back().get();
4871   // A #line 0 macro carried no line information but still take column
4872   // information. This will finally be rejected by the lexer but is legal.
4873   unsigned Line = getLineNumber(CurLoc);
4874   unsigned Column = Line ? getColumnNumber(CurLoc) : 0;
4875   LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
4876       cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), Line, Column));
4877 }
4878 
4879 void CGDebugInfo::AppendAddressSpaceXDeref(
4880     unsigned AddressSpace, SmallVectorImpl<uint64_t> &Expr) const {
4881   std::optional<unsigned> DWARFAddressSpace =
4882       CGM.getTarget().getDWARFAddressSpace(AddressSpace);
4883   if (!DWARFAddressSpace)
4884     return;
4885 
4886   Expr.push_back(llvm::dwarf::DW_OP_constu);
4887   Expr.push_back(*DWARFAddressSpace);
4888   Expr.push_back(llvm::dwarf::DW_OP_swap);
4889   Expr.push_back(llvm::dwarf::DW_OP_xderef);
4890 }
4891 
4892 void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
4893                                         SourceLocation Loc) {
4894   // Set our current location.
4895   setLocation(Loc);
4896 
4897   // Emit a line table change for the current location inside the new scope.
4898   Builder.SetCurrentDebugLocation(llvm::DILocation::get(
4899       CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc),
4900       LexicalBlockStack.back(), CurInlinedAt));
4901 
4902   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
4903     return;
4904 
4905   // Create a new lexical block and push it on the stack.
4906   CreateLexicalBlock(Loc);
4907 }
4908 
4909 void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
4910                                       SourceLocation Loc) {
4911   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
4912 
4913   // Provide an entry in the line table for the end of the block.
4914   EmitLocation(Builder, Loc);
4915 
4916   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
4917     return;
4918 
4919   LexicalBlockStack.pop_back();
4920 }
4921 
4922 void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
4923   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
4924   unsigned RCount = FnBeginRegionCount.back();
4925   assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
4926 
4927   // Pop all regions for this function.
4928   while (LexicalBlockStack.size() != RCount) {
4929     // Provide an entry in the line table for the end of the block.
4930     EmitLocation(Builder, CurLoc);
4931     LexicalBlockStack.pop_back();
4932   }
4933   FnBeginRegionCount.pop_back();
4934 
4935   if (Fn && Fn->getSubprogram())
4936     DBuilder.finalizeSubprogram(Fn->getSubprogram());
4937 }
4938 
4939 CGDebugInfo::BlockByRefType
4940 CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
4941                                           uint64_t *XOffset) {
4942   SmallVector<llvm::Metadata *, 5> EltTys;
4943   QualType FType;
4944   uint64_t FieldSize, FieldOffset;
4945   uint32_t FieldAlign;
4946 
4947   llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
4948   QualType Type = VD->getType();
4949 
4950   FieldOffset = 0;
4951   FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4952   EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
4953   EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
4954   FType = CGM.getContext().IntTy;
4955   EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
4956   EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
4957 
4958   bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
4959   if (HasCopyAndDispose) {
4960     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4961     EltTys.push_back(
4962         CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
4963     EltTys.push_back(
4964         CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
4965   }
4966   bool HasByrefExtendedLayout;
4967   Qualifiers::ObjCLifetime Lifetime;
4968   if (CGM.getContext().getByrefLifetime(Type, Lifetime,
4969                                         HasByrefExtendedLayout) &&
4970       HasByrefExtendedLayout) {
4971     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4972     EltTys.push_back(
4973         CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
4974   }
4975 
4976   CharUnits Align = CGM.getContext().getDeclAlign(VD);
4977   if (Align > CGM.getContext().toCharUnitsFromBits(
4978                   CGM.getTarget().getPointerAlign(LangAS::Default))) {
4979     CharUnits FieldOffsetInBytes =
4980         CGM.getContext().toCharUnitsFromBits(FieldOffset);
4981     CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
4982     CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
4983 
4984     if (NumPaddingBytes.isPositive()) {
4985       llvm::APInt pad(32, NumPaddingBytes.getQuantity());
4986       FType = CGM.getContext().getConstantArrayType(
4987           CGM.getContext().CharTy, pad, nullptr, ArraySizeModifier::Normal, 0);
4988       EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
4989     }
4990   }
4991 
4992   FType = Type;
4993   llvm::DIType *WrappedTy = getOrCreateType(FType, Unit);
4994   FieldSize = CGM.getContext().getTypeSize(FType);
4995   FieldAlign = CGM.getContext().toBits(Align);
4996 
4997   *XOffset = FieldOffset;
4998   llvm::DIType *FieldTy = DBuilder.createMemberType(
4999       Unit, VD->getName(), Unit, 0, FieldSize, FieldAlign, FieldOffset,
5000       llvm::DINode::FlagZero, WrappedTy);
5001   EltTys.push_back(FieldTy);
5002   FieldOffset += FieldSize;
5003 
5004   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
5005   return {DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0,
5006                                     llvm::DINode::FlagZero, nullptr, Elements),
5007           WrappedTy};
5008 }
5009 
5010 llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
5011                                                 llvm::Value *Storage,
5012                                                 std::optional<unsigned> ArgNo,
5013                                                 CGBuilderTy &Builder,
5014                                                 const bool UsePointerValue) {
5015   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5016   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
5017   if (VD->hasAttr<NoDebugAttr>())
5018     return nullptr;
5019 
5020   const bool VarIsArtificial = IsArtificial(VD);
5021 
5022   llvm::DIFile *Unit = nullptr;
5023   if (!VarIsArtificial)
5024     Unit = getOrCreateFile(VD->getLocation());
5025   llvm::DIType *Ty;
5026   uint64_t XOffset = 0;
5027   if (VD->hasAttr<BlocksAttr>())
5028     Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
5029   else
5030     Ty = getOrCreateType(VD->getType(), Unit);
5031 
5032   // If there is no debug info for this type then do not emit debug info
5033   // for this variable.
5034   if (!Ty)
5035     return nullptr;
5036 
5037   // Get location information.
5038   unsigned Line = 0;
5039   unsigned Column = 0;
5040   if (!VarIsArtificial) {
5041     Line = getLineNumber(VD->getLocation());
5042     Column = getColumnNumber(VD->getLocation());
5043   }
5044   SmallVector<uint64_t, 13> Expr;
5045   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
5046   if (VarIsArtificial)
5047     Flags |= llvm::DINode::FlagArtificial;
5048 
5049   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5050 
5051   unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(VD->getType());
5052   AppendAddressSpaceXDeref(AddressSpace, Expr);
5053 
5054   // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
5055   // object pointer flag.
5056   if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
5057     if (IPD->getParameterKind() == ImplicitParamKind::CXXThis ||
5058         IPD->getParameterKind() == ImplicitParamKind::ObjCSelf)
5059       Flags |= llvm::DINode::FlagObjectPointer;
5060   } else if (const auto *PVD = dyn_cast<ParmVarDecl>(VD)) {
5061     if (PVD->isExplicitObjectParameter())
5062       Flags |= llvm::DINode::FlagObjectPointer;
5063   }
5064 
5065   // Note: Older versions of clang used to emit byval references with an extra
5066   // DW_OP_deref, because they referenced the IR arg directly instead of
5067   // referencing an alloca. Newer versions of LLVM don't treat allocas
5068   // differently from other function arguments when used in a dbg.declare.
5069   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
5070   StringRef Name = VD->getName();
5071   if (!Name.empty()) {
5072     // __block vars are stored on the heap if they are captured by a block that
5073     // can escape the local scope.
5074     if (VD->isEscapingByref()) {
5075       // Here, we need an offset *into* the alloca.
5076       CharUnits offset = CharUnits::fromQuantity(32);
5077       Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5078       // offset of __forwarding field
5079       offset = CGM.getContext().toCharUnitsFromBits(
5080           CGM.getTarget().getPointerWidth(LangAS::Default));
5081       Expr.push_back(offset.getQuantity());
5082       Expr.push_back(llvm::dwarf::DW_OP_deref);
5083       Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5084       // offset of x field
5085       offset = CGM.getContext().toCharUnitsFromBits(XOffset);
5086       Expr.push_back(offset.getQuantity());
5087     }
5088   } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
5089     // If VD is an anonymous union then Storage represents value for
5090     // all union fields.
5091     const RecordDecl *RD = RT->getDecl();
5092     if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
5093       // GDB has trouble finding local variables in anonymous unions, so we emit
5094       // artificial local variables for each of the members.
5095       //
5096       // FIXME: Remove this code as soon as GDB supports this.
5097       // The debug info verifier in LLVM operates based on the assumption that a
5098       // variable has the same size as its storage and we had to disable the
5099       // check for artificial variables.
5100       for (const auto *Field : RD->fields()) {
5101         llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
5102         StringRef FieldName = Field->getName();
5103 
5104         // Ignore unnamed fields. Do not ignore unnamed records.
5105         if (FieldName.empty() && !isa<RecordType>(Field->getType()))
5106           continue;
5107 
5108         // Use VarDecl's Tag, Scope and Line number.
5109         auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
5110         auto *D = DBuilder.createAutoVariable(
5111             Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
5112             Flags | llvm::DINode::FlagArtificial, FieldAlign);
5113 
5114         // Insert an llvm.dbg.declare into the current block.
5115         DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5116                                llvm::DILocation::get(CGM.getLLVMContext(), Line,
5117                                                      Column, Scope,
5118                                                      CurInlinedAt),
5119                                Builder.GetInsertBlock());
5120       }
5121     }
5122   }
5123 
5124   // Clang stores the sret pointer provided by the caller in a static alloca.
5125   // Use DW_OP_deref to tell the debugger to load the pointer and treat it as
5126   // the address of the variable.
5127   if (UsePointerValue) {
5128     assert(!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) &&
5129            "Debug info already contains DW_OP_deref.");
5130     Expr.push_back(llvm::dwarf::DW_OP_deref);
5131   }
5132 
5133   // Create the descriptor for the variable.
5134   llvm::DILocalVariable *D = nullptr;
5135   if (ArgNo) {
5136     llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(VD);
5137     D = DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line, Ty,
5138                                          CGM.getLangOpts().Optimize, Flags,
5139                                          Annotations);
5140   } else {
5141     // For normal local variable, we will try to find out whether 'VD' is the
5142     // copy parameter of coroutine.
5143     // If yes, we are going to use DIVariable of the origin parameter instead
5144     // of creating the new one.
5145     // If no, it might be a normal alloc, we just create a new one for it.
5146 
5147     // Check whether the VD is move parameters.
5148     auto RemapCoroArgToLocalVar = [&]() -> llvm::DILocalVariable * {
5149       // The scope of parameter and move-parameter should be distinct
5150       // DISubprogram.
5151       if (!isa<llvm::DISubprogram>(Scope) || !Scope->isDistinct())
5152         return nullptr;
5153 
5154       auto Iter = llvm::find_if(CoroutineParameterMappings, [&](auto &Pair) {
5155         Stmt *StmtPtr = const_cast<Stmt *>(Pair.second);
5156         if (DeclStmt *DeclStmtPtr = dyn_cast<DeclStmt>(StmtPtr)) {
5157           DeclGroupRef DeclGroup = DeclStmtPtr->getDeclGroup();
5158           Decl *Decl = DeclGroup.getSingleDecl();
5159           if (VD == dyn_cast_or_null<VarDecl>(Decl))
5160             return true;
5161         }
5162         return false;
5163       });
5164 
5165       if (Iter != CoroutineParameterMappings.end()) {
5166         ParmVarDecl *PD = const_cast<ParmVarDecl *>(Iter->first);
5167         auto Iter2 = llvm::find_if(ParamDbgMappings, [&](auto &DbgPair) {
5168           return DbgPair.first == PD && DbgPair.second->getScope() == Scope;
5169         });
5170         if (Iter2 != ParamDbgMappings.end())
5171           return const_cast<llvm::DILocalVariable *>(Iter2->second);
5172       }
5173       return nullptr;
5174     };
5175 
5176     // If we couldn't find a move param DIVariable, create a new one.
5177     D = RemapCoroArgToLocalVar();
5178     // Or we will create a new DIVariable for this Decl if D dose not exists.
5179     if (!D)
5180       D = DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
5181                                       CGM.getLangOpts().Optimize, Flags, Align);
5182   }
5183   // Insert an llvm.dbg.declare into the current block.
5184   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5185                          llvm::DILocation::get(CGM.getLLVMContext(), Line,
5186                                                Column, Scope, CurInlinedAt),
5187                          Builder.GetInsertBlock());
5188 
5189   return D;
5190 }
5191 
5192 llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
5193                                                 llvm::Value *Storage,
5194                                                 std::optional<unsigned> ArgNo,
5195                                                 CGBuilderTy &Builder,
5196                                                 const bool UsePointerValue) {
5197   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5198   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
5199   if (BD->hasAttr<NoDebugAttr>())
5200     return nullptr;
5201 
5202   // Skip the tuple like case, we don't handle that here
5203   if (isa<DeclRefExpr>(BD->getBinding()))
5204     return nullptr;
5205 
5206   llvm::DIFile *Unit = getOrCreateFile(BD->getLocation());
5207   llvm::DIType *Ty = getOrCreateType(BD->getType(), Unit);
5208 
5209   // If there is no debug info for this type then do not emit debug info
5210   // for this variable.
5211   if (!Ty)
5212     return nullptr;
5213 
5214   auto Align = getDeclAlignIfRequired(BD, CGM.getContext());
5215   unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(BD->getType());
5216 
5217   SmallVector<uint64_t, 3> Expr;
5218   AppendAddressSpaceXDeref(AddressSpace, Expr);
5219 
5220   // Clang stores the sret pointer provided by the caller in a static alloca.
5221   // Use DW_OP_deref to tell the debugger to load the pointer and treat it as
5222   // the address of the variable.
5223   if (UsePointerValue) {
5224     assert(!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) &&
5225            "Debug info already contains DW_OP_deref.");
5226     Expr.push_back(llvm::dwarf::DW_OP_deref);
5227   }
5228 
5229   unsigned Line = getLineNumber(BD->getLocation());
5230   unsigned Column = getColumnNumber(BD->getLocation());
5231   StringRef Name = BD->getName();
5232   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
5233   // Create the descriptor for the variable.
5234   llvm::DILocalVariable *D = DBuilder.createAutoVariable(
5235       Scope, Name, Unit, Line, Ty, CGM.getLangOpts().Optimize,
5236       llvm::DINode::FlagZero, Align);
5237 
5238   if (const MemberExpr *ME = dyn_cast<MemberExpr>(BD->getBinding())) {
5239     if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
5240       const unsigned fieldIndex = FD->getFieldIndex();
5241       const clang::CXXRecordDecl *parent =
5242           (const CXXRecordDecl *)FD->getParent();
5243       const ASTRecordLayout &layout =
5244           CGM.getContext().getASTRecordLayout(parent);
5245       const uint64_t fieldOffset = layout.getFieldOffset(fieldIndex);
5246       if (FD->isBitField()) {
5247         const CGRecordLayout &RL =
5248             CGM.getTypes().getCGRecordLayout(FD->getParent());
5249         const CGBitFieldInfo &Info = RL.getBitFieldInfo(FD);
5250         // Use DW_OP_plus_uconst to adjust to the start of the bitfield
5251         // storage.
5252         if (!Info.StorageOffset.isZero()) {
5253           Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5254           Expr.push_back(Info.StorageOffset.getQuantity());
5255         }
5256         // Use LLVM_extract_bits to extract the appropriate bits from this
5257         // bitfield.
5258         Expr.push_back(Info.IsSigned
5259                            ? llvm::dwarf::DW_OP_LLVM_extract_bits_sext
5260                            : llvm::dwarf::DW_OP_LLVM_extract_bits_zext);
5261         Expr.push_back(Info.Offset);
5262         // If we have an oversized bitfield then the value won't be more than
5263         // the size of the type.
5264         const uint64_t TypeSize = CGM.getContext().getTypeSize(BD->getType());
5265         Expr.push_back(std::min((uint64_t)Info.Size, TypeSize));
5266       } else if (fieldOffset != 0) {
5267         assert(fieldOffset % CGM.getContext().getCharWidth() == 0 &&
5268                "Unexpected non-bitfield with non-byte-aligned offset");
5269         Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5270         Expr.push_back(
5271             CGM.getContext().toCharUnitsFromBits(fieldOffset).getQuantity());
5272       }
5273     }
5274   } else if (const ArraySubscriptExpr *ASE =
5275                  dyn_cast<ArraySubscriptExpr>(BD->getBinding())) {
5276     if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ASE->getIdx())) {
5277       const uint64_t value = IL->getValue().getZExtValue();
5278       const uint64_t typeSize = CGM.getContext().getTypeSize(BD->getType());
5279 
5280       if (value != 0) {
5281         Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5282         Expr.push_back(CGM.getContext()
5283                            .toCharUnitsFromBits(value * typeSize)
5284                            .getQuantity());
5285       }
5286     }
5287   }
5288 
5289   // Insert an llvm.dbg.declare into the current block.
5290   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5291                          llvm::DILocation::get(CGM.getLLVMContext(), Line,
5292                                                Column, Scope, CurInlinedAt),
5293                          Builder.GetInsertBlock());
5294 
5295   return D;
5296 }
5297 
5298 llvm::DILocalVariable *
5299 CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage,
5300                                        CGBuilderTy &Builder,
5301                                        const bool UsePointerValue) {
5302   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5303 
5304   if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
5305     for (BindingDecl *B : DD->flat_bindings())
5306       EmitDeclare(B, Storage, std::nullopt, Builder,
5307                   VD->getType()->isReferenceType());
5308     // Don't emit an llvm.dbg.declare for the composite storage as it doesn't
5309     // correspond to a user variable.
5310     return nullptr;
5311   }
5312 
5313   return EmitDeclare(VD, Storage, std::nullopt, Builder, UsePointerValue);
5314 }
5315 
5316 void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
5317   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5318   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
5319 
5320   if (D->hasAttr<NoDebugAttr>())
5321     return;
5322 
5323   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
5324   llvm::DIFile *Unit = getOrCreateFile(D->getLocation());
5325 
5326   // Get location information.
5327   unsigned Line = getLineNumber(D->getLocation());
5328   unsigned Column = getColumnNumber(D->getLocation());
5329 
5330   StringRef Name = D->getName();
5331 
5332   // Create the descriptor for the label.
5333   auto *L = DBuilder.createLabel(
5334       Scope, Name, Unit, Line, Column, /*IsArtificial=*/false,
5335       /*CoroSuspendIdx=*/std::nullopt, CGM.getLangOpts().Optimize);
5336 
5337   // Insert an llvm.dbg.label into the current block.
5338   DBuilder.insertLabel(L,
5339                        llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
5340                                              Scope, CurInlinedAt),
5341                        Builder.GetInsertBlock()->end());
5342 }
5343 
5344 llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
5345                                           llvm::DIType *Ty) {
5346   llvm::DIType *CachedTy = getTypeOrNull(QualTy);
5347   if (CachedTy)
5348     Ty = CachedTy;
5349   return DBuilder.createObjectPointerType(Ty, /*Implicit=*/true);
5350 }
5351 
5352 void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
5353     const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
5354     const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
5355   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5356   assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
5357 
5358   if (Builder.GetInsertBlock() == nullptr)
5359     return;
5360   if (VD->hasAttr<NoDebugAttr>())
5361     return;
5362 
5363   bool isByRef = VD->hasAttr<BlocksAttr>();
5364 
5365   uint64_t XOffset = 0;
5366   llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
5367   llvm::DIType *Ty;
5368   if (isByRef)
5369     Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
5370   else
5371     Ty = getOrCreateType(VD->getType(), Unit);
5372 
5373   // Self is passed along as an implicit non-arg variable in a
5374   // block. Mark it as the object pointer.
5375   if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
5376     if (IPD->getParameterKind() == ImplicitParamKind::ObjCSelf)
5377       Ty = CreateSelfType(VD->getType(), Ty);
5378 
5379   // Get location information.
5380   const unsigned Line =
5381       getLineNumber(VD->getLocation().isValid() ? VD->getLocation() : CurLoc);
5382   unsigned Column = getColumnNumber(VD->getLocation());
5383 
5384   const llvm::DataLayout &target = CGM.getDataLayout();
5385 
5386   CharUnits offset = CharUnits::fromQuantity(
5387       target.getStructLayout(blockInfo.StructureType)
5388           ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
5389 
5390   SmallVector<uint64_t, 9> addr;
5391   addr.push_back(llvm::dwarf::DW_OP_deref);
5392   addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5393   addr.push_back(offset.getQuantity());
5394   if (isByRef) {
5395     addr.push_back(llvm::dwarf::DW_OP_deref);
5396     addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5397     // offset of __forwarding field
5398     offset =
5399         CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
5400     addr.push_back(offset.getQuantity());
5401     addr.push_back(llvm::dwarf::DW_OP_deref);
5402     addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5403     // offset of x field
5404     offset = CGM.getContext().toCharUnitsFromBits(XOffset);
5405     addr.push_back(offset.getQuantity());
5406   }
5407 
5408   // Create the descriptor for the variable.
5409   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5410   auto *D = DBuilder.createAutoVariable(
5411       cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
5412       Line, Ty, false, llvm::DINode::FlagZero, Align);
5413 
5414   // Insert an llvm.dbg.declare into the current block.
5415   auto DL = llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
5416                                   LexicalBlockStack.back(), CurInlinedAt);
5417   auto *Expr = DBuilder.createExpression(addr);
5418   if (InsertPoint)
5419     DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
5420   else
5421     DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
5422 }
5423 
5424 llvm::DILocalVariable *
5425 CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
5426                                       unsigned ArgNo, CGBuilderTy &Builder,
5427                                       bool UsePointerValue) {
5428   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5429   return EmitDeclare(VD, AI, ArgNo, Builder, UsePointerValue);
5430 }
5431 
5432 namespace {
5433 struct BlockLayoutChunk {
5434   uint64_t OffsetInBits;
5435   const BlockDecl::Capture *Capture;
5436 };
5437 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
5438   return l.OffsetInBits < r.OffsetInBits;
5439 }
5440 } // namespace
5441 
5442 void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
5443     const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
5444     const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
5445     SmallVectorImpl<llvm::Metadata *> &Fields) {
5446   // Blocks in OpenCL have unique constraints which make the standard fields
5447   // redundant while requiring size and align fields for enqueue_kernel. See
5448   // initializeForBlockHeader in CGBlocks.cpp
5449   if (CGM.getLangOpts().OpenCL) {
5450     Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public,
5451                                      BlockLayout.getElementOffsetInBits(0),
5452                                      Unit, Unit));
5453     Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
5454                                      BlockLayout.getElementOffsetInBits(1),
5455                                      Unit, Unit));
5456   } else {
5457     Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
5458                                      BlockLayout.getElementOffsetInBits(0),
5459                                      Unit, Unit));
5460     Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
5461                                      BlockLayout.getElementOffsetInBits(1),
5462                                      Unit, Unit));
5463     Fields.push_back(
5464         createFieldType("__reserved", Context.IntTy, Loc, AS_public,
5465                         BlockLayout.getElementOffsetInBits(2), Unit, Unit));
5466     auto *FnTy = Block.getBlockExpr()->getFunctionType();
5467     auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
5468     Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
5469                                      BlockLayout.getElementOffsetInBits(3),
5470                                      Unit, Unit));
5471     Fields.push_back(createFieldType(
5472         "__descriptor",
5473         Context.getPointerType(Block.NeedsCopyDispose
5474                                    ? Context.getBlockDescriptorExtendedType()
5475                                    : Context.getBlockDescriptorType()),
5476         Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
5477   }
5478 }
5479 
5480 void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
5481                                                        StringRef Name,
5482                                                        unsigned ArgNo,
5483                                                        llvm::AllocaInst *Alloca,
5484                                                        CGBuilderTy &Builder) {
5485   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5486   ASTContext &C = CGM.getContext();
5487   const BlockDecl *blockDecl = block.getBlockDecl();
5488 
5489   // Collect some general information about the block's location.
5490   SourceLocation loc = blockDecl->getCaretLocation();
5491   llvm::DIFile *tunit = getOrCreateFile(loc);
5492   unsigned line = getLineNumber(loc);
5493   unsigned column = getColumnNumber(loc);
5494 
5495   // Build the debug-info type for the block literal.
5496   getDeclContextDescriptor(blockDecl);
5497 
5498   const llvm::StructLayout *blockLayout =
5499       CGM.getDataLayout().getStructLayout(block.StructureType);
5500 
5501   SmallVector<llvm::Metadata *, 16> fields;
5502   collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
5503                                              fields);
5504 
5505   // We want to sort the captures by offset, not because DWARF
5506   // requires this, but because we're paranoid about debuggers.
5507   SmallVector<BlockLayoutChunk, 8> chunks;
5508 
5509   // 'this' capture.
5510   if (blockDecl->capturesCXXThis()) {
5511     BlockLayoutChunk chunk;
5512     chunk.OffsetInBits =
5513         blockLayout->getElementOffsetInBits(block.CXXThisIndex);
5514     chunk.Capture = nullptr;
5515     chunks.push_back(chunk);
5516   }
5517 
5518   // Variable captures.
5519   for (const auto &capture : blockDecl->captures()) {
5520     const VarDecl *variable = capture.getVariable();
5521     const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
5522 
5523     // Ignore constant captures.
5524     if (captureInfo.isConstant())
5525       continue;
5526 
5527     BlockLayoutChunk chunk;
5528     chunk.OffsetInBits =
5529         blockLayout->getElementOffsetInBits(captureInfo.getIndex());
5530     chunk.Capture = &capture;
5531     chunks.push_back(chunk);
5532   }
5533 
5534   // Sort by offset.
5535   llvm::array_pod_sort(chunks.begin(), chunks.end());
5536 
5537   for (const BlockLayoutChunk &Chunk : chunks) {
5538     uint64_t offsetInBits = Chunk.OffsetInBits;
5539     const BlockDecl::Capture *capture = Chunk.Capture;
5540 
5541     // If we have a null capture, this must be the C++ 'this' capture.
5542     if (!capture) {
5543       QualType type;
5544       if (auto *Method =
5545               cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
5546         type = Method->getThisType();
5547       else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
5548         type = QualType(RDecl->getTypeForDecl(), 0);
5549       else
5550         llvm_unreachable("unexpected block declcontext");
5551 
5552       fields.push_back(createFieldType("this", type, loc, AS_public,
5553                                        offsetInBits, tunit, tunit));
5554       continue;
5555     }
5556 
5557     const VarDecl *variable = capture->getVariable();
5558     StringRef name = variable->getName();
5559 
5560     llvm::DIType *fieldType;
5561     if (capture->isByRef()) {
5562       TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
5563       auto Align = PtrInfo.isAlignRequired() ? PtrInfo.Align : 0;
5564       // FIXME: This recomputes the layout of the BlockByRefWrapper.
5565       uint64_t xoffset;
5566       fieldType =
5567           EmitTypeForVarWithBlocksAttr(variable, &xoffset).BlockByRefWrapper;
5568       fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
5569       fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
5570                                             PtrInfo.Width, Align, offsetInBits,
5571                                             llvm::DINode::FlagZero, fieldType);
5572     } else {
5573       auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
5574       fieldType = createFieldType(name, variable->getType(), loc, AS_public,
5575                                   offsetInBits, Align, tunit, tunit);
5576     }
5577     fields.push_back(fieldType);
5578   }
5579 
5580   SmallString<36> typeName;
5581   llvm::raw_svector_ostream(typeName)
5582       << "__block_literal_" << CGM.getUniqueBlockCount();
5583 
5584   llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
5585 
5586   llvm::DIType *type =
5587       DBuilder.createStructType(tunit, typeName.str(), tunit, line,
5588                                 CGM.getContext().toBits(block.BlockSize), 0,
5589                                 llvm::DINode::FlagZero, nullptr, fieldsArray);
5590   type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
5591 
5592   // Get overall information about the block.
5593   llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
5594   auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
5595 
5596   // Create the descriptor for the parameter.
5597   auto *debugVar = DBuilder.createParameterVariable(
5598       scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags);
5599 
5600   // Insert an llvm.dbg.declare into the current block.
5601   DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
5602                          llvm::DILocation::get(CGM.getLLVMContext(), line,
5603                                                column, scope, CurInlinedAt),
5604                          Builder.GetInsertBlock());
5605 }
5606 
5607 llvm::DIDerivedType *
5608 CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
5609   if (!D || !D->isStaticDataMember())
5610     return nullptr;
5611 
5612   auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
5613   if (MI != StaticDataMemberCache.end()) {
5614     assert(MI->second && "Static data member declaration should still exist");
5615     return MI->second;
5616   }
5617 
5618   // If the member wasn't found in the cache, lazily construct and add it to the
5619   // type (used when a limited form of the type is emitted).
5620   auto DC = D->getDeclContext();
5621   auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
5622   return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
5623 }
5624 
5625 llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
5626     const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
5627     StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
5628   llvm::DIGlobalVariableExpression *GVE = nullptr;
5629 
5630   for (const auto *Field : RD->fields()) {
5631     llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
5632     StringRef FieldName = Field->getName();
5633 
5634     // Ignore unnamed fields, but recurse into anonymous records.
5635     if (FieldName.empty()) {
5636       if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
5637         GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
5638                                      Var, DContext);
5639       continue;
5640     }
5641     // Use VarDecl's Tag, Scope and Line number.
5642     GVE = DBuilder.createGlobalVariableExpression(
5643         DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
5644         Var->hasLocalLinkage());
5645     Var->addDebugInfo(GVE);
5646   }
5647   return GVE;
5648 }
5649 
5650 static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args);
5651 static bool ReferencesAnonymousEntity(RecordType *RT) {
5652   // Unnamed classes/lambdas can't be reconstituted due to a lack of column
5653   // info we produce in the DWARF, so we can't get Clang's full name back.
5654   // But so long as it's not one of those, it doesn't matter if some sub-type
5655   // of the record (a template parameter) can't be reconstituted - because the
5656   // un-reconstitutable type itself will carry its own name.
5657   const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
5658   if (!RD)
5659     return false;
5660   if (!RD->getIdentifier())
5661     return true;
5662   auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD);
5663   if (!TSpecial)
5664     return false;
5665   return ReferencesAnonymousEntity(TSpecial->getTemplateArgs().asArray());
5666 }
5667 static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args) {
5668   return llvm::any_of(Args, [&](const TemplateArgument &TA) {
5669     switch (TA.getKind()) {
5670     case TemplateArgument::Pack:
5671       return ReferencesAnonymousEntity(TA.getPackAsArray());
5672     case TemplateArgument::Type: {
5673       struct ReferencesAnonymous
5674           : public RecursiveASTVisitor<ReferencesAnonymous> {
5675         bool RefAnon = false;
5676         bool VisitRecordType(RecordType *RT) {
5677           if (ReferencesAnonymousEntity(RT)) {
5678             RefAnon = true;
5679             return false;
5680           }
5681           return true;
5682         }
5683       };
5684       ReferencesAnonymous RT;
5685       RT.TraverseType(TA.getAsType());
5686       if (RT.RefAnon)
5687         return true;
5688       break;
5689     }
5690     default:
5691       break;
5692     }
5693     return false;
5694   });
5695 }
5696 namespace {
5697 struct ReconstitutableType : public RecursiveASTVisitor<ReconstitutableType> {
5698   bool Reconstitutable = true;
5699   bool VisitVectorType(VectorType *FT) {
5700     Reconstitutable = false;
5701     return false;
5702   }
5703   bool VisitAtomicType(AtomicType *FT) {
5704     Reconstitutable = false;
5705     return false;
5706   }
5707   bool VisitType(Type *T) {
5708     // _BitInt(N) isn't reconstitutable because the bit width isn't encoded in
5709     // the DWARF, only the byte width.
5710     if (T->isBitIntType()) {
5711       Reconstitutable = false;
5712       return false;
5713     }
5714     return true;
5715   }
5716   bool TraverseEnumType(EnumType *ET) {
5717     // Unnamed enums can't be reconstituted due to a lack of column info we
5718     // produce in the DWARF, so we can't get Clang's full name back.
5719     if (const auto *ED = dyn_cast<EnumDecl>(ET->getDecl())) {
5720       if (!ED->getIdentifier()) {
5721         Reconstitutable = false;
5722         return false;
5723       }
5724       if (!ED->isExternallyVisible()) {
5725         Reconstitutable = false;
5726         return false;
5727       }
5728     }
5729     return true;
5730   }
5731   bool VisitFunctionProtoType(FunctionProtoType *FT) {
5732     // noexcept is not encoded in DWARF, so the reversi
5733     Reconstitutable &= !isNoexceptExceptionSpec(FT->getExceptionSpecType());
5734     Reconstitutable &= !FT->getNoReturnAttr();
5735     return Reconstitutable;
5736   }
5737   bool VisitRecordType(RecordType *RT) {
5738     if (ReferencesAnonymousEntity(RT)) {
5739       Reconstitutable = false;
5740       return false;
5741     }
5742     return true;
5743   }
5744 };
5745 } // anonymous namespace
5746 
5747 // Test whether a type name could be rebuilt from emitted debug info.
5748 static bool IsReconstitutableType(QualType QT) {
5749   ReconstitutableType T;
5750   T.TraverseType(QT);
5751   return T.Reconstitutable;
5752 }
5753 
5754 bool CGDebugInfo::HasReconstitutableArgs(
5755     ArrayRef<TemplateArgument> Args) const {
5756   return llvm::all_of(Args, [&](const TemplateArgument &TA) {
5757     switch (TA.getKind()) {
5758     case TemplateArgument::Template:
5759       // Easy to reconstitute - the value of the parameter in the debug
5760       // info is the string name of the template. The template name
5761       // itself won't benefit from any name rebuilding, but that's a
5762       // representational limitation - maybe DWARF could be
5763       // changed/improved to use some more structural representation.
5764       return true;
5765     case TemplateArgument::Declaration:
5766       // Reference and pointer non-type template parameters point to
5767       // variables, functions, etc and their value is, at best (for
5768       // variables) represented as an address - not a reference to the
5769       // DWARF describing the variable/function/etc. This makes it hard,
5770       // possibly impossible to rebuild the original name - looking up
5771       // the address in the executable file's symbol table would be
5772       // needed.
5773       return false;
5774     case TemplateArgument::NullPtr:
5775       // These could be rebuilt, but figured they're close enough to the
5776       // declaration case, and not worth rebuilding.
5777       return false;
5778     case TemplateArgument::Pack:
5779       // A pack is invalid if any of the elements of the pack are
5780       // invalid.
5781       return HasReconstitutableArgs(TA.getPackAsArray());
5782     case TemplateArgument::Integral:
5783       // Larger integers get encoded as DWARF blocks which are a bit
5784       // harder to parse back into a large integer, etc - so punting on
5785       // this for now. Re-parsing the integers back into APInt is
5786       // probably feasible some day.
5787       return TA.getAsIntegral().getBitWidth() <= 64 &&
5788              IsReconstitutableType(TA.getIntegralType());
5789     case TemplateArgument::StructuralValue:
5790       return false;
5791     case TemplateArgument::Type:
5792       return IsReconstitutableType(TA.getAsType());
5793     case TemplateArgument::Expression:
5794       return IsReconstitutableType(TA.getAsExpr()->getType());
5795     default:
5796       llvm_unreachable("Other, unresolved, template arguments should "
5797                        "not be seen here");
5798     }
5799   });
5800 }
5801 
5802 std::string CGDebugInfo::GetName(const Decl *D, bool Qualified) const {
5803   std::string Name;
5804   llvm::raw_string_ostream OS(Name);
5805   const NamedDecl *ND = dyn_cast<NamedDecl>(D);
5806   if (!ND)
5807     return Name;
5808   llvm::codegenoptions::DebugTemplateNamesKind TemplateNamesKind =
5809       CGM.getCodeGenOpts().getDebugSimpleTemplateNames();
5810 
5811   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
5812     TemplateNamesKind = llvm::codegenoptions::DebugTemplateNamesKind::Full;
5813 
5814   std::optional<TemplateArgs> Args;
5815 
5816   bool IsOperatorOverload = false; // isa<CXXConversionDecl>(ND);
5817   if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
5818     Args = GetTemplateArgs(RD);
5819   } else if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
5820     Args = GetTemplateArgs(FD);
5821     auto NameKind = ND->getDeclName().getNameKind();
5822     IsOperatorOverload |=
5823         NameKind == DeclarationName::CXXOperatorName ||
5824         NameKind == DeclarationName::CXXConversionFunctionName;
5825   } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
5826     Args = GetTemplateArgs(VD);
5827   }
5828 
5829   // A conversion operator presents complications/ambiguity if there's a
5830   // conversion to class template that is itself a template, eg:
5831   // template<typename T>
5832   // operator ns::t1<T, int>();
5833   // This should be named, eg: "operator ns::t1<float, int><float>"
5834   // (ignoring clang bug that means this is currently "operator t1<float>")
5835   // but if the arguments were stripped, the consumer couldn't differentiate
5836   // whether the template argument list for the conversion type was the
5837   // function's argument list (& no reconstitution was needed) or not.
5838   // This could be handled if reconstitutable names had a separate attribute
5839   // annotating them as such - this would remove the ambiguity.
5840   //
5841   // Alternatively the template argument list could be parsed enough to check
5842   // whether there's one list or two, then compare that with the DWARF
5843   // description of the return type and the template argument lists to determine
5844   // how many lists there should be and if one is missing it could be assumed(?)
5845   // to be the function's template argument list  & then be rebuilt.
5846   //
5847   // Other operator overloads that aren't conversion operators could be
5848   // reconstituted but would require a bit more nuance about detecting the
5849   // difference between these different operators during that rebuilding.
5850   bool Reconstitutable =
5851       Args && HasReconstitutableArgs(Args->Args) && !IsOperatorOverload;
5852 
5853   PrintingPolicy PP = getPrintingPolicy();
5854 
5855   if (TemplateNamesKind == llvm::codegenoptions::DebugTemplateNamesKind::Full ||
5856       !Reconstitutable) {
5857     ND->getNameForDiagnostic(OS, PP, Qualified);
5858   } else {
5859     bool Mangled = TemplateNamesKind ==
5860                    llvm::codegenoptions::DebugTemplateNamesKind::Mangled;
5861     // check if it's a template
5862     if (Mangled)
5863       OS << "_STN|";
5864 
5865     OS << ND->getDeclName();
5866     std::string EncodedOriginalName;
5867     llvm::raw_string_ostream EncodedOriginalNameOS(EncodedOriginalName);
5868     EncodedOriginalNameOS << ND->getDeclName();
5869 
5870     if (Mangled) {
5871       OS << "|";
5872       printTemplateArgumentList(OS, Args->Args, PP);
5873       printTemplateArgumentList(EncodedOriginalNameOS, Args->Args, PP);
5874 #ifndef NDEBUG
5875       std::string CanonicalOriginalName;
5876       llvm::raw_string_ostream OriginalOS(CanonicalOriginalName);
5877       ND->getNameForDiagnostic(OriginalOS, PP, Qualified);
5878       assert(EncodedOriginalName == CanonicalOriginalName);
5879 #endif
5880     }
5881   }
5882   return Name;
5883 }
5884 
5885 void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
5886                                      const VarDecl *D) {
5887   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5888   if (D->hasAttr<NoDebugAttr>())
5889     return;
5890 
5891   llvm::TimeTraceScope TimeScope("DebugGlobalVariable", [&]() {
5892     return GetName(D, true);
5893   });
5894 
5895   // If we already created a DIGlobalVariable for this declaration, just attach
5896   // it to the llvm::GlobalVariable.
5897   auto Cached = DeclCache.find(D->getCanonicalDecl());
5898   if (Cached != DeclCache.end())
5899     return Var->addDebugInfo(
5900         cast<llvm::DIGlobalVariableExpression>(Cached->second));
5901 
5902   // Create global variable debug descriptor.
5903   llvm::DIFile *Unit = nullptr;
5904   llvm::DIScope *DContext = nullptr;
5905   unsigned LineNo;
5906   StringRef DeclName, LinkageName;
5907   QualType T;
5908   llvm::MDTuple *TemplateParameters = nullptr;
5909   collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName,
5910                       TemplateParameters, DContext);
5911 
5912   // Attempt to store one global variable for the declaration - even if we
5913   // emit a lot of fields.
5914   llvm::DIGlobalVariableExpression *GVE = nullptr;
5915 
5916   // If this is an anonymous union then we'll want to emit a global
5917   // variable for each member of the anonymous union so that it's possible
5918   // to find the name of any field in the union.
5919   if (T->isUnionType() && DeclName.empty()) {
5920     const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
5921     assert(RD->isAnonymousStructOrUnion() &&
5922            "unnamed non-anonymous struct or union?");
5923     GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
5924   } else {
5925     auto Align = getDeclAlignIfRequired(D, CGM.getContext());
5926 
5927     SmallVector<uint64_t, 4> Expr;
5928     unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(D->getType());
5929     if (CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) {
5930       if (D->hasAttr<CUDASharedAttr>())
5931         AddressSpace =
5932             CGM.getContext().getTargetAddressSpace(LangAS::cuda_shared);
5933       else if (D->hasAttr<CUDAConstantAttr>())
5934         AddressSpace =
5935             CGM.getContext().getTargetAddressSpace(LangAS::cuda_constant);
5936     }
5937     AppendAddressSpaceXDeref(AddressSpace, Expr);
5938 
5939     llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
5940     GVE = DBuilder.createGlobalVariableExpression(
5941         DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
5942         Var->hasLocalLinkage(), true,
5943         Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
5944         getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
5945         Align, Annotations);
5946     Var->addDebugInfo(GVE);
5947   }
5948   DeclCache[D->getCanonicalDecl()].reset(GVE);
5949 }
5950 
5951 void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
5952   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5953   if (VD->hasAttr<NoDebugAttr>())
5954     return;
5955   llvm::TimeTraceScope TimeScope("DebugConstGlobalVariable", [&]() {
5956     return GetName(VD, true);
5957   });
5958 
5959   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5960   // Create the descriptor for the variable.
5961   llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
5962   StringRef Name = VD->getName();
5963   llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
5964 
5965   if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
5966     const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
5967     assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
5968 
5969     if (CGM.getCodeGenOpts().EmitCodeView) {
5970       // If CodeView, emit enums as global variables, unless they are defined
5971       // inside a class. We do this because MSVC doesn't emit S_CONSTANTs for
5972       // enums in classes, and because it is difficult to attach this scope
5973       // information to the global variable.
5974       if (isa<RecordDecl>(ED->getDeclContext()))
5975         return;
5976     } else {
5977       // If not CodeView, emit DW_TAG_enumeration_type if necessary. For
5978       // example: for "enum { ZERO };", a DW_TAG_enumeration_type is created the
5979       // first time `ZERO` is referenced in a function.
5980       llvm::DIType *EDTy =
5981           getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
5982       assert (EDTy->getTag() == llvm::dwarf::DW_TAG_enumeration_type);
5983       (void)EDTy;
5984       return;
5985     }
5986   }
5987 
5988   // Do not emit separate definitions for function local consts.
5989   if (isa<FunctionDecl>(VD->getDeclContext()))
5990     return;
5991 
5992   VD = cast<ValueDecl>(VD->getCanonicalDecl());
5993   auto *VarD = dyn_cast<VarDecl>(VD);
5994   if (VarD && VarD->isStaticDataMember()) {
5995     auto *RD = cast<RecordDecl>(VarD->getDeclContext());
5996     getDeclContextDescriptor(VarD);
5997     // Ensure that the type is retained even though it's otherwise unreferenced.
5998     //
5999     // FIXME: This is probably unnecessary, since Ty should reference RD
6000     // through its scope.
6001     RetainedTypes.push_back(
6002         CGM.getContext().getRecordType(RD).getAsOpaquePtr());
6003 
6004     return;
6005   }
6006   llvm::DIScope *DContext = getDeclContextDescriptor(VD);
6007 
6008   auto &GV = DeclCache[VD];
6009   if (GV)
6010     return;
6011 
6012   llvm::DIExpression *InitExpr = createConstantValueExpression(VD, Init);
6013   llvm::MDTuple *TemplateParameters = nullptr;
6014 
6015   if (isa<VarTemplateSpecializationDecl>(VD))
6016     if (VarD) {
6017       llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VarD, &*Unit);
6018       TemplateParameters = parameterNodes.get();
6019     }
6020 
6021   GV.reset(DBuilder.createGlobalVariableExpression(
6022       DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
6023       true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
6024       TemplateParameters, Align));
6025 }
6026 
6027 void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
6028                                        const VarDecl *D) {
6029   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
6030   if (D->hasAttr<NoDebugAttr>())
6031     return;
6032 
6033   auto Align = getDeclAlignIfRequired(D, CGM.getContext());
6034   llvm::DIFile *Unit = getOrCreateFile(D->getLocation());
6035   StringRef Name = D->getName();
6036   llvm::DIType *Ty = getOrCreateType(D->getType(), Unit);
6037 
6038   llvm::DIScope *DContext = getDeclContextDescriptor(D);
6039   llvm::DIGlobalVariableExpression *GVE =
6040       DBuilder.createGlobalVariableExpression(
6041           DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()),
6042           Ty, false, false, nullptr, nullptr, nullptr, Align);
6043   Var->addDebugInfo(GVE);
6044 }
6045 
6046 void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
6047                                      llvm::Instruction *Value, QualType Ty) {
6048   // Only when -g2 or above is specified, debug info for variables will be
6049   // generated.
6050   if (CGM.getCodeGenOpts().getDebugInfo() <=
6051       llvm::codegenoptions::DebugLineTablesOnly)
6052     return;
6053 
6054   llvm::DILocation *DIL = Value->getDebugLoc().get();
6055   if (!DIL)
6056     return;
6057 
6058   llvm::DIFile *Unit = DIL->getFile();
6059   llvm::DIType *Type = getOrCreateType(Ty, Unit);
6060 
6061   // Check if Value is already a declared variable and has debug info, in this
6062   // case we have nothing to do. Clang emits a declared variable as alloca, and
6063   // it is loaded upon use, so we identify such pattern here.
6064   if (llvm::LoadInst *Load = dyn_cast<llvm::LoadInst>(Value)) {
6065     llvm::Value *Var = Load->getPointerOperand();
6066     // There can be implicit type cast applied on a variable if it is an opaque
6067     // ptr, in this case its debug info may not match the actual type of object
6068     // being used as in the next instruction, so we will need to emit a pseudo
6069     // variable for type-casted value.
6070     auto DeclareTypeMatches = [&](auto *DbgDeclare) {
6071       return DbgDeclare->getVariable()->getType() == Type;
6072     };
6073     if (any_of(llvm::findDbgDeclares(Var), DeclareTypeMatches) ||
6074         any_of(llvm::findDVRDeclares(Var), DeclareTypeMatches))
6075       return;
6076   }
6077 
6078   llvm::DILocalVariable *D =
6079       DBuilder.createAutoVariable(LexicalBlockStack.back(), "", nullptr, 0,
6080                                   Type, false, llvm::DINode::FlagArtificial);
6081 
6082   if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
6083     DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
6084                                      *InsertPoint);
6085   }
6086 }
6087 
6088 void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
6089                                   const GlobalDecl GD) {
6090 
6091   assert(GV);
6092 
6093   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
6094     return;
6095 
6096   const auto *D = cast<ValueDecl>(GD.getDecl());
6097   if (D->hasAttr<NoDebugAttr>())
6098     return;
6099 
6100   auto AliaseeDecl = CGM.getMangledNameDecl(GV->getName());
6101   llvm::DINode *DI;
6102 
6103   if (!AliaseeDecl)
6104     // FIXME: Aliasee not declared yet - possibly declared later
6105     // For example,
6106     //
6107     //   1 extern int newname __attribute__((alias("oldname")));
6108     //   2 int oldname = 1;
6109     //
6110     // No debug info would be generated for 'newname' in this case.
6111     //
6112     // Fix compiler to generate "newname" as imported_declaration
6113     // pointing to the DIE of "oldname".
6114     return;
6115   if (!(DI = getDeclarationOrDefinition(
6116             AliaseeDecl.getCanonicalDecl().getDecl())))
6117     return;
6118 
6119   llvm::DIScope *DContext = getDeclContextDescriptor(D);
6120   auto Loc = D->getLocation();
6121 
6122   llvm::DIImportedEntity *ImportDI = DBuilder.createImportedDeclaration(
6123       DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName());
6124 
6125   // Record this DIE in the cache for nested declaration reference.
6126   ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI);
6127 }
6128 
6129 void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
6130                                             const StringLiteral *S) {
6131   SourceLocation Loc = S->getStrTokenLoc(0);
6132   PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
6133   if (!PLoc.isValid())
6134     return;
6135 
6136   llvm::DIFile *File = getOrCreateFile(Loc);
6137   llvm::DIGlobalVariableExpression *Debug =
6138       DBuilder.createGlobalVariableExpression(
6139           nullptr, StringRef(), StringRef(), getOrCreateFile(Loc),
6140           getLineNumber(Loc), getOrCreateType(S->getType(), File), true);
6141   GV->addDebugInfo(Debug);
6142 }
6143 
6144 llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
6145   if (!LexicalBlockStack.empty())
6146     return LexicalBlockStack.back();
6147   llvm::DIScope *Mod = getParentModuleOrNull(D);
6148   return getContextDescriptor(D, Mod ? Mod : TheCU);
6149 }
6150 
6151 void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
6152   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
6153     return;
6154   const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
6155   if (!NSDecl->isAnonymousNamespace() ||
6156       CGM.getCodeGenOpts().DebugExplicitImport) {
6157     auto Loc = UD.getLocation();
6158     if (!Loc.isValid())
6159       Loc = CurLoc;
6160     DBuilder.createImportedModule(
6161         getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
6162         getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
6163   }
6164 }
6165 
6166 void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
6167   if (llvm::DINode *Target =
6168           getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
6169     auto Loc = USD.getLocation();
6170     DBuilder.createImportedDeclaration(
6171         getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
6172         getOrCreateFile(Loc), getLineNumber(Loc));
6173   }
6174 }
6175 
6176 void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
6177   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
6178     return;
6179   assert(UD.shadow_size() &&
6180          "We shouldn't be codegening an invalid UsingDecl containing no decls");
6181 
6182   for (const auto *USD : UD.shadows()) {
6183     // FIXME: Skip functions with undeduced auto return type for now since we
6184     // don't currently have the plumbing for separate declarations & definitions
6185     // of free functions and mismatched types (auto in the declaration, concrete
6186     // return type in the definition)
6187     if (const auto *FD = dyn_cast<FunctionDecl>(USD->getUnderlyingDecl()))
6188       if (const auto *AT = FD->getType()
6189                                ->castAs<FunctionProtoType>()
6190                                ->getContainedAutoType())
6191         if (AT->getDeducedType().isNull())
6192           continue;
6193 
6194     EmitUsingShadowDecl(*USD);
6195     // Emitting one decl is sufficient - debuggers can detect that this is an
6196     // overloaded name & provide lookup for all the overloads.
6197     break;
6198   }
6199 }
6200 
6201 void CGDebugInfo::EmitUsingEnumDecl(const UsingEnumDecl &UD) {
6202   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
6203     return;
6204   assert(UD.shadow_size() &&
6205          "We shouldn't be codegening an invalid UsingEnumDecl"
6206          " containing no decls");
6207 
6208   for (const auto *USD : UD.shadows())
6209     EmitUsingShadowDecl(*USD);
6210 }
6211 
6212 void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
6213   if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
6214     return;
6215   if (Module *M = ID.getImportedModule()) {
6216     auto Info = ASTSourceDescriptor(*M);
6217     auto Loc = ID.getLocation();
6218     DBuilder.createImportedDeclaration(
6219         getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
6220         getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
6221         getLineNumber(Loc));
6222   }
6223 }
6224 
6225 llvm::DIImportedEntity *
6226 CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
6227   if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
6228     return nullptr;
6229   auto &VH = NamespaceAliasCache[&NA];
6230   if (VH)
6231     return cast<llvm::DIImportedEntity>(VH);
6232   llvm::DIImportedEntity *R;
6233   auto Loc = NA.getLocation();
6234   if (const auto *Underlying =
6235           dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
6236     // This could cache & dedup here rather than relying on metadata deduping.
6237     R = DBuilder.createImportedDeclaration(
6238         getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
6239         EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
6240         getLineNumber(Loc), NA.getName());
6241   else
6242     R = DBuilder.createImportedDeclaration(
6243         getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
6244         getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
6245         getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
6246   VH.reset(R);
6247   return R;
6248 }
6249 
6250 llvm::DINamespace *
6251 CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
6252   // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
6253   // if necessary, and this way multiple declarations of the same namespace in
6254   // different parent modules stay distinct.
6255   auto I = NamespaceCache.find(NSDecl);
6256   if (I != NamespaceCache.end())
6257     return cast<llvm::DINamespace>(I->second);
6258 
6259   llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
6260   // Don't trust the context if it is a DIModule (see comment above).
6261   llvm::DINamespace *NS =
6262       DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
6263   NamespaceCache[NSDecl].reset(NS);
6264   return NS;
6265 }
6266 
6267 void CGDebugInfo::setDwoId(uint64_t Signature) {
6268   assert(TheCU && "no main compile unit");
6269   TheCU->setDWOId(Signature);
6270 }
6271 
6272 void CGDebugInfo::finalize() {
6273   // Creating types might create further types - invalidating the current
6274   // element and the size(), so don't cache/reference them.
6275   for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
6276     ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
6277     llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
6278                            ? CreateTypeDefinition(E.Type, E.Unit)
6279                            : E.Decl;
6280     DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
6281   }
6282 
6283   // Add methods to interface.
6284   for (const auto &P : ObjCMethodCache) {
6285     if (P.second.empty())
6286       continue;
6287 
6288     QualType QTy(P.first->getTypeForDecl(), 0);
6289     auto It = TypeCache.find(QTy.getAsOpaquePtr());
6290     assert(It != TypeCache.end());
6291 
6292     llvm::DICompositeType *InterfaceDecl =
6293         cast<llvm::DICompositeType>(It->second);
6294 
6295     auto CurElts = InterfaceDecl->getElements();
6296     SmallVector<llvm::Metadata *, 16> EltTys(CurElts.begin(), CurElts.end());
6297 
6298     // For DWARF v4 or earlier, only add objc_direct methods.
6299     for (auto &SubprogramDirect : P.second)
6300       if (CGM.getCodeGenOpts().DwarfVersion >= 5 || SubprogramDirect.getInt())
6301         EltTys.push_back(SubprogramDirect.getPointer());
6302 
6303     llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
6304     DBuilder.replaceArrays(InterfaceDecl, Elements);
6305   }
6306 
6307   for (const auto &P : ReplaceMap) {
6308     assert(P.second);
6309     auto *Ty = cast<llvm::DIType>(P.second);
6310     assert(Ty->isForwardDecl());
6311 
6312     auto It = TypeCache.find(P.first);
6313     assert(It != TypeCache.end());
6314     assert(It->second);
6315 
6316     DBuilder.replaceTemporary(llvm::TempDIType(Ty),
6317                               cast<llvm::DIType>(It->second));
6318   }
6319 
6320   for (const auto &P : FwdDeclReplaceMap) {
6321     assert(P.second);
6322     llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second));
6323     llvm::Metadata *Repl;
6324 
6325     auto It = DeclCache.find(P.first);
6326     // If there has been no definition for the declaration, call RAUW
6327     // with ourselves, that will destroy the temporary MDNode and
6328     // replace it with a standard one, avoiding leaking memory.
6329     if (It == DeclCache.end())
6330       Repl = P.second;
6331     else
6332       Repl = It->second;
6333 
6334     if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
6335       Repl = GVE->getVariable();
6336     DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
6337   }
6338 
6339   // We keep our own list of retained types, because we need to look
6340   // up the final type in the type cache.
6341   for (auto &RT : RetainedTypes)
6342     if (auto MD = TypeCache[RT])
6343       DBuilder.retainType(cast<llvm::DIType>(MD));
6344 
6345   DBuilder.finalize();
6346 }
6347 
6348 // Don't ignore in case of explicit cast where it is referenced indirectly.
6349 void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
6350   if (CGM.getCodeGenOpts().hasReducedDebugInfo())
6351     if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile()))
6352       DBuilder.retainType(DieTy);
6353 }
6354 
6355 void CGDebugInfo::EmitAndRetainType(QualType Ty) {
6356   if (CGM.getCodeGenOpts().hasMaybeUnusedDebugInfo())
6357     if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile()))
6358       DBuilder.retainType(DieTy);
6359 }
6360 
6361 llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
6362   if (LexicalBlockStack.empty())
6363     return llvm::DebugLoc();
6364 
6365   llvm::MDNode *Scope = LexicalBlockStack.back();
6366   return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc),
6367                                getColumnNumber(Loc), Scope);
6368 }
6369 
6370 llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
6371   // Call site-related attributes are only useful in optimized programs, and
6372   // when there's a possibility of debugging backtraces.
6373   if (!CGM.getLangOpts().Optimize ||
6374       DebugKind == llvm::codegenoptions::NoDebugInfo ||
6375       DebugKind == llvm::codegenoptions::LocTrackingOnly)
6376     return llvm::DINode::FlagZero;
6377 
6378   // Call site-related attributes are available in DWARF v5. Some debuggers,
6379   // while not fully DWARF v5-compliant, may accept these attributes as if they
6380   // were part of DWARF v4.
6381   bool SupportsDWARFv4Ext =
6382       CGM.getCodeGenOpts().DwarfVersion == 4 &&
6383       (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB ||
6384        CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::GDB);
6385 
6386   if (!SupportsDWARFv4Ext && CGM.getCodeGenOpts().DwarfVersion < 5)
6387     return llvm::DINode::FlagZero;
6388 
6389   return llvm::DINode::FlagAllCallsDescribed;
6390 }
6391 
6392 llvm::DIExpression *
6393 CGDebugInfo::createConstantValueExpression(const clang::ValueDecl *VD,
6394                                            const APValue &Val) {
6395   // FIXME: Add a representation for integer constants wider than 64 bits.
6396   if (CGM.getContext().getTypeSize(VD->getType()) > 64)
6397     return nullptr;
6398 
6399   if (Val.isFloat())
6400     return DBuilder.createConstantValueExpression(
6401         Val.getFloat().bitcastToAPInt().getZExtValue());
6402 
6403   if (!Val.isInt())
6404     return nullptr;
6405 
6406   llvm::APSInt const &ValInt = Val.getInt();
6407   std::optional<uint64_t> ValIntOpt;
6408   if (ValInt.isUnsigned())
6409     ValIntOpt = ValInt.tryZExtValue();
6410   else if (auto tmp = ValInt.trySExtValue())
6411     // Transform a signed optional to unsigned optional. When cpp 23 comes,
6412     // use std::optional::transform
6413     ValIntOpt = static_cast<uint64_t>(*tmp);
6414 
6415   if (ValIntOpt)
6416     return DBuilder.createConstantValueExpression(ValIntOpt.value());
6417 
6418   return nullptr;
6419 }
6420 
6421 CodeGenFunction::LexicalScope::LexicalScope(CodeGenFunction &CGF,
6422                                             SourceRange Range)
6423     : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
6424   CGF.CurLexicalScope = this;
6425   if (CGDebugInfo *DI = CGF.getDebugInfo())
6426     DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
6427 }
6428 
6429 CodeGenFunction::LexicalScope::~LexicalScope() {
6430   if (CGDebugInfo *DI = CGF.getDebugInfo())
6431     DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
6432 
6433   // If we should perform a cleanup, force them now.  Note that
6434   // this ends the cleanup scope before rescoping any labels.
6435   if (PerformCleanup) {
6436     ApplyDebugLocation DL(CGF, Range.getEnd());
6437     ForceCleanup();
6438   }
6439 }
6440 
6441 static std::string SanitizerHandlerToCheckLabel(SanitizerHandler Handler) {
6442   std::string Label;
6443   switch (Handler) {
6444 #define SANITIZER_CHECK(Enum, Name, Version)                                   \
6445   case Enum:                                                                   \
6446     Label = "__ubsan_check_" #Name;                                            \
6447     break;
6448 
6449     LIST_SANITIZER_CHECKS
6450 #undef SANITIZER_CHECK
6451   };
6452 
6453   // Label doesn't require sanitization
6454   return Label;
6455 }
6456 
6457 static std::string
6458 SanitizerOrdinalToCheckLabel(SanitizerKind::SanitizerOrdinal Ordinal) {
6459   std::string Label;
6460   switch (Ordinal) {
6461 #define SANITIZER(NAME, ID)                                                    \
6462   case SanitizerKind::SO_##ID:                                                 \
6463     Label = "__ubsan_check_" NAME;                                             \
6464     break;
6465 #include "clang/Basic/Sanitizers.def"
6466   default:
6467     llvm_unreachable("unexpected sanitizer kind");
6468   }
6469 
6470   // Sanitize label (convert hyphens to underscores; also futureproof against
6471   // non-alpha)
6472   for (unsigned int i = 0; i < Label.length(); i++)
6473     if (!std::isalpha(Label[i]))
6474       Label[i] = '_';
6475 
6476   return Label;
6477 }
6478 
6479 llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
6480     ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6481     SanitizerHandler Handler) {
6482   std::string Label;
6483   if (Ordinals.size() == 1)
6484     Label = SanitizerOrdinalToCheckLabel(Ordinals[0]);
6485   else
6486     Label = SanitizerHandlerToCheckLabel(Handler);
6487 
6488   llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
6489 
6490   for (auto Ord : Ordinals) {
6491     // TODO: deprecate ClArrayBoundsPseudoFn
6492     if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
6493          CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(Ord)) &&
6494         CheckDI) {
6495       return getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
6496     }
6497   }
6498 
6499   return CheckDI;
6500 }
6501 
6502 SanitizerDebugLocation::SanitizerDebugLocation(
6503     CodeGenFunction *CGF, ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6504     SanitizerHandler Handler)
6505     : CGF(CGF),
6506       Apply(*CGF, CGF->SanitizerAnnotateDebugInfo(Ordinals, Handler)) {
6507   assert(!CGF->IsSanitizerScope);
6508   CGF->IsSanitizerScope = true;
6509 }
6510 
6511 SanitizerDebugLocation::~SanitizerDebugLocation() {
6512   assert(CGF->IsSanitizerScope);
6513   CGF->IsSanitizerScope = false;
6514 }
6515