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