Skip to content

Commit 55605ce

Browse files
committed
ASTDemangler: Implement DynamicSelfType
1 parent 9516575 commit 55605ce

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class ASTBuilder {
104104

105105
ProtocolDecl *createObjCProtocolDecl(StringRef name);
106106

107+
Type createDynamicSelfType(Type selfType);
108+
107109
Type createForeignClassType(StringRef mangledName);
108110

109111
Type getUnnamedForeignClassType();

include/swift/Demangling/TypeDecoder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,16 @@ class TypeDecoder {
288288

289289
return BuiltType();
290290
}
291+
case NodeKind::DynamicSelf: {
292+
if (Node->getNumChildren() != 1)
293+
return BuiltType();
294+
295+
auto selfType = decodeMangledType(Node->getChild(0));
296+
if (!selfType)
297+
return BuiltType();
291298

299+
return Builder.createDynamicSelfType(selfType);
300+
}
292301
case NodeKind::DependentGenericParamType: {
293302
auto depth = Node->getChild(0)->getIndex();
294303
auto index = Node->getChild(1)->getIndex();

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ class TypeRefBuilder {
329329
return SILBoxTypeRef::create(*this, base);
330330
}
331331

332+
const TypeRef *createDynamicSelfType(const TypeRef *selfType) {
333+
// TypeRefs should not contain DynamicSelfType.
334+
return nullptr;
335+
}
336+
332337
const ObjCClassTypeRef *getUnnamedObjCClassType() {
333338
return createObjCClassType("");
334339
}

lib/AST/ASTDemangler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ ProtocolDecl *ASTBuilder::createObjCProtocolDecl(StringRef name) {
316316
return nullptr;
317317
}
318318

319+
Type ASTBuilder::createDynamicSelfType(Type selfType) {
320+
return DynamicSelfType::get(selfType, Ctx);
321+
}
322+
319323
Type ASTBuilder::createForeignClassType(StringRef mangledName) {
320324
auto typeDecl = createNominalTypeDecl(mangledName);
321325
if (!typeDecl) return Type();

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,11 @@ class DecodedMetadataBuilder {
11201120
protocols.size(), protocols.data());
11211121
}
11221122

1123+
BuiltType createDynamicSelfType(BuiltType selfType) const {
1124+
// Free-standing mangled type strings should not contain DynamicSelfType.
1125+
return BuiltType();
1126+
}
1127+
11231128
BuiltType createGenericTypeParameterType(unsigned depth,
11241129
unsigned index) const {
11251130
// Use the callback, when provided.

test/TypeDecoder/dynamic_self.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift -emit-executable %s -g -o %t/dynamic_self -emit-module
4+
// RUN: sed -ne '/\/\/ *DEMANGLE: /s/\/\/ *DEMANGLE: *//p' < %s > %t/input
5+
// RUN: %lldb-moduleimport-test %t/dynamic_self -type-from-mangled=%t/input | %FileCheck %s
6+
7+
class Me {
8+
func mine() -> Self {
9+
let metatype = type(of: self)
10+
return metatype.init()
11+
}
12+
13+
required init() {}
14+
}
15+
16+
// DEMANGLE: $s12dynamic_self2MeCXDXMTD
17+
// CHECK: Self.Type

0 commit comments

Comments
 (0)