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

Sholtee / proxygen / 976

08 Apr 2025 02:56PM UTC coverage: 91.686% (+1.6%) from 90.091%
976

push

appveyor

Sholtee
introduce PlatformAssemblies class, drop TargetFramework settings

4819 of 5256 relevant lines covered (91.69%)

0.92 hits per line

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

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

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

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

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

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

74
                returnTypeSyntax = refReturnTypeSyntax;
1✔
75
            }
1✔
76

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

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

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

1✔
112
                        return parameter;
1✔
113
                    })
1✔
114
                )
1✔
115
            );
1✔
116

117
            if (method.DeclaringType.IsInterface)
1✔
118
                result = result.WithExplicitInterfaceSpecifier
1✔
119
                (
1✔
120
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
1✔
121
                );
1✔
122
            else
123
            {
1✔
124
                List<SyntaxKind> tokens = AccessModifiersToSyntaxList(method.AccessModifiers).ToList();
1✔
125

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

128
                result = result.WithModifiers
×
129
                (
×
130
                    TokenList
×
131
                    (
×
132
                        tokens.Select(Token)
×
133
                    )
×
134
                );
×
135
            }
1✔
136

137
            if (method is IGenericMethodInfo genericMethod)
1✔
138
            {
1✔
139
                result = result.WithTypeParameterList
1✔
140
                (
1✔
141
                    typeParameterList: TypeParameterList
1✔
142
                    (
1✔
143
                        parameters: genericMethod
1✔
144
                            .GenericArguments
1✔
145
                            .ToSyntaxList
1✔
146
                            (
1✔
147
                                type => TypeParameter
1✔
148
                                (
1✔
149
                                    ResolveType(type).ToFullString()
1✔
150
                                )
1✔
151
                            )
1✔
152
                    )
1✔
153
                );
1✔
154

155
                if (genericMethod.IsGenericDefinition)
1✔
156
                {
1✔
157
                    result = result.WithConstraintClauses
1✔
158
                    (
1✔
159
                        List
1✔
160
                        (
1✔
161
                            genericMethod
1✔
162
                                .GenericConstraints
1✔
163
                                .Where(constraint => GetConstraints(constraint).Any())
1✔
164
                                .Select
1✔
165
                                (
1✔
166
                                    constraint => TypeParameterConstraintClause
1✔
167
                                    (
1✔
168
                                        IdentifierName
1✔
169
                                        (
1✔
170
                                            constraint.Target.Name  // T, T, etc
1✔
171
                                        )
1✔
172
                                    )
1✔
173
                                    .WithConstraints
1✔
174
                                    (
1✔
175
                                        GetConstraints(constraint).ToSyntaxList()
1✔
176
                                    )
1✔
177
                                )
1✔
178
                        )
1✔
179
                    );
1✔
180

181
                    IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
182
                    {
1✔
183
                        if (constraint.Struct)
1✔
184
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
1✔
185
                        if (constraint.Reference)
1✔
186
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
1✔
187

188
                        //
189
                        // Explicit interface implementations must not specify type constraints
190
                        //
191

192
                        if (method.DeclaringType.IsInterface)
1✔
193
                            yield break;
1✔
194

195
                        if (constraint.DefaultConstructor)
1✔
196
                            yield return ConstructorConstraint();
1✔
197

198
                        foreach (ITypeInfo typeConstraint in constraint.ConstraintTypes)
1✔
199
                        {
1✔
200
                            yield return TypeConstraint
1✔
201
                            (
1✔
202
                                ResolveType(typeConstraint)
1✔
203
                            );
1✔
204
                        }
1✔
205
                    }
1✔
206
                }
1✔
207
            }
1✔
208

209
            return result;
1✔
210
        }
1✔
211

212
        /// <summary>
213
        /// <code>
214
        /// target.Foo(..., ref ..., ...)
215
        /// </code>
216
        /// </summary>
217
        #if DEBUG
218
        internal
219
        #endif
220
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IEnumerable<ArgumentSyntax> arguments)
221
        {
1✔
222
            Debug.Assert(arguments.Count() == method.Parameters.Count);
1✔
223

224
            return InvocationExpression
1✔
225
            (
1✔
226
                expression: MethodAccess
1✔
227
                (
1✔
228
                    target,
1✔
229
                    method,
1✔
230
                    castTargetTo
1✔
231
                )
1✔
232
            )
1✔
233
            .WithArgumentList
1✔
234
            (
1✔
235
                argumentList: ArgumentList
1✔
236
                (
1✔
237
                    arguments.ToSyntaxList
1✔
238
                    (
1✔
239
                        (arg, i) => method.Parameters[i].Kind switch
1✔
240
                        {
1✔
241
                            ParameterKind.In => arg.WithRefKindKeyword
1✔
242
                            (
1✔
243
                                refKindKeyword: Token(SyntaxKind.InKeyword)
1✔
244
                            ),
1✔
245
                            ParameterKind.Out => arg.WithRefKindKeyword
1✔
246
                            (
1✔
247
                                refKindKeyword: Token(SyntaxKind.OutKeyword)
1✔
248
                            ),
1✔
249
                            ParameterKind.Ref => arg.WithRefKindKeyword
1✔
250
                            (
1✔
251
                                refKindKeyword: Token(SyntaxKind.RefKeyword)
1✔
252
                            ),
1✔
253
                            _ => arg
1✔
254
                        }
1✔
255
                    )
1✔
256
                )
1✔
257
            );
1✔
258
        }
1✔
259

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

272
            return InvokeMethod
1✔
273
            (
1✔
274
                method,
1✔
275
                target,
1✔
276
                castTargetTo,
1✔
277
                arguments: method.Parameters
1✔
278
                    .Select
1✔
279
                    (
1✔
280
                        (param, i) => Argument
1✔
281
                        (
1✔
282
                            expression: IdentifierName(arguments[i])
1✔
283
                        )
1✔
284
                    )
1✔
285
                    .ToArray()
1✔
286
            );
1✔
287
        }
1✔
288
    
289
        #if DEBUG
290
        internal
291
        #endif
292
        protected virtual ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax cls, object context) => cls;
1✔
293

294
        #if DEBUG
295
        internal
296
        #endif
297
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => cls;
×
298
    }
299
}
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