1 //===-- PDBASTParser.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "PDBASTParser.h" 10 11 #include "SymbolFilePDB.h" 12 13 #include "clang/AST/CharUnits.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclCXX.h" 16 17 #include "lldb/Core/Module.h" 18 #include "lldb/Symbol/ClangASTContext.h" 19 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 20 #include "lldb/Symbol/ClangUtil.h" 21 #include "lldb/Symbol/Declaration.h" 22 #include "lldb/Symbol/SymbolFile.h" 23 #include "lldb/Symbol/TypeMap.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 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" 41 42 using namespace lldb; 43 using namespace lldb_private; 44 using namespace llvm::pdb; 45 46 static int TranslateUdtKind(PDB_UdtType pdb_kind) { 47 switch (pdb_kind) { 48 case PDB_UdtType::Class: 49 return clang::TTK_Class; 50 case PDB_UdtType::Struct: 51 return clang::TTK_Struct; 52 case PDB_UdtType::Union: 53 return clang::TTK_Union; 54 case PDB_UdtType::Interface: 55 return clang::TTK_Interface; 56 } 57 llvm_unreachable("unsuported PDB UDT type"); 58 } 59 60 static lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) { 61 switch (type) { 62 case PDB_BuiltinType::Float: 63 return lldb::eEncodingIEEE754; 64 case PDB_BuiltinType::Int: 65 case PDB_BuiltinType::Long: 66 case PDB_BuiltinType::Char: 67 return lldb::eEncodingSint; 68 case PDB_BuiltinType::Bool: 69 case PDB_BuiltinType::Char16: 70 case PDB_BuiltinType::Char32: 71 case PDB_BuiltinType::UInt: 72 case PDB_BuiltinType::ULong: 73 case PDB_BuiltinType::HResult: 74 case PDB_BuiltinType::WCharT: 75 return lldb::eEncodingUint; 76 default: 77 return lldb::eEncodingInvalid; 78 } 79 } 80 81 static lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 82 switch (type) { 83 case PDB_VariantType::Int8: 84 case PDB_VariantType::Int16: 85 case PDB_VariantType::Int32: 86 case PDB_VariantType::Int64: 87 return lldb::eEncodingSint; 88 89 case PDB_VariantType::UInt8: 90 case PDB_VariantType::UInt16: 91 case PDB_VariantType::UInt32: 92 case PDB_VariantType::UInt64: 93 return lldb::eEncodingUint; 94 95 default: 96 break; 97 } 98 99 return lldb::eEncodingSint; 100 } 101 102 static CompilerType 103 GetBuiltinTypeForPDBEncodingAndBitSize(ClangASTContext &clang_ast, 104 const PDBSymbolTypeBuiltin &pdb_type, 105 Encoding encoding, uint32_t width) { 106 auto *ast = clang_ast.getASTContext(); 107 if (!ast) 108 return CompilerType(); 109 110 switch (pdb_type.getBuiltinType()) { 111 default: 112 break; 113 case PDB_BuiltinType::None: 114 return CompilerType(); 115 case PDB_BuiltinType::Void: 116 return clang_ast.GetBasicType(eBasicTypeVoid); 117 case PDB_BuiltinType::Char: 118 return clang_ast.GetBasicType(eBasicTypeChar); 119 case PDB_BuiltinType::Bool: 120 return clang_ast.GetBasicType(eBasicTypeBool); 121 case PDB_BuiltinType::Long: 122 if (width == ast->getTypeSize(ast->LongTy)) 123 return CompilerType(ast, ast->LongTy); 124 if (width == ast->getTypeSize(ast->LongLongTy)) 125 return CompilerType(ast, ast->LongLongTy); 126 break; 127 case PDB_BuiltinType::ULong: 128 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 129 return CompilerType(ast, ast->UnsignedLongTy); 130 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 131 return CompilerType(ast, ast->UnsignedLongLongTy); 132 break; 133 case PDB_BuiltinType::WCharT: 134 if (width == ast->getTypeSize(ast->WCharTy)) 135 return CompilerType(ast, ast->WCharTy); 136 break; 137 case PDB_BuiltinType::Char16: 138 return CompilerType(ast, ast->Char16Ty); 139 case PDB_BuiltinType::Char32: 140 return CompilerType(ast, ast->Char32Ty); 141 case PDB_BuiltinType::Float: 142 // Note: types `long double` and `double` have same bit size in MSVC and 143 // there is no information in the PDB to distinguish them. So when falling 144 // back to default search, the compiler type of `long double` will be 145 // represented by the one generated for `double`. 146 break; 147 } 148 // If there is no match on PDB_BuiltinType, fall back to default search by 149 // encoding and width only 150 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width); 151 } 152 153 static ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, 154 CompilerType &compiler_type) { 155 PDB_BuiltinType kind = pdb_type.getBuiltinType(); 156 switch (kind) { 157 default: 158 break; 159 case PDB_BuiltinType::Currency: 160 return ConstString("CURRENCY"); 161 case PDB_BuiltinType::Date: 162 return ConstString("DATE"); 163 case PDB_BuiltinType::Variant: 164 return ConstString("VARIANT"); 165 case PDB_BuiltinType::Complex: 166 return ConstString("complex"); 167 case PDB_BuiltinType::Bitfield: 168 return ConstString("bitfield"); 169 case PDB_BuiltinType::BSTR: 170 return ConstString("BSTR"); 171 case PDB_BuiltinType::HResult: 172 return ConstString("HRESULT"); 173 case PDB_BuiltinType::BCD: 174 return ConstString("BCD"); 175 case PDB_BuiltinType::Char16: 176 return ConstString("char16_t"); 177 case PDB_BuiltinType::Char32: 178 return ConstString("char32_t"); 179 case PDB_BuiltinType::None: 180 return ConstString("..."); 181 } 182 return compiler_type.GetTypeName(); 183 } 184 185 static bool GetDeclarationForSymbol(const PDBSymbol &symbol, 186 Declaration &decl) { 187 auto &raw_sym = symbol.getRawSymbol(); 188 auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); 189 190 if (!first_line_up) { 191 auto lines_up = symbol.getSession().findLineNumbersByAddress( 192 raw_sym.getVirtualAddress(), raw_sym.getLength()); 193 if (!lines_up) 194 return false; 195 first_line_up = lines_up->getNext(); 196 if (!first_line_up) 197 return false; 198 } 199 uint32_t src_file_id = first_line_up->getSourceFileId(); 200 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); 201 if (!src_file_up) 202 return false; 203 204 FileSpec spec(src_file_up->getFileName()); 205 decl.SetFile(spec); 206 decl.SetColumn(first_line_up->getColumnNumber()); 207 decl.SetLine(first_line_up->getLineNumber()); 208 return true; 209 } 210 211 static AccessType TranslateMemberAccess(PDB_MemberAccess access) { 212 switch (access) { 213 case PDB_MemberAccess::Private: 214 return eAccessPrivate; 215 case PDB_MemberAccess::Protected: 216 return eAccessProtected; 217 case PDB_MemberAccess::Public: 218 return eAccessPublic; 219 } 220 return eAccessNone; 221 } 222 223 static AccessType GetDefaultAccessibilityForUdtKind(PDB_UdtType udt_kind) { 224 switch (udt_kind) { 225 case PDB_UdtType::Struct: 226 case PDB_UdtType::Union: 227 return eAccessPublic; 228 case PDB_UdtType::Class: 229 case PDB_UdtType::Interface: 230 return eAccessPrivate; 231 } 232 llvm_unreachable("unsupported PDB UDT type"); 233 } 234 235 static AccessType GetAccessibilityForUdt(const PDBSymbolTypeUDT &udt) { 236 AccessType access = TranslateMemberAccess(udt.getAccess()); 237 if (access != lldb::eAccessNone || !udt.isNested()) 238 return access; 239 240 auto parent = udt.getClassParent(); 241 if (!parent) 242 return lldb::eAccessNone; 243 244 auto parent_udt = llvm::dyn_cast<PDBSymbolTypeUDT>(parent.get()); 245 if (!parent_udt) 246 return lldb::eAccessNone; 247 248 return GetDefaultAccessibilityForUdtKind(parent_udt->getUdtKind()); 249 } 250 251 static clang::MSInheritanceAttr::Spelling 252 GetMSInheritance(const PDBSymbolTypeUDT &udt) { 253 int base_count = 0; 254 bool has_virtual = false; 255 256 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>(); 257 if (bases_enum) { 258 while (auto base = bases_enum->getNext()) { 259 base_count++; 260 has_virtual |= base->isVirtualBaseClass(); 261 } 262 } 263 264 if (has_virtual) 265 return clang::MSInheritanceAttr::Keyword_virtual_inheritance; 266 if (base_count > 1) 267 return clang::MSInheritanceAttr::Keyword_multiple_inheritance; 268 return clang::MSInheritanceAttr::Keyword_single_inheritance; 269 } 270 271 static std::unique_ptr<llvm::pdb::PDBSymbol> 272 GetClassOrFunctionParent(const llvm::pdb::PDBSymbol &symbol) { 273 const IPDBSession &session = symbol.getSession(); 274 const IPDBRawSymbol &raw = symbol.getRawSymbol(); 275 auto tag = symbol.getSymTag(); 276 277 // For items that are nested inside of a class, return the class that it is 278 // nested inside of. 279 // Note that only certain items can be nested inside of classes. 280 switch (tag) { 281 case PDB_SymType::Function: 282 case PDB_SymType::Data: 283 case PDB_SymType::UDT: 284 case PDB_SymType::Enum: 285 case PDB_SymType::FunctionSig: 286 case PDB_SymType::Typedef: 287 case PDB_SymType::BaseClass: 288 case PDB_SymType::VTable: { 289 auto class_parent_id = raw.getClassParentId(); 290 if (auto class_parent = session.getSymbolById(class_parent_id)) 291 return class_parent; 292 break; 293 } 294 default: 295 break; 296 } 297 298 // Otherwise, if it is nested inside of a function, return the function. 299 // Note that only certain items can be nested inside of functions. 300 switch (tag) { 301 case PDB_SymType::Block: 302 case PDB_SymType::Data: { 303 auto lexical_parent_id = raw.getLexicalParentId(); 304 auto lexical_parent = session.getSymbolById(lexical_parent_id); 305 if (!lexical_parent) 306 return nullptr; 307 308 auto lexical_parent_tag = lexical_parent->getSymTag(); 309 if (lexical_parent_tag == PDB_SymType::Function) 310 return lexical_parent; 311 if (lexical_parent_tag == PDB_SymType::Exe) 312 return nullptr; 313 314 return GetClassOrFunctionParent(*lexical_parent); 315 } 316 default: 317 return nullptr; 318 } 319 } 320 321 static clang::NamedDecl * 322 GetDeclFromContextByName(const clang::ASTContext &ast, 323 const clang::DeclContext &decl_context, 324 llvm::StringRef name) { 325 clang::IdentifierInfo &ident = ast.Idents.get(name); 326 clang::DeclarationName decl_name = ast.DeclarationNames.getIdentifier(&ident); 327 clang::DeclContext::lookup_result result = decl_context.lookup(decl_name); 328 if (result.empty()) 329 return nullptr; 330 331 return result[0]; 332 } 333 334 static bool IsAnonymousNamespaceName(llvm::StringRef name) { 335 return name == "`anonymous namespace'" || name == "`anonymous-namespace'"; 336 } 337 338 static clang::CallingConv TranslateCallingConvention(PDB_CallingConv pdb_cc) { 339 switch (pdb_cc) { 340 case llvm::codeview::CallingConvention::NearC: 341 return clang::CC_C; 342 case llvm::codeview::CallingConvention::NearStdCall: 343 return clang::CC_X86StdCall; 344 case llvm::codeview::CallingConvention::NearFast: 345 return clang::CC_X86FastCall; 346 case llvm::codeview::CallingConvention::ThisCall: 347 return clang::CC_X86ThisCall; 348 case llvm::codeview::CallingConvention::NearVector: 349 return clang::CC_X86VectorCall; 350 case llvm::codeview::CallingConvention::NearPascal: 351 return clang::CC_X86Pascal; 352 default: 353 assert(false && "Unknown calling convention"); 354 return clang::CC_C; 355 } 356 } 357 358 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 359 360 PDBASTParser::~PDBASTParser() {} 361 362 // DebugInfoASTParser interface 363 364 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 365 Declaration decl; 366 switch (type.getSymTag()) { 367 case PDB_SymType::BaseClass: { 368 auto symbol_file = m_ast.GetSymbolFile(); 369 if (!symbol_file) 370 return nullptr; 371 372 auto ty = symbol_file->ResolveTypeUID(type.getRawSymbol().getTypeId()); 373 return ty ? ty->shared_from_this() : nullptr; 374 } break; 375 case PDB_SymType::UDT: { 376 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); 377 assert(udt); 378 379 // Note that, unnamed UDT being typedef-ed is generated as a UDT symbol 380 // other than a Typedef symbol in PDB. For example, 381 // typedef union { short Row; short Col; } Union; 382 // is generated as a named UDT in PDB: 383 // union Union { short Row; short Col; } 384 // Such symbols will be handled here. 385 386 // Some UDT with trival ctor has zero length. Just ignore. 387 if (udt->getLength() == 0) 388 return nullptr; 389 390 // Ignore unnamed-tag UDTs. 391 std::string name = MSVCUndecoratedNameParser::DropScope(udt->getName()); 392 if (name.empty()) 393 return nullptr; 394 395 auto decl_context = GetDeclContextContainingSymbol(type); 396 397 // Check if such an UDT already exists in the current context. 398 // This may occur with const or volatile types. There are separate type 399 // symbols in PDB for types with const or volatile modifiers, but we need 400 // to create only one declaration for them all. 401 Type::ResolveStateTag type_resolve_state_tag; 402 CompilerType clang_type = m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>( 403 ConstString(name), decl_context); 404 if (!clang_type.IsValid()) { 405 auto access = GetAccessibilityForUdt(*udt); 406 407 auto tag_type_kind = TranslateUdtKind(udt->getUdtKind()); 408 409 ClangASTMetadata metadata; 410 metadata.SetUserID(type.getSymIndexId()); 411 metadata.SetIsDynamicCXXType(false); 412 413 clang_type = m_ast.CreateRecordType( 414 decl_context, access, name.c_str(), tag_type_kind, 415 lldb::eLanguageTypeC_plus_plus, &metadata); 416 assert(clang_type.IsValid()); 417 418 auto record_decl = 419 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType()); 420 assert(record_decl); 421 m_uid_to_decl[type.getSymIndexId()] = record_decl; 422 423 auto inheritance_attr = clang::MSInheritanceAttr::CreateImplicit( 424 *m_ast.getASTContext(), GetMSInheritance(*udt)); 425 record_decl->addAttr(inheritance_attr); 426 427 ClangASTContext::StartTagDeclarationDefinition(clang_type); 428 429 auto children = udt->findAllChildren(); 430 if (!children || children->getChildCount() == 0) { 431 // PDB does not have symbol of forwarder. We assume we get an udt w/o 432 // any fields. Just complete it at this point. 433 ClangASTContext::CompleteTagDeclarationDefinition(clang_type); 434 435 ClangASTContext::SetHasExternalStorage(clang_type.GetOpaqueQualType(), 436 false); 437 438 type_resolve_state_tag = Type::eResolveStateFull; 439 } else { 440 // Add the type to the forward declarations. It will help us to avoid 441 // an endless recursion in CompleteTypeFromUdt function. 442 m_forward_decl_to_uid[record_decl] = type.getSymIndexId(); 443 444 ClangASTContext::SetHasExternalStorage(clang_type.GetOpaqueQualType(), 445 true); 446 447 type_resolve_state_tag = Type::eResolveStateForward; 448 } 449 } else 450 type_resolve_state_tag = Type::eResolveStateForward; 451 452 if (udt->isConstType()) 453 clang_type = clang_type.AddConstModifier(); 454 455 if (udt->isVolatileType()) 456 clang_type = clang_type.AddVolatileModifier(); 457 458 GetDeclarationForSymbol(type, decl); 459 return std::make_shared<lldb_private::Type>( 460 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 461 udt->getLength(), nullptr, LLDB_INVALID_UID, 462 lldb_private::Type::eEncodingIsUID, decl, clang_type, 463 type_resolve_state_tag); 464 } break; 465 case PDB_SymType::Enum: { 466 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); 467 assert(enum_type); 468 469 std::string name = 470 MSVCUndecoratedNameParser::DropScope(enum_type->getName()); 471 auto decl_context = GetDeclContextContainingSymbol(type); 472 uint64_t bytes = enum_type->getLength(); 473 474 // Check if such an enum already exists in the current context 475 CompilerType ast_enum = m_ast.GetTypeForIdentifier<clang::EnumDecl>( 476 ConstString(name), decl_context); 477 if (!ast_enum.IsValid()) { 478 auto underlying_type_up = enum_type->getUnderlyingType(); 479 if (!underlying_type_up) 480 return nullptr; 481 482 lldb::Encoding encoding = 483 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 484 // FIXME: Type of underlying builtin is always `Int`. We correct it with 485 // the very first enumerator's encoding if any. 486 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 487 if (first_child) 488 encoding = TranslateEnumEncoding(first_child->getValue().Type); 489 490 CompilerType builtin_type; 491 if (bytes > 0) 492 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 493 m_ast, *underlying_type_up, encoding, bytes * 8); 494 else 495 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 496 497 // FIXME: PDB does not have information about scoped enumeration (Enum 498 // Class). Set it false for now. 499 bool isScoped = false; 500 501 ast_enum = m_ast.CreateEnumerationType(name.c_str(), decl_context, decl, 502 builtin_type, isScoped); 503 504 auto enum_decl = ClangASTContext::GetAsEnumDecl(ast_enum); 505 assert(enum_decl); 506 m_uid_to_decl[type.getSymIndexId()] = enum_decl; 507 508 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 509 if (enum_values) { 510 while (auto enum_value = enum_values->getNext()) { 511 if (enum_value->getDataKind() != PDB_DataKind::Constant) 512 continue; 513 AddEnumValue(ast_enum, *enum_value); 514 } 515 } 516 517 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 518 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 519 } 520 521 if (enum_type->isConstType()) 522 ast_enum = ast_enum.AddConstModifier(); 523 524 if (enum_type->isVolatileType()) 525 ast_enum = ast_enum.AddVolatileModifier(); 526 527 GetDeclarationForSymbol(type, decl); 528 return std::make_shared<lldb_private::Type>( 529 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 530 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 531 ast_enum, lldb_private::Type::eResolveStateFull); 532 } break; 533 case PDB_SymType::Typedef: { 534 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); 535 assert(type_def); 536 537 lldb_private::Type *target_type = 538 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 539 if (!target_type) 540 return nullptr; 541 542 std::string name = 543 MSVCUndecoratedNameParser::DropScope(type_def->getName()); 544 auto decl_ctx = GetDeclContextContainingSymbol(type); 545 546 // Check if such a typedef already exists in the current context 547 CompilerType ast_typedef = 548 m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(ConstString(name), 549 decl_ctx); 550 if (!ast_typedef.IsValid()) { 551 CompilerType target_ast_type = target_type->GetFullCompilerType(); 552 553 ast_typedef = m_ast.CreateTypedefType( 554 target_ast_type, name.c_str(), CompilerDeclContext(&m_ast, decl_ctx)); 555 if (!ast_typedef) 556 return nullptr; 557 558 auto typedef_decl = ClangASTContext::GetAsTypedefDecl(ast_typedef); 559 assert(typedef_decl); 560 m_uid_to_decl[type.getSymIndexId()] = typedef_decl; 561 } 562 563 if (type_def->isConstType()) 564 ast_typedef = ast_typedef.AddConstModifier(); 565 566 if (type_def->isVolatileType()) 567 ast_typedef = ast_typedef.AddVolatileModifier(); 568 569 GetDeclarationForSymbol(type, decl); 570 return std::make_shared<lldb_private::Type>( 571 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 572 type_def->getLength(), nullptr, target_type->GetID(), 573 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 574 lldb_private::Type::eResolveStateFull); 575 } break; 576 case PDB_SymType::Function: 577 case PDB_SymType::FunctionSig: { 578 std::string name; 579 PDBSymbolTypeFunctionSig *func_sig = nullptr; 580 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { 581 if (pdb_func->isCompilerGenerated()) 582 return nullptr; 583 584 auto sig = pdb_func->getSignature(); 585 if (!sig) 586 return nullptr; 587 func_sig = sig.release(); 588 // Function type is named. 589 name = MSVCUndecoratedNameParser::DropScope(pdb_func->getName()); 590 } else if (auto pdb_func_sig = 591 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 592 func_sig = const_cast<PDBSymbolTypeFunctionSig *>(pdb_func_sig); 593 } else 594 llvm_unreachable("Unexpected PDB symbol!"); 595 596 auto arg_enum = func_sig->getArguments(); 597 uint32_t num_args = arg_enum->getChildCount(); 598 std::vector<CompilerType> arg_list; 599 600 bool is_variadic = func_sig->isCVarArgs(); 601 // Drop last variadic argument. 602 if (is_variadic) 603 --num_args; 604 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 605 auto arg = arg_enum->getChildAtIndex(arg_idx); 606 if (!arg) 607 break; 608 lldb_private::Type *arg_type = 609 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 610 // If there's some error looking up one of the dependent types of this 611 // function signature, bail. 612 if (!arg_type) 613 return nullptr; 614 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 615 arg_list.push_back(arg_ast_type); 616 } 617 lldbassert(arg_list.size() <= num_args); 618 619 auto pdb_return_type = func_sig->getReturnType(); 620 lldb_private::Type *return_type = 621 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 622 // If there's some error looking up one of the dependent types of this 623 // function signature, bail. 624 if (!return_type) 625 return nullptr; 626 CompilerType return_ast_type = return_type->GetFullCompilerType(); 627 uint32_t type_quals = 0; 628 if (func_sig->isConstType()) 629 type_quals |= clang::Qualifiers::Const; 630 if (func_sig->isVolatileType()) 631 type_quals |= clang::Qualifiers::Volatile; 632 auto cc = TranslateCallingConvention(func_sig->getCallingConvention()); 633 CompilerType func_sig_ast_type = 634 m_ast.CreateFunctionType(return_ast_type, arg_list.data(), 635 arg_list.size(), is_variadic, type_quals, cc); 636 637 GetDeclarationForSymbol(type, decl); 638 return std::make_shared<lldb_private::Type>( 639 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, 640 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 641 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 642 } break; 643 case PDB_SymType::ArrayType: { 644 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); 645 assert(array_type); 646 uint32_t num_elements = array_type->getCount(); 647 uint32_t element_uid = array_type->getElementTypeId(); 648 uint32_t bytes = array_type->getLength(); 649 650 // If array rank > 0, PDB gives the element type at N=0. So element type 651 // will parsed in the order N=0, N=1,..., N=rank sequentially. 652 lldb_private::Type *element_type = 653 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 654 if (!element_type) 655 return nullptr; 656 657 CompilerType element_ast_type = element_type->GetForwardCompilerType(); 658 // If element type is UDT, it needs to be complete. 659 if (ClangASTContext::IsCXXClassType(element_ast_type) && 660 !element_ast_type.GetCompleteType()) { 661 if (ClangASTContext::StartTagDeclarationDefinition(element_ast_type)) { 662 ClangASTContext::CompleteTagDeclarationDefinition(element_ast_type); 663 } else { 664 // We are not able to start defintion. 665 return nullptr; 666 } 667 } 668 CompilerType array_ast_type = m_ast.CreateArrayType( 669 element_ast_type, num_elements, /*is_gnu_vector*/ false); 670 TypeSP type_sp = std::make_shared<lldb_private::Type>( 671 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 672 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 673 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 674 type_sp->SetEncodingType(element_type); 675 return type_sp; 676 } break; 677 case PDB_SymType::BuiltinType: { 678 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); 679 assert(builtin_type); 680 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType(); 681 if (builtin_kind == PDB_BuiltinType::None) 682 return nullptr; 683 684 uint64_t bytes = builtin_type->getLength(); 685 Encoding encoding = TranslateBuiltinEncoding(builtin_kind); 686 CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( 687 m_ast, *builtin_type, encoding, bytes * 8); 688 689 if (builtin_type->isConstType()) 690 builtin_ast_type = builtin_ast_type.AddConstModifier(); 691 692 if (builtin_type->isVolatileType()) 693 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 694 695 auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type); 696 697 return std::make_shared<lldb_private::Type>( 698 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, bytes, 699 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 700 builtin_ast_type, lldb_private::Type::eResolveStateFull); 701 } break; 702 case PDB_SymType::PointerType: { 703 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); 704 assert(pointer_type); 705 Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID( 706 pointer_type->getPointeeType()->getSymIndexId()); 707 if (!pointee_type) 708 return nullptr; 709 710 if (pointer_type->isPointerToDataMember() || 711 pointer_type->isPointerToMemberFunction()) { 712 auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId(); 713 auto class_parent_type = 714 m_ast.GetSymbolFile()->ResolveTypeUID(class_parent_uid); 715 assert(class_parent_type); 716 717 CompilerType pointer_ast_type; 718 pointer_ast_type = ClangASTContext::CreateMemberPointerType( 719 class_parent_type->GetLayoutCompilerType(), 720 pointee_type->GetForwardCompilerType()); 721 assert(pointer_ast_type); 722 723 return std::make_shared<lldb_private::Type>( 724 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 725 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 726 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, 727 lldb_private::Type::eResolveStateForward); 728 } 729 730 CompilerType pointer_ast_type; 731 pointer_ast_type = pointee_type->GetFullCompilerType(); 732 if (pointer_type->isReference()) 733 pointer_ast_type = pointer_ast_type.GetLValueReferenceType(); 734 else if (pointer_type->isRValueReference()) 735 pointer_ast_type = pointer_ast_type.GetRValueReferenceType(); 736 else 737 pointer_ast_type = pointer_ast_type.GetPointerType(); 738 739 if (pointer_type->isConstType()) 740 pointer_ast_type = pointer_ast_type.AddConstModifier(); 741 742 if (pointer_type->isVolatileType()) 743 pointer_ast_type = pointer_ast_type.AddVolatileModifier(); 744 745 if (pointer_type->isRestrictedType()) 746 pointer_ast_type = pointer_ast_type.AddRestrictModifier(); 747 748 return std::make_shared<lldb_private::Type>( 749 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 750 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 751 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, 752 lldb_private::Type::eResolveStateFull); 753 } break; 754 default: 755 break; 756 } 757 return nullptr; 758 } 759 760 bool PDBASTParser::CompleteTypeFromPDB( 761 lldb_private::CompilerType &compiler_type) { 762 if (GetClangASTImporter().CanImport(compiler_type)) 763 return GetClangASTImporter().CompleteType(compiler_type); 764 765 // Remove the type from the forward declarations to avoid 766 // an endless recursion for types like a linked list. 767 clang::CXXRecordDecl *record_decl = 768 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType()); 769 auto uid_it = m_forward_decl_to_uid.find(record_decl); 770 if (uid_it == m_forward_decl_to_uid.end()) 771 return true; 772 773 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 774 if (!symbol_file) 775 return false; 776 777 std::unique_ptr<PDBSymbol> symbol = 778 symbol_file->GetPDBSession().getSymbolById(uid_it->getSecond()); 779 if (!symbol) 780 return false; 781 782 m_forward_decl_to_uid.erase(uid_it); 783 784 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(), 785 false); 786 787 switch (symbol->getSymTag()) { 788 case PDB_SymType::UDT: { 789 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(symbol.get()); 790 if (!udt) 791 return false; 792 793 return CompleteTypeFromUDT(*symbol_file, compiler_type, *udt); 794 } 795 default: 796 llvm_unreachable("not a forward clang type decl!"); 797 } 798 } 799 800 clang::Decl * 801 PDBASTParser::GetDeclForSymbol(const llvm::pdb::PDBSymbol &symbol) { 802 uint32_t sym_id = symbol.getSymIndexId(); 803 auto it = m_uid_to_decl.find(sym_id); 804 if (it != m_uid_to_decl.end()) 805 return it->second; 806 807 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 808 if (!symbol_file) 809 return nullptr; 810 811 // First of all, check if the symbol is a member of a class. Resolve the full 812 // class type and return the declaration from the cache if so. 813 auto tag = symbol.getSymTag(); 814 if (tag == PDB_SymType::Data || tag == PDB_SymType::Function) { 815 const IPDBSession &session = symbol.getSession(); 816 const IPDBRawSymbol &raw = symbol.getRawSymbol(); 817 818 auto class_parent_id = raw.getClassParentId(); 819 if (std::unique_ptr<PDBSymbol> class_parent = 820 session.getSymbolById(class_parent_id)) { 821 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_id); 822 if (!class_parent_type) 823 return nullptr; 824 825 CompilerType class_parent_ct = class_parent_type->GetFullCompilerType(); 826 827 // Look a declaration up in the cache after completing the class 828 clang::Decl *decl = m_uid_to_decl.lookup(sym_id); 829 if (decl) 830 return decl; 831 832 // A declaration was not found in the cache. It means that the symbol 833 // has the class parent, but the class doesn't have the symbol in its 834 // children list. 835 if (auto func = llvm::dyn_cast_or_null<PDBSymbolFunc>(&symbol)) { 836 // Try to find a class child method with the same RVA and use its 837 // declaration if found. 838 if (uint32_t rva = func->getRelativeVirtualAddress()) { 839 if (std::unique_ptr<ConcreteSymbolEnumerator<PDBSymbolFunc>> 840 methods_enum = 841 class_parent->findAllChildren<PDBSymbolFunc>()) { 842 while (std::unique_ptr<PDBSymbolFunc> method = 843 methods_enum->getNext()) { 844 if (method->getRelativeVirtualAddress() == rva) { 845 decl = m_uid_to_decl.lookup(method->getSymIndexId()); 846 if (decl) 847 break; 848 } 849 } 850 } 851 } 852 853 // If no class methods with the same RVA were found, then create a new 854 // method. It is possible for template methods. 855 if (!decl) 856 decl = AddRecordMethod(*symbol_file, class_parent_ct, *func); 857 } 858 859 if (decl) 860 m_uid_to_decl[sym_id] = decl; 861 862 return decl; 863 } 864 } 865 866 // If we are here, then the symbol is not belonging to a class and is not 867 // contained in the cache. So create a declaration for it. 868 switch (symbol.getSymTag()) { 869 case PDB_SymType::Data: { 870 auto data = llvm::dyn_cast<PDBSymbolData>(&symbol); 871 assert(data); 872 873 auto decl_context = GetDeclContextContainingSymbol(symbol); 874 assert(decl_context); 875 876 // May be the current context is a class really, but we haven't found 877 // any class parent. This happens e.g. in the case of class static 878 // variables - they has two symbols, one is a child of the class when 879 // another is a child of the exe. So always complete the parent and use 880 // an existing declaration if possible. 881 if (auto parent_decl = llvm::dyn_cast_or_null<clang::TagDecl>(decl_context)) 882 m_ast.GetCompleteDecl(parent_decl); 883 884 std::string name = MSVCUndecoratedNameParser::DropScope(data->getName()); 885 886 // Check if the current context already contains the symbol with the name. 887 clang::Decl *decl = 888 GetDeclFromContextByName(*m_ast.getASTContext(), *decl_context, name); 889 if (!decl) { 890 auto type = symbol_file->ResolveTypeUID(data->getTypeId()); 891 if (!type) 892 return nullptr; 893 894 decl = m_ast.CreateVariableDeclaration( 895 decl_context, name.c_str(), 896 ClangUtil::GetQualType(type->GetLayoutCompilerType())); 897 } 898 899 m_uid_to_decl[sym_id] = decl; 900 901 return decl; 902 } 903 case PDB_SymType::Function: { 904 auto func = llvm::dyn_cast<PDBSymbolFunc>(&symbol); 905 assert(func); 906 907 auto decl_context = GetDeclContextContainingSymbol(symbol); 908 assert(decl_context); 909 910 std::string name = MSVCUndecoratedNameParser::DropScope(func->getName()); 911 912 Type *type = symbol_file->ResolveTypeUID(sym_id); 913 if (!type) 914 return nullptr; 915 916 auto storage = func->isStatic() ? clang::StorageClass::SC_Static 917 : clang::StorageClass::SC_None; 918 919 auto decl = m_ast.CreateFunctionDeclaration( 920 decl_context, name.c_str(), type->GetForwardCompilerType(), storage, 921 func->hasInlineAttribute()); 922 923 std::vector<clang::ParmVarDecl *> params; 924 if (std::unique_ptr<PDBSymbolTypeFunctionSig> sig = func->getSignature()) { 925 if (std::unique_ptr<ConcreteSymbolEnumerator<PDBSymbolTypeFunctionArg>> 926 arg_enum = sig->findAllChildren<PDBSymbolTypeFunctionArg>()) { 927 while (std::unique_ptr<PDBSymbolTypeFunctionArg> arg = 928 arg_enum->getNext()) { 929 Type *arg_type = symbol_file->ResolveTypeUID(arg->getTypeId()); 930 if (!arg_type) 931 continue; 932 933 clang::ParmVarDecl *param = m_ast.CreateParameterDeclaration( 934 decl, nullptr, arg_type->GetForwardCompilerType(), 935 clang::SC_None); 936 if (param) 937 params.push_back(param); 938 } 939 } 940 } 941 if (params.size()) 942 m_ast.SetFunctionParameters(decl, params.data(), params.size()); 943 944 m_uid_to_decl[sym_id] = decl; 945 946 return decl; 947 } 948 default: { 949 // It's not a variable and not a function, check if it's a type 950 Type *type = symbol_file->ResolveTypeUID(sym_id); 951 if (!type) 952 return nullptr; 953 954 return m_uid_to_decl.lookup(sym_id); 955 } 956 } 957 } 958 959 clang::DeclContext * 960 PDBASTParser::GetDeclContextForSymbol(const llvm::pdb::PDBSymbol &symbol) { 961 if (symbol.getSymTag() == PDB_SymType::Function) { 962 clang::DeclContext *result = 963 llvm::dyn_cast_or_null<clang::FunctionDecl>(GetDeclForSymbol(symbol)); 964 965 if (result) 966 m_decl_context_to_uid[result] = symbol.getSymIndexId(); 967 968 return result; 969 } 970 971 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 972 if (!symbol_file) 973 return nullptr; 974 975 auto type = symbol_file->ResolveTypeUID(symbol.getSymIndexId()); 976 if (!type) 977 return nullptr; 978 979 clang::DeclContext *result = 980 m_ast.GetDeclContextForType(type->GetForwardCompilerType()); 981 982 if (result) 983 m_decl_context_to_uid[result] = symbol.getSymIndexId(); 984 985 return result; 986 } 987 988 clang::DeclContext *PDBASTParser::GetDeclContextContainingSymbol( 989 const llvm::pdb::PDBSymbol &symbol) { 990 auto parent = GetClassOrFunctionParent(symbol); 991 while (parent) { 992 if (auto parent_context = GetDeclContextForSymbol(*parent)) 993 return parent_context; 994 995 parent = GetClassOrFunctionParent(*parent); 996 } 997 998 // We can't find any class or function parent of the symbol. So analyze 999 // the full symbol name. The symbol may be belonging to a namespace 1000 // or function (or even to a class if it's e.g. a static variable symbol). 1001 1002 // TODO: Make clang to emit full names for variables in namespaces 1003 // (as MSVC does) 1004 1005 std::string name(symbol.getRawSymbol().getName()); 1006 MSVCUndecoratedNameParser parser(name); 1007 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers(); 1008 if (specs.empty()) 1009 return m_ast.GetTranslationUnitDecl(); 1010 1011 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 1012 if (!symbol_file) 1013 return m_ast.GetTranslationUnitDecl(); 1014 1015 auto global = symbol_file->GetPDBSession().getGlobalScope(); 1016 if (!global) 1017 return m_ast.GetTranslationUnitDecl(); 1018 1019 bool has_type_or_function_parent = false; 1020 clang::DeclContext *curr_context = m_ast.GetTranslationUnitDecl(); 1021 for (std::size_t i = 0; i < specs.size() - 1; i++) { 1022 // Check if there is a function or a type with the current context's name. 1023 if (std::unique_ptr<IPDBEnumSymbols> children_enum = global->findChildren( 1024 PDB_SymType::None, specs[i].GetFullName(), NS_CaseSensitive)) { 1025 while (IPDBEnumChildren<PDBSymbol>::ChildTypePtr child = 1026 children_enum->getNext()) { 1027 if (clang::DeclContext *child_context = 1028 GetDeclContextForSymbol(*child)) { 1029 // Note that `GetDeclContextForSymbol' retrieves 1030 // a declaration context for functions and types only, 1031 // so if we are here then `child_context' is guaranteed 1032 // a function or a type declaration context. 1033 has_type_or_function_parent = true; 1034 curr_context = child_context; 1035 } 1036 } 1037 } 1038 1039 // If there were no functions or types above then retrieve a namespace with 1040 // the current context's name. There can be no namespaces inside a function 1041 // or a type. We check it to avoid fake namespaces such as `__l2': 1042 // `N0::N1::CClass::PrivateFunc::__l2::InnerFuncStruct' 1043 if (!has_type_or_function_parent) { 1044 std::string namespace_name = specs[i].GetBaseName(); 1045 const char *namespace_name_c_str = 1046 IsAnonymousNamespaceName(namespace_name) ? nullptr 1047 : namespace_name.data(); 1048 clang::NamespaceDecl *namespace_decl = 1049 m_ast.GetUniqueNamespaceDeclaration(namespace_name_c_str, 1050 curr_context); 1051 1052 m_parent_to_namespaces[curr_context].insert(namespace_decl); 1053 m_namespaces.insert(namespace_decl); 1054 1055 curr_context = namespace_decl; 1056 } 1057 } 1058 1059 return curr_context; 1060 } 1061 1062 void PDBASTParser::ParseDeclsForDeclContext( 1063 const clang::DeclContext *decl_context) { 1064 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 1065 if (!symbol_file) 1066 return; 1067 1068 IPDBSession &session = symbol_file->GetPDBSession(); 1069 auto symbol_up = 1070 session.getSymbolById(m_decl_context_to_uid.lookup(decl_context)); 1071 auto global_up = session.getGlobalScope(); 1072 1073 PDBSymbol *symbol; 1074 if (symbol_up) 1075 symbol = symbol_up.get(); 1076 else if (global_up) 1077 symbol = global_up.get(); 1078 else 1079 return; 1080 1081 if (auto children = symbol->findAllChildren()) 1082 while (auto child = children->getNext()) 1083 GetDeclForSymbol(*child); 1084 } 1085 1086 clang::NamespaceDecl * 1087 PDBASTParser::FindNamespaceDecl(const clang::DeclContext *parent, 1088 llvm::StringRef name) { 1089 NamespacesSet *set; 1090 if (parent) { 1091 auto pit = m_parent_to_namespaces.find(parent); 1092 if (pit == m_parent_to_namespaces.end()) 1093 return nullptr; 1094 1095 set = &pit->second; 1096 } else { 1097 set = &m_namespaces; 1098 } 1099 assert(set); 1100 1101 for (clang::NamespaceDecl *namespace_decl : *set) 1102 if (namespace_decl->getName().equals(name)) 1103 return namespace_decl; 1104 1105 for (clang::NamespaceDecl *namespace_decl : *set) 1106 if (namespace_decl->isAnonymousNamespace()) 1107 return FindNamespaceDecl(namespace_decl, name); 1108 1109 return nullptr; 1110 } 1111 1112 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 1113 const PDBSymbolData &enum_value) { 1114 Declaration decl; 1115 Variant v = enum_value.getValue(); 1116 std::string name = MSVCUndecoratedNameParser::DropScope(enum_value.getName()); 1117 int64_t raw_value; 1118 switch (v.Type) { 1119 case PDB_VariantType::Int8: 1120 raw_value = v.Value.Int8; 1121 break; 1122 case PDB_VariantType::Int16: 1123 raw_value = v.Value.Int16; 1124 break; 1125 case PDB_VariantType::Int32: 1126 raw_value = v.Value.Int32; 1127 break; 1128 case PDB_VariantType::Int64: 1129 raw_value = v.Value.Int64; 1130 break; 1131 case PDB_VariantType::UInt8: 1132 raw_value = v.Value.UInt8; 1133 break; 1134 case PDB_VariantType::UInt16: 1135 raw_value = v.Value.UInt16; 1136 break; 1137 case PDB_VariantType::UInt32: 1138 raw_value = v.Value.UInt32; 1139 break; 1140 case PDB_VariantType::UInt64: 1141 raw_value = v.Value.UInt64; 1142 break; 1143 default: 1144 return false; 1145 } 1146 CompilerType underlying_type = 1147 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 1148 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 1149 ClangUtil::GetQualType(underlying_type)); 1150 auto enum_constant_decl = m_ast.AddEnumerationValueToEnumerationType( 1151 enum_type, decl, name.c_str(), raw_value, byte_size * 8); 1152 if (!enum_constant_decl) 1153 return false; 1154 1155 m_uid_to_decl[enum_value.getSymIndexId()] = enum_constant_decl; 1156 1157 return true; 1158 } 1159 1160 bool PDBASTParser::CompleteTypeFromUDT( 1161 lldb_private::SymbolFile &symbol_file, 1162 lldb_private::CompilerType &compiler_type, 1163 llvm::pdb::PDBSymbolTypeUDT &udt) { 1164 ClangASTImporter::LayoutInfo layout_info; 1165 layout_info.bit_size = udt.getLength() * 8; 1166 1167 auto nested_enums = udt.findAllChildren<PDBSymbolTypeUDT>(); 1168 if (nested_enums) 1169 while (auto nested = nested_enums->getNext()) 1170 symbol_file.ResolveTypeUID(nested->getSymIndexId()); 1171 1172 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>(); 1173 if (bases_enum) 1174 AddRecordBases(symbol_file, compiler_type, 1175 TranslateUdtKind(udt.getUdtKind()), *bases_enum, 1176 layout_info); 1177 1178 auto members_enum = udt.findAllChildren<PDBSymbolData>(); 1179 if (members_enum) 1180 AddRecordMembers(symbol_file, compiler_type, *members_enum, layout_info); 1181 1182 auto methods_enum = udt.findAllChildren<PDBSymbolFunc>(); 1183 if (methods_enum) 1184 AddRecordMethods(symbol_file, compiler_type, *methods_enum); 1185 1186 m_ast.AddMethodOverridesForCXXRecordType(compiler_type.GetOpaqueQualType()); 1187 ClangASTContext::BuildIndirectFields(compiler_type); 1188 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type); 1189 1190 clang::CXXRecordDecl *record_decl = 1191 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType()); 1192 if (!record_decl) 1193 return static_cast<bool>(compiler_type); 1194 1195 GetClangASTImporter().InsertRecordDecl(record_decl, layout_info); 1196 1197 return static_cast<bool>(compiler_type); 1198 } 1199 1200 void PDBASTParser::AddRecordMembers( 1201 lldb_private::SymbolFile &symbol_file, 1202 lldb_private::CompilerType &record_type, 1203 PDBDataSymbolEnumerator &members_enum, 1204 lldb_private::ClangASTImporter::LayoutInfo &layout_info) { 1205 while (auto member = members_enum.getNext()) { 1206 if (member->isCompilerGenerated()) 1207 continue; 1208 1209 auto member_name = member->getName(); 1210 1211 auto member_type = symbol_file.ResolveTypeUID(member->getTypeId()); 1212 if (!member_type) 1213 continue; 1214 1215 auto member_comp_type = member_type->GetLayoutCompilerType(); 1216 if (!member_comp_type.GetCompleteType()) { 1217 symbol_file.GetObjectFile()->GetModule()->ReportError( 1218 ":: Class '%s' has a member '%s' of type '%s' " 1219 "which does not have a complete definition.", 1220 record_type.GetTypeName().GetCString(), member_name.c_str(), 1221 member_comp_type.GetTypeName().GetCString()); 1222 if (ClangASTContext::StartTagDeclarationDefinition(member_comp_type)) 1223 ClangASTContext::CompleteTagDeclarationDefinition(member_comp_type); 1224 } 1225 1226 auto access = TranslateMemberAccess(member->getAccess()); 1227 1228 switch (member->getDataKind()) { 1229 case PDB_DataKind::Member: { 1230 auto location_type = member->getLocationType(); 1231 1232 auto bit_size = member->getLength(); 1233 if (location_type == PDB_LocType::ThisRel) 1234 bit_size *= 8; 1235 1236 auto decl = ClangASTContext::AddFieldToRecordType( 1237 record_type, member_name.c_str(), member_comp_type, access, bit_size); 1238 if (!decl) 1239 continue; 1240 1241 m_uid_to_decl[member->getSymIndexId()] = decl; 1242 1243 auto offset = member->getOffset() * 8; 1244 if (location_type == PDB_LocType::BitField) 1245 offset += member->getBitPosition(); 1246 1247 layout_info.field_offsets.insert(std::make_pair(decl, offset)); 1248 1249 break; 1250 } 1251 case PDB_DataKind::StaticMember: { 1252 auto decl = ClangASTContext::AddVariableToRecordType( 1253 record_type, member_name.c_str(), member_comp_type, access); 1254 if (!decl) 1255 continue; 1256 1257 m_uid_to_decl[member->getSymIndexId()] = decl; 1258 1259 break; 1260 } 1261 default: 1262 llvm_unreachable("unsupported PDB data kind"); 1263 } 1264 } 1265 } 1266 1267 void PDBASTParser::AddRecordBases( 1268 lldb_private::SymbolFile &symbol_file, 1269 lldb_private::CompilerType &record_type, int record_kind, 1270 PDBBaseClassSymbolEnumerator &bases_enum, 1271 lldb_private::ClangASTImporter::LayoutInfo &layout_info) const { 1272 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> base_classes; 1273 1274 while (auto base = bases_enum.getNext()) { 1275 auto base_type = symbol_file.ResolveTypeUID(base->getTypeId()); 1276 if (!base_type) 1277 continue; 1278 1279 auto base_comp_type = base_type->GetFullCompilerType(); 1280 if (!base_comp_type.GetCompleteType()) { 1281 symbol_file.GetObjectFile()->GetModule()->ReportError( 1282 ":: Class '%s' has a base class '%s' " 1283 "which does not have a complete definition.", 1284 record_type.GetTypeName().GetCString(), 1285 base_comp_type.GetTypeName().GetCString()); 1286 if (ClangASTContext::StartTagDeclarationDefinition(base_comp_type)) 1287 ClangASTContext::CompleteTagDeclarationDefinition(base_comp_type); 1288 } 1289 1290 auto access = TranslateMemberAccess(base->getAccess()); 1291 1292 auto is_virtual = base->isVirtualBaseClass(); 1293 1294 std::unique_ptr<clang::CXXBaseSpecifier> base_spec = 1295 m_ast.CreateBaseClassSpecifier(base_comp_type.GetOpaqueQualType(), 1296 access, is_virtual, 1297 record_kind == clang::TTK_Class); 1298 lldbassert(base_spec); 1299 1300 base_classes.push_back(std::move(base_spec)); 1301 1302 if (is_virtual) 1303 continue; 1304 1305 auto decl = m_ast.GetAsCXXRecordDecl(base_comp_type.GetOpaqueQualType()); 1306 if (!decl) 1307 continue; 1308 1309 auto offset = clang::CharUnits::fromQuantity(base->getOffset()); 1310 layout_info.base_offsets.insert(std::make_pair(decl, offset)); 1311 } 1312 1313 m_ast.TransferBaseClasses(record_type.GetOpaqueQualType(), 1314 std::move(base_classes)); 1315 } 1316 1317 void PDBASTParser::AddRecordMethods(lldb_private::SymbolFile &symbol_file, 1318 lldb_private::CompilerType &record_type, 1319 PDBFuncSymbolEnumerator &methods_enum) { 1320 while (std::unique_ptr<PDBSymbolFunc> method = methods_enum.getNext()) 1321 if (clang::CXXMethodDecl *decl = 1322 AddRecordMethod(symbol_file, record_type, *method)) 1323 m_uid_to_decl[method->getSymIndexId()] = decl; 1324 } 1325 1326 clang::CXXMethodDecl * 1327 PDBASTParser::AddRecordMethod(lldb_private::SymbolFile &symbol_file, 1328 lldb_private::CompilerType &record_type, 1329 const llvm::pdb::PDBSymbolFunc &method) const { 1330 std::string name = MSVCUndecoratedNameParser::DropScope(method.getName()); 1331 1332 Type *method_type = symbol_file.ResolveTypeUID(method.getSymIndexId()); 1333 // MSVC specific __vecDelDtor. 1334 if (!method_type) 1335 return nullptr; 1336 1337 CompilerType method_comp_type = method_type->GetFullCompilerType(); 1338 if (!method_comp_type.GetCompleteType()) { 1339 symbol_file.GetObjectFile()->GetModule()->ReportError( 1340 ":: Class '%s' has a method '%s' whose type cannot be completed.", 1341 record_type.GetTypeName().GetCString(), 1342 method_comp_type.GetTypeName().GetCString()); 1343 if (ClangASTContext::StartTagDeclarationDefinition(method_comp_type)) 1344 ClangASTContext::CompleteTagDeclarationDefinition(method_comp_type); 1345 } 1346 1347 AccessType access = TranslateMemberAccess(method.getAccess()); 1348 if (access == eAccessNone) 1349 access = eAccessPublic; 1350 1351 // TODO: get mangled name for the method. 1352 return m_ast.AddMethodToCXXRecordType( 1353 record_type.GetOpaqueQualType(), name.c_str(), 1354 /*mangled_name*/ nullptr, method_comp_type, access, method.isVirtual(), 1355 method.isStatic(), method.hasInlineAttribute(), 1356 /*is_explicit*/ false, // FIXME: Need this field in CodeView. 1357 /*is_attr_used*/ false, 1358 /*is_artificial*/ method.isCompilerGenerated()); 1359 } 1360