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