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