-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathActivatorGenerator.cs
More file actions
145 lines (122 loc) · 5.9 KB
/
Copy pathActivatorGenerator.cs
File metadata and controls
145 lines (122 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Orleans.CodeGenerator.SyntaxGeneration;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Generic;
namespace Orleans.CodeGenerator
{
internal class ActivatorGenerator
{
private readonly IGeneratorServices _generatorServices;
private struct ConstructorArgument
{
public TypeSyntax Type { get; set; }
public string FieldName { get; set; }
public string ParameterName { get; set; }
}
public ActivatorGenerator(IGeneratorServices generatorServices)
{
_generatorServices = generatorServices;
}
public ClassDeclarationSyntax GenerateActivator(ISerializableTypeDescription type)
{
var simpleClassName = GetSimpleClassName(type);
var baseInterface = _generatorServices.LibraryTypes.IActivator_1.ToTypeSyntax(type.TypeSyntax);
var orderedFields = new List<ConstructorArgument>();
var index = 0;
if (type.ActivatorConstructorParameters is { Count: > 0 } parameters)
{
foreach (var arg in parameters)
{
orderedFields.Add(new ConstructorArgument { Type = arg, FieldName = $"_arg{index}", ParameterName = $"arg{index}" });
index++;
}
}
var members = new List<MemberDeclarationSyntax>();
foreach (var field in orderedFields)
{
members.Add(
FieldDeclaration(VariableDeclaration(field.Type, SingletonSeparatedList(VariableDeclarator(field.FieldName))))
.AddModifiers(
Token(SyntaxKind.PrivateKeyword),
Token(SyntaxKind.ReadOnlyKeyword)));
}
if (orderedFields.Count > 0)
members.Add(GenerateConstructor(simpleClassName, orderedFields));
members.Add(GenerateCreateMethod(type, orderedFields));
var classDeclaration = ClassDeclaration(simpleClassName)
.AddBaseListTypes(SimpleBaseType(baseInterface))
.AddModifiers(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword))
.AddAttributeLists(GeneratedCodeUtilities.GetGeneratedCodeAttributes())
.AddMembers(members.ToArray());
if (type.IsGenericType)
{
classDeclaration = SyntaxFactoryUtility.AddGenericTypeParameters(classDeclaration, type.TypeParameters);
}
return classDeclaration;
}
public static string GetSimpleClassName(ISerializableTypeDescription serializableType) => GetSimpleClassName(serializableType.Name);
public static string GetSimpleClassName(string name) => $"Activator_{name}";
/// <summary>
/// Determines whether an activator should be generated for the specified type.
/// </summary>
internal static bool ShouldGenerateActivator(ISerializableTypeDescription type)
{
return !type.IsAbstractType
&& !type.IsEnumType
&& (!type.IsValueType
&& type.IsEmptyConstructable
&& !type.UseActivator
&& type is not GeneratedInvokableDescription
|| type.HasActivatorConstructor);
}
private ConstructorDeclarationSyntax GenerateConstructor(
string simpleClassName,
List<ConstructorArgument> orderedFields)
{
var parameters = new List<ParameterSyntax>();
var body = new List<StatementSyntax>();
foreach (var field in orderedFields)
{
parameters.Add(Parameter(field.ParameterName.ToIdentifier()).WithType(field.Type));
body.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
field.FieldName.ToIdentifierName(),
Unwrapped(field.ParameterName.ToIdentifierName()))));
}
var constructorDeclaration = ConstructorDeclaration(simpleClassName)
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddParameterListParameters(parameters.ToArray())
.AddBodyStatements(body.ToArray());
return constructorDeclaration;
static ExpressionSyntax Unwrapped(ExpressionSyntax expr)
{
return InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("OrleansGeneratedCodeHelper"), IdentifierName("UnwrapService")),
ArgumentList(SeparatedList(new[] { Argument(ThisExpression()), Argument(expr) })));
}
}
private MemberDeclarationSyntax GenerateCreateMethod(ISerializableTypeDescription type, List<ConstructorArgument> orderedFields)
{
ExpressionSyntax createObject;
if (type.ActivatorConstructorParameters is { Count: > 0 })
{
var argList = new List<ArgumentSyntax>();
foreach (var field in orderedFields)
{
argList.Add(Argument(field.FieldName.ToIdentifierName()));
}
createObject = ObjectCreationExpression(type.TypeSyntax).WithArgumentList(ArgumentList(SeparatedList(argList)));
}
else
{
createObject = type.GetObjectCreationExpression();
}
return MethodDeclaration(type.TypeSyntax, "Create")
.WithExpressionBody(ArrowExpressionClause(createObject))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.AddModifiers(Token(SyntaxKind.PublicKeyword));
}
}
}