Skip to content

Commit ba3a5bc

Browse files
authored
Merge pull request #6452 from apple/dl/this-or-self-ivar-direct-access
[lldb] Implement direct ivar access for Swift
2 parents 0b028d5 + 47e8dc8 commit ba3a5bc

File tree

25 files changed

+230
-128
lines changed

25 files changed

+230
-128
lines changed

lldb/include/lldb/Symbol/CompilerDeclContext.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,13 @@ class CompilerDeclContext {
6161

6262
/// Checks if this decl context represents a method of a class.
6363
///
64-
/// \param[out] language_ptr
65-
/// If non NULL and \b true is returned from this function,
66-
/// this will indicate if the language that respresents the method.
67-
///
68-
/// \param[out] is_instance_method_ptr
69-
/// If non NULL and \b true is returned from this function,
70-
/// this will indicate if the method is an instance function (true)
71-
/// or a class method (false indicating the function is static, or
72-
/// doesn't require an instance of the class to be called).
73-
///
74-
/// \param[out] language_object_name_ptr
75-
/// If non NULL and \b true is returned from this function,
76-
/// this will indicate if implicit object name for the language
77-
/// like "this" for C++, and "self" for Objective C.
78-
///
7964
/// \return
8065
/// Returns true if this is a decl context that represents a method
8166
/// in a struct, union or class.
82-
bool IsClassMethod(lldb::LanguageType *language_ptr,
83-
bool *is_instance_method_ptr,
84-
ConstString *language_object_name_ptr);
67+
bool IsClassMethod();
68+
69+
/// Determines the original language of the decl context.
70+
lldb::LanguageType GetLanguage();
8571

8672
/// Check if the given other decl context is contained in the lookup
8773
/// of this decl context (for example because the other context is a nested

lldb/include/lldb/Symbol/SymbolContext.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,13 @@ class SymbolContext {
245245
/// represented by this symbol context object, nullptr otherwise.
246246
Block *GetFunctionBlock();
247247

248-
/// If this symbol context represents a function that is a method, return
249-
/// true and provide information about the method.
248+
/// Determines the name of the instance variable for the this decl context.
250249
///
251-
/// \param[out] language
252-
/// If \b true is returned, the language for the method.
253-
///
254-
/// \param[out] is_instance_method
255-
/// If \b true is returned, \b true if this is a instance method,
256-
/// \b false if this is a static/class function.
257-
///
258-
/// \param[out] language_object_name
259-
/// If \b true is returned, the name of the artificial variable
260-
/// for the language ("this" for C++, "self" for ObjC).
250+
/// For C++ the name is "this", for Objective-C the name is "self".
261251
///
262252
/// \return
263-
/// \b True if this symbol context represents a function that
264-
/// is a method of a class, \b false otherwise.
265-
bool GetFunctionMethodInfo(lldb::LanguageType &language,
266-
bool &is_instance_method,
267-
ConstString &language_object_name);
253+
/// Returns a string for the name of the instance variable.
254+
ConstString GetInstanceVariableName();
268255

269256
/// Sorts the types in TypeMap according to SymbolContext to TypeList
270257
///

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ class TypeSystem : public PluginInterface,
131131
virtual ConstString
132132
DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
133133

134-
virtual bool DeclContextIsClassMethod(
135-
void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
136-
bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
134+
virtual bool DeclContextIsClassMethod(void *opaque_decl_ctx) = 0;
137135

138136
virtual bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
139137
void *other_opaque_decl_ctx) = 0;
140138

139+
virtual lldb::LanguageType DeclContextGetLanguage(void *opaque_decl_ctx) = 0;
140+
141141
// Tests
142142
#ifndef NDEBUG
143143
/// Verify the integrity of the type to catch CompilerTypes that mix

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/ExpressionParser/Clang/ClangExpressionDeclMap.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,7 @@ SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts(
11731173
// class/instance methods, since they'll be skipped in the code that
11741174
// follows anyway.
11751175
CompilerDeclContext func_decl_context = function->GetDeclContext();
1176-
if (!func_decl_context ||
1177-
func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1176+
if (!func_decl_context || func_decl_context.IsClassMethod())
11781177
continue;
11791178
// We can only prune functions for which we can copy the type.
11801179
CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
@@ -1308,7 +1307,7 @@ void ClangExpressionDeclMap::LookupFunction(
13081307
continue;
13091308

13101309
// Filter out class/instance methods.
1311-
if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1310+
if (decl_ctx.IsClassMethod())
13121311
continue;
13131312

13141313
AddOneFunction(context, sym_ctx.function, nullptr);

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/Language/Swift/SwiftLanguage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Other libraries and framework includes
1919
// Project includes
2020
#include "lldb/Target/Language.h"
21+
#include "lldb/Utility/ConstString.h"
2122
#include "lldb/lldb-private.h"
2223

2324
namespace lldb_private {
@@ -88,6 +89,8 @@ class SwiftLanguage : public Language {
8889

8990
bool SymbolNameFitsToLanguage(Mangled mangled) const override;
9091

92+
ConstString GetInstanceVariableName() override { return ConstString("self"); }
93+
9194
//------------------------------------------------------------------
9295
// PluginInterface protocol
9396
//------------------------------------------------------------------

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

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "TypeSystemClang.h"
1010

11+
#include "clang/AST/DeclBase.h"
12+
#include "llvm/Support/Casting.h"
1113
#include "llvm/Support/FormatAdapters.h"
1214
#include "llvm/Support/FormatVariadic.h"
1315

@@ -9721,43 +9723,21 @@ TypeSystemClang::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
97219723
return ConstString();
97229724
}
97239725

9724-
bool TypeSystemClang::DeclContextIsClassMethod(
9725-
void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9726-
bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9727-
if (opaque_decl_ctx) {
9728-
clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9729-
if (ObjCMethodDecl *objc_method =
9730-
llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9731-
if (is_instance_method_ptr)
9732-
*is_instance_method_ptr = objc_method->isInstanceMethod();
9733-
if (language_ptr)
9734-
*language_ptr = eLanguageTypeObjC;
9735-
if (language_object_name_ptr)
9736-
language_object_name_ptr->SetCString("self");
9737-
return true;
9738-
} else if (CXXMethodDecl *cxx_method =
9739-
llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9740-
if (is_instance_method_ptr)
9741-
*is_instance_method_ptr = cxx_method->isInstance();
9742-
if (language_ptr)
9743-
*language_ptr = eLanguageTypeC_plus_plus;
9744-
if (language_object_name_ptr)
9745-
language_object_name_ptr->SetCString("this");
9746-
return true;
9747-
} else if (clang::FunctionDecl *function_decl =
9748-
llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9749-
ClangASTMetadata *metadata = GetMetadata(function_decl);
9750-
if (metadata && metadata->HasObjectPtr()) {
9751-
if (is_instance_method_ptr)
9752-
*is_instance_method_ptr = true;
9753-
if (language_ptr)
9754-
*language_ptr = eLanguageTypeObjC;
9755-
if (language_object_name_ptr)
9756-
language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9757-
return true;
9758-
}
9759-
}
9726+
bool TypeSystemClang::DeclContextIsClassMethod(void *opaque_decl_ctx) {
9727+
if (!opaque_decl_ctx)
9728+
return false;
9729+
9730+
clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9731+
if (llvm::isa<clang::ObjCMethodDecl>(decl_ctx)) {
9732+
return true;
9733+
} else if (llvm::isa<clang::CXXMethodDecl>(decl_ctx)) {
9734+
return true;
9735+
} else if (clang::FunctionDecl *fun_decl =
9736+
llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9737+
if (ClangASTMetadata *metadata = GetMetadata(fun_decl))
9738+
return metadata->HasObjectPtr();
97609739
}
9740+
97619741
return false;
97629742
}
97639743

@@ -9778,6 +9758,24 @@ bool TypeSystemClang::DeclContextIsContainedInLookup(
97789758
return false;
97799759
}
97809760

9761+
lldb::LanguageType
9762+
TypeSystemClang::DeclContextGetLanguage(void *opaque_decl_ctx) {
9763+
if (!opaque_decl_ctx)
9764+
return eLanguageTypeUnknown;
9765+
9766+
auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9767+
if (llvm::isa<clang::ObjCMethodDecl>(decl_ctx)) {
9768+
return eLanguageTypeObjC;
9769+
} else if (llvm::isa<clang::CXXMethodDecl>(decl_ctx)) {
9770+
return eLanguageTypeC_plus_plus;
9771+
} else if (auto *fun_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9772+
if (ClangASTMetadata *metadata = GetMetadata(fun_decl))
9773+
return metadata->GetObjectPtrLanguage();
9774+
}
9775+
9776+
return eLanguageTypeUnknown;
9777+
}
9778+
97819779
static bool IsClangDeclContext(const CompilerDeclContext &dc) {
97829780
return dc.IsValid() && isa<TypeSystemClang>(dc.GetTypeSystem());
97839781
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,13 @@ class TypeSystemClang : public TypeSystem {
570570

571571
ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override;
572572

573-
bool DeclContextIsClassMethod(void *opaque_decl_ctx,
574-
lldb::LanguageType *language_ptr,
575-
bool *is_instance_method_ptr,
576-
ConstString *language_object_name_ptr) override;
573+
bool DeclContextIsClassMethod(void *opaque_decl_ctx) override;
577574

578575
bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
579576
void *other_opaque_decl_ctx) override;
580577

578+
lldb::LanguageType DeclContextGetLanguage(void *opaque_decl_ctx) override;
579+
581580
// Clang specific clang::DeclContext functions
582581

583582
static clang::DeclContext *

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "lldb/Symbol/TypeSystem.h"
2020
#include "lldb/Utility/ConstString.h"
2121
#include "lldb/Utility/Flags.h"
22+
#include "lldb/lldb-enumerations.h"
2223
#include "lldb/lldb-private.h"
2324

2425
namespace clang {
@@ -198,13 +199,12 @@ class TypeSystemSwift : public TypeSystem {
198199
ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override {
199200
return {};
200201
}
201-
bool
202-
DeclContextIsClassMethod(void *opaque_decl_ctx,
203-
lldb::LanguageType *language_ptr,
204-
bool *is_instance_method_ptr,
205-
ConstString *language_object_name_ptr) override {
202+
bool DeclContextIsClassMethod(void *opaque_decl_ctx) override {
206203
return false;
207204
}
205+
lldb::LanguageType DeclContextGetLanguage(void *) override {
206+
return lldb::eLanguageTypeSwift;
207+
}
208208
bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override {
209209
return false;
210210
}

lldb/source/Symbol/CompilerDeclContext.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ ConstString CompilerDeclContext::GetScopeQualifiedName() const {
3434
return ConstString();
3535
}
3636

37-
bool CompilerDeclContext::IsClassMethod(lldb::LanguageType *language_ptr,
38-
bool *is_instance_method_ptr,
39-
ConstString *language_object_name_ptr) {
37+
bool CompilerDeclContext::IsClassMethod() {
4038
if (IsValid())
41-
return m_type_system->DeclContextIsClassMethod(
42-
m_opaque_decl_ctx, language_ptr, is_instance_method_ptr,
43-
language_object_name_ptr);
39+
return m_type_system->DeclContextIsClassMethod(m_opaque_decl_ctx);
4440
return false;
4541
}
4642

43+
lldb::LanguageType CompilerDeclContext::GetLanguage() {
44+
if (IsValid())
45+
return m_type_system->DeclContextGetLanguage(m_opaque_decl_ctx);
46+
return {};
47+
}
48+
4749
bool CompilerDeclContext::IsContainedInLookup(CompilerDeclContext other) const {
4850
if (!IsValid())
4951
return false;

lldb/source/Symbol/SymbolContext.cpp

Lines changed: 16 additions & 13 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;
@@ -539,19 +541,20 @@ Block *SymbolContext::GetFunctionBlock() {
539541
return nullptr;
540542
}
541543

542-
bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language,
543-
bool &is_instance_method,
544-
ConstString &language_object_name)
545-
546-
{
547-
Block *function_block = GetFunctionBlock();
548-
if (function_block) {
549-
CompilerDeclContext decl_ctx = function_block->GetDeclContext();
550-
if (decl_ctx)
551-
return decl_ctx.IsClassMethod(&language, &is_instance_method,
552-
&language_object_name);
553-
}
554-
return false;
544+
ConstString SymbolContext::GetInstanceVariableName() {
545+
LanguageType lang_type = eLanguageTypeUnknown;
546+
547+
if (Block *function_block = GetFunctionBlock())
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();
556+
557+
return {};
555558
}
556559

557560
void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const {

0 commit comments

Comments
 (0)