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

SamboyCoding / Cpp2IL / 15172415984

21 May 2025 08:56PM UTC coverage: 34.294% (+0.2%) from 34.062%
15172415984

Pull #462

github

web-flow
Merge 483c13920 into 5807d2b6c
Pull Request #462: Support overriding member types

1801 of 6644 branches covered (27.11%)

Branch coverage included in aggregate %.

128 of 232 new or added lines in 35 files covered. (55.17%)

5 existing lines in 5 files now uncovered.

4199 of 10852 relevant lines covered (38.69%)

186271.78 hits per line

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

80.77
/Cpp2IL.Core/Model/Contexts/ConcreteGenericMethodAnalysisContext.cs
1
using System;
2
using System.Reflection;
3
using Cpp2IL.Core.Utils;
4
using LibCpp2IL;
5
using LibCpp2IL.Reflection;
6

7
namespace Cpp2IL.Core.Model.Contexts;
8

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

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

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

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

36
    public sealed override ulong UnderlyingPointer => MethodRef?.GenericVariantPtr ?? default;
894,204✔
37

38
    public override string DefaultName => BaseMethodContext.DefaultName;
325✔
39

NEW
40
    public override TypeAnalysisContext DefaultReturnType { get; }
×
41

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

NEW
44
    public override MethodAttributes DefaultAttributes => BaseMethodContext.DefaultAttributes;
×
45

NEW
46
    public override MethodAttributes? OverrideAttributes { get => BaseMethodContext.OverrideAttributes; set => BaseMethodContext.OverrideAttributes = value; }
×
47

NEW
48
    public override MethodImplAttributes DefaultImplAttributes => BaseMethodContext.DefaultImplAttributes;
×
49

NEW
50
    public override MethodImplAttributes? OverrideImplAttributes { get => BaseMethodContext.OverrideImplAttributes; set => BaseMethodContext.OverrideImplAttributes = value; }
×
51

52
    public override AssemblyAnalysisContext CustomAttributeAssembly => BaseMethodContext.CustomAttributeAssembly;
×
53

54
    public ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, ApplicationAnalysisContext context)
55
        : this(methodRef, ResolveDeclaringAssembly(methodRef, context))
298,308✔
56
    {
57
    }
298,308✔
58

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

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

91
        if (methodGenericParameters.Length > 0 && baseMethod.GenericParameters.Count != methodGenericParameters.Length)
330!
92
            throw new ArgumentException("The number of method generic parameters must match the number of generic parameters on the base method.");
×
93
    }
330✔
94

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

102
        TypeGenericParameters = typeGenericParameters;
298,638✔
103
        MethodGenericParameters = methodGenericParameters;
298,638✔
104

105
        // For the purpose of generic instantiation, we need an array of method generic parameters, even if none are provided.
106
        if (methodGenericParameters.Length == 0 && baseMethodContext.GenericParameters.Count > 0)
298,638✔
107
            methodGenericParameters = baseMethodContext.GenericParameters.ToArray();
5✔
108

109
        for (var i = 0; i < BaseMethodContext.Parameters.Count; i++)
1,228,522✔
110
        {
111
            var parameter = BaseMethodContext.Parameters[i];
315,623✔
112
            var instantiatedType = GenericInstantiation.Instantiate(
315,623✔
113
                parameter.ParameterType,
315,623✔
114
                typeGenericParameters,
315,623✔
115
                methodGenericParameters);
315,623✔
116

117
            Parameters.Add(new ConcreteGenericParameterAnalysisContext(parameter, instantiatedType, this));
315,623✔
118
        }
119

120
        DefaultReturnType = GenericInstantiation.Instantiate(BaseMethodContext.ReturnType, typeGenericParameters, methodGenericParameters);
298,638✔
121

122
        if (UnderlyingPointer != 0)
298,638✔
123
            rawMethodBody = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
297,258✔
124
    }
298,638✔
125

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

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

137
        if (methodRef.TypeGenericParams.Length == 0)
298,308✔
138
            return baseType;
43,716✔
139

140
        var genericParams = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);
254,592✔
141

142
        return new GenericInstanceTypeAnalysisContext(baseType, genericParams, declaringAssembly);
254,592✔
143
    }
144

145
    private static TypeAnalysisContext[] ResolveTypeArray(Il2CppTypeReflectionData[] array, AssemblyAnalysisContext declaringAssembly)
146
    {
147
        if (array.Length == 0)
851,208✔
148
            return [];
294,366✔
149

150
        var ret = new TypeAnalysisContext[array.Length];
556,842✔
151
        for (var i = 0; i < array.Length; i++)
2,552,904✔
152
        {
153
            ret[i] = array[i].ToContext(declaringAssembly)
719,610!
154
                     ?? throw new($"Unable to resolve generic parameter {array[i]} for generic method.");
719,610✔
155
        }
156

157
        return ret;
556,842✔
158
    }
159

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