Skip to content

Commit 1338be5

Browse files
committed
[lldb] Refactor deduction of the instance variable's name (NFC)
Move responsibility of providing the instance variable name (`this`, `self`) from `TypeSystem` to `Language`. `Language` the natural place for this, but also has downstream benefits. Some languages have multiple `TypeSystem` implementations (ex Swift), and by placing this logic in the `Language`, redundancy is avoided. This change relies on the tests from D145348 and D146320. Differential Revision: https://reviews.llvm.org/D146548 (cherry picked from commit 0a7488d72ceb59942cc14f567d77f2e97f72a2e4)
1 parent 4faf411 commit 1338be5

File tree

10 files changed

+20
-43
lines changed

10 files changed

+20
-43
lines changed

lldb/include/lldb/Symbol/CompilerDeclContext.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ class CompilerDeclContext {
6969
/// Determines the original language of the decl context.
7070
lldb::LanguageType GetLanguage();
7171

72-
/// Determines the name of the instance variable for the this decl context.
73-
///
74-
/// For C++ the name is "this", for Objective-C the name is "self".
75-
///
76-
/// \return
77-
/// Returns a string for the name of the instance variable.
78-
ConstString GetInstanceVariableName(lldb::LanguageType language);
79-
8072
/// Check if the given other decl context is contained in the lookup
8173
/// of this decl context (for example because the other context is a nested
8274
/// inline namespace).

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,6 @@ class TypeSystem : public PluginInterface,
217217
// an error.
218218
virtual Status IsCompatible();
219219

220-
/// The name of the variable used for explicitly accessing data scoped to the
221-
/// current instance (or type). C++ uses "this", ObjC uses "self".
222-
virtual ConstString GetInstanceVariableName(lldb::LanguageType language) = 0;
223-
224220
// Type Completion
225221

226222
virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;

lldb/include/lldb/Target/Language.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class Language : public PluginInterface {
318318
return ConstString();
319319
}
320320

321+
virtual ConstString GetInstanceVariableName() { return {}; }
322+
321323
protected:
322324
// Classes that inherit from Language can see and modify these
323325

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class CPlusPlusLanguage : public Language {
153153
ConstString FindBestAlternateFunctionMangledName(
154154
const Mangled mangled, const SymbolContext &sym_ctx) const override;
155155

156+
ConstString GetInstanceVariableName() override { return ConstString("this"); }
157+
156158
// PluginInterface protocol
157159
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
158160
};

lldb/source/Plugins/Language/ObjC/ObjCLanguage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class ObjCLanguage : public Language {
155155
return false;
156156
}
157157

158+
ConstString GetInstanceVariableName() override { return ConstString("self"); }
159+
158160
// PluginInterface protocol
159161
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
160162
};

lldb/source/Plugins/Language/ObjCPlusPlus/ObjCPlusPlusLanguage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class ObjCPlusPlusLanguage : public Language {
4040

4141
static lldb_private::Language *CreateInstance(lldb::LanguageType language);
4242

43+
ConstString GetInstanceVariableName() override { return ConstString("self"); }
44+
4345
static llvm::StringRef GetPluginNameStatic() { return "objcplusplus"; }
4446

4547
// PluginInterface protocol

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,22 +3709,6 @@ bool TypeSystemClang::SupportsLanguage(lldb::LanguageType language) {
37093709
return TypeSystemClangSupportsLanguage(language);
37103710
}
37113711

3712-
ConstString
3713-
TypeSystemClang::GetInstanceVariableName(lldb::LanguageType language) {
3714-
switch (language) {
3715-
case LanguageType::eLanguageTypeC_plus_plus:
3716-
case LanguageType::eLanguageTypeC_plus_plus_03:
3717-
case LanguageType::eLanguageTypeC_plus_plus_11:
3718-
case LanguageType::eLanguageTypeC_plus_plus_14:
3719-
return ConstString("this");
3720-
case LanguageType::eLanguageTypeObjC:
3721-
case LanguageType::eLanguageTypeObjC_plus_plus:
3722-
return ConstString("self");
3723-
default:
3724-
return {};
3725-
}
3726-
}
3727-
37283712
Optional<std::string>
37293713
TypeSystemClang::GetCXXClassName(const CompilerType &type) {
37303714
if (!type)

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ class TypeSystemClang : public TypeSystem {
704704

705705
bool SupportsLanguage(lldb::LanguageType language) override;
706706

707-
ConstString GetInstanceVariableName(lldb::LanguageType language) override;
708-
709707
static llvm::Optional<std::string> GetCXXClassName(const CompilerType &type);
710708

711709
// Type Completion

lldb/source/Symbol/CompilerDeclContext.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ lldb::LanguageType CompilerDeclContext::GetLanguage() {
4646
return {};
4747
}
4848

49-
ConstString
50-
CompilerDeclContext::GetInstanceVariableName(lldb::LanguageType language) {
51-
if (IsValid())
52-
return m_type_system->GetInstanceVariableName(language);
53-
return {};
54-
}
55-
5649
bool CompilerDeclContext::IsContainedInLookup(CompilerDeclContext other) const {
5750
if (!IsValid())
5851
return false;

lldb/source/Symbol/SymbolContext.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
#include "lldb/Symbol/SymbolFile.h"
2020
#include "lldb/Symbol/SymbolVendor.h"
2121
#include "lldb/Symbol/Variable.h"
22+
#include "lldb/Target/Language.h"
2223
#include "lldb/Target/Target.h"
2324
#include "lldb/Utility/LLDBLog.h"
2425
#include "lldb/Utility/Log.h"
2526
#include "lldb/Utility/StreamString.h"
27+
#include "lldb/lldb-enumerations.h"
2628

2729
using namespace lldb;
2830
using namespace lldb_private;
@@ -540,13 +542,17 @@ Block *SymbolContext::GetFunctionBlock() {
540542
}
541543

542544
ConstString SymbolContext::GetInstanceVariableName() {
545+
LanguageType lang_type = eLanguageTypeUnknown;
546+
543547
if (Block *function_block = GetFunctionBlock())
544-
if (CompilerDeclContext decl_ctx = function_block->GetDeclContext()) {
545-
auto language = decl_ctx.GetLanguage();
546-
if (language == eLanguageTypeUnknown)
547-
language = GetLanguage();
548-
return decl_ctx.GetInstanceVariableName(language);
549-
}
548+
if (CompilerDeclContext decl_ctx = function_block->GetDeclContext())
549+
lang_type = decl_ctx.GetLanguage();
550+
551+
if (lang_type == eLanguageTypeUnknown)
552+
lang_type = GetLanguage();
553+
554+
if (auto *lang = Language::FindPlugin(lang_type))
555+
return lang->GetInstanceVariableName();
550556

551557
return {};
552558
}

0 commit comments

Comments
 (0)