Skip to content

Commit 03456ac

Browse files
authored
Merge pull request #64148 from artemcm/ConstExtractMangledTypeValue
[Compile Time Constant Extraction] Extract mangled names for Type values
2 parents 5665705 + 87f1c2f commit 03456ac

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/ConstExtract/ConstExtract.h"
1414
#include "swift/AST/ASTContext.h"
15+
#include "swift/AST/ASTMangler.h"
1516
#include "swift/AST/ASTWalker.h"
1617
#include "swift/AST/Decl.h"
1718
#include "swift/AST/DiagnosticEngine.h"
@@ -75,6 +76,10 @@ std::string toFullyQualifiedTypeNameString(const swift::Type &Type) {
7576
return TypeNameOutput;
7677
}
7778

79+
std::string toMangledTypeNameString(const swift::Type &Type) {
80+
return Mangle::ASTMangler().mangleTypeWithoutPrefix(Type);
81+
}
82+
7883
} // namespace
7984

8085
namespace swift {
@@ -623,10 +628,13 @@ void writeValue(llvm::json::OStream &JSON,
623628

624629
case CompileTimeValue::ValueKind::Type: {
625630
auto typeValue = cast<TypeValue>(value);
631+
Type type = typeValue->getType();
626632
JSON.attribute("valueKind", "Type");
627633
JSON.attributeObject("value", [&]() {
628634
JSON.attribute("type",
629-
toFullyQualifiedTypeNameString(typeValue->getType()));
635+
toFullyQualifiedTypeNameString(type));
636+
JSON.attribute("mangledName",
637+
toMangledTypeNameString(type));
630638
});
631639
break;
632640
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "[MyProto]" > %t/protocols.json
3+
4+
// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractLiterals.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
5+
// RUN: cat %t/ExtractLiterals.swiftconstvalues 2>&1 | %FileCheck %s
6+
7+
protocol MyProto {}
8+
9+
protocol Bird {}
10+
struct Warbler<T> : Bird {}
11+
struct Avocet : Bird {}
12+
struct RainbowLorikeet : Bird {}
13+
14+
struct TypeValuePropertyStruct : MyProto {
15+
var birdTypes: [any Bird.Type] = [Warbler<String>.self, Avocet.self, RainbowLorikeet.self]
16+
}
17+
18+
// CHECK: "label": "birdTypes",
19+
// CHECK-NEXT: "type": "Swift.Array<ExtractTypeValue.Bird.Type>",
20+
// CHECK-NEXT: "isStatic": "false",
21+
// CHECK-NEXT: "isComputed": "false",
22+
// CHECK-NEXT: "file": "{{.*}}ExtractTypeValue.swift",
23+
// CHECK-NEXT: "line": 15,
24+
// CHECK-NEXT: "valueKind": "Array",
25+
// CHECK-NEXT: "value": [
26+
// CHECK-NEXT: {
27+
// CHECK-NEXT: "valueKind": "Type",
28+
// CHECK-NEXT: "value": {
29+
// CHECK-NEXT: "type": "ExtractTypeValue.Warbler<Swift.String>.Type",
30+
// CHECK-NEXT: "mangledName": "16ExtractTypeValue7WarblerVySSGm"
31+
// CHECK-NEXT: }
32+
// CHECK-NEXT: },
33+
// CHECK-NEXT: {
34+
// CHECK-NEXT: "valueKind": "Type",
35+
// CHECK-NEXT: "value": {
36+
// CHECK-NEXT: "type": "ExtractTypeValue.Avocet.Type",
37+
// CHECK-NEXT: "mangledName": "16ExtractTypeValue6AvocetVm"
38+
// CHECK-NEXT: }
39+
// CHECK-NEXT: },
40+
// CHECK-NEXT: {
41+
// CHECK-NEXT: "valueKind": "Type",
42+
// CHECK-NEXT: "value": {
43+
// CHECK-NEXT: "type": "ExtractTypeValue.RainbowLorikeet.Type",
44+
// CHECK-NEXT: "mangledName": "16ExtractTypeValue15RainbowLorikeetVm"
45+
// CHECK-NEXT: }
46+
// CHECK-NEXT: }
47+
// CHECK-NEXT: ]

test/ConstExtraction/ExtractTypes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ public struct Types : MyProto {
2727
// CHECK-NEXT: "valueKind": "Type",
2828
// CHECK-NEXT: "value": {
2929
// CHECK-NEXT: "type": "ExtractTypes.TypeA.Type"
30+
// CHECK-NEXT: "mangledName": "12ExtractTypes5TypeAVm"
3031
// CHECK-NEXT: }
3132
// CHECK-NEXT: },
3233
// CHECK-NEXT: {
3334
// CHECK-NEXT: "valueKind": "Type",
3435
// CHECK-NEXT: "value": {
3536
// CHECK-NEXT: "type": "ExtractTypes.TypeB.Type"
37+
// CHECK-NEXT: "mangledName": "12ExtractTypes5TypeBOm"
3638
// CHECK-NEXT: }
3739
// CHECK-NEXT: },
3840
// CHECK-NEXT: {
3941
// CHECK-NEXT: "valueKind": "Type",
4042
// CHECK-NEXT: "value": {
4143
// CHECK-NEXT: "type": "ExtractTypes.TypeC.Type"
44+
// CHECK-NEXT: "mangledName": "12ExtractTypes5TypeCCm"
4245
// CHECK-NEXT: }
4346
// CHECK-NEXT: }
4447
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)