-
Notifications
You must be signed in to change notification settings - Fork 1
/
Worker.cs
242 lines (228 loc) · 9.88 KB
/
Worker.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// Import Checker; simple program to show what 1 or more dlls import
//
// Authors:
// Carlo Kok <ck@remobjects.com>
//
// Copyright (C) 2010 RemObjects Software
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
namespace ImportChecker
{
class Worker
{
private importchecker output = new importchecker();
private Dictionary<TypeReference, ImportedType> typeCache = new Dictionary<TypeReference, ImportedType>();
private Dictionary<MemberReference, ImportedMember> memberCache = new Dictionary<MemberReference, ImportedMember>();
public importchecker Output { get { return output; } }
public Worker()
{
}
internal void Work(Mono.Cecil.ModuleDefinition md)
{
foreach (TypeDefinition def in md.Types)
{
if (def.BaseType != null)
ImportType(def.BaseType);
foreach (var gp in def.GenericParameters)
foreach (var ct in gp.Constraints)
ImportType(ct);
foreach (var el in def.Interfaces)
ImplementType(el);
foreach (var el in def.CustomAttributes)
ImportMember(el.Constructor);
foreach (var el in def.Events)
ImportType(el.EventType);
foreach (var el in def.Properties)
{
ImportType(el.PropertyType);
if (el.HasParameters)
foreach (var par in el.Parameters)
ImportType(par.ParameterType);
}
foreach (var el in def.Fields)
ImportType(el.FieldType);
foreach (var el in def.Methods)
{
ImportType(el.ReturnType);
foreach (var gp in el.GenericParameters)
foreach (var ct in gp.Constraints)
ImportType(ct);
foreach (var par in el.Parameters)
ImportType(par.ParameterType);
foreach (var ov in el.Overrides)
ImplementMember(ov);
if (el.IsVirtual && !el.IsNewSlot)
ImplementMember(el);
if (el.HasBody) {
if (el.Body.HasExceptionHandlers)
foreach (var ex in el.Body.ExceptionHandlers)
ImportType(ex.CatchType);
foreach (var inst in el.Body.Instructions)
{
if (inst.Operand is TypeReference)
ImportType((TypeReference)inst.Operand);
else if (inst.Operand is MemberReference)
ImportMember((MemberReference)inst.Operand);
}
}
}
}
}
private void ImplementMember(MethodReference mref)
{
if (mref is MethodDefinition) return;
ImportedType it = ImportType(mref.DeclaringType);
if (it == null) return; // nothing to do here
if (it.implemented == null) it.implemented = new List<ImplementedMember>();
it.implemented.Add(new ImplementedMember
{
name = mref.Name,
signature = mref.FullName
});
}
private void ImplementType(TypeReference tref)
{
if (tref is TypeDefinition) return;
if (tref is GenericInstanceType)
{
GenericInstanceType gt = (GenericInstanceType)tref;
for (int i = 0; i < gt.GenericArguments.Count; i++)
ImportType(gt.GenericArguments[i]);
if (gt.ElementType is TypeDefinition) return;
}
ImportedType it = ImportType(tref);
if (it != null)
{
if (it.implementedByType) return; // already done
it.implementedByType = true;
it.implementedByTypeSpecified = true;
it.implemented = new List<ImplementedMember>();
TypeDefinition type = tref.Resolve();
foreach (var el in type.Methods) // at this point we have to presume the current class implements them all
{
ImplementedMember im = new ImplementedMember();
im.name = el.Name;
im.signature = el.FullName;
it.implemented.Add(im);
}
}
}
private ImportedMember ImportMember(MemberReference mref)
{
if (mref == null) return null;
if (mref is GenericInstanceMethod) {
GenericInstanceMethod gm = (GenericInstanceMethod)mref;
for (int i = 0; i < gm.GenericArguments.Count; i++)
ImportType(gm.GenericArguments[i]);
mref = gm.ElementMethod;
}
if (mref is MethodDefinition) return null;
if (memberCache.ContainsKey(mref)) return memberCache[mref];
ImportedType it = ImportType(mref.DeclaringType);
if (it == null) return null;
ImportedMember mb = new ImportedMember();
it.members.Add(mb);
mb.name = mref.Name;
if (mref is FieldReference)
{
mb.signature = ((FieldReference)mref).Resolve().FullName;
mb.kind = ElementType.field;
}
else
{
mb.signature = ((MethodReference)mref).Resolve().FullName;
mb.kind = ElementType.method;
}
mb.kindSpecified = true;
memberCache.Add(mref, mb);
return mb;
}
private ImportedType ImportType(TypeReference tref)
{
if (tref == null) return null;
switch (tref.MetadataType)
{
case MetadataType.Pointer:
case MetadataType.Pinned:
case MetadataType.Array:
ImportType(tref.GetElementType());
return null;
case MetadataType.OptionalModifier:
case MetadataType.RequiredModifier:
case MetadataType.ByReference:
return ImportType(tref.GetElementType());
case MetadataType.Boolean:
case MetadataType.Byte:
case MetadataType.Char:
case MetadataType.Double:
case MetadataType.FunctionPointer:
case MetadataType.GenericInstance:
case MetadataType.Int16:
case MetadataType.Int32:
case MetadataType.Int64:
case MetadataType.IntPtr:
case MetadataType.MVar:
case MetadataType.Object:
case MetadataType.SByte:
case MetadataType.Single:
case MetadataType.String:
case MetadataType.UInt16:
case MetadataType.UInt32:
case MetadataType.UInt64:
case MetadataType.UIntPtr:
case MetadataType.ValueType:
case MetadataType.Var:
case MetadataType.Void:
case MetadataType.TypedByReference:
return null;
}
if (tref is GenericInstanceType)
{
GenericInstanceType gt = (GenericInstanceType)tref;
for (int i = 0; i < gt.GenericArguments.Count; i++)
ImportType(gt.GenericArguments[i]);
if (gt.ElementType is TypeDefinition) return null;
tref = gt.ElementType;
}
if (tref is TypeDefinition) return null;
if (typeCache.ContainsKey(tref)) return typeCache[tref];
if (Program.IsFiltered(tref)) return null;
var mod = output.library.Where(a => a.name == tref.Scope.Name).FirstOrDefault();
if (mod == null)
{
mod = new ImportedLibrary();
mod.name = tref.Scope.Name;
mod.fullname = tref.Module.AssemblyReferences.Where(a => a.Name == tref.Scope.Name).FirstOrDefault().FullName;
output.library.Add(mod);
}
var it = new ImportedType();
it.name = tref.ToString();
mod.type.Add(it);
typeCache.Add(tref, it);
return it;
}
}
}