Skip to content

[Compile Time Constant Extraction] Add extraction of property wrappers #62555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/swift/AST/ConstTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,25 @@ class RuntimeValue : public CompileTimeValue {
}
};

struct CustomAttrValue {
swift::Type Type;
std::vector<FunctionParameter> Parameters;
};

struct ConstValueTypePropertyInfo {
swift::VarDecl *VarDecl;
std::shared_ptr<CompileTimeValue> Value;
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers;

ConstValueTypePropertyInfo(
swift::VarDecl *VarDecl, std::shared_ptr<CompileTimeValue> Value,
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers)
: VarDecl(VarDecl), Value(Value), PropertyWrappers(PropertyWrappers) {}

ConstValueTypePropertyInfo(swift::VarDecl *VarDecl,
std::shared_ptr<CompileTimeValue> Value)
: VarDecl(VarDecl), Value(Value),
PropertyWrappers(llvm::Optional<std::vector<CustomAttrValue>>()) {}
};

struct ConstValueTypeInfo {
Expand Down
80 changes: 69 additions & 11 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,57 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
return std::make_shared<RuntimeValue>();
}

static std::shared_ptr<CompileTimeValue>
extractPropertyInitializationValue(VarDecl *propertyDecl) {
if (auto binding = propertyDecl->getParentPatternBinding()) {
if (auto originalInit = binding->getOriginalInit(0)) {
return extractCompileTimeValue(originalInit);
static std::vector<CustomAttrValue>
extractCustomAttrValues(VarDecl *propertyDecl) {
std::vector<CustomAttrValue> customAttrValues;

for (auto *propertyWrapper : propertyDecl->getAttachedPropertyWrappers()) {
std::vector<FunctionParameter> parameters;

if (const auto *args = propertyWrapper->getArgs()) {
for (auto arg : *args) {
const auto label = arg.getLabel().str().str();
auto argExpr = arg.getExpr();

if (auto defaultArgument = dyn_cast<DefaultArgumentExpr>(argExpr)) {
auto *decl = defaultArgument->getParamDecl();
if (decl->hasDefaultExpr()) {
argExpr = decl->getTypeCheckedDefaultExpr();
}
}
parameters.push_back(
{label, argExpr->getType(), extractCompileTimeValue(argExpr)});
}
}
customAttrValues.push_back({propertyWrapper->getType(), parameters});
}

return customAttrValues;
}

static ConstValueTypePropertyInfo
extractTypePropertyInfo(VarDecl *propertyDecl) {
if (const auto binding = propertyDecl->getParentPatternBinding()) {
if (const auto originalInit = binding->getOriginalInit(0)) {
if (propertyDecl->hasAttachedPropertyWrapper()) {
return {propertyDecl, extractCompileTimeValue(originalInit),
extractCustomAttrValues(propertyDecl)};
}

return {propertyDecl, extractCompileTimeValue(originalInit)};
}
}

if (auto accessorDecl = propertyDecl->getAccessor(AccessorKind::Get)) {
auto node = accessorDecl->getTypecheckedBody()->getFirstElement();
if (node.is<Stmt *>()) {
if (auto returnStmt = dyn_cast<ReturnStmt>(node.get<Stmt *>())) {
return extractCompileTimeValue(returnStmt->getResult());
return {propertyDecl, extractCompileTimeValue(returnStmt->getResult())};
}
}
}

return std::make_shared<RuntimeValue>();
return {propertyDecl, std::make_shared<RuntimeValue>()};
}

ConstValueTypeInfo
Expand All @@ -228,8 +261,7 @@ ConstantValueInfoRequest::evaluate(Evaluator &Evaluator,

std::vector<ConstValueTypePropertyInfo> Properties;
for (auto Property : StoredProperties) {
Properties.push_back(
{Property, extractPropertyInitializationValue(Property)});
Properties.push_back(extractTypePropertyInfo(Property));
}

for (auto Member : Decl->getMembers()) {
Expand All @@ -238,13 +270,13 @@ ConstantValueInfoRequest::evaluate(Evaluator &Evaluator,
// instead gather up remaining static and computed properties.
if (!VD || StoredPropertiesSet.count(VD))
continue;
Properties.push_back({VD, extractPropertyInitializationValue(VD)});
Properties.push_back(extractTypePropertyInfo(VD));
}

for (auto Extension: Decl->getExtensions()) {
for (auto Member : Extension->getMembers()) {
if (auto *VD = dyn_cast<VarDecl>(Member)) {
Properties.push_back({VD, extractPropertyInitializationValue(VD)});
Properties.push_back(extractTypePropertyInfo(VD));
}
}
}
Expand Down Expand Up @@ -348,6 +380,31 @@ void writeValue(llvm::json::OStream &JSON,
}
}

void writeAttributes(
llvm::json::OStream &JSON,
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers) {
if (!PropertyWrappers.hasValue()) {
return;
}

JSON.attributeArray("attributes", [&] {
for (auto PW : PropertyWrappers.value()) {
JSON.object([&] {
JSON.attribute("type", toFullyQualifiedTypeNameString(PW.Type));
JSON.attributeArray("arguments", [&] {
for (auto FP : PW.Parameters) {
JSON.object([&] {
JSON.attribute("label", FP.Label);
JSON.attribute("type", toFullyQualifiedTypeNameString(FP.Type));
writeValue(JSON, FP.Value);
});
}
});
});
}
});
}

bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
llvm::raw_fd_ostream &OS) {
llvm::json::OStream JSON(OS, 2);
Expand All @@ -371,6 +428,7 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
JSON.attribute("isComputed",
!decl->hasStorage() ? "true" : "false");
writeValue(JSON, PropertyInfo.Value);
writeAttributes(JSON, PropertyInfo.PropertyWrappers);
});
}
});
Expand Down
1 change: 0 additions & 1 deletion test/ConstExtraction/ExtractCalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public struct Foo : MyProto {
let func1: Int = adder(2, 3)
}


extension Foo {
struct Boo {}

Expand Down
170 changes: 170 additions & 0 deletions test/ConstExtraction/ExtractLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,119 @@
// CHECK-NEXT: "value": "\"Hello, World\""
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "typeName": "ExtractLiterals.PropertyWrappers",
// CHECK-NEXT: "kind": "struct",
// CHECK-NEXT: "properties": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "_propertyWrapper1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessarily a comment on this, and the current output actually captures what we do to the AST to de-sugar property-wrapped properties.

But I wonder if its best to leave this as-is and let clients figure out how to connect a property with an attribute to its corresponding underlying _ and $ properties, or if we can bundle them up here somehow. Wdyt @jPaolantonio?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a really great question, and one I don't quite have the answer for yet.

I briefly considered omitting _ and $ prefaced properties until we determined if they should be represented differently, but decided against it.

I think leaving them as-is for now to match the AST seems reasonable, but open to other ideas.

// CHECK-NEXT: "type": "ExtractLiterals.Buffered<Swift.String>",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "_propertyWrapper2",
// CHECK-NEXT: "type": "ExtractLiterals.Clamping<Swift.Int>",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "_propertyWrapper3",
// CHECK-NEXT: "type": "ExtractLiterals.Buffered<ExtractLiterals.Clamping<Swift.Int>>",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "propertyWrapper1",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "\"Hello\"",
// CHECK-NEXT: "attributes": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ExtractLiterals.Buffered",
// CHECK-NEXT: "arguments": []
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "$propertyWrapper1",
// CHECK-NEXT: "type": "(Swift.String, Swift.String?)",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "propertyWrapper2",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "128",
// CHECK-NEXT: "attributes": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ExtractLiterals.Clamping",
// CHECK-NEXT: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "min",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "0"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "max",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "255"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "propertyWrapper3",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "128",
// CHECK-NEXT: "attributes": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ExtractLiterals.Buffered",
// CHECK-NEXT: "arguments": []
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ExtractLiterals.Clamping",
// CHECK-NEXT: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "min",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "0"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "max",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "255"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "$propertyWrapper3",
// CHECK-NEXT: "type": "(ExtractLiterals.Clamping<Swift.Int>, ExtractLiterals.Clamping<Swift.Int>?)",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT:]

Expand All @@ -107,3 +220,60 @@ public struct Floats : MyProto {
public struct Strings : MyProto {
let string1: String = "Hello, World"
}

public struct PropertyWrappers : MyProto {
@Buffered
var propertyWrapper1: String = "Hello"

@Clamping(min: 0, max: 255)
var propertyWrapper2: Int = 128

@Buffered @Clamping(min: 0, max: 255)
var propertyWrapper3: Int = 128
}

@propertyWrapper
struct Clamping<V: Comparable> {
var value: V
let min: V
let max: V

init(wrappedValue: V, min: V, max: V) {
self.value = wrappedValue
self.min = min
self.max = max
}

var wrappedValue: V {
get { return self.value }
set {
if newValue < self.min {
self.value = self.min
} else if newValue > self.max {
self.value = self.max
} else {
self.value = newValue
}
}
}
}

@propertyWrapper
struct Buffered<V> {
var value: V
var lastValue: V?

init(wrappedValue: V) {
self.value = wrappedValue
}

var wrappedValue: V {
get { return value }
set {
self.lastValue = self.value
self.value = newValue
}
}

var projectedValue: (V, V?) { (self.value, self.lastValue) }
}