-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathActivatorGenerator.cs
More file actions
184 lines (157 loc) · 7.86 KB
/
Copy pathActivatorGenerator.cs
File metadata and controls
184 lines (157 loc) · 7.86 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Orleans.CodeGenerator.SyntaxGeneration;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Generic;
using System.Linq;
namespace Orleans.CodeGenerator
{
internal class ActivatorGenerator
{
private readonly CodeGenerator _codeGenerator;
private struct ConstructorArgument
{
public TypeSyntax Type { get; set; }
public string FieldName { get; set; }
public string ParameterName { get; set; }
public bool IsPool { get; set; }
}
public ActivatorGenerator(CodeGenerator codeGenerator)
{
_codeGenerator = codeGenerator;
}
public ClassDeclarationSyntax GenerateActivator(ISerializableTypeDescription type)
{
var simpleClassName = GetSimpleClassName(type);
var baseInterface = _codeGenerator.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)
{
// Detect if this is an InvokablePool<T> parameter
var isPool = arg is GenericNameSyntax gns && gns.Identifier.Text == "InvokablePool";
orderedFields.Add(new ConstructorArgument { Type = arg, FieldName = $"_arg{index}", ParameterName = $"arg{index}", IsPool = isPool });
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(CodeGenerator.GetGeneratedCodeAttributes())
.AddMembers(members.ToArray());
if (type.IsGenericType)
{
classDeclaration = SyntaxFactoryUtility.AddGenericTypeParameters(classDeclaration, type.TypeParameters);
}
return classDeclaration;
}
public static string GetSimpleClassName(ISerializableTypeDescription serializableType) => $"Activator_{serializableType.Name}";
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));
// Pool fields are not wrapped services, assign directly
if (field.IsPool)
{
body.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
field.FieldName.ToIdentifierName(),
field.ParameterName.ToIdentifierName())));
}
else
{
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)
{
// Check if this is a poolable invokable (has InvokablePool<T> as first constructor argument)
var poolField = orderedFields.FirstOrDefault(f => f.IsPool);
if (poolField.IsPool)
{
// Generate: _pool.TryGet(out var item) ? item : new T(_pool, ...otherArgs)
var argList = new List<ArgumentSyntax>();
foreach (var field in orderedFields)
{
argList.Add(Argument(field.FieldName.ToIdentifierName()));
}
var newExpression = ObjectCreationExpression(type.TypeSyntax)
.WithArgumentList(ArgumentList(SeparatedList(argList)));
// _pool.TryGet(out var item)
var tryGetCall = InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
poolField.FieldName.ToIdentifierName(),
IdentifierName("TryGet")),
ArgumentList(SingletonSeparatedList(
Argument(DeclarationExpression(
IdentifierName("var"),
SingleVariableDesignation(Identifier("item"))))
.WithRefKindKeyword(Token(SyntaxKind.OutKeyword)))));
// Conditional: tryGet ? item : new T(...)
var conditionalExpression = ConditionalExpression(
tryGetCall,
IdentifierName("item"),
newExpression);
return MethodDeclaration(type.TypeSyntax, "Create")
.WithExpressionBody(ArrowExpressionClause(conditionalExpression))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.AddModifiers(Token(SyntaxKind.PublicKeyword));
}
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));
}
}
}