blob: 9868a3283e04220ca33030114ece299ffd6b9c89 [file] [log] [blame]
Zachary Turner307f5ae2018-10-12 19:47:13 +00001//===-- SymbolFileNativePDB.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileNativePDB.h"
11
Zachary Turner2f7efbc2018-10-23 16:37:53 +000012#include "clang/AST/Attr.h"
13#include "clang/AST/CharUnits.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000016#include "clang/AST/Type.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000017
Zachary Turner307f5ae2018-10-12 19:47:13 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
Zachary Turner9f727952018-10-26 09:06:38 +000020#include "lldb/Core/StreamBuffer.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000021#include "lldb/Core/StreamFile.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000022#include "lldb/Symbol/ClangASTContext.h"
23#include "lldb/Symbol/ClangASTImporter.h"
24#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000025#include "lldb/Symbol/ClangUtil.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000026#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"
Zachary Turner9f727952018-10-26 09:06:38 +000031#include "lldb/Symbol/Variable.h"
32#include "lldb/Symbol/VariableList.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000033
34#include "llvm/DebugInfo/CodeView/CVRecord.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000035#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000036#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000037#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000038#include "llvm/DebugInfo/CodeView/RecordName.h"
39#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
Zachary Turnera42bbe32018-12-07 19:34:02 +000040#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000041#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000042#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"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000048#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000049#include "llvm/DebugInfo/PDB/PDBTypes.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000050#include "llvm/Demangle/MicrosoftDemangle.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000051#include "llvm/Object/COFF.h"
52#include "llvm/Support/Allocator.h"
53#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000054#include "llvm/Support/Error.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000055#include "llvm/Support/ErrorOr.h"
56#include "llvm/Support/MemoryBuffer.h"
57
Zachary Turnera93458b2018-12-06 17:49:15 +000058#include "DWARFLocationExpression.h"
Zachary Turner594c85e2018-12-17 19:43:33 +000059#include "PdbAstBuilder.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000060#include "PdbSymUid.h"
61#include "PdbUtil.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000062#include "UdtRecordCompleter.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000063
64using namespace lldb;
65using namespace lldb_private;
Zachary Turner2f7efbc2018-10-23 16:37:53 +000066using namespace npdb;
Zachary Turner307f5ae2018-10-12 19:47:13 +000067using namespace llvm::codeview;
68using namespace llvm::pdb;
69
70static 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 default:
77 return lldb::LanguageType::eLanguageTypeUnknown;
78 }
79}
80
81static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath,
82 llvm::BumpPtrAllocator &Allocator) {
83 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
84 llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1,
85 /*RequiresNullTerminator=*/false);
86 if (!ErrorOrBuffer)
87 return nullptr;
88 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
89
90 llvm::StringRef Path = Buffer->getBufferIdentifier();
91 auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>(
92 std::move(Buffer), llvm::support::little);
93
94 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +000095 if (auto EC = File->parseFileHeaders()) {
96 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +000097 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +000098 }
99 if (auto EC = File->parseStreamData()) {
100 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +0000101 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +0000102 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000103
104 return File;
105}
106
107static std::unique_ptr<PDBFile>
108loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) {
109 // Try to find a matching PDB for an EXE.
110 using namespace llvm::object;
111 auto expected_binary = createBinary(exe_path);
112
113 // If the file isn't a PE/COFF executable, fail.
114 if (!expected_binary) {
115 llvm::consumeError(expected_binary.takeError());
116 return nullptr;
117 }
118 OwningBinary<Binary> binary = std::move(*expected_binary);
119
120 auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary());
121 if (!obj)
122 return nullptr;
123 const llvm::codeview::DebugInfo *pdb_info = nullptr;
124
125 // If it doesn't have a debug directory, fail.
126 llvm::StringRef pdb_file;
127 auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file);
128 if (ec)
129 return nullptr;
130
131 // if the file doesn't exist, is not a pdb, or doesn't have a matching guid,
132 // fail.
133 llvm::file_magic magic;
134 ec = llvm::identify_magic(pdb_file, magic);
135 if (ec || magic != llvm::file_magic::pdb)
136 return nullptr;
137 std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +0000138 if (!pdb)
139 return nullptr;
140
Zachary Turner307f5ae2018-10-12 19:47:13 +0000141 auto expected_info = pdb->getPDBInfoStream();
142 if (!expected_info) {
143 llvm::consumeError(expected_info.takeError());
144 return nullptr;
145 }
146 llvm::codeview::GUID guid;
147 memcpy(&guid, pdb_info->PDB70.Signature, 16);
148
149 if (expected_info->getGuid() != guid)
150 return nullptr;
151 return pdb;
152}
153
154static bool IsFunctionPrologue(const CompilandIndexItem &cci,
155 lldb::addr_t addr) {
156 // FIXME: Implement this.
157 return false;
158}
159
160static bool IsFunctionEpilogue(const CompilandIndexItem &cci,
161 lldb::addr_t addr) {
162 // FIXME: Implement this.
163 return false;
164}
165
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000166static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) {
167 switch (kind) {
168 case SimpleTypeKind::Boolean128:
169 case SimpleTypeKind::Boolean16:
170 case SimpleTypeKind::Boolean32:
171 case SimpleTypeKind::Boolean64:
172 case SimpleTypeKind::Boolean8:
173 return "bool";
174 case SimpleTypeKind::Byte:
175 case SimpleTypeKind::UnsignedCharacter:
176 return "unsigned char";
177 case SimpleTypeKind::NarrowCharacter:
178 return "char";
179 case SimpleTypeKind::SignedCharacter:
180 case SimpleTypeKind::SByte:
Zachary Turner71ebb722018-10-23 22:15:05 +0000181 return "signed char";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000182 case SimpleTypeKind::Character16:
183 return "char16_t";
184 case SimpleTypeKind::Character32:
185 return "char32_t";
186 case SimpleTypeKind::Complex80:
187 case SimpleTypeKind::Complex64:
188 case SimpleTypeKind::Complex32:
189 return "complex";
190 case SimpleTypeKind::Float128:
191 case SimpleTypeKind::Float80:
192 return "long double";
193 case SimpleTypeKind::Float64:
194 return "double";
195 case SimpleTypeKind::Float32:
196 return "float";
197 case SimpleTypeKind::Float16:
198 return "single";
199 case SimpleTypeKind::Int128:
200 return "__int128";
201 case SimpleTypeKind::Int64:
202 case SimpleTypeKind::Int64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000203 return "int64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000204 case SimpleTypeKind::Int32:
205 return "int";
206 case SimpleTypeKind::Int16:
207 return "short";
208 case SimpleTypeKind::UInt128:
209 return "unsigned __int128";
210 case SimpleTypeKind::UInt64:
211 case SimpleTypeKind::UInt64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000212 return "uint64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000213 case SimpleTypeKind::HResult:
214 return "HRESULT";
215 case SimpleTypeKind::UInt32:
216 return "unsigned";
217 case SimpleTypeKind::UInt16:
218 case SimpleTypeKind::UInt16Short:
219 return "unsigned short";
220 case SimpleTypeKind::Int32Long:
221 return "long";
222 case SimpleTypeKind::UInt32Long:
223 return "unsigned long";
224 case SimpleTypeKind::Void:
225 return "void";
226 case SimpleTypeKind::WideCharacter:
227 return "wchar_t";
228 default:
229 return "";
230 }
231}
232
233static bool IsClassRecord(TypeLeafKind kind) {
234 switch (kind) {
235 case LF_STRUCTURE:
236 case LF_CLASS:
237 case LF_INTERFACE:
238 return true;
239 default:
240 return false;
241 }
242}
243
Zachary Turner307f5ae2018-10-12 19:47:13 +0000244void SymbolFileNativePDB::Initialize() {
245 PluginManager::RegisterPlugin(GetPluginNameStatic(),
246 GetPluginDescriptionStatic(), CreateInstance,
247 DebuggerInitialize);
248}
249
250void SymbolFileNativePDB::Terminate() {
251 PluginManager::UnregisterPlugin(CreateInstance);
252}
253
Zachary Turnerb96181c2018-10-22 16:19:07 +0000254void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {}
Zachary Turner307f5ae2018-10-12 19:47:13 +0000255
Zachary Turnerb96181c2018-10-22 16:19:07 +0000256ConstString SymbolFileNativePDB::GetPluginNameStatic() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000257 static ConstString g_name("native-pdb");
258 return g_name;
259}
260
261const char *SymbolFileNativePDB::GetPluginDescriptionStatic() {
262 return "Microsoft PDB debug symbol cross-platform file reader.";
263}
264
Zachary Turnerb96181c2018-10-22 16:19:07 +0000265SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000266 return new SymbolFileNativePDB(obj_file);
267}
268
Zachary Turnerb96181c2018-10-22 16:19:07 +0000269SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000270 : SymbolFile(object_file) {}
271
272SymbolFileNativePDB::~SymbolFileNativePDB() {}
273
274uint32_t SymbolFileNativePDB::CalculateAbilities() {
275 uint32_t abilities = 0;
276 if (!m_obj_file)
277 return 0;
278
279 if (!m_index) {
280 // Lazily load and match the PDB file, but only do this once.
281 std::unique_ptr<PDBFile> file_up =
282 loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator);
283
284 if (!file_up) {
285 auto module_sp = m_obj_file->GetModule();
286 if (!module_sp)
287 return 0;
288 // See if any symbol file is specified through `--symfile` option.
289 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
290 if (!symfile)
291 return 0;
292 file_up = loadPDBFile(symfile.GetPath(), m_allocator);
293 }
294
295 if (!file_up)
296 return 0;
297
298 auto expected_index = PdbIndex::create(std::move(file_up));
299 if (!expected_index) {
300 llvm::consumeError(expected_index.takeError());
301 return 0;
302 }
303 m_index = std::move(*expected_index);
304 }
305 if (!m_index)
306 return 0;
307
308 // We don't especially have to be precise here. We only distinguish between
309 // stripped and not stripped.
310 abilities = kAllAbilities;
311
312 if (m_index->dbi().isStripped())
313 abilities &= ~(Blocks | LocalVariables);
314 return abilities;
315}
316
317void SymbolFileNativePDB::InitializeObject() {
318 m_obj_load_address = m_obj_file->GetFileOffset();
319 m_index->SetLoadAddress(m_obj_load_address);
320 m_index->ParseSectionContribs();
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000321
Zachary Turner594c85e2018-12-17 19:43:33 +0000322 TypeSystem *ts = m_obj_file->GetModule()->GetTypeSystemForLanguage(
323 lldb::eLanguageTypeC_plus_plus);
324 if (ts)
325 ts->SetSymbolFile(this);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000326
Zachary Turner594c85e2018-12-17 19:43:33 +0000327 m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000328}
329
Zachary Turner307f5ae2018-10-12 19:47:13 +0000330uint32_t SymbolFileNativePDB::GetNumCompileUnits() {
331 const DbiModuleList &modules = m_index->dbi().modules();
332 uint32_t count = modules.getModuleCount();
333 if (count == 0)
334 return count;
335
336 // The linker can inject an additional "dummy" compilation unit into the
337 // PDB. Ignore this special compile unit for our purposes, if it is there.
338 // It is always the last one.
339 DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1);
340 if (last.getModuleName() == "* Linker *")
341 --count;
342 return count;
343}
344
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000345Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
346 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
347 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000348
349 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) {
350 // This is a function. It must be global. Creating the Function entry for
351 // it automatically creates a block for it.
352 CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii);
353 return GetOrCreateFunction(block_id, *comp_unit)->GetBlock(false);
354 }
355
356 lldbassert(sym.kind() == S_BLOCK32);
357
358 // This is a block. Its parent is either a function or another block. In
359 // either case, its parent can be viewed as a block (e.g. a function contains
360 // 1 big block. So just get the parent block and add this block to it.
361 BlockSym block(static_cast<SymbolRecordKind>(sym.kind()));
362 cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block));
363 lldbassert(block.Parent != 0);
364 PdbCompilandSymId parent_id(block_id.modi, block.Parent);
365 Block &parent_block = GetOrCreateBlock(parent_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000366 lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id);
367 BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
368 parent_block.AddChild(child_block);
Zachary Turner594c85e2018-12-17 19:43:33 +0000369
370 m_ast->GetOrCreateBlockDecl(block_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000371
372 m_blocks.insert({opaque_block_uid, child_block});
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000373 return *child_block;
374}
375
Zachary Turner6284aee2018-11-16 02:42:32 +0000376lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000377 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000378 const CompilandIndexItem *cci =
379 m_index->compilands().GetCompiland(func_id.modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000380 lldbassert(cci);
Zachary Turner6284aee2018-11-16 02:42:32 +0000381 CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(func_id.offset);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000382
383 lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32);
384 SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record);
385
386 auto file_vm_addr = m_index->MakeVirtualAddress(sol.so);
387 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
388 return nullptr;
389
390 AddressRange func_range(file_vm_addr, sol.length,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000391 comp_unit.GetModule()->GetSectionList());
Zachary Turner307f5ae2018-10-12 19:47:13 +0000392 if (!func_range.GetBaseAddress().IsValid())
393 return nullptr;
394
Zachary Turnera42bbe32018-12-07 19:34:02 +0000395 ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind()));
396 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym_record, proc));
397 TypeSP func_type = GetOrCreateType(proc.FunctionType);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000398
Zachary Turnera42bbe32018-12-07 19:34:02 +0000399 PdbTypeSymId sig_id(proc.FunctionType, false);
400 Mangled mangled(proc.Name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000401 FunctionSP func_sp = std::make_shared<Function>(
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000402 &comp_unit, toOpaqueUid(func_id), toOpaqueUid(sig_id), mangled,
Zachary Turnera42bbe32018-12-07 19:34:02 +0000403 func_type.get(), func_range);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000404
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000405 comp_unit.AddFunction(func_sp);
406
Zachary Turner594c85e2018-12-17 19:43:33 +0000407 m_ast->GetOrCreateFunctionDecl(func_id);
Zachary Turnera42bbe32018-12-07 19:34:02 +0000408
Zachary Turner307f5ae2018-10-12 19:47:13 +0000409 return func_sp;
410}
411
412CompUnitSP
413SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) {
414 lldb::LanguageType lang =
415 cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage())
416 : lldb::eLanguageTypeUnknown;
417
418 LazyBool optimized = eLazyBoolNo;
419 if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations())
420 optimized = eLazyBoolYes;
421
Zachary Turner51f88af2018-12-19 19:45:30 +0000422 llvm::SmallString<64> source_file_name =
Zachary Turner307f5ae2018-10-12 19:47:13 +0000423 m_index->compilands().GetMainSourceFile(cci);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000424 FileSpec fs(source_file_name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000425
426 CompUnitSP cu_sp =
427 std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs,
Zachary Turner6284aee2018-11-16 02:42:32 +0000428 toOpaqueUid(cci.m_id), lang, optimized);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000429
Zachary Turner6284aee2018-11-16 02:42:32 +0000430 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
431 cci.m_id.modi, cu_sp);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000432 return cu_sp;
433}
434
Zachary Turner6284aee2018-11-16 02:42:32 +0000435lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000436 const ModifierRecord &mr,
437 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000438 TpiStream &stream = m_index->tpi();
439
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000440 std::string name;
441 if (mr.ModifiedType.isSimple())
442 name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind());
443 else
444 name = computeTypeName(stream.typeCollection(), mr.ModifiedType);
445 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000446 lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType);
447
448 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(name),
449 modified_type->GetByteSize(), nullptr,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000450 LLDB_INVALID_UID, Type::eEncodingIsUID, decl,
451 ct, Type::eResolveStateFull);
452}
453
Zachary Turner594c85e2018-12-17 19:43:33 +0000454lldb::TypeSP
455SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id,
456 const llvm::codeview::PointerRecord &pr,
457 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000458 TypeSP pointee = GetOrCreateType(pr.ReferentType);
Zachary Turner544a66d82018-11-01 16:37:29 +0000459 if (!pointee)
460 return nullptr;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000461
462 if (pr.isPointerToMember()) {
463 MemberPointerInfo mpi = pr.getMemberInfo();
Zachary Turner594c85e2018-12-17 19:43:33 +0000464 GetOrCreateType(mpi.ContainingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000465 }
466
Zachary Turner594c85e2018-12-17 19:43:33 +0000467 Declaration decl;
468 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(),
469 pr.getSize(), nullptr, LLDB_INVALID_UID,
470 Type::eEncodingIsUID, decl, ct,
471 Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000472}
473
Zachary Turner594c85e2018-12-17 19:43:33 +0000474lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti,
475 CompilerType ct) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000476 uint64_t uid = toOpaqueUid(PdbTypeSymId(ti, false));
Zachary Turner544a66d82018-11-01 16:37:29 +0000477 if (ti == TypeIndex::NullptrT()) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000478 Declaration decl;
Zachary Turner6284aee2018-11-16 02:42:32 +0000479 return std::make_shared<Type>(
480 uid, this, ConstString("std::nullptr_t"), 0, nullptr, LLDB_INVALID_UID,
481 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner544a66d82018-11-01 16:37:29 +0000482 }
483
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000484 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000485 TypeSP direct_sp = GetOrCreateType(ti.makeDirect());
Zachary Turner71ebb722018-10-23 22:15:05 +0000486 uint32_t pointer_size = 0;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000487 switch (ti.getSimpleMode()) {
488 case SimpleTypeMode::FarPointer32:
489 case SimpleTypeMode::NearPointer32:
490 pointer_size = 4;
491 break;
492 case SimpleTypeMode::NearPointer64:
493 pointer_size = 8;
494 break;
495 default:
496 // 128-bit and 16-bit pointers unsupported.
497 return nullptr;
498 }
499 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000500 return std::make_shared<Type>(
501 uid, this, ConstString(), pointer_size, nullptr, LLDB_INVALID_UID,
502 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000503 }
504
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000505 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
506 return nullptr;
507
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000508 size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind());
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000509 llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind());
510
511 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000512 return std::make_shared<Type>(uid, this, ConstString(type_name), size,
513 nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID,
514 decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000515}
516
Zachary Turner594c85e2018-12-17 19:43:33 +0000517static std::string GetUnqualifiedTypeName(const TagRecord &record) {
Zachary Turner056e4ab2018-11-08 18:50:11 +0000518 llvm::ms_demangle::Demangler demangler;
519 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
520 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
521 llvm::ms_demangle::IdentifierNode *idn =
522 ttn->QualifiedName->getUnqualifiedIdentifier();
Zachary Turner594c85e2018-12-17 19:43:33 +0000523 return idn->toString();
Zachary Turner056e4ab2018-11-08 18:50:11 +0000524}
525
Zachary Turner594c85e2018-12-17 19:43:33 +0000526lldb::TypeSP
527SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id,
528 const TagRecord &record,
529 size_t size, CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000530
Zachary Turner594c85e2018-12-17 19:43:33 +0000531 std::string uname = GetUnqualifiedTypeName(record);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000532
533 // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE.
534 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000535 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(uname),
536 size, nullptr, LLDB_INVALID_UID,
537 Type::eEncodingIsUID, decl, ct,
538 Type::eResolveStateForward);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000539}
540
Zachary Turner6284aee2018-11-16 02:42:32 +0000541lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000542 const ClassRecord &cr,
543 CompilerType ct) {
544 return CreateClassStructUnion(type_id, cr, cr.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000545}
546
Zachary Turner6284aee2018-11-16 02:42:32 +0000547lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000548 const UnionRecord &ur,
549 CompilerType ct) {
550 return CreateClassStructUnion(type_id, ur, ur.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000551}
552
Zachary Turner6284aee2018-11-16 02:42:32 +0000553lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000554 const EnumRecord &er,
555 CompilerType ct) {
556 std::string uname = GetUnqualifiedTypeName(er);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000557
558 Declaration decl;
559 TypeSP underlying_type = GetOrCreateType(er.UnderlyingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000560
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000561 return std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000562 toOpaqueUid(type_id), this, ConstString(uname),
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000563 underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000564 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000565 lldb_private::Type::eResolveStateForward);
566}
567
Zachary Turner6284aee2018-11-16 02:42:32 +0000568TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000569 const ArrayRecord &ar,
570 CompilerType ct) {
Zachary Turner511bff22018-10-30 18:57:08 +0000571 TypeSP element_type = GetOrCreateType(ar.ElementType);
Zachary Turner511bff22018-10-30 18:57:08 +0000572
573 Declaration decl;
574 TypeSP array_sp = std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000575 toOpaqueUid(type_id), this, ConstString(), ar.Size, nullptr,
576 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct,
577 lldb_private::Type::eResolveStateFull);
Zachary Turner511bff22018-10-30 18:57:08 +0000578 array_sp->SetEncodingType(element_type.get());
579 return array_sp;
580}
581
Zachary Turner6284aee2018-11-16 02:42:32 +0000582TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000583 const ProcedureRecord &pr,
584 CompilerType ct) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000585 Declaration decl;
586 return std::make_shared<lldb_private::Type>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000587 toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000588 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner544a66d82018-11-01 16:37:29 +0000589 lldb_private::Type::eResolveStateFull);
590}
591
Zachary Turner594c85e2018-12-17 19:43:33 +0000592TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000593 if (type_id.index.isSimple())
Zachary Turner594c85e2018-12-17 19:43:33 +0000594 return CreateSimpleType(type_id.index, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000595
Zachary Turner6284aee2018-11-16 02:42:32 +0000596 TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi();
597 CVType cvt = stream.getType(type_id.index);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000598
599 if (cvt.kind() == LF_MODIFIER) {
600 ModifierRecord modifier;
601 llvm::cantFail(
602 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
Zachary Turner594c85e2018-12-17 19:43:33 +0000603 return CreateModifierType(type_id, modifier, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000604 }
605
606 if (cvt.kind() == LF_POINTER) {
607 PointerRecord pointer;
608 llvm::cantFail(
609 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
Zachary Turner594c85e2018-12-17 19:43:33 +0000610 return CreatePointerType(type_id, pointer, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000611 }
612
613 if (IsClassRecord(cvt.kind())) {
614 ClassRecord cr;
615 llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000616 return CreateTagType(type_id, cr, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000617 }
618
619 if (cvt.kind() == LF_ENUM) {
620 EnumRecord er;
621 llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er));
Zachary Turner594c85e2018-12-17 19:43:33 +0000622 return CreateTagType(type_id, er, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000623 }
624
625 if (cvt.kind() == LF_UNION) {
626 UnionRecord ur;
627 llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur));
Zachary Turner594c85e2018-12-17 19:43:33 +0000628 return CreateTagType(type_id, ur, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000629 }
630
Zachary Turner511bff22018-10-30 18:57:08 +0000631 if (cvt.kind() == LF_ARRAY) {
632 ArrayRecord ar;
633 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
Zachary Turner594c85e2018-12-17 19:43:33 +0000634 return CreateArrayType(type_id, ar, ct);
Zachary Turner511bff22018-10-30 18:57:08 +0000635 }
636
Zachary Turner544a66d82018-11-01 16:37:29 +0000637 if (cvt.kind() == LF_PROCEDURE) {
638 ProcedureRecord pr;
639 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000640 return CreateProcedureType(type_id, pr, ct);
Zachary Turner544a66d82018-11-01 16:37:29 +0000641 }
642
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000643 return nullptr;
644}
645
Zachary Turner6284aee2018-11-16 02:42:32 +0000646TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000647 // If they search for a UDT which is a forward ref, try and resolve the full
648 // decl and just map the forward ref uid to the full decl record.
Zachary Turner6284aee2018-11-16 02:42:32 +0000649 llvm::Optional<PdbTypeSymId> full_decl_uid;
650 if (IsForwardRefUdt(type_id, m_index->tpi())) {
651 auto expected_full_ti =
652 m_index->tpi().findFullDeclForForwardRef(type_id.index);
653 if (!expected_full_ti)
654 llvm::consumeError(expected_full_ti.takeError());
655 else if (*expected_full_ti != type_id.index) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000656 full_decl_uid = PdbTypeSymId(*expected_full_ti, false);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000657
Zachary Turner6284aee2018-11-16 02:42:32 +0000658 // It's possible that a lookup would occur for the full decl causing it
659 // to be cached, then a second lookup would occur for the forward decl.
660 // We don't want to create a second full decl, so make sure the full
661 // decl hasn't already been cached.
662 auto full_iter = m_types.find(toOpaqueUid(*full_decl_uid));
663 if (full_iter != m_types.end()) {
664 TypeSP result = full_iter->second;
665 // Map the forward decl to the TypeSP for the full decl so we can take
666 // the fast path next time.
667 m_types[toOpaqueUid(type_id)] = result;
668 return result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000669 }
670 }
671 }
672
Zachary Turner6284aee2018-11-16 02:42:32 +0000673 PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id;
Zachary Turner594c85e2018-12-17 19:43:33 +0000674
675 clang::QualType qt = m_ast->GetOrCreateType(best_decl_id);
676
677 TypeSP result = CreateType(best_decl_id, m_ast->ToCompilerType(qt));
Zachary Turner544a66d82018-11-01 16:37:29 +0000678 if (!result)
679 return nullptr;
Zachary Turner6284aee2018-11-16 02:42:32 +0000680
681 uint64_t best_uid = toOpaqueUid(best_decl_id);
682 m_types[best_uid] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000683 // If we had both a forward decl and a full decl, make both point to the new
684 // type.
685 if (full_decl_uid)
Zachary Turner6284aee2018-11-16 02:42:32 +0000686 m_types[toOpaqueUid(type_id)] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000687
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000688 return result;
689}
690
Zachary Turner6284aee2018-11-16 02:42:32 +0000691TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000692 // We can't use try_emplace / overwrite here because the process of creating
693 // a type could create nested types, which could invalidate iterators. So
694 // we have to do a 2-phase lookup / insert.
Zachary Turner6284aee2018-11-16 02:42:32 +0000695 auto iter = m_types.find(toOpaqueUid(type_id));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000696 if (iter != m_types.end())
697 return iter->second;
698
Zachary Turner6284aee2018-11-16 02:42:32 +0000699 return CreateAndCacheType(type_id);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000700}
701
Zachary Turner6284aee2018-11-16 02:42:32 +0000702VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
703 CVSymbol sym = m_index->symrecords().readRecord(var_id.offset);
Zachary Turner2af34162018-11-13 20:07:57 +0000704 if (sym.kind() == S_CONSTANT)
Zachary Turner6284aee2018-11-16 02:42:32 +0000705 return CreateConstantSymbol(var_id, sym);
Zachary Turner2af34162018-11-13 20:07:57 +0000706
Zachary Turner9f727952018-10-26 09:06:38 +0000707 lldb::ValueType scope = eValueTypeInvalid;
708 TypeIndex ti;
709 llvm::StringRef name;
710 lldb::addr_t addr = 0;
711 uint16_t section = 0;
712 uint32_t offset = 0;
713 bool is_external = false;
714 switch (sym.kind()) {
715 case S_GDATA32:
716 is_external = true;
717 LLVM_FALLTHROUGH;
718 case S_LDATA32: {
719 DataSym ds(sym.kind());
720 llvm::cantFail(SymbolDeserializer::deserializeAs<DataSym>(sym, ds));
721 ti = ds.Type;
722 scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal
723 : eValueTypeVariableStatic;
724 name = ds.Name;
725 section = ds.Segment;
726 offset = ds.DataOffset;
727 addr = m_index->MakeVirtualAddress(ds.Segment, ds.DataOffset);
728 break;
729 }
730 case S_GTHREAD32:
731 is_external = true;
732 LLVM_FALLTHROUGH;
733 case S_LTHREAD32: {
734 ThreadLocalDataSym tlds(sym.kind());
735 llvm::cantFail(
736 SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(sym, tlds));
737 ti = tlds.Type;
738 name = tlds.Name;
739 section = tlds.Segment;
740 offset = tlds.DataOffset;
741 addr = m_index->MakeVirtualAddress(tlds.Segment, tlds.DataOffset);
742 scope = eValueTypeVariableThreadLocal;
743 break;
744 }
745 default:
746 llvm_unreachable("unreachable!");
747 }
748
749 CompUnitSP comp_unit;
750 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr);
751 if (modi) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000752 CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi);
Zachary Turner9f727952018-10-26 09:06:38 +0000753 comp_unit = GetOrCreateCompileUnit(cci);
754 }
755
756 Declaration decl;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000757 PdbTypeSymId tid(ti, false);
Zachary Turner9f727952018-10-26 09:06:38 +0000758 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000759 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner9f727952018-10-26 09:06:38 +0000760 Variable::RangeList ranges;
761
Zachary Turner22566332019-01-02 18:33:54 +0000762 m_ast->GetOrCreateVariableDecl(var_id);
Zachary Turner37900292018-12-20 23:32:37 +0000763
Zachary Turner9f727952018-10-26 09:06:38 +0000764 DWARFExpression location = MakeGlobalLocationExpression(
765 section, offset, GetObjectFile()->GetModule());
766
767 std::string global_name("::");
768 global_name += name;
769 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000770 toOpaqueUid(var_id), name.str().c_str(), global_name.c_str(), type_sp,
Zachary Turner9f727952018-10-26 09:06:38 +0000771 scope, comp_unit.get(), ranges, &decl, location, is_external, false,
772 false);
773 var_sp->SetLocationIsConstantValueData(false);
774
775 return var_sp;
776}
777
Zachary Turner2af34162018-11-13 20:07:57 +0000778lldb::VariableSP
Zachary Turner6284aee2018-11-16 02:42:32 +0000779SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
Zachary Turner2af34162018-11-13 20:07:57 +0000780 const CVSymbol &cvs) {
781 TpiStream &tpi = m_index->tpi();
782 ConstantSym constant(cvs.kind());
783
784 llvm::cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(cvs, constant));
785 std::string global_name("::");
786 global_name += constant.Name;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000787 PdbTypeSymId tid(constant.Type, false);
Zachary Turner2af34162018-11-13 20:07:57 +0000788 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000789 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner2af34162018-11-13 20:07:57 +0000790
791 Declaration decl;
792 Variable::RangeList ranges;
793 ModuleSP module = GetObjectFile()->GetModule();
Zachary Turnera93458b2018-12-06 17:49:15 +0000794 DWARFExpression location = MakeConstantLocationExpression(
795 constant.Type, tpi, constant.Value, module);
Zachary Turner2af34162018-11-13 20:07:57 +0000796
797 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000798 toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(),
Zachary Turner2af34162018-11-13 20:07:57 +0000799 type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location,
800 false, false, false);
801 var_sp->SetLocationIsConstantValueData(true);
802 return var_sp;
803}
804
Zachary Turner6284aee2018-11-16 02:42:32 +0000805VariableSP
806SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) {
807 auto emplace_result = m_global_vars.try_emplace(toOpaqueUid(var_id), nullptr);
Zachary Turner9f727952018-10-26 09:06:38 +0000808 if (emplace_result.second)
Zachary Turner6284aee2018-11-16 02:42:32 +0000809 emplace_result.first->second = CreateGlobalVariable(var_id);
Zachary Turner9f727952018-10-26 09:06:38 +0000810
811 return emplace_result.first->second;
812}
813
Zachary Turner6284aee2018-11-16 02:42:32 +0000814lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000815 return GetOrCreateType(PdbTypeSymId(ti, false));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000816}
817
Zachary Turner6284aee2018-11-16 02:42:32 +0000818FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000819 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000820 auto emplace_result = m_functions.try_emplace(toOpaqueUid(func_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000821 if (emplace_result.second)
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000822 emplace_result.first->second = CreateFunction(func_id, comp_unit);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000823
824 lldbassert(emplace_result.first->second);
825 return emplace_result.first->second;
826}
827
828CompUnitSP
829SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000830
Zachary Turner307f5ae2018-10-12 19:47:13 +0000831 auto emplace_result =
Zachary Turner6284aee2018-11-16 02:42:32 +0000832 m_compilands.try_emplace(toOpaqueUid(cci.m_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000833 if (emplace_result.second)
834 emplace_result.first->second = CreateCompileUnit(cci);
835
836 lldbassert(emplace_result.first->second);
837 return emplace_result.first->second;
838}
839
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000840Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
841 auto iter = m_blocks.find(toOpaqueUid(block_id));
842 if (iter != m_blocks.end())
843 return *iter->second;
844
845 return CreateBlock(block_id);
846}
847
Zachary Turner22566332019-01-02 18:33:54 +0000848void SymbolFileNativePDB::ParseDeclsForContext(
849 lldb_private::CompilerDeclContext decl_ctx) {
850 clang::DeclContext *context = m_ast->FromCompilerDeclContext(decl_ctx);
851 if (!context)
852 return;
853 m_ast->ParseDeclsForContext(*context);
854}
855
Zachary Turner307f5ae2018-10-12 19:47:13 +0000856lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) {
857 if (index >= GetNumCompileUnits())
858 return CompUnitSP();
859 lldbassert(index < UINT16_MAX);
860 if (index >= UINT16_MAX)
861 return nullptr;
862
863 CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index);
864
865 return GetOrCreateCompileUnit(item);
866}
867
Zachary Turnerb96181c2018-10-22 16:19:07 +0000868lldb::LanguageType
869SymbolFileNativePDB::ParseCompileUnitLanguage(const SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000870 // What fields should I expect to be filled out on the SymbolContext? Is it
871 // safe to assume that `sc.comp_unit` is valid?
872 if (!sc.comp_unit)
873 return lldb::eLanguageTypeUnknown;
Zachary Turner6284aee2018-11-16 02:42:32 +0000874 PdbSymUid uid(sc.comp_unit->GetID());
875 lldbassert(uid.kind() == PdbSymUidKind::Compiland);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000876
Zachary Turner6284aee2018-11-16 02:42:32 +0000877 CompilandIndexItem *item =
878 m_index->compilands().GetCompiland(uid.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000879 lldbassert(item);
880 if (!item->m_compile_opts)
881 return lldb::eLanguageTypeUnknown;
882
883 return TranslateLanguage(item->m_compile_opts->getLanguage());
884}
885
Zachary Turnerb3130b42019-01-02 18:32:50 +0000886void SymbolFileNativePDB::AddSymbols(Symtab &symtab) { return; }
887
Zachary Turnerb96181c2018-10-22 16:19:07 +0000888size_t SymbolFileNativePDB::ParseCompileUnitFunctions(const SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000889 lldbassert(sc.comp_unit);
890 return false;
891}
892
893static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) {
894 // If any of these flags are set, we need to resolve the compile unit.
895 uint32_t flags = eSymbolContextCompUnit;
896 flags |= eSymbolContextVariable;
897 flags |= eSymbolContextFunction;
898 flags |= eSymbolContextBlock;
899 flags |= eSymbolContextLineEntry;
900 return (resolve_scope & flags) != 0;
901}
902
Zachary Turner991e4452018-10-25 20:45:19 +0000903uint32_t SymbolFileNativePDB::ResolveSymbolContext(
904 const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000905 uint32_t resolved_flags = 0;
906 lldb::addr_t file_addr = addr.GetFileAddress();
907
908 if (NeedsResolvedCompileUnit(resolve_scope)) {
909 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr);
910 if (!modi)
911 return 0;
Zachary Turner6284aee2018-11-16 02:42:32 +0000912 CompilandIndexItem *cci = m_index->compilands().GetCompiland(*modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000913 if (!cci)
914 return 0;
915
916 sc.comp_unit = GetOrCreateCompileUnit(*cci).get();
917 resolved_flags |= eSymbolContextCompUnit;
918 }
919
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000920 if (resolve_scope & eSymbolContextFunction ||
921 resolve_scope & eSymbolContextBlock) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000922 lldbassert(sc.comp_unit);
923 std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000924 // Search the matches in reverse. This way if there are multiple matches
925 // (for example we are 3 levels deep in a nested scope) it will find the
926 // innermost one first.
927 for (const auto &match : llvm::reverse(matches)) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000928 if (match.uid.kind() != PdbSymUidKind::CompilandSym)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000929 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000930
Zachary Turner6284aee2018-11-16 02:42:32 +0000931 PdbCompilandSymId csid = match.uid.asCompilandSym();
932 CVSymbol cvs = m_index->ReadSymbolRecord(csid);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000933 PDB_SymType type = CVSymToPDBSym(cvs.kind());
934 if (type != PDB_SymType::Function && type != PDB_SymType::Block)
Zachary Turner6284aee2018-11-16 02:42:32 +0000935 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000936 if (type == PDB_SymType::Function) {
937 sc.function = GetOrCreateFunction(csid, *sc.comp_unit).get();
938 sc.block = sc.GetFunctionBlock();
939 }
940
941 if (type == PDB_SymType::Block) {
942 sc.block = &GetOrCreateBlock(csid);
943 sc.function = sc.block->CalculateSymbolContextFunction();
944 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000945 resolved_flags |= eSymbolContextFunction;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000946 resolved_flags |= eSymbolContextBlock;
947 break;
948 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000949 }
950
951 if (resolve_scope & eSymbolContextLineEntry) {
952 lldbassert(sc.comp_unit);
953 if (auto *line_table = sc.comp_unit->GetLineTable()) {
954 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
955 resolved_flags |= eSymbolContextLineEntry;
956 }
957 }
958
959 return resolved_flags;
960}
961
Zachary Turnerb3130b42019-01-02 18:32:50 +0000962uint32_t SymbolFileNativePDB::ResolveSymbolContext(
963 const FileSpec &file_spec, uint32_t line, bool check_inlines,
964 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
965 return 0;
966}
967
Zachary Turner307f5ae2018-10-12 19:47:13 +0000968static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence,
969 const CompilandIndexItem &cci,
970 lldb::addr_t base_addr,
971 uint32_t file_number,
972 const LineFragmentHeader &block,
973 const LineNumberEntry &cur) {
974 LineInfo cur_info(cur.Flags);
975
976 if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto())
977 return;
978
979 uint64_t addr = base_addr + cur.Offset;
980
981 bool is_statement = cur_info.isStatement();
982 bool is_prologue = IsFunctionPrologue(cci, addr);
983 bool is_epilogue = IsFunctionEpilogue(cci, addr);
984
985 uint32_t lno = cur_info.getStartLine();
986
987 table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number,
988 is_statement, false, is_prologue, is_epilogue,
989 false);
990}
991
992static void TerminateLineSequence(LineTable &table,
993 const LineFragmentHeader &block,
994 lldb::addr_t base_addr, uint32_t file_number,
995 uint32_t last_line,
996 std::unique_ptr<LineSequence> seq) {
997 // The end is always a terminal entry, so insert it regardless.
998 table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize,
999 last_line, 0, file_number, false, false,
1000 false, false, true);
1001 table.InsertSequence(seq.release());
1002}
1003
Zachary Turnerb96181c2018-10-22 16:19:07 +00001004bool SymbolFileNativePDB::ParseCompileUnitLineTable(const SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001005 // Unfortunately LLDB is set up to parse the entire compile unit line table
1006 // all at once, even if all it really needs is line info for a specific
1007 // function. In the future it would be nice if it could set the sc.m_function
1008 // member, and we could only get the line info for the function in question.
1009 lldbassert(sc.comp_unit);
Zachary Turner6284aee2018-11-16 02:42:32 +00001010 PdbSymUid cu_id(sc.comp_unit->GetID());
1011 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1012 CompilandIndexItem *cci =
1013 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001014 lldbassert(cci);
1015 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
1016
1017 // This is basically a copy of the .debug$S subsections from all original COFF
1018 // object files merged together with address relocations applied. We are
1019 // looking for all DEBUG_S_LINES subsections.
1020 for (const DebugSubsectionRecord &dssr :
1021 cci->m_debug_stream.getSubsectionsArray()) {
1022 if (dssr.kind() != DebugSubsectionKind::Lines)
1023 continue;
1024
1025 DebugLinesSubsectionRef lines;
1026 llvm::BinaryStreamReader reader(dssr.getRecordData());
1027 if (auto EC = lines.initialize(reader)) {
1028 llvm::consumeError(std::move(EC));
1029 return false;
1030 }
1031
1032 const LineFragmentHeader *lfh = lines.header();
1033 uint64_t virtual_addr =
1034 m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset);
1035
1036 const auto &checksums = cci->m_strings.checksums().getArray();
1037 const auto &strings = cci->m_strings.strings();
1038 for (const LineColumnEntry &group : lines) {
1039 // Indices in this structure are actually offsets of records in the
1040 // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index
1041 // into the global PDB string table.
1042 auto iter = checksums.at(group.NameIndex);
1043 if (iter == checksums.end())
1044 continue;
1045
1046 llvm::Expected<llvm::StringRef> efn =
1047 strings.getString(iter->FileNameOffset);
1048 if (!efn) {
1049 llvm::consumeError(efn.takeError());
1050 continue;
1051 }
1052
1053 // LLDB wants the index of the file in the list of support files.
1054 auto fn_iter = llvm::find(cci->m_file_list, *efn);
1055 lldbassert(fn_iter != cci->m_file_list.end());
Zachary Turnerb3130b42019-01-02 18:32:50 +00001056 // LLDB support file indices are 1-based.
1057 uint32_t file_index =
1058 1 + std::distance(cci->m_file_list.begin(), fn_iter);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001059
1060 std::unique_ptr<LineSequence> sequence(
1061 line_table->CreateLineSequenceContainer());
1062 lldbassert(!group.LineNumbers.empty());
1063
1064 for (const LineNumberEntry &entry : group.LineNumbers) {
1065 AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr,
1066 file_index, *lfh, entry);
1067 }
1068 LineInfo last_line(group.LineNumbers.back().Flags);
1069 TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index,
1070 last_line.getEndLine(), std::move(sequence));
1071 }
1072 }
1073
1074 if (line_table->GetSize() == 0)
1075 return false;
1076
1077 sc.comp_unit->SetLineTable(line_table.release());
1078 return true;
1079}
1080
Zachary Turnerb96181c2018-10-22 16:19:07 +00001081bool SymbolFileNativePDB::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001082 // PDB doesn't contain information about macros
1083 return false;
1084}
1085
1086bool SymbolFileNativePDB::ParseCompileUnitSupportFiles(
Zachary Turnerb96181c2018-10-22 16:19:07 +00001087 const SymbolContext &sc, FileSpecList &support_files) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001088 lldbassert(sc.comp_unit);
1089
Zachary Turner6284aee2018-11-16 02:42:32 +00001090 PdbSymUid cu_id(sc.comp_unit->GetID());
1091 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1092 CompilandIndexItem *cci =
1093 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001094 lldbassert(cci);
1095
1096 for (llvm::StringRef f : cci->m_file_list) {
1097 FileSpec::Style style =
1098 f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001099 FileSpec spec(f, style);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001100 support_files.Append(spec);
1101 }
1102
Zachary Turnerb3130b42019-01-02 18:32:50 +00001103 llvm::SmallString<64> main_source_file =
1104 m_index->compilands().GetMainSourceFile(*cci);
1105 FileSpec::Style style = main_source_file.startswith("/")
1106 ? FileSpec::Style::posix
1107 : FileSpec::Style::windows;
1108 FileSpec spec(main_source_file, style);
1109 support_files.Insert(0, spec);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001110 return true;
1111}
1112
1113bool SymbolFileNativePDB::ParseImportedModules(
Zachary Turnerb96181c2018-10-22 16:19:07 +00001114 const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001115 // PDB does not yet support module debug info
1116 return false;
1117}
1118
Zachary Turnerb96181c2018-10-22 16:19:07 +00001119size_t SymbolFileNativePDB::ParseFunctionBlocks(const SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001120 lldbassert(sc.comp_unit && sc.function);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001121 GetOrCreateBlock(PdbSymUid(sc.function->GetID()).asCompilandSym());
1122 // FIXME: Parse child blocks
1123 return 1;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001124}
1125
Zachary Turner594c85e2018-12-17 19:43:33 +00001126void SymbolFileNativePDB::DumpClangAST(Stream &s) { m_ast->Dump(s); }
Zachary Turner49110232018-11-05 17:40:28 +00001127
Zachary Turner9f727952018-10-26 09:06:38 +00001128uint32_t SymbolFileNativePDB::FindGlobalVariables(
1129 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
1130 uint32_t max_matches, VariableList &variables) {
1131 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1132
1133 std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName(
1134 name.GetStringRef(), m_index->symrecords());
1135 for (const SymbolAndOffset &result : results) {
1136 VariableSP var;
1137 switch (result.second.kind()) {
1138 case SymbolKind::S_GDATA32:
1139 case SymbolKind::S_LDATA32:
1140 case SymbolKind::S_GTHREAD32:
Zachary Turner2af34162018-11-13 20:07:57 +00001141 case SymbolKind::S_LTHREAD32:
1142 case SymbolKind::S_CONSTANT: {
Zachary Turner9fbf9352018-11-16 03:16:27 +00001143 PdbGlobalSymId global(result.first, false);
Zachary Turner6284aee2018-11-16 02:42:32 +00001144 var = GetOrCreateGlobalVariable(global);
Zachary Turner9f727952018-10-26 09:06:38 +00001145 variables.AddVariable(var);
1146 break;
1147 }
1148 default:
1149 continue;
1150 }
1151 }
1152 return variables.GetSize();
1153}
1154
Zachary Turner307f5ae2018-10-12 19:47:13 +00001155uint32_t SymbolFileNativePDB::FindFunctions(
Zachary Turnerb96181c2018-10-22 16:19:07 +00001156 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001157 FunctionNameType name_type_mask, bool include_inlines, bool append,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001158 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001159 // For now we only support lookup by method name.
1160 if (!(name_type_mask & eFunctionNameTypeMethod))
1161 return 0;
1162
1163 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1164
1165 std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName(
1166 name.GetStringRef(), m_index->symrecords());
1167 for (const SymbolAndOffset &match : matches) {
1168 if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF)
1169 continue;
1170 ProcRefSym proc(match.second.kind());
1171 cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc));
1172
1173 if (!IsValidRecord(proc))
1174 continue;
1175
Zachary Turner6284aee2018-11-16 02:42:32 +00001176 CompilandIndexItem &cci =
1177 m_index->compilands().GetOrCreateCompiland(proc.modi());
Zachary Turnerb96181c2018-10-22 16:19:07 +00001178 SymbolContext sc;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001179
1180 sc.comp_unit = GetOrCreateCompileUnit(cci).get();
Zachary Turner9fbf9352018-11-16 03:16:27 +00001181 PdbCompilandSymId func_id(proc.modi(), proc.SymOffset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001182 sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get();
Zachary Turner307f5ae2018-10-12 19:47:13 +00001183
1184 sc_list.Append(sc);
1185 }
1186
1187 return sc_list.GetSize();
1188}
1189
Zachary Turnerb96181c2018-10-22 16:19:07 +00001190uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression &regex,
1191 bool include_inlines, bool append,
1192 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001193 return 0;
1194}
1195
Zachary Turnerb96181c2018-10-22 16:19:07 +00001196uint32_t SymbolFileNativePDB::FindTypes(
1197 const SymbolContext &sc, const ConstString &name,
1198 const CompilerDeclContext *parent_decl_ctx, bool append,
1199 uint32_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files,
1200 TypeMap &types) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001201 if (!append)
1202 types.Clear();
1203 if (!name)
1204 return 0;
1205
1206 searched_symbol_files.clear();
1207 searched_symbol_files.insert(this);
1208
1209 // There is an assumption 'name' is not a regex
1210 size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types);
1211
1212 return match_count;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001213}
1214
1215size_t
1216SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context,
1217 bool append, TypeMap &types) {
1218 return 0;
1219}
1220
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001221size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name,
1222 uint32_t max_matches,
1223 TypeMap &types) {
1224
1225 size_t match_count = 0;
1226 std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name);
1227 if (max_matches > 0 && max_matches < matches.size())
1228 matches.resize(max_matches);
1229
1230 for (TypeIndex ti : matches) {
1231 TypeSP type = GetOrCreateType(ti);
1232 if (!type)
1233 continue;
1234
1235 types.Insert(type);
1236 ++match_count;
1237 }
1238 return match_count;
1239}
1240
Zachary Turnerb96181c2018-10-22 16:19:07 +00001241size_t SymbolFileNativePDB::ParseTypes(const SymbolContext &sc) { return 0; }
1242
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001243size_t
1244SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit,
1245 VariableList &variables) {
1246 PdbSymUid sym_uid(comp_unit.GetID());
1247 lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland);
1248 return 0;
1249}
1250
1251VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
1252 PdbCompilandSymId var_id,
1253 bool is_param) {
1254 ModuleSP module = GetObjectFile()->GetModule();
Zachary Turner594c85e2018-12-17 19:43:33 +00001255 VariableInfo var_info = GetVariableLocationInfo(*m_index, var_id, module);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001256
1257 CompilandIndexItem *cii = m_index->compilands().GetCompiland(var_id.modi);
1258 CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(*cii);
1259 TypeSP type_sp = GetOrCreateType(var_info.type);
1260 std::string name = var_info.name.str();
1261 Declaration decl;
1262 SymbolFileTypeSP sftype =
1263 std::make_shared<SymbolFileType>(*this, type_sp->GetID());
1264
1265 ValueType var_scope =
1266 is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal;
1267 VariableSP var_sp = std::make_shared<Variable>(
1268 toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope,
1269 comp_unit_sp.get(), *var_info.ranges, &decl, *var_info.location, false,
1270 false, false);
1271
Zachary Turner594c85e2018-12-17 19:43:33 +00001272 if (!is_param)
Zachary Turner22566332019-01-02 18:33:54 +00001273 m_ast->GetOrCreateVariableDecl(scope_id, var_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001274
1275 m_local_variables[toOpaqueUid(var_id)] = var_sp;
1276 return var_sp;
1277}
1278
1279VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable(
1280 PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) {
1281 auto iter = m_local_variables.find(toOpaqueUid(var_id));
1282 if (iter != m_local_variables.end())
1283 return iter->second;
1284
1285 return CreateLocalVariable(scope_id, var_id, is_param);
1286}
1287
1288size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
1289 Block &block = GetOrCreateBlock(block_id);
1290
1291 size_t count = 0;
1292
1293 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
1294 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
1295 uint32_t params_remaining = 0;
1296 switch (sym.kind()) {
1297 case S_GPROC32:
1298 case S_LPROC32: {
1299 ProcSym proc(static_cast<SymbolRecordKind>(sym.kind()));
1300 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym, proc));
1301 CVType signature = m_index->tpi().getType(proc.FunctionType);
1302 ProcedureRecord sig;
1303 cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(signature, sig));
1304 params_remaining = sig.getParameterCount();
1305 break;
1306 }
1307 case S_BLOCK32:
1308 break;
1309 default:
1310 lldbassert(false && "Symbol is not a block!");
1311 return 0;
1312 }
1313
1314 VariableListSP variables = block.GetBlockVariableList(false);
1315 if (!variables) {
1316 variables = std::make_shared<VariableList>();
1317 block.SetVariableList(variables);
1318 }
1319
1320 CVSymbolArray syms = limitSymbolArrayToScope(
1321 cii->m_debug_stream.getSymbolArray(), block_id.offset);
1322
1323 // Skip the first record since it's a PROC32 or BLOCK32, and there's
1324 // no point examining it since we know it's not a local variable.
1325 syms.drop_front();
1326 auto iter = syms.begin();
1327 auto end = syms.end();
1328
1329 while (iter != end) {
1330 uint32_t record_offset = iter.offset();
1331 CVSymbol variable_cvs = *iter;
1332 PdbCompilandSymId child_sym_id(block_id.modi, record_offset);
1333 ++iter;
1334
1335 // If this is a block, recurse into its children and then skip it.
1336 if (variable_cvs.kind() == S_BLOCK32) {
1337 uint32_t block_end = getScopeEndOffset(variable_cvs);
1338 count += ParseVariablesForBlock(child_sym_id);
1339 iter = syms.at(block_end);
1340 continue;
1341 }
1342
1343 bool is_param = params_remaining > 0;
1344 VariableSP variable;
1345 switch (variable_cvs.kind()) {
1346 case S_REGREL32:
1347 case S_REGISTER:
1348 case S_LOCAL:
1349 variable = GetOrCreateLocalVariable(block_id, child_sym_id, is_param);
1350 if (is_param)
1351 --params_remaining;
1352 variables->AddVariableIfUnique(variable);
1353 break;
1354 default:
1355 break;
1356 }
1357 }
1358
1359 // Pass false for set_children, since we call this recursively so that the
1360 // children will call this for themselves.
1361 block.SetDidParseVariables(true, false);
1362
1363 return count;
1364}
1365
1366size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) {
1367 lldbassert(sc.function || sc.comp_unit);
1368
1369 VariableListSP variables;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001370 if (sc.block) {
1371 PdbSymUid block_id(sc.block->GetID());
1372
1373 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1374 return count;
1375 }
1376
1377 if (sc.function) {
1378 PdbSymUid block_id(sc.function->GetID());
1379
1380 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1381 return count;
1382 }
1383
1384 if (sc.comp_unit) {
1385 variables = sc.comp_unit->GetVariableList(false);
1386 if (!variables) {
1387 variables = std::make_shared<VariableList>();
1388 sc.comp_unit->SetVariableList(variables);
1389 }
1390 return ParseVariablesForCompileUnit(*sc.comp_unit, *variables);
1391 }
1392
1393 llvm_unreachable("Unreachable!");
1394}
1395
1396CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001397 clang::Decl *decl = m_ast->GetOrCreateDeclForUid(PdbSymUid(uid));
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001398
Zachary Turner594c85e2018-12-17 19:43:33 +00001399 return m_ast->ToCompilerDecl(*decl);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001400}
1401
1402CompilerDeclContext
1403SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001404 clang::DeclContext *context =
1405 m_ast->GetOrCreateDeclContextForUid(PdbSymUid(uid));
1406 if (!context)
1407 return {};
1408
1409 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001410}
1411
Zachary Turnera42bbe32018-12-07 19:34:02 +00001412CompilerDeclContext
1413SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001414 clang::DeclContext *context = m_ast->GetParentDeclContext(PdbSymUid(uid));
1415 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnera42bbe32018-12-07 19:34:02 +00001416}
1417
Zachary Turnerb96181c2018-10-22 16:19:07 +00001418Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001419 auto iter = m_types.find(type_uid);
1420 // lldb should not be passing us non-sensical type uids. the only way it
1421 // could have a type uid in the first place is if we handed it out, in which
Zachary Turner9f727952018-10-26 09:06:38 +00001422 // case we should know about the type. However, that doesn't mean we've
1423 // instantiated it yet. We can vend out a UID for a future type. So if the
1424 // type doesn't exist, let's instantiate it now.
1425 if (iter != m_types.end())
1426 return &*iter->second;
1427
Zachary Turner6284aee2018-11-16 02:42:32 +00001428 PdbSymUid uid(type_uid);
1429 lldbassert(uid.kind() == PdbSymUidKind::Type);
1430 PdbTypeSymId type_id = uid.asTypeSym();
1431 if (type_id.index.isNoneType())
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001432 return nullptr;
Zachary Turner9f727952018-10-26 09:06:38 +00001433
Zachary Turner6284aee2018-11-16 02:42:32 +00001434 TypeSP type_sp = CreateAndCacheType(type_id);
Zachary Turner9f727952018-10-26 09:06:38 +00001435 return &*type_sp;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001436}
1437
Adrian Prantleca07c52018-11-05 20:49:07 +00001438llvm::Optional<SymbolFile::ArrayInfo>
1439SymbolFileNativePDB::GetDynamicArrayInfoForUID(
1440 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1441 return llvm::None;
1442}
1443
1444
Zachary Turnerb96181c2018-10-22 16:19:07 +00001445bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001446 clang::QualType qt =
1447 clang::QualType::getFromOpaquePtr(compiler_type.GetOpaqueQualType());
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001448
Zachary Turner594c85e2018-12-17 19:43:33 +00001449 return m_ast->CompleteType(qt);
Zachary Turnerb96181c2018-10-22 16:19:07 +00001450}
1451
1452size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001453 TypeClass type_mask,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001454 lldb_private::TypeList &type_list) {
1455 return 0;
1456}
1457
1458CompilerDeclContext
1459SymbolFileNativePDB::FindNamespace(const SymbolContext &sc,
1460 const ConstString &name,
1461 const CompilerDeclContext *parent_decl_ctx) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001462 return {};
1463}
1464
Zachary Turnerb96181c2018-10-22 16:19:07 +00001465TypeSystem *
Zachary Turner307f5ae2018-10-12 19:47:13 +00001466SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1467 auto type_system =
1468 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1469 if (type_system)
1470 type_system->SetSymbolFile(this);
1471 return type_system;
1472}
1473
Zachary Turnerb96181c2018-10-22 16:19:07 +00001474ConstString SymbolFileNativePDB::GetPluginName() {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001475 static ConstString g_name("pdb");
1476 return g_name;
1477}
1478
1479uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; }