Skip to content

[lldb] Thread mangling flavor together with node pointer #9496

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 1 commit into from
Dec 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "SwiftExpressionParser.h"

#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
#include "SwiftASTManipulator.h"
#include "SwiftDiagnostic.h"
#include "SwiftExpressionSourceCode.h"
Expand Down Expand Up @@ -1181,8 +1182,9 @@ AddArchetypeTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
llvm::StringRef &type_name = pair.getFirst();
MetadataPointerInfo &info = pair.getSecond();

auto dependent_type =
typeref_typesystem->CreateGenericTypeParamType(info.depth, info.index);
auto flavor = SwiftLanguageRuntime::GetManglingFlavor(type_name);
auto dependent_type = typeref_typesystem->CreateGenericTypeParamType(
info.depth, info.index, flavor);
auto bound_type =
runtime->BindGenericTypeParameters(stack_frame, dependent_type);
if (!bound_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "SwiftExpressionSourceCode.h"
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "SwiftPersistentExpressionState.h"

#include "Plugins/ExpressionParser/Swift/SwiftASTManipulator.h"
Expand Down Expand Up @@ -60,10 +61,14 @@ static llvm::Expected<std::string> TransformPackType(
if (!ts)
return llvm::createStringError(llvm::errc::not_supported,
"no typeref typesystem");

auto mangled_name = type.GetMangledTypeName().GetStringRef();
auto flavor = SwiftLanguageRuntime::GetManglingFlavor(mangled_name);

using namespace swift::Demangle;
Demangler dem;
NodePointer node = ts->GetCanonicalDemangleTree(
dem, type.GetMangledTypeName().GetStringRef());

NodePointer node = ts->GetCanonicalDemangleTree(dem, mangled_name);

node = TypeSystemSwiftTypeRef::Transform(dem, node, [](NodePointer n) {
if (n->getKind() == Node::Kind::SILPackIndirect &&
Expand All @@ -76,7 +81,8 @@ static llvm::Expected<std::string> TransformPackType(
});

bool error = false;
ConstString type_name = ts->RemangleAsType(dem, node).GetMangledTypeName();
ConstString type_name =
ts->RemangleAsType(dem, node, flavor).GetMangledTypeName();
swift::Demangle::DemangleOptions options;
options = swift::Demangle::DemangleOptions::SimplifiedUIDemangleOptions();
options.DisplayStdlibModule = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "lldb/Core/PluginInterface.h"
#include "lldb/Target/LanguageRuntime.h"
#include "lldb/lldb-private.h"
#include "swift/Demangling/ManglingFlavor.h"

#include <optional>
#include "llvm/ADT/StringSet.h"
Expand Down Expand Up @@ -132,6 +133,12 @@ class SwiftLanguageRuntime : public LanguageRuntime {
/// since some day we may want to support more than one swift variant.
static bool IsSwiftMangledName(llvm::StringRef name);

static swift::Mangle::ManglingFlavor
GetManglingFlavor(llvm::StringRef mangledName) {
if (mangledName.starts_with("$e") || mangledName.starts_with("_$e"))
return swift::Mangle::ManglingFlavor::Embedded;
return swift::Mangle::ManglingFlavor::Default;
}
enum class FuncletComparisonResult {
NotBothFunclets,
DifferentAsyncFunctions,
Expand Down Expand Up @@ -271,6 +278,7 @@ class SwiftLanguageRuntime : public LanguageRuntime {
/// context.
static void GetGenericParameterNamesForFunction(
const SymbolContext &sc, const ExecutionContext *exe_ctx,
swift::Mangle::ManglingFlavor flavor,
llvm::DenseMap<ArchetypePath, llvm::StringRef> &dict);

/// Invoke callback for each DependentGenericParamType.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "swift/AST/ASTMangler.h"
#include "swift/AST/ASTWalker.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Demangling/ManglingFlavor.h"
#include "swift/RemoteInspection/ReflectionContext.h"
#include "swift/RemoteInspection/TypeRefBuilder.h"
#include "swift/Strings.h"
Expand Down Expand Up @@ -318,8 +319,11 @@ class LLDBTypeInfoProvider : public swift::remote::TypeInfoProvider {
auto *node = dem.demangleSymbol(wrapped);
if (!node) {
// Try `mangledName` as plain ObjC class name. Ex: NSObject, NSView, etc.
// Since this looking up an ObjC type, the default mangling falvor should
// be used.
auto maybeMangled = swift_demangle::mangleClass(
dem, swift::MANGLING_MODULE_OBJC, mangledName);
dem, swift::MANGLING_MODULE_OBJC, mangledName,
swift::Mangle::ManglingFlavor::Default);
if (!maybeMangled.isSuccess()) {
LLDB_LOG(GetLog(LLDBLog::Types),
"[LLDBTypeInfoProvider] invalid mangled name: {0}",
Expand Down Expand Up @@ -641,7 +645,9 @@ CompilerType GetWeakReferent(TypeSystemSwiftTypeRef &ts, CompilerType type) {
if (!n || n->getKind() != Node::Kind::SugaredOptional || !n->hasChildren())
return {};
n = n->getFirstChild();
return ts.RemangleAsType(dem, n);
return ts.RemangleAsType(
dem, n,
SwiftLanguageRuntime::GetManglingFlavor(type.GetMangledTypeName()));
}

CompilerType GetTypeFromTypeRef(TypeSystemSwiftTypeRef &ts,
Expand All @@ -650,7 +656,8 @@ CompilerType GetTypeFromTypeRef(TypeSystemSwiftTypeRef &ts,
return {};
swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = type_ref->getDemangling(dem);
return ts.RemangleAsType(dem, node);
// TODO: the mangling flavor should come from the TypeRef.
return ts.RemangleAsType(dem, node, ts.GetManglingFlavor());
}

struct ExistentialSyntheticChild {
Expand Down Expand Up @@ -1567,8 +1574,9 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(

swift::Demangle::Demangler dem;

auto expand_pack_type = [&](ConstString mangled_pack_type,
bool indirect) -> swift::Demangle::NodePointer {
auto expand_pack_type = [&](ConstString mangled_pack_type, bool indirect,
swift::Mangle::ManglingFlavor flavor)
-> swift::Demangle::NodePointer {
// Find pack_type in the pack_expansions.
unsigned i = 0;
SwiftLanguageRuntime::GenericSignature::PackExpansion *pack_expansion =
Expand Down Expand Up @@ -1698,7 +1706,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(
auto bound_typeref = reflection_ctx->ApplySubstitutions(
type_ref, substitutions, ts->GetDescriptorFinder());
swift::Demangle::NodePointer node = bound_typeref->getDemangling(dem);
CompilerType type = ts->RemangleAsType(dem, node);
CompilerType type = ts->RemangleAsType(dem, node, flavor);

// Add the substituted type to the tuple.
elements.push_back({{}, type});
Expand All @@ -1724,6 +1732,9 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(
auto node = dem_ctx.demangleSymbolAsNode(
pack_type.GetMangledTypeName().GetStringRef());

auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(pack_type.GetMangledTypeName());

// Expand all the pack types that appear in the incoming type,
// either at the root level or as arguments of bound generic types.
bool indirect = false;
Expand All @@ -1739,18 +1750,18 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(
if (node->getNumChildren() != 1)
return node;
node = node->getChild(0);
CompilerType pack_type = ts->RemangleAsType(dem, node);
CompilerType pack_type = ts->RemangleAsType(dem, node, flavor);
ConstString mangled_pack_type = pack_type.GetMangledTypeName();
LLDB_LOG(log, "decoded pack_expansion type: {0}", mangled_pack_type);
auto result = expand_pack_type(mangled_pack_type, indirect);
auto result = expand_pack_type(mangled_pack_type, indirect, flavor);
if (!result) {
LLDB_LOG(log, "failed to expand pack type: {0}", mangled_pack_type);
return node;
}
return result;
});

CompilerType expanded_type = ts->RemangleAsType(dem, transformed);
CompilerType expanded_type = ts->RemangleAsType(dem, transformed, flavor);
pack_type_or_name.SetCompilerType(expanded_type);

AddressType address_type;
Expand Down Expand Up @@ -1824,7 +1835,8 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
// useful to users.
if (IsPrivateNSClass(node))
return false;
class_type_or_name.SetCompilerType(ts->RemangleAsType(dem, node));
class_type_or_name.SetCompilerType(ts->RemangleAsType(
dem, node, swift::Mangle::ManglingFlavor::Default));
found = true;
return true;
}
Expand Down Expand Up @@ -1854,9 +1866,13 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
class_type.GetMangledTypeName(), instance_ptr);
return false;
}

auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(class_type.GetMangledTypeName());

swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = typeref->getDemangling(dem);
CompilerType dynamic_type = ts->RemangleAsType(dem, node);
CompilerType dynamic_type = ts->RemangleAsType(dem, node, flavor);
LLDB_LOG(log, "dynamic type of instance_ptr {0:x} is {1}", instance_ptr,
class_type.GetMangledTypeName());
class_type_or_name.SetCompilerType(dynamic_type);
Expand Down Expand Up @@ -2022,7 +2038,10 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Existential(
return false;
swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = typeref->getDemangling(dem);
class_type_or_name.SetCompilerType(ts->RemangleAsType(dem, node));
auto flavor = SwiftLanguageRuntime::GetManglingFlavor(
existential_type.GetMangledTypeName());

class_type_or_name.SetCompilerType(ts->RemangleAsType(dem, node, flavor));
address.SetRawAddress(out_address.getAddressData());

#ifndef NDEBUG
Expand Down Expand Up @@ -2087,7 +2106,10 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ExistentialMetatype(
meta->addChild(node, dem);
wrapped->addChild(meta,dem);

meta_type = tr_ts->RemangleAsType(dem, wrapped);
auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(meta_type.GetMangledTypeName());
meta_type =
tss->GetTypeSystemSwiftTypeRef()->RemangleAsType(dem, wrapped, flavor);
class_type_or_name.SetCompilerType(meta_type);
address.SetRawAddress(ptr);
return true;
Expand All @@ -2113,7 +2135,9 @@ CompilerType SwiftLanguageRuntimeImpl::GetTypeFromMetadata(TypeSystemSwift &ts,
using namespace swift::Demangle;
Demangler dem;
NodePointer node = type_ref->getDemangling(dem);
return tr_ts->RemangleAsType(dem, node);
// TODO: the mangling flavor should come from the TypeRef.
return ts.GetTypeSystemSwiftTypeRef()->RemangleAsType(
dem, node, ts.GetTypeSystemSwiftTypeRef()->GetManglingFlavor());
}

std::optional<lldb::addr_t>
Expand Down Expand Up @@ -2236,7 +2260,10 @@ CompilerType SwiftLanguageRuntimeImpl::BindGenericTypeParameters(
type_ref, substitutions,
tr_ts->GetDescriptorFinder());
NodePointer node = bound_type_ref->getDemangling(dem);
return tr_ts->RemangleAsType(dem, node);
return ts->GetTypeSystemSwiftTypeRef()->RemangleAsType(
dem, node,
SwiftLanguageRuntime::GetManglingFlavor(
unbound_type.GetMangledTypeName()));
}

CompilerType
Expand Down Expand Up @@ -2278,10 +2305,12 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
return;
substitutions.insert({{depth, index}, type_ref});
});
auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(mangled_name.GetStringRef());

// Nothing to do if there are no type parameters.
auto get_canonical = [&]() {
auto mangling = mangleNode(canonical);
auto mangling = mangleNode(canonical, flavor);
if (!mangling.isSuccess())
return CompilerType();
return ts.GetTypeFromMangledTypename(ConstString(mangling.result()));
Expand Down Expand Up @@ -2315,7 +2344,7 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
"No scratch context available.");
return ts.GetTypeFromMangledTypename(mangled_name);
}
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node);
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node, flavor);
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types), "Bound {0} -> {1}.",
mangled_name, bound_type.GetMangledTypeName());
return bound_type;
Expand Down Expand Up @@ -2640,6 +2669,13 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(

std::string remangled;
{
auto type = in_value.GetCompilerType();
std::optional<swift::Mangle::ManglingFlavor> mangling_flavor =
SwiftLanguageRuntime::GetManglingFlavor(
type.GetMangledTypeName().GetStringRef());
if (!mangling_flavor)
return false;

// Create a mangle tree for Swift.Optional<$module.$class>
using namespace swift::Demangle;
NodeFactory factory;
Expand Down Expand Up @@ -2668,7 +2704,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ClangType(
factory);
cty->addChild(c, factory);

auto mangling = mangleNode(global);
auto mangling = mangleNode(global, *mangling_flavor);
if (!mangling.isSuccess())
return false;
remangled = mangling.result();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "Plugins/Process/Utility/RegisterContext_x86.h"
#include "Utility/ARM64_DWARF_Registers.h"
#include "swift/Demangling/ManglingFlavor.h"
#include "llvm/ADT/SmallSet.h"

using namespace lldb;
Expand Down Expand Up @@ -622,6 +623,7 @@ bool SwiftLanguageRuntime::IsSwiftMangledName(llvm::StringRef name) {

void SwiftLanguageRuntime::GetGenericParameterNamesForFunction(
const SymbolContext &const_sc, const ExecutionContext *exe_ctx,
swift::Mangle::ManglingFlavor flavor,
llvm::DenseMap<SwiftLanguageRuntime::ArchetypePath, StringRef> &dict) {
// This terrifying cast avoids having too many differences with llvm.org.
SymbolContext &sc = const_cast<SymbolContext &>(const_sc);
Expand Down Expand Up @@ -681,7 +683,8 @@ void SwiftLanguageRuntime::GetGenericParameterNamesForFunction(
llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
if (!ts)
break;
CompilerType generic_type = ts->CreateGenericTypeParamType(depth, index);
CompilerType generic_type =
ts->CreateGenericTypeParamType(depth, index, flavor);
CompilerType bound_type =
runtime->BindGenericTypeParameters(*frame, generic_type);
type_name = bound_type.GetDisplayTypeName();
Expand Down Expand Up @@ -735,7 +738,9 @@ std::string SwiftLanguageRuntime::DemangleSymbolAsString(
// Resolve generic parameters in the current function.
options.GenericParameterName = [&](uint64_t depth, uint64_t index) {
if (!did_init) {
GetGenericParameterNamesForFunction(*sc, exe_ctx, dict);
GetGenericParameterNamesForFunction(
*sc, exe_ctx, SwiftLanguageRuntime::GetManglingFlavor(symbol),
dict);
did_init = true;
}
auto it = dict.find({depth, index});
Expand Down Expand Up @@ -1205,6 +1210,7 @@ SwiftLanguageRuntime::GetGenericSignature(StringRef function_name,
GenericSignature signature;
unsigned num_generic_params = 0;

auto flavor = SwiftLanguageRuntime::GetManglingFlavor(function_name);
// Walk to the function type.
Context ctx;
auto *node = SwiftLanguageRuntime::DemangleSymbolAsNode(function_name, ctx);
Expand Down Expand Up @@ -1313,10 +1319,10 @@ SwiftLanguageRuntime::GetGenericSignature(StringRef function_name,

// Store the various type packs.
swift::Demangle::Demangler dem;
auto mangling = swift::Demangle::mangleNode(type_node);
auto mangling = swift::Demangle::mangleNode(type_node, flavor);
if (mangling.isSuccess())
signature.pack_expansions.back().mangled_type =
ts.RemangleAsType(dem, type_node).GetMangledTypeName();
ts.RemangleAsType(dem, type_node, flavor).GetMangledTypeName();

// Assuming that there are no nested pack_expansions.
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
#include "ReflectionContextInterface.h"
#include "SwiftLanguageRuntimeImpl.h"
#include "lldb/Symbol/VariableList.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "DWARFDIE.h"

#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
#include "swift/Demangling/ManglingFlavor.h"
#include "swift/RemoteInspection/TypeLowering.h"

#include "lldb/Utility/LLDBLog.h"
Expand Down Expand Up @@ -63,7 +64,9 @@ getTypeAndDie(TypeSystemSwiftTypeRef &ts,
const swift::reflection::TypeRef *TR) {
swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = TR->getDemangling(dem);
auto type = ts.RemangleAsType(dem, node);
// TODO: mangling flavor should come from the TypeRef.
auto type =
ts.RemangleAsType(dem, node, swift::Mangle::ManglingFlavor::Embedded);
if (!type) {
if (auto log = GetLog(LLDBLog::Types)) {
std::stringstream ss;
Expand Down
Loading