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

SamboyCoding / Cpp2IL / 15618994651

12 Jun 2025 07:14PM UTC coverage: 34.338% (+0.03%) from 34.308%
15618994651

Pull #472

github

web-flow
Merge a04e5f9ec into 9ce22e911
Pull Request #472: Handle self-referencing generic classes

1782 of 6566 branches covered (27.14%)

Branch coverage included in aggregate %.

10 of 14 new or added lines in 3 files covered. (71.43%)

7 existing lines in 2 files now uncovered.

4189 of 10823 relevant lines covered (38.7%)

180993.43 hits per line

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

63.33
/Cpp2IL.Core/Model/Contexts/GenericInstanceTypeAnalysisContext.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics;
4
using System.Linq;
5
using System.Reflection;
6
using System.Text;
7
using Cpp2IL.Core.Utils;
8
using LibCpp2IL.BinaryStructures;
9

10
namespace Cpp2IL.Core.Model.Contexts;
11

12
public class GenericInstanceTypeAnalysisContext : ReferencedTypeAnalysisContext
13
{
14
    public TypeAnalysisContext GenericType { get; }
545,865✔
15

16
    public List<TypeAnalysisContext> GenericArguments { get; } = [];
1,153,221✔
17

18
    public override TypeAttributes DefaultAttributes => GenericType.DefaultAttributes;
327✔
19

20
    public override TypeAttributes? OverrideAttributes { get => GenericType.OverrideAttributes; set => GenericType.OverrideAttributes = value; }
327✔
21

22
    public override string DefaultName => $"{GenericType.Name}<{string.Join(", ", GenericArguments.Select(a => a.Name))}>";
×
23

24
    public override string DefaultNamespace => GenericType.Namespace;
×
25

26
    public override TypeAnalysisContext? DefaultBaseType { get; }
65✔
27

28
    public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST;
×
29

30
    public sealed override bool IsGenericInstance => true;
×
31

32
    public sealed override bool IsValueType => GenericType.IsValueType; //We don't set a definition so the default implementation cannot determine if we're a value type or not. 
8,920✔
33

34
    private GenericInstanceTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext referencedFrom) : base(referencedFrom)
11,313✔
35
    {
36
        // Cache this instance before resolving anything else, which might contain a reference to this instance.
37
        // https://github.com/SamboyCoding/Cpp2IL/issues/469
38
        referencedFrom.GenericInstanceTypesByIl2CppType.Add(rawType, this);
11,313✔
39

40
        //Generic type has to be a type definition
41
        var gClass = rawType.GetGenericClass();
11,313✔
42
        GenericType = AppContext.ResolveContextForType(gClass.TypeDefinition) ?? throw new($"Could not resolve type {gClass.TypeDefinition.FullName} for generic instance base type");
11,313!
43

44
        GenericArguments.AddRange(gClass.Context.ClassInst.Types.Select(referencedFrom.ResolveIl2CppType)!);
11,313✔
45

46
        SetDeclaringType();
11,313✔
47
    }
11,313✔
48

49
    public GenericInstanceTypeAnalysisContext(TypeAnalysisContext genericType, IEnumerable<TypeAnalysisContext> genericArguments, AssemblyAnalysisContext referencedFrom) : base(referencedFrom)
422,826✔
50
    {
51
        GenericType = genericType;
422,826✔
52
        GenericArguments.AddRange(genericArguments);
422,826✔
53
        DefaultBaseType = genericType.BaseType;
422,826✔
54

55
        SetDeclaringType();
422,826✔
56
    }
422,826✔
57

58
    public static GenericInstanceTypeAnalysisContext GetOrCreate(Il2CppType rawType, AssemblyAnalysisContext referencedFrom)
59
    {
60
        if (rawType.Type != Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
108,983!
NEW
61
            throw new ArgumentException($"Cannot create {nameof(GenericInstanceTypeAnalysisContext)} from type {rawType.Type}. Expected {Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST}.");
×
62

63
        if (!referencedFrom.GenericInstanceTypesByIl2CppType.TryGetValue(rawType, out var result))
108,983✔
64
        {
65
            result = new GenericInstanceTypeAnalysisContext(rawType, referencedFrom);
11,313✔
66
            Debug.Assert(referencedFrom.GenericInstanceTypesByIl2CppType.ContainsKey(rawType), $"The {nameof(GenericInstanceTypeAnalysisContext)} constructor should add itself to the dictionary.");
67
        }
68

69
        return result;
108,983✔
70
    }
71

72
    public override string GetCSharpSourceString()
73
    {
NEW
74
        var sb = new StringBuilder();
×
75

NEW
76
        sb.Append(GenericType.GetCSharpSourceString());
×
NEW
77
        sb.Append('<');
×
UNCOV
78
        var first = true;
×
UNCOV
79
        foreach (var genericArgument in GenericArguments)
×
80
        {
UNCOV
81
            if (!first)
×
82
                sb.Append(", ");
×
83
            else
84
                first = false;
×
85

UNCOV
86
            sb.Append(genericArgument.GetCSharpSourceString());
×
87
        }
88

UNCOV
89
        sb.Append('>');
×
90

UNCOV
91
        return sb.ToString();
×
92
    }
93

94
    private void SetDeclaringType()
95
    {
96
        var declaringType = GenericType.DeclaringType;
434,139✔
97
        if (declaringType is null)
434,139✔
98
            return;
321,312✔
99

100
        DeclaringType = declaringType.GenericParameters.Count == 0
112,827✔
101
            ? declaringType
112,827✔
102
            : declaringType.MakeGenericInstanceType(GenericArguments.Take(declaringType.GenericParameters.Count));
112,827✔
103
    }
112,827✔
104
}
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