Skip to content

[Compile Time Constant Extraction] Extract Runtime Metadata Attributes #63503

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 1 commit into from
Feb 11, 2023
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
15 changes: 10 additions & 5 deletions include/swift/AST/ConstTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class RuntimeValue : public CompileTimeValue {
};

struct CustomAttrValue {
swift::CustomAttr *Attr;
const swift::CustomAttr *Attr;
std::vector<FunctionParameter> Parameters;
};

Expand All @@ -212,20 +212,25 @@ struct EnumElementDeclValue {
llvm::Optional<std::vector<EnumElementParameterValue>> Parameters;
};

using AttrValueVector = llvm::SmallVector<CustomAttrValue, 2>;
struct ConstValueTypePropertyInfo {
swift::VarDecl *VarDecl;
std::shared_ptr<CompileTimeValue> Value;
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers;
llvm::Optional<AttrValueVector> PropertyWrappers;
llvm::Optional<AttrValueVector> RuntimeMetadataAttributes;

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

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

struct ConstValueTypeInfo {
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5799,6 +5799,9 @@ class VarDecl : public AbstractStorageDecl {
/// that has an external effect on the function.
bool hasExternalPropertyWrapper() const;

/// Whether this property has any attached runtime metadata attributes.
bool hasRuntimeMetadataAttributes() const;

/// Whether all of the attached property wrappers have an init(wrappedValue:)
/// initializer.
bool allAttachedPropertyWrappersHaveWrappedValueInit() const;
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6814,6 +6814,11 @@ bool VarDecl::hasAttachedPropertyWrapper() const {
return false;
}

/// Whether this property has any attached runtime metadata attributes.
bool VarDecl::hasRuntimeMetadataAttributes() const {
return !getRuntimeDiscoverableAttrs().empty();
}

bool VarDecl::hasImplicitPropertyWrapper() const {
if (getAttrs().hasAttribute<CustomAttr>()) {
if (!getAttachedPropertyWrappers().empty())
Expand Down
112 changes: 72 additions & 40 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,42 +303,52 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
return std::make_shared<RuntimeValue>();
}

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();
}
static CustomAttrValue
extractAttributeValue(const CustomAttr *attr) {
std::vector<FunctionParameter> parameters;
if (const auto *args = attr->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)});
}
parameters.push_back(
{label, argExpr->getType(), extractCompileTimeValue(argExpr)});
}

customAttrValues.push_back({propertyWrapper, parameters});
}
return {attr, parameters};
}

static AttrValueVector
extractPropertyWrapperAttrValues(VarDecl *propertyDecl) {
AttrValueVector customAttrValues;
for (auto *propertyWrapper : propertyDecl->getAttachedPropertyWrappers())
customAttrValues.push_back(extractAttributeValue(propertyWrapper));
return customAttrValues;
}

static AttrValueVector
extractRuntimeMetadataAttrValues(VarDecl *propertyDecl) {
AttrValueVector customAttrValues;
for (auto *runtimeMetadataAttribute : propertyDecl->getRuntimeDiscoverableAttrs())
customAttrValues.push_back(extractAttributeValue(runtimeMetadataAttribute));
return customAttrValues;
}

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

return {propertyDecl, extractCompileTimeValue(originalInit)};
Expand Down Expand Up @@ -578,31 +588,51 @@ void writeValue(llvm::json::OStream &JSON,
}
}

void writeAttributeInfo(llvm::json::OStream &JSON,
const CustomAttrValue &AttrVal,
const ASTContext &ctx) {
JSON.object([&] {
JSON.attribute("type",
toFullyQualifiedTypeNameString(AttrVal.Attr->getType()));
writeLocationInformation(JSON, AttrVal.Attr->getLocation(), ctx);
JSON.attributeArray("arguments", [&] {
for (auto FP : AttrVal.Parameters) {
JSON.object([&] {
JSON.attribute("label", FP.Label);
JSON.attribute("type", toFullyQualifiedTypeNameString(FP.Type));
writeValue(JSON, FP.Value);
});
}
});
});
}

void writePropertyWrapperAttributes(
llvm::json::OStream &JSON,
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers,
llvm::Optional<AttrValueVector> PropertyWrappers,
const ASTContext &ctx) {
if (!PropertyWrappers.has_value()) {
return;
}

JSON.attributeArray("propertyWrappers", [&] {
for (auto PW : PropertyWrappers.value()) {
JSON.object([&] {
JSON.attribute("type",
toFullyQualifiedTypeNameString(PW.Attr->getType()));
writeLocationInformation(JSON, PW.Attr->getLocation(), ctx);
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);
});
}
});
});
}
for (auto PW : PropertyWrappers.value())
writeAttributeInfo(JSON, PW, ctx);
});
}

void writeRuntimeMetadataAttributes(
llvm::json::OStream &JSON,
llvm::Optional<AttrValueVector> RuntimeMetadataAttributes,
const ASTContext &ctx) {
if (!RuntimeMetadataAttributes.has_value() ||
RuntimeMetadataAttributes.value().empty()) {
return;
}

JSON.attributeArray("runtimeMetadataAttributes", [&] {
for (auto RMA : RuntimeMetadataAttributes.value())
writeAttributeInfo(JSON, RMA, ctx);;
});
}

Expand Down Expand Up @@ -737,6 +767,8 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
writeValue(JSON, PropertyInfo.Value);
writePropertyWrapperAttributes(
JSON, PropertyInfo.PropertyWrappers, decl->getASTContext());
writeRuntimeMetadataAttributes(
JSON, PropertyInfo.RuntimeMetadataAttributes, decl->getASTContext());
writeResultBuilderInformation(JSON, TypeDecl, decl);
writeAttrInformation(JSON, decl->getAttrs());
});
Expand Down
53 changes: 53 additions & 0 deletions test/ConstExtraction/ExtractRuntimeMetadataAttr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %empty-directory(%t)
// RUN: echo "[MyProto]" > %t/protocols.json

// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractRuntimeMetadataAttr.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s -enable-experimental-feature RuntimeDiscoverableAttrs
// RUN: cat %t/ExtractRuntimeMetadataAttr.swiftconstvalues 2>&1 | %FileCheck %s

@runtimeMetadata
struct Flag<T> {
init(attachedTo: T.Type, _ description: String = "") {}
init<Args>(attachedTo: (Args) -> T, _ description: String = "") {}
init<Base>(attachedTo: KeyPath<Base, T>, _ description: String = "") {}
}

protocol MyProto {}

struct A : MyProto {
@Flag("v1") var v1: String = "foo"
}

// CHECK: "typeName": "ExtractRuntimeMetadataAttr.A",
// CHECK-NEXT: "kind": "struct",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractRuntimeMetadataAttr.swift",
// CHECK-NEXT: "line": 16,
// CHECK-NEXT: "properties": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "v1",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractRuntimeMetadataAttr.swift",
// CHECK-NEXT: "line": 17,
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "foo",
// CHECK-NEXT: "propertyWrappers": [],
// CHECK-NEXT: "runtimeMetadataAttributes": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ExtractRuntimeMetadataAttr.Flag",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractRuntimeMetadataAttr.swift",
// CHECK-NEXT: "line": 17,
// CHECK-NEXT: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "v1"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT:]