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