xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing Microsoft CodeView debug info.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeViewDebug.h"
14 #include "llvm/ADT/APSInt.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/BinaryFormat/Dwarf.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/LexicalScopes.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/TargetFrameLowering.h"
30 #include "llvm/CodeGen/TargetRegisterInfo.h"
31 #include "llvm/CodeGen/TargetSubtargetInfo.h"
32 #include "llvm/Config/llvm-config.h"
33 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
34 #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
35 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
36 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
37 #include "llvm/DebugInfo/CodeView/EnumTables.h"
38 #include "llvm/DebugInfo/CodeView/Line.h"
39 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
40 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
41 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
42 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DataLayout.h"
45 #include "llvm/IR/DebugInfoMetadata.h"
46 #include "llvm/IR/Function.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/Metadata.h"
50 #include "llvm/IR/Module.h"
51 #include "llvm/MC/MCAsmInfo.h"
52 #include "llvm/MC/MCContext.h"
53 #include "llvm/MC/MCSectionCOFF.h"
54 #include "llvm/MC/MCStreamer.h"
55 #include "llvm/MC/MCSymbol.h"
56 #include "llvm/Support/BinaryStreamWriter.h"
57 #include "llvm/Support/Casting.h"
58 #include "llvm/Support/Endian.h"
59 #include "llvm/Support/Error.h"
60 #include "llvm/Support/ErrorHandling.h"
61 #include "llvm/Support/FormatVariadic.h"
62 #include "llvm/Support/Path.h"
63 #include "llvm/Support/Program.h"
64 #include "llvm/Support/SMLoc.h"
65 #include "llvm/Support/ScopedPrinter.h"
66 #include "llvm/Target/TargetLoweringObjectFile.h"
67 #include "llvm/Target/TargetMachine.h"
68 #include <algorithm>
69 #include <cassert>
70 #include <cctype>
71 #include <cstddef>
72 #include <iterator>
73 #include <limits>
74 
75 using namespace llvm;
76 using namespace llvm::codeview;
77 
78 namespace {
79 class CVMCAdapter : public CodeViewRecordStreamer {
80 public:
81   CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable)
82       : OS(&OS), TypeTable(TypeTable) {}
83 
84   void emitBytes(StringRef Data) override { OS->emitBytes(Data); }
85 
86   void emitIntValue(uint64_t Value, unsigned Size) override {
87     OS->emitIntValueInHex(Value, Size);
88   }
89 
90   void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); }
91 
92   void AddComment(const Twine &T) override { OS->AddComment(T); }
93 
94   void AddRawComment(const Twine &T) override { OS->emitRawComment(T); }
95 
96   bool isVerboseAsm() override { return OS->isVerboseAsm(); }
97 
98   std::string getTypeName(TypeIndex TI) override {
99     std::string TypeName;
100     if (!TI.isNoneType()) {
101       if (TI.isSimple())
102         TypeName = std::string(TypeIndex::simpleTypeName(TI));
103       else
104         TypeName = std::string(TypeTable.getTypeName(TI));
105     }
106     return TypeName;
107   }
108 
109 private:
110   MCStreamer *OS = nullptr;
111   TypeCollection &TypeTable;
112 };
113 } // namespace
114 
115 static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
116   switch (Type) {
117   case Triple::ArchType::x86:
118     return CPUType::Pentium3;
119   case Triple::ArchType::x86_64:
120     return CPUType::X64;
121   case Triple::ArchType::thumb:
122     // LLVM currently doesn't support Windows CE and so thumb
123     // here is indiscriminately mapped to ARMNT specifically.
124     return CPUType::ARMNT;
125   case Triple::ArchType::aarch64:
126     return CPUType::ARM64;
127   default:
128     report_fatal_error("target architecture doesn't map to a CodeView CPUType");
129   }
130 }
131 
132 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
133     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {}
134 
135 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
136   std::string &Filepath = FileToFilepathMap[File];
137   if (!Filepath.empty())
138     return Filepath;
139 
140   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
141 
142   // If this is a Unix-style path, just use it as is. Don't try to canonicalize
143   // it textually because one of the path components could be a symlink.
144   if (Dir.startswith("/") || Filename.startswith("/")) {
145     if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix))
146       return Filename;
147     Filepath = std::string(Dir);
148     if (Dir.back() != '/')
149       Filepath += '/';
150     Filepath += Filename;
151     return Filepath;
152   }
153 
154   // Clang emits directory and relative filename info into the IR, but CodeView
155   // operates on full paths.  We could change Clang to emit full paths too, but
156   // that would increase the IR size and probably not needed for other users.
157   // For now, just concatenate and canonicalize the path here.
158   if (Filename.find(':') == 1)
159     Filepath = std::string(Filename);
160   else
161     Filepath = (Dir + "\\" + Filename).str();
162 
163   // Canonicalize the path.  We have to do it textually because we may no longer
164   // have access the file in the filesystem.
165   // First, replace all slashes with backslashes.
166   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
167 
168   // Remove all "\.\" with "\".
169   size_t Cursor = 0;
170   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
171     Filepath.erase(Cursor, 2);
172 
173   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
174   // path should be well-formatted, e.g. start with a drive letter, etc.
175   Cursor = 0;
176   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
177     // Something's wrong if the path starts with "\..\", abort.
178     if (Cursor == 0)
179       break;
180 
181     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
182     if (PrevSlash == std::string::npos)
183       // Something's wrong, abort.
184       break;
185 
186     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
187     // The next ".." might be following the one we've just erased.
188     Cursor = PrevSlash;
189   }
190 
191   // Remove all duplicate backslashes.
192   Cursor = 0;
193   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
194     Filepath.erase(Cursor, 1);
195 
196   return Filepath;
197 }
198 
199 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
200   StringRef FullPath = getFullFilepath(F);
201   unsigned NextId = FileIdMap.size() + 1;
202   auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
203   if (Insertion.second) {
204     // We have to compute the full filepath and emit a .cv_file directive.
205     ArrayRef<uint8_t> ChecksumAsBytes;
206     FileChecksumKind CSKind = FileChecksumKind::None;
207     if (F->getChecksum()) {
208       std::string Checksum = fromHex(F->getChecksum()->Value);
209       void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
210       memcpy(CKMem, Checksum.data(), Checksum.size());
211       ChecksumAsBytes = ArrayRef<uint8_t>(
212           reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
213       switch (F->getChecksum()->Kind) {
214       case DIFile::CSK_MD5:
215         CSKind = FileChecksumKind::MD5;
216         break;
217       case DIFile::CSK_SHA1:
218         CSKind = FileChecksumKind::SHA1;
219         break;
220       case DIFile::CSK_SHA256:
221         CSKind = FileChecksumKind::SHA256;
222         break;
223       }
224     }
225     bool Success = OS.emitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
226                                           static_cast<unsigned>(CSKind));
227     (void)Success;
228     assert(Success && ".cv_file directive failed");
229   }
230   return Insertion.first->second;
231 }
232 
233 CodeViewDebug::InlineSite &
234 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
235                              const DISubprogram *Inlinee) {
236   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
237   InlineSite *Site = &SiteInsertion.first->second;
238   if (SiteInsertion.second) {
239     unsigned ParentFuncId = CurFn->FuncId;
240     if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
241       ParentFuncId =
242           getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
243               .SiteFuncId;
244 
245     Site->SiteFuncId = NextFuncId++;
246     OS.emitCVInlineSiteIdDirective(
247         Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
248         InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
249     Site->Inlinee = Inlinee;
250     InlinedSubprograms.insert(Inlinee);
251     getFuncIdForSubprogram(Inlinee);
252   }
253   return *Site;
254 }
255 
256 static StringRef getPrettyScopeName(const DIScope *Scope) {
257   StringRef ScopeName = Scope->getName();
258   if (!ScopeName.empty())
259     return ScopeName;
260 
261   switch (Scope->getTag()) {
262   case dwarf::DW_TAG_enumeration_type:
263   case dwarf::DW_TAG_class_type:
264   case dwarf::DW_TAG_structure_type:
265   case dwarf::DW_TAG_union_type:
266     return "<unnamed-tag>";
267   case dwarf::DW_TAG_namespace:
268     return "`anonymous namespace'";
269   default:
270     return StringRef();
271   }
272 }
273 
274 const DISubprogram *CodeViewDebug::collectParentScopeNames(
275     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
276   const DISubprogram *ClosestSubprogram = nullptr;
277   while (Scope != nullptr) {
278     if (ClosestSubprogram == nullptr)
279       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
280 
281     // If a type appears in a scope chain, make sure it gets emitted. The
282     // frontend will be responsible for deciding if this should be a forward
283     // declaration or a complete type.
284     if (const auto *Ty = dyn_cast<DICompositeType>(Scope))
285       DeferredCompleteTypes.push_back(Ty);
286 
287     StringRef ScopeName = getPrettyScopeName(Scope);
288     if (!ScopeName.empty())
289       QualifiedNameComponents.push_back(ScopeName);
290     Scope = Scope->getScope();
291   }
292   return ClosestSubprogram;
293 }
294 
295 static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents,
296                                     StringRef TypeName) {
297   std::string FullyQualifiedName;
298   for (StringRef QualifiedNameComponent :
299        llvm::reverse(QualifiedNameComponents)) {
300     FullyQualifiedName.append(std::string(QualifiedNameComponent));
301     FullyQualifiedName.append("::");
302   }
303   FullyQualifiedName.append(std::string(TypeName));
304   return FullyQualifiedName;
305 }
306 
307 struct CodeViewDebug::TypeLoweringScope {
308   TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
309   ~TypeLoweringScope() {
310     // Don't decrement TypeEmissionLevel until after emitting deferred types, so
311     // inner TypeLoweringScopes don't attempt to emit deferred types.
312     if (CVD.TypeEmissionLevel == 1)
313       CVD.emitDeferredCompleteTypes();
314     --CVD.TypeEmissionLevel;
315   }
316   CodeViewDebug &CVD;
317 };
318 
319 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope,
320                                                  StringRef Name) {
321   // Ensure types in the scope chain are emitted as soon as possible.
322   // This can create otherwise a situation where S_UDTs are emitted while
323   // looping in emitDebugInfoForUDTs.
324   TypeLoweringScope S(*this);
325   SmallVector<StringRef, 5> QualifiedNameComponents;
326   collectParentScopeNames(Scope, QualifiedNameComponents);
327   return formatNestedName(QualifiedNameComponents, Name);
328 }
329 
330 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) {
331   const DIScope *Scope = Ty->getScope();
332   return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
333 }
334 
335 TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
336   // No scope means global scope and that uses the zero index.
337   //
338   // We also use zero index when the scope is a DISubprogram
339   // to suppress the emission of LF_STRING_ID for the function,
340   // which can trigger a link-time error with the linker in
341   // VS2019 version 16.11.2 or newer.
342   // Note, however, skipping the debug info emission for the DISubprogram
343   // is a temporary fix. The root issue here is that we need to figure out
344   // the proper way to encode a function nested in another function
345   // (as introduced by the Fortran 'contains' keyword) in CodeView.
346   if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope))
347     return TypeIndex();
348 
349   assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
350 
351   // Check if we've already translated this scope.
352   auto I = TypeIndices.find({Scope, nullptr});
353   if (I != TypeIndices.end())
354     return I->second;
355 
356   // Build the fully qualified name of the scope.
357   std::string ScopeName = getFullyQualifiedName(Scope);
358   StringIdRecord SID(TypeIndex(), ScopeName);
359   auto TI = TypeTable.writeLeafType(SID);
360   return recordTypeIndexForDINode(Scope, TI);
361 }
362 
363 static StringRef removeTemplateArgs(StringRef Name) {
364   // Remove template args from the display name. Assume that the template args
365   // are the last thing in the name.
366   if (Name.empty() || Name.back() != '>')
367     return Name;
368 
369   int OpenBrackets = 0;
370   for (int i = Name.size() - 1; i >= 0; --i) {
371     if (Name[i] == '>')
372       ++OpenBrackets;
373     else if (Name[i] == '<') {
374       --OpenBrackets;
375       if (OpenBrackets == 0)
376         return Name.substr(0, i);
377     }
378   }
379   return Name;
380 }
381 
382 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
383   assert(SP);
384 
385   // Check if we've already translated this subprogram.
386   auto I = TypeIndices.find({SP, nullptr});
387   if (I != TypeIndices.end())
388     return I->second;
389 
390   // The display name includes function template arguments. Drop them to match
391   // MSVC. We need to have the template arguments in the DISubprogram name
392   // because they are used in other symbol records, such as S_GPROC32_IDs.
393   StringRef DisplayName = removeTemplateArgs(SP->getName());
394 
395   const DIScope *Scope = SP->getScope();
396   TypeIndex TI;
397   if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
398     // If the scope is a DICompositeType, then this must be a method. Member
399     // function types take some special handling, and require access to the
400     // subprogram.
401     TypeIndex ClassType = getTypeIndex(Class);
402     MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
403                                DisplayName);
404     TI = TypeTable.writeLeafType(MFuncId);
405   } else {
406     // Otherwise, this must be a free function.
407     TypeIndex ParentScope = getScopeIndex(Scope);
408     FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
409     TI = TypeTable.writeLeafType(FuncId);
410   }
411 
412   return recordTypeIndexForDINode(SP, TI);
413 }
414 
415 static bool isNonTrivial(const DICompositeType *DCTy) {
416   return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial);
417 }
418 
419 static FunctionOptions
420 getFunctionOptions(const DISubroutineType *Ty,
421                    const DICompositeType *ClassTy = nullptr,
422                    StringRef SPName = StringRef("")) {
423   FunctionOptions FO = FunctionOptions::None;
424   const DIType *ReturnTy = nullptr;
425   if (auto TypeArray = Ty->getTypeArray()) {
426     if (TypeArray.size())
427       ReturnTy = TypeArray[0];
428   }
429 
430   // Add CxxReturnUdt option to functions that return nontrivial record types
431   // or methods that return record types.
432   if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy))
433     if (isNonTrivial(ReturnDCTy) || ClassTy)
434       FO |= FunctionOptions::CxxReturnUdt;
435 
436   // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.
437   if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) {
438     FO |= FunctionOptions::Constructor;
439 
440   // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.
441 
442   }
443   return FO;
444 }
445 
446 TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
447                                                const DICompositeType *Class) {
448   // Always use the method declaration as the key for the function type. The
449   // method declaration contains the this adjustment.
450   if (SP->getDeclaration())
451     SP = SP->getDeclaration();
452   assert(!SP->getDeclaration() && "should use declaration as key");
453 
454   // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
455   // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
456   auto I = TypeIndices.find({SP, Class});
457   if (I != TypeIndices.end())
458     return I->second;
459 
460   // Make sure complete type info for the class is emitted *after* the member
461   // function type, as the complete class type is likely to reference this
462   // member function type.
463   TypeLoweringScope S(*this);
464   const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
465 
466   FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());
467   TypeIndex TI = lowerTypeMemberFunction(
468       SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);
469   return recordTypeIndexForDINode(SP, TI, Class);
470 }
471 
472 TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
473                                                   TypeIndex TI,
474                                                   const DIType *ClassTy) {
475   auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
476   (void)InsertResult;
477   assert(InsertResult.second && "DINode was already assigned a type index");
478   return TI;
479 }
480 
481 unsigned CodeViewDebug::getPointerSizeInBytes() {
482   return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
483 }
484 
485 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
486                                         const LexicalScope *LS) {
487   if (const DILocation *InlinedAt = LS->getInlinedAt()) {
488     // This variable was inlined. Associate it with the InlineSite.
489     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
490     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
491     Site.InlinedLocals.emplace_back(Var);
492   } else {
493     // This variable goes into the corresponding lexical scope.
494     ScopeVariables[LS].emplace_back(Var);
495   }
496 }
497 
498 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
499                                const DILocation *Loc) {
500   if (!llvm::is_contained(Locs, Loc))
501     Locs.push_back(Loc);
502 }
503 
504 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
505                                         const MachineFunction *MF) {
506   // Skip this instruction if it has the same location as the previous one.
507   if (!DL || DL == PrevInstLoc)
508     return;
509 
510   const DIScope *Scope = DL->getScope();
511   if (!Scope)
512     return;
513 
514   // Skip this line if it is longer than the maximum we can record.
515   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
516   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
517       LI.isNeverStepInto())
518     return;
519 
520   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
521   if (CI.getStartColumn() != DL.getCol())
522     return;
523 
524   if (!CurFn->HaveLineInfo)
525     CurFn->HaveLineInfo = true;
526   unsigned FileId = 0;
527   if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
528     FileId = CurFn->LastFileId;
529   else
530     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
531   PrevInstLoc = DL;
532 
533   unsigned FuncId = CurFn->FuncId;
534   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
535     const DILocation *Loc = DL.get();
536 
537     // If this location was actually inlined from somewhere else, give it the ID
538     // of the inline call site.
539     FuncId =
540         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
541 
542     // Ensure we have links in the tree of inline call sites.
543     bool FirstLoc = true;
544     while ((SiteLoc = Loc->getInlinedAt())) {
545       InlineSite &Site =
546           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
547       if (!FirstLoc)
548         addLocIfNotPresent(Site.ChildSites, Loc);
549       FirstLoc = false;
550       Loc = SiteLoc;
551     }
552     addLocIfNotPresent(CurFn->ChildSites, Loc);
553   }
554 
555   OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
556                         /*PrologueEnd=*/false, /*IsStmt=*/false,
557                         DL->getFilename(), SMLoc());
558 }
559 
560 void CodeViewDebug::emitCodeViewMagicVersion() {
561   OS.emitValueToAlignment(Align(4));
562   OS.AddComment("Debug section magic");
563   OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);
564 }
565 
566 static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
567   switch (DWLang) {
568   case dwarf::DW_LANG_C:
569   case dwarf::DW_LANG_C89:
570   case dwarf::DW_LANG_C99:
571   case dwarf::DW_LANG_C11:
572   case dwarf::DW_LANG_ObjC:
573     return SourceLanguage::C;
574   case dwarf::DW_LANG_C_plus_plus:
575   case dwarf::DW_LANG_C_plus_plus_03:
576   case dwarf::DW_LANG_C_plus_plus_11:
577   case dwarf::DW_LANG_C_plus_plus_14:
578     return SourceLanguage::Cpp;
579   case dwarf::DW_LANG_Fortran77:
580   case dwarf::DW_LANG_Fortran90:
581   case dwarf::DW_LANG_Fortran95:
582   case dwarf::DW_LANG_Fortran03:
583   case dwarf::DW_LANG_Fortran08:
584     return SourceLanguage::Fortran;
585   case dwarf::DW_LANG_Pascal83:
586     return SourceLanguage::Pascal;
587   case dwarf::DW_LANG_Cobol74:
588   case dwarf::DW_LANG_Cobol85:
589     return SourceLanguage::Cobol;
590   case dwarf::DW_LANG_Java:
591     return SourceLanguage::Java;
592   case dwarf::DW_LANG_D:
593     return SourceLanguage::D;
594   case dwarf::DW_LANG_Swift:
595     return SourceLanguage::Swift;
596   case dwarf::DW_LANG_Rust:
597     return SourceLanguage::Rust;
598   default:
599     // There's no CodeView representation for this language, and CV doesn't
600     // have an "unknown" option for the language field, so we'll use MASM,
601     // as it's very low level.
602     return SourceLanguage::Masm;
603   }
604 }
605 
606 void CodeViewDebug::beginModule(Module *M) {
607   // If module doesn't have named metadata anchors or COFF debug section
608   // is not available, skip any debug info related stuff.
609   if (!MMI->hasDebugInfo() ||
610       !Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
611     Asm = nullptr;
612     return;
613   }
614 
615   TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch());
616 
617   // Get the current source language.
618   const MDNode *Node = *M->debug_compile_units_begin();
619   const auto *CU = cast<DICompileUnit>(Node);
620 
621   CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage());
622 
623   collectGlobalVariableInfo();
624 
625   // Check if we should emit type record hashes.
626   ConstantInt *GH =
627       mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash"));
628   EmitDebugGlobalHashes = GH && !GH->isZero();
629 }
630 
631 void CodeViewDebug::endModule() {
632   if (!Asm || !MMI->hasDebugInfo())
633     return;
634 
635   // The COFF .debug$S section consists of several subsections, each starting
636   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
637   // of the payload followed by the payload itself.  The subsections are 4-byte
638   // aligned.
639 
640   // Use the generic .debug$S section, and make a subsection for all the inlined
641   // subprograms.
642   switchToDebugSectionForSymbol(nullptr);
643 
644   MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
645   emitObjName();
646   emitCompilerInformation();
647   endCVSubsection(CompilerInfo);
648 
649   emitInlineeLinesSubsection();
650 
651   // Emit per-function debug information.
652   for (auto &P : FnDebugInfo)
653     if (!P.first->isDeclarationForLinker())
654       emitDebugInfoForFunction(P.first, *P.second);
655 
656   // Get types used by globals without emitting anything.
657   // This is meant to collect all static const data members so they can be
658   // emitted as globals.
659   collectDebugInfoForGlobals();
660 
661   // Emit retained types.
662   emitDebugInfoForRetainedTypes();
663 
664   // Emit global variable debug information.
665   setCurrentSubprogram(nullptr);
666   emitDebugInfoForGlobals();
667 
668   // Switch back to the generic .debug$S section after potentially processing
669   // comdat symbol sections.
670   switchToDebugSectionForSymbol(nullptr);
671 
672   // Emit UDT records for any types used by global variables.
673   if (!GlobalUDTs.empty()) {
674     MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
675     emitDebugInfoForUDTs(GlobalUDTs);
676     endCVSubsection(SymbolsEnd);
677   }
678 
679   // This subsection holds a file index to offset in string table table.
680   OS.AddComment("File index to string table offset subsection");
681   OS.emitCVFileChecksumsDirective();
682 
683   // This subsection holds the string table.
684   OS.AddComment("String table");
685   OS.emitCVStringTableDirective();
686 
687   // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol
688   // subsection in the generic .debug$S section at the end. There is no
689   // particular reason for this ordering other than to match MSVC.
690   emitBuildInfo();
691 
692   // Emit type information and hashes last, so that any types we translate while
693   // emitting function info are included.
694   emitTypeInformation();
695 
696   if (EmitDebugGlobalHashes)
697     emitTypeGlobalHashes();
698 
699   clear();
700 }
701 
702 static void
703 emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S,
704                              unsigned MaxFixedRecordLength = 0xF00) {
705   // The maximum CV record length is 0xFF00. Most of the strings we emit appear
706   // after a fixed length portion of the record. The fixed length portion should
707   // always be less than 0xF00 (3840) bytes, so truncate the string so that the
708   // overall record size is less than the maximum allowed.
709   SmallString<32> NullTerminatedString(
710       S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
711   NullTerminatedString.push_back('\0');
712   OS.emitBytes(NullTerminatedString);
713 }
714 
715 void CodeViewDebug::emitTypeInformation() {
716   if (TypeTable.empty())
717     return;
718 
719   // Start the .debug$T or .debug$P section with 0x4.
720   OS.switchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
721   emitCodeViewMagicVersion();
722 
723   TypeTableCollection Table(TypeTable.records());
724   TypeVisitorCallbackPipeline Pipeline;
725 
726   // To emit type record using Codeview MCStreamer adapter
727   CVMCAdapter CVMCOS(OS, Table);
728   TypeRecordMapping typeMapping(CVMCOS);
729   Pipeline.addCallbackToPipeline(typeMapping);
730 
731   std::optional<TypeIndex> B = Table.getFirst();
732   while (B) {
733     // This will fail if the record data is invalid.
734     CVType Record = Table.getType(*B);
735 
736     Error E = codeview::visitTypeRecord(Record, *B, Pipeline);
737 
738     if (E) {
739       logAllUnhandledErrors(std::move(E), errs(), "error: ");
740       llvm_unreachable("produced malformed type record");
741     }
742 
743     B = Table.getNext(*B);
744   }
745 }
746 
747 void CodeViewDebug::emitTypeGlobalHashes() {
748   if (TypeTable.empty())
749     return;
750 
751   // Start the .debug$H section with the version and hash algorithm, currently
752   // hardcoded to version 0, SHA1.
753   OS.switchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection());
754 
755   OS.emitValueToAlignment(Align(4));
756   OS.AddComment("Magic");
757   OS.emitInt32(COFF::DEBUG_HASHES_SECTION_MAGIC);
758   OS.AddComment("Section Version");
759   OS.emitInt16(0);
760   OS.AddComment("Hash Algorithm");
761   OS.emitInt16(uint16_t(GlobalTypeHashAlg::BLAKE3));
762 
763   TypeIndex TI(TypeIndex::FirstNonSimpleIndex);
764   for (const auto &GHR : TypeTable.hashes()) {
765     if (OS.isVerboseAsm()) {
766       // Emit an EOL-comment describing which TypeIndex this hash corresponds
767       // to, as well as the stringified SHA1 hash.
768       SmallString<32> Comment;
769       raw_svector_ostream CommentOS(Comment);
770       CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);
771       OS.AddComment(Comment);
772       ++TI;
773     }
774     assert(GHR.Hash.size() == 8);
775     StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),
776                 GHR.Hash.size());
777     OS.emitBinaryData(S);
778   }
779 }
780 
781 void CodeViewDebug::emitObjName() {
782   MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME);
783 
784   StringRef PathRef(Asm->TM.Options.ObjectFilenameForDebug);
785   llvm::SmallString<256> PathStore(PathRef);
786 
787   if (PathRef.empty() || PathRef == "-") {
788     // Don't emit the filename if we're writing to stdout or to /dev/null.
789     PathRef = {};
790   } else {
791     llvm::sys::path::remove_dots(PathStore, /*remove_dot_dot=*/true);
792     PathRef = PathStore;
793   }
794 
795   OS.AddComment("Signature");
796   OS.emitIntValue(0, 4);
797 
798   OS.AddComment("Object name");
799   emitNullTerminatedSymbolName(OS, PathRef);
800 
801   endSymbolRecord(CompilerEnd);
802 }
803 
804 namespace {
805 struct Version {
806   int Part[4];
807 };
808 } // end anonymous namespace
809 
810 // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
811 // the version number.
812 static Version parseVersion(StringRef Name) {
813   Version V = {{0}};
814   int N = 0;
815   for (const char C : Name) {
816     if (isdigit(C)) {
817       V.Part[N] *= 10;
818       V.Part[N] += C - '0';
819       V.Part[N] =
820           std::min<int>(V.Part[N], std::numeric_limits<uint16_t>::max());
821     } else if (C == '.') {
822       ++N;
823       if (N >= 4)
824         return V;
825     } else if (N > 0)
826       return V;
827   }
828   return V;
829 }
830 
831 void CodeViewDebug::emitCompilerInformation() {
832   MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
833   uint32_t Flags = 0;
834 
835   // The low byte of the flags indicates the source language.
836   Flags = CurrentSourceLanguage;
837   // TODO:  Figure out which other flags need to be set.
838   if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {
839     Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);
840   }
841   using ArchType = llvm::Triple::ArchType;
842   ArchType Arch = Triple(MMI->getModule()->getTargetTriple()).getArch();
843   if (Asm->TM.Options.Hotpatch || Arch == ArchType::thumb ||
844       Arch == ArchType::aarch64) {
845     Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch);
846   }
847 
848   OS.AddComment("Flags and language");
849   OS.emitInt32(Flags);
850 
851   OS.AddComment("CPUType");
852   OS.emitInt16(static_cast<uint64_t>(TheCPU));
853 
854   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
855   const MDNode *Node = *CUs->operands().begin();
856   const auto *CU = cast<DICompileUnit>(Node);
857 
858   StringRef CompilerVersion = CU->getProducer();
859   Version FrontVer = parseVersion(CompilerVersion);
860   OS.AddComment("Frontend version");
861   for (int N : FrontVer.Part) {
862     OS.emitInt16(N);
863   }
864 
865   // Some Microsoft tools, like Binscope, expect a backend version number of at
866   // least 8.something, so we'll coerce the LLVM version into a form that
867   // guarantees it'll be big enough without really lying about the version.
868   int Major = 1000 * LLVM_VERSION_MAJOR +
869               10 * LLVM_VERSION_MINOR +
870               LLVM_VERSION_PATCH;
871   // Clamp it for builds that use unusually large version numbers.
872   Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
873   Version BackVer = {{ Major, 0, 0, 0 }};
874   OS.AddComment("Backend version");
875   for (int N : BackVer.Part)
876     OS.emitInt16(N);
877 
878   OS.AddComment("Null-terminated compiler version string");
879   emitNullTerminatedSymbolName(OS, CompilerVersion);
880 
881   endSymbolRecord(CompilerEnd);
882 }
883 
884 static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
885                                     StringRef S) {
886   StringIdRecord SIR(TypeIndex(0x0), S);
887   return TypeTable.writeLeafType(SIR);
888 }
889 
890 static std::string flattenCommandLine(ArrayRef<std::string> Args,
891                                       StringRef MainFilename) {
892   std::string FlatCmdLine;
893   raw_string_ostream OS(FlatCmdLine);
894   bool PrintedOneArg = false;
895   if (!StringRef(Args[0]).contains("-cc1")) {
896     llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
897     PrintedOneArg = true;
898   }
899   for (unsigned i = 0; i < Args.size(); i++) {
900     StringRef Arg = Args[i];
901     if (Arg.empty())
902       continue;
903     if (Arg == "-main-file-name" || Arg == "-o") {
904       i++; // Skip this argument and next one.
905       continue;
906     }
907     if (Arg.startswith("-object-file-name") || Arg == MainFilename)
908       continue;
909     // Skip fmessage-length for reproduciability.
910     if (Arg.startswith("-fmessage-length"))
911       continue;
912     if (PrintedOneArg)
913       OS << " ";
914     llvm::sys::printArg(OS, Arg, /*Quote=*/true);
915     PrintedOneArg = true;
916   }
917   OS.flush();
918   return FlatCmdLine;
919 }
920 
921 void CodeViewDebug::emitBuildInfo() {
922   // First, make LF_BUILDINFO. It's a sequence of strings with various bits of
923   // build info. The known prefix is:
924   // - Absolute path of current directory
925   // - Compiler path
926   // - Main source file path, relative to CWD or absolute
927   // - Type server PDB file
928   // - Canonical compiler command line
929   // If frontend and backend compilation are separated (think llc or LTO), it's
930   // not clear if the compiler path should refer to the executable for the
931   // frontend or the backend. Leave it blank for now.
932   TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
933   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
934   const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
935   const auto *CU = cast<DICompileUnit>(Node);
936   const DIFile *MainSourceFile = CU->getFile();
937   BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
938       getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());
939   BuildInfoArgs[BuildInfoRecord::SourceFile] =
940       getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());
941   // FIXME: PDB is intentionally blank unless we implement /Zi type servers.
942   BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
943       getStringIdTypeIdx(TypeTable, "");
944   if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
945     BuildInfoArgs[BuildInfoRecord::BuildTool] =
946         getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
947     BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
948         TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
949                                       MainSourceFile->getFilename()));
950   }
951   BuildInfoRecord BIR(BuildInfoArgs);
952   TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
953 
954   // Make a new .debug$S subsection for the S_BUILDINFO record, which points
955   // from the module symbols into the type stream.
956   MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
957   MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);
958   OS.AddComment("LF_BUILDINFO index");
959   OS.emitInt32(BuildInfoIndex.getIndex());
960   endSymbolRecord(BIEnd);
961   endCVSubsection(BISubsecEnd);
962 }
963 
964 void CodeViewDebug::emitInlineeLinesSubsection() {
965   if (InlinedSubprograms.empty())
966     return;
967 
968   OS.AddComment("Inlinee lines subsection");
969   MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
970 
971   // We emit the checksum info for files.  This is used by debuggers to
972   // determine if a pdb matches the source before loading it.  Visual Studio,
973   // for instance, will display a warning that the breakpoints are not valid if
974   // the pdb does not match the source.
975   OS.AddComment("Inlinee lines signature");
976   OS.emitInt32(unsigned(InlineeLinesSignature::Normal));
977 
978   for (const DISubprogram *SP : InlinedSubprograms) {
979     assert(TypeIndices.count({SP, nullptr}));
980     TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
981 
982     OS.addBlankLine();
983     unsigned FileId = maybeRecordFile(SP->getFile());
984     OS.AddComment("Inlined function " + SP->getName() + " starts at " +
985                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
986     OS.addBlankLine();
987     OS.AddComment("Type index of inlined function");
988     OS.emitInt32(InlineeIdx.getIndex());
989     OS.AddComment("Offset into filechecksum table");
990     OS.emitCVFileChecksumOffsetDirective(FileId);
991     OS.AddComment("Starting line number");
992     OS.emitInt32(SP->getLine());
993   }
994 
995   endCVSubsection(InlineEnd);
996 }
997 
998 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
999                                         const DILocation *InlinedAt,
1000                                         const InlineSite &Site) {
1001   assert(TypeIndices.count({Site.Inlinee, nullptr}));
1002   TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
1003 
1004   // SymbolRecord
1005   MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);
1006 
1007   OS.AddComment("PtrParent");
1008   OS.emitInt32(0);
1009   OS.AddComment("PtrEnd");
1010   OS.emitInt32(0);
1011   OS.AddComment("Inlinee type index");
1012   OS.emitInt32(InlineeIdx.getIndex());
1013 
1014   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
1015   unsigned StartLineNum = Site.Inlinee->getLine();
1016 
1017   OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
1018                                     FI.Begin, FI.End);
1019 
1020   endSymbolRecord(InlineEnd);
1021 
1022   emitLocalVariableList(FI, Site.InlinedLocals);
1023 
1024   // Recurse on child inlined call sites before closing the scope.
1025   for (const DILocation *ChildSite : Site.ChildSites) {
1026     auto I = FI.InlineSites.find(ChildSite);
1027     assert(I != FI.InlineSites.end() &&
1028            "child site not in function inline site map");
1029     emitInlinedCallSite(FI, ChildSite, I->second);
1030   }
1031 
1032   // Close the scope.
1033   emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);
1034 }
1035 
1036 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
1037   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
1038   // comdat key. A section may be comdat because of -ffunction-sections or
1039   // because it is comdat in the IR.
1040   MCSectionCOFF *GVSec =
1041       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
1042   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
1043 
1044   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
1045       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
1046   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
1047 
1048   OS.switchSection(DebugSec);
1049 
1050   // Emit the magic version number if this is the first time we've switched to
1051   // this section.
1052   if (ComdatDebugSections.insert(DebugSec).second)
1053     emitCodeViewMagicVersion();
1054 }
1055 
1056 // Emit an S_THUNK32/S_END symbol pair for a thunk routine.
1057 // The only supported thunk ordinal is currently the standard type.
1058 void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,
1059                                           FunctionInfo &FI,
1060                                           const MCSymbol *Fn) {
1061   std::string FuncName =
1062       std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1063   const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.
1064 
1065   OS.AddComment("Symbol subsection for " + Twine(FuncName));
1066   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1067 
1068   // Emit S_THUNK32
1069   MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);
1070   OS.AddComment("PtrParent");
1071   OS.emitInt32(0);
1072   OS.AddComment("PtrEnd");
1073   OS.emitInt32(0);
1074   OS.AddComment("PtrNext");
1075   OS.emitInt32(0);
1076   OS.AddComment("Thunk section relative address");
1077   OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1078   OS.AddComment("Thunk section index");
1079   OS.emitCOFFSectionIndex(Fn);
1080   OS.AddComment("Code size");
1081   OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);
1082   OS.AddComment("Ordinal");
1083   OS.emitInt8(unsigned(ordinal));
1084   OS.AddComment("Function name");
1085   emitNullTerminatedSymbolName(OS, FuncName);
1086   // Additional fields specific to the thunk ordinal would go here.
1087   endSymbolRecord(ThunkRecordEnd);
1088 
1089   // Local variables/inlined routines are purposely omitted here.  The point of
1090   // marking this as a thunk is so Visual Studio will NOT stop in this routine.
1091 
1092   // Emit S_PROC_ID_END
1093   emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1094 
1095   endCVSubsection(SymbolsEnd);
1096 }
1097 
1098 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
1099                                              FunctionInfo &FI) {
1100   // For each function there is a separate subsection which holds the PC to
1101   // file:line table.
1102   const MCSymbol *Fn = Asm->getSymbol(GV);
1103   assert(Fn);
1104 
1105   // Switch to the to a comdat section, if appropriate.
1106   switchToDebugSectionForSymbol(Fn);
1107 
1108   std::string FuncName;
1109   auto *SP = GV->getSubprogram();
1110   assert(SP);
1111   setCurrentSubprogram(SP);
1112 
1113   if (SP->isThunk()) {
1114     emitDebugInfoForThunk(GV, FI, Fn);
1115     return;
1116   }
1117 
1118   // If we have a display name, build the fully qualified name by walking the
1119   // chain of scopes.
1120   if (!SP->getName().empty())
1121     FuncName = getFullyQualifiedName(SP->getScope(), SP->getName());
1122 
1123   // If our DISubprogram name is empty, use the mangled name.
1124   if (FuncName.empty())
1125     FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1126 
1127   // Emit FPO data, but only on 32-bit x86. No other platforms use it.
1128   if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86)
1129     OS.emitCVFPOData(Fn);
1130 
1131   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
1132   OS.AddComment("Symbol subsection for " + Twine(FuncName));
1133   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1134   {
1135     SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID
1136                                                 : SymbolKind::S_GPROC32_ID;
1137     MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);
1138 
1139     // These fields are filled in by tools like CVPACK which run after the fact.
1140     OS.AddComment("PtrParent");
1141     OS.emitInt32(0);
1142     OS.AddComment("PtrEnd");
1143     OS.emitInt32(0);
1144     OS.AddComment("PtrNext");
1145     OS.emitInt32(0);
1146     // This is the important bit that tells the debugger where the function
1147     // code is located and what's its size:
1148     OS.AddComment("Code size");
1149     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
1150     OS.AddComment("Offset after prologue");
1151     OS.emitInt32(0);
1152     OS.AddComment("Offset before epilogue");
1153     OS.emitInt32(0);
1154     OS.AddComment("Function type index");
1155     OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex());
1156     OS.AddComment("Function section relative address");
1157     OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1158     OS.AddComment("Function section index");
1159     OS.emitCOFFSectionIndex(Fn);
1160     OS.AddComment("Flags");
1161     OS.emitInt8(0);
1162     // Emit the function display name as a null-terminated string.
1163     OS.AddComment("Function name");
1164     // Truncate the name so we won't overflow the record length field.
1165     emitNullTerminatedSymbolName(OS, FuncName);
1166     endSymbolRecord(ProcRecordEnd);
1167 
1168     MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);
1169     // Subtract out the CSR size since MSVC excludes that and we include it.
1170     OS.AddComment("FrameSize");
1171     OS.emitInt32(FI.FrameSize - FI.CSRSize);
1172     OS.AddComment("Padding");
1173     OS.emitInt32(0);
1174     OS.AddComment("Offset of padding");
1175     OS.emitInt32(0);
1176     OS.AddComment("Bytes of callee saved registers");
1177     OS.emitInt32(FI.CSRSize);
1178     OS.AddComment("Exception handler offset");
1179     OS.emitInt32(0);
1180     OS.AddComment("Exception handler section");
1181     OS.emitInt16(0);
1182     OS.AddComment("Flags (defines frame register)");
1183     OS.emitInt32(uint32_t(FI.FrameProcOpts));
1184     endSymbolRecord(FrameProcEnd);
1185 
1186     emitLocalVariableList(FI, FI.Locals);
1187     emitGlobalVariableList(FI.Globals);
1188     emitLexicalBlockList(FI.ChildBlocks, FI);
1189 
1190     // Emit inlined call site information. Only emit functions inlined directly
1191     // into the parent function. We'll emit the other sites recursively as part
1192     // of their parent inline site.
1193     for (const DILocation *InlinedAt : FI.ChildSites) {
1194       auto I = FI.InlineSites.find(InlinedAt);
1195       assert(I != FI.InlineSites.end() &&
1196              "child site not in function inline site map");
1197       emitInlinedCallSite(FI, InlinedAt, I->second);
1198     }
1199 
1200     for (auto Annot : FI.Annotations) {
1201       MCSymbol *Label = Annot.first;
1202       MDTuple *Strs = cast<MDTuple>(Annot.second);
1203       MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);
1204       OS.emitCOFFSecRel32(Label, /*Offset=*/0);
1205       // FIXME: Make sure we don't overflow the max record size.
1206       OS.emitCOFFSectionIndex(Label);
1207       OS.emitInt16(Strs->getNumOperands());
1208       for (Metadata *MD : Strs->operands()) {
1209         // MDStrings are null terminated, so we can do EmitBytes and get the
1210         // nice .asciz directive.
1211         StringRef Str = cast<MDString>(MD)->getString();
1212         assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
1213         OS.emitBytes(StringRef(Str.data(), Str.size() + 1));
1214       }
1215       endSymbolRecord(AnnotEnd);
1216     }
1217 
1218     for (auto HeapAllocSite : FI.HeapAllocSites) {
1219       const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
1220       const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
1221       const DIType *DITy = std::get<2>(HeapAllocSite);
1222       MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);
1223       OS.AddComment("Call site offset");
1224       OS.emitCOFFSecRel32(BeginLabel, /*Offset=*/0);
1225       OS.AddComment("Call site section index");
1226       OS.emitCOFFSectionIndex(BeginLabel);
1227       OS.AddComment("Call instruction length");
1228       OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
1229       OS.AddComment("Type index");
1230       OS.emitInt32(getCompleteTypeIndex(DITy).getIndex());
1231       endSymbolRecord(HeapAllocEnd);
1232     }
1233 
1234     if (SP != nullptr)
1235       emitDebugInfoForUDTs(LocalUDTs);
1236 
1237     // We're done with this function.
1238     emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1239   }
1240   endCVSubsection(SymbolsEnd);
1241 
1242   // We have an assembler directive that takes care of the whole line table.
1243   OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End);
1244 }
1245 
1246 CodeViewDebug::LocalVarDef
1247 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
1248   LocalVarDef DR;
1249   DR.InMemory = -1;
1250   DR.DataOffset = Offset;
1251   assert(DR.DataOffset == Offset && "truncation");
1252   DR.IsSubfield = 0;
1253   DR.StructOffset = 0;
1254   DR.CVRegister = CVRegister;
1255   return DR;
1256 }
1257 
1258 void CodeViewDebug::collectVariableInfoFromMFTable(
1259     DenseSet<InlinedEntity> &Processed) {
1260   const MachineFunction &MF = *Asm->MF;
1261   const TargetSubtargetInfo &TSI = MF.getSubtarget();
1262   const TargetFrameLowering *TFI = TSI.getFrameLowering();
1263   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1264 
1265   for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) {
1266     if (!VI.Var)
1267       continue;
1268     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1269            "Expected inlined-at fields to agree");
1270 
1271     Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));
1272     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
1273 
1274     // If variable scope is not found then skip this variable.
1275     if (!Scope)
1276       continue;
1277 
1278     // If the variable has an attached offset expression, extract it.
1279     // FIXME: Try to handle DW_OP_deref as well.
1280     int64_t ExprOffset = 0;
1281     bool Deref = false;
1282     if (VI.Expr) {
1283       // If there is one DW_OP_deref element, use offset of 0 and keep going.
1284       if (VI.Expr->getNumElements() == 1 &&
1285           VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref)
1286         Deref = true;
1287       else if (!VI.Expr->extractIfOffset(ExprOffset))
1288         continue;
1289     }
1290 
1291     // Get the frame register used and the offset.
1292     Register FrameReg;
1293     StackOffset FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
1294     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
1295 
1296     assert(!FrameOffset.getScalable() &&
1297            "Frame offsets with a scalable component are not supported");
1298 
1299     // Calculate the label ranges.
1300     LocalVarDef DefRange =
1301         createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset);
1302 
1303     LocalVariable Var;
1304     Var.DIVar = VI.Var;
1305 
1306     for (const InsnRange &Range : Scope->getRanges()) {
1307       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1308       const MCSymbol *End = getLabelAfterInsn(Range.second);
1309       End = End ? End : Asm->getFunctionEnd();
1310       Var.DefRanges[DefRange].emplace_back(Begin, End);
1311     }
1312 
1313     if (Deref)
1314       Var.UseReferenceType = true;
1315 
1316     recordLocalVariable(std::move(Var), Scope);
1317   }
1318 }
1319 
1320 static bool canUseReferenceType(const DbgVariableLocation &Loc) {
1321   return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
1322 }
1323 
1324 static bool needsReferenceType(const DbgVariableLocation &Loc) {
1325   return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
1326 }
1327 
1328 void CodeViewDebug::calculateRanges(
1329     LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) {
1330   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
1331 
1332   // Calculate the definition ranges.
1333   for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) {
1334     const auto &Entry = *I;
1335     if (!Entry.isDbgValue())
1336       continue;
1337     const MachineInstr *DVInst = Entry.getInstr();
1338     assert(DVInst->isDebugValue() && "Invalid History entry");
1339     // FIXME: Find a way to represent constant variables, since they are
1340     // relatively common.
1341     std::optional<DbgVariableLocation> Location =
1342         DbgVariableLocation::extractFromMachineInstruction(*DVInst);
1343     if (!Location)
1344     {
1345       // When we don't have a location this is usually because LLVM has
1346       // transformed it into a constant and we only have an llvm.dbg.value. We
1347       // can't represent these well in CodeView since S_LOCAL only works on
1348       // registers and memory locations. Instead, we will pretend this to be a
1349       // constant value to at least have it show up in the debugger.
1350       auto Op = DVInst->getDebugOperand(0);
1351       if (Op.isImm())
1352         Var.ConstantValue = APSInt(APInt(64, Op.getImm()), false);
1353       continue;
1354     }
1355 
1356     // CodeView can only express variables in register and variables in memory
1357     // at a constant offset from a register. However, for variables passed
1358     // indirectly by pointer, it is common for that pointer to be spilled to a
1359     // stack location. For the special case of one offseted load followed by a
1360     // zero offset load (a pointer spilled to the stack), we change the type of
1361     // the local variable from a value type to a reference type. This tricks the
1362     // debugger into doing the load for us.
1363     if (Var.UseReferenceType) {
1364       // We're using a reference type. Drop the last zero offset load.
1365       if (canUseReferenceType(*Location))
1366         Location->LoadChain.pop_back();
1367       else
1368         continue;
1369     } else if (needsReferenceType(*Location)) {
1370       // This location can't be expressed without switching to a reference type.
1371       // Start over using that.
1372       Var.UseReferenceType = true;
1373       Var.DefRanges.clear();
1374       calculateRanges(Var, Entries);
1375       return;
1376     }
1377 
1378     // We can only handle a register or an offseted load of a register.
1379     if (Location->Register == 0 || Location->LoadChain.size() > 1)
1380       continue;
1381 
1382     LocalVarDef DR;
1383     DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
1384     DR.InMemory = !Location->LoadChain.empty();
1385     DR.DataOffset =
1386         !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
1387     if (Location->FragmentInfo) {
1388       DR.IsSubfield = true;
1389       DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
1390     } else {
1391       DR.IsSubfield = false;
1392       DR.StructOffset = 0;
1393     }
1394 
1395     // Compute the label range.
1396     const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr());
1397     const MCSymbol *End;
1398     if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) {
1399       auto &EndingEntry = Entries[Entry.getEndIndex()];
1400       End = EndingEntry.isDbgValue()
1401                 ? getLabelBeforeInsn(EndingEntry.getInstr())
1402                 : getLabelAfterInsn(EndingEntry.getInstr());
1403     } else
1404       End = Asm->getFunctionEnd();
1405 
1406     // If the last range end is our begin, just extend the last range.
1407     // Otherwise make a new range.
1408     SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =
1409         Var.DefRanges[DR];
1410     if (!R.empty() && R.back().second == Begin)
1411       R.back().second = End;
1412     else
1413       R.emplace_back(Begin, End);
1414 
1415     // FIXME: Do more range combining.
1416   }
1417 }
1418 
1419 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
1420   DenseSet<InlinedEntity> Processed;
1421   // Grab the variable info that was squirreled away in the MMI side-table.
1422   collectVariableInfoFromMFTable(Processed);
1423 
1424   for (const auto &I : DbgValues) {
1425     InlinedEntity IV = I.first;
1426     if (Processed.count(IV))
1427       continue;
1428     const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);
1429     const DILocation *InlinedAt = IV.second;
1430 
1431     // Instruction ranges, specifying where IV is accessible.
1432     const auto &Entries = I.second;
1433 
1434     LexicalScope *Scope = nullptr;
1435     if (InlinedAt)
1436       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
1437     else
1438       Scope = LScopes.findLexicalScope(DIVar->getScope());
1439     // If variable scope is not found then skip this variable.
1440     if (!Scope)
1441       continue;
1442 
1443     LocalVariable Var;
1444     Var.DIVar = DIVar;
1445 
1446     calculateRanges(Var, Entries);
1447     recordLocalVariable(std::move(Var), Scope);
1448   }
1449 }
1450 
1451 void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
1452   const TargetSubtargetInfo &TSI = MF->getSubtarget();
1453   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1454   const MachineFrameInfo &MFI = MF->getFrameInfo();
1455   const Function &GV = MF->getFunction();
1456   auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()});
1457   assert(Insertion.second && "function already has info");
1458   CurFn = Insertion.first->second.get();
1459   CurFn->FuncId = NextFuncId++;
1460   CurFn->Begin = Asm->getFunctionBegin();
1461 
1462   // The S_FRAMEPROC record reports the stack size, and how many bytes of
1463   // callee-saved registers were used. For targets that don't use a PUSH
1464   // instruction (AArch64), this will be zero.
1465   CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();
1466   CurFn->FrameSize = MFI.getStackSize();
1467   CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();
1468   CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF);
1469 
1470   // For this function S_FRAMEPROC record, figure out which codeview register
1471   // will be the frame pointer.
1472   CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.
1473   CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.
1474   if (CurFn->FrameSize > 0) {
1475     if (!TSI.getFrameLowering()->hasFP(*MF)) {
1476       CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1477       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;
1478     } else {
1479       // If there is an FP, parameters are always relative to it.
1480       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;
1481       if (CurFn->HasStackRealignment) {
1482         // If the stack needs realignment, locals are relative to SP or VFRAME.
1483         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1484       } else {
1485         // Otherwise, locals are relative to EBP, and we probably have VLAs or
1486         // other stack adjustments.
1487         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;
1488       }
1489     }
1490   }
1491 
1492   // Compute other frame procedure options.
1493   FrameProcedureOptions FPO = FrameProcedureOptions::None;
1494   if (MFI.hasVarSizedObjects())
1495     FPO |= FrameProcedureOptions::HasAlloca;
1496   if (MF->exposesReturnsTwice())
1497     FPO |= FrameProcedureOptions::HasSetJmp;
1498   // FIXME: Set HasLongJmp if we ever track that info.
1499   if (MF->hasInlineAsm())
1500     FPO |= FrameProcedureOptions::HasInlineAssembly;
1501   if (GV.hasPersonalityFn()) {
1502     if (isAsynchronousEHPersonality(
1503             classifyEHPersonality(GV.getPersonalityFn())))
1504       FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;
1505     else
1506       FPO |= FrameProcedureOptions::HasExceptionHandling;
1507   }
1508   if (GV.hasFnAttribute(Attribute::InlineHint))
1509     FPO |= FrameProcedureOptions::MarkedInline;
1510   if (GV.hasFnAttribute(Attribute::Naked))
1511     FPO |= FrameProcedureOptions::Naked;
1512   if (MFI.hasStackProtectorIndex()) {
1513     FPO |= FrameProcedureOptions::SecurityChecks;
1514     if (GV.hasFnAttribute(Attribute::StackProtectStrong) ||
1515         GV.hasFnAttribute(Attribute::StackProtectReq)) {
1516       FPO |= FrameProcedureOptions::StrictSecurityChecks;
1517     }
1518   } else if (!GV.hasStackProtectorFnAttr()) {
1519     // __declspec(safebuffers) disables stack guards.
1520     FPO |= FrameProcedureOptions::SafeBuffers;
1521   }
1522   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
1523   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
1524   if (Asm->TM.getOptLevel() != CodeGenOpt::None &&
1525       !GV.hasOptSize() && !GV.hasOptNone())
1526     FPO |= FrameProcedureOptions::OptimizedForSpeed;
1527   if (GV.hasProfileData()) {
1528     FPO |= FrameProcedureOptions::ValidProfileCounts;
1529     FPO |= FrameProcedureOptions::ProfileGuidedOptimization;
1530   }
1531   // FIXME: Set GuardCfg when it is implemented.
1532   CurFn->FrameProcOpts = FPO;
1533 
1534   OS.emitCVFuncIdDirective(CurFn->FuncId);
1535 
1536   // Find the end of the function prolog.  First known non-DBG_VALUE and
1537   // non-frame setup location marks the beginning of the function body.
1538   // FIXME: is there a simpler a way to do this? Can we just search
1539   // for the first instruction of the function, not the last of the prolog?
1540   DebugLoc PrologEndLoc;
1541   bool EmptyPrologue = true;
1542   for (const auto &MBB : *MF) {
1543     for (const auto &MI : MBB) {
1544       if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1545           MI.getDebugLoc()) {
1546         PrologEndLoc = MI.getDebugLoc();
1547         break;
1548       } else if (!MI.isMetaInstruction()) {
1549         EmptyPrologue = false;
1550       }
1551     }
1552   }
1553 
1554   // Record beginning of function if we have a non-empty prologue.
1555   if (PrologEndLoc && !EmptyPrologue) {
1556     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
1557     maybeRecordLocation(FnStartDL, MF);
1558   }
1559 
1560   // Find heap alloc sites and emit labels around them.
1561   for (const auto &MBB : *MF) {
1562     for (const auto &MI : MBB) {
1563       if (MI.getHeapAllocMarker()) {
1564         requestLabelBeforeInsn(&MI);
1565         requestLabelAfterInsn(&MI);
1566       }
1567     }
1568   }
1569 }
1570 
1571 static bool shouldEmitUdt(const DIType *T) {
1572   if (!T)
1573     return false;
1574 
1575   // MSVC does not emit UDTs for typedefs that are scoped to classes.
1576   if (T->getTag() == dwarf::DW_TAG_typedef) {
1577     if (DIScope *Scope = T->getScope()) {
1578       switch (Scope->getTag()) {
1579       case dwarf::DW_TAG_structure_type:
1580       case dwarf::DW_TAG_class_type:
1581       case dwarf::DW_TAG_union_type:
1582         return false;
1583       default:
1584           // do nothing.
1585           ;
1586       }
1587     }
1588   }
1589 
1590   while (true) {
1591     if (!T || T->isForwardDecl())
1592       return false;
1593 
1594     const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
1595     if (!DT)
1596       return true;
1597     T = DT->getBaseType();
1598   }
1599   return true;
1600 }
1601 
1602 void CodeViewDebug::addToUDTs(const DIType *Ty) {
1603   // Don't record empty UDTs.
1604   if (Ty->getName().empty())
1605     return;
1606   if (!shouldEmitUdt(Ty))
1607     return;
1608 
1609   SmallVector<StringRef, 5> ParentScopeNames;
1610   const DISubprogram *ClosestSubprogram =
1611       collectParentScopeNames(Ty->getScope(), ParentScopeNames);
1612 
1613   std::string FullyQualifiedName =
1614       formatNestedName(ParentScopeNames, getPrettyScopeName(Ty));
1615 
1616   if (ClosestSubprogram == nullptr) {
1617     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1618   } else if (ClosestSubprogram == CurrentSubprogram) {
1619     LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1620   }
1621 
1622   // TODO: What if the ClosestSubprogram is neither null or the current
1623   // subprogram?  Currently, the UDT just gets dropped on the floor.
1624   //
1625   // The current behavior is not desirable.  To get maximal fidelity, we would
1626   // need to perform all type translation before beginning emission of .debug$S
1627   // and then make LocalUDTs a member of FunctionInfo
1628 }
1629 
1630 TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
1631   // Generic dispatch for lowering an unknown type.
1632   switch (Ty->getTag()) {
1633   case dwarf::DW_TAG_array_type:
1634     return lowerTypeArray(cast<DICompositeType>(Ty));
1635   case dwarf::DW_TAG_typedef:
1636     return lowerTypeAlias(cast<DIDerivedType>(Ty));
1637   case dwarf::DW_TAG_base_type:
1638     return lowerTypeBasic(cast<DIBasicType>(Ty));
1639   case dwarf::DW_TAG_pointer_type:
1640     if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
1641       return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
1642     [[fallthrough]];
1643   case dwarf::DW_TAG_reference_type:
1644   case dwarf::DW_TAG_rvalue_reference_type:
1645     return lowerTypePointer(cast<DIDerivedType>(Ty));
1646   case dwarf::DW_TAG_ptr_to_member_type:
1647     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
1648   case dwarf::DW_TAG_restrict_type:
1649   case dwarf::DW_TAG_const_type:
1650   case dwarf::DW_TAG_volatile_type:
1651   // TODO: add support for DW_TAG_atomic_type here
1652     return lowerTypeModifier(cast<DIDerivedType>(Ty));
1653   case dwarf::DW_TAG_subroutine_type:
1654     if (ClassTy) {
1655       // The member function type of a member function pointer has no
1656       // ThisAdjustment.
1657       return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
1658                                      /*ThisAdjustment=*/0,
1659                                      /*IsStaticMethod=*/false);
1660     }
1661     return lowerTypeFunction(cast<DISubroutineType>(Ty));
1662   case dwarf::DW_TAG_enumeration_type:
1663     return lowerTypeEnum(cast<DICompositeType>(Ty));
1664   case dwarf::DW_TAG_class_type:
1665   case dwarf::DW_TAG_structure_type:
1666     return lowerTypeClass(cast<DICompositeType>(Ty));
1667   case dwarf::DW_TAG_union_type:
1668     return lowerTypeUnion(cast<DICompositeType>(Ty));
1669   case dwarf::DW_TAG_string_type:
1670     return lowerTypeString(cast<DIStringType>(Ty));
1671   case dwarf::DW_TAG_unspecified_type:
1672     if (Ty->getName() == "decltype(nullptr)")
1673       return TypeIndex::NullptrT();
1674     return TypeIndex::None();
1675   default:
1676     // Use the null type index.
1677     return TypeIndex();
1678   }
1679 }
1680 
1681 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
1682   TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType());
1683   StringRef TypeName = Ty->getName();
1684 
1685   addToUDTs(Ty);
1686 
1687   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
1688       TypeName == "HRESULT")
1689     return TypeIndex(SimpleTypeKind::HResult);
1690   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
1691       TypeName == "wchar_t")
1692     return TypeIndex(SimpleTypeKind::WideCharacter);
1693 
1694   return UnderlyingTypeIndex;
1695 }
1696 
1697 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
1698   const DIType *ElementType = Ty->getBaseType();
1699   TypeIndex ElementTypeIndex = getTypeIndex(ElementType);
1700   // IndexType is size_t, which depends on the bitness of the target.
1701   TypeIndex IndexType = getPointerSizeInBytes() == 8
1702                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1703                             : TypeIndex(SimpleTypeKind::UInt32Long);
1704 
1705   uint64_t ElementSize = getBaseTypeSize(ElementType) / 8;
1706 
1707   // Add subranges to array type.
1708   DINodeArray Elements = Ty->getElements();
1709   for (int i = Elements.size() - 1; i >= 0; --i) {
1710     const DINode *Element = Elements[i];
1711     assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1712 
1713     const DISubrange *Subrange = cast<DISubrange>(Element);
1714     int64_t Count = -1;
1715 
1716     // If Subrange has a Count field, use it.
1717     // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),
1718     // where lowerbound is from the LowerBound field of the Subrange,
1719     // or the language default lowerbound if that field is unspecified.
1720     if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt *>())
1721       Count = CI->getSExtValue();
1722     else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt *>()) {
1723       // Fortran uses 1 as the default lowerbound; other languages use 0.
1724       int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;
1725       auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>();
1726       Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;
1727       Count = UI->getSExtValue() - Lowerbound + 1;
1728     }
1729 
1730     // Forward declarations of arrays without a size and VLAs use a count of -1.
1731     // Emit a count of zero in these cases to match what MSVC does for arrays
1732     // without a size. MSVC doesn't support VLAs, so it's not clear what we
1733     // should do for them even if we could distinguish them.
1734     if (Count == -1)
1735       Count = 0;
1736 
1737     // Update the element size and element type index for subsequent subranges.
1738     ElementSize *= Count;
1739 
1740     // If this is the outermost array, use the size from the array. It will be
1741     // more accurate if we had a VLA or an incomplete element type size.
1742     uint64_t ArraySize =
1743         (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
1744 
1745     StringRef Name = (i == 0) ? Ty->getName() : "";
1746     ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
1747     ElementTypeIndex = TypeTable.writeLeafType(AR);
1748   }
1749 
1750   return ElementTypeIndex;
1751 }
1752 
1753 // This function lowers a Fortran character type (DIStringType).
1754 // Note that it handles only the character*n variant (using SizeInBits
1755 // field in DIString to describe the type size) at the moment.
1756 // Other variants (leveraging the StringLength and StringLengthExp
1757 // fields in DIStringType) remain TBD.
1758 TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {
1759   TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);
1760   uint64_t ArraySize = Ty->getSizeInBits() >> 3;
1761   StringRef Name = Ty->getName();
1762   // IndexType is size_t, which depends on the bitness of the target.
1763   TypeIndex IndexType = getPointerSizeInBytes() == 8
1764                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1765                             : TypeIndex(SimpleTypeKind::UInt32Long);
1766 
1767   // Create a type of character array of ArraySize.
1768   ArrayRecord AR(CharType, IndexType, ArraySize, Name);
1769 
1770   return TypeTable.writeLeafType(AR);
1771 }
1772 
1773 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1774   TypeIndex Index;
1775   dwarf::TypeKind Kind;
1776   uint32_t ByteSize;
1777 
1778   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1779   ByteSize = Ty->getSizeInBits() / 8;
1780 
1781   SimpleTypeKind STK = SimpleTypeKind::None;
1782   switch (Kind) {
1783   case dwarf::DW_ATE_address:
1784     // FIXME: Translate
1785     break;
1786   case dwarf::DW_ATE_boolean:
1787     switch (ByteSize) {
1788     case 1:  STK = SimpleTypeKind::Boolean8;   break;
1789     case 2:  STK = SimpleTypeKind::Boolean16;  break;
1790     case 4:  STK = SimpleTypeKind::Boolean32;  break;
1791     case 8:  STK = SimpleTypeKind::Boolean64;  break;
1792     case 16: STK = SimpleTypeKind::Boolean128; break;
1793     }
1794     break;
1795   case dwarf::DW_ATE_complex_float:
1796     switch (ByteSize) {
1797     case 2:  STK = SimpleTypeKind::Complex16;  break;
1798     case 4:  STK = SimpleTypeKind::Complex32;  break;
1799     case 8:  STK = SimpleTypeKind::Complex64;  break;
1800     case 10: STK = SimpleTypeKind::Complex80;  break;
1801     case 16: STK = SimpleTypeKind::Complex128; break;
1802     }
1803     break;
1804   case dwarf::DW_ATE_float:
1805     switch (ByteSize) {
1806     case 2:  STK = SimpleTypeKind::Float16;  break;
1807     case 4:  STK = SimpleTypeKind::Float32;  break;
1808     case 6:  STK = SimpleTypeKind::Float48;  break;
1809     case 8:  STK = SimpleTypeKind::Float64;  break;
1810     case 10: STK = SimpleTypeKind::Float80;  break;
1811     case 16: STK = SimpleTypeKind::Float128; break;
1812     }
1813     break;
1814   case dwarf::DW_ATE_signed:
1815     switch (ByteSize) {
1816     case 1:  STK = SimpleTypeKind::SignedCharacter; break;
1817     case 2:  STK = SimpleTypeKind::Int16Short;      break;
1818     case 4:  STK = SimpleTypeKind::Int32;           break;
1819     case 8:  STK = SimpleTypeKind::Int64Quad;       break;
1820     case 16: STK = SimpleTypeKind::Int128Oct;       break;
1821     }
1822     break;
1823   case dwarf::DW_ATE_unsigned:
1824     switch (ByteSize) {
1825     case 1:  STK = SimpleTypeKind::UnsignedCharacter; break;
1826     case 2:  STK = SimpleTypeKind::UInt16Short;       break;
1827     case 4:  STK = SimpleTypeKind::UInt32;            break;
1828     case 8:  STK = SimpleTypeKind::UInt64Quad;        break;
1829     case 16: STK = SimpleTypeKind::UInt128Oct;        break;
1830     }
1831     break;
1832   case dwarf::DW_ATE_UTF:
1833     switch (ByteSize) {
1834     case 1: STK = SimpleTypeKind::Character8; break;
1835     case 2: STK = SimpleTypeKind::Character16; break;
1836     case 4: STK = SimpleTypeKind::Character32; break;
1837     }
1838     break;
1839   case dwarf::DW_ATE_signed_char:
1840     if (ByteSize == 1)
1841       STK = SimpleTypeKind::SignedCharacter;
1842     break;
1843   case dwarf::DW_ATE_unsigned_char:
1844     if (ByteSize == 1)
1845       STK = SimpleTypeKind::UnsignedCharacter;
1846     break;
1847   default:
1848     break;
1849   }
1850 
1851   // Apply some fixups based on the source-level type name.
1852   // Include some amount of canonicalization from an old naming scheme Clang
1853   // used to use for integer types (in an outdated effort to be compatible with
1854   // GCC's debug info/GDB's behavior, which has since been addressed).
1855   if (STK == SimpleTypeKind::Int32 &&
1856       (Ty->getName() == "long int" || Ty->getName() == "long"))
1857     STK = SimpleTypeKind::Int32Long;
1858   if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" ||
1859                                         Ty->getName() == "unsigned long"))
1860     STK = SimpleTypeKind::UInt32Long;
1861   if (STK == SimpleTypeKind::UInt16Short &&
1862       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1863     STK = SimpleTypeKind::WideCharacter;
1864   if ((STK == SimpleTypeKind::SignedCharacter ||
1865        STK == SimpleTypeKind::UnsignedCharacter) &&
1866       Ty->getName() == "char")
1867     STK = SimpleTypeKind::NarrowCharacter;
1868 
1869   return TypeIndex(STK);
1870 }
1871 
1872 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,
1873                                           PointerOptions PO) {
1874   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1875 
1876   // Pointers to simple types without any options can use SimpleTypeMode, rather
1877   // than having a dedicated pointer type record.
1878   if (PointeeTI.isSimple() && PO == PointerOptions::None &&
1879       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1880       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1881     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1882                               ? SimpleTypeMode::NearPointer64
1883                               : SimpleTypeMode::NearPointer32;
1884     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1885   }
1886 
1887   PointerKind PK =
1888       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1889   PointerMode PM = PointerMode::Pointer;
1890   switch (Ty->getTag()) {
1891   default: llvm_unreachable("not a pointer tag type");
1892   case dwarf::DW_TAG_pointer_type:
1893     PM = PointerMode::Pointer;
1894     break;
1895   case dwarf::DW_TAG_reference_type:
1896     PM = PointerMode::LValueReference;
1897     break;
1898   case dwarf::DW_TAG_rvalue_reference_type:
1899     PM = PointerMode::RValueReference;
1900     break;
1901   }
1902 
1903   if (Ty->isObjectPointer())
1904     PO |= PointerOptions::Const;
1905 
1906   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1907   return TypeTable.writeLeafType(PR);
1908 }
1909 
1910 static PointerToMemberRepresentation
1911 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1912   // SizeInBytes being zero generally implies that the member pointer type was
1913   // incomplete, which can happen if it is part of a function prototype. In this
1914   // case, use the unknown model instead of the general model.
1915   if (IsPMF) {
1916     switch (Flags & DINode::FlagPtrToMemberRep) {
1917     case 0:
1918       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1919                               : PointerToMemberRepresentation::GeneralFunction;
1920     case DINode::FlagSingleInheritance:
1921       return PointerToMemberRepresentation::SingleInheritanceFunction;
1922     case DINode::FlagMultipleInheritance:
1923       return PointerToMemberRepresentation::MultipleInheritanceFunction;
1924     case DINode::FlagVirtualInheritance:
1925       return PointerToMemberRepresentation::VirtualInheritanceFunction;
1926     }
1927   } else {
1928     switch (Flags & DINode::FlagPtrToMemberRep) {
1929     case 0:
1930       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1931                               : PointerToMemberRepresentation::GeneralData;
1932     case DINode::FlagSingleInheritance:
1933       return PointerToMemberRepresentation::SingleInheritanceData;
1934     case DINode::FlagMultipleInheritance:
1935       return PointerToMemberRepresentation::MultipleInheritanceData;
1936     case DINode::FlagVirtualInheritance:
1937       return PointerToMemberRepresentation::VirtualInheritanceData;
1938     }
1939   }
1940   llvm_unreachable("invalid ptr to member representation");
1941 }
1942 
1943 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,
1944                                                 PointerOptions PO) {
1945   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1946   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1947   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1948   TypeIndex PointeeTI =
1949       getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr);
1950   PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
1951                                                 : PointerKind::Near32;
1952   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1953                          : PointerMode::PointerToDataMember;
1954 
1955   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1956   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1957   MemberPointerInfo MPI(
1958       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1959   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1960   return TypeTable.writeLeafType(PR);
1961 }
1962 
1963 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1964 /// have a translation, use the NearC convention.
1965 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1966   switch (DwarfCC) {
1967   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1968   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1969   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1970   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1971   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1972   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1973   }
1974   return CallingConvention::NearC;
1975 }
1976 
1977 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1978   ModifierOptions Mods = ModifierOptions::None;
1979   PointerOptions PO = PointerOptions::None;
1980   bool IsModifier = true;
1981   const DIType *BaseTy = Ty;
1982   while (IsModifier && BaseTy) {
1983     // FIXME: Need to add DWARF tags for __unaligned and _Atomic
1984     switch (BaseTy->getTag()) {
1985     case dwarf::DW_TAG_const_type:
1986       Mods |= ModifierOptions::Const;
1987       PO |= PointerOptions::Const;
1988       break;
1989     case dwarf::DW_TAG_volatile_type:
1990       Mods |= ModifierOptions::Volatile;
1991       PO |= PointerOptions::Volatile;
1992       break;
1993     case dwarf::DW_TAG_restrict_type:
1994       // Only pointer types be marked with __restrict. There is no known flag
1995       // for __restrict in LF_MODIFIER records.
1996       PO |= PointerOptions::Restrict;
1997       break;
1998     default:
1999       IsModifier = false;
2000       break;
2001     }
2002     if (IsModifier)
2003       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType();
2004   }
2005 
2006   // Check if the inner type will use an LF_POINTER record. If so, the
2007   // qualifiers will go in the LF_POINTER record. This comes up for types like
2008   // 'int *const' and 'int *__restrict', not the more common cases like 'const
2009   // char *'.
2010   if (BaseTy) {
2011     switch (BaseTy->getTag()) {
2012     case dwarf::DW_TAG_pointer_type:
2013     case dwarf::DW_TAG_reference_type:
2014     case dwarf::DW_TAG_rvalue_reference_type:
2015       return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);
2016     case dwarf::DW_TAG_ptr_to_member_type:
2017       return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);
2018     default:
2019       break;
2020     }
2021   }
2022 
2023   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
2024 
2025   // Return the base type index if there aren't any modifiers. For example, the
2026   // metadata could contain restrict wrappers around non-pointer types.
2027   if (Mods == ModifierOptions::None)
2028     return ModifiedTI;
2029 
2030   ModifierRecord MR(ModifiedTI, Mods);
2031   return TypeTable.writeLeafType(MR);
2032 }
2033 
2034 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
2035   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
2036   for (const DIType *ArgType : Ty->getTypeArray())
2037     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType));
2038 
2039   // MSVC uses type none for variadic argument.
2040   if (ReturnAndArgTypeIndices.size() > 1 &&
2041       ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {
2042     ReturnAndArgTypeIndices.back() = TypeIndex::None();
2043   }
2044   TypeIndex ReturnTypeIndex = TypeIndex::Void();
2045   ArrayRef<TypeIndex> ArgTypeIndices = std::nullopt;
2046   if (!ReturnAndArgTypeIndices.empty()) {
2047     auto ReturnAndArgTypesRef = ArrayRef(ReturnAndArgTypeIndices);
2048     ReturnTypeIndex = ReturnAndArgTypesRef.front();
2049     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
2050   }
2051 
2052   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2053   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2054 
2055   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
2056 
2057   FunctionOptions FO = getFunctionOptions(Ty);
2058   ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),
2059                             ArgListIndex);
2060   return TypeTable.writeLeafType(Procedure);
2061 }
2062 
2063 TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
2064                                                  const DIType *ClassTy,
2065                                                  int ThisAdjustment,
2066                                                  bool IsStaticMethod,
2067                                                  FunctionOptions FO) {
2068   // Lower the containing class type.
2069   TypeIndex ClassType = getTypeIndex(ClassTy);
2070 
2071   DITypeRefArray ReturnAndArgs = Ty->getTypeArray();
2072 
2073   unsigned Index = 0;
2074   SmallVector<TypeIndex, 8> ArgTypeIndices;
2075   TypeIndex ReturnTypeIndex = TypeIndex::Void();
2076   if (ReturnAndArgs.size() > Index) {
2077     ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);
2078   }
2079 
2080   // If the first argument is a pointer type and this isn't a static method,
2081   // treat it as the special 'this' parameter, which is encoded separately from
2082   // the arguments.
2083   TypeIndex ThisTypeIndex;
2084   if (!IsStaticMethod && ReturnAndArgs.size() > Index) {
2085     if (const DIDerivedType *PtrTy =
2086             dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) {
2087       if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
2088         ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);
2089         Index++;
2090       }
2091     }
2092   }
2093 
2094   while (Index < ReturnAndArgs.size())
2095     ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));
2096 
2097   // MSVC uses type none for variadic argument.
2098   if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())
2099     ArgTypeIndices.back() = TypeIndex::None();
2100 
2101   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2102   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2103 
2104   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
2105 
2106   MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,
2107                            ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);
2108   return TypeTable.writeLeafType(MFR);
2109 }
2110 
2111 TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
2112   unsigned VSlotCount =
2113       Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
2114   SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
2115 
2116   VFTableShapeRecord VFTSR(Slots);
2117   return TypeTable.writeLeafType(VFTSR);
2118 }
2119 
2120 static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
2121   switch (Flags & DINode::FlagAccessibility) {
2122   case DINode::FlagPrivate:   return MemberAccess::Private;
2123   case DINode::FlagPublic:    return MemberAccess::Public;
2124   case DINode::FlagProtected: return MemberAccess::Protected;
2125   case 0:
2126     // If there was no explicit access control, provide the default for the tag.
2127     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
2128                                                  : MemberAccess::Public;
2129   }
2130   llvm_unreachable("access flags are exclusive");
2131 }
2132 
2133 static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
2134   if (SP->isArtificial())
2135     return MethodOptions::CompilerGenerated;
2136 
2137   // FIXME: Handle other MethodOptions.
2138 
2139   return MethodOptions::None;
2140 }
2141 
2142 static MethodKind translateMethodKindFlags(const DISubprogram *SP,
2143                                            bool Introduced) {
2144   if (SP->getFlags() & DINode::FlagStaticMember)
2145     return MethodKind::Static;
2146 
2147   switch (SP->getVirtuality()) {
2148   case dwarf::DW_VIRTUALITY_none:
2149     break;
2150   case dwarf::DW_VIRTUALITY_virtual:
2151     return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
2152   case dwarf::DW_VIRTUALITY_pure_virtual:
2153     return Introduced ? MethodKind::PureIntroducingVirtual
2154                       : MethodKind::PureVirtual;
2155   default:
2156     llvm_unreachable("unhandled virtuality case");
2157   }
2158 
2159   return MethodKind::Vanilla;
2160 }
2161 
2162 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
2163   switch (Ty->getTag()) {
2164   case dwarf::DW_TAG_class_type:
2165     return TypeRecordKind::Class;
2166   case dwarf::DW_TAG_structure_type:
2167     return TypeRecordKind::Struct;
2168   default:
2169     llvm_unreachable("unexpected tag");
2170   }
2171 }
2172 
2173 /// Return ClassOptions that should be present on both the forward declaration
2174 /// and the defintion of a tag type.
2175 static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
2176   ClassOptions CO = ClassOptions::None;
2177 
2178   // MSVC always sets this flag, even for local types. Clang doesn't always
2179   // appear to give every type a linkage name, which may be problematic for us.
2180   // FIXME: Investigate the consequences of not following them here.
2181   if (!Ty->getIdentifier().empty())
2182     CO |= ClassOptions::HasUniqueName;
2183 
2184   // Put the Nested flag on a type if it appears immediately inside a tag type.
2185   // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
2186   // here. That flag is only set on definitions, and not forward declarations.
2187   const DIScope *ImmediateScope = Ty->getScope();
2188   if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
2189     CO |= ClassOptions::Nested;
2190 
2191   // Put the Scoped flag on function-local types. MSVC puts this flag for enum
2192   // type only when it has an immediate function scope. Clang never puts enums
2193   // inside DILexicalBlock scopes. Enum types, as generated by clang, are
2194   // always in function, class, or file scopes.
2195   if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {
2196     if (ImmediateScope && isa<DISubprogram>(ImmediateScope))
2197       CO |= ClassOptions::Scoped;
2198   } else {
2199     for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
2200          Scope = Scope->getScope()) {
2201       if (isa<DISubprogram>(Scope)) {
2202         CO |= ClassOptions::Scoped;
2203         break;
2204       }
2205     }
2206   }
2207 
2208   return CO;
2209 }
2210 
2211 void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {
2212   switch (Ty->getTag()) {
2213   case dwarf::DW_TAG_class_type:
2214   case dwarf::DW_TAG_structure_type:
2215   case dwarf::DW_TAG_union_type:
2216   case dwarf::DW_TAG_enumeration_type:
2217     break;
2218   default:
2219     return;
2220   }
2221 
2222   if (const auto *File = Ty->getFile()) {
2223     StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
2224     TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
2225 
2226     UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());
2227     TypeTable.writeLeafType(USLR);
2228   }
2229 }
2230 
2231 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
2232   ClassOptions CO = getCommonClassOptions(Ty);
2233   TypeIndex FTI;
2234   unsigned EnumeratorCount = 0;
2235 
2236   if (Ty->isForwardDecl()) {
2237     CO |= ClassOptions::ForwardReference;
2238   } else {
2239     ContinuationRecordBuilder ContinuationBuilder;
2240     ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2241     for (const DINode *Element : Ty->getElements()) {
2242       // We assume that the frontend provides all members in source declaration
2243       // order, which is what MSVC does.
2244       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
2245         // FIXME: Is it correct to always emit these as unsigned here?
2246         EnumeratorRecord ER(MemberAccess::Public,
2247                             APSInt(Enumerator->getValue(), true),
2248                             Enumerator->getName());
2249         ContinuationBuilder.writeMemberType(ER);
2250         EnumeratorCount++;
2251       }
2252     }
2253     FTI = TypeTable.insertRecord(ContinuationBuilder);
2254   }
2255 
2256   std::string FullName = getFullyQualifiedName(Ty);
2257 
2258   EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
2259                 getTypeIndex(Ty->getBaseType()));
2260   TypeIndex EnumTI = TypeTable.writeLeafType(ER);
2261 
2262   addUDTSrcLine(Ty, EnumTI);
2263 
2264   return EnumTI;
2265 }
2266 
2267 //===----------------------------------------------------------------------===//
2268 // ClassInfo
2269 //===----------------------------------------------------------------------===//
2270 
2271 struct llvm::ClassInfo {
2272   struct MemberInfo {
2273     const DIDerivedType *MemberTypeNode;
2274     uint64_t BaseOffset;
2275   };
2276   // [MemberInfo]
2277   using MemberList = std::vector<MemberInfo>;
2278 
2279   using MethodsList = TinyPtrVector<const DISubprogram *>;
2280   // MethodName -> MethodsList
2281   using MethodsMap = MapVector<MDString *, MethodsList>;
2282 
2283   /// Base classes.
2284   std::vector<const DIDerivedType *> Inheritance;
2285 
2286   /// Direct members.
2287   MemberList Members;
2288   // Direct overloaded methods gathered by name.
2289   MethodsMap Methods;
2290 
2291   TypeIndex VShapeTI;
2292 
2293   std::vector<const DIType *> NestedTypes;
2294 };
2295 
2296 void CodeViewDebug::clear() {
2297   assert(CurFn == nullptr);
2298   FileIdMap.clear();
2299   FnDebugInfo.clear();
2300   FileToFilepathMap.clear();
2301   LocalUDTs.clear();
2302   GlobalUDTs.clear();
2303   TypeIndices.clear();
2304   CompleteTypeIndices.clear();
2305   ScopeGlobals.clear();
2306   CVGlobalVariableOffsets.clear();
2307 }
2308 
2309 void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
2310                                       const DIDerivedType *DDTy) {
2311   if (!DDTy->getName().empty()) {
2312     Info.Members.push_back({DDTy, 0});
2313 
2314     // Collect static const data members with values.
2315     if ((DDTy->getFlags() & DINode::FlagStaticMember) ==
2316         DINode::FlagStaticMember) {
2317       if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) ||
2318                                   isa<ConstantFP>(DDTy->getConstant())))
2319         StaticConstMembers.push_back(DDTy);
2320     }
2321 
2322     return;
2323   }
2324 
2325   // An unnamed member may represent a nested struct or union. Attempt to
2326   // interpret the unnamed member as a DICompositeType possibly wrapped in
2327   // qualifier types. Add all the indirect fields to the current record if that
2328   // succeeds, and drop the member if that fails.
2329   assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
2330   uint64_t Offset = DDTy->getOffsetInBits();
2331   const DIType *Ty = DDTy->getBaseType();
2332   bool FullyResolved = false;
2333   while (!FullyResolved) {
2334     switch (Ty->getTag()) {
2335     case dwarf::DW_TAG_const_type:
2336     case dwarf::DW_TAG_volatile_type:
2337       // FIXME: we should apply the qualifier types to the indirect fields
2338       // rather than dropping them.
2339       Ty = cast<DIDerivedType>(Ty)->getBaseType();
2340       break;
2341     default:
2342       FullyResolved = true;
2343       break;
2344     }
2345   }
2346 
2347   const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);
2348   if (!DCTy)
2349     return;
2350 
2351   ClassInfo NestedInfo = collectClassInfo(DCTy);
2352   for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
2353     Info.Members.push_back(
2354         {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
2355 }
2356 
2357 ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
2358   ClassInfo Info;
2359   // Add elements to structure type.
2360   DINodeArray Elements = Ty->getElements();
2361   for (auto *Element : Elements) {
2362     // We assume that the frontend provides all members in source declaration
2363     // order, which is what MSVC does.
2364     if (!Element)
2365       continue;
2366     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
2367       Info.Methods[SP->getRawName()].push_back(SP);
2368     } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
2369       if (DDTy->getTag() == dwarf::DW_TAG_member) {
2370         collectMemberInfo(Info, DDTy);
2371       } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
2372         Info.Inheritance.push_back(DDTy);
2373       } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
2374                  DDTy->getName() == "__vtbl_ptr_type") {
2375         Info.VShapeTI = getTypeIndex(DDTy);
2376       } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
2377         Info.NestedTypes.push_back(DDTy);
2378       } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
2379         // Ignore friend members. It appears that MSVC emitted info about
2380         // friends in the past, but modern versions do not.
2381       }
2382     } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
2383       Info.NestedTypes.push_back(Composite);
2384     }
2385     // Skip other unrecognized kinds of elements.
2386   }
2387   return Info;
2388 }
2389 
2390 static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) {
2391   // This routine is used by lowerTypeClass and lowerTypeUnion to determine
2392   // if a complete type should be emitted instead of a forward reference.
2393   return Ty->getName().empty() && Ty->getIdentifier().empty() &&
2394       !Ty->isForwardDecl();
2395 }
2396 
2397 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
2398   // Emit the complete type for unnamed structs.  C++ classes with methods
2399   // which have a circular reference back to the class type are expected to
2400   // be named by the front-end and should not be "unnamed".  C unnamed
2401   // structs should not have circular references.
2402   if (shouldAlwaysEmitCompleteClassType(Ty)) {
2403     // If this unnamed complete type is already in the process of being defined
2404     // then the description of the type is malformed and cannot be emitted
2405     // into CodeView correctly so report a fatal error.
2406     auto I = CompleteTypeIndices.find(Ty);
2407     if (I != CompleteTypeIndices.end() && I->second == TypeIndex())
2408       report_fatal_error("cannot debug circular reference to unnamed type");
2409     return getCompleteTypeIndex(Ty);
2410   }
2411 
2412   // First, construct the forward decl.  Don't look into Ty to compute the
2413   // forward decl options, since it might not be available in all TUs.
2414   TypeRecordKind Kind = getRecordKind(Ty);
2415   ClassOptions CO =
2416       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2417   std::string FullName = getFullyQualifiedName(Ty);
2418   ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
2419                  FullName, Ty->getIdentifier());
2420   TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
2421   if (!Ty->isForwardDecl())
2422     DeferredCompleteTypes.push_back(Ty);
2423   return FwdDeclTI;
2424 }
2425 
2426 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
2427   // Construct the field list and complete type record.
2428   TypeRecordKind Kind = getRecordKind(Ty);
2429   ClassOptions CO = getCommonClassOptions(Ty);
2430   TypeIndex FieldTI;
2431   TypeIndex VShapeTI;
2432   unsigned FieldCount;
2433   bool ContainsNestedClass;
2434   std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
2435       lowerRecordFieldList(Ty);
2436 
2437   if (ContainsNestedClass)
2438     CO |= ClassOptions::ContainsNestedClass;
2439 
2440   // MSVC appears to set this flag by searching any destructor or method with
2441   // FunctionOptions::Constructor among the emitted members. Clang AST has all
2442   // the members, however special member functions are not yet emitted into
2443   // debug information. For now checking a class's non-triviality seems enough.
2444   // FIXME: not true for a nested unnamed struct.
2445   if (isNonTrivial(Ty))
2446     CO |= ClassOptions::HasConstructorOrDestructor;
2447 
2448   std::string FullName = getFullyQualifiedName(Ty);
2449 
2450   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2451 
2452   ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
2453                  SizeInBytes, FullName, Ty->getIdentifier());
2454   TypeIndex ClassTI = TypeTable.writeLeafType(CR);
2455 
2456   addUDTSrcLine(Ty, ClassTI);
2457 
2458   addToUDTs(Ty);
2459 
2460   return ClassTI;
2461 }
2462 
2463 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
2464   // Emit the complete type for unnamed unions.
2465   if (shouldAlwaysEmitCompleteClassType(Ty))
2466     return getCompleteTypeIndex(Ty);
2467 
2468   ClassOptions CO =
2469       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2470   std::string FullName = getFullyQualifiedName(Ty);
2471   UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
2472   TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
2473   if (!Ty->isForwardDecl())
2474     DeferredCompleteTypes.push_back(Ty);
2475   return FwdDeclTI;
2476 }
2477 
2478 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
2479   ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
2480   TypeIndex FieldTI;
2481   unsigned FieldCount;
2482   bool ContainsNestedClass;
2483   std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
2484       lowerRecordFieldList(Ty);
2485 
2486   if (ContainsNestedClass)
2487     CO |= ClassOptions::ContainsNestedClass;
2488 
2489   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2490   std::string FullName = getFullyQualifiedName(Ty);
2491 
2492   UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
2493                  Ty->getIdentifier());
2494   TypeIndex UnionTI = TypeTable.writeLeafType(UR);
2495 
2496   addUDTSrcLine(Ty, UnionTI);
2497 
2498   addToUDTs(Ty);
2499 
2500   return UnionTI;
2501 }
2502 
2503 std::tuple<TypeIndex, TypeIndex, unsigned, bool>
2504 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
2505   // Manually count members. MSVC appears to count everything that generates a
2506   // field list record. Each individual overload in a method overload group
2507   // contributes to this count, even though the overload group is a single field
2508   // list record.
2509   unsigned MemberCount = 0;
2510   ClassInfo Info = collectClassInfo(Ty);
2511   ContinuationRecordBuilder ContinuationBuilder;
2512   ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2513 
2514   // Create base classes.
2515   for (const DIDerivedType *I : Info.Inheritance) {
2516     if (I->getFlags() & DINode::FlagVirtual) {
2517       // Virtual base.
2518       unsigned VBPtrOffset = I->getVBPtrOffset();
2519       // FIXME: Despite the accessor name, the offset is really in bytes.
2520       unsigned VBTableIndex = I->getOffsetInBits() / 4;
2521       auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
2522                             ? TypeRecordKind::IndirectVirtualBaseClass
2523                             : TypeRecordKind::VirtualBaseClass;
2524       VirtualBaseClassRecord VBCR(
2525           RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
2526           getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
2527           VBTableIndex);
2528 
2529       ContinuationBuilder.writeMemberType(VBCR);
2530       MemberCount++;
2531     } else {
2532       assert(I->getOffsetInBits() % 8 == 0 &&
2533              "bases must be on byte boundaries");
2534       BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
2535                           getTypeIndex(I->getBaseType()),
2536                           I->getOffsetInBits() / 8);
2537       ContinuationBuilder.writeMemberType(BCR);
2538       MemberCount++;
2539     }
2540   }
2541 
2542   // Create members.
2543   for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
2544     const DIDerivedType *Member = MemberInfo.MemberTypeNode;
2545     TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
2546     StringRef MemberName = Member->getName();
2547     MemberAccess Access =
2548         translateAccessFlags(Ty->getTag(), Member->getFlags());
2549 
2550     if (Member->isStaticMember()) {
2551       StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
2552       ContinuationBuilder.writeMemberType(SDMR);
2553       MemberCount++;
2554       continue;
2555     }
2556 
2557     // Virtual function pointer member.
2558     if ((Member->getFlags() & DINode::FlagArtificial) &&
2559         Member->getName().startswith("_vptr$")) {
2560       VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
2561       ContinuationBuilder.writeMemberType(VFPR);
2562       MemberCount++;
2563       continue;
2564     }
2565 
2566     // Data member.
2567     uint64_t MemberOffsetInBits =
2568         Member->getOffsetInBits() + MemberInfo.BaseOffset;
2569     if (Member->isBitField()) {
2570       uint64_t StartBitOffset = MemberOffsetInBits;
2571       if (const auto *CI =
2572               dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
2573         MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
2574       }
2575       StartBitOffset -= MemberOffsetInBits;
2576       BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
2577                          StartBitOffset);
2578       MemberBaseType = TypeTable.writeLeafType(BFR);
2579     }
2580     uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
2581     DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
2582                          MemberName);
2583     ContinuationBuilder.writeMemberType(DMR);
2584     MemberCount++;
2585   }
2586 
2587   // Create methods
2588   for (auto &MethodItr : Info.Methods) {
2589     StringRef Name = MethodItr.first->getString();
2590 
2591     std::vector<OneMethodRecord> Methods;
2592     for (const DISubprogram *SP : MethodItr.second) {
2593       TypeIndex MethodType = getMemberFunctionType(SP, Ty);
2594       bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
2595 
2596       unsigned VFTableOffset = -1;
2597       if (Introduced)
2598         VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
2599 
2600       Methods.push_back(OneMethodRecord(
2601           MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
2602           translateMethodKindFlags(SP, Introduced),
2603           translateMethodOptionFlags(SP), VFTableOffset, Name));
2604       MemberCount++;
2605     }
2606     assert(!Methods.empty() && "Empty methods map entry");
2607     if (Methods.size() == 1)
2608       ContinuationBuilder.writeMemberType(Methods[0]);
2609     else {
2610       // FIXME: Make this use its own ContinuationBuilder so that
2611       // MethodOverloadList can be split correctly.
2612       MethodOverloadListRecord MOLR(Methods);
2613       TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
2614 
2615       OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
2616       ContinuationBuilder.writeMemberType(OMR);
2617     }
2618   }
2619 
2620   // Create nested classes.
2621   for (const DIType *Nested : Info.NestedTypes) {
2622     NestedTypeRecord R(getTypeIndex(Nested), Nested->getName());
2623     ContinuationBuilder.writeMemberType(R);
2624     MemberCount++;
2625   }
2626 
2627   TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
2628   return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
2629                          !Info.NestedTypes.empty());
2630 }
2631 
2632 TypeIndex CodeViewDebug::getVBPTypeIndex() {
2633   if (!VBPType.getIndex()) {
2634     // Make a 'const int *' type.
2635     ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
2636     TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
2637 
2638     PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
2639                                                   : PointerKind::Near32;
2640     PointerMode PM = PointerMode::Pointer;
2641     PointerOptions PO = PointerOptions::None;
2642     PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
2643     VBPType = TypeTable.writeLeafType(PR);
2644   }
2645 
2646   return VBPType;
2647 }
2648 
2649 TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) {
2650   // The null DIType is the void type. Don't try to hash it.
2651   if (!Ty)
2652     return TypeIndex::Void();
2653 
2654   // Check if we've already translated this type. Don't try to do a
2655   // get-or-create style insertion that caches the hash lookup across the
2656   // lowerType call. It will update the TypeIndices map.
2657   auto I = TypeIndices.find({Ty, ClassTy});
2658   if (I != TypeIndices.end())
2659     return I->second;
2660 
2661   TypeLoweringScope S(*this);
2662   TypeIndex TI = lowerType(Ty, ClassTy);
2663   return recordTypeIndexForDINode(Ty, TI, ClassTy);
2664 }
2665 
2666 codeview::TypeIndex
2667 CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
2668                                       const DISubroutineType *SubroutineTy) {
2669   assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&
2670          "this type must be a pointer type");
2671 
2672   PointerOptions Options = PointerOptions::None;
2673   if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)
2674     Options = PointerOptions::LValueRefThisPointer;
2675   else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)
2676     Options = PointerOptions::RValueRefThisPointer;
2677 
2678   // Check if we've already translated this type.  If there is no ref qualifier
2679   // on the function then we look up this pointer type with no associated class
2680   // so that the TypeIndex for the this pointer can be shared with the type
2681   // index for other pointers to this class type.  If there is a ref qualifier
2682   // then we lookup the pointer using the subroutine as the parent type.
2683   auto I = TypeIndices.find({PtrTy, SubroutineTy});
2684   if (I != TypeIndices.end())
2685     return I->second;
2686 
2687   TypeLoweringScope S(*this);
2688   TypeIndex TI = lowerTypePointer(PtrTy, Options);
2689   return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);
2690 }
2691 
2692 TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) {
2693   PointerRecord PR(getTypeIndex(Ty),
2694                    getPointerSizeInBytes() == 8 ? PointerKind::Near64
2695                                                 : PointerKind::Near32,
2696                    PointerMode::LValueReference, PointerOptions::None,
2697                    Ty->getSizeInBits() / 8);
2698   return TypeTable.writeLeafType(PR);
2699 }
2700 
2701 TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) {
2702   // The null DIType is the void type. Don't try to hash it.
2703   if (!Ty)
2704     return TypeIndex::Void();
2705 
2706   // Look through typedefs when getting the complete type index. Call
2707   // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are
2708   // emitted only once.
2709   if (Ty->getTag() == dwarf::DW_TAG_typedef)
2710     (void)getTypeIndex(Ty);
2711   while (Ty->getTag() == dwarf::DW_TAG_typedef)
2712     Ty = cast<DIDerivedType>(Ty)->getBaseType();
2713 
2714   // If this is a non-record type, the complete type index is the same as the
2715   // normal type index. Just call getTypeIndex.
2716   switch (Ty->getTag()) {
2717   case dwarf::DW_TAG_class_type:
2718   case dwarf::DW_TAG_structure_type:
2719   case dwarf::DW_TAG_union_type:
2720     break;
2721   default:
2722     return getTypeIndex(Ty);
2723   }
2724 
2725   const auto *CTy = cast<DICompositeType>(Ty);
2726 
2727   TypeLoweringScope S(*this);
2728 
2729   // Make sure the forward declaration is emitted first. It's unclear if this
2730   // is necessary, but MSVC does it, and we should follow suit until we can show
2731   // otherwise.
2732   // We only emit a forward declaration for named types.
2733   if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {
2734     TypeIndex FwdDeclTI = getTypeIndex(CTy);
2735 
2736     // Just use the forward decl if we don't have complete type info. This
2737     // might happen if the frontend is using modules and expects the complete
2738     // definition to be emitted elsewhere.
2739     if (CTy->isForwardDecl())
2740       return FwdDeclTI;
2741   }
2742 
2743   // Check if we've already translated the complete record type.
2744   // Insert the type with a null TypeIndex to signify that the type is currently
2745   // being lowered.
2746   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2747   if (!InsertResult.second)
2748     return InsertResult.first->second;
2749 
2750   TypeIndex TI;
2751   switch (CTy->getTag()) {
2752   case dwarf::DW_TAG_class_type:
2753   case dwarf::DW_TAG_structure_type:
2754     TI = lowerCompleteTypeClass(CTy);
2755     break;
2756   case dwarf::DW_TAG_union_type:
2757     TI = lowerCompleteTypeUnion(CTy);
2758     break;
2759   default:
2760     llvm_unreachable("not a record");
2761   }
2762 
2763   // Update the type index associated with this CompositeType.  This cannot
2764   // use the 'InsertResult' iterator above because it is potentially
2765   // invalidated by map insertions which can occur while lowering the class
2766   // type above.
2767   CompleteTypeIndices[CTy] = TI;
2768   return TI;
2769 }
2770 
2771 /// Emit all the deferred complete record types. Try to do this in FIFO order,
2772 /// and do this until fixpoint, as each complete record type typically
2773 /// references
2774 /// many other record types.
2775 void CodeViewDebug::emitDeferredCompleteTypes() {
2776   SmallVector<const DICompositeType *, 4> TypesToEmit;
2777   while (!DeferredCompleteTypes.empty()) {
2778     std::swap(DeferredCompleteTypes, TypesToEmit);
2779     for (const DICompositeType *RecordTy : TypesToEmit)
2780       getCompleteTypeIndex(RecordTy);
2781     TypesToEmit.clear();
2782   }
2783 }
2784 
2785 void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,
2786                                           ArrayRef<LocalVariable> Locals) {
2787   // Get the sorted list of parameters and emit them first.
2788   SmallVector<const LocalVariable *, 6> Params;
2789   for (const LocalVariable &L : Locals)
2790     if (L.DIVar->isParameter())
2791       Params.push_back(&L);
2792   llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {
2793     return L->DIVar->getArg() < R->DIVar->getArg();
2794   });
2795   for (const LocalVariable *L : Params)
2796     emitLocalVariable(FI, *L);
2797 
2798   // Next emit all non-parameters in the order that we found them.
2799   for (const LocalVariable &L : Locals) {
2800     if (!L.DIVar->isParameter()) {
2801       if (L.ConstantValue) {
2802         // If ConstantValue is set we will emit it as a S_CONSTANT instead of a
2803         // S_LOCAL in order to be able to represent it at all.
2804         const DIType *Ty = L.DIVar->getType();
2805         APSInt Val(*L.ConstantValue);
2806         emitConstantSymbolRecord(Ty, Val, std::string(L.DIVar->getName()));
2807       } else {
2808         emitLocalVariable(FI, L);
2809       }
2810     }
2811   }
2812 }
2813 
2814 void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,
2815                                       const LocalVariable &Var) {
2816   // LocalSym record, see SymbolRecord.h for more info.
2817   MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);
2818 
2819   LocalSymFlags Flags = LocalSymFlags::None;
2820   if (Var.DIVar->isParameter())
2821     Flags |= LocalSymFlags::IsParameter;
2822   if (Var.DefRanges.empty())
2823     Flags |= LocalSymFlags::IsOptimizedOut;
2824 
2825   OS.AddComment("TypeIndex");
2826   TypeIndex TI = Var.UseReferenceType
2827                      ? getTypeIndexForReferenceTo(Var.DIVar->getType())
2828                      : getCompleteTypeIndex(Var.DIVar->getType());
2829   OS.emitInt32(TI.getIndex());
2830   OS.AddComment("Flags");
2831   OS.emitInt16(static_cast<uint16_t>(Flags));
2832   // Truncate the name so we won't overflow the record length field.
2833   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
2834   endSymbolRecord(LocalEnd);
2835 
2836   // Calculate the on disk prefix of the appropriate def range record. The
2837   // records and on disk formats are described in SymbolRecords.h. BytePrefix
2838   // should be big enough to hold all forms without memory allocation.
2839   SmallString<20> BytePrefix;
2840   for (const auto &Pair : Var.DefRanges) {
2841     LocalVarDef DefRange = Pair.first;
2842     const auto &Ranges = Pair.second;
2843     BytePrefix.clear();
2844     if (DefRange.InMemory) {
2845       int Offset = DefRange.DataOffset;
2846       unsigned Reg = DefRange.CVRegister;
2847 
2848       // 32-bit x86 call sequences often use PUSH instructions, which disrupt
2849       // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,
2850       // instead. In frames without stack realignment, $T0 will be the CFA.
2851       if (RegisterId(Reg) == RegisterId::ESP) {
2852         Reg = unsigned(RegisterId::VFRAME);
2853         Offset += FI.OffsetAdjustment;
2854       }
2855 
2856       // If we can use the chosen frame pointer for the frame and this isn't a
2857       // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.
2858       // Otherwise, use S_DEFRANGE_REGISTER_REL.
2859       EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);
2860       if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&
2861           (bool(Flags & LocalSymFlags::IsParameter)
2862                ? (EncFP == FI.EncodedParamFramePtrReg)
2863                : (EncFP == FI.EncodedLocalFramePtrReg))) {
2864         DefRangeFramePointerRelHeader DRHdr;
2865         DRHdr.Offset = Offset;
2866         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2867       } else {
2868         uint16_t RegRelFlags = 0;
2869         if (DefRange.IsSubfield) {
2870           RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |
2871                         (DefRange.StructOffset
2872                          << DefRangeRegisterRelSym::OffsetInParentShift);
2873         }
2874         DefRangeRegisterRelHeader DRHdr;
2875         DRHdr.Register = Reg;
2876         DRHdr.Flags = RegRelFlags;
2877         DRHdr.BasePointerOffset = Offset;
2878         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2879       }
2880     } else {
2881       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
2882       if (DefRange.IsSubfield) {
2883         DefRangeSubfieldRegisterHeader DRHdr;
2884         DRHdr.Register = DefRange.CVRegister;
2885         DRHdr.MayHaveNoName = 0;
2886         DRHdr.OffsetInParent = DefRange.StructOffset;
2887         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2888       } else {
2889         DefRangeRegisterHeader DRHdr;
2890         DRHdr.Register = DefRange.CVRegister;
2891         DRHdr.MayHaveNoName = 0;
2892         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2893       }
2894     }
2895   }
2896 }
2897 
2898 void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
2899                                          const FunctionInfo& FI) {
2900   for (LexicalBlock *Block : Blocks)
2901     emitLexicalBlock(*Block, FI);
2902 }
2903 
2904 /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a
2905 /// lexical block scope.
2906 void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,
2907                                      const FunctionInfo& FI) {
2908   MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);
2909   OS.AddComment("PtrParent");
2910   OS.emitInt32(0); // PtrParent
2911   OS.AddComment("PtrEnd");
2912   OS.emitInt32(0); // PtrEnd
2913   OS.AddComment("Code size");
2914   OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4);   // Code Size
2915   OS.AddComment("Function section relative address");
2916   OS.emitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset
2917   OS.AddComment("Function section index");
2918   OS.emitCOFFSectionIndex(FI.Begin); // Func Symbol
2919   OS.AddComment("Lexical block name");
2920   emitNullTerminatedSymbolName(OS, Block.Name);           // Name
2921   endSymbolRecord(RecordEnd);
2922 
2923   // Emit variables local to this lexical block.
2924   emitLocalVariableList(FI, Block.Locals);
2925   emitGlobalVariableList(Block.Globals);
2926 
2927   // Emit lexical blocks contained within this block.
2928   emitLexicalBlockList(Block.Children, FI);
2929 
2930   // Close the lexical block scope.
2931   emitEndSymbolRecord(SymbolKind::S_END);
2932 }
2933 
2934 /// Convenience routine for collecting lexical block information for a list
2935 /// of lexical scopes.
2936 void CodeViewDebug::collectLexicalBlockInfo(
2937         SmallVectorImpl<LexicalScope *> &Scopes,
2938         SmallVectorImpl<LexicalBlock *> &Blocks,
2939         SmallVectorImpl<LocalVariable> &Locals,
2940         SmallVectorImpl<CVGlobalVariable> &Globals) {
2941   for (LexicalScope *Scope : Scopes)
2942     collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);
2943 }
2944 
2945 /// Populate the lexical blocks and local variable lists of the parent with
2946 /// information about the specified lexical scope.
2947 void CodeViewDebug::collectLexicalBlockInfo(
2948     LexicalScope &Scope,
2949     SmallVectorImpl<LexicalBlock *> &ParentBlocks,
2950     SmallVectorImpl<LocalVariable> &ParentLocals,
2951     SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {
2952   if (Scope.isAbstractScope())
2953     return;
2954 
2955   // Gather information about the lexical scope including local variables,
2956   // global variables, and address ranges.
2957   bool IgnoreScope = false;
2958   auto LI = ScopeVariables.find(&Scope);
2959   SmallVectorImpl<LocalVariable> *Locals =
2960       LI != ScopeVariables.end() ? &LI->second : nullptr;
2961   auto GI = ScopeGlobals.find(Scope.getScopeNode());
2962   SmallVectorImpl<CVGlobalVariable> *Globals =
2963       GI != ScopeGlobals.end() ? GI->second.get() : nullptr;
2964   const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());
2965   const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();
2966 
2967   // Ignore lexical scopes which do not contain variables.
2968   if (!Locals && !Globals)
2969     IgnoreScope = true;
2970 
2971   // Ignore lexical scopes which are not lexical blocks.
2972   if (!DILB)
2973     IgnoreScope = true;
2974 
2975   // Ignore scopes which have too many address ranges to represent in the
2976   // current CodeView format or do not have a valid address range.
2977   //
2978   // For lexical scopes with multiple address ranges you may be tempted to
2979   // construct a single range covering every instruction where the block is
2980   // live and everything in between.  Unfortunately, Visual Studio only
2981   // displays variables from the first matching lexical block scope.  If the
2982   // first lexical block contains exception handling code or cold code which
2983   // is moved to the bottom of the routine creating a single range covering
2984   // nearly the entire routine, then it will hide all other lexical blocks
2985   // and the variables they contain.
2986   if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))
2987     IgnoreScope = true;
2988 
2989   if (IgnoreScope) {
2990     // This scope can be safely ignored and eliminating it will reduce the
2991     // size of the debug information. Be sure to collect any variable and scope
2992     // information from the this scope or any of its children and collapse them
2993     // into the parent scope.
2994     if (Locals)
2995       ParentLocals.append(Locals->begin(), Locals->end());
2996     if (Globals)
2997       ParentGlobals.append(Globals->begin(), Globals->end());
2998     collectLexicalBlockInfo(Scope.getChildren(),
2999                             ParentBlocks,
3000                             ParentLocals,
3001                             ParentGlobals);
3002     return;
3003   }
3004 
3005   // Create a new CodeView lexical block for this lexical scope.  If we've
3006   // seen this DILexicalBlock before then the scope tree is malformed and
3007   // we can handle this gracefully by not processing it a second time.
3008   auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()});
3009   if (!BlockInsertion.second)
3010     return;
3011 
3012   // Create a lexical block containing the variables and collect the the
3013   // lexical block information for the children.
3014   const InsnRange &Range = Ranges.front();
3015   assert(Range.first && Range.second);
3016   LexicalBlock &Block = BlockInsertion.first->second;
3017   Block.Begin = getLabelBeforeInsn(Range.first);
3018   Block.End = getLabelAfterInsn(Range.second);
3019   assert(Block.Begin && "missing label for scope begin");
3020   assert(Block.End && "missing label for scope end");
3021   Block.Name = DILB->getName();
3022   if (Locals)
3023     Block.Locals = std::move(*Locals);
3024   if (Globals)
3025     Block.Globals = std::move(*Globals);
3026   ParentBlocks.push_back(&Block);
3027   collectLexicalBlockInfo(Scope.getChildren(),
3028                           Block.Children,
3029                           Block.Locals,
3030                           Block.Globals);
3031 }
3032 
3033 void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
3034   const Function &GV = MF->getFunction();
3035   assert(FnDebugInfo.count(&GV));
3036   assert(CurFn == FnDebugInfo[&GV].get());
3037 
3038   collectVariableInfo(GV.getSubprogram());
3039 
3040   // Build the lexical block structure to emit for this routine.
3041   if (LexicalScope *CFS = LScopes.getCurrentFunctionScope())
3042     collectLexicalBlockInfo(*CFS,
3043                             CurFn->ChildBlocks,
3044                             CurFn->Locals,
3045                             CurFn->Globals);
3046 
3047   // Clear the scope and variable information from the map which will not be
3048   // valid after we have finished processing this routine.  This also prepares
3049   // the map for the subsequent routine.
3050   ScopeVariables.clear();
3051 
3052   // Don't emit anything if we don't have any line tables.
3053   // Thunks are compiler-generated and probably won't have source correlation.
3054   if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {
3055     FnDebugInfo.erase(&GV);
3056     CurFn = nullptr;
3057     return;
3058   }
3059 
3060   // Find heap alloc sites and add to list.
3061   for (const auto &MBB : *MF) {
3062     for (const auto &MI : MBB) {
3063       if (MDNode *MD = MI.getHeapAllocMarker()) {
3064         CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),
3065                                                         getLabelAfterInsn(&MI),
3066                                                         dyn_cast<DIType>(MD)));
3067       }
3068     }
3069   }
3070 
3071   CurFn->Annotations = MF->getCodeViewAnnotations();
3072 
3073   CurFn->End = Asm->getFunctionEnd();
3074 
3075   CurFn = nullptr;
3076 }
3077 
3078 // Usable locations are valid with non-zero line numbers. A line number of zero
3079 // corresponds to optimized code that doesn't have a distinct source location.
3080 // In this case, we try to use the previous or next source location depending on
3081 // the context.
3082 static bool isUsableDebugLoc(DebugLoc DL) {
3083   return DL && DL.getLine() != 0;
3084 }
3085 
3086 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
3087   DebugHandlerBase::beginInstruction(MI);
3088 
3089   // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
3090   if (!Asm || !CurFn || MI->isDebugInstr() ||
3091       MI->getFlag(MachineInstr::FrameSetup))
3092     return;
3093 
3094   // If the first instruction of a new MBB has no location, find the first
3095   // instruction with a location and use that.
3096   DebugLoc DL = MI->getDebugLoc();
3097   if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) {
3098     for (const auto &NextMI : *MI->getParent()) {
3099       if (NextMI.isDebugInstr())
3100         continue;
3101       DL = NextMI.getDebugLoc();
3102       if (isUsableDebugLoc(DL))
3103         break;
3104     }
3105     // FIXME: Handle the case where the BB has no valid locations. This would
3106     // probably require doing a real dataflow analysis.
3107   }
3108   PrevInstBB = MI->getParent();
3109 
3110   // If we still don't have a debug location, don't record a location.
3111   if (!isUsableDebugLoc(DL))
3112     return;
3113 
3114   maybeRecordLocation(DL, Asm->MF);
3115 }
3116 
3117 MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
3118   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3119            *EndLabel = MMI->getContext().createTempSymbol();
3120   OS.emitInt32(unsigned(Kind));
3121   OS.AddComment("Subsection size");
3122   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
3123   OS.emitLabel(BeginLabel);
3124   return EndLabel;
3125 }
3126 
3127 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
3128   OS.emitLabel(EndLabel);
3129   // Every subsection must be aligned to a 4-byte boundary.
3130   OS.emitValueToAlignment(Align(4));
3131 }
3132 
3133 static StringRef getSymbolName(SymbolKind SymKind) {
3134   for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())
3135     if (EE.Value == SymKind)
3136       return EE.Name;
3137   return "";
3138 }
3139 
3140 MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {
3141   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3142            *EndLabel = MMI->getContext().createTempSymbol();
3143   OS.AddComment("Record length");
3144   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
3145   OS.emitLabel(BeginLabel);
3146   if (OS.isVerboseAsm())
3147     OS.AddComment("Record kind: " + getSymbolName(SymKind));
3148   OS.emitInt16(unsigned(SymKind));
3149   return EndLabel;
3150 }
3151 
3152 void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {
3153   // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid
3154   // an extra copy of every symbol record in LLD. This increases object file
3155   // size by less than 1% in the clang build, and is compatible with the Visual
3156   // C++ linker.
3157   OS.emitValueToAlignment(Align(4));
3158   OS.emitLabel(SymEnd);
3159 }
3160 
3161 void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {
3162   OS.AddComment("Record length");
3163   OS.emitInt16(2);
3164   if (OS.isVerboseAsm())
3165     OS.AddComment("Record kind: " + getSymbolName(EndKind));
3166   OS.emitInt16(uint16_t(EndKind)); // Record Kind
3167 }
3168 
3169 void CodeViewDebug::emitDebugInfoForUDTs(
3170     const std::vector<std::pair<std::string, const DIType *>> &UDTs) {
3171 #ifndef NDEBUG
3172   size_t OriginalSize = UDTs.size();
3173 #endif
3174   for (const auto &UDT : UDTs) {
3175     const DIType *T = UDT.second;
3176     assert(shouldEmitUdt(T));
3177     MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);
3178     OS.AddComment("Type");
3179     OS.emitInt32(getCompleteTypeIndex(T).getIndex());
3180     assert(OriginalSize == UDTs.size() &&
3181            "getCompleteTypeIndex found new UDTs!");
3182     emitNullTerminatedSymbolName(OS, UDT.first);
3183     endSymbolRecord(UDTRecordEnd);
3184   }
3185 }
3186 
3187 void CodeViewDebug::collectGlobalVariableInfo() {
3188   DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
3189       GlobalMap;
3190   for (const GlobalVariable &GV : MMI->getModule()->globals()) {
3191     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
3192     GV.getDebugInfo(GVEs);
3193     for (const auto *GVE : GVEs)
3194       GlobalMap[GVE] = &GV;
3195   }
3196 
3197   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3198   for (const MDNode *Node : CUs->operands()) {
3199     const auto *CU = cast<DICompileUnit>(Node);
3200     for (const auto *GVE : CU->getGlobalVariables()) {
3201       const DIGlobalVariable *DIGV = GVE->getVariable();
3202       const DIExpression *DIE = GVE->getExpression();
3203       // Don't emit string literals in CodeView, as the only useful parts are
3204       // generally the filename and line number, which isn't possible to output
3205       // in CodeView. String literals should be the only unnamed GlobalVariable
3206       // with debug info.
3207       if (DIGV->getName().empty()) continue;
3208 
3209       if ((DIE->getNumElements() == 2) &&
3210           (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
3211         // Record the constant offset for the variable.
3212         //
3213         // A Fortran common block uses this idiom to encode the offset
3214         // of a variable from the common block's starting address.
3215         CVGlobalVariableOffsets.insert(
3216             std::make_pair(DIGV, DIE->getElement(1)));
3217 
3218       // Emit constant global variables in a global symbol section.
3219       if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
3220         CVGlobalVariable CVGV = {DIGV, DIE};
3221         GlobalVariables.emplace_back(std::move(CVGV));
3222       }
3223 
3224       const auto *GV = GlobalMap.lookup(GVE);
3225       if (!GV || GV->isDeclarationForLinker())
3226         continue;
3227 
3228       DIScope *Scope = DIGV->getScope();
3229       SmallVector<CVGlobalVariable, 1> *VariableList;
3230       if (Scope && isa<DILocalScope>(Scope)) {
3231         // Locate a global variable list for this scope, creating one if
3232         // necessary.
3233         auto Insertion = ScopeGlobals.insert(
3234             {Scope, std::unique_ptr<GlobalVariableList>()});
3235         if (Insertion.second)
3236           Insertion.first->second = std::make_unique<GlobalVariableList>();
3237         VariableList = Insertion.first->second.get();
3238       } else if (GV->hasComdat())
3239         // Emit this global variable into a COMDAT section.
3240         VariableList = &ComdatVariables;
3241       else
3242         // Emit this global variable in a single global symbol section.
3243         VariableList = &GlobalVariables;
3244       CVGlobalVariable CVGV = {DIGV, GV};
3245       VariableList->emplace_back(std::move(CVGV));
3246     }
3247   }
3248 }
3249 
3250 void CodeViewDebug::collectDebugInfoForGlobals() {
3251   for (const CVGlobalVariable &CVGV : GlobalVariables) {
3252     const DIGlobalVariable *DIGV = CVGV.DIGV;
3253     const DIScope *Scope = DIGV->getScope();
3254     getCompleteTypeIndex(DIGV->getType());
3255     getFullyQualifiedName(Scope, DIGV->getName());
3256   }
3257 
3258   for (const CVGlobalVariable &CVGV : ComdatVariables) {
3259     const DIGlobalVariable *DIGV = CVGV.DIGV;
3260     const DIScope *Scope = DIGV->getScope();
3261     getCompleteTypeIndex(DIGV->getType());
3262     getFullyQualifiedName(Scope, DIGV->getName());
3263   }
3264 }
3265 
3266 void CodeViewDebug::emitDebugInfoForGlobals() {
3267   // First, emit all globals that are not in a comdat in a single symbol
3268   // substream. MSVC doesn't like it if the substream is empty, so only open
3269   // it if we have at least one global to emit.
3270   switchToDebugSectionForSymbol(nullptr);
3271   if (!GlobalVariables.empty() || !StaticConstMembers.empty()) {
3272     OS.AddComment("Symbol subsection for globals");
3273     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3274     emitGlobalVariableList(GlobalVariables);
3275     emitStaticConstMemberList();
3276     endCVSubsection(EndLabel);
3277   }
3278 
3279   // Second, emit each global that is in a comdat into its own .debug$S
3280   // section along with its own symbol substream.
3281   for (const CVGlobalVariable &CVGV : ComdatVariables) {
3282     const GlobalVariable *GV = CVGV.GVInfo.get<const GlobalVariable *>();
3283     MCSymbol *GVSym = Asm->getSymbol(GV);
3284     OS.AddComment("Symbol subsection for " +
3285                   Twine(GlobalValue::dropLLVMManglingEscape(GV->getName())));
3286     switchToDebugSectionForSymbol(GVSym);
3287     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3288     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3289     emitDebugInfoForGlobal(CVGV);
3290     endCVSubsection(EndLabel);
3291   }
3292 }
3293 
3294 void CodeViewDebug::emitDebugInfoForRetainedTypes() {
3295   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3296   for (const MDNode *Node : CUs->operands()) {
3297     for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
3298       if (DIType *RT = dyn_cast<DIType>(Ty)) {
3299         getTypeIndex(RT);
3300         // FIXME: Add to global/local DTU list.
3301       }
3302     }
3303   }
3304 }
3305 
3306 // Emit each global variable in the specified array.
3307 void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {
3308   for (const CVGlobalVariable &CVGV : Globals) {
3309     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3310     emitDebugInfoForGlobal(CVGV);
3311   }
3312 }
3313 
3314 void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
3315                                              const std::string &QualifiedName) {
3316   MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);
3317   OS.AddComment("Type");
3318   OS.emitInt32(getTypeIndex(DTy).getIndex());
3319 
3320   OS.AddComment("Value");
3321 
3322   // Encoded integers shouldn't need more than 10 bytes.
3323   uint8_t Data[10];
3324   BinaryStreamWriter Writer(Data, llvm::support::endianness::little);
3325   CodeViewRecordIO IO(Writer);
3326   cantFail(IO.mapEncodedInteger(Value));
3327   StringRef SRef((char *)Data, Writer.getOffset());
3328   OS.emitBinaryData(SRef);
3329 
3330   OS.AddComment("Name");
3331   emitNullTerminatedSymbolName(OS, QualifiedName);
3332   endSymbolRecord(SConstantEnd);
3333 }
3334 
3335 void CodeViewDebug::emitStaticConstMemberList() {
3336   for (const DIDerivedType *DTy : StaticConstMembers) {
3337     const DIScope *Scope = DTy->getScope();
3338 
3339     APSInt Value;
3340     if (const ConstantInt *CI =
3341             dyn_cast_or_null<ConstantInt>(DTy->getConstant()))
3342       Value = APSInt(CI->getValue(),
3343                      DebugHandlerBase::isUnsignedDIType(DTy->getBaseType()));
3344     else if (const ConstantFP *CFP =
3345                  dyn_cast_or_null<ConstantFP>(DTy->getConstant()))
3346       Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true);
3347     else
3348       llvm_unreachable("cannot emit a constant without a value");
3349 
3350     emitConstantSymbolRecord(DTy->getBaseType(), Value,
3351                              getFullyQualifiedName(Scope, DTy->getName()));
3352   }
3353 }
3354 
3355 static bool isFloatDIType(const DIType *Ty) {
3356   if (isa<DICompositeType>(Ty))
3357     return false;
3358 
3359   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
3360     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
3361     if (T == dwarf::DW_TAG_pointer_type ||
3362         T == dwarf::DW_TAG_ptr_to_member_type ||
3363         T == dwarf::DW_TAG_reference_type ||
3364         T == dwarf::DW_TAG_rvalue_reference_type)
3365       return false;
3366     assert(DTy->getBaseType() && "Expected valid base type");
3367     return isFloatDIType(DTy->getBaseType());
3368   }
3369 
3370   auto *BTy = cast<DIBasicType>(Ty);
3371   return (BTy->getEncoding() == dwarf::DW_ATE_float);
3372 }
3373 
3374 void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
3375   const DIGlobalVariable *DIGV = CVGV.DIGV;
3376 
3377   const DIScope *Scope = DIGV->getScope();
3378   // For static data members, get the scope from the declaration.
3379   if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
3380           DIGV->getRawStaticDataMemberDeclaration()))
3381     Scope = MemberDecl->getScope();
3382   // For static local variables and Fortran, the scoping portion is elided
3383   // in its name so that we can reference the variable in the command line
3384   // of the VS debugger.
3385   std::string QualifiedName =
3386       (moduleIsInFortran() || (Scope && isa<DILocalScope>(Scope)))
3387           ? std::string(DIGV->getName())
3388           : getFullyQualifiedName(Scope, DIGV->getName());
3389 
3390   if (const GlobalVariable *GV =
3391           CVGV.GVInfo.dyn_cast<const GlobalVariable *>()) {
3392     // DataSym record, see SymbolRecord.h for more info. Thread local data
3393     // happens to have the same format as global data.
3394     MCSymbol *GVSym = Asm->getSymbol(GV);
3395     SymbolKind DataSym = GV->isThreadLocal()
3396                              ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32
3397                                                       : SymbolKind::S_GTHREAD32)
3398                              : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32
3399                                                       : SymbolKind::S_GDATA32);
3400     MCSymbol *DataEnd = beginSymbolRecord(DataSym);
3401     OS.AddComment("Type");
3402     OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());
3403     OS.AddComment("DataOffset");
3404 
3405     uint64_t Offset = 0;
3406     if (CVGlobalVariableOffsets.find(DIGV) != CVGlobalVariableOffsets.end())
3407       // Use the offset seen while collecting info on globals.
3408       Offset = CVGlobalVariableOffsets[DIGV];
3409     OS.emitCOFFSecRel32(GVSym, Offset);
3410 
3411     OS.AddComment("Segment");
3412     OS.emitCOFFSectionIndex(GVSym);
3413     OS.AddComment("Name");
3414     const unsigned LengthOfDataRecord = 12;
3415     emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord);
3416     endSymbolRecord(DataEnd);
3417   } else {
3418     const DIExpression *DIE = CVGV.GVInfo.get<const DIExpression *>();
3419     assert(DIE->isConstant() &&
3420            "Global constant variables must contain a constant expression.");
3421 
3422     // Use unsigned for floats.
3423     bool isUnsigned = isFloatDIType(DIGV->getType())
3424                           ? true
3425                           : DebugHandlerBase::isUnsignedDIType(DIGV->getType());
3426     APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);
3427     emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName);
3428   }
3429 }
3430