Skip to content

Commit bbe6a56

Browse files
committed
ASTDemangler: Implement builtin types
1 parent 2d21233 commit bbe6a56

File tree

8 files changed

+143
-11
lines changed

8 files changed

+143
-11
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ASTBuilder {
5757

5858
Demangle::NodeFactory &getNodeFactory() { return Factory; }
5959

60-
Type createBuiltinType(const std::string &mangledName);
60+
Type createBuiltinType(StringRef builtinName, StringRef mangledName);
6161

6262
NominalTypeDecl *createNominalTypeDecl(StringRef mangledName);
6363

include/swift/Demangling/TypeDecoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class TypeDecoder {
202202
}
203203
case NodeKind::BuiltinTypeName: {
204204
auto mangledName = Demangle::mangleNode(Node);
205-
return Builder.createBuiltinType(mangledName);
205+
return Builder.createBuiltinType(Node->getText(), mangledName);
206206
}
207207
case NodeKind::Metatype:
208208
case NodeKind::ExistentialMetatype: {
@@ -516,7 +516,7 @@ class TypeDecoder {
516516
case NodeKind::SILBoxTypeWithLayout: {
517517
// TODO: Implement SILBoxTypeRefs with layout. As a stopgap, specify the
518518
// NativeObject type ref.
519-
return Builder.createBuiltinType("Bo");
519+
return Builder.createBuiltinType("Builtin.NativeObject", "Bo");
520520
}
521521
default:
522522
return BuiltType();

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ class TypeRefBuilder {
205205
/// Factory methods for all TypeRef kinds
206206
///
207207

208-
const BuiltinTypeRef *createBuiltinType(const std::string &mangledName) {
208+
const BuiltinTypeRef *createBuiltinType(const std::string &builtinName,
209+
const std::string &mangledName) {
209210
return BuiltinTypeRef::create(*this, mangledName);
210211
}
211212

include/swift/Remote/MetadataReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ class MetadataReader {
608608
case MetadataKind::HeapGenericLocalVariable:
609609
case MetadataKind::ErrorObject:
610610
// Treat these all as Builtin.NativeObject for type lowering purposes.
611-
return Builder.createBuiltinType("Bo");
611+
return Builder.createBuiltinType("Builtin.NativeObject", "Bo");
612612
case MetadataKind::Opaque:
613613
default: {
614614
auto BuiltOpaque = Builder.getOpaqueType();

lib/AST/ASTDemangler.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ Type swift::Demangle::getTypeForMangling(ASTContext &ctx,
4747

4848

4949
Type
50-
ASTBuilder::createBuiltinType(const std::string &mangledName) {
51-
// TODO
50+
ASTBuilder::createBuiltinType(StringRef builtinName,
51+
StringRef mangledName) {
52+
if (builtinName.startswith(BUILTIN_TYPE_NAME_PREFIX)) {
53+
SmallVector<ValueDecl *, 1> decls;
54+
55+
ModuleDecl::AccessPathTy accessPath;
56+
StringRef strippedName =
57+
builtinName.drop_front(strlen(BUILTIN_TYPE_NAME_PREFIX));
58+
Ctx.TheBuiltinModule->lookupValue(accessPath,
59+
Ctx.getIdentifier(strippedName),
60+
NLKind::QualifiedLookup,
61+
decls);
62+
63+
if (decls.size() == 1 && isa<TypeDecl>(decls[0]))
64+
return cast<TypeDecl>(decls[0])->getDeclaredInterfaceType();
65+
}
66+
5267
return Type();
5368
}
5469

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ class DecodedMetadataBuilder {
10831083
return accessFunction(MetadataState::Abstract, allGenericArgsVec).Value;
10841084
}
10851085

1086-
BuiltType createBuiltinType(StringRef mangledName) const {
1086+
BuiltType createBuiltinType(StringRef builtinName,
1087+
StringRef mangledName) const {
10871088
#define BUILTIN_TYPE(Symbol, _) \
10881089
if (mangledName.equals(#Symbol)) \
10891090
return &METADATA_SYM(Symbol).base;

test/TypeDecoder/builtins.swift

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift -emit-executable %s -g -o %t/generics -emit-module
4+
// RUN: sed -ne '/\/\/ *DEMANGLE: /s/\/\/ *DEMANGLE: *//p' < %s > %t/input
5+
// RUN: %lldb-moduleimport-test %t/generics -type-from-mangled=%t/input | %FileCheck %s
6+
7+
// DEMANGLE: $sBbD
8+
// DEMANGLE: $sBoD
9+
// DEMANGLE: $sBpD
10+
// DEMANGLE: $sBwD
11+
// CHECK: BridgeObject
12+
// CHECK: NativeObject
13+
// CHECK: RawPointer
14+
// CHECK: Word
15+
16+
// DEMANGLE: $sBID
17+
// CHECK: IntLiteral
18+
19+
// DEMANGLE: $sBf32_D
20+
// DEMANGLE: $sBf64_D
21+
// DEMANGLE: $sBf80_D
22+
// CHECK: FPIEEE32
23+
// CHECK: FPIEEE64
24+
// CHECK: FPIEEE80
25+
26+
// DEMANGLE: $sBi1_D
27+
// DEMANGLE: $sBi8_D
28+
// DEMANGLE: $sBi16_D
29+
// DEMANGLE: $sBi32_D
30+
// DEMANGLE: $sBi64_D
31+
// DEMANGLE: $sBi128_D
32+
// CHECK: Int1
33+
// CHECK: Int8
34+
// CHECK: Int16
35+
// CHECK: Int32
36+
// CHECK: Int64
37+
// CHECK: Int128
38+
39+
// DEMANGLE: $sBf32_Bv2_D
40+
// DEMANGLE: $sBf32_Bv4_D
41+
// DEMANGLE: $sBf32_Bv8_D
42+
// DEMANGLE: $sBf32_Bv16_D
43+
// DEMANGLE: $sBf32_Bv32_D
44+
// DEMANGLE: $sBf32_Bv64_D
45+
// CHECK: Vec2xFPIEEE32
46+
// CHECK: Vec4xFPIEEE32
47+
// CHECK: Vec8xFPIEEE32
48+
// CHECK: Vec16xFPIEEE32
49+
// CHECK: Vec32xFPIEEE32
50+
// CHECK: Vec64xFPIEEE32
51+
52+
// DEMANGLE: $sBf64_Bv2_D
53+
// DEMANGLE: $sBf64_Bv4_D
54+
// DEMANGLE: $sBf64_Bv8_D
55+
// DEMANGLE: $sBf64_Bv16_D
56+
// DEMANGLE: $sBf64_Bv32_D
57+
// DEMANGLE: $sBf64_Bv64_D
58+
// CHECK: Vec2xFPIEEE64
59+
// CHECK: Vec4xFPIEEE64
60+
// CHECK: Vec8xFPIEEE64
61+
// CHECK: Vec16xFPIEEE64
62+
// CHECK: Vec32xFPIEEE64
63+
// CHECK: Vec64xFPIEEE64
64+
65+
// DEMANGLE: $sBi16_Bv2_D
66+
// DEMANGLE: $sBi16_Bv4_D
67+
// DEMANGLE: $sBi16_Bv8_D
68+
// DEMANGLE: $sBi16_Bv16_D
69+
// DEMANGLE: $sBi16_Bv32_D
70+
// DEMANGLE: $sBi16_Bv64_D
71+
// CHECK: Vec2xInt16
72+
// CHECK: Vec4xInt16
73+
// CHECK: Vec8xInt16
74+
// CHECK: Vec16xInt16
75+
// CHECK: Vec32xInt16
76+
// CHECK: Vec64xInt16
77+
78+
// DEMANGLE: $sBi32_Bv2_D
79+
// DEMANGLE: $sBi32_Bv4_D
80+
// DEMANGLE: $sBi32_Bv8_D
81+
// DEMANGLE: $sBi32_Bv16_D
82+
// DEMANGLE: $sBi32_Bv32_D
83+
// DEMANGLE: $sBi32_Bv64_D
84+
// CHECK: Vec2xInt32
85+
// CHECK: Vec4xInt32
86+
// CHECK: Vec8xInt32
87+
// CHECK: Vec16xInt32
88+
// CHECK: Vec32xInt32
89+
// CHECK: Vec64xInt32
90+
91+
// DEMANGLE: $sBi64_Bv2_D
92+
// DEMANGLE: $sBi64_Bv4_D
93+
// DEMANGLE: $sBi64_Bv8_D
94+
// DEMANGLE: $sBi64_Bv16_D
95+
// DEMANGLE: $sBi64_Bv32_D
96+
// DEMANGLE: $sBi64_Bv64_D
97+
// CHECK: Vec2xInt64
98+
// CHECK: Vec4xInt64
99+
// CHECK: Vec8xInt64
100+
// CHECK: Vec16xInt64
101+
// CHECK: Vec32xInt64
102+
// CHECK: Vec64xInt64
103+
104+
// DEMANGLE: $sBi8_Bv2_D
105+
// DEMANGLE: $sBi8_Bv4_D
106+
// DEMANGLE: $sBi8_Bv8_D
107+
// DEMANGLE: $sBi8_Bv16_D
108+
// DEMANGLE: $sBi8_Bv32_D
109+
// DEMANGLE: $sBi8_Bv64_D
110+
// CHECK: Vec2xInt8
111+
// CHECK: Vec4xInt8
112+
// CHECK: Vec8xInt8
113+
// CHECK: Vec16xInt8
114+
// CHECK: Vec32xInt8
115+
// CHECK: Vec64xInt8

unittests/Reflection/TypeRef.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ using Param = remote::FunctionParam<const TypeRef *>;
3333
TEST(TypeRefTest, UniqueBuiltinTypeRef) {
3434
TypeRefBuilder Builder;
3535

36-
auto BI1 = Builder.createBuiltinType(ABC);
37-
auto BI2 = Builder.createBuiltinType(ABC);
38-
auto BI3 = Builder.createBuiltinType(ABCD);
36+
auto BI1 = Builder.createBuiltinType(ABC, ABC);
37+
auto BI2 = Builder.createBuiltinType(ABC, ABC);
38+
auto BI3 = Builder.createBuiltinType(ABCD, ABCD);
3939

4040
EXPECT_EQ(BI1, BI2);
4141
EXPECT_NE(BI2, BI3);

0 commit comments

Comments
 (0)