Debian Codegraph @

external/ikvm/reflect/Reader/TypeDefImpl.cs

raw
1 /*
2 Copyright (C) 2009-2011 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 [email protected]
22
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Text;
27 using System.Runtime.InteropServices;
28 using IKVM.Reflection.Metadata;
29
30 namespace IKVM.Reflection.Reader
31 {
32 sealed class TypeDefImpl : TypeInfo
33 {
34 private readonly ModuleReader module;
35 private readonly int index;
36 private readonly string typeName;
37 private readonly string typeNamespace;
38 private Type[] typeArgs;
39
40 internal TypeDefImpl(ModuleReader module, int index)
41 {
42 this.module = module;
43 this.index = index;
44 // empty typeName is not allowed, but obfuscators...
45 this.typeName = module.GetString(module.TypeDef.records[index].TypeName) ?? "";
46 this.typeNamespace = module.GetString(module.TypeDef.records[index].TypeNamespace);
47 MarkKnownType(typeNamespace, typeName);
48 }
49
50 public override Type BaseType
51 {
52 get
53 {
54 int extends = module.TypeDef.records[index].Extends;
55 if ((extends & 0xFFFFFF) == 0)
56 {
57 return null;
58 }
59 return module.ResolveType(extends, this);
60 }
61 }
62
63 public override TypeAttributes Attributes
64 {
65 get { return (TypeAttributes)module.TypeDef.records[index].Flags; }
66 }
67
68 public override EventInfo[] __GetDeclaredEvents()
69 {
70 foreach (int i in module.EventMap.Filter(this.MetadataToken))
71 {
72 int evt = module.EventMap.records[i].EventList - 1;
73 int end = module.EventMap.records.Length > i + 1 ? module.EventMap.records[i + 1].EventList - 1 : module.Event.records.Length;
74 EventInfo[] events = new EventInfo[end - evt];
75 if (module.EventPtr.RowCount == 0)
76 {
77 for (int j = 0; evt < end; evt++, j++)
78 {
79 events[j] = new EventInfoImpl(module, this, evt);
80 }
81 }
82 else
83 {
84 for (int j = 0; evt < end; evt++, j++)
85 {
86 events[j] = new EventInfoImpl(module, this, module.EventPtr.records[evt] - 1);
87 }
88 }
89 return events;
90 }
91 return Empty<EventInfo>.Array;
92 }
93
94 public override FieldInfo[] __GetDeclaredFields()
95 {
96 int field = module.TypeDef.records[index].FieldList - 1;
97 int end = module.TypeDef.records.Length > index + 1 ? module.TypeDef.records[index + 1].FieldList - 1 : module.Field.records.Length;
98 FieldInfo[] fields = new FieldInfo[end - field];
99 if (module.FieldPtr.RowCount == 0)
100 {
101 for (int i = 0; field < end; i++, field++)
102 {
103 fields[i] = module.GetFieldAt(this, field);
104 }
105 }
106 else
107 {
108 for (int i = 0; field < end; i++, field++)
109 {
110 fields[i] = module.GetFieldAt(this, module.FieldPtr.records[field] - 1);
111 }
112 }
113 return fields;
114 }
115
116 public override Type[] __GetDeclaredInterfaces()
117 {
118 List<Type> list = null;
119 foreach (int i in module.InterfaceImpl.Filter(this.MetadataToken))
120 {
121 if (list == null)
122 {
123 list = new List<Type>();
124 }
125 list.Add(module.ResolveType(module.InterfaceImpl.records[i].Interface, this));
126 }
127 return Util.ToArray(list, Type.EmptyTypes);
128 }
129
130 public override MethodBase[] __GetDeclaredMethods()
131 {
132 int method = module.TypeDef.records[index].MethodList - 1;
133 int end = module.TypeDef.records.Length > index + 1 ? module.TypeDef.records[index + 1].MethodList - 1 : module.MethodDef.records.Length;
134 MethodBase[] methods = new MethodBase[end - method];
135 if (module.MethodPtr.RowCount == 0)
136 {
137 for (int i = 0; method < end; method++, i++)
138 {
139 methods[i] = module.GetMethodAt(this, method);
140 }
141 }
142 else
143 {
144 for (int i = 0; method < end; method++, i++)
145 {
146 methods[i] = module.GetMethodAt(this, module.MethodPtr.records[method] - 1);
147 }
148 }
149 return methods;
150 }
151
152 public override __MethodImplMap __GetMethodImplMap()
153 {
154 PopulateGenericArguments();
155 List<MethodInfo> bodies = new List<MethodInfo>();
156 List<List<MethodInfo>> declarations = new List<List<MethodInfo>>();
157 foreach (int i in module.MethodImpl.Filter(this.MetadataToken))
158 {
159 MethodInfo body = (MethodInfo)module.ResolveMethod(module.MethodImpl.records[i].MethodBody, typeArgs, null);
160 int index = bodies.IndexOf(body);
161 if (index == -1)
162 {
163 index = bodies.Count;
164 bodies.Add(body);
165 declarations.Add(new List<MethodInfo>());
166 }
167 MethodInfo declaration = (MethodInfo)module.ResolveMethod(module.MethodImpl.records[i].MethodDeclaration, typeArgs, null);
168 declarations[index].Add(declaration);
169 }
170 __MethodImplMap map = new __MethodImplMap();
171 map.TargetType = this;
172 map.MethodBodies = bodies.ToArray();
173 map.MethodDeclarations = new MethodInfo[declarations.Count][];
174 for (int i = 0; i < map.MethodDeclarations.Length; i++)
175 {
176 map.MethodDeclarations[i] = declarations[i].ToArray();
177 }
178 return map;
179 }
180
181 public override Type[] __GetDeclaredTypes()
182 {
183 int token = this.MetadataToken;
184 List<Type> list = new List<Type>();
185 // note that the NestedClass table is sorted on NestedClass, so we can't use binary search
186 for (int i = 0; i < module.NestedClass.records.Length; i++)
187 {
188 if (module.NestedClass.records[i].EnclosingClass == token)
189 {
190 list.Add(module.ResolveType(module.NestedClass.records[i].NestedClass));
191 }
192 }
193 return list.ToArray();
194 }
195
196 public override PropertyInfo[] __GetDeclaredProperties()
197 {
198 foreach (int i in module.PropertyMap.Filter(this.MetadataToken))
199 {
200 int property = module.PropertyMap.records[i].PropertyList - 1;
201 int end = module.PropertyMap.records.Length > i + 1 ? module.PropertyMap.records[i + 1].PropertyList - 1 : module.Property.records.Length;
202 PropertyInfo[] properties = new PropertyInfo[end - property];
203 if (module.PropertyPtr.RowCount == 0)
204 {
205 for (int j = 0; property < end; property++, j++)
206 {
207 properties[j] = new PropertyInfoImpl(module, this, property);
208 }
209 }
210 else
211 {
212 for (int j = 0; property < end; property++, j++)
213 {
214 properties[j] = new PropertyInfoImpl(module, this, module.PropertyPtr.records[property] - 1);
215 }
216 }
217 return properties;
218 }
219 return Empty<PropertyInfo>.Array;
220 }
221
222 internal override TypeName TypeName
223 {
224 get { return new TypeName(typeNamespace, typeName); }
225 }
226
227 public override string Name
228 {
229 get { return TypeNameParser.Escape(typeName); }
230 }
231
232 public override string FullName
233 {
234 get { return GetFullName(); }
235 }
236
237 public override int MetadataToken
238 {
239 get { return (TypeDefTable.Index << 24) + index + 1; }
240 }
241
242 public override Type[] GetGenericArguments()
243 {
244 PopulateGenericArguments();
245 return Util.Copy(typeArgs);
246 }
247
248 private void PopulateGenericArguments()
249 {
250 if (typeArgs == null)
251 {
252 int token = this.MetadataToken;
253 int first = module.GenericParam.FindFirstByOwner(token);
254 if (first == -1)
255 {
256 typeArgs = Type.EmptyTypes;
257 }
258 else
259 {
260 List<Type> list = new List<Type>();
261 int len = module.GenericParam.records.Length;
262 for (int i = first; i < len && module.GenericParam.records[i].Owner == token; i++)
263 {
264 list.Add(new GenericTypeParameter(module, i, Signature.ELEMENT_TYPE_VAR));
265 }
266 typeArgs = list.ToArray();
267 }
268 }
269 }
270
271 internal override Type GetGenericTypeArgument(int index)
272 {
273 PopulateGenericArguments();
274 return typeArgs[index];
275 }
276
277 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
278 {
279 PopulateGenericArguments();
280 return new CustomModifiers[typeArgs.Length];
281 }
282
283 public override bool IsGenericType
284 {
285 get { return IsGenericTypeDefinition; }
286 }
287
288 public override bool IsGenericTypeDefinition
289 {
290 get
291 {
292 if ((typeFlags & (TypeFlags.IsGenericTypeDefinition | TypeFlags.NotGenericTypeDefinition)) == 0)
293 {
294 typeFlags |= module.GenericParam.FindFirstByOwner(this.MetadataToken) == -1
295 ? TypeFlags.NotGenericTypeDefinition
296 : TypeFlags.IsGenericTypeDefinition;
297 }
298 return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0;
299 }
300 }
301
302 public override Type GetGenericTypeDefinition()
303 {
304 if (IsGenericTypeDefinition)
305 {
306 return this;
307 }
308 throw new InvalidOperationException();
309 }
310
311 public override string ToString()
312 {
313 StringBuilder sb = new StringBuilder(this.FullName);
314 string sep = "[";
315 foreach (Type arg in GetGenericArguments())
316 {
317 sb.Append(sep);
318 sb.Append(arg);
319 sep = ",";
320 }
321 if (sep != "[")
322 {
323 sb.Append(']');
324 }
325 return sb.ToString();
326 }
327
328 internal bool IsNestedByFlags
329 {
330 get { return (this.Attributes & TypeAttributes.VisibilityMask & ~TypeAttributes.Public) != 0; }
331 }
332
333 public override Type DeclaringType
334 {
335 get
336 {
337 // note that we cannot use Type.IsNested for this, because that calls DeclaringType
338 if (!IsNestedByFlags)
339 {
340 return null;
341 }
342 foreach (int i in module.NestedClass.Filter(this.MetadataToken))
343 {
344 return module.ResolveType(module.NestedClass.records[i].EnclosingClass, null, null);
345 }
346 throw new InvalidOperationException();
347 }
348 }
349
350 public override bool __GetLayout(out int packingSize, out int typeSize)
351 {
352 foreach (int i in module.ClassLayout.Filter(this.MetadataToken))
353 {
354 packingSize = module.ClassLayout.records[i].PackingSize;
355 typeSize = module.ClassLayout.records[i].ClassSize;
356 return true;
357 }
358 packingSize = 0;
359 typeSize = 0;
360 return false;
361 }
362
363 public override Module Module
364 {
365 get { return module; }
366 }
367
368 internal override bool IsModulePseudoType
369 {
370 get { return index == 0; }
371 }
372
373 internal override bool IsBaked
374 {
375 get { return true; }
376 }
377 }
378 }