1 //===-- SymbolFileNativePDB.cpp ---------------------------------*- C++ -*-===//
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 #include "SymbolFileNativePDB.h"
10 
11 #include "clang/AST/Attr.h"
12 #include "clang/AST/CharUnits.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/Type.h"
16 
17 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/StreamBuffer.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Symbol/ClangASTContext.h"
23 #include "lldb/Symbol/ClangASTImporter.h"
24 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
25 #include "lldb/Symbol/ClangUtil.h"
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/LineTable.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Symbol/SymbolContext.h"
30 #include "lldb/Symbol/SymbolVendor.h"
31 #include "lldb/Symbol/Variable.h"
32 #include "lldb/Symbol/VariableList.h"
33 
34 #include "llvm/DebugInfo/CodeView/CVRecord.h"
35 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
36 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
37 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
38 #include "llvm/DebugInfo/CodeView/RecordName.h"
39 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
40 #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
41 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
42 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
43 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
44 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
45 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
46 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
47 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
48 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
49 #include "llvm/DebugInfo/PDB/PDBTypes.h"
50 #include "llvm/Demangle/MicrosoftDemangle.h"
51 #include "llvm/Object/COFF.h"
52 #include "llvm/Support/Allocator.h"
53 #include "llvm/Support/BinaryStreamReader.h"
54 #include "llvm/Support/Error.h"
55 #include "llvm/Support/ErrorOr.h"
56 #include "llvm/Support/MemoryBuffer.h"
57 
58 #include "DWARFLocationExpression.h"
59 #include "PdbAstBuilder.h"
60 #include "PdbSymUid.h"
61 #include "PdbUtil.h"
62 #include "UdtRecordCompleter.h"
63 
64 using namespace lldb;
65 using namespace lldb_private;
66 using namespace npdb;
67 using namespace llvm::codeview;
68 using namespace llvm::pdb;
69 
70 static lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
71   switch (lang) {
72   case PDB_Lang::Cpp:
73     return lldb::LanguageType::eLanguageTypeC_plus_plus;
74   case PDB_Lang::C:
75     return lldb::LanguageType::eLanguageTypeC;
76   case PDB_Lang::Swift:
77     return lldb::LanguageType::eLanguageTypeSwift;
78   default:
79     return lldb::LanguageType::eLanguageTypeUnknown;
80   }
81 }
82 
83 static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath,
84                                             llvm::BumpPtrAllocator &Allocator) {
85   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
86       llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1,
87                                   /*RequiresNullTerminator=*/false);
88   if (!ErrorOrBuffer)
89     return nullptr;
90   std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
91 
92   llvm::StringRef Path = Buffer->getBufferIdentifier();
93   auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>(
94       std::move(Buffer), llvm::support::little);
95 
96   auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
97   if (auto EC = File->parseFileHeaders()) {
98     llvm::consumeError(std::move(EC));
99     return nullptr;
100   }
101   if (auto EC = File->parseStreamData()) {
102     llvm::consumeError(std::move(EC));
103     return nullptr;
104   }
105 
106   return File;
107 }
108 
109 static std::unique_ptr<PDBFile>
110 loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) {
111   // Try to find a matching PDB for an EXE.
112   using namespace llvm::object;
113   auto expected_binary = createBinary(exe_path);
114 
115   // If the file isn't a PE/COFF executable, fail.
116   if (!expected_binary) {
117     llvm::consumeError(expected_binary.takeError());
118     return nullptr;
119   }
120   OwningBinary<Binary> binary = std::move(*expected_binary);
121 
122   auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary());
123   if (!obj)
124     return nullptr;
125   const llvm::codeview::DebugInfo *pdb_info = nullptr;
126 
127   // If it doesn't have a debug directory, fail.
128   llvm::StringRef pdb_file;
129   auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file);
130   if (ec)
131     return nullptr;
132 
133   // if the file doesn't exist, is not a pdb, or doesn't have a matching guid,
134   // fail.
135   llvm::file_magic magic;
136   ec = llvm::identify_magic(pdb_file, magic);
137   if (ec || magic != llvm::file_magic::pdb)
138     return nullptr;
139   std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator);
140   if (!pdb)
141     return nullptr;
142 
143   auto expected_info = pdb->getPDBInfoStream();
144   if (!expected_info) {
145     llvm::consumeError(expected_info.takeError());
146     return nullptr;
147   }
148   llvm::codeview::GUID guid;
149   memcpy(&guid, pdb_info->PDB70.Signature, 16);
150 
151   if (expected_info->getGuid() != guid)
152     return nullptr;
153   return pdb;
154 }
155 
156 static bool IsFunctionPrologue(const CompilandIndexItem &cci,
157                                lldb::addr_t addr) {
158   // FIXME: Implement this.
159   return false;
160 }
161 
162 static bool IsFunctionEpilogue(const CompilandIndexItem &cci,
163                                lldb::addr_t addr) {
164   // FIXME: Implement this.
165   return false;
166 }
167 
168 static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) {
169   switch (kind) {
170   case SimpleTypeKind::Boolean128:
171   case SimpleTypeKind::Boolean16:
172   case SimpleTypeKind::Boolean32:
173   case SimpleTypeKind::Boolean64:
174   case SimpleTypeKind::Boolean8:
175     return "bool";
176   case SimpleTypeKind::Byte:
177   case SimpleTypeKind::UnsignedCharacter:
178     return "unsigned char";
179   case SimpleTypeKind::NarrowCharacter:
180     return "char";
181   case SimpleTypeKind::SignedCharacter:
182   case SimpleTypeKind::SByte:
183     return "signed char";
184   case SimpleTypeKind::Character16:
185     return "char16_t";
186   case SimpleTypeKind::Character32:
187     return "char32_t";
188   case SimpleTypeKind::Complex80:
189   case SimpleTypeKind::Complex64:
190   case SimpleTypeKind::Complex32:
191     return "complex";
192   case SimpleTypeKind::Float128:
193   case SimpleTypeKind::Float80:
194     return "long double";
195   case SimpleTypeKind::Float64:
196     return "double";
197   case SimpleTypeKind::Float32:
198     return "float";
199   case SimpleTypeKind::Float16:
200     return "single";
201   case SimpleTypeKind::Int128:
202     return "__int128";
203   case SimpleTypeKind::Int64:
204   case SimpleTypeKind::Int64Quad:
205     return "int64_t";
206   case SimpleTypeKind::Int32:
207     return "int";
208   case SimpleTypeKind::Int16:
209     return "short";
210   case SimpleTypeKind::UInt128:
211     return "unsigned __int128";
212   case SimpleTypeKind::UInt64:
213   case SimpleTypeKind::UInt64Quad:
214     return "uint64_t";
215   case SimpleTypeKind::HResult:
216     return "HRESULT";
217   case SimpleTypeKind::UInt32:
218     return "unsigned";
219   case SimpleTypeKind::UInt16:
220   case SimpleTypeKind::UInt16Short:
221     return "unsigned short";
222   case SimpleTypeKind::Int32Long:
223     return "long";
224   case SimpleTypeKind::UInt32Long:
225     return "unsigned long";
226   case SimpleTypeKind::Void:
227     return "void";
228   case SimpleTypeKind::WideCharacter:
229     return "wchar_t";
230   default:
231     return "";
232   }
233 }
234 
235 static bool IsClassRecord(TypeLeafKind kind) {
236   switch (kind) {
237   case LF_STRUCTURE:
238   case LF_CLASS:
239   case LF_INTERFACE:
240     return true;
241   default:
242     return false;
243   }
244 }
245 
246 void SymbolFileNativePDB::Initialize() {
247   PluginManager::RegisterPlugin(GetPluginNameStatic(),
248                                 GetPluginDescriptionStatic(), CreateInstance,
249                                 DebuggerInitialize);
250 }
251 
252 void SymbolFileNativePDB::Terminate() {
253   PluginManager::UnregisterPlugin(CreateInstance);
254 }
255 
256 void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {}
257 
258 ConstString SymbolFileNativePDB::GetPluginNameStatic() {
259   static ConstString g_name("native-pdb");
260   return g_name;
261 }
262 
263 const char *SymbolFileNativePDB::GetPluginDescriptionStatic() {
264   return "Microsoft PDB debug symbol cross-platform file reader.";
265 }
266 
267 SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) {
268   return new SymbolFileNativePDB(obj_file);
269 }
270 
271 SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file)
272     : SymbolFile(object_file) {}
273 
274 SymbolFileNativePDB::~SymbolFileNativePDB() {}
275 
276 uint32_t SymbolFileNativePDB::CalculateAbilities() {
277   uint32_t abilities = 0;
278   if (!m_obj_file)
279     return 0;
280 
281   if (!m_index) {
282     // Lazily load and match the PDB file, but only do this once.
283     std::unique_ptr<PDBFile> file_up =
284         loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator);
285 
286     if (!file_up) {
287       auto module_sp = m_obj_file->GetModule();
288       if (!module_sp)
289         return 0;
290       // See if any symbol file is specified through `--symfile` option.
291       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
292       if (!symfile)
293         return 0;
294       file_up = loadPDBFile(symfile.GetPath(), m_allocator);
295     }
296 
297     if (!file_up)
298       return 0;
299 
300     auto expected_index = PdbIndex::create(std::move(file_up));
301     if (!expected_index) {
302       llvm::consumeError(expected_index.takeError());
303       return 0;
304     }
305     m_index = std::move(*expected_index);
306   }
307   if (!m_index)
308     return 0;
309 
310   // We don't especially have to be precise here.  We only distinguish between
311   // stripped and not stripped.
312   abilities = kAllAbilities;
313 
314   if (m_index->dbi().isStripped())
315     abilities &= ~(Blocks | LocalVariables);
316   return abilities;
317 }
318 
319 void SymbolFileNativePDB::InitializeObject() {
320   m_obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress();
321   m_index->SetLoadAddress(m_obj_load_address);
322   m_index->ParseSectionContribs();
323 
324   TypeSystem *ts = m_obj_file->GetModule()->GetTypeSystemForLanguage(
325       lldb::eLanguageTypeC_plus_plus);
326   if (ts)
327     ts->SetSymbolFile(this);
328 
329   m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index);
330 }
331 
332 uint32_t SymbolFileNativePDB::GetNumCompileUnits() {
333   const DbiModuleList &modules = m_index->dbi().modules();
334   uint32_t count = modules.getModuleCount();
335   if (count == 0)
336     return count;
337 
338   // The linker can inject an additional "dummy" compilation unit into the
339   // PDB. Ignore this special compile unit for our purposes, if it is there.
340   // It is always the last one.
341   DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1);
342   if (last.getModuleName() == "* Linker *")
343     --count;
344   return count;
345 }
346 
347 Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
348   CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
349   CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
350 
351   if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) {
352     // This is a function.  It must be global.  Creating the Function entry for
353     // it automatically creates a block for it.
354     CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii);
355     return GetOrCreateFunction(block_id, *comp_unit)->GetBlock(false);
356   }
357 
358   lldbassert(sym.kind() == S_BLOCK32);
359 
360   // This is a block.  Its parent is either a function or another block.  In
361   // either case, its parent can be viewed as a block (e.g. a function contains
362   // 1 big block.  So just get the parent block and add this block to it.
363   BlockSym block(static_cast<SymbolRecordKind>(sym.kind()));
364   cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block));
365   lldbassert(block.Parent != 0);
366   PdbCompilandSymId parent_id(block_id.modi, block.Parent);
367   Block &parent_block = GetOrCreateBlock(parent_id);
368   lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id);
369   BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
370   parent_block.AddChild(child_block);
371 
372   m_ast->GetOrCreateBlockDecl(block_id);
373 
374   m_blocks.insert({opaque_block_uid, child_block});
375   return *child_block;
376 }
377 
378 lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id,
379                                                      CompileUnit &comp_unit) {
380   const CompilandIndexItem *cci =
381       m_index->compilands().GetCompiland(func_id.modi);
382   lldbassert(cci);
383   CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(func_id.offset);
384 
385   lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32);
386   SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record);
387 
388   auto file_vm_addr = m_index->MakeVirtualAddress(sol.so);
389   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
390     return nullptr;
391 
392   AddressRange func_range(file_vm_addr, sol.length,
393                           comp_unit.GetModule()->GetSectionList());
394   if (!func_range.GetBaseAddress().IsValid())
395     return nullptr;
396 
397   ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind()));
398   cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym_record, proc));
399   if (proc.FunctionType == TypeIndex::None())
400     return nullptr;
401   TypeSP func_type = GetOrCreateType(proc.FunctionType);
402   if (!func_type)
403     return nullptr;
404 
405   PdbTypeSymId sig_id(proc.FunctionType, false);
406   Mangled mangled(proc.Name);
407   FunctionSP func_sp = std::make_shared<Function>(
408       &comp_unit, toOpaqueUid(func_id), toOpaqueUid(sig_id), mangled,
409       func_type.get(), func_range);
410 
411   comp_unit.AddFunction(func_sp);
412 
413   m_ast->GetOrCreateFunctionDecl(func_id);
414 
415   return func_sp;
416 }
417 
418 CompUnitSP
419 SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) {
420   lldb::LanguageType lang =
421       cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage())
422                          : lldb::eLanguageTypeUnknown;
423 
424   LazyBool optimized = eLazyBoolNo;
425   if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations())
426     optimized = eLazyBoolYes;
427 
428   llvm::SmallString<64> source_file_name =
429       m_index->compilands().GetMainSourceFile(cci);
430   FileSpec fs(source_file_name);
431 
432   CompUnitSP cu_sp =
433       std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs,
434                                     toOpaqueUid(cci.m_id), lang, optimized);
435 
436   m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
437       cci.m_id.modi, cu_sp);
438   return cu_sp;
439 }
440 
441 lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id,
442                                                      const ModifierRecord &mr,
443                                                      CompilerType ct) {
444   TpiStream &stream = m_index->tpi();
445 
446   std::string name;
447   if (mr.ModifiedType.isSimple())
448     name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind());
449   else
450     name = computeTypeName(stream.typeCollection(), mr.ModifiedType);
451   Declaration decl;
452   lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType);
453 
454   return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(name),
455                                 modified_type->GetByteSize(), nullptr,
456                                 LLDB_INVALID_UID, Type::eEncodingIsUID, decl,
457                                 ct, Type::eResolveStateFull);
458 }
459 
460 lldb::TypeSP
461 SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id,
462                                        const llvm::codeview::PointerRecord &pr,
463                                        CompilerType ct) {
464   TypeSP pointee = GetOrCreateType(pr.ReferentType);
465   if (!pointee)
466     return nullptr;
467 
468   if (pr.isPointerToMember()) {
469     MemberPointerInfo mpi = pr.getMemberInfo();
470     GetOrCreateType(mpi.ContainingType);
471   }
472 
473   Declaration decl;
474   return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(),
475                                 pr.getSize(), nullptr, LLDB_INVALID_UID,
476                                 Type::eEncodingIsUID, decl, ct,
477                                 Type::eResolveStateFull);
478 }
479 
480 lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti,
481                                                    CompilerType ct) {
482   uint64_t uid = toOpaqueUid(PdbTypeSymId(ti, false));
483   if (ti == TypeIndex::NullptrT()) {
484     Declaration decl;
485     return std::make_shared<Type>(
486         uid, this, ConstString("std::nullptr_t"), 0, nullptr, LLDB_INVALID_UID,
487         Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
488   }
489 
490   if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
491     TypeSP direct_sp = GetOrCreateType(ti.makeDirect());
492     uint32_t pointer_size = 0;
493     switch (ti.getSimpleMode()) {
494     case SimpleTypeMode::FarPointer32:
495     case SimpleTypeMode::NearPointer32:
496       pointer_size = 4;
497       break;
498     case SimpleTypeMode::NearPointer64:
499       pointer_size = 8;
500       break;
501     default:
502       // 128-bit and 16-bit pointers unsupported.
503       return nullptr;
504     }
505     Declaration decl;
506     return std::make_shared<Type>(
507         uid, this, ConstString(), pointer_size, nullptr, LLDB_INVALID_UID,
508         Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
509   }
510 
511   if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
512     return nullptr;
513 
514   size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind());
515   llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind());
516 
517   Declaration decl;
518   return std::make_shared<Type>(uid, this, ConstString(type_name), size,
519                                 nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID,
520                                 decl, ct, Type::eResolveStateFull);
521 }
522 
523 static std::string GetUnqualifiedTypeName(const TagRecord &record) {
524   if (!record.hasUniqueName()) {
525     MSVCUndecoratedNameParser parser(record.Name);
526     llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
527 
528     return specs.back().GetBaseName();
529   }
530 
531   llvm::ms_demangle::Demangler demangler;
532   StringView sv(record.UniqueName.begin(), record.UniqueName.size());
533   llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
534   if (demangler.Error)
535     return record.Name;
536 
537   llvm::ms_demangle::IdentifierNode *idn =
538       ttn->QualifiedName->getUnqualifiedIdentifier();
539   return idn->toString();
540 }
541 
542 lldb::TypeSP
543 SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id,
544                                             const TagRecord &record,
545                                             size_t size, CompilerType ct) {
546 
547   std::string uname = GetUnqualifiedTypeName(record);
548 
549   // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE.
550   Declaration decl;
551   return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(uname),
552                                 size, nullptr, LLDB_INVALID_UID,
553                                 Type::eEncodingIsUID, decl, ct,
554                                 Type::eResolveStateForward);
555 }
556 
557 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
558                                                 const ClassRecord &cr,
559                                                 CompilerType ct) {
560   return CreateClassStructUnion(type_id, cr, cr.getSize(), ct);
561 }
562 
563 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
564                                                 const UnionRecord &ur,
565                                                 CompilerType ct) {
566   return CreateClassStructUnion(type_id, ur, ur.getSize(), ct);
567 }
568 
569 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
570                                                 const EnumRecord &er,
571                                                 CompilerType ct) {
572   std::string uname = GetUnqualifiedTypeName(er);
573 
574   Declaration decl;
575   TypeSP underlying_type = GetOrCreateType(er.UnderlyingType);
576 
577   return std::make_shared<lldb_private::Type>(
578       toOpaqueUid(type_id), this, ConstString(uname),
579       underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID,
580       lldb_private::Type::eEncodingIsUID, decl, ct,
581       lldb_private::Type::eResolveStateForward);
582 }
583 
584 TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id,
585                                             const ArrayRecord &ar,
586                                             CompilerType ct) {
587   TypeSP element_type = GetOrCreateType(ar.ElementType);
588 
589   Declaration decl;
590   TypeSP array_sp = std::make_shared<lldb_private::Type>(
591       toOpaqueUid(type_id), this, ConstString(), ar.Size, nullptr,
592       LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct,
593       lldb_private::Type::eResolveStateFull);
594   array_sp->SetEncodingType(element_type.get());
595   return array_sp;
596 }
597 
598 TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id,
599                                                 const ProcedureRecord &pr,
600                                                 CompilerType ct) {
601   Declaration decl;
602   return std::make_shared<lldb_private::Type>(
603       toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID,
604       lldb_private::Type::eEncodingIsUID, decl, ct,
605       lldb_private::Type::eResolveStateFull);
606 }
607 
608 TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) {
609   if (type_id.index.isSimple())
610     return CreateSimpleType(type_id.index, ct);
611 
612   TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi();
613   CVType cvt = stream.getType(type_id.index);
614 
615   if (cvt.kind() == LF_MODIFIER) {
616     ModifierRecord modifier;
617     llvm::cantFail(
618         TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
619     return CreateModifierType(type_id, modifier, ct);
620   }
621 
622   if (cvt.kind() == LF_POINTER) {
623     PointerRecord pointer;
624     llvm::cantFail(
625         TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
626     return CreatePointerType(type_id, pointer, ct);
627   }
628 
629   if (IsClassRecord(cvt.kind())) {
630     ClassRecord cr;
631     llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr));
632     return CreateTagType(type_id, cr, ct);
633   }
634 
635   if (cvt.kind() == LF_ENUM) {
636     EnumRecord er;
637     llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er));
638     return CreateTagType(type_id, er, ct);
639   }
640 
641   if (cvt.kind() == LF_UNION) {
642     UnionRecord ur;
643     llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur));
644     return CreateTagType(type_id, ur, ct);
645   }
646 
647   if (cvt.kind() == LF_ARRAY) {
648     ArrayRecord ar;
649     llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
650     return CreateArrayType(type_id, ar, ct);
651   }
652 
653   if (cvt.kind() == LF_PROCEDURE) {
654     ProcedureRecord pr;
655     llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
656     return CreateProcedureType(type_id, pr, ct);
657   }
658 
659   return nullptr;
660 }
661 
662 TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) {
663   // If they search for a UDT which is a forward ref, try and resolve the full
664   // decl and just map the forward ref uid to the full decl record.
665   llvm::Optional<PdbTypeSymId> full_decl_uid;
666   if (IsForwardRefUdt(type_id, m_index->tpi())) {
667     auto expected_full_ti =
668         m_index->tpi().findFullDeclForForwardRef(type_id.index);
669     if (!expected_full_ti)
670       llvm::consumeError(expected_full_ti.takeError());
671     else if (*expected_full_ti != type_id.index) {
672       full_decl_uid = PdbTypeSymId(*expected_full_ti, false);
673 
674       // It's possible that a lookup would occur for the full decl causing it
675       // to be cached, then a second lookup would occur for the forward decl.
676       // We don't want to create a second full decl, so make sure the full
677       // decl hasn't already been cached.
678       auto full_iter = m_types.find(toOpaqueUid(*full_decl_uid));
679       if (full_iter != m_types.end()) {
680         TypeSP result = full_iter->second;
681         // Map the forward decl to the TypeSP for the full decl so we can take
682         // the fast path next time.
683         m_types[toOpaqueUid(type_id)] = result;
684         return result;
685       }
686     }
687   }
688 
689   PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id;
690 
691   clang::QualType qt = m_ast->GetOrCreateType(best_decl_id);
692 
693   TypeSP result = CreateType(best_decl_id, m_ast->ToCompilerType(qt));
694   if (!result)
695     return nullptr;
696 
697   uint64_t best_uid = toOpaqueUid(best_decl_id);
698   m_types[best_uid] = result;
699   // If we had both a forward decl and a full decl, make both point to the new
700   // type.
701   if (full_decl_uid)
702     m_types[toOpaqueUid(type_id)] = result;
703 
704   return result;
705 }
706 
707 TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) {
708   // We can't use try_emplace / overwrite here because the process of creating
709   // a type could create nested types, which could invalidate iterators.  So
710   // we have to do a 2-phase lookup / insert.
711   auto iter = m_types.find(toOpaqueUid(type_id));
712   if (iter != m_types.end())
713     return iter->second;
714 
715   TypeSP type = CreateAndCacheType(type_id);
716   if (type)
717     m_obj_file->GetModule()->GetTypeList()->Insert(type);
718   return type;
719 }
720 
721 VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
722   CVSymbol sym = m_index->symrecords().readRecord(var_id.offset);
723   if (sym.kind() == S_CONSTANT)
724     return CreateConstantSymbol(var_id, sym);
725 
726   lldb::ValueType scope = eValueTypeInvalid;
727   TypeIndex ti;
728   llvm::StringRef name;
729   lldb::addr_t addr = 0;
730   uint16_t section = 0;
731   uint32_t offset = 0;
732   bool is_external = false;
733   switch (sym.kind()) {
734   case S_GDATA32:
735     is_external = true;
736     LLVM_FALLTHROUGH;
737   case S_LDATA32: {
738     DataSym ds(sym.kind());
739     llvm::cantFail(SymbolDeserializer::deserializeAs<DataSym>(sym, ds));
740     ti = ds.Type;
741     scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal
742                                       : eValueTypeVariableStatic;
743     name = ds.Name;
744     section = ds.Segment;
745     offset = ds.DataOffset;
746     addr = m_index->MakeVirtualAddress(ds.Segment, ds.DataOffset);
747     break;
748   }
749   case S_GTHREAD32:
750     is_external = true;
751     LLVM_FALLTHROUGH;
752   case S_LTHREAD32: {
753     ThreadLocalDataSym tlds(sym.kind());
754     llvm::cantFail(
755         SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(sym, tlds));
756     ti = tlds.Type;
757     name = tlds.Name;
758     section = tlds.Segment;
759     offset = tlds.DataOffset;
760     addr = m_index->MakeVirtualAddress(tlds.Segment, tlds.DataOffset);
761     scope = eValueTypeVariableThreadLocal;
762     break;
763   }
764   default:
765     llvm_unreachable("unreachable!");
766   }
767 
768   CompUnitSP comp_unit;
769   llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr);
770   if (modi) {
771     CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi);
772     comp_unit = GetOrCreateCompileUnit(cci);
773   }
774 
775   Declaration decl;
776   PdbTypeSymId tid(ti, false);
777   SymbolFileTypeSP type_sp =
778       std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
779   Variable::RangeList ranges;
780 
781   m_ast->GetOrCreateVariableDecl(var_id);
782 
783   DWARFExpression location = MakeGlobalLocationExpression(
784       section, offset, GetObjectFile()->GetModule());
785 
786   std::string global_name("::");
787   global_name += name;
788   VariableSP var_sp = std::make_shared<Variable>(
789       toOpaqueUid(var_id), name.str().c_str(), global_name.c_str(), type_sp,
790       scope, comp_unit.get(), ranges, &decl, location, is_external, false,
791       false);
792   var_sp->SetLocationIsConstantValueData(false);
793 
794   return var_sp;
795 }
796 
797 lldb::VariableSP
798 SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
799                                           const CVSymbol &cvs) {
800   TpiStream &tpi = m_index->tpi();
801   ConstantSym constant(cvs.kind());
802 
803   llvm::cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(cvs, constant));
804   std::string global_name("::");
805   global_name += constant.Name;
806   PdbTypeSymId tid(constant.Type, false);
807   SymbolFileTypeSP type_sp =
808       std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
809 
810   Declaration decl;
811   Variable::RangeList ranges;
812   ModuleSP module = GetObjectFile()->GetModule();
813   DWARFExpression location = MakeConstantLocationExpression(
814       constant.Type, tpi, constant.Value, module);
815 
816   VariableSP var_sp = std::make_shared<Variable>(
817       toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(),
818       type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location,
819       false, false, false);
820   var_sp->SetLocationIsConstantValueData(true);
821   return var_sp;
822 }
823 
824 VariableSP
825 SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) {
826   auto emplace_result = m_global_vars.try_emplace(toOpaqueUid(var_id), nullptr);
827   if (emplace_result.second)
828     emplace_result.first->second = CreateGlobalVariable(var_id);
829 
830   return emplace_result.first->second;
831 }
832 
833 lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) {
834   return GetOrCreateType(PdbTypeSymId(ti, false));
835 }
836 
837 FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id,
838                                                     CompileUnit &comp_unit) {
839   auto emplace_result = m_functions.try_emplace(toOpaqueUid(func_id), nullptr);
840   if (emplace_result.second)
841     emplace_result.first->second = CreateFunction(func_id, comp_unit);
842 
843   return emplace_result.first->second;
844 }
845 
846 CompUnitSP
847 SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) {
848 
849   auto emplace_result =
850       m_compilands.try_emplace(toOpaqueUid(cci.m_id), nullptr);
851   if (emplace_result.second)
852     emplace_result.first->second = CreateCompileUnit(cci);
853 
854   lldbassert(emplace_result.first->second);
855   return emplace_result.first->second;
856 }
857 
858 Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
859   auto iter = m_blocks.find(toOpaqueUid(block_id));
860   if (iter != m_blocks.end())
861     return *iter->second;
862 
863   return CreateBlock(block_id);
864 }
865 
866 void SymbolFileNativePDB::ParseDeclsForContext(
867     lldb_private::CompilerDeclContext decl_ctx) {
868   clang::DeclContext *context = m_ast->FromCompilerDeclContext(decl_ctx);
869   if (!context)
870     return;
871   m_ast->ParseDeclsForContext(*context);
872 }
873 
874 lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) {
875   if (index >= GetNumCompileUnits())
876     return CompUnitSP();
877   lldbassert(index < UINT16_MAX);
878   if (index >= UINT16_MAX)
879     return nullptr;
880 
881   CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index);
882 
883   return GetOrCreateCompileUnit(item);
884 }
885 
886 lldb::LanguageType SymbolFileNativePDB::ParseLanguage(CompileUnit &comp_unit) {
887   PdbSymUid uid(comp_unit.GetID());
888   lldbassert(uid.kind() == PdbSymUidKind::Compiland);
889 
890   CompilandIndexItem *item =
891       m_index->compilands().GetCompiland(uid.asCompiland().modi);
892   lldbassert(item);
893   if (!item->m_compile_opts)
894     return lldb::eLanguageTypeUnknown;
895 
896   return TranslateLanguage(item->m_compile_opts->getLanguage());
897 }
898 
899 void SymbolFileNativePDB::AddSymbols(Symtab &symtab) { return; }
900 
901 size_t SymbolFileNativePDB::ParseFunctions(CompileUnit &comp_unit) {
902   PdbSymUid uid{comp_unit.GetID()};
903   lldbassert(uid.kind() == PdbSymUidKind::Compiland);
904   uint16_t modi = uid.asCompiland().modi;
905   CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi);
906 
907   size_t count = comp_unit.GetNumFunctions();
908   const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray();
909   for (auto iter = syms.begin(); iter != syms.end(); ++iter) {
910     if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32)
911       continue;
912 
913     PdbCompilandSymId sym_id{modi, iter.offset()};
914 
915     FunctionSP func = GetOrCreateFunction(sym_id, comp_unit);
916   }
917 
918   size_t new_count = comp_unit.GetNumFunctions();
919   lldbassert(new_count >= count);
920   return new_count - count;
921 }
922 
923 static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) {
924   // If any of these flags are set, we need to resolve the compile unit.
925   uint32_t flags = eSymbolContextCompUnit;
926   flags |= eSymbolContextVariable;
927   flags |= eSymbolContextFunction;
928   flags |= eSymbolContextBlock;
929   flags |= eSymbolContextLineEntry;
930   return (resolve_scope & flags) != 0;
931 }
932 
933 uint32_t SymbolFileNativePDB::ResolveSymbolContext(
934     const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) {
935   uint32_t resolved_flags = 0;
936   lldb::addr_t file_addr = addr.GetFileAddress();
937 
938   if (NeedsResolvedCompileUnit(resolve_scope)) {
939     llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr);
940     if (!modi)
941       return 0;
942     CompilandIndexItem *cci = m_index->compilands().GetCompiland(*modi);
943     if (!cci)
944       return 0;
945 
946     sc.comp_unit = GetOrCreateCompileUnit(*cci).get();
947     resolved_flags |= eSymbolContextCompUnit;
948   }
949 
950   if (resolve_scope & eSymbolContextFunction ||
951       resolve_scope & eSymbolContextBlock) {
952     lldbassert(sc.comp_unit);
953     std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr);
954     // Search the matches in reverse.  This way if there are multiple matches
955     // (for example we are 3 levels deep in a nested scope) it will find the
956     // innermost one first.
957     for (const auto &match : llvm::reverse(matches)) {
958       if (match.uid.kind() != PdbSymUidKind::CompilandSym)
959         continue;
960 
961       PdbCompilandSymId csid = match.uid.asCompilandSym();
962       CVSymbol cvs = m_index->ReadSymbolRecord(csid);
963       PDB_SymType type = CVSymToPDBSym(cvs.kind());
964       if (type != PDB_SymType::Function && type != PDB_SymType::Block)
965         continue;
966       if (type == PDB_SymType::Function) {
967         sc.function = GetOrCreateFunction(csid, *sc.comp_unit).get();
968         sc.block = sc.GetFunctionBlock();
969       }
970 
971       if (type == PDB_SymType::Block) {
972         sc.block = &GetOrCreateBlock(csid);
973         sc.function = sc.block->CalculateSymbolContextFunction();
974       }
975     resolved_flags |= eSymbolContextFunction;
976     resolved_flags |= eSymbolContextBlock;
977     break;
978     }
979   }
980 
981   if (resolve_scope & eSymbolContextLineEntry) {
982     lldbassert(sc.comp_unit);
983     if (auto *line_table = sc.comp_unit->GetLineTable()) {
984       if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
985         resolved_flags |= eSymbolContextLineEntry;
986     }
987   }
988 
989   return resolved_flags;
990 }
991 
992 uint32_t SymbolFileNativePDB::ResolveSymbolContext(
993     const FileSpec &file_spec, uint32_t line, bool check_inlines,
994     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
995   return 0;
996 }
997 
998 static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence,
999                                       const CompilandIndexItem &cci,
1000                                       lldb::addr_t base_addr,
1001                                       uint32_t file_number,
1002                                       const LineFragmentHeader &block,
1003                                       const LineNumberEntry &cur) {
1004   LineInfo cur_info(cur.Flags);
1005 
1006   if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto())
1007     return;
1008 
1009   uint64_t addr = base_addr + cur.Offset;
1010 
1011   bool is_statement = cur_info.isStatement();
1012   bool is_prologue = IsFunctionPrologue(cci, addr);
1013   bool is_epilogue = IsFunctionEpilogue(cci, addr);
1014 
1015   uint32_t lno = cur_info.getStartLine();
1016 
1017   table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number,
1018                                   is_statement, false, is_prologue, is_epilogue,
1019                                   false);
1020 }
1021 
1022 static void TerminateLineSequence(LineTable &table,
1023                                   const LineFragmentHeader &block,
1024                                   lldb::addr_t base_addr, uint32_t file_number,
1025                                   uint32_t last_line,
1026                                   std::unique_ptr<LineSequence> seq) {
1027   // The end is always a terminal entry, so insert it regardless.
1028   table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize,
1029                                   last_line, 0, file_number, false, false,
1030                                   false, false, true);
1031   table.InsertSequence(seq.release());
1032 }
1033 
1034 bool SymbolFileNativePDB::ParseLineTable(CompileUnit &comp_unit) {
1035   // Unfortunately LLDB is set up to parse the entire compile unit line table
1036   // all at once, even if all it really needs is line info for a specific
1037   // function.  In the future it would be nice if it could set the sc.m_function
1038   // member, and we could only get the line info for the function in question.
1039   PdbSymUid cu_id(comp_unit.GetID());
1040   lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1041   CompilandIndexItem *cci =
1042       m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
1043   lldbassert(cci);
1044   auto line_table = llvm::make_unique<LineTable>(&comp_unit);
1045 
1046   // This is basically a copy of the .debug$S subsections from all original COFF
1047   // object files merged together with address relocations applied.  We are
1048   // looking for all DEBUG_S_LINES subsections.
1049   for (const DebugSubsectionRecord &dssr :
1050        cci->m_debug_stream.getSubsectionsArray()) {
1051     if (dssr.kind() != DebugSubsectionKind::Lines)
1052       continue;
1053 
1054     DebugLinesSubsectionRef lines;
1055     llvm::BinaryStreamReader reader(dssr.getRecordData());
1056     if (auto EC = lines.initialize(reader)) {
1057       llvm::consumeError(std::move(EC));
1058       return false;
1059     }
1060 
1061     const LineFragmentHeader *lfh = lines.header();
1062     uint64_t virtual_addr =
1063         m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset);
1064 
1065     const auto &checksums = cci->m_strings.checksums().getArray();
1066     const auto &strings = cci->m_strings.strings();
1067     for (const LineColumnEntry &group : lines) {
1068       // Indices in this structure are actually offsets of records in the
1069       // DEBUG_S_FILECHECKSUMS subsection.  Those entries then have an index
1070       // into the global PDB string table.
1071       auto iter = checksums.at(group.NameIndex);
1072       if (iter == checksums.end())
1073         continue;
1074 
1075       llvm::Expected<llvm::StringRef> efn =
1076           strings.getString(iter->FileNameOffset);
1077       if (!efn) {
1078         llvm::consumeError(efn.takeError());
1079         continue;
1080       }
1081 
1082       // LLDB wants the index of the file in the list of support files.
1083       auto fn_iter = llvm::find(cci->m_file_list, *efn);
1084       lldbassert(fn_iter != cci->m_file_list.end());
1085       // LLDB support file indices are 1-based.
1086       uint32_t file_index =
1087           1 + std::distance(cci->m_file_list.begin(), fn_iter);
1088 
1089       std::unique_ptr<LineSequence> sequence(
1090           line_table->CreateLineSequenceContainer());
1091       lldbassert(!group.LineNumbers.empty());
1092 
1093       for (const LineNumberEntry &entry : group.LineNumbers) {
1094         AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr,
1095                                   file_index, *lfh, entry);
1096       }
1097       LineInfo last_line(group.LineNumbers.back().Flags);
1098       TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index,
1099                             last_line.getEndLine(), std::move(sequence));
1100     }
1101   }
1102 
1103   if (line_table->GetSize() == 0)
1104     return false;
1105 
1106   comp_unit.SetLineTable(line_table.release());
1107   return true;
1108 }
1109 
1110 bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit &comp_unit) {
1111   // PDB doesn't contain information about macros
1112   return false;
1113 }
1114 
1115 bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit &comp_unit,
1116                                             FileSpecList &support_files) {
1117   PdbSymUid cu_id(comp_unit.GetID());
1118   lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1119   CompilandIndexItem *cci =
1120       m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
1121   lldbassert(cci);
1122 
1123   for (llvm::StringRef f : cci->m_file_list) {
1124     FileSpec::Style style =
1125         f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows;
1126     FileSpec spec(f, style);
1127     support_files.Append(spec);
1128   }
1129 
1130   llvm::SmallString<64> main_source_file =
1131       m_index->compilands().GetMainSourceFile(*cci);
1132   FileSpec::Style style = main_source_file.startswith("/")
1133                               ? FileSpec::Style::posix
1134                               : FileSpec::Style::windows;
1135   FileSpec spec(main_source_file, style);
1136   support_files.Insert(0, spec);
1137   return true;
1138 }
1139 
1140 bool SymbolFileNativePDB::ParseImportedModules(
1141     const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
1142   // PDB does not yet support module debug info
1143   return false;
1144 }
1145 
1146 size_t SymbolFileNativePDB::ParseBlocksRecursive(Function &func) {
1147   GetOrCreateBlock(PdbSymUid(func.GetID()).asCompilandSym());
1148   // FIXME: Parse child blocks
1149   return 1;
1150 }
1151 
1152 void SymbolFileNativePDB::DumpClangAST(Stream &s) { m_ast->Dump(s); }
1153 
1154 uint32_t SymbolFileNativePDB::FindGlobalVariables(
1155     ConstString name, const CompilerDeclContext *parent_decl_ctx,
1156     uint32_t max_matches, VariableList &variables) {
1157   using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1158 
1159   std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName(
1160       name.GetStringRef(), m_index->symrecords());
1161   for (const SymbolAndOffset &result : results) {
1162     VariableSP var;
1163     switch (result.second.kind()) {
1164     case SymbolKind::S_GDATA32:
1165     case SymbolKind::S_LDATA32:
1166     case SymbolKind::S_GTHREAD32:
1167     case SymbolKind::S_LTHREAD32:
1168     case SymbolKind::S_CONSTANT: {
1169       PdbGlobalSymId global(result.first, false);
1170       var = GetOrCreateGlobalVariable(global);
1171       variables.AddVariable(var);
1172       break;
1173     }
1174     default:
1175       continue;
1176     }
1177   }
1178   return variables.GetSize();
1179 }
1180 
1181 uint32_t SymbolFileNativePDB::FindFunctions(
1182     ConstString name, const CompilerDeclContext *parent_decl_ctx,
1183     FunctionNameType name_type_mask, bool include_inlines, bool append,
1184     SymbolContextList &sc_list) {
1185   // For now we only support lookup by method name.
1186   if (!(name_type_mask & eFunctionNameTypeMethod))
1187     return 0;
1188 
1189   using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1190 
1191   std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName(
1192       name.GetStringRef(), m_index->symrecords());
1193   for (const SymbolAndOffset &match : matches) {
1194     if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF)
1195       continue;
1196     ProcRefSym proc(match.second.kind());
1197     cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc));
1198 
1199     if (!IsValidRecord(proc))
1200       continue;
1201 
1202     CompilandIndexItem &cci =
1203         m_index->compilands().GetOrCreateCompiland(proc.modi());
1204     SymbolContext sc;
1205 
1206     sc.comp_unit = GetOrCreateCompileUnit(cci).get();
1207     PdbCompilandSymId func_id(proc.modi(), proc.SymOffset);
1208     sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get();
1209 
1210     sc_list.Append(sc);
1211   }
1212 
1213   return sc_list.GetSize();
1214 }
1215 
1216 uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression &regex,
1217                                             bool include_inlines, bool append,
1218                                             SymbolContextList &sc_list) {
1219   return 0;
1220 }
1221 
1222 uint32_t SymbolFileNativePDB::FindTypes(
1223     ConstString name, const CompilerDeclContext *parent_decl_ctx,
1224     bool append, uint32_t max_matches,
1225     llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
1226   if (!append)
1227     types.Clear();
1228   if (!name)
1229     return 0;
1230 
1231   searched_symbol_files.clear();
1232   searched_symbol_files.insert(this);
1233 
1234   // There is an assumption 'name' is not a regex
1235   size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types);
1236 
1237   return match_count;
1238 }
1239 
1240 size_t
1241 SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context,
1242                                bool append, TypeMap &types) {
1243   return 0;
1244 }
1245 
1246 size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name,
1247                                             uint32_t max_matches,
1248                                             TypeMap &types) {
1249 
1250   size_t match_count = 0;
1251   std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name);
1252   if (max_matches > 0 && max_matches < matches.size())
1253     matches.resize(max_matches);
1254 
1255   for (TypeIndex ti : matches) {
1256     TypeSP type = GetOrCreateType(ti);
1257     if (!type)
1258       continue;
1259 
1260     types.Insert(type);
1261     ++match_count;
1262   }
1263   return match_count;
1264 }
1265 
1266 size_t SymbolFileNativePDB::ParseTypes(CompileUnit &comp_unit) {
1267   // Only do the full type scan the first time.
1268   if (m_done_full_type_scan)
1269     return 0;
1270 
1271   size_t old_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1272   LazyRandomTypeCollection &types = m_index->tpi().typeCollection();
1273 
1274   // First process the entire TPI stream.
1275   for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) {
1276     TypeSP type = GetOrCreateType(*ti);
1277     if (type)
1278       (void)type->GetFullCompilerType();
1279   }
1280 
1281   // Next look for S_UDT records in the globals stream.
1282   for (const uint32_t gid : m_index->globals().getGlobalsTable()) {
1283     PdbGlobalSymId global{gid, false};
1284     CVSymbol sym = m_index->ReadSymbolRecord(global);
1285     if (sym.kind() != S_UDT)
1286       continue;
1287 
1288     UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1289     bool is_typedef = true;
1290     if (IsTagRecord(PdbTypeSymId{udt.Type, false}, m_index->tpi())) {
1291       CVType cvt = m_index->tpi().getType(udt.Type);
1292       llvm::StringRef name = CVTagRecord::create(cvt).name();
1293       if (name == udt.Name)
1294         is_typedef = false;
1295     }
1296 
1297     if (is_typedef)
1298       GetOrCreateTypedef(global);
1299   }
1300 
1301   size_t new_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1302 
1303   m_done_full_type_scan = true;
1304 
1305   return new_count - old_count;
1306 }
1307 
1308 size_t
1309 SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit,
1310                                                   VariableList &variables) {
1311   PdbSymUid sym_uid(comp_unit.GetID());
1312   lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland);
1313   return 0;
1314 }
1315 
1316 VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
1317                                                     PdbCompilandSymId var_id,
1318                                                     bool is_param) {
1319   ModuleSP module = GetObjectFile()->GetModule();
1320   Block &block = GetOrCreateBlock(scope_id);
1321   VariableInfo var_info =
1322       GetVariableLocationInfo(*m_index, var_id, block, module);
1323   if (!var_info.location || !var_info.ranges)
1324     return nullptr;
1325 
1326   CompilandIndexItem *cii = m_index->compilands().GetCompiland(var_id.modi);
1327   CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(*cii);
1328   TypeSP type_sp = GetOrCreateType(var_info.type);
1329   std::string name = var_info.name.str();
1330   Declaration decl;
1331   SymbolFileTypeSP sftype =
1332       std::make_shared<SymbolFileType>(*this, type_sp->GetID());
1333 
1334   ValueType var_scope =
1335       is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal;
1336   VariableSP var_sp = std::make_shared<Variable>(
1337       toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope,
1338       comp_unit_sp.get(), *var_info.ranges, &decl, *var_info.location, false,
1339       false, false);
1340 
1341   if (!is_param)
1342     m_ast->GetOrCreateVariableDecl(scope_id, var_id);
1343 
1344   m_local_variables[toOpaqueUid(var_id)] = var_sp;
1345   return var_sp;
1346 }
1347 
1348 VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable(
1349     PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) {
1350   auto iter = m_local_variables.find(toOpaqueUid(var_id));
1351   if (iter != m_local_variables.end())
1352     return iter->second;
1353 
1354   return CreateLocalVariable(scope_id, var_id, is_param);
1355 }
1356 
1357 TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) {
1358   CVSymbol sym = m_index->ReadSymbolRecord(id);
1359   lldbassert(sym.kind() == SymbolKind::S_UDT);
1360 
1361   UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1362 
1363   TypeSP target_type = GetOrCreateType(udt.Type);
1364 
1365   (void)m_ast->GetOrCreateTypedefDecl(id);
1366 
1367   Declaration decl;
1368   return std::make_shared<lldb_private::Type>(
1369       toOpaqueUid(id), this, ConstString(udt.Name), target_type->GetByteSize(),
1370       nullptr, target_type->GetID(), lldb_private::Type::eEncodingIsTypedefUID,
1371       decl, target_type->GetForwardCompilerType(),
1372       lldb_private::Type::eResolveStateForward);
1373 }
1374 
1375 TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) {
1376   auto iter = m_types.find(toOpaqueUid(id));
1377   if (iter != m_types.end())
1378     return iter->second;
1379 
1380   return CreateTypedef(id);
1381 }
1382 
1383 size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
1384   Block &block = GetOrCreateBlock(block_id);
1385 
1386   size_t count = 0;
1387 
1388   CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
1389   CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
1390   uint32_t params_remaining = 0;
1391   switch (sym.kind()) {
1392   case S_GPROC32:
1393   case S_LPROC32: {
1394     ProcSym proc(static_cast<SymbolRecordKind>(sym.kind()));
1395     cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym, proc));
1396     CVType signature = m_index->tpi().getType(proc.FunctionType);
1397     ProcedureRecord sig;
1398     cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(signature, sig));
1399     params_remaining = sig.getParameterCount();
1400     break;
1401   }
1402   case S_BLOCK32:
1403     break;
1404   default:
1405     lldbassert(false && "Symbol is not a block!");
1406     return 0;
1407   }
1408 
1409   VariableListSP variables = block.GetBlockVariableList(false);
1410   if (!variables) {
1411     variables = std::make_shared<VariableList>();
1412     block.SetVariableList(variables);
1413   }
1414 
1415   CVSymbolArray syms = limitSymbolArrayToScope(
1416       cii->m_debug_stream.getSymbolArray(), block_id.offset);
1417 
1418   // Skip the first record since it's a PROC32 or BLOCK32, and there's
1419   // no point examining it since we know it's not a local variable.
1420   syms.drop_front();
1421   auto iter = syms.begin();
1422   auto end = syms.end();
1423 
1424   while (iter != end) {
1425     uint32_t record_offset = iter.offset();
1426     CVSymbol variable_cvs = *iter;
1427     PdbCompilandSymId child_sym_id(block_id.modi, record_offset);
1428     ++iter;
1429 
1430     // If this is a block, recurse into its children and then skip it.
1431     if (variable_cvs.kind() == S_BLOCK32) {
1432       uint32_t block_end = getScopeEndOffset(variable_cvs);
1433       count += ParseVariablesForBlock(child_sym_id);
1434       iter = syms.at(block_end);
1435       continue;
1436     }
1437 
1438     bool is_param = params_remaining > 0;
1439     VariableSP variable;
1440     switch (variable_cvs.kind()) {
1441     case S_REGREL32:
1442     case S_REGISTER:
1443     case S_LOCAL:
1444       variable = GetOrCreateLocalVariable(block_id, child_sym_id, is_param);
1445       if (is_param)
1446         --params_remaining;
1447       if (variable)
1448         variables->AddVariableIfUnique(variable);
1449       break;
1450     default:
1451       break;
1452     }
1453   }
1454 
1455   // Pass false for set_children, since we call this recursively so that the
1456   // children will call this for themselves.
1457   block.SetDidParseVariables(true, false);
1458 
1459   return count;
1460 }
1461 
1462 size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) {
1463   lldbassert(sc.function || sc.comp_unit);
1464 
1465   VariableListSP variables;
1466   if (sc.block) {
1467     PdbSymUid block_id(sc.block->GetID());
1468 
1469     size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1470     return count;
1471   }
1472 
1473   if (sc.function) {
1474     PdbSymUid block_id(sc.function->GetID());
1475 
1476     size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1477     return count;
1478   }
1479 
1480   if (sc.comp_unit) {
1481     variables = sc.comp_unit->GetVariableList(false);
1482     if (!variables) {
1483       variables = std::make_shared<VariableList>();
1484       sc.comp_unit->SetVariableList(variables);
1485     }
1486     return ParseVariablesForCompileUnit(*sc.comp_unit, *variables);
1487   }
1488 
1489   llvm_unreachable("Unreachable!");
1490 }
1491 
1492 CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) {
1493   clang::Decl *decl = m_ast->GetOrCreateDeclForUid(PdbSymUid(uid));
1494 
1495   return m_ast->ToCompilerDecl(*decl);
1496 }
1497 
1498 CompilerDeclContext
1499 SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) {
1500   clang::DeclContext *context =
1501       m_ast->GetOrCreateDeclContextForUid(PdbSymUid(uid));
1502   if (!context)
1503     return {};
1504 
1505   return m_ast->ToCompilerDeclContext(*context);
1506 }
1507 
1508 CompilerDeclContext
1509 SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
1510   clang::DeclContext *context = m_ast->GetParentDeclContext(PdbSymUid(uid));
1511   return m_ast->ToCompilerDeclContext(*context);
1512 }
1513 
1514 Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
1515   auto iter = m_types.find(type_uid);
1516   // lldb should not be passing us non-sensical type uids.  the only way it
1517   // could have a type uid in the first place is if we handed it out, in which
1518   // case we should know about the type.  However, that doesn't mean we've
1519   // instantiated it yet.  We can vend out a UID for a future type.  So if the
1520   // type doesn't exist, let's instantiate it now.
1521   if (iter != m_types.end())
1522     return &*iter->second;
1523 
1524   PdbSymUid uid(type_uid);
1525   lldbassert(uid.kind() == PdbSymUidKind::Type);
1526   PdbTypeSymId type_id = uid.asTypeSym();
1527   if (type_id.index.isNoneType())
1528     return nullptr;
1529 
1530   TypeSP type_sp = CreateAndCacheType(type_id);
1531   return &*type_sp;
1532 }
1533 
1534 llvm::Optional<SymbolFile::ArrayInfo>
1535 SymbolFileNativePDB::GetDynamicArrayInfoForUID(
1536     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1537   return llvm::None;
1538 }
1539 
1540 
1541 bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) {
1542   clang::QualType qt =
1543       clang::QualType::getFromOpaquePtr(compiler_type.GetOpaqueQualType());
1544 
1545   return m_ast->CompleteType(qt);
1546 }
1547 
1548 size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1549                                      TypeClass type_mask,
1550                                      lldb_private::TypeList &type_list) {
1551   return 0;
1552 }
1553 
1554 CompilerDeclContext
1555 SymbolFileNativePDB::FindNamespace(ConstString name,
1556                                    const CompilerDeclContext *parent_decl_ctx) {
1557   return {};
1558 }
1559 
1560 TypeSystem *
1561 SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1562   auto type_system =
1563       m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1564   if (type_system)
1565     type_system->SetSymbolFile(this);
1566   return type_system;
1567 }
1568 
1569 ConstString SymbolFileNativePDB::GetPluginName() {
1570   static ConstString g_name("pdb");
1571   return g_name;
1572 }
1573 
1574 uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; }
1575