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