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 llvm::Optional<clang::CallingConv>
125 TranslateCallingConvention(llvm::codeview::CallingConvention conv) {
126 using CC = llvm::codeview::CallingConvention;
131 return clang::CallingConv::CC_C;
134 return clang::CallingConv::CC_X86Pascal;
137 return clang::CallingConv::CC_X86FastCall;
138 case CC::NearStdCall:
140 return clang::CallingConv::CC_X86StdCall;
142 return clang::CallingConv::CC_X86ThisCall;
144 return clang::CallingConv::CC_X86VectorCall;
150 static llvm::Optional<CVTagRecord>
151 GetNestedTagDefinition(const NestedTypeRecord &Record,
152 const CVTagRecord &parent, TpiStream &tpi) {
153 // An LF_NESTTYPE is essentially a nested typedef / using declaration, but it
154 // is also used to indicate the primary definition of a nested class. That is
155 // to say, if you have:
160 // Then in the debug info, this will appear as:
161 // LF_STRUCTURE `A::B` [type index = N]
163 // LF_NESTTYPE [name = `B`, index = N]
164 // LF_NESTTYPE [name = `C`, index = N]
165 // In order to accurately reconstruct the decl context hierarchy, we need to
166 // know which ones are actual definitions and which ones are just aliases.
168 // If it's a simple type, then this is something like `using foo = int`.
169 if (Record.Type.isSimple())
172 CVType cvt = tpi.getType(Record.Type);
174 if (!IsTagRecord(cvt))
177 // If it's an inner definition, then treat whatever name we have here as a
178 // single component of a mangled name. So we can inject it into the parent's
179 // mangled name to see if it matches.
180 CVTagRecord child = CVTagRecord::create(cvt);
181 std::string qname = parent.asTag().getUniqueName();
182 if (qname.size() < 4 || child.asTag().getUniqueName().size() < 4)
185 // qname[3] is the tag type identifier (struct, class, union, etc). Since the
186 // inner tag type is not necessarily the same as the outer tag type, re-write
187 // it to match the inner tag type.
188 qname[3] = child.asTag().getUniqueName()[3];
192 piece += Record.Name;
193 piece.push_back('@');
194 qname.insert(4, std::move(piece));
195 if (qname != child.asTag().UniqueName)
198 return std::move(child);
201 static bool IsAnonymousNamespaceName(llvm::StringRef name) {
202 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
205 PdbAstBuilder::PdbAstBuilder(ObjectFile &obj, PdbIndex &index, ClangASTContext &clang)
206 : m_index(index), m_clang(clang) {
210 lldb_private::CompilerDeclContext PdbAstBuilder::GetTranslationUnitDecl() {
211 return ToCompilerDeclContext(*m_clang.GetTranslationUnitDecl());
214 std::pair<clang::DeclContext *, std::string>
215 PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
216 // FIXME: Move this to GetDeclContextContainingUID.
217 if (!record.hasUniqueName())
218 return CreateDeclInfoForUndecoratedName(record.Name);
220 llvm::ms_demangle::Demangler demangler;
221 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
222 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
224 return {m_clang.GetTranslationUnitDecl(), record.UniqueName};
226 llvm::ms_demangle::IdentifierNode *idn =
227 ttn->QualifiedName->getUnqualifiedIdentifier();
228 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
230 llvm::ms_demangle::NodeArrayNode *name_components =
231 ttn->QualifiedName->Components;
232 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
233 name_components->Count - 1);
235 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
237 // If this type doesn't have a parent type in the debug info, then the best we
238 // can do is to say that it's either a series of namespaces (if the scope is
239 // non-empty), or the translation unit (if the scope is empty).
240 auto parent_iter = m_parent_types.find(ti);
241 if (parent_iter == m_parent_types.end()) {
243 return {context, uname};
245 // If there is no parent in the debug info, but some of the scopes have
246 // template params, then this is a case of bad debug info. See, for
247 // example, llvm.org/pr39607. We don't want to create an ambiguity between
248 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
249 // global scope with the fully qualified name.
250 if (AnyScopesHaveTemplateParams(scopes))
251 return {context, record.Name};
253 for (llvm::ms_demangle::Node *scope : scopes) {
254 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
255 std::string str = nii->toString();
256 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
258 return {context, uname};
261 // Otherwise, all we need to do is get the parent type of this type and
262 // recurse into our lazy type creation / AST reconstruction logic to get an
263 // LLDB TypeSP for the parent. This will cause the AST to automatically get
264 // the right DeclContext created for any parent.
265 clang::QualType parent_qt = GetOrCreateType(parent_iter->second);
267 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
268 return {context, uname};
271 void PdbAstBuilder::BuildParentMap() {
272 LazyRandomTypeCollection &types = m_index.tpi().typeCollection();
274 llvm::DenseMap<TypeIndex, TypeIndex> forward_to_full;
275 llvm::DenseMap<TypeIndex, TypeIndex> full_to_forward;
277 struct RecordIndices {
282 llvm::StringMap<RecordIndices> record_indices;
284 for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) {
285 CVType type = types.getType(*ti);
286 if (!IsTagRecord(type))
289 CVTagRecord tag = CVTagRecord::create(type);
291 RecordIndices &indices = record_indices[tag.asTag().getUniqueName()];
292 if (tag.asTag().isForwardRef())
293 indices.forward = *ti;
297 if (indices.full != TypeIndex::None() &&
298 indices.forward != TypeIndex::None()) {
299 forward_to_full[indices.forward] = indices.full;
300 full_to_forward[indices.full] = indices.forward;
303 // We're looking for LF_NESTTYPE records in the field list, so ignore
304 // forward references (no field list), and anything without a nested class
305 // (since there won't be any LF_NESTTYPE records).
306 if (tag.asTag().isForwardRef() || !tag.asTag().containsNestedClass())
309 struct ProcessTpiStream : public TypeVisitorCallbacks {
310 ProcessTpiStream(PdbIndex &index, TypeIndex parent,
311 const CVTagRecord &parent_cvt,
312 llvm::DenseMap<TypeIndex, TypeIndex> &parents)
313 : index(index), parents(parents), parent(parent),
314 parent_cvt(parent_cvt) {}
317 llvm::DenseMap<TypeIndex, TypeIndex> &parents;
319 unsigned unnamed_type_index = 1;
321 const CVTagRecord &parent_cvt;
323 llvm::Error visitKnownMember(CVMemberRecord &CVR,
324 NestedTypeRecord &Record) override {
325 std::string unnamed_type_name;
326 if (Record.Name.empty()) {
328 llvm::formatv("<unnamed-type-$S{0}>", unnamed_type_index).str();
329 Record.Name = unnamed_type_name;
330 ++unnamed_type_index;
332 llvm::Optional<CVTagRecord> tag =
333 GetNestedTagDefinition(Record, parent_cvt, index.tpi());
335 return llvm::ErrorSuccess();
337 parents[Record.Type] = parent;
338 return llvm::ErrorSuccess();
342 CVType field_list = m_index.tpi().getType(tag.asTag().FieldList);
343 ProcessTpiStream process(m_index, *ti, tag, m_parent_types);
344 llvm::Error error = visitMemberRecordStream(field_list.data(), process);
346 llvm::consumeError(std::move(error));
349 // Now that we know the forward -> full mapping of all type indices, we can
350 // re-write all the indices. At the end of this process, we want a mapping
351 // consisting of fwd -> full and full -> full for all child -> parent indices.
352 // We can re-write the values in place, but for the keys, we must save them
353 // off so that we don't modify the map in place while also iterating it.
354 std::vector<TypeIndex> full_keys;
355 std::vector<TypeIndex> fwd_keys;
356 for (auto &entry : m_parent_types) {
357 TypeIndex key = entry.first;
358 TypeIndex value = entry.second;
360 auto iter = forward_to_full.find(value);
361 if (iter != forward_to_full.end())
362 entry.second = iter->second;
364 iter = forward_to_full.find(key);
365 if (iter != forward_to_full.end())
366 fwd_keys.push_back(key);
368 full_keys.push_back(key);
370 for (TypeIndex fwd : fwd_keys) {
371 TypeIndex full = forward_to_full[fwd];
372 m_parent_types[full] = m_parent_types[fwd];
374 for (TypeIndex full : full_keys) {
375 TypeIndex fwd = full_to_forward[full];
376 m_parent_types[fwd] = m_parent_types[full];
382 static bool isLocalVariableType(SymbolKind K) {
395 RenderScopeList(llvm::ArrayRef<llvm::ms_demangle::Node *> nodes) {
396 lldbassert(!nodes.empty());
398 std::string result = nodes.front()->toString();
399 nodes = nodes.drop_front();
400 while (!nodes.empty()) {
402 result += nodes.front()->toString(llvm::ms_demangle::OF_NoTagSpecifier);
403 nodes = nodes.drop_front();
408 static llvm::Optional<PublicSym32> FindPublicSym(const SegmentOffset &addr,
410 PublicsStream &publics) {
411 llvm::FixedStreamArray<ulittle32_t> addr_map = publics.getAddressMap();
412 auto iter = std::lower_bound(
413 addr_map.begin(), addr_map.end(), addr,
414 [&](const ulittle32_t &x, const SegmentOffset &y) {
415 CVSymbol s1 = syms.readRecord(x);
416 lldbassert(s1.kind() == S_PUB32);
418 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(s1, p1));
419 if (p1.Segment < y.segment)
421 return p1.Offset < y.offset;
423 if (iter == addr_map.end())
425 CVSymbol sym = syms.readRecord(*iter);
426 lldbassert(sym.kind() == S_PUB32);
428 llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(sym, p));
429 if (p.Segment == addr.segment && p.Offset == addr.offset)
434 clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) {
435 CVSymbol cvs = m_index.ReadSymbolRecord(id);
437 if (isLocalVariableType(cvs.kind())) {
438 clang::DeclContext *scope = GetParentDeclContext(id);
439 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
440 PdbCompilandSymId scope_id(id.modi, m_decl_to_status[scope_decl].uid);
441 return GetOrCreateVariableDecl(scope_id, id);
444 switch (cvs.kind()) {
447 return GetOrCreateFunctionDecl(id);
455 return GetOrCreateBlockDecl(id);
461 llvm::Optional<CompilerDecl> PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
462 if (clang::Decl *result = TryGetDecl(uid))
463 return ToCompilerDecl(*result);
465 clang::Decl *result = nullptr;
466 switch (uid.kind()) {
467 case PdbSymUidKind::CompilandSym:
468 result = GetOrCreateSymbolForId(uid.asCompilandSym());
470 case PdbSymUidKind::Type: {
471 clang::QualType qt = GetOrCreateType(uid.asTypeSym());
472 if (auto *tag = qt->getAsTagDecl()) {
481 m_uid_to_decl[toOpaqueUid(uid)] = result;
482 return ToCompilerDecl(*result);
485 clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
486 if (uid.kind() == PdbSymUidKind::CompilandSym) {
487 if (uid.asCompilandSym().offset == 0)
488 return FromCompilerDeclContext(GetTranslationUnitDecl());
490 auto option = GetOrCreateDeclForUid(uid);
493 clang::Decl *decl = FromCompilerDecl(option.getValue());
497 return clang::Decl::castToDeclContext(decl);
500 std::pair<clang::DeclContext *, std::string>
501 PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
502 MSVCUndecoratedNameParser parser(name);
503 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
505 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
507 llvm::StringRef uname = specs.back().GetBaseName();
508 specs = specs.drop_back();
510 return {context, name};
512 llvm::StringRef scope_name = specs.back().GetFullName();
514 // It might be a class name, try that first.
515 std::vector<TypeIndex> types = m_index.tpi().findRecordsByName(scope_name);
516 while (!types.empty()) {
517 clang::QualType qt = GetOrCreateType(types.back());
518 clang::TagDecl *tag = qt->getAsTagDecl();
520 return {clang::TagDecl::castToDeclContext(tag), uname};
524 // If that fails, treat it as a series of namespaces.
525 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
526 std::string ns_name = spec.GetBaseName().str();
527 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
529 return {context, uname};
533 PdbAstBuilder::GetParentDeclContextForSymbol(const CVSymbol &sym) {
534 if (!SymbolHasAddress(sym))
535 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
536 SegmentOffset addr = GetSegmentAndOffset(sym);
537 llvm::Optional<PublicSym32> pub =
538 FindPublicSym(addr, m_index.symrecords(), m_index.publics());
540 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
542 llvm::ms_demangle::Demangler demangler;
543 StringView name{pub->Name.begin(), pub->Name.size()};
544 llvm::ms_demangle::SymbolNode *node = demangler.parse(name);
546 return FromCompilerDeclContext(GetTranslationUnitDecl());
547 llvm::ArrayRef<llvm::ms_demangle::Node *> name_components{
548 node->Name->Components->Nodes, node->Name->Components->Count - 1};
550 if (!name_components.empty()) {
551 // Render the current list of scope nodes as a fully qualified name, and
552 // look it up in the debug info as a type name. If we find something,
553 // this is a type (which may itself be prefixed by a namespace). If we
554 // don't, this is a list of namespaces.
555 std::string qname = RenderScopeList(name_components);
556 std::vector<TypeIndex> matches = m_index.tpi().findRecordsByName(qname);
557 while (!matches.empty()) {
558 clang::QualType qt = GetOrCreateType(matches.back());
559 clang::TagDecl *tag = qt->getAsTagDecl();
561 return clang::TagDecl::castToDeclContext(tag);
566 // It's not a type. It must be a series of namespaces.
567 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
568 while (!name_components.empty()) {
569 std::string ns = name_components.front()->toString();
570 context = GetOrCreateNamespaceDecl(ns.c_str(), *context);
571 name_components = name_components.drop_front();
576 clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
577 // We must do this *without* calling GetOrCreate on the current uid, as
578 // that would be an infinite recursion.
579 switch (uid.kind()) {
580 case PdbSymUidKind::CompilandSym: {
581 llvm::Optional<PdbCompilandSymId> scope =
582 FindSymbolScope(m_index, uid.asCompilandSym());
584 return GetOrCreateDeclContextForUid(*scope);
586 CVSymbol sym = m_index.ReadSymbolRecord(uid.asCompilandSym());
587 return GetParentDeclContextForSymbol(sym);
589 case PdbSymUidKind::Type: {
590 // It could be a namespace, class, or global. We don't support nested
591 // functions yet. Anyway, we just need to consult the parent type map.
592 PdbTypeSymId type_id = uid.asTypeSym();
593 auto iter = m_parent_types.find(type_id.index);
594 if (iter == m_parent_types.end())
595 return FromCompilerDeclContext(GetTranslationUnitDecl());
596 return GetOrCreateDeclContextForUid(PdbTypeSymId(iter->second));
598 case PdbSymUidKind::FieldListMember:
599 // In this case the parent DeclContext is the one for the class that this
600 // member is inside of.
602 case PdbSymUidKind::GlobalSym: {
603 // If this refers to a compiland symbol, just recurse in with that symbol.
604 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
605 // need to parse the undecorated name to figure out the scope, then look
606 // that up in the TPI stream. If it's found, it's a type, othewrise it's
607 // a series of namespaces.
609 CVSymbol global = m_index.ReadSymbolRecord(uid.asGlobalSym());
610 switch (global.kind()) {
611 case SymbolKind::S_GDATA32:
612 case SymbolKind::S_LDATA32:
613 return GetParentDeclContextForSymbol(global);
614 case SymbolKind::S_PROCREF:
615 case SymbolKind::S_LPROCREF: {
616 ProcRefSym ref{global.kind()};
618 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
619 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
620 return GetParentDeclContext(cu_sym_id);
622 case SymbolKind::S_CONSTANT:
623 case SymbolKind::S_UDT:
624 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
633 return FromCompilerDeclContext(GetTranslationUnitDecl());
636 bool PdbAstBuilder::CompleteType(clang::QualType qt) {
637 clang::TagDecl *tag = qt->getAsTagDecl();
641 return CompleteTagDecl(*tag);
644 bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
645 // If this is not in our map, it's an error.
646 auto status_iter = m_decl_to_status.find(&tag);
647 lldbassert(status_iter != m_decl_to_status.end());
649 // If it's already complete, just return.
650 DeclStatus &status = status_iter->second;
654 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
656 lldbassert(IsTagRecord(type_id, m_index.tpi()));
658 clang::QualType tag_qt = m_clang.getASTContext()->getTypeDeclType(&tag);
659 ClangASTContext::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
661 TypeIndex tag_ti = type_id.index;
662 CVType cvt = m_index.tpi().getType(tag_ti);
663 if (cvt.kind() == LF_MODIFIER)
664 tag_ti = LookThroughModifierRecord(cvt);
666 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, m_index.tpi());
667 cvt = m_index.tpi().getType(best_ti.index);
668 lldbassert(IsTagRecord(cvt));
670 if (IsForwardRefUdt(cvt)) {
671 // If we can't find a full decl for this forward ref anywhere in the debug
672 // info, then we have no way to complete it.
676 TypeIndex field_list_ti = GetFieldListIndex(cvt);
677 CVType field_list_cvt = m_index.tpi().getType(field_list_ti);
678 if (field_list_cvt.kind() != LF_FIELDLIST)
681 // Visit all members of this class, then perform any finalization necessary
682 // to complete the class.
683 CompilerType ct = ToCompilerType(tag_qt);
684 UdtRecordCompleter completer(best_ti, ct, tag, *this, m_index.tpi());
686 llvm::codeview::visitMemberRecordStream(field_list_cvt.data(), completer);
687 completer.complete();
689 status.resolved = true;
693 llvm::consumeError(std::move(error));
697 clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
698 if (ti == TypeIndex::NullptrT())
699 return GetBasicType(lldb::eBasicTypeNullPtr);
701 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
702 clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
703 return m_clang.getASTContext()->getPointerType(direct_type);
706 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
709 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
710 if (bt == lldb::eBasicTypeInvalid)
713 return GetBasicType(bt);
716 clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
717 clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
719 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
720 // create in the AST.
721 if (pointee_type.isNull())
724 if (pointer.isPointerToMember()) {
725 MemberPointerInfo mpi = pointer.getMemberInfo();
726 clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
728 return m_clang.getASTContext()->getMemberPointerType(
729 pointee_type, class_type.getTypePtr());
732 clang::QualType pointer_type;
733 if (pointer.getMode() == PointerMode::LValueReference)
735 m_clang.getASTContext()->getLValueReferenceType(pointee_type);
736 else if (pointer.getMode() == PointerMode::RValueReference)
738 m_clang.getASTContext()->getRValueReferenceType(pointee_type);
740 pointer_type = m_clang.getASTContext()->getPointerType(pointee_type);
742 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
743 pointer_type.addConst();
745 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
746 pointer_type.addVolatile();
748 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
749 pointer_type.addRestrict();
755 PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
756 clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
757 if (unmodified_type.isNull())
760 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
761 unmodified_type.addConst();
762 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
763 unmodified_type.addVolatile();
765 return unmodified_type;
768 clang::QualType PdbAstBuilder::CreateRecordType(PdbTypeSymId id,
769 const TagRecord &record) {
770 clang::DeclContext *context = nullptr;
772 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
773 clang::TagTypeKind ttk = TranslateUdtKind(record);
774 lldb::AccessType access =
775 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic;
777 ClangASTMetadata metadata;
778 metadata.SetUserID(toOpaqueUid(id));
779 metadata.SetIsDynamicCXXType(false);
782 m_clang.CreateRecordType(context, access, uname.c_str(), ttk,
783 lldb::eLanguageTypeC_plus_plus, &metadata);
785 lldbassert(ct.IsValid());
787 ClangASTContext::StartTagDeclarationDefinition(ct);
789 // Even if it's possible, don't complete it at this point. Just mark it
790 // forward resolved, and if/when LLDB needs the full definition, it can
792 clang::QualType result =
793 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
795 ClangASTContext::SetHasExternalStorage(result.getAsOpaquePtr(), true);
799 clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const {
800 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
801 if (iter != m_uid_to_decl.end())
806 clang::NamespaceDecl *
807 PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name,
808 clang::DeclContext &context) {
809 return m_clang.GetUniqueNamespaceDeclaration(
810 IsAnonymousNamespaceName(name) ? nullptr : name, &context);
814 PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
815 if (clang::Decl *decl = TryGetDecl(block_id))
816 return llvm::dyn_cast<clang::BlockDecl>(decl);
818 clang::DeclContext *scope = GetParentDeclContext(block_id);
820 clang::BlockDecl *block_decl = m_clang.CreateBlockDeclaration(scope);
821 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
824 status.resolved = true;
825 status.uid = toOpaqueUid(block_id);
826 m_decl_to_status.insert({block_decl, status});
831 clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
832 clang::DeclContext &scope) {
833 VariableInfo var_info = GetVariableNameInfo(sym);
834 clang::QualType qt = GetOrCreateType(var_info.type);
836 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
837 &scope, var_info.name.str().c_str(), qt);
839 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
841 status.resolved = true;
842 status.uid = toOpaqueUid(uid);
843 m_decl_to_status.insert({var_decl, status});
848 PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id,
849 PdbCompilandSymId var_id) {
850 if (clang::Decl *decl = TryGetDecl(var_id))
851 return llvm::dyn_cast<clang::VarDecl>(decl);
853 clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
855 CVSymbol sym = m_index.ReadSymbolRecord(var_id);
856 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
859 clang::VarDecl *PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id) {
860 if (clang::Decl *decl = TryGetDecl(var_id))
861 return llvm::dyn_cast<clang::VarDecl>(decl);
863 CVSymbol sym = m_index.ReadSymbolRecord(var_id);
864 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
865 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
868 clang::TypedefNameDecl *
869 PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) {
870 if (clang::Decl *decl = TryGetDecl(id))
871 return llvm::dyn_cast<clang::TypedefNameDecl>(decl);
873 CVSymbol sym = m_index.ReadSymbolRecord(id);
874 lldbassert(sym.kind() == S_UDT);
875 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
877 clang::DeclContext *scope = GetParentDeclContext(id);
879 PdbTypeSymId real_type_id{udt.Type, false};
880 clang::QualType qt = GetOrCreateType(real_type_id);
882 std::string uname = DropNameScope(udt.Name);
884 CompilerType ct = m_clang.CreateTypedefType(ToCompilerType(qt), uname.c_str(),
885 ToCompilerDeclContext(*scope));
886 clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct);
888 status.resolved = true;
889 status.uid = toOpaqueUid(id);
890 m_decl_to_status.insert({tnd, status});
894 clang::QualType PdbAstBuilder::GetBasicType(lldb::BasicType type) {
895 CompilerType ct = m_clang.GetBasicType(type);
896 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
899 clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) {
900 if (type.index.isSimple())
901 return CreateSimpleType(type.index);
903 CVType cvt = m_index.tpi().getType(type.index);
905 if (cvt.kind() == LF_MODIFIER) {
906 ModifierRecord modifier;
908 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
909 return CreateModifierType(modifier);
912 if (cvt.kind() == LF_POINTER) {
913 PointerRecord pointer;
915 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
916 return CreatePointerType(pointer);
919 if (IsTagRecord(cvt)) {
920 CVTagRecord tag = CVTagRecord::create(cvt);
921 if (tag.kind() == CVTagRecord::Union)
922 return CreateRecordType(type.index, tag.asUnion());
923 if (tag.kind() == CVTagRecord::Enum)
924 return CreateEnumType(type.index, tag.asEnum());
925 return CreateRecordType(type.index, tag.asClass());
928 if (cvt.kind() == LF_ARRAY) {
930 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
931 return CreateArrayType(ar);
934 if (cvt.kind() == LF_PROCEDURE) {
936 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
937 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
940 if (cvt.kind() == LF_MFUNCTION) {
941 MemberFunctionRecord mfr;
943 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
944 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
950 clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
951 lldb::user_id_t uid = toOpaqueUid(type);
952 auto iter = m_uid_to_type.find(uid);
953 if (iter != m_uid_to_type.end())
956 PdbTypeSymId best_type = GetBestPossibleDecl(type, m_index.tpi());
959 if (best_type.index != type.index) {
960 // This is a forward decl. Call GetOrCreate on the full decl, then map the
961 // forward decl id to the full decl QualType.
962 clang::QualType qt = GetOrCreateType(best_type);
963 m_uid_to_type[toOpaqueUid(type)] = qt;
967 // This is either a full decl, or a forward decl with no matching full decl
968 // in the debug info.
969 qt = CreateType(type);
970 m_uid_to_type[toOpaqueUid(type)] = qt;
971 if (IsTagRecord(type, m_index.tpi())) {
972 clang::TagDecl *tag = qt->getAsTagDecl();
973 lldbassert(m_decl_to_status.count(tag) == 0);
975 DeclStatus &status = m_decl_to_status[tag];
977 status.resolved = false;
982 clang::FunctionDecl *
983 PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
984 if (clang::Decl *decl = TryGetDecl(func_id))
985 return llvm::dyn_cast<clang::FunctionDecl>(decl);
987 clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
988 std::string context_name;
989 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
990 context_name = ns->getQualifiedNameAsString();
991 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
992 context_name = tag->getQualifiedNameAsString();
995 CVSymbol cvs = m_index.ReadSymbolRecord(func_id);
996 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
997 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
999 PdbTypeSymId type_id(proc.FunctionType);
1000 clang::QualType qt = GetOrCreateType(type_id);
1004 clang::StorageClass storage = clang::SC_None;
1005 if (proc.Kind == SymbolRecordKind::ProcSym)
1006 storage = clang::SC_Static;
1008 const clang::FunctionProtoType *func_type =
1009 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1011 CompilerType func_ct = ToCompilerType(qt);
1013 llvm::StringRef proc_name = proc.Name;
1014 proc_name.consume_front(context_name);
1015 proc_name.consume_front("::");
1017 clang::FunctionDecl *function_decl = m_clang.CreateFunctionDeclaration(
1018 parent, proc_name.str().c_str(), func_ct, storage, false);
1020 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1021 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1023 status.resolved = true;
1024 status.uid = toOpaqueUid(func_id);
1025 m_decl_to_status.insert({function_decl, status});
1027 CreateFunctionParameters(func_id, *function_decl, func_type->getNumParams());
1029 return function_decl;
1032 void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id,
1033 clang::FunctionDecl &function_decl,
1034 uint32_t param_count) {
1035 CompilandIndexItem *cii = m_index.compilands().GetCompiland(func_id.modi);
1036 CVSymbolArray scope =
1037 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1039 auto begin = scope.begin();
1040 auto end = scope.end();
1041 std::vector<clang::ParmVarDecl *> params;
1042 while (begin != end && param_count > 0) {
1043 uint32_t record_offset = begin.offset();
1044 CVSymbol sym = *begin++;
1046 TypeIndex param_type;
1047 llvm::StringRef param_name;
1048 switch (sym.kind()) {
1050 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1051 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1052 param_type = reg.Type;
1053 param_name = reg.Name;
1057 RegisterSym reg(SymbolRecordKind::RegisterSym);
1058 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1059 param_type = reg.Index;
1060 param_name = reg.Name;
1064 LocalSym local(SymbolRecordKind::LocalSym);
1065 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1066 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1068 param_type = local.Type;
1069 param_name = local.Name;
1073 // All parameters should come before the first block. If that isn't the
1074 // case, then perhaps this is bad debug info that doesn't contain
1075 // information about all parameters.
1081 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1082 clang::QualType qt = GetOrCreateType(param_type);
1084 CompilerType param_type_ct(&m_clang, qt.getAsOpaquePtr());
1085 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1086 &function_decl, param_name.str().c_str(), param_type_ct,
1087 clang::SC_None, true);
1088 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1090 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1091 params.push_back(param);
1095 if (!params.empty())
1096 m_clang.SetFunctionParameters(&function_decl, params.data(), params.size());
1099 clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
1100 const EnumRecord &er) {
1101 clang::DeclContext *decl_context = nullptr;
1103 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1104 clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1106 Declaration declaration;
1107 CompilerType enum_ct = m_clang.CreateEnumerationType(
1108 uname.c_str(), decl_context, declaration, ToCompilerType(underlying_type),
1111 ClangASTContext::StartTagDeclarationDefinition(enum_ct);
1112 ClangASTContext::SetHasExternalStorage(enum_ct.GetOpaqueQualType(), true);
1114 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1117 clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1118 clang::QualType element_type = GetOrCreateType(ar.ElementType);
1120 uint64_t element_count =
1121 ar.Size / GetSizeOfType({ar.ElementType}, m_index.tpi());
1123 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1124 element_count, false);
1125 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1128 clang::QualType PdbAstBuilder::CreateFunctionType(
1129 TypeIndex args_type_idx, TypeIndex return_type_idx,
1130 llvm::codeview::CallingConvention calling_convention) {
1131 TpiStream &stream = m_index.tpi();
1132 CVType args_cvt = stream.getType(args_type_idx);
1135 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1137 llvm::ArrayRef<TypeIndex> arg_indices = llvm::makeArrayRef(args.ArgIndices);
1138 bool is_variadic = IsCVarArgsFunction(arg_indices);
1140 arg_indices = arg_indices.drop_back();
1142 std::vector<CompilerType> arg_types;
1143 arg_types.reserve(arg_indices.size());
1145 for (TypeIndex arg_index : arg_indices) {
1146 clang::QualType arg_type = GetOrCreateType(arg_index);
1147 arg_types.push_back(ToCompilerType(arg_type));
1150 clang::QualType return_type = GetOrCreateType(return_type_idx);
1152 llvm::Optional<clang::CallingConv> cc =
1153 TranslateCallingConvention(calling_convention);
1157 CompilerType return_ct = ToCompilerType(return_type);
1158 CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1159 return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc);
1161 return clang::QualType::getFromOpaquePtr(
1162 func_sig_ast_type.GetOpaqueQualType());
1165 static bool isTagDecl(clang::DeclContext &context) {
1166 return !!llvm::dyn_cast<clang::TagDecl>(&context);
1169 static bool isFunctionDecl(clang::DeclContext &context) {
1170 return !!llvm::dyn_cast<clang::FunctionDecl>(&context);
1173 static bool isBlockDecl(clang::DeclContext &context) {
1174 return !!llvm::dyn_cast<clang::BlockDecl>(&context);
1177 void PdbAstBuilder::ParseAllNamespacesPlusChildrenOf(
1178 llvm::Optional<llvm::StringRef> parent) {
1179 TypeIndex ti{m_index.tpi().TypeIndexBegin()};
1180 for (const CVType &cvt : m_index.tpi().typeArray()) {
1181 PdbTypeSymId tid{ti};
1184 if (!IsTagRecord(cvt))
1187 CVTagRecord tag = CVTagRecord::create(cvt);
1189 if (!parent.hasValue()) {
1190 clang::QualType qt = GetOrCreateType(tid);
1195 // Call CreateDeclInfoForType unconditionally so that the namespace info
1196 // gets created. But only call CreateRecordType if the namespace name
1198 clang::DeclContext *context = nullptr;
1200 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1201 if (!context->isNamespace())
1204 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(context);
1205 std::string actual_ns = ns->getQualifiedNameAsString();
1206 if (llvm::StringRef(actual_ns).startswith(*parent)) {
1207 clang::QualType qt = GetOrCreateType(tid);
1213 uint32_t module_count = m_index.dbi().modules().getModuleCount();
1214 for (uint16_t modi = 0; modi < module_count; ++modi) {
1215 CompilandIndexItem &cii = m_index.compilands().GetOrCreateCompiland(modi);
1216 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1217 auto iter = symbols.begin();
1218 while (iter != symbols.end()) {
1219 PdbCompilandSymId sym_id{modi, iter.offset()};
1221 switch (iter->kind()) {
1224 GetOrCreateFunctionDecl(sym_id);
1225 iter = symbols.at(getScopeEndOffset(*iter));
1231 GetOrCreateVariableDecl(PdbCompilandSymId(modi, 0), sym_id);
1242 static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1243 const CVSymbolArray &symbols) {
1244 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1247 unsigned int params = func_decl->getNumParams();
1251 CVSymbolArray result = symbols;
1253 while (!result.empty()) {
1257 CVSymbol sym = *result.begin();
1258 result.drop_front();
1260 if (!isLocalVariableType(sym.kind()))
1268 void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id) {
1269 CVSymbol sym = m_index.ReadSymbolRecord(block_id);
1270 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1271 sym.kind() == S_BLOCK32);
1272 CompilandIndexItem &cii =
1273 m_index.compilands().GetOrCreateCompiland(block_id.modi);
1274 CVSymbolArray symbols =
1275 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1277 // Function parameters should already have been created when the function was
1279 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1281 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1283 auto begin = symbols.begin();
1284 while (begin != symbols.end()) {
1285 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1286 GetOrCreateSymbolForId(child_sym_id);
1287 if (begin->kind() == S_BLOCK32) {
1288 ParseBlockChildren(child_sym_id);
1289 begin = symbols.at(getScopeEndOffset(*begin));
1295 void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
1297 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1300 auto iter = m_decl_to_status.find(decl);
1301 lldbassert(iter != m_decl_to_status.end());
1303 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1304 CompleteTagDecl(*tag);
1308 if (isFunctionDecl(context) || isBlockDecl(context)) {
1309 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1310 ParseBlockChildren(block_id);
1314 void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1315 // Namespaces aren't explicitly represented in the debug info, and the only
1316 // way to parse them is to parse all type info, demangling every single type
1317 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1318 // is an expensive operation, we have to special case it so that we do other
1319 // work (such as parsing the items that appear within the namespaces) at the
1321 if (context.isTranslationUnit()) {
1322 ParseAllNamespacesPlusChildrenOf(llvm::None);
1326 if (context.isNamespace()) {
1327 clang::NamespaceDecl &ns = *llvm::dyn_cast<clang::NamespaceDecl>(&context);
1328 std::string qname = ns.getQualifiedNameAsString();
1329 ParseAllNamespacesPlusChildrenOf(llvm::StringRef{qname});
1333 if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1334 ParseDeclsForSimpleContext(context);
1339 CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) {
1340 return {&m_clang, &decl};
1343 CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) {
1344 return {&m_clang, qt.getAsOpaquePtr()};
1348 PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) {
1349 return {&m_clang, &context};
1352 clang::Decl * PdbAstBuilder::FromCompilerDecl(CompilerDecl decl) {
1353 return static_cast<clang::Decl *>(decl.GetOpaqueDecl());
1356 clang::DeclContext *
1357 PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
1358 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1361 void PdbAstBuilder::Dump(Stream &stream) { m_clang.Dump(stream); }