• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / proxygen / 1046

27 Apr 2025 01:21PM UTC coverage: 92.705% (+0.6%) from 92.112%
1046

push

appveyor

Sholtee
Merge branch 'v10-preview1'

4791 of 5168 relevant lines covered (92.71%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Method.cs
1
/********************************************************************************
2
* ClassSyntaxFactoryBase.Method.cs                                              *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.Generic;
8
using System.Diagnostics;
9
using System.Linq;
10

11
using Microsoft.CodeAnalysis;
12
using Microsoft.CodeAnalysis.CSharp;
13
using Microsoft.CodeAnalysis.CSharp.Syntax;
14

15
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
16

17
namespace Solti.Utils.Proxy.Internals
18
{
19
    internal partial class ClassSyntaxFactoryBase
20
    {
21
        private static ArgumentListSyntax ResolveArgumentList(IMethodInfo method, IEnumerable<ArgumentSyntax> arguments) => ArgumentList
1✔
22
        (
1✔
23
            arguments.ToSyntaxList
1✔
24
            (
1✔
25
                (arg, i) => method.Parameters[i].Kind switch
1✔
26
                {
1✔
27
                    ParameterKind.In => arg.WithRefKindKeyword
1✔
28
                    (
1✔
29
                        refKindKeyword: Token(SyntaxKind.InKeyword)
1✔
30
                    ),
1✔
31
                    ParameterKind.Out => arg.WithRefKindKeyword
1✔
32
                    (
1✔
33
                        refKindKeyword: Token(SyntaxKind.OutKeyword)
1✔
34
                    ),
1✔
35
                    ParameterKind.Ref => arg.WithRefKindKeyword
1✔
36
                    (
1✔
37
                        refKindKeyword: Token(SyntaxKind.RefKeyword)
1✔
38
                    ),
1✔
39
                    _ => arg
1✔
40
                }
1✔
41
            )
1✔
42
        );
1✔
43

44
        private ParameterListSyntax ResolveParameterList(IMethodInfo method) => ParameterList
1✔
45
        (
1✔
46
            method.Parameters.ToSyntaxList(param =>
1✔
47
            {
1✔
48
                ParameterSyntax parameter = Parameter
1✔
49
                (
1✔
50
                    Identifier(param.Name)
1✔
51
                )
1✔
52
                .WithType
1✔
53
                (
1✔
54
                    type: ResolveType(param.Type)
1✔
55
                );
1✔
56

1✔
57
                SyntaxKind? modifier = param.Kind switch
1✔
58
                {
1✔
59
                    ParameterKind.In => SyntaxKind.InKeyword,
1✔
60
                    ParameterKind.Out => SyntaxKind.OutKeyword,
1✔
61
                    ParameterKind.Ref => SyntaxKind.RefKeyword,
1✔
62
                    ParameterKind.Params => SyntaxKind.ParamsKeyword,
1✔
63
                    _ => null
1✔
64
                };
1✔
65

1✔
66
                return modifier is null ? parameter : parameter.WithModifiers
1✔
67
                (
1✔
68
                    TokenList
1✔
69
                    (
1✔
70
                        Token(modifier.Value)
1✔
71
                    )
1✔
72
                );
1✔
73
            })
1✔
74
        );
1✔
75

76
        /// <summary>
77
        /// <code>
78
        /// [[(Type)] target | [(Type)] this | Namespace.Type].Method&lt;...&gt;(...)
79
        /// </code>
80
        /// </summary>
81
        #if DEBUG
82
        internal
83
        #endif
84
        protected MemberAccessExpressionSyntax MethodAccess(ExpressionSyntax? target, IMethodInfo method, ITypeInfo? castTargetTo = null)
85
        {
1✔
86
            SimpleNameSyntax identifier = IdentifierName(method.Name);
1✔
87
            if (method is IGenericMethodInfo genericMethod)
1✔
88
                identifier = GenericName(identifier.Identifier).WithTypeArgumentList
1✔
89
                (
1✔
90
                    typeArgumentList: TypeArgumentList
1✔
91
                    (
1✔
92
                        arguments: genericMethod.GenericArguments.ToSyntaxList(ResolveType)
1✔
93
                    )
1✔
94
                );
1✔
95

96
            return SimpleMemberAccess
1✔
97
            (
1✔
98
                AmendTarget(target, method, castTargetTo),
1✔
99
                identifier
1✔
100
            );
1✔
101
        }
1✔
102

103

104
        /// <summary>
105
        /// <code>
106
        /// int Foo(T a, ref TT b)
107
        /// </code>
108
        /// </summary>
109
        #if DEBUG
110
        internal
111
        #endif
112
        protected MethodDeclarationSyntax ResolveMethodCore(IMethodInfo method)
113
        {
1✔
114
            TypeSyntax returnTypeSyntax = ResolveType(method.ReturnValue.Type);
1✔
115

116
            if (method.ReturnValue.Kind >= ParameterKind.Ref)
1✔
117
            {
1✔
118
                RefTypeSyntax refReturnTypeSyntax = RefType(returnTypeSyntax);
1✔
119

120
                if (method.ReturnValue.Kind is ParameterKind.RefReadonly)
1✔
121
                    refReturnTypeSyntax = refReturnTypeSyntax.WithReadOnlyKeyword
1✔
122
                    (
1✔
123
                        Token(SyntaxKind.ReadOnlyKeyword)
1✔
124
                    );
1✔
125

126
                returnTypeSyntax = refReturnTypeSyntax;
1✔
127
            }
1✔
128

129
            return MethodDeclaration
1✔
130
            (
1✔
131
                returnType: returnTypeSyntax,
1✔
132
                identifier: Identifier(method.Name)
1✔
133
            )
1✔
134
            .WithParameterList
1✔
135
            (
1✔
136
                ResolveParameterList(method)
1✔
137
            );
1✔
138
        }
1✔
139

140
        /// <summary>
141
        /// <code>
142
        /// int IInterface.Foo&lt;...&gt;(T a, ref TT b) [where ...]
143
        /// </code>
144
        /// or
145
        /// <code>
146
        /// public override int Foo&lt;...&gt;(T a, ref TT b) [where ...]
147
        /// </code>
148
        /// </summary>
149
        #if DEBUG
150
        internal
151
        #endif
152
        protected MethodDeclarationSyntax ResolveMethod(IMethodInfo method)
153
        {
1✔
154
            MethodDeclarationSyntax result = ResolveMethodCore(method);
1✔
155

156
            if (method.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
157
            {
1✔
158
                CheckNotStaticAbstract(method);
1✔
159

160
                result = result.WithExplicitInterfaceSpecifier
1✔
161
                (
1✔
162
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
1✔
163
                );
1✔
164
            }
1✔
165
            else
166
            {
1✔
167
                List<SyntaxKind> tokens = [.. ResolveAccessModifiers(method)];
1✔
168

169
                tokens.Add(method.IsVirtual || method.IsAbstract ? SyntaxKind.OverrideKeyword : SyntaxKind.NewKeyword);
1✔
170

171
                result = result.WithModifiers
×
172
                (
×
173
                    TokenList
×
174
                    (
×
175
                        tokens.Select(Token)
×
176
                    )
×
177
                );
×
178
            }
1✔
179

180
            if (method is IGenericMethodInfo genericMethod)
1✔
181
            {
1✔
182
                result = result.WithTypeParameterList
1✔
183
                (
1✔
184
                    typeParameterList: TypeParameterList
1✔
185
                    (
1✔
186
                        parameters: genericMethod
1✔
187
                            .GenericArguments
1✔
188
                            .ToSyntaxList
1✔
189
                            (
1✔
190
                                type => TypeParameter
1✔
191
                                (
1✔
192
                                    ResolveType(type).ToFullString()
1✔
193
                                )
1✔
194
                            )
1✔
195
                    )
1✔
196
                );
1✔
197

198
                if (genericMethod.IsGenericDefinition)
1✔
199
                {
1✔
200
                    result = result.WithConstraintClauses
1✔
201
                    (
1✔
202
                        List
1✔
203
                        (
1✔
204
                            genericMethod
1✔
205
                                .GenericConstraints
1✔
206
                                .Where(constraint => GetConstraints(constraint).Any())
1✔
207
                                .Select
1✔
208
                                (
1✔
209
                                    constraint => TypeParameterConstraintClause
1✔
210
                                    (
1✔
211
                                        IdentifierName
1✔
212
                                        (
1✔
213
                                            constraint.Target.Name  // T, T, etc
1✔
214
                                        )
1✔
215
                                    )
1✔
216
                                    .WithConstraints
1✔
217
                                    (
1✔
218
                                        GetConstraints(constraint).ToSyntaxList()
1✔
219
                                    )
1✔
220
                                )
1✔
221
                        )
1✔
222
                    );
1✔
223

224
                    IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
225
                    {
1✔
226
                        if (constraint.Struct)
1✔
227
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
1✔
228
                        if (constraint.Reference)
1✔
229
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
1✔
230

231
                        //
232
                        // Explicit interface implementations must not specify type constraints
233
                        //
234

235
                        if (method.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
236
                            yield break;
1✔
237

238
                        if (constraint.DefaultConstructor)
1✔
239
                            yield return ConstructorConstraint();
1✔
240

241
                        foreach (ITypeInfo typeConstraint in constraint.ConstraintTypes)
1✔
242
                        {
1✔
243
                            yield return TypeConstraint
1✔
244
                            (
1✔
245
                                ResolveType(typeConstraint)
1✔
246
                            );
1✔
247
                        }
1✔
248
                    }
1✔
249
                }
1✔
250
            }
1✔
251

252
            return result;
1✔
253
        }
1✔
254

255
        /// <summary>
256
        /// <code>
257
        /// target.Foo(..., ref ..., ...)
258
        /// </code>
259
        /// </summary>
260
        #if DEBUG
261
        internal
262
        #endif
263
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IEnumerable<ArgumentSyntax> arguments)
264
        {
1✔
265
            Debug.Assert(arguments.Count() == method.Parameters.Count);
1✔
266

267
            return InvocationExpression
1✔
268
            (
1✔
269
                expression: MethodAccess
1✔
270
                (
1✔
271
                    target,
1✔
272
                    method,
1✔
273
                    castTargetTo
1✔
274
                )
1✔
275
            )
1✔
276
            .WithArgumentList
1✔
277
            (
1✔
278
                ResolveArgumentList(method, arguments)
1✔
279
            );
1✔
280
        }
1✔
281

282
        /// <summary>
283
        /// <code>
284
        /// target.Foo(ref a, b, c)
285
        /// </code>
286
        /// </summary>
287
        #if DEBUG
288
        internal
289
        #endif
290
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IReadOnlyList<string> arguments)
291
        {
1✔
292
            Debug.Assert(arguments.Count == method.Parameters.Count);
1✔
293

294
            return InvokeMethod
1✔
295
            (
1✔
296
                method,
1✔
297
                target,
1✔
298
                castTargetTo,
1✔
299
                arguments: method
1✔
300
                    .Parameters
1✔
301
                    .Select
1✔
302
                    (
1✔
303
                        (param, i) => Argument
1✔
304
                        (
1✔
305
                            expression: IdentifierName(arguments[i])
1✔
306
                        )
1✔
307
                    )
1✔
308
                    .ToArray()
1✔
309
            );
1✔
310
        }
1✔
311
    
312
        #if DEBUG
313
        internal
314
        #endif
315
        protected virtual ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax cls, object context) => cls;
1✔
316

317
        #if DEBUG
318
        internal
319
        #endif
320
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => throw new NotImplementedException();
×
321
    }
322
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc