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"
18 #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
19 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
20 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
21 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
22 #include "SymbolFileNativePDB.h"
23 #include "UdtRecordCompleter.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Symbol/ObjectFile.h"
26 #include "lldb/Utility/LLDBAssert.h"
28 #include <string_view>
30 using namespace lldb_private
;
31 using namespace lldb_private::npdb
;
32 using namespace llvm::codeview
;
33 using namespace llvm::pdb
;
36 struct CreateMethodDecl
: public TypeVisitorCallbacks
{
37 CreateMethodDecl(PdbIndex
&m_index
, TypeSystemClang
&m_clang
,
38 TypeIndex func_type_index
,
39 clang::FunctionDecl
*&function_decl
,
40 lldb::opaque_compiler_type_t parent_ty
,
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
) {}
46 TypeSystemClang
&m_clang
;
47 TypeIndex func_type_index
;
48 clang::FunctionDecl
*&function_decl
;
49 lldb::opaque_compiler_type_t parent_ty
;
50 llvm::StringRef proc_name
;
53 llvm::Error
visitKnownMember(CVMemberRecord
&cvr
,
54 OverloadedMethodRecord
&overloaded
) override
{
55 TypeIndex method_list_idx
= overloaded
.MethodList
;
57 CVType method_list_type
= m_index
.tpi().getType(method_list_idx
);
58 assert(method_list_type
.kind() == LF_METHODLIST
);
60 MethodOverloadListRecord method_list
;
61 llvm::cantFail(TypeDeserializer::deserializeAs
<MethodOverloadListRecord
>(
62 method_list_type
, method_list
));
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(),
70 return llvm::Error::success();
73 llvm::Error
visitKnownMember(CVMemberRecord
&cvr
,
74 OneMethodRecord
&record
) override
{
75 AddMethod(record
.getName(), record
.getAccess(), record
.getOptions(),
77 return llvm::Error::success();
80 void AddMethod(llvm::StringRef name
, MemberAccess access
,
81 MethodOptions options
, MemberAttributes attrs
) {
82 if (name
!= proc_name
|| function_decl
)
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(
91 /*mangled_name=*/nullptr, 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
);
99 static clang::TagTypeKind
TranslateUdtKind(const TagRecord
&cr
) {
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
;
112 lldbassert(false && "Invalid tag record kind!");
113 return clang::TagTypeKind::Struct
;
117 static bool IsCVarArgsFunction(llvm::ArrayRef
<TypeIndex
> args
) {
120 return args
.back() == TypeIndex::None();
124 AnyScopesHaveTemplateParams(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
)
133 static std::optional
<clang::CallingConv
>
134 TranslateCallingConvention(llvm::codeview::CallingConvention conv
) {
135 using CC
= llvm::codeview::CallingConvention
;
140 return clang::CallingConv::CC_C
;
143 return clang::CallingConv::CC_X86Pascal
;
146 return clang::CallingConv::CC_X86FastCall
;
147 case CC::NearStdCall
:
149 return clang::CallingConv::CC_X86StdCall
;
151 return clang::CallingConv::CC_X86ThisCall
;
153 return clang::CallingConv::CC_X86VectorCall
;
159 static bool IsAnonymousNamespaceName(llvm::StringRef name
) {
160 return name
== "`anonymous namespace'" || name
== "`anonymous-namespace'";
163 PdbAstBuilder::PdbAstBuilder(TypeSystemClang
&clang
) : m_clang(clang
) {}
165 lldb_private::CompilerDeclContext
PdbAstBuilder::GetTranslationUnitDecl() {
166 return ToCompilerDeclContext(*m_clang
.GetTranslationUnitDecl());
169 std::pair
<clang::DeclContext
*, std::string
>
170 PdbAstBuilder::CreateDeclInfoForType(const TagRecord
&record
, TypeIndex ti
) {
171 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
172 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
173 // FIXME: Move this to GetDeclContextContainingUID.
174 if (!record
.hasUniqueName())
175 return CreateDeclInfoForUndecoratedName(record
.Name
);
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
);
181 return {m_clang
.GetTranslationUnitDecl(), std::string(record
.UniqueName
)};
183 llvm::ms_demangle::IdentifierNode
*idn
=
184 ttn
->QualifiedName
->getUnqualifiedIdentifier();
185 std::string uname
= idn
->toString(llvm::ms_demangle::OF_NoTagSpecifier
);
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);
192 clang::DeclContext
*context
= m_clang
.GetTranslationUnitDecl();
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
);
200 return {context
, uname
};
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
)};
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
);
215 return {context
, uname
};
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, ""};
226 context
= clang::TagDecl::castToDeclContext(parent_qt
->getAsTagDecl());
227 return {context
, uname
};
230 static bool isLocalVariableType(SymbolKind K
) {
242 clang::Decl
*PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id
) {
243 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
244 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
245 PdbIndex
&index
= pdb
->GetIndex();
246 CVSymbol cvs
= index
.ReadSymbolRecord(id
);
248 if (isLocalVariableType(cvs
.kind())) {
249 clang::DeclContext
*scope
= GetParentDeclContext(id
);
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
);
258 switch (cvs
.kind()) {
261 return GetOrCreateFunctionDecl(id
);
269 return GetOrCreateBlockDecl(id
);
271 return GetOrCreateInlinedFunctionDecl(id
);
277 std::optional
<CompilerDecl
>
278 PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid
) {
279 if (clang::Decl
*result
= TryGetDecl(uid
))
280 return ToCompilerDecl(*result
);
282 clang::Decl
*result
= nullptr;
283 switch (uid
.kind()) {
284 case PdbSymUidKind::CompilandSym
:
285 result
= GetOrCreateSymbolForId(uid
.asCompilandSym());
287 case PdbSymUidKind::Type
: {
288 clang::QualType qt
= GetOrCreateType(uid
.asTypeSym());
291 if (auto *tag
= qt
->getAsTagDecl()) {
303 m_uid_to_decl
[toOpaqueUid(uid
)] = result
;
304 return ToCompilerDecl(*result
);
307 clang::DeclContext
*PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid
) {
308 if (uid
.kind() == PdbSymUidKind::CompilandSym
) {
309 if (uid
.asCompilandSym().offset
== 0)
310 return FromCompilerDeclContext(GetTranslationUnitDecl());
312 auto option
= GetOrCreateDeclForUid(uid
);
315 clang::Decl
*decl
= FromCompilerDecl(*option
);
319 return clang::Decl::castToDeclContext(decl
);
322 std::pair
<clang::DeclContext
*, std::string
>
323 PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name
) {
324 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
325 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
326 PdbIndex
&index
= pdb
->GetIndex();
327 MSVCUndecoratedNameParser
parser(name
);
328 llvm::ArrayRef
<MSVCUndecoratedNameSpecifier
> specs
= parser
.GetSpecifiers();
330 auto *context
= FromCompilerDeclContext(GetTranslationUnitDecl());
332 llvm::StringRef uname
= specs
.back().GetBaseName();
333 specs
= specs
.drop_back();
335 return {context
, std::string(name
)};
337 llvm::StringRef scope_name
= specs
.back().GetFullName();
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());
345 clang::TagDecl
*tag
= qt
->getAsTagDecl();
347 return {clang::TagDecl::castToDeclContext(tag
), std::string(uname
)};
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
);
356 return {context
, std::string(uname
)};
359 clang::DeclContext
*PdbAstBuilder::GetParentDeclContext(PdbSymUid uid
) {
360 // We must do this *without* calling GetOrCreate on the current uid, as
361 // that would be an infinite recursion.
362 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
363 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
364 PdbIndex
& index
= pdb
->GetIndex();
365 switch (uid
.kind()) {
366 case PdbSymUidKind::CompilandSym
: {
367 std::optional
<PdbCompilandSymId
> scope
=
368 pdb
->FindSymbolScope(uid
.asCompilandSym());
370 return GetOrCreateDeclContextForUid(*scope
);
372 CVSymbol sym
= index
.ReadSymbolRecord(uid
.asCompilandSym());
373 return CreateDeclInfoForUndecoratedName(getSymbolName(sym
)).first
;
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
);
381 return FromCompilerDeclContext(GetTranslationUnitDecl());
382 return GetOrCreateDeclContextForUid(PdbTypeSymId(*parent_index
));
384 case PdbSymUidKind::FieldListMember
:
385 // In this case the parent DeclContext is the one for the class that this
386 // member is inside of.
388 case PdbSymUidKind::GlobalSym
: {
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.
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()};
404 SymbolDeserializer::deserializeAs
<ProcRefSym
>(global
, ref
));
405 PdbCompilandSymId cu_sym_id
{ref
.modi(), ref
.SymOffset
};
406 return GetParentDeclContext(cu_sym_id
);
408 case SymbolKind::S_CONSTANT
:
409 case SymbolKind::S_UDT
:
410 return CreateDeclInfoForUndecoratedName(getSymbolName(global
)).first
;
419 return FromCompilerDeclContext(GetTranslationUnitDecl());
422 bool PdbAstBuilder::CompleteType(clang::QualType qt
) {
425 clang::TagDecl
*tag
= qt
->getAsTagDecl();
426 if (qt
->isArrayType()) {
427 const clang::Type
*element_type
= qt
->getArrayElementTypeNoTypeQual();
428 tag
= element_type
->getAsTagDecl();
433 return CompleteTagDecl(*tag
);
436 bool 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());
441 // If it's already complete, just return.
442 DeclStatus
&status
= status_iter
->second
;
446 PdbTypeSymId type_id
= PdbSymUid(status
.uid
).asTypeSym();
447 PdbIndex
&index
= static_cast<SymbolFileNativePDB
*>(
448 m_clang
.GetSymbolFile()->GetBackingSymbolFile())
450 lldbassert(IsTagRecord(type_id
, index
.tpi()));
452 clang::QualType tag_qt
= m_clang
.getASTContext().getTypeDeclType(&tag
);
453 TypeSystemClang::SetHasExternalStorage(tag_qt
.getAsOpaquePtr(), false);
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
);
460 PdbTypeSymId best_ti
= GetBestPossibleDecl(tag_ti
, index
.tpi());
461 cvt
= index
.tpi().getType(best_ti
.index
);
462 lldbassert(IsTagRecord(cvt
));
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.
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
)
474 FieldListRecord field_list
;
475 if (llvm::Error error
= TypeDeserializer::deserializeAs
<FieldListRecord
>(
476 field_list_cvt
, field_list
))
477 llvm::consumeError(std::move(error
));
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
,
485 llvm::codeview::visitMemberRecordStream(field_list
.Data
, completer
);
486 completer
.complete();
488 m_decl_to_status
[&tag
].resolved
= true;
490 llvm::consumeError(std::move(error
));
496 clang::QualType
PdbAstBuilder::CreateSimpleType(TypeIndex ti
) {
497 if (ti
== TypeIndex::NullptrT())
498 return GetBasicType(lldb::eBasicTypeNullPtr
);
500 if (ti
.getSimpleMode() != SimpleTypeMode::Direct
) {
501 clang::QualType direct_type
= GetOrCreateType(ti
.makeDirect());
502 if (direct_type
.isNull())
504 return m_clang
.getASTContext().getPointerType(direct_type
);
507 if (ti
.getSimpleKind() == SimpleTypeKind::NotTranslated
)
510 lldb::BasicType bt
= GetCompilerTypeForSimpleKind(ti
.getSimpleKind());
511 if (bt
== lldb::eBasicTypeInvalid
)
514 return GetBasicType(bt
);
517 clang::QualType
PdbAstBuilder::CreatePointerType(const PointerRecord
&pointer
) {
518 clang::QualType pointee_type
= GetOrCreateType(pointer
.ReferentType
);
520 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
521 // create in the AST.
522 if (pointee_type
.isNull())
525 if (pointer
.isPointerToMember()) {
526 MemberPointerInfo mpi
= pointer
.getMemberInfo();
527 clang::QualType class_type
= GetOrCreateType(mpi
.ContainingType
);
528 if (class_type
.isNull())
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
:
537 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance
;
539 case llvm::codeview::PointerToMemberRepresentation::
540 MultipleInheritanceData
:
541 case llvm::codeview::PointerToMemberRepresentation::
542 MultipleInheritanceFunction
:
544 clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance
;
546 case llvm::codeview::PointerToMemberRepresentation::
547 VirtualInheritanceData
:
548 case llvm::codeview::PointerToMemberRepresentation::
549 VirtualInheritanceFunction
:
551 clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance
;
553 case llvm::codeview::PointerToMemberRepresentation::Unknown
:
555 clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance
;
558 spelling
= clang::MSInheritanceAttr::Spelling::SpellingNotCalculated
;
561 tag
->addAttr(clang::MSInheritanceAttr::CreateImplicit(
562 m_clang
.getASTContext(), spelling
));
564 return m_clang
.getASTContext().getMemberPointerType(
565 pointee_type
, class_type
.getTypePtr());
568 clang::QualType pointer_type
;
569 if (pointer
.getMode() == PointerMode::LValueReference
)
570 pointer_type
= m_clang
.getASTContext().getLValueReferenceType(pointee_type
);
571 else if (pointer
.getMode() == PointerMode::RValueReference
)
572 pointer_type
= m_clang
.getASTContext().getRValueReferenceType(pointee_type
);
574 pointer_type
= m_clang
.getASTContext().getPointerType(pointee_type
);
576 if ((pointer
.getOptions() & PointerOptions::Const
) != PointerOptions::None
)
577 pointer_type
.addConst();
579 if ((pointer
.getOptions() & PointerOptions::Volatile
) != PointerOptions::None
)
580 pointer_type
.addVolatile();
582 if ((pointer
.getOptions() & PointerOptions::Restrict
) != PointerOptions::None
)
583 pointer_type
.addRestrict();
589 PdbAstBuilder::CreateModifierType(const ModifierRecord
&modifier
) {
590 clang::QualType unmodified_type
= GetOrCreateType(modifier
.ModifiedType
);
591 if (unmodified_type
.isNull())
594 if ((modifier
.Modifiers
& ModifierOptions::Const
) != ModifierOptions::None
)
595 unmodified_type
.addConst();
596 if ((modifier
.Modifiers
& ModifierOptions::Volatile
) != ModifierOptions::None
)
597 unmodified_type
.addVolatile();
599 return unmodified_type
;
602 clang::QualType
PdbAstBuilder::CreateRecordType(PdbTypeSymId id
,
603 const TagRecord
&record
) {
604 clang::DeclContext
*context
= nullptr;
606 std::tie(context
, uname
) = CreateDeclInfoForType(record
, id
.index
);
610 clang::TagTypeKind ttk
= TranslateUdtKind(record
);
611 lldb::AccessType access
= (ttk
== clang::TagTypeKind::Class
)
612 ? lldb::eAccessPrivate
613 : lldb::eAccessPublic
;
615 ClangASTMetadata metadata
;
616 metadata
.SetUserID(toOpaqueUid(id
));
617 metadata
.SetIsDynamicCXXType(false);
619 CompilerType ct
= m_clang
.CreateRecordType(
620 context
, OptionalClangModuleID(), access
, uname
, llvm::to_underlying(ttk
),
621 lldb::eLanguageTypeC_plus_plus
, metadata
);
623 lldbassert(ct
.IsValid());
625 TypeSystemClang::StartTagDeclarationDefinition(ct
);
627 // Even if it's possible, don't complete it at this point. Just mark it
628 // forward resolved, and if/when LLDB needs the full definition, it can
630 clang::QualType result
=
631 clang::QualType::getFromOpaquePtr(ct
.GetOpaqueQualType());
633 TypeSystemClang::SetHasExternalStorage(result
.getAsOpaquePtr(), true);
637 clang::Decl
*PdbAstBuilder::TryGetDecl(PdbSymUid uid
) const {
638 auto iter
= m_uid_to_decl
.find(toOpaqueUid(uid
));
639 if (iter
!= m_uid_to_decl
.end())
644 clang::NamespaceDecl
*
645 PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name
,
646 clang::DeclContext
&context
) {
647 return m_clang
.GetUniqueNamespaceDeclaration(
648 IsAnonymousNamespaceName(name
) ? nullptr : name
, &context
,
649 OptionalClangModuleID());
653 PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id
) {
654 if (clang::Decl
*decl
= TryGetDecl(block_id
))
655 return llvm::dyn_cast
<clang::BlockDecl
>(decl
);
657 clang::DeclContext
*scope
= GetParentDeclContext(block_id
);
659 clang::BlockDecl
*block_decl
=
660 m_clang
.CreateBlockDeclaration(scope
, OptionalClangModuleID());
661 m_uid_to_decl
.insert({toOpaqueUid(block_id
), block_decl
});
664 status
.resolved
= true;
665 status
.uid
= toOpaqueUid(block_id
);
666 m_decl_to_status
.insert({block_decl
, status
});
671 clang::VarDecl
*PdbAstBuilder::CreateVariableDecl(PdbSymUid uid
, CVSymbol sym
,
672 clang::DeclContext
&scope
) {
673 VariableInfo var_info
= GetVariableNameInfo(sym
);
674 clang::QualType qt
= GetOrCreateType(var_info
.type
);
678 clang::VarDecl
*var_decl
= m_clang
.CreateVariableDeclaration(
679 &scope
, OptionalClangModuleID(), var_info
.name
.str().c_str(), qt
);
681 m_uid_to_decl
[toOpaqueUid(uid
)] = var_decl
;
683 status
.resolved
= true;
684 status
.uid
= toOpaqueUid(uid
);
685 m_decl_to_status
.insert({var_decl
, status
});
690 PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id
,
691 PdbCompilandSymId var_id
) {
692 if (clang::Decl
*decl
= TryGetDecl(var_id
))
693 return llvm::dyn_cast
<clang::VarDecl
>(decl
);
695 clang::DeclContext
*scope
= GetOrCreateDeclContextForUid(scope_id
);
699 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
700 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
701 PdbIndex
&index
= pdb
->GetIndex();
702 CVSymbol sym
= index
.ReadSymbolRecord(var_id
);
703 return CreateVariableDecl(PdbSymUid(var_id
), sym
, *scope
);
706 clang::VarDecl
*PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id
) {
707 if (clang::Decl
*decl
= TryGetDecl(var_id
))
708 return llvm::dyn_cast
<clang::VarDecl
>(decl
);
710 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
711 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
712 PdbIndex
&index
= pdb
->GetIndex();
713 CVSymbol sym
= index
.ReadSymbolRecord(var_id
);
714 auto context
= FromCompilerDeclContext(GetTranslationUnitDecl());
715 return CreateVariableDecl(PdbSymUid(var_id
), sym
, *context
);
718 clang::TypedefNameDecl
*
719 PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id
) {
720 if (clang::Decl
*decl
= TryGetDecl(id
))
721 return llvm::dyn_cast
<clang::TypedefNameDecl
>(decl
);
723 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
724 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
725 PdbIndex
&index
= pdb
->GetIndex();
726 CVSymbol sym
= index
.ReadSymbolRecord(id
);
727 lldbassert(sym
.kind() == S_UDT
);
728 UDTSym udt
= llvm::cantFail(SymbolDeserializer::deserializeAs
<UDTSym
>(sym
));
730 clang::DeclContext
*scope
= GetParentDeclContext(id
);
732 PdbTypeSymId real_type_id
{udt
.Type
, false};
733 clang::QualType qt
= GetOrCreateType(real_type_id
);
734 if (qt
.isNull() || !scope
)
737 std::string uname
= std::string(DropNameScope(udt
.Name
));
739 CompilerType ct
= ToCompilerType(qt
).CreateTypedef(
740 uname
.c_str(), ToCompilerDeclContext(*scope
), 0);
741 clang::TypedefNameDecl
*tnd
= m_clang
.GetAsTypedefDecl(ct
);
743 status
.resolved
= true;
744 status
.uid
= toOpaqueUid(id
);
745 m_decl_to_status
.insert({tnd
, status
});
749 clang::QualType
PdbAstBuilder::GetBasicType(lldb::BasicType type
) {
750 CompilerType ct
= m_clang
.GetBasicType(type
);
751 return clang::QualType::getFromOpaquePtr(ct
.GetOpaqueQualType());
754 clang::QualType
PdbAstBuilder::CreateType(PdbTypeSymId type
) {
755 if (type
.index
.isSimple())
756 return CreateSimpleType(type
.index
);
758 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
759 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
760 PdbIndex
&index
= pdb
->GetIndex();
761 CVType cvt
= index
.tpi().getType(type
.index
);
763 if (cvt
.kind() == LF_MODIFIER
) {
764 ModifierRecord modifier
;
766 TypeDeserializer::deserializeAs
<ModifierRecord
>(cvt
, modifier
));
767 return CreateModifierType(modifier
);
770 if (cvt
.kind() == LF_POINTER
) {
771 PointerRecord pointer
;
773 TypeDeserializer::deserializeAs
<PointerRecord
>(cvt
, pointer
));
774 return CreatePointerType(pointer
);
777 if (IsTagRecord(cvt
)) {
778 CVTagRecord tag
= CVTagRecord::create(cvt
);
779 if (tag
.kind() == CVTagRecord::Union
)
780 return CreateRecordType(type
.index
, tag
.asUnion());
781 if (tag
.kind() == CVTagRecord::Enum
)
782 return CreateEnumType(type
.index
, tag
.asEnum());
783 return CreateRecordType(type
.index
, tag
.asClass());
786 if (cvt
.kind() == LF_ARRAY
) {
788 llvm::cantFail(TypeDeserializer::deserializeAs
<ArrayRecord
>(cvt
, ar
));
789 return CreateArrayType(ar
);
792 if (cvt
.kind() == LF_PROCEDURE
) {
794 llvm::cantFail(TypeDeserializer::deserializeAs
<ProcedureRecord
>(cvt
, pr
));
795 return CreateFunctionType(pr
.ArgumentList
, pr
.ReturnType
, pr
.CallConv
);
798 if (cvt
.kind() == LF_MFUNCTION
) {
799 MemberFunctionRecord mfr
;
801 TypeDeserializer::deserializeAs
<MemberFunctionRecord
>(cvt
, mfr
));
802 return CreateFunctionType(mfr
.ArgumentList
, mfr
.ReturnType
, mfr
.CallConv
);
808 clang::QualType
PdbAstBuilder::GetOrCreateType(PdbTypeSymId type
) {
809 if (type
.index
.isNoneType())
812 lldb::user_id_t uid
= toOpaqueUid(type
);
813 auto iter
= m_uid_to_type
.find(uid
);
814 if (iter
!= m_uid_to_type
.end())
817 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
818 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
819 PdbIndex
&index
= pdb
->GetIndex();
820 PdbTypeSymId best_type
= GetBestPossibleDecl(type
, index
.tpi());
823 if (best_type
.index
!= type
.index
) {
824 // This is a forward decl. Call GetOrCreate on the full decl, then map the
825 // forward decl id to the full decl QualType.
826 clang::QualType qt
= GetOrCreateType(best_type
);
829 m_uid_to_type
[toOpaqueUid(type
)] = qt
;
833 // This is either a full decl, or a forward decl with no matching full decl
834 // in the debug info.
835 qt
= CreateType(type
);
839 m_uid_to_type
[toOpaqueUid(type
)] = qt
;
840 if (IsTagRecord(type
, index
.tpi())) {
841 clang::TagDecl
*tag
= qt
->getAsTagDecl();
842 lldbassert(m_decl_to_status
.count(tag
) == 0);
844 DeclStatus
&status
= m_decl_to_status
[tag
];
846 status
.resolved
= false;
851 clang::FunctionDecl
*
852 PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id
,
853 llvm::StringRef func_name
, TypeIndex func_ti
,
854 CompilerType func_ct
, uint32_t param_count
,
855 clang::StorageClass func_storage
,
856 bool is_inline
, clang::DeclContext
*parent
) {
857 clang::FunctionDecl
*function_decl
= nullptr;
858 if (parent
->isRecord()) {
859 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
860 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
861 PdbIndex
&index
= pdb
->GetIndex();
862 clang::QualType parent_qt
= llvm::cast
<clang::TypeDecl
>(parent
)
864 ->getCanonicalTypeInternal();
865 lldb::opaque_compiler_type_t parent_opaque_ty
=
866 ToCompilerType(parent_qt
).GetOpaqueQualType();
867 // FIXME: Remove this workaround.
868 auto iter
= m_cxx_record_map
.find(parent_opaque_ty
);
869 if (iter
!= m_cxx_record_map
.end()) {
870 if (iter
->getSecond().contains({func_name
, func_ct
})) {
875 CVType cvt
= index
.tpi().getType(func_ti
);
876 MemberFunctionRecord
func_record(static_cast<TypeRecordKind
>(cvt
.kind()));
877 llvm::cantFail(TypeDeserializer::deserializeAs
<MemberFunctionRecord
>(
879 TypeIndex class_index
= func_record
.getClassType();
881 CVType parent_cvt
= index
.tpi().getType(class_index
);
882 TagRecord tag_record
= CVTagRecord::create(parent_cvt
).asTag();
883 // If it's a forward reference, try to get the real TypeIndex.
884 if (tag_record
.isForwardRef()) {
885 llvm::Expected
<TypeIndex
> eti
=
886 index
.tpi().findFullDeclForForwardRef(class_index
);
888 tag_record
= CVTagRecord::create(index
.tpi().getType(*eti
)).asTag();
891 if (!tag_record
.FieldList
.isSimple()) {
892 CVType field_list_cvt
= index
.tpi().getType(tag_record
.FieldList
);
893 FieldListRecord field_list
;
894 if (llvm::Error error
= TypeDeserializer::deserializeAs
<FieldListRecord
>(
895 field_list_cvt
, field_list
))
896 llvm::consumeError(std::move(error
));
897 CreateMethodDecl
process(index
, m_clang
, func_ti
, function_decl
,
898 parent_opaque_ty
, func_name
, func_ct
);
899 if (llvm::Error err
= visitMemberRecordStream(field_list
.Data
, process
))
900 llvm::consumeError(std::move(err
));
903 if (!function_decl
) {
904 function_decl
= m_clang
.AddMethodToCXXRecordType(
905 parent_opaque_ty
, func_name
,
906 /*mangled_name=*/nullptr, func_ct
,
907 /*access=*/lldb::AccessType::eAccessPublic
,
908 /*is_virtual=*/false, /*is_static=*/false,
909 /*is_inline=*/false, /*is_explicit=*/false,
910 /*is_attr_used=*/false, /*is_artificial=*/false);
912 m_cxx_record_map
[parent_opaque_ty
].insert({func_name
, func_ct
});
914 function_decl
= m_clang
.CreateFunctionDeclaration(
915 parent
, OptionalClangModuleID(), func_name
, func_ct
, func_storage
,
917 CreateFunctionParameters(func_id
, *function_decl
, param_count
);
919 return function_decl
;
922 clang::FunctionDecl
*
923 PdbAstBuilder::GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id
) {
924 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
925 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
926 PdbIndex
&index
= pdb
->GetIndex();
927 CompilandIndexItem
*cii
=
928 index
.compilands().GetCompiland(inlinesite_id
.modi
);
929 CVSymbol sym
= cii
->m_debug_stream
.readSymbolAtOffset(inlinesite_id
.offset
);
930 InlineSiteSym
inline_site(static_cast<SymbolRecordKind
>(sym
.kind()));
931 cantFail(SymbolDeserializer::deserializeAs
<InlineSiteSym
>(sym
, inline_site
));
933 // Inlinee is the id index to the function id record that is inlined.
934 PdbTypeSymId
func_id(inline_site
.Inlinee
, true);
935 // Look up the function decl by the id index to see if we have created a
936 // function decl for a different inlinesite that refers the same function.
937 if (clang::Decl
*decl
= TryGetDecl(func_id
))
938 return llvm::dyn_cast
<clang::FunctionDecl
>(decl
);
939 clang::FunctionDecl
*function_decl
=
940 CreateFunctionDeclFromId(func_id
, inlinesite_id
);
941 if (function_decl
== nullptr)
944 // Use inline site id in m_decl_to_status because it's expected to be a
945 // PdbCompilandSymId so that we can parse local variables info after it.
946 uint64_t inlinesite_uid
= toOpaqueUid(inlinesite_id
);
948 status
.resolved
= true;
949 status
.uid
= inlinesite_uid
;
950 m_decl_to_status
.insert({function_decl
, status
});
951 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
952 // stream are unique and there could be multiple inline sites (different ids)
953 // referring the same inline function. This avoid creating multiple same
954 // inline function delcs.
955 uint64_t func_uid
= toOpaqueUid(func_id
);
956 lldbassert(m_uid_to_decl
.count(func_uid
) == 0);
957 m_uid_to_decl
[func_uid
] = function_decl
;
958 return function_decl
;
961 clang::FunctionDecl
*
962 PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid
,
963 PdbCompilandSymId func_sid
) {
964 lldbassert(func_tid
.is_ipi
);
965 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
966 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
967 PdbIndex
&index
= pdb
->GetIndex();
968 CVType func_cvt
= index
.ipi().getType(func_tid
.index
);
969 llvm::StringRef func_name
;
971 clang::DeclContext
*parent
= nullptr;
972 switch (func_cvt
.kind()) {
974 MemberFuncIdRecord mfr
;
976 TypeDeserializer::deserializeAs
<MemberFuncIdRecord
>(func_cvt
, mfr
));
977 func_name
= mfr
.getName();
978 func_ti
= mfr
.getFunctionType();
979 PdbTypeSymId
class_type_id(mfr
.ClassType
, false);
980 parent
= GetOrCreateDeclContextForUid(class_type_id
);
985 cantFail(TypeDeserializer::deserializeAs
<FuncIdRecord
>(func_cvt
, fir
));
986 func_name
= fir
.getName();
987 func_ti
= fir
.getFunctionType();
988 parent
= FromCompilerDeclContext(GetTranslationUnitDecl());
989 if (!fir
.ParentScope
.isNoneType()) {
990 CVType parent_cvt
= index
.ipi().getType(fir
.ParentScope
);
991 if (parent_cvt
.kind() == LF_STRING_ID
) {
994 TypeDeserializer::deserializeAs
<StringIdRecord
>(parent_cvt
, sir
));
995 parent
= GetOrCreateNamespaceDecl(sir
.String
.data(), *parent
);
1001 lldbassert(false && "Invalid function id type!");
1003 clang::QualType func_qt
= GetOrCreateType(func_ti
);
1004 if (func_qt
.isNull() || !parent
)
1006 CompilerType func_ct
= ToCompilerType(func_qt
);
1007 uint32_t param_count
=
1008 llvm::cast
<clang::FunctionProtoType
>(func_qt
)->getNumParams();
1009 return CreateFunctionDecl(func_sid
, func_name
, func_ti
, func_ct
, param_count
,
1010 clang::SC_None
, true, parent
);
1013 clang::FunctionDecl
*
1014 PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id
) {
1015 if (clang::Decl
*decl
= TryGetDecl(func_id
))
1016 return llvm::dyn_cast
<clang::FunctionDecl
>(decl
);
1018 clang::DeclContext
*parent
= GetParentDeclContext(PdbSymUid(func_id
));
1021 std::string context_name
;
1022 if (clang::NamespaceDecl
*ns
= llvm::dyn_cast
<clang::NamespaceDecl
>(parent
)) {
1023 context_name
= ns
->getQualifiedNameAsString();
1024 } else if (clang::TagDecl
*tag
= llvm::dyn_cast
<clang::TagDecl
>(parent
)) {
1025 context_name
= tag
->getQualifiedNameAsString();
1028 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1029 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1030 PdbIndex
&index
= pdb
->GetIndex();
1031 CVSymbol cvs
= index
.ReadSymbolRecord(func_id
);
1032 ProcSym
proc(static_cast<SymbolRecordKind
>(cvs
.kind()));
1033 llvm::cantFail(SymbolDeserializer::deserializeAs
<ProcSym
>(cvs
, proc
));
1035 PdbTypeSymId
type_id(proc
.FunctionType
);
1036 clang::QualType qt
= GetOrCreateType(type_id
);
1040 clang::StorageClass storage
= clang::SC_None
;
1041 if (proc
.Kind
== SymbolRecordKind::ProcSym
)
1042 storage
= clang::SC_Static
;
1044 const clang::FunctionProtoType
*func_type
=
1045 llvm::dyn_cast
<clang::FunctionProtoType
>(qt
);
1047 CompilerType func_ct
= ToCompilerType(qt
);
1049 llvm::StringRef proc_name
= proc
.Name
;
1050 proc_name
.consume_front(context_name
);
1051 proc_name
.consume_front("::");
1052 clang::FunctionDecl
*function_decl
=
1053 CreateFunctionDecl(func_id
, proc_name
, proc
.FunctionType
, func_ct
,
1054 func_type
->getNumParams(), storage
, false, parent
);
1055 if (function_decl
== nullptr)
1058 lldbassert(m_uid_to_decl
.count(toOpaqueUid(func_id
)) == 0);
1059 m_uid_to_decl
[toOpaqueUid(func_id
)] = function_decl
;
1061 status
.resolved
= true;
1062 status
.uid
= toOpaqueUid(func_id
);
1063 m_decl_to_status
.insert({function_decl
, status
});
1065 return function_decl
;
1068 void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id
,
1069 clang::FunctionDecl
&function_decl
,
1070 uint32_t param_count
) {
1071 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1072 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1073 PdbIndex
&index
= pdb
->GetIndex();
1074 CompilandIndexItem
*cii
= index
.compilands().GetCompiland(func_id
.modi
);
1075 CVSymbolArray scope
=
1076 cii
->m_debug_stream
.getSymbolArrayForScope(func_id
.offset
);
1079 auto begin
= scope
.begin();
1080 auto end
= scope
.end();
1081 std::vector
<clang::ParmVarDecl
*> params
;
1082 for (uint32_t i
= 0; i
< param_count
&& begin
!= end
;) {
1083 uint32_t record_offset
= begin
.offset();
1084 CVSymbol sym
= *begin
++;
1086 TypeIndex param_type
;
1087 llvm::StringRef param_name
;
1088 switch (sym
.kind()) {
1090 RegRelativeSym
reg(SymbolRecordKind::RegRelativeSym
);
1091 cantFail(SymbolDeserializer::deserializeAs
<RegRelativeSym
>(sym
, reg
));
1092 param_type
= reg
.Type
;
1093 param_name
= reg
.Name
;
1097 RegisterSym
reg(SymbolRecordKind::RegisterSym
);
1098 cantFail(SymbolDeserializer::deserializeAs
<RegisterSym
>(sym
, reg
));
1099 param_type
= reg
.Index
;
1100 param_name
= reg
.Name
;
1104 LocalSym
local(SymbolRecordKind::LocalSym
);
1105 cantFail(SymbolDeserializer::deserializeAs
<LocalSym
>(sym
, local
));
1106 if ((local
.Flags
& LocalSymFlags::IsParameter
) == LocalSymFlags::None
)
1108 param_type
= local
.Type
;
1109 param_name
= local
.Name
;
1115 // All parameters should come before the first block/inlinesite. If that
1116 // isn't the case, then perhaps this is bad debug info that doesn't
1117 // contain information about all parameters.
1123 PdbCompilandSymId
param_uid(func_id
.modi
, record_offset
);
1124 clang::QualType qt
= GetOrCreateType(param_type
);
1128 CompilerType param_type_ct
= m_clang
.GetType(qt
);
1129 clang::ParmVarDecl
*param
= m_clang
.CreateParameterDeclaration(
1130 &function_decl
, OptionalClangModuleID(), param_name
.str().c_str(),
1131 param_type_ct
, clang::SC_None
, true);
1132 lldbassert(m_uid_to_decl
.count(toOpaqueUid(param_uid
)) == 0);
1134 m_uid_to_decl
[toOpaqueUid(param_uid
)] = param
;
1135 params
.push_back(param
);
1139 if (!params
.empty() && params
.size() == param_count
)
1140 m_clang
.SetFunctionParameters(&function_decl
, params
);
1143 clang::QualType
PdbAstBuilder::CreateEnumType(PdbTypeSymId id
,
1144 const EnumRecord
&er
) {
1145 clang::DeclContext
*decl_context
= nullptr;
1147 std::tie(decl_context
, uname
) = CreateDeclInfoForType(er
, id
.index
);
1151 clang::QualType underlying_type
= GetOrCreateType(er
.UnderlyingType
);
1152 if (underlying_type
.isNull())
1155 Declaration declaration
;
1156 CompilerType enum_ct
= m_clang
.CreateEnumerationType(
1157 uname
, decl_context
, OptionalClangModuleID(), declaration
,
1158 ToCompilerType(underlying_type
), er
.isScoped());
1160 TypeSystemClang::StartTagDeclarationDefinition(enum_ct
);
1161 TypeSystemClang::SetHasExternalStorage(enum_ct
.GetOpaqueQualType(), true);
1163 return clang::QualType::getFromOpaquePtr(enum_ct
.GetOpaqueQualType());
1166 clang::QualType
PdbAstBuilder::CreateArrayType(const ArrayRecord
&ar
) {
1167 clang::QualType element_type
= GetOrCreateType(ar
.ElementType
);
1169 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1170 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1171 PdbIndex
&index
= pdb
->GetIndex();
1172 uint64_t element_size
= GetSizeOfType({ar
.ElementType
}, index
.tpi());
1173 if (element_type
.isNull() || element_size
== 0)
1175 uint64_t element_count
= ar
.Size
/ element_size
;
1177 CompilerType array_ct
= m_clang
.CreateArrayType(ToCompilerType(element_type
),
1178 element_count
, false);
1179 return clang::QualType::getFromOpaquePtr(array_ct
.GetOpaqueQualType());
1182 clang::QualType
PdbAstBuilder::CreateFunctionType(
1183 TypeIndex args_type_idx
, TypeIndex return_type_idx
,
1184 llvm::codeview::CallingConvention calling_convention
) {
1185 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1186 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1187 PdbIndex
&index
= pdb
->GetIndex();
1188 TpiStream
&stream
= index
.tpi();
1189 CVType args_cvt
= stream
.getType(args_type_idx
);
1192 TypeDeserializer::deserializeAs
<ArgListRecord
>(args_cvt
, args
));
1194 llvm::ArrayRef
<TypeIndex
> arg_indices
= llvm::ArrayRef(args
.ArgIndices
);
1195 bool is_variadic
= IsCVarArgsFunction(arg_indices
);
1197 arg_indices
= arg_indices
.drop_back();
1199 std::vector
<CompilerType
> arg_types
;
1200 arg_types
.reserve(arg_indices
.size());
1202 for (TypeIndex arg_index
: arg_indices
) {
1203 clang::QualType arg_type
= GetOrCreateType(arg_index
);
1204 if (arg_type
.isNull())
1206 arg_types
.push_back(ToCompilerType(arg_type
));
1209 clang::QualType return_type
= GetOrCreateType(return_type_idx
);
1210 if (return_type
.isNull())
1213 std::optional
<clang::CallingConv
> cc
=
1214 TranslateCallingConvention(calling_convention
);
1218 CompilerType return_ct
= ToCompilerType(return_type
);
1219 CompilerType func_sig_ast_type
= m_clang
.CreateFunctionType(
1220 return_ct
, arg_types
.data(), arg_types
.size(), is_variadic
, 0, *cc
);
1222 return clang::QualType::getFromOpaquePtr(
1223 func_sig_ast_type
.GetOpaqueQualType());
1226 static bool isTagDecl(clang::DeclContext
&context
) {
1227 return llvm::isa
<clang::TagDecl
>(&context
);
1230 static bool isFunctionDecl(clang::DeclContext
&context
) {
1231 return llvm::isa
<clang::FunctionDecl
>(&context
);
1234 static bool isBlockDecl(clang::DeclContext
&context
) {
1235 return llvm::isa
<clang::BlockDecl
>(&context
);
1238 void PdbAstBuilder::ParseNamespace(clang::DeclContext
&context
) {
1239 clang::NamespaceDecl
*ns
= llvm::dyn_cast
<clang::NamespaceDecl
>(&context
);
1240 if (m_parsed_namespaces
.contains(ns
))
1242 std::string qname
= ns
->getQualifiedNameAsString();
1243 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1244 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1245 PdbIndex
&index
= pdb
->GetIndex();
1246 TypeIndex ti
{index
.tpi().TypeIndexBegin()};
1247 for (const CVType
&cvt
: index
.tpi().typeArray()) {
1248 PdbTypeSymId tid
{ti
};
1251 if (!IsTagRecord(cvt
))
1254 CVTagRecord tag
= CVTagRecord::create(cvt
);
1256 // Call CreateDeclInfoForType unconditionally so that the namespace info
1257 // gets created. But only call CreateRecordType if the namespace name
1259 clang::DeclContext
*context
= nullptr;
1261 std::tie(context
, uname
) = CreateDeclInfoForType(tag
.asTag(), tid
.index
);
1262 if (!context
|| !context
->isNamespace())
1265 clang::NamespaceDecl
*ns
= llvm::cast
<clang::NamespaceDecl
>(context
);
1266 llvm::StringRef ns_name
= ns
->getName();
1267 if (ns_name
.starts_with(qname
)) {
1268 ns_name
= ns_name
.drop_front(qname
.size());
1269 if (ns_name
.starts_with("::"))
1270 GetOrCreateType(tid
);
1273 ParseAllFunctionsAndNonLocalVars();
1274 m_parsed_namespaces
.insert(ns
);
1277 void PdbAstBuilder::ParseAllTypes() {
1278 llvm::call_once(m_parse_all_types
, [this]() {
1279 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1280 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1281 PdbIndex
&index
= pdb
->GetIndex();
1282 TypeIndex ti
{index
.tpi().TypeIndexBegin()};
1283 for (const CVType
&cvt
: index
.tpi().typeArray()) {
1284 PdbTypeSymId tid
{ti
};
1287 if (!IsTagRecord(cvt
))
1290 GetOrCreateType(tid
);
1295 void PdbAstBuilder::ParseAllFunctionsAndNonLocalVars() {
1296 llvm::call_once(m_parse_functions_and_non_local_vars
, [this]() {
1297 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1298 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1299 PdbIndex
&index
= pdb
->GetIndex();
1300 uint32_t module_count
= index
.dbi().modules().getModuleCount();
1301 for (uint16_t modi
= 0; modi
< module_count
; ++modi
) {
1302 CompilandIndexItem
&cii
= index
.compilands().GetOrCreateCompiland(modi
);
1303 const CVSymbolArray
&symbols
= cii
.m_debug_stream
.getSymbolArray();
1304 auto iter
= symbols
.begin();
1305 while (iter
!= symbols
.end()) {
1306 PdbCompilandSymId sym_id
{modi
, iter
.offset()};
1308 switch (iter
->kind()) {
1311 GetOrCreateFunctionDecl(sym_id
);
1312 iter
= symbols
.at(getScopeEndOffset(*iter
));
1318 GetOrCreateVariableDecl(PdbCompilandSymId(modi
, 0), sym_id
);
1330 static CVSymbolArray
skipFunctionParameters(clang::Decl
&decl
,
1331 const CVSymbolArray
&symbols
) {
1332 clang::FunctionDecl
*func_decl
= llvm::dyn_cast
<clang::FunctionDecl
>(&decl
);
1335 unsigned int params
= func_decl
->getNumParams();
1339 CVSymbolArray result
= symbols
;
1341 while (!result
.empty()) {
1345 CVSymbol sym
= *result
.begin();
1346 result
.drop_front();
1348 if (!isLocalVariableType(sym
.kind()))
1356 void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id
) {
1357 SymbolFileNativePDB
*pdb
= static_cast<SymbolFileNativePDB
*>(
1358 m_clang
.GetSymbolFile()->GetBackingSymbolFile());
1359 PdbIndex
&index
= pdb
->GetIndex();
1360 CVSymbol sym
= index
.ReadSymbolRecord(block_id
);
1361 lldbassert(sym
.kind() == S_GPROC32
|| sym
.kind() == S_LPROC32
||
1362 sym
.kind() == S_BLOCK32
|| sym
.kind() == S_INLINESITE
);
1363 CompilandIndexItem
&cii
=
1364 index
.compilands().GetOrCreateCompiland(block_id
.modi
);
1365 CVSymbolArray symbols
=
1366 cii
.m_debug_stream
.getSymbolArrayForScope(block_id
.offset
);
1368 // Function parameters should already have been created when the function was
1370 if (sym
.kind() == S_GPROC32
|| sym
.kind() == S_LPROC32
)
1372 skipFunctionParameters(*m_uid_to_decl
[toOpaqueUid(block_id
)], symbols
);
1374 symbols
.drop_front();
1375 auto begin
= symbols
.begin();
1376 while (begin
!= symbols
.end()) {
1377 PdbCompilandSymId
child_sym_id(block_id
.modi
, begin
.offset());
1378 GetOrCreateSymbolForId(child_sym_id
);
1379 if (begin
->kind() == S_BLOCK32
|| begin
->kind() == S_INLINESITE
) {
1380 ParseBlockChildren(child_sym_id
);
1381 begin
= symbols
.at(getScopeEndOffset(*begin
));
1387 void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext
&context
) {
1389 clang::Decl
*decl
= clang::Decl::castFromDeclContext(&context
);
1392 auto iter
= m_decl_to_status
.find(decl
);
1393 lldbassert(iter
!= m_decl_to_status
.end());
1395 if (auto *tag
= llvm::dyn_cast
<clang::TagDecl
>(&context
)) {
1396 CompleteTagDecl(*tag
);
1400 if (isFunctionDecl(context
) || isBlockDecl(context
)) {
1401 PdbCompilandSymId block_id
= PdbSymUid(iter
->second
.uid
).asCompilandSym();
1402 ParseBlockChildren(block_id
);
1406 void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext
&context
) {
1407 // Namespaces aren't explicitly represented in the debug info, and the only
1408 // way to parse them is to parse all type info, demangling every single type
1409 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1410 // is an expensive operation, we have to special case it so that we do other
1411 // work (such as parsing the items that appear within the namespaces) at the
1413 if (context
.isTranslationUnit()) {
1415 ParseAllFunctionsAndNonLocalVars();
1419 if (context
.isNamespace()) {
1420 ParseNamespace(context
);
1424 if (isTagDecl(context
) || isFunctionDecl(context
) || isBlockDecl(context
)) {
1425 ParseDeclsForSimpleContext(context
);
1430 CompilerDecl
PdbAstBuilder::ToCompilerDecl(clang::Decl
&decl
) {
1431 return m_clang
.GetCompilerDecl(&decl
);
1434 CompilerType
PdbAstBuilder::ToCompilerType(clang::QualType qt
) {
1435 return {m_clang
.weak_from_this(), qt
.getAsOpaquePtr()};
1439 PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext
&context
) {
1440 return m_clang
.CreateDeclContext(&context
);
1443 clang::Decl
* PdbAstBuilder::FromCompilerDecl(CompilerDecl decl
) {
1444 return ClangUtil::GetDecl(decl
);
1447 clang::DeclContext
*
1448 PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context
) {
1449 return static_cast<clang::DeclContext
*>(context
.GetOpaqueDeclContext());
1452 void PdbAstBuilder::Dump(Stream
&stream
) {
1453 m_clang
.Dump(stream
.AsRawOstream());