-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathParametricInterfaceManager.cs
115 lines (100 loc) · 4.06 KB
/
ParametricInterfaceManager.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Generator.Types;
namespace Generator
{
public class ParametricInterfaceManager
{
private static IEnumerable<Type> baseTypes = new List<Type>
{
//typeof(void),
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(char),
typeof(bool),
typeof(string),
typeof(object),
typeof(Guid),
};
private static IEnumerable<string> baseTypes2 = new List<string>
{
"DateTime",
"TimeSpan",
"Point",
"Size",
"Rect"
};
private Dictionary<string, GenericInstanceType> genericInstances = new Dictionary<string, GenericInstanceType>();
public void AddBaseIReferenceInstances(Generator gen)
{
var tyIReference = gen.GetTypeDefinition("Windows.Foundation.IReference`1");
var tyIReferenceArray = gen.GetTypeDefinition("Windows.Foundation.IReferenceArray`1");
var assembly = tyIReference.Module.Assembly; // we need just any assembly
var allBaseTypes = baseTypes.Select(t => assembly.MainModule.ImportReference(t)).Concat(baseTypes2.Select(t => gen.GetTypeDefinition("Windows.Foundation." + t).Type));
foreach (var baseType in allBaseTypes)
{
AddGenericInstance(tyIReference.MakeGenericInstanceType(baseType));
AddGenericInstance(tyIReferenceArray.MakeGenericInstanceType(baseType));
}
}
public void AddGenericInstance(GenericInstanceType ty)
{
if (genericInstances.ContainsKey(ty.FullName)) return;
genericInstances.Add(ty.FullName, ty);
// recursively add generic arguments
foreach (var arg in ty.GenericArguments)
{
if (arg.IsGenericInstance)
{
AddGenericInstance((GenericInstanceType)arg);
}
}
// build mapping from generic parameter names to instantiated types
TypeDefinition def = ty.Resolve();
var genericParameterMap = new Dictionary<string, TypeReference>();
for (int i = 0; i < def.GenericParameters.Count; i++)
{
genericParameterMap.Add(def.GenericParameters[i].FullName, ty.GenericArguments[i]);
}
// recursively add implemented interfaces
foreach (var intf in def.Interfaces.Select(i => i.InterfaceType).Where(i => i.ContainsGenericParameter && i is GenericInstanceType).Cast<GenericInstanceType>())
{
var type = TypeHelpers.InstantiateType(intf, genericParameterMap);
if (type is GenericInstanceType)
{
AddGenericInstance(type as GenericInstanceType);
}
}
// recursively add other instances introduced by function parameters
foreach (var m in def.Methods)
{
foreach (GenericInstanceType pty in m.Parameters
.Select(p => p.ParameterType)
.Concat(Enumerable.Repeat(m.ReturnType, 1))
.Where(t => t.ContainsGenericParameter && t is GenericInstanceType)
.Cast<GenericInstanceType>())
{
var type = TypeHelpers.InstantiateType(pty, genericParameterMap);
if (type is GenericInstanceType)
{
AddGenericInstance(type as GenericInstanceType);
}
}
}
}
public IEnumerable<ParametricInterfaceInstance> Collect(Generator gen)
{
return genericInstances.Values.Select(t => new ParametricInterfaceInstance(gen, t));
}
}
}