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

SamboyCoding / Cpp2IL / 14150414882

29 Mar 2025 11:29PM UTC coverage: 34.736% (+0.004%) from 34.732%
14150414882

push

github

web-flow
Make HasCustomAttributesAndName::OverrideName virtual (#435)

1791 of 6458 branches covered (27.73%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

4146 of 10634 relevant lines covered (38.99%)

155495.41 hits per line

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

83.49
/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>
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>
31
    public TypeAnalysisContext[] MethodGenericParameters { get; }
325✔
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>
36
    public bool IsPartialInstantiation => MethodGenericParameters.Length == 0 && BaseMethodContext.GenericParameterCount > 0;
×
37

38
    public sealed override ulong UnderlyingPointer => MethodRef?.GenericVariantPtr ?? default;
644,263✔
39

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

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

44
    public override string DefaultName => BaseMethodContext.DefaultName;
325✔
45

46
    public override string? OverrideName { get => BaseMethodContext.OverrideName; set => BaseMethodContext.OverrideName = value; }
325✔
47

UNCOV
48
    public override MethodAttributes Attributes => BaseMethodContext.Attributes;
×
49

50
    public override AssemblyAnalysisContext CustomAttributeAssembly => BaseMethodContext.CustomAttributeAssembly;
×
51

52
    public ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, ApplicationAnalysisContext context)
53
        : this(methodRef, ResolveDeclaringAssembly(methodRef, context))
214,896✔
54
    {
55
    }
214,896✔
56

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

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

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

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

100
        TypeGenericParameters = typeGenericParameters;
215,221✔
101
        MethodGenericParameters = methodGenericParameters;
215,221✔
102

103
        // For the purpose of generic instantiation, we need an array of method generic parameters, even if none are provided.
104
        if (methodGenericParameters.Length == 0 && baseMethodContext.GenericParameterCount > 0)
215,221✔
105
            methodGenericParameters = Enumerable.Range(0, baseMethodContext.GenericParameterCount)
5✔
106
                .Select(i => new GenericParameterTypeAnalysisContext("T", i, Il2CppTypeEnum.IL2CPP_TYPE_MVAR, declaringAssembly))
5✔
107
                .ToArray();
5✔
108

109
        for (var i = 0; i < BaseMethodContext.Parameters.Count; i++)
885,528✔
110
        {
111
            var parameter = BaseMethodContext.Parameters[i];
227,543✔
112
            var parameterType = parameter.ParameterTypeContext;
227,543✔
113
            var instantiatedType = GenericInstantiation.Instantiate(
227,543✔
114
                parameter.ParameterTypeContext,
227,543✔
115
                typeGenericParameters,
227,543✔
116
                methodGenericParameters);
227,543✔
117

118
            Parameters.Add(parameterType == instantiatedType
227,543✔
119
                ? parameter
227,543✔
120
                : new InjectedParameterAnalysisContext(parameter.Name, instantiatedType, i, BaseMethodContext));
227,543✔
121
        }
122

123
        InjectedReturnType = GenericInstantiation.Instantiate(BaseMethodContext.ReturnTypeContext, typeGenericParameters, methodGenericParameters);
215,221✔
124

125
        if (UnderlyingPointer != 0)
215,221✔
126
            rawMethodBody = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
214,146✔
127
    }
215,221✔
128

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

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

140
        if (methodRef.TypeGenericParams.Length == 0)
214,896✔
141
            return baseType;
31,536✔
142

143
        var genericParams = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);
183,360✔
144

145
        return new GenericInstanceTypeAnalysisContext(baseType, genericParams, declaringAssembly);
183,360✔
146
    }
147

148
    private static TypeAnalysisContext[] ResolveTypeArray(Il2CppTypeReflectionData[] array, AssemblyAnalysisContext declaringAssembly)
149
    {
150
        if (array.Length == 0)
613,152✔
151
            return [];
212,070✔
152

153
        var ret = new TypeAnalysisContext[array.Length];
401,082✔
154
        for (var i = 0; i < array.Length; i++)
1,839,696✔
155
        {
156
            ret[i] = array[i].ToContext(declaringAssembly)
518,766!
157
                     ?? throw new($"Unable to resolve generic parameter {array[i]} for generic method.");
518,766✔
158
        }
159

160
        return ret;
401,082✔
161
    }
162

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

© 2025 Coveralls, Inc