Skip to content
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

Lazily convert attribute data into statement properties #5745

Merged
merged 1 commit into from
Jan 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ namespace Microsoft.Generator.CSharp.Statements
{
public sealed class AttributeStatement : MethodBodyStatement
{
public CSharpType Type { get; }
public IReadOnlyList<ValueExpression> Arguments { get; }
public IReadOnlyList<KeyValuePair<string, ValueExpression>> PositionalArguments { get; }
public CSharpType Type => _type ??= Data!.AttributeClass!.GetCSharpType();
private CSharpType? _type;

public IReadOnlyList<ValueExpression> Arguments
=> _arguments ??= Data!.ConstructorArguments.SelectMany(ConvertArgumentToValueExpression).ToList();
private IReadOnlyList<ValueExpression>? _arguments;

public IReadOnlyList<KeyValuePair<string, ValueExpression>> PositionalArguments
=> _positionalArguments ??= Data!.NamedArguments.Select(a => new KeyValuePair<string, ValueExpression>(a.Key, ConvertArgumentToValueExpression(a.Value)[0])).ToList();
private IReadOnlyList<KeyValuePair<string, ValueExpression>>? _positionalArguments;

internal AttributeData? Data { get; }

public AttributeStatement(CSharpType type, IReadOnlyList<ValueExpression> arguments, IReadOnlyList<KeyValuePair<string, ValueExpression>> positionalArguments)
{
Type = type;
Arguments = arguments;
PositionalArguments = positionalArguments;
_type = type;
_arguments = arguments;
_positionalArguments = positionalArguments;
}

public AttributeStatement(CSharpType type, IReadOnlyList<ValueExpression> arguments) : this(type, arguments, []) { }
Expand All @@ -35,12 +42,9 @@ public AttributeStatement(CSharpType type, params ValueExpression[] arguments) :
internal AttributeStatement(AttributeData data)
{
Data = data;
Type = data.AttributeClass!.GetCSharpType();
Arguments = data.ConstructorArguments.SelectMany(ConvertArgumentToValueExpression).ToList();
PositionalArguments = data.NamedArguments.Select(a => new KeyValuePair<string, ValueExpression>(a.Key, ConvertArgumentToValueExpression(a.Value)[0])).ToList();
}

private static ValueExpression[] ConvertArgumentToValueExpression(TypedConstant argument)
private static IReadOnlyList<ValueExpression> ConvertArgumentToValueExpression(TypedConstant argument)
{
return argument.Kind switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ public async Task CustomCodeAttributesAreLoadedIntoAttributeStatements()
Assert.AreEqual("[global::Microsoft.Generator.CSharp.Customization.CodeGenSuppressAttribute(\"MockInputClient\", typeof(bool), typeof(int))]\n", attributes[2].ToDisplayString());
Assert.AreEqual("[global::Microsoft.Generator.CSharp.Customization.CodeGenSerializationAttribute(\"MockInputClient\", SerializationValueHook = \"foo\", DeserializationValueHook = \"bar\")]\n", attributes[3].ToDisplayString());

// validate that the properties are cached
Assert.AreSame(attributes[0].Type, attributes[0].Type);
Assert.AreSame(attributes[0].Arguments, attributes[0].Arguments);
Assert.AreSame(attributes[0].PositionalArguments, attributes[0].PositionalArguments);

}

private class ClientOutputLibrary : OutputLibrary
Expand Down
Loading