1 #include "PdbAstBuilder.h" 2 3 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 4 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 5 #include "llvm/DebugInfo/CodeView/RecordName.h" 6 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 7 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 8 #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" 9 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 10 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" 11 #include "llvm/DebugInfo/PDB/Native/DbiStream.h" 12 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" 13 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" 14 #include "llvm/DebugInfo/PDB/Native/TpiStream.h" 15 #include "llvm/Demangle/MicrosoftDemangle.h" 16 17 #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h" 18 #include "Plugins/ExpressionParser/Clang/ClangUtil.h" 19 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" 20 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Symbol/ObjectFile.h" 23 #include "lldb/Utility/LLDBAssert.h" 24 25 #include "PdbUtil.h" 26 #include "UdtRecordCompleter.h" 27 28 using namespace lldb_private; 29 using namespace lldb_private::npdb; 30 using namespace llvm::codeview; 31 using namespace llvm::pdb; 32 33 namespace { 34 struct CreateMethodDecl : public TypeVisitorCallbacks { 35 CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang, 36 TypeIndex func_type_index, 37 clang::FunctionDecl *&function_decl, 38 lldb::opaque_compiler_type_t parent_ty, 39 llvm::StringRef proc_name, CompilerType func_ct) 40 : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index), 41 function_decl(function_decl), parent_ty(parent_ty), 42 proc_name(proc_name), func_ct(func_ct) {} 43 PdbIndex &m_index; 44 TypeSystemClang &m_clang; 45 TypeIndex func_type_index; 46 clang::FunctionDecl *&function_decl; 47 lldb::opaque_compiler_type_t parent_ty; 48 llvm::StringRef proc_name; 49 CompilerType func_ct; 50 51 llvm::Error visitKnownMember(CVMemberRecord &cvr, 52 OverloadedMethodRecord &overloaded) override { 53 TypeIndex method_list_idx = overloaded.MethodList; 54 55 CVType method_list_type = m_index.tpi().getType(method_list_idx); 56 assert(method_list_type.kind() == LF_METHODLIST); 57 58 MethodOverloadListRecord method_list; 59 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>( 60 method_list_type, method_list)); 61 62 for (const OneMethodRecord &method : method_list.Methods) { 63 if (method.getType().getIndex() == func_type_index.getIndex()) 64 AddMethod(overloaded.Name, method.getAccess(), method.getOptions(), 65 method.Attrs); 66 } 67 68 return llvm::Error::success(); 69 } 70 71 llvm::Error visitKnownMember(CVMemberRecord &cvr, 72 OneMethodRecord &record) override { 73 AddMethod(record.getName(), record.getAccess(), record.getOptions(), 74 record.Attrs); 75 return llvm::Error::success(); 76 } 77 78 void AddMethod(llvm::StringRef name, MemberAccess access, 79 MethodOptions options, MemberAttributes attrs) { 80 if (name != proc_name || function_decl) 81 return; 82 lldb::AccessType access_type = TranslateMemberAccess(access); 83 bool is_virtual = attrs.isVirtual(); 84 bool is_static = attrs.isStatic(); 85 bool is_artificial = (options & MethodOptions::CompilerGenerated) == 86 MethodOptions::CompilerGenerated; 87 function_decl = m_clang.AddMethodToCXXRecordType( 88 parent_ty, proc_name, 89 /*mangled_name=*/nullptr, func_ct, /*access=*/access_type, 90 /*is_virtual=*/is_virtual, /*is_static=*/is_static, 91 /*is_inline=*/false, /*is_explicit=*/false, 92 /*is_attr_used=*/false, /*is_artificial=*/is_artificial); 93 } 94 }; 95 } // namespace 96 97 static llvm::Optional<PdbCompilandSymId> FindSymbolScope(PdbIndex &index, 98 PdbCompilandSymId id) { 99 CVSymbol sym = index.ReadSymbolRecord(id); 100 if (symbolOpensScope(sym.kind())) { 101 // If this exact symbol opens a scope, we can just directly access its 102 // parent. 103 id.offset = getScopeParentOffset(sym); 104 // Global symbols have parent offset of 0. Return llvm::None to indicate 105 // this. 106 if (id.offset == 0) 107 return llvm::None; 108 return id; 109 } 110 111 // Otherwise we need to start at the beginning and iterate forward until we 112 // reach (or pass) this particular symbol 113 CompilandIndexItem &cii = index.compilands().GetOrCreateCompiland(id.modi); 114 const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray(); 115 116 auto begin = syms.begin(); 117 auto end = syms.at(id.offset); 118 std::vector<PdbCompilandSymId> scope_stack; 119 120 while (begin != end) { 121 if (begin.offset() > id.offset) { 122 // We passed it. We couldn't even find this symbol record. 123 lldbassert(false && "Invalid compiland symbol id!"); 124 return llvm::None; 125 } 126 127 // We haven't found the symbol yet. Check if we need to open or close the 128 // scope stack. 129 if (symbolOpensScope(begin->kind())) { 130 // We can use the end offset of the scope to determine whether or not 131 // we can just outright skip this entire scope. 132 uint32_t scope_end = getScopeEndOffset(*begin); 133 if (scope_end < id.offset) { 134 begin = syms.at(scope_end); 135 } else { 136 // The symbol we're looking for is somewhere in this scope. 137 scope_stack.emplace_back(id.modi, begin.offset()); 138 } 139 } else if (symbolEndsScope(begin->kind())) { 140 scope_stack.pop_back(); 141 } 142 ++begin; 143 } 144 if (scope_stack.empty()) 145 return llvm::None; 146 // We have a match! Return the top of the stack 147 return scope_stack.back(); 148 } 149 150 static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) { 151 switch (cr.Kind) { 152 case TypeRecordKind::Class: 153 return clang::TTK_Class; 154 case TypeRecordKind::Struct: 155 return clang::TTK_Struct; 156 case TypeRecordKind::Union: 157 return clang::TTK_Union; 158 case TypeRecordKind::Interface: 159 return clang::TTK_Interface; 160 case TypeRecordKind::Enum: 161 return clang::TTK_Enum; 162 default: 163 lldbassert(false && "Invalid tag record kind!"); 164 return clang::TTK_Struct; 165 } 166 } 167 168 static bool IsCVarArgsFunction(llvm::ArrayRef<TypeIndex> args) { 169 if (args.empty()) 170 return false; 171 return args.back() == TypeIndex::None(); 172 } 173 174 static bool 175 AnyScopesHaveTemplateParams(llvm::ArrayRef<llvm::ms_demangle::Node *> scopes) { 176 for (llvm::ms_demangle::Node *n : scopes) { 177 auto *idn = static_cast<llvm::ms_demangle::IdentifierNode *>(n); 178 if (idn->TemplateParams) 179 return true; 180 } 181 return false; 182 } 183 184 static llvm::Optional<clang::CallingConv> 185 TranslateCallingConvention(llvm::codeview::CallingConvention conv) { 186 using CC = llvm::codeview::CallingConvention; 187 switch (conv) { 188 189 case CC::NearC: 190 case CC::FarC: 191 return clang::CallingConv::CC_C; 192 case CC::NearPascal: 193 case CC::FarPascal: 194 return clang::CallingConv::CC_X86Pascal; 195 case CC::NearFast: 196 case CC::FarFast: 197 return clang::CallingConv::CC_X86FastCall; 198 case CC::NearStdCall: 199 case CC::FarStdCall: 200 return clang::CallingConv::CC_X86StdCall; 201 case CC::ThisCall: 202 return clang::CallingConv::CC_X86ThisCall; 203 case CC::NearVector: 204 return clang::CallingConv::CC_X86VectorCall; 205 default: 206 return llvm::None; 207 } 208 } 209 210 static llvm::Optional<CVTagRecord> 211 GetNestedTagDefinition(const NestedTypeRecord &Record, 212 const CVTagRecord &parent, TpiStream &tpi) { 213 // An LF_NESTTYPE is essentially a nested typedef / using declaration, but it 214 // is also used to indicate the primary definition of a nested class. That is 215 // to say, if you have: 216 // struct A { 217 // struct B {}; 218 // using C = B; 219 // }; 220 // Then in the debug info, this will appear as: 221 // LF_STRUCTURE `A::B` [type index = N] 222 // LF_STRUCTURE `A` 223 // LF_NESTTYPE [name = `B`, index = N] 224 // LF_NESTTYPE [name = `C`, index = N] 225 // In order to accurately reconstruct the decl context hierarchy, we need to 226 // know which ones are actual definitions and which ones are just aliases. 227 228 // If it's a simple type, then this is something like `using foo = int`. 229 if (Record.Type.isSimple()) 230 return llvm::None; 231 232 CVType cvt = tpi.getType(Record.Type); 233 234 if (!IsTagRecord(cvt)) 235 return llvm::None; 236 237 // If it's an inner definition, then treat whatever name we have here as a 238 // single component of a mangled name. So we can inject it into the parent's 239 // mangled name to see if it matches. 240 CVTagRecord child = CVTagRecord::create(cvt); 241 std::string qname = std::string(parent.asTag().getUniqueName()); 242 if (qname.size() < 4 || child.asTag().getUniqueName().size() < 4) 243 return llvm::None; 244 245 // qname[3] is the tag type identifier (struct, class, union, etc). Since the 246 // inner tag type is not necessarily the same as the outer tag type, re-write 247 // it to match the inner tag type. 248 qname[3] = child.asTag().getUniqueName()[3]; 249 std::string piece; 250 if (qname[3] == 'W') 251 piece = "4"; 252 piece += Record.Name; 253 piece.push_back('@'); 254 qname.insert(4, std::move(piece)); 255 if (qname != child.asTag().UniqueName) 256 return llvm::None; 257 258 return std::move(child); 259 } 260 261 static bool IsAnonymousNamespaceName(llvm::StringRef name) { 262 return name == "`anonymous namespace'" || name == "`anonymous-namespace'"; 263 } 264 265 PdbAstBuilder::PdbAstBuilder(ObjectFile &obj, PdbIndex &index, TypeSystemClang &clang) 266 : m_index(index), m_clang(clang) { 267 BuildParentMap(); 268 } 269 270 lldb_private::CompilerDeclContext PdbAstBuilder::GetTranslationUnitDecl() { 271 return ToCompilerDeclContext(*m_clang.GetTranslationUnitDecl()); 272 } 273 274 std::pair<clang::DeclContext *, std::string> 275 PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) { 276 // FIXME: Move this to GetDeclContextContainingUID. 277 if (!record.hasUniqueName()) 278 return CreateDeclInfoForUndecoratedName(record.Name); 279 280 llvm::ms_demangle::Demangler demangler; 281 StringView sv(record.UniqueName.begin(), record.UniqueName.size()); 282 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv); 283 if (demangler.Error) 284 return {m_clang.GetTranslationUnitDecl(), std::string(record.UniqueName)}; 285 286 llvm::ms_demangle::IdentifierNode *idn = 287 ttn->QualifiedName->getUnqualifiedIdentifier(); 288 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier); 289 290 llvm::ms_demangle::NodeArrayNode *name_components = 291 ttn->QualifiedName->Components; 292 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes, 293 name_components->Count - 1); 294 295 clang::DeclContext *context = m_clang.GetTranslationUnitDecl(); 296 297 // If this type doesn't have a parent type in the debug info, then the best we 298 // can do is to say that it's either a series of namespaces (if the scope is 299 // non-empty), or the translation unit (if the scope is empty). 300 auto parent_iter = m_parent_types.find(ti); 301 if (parent_iter == m_parent_types.end()) { 302 if (scopes.empty()) 303 return {context, uname}; 304 305 // If there is no parent in the debug info, but some of the scopes have 306 // template params, then this is a case of bad debug info. See, for 307 // example, llvm.org/pr39607. We don't want to create an ambiguity between 308 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at 309 // global scope with the fully qualified name. 310 if (AnyScopesHaveTemplateParams(scopes)) 311 return {context, std::string(record.Name)}; 312 313 for (llvm::ms_demangle::Node *scope : scopes) { 314 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope); 315 std::string str = nii->toString(); 316 context = GetOrCreateNamespaceDecl(str.c_str(), *context); 317 } 318 return {context, uname}; 319 } 320 321 // Otherwise, all we need to do is get the parent type of this type and 322 // recurse into our lazy type creation / AST reconstruction logic to get an 323 // LLDB TypeSP for the parent. This will cause the AST to automatically get 324 // the right DeclContext created for any parent. 325 clang::QualType parent_qt = GetOrCreateType(parent_iter->second); 326 327 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl()); 328 return {context, uname}; 329 } 330 331 void PdbAstBuilder::BuildParentMap() { 332 LazyRandomTypeCollection &types = m_index.tpi().typeCollection(); 333 334 llvm::DenseMap<TypeIndex, TypeIndex> forward_to_full; 335 llvm::DenseMap<TypeIndex, TypeIndex> full_to_forward; 336 337 struct RecordIndices { 338 TypeIndex forward; 339 TypeIndex full; 340 }; 341 342 llvm::StringMap<RecordIndices> record_indices; 343 344 for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) { 345 CVType type = types.getType(*ti); 346 if (!IsTagRecord(type)) 347 continue; 348 349 CVTagRecord tag = CVTagRecord::create(type); 350 351 RecordIndices &indices = record_indices[tag.asTag().getUniqueName()]; 352 if (tag.asTag().isForwardRef()) 353 indices.forward = *ti; 354 else 355 indices.full = *ti; 356 357 if (indices.full != TypeIndex::None() && 358 indices.forward != TypeIndex::None()) { 359 forward_to_full[indices.forward] = indices.full; 360 full_to_forward[indices.full] = indices.forward; 361 } 362 363 // We're looking for LF_NESTTYPE records in the field list, so ignore 364 // forward references (no field list), and anything without a nested class 365 // (since there won't be any LF_NESTTYPE records). 366 if (tag.asTag().isForwardRef() || !tag.asTag().containsNestedClass()) 367 continue; 368 369 struct ProcessTpiStream : public TypeVisitorCallbacks { 370 ProcessTpiStream(PdbIndex &index, TypeIndex parent, 371 const CVTagRecord &parent_cvt, 372 llvm::DenseMap<TypeIndex, TypeIndex> &parents) 373 : index(index), parents(parents), parent(parent), 374 parent_cvt(parent_cvt) {} 375 376 PdbIndex &index; 377 llvm::DenseMap<TypeIndex, TypeIndex> &parents; 378 379 unsigned unnamed_type_index = 1; 380 TypeIndex parent; 381 const CVTagRecord &parent_cvt; 382 383 llvm::Error visitKnownMember(CVMemberRecord &CVR, 384 NestedTypeRecord &Record) override { 385 std::string unnamed_type_name; 386 if (Record.Name.empty()) { 387 unnamed_type_name = 388 llvm::formatv("<unnamed-type-$S{0}>", unnamed_type_index).str(); 389 Record.Name = unnamed_type_name; 390 ++unnamed_type_index; 391 } 392 llvm::Optional<CVTagRecord> tag = 393 GetNestedTagDefinition(Record, parent_cvt, index.tpi()); 394 if (!tag) 395 return llvm::ErrorSuccess(); 396 397 parents[Record.Type] = parent; 398 return llvm::ErrorSuccess(); 399 } 400 }; 401 402 CVType field_list = m_index.tpi().getType(tag.asTag().FieldList); 403 ProcessTpiStream process(m_index, *ti, tag, m_parent_types); 404 llvm::Error error = visitMemberRecordStream(field_list.data(), process); 405 if (error) 406 llvm::consumeError(std::move(error)); 407 } 408 409 // Now that we know the forward -> full mapping of all type indices, we can 410 // re-write all the indices. At the end of this process, we want a mapping 411 // consisting of fwd -> full and full -> full for all child -> parent indices. 412 // We can re-write the values in place, but for the keys, we must save them 413 // off so that we don't modify the map in place while also iterating it. 414 std::vector<TypeIndex> full_keys; 415 std::vector<TypeIndex> fwd_keys; 416 for (auto &entry : m_parent_types) { 417 TypeIndex key = entry.first; 418 TypeIndex value = entry.second; 419 420 auto iter = forward_to_full.find(value); 421 if (iter != forward_to_full.end()) 422 entry.second = iter->second; 423 424 iter = forward_to_full.find(key); 425 if (iter != forward_to_full.end()) 426 fwd_keys.push_back(key); 427 else 428 full_keys.push_back(key); 429 } 430 for (TypeIndex fwd : fwd_keys) { 431 TypeIndex full = forward_to_full[fwd]; 432 m_parent_types[full] = m_parent_types[fwd]; 433 } 434 for (TypeIndex full : full_keys) { 435 TypeIndex fwd = full_to_forward[full]; 436 m_parent_types[fwd] = m_parent_types[full]; 437 } 438 439 // Now that 440 } 441 442 static bool isLocalVariableType(SymbolKind K) { 443 switch (K) { 444 case S_REGISTER: 445 case S_REGREL32: 446 case S_LOCAL: 447 return true; 448 default: 449 break; 450 } 451 return false; 452 } 453 454 static std::string 455 RenderScopeList(llvm::ArrayRef<llvm::ms_demangle::Node *> nodes) { 456 lldbassert(!nodes.empty()); 457 458 std::string result = nodes.front()->toString(); 459 nodes = nodes.drop_front(); 460 while (!nodes.empty()) { 461 result += "::"; 462 result += nodes.front()->toString(llvm::ms_demangle::OF_NoTagSpecifier); 463 nodes = nodes.drop_front(); 464 } 465 return result; 466 } 467 468 static llvm::Optional<PublicSym32> FindPublicSym(const SegmentOffset &addr, 469 SymbolStream &syms, 470 PublicsStream &publics) { 471 llvm::FixedStreamArray<ulittle32_t> addr_map = publics.getAddressMap(); 472 auto iter = std::lower_bound( 473 addr_map.begin(), addr_map.end(), addr, 474 [&](const ulittle32_t &x, const SegmentOffset &y) { 475 CVSymbol s1 = syms.readRecord(x); 476 lldbassert(s1.kind() == S_PUB32); 477 PublicSym32 p1; 478 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(s1, p1)); 479 if (p1.Segment < y.segment) 480 return true; 481 return p1.Offset < y.offset; 482 }); 483 if (iter == addr_map.end()) 484 return llvm::None; 485 CVSymbol sym = syms.readRecord(*iter); 486 lldbassert(sym.kind() == S_PUB32); 487 PublicSym32 p; 488 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(sym, p)); 489 if (p.Segment == addr.segment && p.Offset == addr.offset) 490 return p; 491 return llvm::None; 492 } 493 494 clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) { 495 CVSymbol cvs = m_index.ReadSymbolRecord(id); 496 497 if (isLocalVariableType(cvs.kind())) { 498 clang::DeclContext *scope = GetParentDeclContext(id); 499 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope); 500 PdbCompilandSymId scope_id = 501 PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym(); 502 return GetOrCreateVariableDecl(scope_id, id); 503 } 504 505 switch (cvs.kind()) { 506 case S_GPROC32: 507 case S_LPROC32: 508 return GetOrCreateFunctionDecl(id); 509 case S_GDATA32: 510 case S_LDATA32: 511 case S_GTHREAD32: 512 case S_CONSTANT: 513 // global variable 514 return nullptr; 515 case S_BLOCK32: 516 return GetOrCreateBlockDecl(id); 517 case S_INLINESITE: 518 return GetOrCreateInlinedFunctionDecl(id); 519 default: 520 return nullptr; 521 } 522 } 523 524 llvm::Optional<CompilerDecl> PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) { 525 if (clang::Decl *result = TryGetDecl(uid)) 526 return ToCompilerDecl(*result); 527 528 clang::Decl *result = nullptr; 529 switch (uid.kind()) { 530 case PdbSymUidKind::CompilandSym: 531 result = GetOrCreateSymbolForId(uid.asCompilandSym()); 532 break; 533 case PdbSymUidKind::Type: { 534 clang::QualType qt = GetOrCreateType(uid.asTypeSym()); 535 if (auto *tag = qt->getAsTagDecl()) { 536 result = tag; 537 break; 538 } 539 return llvm::None; 540 } 541 default: 542 return llvm::None; 543 } 544 545 if (!result) 546 return llvm::None; 547 m_uid_to_decl[toOpaqueUid(uid)] = result; 548 return ToCompilerDecl(*result); 549 } 550 551 clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) { 552 if (uid.kind() == PdbSymUidKind::CompilandSym) { 553 if (uid.asCompilandSym().offset == 0) 554 return FromCompilerDeclContext(GetTranslationUnitDecl()); 555 } 556 auto option = GetOrCreateDeclForUid(uid); 557 if (!option) 558 return nullptr; 559 clang::Decl *decl = FromCompilerDecl(option.getValue()); 560 if (!decl) 561 return nullptr; 562 563 return clang::Decl::castToDeclContext(decl); 564 } 565 566 std::pair<clang::DeclContext *, std::string> 567 PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) { 568 MSVCUndecoratedNameParser parser(name); 569 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers(); 570 571 auto context = FromCompilerDeclContext(GetTranslationUnitDecl()); 572 573 llvm::StringRef uname = specs.back().GetBaseName(); 574 specs = specs.drop_back(); 575 if (specs.empty()) 576 return {context, std::string(name)}; 577 578 llvm::StringRef scope_name = specs.back().GetFullName(); 579 580 // It might be a class name, try that first. 581 std::vector<TypeIndex> types = m_index.tpi().findRecordsByName(scope_name); 582 while (!types.empty()) { 583 clang::QualType qt = GetOrCreateType(types.back()); 584 clang::TagDecl *tag = qt->getAsTagDecl(); 585 if (tag) 586 return {clang::TagDecl::castToDeclContext(tag), std::string(uname)}; 587 types.pop_back(); 588 } 589 590 // If that fails, treat it as a series of namespaces. 591 for (const MSVCUndecoratedNameSpecifier &spec : specs) { 592 std::string ns_name = spec.GetBaseName().str(); 593 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context); 594 } 595 return {context, std::string(uname)}; 596 } 597 598 clang::DeclContext * 599 PdbAstBuilder::GetParentDeclContextForSymbol(const CVSymbol &sym) { 600 if (!SymbolHasAddress(sym)) 601 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first; 602 SegmentOffset addr = GetSegmentAndOffset(sym); 603 llvm::Optional<PublicSym32> pub = 604 FindPublicSym(addr, m_index.symrecords(), m_index.publics()); 605 if (!pub) 606 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first; 607 608 llvm::ms_demangle::Demangler demangler; 609 StringView name{pub->Name.begin(), pub->Name.size()}; 610 llvm::ms_demangle::SymbolNode *node = demangler.parse(name); 611 if (!node) 612 return FromCompilerDeclContext(GetTranslationUnitDecl()); 613 llvm::ArrayRef<llvm::ms_demangle::Node *> name_components{ 614 node->Name->Components->Nodes, node->Name->Components->Count - 1}; 615 616 if (!name_components.empty()) { 617 // Render the current list of scope nodes as a fully qualified name, and 618 // look it up in the debug info as a type name. If we find something, 619 // this is a type (which may itself be prefixed by a namespace). If we 620 // don't, this is a list of namespaces. 621 std::string qname = RenderScopeList(name_components); 622 std::vector<TypeIndex> matches = m_index.tpi().findRecordsByName(qname); 623 while (!matches.empty()) { 624 clang::QualType qt = GetOrCreateType(matches.back()); 625 clang::TagDecl *tag = qt->getAsTagDecl(); 626 if (tag) 627 return clang::TagDecl::castToDeclContext(tag); 628 matches.pop_back(); 629 } 630 } 631 632 // It's not a type. It must be a series of namespaces. 633 auto context = FromCompilerDeclContext(GetTranslationUnitDecl()); 634 while (!name_components.empty()) { 635 std::string ns = name_components.front()->toString(); 636 context = GetOrCreateNamespaceDecl(ns.c_str(), *context); 637 name_components = name_components.drop_front(); 638 } 639 return context; 640 } 641 642 clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) { 643 // We must do this *without* calling GetOrCreate on the current uid, as 644 // that would be an infinite recursion. 645 switch (uid.kind()) { 646 case PdbSymUidKind::CompilandSym: { 647 llvm::Optional<PdbCompilandSymId> scope = 648 FindSymbolScope(m_index, uid.asCompilandSym()); 649 if (scope) 650 return GetOrCreateDeclContextForUid(*scope); 651 652 CVSymbol sym = m_index.ReadSymbolRecord(uid.asCompilandSym()); 653 return GetParentDeclContextForSymbol(sym); 654 } 655 case PdbSymUidKind::Type: { 656 // It could be a namespace, class, or global. We don't support nested 657 // functions yet. Anyway, we just need to consult the parent type map. 658 PdbTypeSymId type_id = uid.asTypeSym(); 659 auto iter = m_parent_types.find(type_id.index); 660 if (iter == m_parent_types.end()) 661 return FromCompilerDeclContext(GetTranslationUnitDecl()); 662 return GetOrCreateDeclContextForUid(PdbTypeSymId(iter->second)); 663 } 664 case PdbSymUidKind::FieldListMember: 665 // In this case the parent DeclContext is the one for the class that this 666 // member is inside of. 667 break; 668 case PdbSymUidKind::GlobalSym: { 669 // If this refers to a compiland symbol, just recurse in with that symbol. 670 // The only other possibilities are S_CONSTANT and S_UDT, in which case we 671 // need to parse the undecorated name to figure out the scope, then look 672 // that up in the TPI stream. If it's found, it's a type, othewrise it's 673 // a series of namespaces. 674 // FIXME: do this. 675 CVSymbol global = m_index.ReadSymbolRecord(uid.asGlobalSym()); 676 switch (global.kind()) { 677 case SymbolKind::S_GDATA32: 678 case SymbolKind::S_LDATA32: 679 return GetParentDeclContextForSymbol(global); 680 case SymbolKind::S_PROCREF: 681 case SymbolKind::S_LPROCREF: { 682 ProcRefSym ref{global.kind()}; 683 llvm::cantFail( 684 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref)); 685 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset}; 686 return GetParentDeclContext(cu_sym_id); 687 } 688 case SymbolKind::S_CONSTANT: 689 case SymbolKind::S_UDT: 690 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first; 691 default: 692 break; 693 } 694 break; 695 } 696 default: 697 break; 698 } 699 return FromCompilerDeclContext(GetTranslationUnitDecl()); 700 } 701 702 bool PdbAstBuilder::CompleteType(clang::QualType qt) { 703 clang::TagDecl *tag = qt->getAsTagDecl(); 704 if (!tag) 705 return false; 706 707 return CompleteTagDecl(*tag); 708 } 709 710 bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) { 711 // If this is not in our map, it's an error. 712 auto status_iter = m_decl_to_status.find(&tag); 713 lldbassert(status_iter != m_decl_to_status.end()); 714 715 // If it's already complete, just return. 716 DeclStatus &status = status_iter->second; 717 if (status.resolved) 718 return true; 719 720 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym(); 721 722 lldbassert(IsTagRecord(type_id, m_index.tpi())); 723 724 clang::QualType tag_qt = m_clang.getASTContext().getTypeDeclType(&tag); 725 TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false); 726 727 TypeIndex tag_ti = type_id.index; 728 CVType cvt = m_index.tpi().getType(tag_ti); 729 if (cvt.kind() == LF_MODIFIER) 730 tag_ti = LookThroughModifierRecord(cvt); 731 732 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, m_index.tpi()); 733 cvt = m_index.tpi().getType(best_ti.index); 734 lldbassert(IsTagRecord(cvt)); 735 736 if (IsForwardRefUdt(cvt)) { 737 // If we can't find a full decl for this forward ref anywhere in the debug 738 // info, then we have no way to complete it. 739 return false; 740 } 741 742 TypeIndex field_list_ti = GetFieldListIndex(cvt); 743 CVType field_list_cvt = m_index.tpi().getType(field_list_ti); 744 if (field_list_cvt.kind() != LF_FIELDLIST) 745 return false; 746 747 // Visit all members of this class, then perform any finalization necessary 748 // to complete the class. 749 CompilerType ct = ToCompilerType(tag_qt); 750 UdtRecordCompleter completer(best_ti, ct, tag, *this, m_index, 751 m_cxx_record_map); 752 auto error = 753 llvm::codeview::visitMemberRecordStream(field_list_cvt.data(), completer); 754 completer.complete(); 755 756 status.resolved = true; 757 if (!error) 758 return true; 759 760 llvm::consumeError(std::move(error)); 761 return false; 762 } 763 764 clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) { 765 if (ti == TypeIndex::NullptrT()) 766 return GetBasicType(lldb::eBasicTypeNullPtr); 767 768 if (ti.getSimpleMode() != SimpleTypeMode::Direct) { 769 clang::QualType direct_type = GetOrCreateType(ti.makeDirect()); 770 return m_clang.getASTContext().getPointerType(direct_type); 771 } 772 773 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated) 774 return {}; 775 776 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind()); 777 if (bt == lldb::eBasicTypeInvalid) 778 return {}; 779 780 return GetBasicType(bt); 781 } 782 783 clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) { 784 clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType); 785 786 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't 787 // create in the AST. 788 if (pointee_type.isNull()) 789 return {}; 790 791 if (pointer.isPointerToMember()) { 792 MemberPointerInfo mpi = pointer.getMemberInfo(); 793 clang::QualType class_type = GetOrCreateType(mpi.ContainingType); 794 795 return m_clang.getASTContext().getMemberPointerType( 796 pointee_type, class_type.getTypePtr()); 797 } 798 799 clang::QualType pointer_type; 800 if (pointer.getMode() == PointerMode::LValueReference) 801 pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type); 802 else if (pointer.getMode() == PointerMode::RValueReference) 803 pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type); 804 else 805 pointer_type = m_clang.getASTContext().getPointerType(pointee_type); 806 807 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None) 808 pointer_type.addConst(); 809 810 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None) 811 pointer_type.addVolatile(); 812 813 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None) 814 pointer_type.addRestrict(); 815 816 return pointer_type; 817 } 818 819 clang::QualType 820 PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) { 821 clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType); 822 if (unmodified_type.isNull()) 823 return {}; 824 825 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None) 826 unmodified_type.addConst(); 827 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None) 828 unmodified_type.addVolatile(); 829 830 return unmodified_type; 831 } 832 833 clang::QualType PdbAstBuilder::CreateRecordType(PdbTypeSymId id, 834 const TagRecord &record) { 835 clang::DeclContext *context = nullptr; 836 std::string uname; 837 std::tie(context, uname) = CreateDeclInfoForType(record, id.index); 838 clang::TagTypeKind ttk = TranslateUdtKind(record); 839 lldb::AccessType access = 840 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic; 841 842 ClangASTMetadata metadata; 843 metadata.SetUserID(toOpaqueUid(id)); 844 metadata.SetIsDynamicCXXType(false); 845 846 CompilerType ct = 847 m_clang.CreateRecordType(context, OptionalClangModuleID(), access, uname, 848 ttk, lldb::eLanguageTypeC_plus_plus, &metadata); 849 850 lldbassert(ct.IsValid()); 851 852 TypeSystemClang::StartTagDeclarationDefinition(ct); 853 854 // Even if it's possible, don't complete it at this point. Just mark it 855 // forward resolved, and if/when LLDB needs the full definition, it can 856 // ask us. 857 clang::QualType result = 858 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType()); 859 860 TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true); 861 return result; 862 } 863 864 clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const { 865 auto iter = m_uid_to_decl.find(toOpaqueUid(uid)); 866 if (iter != m_uid_to_decl.end()) 867 return iter->second; 868 return nullptr; 869 } 870 871 clang::NamespaceDecl * 872 PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name, 873 clang::DeclContext &context) { 874 return m_clang.GetUniqueNamespaceDeclaration( 875 IsAnonymousNamespaceName(name) ? nullptr : name, &context, 876 OptionalClangModuleID()); 877 } 878 879 clang::BlockDecl * 880 PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) { 881 if (clang::Decl *decl = TryGetDecl(block_id)) 882 return llvm::dyn_cast<clang::BlockDecl>(decl); 883 884 clang::DeclContext *scope = GetParentDeclContext(block_id); 885 886 clang::BlockDecl *block_decl = 887 m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID()); 888 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl}); 889 890 DeclStatus status; 891 status.resolved = true; 892 status.uid = toOpaqueUid(block_id); 893 m_decl_to_status.insert({block_decl, status}); 894 895 return block_decl; 896 } 897 898 clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym, 899 clang::DeclContext &scope) { 900 VariableInfo var_info = GetVariableNameInfo(sym); 901 clang::QualType qt = GetOrCreateType(var_info.type); 902 903 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration( 904 &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt); 905 906 m_uid_to_decl[toOpaqueUid(uid)] = var_decl; 907 DeclStatus status; 908 status.resolved = true; 909 status.uid = toOpaqueUid(uid); 910 m_decl_to_status.insert({var_decl, status}); 911 return var_decl; 912 } 913 914 clang::VarDecl * 915 PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id, 916 PdbCompilandSymId var_id) { 917 if (clang::Decl *decl = TryGetDecl(var_id)) 918 return llvm::dyn_cast<clang::VarDecl>(decl); 919 920 clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id); 921 if (!scope) 922 return nullptr; 923 924 CVSymbol sym = m_index.ReadSymbolRecord(var_id); 925 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope); 926 } 927 928 clang::VarDecl *PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id) { 929 if (clang::Decl *decl = TryGetDecl(var_id)) 930 return llvm::dyn_cast<clang::VarDecl>(decl); 931 932 CVSymbol sym = m_index.ReadSymbolRecord(var_id); 933 auto context = FromCompilerDeclContext(GetTranslationUnitDecl()); 934 return CreateVariableDecl(PdbSymUid(var_id), sym, *context); 935 } 936 937 clang::TypedefNameDecl * 938 PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) { 939 if (clang::Decl *decl = TryGetDecl(id)) 940 return llvm::dyn_cast<clang::TypedefNameDecl>(decl); 941 942 CVSymbol sym = m_index.ReadSymbolRecord(id); 943 lldbassert(sym.kind() == S_UDT); 944 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym)); 945 946 clang::DeclContext *scope = GetParentDeclContext(id); 947 948 PdbTypeSymId real_type_id{udt.Type, false}; 949 clang::QualType qt = GetOrCreateType(real_type_id); 950 951 std::string uname = std::string(DropNameScope(udt.Name)); 952 953 CompilerType ct = ToCompilerType(qt).CreateTypedef( 954 uname.c_str(), ToCompilerDeclContext(*scope), 0); 955 clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct); 956 DeclStatus status; 957 status.resolved = true; 958 status.uid = toOpaqueUid(id); 959 m_decl_to_status.insert({tnd, status}); 960 return tnd; 961 } 962 963 clang::QualType PdbAstBuilder::GetBasicType(lldb::BasicType type) { 964 CompilerType ct = m_clang.GetBasicType(type); 965 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType()); 966 } 967 968 clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) { 969 if (type.index.isSimple()) 970 return CreateSimpleType(type.index); 971 972 CVType cvt = m_index.tpi().getType(type.index); 973 974 if (cvt.kind() == LF_MODIFIER) { 975 ModifierRecord modifier; 976 llvm::cantFail( 977 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier)); 978 return CreateModifierType(modifier); 979 } 980 981 if (cvt.kind() == LF_POINTER) { 982 PointerRecord pointer; 983 llvm::cantFail( 984 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer)); 985 return CreatePointerType(pointer); 986 } 987 988 if (IsTagRecord(cvt)) { 989 CVTagRecord tag = CVTagRecord::create(cvt); 990 if (tag.kind() == CVTagRecord::Union) 991 return CreateRecordType(type.index, tag.asUnion()); 992 if (tag.kind() == CVTagRecord::Enum) 993 return CreateEnumType(type.index, tag.asEnum()); 994 return CreateRecordType(type.index, tag.asClass()); 995 } 996 997 if (cvt.kind() == LF_ARRAY) { 998 ArrayRecord ar; 999 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar)); 1000 return CreateArrayType(ar); 1001 } 1002 1003 if (cvt.kind() == LF_PROCEDURE) { 1004 ProcedureRecord pr; 1005 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr)); 1006 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv); 1007 } 1008 1009 if (cvt.kind() == LF_MFUNCTION) { 1010 MemberFunctionRecord mfr; 1011 llvm::cantFail( 1012 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr)); 1013 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv); 1014 } 1015 1016 return {}; 1017 } 1018 1019 clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) { 1020 lldb::user_id_t uid = toOpaqueUid(type); 1021 auto iter = m_uid_to_type.find(uid); 1022 if (iter != m_uid_to_type.end()) 1023 return iter->second; 1024 1025 PdbTypeSymId best_type = GetBestPossibleDecl(type, m_index.tpi()); 1026 1027 clang::QualType qt; 1028 if (best_type.index != type.index) { 1029 // This is a forward decl. Call GetOrCreate on the full decl, then map the 1030 // forward decl id to the full decl QualType. 1031 clang::QualType qt = GetOrCreateType(best_type); 1032 m_uid_to_type[toOpaqueUid(type)] = qt; 1033 return qt; 1034 } 1035 1036 // This is either a full decl, or a forward decl with no matching full decl 1037 // in the debug info. 1038 qt = CreateType(type); 1039 m_uid_to_type[toOpaqueUid(type)] = qt; 1040 if (IsTagRecord(type, m_index.tpi())) { 1041 clang::TagDecl *tag = qt->getAsTagDecl(); 1042 lldbassert(m_decl_to_status.count(tag) == 0); 1043 1044 DeclStatus &status = m_decl_to_status[tag]; 1045 status.uid = uid; 1046 status.resolved = false; 1047 } 1048 return qt; 1049 } 1050 1051 clang::FunctionDecl * 1052 PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id, 1053 llvm::StringRef func_name, TypeIndex func_ti, 1054 CompilerType func_ct, uint32_t param_count, 1055 clang::StorageClass func_storage, 1056 bool is_inline, clang::DeclContext *parent) { 1057 clang::FunctionDecl *function_decl = nullptr; 1058 if (parent->isRecord()) { 1059 clang::QualType parent_qt = llvm::cast<clang::TypeDecl>(parent) 1060 ->getTypeForDecl() 1061 ->getCanonicalTypeInternal(); 1062 lldb::opaque_compiler_type_t parent_opaque_ty = 1063 ToCompilerType(parent_qt).GetOpaqueQualType(); 1064 auto iter = m_cxx_record_map.find(parent_opaque_ty); 1065 if (iter != m_cxx_record_map.end()) { 1066 if (iter->getSecond().contains({func_name, func_ct})) { 1067 return nullptr; 1068 } 1069 } 1070 1071 CVType cvt = m_index.tpi().getType(func_ti); 1072 MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind())); 1073 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>( 1074 cvt, func_record)); 1075 TypeIndex class_index = func_record.getClassType(); 1076 1077 CVType parent_cvt = m_index.tpi().getType(class_index); 1078 ClassRecord class_record = CVTagRecord::create(parent_cvt).asClass(); 1079 // If it's a forward reference, try to get the real TypeIndex. 1080 if (class_record.isForwardRef()) { 1081 llvm::Expected<TypeIndex> eti = 1082 m_index.tpi().findFullDeclForForwardRef(class_index); 1083 if (eti) { 1084 class_record = 1085 CVTagRecord::create(m_index.tpi().getType(*eti)).asClass(); 1086 } 1087 } 1088 if (!class_record.FieldList.isSimple()) { 1089 CVType field_list = m_index.tpi().getType(class_record.FieldList); 1090 CreateMethodDecl process(m_index, m_clang, func_ti, function_decl, 1091 parent_opaque_ty, func_name, func_ct); 1092 if (llvm::Error err = visitMemberRecordStream(field_list.data(), process)) 1093 llvm::consumeError(std::move(err)); 1094 } 1095 1096 if (!function_decl) { 1097 function_decl = m_clang.AddMethodToCXXRecordType( 1098 parent_opaque_ty, func_name, 1099 /*mangled_name=*/nullptr, func_ct, 1100 /*access=*/lldb::AccessType::eAccessPublic, 1101 /*is_virtual=*/false, /*is_static=*/false, 1102 /*is_inline=*/false, /*is_explicit=*/false, 1103 /*is_attr_used=*/false, /*is_artificial=*/false); 1104 } 1105 m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct}); 1106 } else { 1107 function_decl = m_clang.CreateFunctionDeclaration( 1108 parent, OptionalClangModuleID(), func_name, func_ct, func_storage, 1109 is_inline); 1110 CreateFunctionParameters(func_id, *function_decl, param_count); 1111 } 1112 return function_decl; 1113 } 1114 1115 clang::FunctionDecl * 1116 PdbAstBuilder::GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id) { 1117 CompilandIndexItem *cii = 1118 m_index.compilands().GetCompiland(inlinesite_id.modi); 1119 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset); 1120 InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind())); 1121 cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site)); 1122 1123 // Inlinee is the id index to the function id record that is inlined. 1124 PdbTypeSymId func_id(inline_site.Inlinee, true); 1125 // Look up the function decl by the id index to see if we have created a 1126 // function decl for a different inlinesite that refers the same function. 1127 if (clang::Decl *decl = TryGetDecl(func_id)) 1128 return llvm::dyn_cast<clang::FunctionDecl>(decl); 1129 clang::FunctionDecl *function_decl = 1130 CreateFunctionDeclFromId(func_id, inlinesite_id); 1131 1132 // Use inline site id in m_decl_to_status because it's expected to be a 1133 // PdbCompilandSymId so that we can parse local variables info after it. 1134 uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id); 1135 DeclStatus status; 1136 status.resolved = true; 1137 status.uid = inlinesite_uid; 1138 m_decl_to_status.insert({function_decl, status}); 1139 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI 1140 // stream are unique and there could be multiple inline sites (different ids) 1141 // referring the same inline function. This avoid creating multiple same 1142 // inline function delcs. 1143 uint64_t func_uid = toOpaqueUid(func_id); 1144 lldbassert(m_uid_to_decl.count(func_uid) == 0); 1145 m_uid_to_decl[func_uid] = function_decl; 1146 return function_decl; 1147 } 1148 1149 clang::FunctionDecl * 1150 PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid, 1151 PdbCompilandSymId func_sid) { 1152 lldbassert(func_tid.is_ipi); 1153 CVType func_cvt = m_index.ipi().getType(func_tid.index); 1154 llvm::StringRef func_name; 1155 TypeIndex func_ti; 1156 clang::DeclContext *parent = nullptr; 1157 switch (func_cvt.kind()) { 1158 case LF_MFUNC_ID: { 1159 MemberFuncIdRecord mfr; 1160 cantFail( 1161 TypeDeserializer::deserializeAs<MemberFuncIdRecord>(func_cvt, mfr)); 1162 func_name = mfr.getName(); 1163 func_ti = mfr.getFunctionType(); 1164 PdbTypeSymId class_type_id(mfr.ClassType, false); 1165 parent = GetOrCreateDeclContextForUid(class_type_id); 1166 break; 1167 } 1168 case LF_FUNC_ID: { 1169 FuncIdRecord fir; 1170 cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(func_cvt, fir)); 1171 func_name = fir.getName(); 1172 func_ti = fir.getFunctionType(); 1173 parent = FromCompilerDeclContext(GetTranslationUnitDecl()); 1174 if (!fir.ParentScope.isNoneType()) { 1175 CVType parent_cvt = m_index.ipi().getType(fir.ParentScope); 1176 if (parent_cvt.kind() == LF_STRING_ID) { 1177 StringIdRecord sir; 1178 cantFail( 1179 TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir)); 1180 parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent); 1181 } 1182 } 1183 break; 1184 } 1185 default: 1186 lldbassert(false && "Invalid function id type!"); 1187 } 1188 clang::QualType func_qt = GetOrCreateType(func_ti); 1189 if (func_qt.isNull()) 1190 return nullptr; 1191 CompilerType func_ct = ToCompilerType(func_qt); 1192 uint32_t param_count = 1193 llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams(); 1194 return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count, 1195 clang::SC_None, true, parent); 1196 } 1197 1198 clang::FunctionDecl * 1199 PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) { 1200 if (clang::Decl *decl = TryGetDecl(func_id)) 1201 return llvm::dyn_cast<clang::FunctionDecl>(decl); 1202 1203 clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id)); 1204 std::string context_name; 1205 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) { 1206 context_name = ns->getQualifiedNameAsString(); 1207 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) { 1208 context_name = tag->getQualifiedNameAsString(); 1209 } 1210 1211 CVSymbol cvs = m_index.ReadSymbolRecord(func_id); 1212 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind())); 1213 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc)); 1214 1215 PdbTypeSymId type_id(proc.FunctionType); 1216 clang::QualType qt = GetOrCreateType(type_id); 1217 if (qt.isNull()) 1218 return nullptr; 1219 1220 clang::StorageClass storage = clang::SC_None; 1221 if (proc.Kind == SymbolRecordKind::ProcSym) 1222 storage = clang::SC_Static; 1223 1224 const clang::FunctionProtoType *func_type = 1225 llvm::dyn_cast<clang::FunctionProtoType>(qt); 1226 1227 CompilerType func_ct = ToCompilerType(qt); 1228 1229 llvm::StringRef proc_name = proc.Name; 1230 proc_name.consume_front(context_name); 1231 proc_name.consume_front("::"); 1232 1233 clang::FunctionDecl *function_decl = 1234 CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct, 1235 func_type->getNumParams(), storage, false, parent); 1236 1237 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0); 1238 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl; 1239 DeclStatus status; 1240 status.resolved = true; 1241 status.uid = toOpaqueUid(func_id); 1242 m_decl_to_status.insert({function_decl, status}); 1243 1244 return function_decl; 1245 } 1246 1247 void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id, 1248 clang::FunctionDecl &function_decl, 1249 uint32_t param_count) { 1250 CompilandIndexItem *cii = m_index.compilands().GetCompiland(func_id.modi); 1251 CVSymbolArray scope = 1252 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset); 1253 1254 scope.drop_front(); 1255 auto begin = scope.begin(); 1256 auto end = scope.end(); 1257 std::vector<clang::ParmVarDecl *> params; 1258 while (begin != end && param_count > 0) { 1259 uint32_t record_offset = begin.offset(); 1260 CVSymbol sym = *begin++; 1261 1262 TypeIndex param_type; 1263 llvm::StringRef param_name; 1264 switch (sym.kind()) { 1265 case S_REGREL32: { 1266 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym); 1267 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg)); 1268 param_type = reg.Type; 1269 param_name = reg.Name; 1270 break; 1271 } 1272 case S_REGISTER: { 1273 RegisterSym reg(SymbolRecordKind::RegisterSym); 1274 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg)); 1275 param_type = reg.Index; 1276 param_name = reg.Name; 1277 break; 1278 } 1279 case S_LOCAL: { 1280 LocalSym local(SymbolRecordKind::LocalSym); 1281 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local)); 1282 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None) 1283 continue; 1284 param_type = local.Type; 1285 param_name = local.Name; 1286 break; 1287 } 1288 case S_BLOCK32: 1289 case S_INLINESITE: 1290 case S_INLINESITE2: 1291 // All parameters should come before the first block/inlinesite. If that 1292 // isn't the case, then perhaps this is bad debug info that doesn't 1293 // contain information about all parameters. 1294 return; 1295 default: 1296 continue; 1297 } 1298 1299 PdbCompilandSymId param_uid(func_id.modi, record_offset); 1300 clang::QualType qt = GetOrCreateType(param_type); 1301 1302 CompilerType param_type_ct = m_clang.GetType(qt); 1303 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration( 1304 &function_decl, OptionalClangModuleID(), param_name.str().c_str(), 1305 param_type_ct, clang::SC_None, true); 1306 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0); 1307 1308 m_uid_to_decl[toOpaqueUid(param_uid)] = param; 1309 params.push_back(param); 1310 --param_count; 1311 } 1312 1313 if (!params.empty()) 1314 m_clang.SetFunctionParameters(&function_decl, params); 1315 } 1316 1317 clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id, 1318 const EnumRecord &er) { 1319 clang::DeclContext *decl_context = nullptr; 1320 std::string uname; 1321 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index); 1322 clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType); 1323 1324 Declaration declaration; 1325 CompilerType enum_ct = m_clang.CreateEnumerationType( 1326 uname, decl_context, OptionalClangModuleID(), declaration, 1327 ToCompilerType(underlying_type), er.isScoped()); 1328 1329 TypeSystemClang::StartTagDeclarationDefinition(enum_ct); 1330 TypeSystemClang::SetHasExternalStorage(enum_ct.GetOpaqueQualType(), true); 1331 1332 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType()); 1333 } 1334 1335 clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) { 1336 clang::QualType element_type = GetOrCreateType(ar.ElementType); 1337 1338 uint64_t element_size = GetSizeOfType({ar.ElementType}, m_index.tpi()); 1339 if (element_size == 0) 1340 return {}; 1341 uint64_t element_count = ar.Size / element_size; 1342 1343 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type), 1344 element_count, false); 1345 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType()); 1346 } 1347 1348 clang::QualType PdbAstBuilder::CreateFunctionType( 1349 TypeIndex args_type_idx, TypeIndex return_type_idx, 1350 llvm::codeview::CallingConvention calling_convention) { 1351 TpiStream &stream = m_index.tpi(); 1352 CVType args_cvt = stream.getType(args_type_idx); 1353 ArgListRecord args; 1354 llvm::cantFail( 1355 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args)); 1356 1357 llvm::ArrayRef<TypeIndex> arg_indices = llvm::makeArrayRef(args.ArgIndices); 1358 bool is_variadic = IsCVarArgsFunction(arg_indices); 1359 if (is_variadic) 1360 arg_indices = arg_indices.drop_back(); 1361 1362 std::vector<CompilerType> arg_types; 1363 arg_types.reserve(arg_indices.size()); 1364 1365 for (TypeIndex arg_index : arg_indices) { 1366 clang::QualType arg_type = GetOrCreateType(arg_index); 1367 arg_types.push_back(ToCompilerType(arg_type)); 1368 } 1369 1370 clang::QualType return_type = GetOrCreateType(return_type_idx); 1371 1372 llvm::Optional<clang::CallingConv> cc = 1373 TranslateCallingConvention(calling_convention); 1374 if (!cc) 1375 return {}; 1376 1377 CompilerType return_ct = ToCompilerType(return_type); 1378 CompilerType func_sig_ast_type = m_clang.CreateFunctionType( 1379 return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc); 1380 1381 return clang::QualType::getFromOpaquePtr( 1382 func_sig_ast_type.GetOpaqueQualType()); 1383 } 1384 1385 static bool isTagDecl(clang::DeclContext &context) { 1386 return llvm::isa<clang::TagDecl>(&context); 1387 } 1388 1389 static bool isFunctionDecl(clang::DeclContext &context) { 1390 return llvm::isa<clang::FunctionDecl>(&context); 1391 } 1392 1393 static bool isBlockDecl(clang::DeclContext &context) { 1394 return llvm::isa<clang::BlockDecl>(&context); 1395 } 1396 1397 void PdbAstBuilder::ParseAllNamespacesPlusChildrenOf( 1398 llvm::Optional<llvm::StringRef> parent) { 1399 TypeIndex ti{m_index.tpi().TypeIndexBegin()}; 1400 for (const CVType &cvt : m_index.tpi().typeArray()) { 1401 PdbTypeSymId tid{ti}; 1402 ++ti; 1403 1404 if (!IsTagRecord(cvt)) 1405 continue; 1406 1407 CVTagRecord tag = CVTagRecord::create(cvt); 1408 1409 if (!parent.hasValue()) { 1410 clang::QualType qt = GetOrCreateType(tid); 1411 CompleteType(qt); 1412 continue; 1413 } 1414 1415 // Call CreateDeclInfoForType unconditionally so that the namespace info 1416 // gets created. But only call CreateRecordType if the namespace name 1417 // matches. 1418 clang::DeclContext *context = nullptr; 1419 std::string uname; 1420 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index); 1421 if (!context->isNamespace()) 1422 continue; 1423 1424 clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context); 1425 std::string actual_ns = ns->getQualifiedNameAsString(); 1426 if (llvm::StringRef(actual_ns).startswith(*parent)) { 1427 clang::QualType qt = GetOrCreateType(tid); 1428 CompleteType(qt); 1429 continue; 1430 } 1431 } 1432 1433 uint32_t module_count = m_index.dbi().modules().getModuleCount(); 1434 for (uint16_t modi = 0; modi < module_count; ++modi) { 1435 CompilandIndexItem &cii = m_index.compilands().GetOrCreateCompiland(modi); 1436 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray(); 1437 auto iter = symbols.begin(); 1438 while (iter != symbols.end()) { 1439 PdbCompilandSymId sym_id{modi, iter.offset()}; 1440 1441 switch (iter->kind()) { 1442 case S_GPROC32: 1443 case S_LPROC32: 1444 GetOrCreateFunctionDecl(sym_id); 1445 iter = symbols.at(getScopeEndOffset(*iter)); 1446 break; 1447 case S_GDATA32: 1448 case S_GTHREAD32: 1449 case S_LDATA32: 1450 case S_LTHREAD32: 1451 GetOrCreateVariableDecl(PdbCompilandSymId(modi, 0), sym_id); 1452 ++iter; 1453 break; 1454 default: 1455 ++iter; 1456 continue; 1457 } 1458 } 1459 } 1460 } 1461 1462 static CVSymbolArray skipFunctionParameters(clang::Decl &decl, 1463 const CVSymbolArray &symbols) { 1464 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl); 1465 if (!func_decl) 1466 return symbols; 1467 unsigned int params = func_decl->getNumParams(); 1468 if (params == 0) 1469 return symbols; 1470 1471 CVSymbolArray result = symbols; 1472 1473 while (!result.empty()) { 1474 if (params == 0) 1475 return result; 1476 1477 CVSymbol sym = *result.begin(); 1478 result.drop_front(); 1479 1480 if (!isLocalVariableType(sym.kind())) 1481 continue; 1482 1483 --params; 1484 } 1485 return result; 1486 } 1487 1488 void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id) { 1489 CVSymbol sym = m_index.ReadSymbolRecord(block_id); 1490 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 || 1491 sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE); 1492 CompilandIndexItem &cii = 1493 m_index.compilands().GetOrCreateCompiland(block_id.modi); 1494 CVSymbolArray symbols = 1495 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset); 1496 1497 // Function parameters should already have been created when the function was 1498 // parsed. 1499 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) 1500 symbols = 1501 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols); 1502 1503 symbols.drop_front(); 1504 auto begin = symbols.begin(); 1505 while (begin != symbols.end()) { 1506 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset()); 1507 GetOrCreateSymbolForId(child_sym_id); 1508 if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) { 1509 ParseBlockChildren(child_sym_id); 1510 begin = symbols.at(getScopeEndOffset(*begin)); 1511 } 1512 ++begin; 1513 } 1514 } 1515 1516 void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) { 1517 1518 clang::Decl *decl = clang::Decl::castFromDeclContext(&context); 1519 lldbassert(decl); 1520 1521 auto iter = m_decl_to_status.find(decl); 1522 lldbassert(iter != m_decl_to_status.end()); 1523 1524 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) { 1525 CompleteTagDecl(*tag); 1526 return; 1527 } 1528 1529 if (isFunctionDecl(context) || isBlockDecl(context)) { 1530 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym(); 1531 ParseBlockChildren(block_id); 1532 } 1533 } 1534 1535 void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) { 1536 // Namespaces aren't explicitly represented in the debug info, and the only 1537 // way to parse them is to parse all type info, demangling every single type 1538 // and trying to reconstruct the DeclContext hierarchy this way. Since this 1539 // is an expensive operation, we have to special case it so that we do other 1540 // work (such as parsing the items that appear within the namespaces) at the 1541 // same time. 1542 if (context.isTranslationUnit()) { 1543 ParseAllNamespacesPlusChildrenOf(llvm::None); 1544 return; 1545 } 1546 1547 if (context.isNamespace()) { 1548 clang::NamespaceDecl &ns = *llvm::dyn_cast<clang::NamespaceDecl>(&context); 1549 std::string qname = ns.getQualifiedNameAsString(); 1550 ParseAllNamespacesPlusChildrenOf(llvm::StringRef{qname}); 1551 return; 1552 } 1553 1554 if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) { 1555 ParseDeclsForSimpleContext(context); 1556 return; 1557 } 1558 } 1559 1560 CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) { 1561 return m_clang.GetCompilerDecl(&decl); 1562 } 1563 1564 CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) { 1565 return {&m_clang, qt.getAsOpaquePtr()}; 1566 } 1567 1568 CompilerDeclContext 1569 PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) { 1570 return m_clang.CreateDeclContext(&context); 1571 } 1572 1573 clang::Decl * PdbAstBuilder::FromCompilerDecl(CompilerDecl decl) { 1574 return ClangUtil::GetDecl(decl); 1575 } 1576 1577 clang::DeclContext * 1578 PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) { 1579 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext()); 1580 } 1581 1582 void PdbAstBuilder::Dump(Stream &stream) { 1583 m_clang.Dump(stream.AsRawOstream()); 1584 } 1585