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

Sholtee / proxygen / 868

01 Nov 2023 05:34AM UTC coverage: 95.021% (+0.1%) from 94.922%
868

push

appveyor

Sholtee
translate some comments, minor refactor

2500 of 2631 relevant lines covered (95.02%)

5.61 hits per line

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

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

10
using Microsoft.CodeAnalysis.CSharp;
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
12

13
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
14

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

39
            return SimpleMemberAccess
6✔
40
            (
41
                AmendTarget(target, method, castTargetTo),
42
                identifier
43
            );
44
        }
6✔
45

46
        /// <summary>
47
        /// <code>
48
        /// int IInterface.Foo&lt;...&gt;(T a, ref TT b) [where ...]
49
        /// </code>
50
        /// </summary>
51
        #if DEBUG
52
        internal
53
        #endif
54
        protected MethodDeclarationSyntax ResolveMethod(IMethodInfo method, bool forceInlining = false)
55
        {
6✔
56
            TypeSyntax returnTypeSytax = ResolveType(method.ReturnValue.Type);
6✔
57

58
            if (method.ReturnValue.Kind >= ParameterKind.Ref)
6✔
59
            {
6✔
60
                RefTypeSyntax refReturnTypeSyntax = RefType(returnTypeSytax);
6✔
61

62
                if (method.ReturnValue.Kind is ParameterKind.RefReadonly)
6✔
63
                    refReturnTypeSyntax = refReturnTypeSyntax.WithReadOnlyKeyword
6✔
64
                    (
65
                        Token(SyntaxKind.ReadOnlyKeyword)
66
                    );
67

68
                returnTypeSytax = refReturnTypeSyntax;
6✔
69
            }
6✔
70

71
            MethodDeclarationSyntax result = MethodDeclaration
6✔
72
            (
73
                returnType: returnTypeSytax,
74
                identifier: Identifier(method.Name)
75
            )
76
            .WithParameterList
77
            (
78
                ParameterList
79
                (
80
                    parameters: method.Parameters.ToSyntaxList(param =>
81
                    {
6✔
82
                        ParameterSyntax parameter = Parameter
6✔
83
                        (
84
                            Identifier(param.Name)
85
                        )
86
                        .WithType
87
                        (
88
                            type: ResolveType(param.Type)
89
                        );
90

91
                        SyntaxKind? modifier = param.Kind switch
6✔
92
                        {
93
                            ParameterKind.In => SyntaxKind.InKeyword,
6✔
94
                            ParameterKind.Out => SyntaxKind.OutKeyword,
6✔
95
                            ParameterKind.Ref => SyntaxKind.RefKeyword,
6✔
96
                            ParameterKind.Params => SyntaxKind.ParamsKeyword,
6✔
97
                            _ => null
6✔
98
                        };
99

100
                        if (modifier is not null)
6✔
101
                            parameter = parameter.WithModifiers
6✔
102
                            (
103
                                TokenList(Token(modifier.Value))
104
                            );
105

106
                        return parameter;
6✔
107
                    })
6✔
108
                )
109
            )
110
            .WithExplicitInterfaceSpecifier
111
            (
112
                explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
113
            );
114

115
            if (method is IGenericMethodInfo genericMethod)
6✔
116
            {
6✔
117
                result = result.WithTypeParameterList
6✔
118
                (
119
                    typeParameterList: TypeParameterList
120
                    (
121
                        parameters: genericMethod
122
                            .GenericArguments
123
                            .ToSyntaxList
124
                            (
125
                                type => TypeParameter
6✔
126
                                (
127
                                    ResolveType(type).ToFullString()
128
                                )
129
                            )
130
                    )
131
                );
132

133
                if (genericMethod.IsGenericDefinition)
6✔
134
                {
6✔
135
                    result = result.WithConstraintClauses
6✔
136
                    (
137
                        List
138
                        (
139
                            genericMethod
140
                                .GenericConstraints
141
                                .Where(static constraint => GetConstraints(constraint).Any())
6✔
142
                                .Select
143
                                (
144
                                    constraint => TypeParameterConstraintClause
6✔
145
                                    (
146
                                        IdentifierName
147
                                        (
148
                                            constraint.Target.Name  // T, T, etc
149
                                        )
150
                                    )
151
                                    .WithConstraints
152
                                    (
153
                                        GetConstraints(constraint).ToSyntaxList()
154
                                    )
155
                                )
156
                        )
157
                    );
158

159
                    static IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
160
                    {
6✔
161
                        //
162
                        // Explicit interface implementations must not specify type constraints
163
                        //
164

165
                        if (constraint.Struct)
6✔
166
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
6✔
167
                        if (constraint.Reference)
6✔
168
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
6✔
169
                    }
6✔
170
                }
6✔
171
            }
6✔
172

173
            if (forceInlining) result = result.WithAttributeLists
6✔
174
            (
175
                attributeLists: ResolveMethodImplAttributeToForceInlining()
176
            );
177

178
            return result;
6✔
179
        }
6✔
180

181
        /// <summary>
182
        /// <code>
183
        /// target.Foo(..., ref ..., ...)
184
        /// </code>
185
        /// </summary>
186
        #if DEBUG
187
        internal
188
        #endif
189
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target, ITypeInfo? castTargetTo = null, params ArgumentSyntax[] arguments)
190
        {
6✔
191
            IReadOnlyList<IParameterInfo> paramz = method.Parameters;
6✔
192

193
            Debug.Assert(arguments.Length == paramz.Count);
6✔
194

195
            return InvocationExpression
6✔
196
            (
197
                expression: MethodAccess
198
                (
199
                    target,
200
                    method,
201
                    castTargetTo
202
                )
203
            )
204
            .WithArgumentList
205
            (
206
                argumentList: ArgumentList
207
                (
208
                    arguments.ToSyntaxList
209
                    (
210
                        (arg, i) => paramz[i].Kind switch
6✔
211
                        {
212
                            ParameterKind.In => arg.WithRefKindKeyword
6✔
213
                            (
214
                                refKindKeyword: Token(SyntaxKind.InKeyword)
215
                            ),
216
                            ParameterKind.Out => arg.WithRefKindKeyword
6✔
217
                            (
218
                                refKindKeyword: Token(SyntaxKind.OutKeyword)
219
                            ),
220
                            ParameterKind.Ref => arg.WithRefKindKeyword
6✔
221
                            (
222
                                refKindKeyword: Token(SyntaxKind.RefKeyword)
223
                            ),
224
                            _ => arg
6✔
225
                        }
226
                    )
227
                )
228
            );
229
        }
6✔
230

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

243
            Debug.Assert(arguments.Length == paramz.Count);
6✔
244

245
            return InvokeMethod
6✔
246
            (
247
                method,
248
                target,
249
                castTargetTo,
250
                arguments: paramz
251
                    .Select
252
                    (
253
                        (param, i) => Argument
6✔
254
                        (
255
                            expression: IdentifierName(arguments[i])
256
                        )
257
                    )
258
                    .ToArray()
259
            );
260
        }
6✔
261
    
262
        #if DEBUG
263
        internal
264
        #endif
265
        protected virtual ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax cls, object context) => cls;
6✔
266

267
        #if DEBUG
268
        internal
269
        #endif
270
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => cls;
×
271
    }
272
}
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