-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
ResolveGenericsVisitor.cs
84 lines (71 loc) · 3.38 KB
/
ResolveGenericsVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
namespace Stride.Core.AssemblyProcessor
{
/// <summary>
/// Transform open generic types to closed instantiation using context information.
/// See <see cref="Process"/> for more details.
/// </summary>
class ResolveGenericsVisitor : CecilTypeReferenceVisitor
{
private Dictionary<TypeReference, TypeReference> genericTypeMapping;
public ResolveGenericsVisitor(Dictionary<TypeReference, TypeReference> genericTypeMapping)
{
this.genericTypeMapping = genericTypeMapping;
}
/// <summary>
/// Transform open generic types to closed instantiation using context information.
/// As an example, if B{T} inherits from A{T}, running it with B{C} as context and A{B.T} as type, ti will return A{C}.
/// </summary>
public static TypeReference Process(TypeReference context, TypeReference type)
{
if (type == null)
return null;
var parentContext = context;
GenericInstanceType genericInstanceTypeContext = null;
while (parentContext != null)
{
genericInstanceTypeContext = parentContext as GenericInstanceType;
if (genericInstanceTypeContext != null)
break;
parentContext = parentContext.Resolve().BaseType;
}
if (genericInstanceTypeContext == null || genericInstanceTypeContext.ContainsGenericParameter())
return type;
// Build dictionary that will map generic type to their real implementation type
var genericTypeMapping = new Dictionary<TypeReference, TypeReference>();
while (parentContext != null)
{
var resolvedType = parentContext.Resolve();
for (int i = 0; i < resolvedType.GenericParameters.Count; ++i)
{
var genericParameter = parentContext.GetElementType().Resolve().GenericParameters[i];
genericTypeMapping.Add(genericParameter, genericInstanceTypeContext.GenericArguments[i]);
}
parentContext = parentContext.Resolve().BaseType;
if (parentContext is GenericInstanceType)
genericInstanceTypeContext = parentContext as GenericInstanceType;
}
var visitor = new ResolveGenericsVisitor(genericTypeMapping);
var result = visitor.VisitDynamic(type);
// Make sure type is closed now
if (result.ContainsGenericParameter())
throw new InvalidOperationException("Unsupported generic resolution.");
return result;
}
public override TypeReference Visit(GenericParameter type)
{
TypeReference result;
TypeReference typeParent = type;
while (genericTypeMapping.TryGetValue(typeParent, out result))
typeParent = result;
if (typeParent != type)
return typeParent;
return base.Visit(type);
}
}
}