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 "SymbolFilePDB.h" 13 14 #include "clang/AST/CharUnits.h" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/DeclCXX.h" 17 18 #include "lldb/Core/Module.h" 19 #include "lldb/Symbol/ClangASTContext.h" 20 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 21 #include "lldb/Symbol/ClangUtil.h" 22 #include "lldb/Symbol/Declaration.h" 23 #include "lldb/Symbol/SymbolFile.h" 24 #include "lldb/Symbol/TypeSystem.h" 25 26 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 27 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 28 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 29 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 30 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 31 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h" 32 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h" 33 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 34 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h" 35 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" 36 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" 37 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 38 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 39 40 using namespace lldb; 41 using namespace lldb_private; 42 using namespace llvm::pdb; 43 44 namespace { 45 int TranslateUdtKind(PDB_UdtType pdb_kind) { 46 switch (pdb_kind) { 47 case PDB_UdtType::Class: 48 return clang::TTK_Class; 49 case PDB_UdtType::Struct: 50 return clang::TTK_Struct; 51 case PDB_UdtType::Union: 52 return clang::TTK_Union; 53 case PDB_UdtType::Interface: 54 return clang::TTK_Interface; 55 } 56 llvm_unreachable("unsuported PDB UDT type"); 57 } 58 59 lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) { 60 switch (type) { 61 case PDB_BuiltinType::Float: 62 return lldb::eEncodingIEEE754; 63 case PDB_BuiltinType::Int: 64 case PDB_BuiltinType::Long: 65 case PDB_BuiltinType::Char: 66 return lldb::eEncodingSint; 67 case PDB_BuiltinType::Bool: 68 case PDB_BuiltinType::Char16: 69 case PDB_BuiltinType::Char32: 70 case PDB_BuiltinType::UInt: 71 case PDB_BuiltinType::ULong: 72 case PDB_BuiltinType::HResult: 73 case PDB_BuiltinType::WCharT: 74 return lldb::eEncodingUint; 75 default: 76 return lldb::eEncodingInvalid; 77 } 78 } 79 80 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 81 switch (type) { 82 case PDB_VariantType::Int8: 83 case PDB_VariantType::Int16: 84 case PDB_VariantType::Int32: 85 case PDB_VariantType::Int64: 86 return lldb::eEncodingSint; 87 88 case PDB_VariantType::UInt8: 89 case PDB_VariantType::UInt16: 90 case PDB_VariantType::UInt32: 91 case PDB_VariantType::UInt64: 92 return lldb::eEncodingUint; 93 94 default: 95 break; 96 } 97 98 return lldb::eEncodingSint; 99 } 100 101 CompilerType 102 GetBuiltinTypeForPDBEncodingAndBitSize(ClangASTContext &clang_ast, 103 const PDBSymbolTypeBuiltin &pdb_type, 104 Encoding encoding, uint32_t width) { 105 auto *ast = clang_ast.getASTContext(); 106 if (!ast) 107 return CompilerType(); 108 109 switch (pdb_type.getBuiltinType()) { 110 default: 111 break; 112 case PDB_BuiltinType::None: 113 return CompilerType(); 114 case PDB_BuiltinType::Void: 115 return clang_ast.GetBasicType(eBasicTypeVoid); 116 case PDB_BuiltinType::Bool: 117 return clang_ast.GetBasicType(eBasicTypeBool); 118 case PDB_BuiltinType::Long: 119 if (width == ast->getTypeSize(ast->LongTy)) 120 return CompilerType(ast, ast->LongTy); 121 if (width == ast->getTypeSize(ast->LongLongTy)) 122 return CompilerType(ast, ast->LongLongTy); 123 break; 124 case PDB_BuiltinType::ULong: 125 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 126 return CompilerType(ast, ast->UnsignedLongTy); 127 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 128 return CompilerType(ast, ast->UnsignedLongLongTy); 129 break; 130 case PDB_BuiltinType::WCharT: 131 if (width == ast->getTypeSize(ast->WCharTy)) 132 return CompilerType(ast, ast->WCharTy); 133 break; 134 case PDB_BuiltinType::Char16: 135 return CompilerType(ast, ast->Char16Ty); 136 case PDB_BuiltinType::Char32: 137 return CompilerType(ast, ast->Char32Ty); 138 case PDB_BuiltinType::Float: 139 // Note: types `long double` and `double` have same bit size in MSVC and 140 // there is no information in the PDB to distinguish them. So when falling 141 // back to default search, the compiler type of `long double` will be 142 // represented by the one generated for `double`. 143 break; 144 } 145 // If there is no match on PDB_BuiltinType, fall back to default search by 146 // encoding and width only 147 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width); 148 } 149 150 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, 151 CompilerType &compiler_type) { 152 PDB_BuiltinType kind = pdb_type.getBuiltinType(); 153 switch (kind) { 154 default: 155 break; 156 case PDB_BuiltinType::Currency: 157 return ConstString("CURRENCY"); 158 case PDB_BuiltinType::Date: 159 return ConstString("DATE"); 160 case PDB_BuiltinType::Variant: 161 return ConstString("VARIANT"); 162 case PDB_BuiltinType::Complex: 163 return ConstString("complex"); 164 case PDB_BuiltinType::Bitfield: 165 return ConstString("bitfield"); 166 case PDB_BuiltinType::BSTR: 167 return ConstString("BSTR"); 168 case PDB_BuiltinType::HResult: 169 return ConstString("HRESULT"); 170 case PDB_BuiltinType::BCD: 171 return ConstString("BCD"); 172 case PDB_BuiltinType::Char16: 173 return ConstString("char16_t"); 174 case PDB_BuiltinType::Char32: 175 return ConstString("char32_t"); 176 case PDB_BuiltinType::None: 177 return ConstString("..."); 178 } 179 return compiler_type.GetTypeName(); 180 } 181 182 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { 183 auto &raw_sym = symbol.getRawSymbol(); 184 auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); 185 186 if (!first_line_up) { 187 auto lines_up = symbol.getSession().findLineNumbersByAddress( 188 raw_sym.getVirtualAddress(), raw_sym.getLength()); 189 if (!lines_up) 190 return false; 191 first_line_up = lines_up->getNext(); 192 if (!first_line_up) 193 return false; 194 } 195 uint32_t src_file_id = first_line_up->getSourceFileId(); 196 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); 197 if (!src_file_up) 198 return false; 199 200 FileSpec spec(src_file_up->getFileName(), /*resolve_path*/ false); 201 decl.SetFile(spec); 202 decl.SetColumn(first_line_up->getColumnNumber()); 203 decl.SetLine(first_line_up->getLineNumber()); 204 return true; 205 } 206 207 AccessType TranslateMemberAccess(PDB_MemberAccess access) { 208 switch (access) { 209 case PDB_MemberAccess::Private: 210 return eAccessPrivate; 211 case PDB_MemberAccess::Protected: 212 return eAccessProtected; 213 case PDB_MemberAccess::Public: 214 return eAccessPublic; 215 } 216 return eAccessNone; 217 } 218 219 AccessType GetDefaultAccessibilityForUdtKind(PDB_UdtType udt_kind) { 220 switch (udt_kind) { 221 case PDB_UdtType::Struct: 222 case PDB_UdtType::Union: 223 return eAccessPublic; 224 case PDB_UdtType::Class: 225 case PDB_UdtType::Interface: 226 return eAccessPrivate; 227 } 228 llvm_unreachable("unsupported PDB UDT type"); 229 } 230 231 AccessType GetAccessibilityForUdt(const PDBSymbolTypeUDT &udt) { 232 AccessType access = TranslateMemberAccess(udt.getAccess()); 233 if (access != lldb::eAccessNone || !udt.isNested()) 234 return access; 235 236 auto parent = udt.getClassParent(); 237 if (!parent) 238 return lldb::eAccessNone; 239 240 auto parent_udt = llvm::dyn_cast<PDBSymbolTypeUDT>(parent.get()); 241 if (!parent_udt) 242 return lldb::eAccessNone; 243 244 return GetDefaultAccessibilityForUdtKind(parent_udt->getUdtKind()); 245 } 246 247 clang::MSInheritanceAttr::Spelling GetMSInheritance( 248 const PDBSymbolTypeUDT &udt) { 249 int base_count = 0; 250 bool has_virtual = false; 251 252 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>(); 253 if (bases_enum) { 254 while (auto base = bases_enum->getNext()) { 255 base_count++; 256 has_virtual |= base->isVirtualBaseClass(); 257 } 258 } 259 260 if (has_virtual) 261 return clang::MSInheritanceAttr::Keyword_virtual_inheritance; 262 if (base_count > 1) 263 return clang::MSInheritanceAttr::Keyword_multiple_inheritance; 264 return clang::MSInheritanceAttr::Keyword_single_inheritance; 265 } 266 } // namespace 267 268 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 269 270 PDBASTParser::~PDBASTParser() {} 271 272 // DebugInfoASTParser interface 273 274 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 275 // PDB doesn't maintain enough information to robustly rebuild the entire 276 // tree, and this is most problematic when it comes to figure out the right 277 // DeclContext to put a type in. So for now, everything goes in the 278 // translation unit decl as a fully qualified type. 279 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 280 Declaration decl; 281 282 switch (type.getSymTag()) { 283 case PDB_SymType::BaseClass: { 284 auto symbol_file = m_ast.GetSymbolFile(); 285 if (!symbol_file) 286 return nullptr; 287 288 auto ty = symbol_file->ResolveTypeUID(type.getRawSymbol().getTypeId()); 289 return ty ? ty->shared_from_this() : nullptr; 290 } break; 291 case PDB_SymType::UDT: { 292 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); 293 assert(udt); 294 295 // Note that, unnamed UDT being typedef-ed is generated as a UDT symbol 296 // other than a Typedef symbol in PDB. For example, 297 // typedef union { short Row; short Col; } Union; 298 // is generated as a named UDT in PDB: 299 // union Union { short Row; short Col; } 300 // Such symbols will be handled here. 301 302 // Some UDT with trival ctor has zero length. Just ignore. 303 if (udt->getLength() == 0) 304 return nullptr; 305 306 // Ignore unnamed-tag UDTs. 307 if (udt->getName().empty()) 308 return nullptr; 309 310 auto access = GetAccessibilityForUdt(*udt); 311 312 auto tag_type_kind = TranslateUdtKind(udt->getUdtKind()); 313 314 ClangASTMetadata metadata; 315 metadata.SetUserID(type.getSymIndexId()); 316 metadata.SetIsDynamicCXXType(false); 317 318 CompilerType clang_type = m_ast.CreateRecordType( 319 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, 320 lldb::eLanguageTypeC_plus_plus, &metadata); 321 assert(clang_type.IsValid()); 322 323 if (udt->isConstType()) 324 clang_type = clang_type.AddConstModifier(); 325 326 if (udt->isVolatileType()) 327 clang_type = clang_type.AddVolatileModifier(); 328 329 clang::CXXRecordDecl *record_decl = 330 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType()); 331 assert(record_decl); 332 auto inheritance_attr = clang::MSInheritanceAttr::CreateImplicit( 333 *m_ast.getASTContext(), GetMSInheritance(*udt)); 334 record_decl->addAttr(inheritance_attr); 335 336 ClangASTContext::StartTagDeclarationDefinition(clang_type); 337 338 Type::ResolveStateTag type_resolve_state_tag; 339 auto children = udt->findAllChildren(); 340 if (!children || children->getChildCount() == 0) { 341 // PDB does not have symbol of forwarder. We assume we get an udt w/o any 342 // fields. Just complete it at this point. 343 ClangASTContext::CompleteTagDeclarationDefinition(clang_type); 344 345 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false); 346 347 type_resolve_state_tag = Type::eResolveStateFull; 348 } else { 349 // Add the type to the forward declarations. It will help us to avoid 350 // an endless recursion in CompleteTypeFromUdt function. 351 auto clang_type_removed_fast_quals = 352 ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType(); 353 m_forward_decl_clang_type_to_uid[clang_type_removed_fast_quals] = 354 type.getSymIndexId(); 355 356 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 357 358 type_resolve_state_tag = Type::eResolveStateForward; 359 } 360 361 GetDeclarationForSymbol(type, decl); 362 return std::make_shared<lldb_private::Type>( 363 type.getSymIndexId(), m_ast.GetSymbolFile(), 364 ConstString(udt->getName()), udt->getLength(), nullptr, 365 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 366 type_resolve_state_tag); 367 } break; 368 case PDB_SymType::Enum: { 369 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); 370 assert(enum_type); 371 auto underlying_type_up = enum_type->getUnderlyingType(); 372 if (!underlying_type_up) 373 return nullptr; 374 lldb::Encoding encoding = 375 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 376 // FIXME: Type of underlying builtin is always `Int`. We correct it with 377 // the very first enumerator's encoding if any. 378 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 379 if (first_child) { 380 encoding = TranslateEnumEncoding(first_child->getValue().Type); 381 } 382 std::string name = enum_type->getName(); 383 uint64_t bytes = enum_type->getLength(); 384 CompilerType builtin_type; 385 if (bytes > 0) 386 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 387 m_ast, *underlying_type_up, encoding, bytes * 8); 388 else 389 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 390 // FIXME: PDB does not have information about scoped enumeration (Enum 391 // Class). Set it false for now. 392 bool isScoped = false; 393 394 CompilerType ast_enum = m_ast.CreateEnumerationType( 395 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); 396 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 397 if (enum_values) { 398 while (auto enum_value = enum_values->getNext()) { 399 if (enum_value->getDataKind() != PDB_DataKind::Constant) 400 continue; 401 AddEnumValue(ast_enum, *enum_value); 402 } 403 } 404 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 405 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 406 407 GetDeclarationForSymbol(type, decl); 408 return std::make_shared<lldb_private::Type>( 409 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 410 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 411 ast_enum, lldb_private::Type::eResolveStateFull); 412 } break; 413 case PDB_SymType::Typedef: { 414 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); 415 assert(type_def); 416 lldb_private::Type *target_type = 417 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 418 if (!target_type) 419 return nullptr; 420 std::string name = type_def->getName(); 421 uint64_t bytes = type_def->getLength(); 422 CompilerType target_ast_type = target_type->GetFullCompilerType(); 423 CompilerDeclContext target_decl_ctx = 424 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 425 CompilerType ast_typedef = 426 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 427 if (!ast_typedef) 428 return nullptr; 429 430 return std::make_shared<lldb_private::Type>( 431 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 432 bytes, nullptr, target_type->GetID(), 433 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 434 lldb_private::Type::eResolveStateFull); 435 } break; 436 case PDB_SymType::Function: 437 case PDB_SymType::FunctionSig: { 438 std::string name; 439 PDBSymbolTypeFunctionSig *func_sig = nullptr; 440 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { 441 if (pdb_func->isCompilerGenerated()) 442 return nullptr; 443 444 auto sig = pdb_func->getSignature(); 445 if (!sig) 446 return nullptr; 447 func_sig = sig.release(); 448 // Function type is named. 449 name = pdb_func->getName(); 450 } else if (auto pdb_func_sig = 451 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 452 func_sig = const_cast<PDBSymbolTypeFunctionSig *>(pdb_func_sig); 453 } else 454 llvm_unreachable("Unexpected PDB symbol!"); 455 456 auto arg_enum = func_sig->getArguments(); 457 uint32_t num_args = arg_enum->getChildCount(); 458 std::vector<CompilerType> arg_list; 459 460 bool is_variadic = func_sig->isCVarArgs(); 461 // Drop last variadic argument. 462 if (is_variadic) 463 --num_args; 464 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 465 auto arg = arg_enum->getChildAtIndex(arg_idx); 466 if (!arg) 467 break; 468 lldb_private::Type *arg_type = 469 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 470 // If there's some error looking up one of the dependent types of this 471 // function signature, bail. 472 if (!arg_type) 473 return nullptr; 474 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 475 arg_list.push_back(arg_ast_type); 476 } 477 lldbassert(arg_list.size() <= num_args); 478 479 auto pdb_return_type = func_sig->getReturnType(); 480 lldb_private::Type *return_type = 481 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 482 // If there's some error looking up one of the dependent types of this 483 // function signature, bail. 484 if (!return_type) 485 return nullptr; 486 CompilerType return_ast_type = return_type->GetFullCompilerType(); 487 uint32_t type_quals = 0; 488 if (func_sig->isConstType()) 489 type_quals |= clang::Qualifiers::Const; 490 if (func_sig->isVolatileType()) 491 type_quals |= clang::Qualifiers::Volatile; 492 CompilerType func_sig_ast_type = 493 m_ast.CreateFunctionType(return_ast_type, arg_list.data(), 494 arg_list.size(), is_variadic, type_quals); 495 496 GetDeclarationForSymbol(type, decl); 497 return std::make_shared<lldb_private::Type>( 498 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, 499 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 500 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 501 } break; 502 case PDB_SymType::ArrayType: { 503 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); 504 assert(array_type); 505 uint32_t num_elements = array_type->getCount(); 506 uint32_t element_uid = array_type->getElementTypeId(); 507 uint32_t bytes = array_type->getLength(); 508 509 // If array rank > 0, PDB gives the element type at N=0. So element type 510 // will parsed in the order N=0, N=1,..., N=rank sequentially. 511 lldb_private::Type *element_type = 512 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 513 if (!element_type) 514 return nullptr; 515 516 CompilerType element_ast_type = element_type->GetForwardCompilerType(); 517 // If element type is UDT, it needs to be complete. 518 if (ClangASTContext::IsCXXClassType(element_ast_type) && 519 element_ast_type.GetCompleteType() == false) { 520 if (ClangASTContext::StartTagDeclarationDefinition(element_ast_type)) { 521 ClangASTContext::CompleteTagDeclarationDefinition(element_ast_type); 522 } else { 523 // We are not able to start defintion. 524 return nullptr; 525 } 526 } 527 CompilerType array_ast_type = m_ast.CreateArrayType( 528 element_ast_type, num_elements, /*is_gnu_vector*/ false); 529 TypeSP type_sp = std::make_shared<lldb_private::Type>( 530 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 531 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 532 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 533 type_sp->SetEncodingType(element_type); 534 return type_sp; 535 } break; 536 case PDB_SymType::BuiltinType: { 537 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); 538 assert(builtin_type); 539 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType(); 540 if (builtin_kind == PDB_BuiltinType::None) 541 return nullptr; 542 543 uint64_t bytes = builtin_type->getLength(); 544 Encoding encoding = TranslateBuiltinEncoding(builtin_kind); 545 CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( 546 m_ast, *builtin_type, encoding, bytes * 8); 547 548 if (builtin_type->isConstType()) 549 builtin_ast_type = builtin_ast_type.AddConstModifier(); 550 551 if (builtin_type->isVolatileType()) 552 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 553 554 auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type); 555 556 return std::make_shared<lldb_private::Type>( 557 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, bytes, 558 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 559 builtin_ast_type, lldb_private::Type::eResolveStateFull); 560 } break; 561 case PDB_SymType::PointerType: { 562 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); 563 assert(pointer_type); 564 Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID( 565 pointer_type->getPointeeType()->getSymIndexId()); 566 if (!pointee_type) 567 return nullptr; 568 569 if (pointer_type->isPointerToDataMember() || 570 pointer_type->isPointerToMemberFunction()) { 571 auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId(); 572 auto class_parent_type = 573 m_ast.GetSymbolFile()->ResolveTypeUID(class_parent_uid); 574 assert(class_parent_type); 575 576 CompilerType pointer_ast_type; 577 pointer_ast_type = ClangASTContext::CreateMemberPointerType( 578 class_parent_type->GetLayoutCompilerType(), 579 pointee_type->GetForwardCompilerType()); 580 assert(pointer_ast_type); 581 582 return std::make_shared<lldb_private::Type>( 583 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 584 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 585 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, 586 lldb_private::Type::eResolveStateForward); 587 } 588 589 CompilerType pointer_ast_type; 590 pointer_ast_type = pointee_type->GetFullCompilerType(); 591 if (pointer_type->isReference()) 592 pointer_ast_type = pointer_ast_type.GetLValueReferenceType(); 593 else if (pointer_type->isRValueReference()) 594 pointer_ast_type = pointer_ast_type.GetRValueReferenceType(); 595 else 596 pointer_ast_type = pointer_ast_type.GetPointerType(); 597 598 if (pointer_type->isConstType()) 599 pointer_ast_type = pointer_ast_type.AddConstModifier(); 600 601 if (pointer_type->isVolatileType()) 602 pointer_ast_type = pointer_ast_type.AddVolatileModifier(); 603 604 if (pointer_type->isRestrictedType()) 605 pointer_ast_type = pointer_ast_type.AddRestrictModifier(); 606 607 return std::make_shared<lldb_private::Type>( 608 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 609 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 610 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, 611 lldb_private::Type::eResolveStateFull); 612 } break; 613 default: 614 break; 615 } 616 return nullptr; 617 } 618 619 bool PDBASTParser::CompleteTypeFromPDB( 620 lldb_private::CompilerType &compiler_type) { 621 if (GetClangASTImporter().CanImport(compiler_type)) 622 return GetClangASTImporter().CompleteType(compiler_type); 623 624 // Remove the type from the forward declarations to avoid 625 // an endless recursion for types like a linked list. 626 CompilerType compiler_type_no_qualifiers = 627 ClangUtil::RemoveFastQualifiers(compiler_type); 628 auto uid_it = m_forward_decl_clang_type_to_uid.find( 629 compiler_type_no_qualifiers.GetOpaqueQualType()); 630 if (uid_it == m_forward_decl_clang_type_to_uid.end()) 631 return true; 632 633 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 634 if (!symbol_file) 635 return false; 636 637 std::unique_ptr<PDBSymbol> symbol = 638 symbol_file->GetPDBSession().getSymbolById(uid_it->getSecond()); 639 if (!symbol) 640 return false; 641 642 m_forward_decl_clang_type_to_uid.erase(uid_it); 643 644 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(), 645 false); 646 647 switch (symbol->getSymTag()) { 648 case PDB_SymType::UDT: { 649 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(symbol.get()); 650 if (!udt) 651 return false; 652 653 return CompleteTypeFromUDT(*symbol_file, compiler_type, *udt); 654 } 655 default: 656 llvm_unreachable("not a forward clang type decl!"); 657 } 658 } 659 660 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 661 const PDBSymbolData &enum_value) const { 662 Declaration decl; 663 Variant v = enum_value.getValue(); 664 std::string name = enum_value.getName(); 665 int64_t raw_value; 666 switch (v.Type) { 667 case PDB_VariantType::Int8: 668 raw_value = v.Value.Int8; 669 break; 670 case PDB_VariantType::Int16: 671 raw_value = v.Value.Int16; 672 break; 673 case PDB_VariantType::Int32: 674 raw_value = v.Value.Int32; 675 break; 676 case PDB_VariantType::Int64: 677 raw_value = v.Value.Int64; 678 break; 679 case PDB_VariantType::UInt8: 680 raw_value = v.Value.UInt8; 681 break; 682 case PDB_VariantType::UInt16: 683 raw_value = v.Value.UInt16; 684 break; 685 case PDB_VariantType::UInt32: 686 raw_value = v.Value.UInt32; 687 break; 688 case PDB_VariantType::UInt64: 689 raw_value = v.Value.UInt64; 690 break; 691 default: 692 return false; 693 } 694 CompilerType underlying_type = 695 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 696 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 697 ClangUtil::GetQualType(underlying_type)); 698 return m_ast.AddEnumerationValueToEnumerationType( 699 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 700 raw_value, byte_size * 8); 701 } 702 703 bool PDBASTParser::CompleteTypeFromUDT( 704 lldb_private::SymbolFile &symbol_file, 705 lldb_private::CompilerType &compiler_type, 706 llvm::pdb::PDBSymbolTypeUDT &udt) { 707 ClangASTImporter::LayoutInfo layout_info; 708 layout_info.bit_size = udt.getLength() * 8; 709 710 auto nested_enums = udt.findAllChildren<PDBSymbolTypeUDT>(); 711 if (nested_enums) 712 while (auto nested = nested_enums->getNext()) 713 symbol_file.ResolveTypeUID(nested->getSymIndexId()); 714 715 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>(); 716 if (bases_enum) 717 AddRecordBases(symbol_file, compiler_type, 718 TranslateUdtKind(udt.getUdtKind()), *bases_enum, 719 layout_info); 720 721 auto members_enum = udt.findAllChildren<PDBSymbolData>(); 722 if (members_enum) 723 AddRecordMembers(symbol_file, compiler_type, *members_enum, layout_info); 724 725 auto methods_enum = udt.findAllChildren<PDBSymbolFunc>(); 726 if (methods_enum) 727 AddRecordMethods(symbol_file, compiler_type, *methods_enum); 728 729 m_ast.AddMethodOverridesForCXXRecordType(compiler_type.GetOpaqueQualType()); 730 ClangASTContext::BuildIndirectFields(compiler_type); 731 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type); 732 733 clang::CXXRecordDecl *record_decl = 734 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType()); 735 if (!record_decl) 736 return static_cast<bool>(compiler_type); 737 738 GetClangASTImporter().InsertRecordDecl(record_decl, layout_info); 739 740 return static_cast<bool>(compiler_type); 741 } 742 743 void PDBASTParser::AddRecordMembers( 744 lldb_private::SymbolFile &symbol_file, 745 lldb_private::CompilerType &record_type, 746 PDBDataSymbolEnumerator &members_enum, 747 lldb_private::ClangASTImporter::LayoutInfo &layout_info) const { 748 while (auto member = members_enum.getNext()) { 749 if (member->isCompilerGenerated()) 750 continue; 751 752 auto member_name = member->getName(); 753 754 auto member_type = symbol_file.ResolveTypeUID(member->getTypeId()); 755 if (!member_type) 756 continue; 757 758 auto member_comp_type = member_type->GetLayoutCompilerType(); 759 if (!member_comp_type.GetCompleteType()) { 760 symbol_file.GetObjectFile()->GetModule()->ReportError( 761 ":: Class '%s' has a member '%s' of type '%s' " 762 "which does not have a complete definition.", 763 record_type.GetTypeName().GetCString(), member_name.c_str(), 764 member_comp_type.GetTypeName().GetCString()); 765 if (ClangASTContext::StartTagDeclarationDefinition(member_comp_type)) 766 ClangASTContext::CompleteTagDeclarationDefinition(member_comp_type); 767 } 768 769 auto access = TranslateMemberAccess(member->getAccess()); 770 771 switch (member->getDataKind()) { 772 case PDB_DataKind::Member: { 773 auto location_type = member->getLocationType(); 774 775 auto bit_size = member->getLength(); 776 if (location_type == PDB_LocType::ThisRel) 777 bit_size *= 8; 778 779 auto decl = ClangASTContext::AddFieldToRecordType( 780 record_type, member_name.c_str(), member_comp_type, access, bit_size); 781 if (!decl) 782 continue; 783 784 auto offset = member->getOffset() * 8; 785 if (location_type == PDB_LocType::BitField) 786 offset += member->getBitPosition(); 787 788 layout_info.field_offsets.insert(std::make_pair(decl, offset)); 789 790 break; 791 } 792 case PDB_DataKind::StaticMember: 793 ClangASTContext::AddVariableToRecordType(record_type, member_name.c_str(), 794 member_comp_type, access); 795 break; 796 default: 797 llvm_unreachable("unsupported PDB data kind"); 798 } 799 } 800 } 801 802 void PDBASTParser::AddRecordBases( 803 lldb_private::SymbolFile &symbol_file, 804 lldb_private::CompilerType &record_type, int record_kind, 805 PDBBaseClassSymbolEnumerator &bases_enum, 806 lldb_private::ClangASTImporter::LayoutInfo &layout_info) const { 807 std::vector<clang::CXXBaseSpecifier *> base_classes; 808 while (auto base = bases_enum.getNext()) { 809 auto base_type = symbol_file.ResolveTypeUID(base->getTypeId()); 810 if (!base_type) 811 continue; 812 813 auto base_comp_type = base_type->GetFullCompilerType(); 814 if (!base_comp_type.GetCompleteType()) { 815 symbol_file.GetObjectFile()->GetModule()->ReportError( 816 ":: Class '%s' has a base class '%s' " 817 "which does not have a complete definition.", 818 record_type.GetTypeName().GetCString(), 819 base_comp_type.GetTypeName().GetCString()); 820 if (ClangASTContext::StartTagDeclarationDefinition(base_comp_type)) 821 ClangASTContext::CompleteTagDeclarationDefinition(base_comp_type); 822 } 823 824 auto access = TranslateMemberAccess(base->getAccess()); 825 826 auto is_virtual = base->isVirtualBaseClass(); 827 828 auto base_class_spec = m_ast.CreateBaseClassSpecifier( 829 base_comp_type.GetOpaqueQualType(), access, is_virtual, 830 record_kind == clang::TTK_Class); 831 if (!base_class_spec) 832 continue; 833 834 base_classes.push_back(base_class_spec); 835 836 if (is_virtual) 837 continue; 838 839 auto decl = m_ast.GetAsCXXRecordDecl(base_comp_type.GetOpaqueQualType()); 840 if (!decl) 841 continue; 842 843 auto offset = clang::CharUnits::fromQuantity(base->getOffset()); 844 layout_info.base_offsets.insert(std::make_pair(decl, offset)); 845 } 846 if (!base_classes.empty()) { 847 m_ast.SetBaseClassesForClassType(record_type.GetOpaqueQualType(), 848 &base_classes.front(), 849 base_classes.size()); 850 ClangASTContext::DeleteBaseClassSpecifiers(&base_classes.front(), 851 base_classes.size()); 852 } 853 } 854 855 void PDBASTParser::AddRecordMethods( 856 lldb_private::SymbolFile &symbol_file, 857 lldb_private::CompilerType &record_type, 858 PDBFuncSymbolEnumerator &methods_enum) const { 859 while (auto method = methods_enum.getNext()) { 860 auto method_type = symbol_file.ResolveTypeUID(method->getSymIndexId()); 861 // MSVC specific __vecDelDtor. 862 if (!method_type) 863 break; 864 865 auto method_comp_type = method_type->GetFullCompilerType(); 866 if (!method_comp_type.GetCompleteType()) { 867 symbol_file.GetObjectFile()->GetModule()->ReportError( 868 ":: Class '%s' has a method '%s' whose type cannot be completed.", 869 record_type.GetTypeName().GetCString(), 870 method_comp_type.GetTypeName().GetCString()); 871 if (ClangASTContext::StartTagDeclarationDefinition(method_comp_type)) 872 ClangASTContext::CompleteTagDeclarationDefinition(method_comp_type); 873 } 874 875 // TODO: get mangled name for the method. 876 m_ast.AddMethodToCXXRecordType( 877 record_type.GetOpaqueQualType(), method->getName().c_str(), 878 /*mangled_name*/ nullptr, method_comp_type, 879 TranslateMemberAccess(method->getAccess()), method->isVirtual(), 880 method->isStatic(), method->hasInlineAttribute(), 881 /*is_explicit*/ false, // FIXME: Need this field in CodeView. 882 /*is_attr_used*/ false, 883 /*is_artificial*/ method->isCompilerGenerated()); 884 } 885 } 886