Skip to content

Commit 9e94cb9

Browse files
authored
[Compile Time Constant Extraction] Extract Types (#63820)
1 parent 92ad107 commit 9e94cb9

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

include/swift/AST/ConstTypeInfo.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CompileTimeValue {
3636
Array,
3737
Tuple,
3838
Enum,
39+
Type,
3940
Runtime
4041
};
4142

@@ -183,6 +184,21 @@ class EnumValue : public CompileTimeValue {
183184
llvm::Optional<std::vector<FunctionParameter>> Parameters;
184185
};
185186

187+
/// An type value representation
188+
class TypeValue : public CompileTimeValue {
189+
public:
190+
TypeValue(swift::Type Type) : CompileTimeValue(ValueKind::Type), Type(Type) {}
191+
192+
swift::Type getType() const { return Type; }
193+
194+
static bool classof(const CompileTimeValue *T) {
195+
return T->getKind() == ValueKind::Type;
196+
}
197+
198+
private:
199+
swift::Type Type;
200+
};
201+
186202
/// A representation of an arbitrary value that does not fall under
187203
/// any of the above categories.
188204
class RuntimeValue : public CompileTimeValue {

lib/ConstExtract/ConstExtract.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
299299
return extractCompileTimeValue(coerceExpr->getSubExpr());
300300
}
301301

302+
case ExprKind::DotSelf: {
303+
auto dotSelfExpr = cast<DotSelfExpr>(expr);
304+
return std::make_shared<TypeValue>(dotSelfExpr->getType());
305+
}
306+
302307
default: {
303308
break;
304309
}
@@ -586,6 +591,16 @@ void writeValue(llvm::json::OStream &JSON,
586591
break;
587592
}
588593

594+
case CompileTimeValue::ValueKind::Type: {
595+
auto typeValue = cast<TypeValue>(value);
596+
JSON.attribute("valueKind", "Type");
597+
JSON.attributeObject("value", [&]() {
598+
JSON.attribute("type",
599+
toFullyQualifiedTypeNameString(typeValue->getType()));
600+
});
601+
break;
602+
}
603+
589604
case CompileTimeValue::ValueKind::Runtime: {
590605
JSON.attribute("valueKind", "Runtime");
591606
break;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "[MyProto]" > %t/protocols.json
3+
4+
// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractTypes.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
5+
// RUN: cat %t/ExtractTypes.swiftconstvalues 2>&1 | %FileCheck %s
6+
7+
protocol MyProto {}
8+
protocol ContainerType {}
9+
10+
struct TypeA: ContainerType {}
11+
enum TypeB: ContainerType {}
12+
final class TypeC: ContainerType {}
13+
14+
public struct Types : MyProto {
15+
static var types1: [ContainerType.Type] = [
16+
TypeA.self,
17+
TypeB.self,
18+
TypeC.self
19+
]
20+
}
21+
22+
// CHECK: "label": "types1",
23+
// CHECK-NEXT: "type": "Swift.Array<ExtractTypes.ContainerType.Type>",
24+
// CHECK: "valueKind": "Array",
25+
// CHECK-NEXT: "value": [
26+
// CHECK-NEXT: {
27+
// CHECK-NEXT: "valueKind": "Type",
28+
// CHECK-NEXT: "value": {
29+
// CHECK-NEXT: "type": "ExtractTypes.TypeA.Type"
30+
// CHECK-NEXT: }
31+
// CHECK-NEXT: },
32+
// CHECK-NEXT: {
33+
// CHECK-NEXT: "valueKind": "Type",
34+
// CHECK-NEXT: "value": {
35+
// CHECK-NEXT: "type": "ExtractTypes.TypeB.Type"
36+
// CHECK-NEXT: }
37+
// CHECK-NEXT: },
38+
// CHECK-NEXT: {
39+
// CHECK-NEXT: "valueKind": "Type",
40+
// CHECK-NEXT: "value": {
41+
// CHECK-NEXT: "type": "ExtractTypes.TypeC.Type"
42+
// CHECK-NEXT: }
43+
// CHECK-NEXT: }
44+
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)