1 //===-- PDBASTParser.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 "PDBASTParser.h" 11 12 #include "clang/AST/CharUnits.h" 13 #include "clang/AST/Decl.h" 14 #include "clang/AST/DeclCXX.h" 15 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" 21 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" 35 36 using namespace lldb; 37 using namespace lldb_private; 38 using namespace llvm::pdb; 39 40 namespace { 41 int TranslateUdtKind(PDB_UdtType pdb_kind) { 42 switch (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; 51 } 52 return -1; 53 } 54 55 lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) { 56 switch (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 case PDB_BuiltinType::Char16: 63 case PDB_BuiltinType::Char32: 64 return lldb::eEncodingSint; 65 case PDB_BuiltinType::Bool: 66 case PDB_BuiltinType::UInt: 67 case PDB_BuiltinType::ULong: 68 case PDB_BuiltinType::HResult: 69 return lldb::eEncodingUint; 70 default: 71 return lldb::eEncodingInvalid; 72 } 73 } 74 75 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 76 switch (type) { 77 case PDB_VariantType::Int8: 78 case PDB_VariantType::Int16: 79 case PDB_VariantType::Int32: 80 case PDB_VariantType::Int64: 81 return lldb::eEncodingSint; 82 83 case PDB_VariantType::UInt8: 84 case PDB_VariantType::UInt16: 85 case PDB_VariantType::UInt32: 86 case PDB_VariantType::UInt64: 87 return lldb::eEncodingUint; 88 89 default: 90 break; 91 } 92 93 return lldb::eEncodingSint; 94 } 95 96 CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( 97 ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type, 98 Encoding encoding, uint32_t width) { 99 if (!pdb_type) 100 return CompilerType(); 101 if (!clang_ast) 102 return CompilerType(); 103 auto *ast = clang_ast->getASTContext(); 104 if (!ast) 105 return CompilerType(); 106 107 switch (pdb_type->getBuiltinType()) { 108 default: break; 109 case PDB_BuiltinType::None: 110 return CompilerType(); 111 case PDB_BuiltinType::Void: 112 return clang_ast->GetBasicType(eBasicTypeVoid); 113 case PDB_BuiltinType::Bool: 114 return clang_ast->GetBasicType(eBasicTypeBool); 115 case PDB_BuiltinType::Long: 116 if (width == ast->getTypeSize(ast->LongTy)) 117 return CompilerType(ast, ast->LongTy); 118 if (width == ast->getTypeSize(ast->LongLongTy)) 119 return CompilerType(ast, ast->LongLongTy); 120 break; 121 case PDB_BuiltinType::ULong: 122 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 123 return CompilerType(ast, ast->UnsignedLongTy); 124 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 125 return CompilerType(ast, ast->UnsignedLongLongTy); 126 break; 127 case PDB_BuiltinType::WCharT: 128 if (width == ast->getTypeSize(ast->WCharTy)) 129 return CompilerType(ast, ast->WCharTy); 130 break; 131 case PDB_BuiltinType::Char16: 132 return CompilerType(ast, ast->Char16Ty); 133 case PDB_BuiltinType::Char32: 134 return CompilerType(ast, ast->Char32Ty); 135 case PDB_BuiltinType::Float: 136 // Note: types `long double` and `double` have same bit size in MSVC and there 137 // is no information in the PDB to distinguish them. So when falling back 138 // to default search, the compiler type of `long double` will be represented by 139 // the one generated for `double`. 140 break; 141 } 142 // If there is no match on PDB_BuiltinType, fall back to default search 143 // by encoding and width only 144 return clang_ast->GetBuiltinTypeForEncodingAndBitSize(encoding, width); 145 } 146 147 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin *pdb_type, 148 CompilerType &compiler_type) { 149 if (!pdb_type) 150 return compiler_type.GetTypeName(); 151 152 PDB_BuiltinType kind = pdb_type->getBuiltinType(); 153 switch (kind) { 154 default: break; 155 case PDB_BuiltinType::Currency: 156 return ConstString("CURRENCY"); 157 case PDB_BuiltinType::Date: 158 return ConstString("DATE"); 159 case PDB_BuiltinType::Variant: 160 return ConstString("VARIANT"); 161 case PDB_BuiltinType::Complex: 162 return ConstString("complex"); 163 case PDB_BuiltinType::Bitfield: 164 return ConstString("bitfield"); 165 case PDB_BuiltinType::BSTR: 166 return ConstString("BSTR"); 167 case PDB_BuiltinType::HResult: 168 return ConstString("HRESULT"); 169 case PDB_BuiltinType::BCD: 170 return ConstString("BCD"); 171 case PDB_BuiltinType::Char16: 172 return ConstString("char16_t"); 173 case PDB_BuiltinType::Char32: 174 return ConstString("char32_t"); 175 case PDB_BuiltinType::None: 176 return ConstString("..."); 177 } 178 return compiler_type.GetTypeName(); 179 } 180 181 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { 182 auto &raw_sym = symbol.getRawSymbol(); 183 auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); 184 185 if (!first_line_up) { 186 auto lines_up = symbol.getSession().findLineNumbersByAddress( 187 raw_sym.getVirtualAddress(), raw_sym.getLength()); 188 if (!lines_up) 189 return false; 190 first_line_up = lines_up->getNext(); 191 if (!first_line_up) 192 return false; 193 } 194 uint32_t src_file_id = first_line_up->getSourceFileId(); 195 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); 196 if (!src_file_up) 197 return false; 198 199 FileSpec spec(src_file_up->getFileName(), /*resolve_path*/false); 200 decl.SetFile(spec); 201 decl.SetColumn(first_line_up->getColumnNumber()); 202 decl.SetLine(first_line_up->getLineNumber()); 203 return true; 204 } 205 } 206 207 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 208 209 PDBASTParser::~PDBASTParser() {} 210 211 // DebugInfoASTParser interface 212 213 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 214 // PDB doesn't maintain enough information to robustly rebuild the entire 215 // tree, and this is most problematic when it comes to figure out the 216 // right DeclContext to put a type in. So for now, everything goes in 217 // the translation unit decl as a fully qualified type. 218 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 219 Declaration decl; 220 221 switch (type.getSymTag()) { 222 case PDB_SymType::UDT: { 223 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); 224 assert(udt); 225 AccessType access = lldb::eAccessPublic; 226 PDB_UdtType udt_kind = udt->getUdtKind(); 227 auto tag_type_kind = TranslateUdtKind(udt_kind); 228 if (tag_type_kind == -1) 229 return nullptr; 230 231 if (udt_kind == PDB_UdtType::Class) 232 access = lldb::eAccessPrivate; 233 234 CompilerType clang_type = m_ast.CreateRecordType( 235 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, 236 lldb::eLanguageTypeC_plus_plus, nullptr); 237 238 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 239 240 return std::make_shared<lldb_private::Type>( 241 type.getSymIndexId(), m_ast.GetSymbolFile(), 242 ConstString(udt->getName()), udt->getLength(), nullptr, 243 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 244 lldb_private::Type::eResolveStateForward); 245 } break; 246 case PDB_SymType::Enum: { 247 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); 248 assert(enum_type); 249 auto underlying_type_up = enum_type->getUnderlyingType(); 250 if (!underlying_type_up) 251 return nullptr; 252 lldb::Encoding encoding = 253 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 254 // FIXME: Type of underlying builtin is always `Int`. We correct it with 255 // the very first enumerator's encoding if any. 256 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 257 if (first_child) { 258 encoding = TranslateEnumEncoding(first_child->getValue().Type); 259 } 260 std::string name = enum_type->getName(); 261 uint64_t bytes = enum_type->getLength(); 262 CompilerType builtin_type; 263 if (bytes > 0) 264 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 265 &m_ast, underlying_type_up.get(), encoding, bytes * 8); 266 else 267 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 268 // FIXME: PDB does not have information about scoped enumeration (Enum Class). 269 // Set it false for now. 270 bool isScoped = false; 271 272 CompilerType ast_enum = m_ast.CreateEnumerationType( 273 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); 274 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 275 if (enum_values) { 276 while (auto enum_value = enum_values->getNext()) { 277 if (enum_value->getDataKind() != PDB_DataKind::Constant) 278 continue; 279 AddEnumValue(ast_enum, *enum_value); 280 } 281 } 282 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 283 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 284 285 GetDeclarationForSymbol(type, decl); 286 return std::make_shared<lldb_private::Type>( 287 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 288 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 289 ast_enum, lldb_private::Type::eResolveStateFull); 290 } break; 291 case PDB_SymType::Typedef: { 292 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); 293 assert(type_def); 294 lldb_private::Type *target_type = 295 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 296 if (!target_type) 297 return nullptr; 298 std::string name = type_def->getName(); 299 uint64_t bytes = type_def->getLength(); 300 CompilerType target_ast_type = target_type->GetFullCompilerType(); 301 CompilerDeclContext target_decl_ctx = 302 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 303 CompilerType ast_typedef = 304 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 305 if (!ast_typedef) 306 return nullptr; 307 308 return std::make_shared<lldb_private::Type>( 309 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 310 bytes, nullptr, target_type->GetID(), 311 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 312 lldb_private::Type::eResolveStateFull); 313 } break; 314 case PDB_SymType::Function: 315 case PDB_SymType::FunctionSig: { 316 std::string name; 317 PDBSymbolTypeFunctionSig *func_sig = nullptr; 318 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { 319 auto sig = pdb_func->getSignature(); 320 if (!sig) 321 return nullptr; 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); 328 } else 329 llvm_unreachable("Unexpected PDB symbol!"); 330 331 auto arg_enum = func_sig->getArguments(); 332 uint32_t num_args = arg_enum->getChildCount(); 333 std::vector<CompilerType> arg_list; 334 335 bool is_variadic = func_sig->isCVarArgs(); 336 // Drop last variadic argument. 337 if (is_variadic) 338 --num_args; 339 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 340 auto arg = arg_enum->getChildAtIndex(arg_idx); 341 if (!arg) 342 break; 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. 347 if (!arg_type) 348 return nullptr; 349 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 350 arg_list.push_back(arg_ast_type); 351 } 352 lldbassert(arg_list.size() <= num_args); 353 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. 359 if (!return_type) 360 return nullptr; 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 = m_ast.CreateFunctionType( 368 return_ast_type, arg_list.data(), arg_list.size(), is_variadic, 369 type_quals); 370 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); 376 } break; 377 case PDB_SymType::ArrayType: { 378 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); 379 assert(array_type); 380 uint32_t num_elements = array_type->getCount(); 381 uint32_t element_uid = array_type->getElementTypeId(); 382 uint32_t bytes = array_type->getLength(); 383 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); 388 if (!element_type) 389 return nullptr; 390 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); 397 } else { 398 // We are not able to start defintion. 399 return nullptr; 400 } 401 } 402 CompilerType array_ast_type = 403 m_ast.CreateArrayType(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); 409 return type_sp; 410 } break; 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) 416 return nullptr; 417 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); 422 423 if (builtin_type->isConstType()) 424 builtin_ast_type = builtin_ast_type.AddConstModifier(); 425 426 if (builtin_type->isVolatileType()) 427 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 428 429 auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type); 430 431 return std::make_shared<lldb_private::Type>( 432 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, 433 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 434 decl, builtin_ast_type, lldb_private::Type::eResolveStateFull); 435 } break; 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()); 441 if (!pointee_type) 442 return nullptr; 443 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->getRawSymbol().isRValueReference()) 449 pointer_ast_type = pointer_ast_type.GetRValueReferenceType(); 450 else 451 pointer_ast_type = pointer_ast_type.GetPointerType(); 452 453 if (pointer_type->isConstType()) 454 pointer_ast_type = pointer_ast_type.AddConstModifier(); 455 456 if (pointer_type->isVolatileType()) 457 pointer_ast_type = pointer_ast_type.AddVolatileModifier(); 458 459 if (pointer_type->getRawSymbol().isRestrictedType()) 460 pointer_ast_type = pointer_ast_type.AddRestrictModifier(); 461 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); 467 } break; 468 default: break; 469 } 470 return nullptr; 471 } 472 473 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 474 const PDBSymbolData &enum_value) const { 475 Declaration decl; 476 Variant v = enum_value.getValue(); 477 std::string name = enum_value.getName(); 478 int64_t raw_value; 479 switch (v.Type) { 480 case PDB_VariantType::Int8: 481 raw_value = v.Value.Int8; 482 break; 483 case PDB_VariantType::Int16: 484 raw_value = v.Value.Int16; 485 break; 486 case PDB_VariantType::Int32: 487 raw_value = v.Value.Int32; 488 break; 489 case PDB_VariantType::Int64: 490 raw_value = v.Value.Int64; 491 break; 492 case PDB_VariantType::UInt8: 493 raw_value = v.Value.UInt8; 494 break; 495 case PDB_VariantType::UInt16: 496 raw_value = v.Value.UInt16; 497 break; 498 case PDB_VariantType::UInt32: 499 raw_value = v.Value.UInt32; 500 break; 501 case PDB_VariantType::UInt64: 502 raw_value = v.Value.UInt64; 503 break; 504 default: 505 return false; 506 } 507 CompilerType underlying_type = 508 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 509 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 510 ClangUtil::GetQualType(underlying_type)); 511 return m_ast.AddEnumerationValueToEnumerationType( 512 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 513 raw_value, byte_size * 8); 514 } 515