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