forked from SharpGenTools/SharpGenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallbackBase.Reflection.cs
More file actions
223 lines (180 loc) · 6.35 KB
/
Copy pathCallbackBase.Reflection.cs
File metadata and controls
223 lines (180 loc) · 6.35 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using SharpGen.Runtime.Trimming;
namespace SharpGen.Runtime;
public abstract partial class CallbackBase
{
private readonly struct ImmediateShadowInterfaceInfo
{
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
public readonly TypeInfo Type;
public readonly List<TypeInfo> ImplementedInterfaces;
public ImmediateShadowInterfaceInfo(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
TypeInfo type)
{
Type = type;
ImplementedInterfaces = new(6);
foreach (var implementedInterface in type.ImplementedInterfaces)
{
var interfaceInfo = implementedInterface.GetTypeInfo();
// If there is no Vtbl attribute then this isn't a native interface.
if (!VtblAttribute.Has(interfaceInfo))
continue;
ImplementedInterfaces.Add(interfaceInfo);
}
}
}
// Cache reflection on interface inheritance
private class CallbackTypeInfo
{
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
private readonly TypeInfo type;
private ImmediateShadowInterfaceInfo[]? _vtbls;
private TypeInfo[]? _shadows;
private Guid[]? _guids;
public CallbackTypeInfo(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
Type type) : this(type.GetTypeInfo())
{
}
private CallbackTypeInfo(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
TypeInfo type)
{
this.type = type ?? throw new ArgumentNullException(nameof(type));
}
/// <summary>
/// Gets a list of implemented interfaces that aren't inherited by any other <c>[Vtbl]</c> interfaces.
/// </summary>
/// <returns>The interface list.</returns>
public ImmediateShadowInterfaceInfo[] Vtbls
{
get
{
lock (this)
if (_vtbls is { } vtbls)
return vtbls;
var list = BuildVtblList();
lock (this)
_vtbls = list;
return list;
}
}
public TypeInfo[] Shadows
{
get
{
lock (this)
if (_shadows is { } shadows)
return shadows;
var list = BuildShadowList();
lock (this)
_shadows = list;
return list;
}
}
public Guid[] Guids
{
get
{
lock (this)
if (_guids is { } guids)
return guids;
var list = BuildGuidList();
lock (this)
_guids = list;
return list;
}
}
private ImmediateShadowInterfaceInfo[] BuildVtblList()
{
HashSet<TypeInfo> removeQueue = new();
List<ImmediateShadowInterfaceInfo> result = new();
foreach (var implementedInterface in type.ImplementedInterfaces)
{
var item = implementedInterface.GetTypeInfoWithNestedPreservedInterfaces();
// Only process interfaces that have vtbl
if (!VtblAttribute.Has(item))
continue;
ImmediateShadowInterfaceInfo interfaceInfo = new(item);
result.Add(interfaceInfo);
// Keep only final interfaces and not intermediate.
foreach (var @interface in interfaceInfo.ImplementedInterfaces)
removeQueue.Add(@interface);
}
result.RemoveAll(item => removeQueue.Contains(item.Type));
return result.ToArray();
}
private Guid[] BuildGuidList()
{
List<Guid> guids = new();
// Associate all shadows with their interfaces.
foreach (var item in Vtbls)
{
var itemType = item.Type;
if (!ExcludeFromTypeListAttribute.Has(itemType))
guids.Add(itemType.GUID);
// Associate also inherited interface to this shadow
foreach (var inheritInterface in item.ImplementedInterfaces)
{
if (!ExcludeFromTypeListAttribute.Has(inheritInterface))
guids.Add(inheritInterface.GUID);
}
}
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1 || NET472
HashSet<Guid> guidSet = new(guids.Count);
#else
HashSet<Guid> guidSet = new();
#endif
return guids.Where(guid => guidSet.Add(guid)).ToArray();
}
private TypeInfo[] BuildShadowList()
{
List<TypeInfo> shadows = new(), result;
foreach (var implementedInterface in type.ImplementedInterfaces)
{
var attribute = ShadowAttribute.Get(implementedInterface);
// Only process interfaces that have Shadow attribute
if (attribute is null)
continue;
shadows.Add(attribute.Type.GetTypeInfo());
}
var count = shadows.Count;
result = new List<TypeInfo>(count);
// Retain only shadows which have no other subtypes (none of the other shadows are children)
for (var i = 0; i < count; i++)
{
var item = shadows[i];
var any = false;
for (var j = 0; j < count; j++)
{
if (i == j)
continue;
if (item.IsAssignableFrom(shadows[j]))
{
any = true;
break;
}
}
if (!any)
result.Add(item);
}
return result.ToArray();
}
}
}