1 //===-- PDBASTParser.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PDBASTParser.h"
12 #include "clang/AST/CharUnits.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/ClangUtil.h"
18 #include "lldb/Symbol/Declaration.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Symbol/TypeSystem.h"
22 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
23 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
29 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
30 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
37 using namespace lldb_private;
38 using namespace llvm::pdb;
41 int TranslateUdtKind(PDB_UdtType pdb_kind) {
43 case PDB_UdtType::Class:
44 return clang::TTK_Class;
45 case PDB_UdtType::Struct:
46 return clang::TTK_Struct;
47 case PDB_UdtType::Union:
48 return clang::TTK_Union;
49 case PDB_UdtType::Interface:
50 return clang::TTK_Interface;
55 lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) {
57 case PDB_BuiltinType::Float:
58 return lldb::eEncodingIEEE754;
59 case PDB_BuiltinType::Int:
60 case PDB_BuiltinType::Long:
61 case PDB_BuiltinType::Char:
62 return lldb::eEncodingSint;
63 case PDB_BuiltinType::Bool:
64 case PDB_BuiltinType::Char16:
65 case PDB_BuiltinType::Char32:
66 case PDB_BuiltinType::UInt:
67 case PDB_BuiltinType::ULong:
68 case PDB_BuiltinType::HResult:
69 case PDB_BuiltinType::WCharT:
70 return lldb::eEncodingUint;
72 return lldb::eEncodingInvalid;
76 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) {
78 case PDB_VariantType::Int8:
79 case PDB_VariantType::Int16:
80 case PDB_VariantType::Int32:
81 case PDB_VariantType::Int64:
82 return lldb::eEncodingSint;
84 case PDB_VariantType::UInt8:
85 case PDB_VariantType::UInt16:
86 case PDB_VariantType::UInt32:
87 case PDB_VariantType::UInt64:
88 return lldb::eEncodingUint;
94 return lldb::eEncodingSint;
98 GetBuiltinTypeForPDBEncodingAndBitSize(ClangASTContext &clang_ast,
99 const PDBSymbolTypeBuiltin &pdb_type,
100 Encoding encoding, uint32_t width) {
101 auto *ast = clang_ast.getASTContext();
103 return CompilerType();
105 switch (pdb_type.getBuiltinType()) {
108 case PDB_BuiltinType::None:
109 return CompilerType();
110 case PDB_BuiltinType::Void:
111 return clang_ast.GetBasicType(eBasicTypeVoid);
112 case PDB_BuiltinType::Bool:
113 return clang_ast.GetBasicType(eBasicTypeBool);
114 case PDB_BuiltinType::Long:
115 if (width == ast->getTypeSize(ast->LongTy))
116 return CompilerType(ast, ast->LongTy);
117 if (width == ast->getTypeSize(ast->LongLongTy))
118 return CompilerType(ast, ast->LongLongTy);
120 case PDB_BuiltinType::ULong:
121 if (width == ast->getTypeSize(ast->UnsignedLongTy))
122 return CompilerType(ast, ast->UnsignedLongTy);
123 if (width == ast->getTypeSize(ast->UnsignedLongLongTy))
124 return CompilerType(ast, ast->UnsignedLongLongTy);
126 case PDB_BuiltinType::WCharT:
127 if (width == ast->getTypeSize(ast->WCharTy))
128 return CompilerType(ast, ast->WCharTy);
130 case PDB_BuiltinType::Char16:
131 return CompilerType(ast, ast->Char16Ty);
132 case PDB_BuiltinType::Char32:
133 return CompilerType(ast, ast->Char32Ty);
134 case PDB_BuiltinType::Float:
135 // Note: types `long double` and `double` have same bit size in MSVC and
136 // there is no information in the PDB to distinguish them. So when falling
137 // back to default search, the compiler type of `long double` will be
138 // represented by the one generated for `double`.
141 // If there is no match on PDB_BuiltinType, fall back to default search by
142 // encoding and width only
143 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
146 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type,
147 CompilerType &compiler_type) {
148 PDB_BuiltinType kind = pdb_type.getBuiltinType();
152 case PDB_BuiltinType::Currency:
153 return ConstString("CURRENCY");
154 case PDB_BuiltinType::Date:
155 return ConstString("DATE");
156 case PDB_BuiltinType::Variant:
157 return ConstString("VARIANT");
158 case PDB_BuiltinType::Complex:
159 return ConstString("complex");
160 case PDB_BuiltinType::Bitfield:
161 return ConstString("bitfield");
162 case PDB_BuiltinType::BSTR:
163 return ConstString("BSTR");
164 case PDB_BuiltinType::HResult:
165 return ConstString("HRESULT");
166 case PDB_BuiltinType::BCD:
167 return ConstString("BCD");
168 case PDB_BuiltinType::Char16:
169 return ConstString("char16_t");
170 case PDB_BuiltinType::Char32:
171 return ConstString("char32_t");
172 case PDB_BuiltinType::None:
173 return ConstString("...");
175 return compiler_type.GetTypeName();
178 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) {
179 auto &raw_sym = symbol.getRawSymbol();
180 auto first_line_up = raw_sym.getSrcLineOnTypeDefn();
182 if (!first_line_up) {
183 auto lines_up = symbol.getSession().findLineNumbersByAddress(
184 raw_sym.getVirtualAddress(), raw_sym.getLength());
187 first_line_up = lines_up->getNext();
191 uint32_t src_file_id = first_line_up->getSourceFileId();
192 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id);
196 FileSpec spec(src_file_up->getFileName(), /*resolve_path*/ false);
198 decl.SetColumn(first_line_up->getColumnNumber());
199 decl.SetLine(first_line_up->getLineNumber());
204 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {}
206 PDBASTParser::~PDBASTParser() {}
208 // DebugInfoASTParser interface
210 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
211 // PDB doesn't maintain enough information to robustly rebuild the entire
212 // tree, and this is most problematic when it comes to figure out the right
213 // DeclContext to put a type in. So for now, everything goes in the
214 // translation unit decl as a fully qualified type.
215 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl();
218 switch (type.getSymTag()) {
219 case PDB_SymType::UDT: {
220 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
222 AccessType access = lldb::eAccessPublic;
223 PDB_UdtType udt_kind = udt->getUdtKind();
224 auto tag_type_kind = TranslateUdtKind(udt_kind);
225 if (tag_type_kind == -1)
228 if (udt_kind == PDB_UdtType::Class)
229 access = lldb::eAccessPrivate;
231 CompilerType clang_type = m_ast.CreateRecordType(
232 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind,
233 lldb::eLanguageTypeC_plus_plus, nullptr);
235 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
237 return std::make_shared<lldb_private::Type>(
238 type.getSymIndexId(), m_ast.GetSymbolFile(),
239 ConstString(udt->getName()), udt->getLength(), nullptr,
240 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type,
241 lldb_private::Type::eResolveStateForward);
243 case PDB_SymType::Enum: {
244 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
246 auto underlying_type_up = enum_type->getUnderlyingType();
247 if (!underlying_type_up)
249 lldb::Encoding encoding =
250 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType());
251 // FIXME: Type of underlying builtin is always `Int`. We correct it with
252 // the very first enumerator's encoding if any.
253 auto first_child = enum_type->findOneChild<PDBSymbolData>();
255 encoding = TranslateEnumEncoding(first_child->getValue().Type);
257 std::string name = enum_type->getName();
258 uint64_t bytes = enum_type->getLength();
259 CompilerType builtin_type;
261 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize(
262 m_ast, *underlying_type_up, encoding, bytes * 8);
264 builtin_type = m_ast.GetBasicType(eBasicTypeInt);
265 // FIXME: PDB does not have information about scoped enumeration (Enum
266 // Class). Set it false for now.
267 bool isScoped = false;
269 CompilerType ast_enum = m_ast.CreateEnumerationType(
270 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped);
271 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
273 while (auto enum_value = enum_values->getNext()) {
274 if (enum_value->getDataKind() != PDB_DataKind::Constant)
276 AddEnumValue(ast_enum, *enum_value);
279 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum))
280 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum);
282 GetDeclarationForSymbol(type, decl);
283 return std::make_shared<lldb_private::Type>(
284 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes,
285 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
286 ast_enum, lldb_private::Type::eResolveStateFull);
288 case PDB_SymType::Typedef: {
289 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
291 lldb_private::Type *target_type =
292 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
295 std::string name = type_def->getName();
296 uint64_t bytes = type_def->getLength();
297 CompilerType target_ast_type = target_type->GetFullCompilerType();
298 CompilerDeclContext target_decl_ctx =
299 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID());
300 CompilerType ast_typedef =
301 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx);
305 return std::make_shared<lldb_private::Type>(
306 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name),
307 bytes, nullptr, target_type->GetID(),
308 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef,
309 lldb_private::Type::eResolveStateFull);
311 case PDB_SymType::Function:
312 case PDB_SymType::FunctionSig: {
314 PDBSymbolTypeFunctionSig *func_sig = nullptr;
315 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
316 if (pdb_func->isCompilerGenerated())
319 auto sig = pdb_func->getSignature();
322 func_sig = sig.release();
323 // Function type is named.
324 name = pdb_func->getName();
325 } else if (auto pdb_func_sig =
326 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
327 func_sig = const_cast<PDBSymbolTypeFunctionSig *>(pdb_func_sig);
329 llvm_unreachable("Unexpected PDB symbol!");
331 auto arg_enum = func_sig->getArguments();
332 uint32_t num_args = arg_enum->getChildCount();
333 std::vector<CompilerType> arg_list;
335 bool is_variadic = func_sig->isCVarArgs();
336 // Drop last variadic argument.
339 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
340 auto arg = arg_enum->getChildAtIndex(arg_idx);
343 lldb_private::Type *arg_type =
344 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
345 // If there's some error looking up one of the dependent types of this
346 // function signature, bail.
349 CompilerType arg_ast_type = arg_type->GetFullCompilerType();
350 arg_list.push_back(arg_ast_type);
352 lldbassert(arg_list.size() <= num_args);
354 auto pdb_return_type = func_sig->getReturnType();
355 lldb_private::Type *return_type =
356 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
357 // If there's some error looking up one of the dependent types of this
358 // function signature, bail.
361 CompilerType return_ast_type = return_type->GetFullCompilerType();
362 uint32_t type_quals = 0;
363 if (func_sig->isConstType())
364 type_quals |= clang::Qualifiers::Const;
365 if (func_sig->isVolatileType())
366 type_quals |= clang::Qualifiers::Volatile;
367 CompilerType func_sig_ast_type =
368 m_ast.CreateFunctionType(return_ast_type, arg_list.data(),
369 arg_list.size(), is_variadic, type_quals);
371 GetDeclarationForSymbol(type, decl);
372 return std::make_shared<lldb_private::Type>(
373 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0,
374 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
375 func_sig_ast_type, lldb_private::Type::eResolveStateFull);
377 case PDB_SymType::ArrayType: {
378 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
380 uint32_t num_elements = array_type->getCount();
381 uint32_t element_uid = array_type->getElementTypeId();
382 uint32_t bytes = array_type->getLength();
384 // If array rank > 0, PDB gives the element type at N=0. So element type
385 // will parsed in the order N=0, N=1,..., N=rank sequentially.
386 lldb_private::Type *element_type =
387 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
391 CompilerType element_ast_type = element_type->GetForwardCompilerType();
392 // If element type is UDT, it needs to be complete.
393 if (ClangASTContext::IsCXXClassType(element_ast_type) &&
394 element_ast_type.GetCompleteType() == false) {
395 if (ClangASTContext::StartTagDeclarationDefinition(element_ast_type)) {
396 ClangASTContext::CompleteTagDeclarationDefinition(element_ast_type);
398 // We are not able to start defintion.
402 CompilerType array_ast_type = m_ast.CreateArrayType(
403 element_ast_type, num_elements, /*is_gnu_vector*/ false);
404 TypeSP type_sp = std::make_shared<lldb_private::Type>(
405 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
406 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
407 decl, array_ast_type, lldb_private::Type::eResolveStateFull);
408 type_sp->SetEncodingType(element_type);
411 case PDB_SymType::BuiltinType: {
412 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
413 assert(builtin_type);
414 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
415 if (builtin_kind == PDB_BuiltinType::None)
418 uint64_t bytes = builtin_type->getLength();
419 Encoding encoding = TranslateBuiltinEncoding(builtin_kind);
420 CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize(
421 m_ast, *builtin_type, encoding, bytes * 8);
423 if (builtin_type->isConstType())
424 builtin_ast_type = builtin_ast_type.AddConstModifier();
426 if (builtin_type->isVolatileType())
427 builtin_ast_type = builtin_ast_type.AddVolatileModifier();
429 auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type);
431 return std::make_shared<lldb_private::Type>(
432 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, bytes,
433 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
434 builtin_ast_type, lldb_private::Type::eResolveStateFull);
436 case PDB_SymType::PointerType: {
437 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
438 assert(pointer_type);
439 Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID(
440 pointer_type->getPointeeType()->getSymIndexId());
444 CompilerType pointer_ast_type;
445 pointer_ast_type = pointee_type->GetFullCompilerType();
446 if (pointer_type->isReference())
447 pointer_ast_type = pointer_ast_type.GetLValueReferenceType();
448 else if (pointer_type->isRValueReference())
449 pointer_ast_type = pointer_ast_type.GetRValueReferenceType();
451 pointer_ast_type = pointer_ast_type.GetPointerType();
453 if (pointer_type->isConstType())
454 pointer_ast_type = pointer_ast_type.AddConstModifier();
456 if (pointer_type->isVolatileType())
457 pointer_ast_type = pointer_ast_type.AddVolatileModifier();
459 if (pointer_type->isRestrictedType())
460 pointer_ast_type = pointer_ast_type.AddRestrictModifier();
462 return std::make_shared<lldb_private::Type>(
463 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
464 pointer_type->getLength(), nullptr, LLDB_INVALID_UID,
465 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type,
466 lldb_private::Type::eResolveStateFull);
474 bool PDBASTParser::AddEnumValue(CompilerType enum_type,
475 const PDBSymbolData &enum_value) const {
477 Variant v = enum_value.getValue();
478 std::string name = enum_value.getName();
481 case PDB_VariantType::Int8:
482 raw_value = v.Value.Int8;
484 case PDB_VariantType::Int16:
485 raw_value = v.Value.Int16;
487 case PDB_VariantType::Int32:
488 raw_value = v.Value.Int32;
490 case PDB_VariantType::Int64:
491 raw_value = v.Value.Int64;
493 case PDB_VariantType::UInt8:
494 raw_value = v.Value.UInt8;
496 case PDB_VariantType::UInt16:
497 raw_value = v.Value.UInt16;
499 case PDB_VariantType::UInt32:
500 raw_value = v.Value.UInt32;
502 case PDB_VariantType::UInt64:
503 raw_value = v.Value.UInt64;
508 CompilerType underlying_type =
509 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
510 uint32_t byte_size = m_ast.getASTContext()->getTypeSize(
511 ClangUtil::GetQualType(underlying_type));
512 return m_ast.AddEnumerationValueToEnumerationType(
513 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(),
514 raw_value, byte_size * 8);