-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathGeneratedInvokableDescription.cs
More file actions
112 lines (104 loc) · 5.49 KB
/
Copy pathGeneratedInvokableDescription.cs
File metadata and controls
112 lines (104 loc) · 5.49 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
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Orleans.CodeGenerator.SyntaxGeneration;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Orleans.CodeGenerator;
[DebuggerDisplay("{MethodDescription}")]
internal sealed class GeneratedInvokableDescription : ISerializableTypeDescription
{
public GeneratedInvokableDescription(
InvokableMethodDescription methodDescription,
Accessibility accessibility,
string generatedClassName,
string generatedNamespaceName,
List<IMemberDescription> members,
List<INamedTypeSymbol> serializationHooks,
INamedTypeSymbol baseType,
List<TypeSyntax> constructorArguments,
bool usesInvokablePool,
List<CompoundTypeAliasComponent[]> compoundTypeAliases,
string? returnValueInitializerMethod,
ClassDeclarationSyntax classDeclarationSyntax)
{
if (methodDescription.AllTypeParameters.Count == 0)
{
MetadataName = $"{generatedNamespaceName}.{generatedClassName}";
}
else
{
MetadataName = $"{generatedNamespaceName}.{generatedClassName}`{methodDescription.AllTypeParameters.Count}";
}
BaseType = baseType;
Name = generatedClassName;
GeneratedNamespace = generatedNamespaceName;
Members = members;
MethodDescription = methodDescription;
Accessibility = accessibility;
SerializationHooks = serializationHooks;
ActivatorConstructorParameters = constructorArguments;
UsesInvokablePool = usesInvokablePool;
CompoundTypeAliases = compoundTypeAliases;
ReturnValueInitializerMethod = returnValueInitializerMethod;
ClassDeclarationSyntax = classDeclarationSyntax;
}
public Accessibility Accessibility { get; }
public TypeSyntax TypeSyntax => field ??= CreateTypeSyntax();
public TypeSyntax OpenTypeSyntax => field ??= CreateOpenTypeSyntax();
public bool HasComplexBaseType => BaseType is { SpecialType: not SpecialType.System_Object };
public bool IncludePrimaryConstructorParameters => false;
public INamedTypeSymbol BaseType { get; }
public TypeSyntax BaseTypeSyntax => field ??= BaseType.ToTypeSyntax(MethodDescription.TypeParameterSubstitutions);
public string Namespace => GeneratedNamespace;
public string GeneratedNamespace { get; }
public string Name { get; }
public bool IsValueType => false;
public bool IsSealedType => true;
public bool IsAbstractType => false;
public bool IsEnumType => false;
public bool IsGenericType => TypeParameters.Count > 0;
public List<IMemberDescription> Members { get; }
public Compilation Compilation => MethodDescription.GenerationContext.Compilation;
public bool IsEmptyConstructable => ActivatorConstructorParameters is not { Count: > 0 };
public bool UseActivator => ActivatorConstructorParameters is { Count: > 0 };
public bool TrackReferences => false;
public bool OmitDefaultMemberValues => false;
public List<(string Name, ITypeParameterSymbol Parameter)> TypeParameters => MethodDescription.AllTypeParameters;
public List<INamedTypeSymbol> SerializationHooks { get; }
public bool IsShallowCopyable => false;
public bool IsUnsealedImmutable => false;
public bool IsImmutable => false;
public bool IsExceptionType => false;
public List<TypeSyntax> ActivatorConstructorParameters { get; }
public bool UsesInvokablePool { get; }
public bool HasActivatorConstructor => UseActivator;
public List<CompoundTypeAliasComponent[]> CompoundTypeAliases { get; }
public ClassDeclarationSyntax ClassDeclarationSyntax { get; }
public string? ReturnValueInitializerMethod { get; }
public InvokableMethodDescription MethodDescription { get; }
public string MetadataName { get; }
public ExpressionSyntax GetObjectCreationExpression() => ObjectCreationExpression(TypeSyntax, ArgumentList(), null);
private TypeSyntax CreateTypeSyntax()
{
var simpleName = InvokableGenerator.GetSimpleClassName(MethodDescription);
var subs = MethodDescription.TypeParameterSubstitutions;
return (TypeParameters, Namespace) switch
{
({ Count: > 0 }, { Length: > 0 }) => QualifiedName(ParseName(Namespace), GenericName(Identifier(simpleName), TypeArgumentList(SeparatedList<TypeSyntax>(TypeParameters.Select(p => IdentifierName(subs[p.Parameter])))))),
({ Count: > 0 }, _) => GenericName(Identifier(simpleName), TypeArgumentList(SeparatedList<TypeSyntax>(TypeParameters.Select(p => IdentifierName(subs[p.Parameter]))))),
(_, { Length: > 0 }) => QualifiedName(ParseName(Namespace), IdentifierName(simpleName)),
_ => IdentifierName(simpleName),
};
}
private TypeSyntax CreateOpenTypeSyntax()
{
var simpleName = InvokableGenerator.GetSimpleClassName(MethodDescription);
return (TypeParameters, Namespace) switch
{
({ Count: > 0 }, { Length: > 0 }) => QualifiedName(ParseName(Namespace), GenericName(Identifier(simpleName), TypeArgumentList(SeparatedList<TypeSyntax>(TypeParameters.Select(p => OmittedTypeArgument()))))),
({ Count: > 0 }, _) => GenericName(Identifier(simpleName), TypeArgumentList(SeparatedList<TypeSyntax>(TypeParameters.Select(p => OmittedTypeArgument())))),
(_, { Length: > 0 }) => QualifiedName(ParseName(Namespace), IdentifierName(simpleName)),
_ => IdentifierName(simpleName),
};
}
}