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

Sholtee / proxygen / 1048

30 Apr 2025 05:58AM UTC coverage: 92.635% (-0.07%) from 92.705%
1048

push

appveyor

Sholtee
visibility check is supposed to be done during initial resolution

4792 of 5173 relevant lines covered (92.63%)

0.93 hits per line

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

96.24
/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, bool allowProtected = false)
113
        {
1✔
114
            //
115
            // Starting from .NET 5.0 interface members may have visibility.
116
            //
117

118
            Visibility.Check(method, ContainingAssembly, allowProtected);
1✔
119

120
            TypeSyntax returnTypeSyntax = ResolveType(method.ReturnValue.Type);
1✔
121

122
            if (method.ReturnValue.Kind >= ParameterKind.Ref)
1✔
123
            {
1✔
124
                RefTypeSyntax refReturnTypeSyntax = RefType(returnTypeSyntax);
1✔
125

126
                if (method.ReturnValue.Kind is ParameterKind.RefReadonly)
1✔
127
                    refReturnTypeSyntax = refReturnTypeSyntax.WithReadOnlyKeyword
1✔
128
                    (
1✔
129
                        Token(SyntaxKind.ReadOnlyKeyword)
1✔
130
                    );
1✔
131

132
                returnTypeSyntax = refReturnTypeSyntax;
1✔
133
            }
1✔
134

135
            return MethodDeclaration
1✔
136
            (
1✔
137
                returnType: returnTypeSyntax,
1✔
138
                identifier: Identifier(method.Name)
1✔
139
            )
1✔
140
            .WithParameterList
1✔
141
            (
1✔
142
                ResolveParameterList(method)
1✔
143
            );
1✔
144
        }
1✔
145

146
        /// <summary>
147
        /// <code>
148
        /// int IInterface.Foo&lt;...&gt;(T a, ref TT b) [where ...]
149
        /// </code>
150
        /// or
151
        /// <code>
152
        /// public override int Foo&lt;...&gt;(T a, ref TT b) [where ...]
153
        /// </code>
154
        /// </summary>
155
        #if DEBUG
156
        internal
157
        #endif
158
        protected MethodDeclarationSyntax ResolveMethod(IMethodInfo method, bool allowProtected = false)
159
        {
1✔
160
            MethodDeclarationSyntax result = ResolveMethodCore(method, allowProtected);
1✔
161

162
            if (method.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
163
            {
1✔
164
                CheckNotStaticAbstract(method);
1✔
165

166
                result = result.WithExplicitInterfaceSpecifier
1✔
167
                (
1✔
168
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
1✔
169
                );
1✔
170
            }
1✔
171
            else
172
            {
1✔
173
                List<SyntaxKind> tokens = [.. ResolveAccessModifiers(method)];
1✔
174

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

177
                result = result.WithModifiers
×
178
                (
×
179
                    TokenList
×
180
                    (
×
181
                        tokens.Select(Token)
×
182
                    )
×
183
                );
×
184
            }
1✔
185

186
            if (method is IGenericMethodInfo genericMethod)
1✔
187
            {
1✔
188
                result = result.WithTypeParameterList
1✔
189
                (
1✔
190
                    typeParameterList: TypeParameterList
1✔
191
                    (
1✔
192
                        parameters: genericMethod
1✔
193
                            .GenericArguments
1✔
194
                            .ToSyntaxList
1✔
195
                            (
1✔
196
                                type => TypeParameter
1✔
197
                                (
1✔
198
                                    ResolveType(type).ToFullString()
1✔
199
                                )
1✔
200
                            )
1✔
201
                    )
1✔
202
                );
1✔
203

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

230
                    IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
231
                    {
1✔
232
                        if (constraint.Struct)
1✔
233
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
1✔
234
                        if (constraint.Reference)
1✔
235
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
1✔
236

237
                        //
238
                        // Explicit interface implementations must not specify type constraints
239
                        //
240

241
                        if (method.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
242
                            yield break;
1✔
243

244
                        if (constraint.DefaultConstructor)
1✔
245
                            yield return ConstructorConstraint();
1✔
246

247
                        foreach (ITypeInfo typeConstraint in constraint.ConstraintTypes)
1✔
248
                        {
1✔
249
                            yield return TypeConstraint
1✔
250
                            (
1✔
251
                                ResolveType(typeConstraint)
1✔
252
                            );
1✔
253
                        }
1✔
254
                    }
1✔
255
                }
1✔
256
            }
1✔
257

258
            return result;
1✔
259
        }
1✔
260

261
        /// <summary>
262
        /// <code>
263
        /// target.Foo(..., ref ..., ...)
264
        /// </code>
265
        /// </summary>
266
        #if DEBUG
267
        internal
268
        #endif
269
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IEnumerable<ArgumentSyntax> arguments)
270
        {
1✔
271
            Debug.Assert(arguments.Count() == method.Parameters.Count);
1✔
272

273
            return InvocationExpression
1✔
274
            (
1✔
275
                expression: MethodAccess
1✔
276
                (
1✔
277
                    target,
1✔
278
                    method,
1✔
279
                    castTargetTo
1✔
280
                )
1✔
281
            )
1✔
282
            .WithArgumentList
1✔
283
            (
1✔
284
                ResolveArgumentList(method, arguments)
1✔
285
            );
1✔
286
        }
1✔
287

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

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

323
        #if DEBUG
324
        internal
325
        #endif
326
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => throw new NotImplementedException();
×
327
    }
328
}
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