Skip to content

Commit 8a33a7f

Browse files
authored
Merge pull request #6099 from augusto2112/1013-type-in-list
[lldb] Adapt lldb-swift usage of types to always be owned by TypeLists
2 parents 8ff7fd3 + 2656fd5 commit 8a33a7f

File tree

11 files changed

+183
-134
lines changed

11 files changed

+183
-134
lines changed

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,21 @@ class SymbolFile : public PluginInterface {
444444
virtual bool GetDebugInfoHadFrameVariableErrors() const = 0;
445445
virtual void SetDebugInfoHadFrameVariableErrors() = 0;
446446

447+
/// This function is used to create types that belong to a SymbolFile. The
448+
/// symbol file will own a strong reference to the type in an internal type
449+
/// list.
450+
virtual lldb::TypeSP MakeType(lldb::user_id_t uid, ConstString name,
451+
llvm::Optional<uint64_t> byte_size,
452+
SymbolContextScope *context,
453+
lldb::user_id_t encoding_uid,
454+
Type::EncodingDataType encoding_uid_type,
455+
const Declaration &decl,
456+
const CompilerType &compiler_qual_type,
457+
Type::ResolveState compiler_type_resolve_state,
458+
uint32_t opaque_payload = 0) = 0;
459+
460+
virtual lldb::TypeSP CopyType(const lldb::TypeSP &other_type) = 0;
461+
447462
protected:
448463
void AssertModuleLock();
449464

@@ -524,6 +539,31 @@ class SymbolFileCommon : public SymbolFile {
524539
m_debug_info_had_variable_errors = true;
525540
}
526541

542+
lldb::TypeSP MakeType(lldb::user_id_t uid, ConstString name,
543+
llvm::Optional<uint64_t> byte_size,
544+
SymbolContextScope *context,
545+
lldb::user_id_t encoding_uid,
546+
Type::EncodingDataType encoding_uid_type,
547+
const Declaration &decl,
548+
const CompilerType &compiler_qual_type,
549+
Type::ResolveState compiler_type_resolve_state,
550+
uint32_t opaque_payload = 0) override {
551+
lldb::TypeSP type_sp(new Type(
552+
uid, this, name, byte_size, context, encoding_uid, encoding_uid_type,
553+
decl, compiler_qual_type, compiler_type_resolve_state, opaque_payload));
554+
m_type_list.Insert(type_sp);
555+
return type_sp;
556+
}
557+
558+
lldb::TypeSP CopyType(const lldb::TypeSP &other_type) override {
559+
// Make sure the real symbol file matches when copying types.
560+
if (GetBackingSymbolFile() != other_type->GetSymbolFile())
561+
return lldb::TypeSP();
562+
lldb::TypeSP type_sp(new Type(*other_type));
563+
m_type_list.Insert(type_sp);
564+
return type_sp;
565+
}
566+
527567
protected:
528568
virtual uint32_t CalculateNumCompileUnits() = 0;
529569
virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0;

lldb/include/lldb/Symbol/SymbolFileOnDemand.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
223223
return m_sym_file_impl->SetDebugInfoHadFrameVariableErrors();
224224
}
225225

226+
lldb::TypeSP MakeType(lldb::user_id_t uid, ConstString name,
227+
llvm::Optional<uint64_t> byte_size,
228+
SymbolContextScope *context,
229+
lldb::user_id_t encoding_uid,
230+
Type::EncodingDataType encoding_uid_type,
231+
const Declaration &decl,
232+
const CompilerType &compiler_qual_type,
233+
Type::ResolveState compiler_type_resolve_state,
234+
uint32_t opaque_payload = 0) override {
235+
return m_sym_file_impl->MakeType(
236+
uid, name, byte_size, context, encoding_uid, encoding_uid_type, decl,
237+
compiler_qual_type, compiler_type_resolve_state, opaque_payload);
238+
}
239+
240+
lldb::TypeSP CopyType(const lldb::TypeSP &other_type) override {
241+
return m_sym_file_impl->CopyType(other_type);
242+
}
243+
226244
private:
227245
Log *GetLog() const { return ::lldb_private::GetLog(LLDBLog::OnDemand); }
228246

lldb/include/lldb/Symbol/Type.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <set>
2222

2323
namespace lldb_private {
24+
class SymbolFileCommon;
2425

2526
/// CompilerContext allows an array of these items to be passed to perform
2627
/// detailed lookups in SymbolVendor and SymbolFile functions.
@@ -100,16 +101,6 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
100101
Full = 3
101102
};
102103

103-
Type(lldb::user_id_t uid, SymbolFile *symbol_file, ConstString name,
104-
llvm::Optional<uint64_t> byte_size, SymbolContextScope *context,
105-
lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
106-
const Declaration &decl, const CompilerType &compiler_qual_type,
107-
ResolveState compiler_type_resolve_state, uint32_t opaque_payload = 0);
108-
109-
// This makes an invalid type. Used for functions that return a Type when
110-
// they get an error.
111-
Type();
112-
113104
void Dump(Stream *s, bool show_context,
114105
lldb::DescriptionLevel level = lldb::eDescriptionLevelFull);
115106

@@ -239,6 +230,30 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
239230
Type *GetEncodingType();
240231

241232
bool ResolveCompilerType(ResolveState compiler_type_resolve_state);
233+
private:
234+
/// Only allow Symbol File to create types, as they should own them by keeping
235+
/// them in their TypeList. \see SymbolFile::MakeType() reference in the
236+
/// header documentation here so users will know what function to use if the
237+
/// get a compile error.
238+
friend class lldb_private::SymbolFileCommon;
239+
240+
Type(lldb::user_id_t uid, SymbolFile *symbol_file, ConstString name,
241+
llvm::Optional<uint64_t> byte_size, SymbolContextScope *context,
242+
lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
243+
const Declaration &decl, const CompilerType &compiler_qual_type,
244+
ResolveState compiler_type_resolve_state, uint32_t opaque_payload = 0);
245+
246+
// This makes an invalid type. Used for functions that return a Type when
247+
// they get an error.
248+
Type();
249+
250+
Type(Type &t) = default;
251+
252+
Type(Type &&t) = default;
253+
254+
Type &operator=(const Type &t) = default;
255+
256+
Type &operator=(Type &&t) = default;
242257
};
243258

244259
// the two classes here are used by the public API as a backend to the SBType

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ static bool AddVariableInfo(
353353
0, variable_sp->GetName().GetCString(), "",
354354
std::make_shared<lldb_private::SymbolFileType>(
355355
*variable_sp->GetType()->GetSymbolFile(),
356-
std::make_shared<lldb_private::Type>(
357-
0, variable_sp->GetType()->GetSymbolFile(),
358-
variable_sp->GetType()->GetName(), llvm::None,
356+
variable_sp->GetType()->GetSymbolFile()->MakeType(
357+
0, variable_sp->GetType()->GetName(), llvm::None,
359358
variable_sp->GetType()->GetSymbolContextScope(), LLDB_INVALID_UID,
360359
Type::eEncodingIsUID, variable_sp->GetType()->GetDeclaration(),
361360
target_type, lldb_private::Type::ResolveState::Full,

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,11 @@ TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
197197
GetClangASTImporter().RequireCompleteType(ClangUtil::GetQualType(type));
198198

199199
SymbolFileDWARF *dwarf = die.GetDWARF();
200-
TypeSP type_sp(new Type(die.GetID(), dwarf, pcm_type_sp->GetName(),
201-
pcm_type_sp->GetByteSize(nullptr), nullptr,
202-
LLDB_INVALID_UID, Type::eEncodingInvalid,
203-
&pcm_type_sp->GetDeclaration(), type,
204-
Type::ResolveState::Forward,
205-
TypePayloadClang(GetOwningClangModule(die))));
206-
207-
dwarf->GetTypeList().Insert(type_sp);
200+
auto type_sp = dwarf->MakeType(
201+
die.GetID(), pcm_type_sp->GetName(), pcm_type_sp->GetByteSize(nullptr),
202+
nullptr, LLDB_INVALID_UID, Type::eEncodingInvalid,
203+
&pcm_type_sp->GetDeclaration(), type, Type::ResolveState::Forward,
204+
TypePayloadClang(GetOwningClangModule(die)));
208205
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
209206
clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
210207
if (tag_decl) {
@@ -753,8 +750,8 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
753750
}
754751
}
755752

756-
type_sp = std::make_shared<Type>(
757-
die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
753+
type_sp = dwarf->MakeType(
754+
die.GetID(), attrs.name, attrs.byte_size, nullptr,
758755
dwarf->GetUID(attrs.type.Reference()), encoding_data_type, &attrs.decl,
759756
clang_type, resolve_state, TypePayloadClang(GetOwningClangModule(die)));
760757

@@ -847,11 +844,11 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
847844

848845
LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);
849846

850-
type_sp = std::make_shared<Type>(
851-
die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
852-
dwarf->GetUID(attrs.type.Reference()), Type::eEncodingIsUID, &attrs.decl,
853-
clang_type, Type::ResolveState::Forward,
854-
TypePayloadClang(GetOwningClangModule(die)));
847+
type_sp = dwarf->MakeType(die.GetID(), attrs.name, attrs.byte_size, nullptr,
848+
dwarf->GetUID(attrs.type.Reference()),
849+
Type::eEncodingIsUID, &attrs.decl, clang_type,
850+
Type::ResolveState::Forward,
851+
TypePayloadClang(GetOwningClangModule(die)));
855852

856853
if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
857854
if (die.HasChildren()) {
@@ -1297,9 +1294,9 @@ TypeSP DWARFASTParserClang::ParseSubroutine(const DWARFDIE &die,
12971294
}
12981295
}
12991296
}
1300-
return std::make_shared<Type>(
1301-
die.GetID(), dwarf, attrs.name, llvm::None, nullptr, LLDB_INVALID_UID,
1302-
Type::eEncodingIsUID, &attrs.decl, clang_type, Type::ResolveState::Full);
1297+
return dwarf->MakeType(die.GetID(), attrs.name, llvm::None, nullptr,
1298+
LLDB_INVALID_UID, Type::eEncodingIsUID, &attrs.decl,
1299+
clang_type, Type::ResolveState::Full);
13031300
}
13041301

13051302
TypeSP
@@ -1347,10 +1344,10 @@ DWARFASTParserClang::ParseArrayType(const DWARFDIE &die,
13471344
m_ast.CreateArrayType(array_element_type, 0, attrs.is_vector);
13481345
}
13491346
ConstString empty_name;
1350-
TypeSP type_sp = std::make_shared<Type>(
1351-
die.GetID(), dwarf, empty_name, array_element_bit_stride / 8, nullptr,
1352-
dwarf->GetUID(type_die), Type::eEncodingIsUID, &attrs.decl, clang_type,
1353-
Type::ResolveState::Full);
1347+
TypeSP type_sp =
1348+
dwarf->MakeType(die.GetID(), empty_name, array_element_bit_stride / 8,
1349+
nullptr, dwarf->GetUID(type_die), Type::eEncodingIsUID,
1350+
&attrs.decl, clang_type, Type::ResolveState::Full);
13541351
type_sp->SetEncodingType(element_type);
13551352
const clang::Type *type = ClangUtil::GetQualType(clang_type).getTypePtr();
13561353
m_ast.SetMetadataAsUserID(type, die.GetID());
@@ -1372,10 +1369,9 @@ TypeSP DWARFASTParserClang::ParsePointerToMemberType(
13721369

13731370
if (llvm::Optional<uint64_t> clang_type_size =
13741371
clang_type.GetByteSize(nullptr)) {
1375-
return std::make_shared<Type>(die.GetID(), dwarf, attrs.name,
1376-
*clang_type_size, nullptr, LLDB_INVALID_UID,
1377-
Type::eEncodingIsUID, nullptr, clang_type,
1378-
Type::ResolveState::Forward);
1372+
return dwarf->MakeType(die.GetID(), attrs.name, *clang_type_size, nullptr,
1373+
LLDB_INVALID_UID, Type::eEncodingIsUID, nullptr,
1374+
clang_type, Type::ResolveState::Forward);
13791375
}
13801376
return nullptr;
13811377
}
@@ -1771,9 +1767,9 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
17711767
// parameters in any class methods need it for the clang types for
17721768
// function prototypes.
17731769
LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
1774-
type_sp = std::make_shared<Type>(
1775-
die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
1776-
LLDB_INVALID_UID, Type::eEncodingIsUID, &attrs.decl, clang_type,
1770+
type_sp = dwarf->MakeType(
1771+
die.GetID(), attrs.name, attrs.byte_size, nullptr, LLDB_INVALID_UID,
1772+
Type::eEncodingIsUID, &attrs.decl, clang_type,
17771773
Type::ResolveState::Forward,
17781774
TypePayloadClang(OptionalClangModuleID(), attrs.is_complete_objc_class));
17791775

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
134134
if (name.GetStringRef().equals("$swift.fixedbuffer")) {
135135
if (auto wrapped_type = get_type(die.GetFirstChild())) {
136136
// Create a unique pointer for the type + fixed buffer flag.
137-
type_sp.reset(new Type(*wrapped_type));
137+
type_sp = wrapped_type->GetSymbolFile()->CopyType(wrapped_type);
138138
type_sp->SetPayload(TypePayloadSwift(true));
139139
return type_sp;
140140
}
@@ -194,13 +194,13 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
194194
}
195195

196196
if (compiler_type) {
197-
type_sp = TypeSP(new Type(
198-
die.GetID(), die.GetDWARF(),
197+
type_sp = die.GetDWARF()->MakeType(
198+
die.GetID(),
199199
preferred_name ? preferred_name : compiler_type.GetTypeName(),
200200
// We don't have an exe_scope here by design, so we need to
201201
// read the size from DWARF.
202202
dwarf_byte_size, nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, &decl,
203-
compiler_type, Type::ResolveState::Full));
203+
compiler_type, Type::ResolveState::Full);
204204
}
205205

206206
// Cache this type.

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,8 +3112,6 @@ TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
31123112

31133113
TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, type_is_new_ptr);
31143114
if (type_sp) {
3115-
GetTypeList().Insert(type_sp);
3116-
31173115
if (die.Tag() == DW_TAG_subprogram) {
31183116
std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
31193117
.GetScopeQualifiedName()

0 commit comments

Comments
 (0)