1 #include "PdbAstBuilder.h"
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"
17 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Symbol/ClangASTContext.h"
20 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
21 #include "lldb/Symbol/ClangUtil.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Utility/LLDBAssert.h"
26 #include "UdtRecordCompleter.h"
28 using namespace lldb_private;
29 using namespace lldb_private::npdb;
30 using namespace llvm::codeview;
31 using namespace llvm::pdb;
33 static llvm::Optional<PdbCompilandSymId> FindSymbolScope(PdbIndex &index,
34 PdbCompilandSymId id) {
35 CVSymbol sym = index.ReadSymbolRecord(id);
36 if (symbolOpensScope(sym.kind())) {
37 // If this exact symbol opens a scope, we can just directly access its
39 id.offset = getScopeParentOffset(sym);
40 // Global symbols have parent offset of 0. Return llvm::None to indicate
47 // Otherwise we need to start at the beginning and iterate forward until we
48 // reach (or pass) this particular symbol
49 CompilandIndexItem &cii = index.compilands().GetOrCreateCompiland(id.modi);
50 const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray();
52 auto begin = syms.begin();
53 auto end = syms.at(id.offset);
54 std::vector<PdbCompilandSymId> scope_stack;
56 while (begin != end) {
57 if (id.offset == begin.offset()) {
58 // We have a match! Return the top of the stack
59 if (scope_stack.empty())
61 return scope_stack.back();
63 if (begin.offset() > id.offset) {
64 // We passed it. We couldn't even find this symbol record.
65 lldbassert(false && "Invalid compiland symbol id!");
69 // We haven't found the symbol yet. Check if we need to open or close the
71 if (symbolOpensScope(begin->kind())) {
72 // We can use the end offset of the scope to determine whether or not
73 // we can just outright skip this entire scope.
74 uint32_t scope_end = getScopeEndOffset(*begin);
75 if (scope_end < id.modi) {
76 begin = syms.at(scope_end);
78 // The symbol we're looking for is somewhere in this scope.
79 scope_stack.emplace_back(id.modi, begin.offset());
81 } else if (symbolEndsScope(begin->kind())) {
82 scope_stack.pop_back();
90 static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) {
92 case TypeRecordKind::Class:
93 return clang::TTK_Class;
94 case TypeRecordKind::Struct:
95 return clang::TTK_Struct;
96 case TypeRecordKind::Union:
97 return clang::TTK_Union;
98 case TypeRecordKind::Interface:
99 return clang::TTK_Interface;
100 case TypeRecordKind::Enum:
101 return clang::TTK_Enum;
103 lldbassert(false && "Invalid tag record kind!");
104 return clang::TTK_Struct;
108 static bool IsCVarArgsFunction(llvm::ArrayRef<TypeIndex> args) {
111 return args.back() == TypeIndex::None();
115 AnyScopesHaveTemplateParams(llvm::ArrayRef<llvm::ms_demangle::Node *> scopes) {
116 for (llvm::ms_demangle::Node *n : scopes) {
117 auto *idn = static_cast<llvm::ms_demangle::IdentifierNode *>(n);
118 if (idn->TemplateParams)
124 static ClangASTContext &GetClangASTContext(ObjectFile &obj) {
126 obj.GetModule()->GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
128 return static_cast<ClangASTContext &>(*ts);
131 static llvm::Optional<clang::CallingConv>
132 TranslateCallingConvention(llvm::codeview::CallingConvention conv) {
133 using CC = llvm::codeview::CallingConvention;
138 return clang::CallingConv::CC_C;
141 return clang::CallingConv::CC_X86Pascal;
144 return clang::CallingConv::CC_X86FastCall;
145 case CC::NearStdCall:
147 return clang::CallingConv::CC_X86StdCall;
149 return clang::CallingConv::CC_X86ThisCall;
151 return clang::CallingConv::CC_X86VectorCall;
157 static llvm::Optional<CVTagRecord>
158 GetNestedTagDefinition(const NestedTypeRecord &Record,
159 const CVTagRecord &parent, TpiStream &tpi) {
160 // An LF_NESTTYPE is essentially a nested typedef / using declaration, but it
161 // is also used to indicate the primary definition of a nested class. That is
162 // to say, if you have:
167 // Then in the debug info, this will appear as:
168 // LF_STRUCTURE `A::B` [type index = N]
170 // LF_NESTTYPE [name = `B`, index = N]
171 // LF_NESTTYPE [name = `C`, index = N]
172 // In order to accurately reconstruct the decl context hierarchy, we need to
173 // know which ones are actual definitions and which ones are just aliases.
175 // If it's a simple type, then this is something like `using foo = int`.
176 if (Record.Type.isSimple())
179 CVType cvt = tpi.getType(Record.Type);
181 if (!IsTagRecord(cvt))
184 // If it's an inner definition, then treat whatever name we have here as a
185 // single component of a mangled name. So we can inject it into the parent's
186 // mangled name to see if it matches.
187 CVTagRecord child = CVTagRecord::create(cvt);
188 std::string qname = parent.asTag().getUniqueName();
189 if (qname.size() < 4 || child.asTag().getUniqueName().size() < 4)
192 // qname[3] is the tag type identifier (struct, class, union, etc). Since the
193 // inner tag type is not necessarily the same as the outer tag type, re-write
194 // it to match the inner tag type.
195 qname[3] = child.asTag().getUniqueName()[3];
199 piece += Record.Name;
200 piece.push_back('@');
201 qname.insert(4, std::move(piece));
202 if (qname != child.asTag().UniqueName)
205 return std::move(child);
208 static bool IsAnonymousNamespaceName(llvm::StringRef name) {
209 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
212 PdbAstBuilder::PdbAstBuilder(ObjectFile &obj, PdbIndex &index)
213 : m_index(index), m_clang(GetClangASTContext(obj)) {
217 lldb_private::CompilerDeclContext PdbAstBuilder::GetTranslationUnitDecl() {
218 return ToCompilerDeclContext(*m_clang.GetTranslationUnitDecl());
221 std::pair<clang::DeclContext *, std::string>
222 PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
223 // FIXME: Move this to GetDeclContextContainingUID.
224 if (!record.hasUniqueName())
225 return CreateDeclInfoForUndecoratedName(record.Name);
227 llvm::ms_demangle::Demangler demangler;
228 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
229 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
231 return {m_clang.GetTranslationUnitDecl(), record.UniqueName};
233 llvm::ms_demangle::IdentifierNode *idn =
234 ttn->QualifiedName->getUnqualifiedIdentifier();
235 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
237 llvm::ms_demangle::NodeArrayNode *name_components =
238 ttn->QualifiedName->Components;
239 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
240 name_components->Count - 1);
242 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
244 // If this type doesn't have a parent type in the debug info, then the best we
245 // can do is to say that it's either a series of namespaces (if the scope is
246 // non-empty), or the translation unit (if the scope is empty).
247 auto parent_iter = m_parent_types.find(ti);
248 if (parent_iter == m_parent_types.end()) {
250 return {context, uname};
252 // If there is no parent in the debug info, but some of the scopes have
253 // template params, then this is a case of bad debug info. See, for
254 // example, llvm.org/pr39607. We don't want to create an ambiguity between
255 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
256 // global scope with the fully qualified name.
257 if (AnyScopesHaveTemplateParams(scopes))
258 return {context, record.Name};
260 for (llvm::ms_demangle::Node *scope : scopes) {
261 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
262 std::string str = nii->toString();
263 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
265 return {context, uname};
268 // Otherwise, all we need to do is get the parent type of this type and
269 // recurse into our lazy type creation / AST reconstruction logic to get an
270 // LLDB TypeSP for the parent. This will cause the AST to automatically get
271 // the right DeclContext created for any parent.
272 clang::QualType parent_qt = GetOrCreateType(parent_iter->second);
274 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
275 return {context, uname};
278 void PdbAstBuilder::BuildParentMap() {
279 LazyRandomTypeCollection &types = m_index.tpi().typeCollection();
281 llvm::DenseMap<TypeIndex, TypeIndex> forward_to_full;
282 llvm::DenseMap<TypeIndex, TypeIndex> full_to_forward;
284 struct RecordIndices {
289 llvm::StringMap<RecordIndices> record_indices;
291 for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) {
292 CVType type = types.getType(*ti);
293 if (!IsTagRecord(type))
296 CVTagRecord tag = CVTagRecord::create(type);
298 RecordIndices &indices = record_indices[tag.asTag().getUniqueName()];
299 if (tag.asTag().isForwardRef())
300 indices.forward = *ti;
304 if (indices.full != TypeIndex::None() &&
305 indices.forward != TypeIndex::None()) {
306 forward_to_full[indices.forward] = indices.full;
307 full_to_forward[indices.full] = indices.forward;
310 // We're looking for LF_NESTTYPE records in the field list, so ignore
311 // forward references (no field list), and anything without a nested class
312 // (since there won't be any LF_NESTTYPE records).
313 if (tag.asTag().isForwardRef() || !tag.asTag().containsNestedClass())
316 struct ProcessTpiStream : public TypeVisitorCallbacks {
317 ProcessTpiStream(PdbIndex &index, TypeIndex parent,
318 const CVTagRecord &parent_cvt,
319 llvm::DenseMap<TypeIndex, TypeIndex> &parents)
320 : index(index), parents(parents), parent(parent),
321 parent_cvt(parent_cvt) {}
324 llvm::DenseMap<TypeIndex, TypeIndex> &parents;
326 unsigned unnamed_type_index = 1;
328 const CVTagRecord &parent_cvt;
330 llvm::Error visitKnownMember(CVMemberRecord &CVR,
331 NestedTypeRecord &Record) override {
332 std::string unnamed_type_name;
333 if (Record.Name.empty()) {
335 llvm::formatv("<unnamed-type-$S{0}>", unnamed_type_index).str();
336 Record.Name = unnamed_type_name;
337 ++unnamed_type_index;
339 llvm::Optional<CVTagRecord> tag =
340 GetNestedTagDefinition(Record, parent_cvt, index.tpi());
342 return llvm::ErrorSuccess();
344 parents[Record.Type] = parent;
345 return llvm::ErrorSuccess();
349 CVType field_list = m_index.tpi().getType(tag.asTag().FieldList);
350 ProcessTpiStream process(m_index, *ti, tag, m_parent_types);
351 llvm::Error error = visitMemberRecordStream(field_list.data(), process);
353 llvm::consumeError(std::move(error));
356 // Now that we know the forward -> full mapping of all type indices, we can
357 // re-write all the indices. At the end of this process, we want a mapping
358 // consisting of fwd -> full and full -> full for all child -> parent indices.
359 // We can re-write the values in place, but for the keys, we must save them
360 // off so that we don't modify the map in place while also iterating it.
361 std::vector<TypeIndex> full_keys;
362 std::vector<TypeIndex> fwd_keys;
363 for (auto &entry : m_parent_types) {
364 TypeIndex key = entry.first;
365 TypeIndex value = entry.second;
367 auto iter = forward_to_full.find(value);
368 if (iter != forward_to_full.end())
369 entry.second = iter->second;
371 iter = forward_to_full.find(key);
372 if (iter != forward_to_full.end())
373 fwd_keys.push_back(key);
375 full_keys.push_back(key);
377 for (TypeIndex fwd : fwd_keys) {
378 TypeIndex full = forward_to_full[fwd];
379 m_parent_types[full] = m_parent_types[fwd];
381 for (TypeIndex full : full_keys) {
382 TypeIndex fwd = full_to_forward[full];
383 m_parent_types[fwd] = m_parent_types[full];
389 static bool isLocalVariableType(SymbolKind K) {
402 RenderScopeList(llvm::ArrayRef<llvm::ms_demangle::Node *> nodes) {
403 lldbassert(!nodes.empty());
405 std::string result = nodes.front()->toString();
406 nodes = nodes.drop_front();
407 while (!nodes.empty()) {
409 result += nodes.front()->toString(llvm::ms_demangle::OF_NoTagSpecifier);
410 nodes = nodes.drop_front();
415 static llvm::Optional<PublicSym32> FindPublicSym(const SegmentOffset &addr,
417 PublicsStream &publics) {
418 llvm::FixedStreamArray<ulittle32_t> addr_map = publics.getAddressMap();
419 auto iter = std::lower_bound(
420 addr_map.begin(), addr_map.end(), addr,
421 [&](const ulittle32_t &x, const SegmentOffset &y) {
422 CVSymbol s1 = syms.readRecord(x);
423 lldbassert(s1.kind() == S_PUB32);
425 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(s1, p1));
426 if (p1.Segment < y.segment)
428 return p1.Offset < y.offset;
430 if (iter == addr_map.end())
432 CVSymbol sym = syms.readRecord(*iter);
433 lldbassert(sym.kind() == S_PUB32);
435 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(sym, p));
436 if (p.Segment == addr.segment && p.Offset == addr.offset)
441 clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) {
442 CVSymbol cvs = m_index.ReadSymbolRecord(id);
444 if (isLocalVariableType(cvs.kind())) {
445 clang::DeclContext *scope = GetParentDeclContext(id);
446 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
447 PdbCompilandSymId scope_id(id.modi, m_decl_to_status[scope_decl].uid);
448 return GetOrCreateVariableDecl(scope_id, id);
451 switch (cvs.kind()) {
454 return GetOrCreateFunctionDecl(id);
462 return GetOrCreateBlockDecl(id);
468 clang::Decl *PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
469 if (clang::Decl *result = TryGetDecl(uid))
472 clang::Decl *result = nullptr;
473 switch (uid.kind()) {
474 case PdbSymUidKind::CompilandSym:
475 result = GetOrCreateSymbolForId(uid.asCompilandSym());
477 case PdbSymUidKind::Type: {
478 clang::QualType qt = GetOrCreateType(uid.asTypeSym());
479 if (auto *tag = qt->getAsTagDecl()) {
488 m_uid_to_decl[toOpaqueUid(uid)] = result;
492 clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
493 if (uid.kind() == PdbSymUidKind::CompilandSym) {
494 if (uid.asCompilandSym().offset == 0)
495 return FromCompilerDeclContext(GetTranslationUnitDecl());
498 clang::Decl *decl = GetOrCreateDeclForUid(uid);
502 return clang::Decl::castToDeclContext(decl);
505 std::pair<clang::DeclContext *, std::string>
506 PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
507 MSVCUndecoratedNameParser parser(name);
508 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
510 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
512 llvm::StringRef uname = specs.back().GetBaseName();
513 specs = specs.drop_back();
515 return {context, name};
517 llvm::StringRef scope_name = specs.back().GetFullName();
519 // It might be a class name, try that first.
520 std::vector<TypeIndex> types = m_index.tpi().findRecordsByName(scope_name);
521 while (!types.empty()) {
522 clang::QualType qt = GetOrCreateType(types.back());
523 clang::TagDecl *tag = qt->getAsTagDecl();
525 return {clang::TagDecl::castToDeclContext(tag), uname};
529 // If that fails, treat it as a series of namespaces.
530 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
531 std::string ns_name = spec.GetBaseName().str();
532 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
534 return {context, uname};
538 PdbAstBuilder::GetParentDeclContextForSymbol(const CVSymbol &sym) {
539 if (!SymbolHasAddress(sym))
540 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
541 SegmentOffset addr = GetSegmentAndOffset(sym);
542 llvm::Optional<PublicSym32> pub =
543 FindPublicSym(addr, m_index.symrecords(), m_index.publics());
545 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
547 llvm::ms_demangle::Demangler demangler;
548 StringView name{pub->Name.begin(), pub->Name.size()};
549 llvm::ms_demangle::SymbolNode *node = demangler.parse(name);
551 return FromCompilerDeclContext(GetTranslationUnitDecl());
552 llvm::ArrayRef<llvm::ms_demangle::Node *> name_components{
553 node->Name->Components->Nodes, node->Name->Components->Count - 1};
555 if (!name_components.empty()) {
556 // Render the current list of scope nodes as a fully qualified name, and
557 // look it up in the debug info as a type name. If we find something,
558 // this is a type (which may itself be prefixed by a namespace). If we
559 // don't, this is a list of namespaces.
560 std::string qname = RenderScopeList(name_components);
561 std::vector<TypeIndex> matches = m_index.tpi().findRecordsByName(qname);
562 while (!matches.empty()) {
563 clang::QualType qt = GetOrCreateType(matches.back());
564 clang::TagDecl *tag = qt->getAsTagDecl();
566 return clang::TagDecl::castToDeclContext(tag);
571 // It's not a type. It must be a series of namespaces.
572 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
573 while (!name_components.empty()) {
574 std::string ns = name_components.front()->toString();
575 context = GetOrCreateNamespaceDecl(ns.c_str(), *context);
576 name_components = name_components.drop_front();
581 clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
582 // We must do this *without* calling GetOrCreate on the current uid, as
583 // that would be an infinite recursion.
584 switch (uid.kind()) {
585 case PdbSymUidKind::CompilandSym: {
586 llvm::Optional<PdbCompilandSymId> scope =
587 FindSymbolScope(m_index, uid.asCompilandSym());
589 return GetOrCreateDeclContextForUid(*scope);
591 CVSymbol sym = m_index.ReadSymbolRecord(uid.asCompilandSym());
592 return GetParentDeclContextForSymbol(sym);
594 case PdbSymUidKind::Type: {
595 // It could be a namespace, class, or global. We don't support nested
596 // functions yet. Anyway, we just need to consult the parent type map.
597 PdbTypeSymId type_id = uid.asTypeSym();
598 auto iter = m_parent_types.find(type_id.index);
599 if (iter == m_parent_types.end())
600 return FromCompilerDeclContext(GetTranslationUnitDecl());
601 return GetOrCreateDeclContextForUid(PdbTypeSymId(iter->second));
603 case PdbSymUidKind::FieldListMember:
604 // In this case the parent DeclContext is the one for the class that this
605 // member is inside of.
607 case PdbSymUidKind::GlobalSym: {
608 // If this refers to a compiland symbol, just recurse in with that symbol.
609 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
610 // need to parse the undecorated name to figure out the scope, then look
611 // that up in the TPI stream. If it's found, it's a type, othewrise it's
612 // a series of namespaces.
614 CVSymbol global = m_index.ReadSymbolRecord(uid.asGlobalSym());
615 switch (global.kind()) {
616 case SymbolKind::S_GDATA32:
617 case SymbolKind::S_LDATA32:
618 return GetParentDeclContextForSymbol(global);
619 case SymbolKind::S_PROCREF:
620 case SymbolKind::S_LPROCREF: {
621 ProcRefSym ref{global.kind()};
623 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
624 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
625 return GetParentDeclContext(cu_sym_id);
627 case SymbolKind::S_CONSTANT:
628 case SymbolKind::S_UDT:
629 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
638 return FromCompilerDeclContext(GetTranslationUnitDecl());
641 bool PdbAstBuilder::CompleteType(clang::QualType qt) {
642 clang::TagDecl *tag = qt->getAsTagDecl();
646 return CompleteTagDecl(*tag);
649 bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
650 // If this is not in our map, it's an error.
651 auto status_iter = m_decl_to_status.find(&tag);
652 lldbassert(status_iter != m_decl_to_status.end());
654 // If it's already complete, just return.
655 DeclStatus &status = status_iter->second;
659 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
661 lldbassert(IsTagRecord(type_id, m_index.tpi()));
663 clang::QualType tag_qt = m_clang.getASTContext()->getTypeDeclType(&tag);
664 ClangASTContext::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
666 TypeIndex tag_ti = type_id.index;
667 CVType cvt = m_index.tpi().getType(tag_ti);
668 if (cvt.kind() == LF_MODIFIER)
669 tag_ti = LookThroughModifierRecord(cvt);
671 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, m_index.tpi());
672 cvt = m_index.tpi().getType(best_ti.index);
673 lldbassert(IsTagRecord(cvt));
675 if (IsForwardRefUdt(cvt)) {
676 // If we can't find a full decl for this forward ref anywhere in the debug
677 // info, then we have no way to complete it.
681 TypeIndex field_list_ti = GetFieldListIndex(cvt);
682 CVType field_list_cvt = m_index.tpi().getType(field_list_ti);
683 if (field_list_cvt.kind() != LF_FIELDLIST)
686 // Visit all members of this class, then perform any finalization necessary
687 // to complete the class.
688 CompilerType ct = ToCompilerType(tag_qt);
689 UdtRecordCompleter completer(best_ti, ct, tag, *this, m_index.tpi());
691 llvm::codeview::visitMemberRecordStream(field_list_cvt.data(), completer);
692 completer.complete();
694 status.resolved = true;
698 llvm::consumeError(std::move(error));
702 clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
703 if (ti == TypeIndex::NullptrT())
704 return GetBasicType(lldb::eBasicTypeNullPtr);
706 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
707 clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
708 return m_clang.getASTContext()->getPointerType(direct_type);
711 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
714 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
715 if (bt == lldb::eBasicTypeInvalid)
718 return GetBasicType(bt);
721 clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
722 clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
724 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
725 // create in the AST.
726 if (pointee_type.isNull())
729 if (pointer.isPointerToMember()) {
730 MemberPointerInfo mpi = pointer.getMemberInfo();
731 clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
733 return m_clang.getASTContext()->getMemberPointerType(
734 pointee_type, class_type.getTypePtr());
737 clang::QualType pointer_type;
738 if (pointer.getMode() == PointerMode::LValueReference)
740 m_clang.getASTContext()->getLValueReferenceType(pointee_type);
741 else if (pointer.getMode() == PointerMode::RValueReference)
743 m_clang.getASTContext()->getRValueReferenceType(pointee_type);
745 pointer_type = m_clang.getASTContext()->getPointerType(pointee_type);
747 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
748 pointer_type.addConst();
750 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
751 pointer_type.addVolatile();
753 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
754 pointer_type.addRestrict();
760 PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
761 clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
762 if (unmodified_type.isNull())
765 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
766 unmodified_type.addConst();
767 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
768 unmodified_type.addVolatile();
770 return unmodified_type;
773 clang::QualType PdbAstBuilder::CreateRecordType(PdbTypeSymId id,
774 const TagRecord &record) {
775 clang::DeclContext *context = nullptr;
777 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
778 clang::TagTypeKind ttk = TranslateUdtKind(record);
779 lldb::AccessType access =
780 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic;
782 ClangASTMetadata metadata;
783 metadata.SetUserID(toOpaqueUid(id));
784 metadata.SetIsDynamicCXXType(false);
787 m_clang.CreateRecordType(context, access, uname.c_str(), ttk,
788 lldb::eLanguageTypeC_plus_plus, &metadata);
790 lldbassert(ct.IsValid());
792 ClangASTContext::StartTagDeclarationDefinition(ct);
794 // Even if it's possible, don't complete it at this point. Just mark it
795 // forward resolved, and if/when LLDB needs the full definition, it can
797 clang::QualType result =
798 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
800 ClangASTContext::SetHasExternalStorage(result.getAsOpaquePtr(), true);
804 clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const {
805 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
806 if (iter != m_uid_to_decl.end())
811 clang::NamespaceDecl *
812 PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name,
813 clang::DeclContext &context) {
814 return m_clang.GetUniqueNamespaceDeclaration(
815 IsAnonymousNamespaceName(name) ? nullptr : name, &context);
819 PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
820 if (clang::Decl *decl = TryGetDecl(block_id))
821 return llvm::dyn_cast<clang::BlockDecl>(decl);
823 clang::DeclContext *scope = GetParentDeclContext(block_id);
825 clang::BlockDecl *block_decl = m_clang.CreateBlockDeclaration(scope);
826 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
829 status.resolved = true;
830 status.uid = toOpaqueUid(block_id);
831 m_decl_to_status.insert({block_decl, status});
836 clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
837 clang::DeclContext &scope) {
838 VariableInfo var_info = GetVariableNameInfo(sym);
839 clang::QualType qt = GetOrCreateType(var_info.type);
841 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
842 &scope, var_info.name.str().c_str(), qt);
844 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
846 status.resolved = true;
847 status.uid = toOpaqueUid(uid);
848 m_decl_to_status.insert({var_decl, status});
853 PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id,
854 PdbCompilandSymId var_id) {
855 if (clang::Decl *decl = TryGetDecl(var_id))
856 return llvm::dyn_cast<clang::VarDecl>(decl);
858 clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
860 CVSymbol sym = m_index.ReadSymbolRecord(var_id);
861 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
864 clang::VarDecl *PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id) {
865 if (clang::Decl *decl = TryGetDecl(var_id))
866 return llvm::dyn_cast<clang::VarDecl>(decl);
868 CVSymbol sym = m_index.ReadSymbolRecord(var_id);
869 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
870 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
873 clang::TypedefNameDecl *
874 PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) {
875 if (clang::Decl *decl = TryGetDecl(id))
876 return llvm::dyn_cast<clang::TypedefNameDecl>(decl);
878 CVSymbol sym = m_index.ReadSymbolRecord(id);
879 lldbassert(sym.kind() == S_UDT);
880 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
882 clang::DeclContext *scope = GetParentDeclContext(id);
884 PdbTypeSymId real_type_id{udt.Type, false};
885 clang::QualType qt = GetOrCreateType(real_type_id);
887 std::string uname = DropNameScope(udt.Name);
889 CompilerType ct = m_clang.CreateTypedefType(ToCompilerType(qt), uname.c_str(),
890 ToCompilerDeclContext(*scope));
891 clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct);
893 status.resolved = true;
894 status.uid = toOpaqueUid(id);
895 m_decl_to_status.insert({tnd, status});
899 clang::QualType PdbAstBuilder::GetBasicType(lldb::BasicType type) {
900 CompilerType ct = m_clang.GetBasicType(type);
901 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
904 clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) {
905 if (type.index.isSimple())
906 return CreateSimpleType(type.index);
908 CVType cvt = m_index.tpi().getType(type.index);
910 if (cvt.kind() == LF_MODIFIER) {
911 ModifierRecord modifier;
913 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
914 return CreateModifierType(modifier);
917 if (cvt.kind() == LF_POINTER) {
918 PointerRecord pointer;
920 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
921 return CreatePointerType(pointer);
924 if (IsTagRecord(cvt)) {
925 CVTagRecord tag = CVTagRecord::create(cvt);
926 if (tag.kind() == CVTagRecord::Union)
927 return CreateRecordType(type.index, tag.asUnion());
928 if (tag.kind() == CVTagRecord::Enum)
929 return CreateEnumType(type.index, tag.asEnum());
930 return CreateRecordType(type.index, tag.asClass());
933 if (cvt.kind() == LF_ARRAY) {
935 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
936 return CreateArrayType(ar);
939 if (cvt.kind() == LF_PROCEDURE) {
941 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
942 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
945 if (cvt.kind() == LF_MFUNCTION) {
946 MemberFunctionRecord mfr;
948 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
949 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
955 clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
956 lldb::user_id_t uid = toOpaqueUid(type);
957 auto iter = m_uid_to_type.find(uid);
958 if (iter != m_uid_to_type.end())
961 PdbTypeSymId best_type = GetBestPossibleDecl(type, m_index.tpi());
964 if (best_type.index != type.index) {
965 // This is a forward decl. Call GetOrCreate on the full decl, then map the
966 // forward decl id to the full decl QualType.
967 clang::QualType qt = GetOrCreateType(best_type);
968 m_uid_to_type[toOpaqueUid(type)] = qt;
972 // This is either a full decl, or a forward decl with no matching full decl
973 // in the debug info.
974 qt = CreateType(type);
975 m_uid_to_type[toOpaqueUid(type)] = qt;
976 if (IsTagRecord(type, m_index.tpi())) {
977 clang::TagDecl *tag = qt->getAsTagDecl();
978 lldbassert(m_decl_to_status.count(tag) == 0);
980 DeclStatus &status = m_decl_to_status[tag];
982 status.resolved = false;
987 clang::FunctionDecl *
988 PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
989 if (clang::Decl *decl = TryGetDecl(func_id))
990 return llvm::dyn_cast<clang::FunctionDecl>(decl);
992 clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
993 std::string context_name;
994 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
995 context_name = ns->getQualifiedNameAsString();
996 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
997 context_name = tag->getQualifiedNameAsString();
1000 CVSymbol cvs = m_index.ReadSymbolRecord(func_id);
1001 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1002 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1004 PdbTypeSymId type_id(proc.FunctionType);
1005 clang::QualType qt = GetOrCreateType(type_id);
1009 clang::StorageClass storage = clang::SC_None;
1010 if (proc.Kind == SymbolRecordKind::ProcSym)
1011 storage = clang::SC_Static;
1013 const clang::FunctionProtoType *func_type =
1014 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1016 CompilerType func_ct = ToCompilerType(qt);
1018 llvm::StringRef proc_name = proc.Name;
1019 proc_name.consume_front(context_name);
1020 proc_name.consume_front("::");
1022 clang::FunctionDecl *function_decl = m_clang.CreateFunctionDeclaration(
1023 parent, proc_name.str().c_str(), func_ct, storage, false);
1025 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1026 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1028 status.resolved = true;
1029 status.uid = toOpaqueUid(func_id);
1030 m_decl_to_status.insert({function_decl, status});
1032 CreateFunctionParameters(func_id, *function_decl, func_type->getNumParams());
1034 return function_decl;
1037 void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id,
1038 clang::FunctionDecl &function_decl,
1039 uint32_t param_count) {
1040 CompilandIndexItem *cii = m_index.compilands().GetCompiland(func_id.modi);
1041 CVSymbolArray scope =
1042 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1044 auto begin = scope.begin();
1045 auto end = scope.end();
1046 std::vector<clang::ParmVarDecl *> params;
1047 while (begin != end && param_count > 0) {
1048 uint32_t record_offset = begin.offset();
1049 CVSymbol sym = *begin++;
1051 TypeIndex param_type;
1052 llvm::StringRef param_name;
1053 switch (sym.kind()) {
1055 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1056 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1057 param_type = reg.Type;
1058 param_name = reg.Name;
1062 RegisterSym reg(SymbolRecordKind::RegisterSym);
1063 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1064 param_type = reg.Index;
1065 param_name = reg.Name;
1069 LocalSym local(SymbolRecordKind::LocalSym);
1070 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1071 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1073 param_type = local.Type;
1074 param_name = local.Name;
1078 // All parameters should come before the first block. If that isn't the
1079 // case, then perhaps this is bad debug info that doesn't contain
1080 // information about all parameters.
1086 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1087 clang::QualType qt = GetOrCreateType(param_type);
1089 CompilerType param_type_ct(&m_clang, qt.getAsOpaquePtr());
1090 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1091 &function_decl, param_name.str().c_str(), param_type_ct,
1093 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1095 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1096 params.push_back(param);
1100 if (!params.empty())
1101 m_clang.SetFunctionParameters(&function_decl, params.data(), params.size());
1104 clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
1105 const EnumRecord &er) {
1106 clang::DeclContext *decl_context = nullptr;
1108 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1109 clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1111 Declaration declaration;
1112 CompilerType enum_ct = m_clang.CreateEnumerationType(
1113 uname.c_str(), decl_context, declaration, ToCompilerType(underlying_type),
1116 ClangASTContext::StartTagDeclarationDefinition(enum_ct);
1117 ClangASTContext::SetHasExternalStorage(enum_ct.GetOpaqueQualType(), true);
1119 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1122 clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1123 clang::QualType element_type = GetOrCreateType(ar.ElementType);
1125 uint64_t element_count =
1126 ar.Size / GetSizeOfType({ar.ElementType}, m_index.tpi());
1128 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1129 element_count, false);
1130 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1133 clang::QualType PdbAstBuilder::CreateFunctionType(
1134 TypeIndex args_type_idx, TypeIndex return_type_idx,
1135 llvm::codeview::CallingConvention calling_convention) {
1136 TpiStream &stream = m_index.tpi();
1137 CVType args_cvt = stream.getType(args_type_idx);
1140 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1142 llvm::ArrayRef<TypeIndex> arg_indices = llvm::makeArrayRef(args.ArgIndices);
1143 bool is_variadic = IsCVarArgsFunction(arg_indices);
1145 arg_indices = arg_indices.drop_back();
1147 std::vector<CompilerType> arg_types;
1148 arg_types.reserve(arg_indices.size());
1150 for (TypeIndex arg_index : arg_indices) {
1151 clang::QualType arg_type = GetOrCreateType(arg_index);
1152 arg_types.push_back(ToCompilerType(arg_type));
1155 clang::QualType return_type = GetOrCreateType(return_type_idx);
1157 llvm::Optional<clang::CallingConv> cc =
1158 TranslateCallingConvention(calling_convention);
1162 CompilerType return_ct = ToCompilerType(return_type);
1163 CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1164 return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc);
1166 return clang::QualType::getFromOpaquePtr(
1167 func_sig_ast_type.GetOpaqueQualType());
1170 static bool isTagDecl(clang::DeclContext &context) {
1171 return !!llvm::dyn_cast<clang::TagDecl>(&context);
1174 static bool isFunctionDecl(clang::DeclContext &context) {
1175 return !!llvm::dyn_cast<clang::FunctionDecl>(&context);
1178 static bool isBlockDecl(clang::DeclContext &context) {
1179 return !!llvm::dyn_cast<clang::BlockDecl>(&context);
1182 void PdbAstBuilder::ParseAllNamespacesPlusChildrenOf(
1183 llvm::Optional<llvm::StringRef> parent) {
1184 TypeIndex ti{m_index.tpi().TypeIndexBegin()};
1185 for (const CVType &cvt : m_index.tpi().typeArray()) {
1186 PdbTypeSymId tid{ti};
1189 if (!IsTagRecord(cvt))
1192 CVTagRecord tag = CVTagRecord::create(cvt);
1194 if (!parent.hasValue()) {
1195 clang::QualType qt = GetOrCreateType(tid);
1200 // Call CreateDeclInfoForType unconditionally so that the namespace info
1201 // gets created. But only call CreateRecordType if the namespace name
1203 clang::DeclContext *context = nullptr;
1205 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1206 if (!context->isNamespace())
1209 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(context);
1210 std::string actual_ns = ns->getQualifiedNameAsString();
1211 if (llvm::StringRef(actual_ns).startswith(*parent)) {
1212 clang::QualType qt = GetOrCreateType(tid);
1218 uint32_t module_count = m_index.dbi().modules().getModuleCount();
1219 for (uint16_t modi = 0; modi < module_count; ++modi) {
1220 CompilandIndexItem &cii = m_index.compilands().GetOrCreateCompiland(modi);
1221 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1222 auto iter = symbols.begin();
1223 while (iter != symbols.end()) {
1224 PdbCompilandSymId sym_id{modi, iter.offset()};
1226 switch (iter->kind()) {
1229 GetOrCreateFunctionDecl(sym_id);
1230 iter = symbols.at(getScopeEndOffset(*iter));
1236 GetOrCreateVariableDecl(PdbCompilandSymId(modi, 0), sym_id);
1247 static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1248 const CVSymbolArray &symbols) {
1249 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1252 unsigned int params = func_decl->getNumParams();
1256 CVSymbolArray result = symbols;
1258 while (!result.empty()) {
1262 CVSymbol sym = *result.begin();
1263 result.drop_front();
1265 if (!isLocalVariableType(sym.kind()))
1273 void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id) {
1274 CVSymbol sym = m_index.ReadSymbolRecord(block_id);
1275 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1276 sym.kind() == S_BLOCK32);
1277 CompilandIndexItem &cii =
1278 m_index.compilands().GetOrCreateCompiland(block_id.modi);
1279 CVSymbolArray symbols =
1280 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1282 // Function parameters should already have been created when the function was
1284 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1286 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1288 auto begin = symbols.begin();
1289 while (begin != symbols.end()) {
1290 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1291 GetOrCreateSymbolForId(child_sym_id);
1292 if (begin->kind() == S_BLOCK32) {
1293 ParseBlockChildren(child_sym_id);
1294 begin = symbols.at(getScopeEndOffset(*begin));
1300 void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
1302 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1305 auto iter = m_decl_to_status.find(decl);
1306 lldbassert(iter != m_decl_to_status.end());
1308 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1309 CompleteTagDecl(*tag);
1313 if (isFunctionDecl(context) || isBlockDecl(context)) {
1314 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1315 ParseBlockChildren(block_id);
1319 void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1320 // Namespaces aren't explicitly represented in the debug info, and the only
1321 // way to parse them is to parse all type info, demangling every single type
1322 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1323 // is an expensive operation, we have to special case it so that we do other
1324 // work (such as parsing the items that appear within the namespaces) at the
1326 if (context.isTranslationUnit()) {
1327 ParseAllNamespacesPlusChildrenOf(llvm::None);
1331 if (context.isNamespace()) {
1332 clang::NamespaceDecl &ns = *llvm::dyn_cast<clang::NamespaceDecl>(&context);
1333 std::string qname = ns.getQualifiedNameAsString();
1334 ParseAllNamespacesPlusChildrenOf(llvm::StringRef{qname});
1338 if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1339 ParseDeclsForSimpleContext(context);
1344 CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) {
1345 return {&m_clang, &decl};
1348 CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) {
1349 return {&m_clang, qt.getAsOpaquePtr()};
1353 PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) {
1354 return {&m_clang, &context};
1357 clang::Decl * PdbAstBuilder::FromCompilerDecl(CompilerDecl decl) {
1358 return static_cast<clang::Decl *>(decl.GetOpaqueDecl());
1361 clang::DeclContext *
1362 PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
1363 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1366 void PdbAstBuilder::Dump(Stream &stream) { m_clang.Dump(stream); }