Skip to content

Commit 87adabe

Browse files
committed
Add support for builtin and pointer types to GetClangTypeNode()
This is a prerequisite for TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(). (cherry picked from commit 662059b)
1 parent 03cbcd5 commit 87adabe

File tree

1 file changed

+74
-32
lines changed

1 file changed

+74
-32
lines changed

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "swift/AST/ClangModuleLoader.h"
2727
#include "swift/Basic/Version.h"
28+
#include "swift/../../lib/ClangImporter/ClangAdapter.h"
2829
#include "swift/Demangling/Demangle.h"
2930
#include "swift/Demangling/Demangler.h"
3031
#include "swift/Strings.h"
@@ -90,20 +91,85 @@ static CompilerType LookupClangForwardType(SwiftASTContext *module_holder,
9091
return {};
9192
}
9293

94+
/// Return a demangle tree for an UnsafePointer<Pointee>.
95+
static swift::Demangle::NodePointer
96+
GetPointerTo(swift::Demangle::Demangler &dem,
97+
swift::Demangle::NodePointer pointee) {
98+
using namespace swift::Demangle;
99+
auto *bgs = dem.createNode(Node::Kind::BoundGenericStructure);
100+
// Construct the first branch of BoundGenericStructure.
101+
{
102+
auto *type = dem.createNode(Node::Kind::Type);
103+
auto *structure = dem.createNode(Node::Kind::Structure);
104+
structure->addChild(
105+
dem.createNodeWithAllocatedText(Node::Kind::Module, swift::STDLIB_NAME),
106+
dem);
107+
structure->addChild(dem.createNode(Node::Kind::Identifier, "UnsafePointer"),
108+
dem);
109+
type->addChild(structure, dem);
110+
bgs->addChild(type, dem);
111+
}
112+
113+
// Construct the second branch of BoundGenericStructure.
114+
{
115+
auto *typelist = dem.createNode(Node::Kind::TypeList);
116+
auto *type = dem.createNode(Node::Kind::Type);
117+
typelist->addChild(type, dem);
118+
type->addChild(pointee, dem);
119+
bgs->addChild(typelist, dem);
120+
}
121+
return bgs;
122+
}
123+
93124
/// Return a demangle tree leaf node representing \p clang_type.
94125
static swift::Demangle::NodePointer
95126
GetClangTypeNode(CompilerType clang_type, swift::Demangle::Demangler &dem) {
96127
using namespace swift::Demangle;
97-
clang::QualType qual_type = ClangUtil::GetQualType(clang_type);
98-
NodePointer structure = dem.createNode(
99-
qual_type->isClassType() ? Node::Kind::Class : Node::Kind::Structure);
100-
NodePointer module = dem.createNodeWithAllocatedText(
101-
Node::Kind::Module, swift::MANGLING_MODULE_OBJC);
128+
Node::Kind kind;
129+
llvm::StringRef swift_name;
130+
llvm::StringRef module_name = swift::MANGLING_MODULE_OBJC;
131+
CompilerType pointee;
132+
if (clang_type.IsPointerType(&pointee))
133+
clang_type = pointee;
134+
135+
switch (clang_type.GetTypeClass()) {
136+
case eTypeClassClass:
137+
case eTypeClassObjCObjectPointer:
138+
// Objective-C objects are first-class entities, not pointers.
139+
kind = Node::Kind::Class;
140+
pointee = {};
141+
break;
142+
case eTypeClassBuiltin:
143+
kind = Node::Kind::Structure;
144+
// Ask ClangImporter about the builtin type's Swift name.
145+
if (auto *ts = llvm::cast<TypeSystemClang>(clang_type.GetTypeSystem())) {
146+
if (clang_type == ts->GetPointerSizedIntType(true)) {
147+
swift_name = "Int";
148+
break;
149+
}
150+
if (clang_type == ts->GetPointerSizedIntType(false)) {
151+
swift_name = "UInt";
152+
break;
153+
}
154+
swift_name = swift::importer::getClangTypeNameForOmission(
155+
ts->getASTContext(), ClangUtil::GetQualType(clang_type))
156+
.Name;
157+
module_name = swift::STDLIB_NAME;
158+
}
159+
break;
160+
default:
161+
kind = Node::Kind::Structure;
162+
}
163+
NodePointer structure = dem.createNode(kind);
164+
NodePointer module =
165+
dem.createNodeWithAllocatedText(Node::Kind::Module, module_name);
102166
structure->addChild(module, dem);
103167
NodePointer identifier = dem.createNodeWithAllocatedText(
104-
Node::Kind::Module, clang_type.GetTypeName().GetStringRef());
168+
Node::Kind::Identifier, swift_name.empty()
169+
? clang_type.GetTypeName().GetStringRef()
170+
: swift_name);
105171
structure->addChild(identifier, dem);
106-
return structure;
172+
return pointee ? GetPointerTo(dem, structure) : structure;
107173
}
108174

109175
/// \return the child of the \p Type node.
@@ -1740,7 +1806,6 @@ CompilerType
17401806
TypeSystemSwiftTypeRef::GetPointeeType(opaque_compiler_type_t type) {
17411807
return m_swift_ast_context->GetPointeeType(ReconstructType(type));
17421808
}
1743-
17441809
CompilerType
17451810
TypeSystemSwiftTypeRef::GetPointerType(opaque_compiler_type_t type) {
17461811
auto impl = [&]() -> CompilerType {
@@ -1752,31 +1817,8 @@ TypeSystemSwiftTypeRef::GetPointerType(opaque_compiler_type_t type) {
17521817
// The UnsafePointer type.
17531818
auto *pointer_type = dem.createNode(Node::Kind::Type);
17541819

1755-
auto *bgs = dem.createNode(Node::Kind::BoundGenericStructure);
1820+
auto *bgs = GetPointerTo(dem, pointee_type);
17561821
pointer_type->addChild(bgs, dem);
1757-
1758-
// Construct the first branch of BoundGenericStructure.
1759-
{
1760-
auto *type = dem.createNode(Node::Kind::Type);
1761-
bgs->addChild(type, dem);
1762-
auto *structure = dem.createNode(Node::Kind::Structure);
1763-
type->addChild(structure, dem);
1764-
structure->addChild(dem.createNodeWithAllocatedText(Node::Kind::Module,
1765-
swift::STDLIB_NAME),
1766-
dem);
1767-
structure->addChild(
1768-
dem.createNode(Node::Kind::Identifier, "UnsafePointer"), dem);
1769-
}
1770-
1771-
// Construct the second branch of BoundGenericStructure.
1772-
{
1773-
auto *typelist = dem.createNode(Node::Kind::TypeList);
1774-
bgs->addChild(typelist, dem);
1775-
auto *type = dem.createNode(Node::Kind::Type);
1776-
typelist->addChild(type, dem);
1777-
type->addChild(pointee_type, dem);
1778-
}
1779-
17801822
return RemangleAsType(dem, pointer_type);
17811823
};
17821824
VALIDATE_AND_RETURN(impl, GetPointerType, type, (ReconstructType(type)));

0 commit comments

Comments
 (0)