Skip to content

Implemement TypeSystemSwiftTypeRef::GetBitSize() (NFC) #1636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lldb/include/lldb/Target/SwiftLanguageRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ class SwiftLanguageRuntime : public LanguageRuntime {
Status *error = nullptr);

/// Ask Remote Mirrors for the size of a Swift type.
llvm::Optional<uint64_t> GetBitSize(CompilerType type);
llvm::Optional<uint64_t> GetBitSize(CompilerType type,
ExecutionContextScope *exe_scope);

/// Ask Remote mirrors for the stride of a Swift type.
llvm::Optional<uint64_t> GetByteStride(CompilerType type);
Expand Down
7 changes: 5 additions & 2 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,9 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
// dynamic archetype (and hence its size). Everything follows naturally
// as the elements are laid out in a contigous buffer without padding.
CompilerType simd_type = valobj.GetCompilerType().GetCanonicalType();
llvm::Optional<uint64_t> opt_type_size = simd_type.GetByteSize(nullptr);
ExecutionContext exe_ctx = valobj.GetExecutionContextRef().Lock(true);
llvm::Optional<uint64_t> opt_type_size =
simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
if (!opt_type_size)
return false;
uint64_t type_size = *opt_type_size;
Expand All @@ -1053,7 +1055,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
auto swift_arg_type = generic_args[0];
CompilerType arg_type = ToCompilerType(swift_arg_type);

llvm::Optional<uint64_t> opt_arg_size = arg_type.GetByteSize(nullptr);
llvm::Optional<uint64_t> opt_arg_size =
arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
if (!opt_arg_size)
return false;
uint64_t arg_size = *opt_arg_size;
Expand Down
58 changes: 30 additions & 28 deletions lldb/source/Plugins/Language/Swift/SwiftOptionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,41 @@ using namespace lldb_private::formatters;
using namespace lldb_private::formatters::swift;

/// If this is a Clang enum wrapped in a Swift type, return the clang::EnumDecl.
static clang::EnumDecl *GetAsEnumDecl(CompilerType swift_type) {
static std::pair<clang::EnumDecl *, TypeSystemClang *>
GetAsEnumDecl(CompilerType swift_type) {
if (!swift_type)
return nullptr;
return {nullptr, nullptr};

TypeSystemSwift *swift_ast_ctx =
llvm::dyn_cast_or_null<TypeSystemSwift>(swift_type.GetTypeSystem());
if (!swift_ast_ctx)
return nullptr;
return {nullptr, nullptr};

CompilerType clang_type;
if (!swift_ast_ctx->IsImportedType(swift_type.GetOpaqueQualType(),
&clang_type))
return nullptr;
return {nullptr, nullptr};

if (!clang_type.IsValid())
return nullptr;
return {nullptr, nullptr};

if (!llvm::isa<TypeSystemClang>(clang_type.GetTypeSystem()))
return nullptr;
auto *clang_ts = llvm::dyn_cast<TypeSystemClang>(clang_type.GetTypeSystem());
if (!clang_ts)
return {nullptr, nullptr};

auto qual_type =
clang::QualType::getFromOpaquePtr(clang_type.GetOpaqueQualType());
if (qual_type->getTypeClass() != clang::Type::TypeClass::Enum)
return nullptr;
return {nullptr, nullptr};

if (const clang::EnumType *enum_type = qual_type->getAs<clang::EnumType>())
return enum_type->getDecl();
return nullptr;
return {enum_type->getDecl(), clang_ts};
return {nullptr, nullptr};
}

bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
WouldEvenConsiderFormatting(CompilerType swift_type) {
return GetAsEnumDecl(swift_type);
return GetAsEnumDecl(swift_type).first;
}

lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
Expand All @@ -70,33 +72,33 @@ lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
TypeSummaryImpl::Flags()),
m_type(clang_type), m_cases() {}

static ConstString GetDisplayCaseName(::swift::ClangImporter *clang_importer,
clang::EnumConstantDecl *case_decl) {
if (clang_importer) {
::swift::Identifier imported_identifier =
clang_importer->getEnumConstantName(case_decl);
if (false == imported_identifier.empty())
return ConstString(imported_identifier.str());
}
return ConstString(case_decl->getName());
}

void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
FillCasesIfNeeded() {
if (m_cases.hasValue())
return;

m_cases = CasesVector();
clang::EnumDecl *enum_decl = GetAsEnumDecl(m_type);
auto decl_ts = GetAsEnumDecl(m_type);
clang::EnumDecl *enum_decl = decl_ts.first;
if (!enum_decl)
return;

if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
m_type.GetTypeSystem()))
m_type = ts->ReconstructType(m_type);
// FIXME: Delete this type reconstruction block. For GetSwiftName() to
// fully work, ClangImporter's ImportName class needs to be made
// standalone and provided with a callback to read the APINote
// information.
auto *ts = llvm::cast<TypeSystemSwift>(m_type.GetTypeSystem());
if (auto *trts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(ts)) {
m_type = trts->ReconstructType(m_type);
ts = llvm::cast<SwiftASTContext>(m_type.GetTypeSystem());
decl_ts = GetAsEnumDecl(m_type);
enum_decl = decl_ts.first;
if (!enum_decl)
return;
}

SwiftASTContext *swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContext>(m_type.GetTypeSystem());
::swift::ClangImporter *clang_importer = swift_ast_ctx->GetClangImporter();
auto iter = enum_decl->enumerator_begin(), end = enum_decl->enumerator_end();
for (; iter != end; ++iter) {
clang::EnumConstantDecl *case_decl = *iter;
Expand All @@ -109,7 +111,7 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
case_init_val = case_init_val.zext(64);
if (case_init_val.getBitWidth() > 64)
continue;
ConstString case_name(GetDisplayCaseName(clang_importer, case_decl));
ConstString case_name(ts->GetSwiftName(case_decl, *decl_ts.second));
m_cases->push_back({case_init_val, case_name});
}
}
Expand Down
39 changes: 27 additions & 12 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4571,17 +4571,17 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
// CompilerType to match this to the version of the type we got from
// the mangled name in the original swift::ASTContext.
ConstString mangled_name(type.GetMangledTypeName());
if (mangled_name) {
swift::TypeBase *our_type_base =
m_mangled_name_to_type_map.lookup(mangled_name.GetCString());
if (our_type_base)
return ToCompilerType({our_type_base});
else {
CompilerType our_type(GetTypeFromMangledTypename(mangled_name));
if (error.Success())
return our_type;
}
}
if (!mangled_name)
return {};
if (llvm::isa<TypeSystemSwiftTypeRef>(ts))
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
swift::TypeBase *our_type_base =
m_mangled_name_to_type_map.lookup(mangled_name.GetCString());
if (our_type_base)
return ToCompilerType({our_type_base});
CompilerType our_type(GetTypeFromMangledTypename(mangled_name));
if (error.Success())
return our_type;
return {};
}

Expand Down Expand Up @@ -5960,7 +5960,7 @@ SwiftASTContext::GetBitSize(opaque_compiler_type_t type,
if (!exe_scope)
return {};
if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
return runtime->GetBitSize({this, type});
return runtime->GetBitSize({this, type}, exe_scope);
return {};
}

Expand Down Expand Up @@ -7808,6 +7808,21 @@ bool SwiftASTContext::IsImportedType(opaque_compiler_type_t type,
return success;
}

std::string SwiftASTContext::GetSwiftName(const clang::Decl *clang_decl,
TypeSystemClang &clang_typesystem) {
if (auto name_decl = llvm::dyn_cast<clang::NamedDecl>(clang_decl))
return ImportName(name_decl);
return {};
}

std::string SwiftASTContext::ImportName(const clang::NamedDecl *clang_decl) {
if (auto clang_importer = GetClangImporter()) {
swift::DeclName imported_name = clang_importer->importName(clang_decl, {});
return imported_name.getBaseName().userFacingName().str();
}
return clang_decl->getName().str();
}

void SwiftASTContext::DumpSummary(opaque_compiler_type_t type,
ExecutionContext *exe_ctx, Stream *s,
const lldb_private::DataExtractor &data,
Expand Down
10 changes: 9 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace clang {
namespace api_notes {
class APINotesManager;
}
class NamedDecl;
} // namespace clang

namespace llvm {
Expand Down Expand Up @@ -249,7 +250,7 @@ class SwiftASTContext : public TypeSystemSwift {

void CacheModule(swift::ModuleDecl *module);

Module *GetModule() const { return m_module; }
Module *GetModule() const override { return m_module; }

// Call this after the search paths are set up, it will find the module given
// by module, load the module into the AST context, and also load any
Expand Down Expand Up @@ -317,6 +318,13 @@ class SwiftASTContext : public TypeSystemSwift {
/// \return Returns an invalid type if unsuccessful.
CompilerType ImportClangType(CompilerType clang_type);

/// Use ClangImporter to determine the swiftified name of \p
/// clang_decl.
std::string GetSwiftName(const clang::Decl *clang_decl,
TypeSystemClang &clang_typesystem) override;
/// Use \p ClangImporter to swiftify the decl's name.
std::string ImportName(const clang::NamedDecl *clang_decl);

static SwiftASTContext *GetSwiftASTContext(swift::ASTContext *ast);

swift::irgen::IRGenerator &GetIRGenerator(swift::IRGenOptions &opts,
Expand Down
11 changes: 9 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include "lldb/Core/PluginManager.h"
#include "lldb/Target/SwiftLanguageRuntime.h"
#include <lldb/lldb-enumerations.h>
#include <llvm/ADT/StringRef.h>

LLDB_PLUGIN_DEFINE(TypeSystemSwift)

using namespace lldb;
using namespace lldb_private;
using llvm::StringRef;

/// TypeSystem Plugin functionality.
/// \{
Expand All @@ -39,10 +41,15 @@ static lldb::TypeSystemSP CreateTypeSystemInstance(lldb::LanguageType language,
llvm_unreachable("Neither type nor module given to CreateTypeSystemInstance");
}

void TypeSystemSwift::Initialize() {
LanguageSet TypeSystemSwift::GetSupportedLanguagesForTypes() {
LanguageSet swift;
SwiftLanguageRuntime::Initialize();
swift.Insert(lldb::eLanguageTypeSwift);
return swift;
}

void TypeSystemSwift::Initialize() {
SwiftLanguageRuntime::Initialize();
LanguageSet swift = GetSupportedLanguagesForTypes();
PluginManager::RegisterPlugin(GetPluginNameStatic(),
"Swift type system and AST context plug-in",
CreateTypeSystemInstance, swift, swift);
Expand Down
14 changes: 14 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
#include "lldb/Utility/Flags.h"
#include "lldb/lldb-private.h"

namespace clang {
class Decl;
}

namespace lldb_private {
class TypeSystemClang;

/// The implementation of lldb::Type's m_payload field for TypeSystemSwift.
class TypePayloadSwift {
Expand Down Expand Up @@ -91,6 +96,8 @@ class TypeSystemSwift : public TypeSystem {
static ConstString GetPluginNameStatic();
/// \}

static LanguageSet GetSupportedLanguagesForTypes();
virtual Module *GetModule() const = 0;
virtual lldb::TypeSP GetCachedType(ConstString mangled) = 0;
virtual void SetCachedType(ConstString mangled,
const lldb::TypeSP &type_sp) = 0;
Expand All @@ -117,11 +124,18 @@ class TypeSystemSwift : public TypeSystem {
lldb::opaque_compiler_type_t type, Stream *s,
bool print_help_if_available, bool print_extensions_if_available,
lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0;

/// Create a CompilerType from a mangled Swift type name.
virtual CompilerType
GetTypeFromMangledTypename(ConstString mangled_typename) = 0;
virtual CompilerType GetGenericArgumentType(lldb::opaque_compiler_type_t type,
size_t idx) = 0;

/// Use API notes or ClangImporter to determine the swiftified name
/// of \p clang_decl.
virtual std::string GetSwiftName(const clang::Decl *clang_decl,
TypeSystemClang &clang_typesystem) = 0;

/// Unavailable hardcoded functions that don't make sense for Swift.
/// \{
ConstString DeclContextGetName(void *opaque_decl_ctx) override { return {}; }
Expand Down
Loading