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

Sholtee / proxygen / 919

21 Mar 2025 06:02PM UTC coverage: 91.496% (+0.03%) from 91.466%
919

push

appveyor

Sholtee
add method override support for ResolveMethod()

4519 of 4939 relevant lines covered (91.5%)

0.91 hits per line

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

98.65
/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
    using Properties;
20

21
    internal partial class ClassSyntaxFactoryBase
22
    {
23
        /// <summary>
24
        /// <code>
25
        /// [[(Type)] target | [(Type)] this | Namespace.Type].Method&lt;...&gt;(...)
26
        /// </code>
27
        /// </summary>
28
        #if DEBUG
29
        internal
30
        #endif
31
        protected MemberAccessExpressionSyntax MethodAccess(ExpressionSyntax? target, IMethodInfo method, ITypeInfo? castTargetTo = null)
32
        {
1✔
33
            SimpleNameSyntax identifier = IdentifierName(method.Name);
1✔
34
            if (method is IGenericMethodInfo genericMethod)
1✔
35
                identifier = GenericName(identifier.Identifier).WithTypeArgumentList
1✔
36
                (
1✔
37
                    typeArgumentList: TypeArgumentList
1✔
38
                    (
1✔
39
                        arguments: genericMethod.GenericArguments.ToSyntaxList(ResolveType)
1✔
40
                    )
1✔
41
                );
1✔
42

43
            return SimpleMemberAccess
1✔
44
            (
1✔
45
                AmendTarget(target, method, castTargetTo),
1✔
46
                identifier
1✔
47
            );
1✔
48
        }
1✔
49

50
        /// <summary>
51
        /// <code>
52
        /// int IInterface.Foo&lt;...&gt;(T a, ref TT b) [where ...]
53
        /// </code>
54
        /// or
55
        /// <code>
56
        /// public override int Foo&lt;...&gt;(T a, ref TT b) [where ...]
57
        /// </code>
58
        /// </summary>
59
        #if DEBUG
60
        internal
61
        #endif
62
        protected MethodDeclarationSyntax ResolveMethod(IMethodInfo method, bool forceInlining = false)
63
        {
1✔
64
            TypeSyntax returnTypeSyntax = ResolveType(method.ReturnValue.Type);
1✔
65

66
            if (method.ReturnValue.Kind >= ParameterKind.Ref)
1✔
67
            {
1✔
68
                RefTypeSyntax refReturnTypeSyntax = RefType(returnTypeSyntax);
1✔
69

70
                if (method.ReturnValue.Kind is ParameterKind.RefReadonly)
1✔
71
                    refReturnTypeSyntax = refReturnTypeSyntax.WithReadOnlyKeyword
1✔
72
                    (
1✔
73
                        Token(SyntaxKind.ReadOnlyKeyword)
1✔
74
                    );
1✔
75

76
                returnTypeSyntax = refReturnTypeSyntax;
1✔
77
            }
1✔
78

79
            MethodDeclarationSyntax result = MethodDeclaration
1✔
80
            (
1✔
81
                returnType: returnTypeSyntax,
1✔
82
                identifier: Identifier(method.Name)
1✔
83
            )
1✔
84
            .WithParameterList
1✔
85
            (
1✔
86
                ParameterList
1✔
87
                (
1✔
88
                    parameters: method.Parameters.ToSyntaxList(param =>
1✔
89
                    {
1✔
90
                        ParameterSyntax parameter = Parameter
1✔
91
                        (
1✔
92
                            Identifier(param.Name)
1✔
93
                        )
1✔
94
                        .WithType
1✔
95
                        (
1✔
96
                            type: ResolveType(param.Type)
1✔
97
                        );
1✔
98

1✔
99
                        SyntaxKind? modifier = param.Kind switch
1✔
100
                        {
1✔
101
                            ParameterKind.In => SyntaxKind.InKeyword,
1✔
102
                            ParameterKind.Out => SyntaxKind.OutKeyword,
1✔
103
                            ParameterKind.Ref => SyntaxKind.RefKeyword,
1✔
104
                            ParameterKind.Params => SyntaxKind.ParamsKeyword,
1✔
105
                            _ => null
1✔
106
                        };
1✔
107

1✔
108
                        if (modifier is not null)
1✔
109
                            parameter = parameter.WithModifiers
1✔
110
                            (
1✔
111
                                TokenList(Token(modifier.Value))
1✔
112
                            );
1✔
113

1✔
114
                        return parameter;
1✔
115
                    })
1✔
116
                )
1✔
117
            );
1✔
118

119
            if (method.DeclaringType.IsInterface)
1✔
120
                result = result.WithExplicitInterfaceSpecifier
1✔
121
                (
1✔
122
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
1✔
123
                );
1✔
124
            else
125
            {
1✔
126
                List<SyntaxKind> tokens = method
1✔
127
                    .AccessModifiers
1✔
128
                    .SetFlags()
1✔
129
                    .Select
1✔
130
                    (
1✔
131
                        am => am switch
1✔
132
                        {
1✔
133
                            AccessModifiers.Public => SyntaxKind.PublicKeyword,
×
134
                            AccessModifiers.Protected => SyntaxKind.ProtectedKeyword,
1✔
135
                            AccessModifiers.Internal => SyntaxKind.InternalKeyword,
1✔
136
                            _ => throw new ArgumentException(string.Format(Resources.METHOD_NOT_VISIBLE, method.Name), nameof(method)),
×
137
                        }
1✔
138
                    )
1✔
139
                    .ToList();
1✔
140

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

143
                result = result.WithModifiers
1✔
144
                (
1✔
145
                    TokenList
1✔
146
                    (
1✔
147
                        tokens.Select(Token)
1✔
148
                    )
1✔
149
                );
1✔
150
            }
1✔
151

152
            if (method is IGenericMethodInfo genericMethod)
1✔
153
            {
1✔
154
                result = result.WithTypeParameterList
1✔
155
                (
1✔
156
                    typeParameterList: TypeParameterList
1✔
157
                    (
1✔
158
                        parameters: genericMethod
1✔
159
                            .GenericArguments
1✔
160
                            .ToSyntaxList
1✔
161
                            (
1✔
162
                                type => TypeParameter
1✔
163
                                (
1✔
164
                                    ResolveType(type).ToFullString()
1✔
165
                                )
1✔
166
                            )
1✔
167
                    )
1✔
168
                );
1✔
169

170
                if (genericMethod.IsGenericDefinition)
1✔
171
                {
1✔
172
                    result = result.WithConstraintClauses
1✔
173
                    (
1✔
174
                        List
1✔
175
                        (
1✔
176
                            genericMethod
1✔
177
                                .GenericConstraints
1✔
178
                                .Where(constraint => GetConstraints(constraint).Any())
1✔
179
                                .Select
1✔
180
                                (
1✔
181
                                    constraint => TypeParameterConstraintClause
1✔
182
                                    (
1✔
183
                                        IdentifierName
1✔
184
                                        (
1✔
185
                                            constraint.Target.Name  // T, T, etc
1✔
186
                                        )
1✔
187
                                    )
1✔
188
                                    .WithConstraints
1✔
189
                                    (
1✔
190
                                        GetConstraints(constraint).ToSyntaxList()
1✔
191
                                    )
1✔
192
                                )
1✔
193
                        )
1✔
194
                    );
1✔
195

196
                    IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
197
                    {
1✔
198
                        if (constraint.Struct)
1✔
199
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
1✔
200
                        if (constraint.Reference)
1✔
201
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
1✔
202

203
                        //
204
                        // Explicit interface implementations must not specify type constraints
205
                        //
206

207
                        if (method.DeclaringType.IsInterface)
1✔
208
                            yield break;
1✔
209

210
                        if (constraint.DefaultConstructor)
1✔
211
                            yield return ConstructorConstraint();
1✔
212

213
                        foreach (ITypeInfo typeConstraint in constraint.ConstraintTypes)
1✔
214
                        {
1✔
215
                            yield return TypeConstraint
1✔
216
                            (
1✔
217
                                ResolveType(typeConstraint)
1✔
218
                            );
1✔
219
                        }
1✔
220
                    }
1✔
221
                }
1✔
222
            }
1✔
223

224
            if (forceInlining) result = result.WithAttributeLists
1✔
225
            (
1✔
226
                attributeLists: ResolveMethodImplAttributeToForceInlining()
1✔
227
            );
1✔
228

229
            return result;
1✔
230
        }
1✔
231

232
        /// <summary>
233
        /// <code>
234
        /// target.Foo(..., ref ..., ...)
235
        /// </code>
236
        /// </summary>
237
        #if DEBUG
238
        internal
239
        #endif
240
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params ArgumentSyntax[] arguments)
241
        {
1✔
242
            IReadOnlyList<IParameterInfo> paramz = method.Parameters;
1✔
243

244
            Debug.Assert(arguments.Length == paramz.Count);
1✔
245

246
            return InvocationExpression
1✔
247
            (
1✔
248
                expression: MethodAccess
1✔
249
                (
1✔
250
                    target,
1✔
251
                    method,
1✔
252
                    castTargetTo
1✔
253
                )
1✔
254
            )
1✔
255
            .WithArgumentList
1✔
256
            (
1✔
257
                argumentList: ArgumentList
1✔
258
                (
1✔
259
                    arguments.ToSyntaxList
1✔
260
                    (
1✔
261
                        (arg, i) => paramz[i].Kind switch
1✔
262
                        {
1✔
263
                            ParameterKind.In => arg.WithRefKindKeyword
1✔
264
                            (
1✔
265
                                refKindKeyword: Token(SyntaxKind.InKeyword)
1✔
266
                            ),
1✔
267
                            ParameterKind.Out => arg.WithRefKindKeyword
1✔
268
                            (
1✔
269
                                refKindKeyword: Token(SyntaxKind.OutKeyword)
1✔
270
                            ),
1✔
271
                            ParameterKind.Ref => arg.WithRefKindKeyword
1✔
272
                            (
1✔
273
                                refKindKeyword: Token(SyntaxKind.RefKeyword)
1✔
274
                            ),
1✔
275
                            _ => arg
1✔
276
                        }
1✔
277
                    )
1✔
278
                )
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 string[] arguments)
291
        {
1✔
292
            IReadOnlyList<IParameterInfo> paramz = method.Parameters;
1✔
293

294
            Debug.Assert(arguments.Length == paramz.Count);
1✔
295

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

318
        #if DEBUG
319
        internal
320
        #endif
321
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => cls;
×
322
    }
323
}
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