Skip to content

Commit 1d58cd9

Browse files
[LLDB][NativePDB] NFC: Add language-independent interface methods for PdbAstParser
1 parent 854ef8d commit 1d58cd9

File tree

4 files changed

+99
-89
lines changed

4 files changed

+99
-89
lines changed

lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
220220
// recurse into our lazy type creation / AST reconstruction logic to get an
221221
// LLDB TypeSP for the parent. This will cause the AST to automatically get
222222
// the right DeclContext created for any parent.
223-
clang::QualType parent_qt = GetOrCreateType(*parent_index);
223+
clang::QualType parent_qt = GetOrCreateClangType(*parent_index);
224224
if (parent_qt.isNull())
225225
return {nullptr, ""};
226226

@@ -247,7 +247,7 @@ clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) {
247247
CVSymbol cvs = index.ReadSymbolRecord(id);
248248

249249
if (isLocalVariableType(cvs.kind())) {
250-
clang::DeclContext *scope = GetParentDeclContext(id);
250+
clang::DeclContext *scope = GetParentClangDeclContext(id);
251251
if (!scope)
252252
return nullptr;
253253
clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
@@ -286,7 +286,7 @@ PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
286286
result = GetOrCreateSymbolForId(uid.asCompilandSym());
287287
break;
288288
case PdbSymUidKind::Type: {
289-
clang::QualType qt = GetOrCreateType(uid.asTypeSym());
289+
clang::QualType qt = GetOrCreateClangType(uid.asTypeSym());
290290
if (qt.isNull())
291291
return std::nullopt;
292292
if (auto *tag = qt->getAsTagDecl()) {
@@ -305,7 +305,7 @@ PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
305305
return ToCompilerDecl(*result);
306306
}
307307

308-
clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
308+
clang::DeclContext *PdbAstBuilder::GetOrCreateClangDeclContextForUid(PdbSymUid uid) {
309309
if (uid.kind() == PdbSymUidKind::CompilandSym) {
310310
if (uid.asCompilandSym().offset == 0)
311311
return FromCompilerDeclContext(GetTranslationUnitDecl());
@@ -320,6 +320,13 @@ clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
320320
return clang::Decl::castToDeclContext(decl);
321321
}
322322

323+
CompilerDeclContext PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
324+
clang::DeclContext *context = GetOrCreateClangDeclContextForUid(uid);
325+
if (!context)
326+
return {};
327+
return ToCompilerDeclContext(*context);
328+
}
329+
323330
std::pair<clang::DeclContext *, std::string>
324331
PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
325332
SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
@@ -340,7 +347,7 @@ PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
340347
// It might be a class name, try that first.
341348
std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
342349
while (!types.empty()) {
343-
clang::QualType qt = GetOrCreateType(types.back());
350+
clang::QualType qt = GetOrCreateClangType(types.back());
344351
if (qt.isNull())
345352
continue;
346353
clang::TagDecl *tag = qt->getAsTagDecl();
@@ -357,7 +364,7 @@ PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
357364
return {context, std::string(uname)};
358365
}
359366

360-
clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
367+
clang::DeclContext *PdbAstBuilder::GetParentClangDeclContext(PdbSymUid uid) {
361368
// We must do this *without* calling GetOrCreate on the current uid, as
362369
// that would be an infinite recursion.
363370
SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
@@ -368,7 +375,7 @@ clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
368375
std::optional<PdbCompilandSymId> scope =
369376
pdb->FindSymbolScope(uid.asCompilandSym());
370377
if (scope)
371-
return GetOrCreateDeclContextForUid(*scope);
378+
return GetOrCreateClangDeclContextForUid(*scope);
372379

373380
CVSymbol sym = index.ReadSymbolRecord(uid.asCompilandSym());
374381
return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
@@ -380,7 +387,7 @@ clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
380387
std::optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
381388
if (!parent_index)
382389
return FromCompilerDeclContext(GetTranslationUnitDecl());
383-
return GetOrCreateDeclContextForUid(PdbTypeSymId(*parent_index));
390+
return GetOrCreateClangDeclContextForUid(PdbTypeSymId(*parent_index));
384391
}
385392
case PdbSymUidKind::FieldListMember:
386393
// In this case the parent DeclContext is the one for the class that this
@@ -404,7 +411,7 @@ clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
404411
llvm::cantFail(
405412
SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
406413
PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
407-
return GetParentDeclContext(cu_sym_id);
414+
return GetParentClangDeclContext(cu_sym_id);
408415
}
409416
case SymbolKind::S_CONSTANT:
410417
case SymbolKind::S_UDT:
@@ -420,7 +427,16 @@ clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
420427
return FromCompilerDeclContext(GetTranslationUnitDecl());
421428
}
422429

423-
bool PdbAstBuilder::CompleteType(clang::QualType qt) {
430+
CompilerDeclContext PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
431+
clang::DeclContext *context = GetParentClangDeclContext(uid);
432+
if (!context)
433+
return {};
434+
return ToCompilerDeclContext(*context);
435+
}
436+
437+
bool PdbAstBuilder::CompleteType(CompilerType ct) {
438+
clang::QualType qt =
439+
clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
424440
if (qt.isNull())
425441
return false;
426442
clang::TagDecl *tag = qt->getAsTagDecl();
@@ -499,7 +515,7 @@ clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
499515
return GetBasicType(lldb::eBasicTypeNullPtr);
500516

501517
if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
502-
clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
518+
clang::QualType direct_type = GetOrCreateClangType(ti.makeDirect());
503519
if (direct_type.isNull())
504520
return {};
505521
return m_clang.getASTContext().getPointerType(direct_type);
@@ -516,7 +532,7 @@ clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
516532
}
517533

518534
clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
519-
clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
535+
clang::QualType pointee_type = GetOrCreateClangType(pointer.ReferentType);
520536

521537
// This can happen for pointers to LF_VTSHAPE records, which we shouldn't
522538
// create in the AST.
@@ -525,7 +541,7 @@ clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
525541

526542
if (pointer.isPointerToMember()) {
527543
MemberPointerInfo mpi = pointer.getMemberInfo();
528-
clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
544+
clang::QualType class_type = GetOrCreateClangType(mpi.ContainingType);
529545
if (class_type.isNull())
530546
return {};
531547
if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
@@ -589,7 +605,7 @@ clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
589605

590606
clang::QualType
591607
PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
592-
clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
608+
clang::QualType unmodified_type = GetOrCreateClangType(modifier.ModifiedType);
593609
if (unmodified_type.isNull())
594610
return {};
595611

@@ -659,7 +675,7 @@ PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
659675
if (clang::Decl *decl = TryGetDecl(block_id))
660676
return llvm::dyn_cast<clang::BlockDecl>(decl);
661677

662-
clang::DeclContext *scope = GetParentDeclContext(block_id);
678+
clang::DeclContext *scope = GetParentClangDeclContext(block_id);
663679

664680
clang::BlockDecl *block_decl =
665681
m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID());
@@ -676,7 +692,7 @@ PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
676692
clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
677693
clang::DeclContext &scope) {
678694
VariableInfo var_info = GetVariableNameInfo(sym);
679-
clang::QualType qt = GetOrCreateType(var_info.type);
695+
clang::QualType qt = GetOrCreateClangType(var_info.type);
680696
if (qt.isNull())
681697
return nullptr;
682698

@@ -697,7 +713,7 @@ PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id,
697713
if (clang::Decl *decl = TryGetDecl(var_id))
698714
return llvm::dyn_cast<clang::VarDecl>(decl);
699715

700-
clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
716+
clang::DeclContext *scope = GetOrCreateClangDeclContextForUid(scope_id);
701717
if (!scope)
702718
return nullptr;
703719

@@ -732,10 +748,10 @@ PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) {
732748
lldbassert(sym.kind() == S_UDT);
733749
UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
734750

735-
clang::DeclContext *scope = GetParentDeclContext(id);
751+
clang::DeclContext *scope = GetParentClangDeclContext(id);
736752

737753
PdbTypeSymId real_type_id{udt.Type, false};
738-
clang::QualType qt = GetOrCreateType(real_type_id);
754+
clang::QualType qt = GetOrCreateClangType(real_type_id);
739755
if (qt.isNull() || !scope)
740756
return nullptr;
741757

@@ -810,7 +826,7 @@ clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) {
810826
return {};
811827
}
812828

813-
clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
829+
clang::QualType PdbAstBuilder::GetOrCreateClangType(PdbTypeSymId type) {
814830
if (type.index.isNoneType())
815831
return {};
816832

@@ -828,7 +844,7 @@ clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
828844
if (best_type.index != type.index) {
829845
// This is a forward decl. Call GetOrCreate on the full decl, then map the
830846
// forward decl id to the full decl QualType.
831-
clang::QualType qt = GetOrCreateType(best_type);
847+
clang::QualType qt = GetOrCreateClangType(best_type);
832848
if (qt.isNull())
833849
return {};
834850
m_uid_to_type[toOpaqueUid(type)] = qt;
@@ -853,6 +869,13 @@ clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
853869
return qt;
854870
}
855871

872+
CompilerType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
873+
clang::QualType qt = GetOrCreateClangType(type);
874+
if (qt.isNull())
875+
return {};
876+
return ToCompilerType(qt);
877+
}
878+
856879
clang::FunctionDecl *
857880
PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id,
858881
llvm::StringRef func_name, TypeIndex func_ti,
@@ -986,7 +1009,7 @@ PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid,
9861009
func_name = mfr.getName();
9871010
func_ti = mfr.getFunctionType();
9881011
PdbTypeSymId class_type_id(mfr.ClassType, false);
989-
parent = GetOrCreateDeclContextForUid(class_type_id);
1012+
parent = GetOrCreateClangDeclContextForUid(class_type_id);
9901013
break;
9911014
}
9921015
case LF_FUNC_ID: {
@@ -1009,7 +1032,7 @@ PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid,
10091032
default:
10101033
lldbassert(false && "Invalid function id type!");
10111034
}
1012-
clang::QualType func_qt = GetOrCreateType(func_ti);
1035+
clang::QualType func_qt = GetOrCreateClangType(func_ti);
10131036
if (func_qt.isNull() || !parent)
10141037
return nullptr;
10151038
CompilerType func_ct = ToCompilerType(func_qt);
@@ -1024,7 +1047,7 @@ PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
10241047
if (clang::Decl *decl = TryGetDecl(func_id))
10251048
return llvm::dyn_cast<clang::FunctionDecl>(decl);
10261049

1027-
clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
1050+
clang::DeclContext *parent = GetParentClangDeclContext(PdbSymUid(func_id));
10281051
if (!parent)
10291052
return nullptr;
10301053
std::string context_name;
@@ -1042,7 +1065,7 @@ PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
10421065
llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
10431066

10441067
PdbTypeSymId type_id(proc.FunctionType);
1045-
clang::QualType qt = GetOrCreateType(type_id);
1068+
clang::QualType qt = GetOrCreateClangType(type_id);
10461069
if (qt.isNull())
10471070
return nullptr;
10481071

@@ -1130,7 +1153,7 @@ void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id,
11301153
}
11311154

11321155
PdbCompilandSymId param_uid(func_id.modi, record_offset);
1133-
clang::QualType qt = GetOrCreateType(param_type);
1156+
clang::QualType qt = GetOrCreateClangType(param_type);
11341157
if (qt.isNull())
11351158
return;
11361159

@@ -1157,7 +1180,7 @@ clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
11571180
if (!decl_context)
11581181
return {};
11591182

1160-
clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1183+
clang::QualType underlying_type = GetOrCreateClangType(er.UnderlyingType);
11611184
if (underlying_type.isNull())
11621185
return {};
11631186

@@ -1173,7 +1196,7 @@ clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
11731196
}
11741197

11751198
clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1176-
clang::QualType element_type = GetOrCreateType(ar.ElementType);
1199+
clang::QualType element_type = GetOrCreateClangType(ar.ElementType);
11771200
TypeSystemClang::RequireCompleteType(ToCompilerType(element_type));
11781201

11791202
SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
@@ -1210,13 +1233,13 @@ clang::QualType PdbAstBuilder::CreateFunctionType(
12101233
arg_types.reserve(arg_indices.size());
12111234

12121235
for (TypeIndex arg_index : arg_indices) {
1213-
clang::QualType arg_type = GetOrCreateType(arg_index);
1236+
clang::QualType arg_type = GetOrCreateClangType(arg_index);
12141237
if (arg_type.isNull())
12151238
continue;
12161239
arg_types.push_back(ToCompilerType(arg_type));
12171240
}
12181241

1219-
clang::QualType return_type = GetOrCreateType(return_type_idx);
1242+
clang::QualType return_type = GetOrCreateClangType(return_type_idx);
12201243
if (return_type.isNull())
12211244
return {};
12221245

@@ -1277,7 +1300,7 @@ void PdbAstBuilder::ParseNamespace(clang::DeclContext &context) {
12771300
if (ns_name.starts_with(qname)) {
12781301
ns_name = ns_name.drop_front(qname.size());
12791302
if (ns_name.starts_with("::"))
1280-
GetOrCreateType(tid);
1303+
GetOrCreateClangType(tid);
12811304
}
12821305
}
12831306
ParseAllFunctionsAndNonLocalVars();
@@ -1297,7 +1320,7 @@ void PdbAstBuilder::ParseAllTypes() {
12971320
if (!IsTagRecord(cvt))
12981321
continue;
12991322

1300-
GetOrCreateType(tid);
1323+
GetOrCreateClangType(tid);
13011324
}
13021325
});
13031326
}
@@ -1413,26 +1436,30 @@ void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
14131436
}
14141437
}
14151438

1416-
void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1439+
void PdbAstBuilder::ParseDeclsForContext(CompilerDeclContext context) {
1440+
clang::DeclContext *dc = FromCompilerDeclContext(context);
1441+
if (!dc)
1442+
return;
1443+
14171444
// Namespaces aren't explicitly represented in the debug info, and the only
14181445
// way to parse them is to parse all type info, demangling every single type
14191446
// and trying to reconstruct the DeclContext hierarchy this way. Since this
14201447
// is an expensive operation, we have to special case it so that we do other
14211448
// work (such as parsing the items that appear within the namespaces) at the
14221449
// same time.
1423-
if (context.isTranslationUnit()) {
1450+
if (dc->isTranslationUnit()) {
14241451
ParseAllTypes();
14251452
ParseAllFunctionsAndNonLocalVars();
14261453
return;
14271454
}
14281455

1429-
if (context.isNamespace()) {
1430-
ParseNamespace(context);
1456+
if (dc->isNamespace()) {
1457+
ParseNamespace(*dc);
14311458
return;
14321459
}
14331460

1434-
if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1435-
ParseDeclsForSimpleContext(context);
1461+
if (isTagDecl(*dc) || isFunctionDecl(*dc) || isBlockDecl(*dc)) {
1462+
ParseDeclsForSimpleContext(*dc);
14361463
return;
14371464
}
14381465
}
@@ -1464,14 +1491,16 @@ void PdbAstBuilder::Dump(Stream &stream, llvm::StringRef filter,
14641491
m_clang.Dump(stream.AsRawOstream(), filter, show_color);
14651492
}
14661493

1467-
clang::NamespaceDecl *
1468-
PdbAstBuilder::FindNamespaceDecl(const clang::DeclContext *parent,
1469-
llvm::StringRef name) {
1470-
NamespaceSet *set;
1494+
CompilerDeclContext
1495+
PdbAstBuilder::FindNamespaceDecl(CompilerDeclContext parent_ctx,
1496+
llvm::StringRef name) {
1497+
clang::DeclContext *parent = FromCompilerDeclContext(parent_ctx);
1498+
NamespaceSet *set;
1499+
14711500
if (parent) {
14721501
auto it = m_parent_to_namespaces.find(parent);
14731502
if (it == m_parent_to_namespaces.end())
1474-
return nullptr;
1503+
return {};
14751504

14761505
set = &it->second;
14771506
} else {
@@ -1482,11 +1511,11 @@ PdbAstBuilder::FindNamespaceDecl(const clang::DeclContext *parent,
14821511

14831512
for (clang::NamespaceDecl *namespace_decl : *set)
14841513
if (namespace_decl->getName() == name)
1485-
return namespace_decl;
1514+
return ToCompilerDeclContext(*namespace_decl);
14861515

14871516
for (clang::NamespaceDecl *namespace_decl : *set)
14881517
if (namespace_decl->isAnonymousNamespace())
1489-
return FindNamespaceDecl(namespace_decl, name);
1518+
return FindNamespaceDecl(ToCompilerDeclContext(*namespace_decl), name);
14901519

1491-
return nullptr;
1520+
return {};
14921521
}

0 commit comments

Comments
 (0)