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.getValueOr(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 *>(m_ast.GetSymbolFile()); 805 if (!symbol_file) 806 return false; 807 808 std::unique_ptr<PDBSymbol> symbol = 809 symbol_file->GetPDBSession().getSymbolById(uid_it->getSecond()); 810 if (!symbol) 811 return false; 812 813 m_forward_decl_to_uid.erase(uid_it); 814 815 TypeSystemClang::SetHasExternalStorage(compiler_type.GetOpaqueQualType(), 816 false); 817 818 switch (symbol->getSymTag()) { 819 case PDB_SymType::UDT: { 820 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(symbol.get()); 821 if (!udt) 822 return false; 823 824 return CompleteTypeFromUDT(*symbol_file, compiler_type, *udt); 825 } 826 default: 827 llvm_unreachable("not a forward clang type decl!"); 828 } 829 } 830 831 clang::Decl * 832 PDBASTParser::GetDeclForSymbol(const llvm::pdb::PDBSymbol &symbol) { 833 uint32_t sym_id = symbol.getSymIndexId(); 834 auto it = m_uid_to_decl.find(sym_id); 835 if (it != m_uid_to_decl.end()) 836 return it->second; 837 838 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 839 if (!symbol_file) 840 return nullptr; 841 842 // First of all, check if the symbol is a member of a class. Resolve the full 843 // class type and return the declaration from the cache if so. 844 auto tag = symbol.getSymTag(); 845 if (tag == PDB_SymType::Data || tag == PDB_SymType::Function) { 846 const IPDBSession &session = symbol.getSession(); 847 const IPDBRawSymbol &raw = symbol.getRawSymbol(); 848 849 auto class_parent_id = raw.getClassParentId(); 850 if (std::unique_ptr<PDBSymbol> class_parent = 851 session.getSymbolById(class_parent_id)) { 852 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_id); 853 if (!class_parent_type) 854 return nullptr; 855 856 CompilerType class_parent_ct = class_parent_type->GetFullCompilerType(); 857 858 // Look a declaration up in the cache after completing the class 859 clang::Decl *decl = m_uid_to_decl.lookup(sym_id); 860 if (decl) 861 return decl; 862 863 // A declaration was not found in the cache. It means that the symbol 864 // has the class parent, but the class doesn't have the symbol in its 865 // children list. 866 if (auto func = llvm::dyn_cast_or_null<PDBSymbolFunc>(&symbol)) { 867 // Try to find a class child method with the same RVA and use its 868 // declaration if found. 869 if (uint32_t rva = func->getRelativeVirtualAddress()) { 870 if (std::unique_ptr<ConcreteSymbolEnumerator<PDBSymbolFunc>> 871 methods_enum = 872 class_parent->findAllChildren<PDBSymbolFunc>()) { 873 while (std::unique_ptr<PDBSymbolFunc> method = 874 methods_enum->getNext()) { 875 if (method->getRelativeVirtualAddress() == rva) { 876 decl = m_uid_to_decl.lookup(method->getSymIndexId()); 877 if (decl) 878 break; 879 } 880 } 881 } 882 } 883 884 // If no class methods with the same RVA were found, then create a new 885 // method. It is possible for template methods. 886 if (!decl) 887 decl = AddRecordMethod(*symbol_file, class_parent_ct, *func); 888 } 889 890 if (decl) 891 m_uid_to_decl[sym_id] = decl; 892 893 return decl; 894 } 895 } 896 897 // If we are here, then the symbol is not belonging to a class and is not 898 // contained in the cache. So create a declaration for it. 899 switch (symbol.getSymTag()) { 900 case PDB_SymType::Data: { 901 auto data = llvm::dyn_cast<PDBSymbolData>(&symbol); 902 assert(data); 903 904 auto decl_context = GetDeclContextContainingSymbol(symbol); 905 assert(decl_context); 906 907 // May be the current context is a class really, but we haven't found 908 // any class parent. This happens e.g. in the case of class static 909 // variables - they has two symbols, one is a child of the class when 910 // another is a child of the exe. So always complete the parent and use 911 // an existing declaration if possible. 912 if (auto parent_decl = llvm::dyn_cast_or_null<clang::TagDecl>(decl_context)) 913 m_ast.GetCompleteDecl(parent_decl); 914 915 std::string name = 916 std::string(MSVCUndecoratedNameParser::DropScope(data->getName())); 917 918 // Check if the current context already contains the symbol with the name. 919 clang::Decl *decl = 920 GetDeclFromContextByName(m_ast.getASTContext(), *decl_context, name); 921 if (!decl) { 922 auto type = symbol_file->ResolveTypeUID(data->getTypeId()); 923 if (!type) 924 return nullptr; 925 926 decl = m_ast.CreateVariableDeclaration( 927 decl_context, OptionalClangModuleID(), name.c_str(), 928 ClangUtil::GetQualType(type->GetLayoutCompilerType())); 929 } 930 931 m_uid_to_decl[sym_id] = decl; 932 933 return decl; 934 } 935 case PDB_SymType::Function: { 936 auto func = llvm::dyn_cast<PDBSymbolFunc>(&symbol); 937 assert(func); 938 939 auto decl_context = GetDeclContextContainingSymbol(symbol); 940 assert(decl_context); 941 942 std::string name = 943 std::string(MSVCUndecoratedNameParser::DropScope(func->getName())); 944 945 Type *type = symbol_file->ResolveTypeUID(sym_id); 946 if (!type) 947 return nullptr; 948 949 auto storage = func->isStatic() ? clang::StorageClass::SC_Static 950 : clang::StorageClass::SC_None; 951 952 auto decl = m_ast.CreateFunctionDeclaration( 953 decl_context, OptionalClangModuleID(), name, 954 type->GetForwardCompilerType(), storage, func->hasInlineAttribute()); 955 956 std::vector<clang::ParmVarDecl *> params; 957 if (std::unique_ptr<PDBSymbolTypeFunctionSig> sig = func->getSignature()) { 958 if (std::unique_ptr<ConcreteSymbolEnumerator<PDBSymbolTypeFunctionArg>> 959 arg_enum = sig->findAllChildren<PDBSymbolTypeFunctionArg>()) { 960 while (std::unique_ptr<PDBSymbolTypeFunctionArg> arg = 961 arg_enum->getNext()) { 962 Type *arg_type = symbol_file->ResolveTypeUID(arg->getTypeId()); 963 if (!arg_type) 964 continue; 965 966 clang::ParmVarDecl *param = m_ast.CreateParameterDeclaration( 967 decl, OptionalClangModuleID(), nullptr, 968 arg_type->GetForwardCompilerType(), clang::SC_None, true); 969 if (param) 970 params.push_back(param); 971 } 972 } 973 } 974 if (params.size()) 975 m_ast.SetFunctionParameters(decl, params); 976 977 m_uid_to_decl[sym_id] = decl; 978 979 return decl; 980 } 981 default: { 982 // It's not a variable and not a function, check if it's a type 983 Type *type = symbol_file->ResolveTypeUID(sym_id); 984 if (!type) 985 return nullptr; 986 987 return m_uid_to_decl.lookup(sym_id); 988 } 989 } 990 } 991 992 clang::DeclContext * 993 PDBASTParser::GetDeclContextForSymbol(const llvm::pdb::PDBSymbol &symbol) { 994 if (symbol.getSymTag() == PDB_SymType::Function) { 995 clang::DeclContext *result = 996 llvm::dyn_cast_or_null<clang::FunctionDecl>(GetDeclForSymbol(symbol)); 997 998 if (result) 999 m_decl_context_to_uid[result] = symbol.getSymIndexId(); 1000 1001 return result; 1002 } 1003 1004 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 1005 if (!symbol_file) 1006 return nullptr; 1007 1008 auto type = symbol_file->ResolveTypeUID(symbol.getSymIndexId()); 1009 if (!type) 1010 return nullptr; 1011 1012 clang::DeclContext *result = 1013 m_ast.GetDeclContextForType(type->GetForwardCompilerType()); 1014 1015 if (result) 1016 m_decl_context_to_uid[result] = symbol.getSymIndexId(); 1017 1018 return result; 1019 } 1020 1021 clang::DeclContext *PDBASTParser::GetDeclContextContainingSymbol( 1022 const llvm::pdb::PDBSymbol &symbol) { 1023 auto parent = GetClassOrFunctionParent(symbol); 1024 while (parent) { 1025 if (auto parent_context = GetDeclContextForSymbol(*parent)) 1026 return parent_context; 1027 1028 parent = GetClassOrFunctionParent(*parent); 1029 } 1030 1031 // We can't find any class or function parent of the symbol. So analyze 1032 // the full symbol name. The symbol may be belonging to a namespace 1033 // or function (or even to a class if it's e.g. a static variable symbol). 1034 1035 // TODO: Make clang to emit full names for variables in namespaces 1036 // (as MSVC does) 1037 1038 std::string name(symbol.getRawSymbol().getName()); 1039 MSVCUndecoratedNameParser parser(name); 1040 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers(); 1041 if (specs.empty()) 1042 return m_ast.GetTranslationUnitDecl(); 1043 1044 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 1045 if (!symbol_file) 1046 return m_ast.GetTranslationUnitDecl(); 1047 1048 auto global = symbol_file->GetPDBSession().getGlobalScope(); 1049 if (!global) 1050 return m_ast.GetTranslationUnitDecl(); 1051 1052 bool has_type_or_function_parent = false; 1053 clang::DeclContext *curr_context = m_ast.GetTranslationUnitDecl(); 1054 for (std::size_t i = 0; i < specs.size() - 1; i++) { 1055 // Check if there is a function or a type with the current context's name. 1056 if (std::unique_ptr<IPDBEnumSymbols> children_enum = global->findChildren( 1057 PDB_SymType::None, specs[i].GetFullName(), NS_CaseSensitive)) { 1058 while (IPDBEnumChildren<PDBSymbol>::ChildTypePtr child = 1059 children_enum->getNext()) { 1060 if (clang::DeclContext *child_context = 1061 GetDeclContextForSymbol(*child)) { 1062 // Note that `GetDeclContextForSymbol' retrieves 1063 // a declaration context for functions and types only, 1064 // so if we are here then `child_context' is guaranteed 1065 // a function or a type declaration context. 1066 has_type_or_function_parent = true; 1067 curr_context = child_context; 1068 } 1069 } 1070 } 1071 1072 // If there were no functions or types above then retrieve a namespace with 1073 // the current context's name. There can be no namespaces inside a function 1074 // or a type. We check it to avoid fake namespaces such as `__l2': 1075 // `N0::N1::CClass::PrivateFunc::__l2::InnerFuncStruct' 1076 if (!has_type_or_function_parent) { 1077 std::string namespace_name = std::string(specs[i].GetBaseName()); 1078 const char *namespace_name_c_str = 1079 IsAnonymousNamespaceName(namespace_name) ? nullptr 1080 : namespace_name.data(); 1081 clang::NamespaceDecl *namespace_decl = 1082 m_ast.GetUniqueNamespaceDeclaration( 1083 namespace_name_c_str, curr_context, OptionalClangModuleID()); 1084 1085 m_parent_to_namespaces[curr_context].insert(namespace_decl); 1086 m_namespaces.insert(namespace_decl); 1087 1088 curr_context = namespace_decl; 1089 } 1090 } 1091 1092 return curr_context; 1093 } 1094 1095 void PDBASTParser::ParseDeclsForDeclContext( 1096 const clang::DeclContext *decl_context) { 1097 auto symbol_file = static_cast<SymbolFilePDB *>(m_ast.GetSymbolFile()); 1098 if (!symbol_file) 1099 return; 1100 1101 IPDBSession &session = symbol_file->GetPDBSession(); 1102 auto symbol_up = 1103 session.getSymbolById(m_decl_context_to_uid.lookup(decl_context)); 1104 auto global_up = session.getGlobalScope(); 1105 1106 PDBSymbol *symbol; 1107 if (symbol_up) 1108 symbol = symbol_up.get(); 1109 else if (global_up) 1110 symbol = global_up.get(); 1111 else 1112 return; 1113 1114 if (auto children = symbol->findAllChildren()) 1115 while (auto child = children->getNext()) 1116 GetDeclForSymbol(*child); 1117 } 1118 1119 clang::NamespaceDecl * 1120 PDBASTParser::FindNamespaceDecl(const clang::DeclContext *parent, 1121 llvm::StringRef name) { 1122 NamespacesSet *set; 1123 if (parent) { 1124 auto pit = m_parent_to_namespaces.find(parent); 1125 if (pit == m_parent_to_namespaces.end()) 1126 return nullptr; 1127 1128 set = &pit->second; 1129 } else { 1130 set = &m_namespaces; 1131 } 1132 assert(set); 1133 1134 for (clang::NamespaceDecl *namespace_decl : *set) 1135 if (namespace_decl->getName().equals(name)) 1136 return namespace_decl; 1137 1138 for (clang::NamespaceDecl *namespace_decl : *set) 1139 if (namespace_decl->isAnonymousNamespace()) 1140 return FindNamespaceDecl(namespace_decl, name); 1141 1142 return nullptr; 1143 } 1144 1145 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 1146 const PDBSymbolData &enum_value) { 1147 Declaration decl; 1148 Variant v = enum_value.getValue(); 1149 std::string name = 1150 std::string(MSVCUndecoratedNameParser::DropScope(enum_value.getName())); 1151 int64_t raw_value; 1152 switch (v.Type) { 1153 case PDB_VariantType::Int8: 1154 raw_value = v.Value.Int8; 1155 break; 1156 case PDB_VariantType::Int16: 1157 raw_value = v.Value.Int16; 1158 break; 1159 case PDB_VariantType::Int32: 1160 raw_value = v.Value.Int32; 1161 break; 1162 case PDB_VariantType::Int64: 1163 raw_value = v.Value.Int64; 1164 break; 1165 case PDB_VariantType::UInt8: 1166 raw_value = v.Value.UInt8; 1167 break; 1168 case PDB_VariantType::UInt16: 1169 raw_value = v.Value.UInt16; 1170 break; 1171 case PDB_VariantType::UInt32: 1172 raw_value = v.Value.UInt32; 1173 break; 1174 case PDB_VariantType::UInt64: 1175 raw_value = v.Value.UInt64; 1176 break; 1177 default: 1178 return false; 1179 } 1180 CompilerType underlying_type = m_ast.GetEnumerationIntegerType(enum_type); 1181 uint32_t byte_size = m_ast.getASTContext().getTypeSize( 1182 ClangUtil::GetQualType(underlying_type)); 1183 auto enum_constant_decl = m_ast.AddEnumerationValueToEnumerationType( 1184 enum_type, decl, name.c_str(), raw_value, byte_size * 8); 1185 if (!enum_constant_decl) 1186 return false; 1187 1188 m_uid_to_decl[enum_value.getSymIndexId()] = enum_constant_decl; 1189 1190 return true; 1191 } 1192 1193 bool PDBASTParser::CompleteTypeFromUDT( 1194 lldb_private::SymbolFile &symbol_file, 1195 lldb_private::CompilerType &compiler_type, 1196 llvm::pdb::PDBSymbolTypeUDT &udt) { 1197 ClangASTImporter::LayoutInfo layout_info; 1198 layout_info.bit_size = udt.getLength() * 8; 1199 1200 auto nested_enums = udt.findAllChildren<PDBSymbolTypeUDT>(); 1201 if (nested_enums) 1202 while (auto nested = nested_enums->getNext()) 1203 symbol_file.ResolveTypeUID(nested->getSymIndexId()); 1204 1205 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>(); 1206 if (bases_enum) 1207 AddRecordBases(symbol_file, compiler_type, 1208 TranslateUdtKind(udt.getUdtKind()), *bases_enum, 1209 layout_info); 1210 1211 auto members_enum = udt.findAllChildren<PDBSymbolData>(); 1212 if (members_enum) 1213 AddRecordMembers(symbol_file, compiler_type, *members_enum, layout_info); 1214 1215 auto methods_enum = udt.findAllChildren<PDBSymbolFunc>(); 1216 if (methods_enum) 1217 AddRecordMethods(symbol_file, compiler_type, *methods_enum); 1218 1219 m_ast.AddMethodOverridesForCXXRecordType(compiler_type.GetOpaqueQualType()); 1220 TypeSystemClang::BuildIndirectFields(compiler_type); 1221 TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type); 1222 1223 clang::CXXRecordDecl *record_decl = 1224 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType()); 1225 if (!record_decl) 1226 return static_cast<bool>(compiler_type); 1227 1228 GetClangASTImporter().SetRecordLayout(record_decl, layout_info); 1229 1230 return static_cast<bool>(compiler_type); 1231 } 1232 1233 void PDBASTParser::AddRecordMembers( 1234 lldb_private::SymbolFile &symbol_file, 1235 lldb_private::CompilerType &record_type, 1236 PDBDataSymbolEnumerator &members_enum, 1237 lldb_private::ClangASTImporter::LayoutInfo &layout_info) { 1238 while (auto member = members_enum.getNext()) { 1239 if (member->isCompilerGenerated()) 1240 continue; 1241 1242 auto member_name = member->getName(); 1243 1244 auto member_type = symbol_file.ResolveTypeUID(member->getTypeId()); 1245 if (!member_type) 1246 continue; 1247 1248 auto member_comp_type = member_type->GetLayoutCompilerType(); 1249 if (!member_comp_type.GetCompleteType()) { 1250 symbol_file.GetObjectFile()->GetModule()->ReportError( 1251 ":: Class '%s' has a member '%s' of type '%s' " 1252 "which does not have a complete definition.", 1253 record_type.GetTypeName().GetCString(), member_name.c_str(), 1254 member_comp_type.GetTypeName().GetCString()); 1255 if (TypeSystemClang::StartTagDeclarationDefinition(member_comp_type)) 1256 TypeSystemClang::CompleteTagDeclarationDefinition(member_comp_type); 1257 } 1258 1259 auto access = TranslateMemberAccess(member->getAccess()); 1260 1261 switch (member->getDataKind()) { 1262 case PDB_DataKind::Member: { 1263 auto location_type = member->getLocationType(); 1264 1265 auto bit_size = member->getLength(); 1266 if (location_type == PDB_LocType::ThisRel) 1267 bit_size *= 8; 1268 1269 auto decl = TypeSystemClang::AddFieldToRecordType( 1270 record_type, member_name.c_str(), member_comp_type, access, bit_size); 1271 if (!decl) 1272 continue; 1273 1274 m_uid_to_decl[member->getSymIndexId()] = decl; 1275 1276 auto offset = member->getOffset() * 8; 1277 if (location_type == PDB_LocType::BitField) 1278 offset += member->getBitPosition(); 1279 1280 layout_info.field_offsets.insert(std::make_pair(decl, offset)); 1281 1282 break; 1283 } 1284 case PDB_DataKind::StaticMember: { 1285 auto decl = TypeSystemClang::AddVariableToRecordType( 1286 record_type, member_name.c_str(), member_comp_type, access); 1287 if (!decl) 1288 continue; 1289 1290 // Static constant members may be a const[expr] declaration. 1291 // Query the symbol's value as the variable initializer if valid. 1292 if (member_comp_type.IsConst()) { 1293 auto value = member->getValue(); 1294 clang::QualType qual_type = decl->getType(); 1295 unsigned type_width = m_ast.getASTContext().getIntWidth(qual_type); 1296 unsigned constant_width = value.getBitWidth(); 1297 1298 if (qual_type->isIntegralOrEnumerationType()) { 1299 if (type_width >= constant_width) { 1300 TypeSystemClang::SetIntegerInitializerForVariable( 1301 decl, value.toAPSInt().extOrTrunc(type_width)); 1302 } else { 1303 LLDB_LOG(GetLog(LLDBLog::AST), 1304 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) " 1305 "which resolves to a wider constant value ({4} bits). " 1306 "Ignoring constant.", 1307 record_type.GetTypeName(), member_name, 1308 member_comp_type.GetTypeName(), type_width, 1309 constant_width); 1310 } 1311 } else { 1312 switch (member_comp_type.GetBasicTypeEnumeration()) { 1313 case lldb::eBasicTypeFloat: 1314 case lldb::eBasicTypeDouble: 1315 case lldb::eBasicTypeLongDouble: 1316 if (type_width == constant_width) { 1317 TypeSystemClang::SetFloatingInitializerForVariable( 1318 decl, value.toAPFloat()); 1319 decl->setConstexpr(true); 1320 } else { 1321 LLDB_LOG(GetLog(LLDBLog::AST), 1322 "Class '{0}' has a member '{1}' of type '{2}' ({3} " 1323 "bits) which resolves to a constant value of mismatched " 1324 "width ({4} bits). Ignoring constant.", 1325 record_type.GetTypeName(), member_name, 1326 member_comp_type.GetTypeName(), type_width, 1327 constant_width); 1328 } 1329 break; 1330 default: 1331 break; 1332 } 1333 } 1334 } 1335 1336 m_uid_to_decl[member->getSymIndexId()] = decl; 1337 1338 break; 1339 } 1340 default: 1341 llvm_unreachable("unsupported PDB data kind"); 1342 } 1343 } 1344 } 1345 1346 void PDBASTParser::AddRecordBases( 1347 lldb_private::SymbolFile &symbol_file, 1348 lldb_private::CompilerType &record_type, int record_kind, 1349 PDBBaseClassSymbolEnumerator &bases_enum, 1350 lldb_private::ClangASTImporter::LayoutInfo &layout_info) const { 1351 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> base_classes; 1352 1353 while (auto base = bases_enum.getNext()) { 1354 auto base_type = symbol_file.ResolveTypeUID(base->getTypeId()); 1355 if (!base_type) 1356 continue; 1357 1358 auto base_comp_type = base_type->GetFullCompilerType(); 1359 if (!base_comp_type.GetCompleteType()) { 1360 symbol_file.GetObjectFile()->GetModule()->ReportError( 1361 ":: Class '%s' has a base class '%s' " 1362 "which does not have a complete definition.", 1363 record_type.GetTypeName().GetCString(), 1364 base_comp_type.GetTypeName().GetCString()); 1365 if (TypeSystemClang::StartTagDeclarationDefinition(base_comp_type)) 1366 TypeSystemClang::CompleteTagDeclarationDefinition(base_comp_type); 1367 } 1368 1369 auto access = TranslateMemberAccess(base->getAccess()); 1370 1371 auto is_virtual = base->isVirtualBaseClass(); 1372 1373 std::unique_ptr<clang::CXXBaseSpecifier> base_spec = 1374 m_ast.CreateBaseClassSpecifier(base_comp_type.GetOpaqueQualType(), 1375 access, is_virtual, 1376 record_kind == clang::TTK_Class); 1377 lldbassert(base_spec); 1378 1379 base_classes.push_back(std::move(base_spec)); 1380 1381 if (is_virtual) 1382 continue; 1383 1384 auto decl = m_ast.GetAsCXXRecordDecl(base_comp_type.GetOpaqueQualType()); 1385 if (!decl) 1386 continue; 1387 1388 auto offset = clang::CharUnits::fromQuantity(base->getOffset()); 1389 layout_info.base_offsets.insert(std::make_pair(decl, offset)); 1390 } 1391 1392 m_ast.TransferBaseClasses(record_type.GetOpaqueQualType(), 1393 std::move(base_classes)); 1394 } 1395 1396 void PDBASTParser::AddRecordMethods(lldb_private::SymbolFile &symbol_file, 1397 lldb_private::CompilerType &record_type, 1398 PDBFuncSymbolEnumerator &methods_enum) { 1399 while (std::unique_ptr<PDBSymbolFunc> method = methods_enum.getNext()) 1400 if (clang::CXXMethodDecl *decl = 1401 AddRecordMethod(symbol_file, record_type, *method)) 1402 m_uid_to_decl[method->getSymIndexId()] = decl; 1403 } 1404 1405 clang::CXXMethodDecl * 1406 PDBASTParser::AddRecordMethod(lldb_private::SymbolFile &symbol_file, 1407 lldb_private::CompilerType &record_type, 1408 const llvm::pdb::PDBSymbolFunc &method) const { 1409 std::string name = 1410 std::string(MSVCUndecoratedNameParser::DropScope(method.getName())); 1411 1412 Type *method_type = symbol_file.ResolveTypeUID(method.getSymIndexId()); 1413 // MSVC specific __vecDelDtor. 1414 if (!method_type) 1415 return nullptr; 1416 1417 CompilerType method_comp_type = method_type->GetFullCompilerType(); 1418 if (!method_comp_type.GetCompleteType()) { 1419 symbol_file.GetObjectFile()->GetModule()->ReportError( 1420 ":: Class '%s' has a method '%s' whose type cannot be completed.", 1421 record_type.GetTypeName().GetCString(), 1422 method_comp_type.GetTypeName().GetCString()); 1423 if (TypeSystemClang::StartTagDeclarationDefinition(method_comp_type)) 1424 TypeSystemClang::CompleteTagDeclarationDefinition(method_comp_type); 1425 } 1426 1427 AccessType access = TranslateMemberAccess(method.getAccess()); 1428 if (access == eAccessNone) 1429 access = eAccessPublic; 1430 1431 // TODO: get mangled name for the method. 1432 return m_ast.AddMethodToCXXRecordType( 1433 record_type.GetOpaqueQualType(), name.c_str(), 1434 /*mangled_name*/ nullptr, method_comp_type, access, method.isVirtual(), 1435 method.isStatic(), method.hasInlineAttribute(), 1436 /*is_explicit*/ false, // FIXME: Need this field in CodeView. 1437 /*is_attr_used*/ false, 1438 /*is_artificial*/ method.isCompilerGenerated()); 1439 } 1440