Skip to content

Commit 33248b4

Browse files
authored
Merge pull request #60655 from artemcm/ConstSideCarFeature
[Compile Time Constant Extraction] Emit fully-qualified types and more kinds of literal expressions
2 parents baeb988 + 869e70f commit 33248b4

File tree

3 files changed

+68
-30
lines changed

3 files changed

+68
-30
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class NominalTypeConformanceCollector : public swift::ASTWalker {
4848
}
4949
};
5050

51+
std::string toFullyQualifiedTypeNameString(const swift::Type &Type) {
52+
std::string TypeNameOutput;
53+
llvm::raw_string_ostream OutputStream(TypeNameOutput);
54+
swift::PrintOptions Options;
55+
Options.FullyQualifiedTypes = true;
56+
Options.PreferTypeRepr = true;
57+
Type.print(OutputStream, Options);
58+
OutputStream.flush();
59+
return TypeNameOutput;
60+
}
61+
5162
} // namespace
5263

5364
namespace swift {
@@ -103,27 +114,11 @@ extractPropertyInitializationValue(VarDecl *propertyDecl) {
103114
if (binding) {
104115
auto originalInit = binding->getOriginalInit(0);
105116
if (originalInit) {
106-
// Integer Literals
107-
if (auto integerExpr =
108-
dyn_cast_or_null<IntegerLiteralExpr>(originalInit)) {
109-
std::string LiteralOutput;
110-
llvm::raw_string_ostream OutputStream(LiteralOutput);
111-
integerExpr->printConstExprValue(&OutputStream, nullptr);
112-
OutputStream.flush();
117+
std::string LiteralOutput;
118+
llvm::raw_string_ostream OutputStream(LiteralOutput);
119+
originalInit->printConstExprValue(&OutputStream, nullptr);
120+
if (!LiteralOutput.empty())
113121
return std::make_shared<RawLiteralValue>(LiteralOutput);
114-
// Float Literals
115-
} else if (auto floatExpr =
116-
dyn_cast_or_null<FloatLiteralExpr>(originalInit)) {
117-
std::string LiteralOutput;
118-
llvm::raw_string_ostream OutputStream(LiteralOutput);
119-
floatExpr->printConstExprValue(&OutputStream, nullptr);
120-
OutputStream.flush();
121-
return std::make_shared<RawLiteralValue>(LiteralOutput);
122-
// String Literals
123-
} else if (auto stringExpr =
124-
dyn_cast_or_null<StringLiteralExpr>(originalInit)) {
125-
return std::make_shared<RawLiteralValue>(stringExpr->getValue().str());
126-
}
127122
}
128123
}
129124

@@ -187,7 +182,7 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
187182
return Result;
188183
}
189184

190-
static std::string toString(const CompileTimeValue *Value) {
185+
std::string toString(const CompileTimeValue *Value) {
191186
switch (Value->getKind()) {
192187
case CompileTimeValue::RawLiteral:
193188
return cast<RawLiteralValue>(Value)->getValue();
@@ -209,7 +204,7 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
209204
for (const auto &TypeInfo : ConstValueInfos) {
210205
JSON.object([&] {
211206
const auto *TypeDecl = TypeInfo.TypeDecl;
212-
JSON.attribute("typeName", TypeDecl->getName().str().str());
207+
JSON.attribute("typeName", toFullyQualifiedTypeNameString(TypeDecl->getDeclaredInterfaceType()));
213208
JSON.attribute(
214209
"kind",
215210
TypeDecl->getDescriptiveKindName(TypeDecl->getDescriptiveKind())
@@ -219,7 +214,7 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
219214
JSON.object([&] {
220215
const auto *PropertyDecl = PropertyInfo.VarDecl;
221216
JSON.attribute("label", PropertyDecl->getName().str().str());
222-
JSON.attribute("type", PropertyDecl->getType().getString());
217+
JSON.attribute("type", toFullyQualifiedTypeNameString(PropertyDecl->getType()));
223218
JSON.attribute("isStatic",
224219
PropertyDecl->isStatic() ? "true" : "false");
225220
JSON.attribute("isComputed",

lib/Option/features.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
},
2424
{
2525
"name": "enable-bare-slash-regex-updated"
26-
}
26+
},
27+
{
28+
"name": "emit-const-value-sidecar"
29+
}
2730
]
2831
}

test/ConstExtraction/fields.swift

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,75 @@
77
// CHECK: [
88
// CHECK-NEXT:
99
// CHECK-NEXT: {
10-
// CHECK-NEXT: "typeName": "Foo",
10+
// CHECK-NEXT: "typeName": "fields.Foo",
1111
// CHECK-NEXT: "kind": "struct",
1212
// CHECK-NEXT: "properties": [
1313
// CHECK-NEXT: {
1414
// CHECK-NEXT: "label": "p1",
15-
// CHECK-NEXT: "type": "String",
15+
// CHECK-NEXT: "type": "Swift.String",
1616
// CHECK-NEXT: "isStatic": "false",
1717
// CHECK-NEXT: "isComputed": "false",
1818
// CHECK-NEXT: "value": "Hello, World"
1919
// CHECK-NEXT: },
2020
// CHECK-NEXT: {
21+
// CHECK-NEXT: "label": "p5",
22+
// CHECK-NEXT: "type": "[Swift.Int]",
23+
// CHECK-NEXT: "isStatic": "false",
24+
// CHECK-NEXT: "isComputed": "false",
25+
// CHECK-NEXT: "value": "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
26+
// CHECK-NEXT: },
27+
// CHECK-NEXT: {
28+
// CHECK-NEXT: "label": "p6",
29+
// CHECK-NEXT: "type": "Swift.Bool",
30+
// CHECK-NEXT: "isStatic": "false",
31+
// CHECK-NEXT: "isComputed": "false",
32+
// CHECK-NEXT: "value": "false"
33+
// CHECK-NEXT: },
34+
// CHECK-NEXT: {
35+
// CHECK-NEXT: "label": "p7",
36+
// CHECK-NEXT: "type": "Swift.Bool?",
37+
// CHECK-NEXT: "isStatic": "false",
38+
// CHECK-NEXT: "isComputed": "false",
39+
// CHECK-NEXT: "value": "nil"
40+
// CHECK-NEXT: },
41+
// CHECK-NEXT: {
42+
// CHECK-NEXT: "label": "p8",
43+
// CHECK-NEXT: "type": "(Swift.Int, Swift.Float)",
44+
// CHECK-NEXT: "isStatic": "false",
45+
// CHECK-NEXT: "isComputed": "false",
46+
// CHECK-NEXT: "value": "(42, 6.6)"
47+
// CHECK-NEXT: },
48+
// CHECK-NEXT: {
49+
// CHECK-NEXT: "label": "p9",
50+
// CHECK-NEXT: "type": "[Swift.String : Swift.Int]",
51+
// CHECK-NEXT: "isStatic": "false",
52+
// CHECK-NEXT: "isComputed": "false",
53+
// CHECK-NEXT: "value": "[(\"One\", 1), (\"Two\", 2), (\"Three\", 3)]"
54+
// CHECK-NEXT: },
55+
// CHECK-NEXT: {
2156
// CHECK-NEXT: "label": "p0",
22-
// CHECK-NEXT: "type": "Int",
57+
// CHECK-NEXT: "type": "Swift.Int",
2358
// CHECK-NEXT: "isStatic": "true",
2459
// CHECK-NEXT: "isComputed": "false",
2560
// CHECK-NEXT: "value": "11"
2661
// CHECK-NEXT: },
2762
// CHECK-NEXT: {
2863
// CHECK-NEXT: "label": "p2",
29-
// CHECK-NEXT: "type": "Float",
64+
// CHECK-NEXT: "type": "Swift.Float",
3065
// CHECK-NEXT: "isStatic": "true",
3166
// CHECK-NEXT: "isComputed": "false",
3267
// CHECK-NEXT: "value": "42.2"
3368
// CHECK-NEXT: },
3469
// CHECK-NEXT: {
3570
// CHECK-NEXT: "label": "p3",
36-
// CHECK-NEXT: "type": "Int",
71+
// CHECK-NEXT: "type": "Swift.Int",
3772
// CHECK-NEXT: "isStatic": "false",
3873
// CHECK-NEXT: "isComputed": "true",
3974
// CHECK-NEXT: "value": "Unknown"
4075
// CHECK-NEXT: },
4176
// CHECK-NEXT: {
4277
// CHECK-NEXT: "label": "p4",
43-
// CHECK-NEXT: "type": "Int",
78+
// CHECK-NEXT: "type": "Swift.Int",
4479
// CHECK-NEXT: "isStatic": "true",
4580
// CHECK-NEXT: "isComputed": "true",
4681
// CHECK-NEXT: "value": "Unknown"
@@ -57,6 +92,11 @@ public struct Foo {
5792
static let p2: Float = 42.2
5893
var p3: Int {3}
5994
static var p4: Int {3}
95+
let p5: [Int] = [1,2,3,4,5,6,7,8,9]
96+
let p6: Bool = false
97+
let p7: Bool? = nil
98+
let p8: (Int, Float) = (42, 6.6)
99+
let p9: [String: Int] = ["One": 1, "Two": 2, "Three": 3]
60100
}
61101

62102
extension Foo : MyProto {}

0 commit comments

Comments
 (0)