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

SamboyCoding / Cpp2IL / 13507960909

24 Feb 2025 09:14PM UTC coverage: 27.229% (-0.02%) from 27.25%
13507960909

push

github

web-flow
Improve documentation and parameter validation in (#415)

1261 of 6456 branches covered (19.53%)

Branch coverage included in aggregate %.

0 of 5 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

3375 of 10570 relevant lines covered (31.93%)

123247.43 hits per line

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

63.89
/Cpp2IL.Core/Model/Contexts/ConcreteGenericMethodAnalysisContext.cs
1
using System;
2
using System.Linq;
3
using System.Reflection;
4
using Cpp2IL.Core.Utils;
5
using LibCpp2IL;
6
using LibCpp2IL.BinaryStructures;
7
using LibCpp2IL.Reflection;
8

9
namespace Cpp2IL.Core.Model.Contexts;
10

11
public class ConcreteGenericMethodAnalysisContext : MethodAnalysisContext
12
{
13
    public readonly AssemblyAnalysisContext DeclaringAsm;
14
    public readonly Cpp2IlMethodRef? MethodRef;
15
    public readonly MethodAnalysisContext BaseMethodContext;
16

17
    /// <summary>
18
    /// The generic parameters for the <see cref="BaseMethodContext"/> declaring type.
19
    /// </summary>
20
    /// <remarks>
21
    /// If not empty, <see cref="MethodAnalysisContext.DeclaringType"/> is a <see cref="GenericInstanceTypeAnalysisContext"/>.
22
    /// </remarks>
UNCOV
23
    public TypeAnalysisContext[] TypeGenericParameters { get; }
×
24

25
    /// <summary>
26
    /// The generic parameters for the <see cref="BaseMethodContext"/>.
27
    /// </summary>
28
    /// <remarks>
29
    /// These may be empty if <see cref="BaseMethodContext"/> has no generic parameters or if <see cref="IsPartialInstantiation"/>.
30
    /// </remarks>
UNCOV
31
    public TypeAnalysisContext[] MethodGenericParameters { get; }
×
32

33
    /// <summary>
34
    /// If true, this is a generic method on a <see cref="GenericInstanceTypeAnalysisContext"/>, but it does not specify any <see cref="MethodGenericParameters"/>.
35
    /// </summary>
NEW
36
    public bool IsPartialInstantiation => MethodGenericParameters.Length == 0 && BaseMethodContext.GenericParameterCount > 0;
×
37

38
    public sealed override ulong UnderlyingPointer => MethodRef?.GenericVariantPtr ?? default;
435,658!
39

40
    public override bool IsStatic => BaseMethodContext.IsStatic;
×
41

42
    public override bool IsVoid => BaseMethodContext.IsVoid;
×
43

44
    public override string DefaultName => BaseMethodContext.DefaultName;
×
45

46
    public override MethodAttributes Attributes => BaseMethodContext.Attributes;
×
47

48
    public override AssemblyAnalysisContext CustomAttributeAssembly => BaseMethodContext.CustomAttributeAssembly;
×
49

50
    public ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, ApplicationAnalysisContext context)
51
        : this(methodRef, ResolveDeclaringAssembly(methodRef, context))
145,386✔
52
    {
53
    }
145,386✔
54

55
    private ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, AssemblyAnalysisContext declaringAssembly)
56
        : this(
145,386✔
57
              methodRef,
145,386✔
58
              ResolveBaseMethod(methodRef, declaringAssembly.GetTypeByDefinition(methodRef.DeclaringType)!),
145,386✔
59
              ResolveDeclaringType(methodRef, declaringAssembly),
145,386✔
60
              ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly),
145,386✔
61
              ResolveTypeArray(methodRef.MethodGenericParams, declaringAssembly),
145,386✔
62
              declaringAssembly)
145,386✔
63
    {
64
    }
145,386✔
65

66
    /// <summary>
67
    /// Generically instantiate a method.
68
    /// </summary>
69
    /// <param name="baseMethod">The method definition on which this instantiation is based.</param>
70
    /// <param name="typeGenericParameters">The type parameters for the declaring type, if any. These must always be specified.</param>
71
    /// <param name="methodGenericParameters">
72
    /// The type parameters for the base method, if any.
73
    /// These may be omitted (<see cref="IsPartialInstantiation"/> == <see langword="true"/>).
74
    /// </param>
75
    public ConcreteGenericMethodAnalysisContext(MethodAnalysisContext baseMethod, TypeAnalysisContext[] typeGenericParameters, TypeAnalysisContext[] methodGenericParameters)
76
        : this(
×
77
              null,
×
78
              baseMethod,
×
79
              typeGenericParameters.Length > 0 ? baseMethod.DeclaringType!.MakeGenericInstanceType(typeGenericParameters) : baseMethod.DeclaringType!,
×
80
              typeGenericParameters,
×
81
              methodGenericParameters,
×
82
              baseMethod.CustomAttributeAssembly)
×
83
    {
NEW
84
        if (baseMethod.DeclaringType!.GenericParameterCount != typeGenericParameters.Length)
×
NEW
85
            throw new ArgumentException("The number of type generic parameters must match the number of generic parameters on the declaring type.");
×
86

NEW
87
        if (methodGenericParameters.Length > 0 && baseMethod.GenericParameterCount != methodGenericParameters.Length)
×
NEW
88
            throw new ArgumentException("The number of method generic parameters must match the number of generic parameters on the base method.");
×
UNCOV
89
    }
×
90

91
    private ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef? methodRef, MethodAnalysisContext baseMethodContext, TypeAnalysisContext declaringType, TypeAnalysisContext[] typeGenericParameters, TypeAnalysisContext[] methodGenericParameters, AssemblyAnalysisContext declaringAssembly)
92
        : base(null, declaringType)
145,386✔
93
    {
94
        MethodRef = methodRef;
145,386✔
95
        DeclaringAsm = declaringAssembly;
145,386✔
96
        BaseMethodContext = baseMethodContext;
145,386✔
97

98
        TypeGenericParameters = typeGenericParameters;
145,386✔
99
        MethodGenericParameters = methodGenericParameters;
145,386✔
100

101
        // For the purpose of generic instantiation, we need an array of method generic parameters, even if none are provided.
102
        if (methodGenericParameters.Length == 0 && baseMethodContext.GenericParameterCount > 0)
145,386!
103
            methodGenericParameters = Enumerable.Range(0, baseMethodContext.GenericParameterCount)
×
104
                .Select(i => new GenericParameterTypeAnalysisContext("T", i, Il2CppTypeEnum.IL2CPP_TYPE_MVAR, declaringAssembly))
×
105
                .ToArray();
×
106

107
        for (var i = 0; i < BaseMethodContext.Parameters.Count; i++)
598,748✔
108
        {
109
            var parameter = BaseMethodContext.Parameters[i];
153,988✔
110
            var parameterType = parameter.ParameterTypeContext;
153,988✔
111
            var instantiatedType = GenericInstantiation.Instantiate(
153,988✔
112
                parameter.ParameterTypeContext,
153,988✔
113
                typeGenericParameters,
153,988✔
114
                methodGenericParameters);
153,988✔
115

116
            Parameters.Add(parameterType == instantiatedType
153,988✔
117
                ? parameter
153,988✔
118
                : new InjectedParameterAnalysisContext(parameter.Name, instantiatedType, i, BaseMethodContext));
153,988✔
119
        }
120

121
        InjectedReturnType = GenericInstantiation.Instantiate(BaseMethodContext.ReturnTypeContext, typeGenericParameters, methodGenericParameters);
145,386✔
122

123
        if (UnderlyingPointer != 0)
145,386✔
124
            rawMethodBody = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
144,886✔
125
    }
145,386✔
126

127
    private static AssemblyAnalysisContext ResolveDeclaringAssembly(Cpp2IlMethodRef methodRef, ApplicationAnalysisContext context)
128
    {
129
        return context.GetAssemblyByName(methodRef.DeclaringType.DeclaringAssembly!.Name!)
145,386!
130
               ?? throw new($"Unable to resolve declaring assembly {methodRef.DeclaringType.DeclaringAssembly.Name} for generic method {methodRef}");
145,386✔
131
    }
132

133
    private static TypeAnalysisContext ResolveDeclaringType(Cpp2IlMethodRef methodRef, AssemblyAnalysisContext declaringAssembly)
134
    {
135
        var baseType = declaringAssembly.AppContext.ResolveContextForType(methodRef.DeclaringType)
145,386!
136
                       ?? throw new($"Unable to resolve declaring type {methodRef.DeclaringType.FullName} for generic method {methodRef}");
145,386✔
137

138
        if (methodRef.TypeGenericParams.Length == 0)
145,386✔
139
            return baseType;
21,386✔
140

141
        var genericParams = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);
124,000✔
142

143
        return new GenericInstanceTypeAnalysisContext(baseType, genericParams, declaringAssembly);
124,000✔
144
    }
145

146
    private static TypeAnalysisContext[] ResolveTypeArray(Il2CppTypeReflectionData[] array, AssemblyAnalysisContext declaringAssembly)
147
    {
148
        if (array.Length == 0)
414,772✔
149
            return [];
143,490✔
150

151
        var ret = new TypeAnalysisContext[array.Length];
271,282✔
152
        for (var i = 0; i < array.Length; i++)
1,245,356✔
153
        {
154
            ret[i] = array[i].ToContext(declaringAssembly)
351,396!
155
                     ?? throw new($"Unable to resolve generic parameter {array[i]} for generic method.");
351,396✔
156
        }
157

158
        return ret;
271,282✔
159
    }
160

161
    private static MethodAnalysisContext ResolveBaseMethod(Cpp2IlMethodRef methodRef, TypeAnalysisContext declaringType)
162
    {
163
        return declaringType.GetMethod(methodRef.BaseMethod)
145,386!
164
               ?? throw new($"Unable to resolve base method {methodRef.BaseMethod} for generic method {methodRef}");
145,386✔
165
    }
166
}
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