2 Copyright (C) 2009 Jeroen Frijters
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.
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:
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.
25 using System.Collections.Generic;
27 using IKVM.Reflection.Metadata;
29 namespace IKVM.Reflection.Reader
31 abstract class TypeParameterType : Type
33 public sealed override string AssemblyQualifiedName
38 public sealed override bool IsValueType
40 get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
43 public sealed override Type BaseType
47 foreach (Type type in GetGenericParameterConstraints())
49 if (!type.IsInterface && !type.IsGenericParameter)
54 return this.IsValueType ? this.Module.universe.System_ValueType : this.Module.universe.System_Object;
58 public override Type[] __GetDeclaredInterfaces()
60 List<Type> list = new List<Type>();
61 foreach (Type type in GetGenericParameterConstraints())
68 return list.ToArray();
71 public sealed override TypeAttributes Attributes
73 get { return TypeAttributes.Public; }
76 public sealed override string FullName
81 public sealed override string ToString()
86 public sealed override bool IsGenericParameter
92 sealed class UnboundGenericMethodParameter : TypeParameterType
94 private static readonly DummyModule module = new DummyModule();
95 private readonly int position;
97 private sealed class DummyModule : NonPEModule
99 internal DummyModule()
100 : base(new Universe())
104 protected override Exception NotSupportedException()
106 return new InvalidOperationException();
109 protected override Exception ArgumentOutOfRangeException()
111 return new InvalidOperationException();
114 public override bool Equals(object obj)
116 throw new InvalidOperationException();
119 public override int GetHashCode()
121 throw new InvalidOperationException();
124 public override string ToString()
126 throw new InvalidOperationException();
129 public override int MDStreamVersion
131 get { throw new InvalidOperationException(); }
134 public override Assembly Assembly
136 get { throw new InvalidOperationException(); }
139 internal override Type FindType(TypeName typeName)
141 throw new InvalidOperationException();
144 internal override void GetTypesImpl(List<Type> list)
146 throw new InvalidOperationException();
149 public override string FullyQualifiedName
151 get { throw new InvalidOperationException(); }
154 public override string Name
156 get { throw new InvalidOperationException(); }
159 public override Guid ModuleVersionId
161 get { throw new InvalidOperationException(); }
164 public override string ScopeName
166 get { throw new InvalidOperationException(); }
170 internal static Type Make(int position)
172 return module.universe.CanonicalizeType(new UnboundGenericMethodParameter(position));
175 private UnboundGenericMethodParameter(int position)
177 this.position = position;
180 public override bool Equals(object obj)
182 UnboundGenericMethodParameter other = obj as UnboundGenericMethodParameter;
183 return other != null && other.position == position;
186 public override int GetHashCode()
191 public override string Namespace
193 get { throw new InvalidOperationException(); }
196 public override string Name
198 get { throw new InvalidOperationException(); }
201 public override int MetadataToken
203 get { throw new InvalidOperationException(); }
206 public override Module Module
208 get { return module; }
211 public override int GenericParameterPosition
213 get { return position; }
216 public override Type DeclaringType
221 public override MethodBase DeclaringMethod
223 get { throw new InvalidOperationException(); }
226 public override Type[] GetGenericParameterConstraints()
228 throw new InvalidOperationException();
231 public override GenericParameterAttributes GenericParameterAttributes
233 get { throw new InvalidOperationException(); }
236 internal override Type BindTypeParameters(IGenericBinder binder)
238 return binder.BindMethodParameter(this);
242 sealed class GenericTypeParameter : TypeParameterType
244 private readonly ModuleReader module;
245 private readonly int index;
247 internal GenericTypeParameter(ModuleReader module, int index)
249 this.module = module;
253 public override bool Equals(object obj)
255 return base.Equals(obj);
258 public override int GetHashCode()
260 return base.GetHashCode();
263 public override string Namespace
265 get { return DeclaringType.Namespace; }
268 public override string Name
270 get { return module.GetString(module.GenericParam.records[index].Name); }
273 public override Module Module
275 get { return module; }
278 public override int MetadataToken
280 get { return (GenericParamTable.Index << 24) + index + 1; }
283 public override int GenericParameterPosition
285 get { return module.GenericParam.records[index].Number; }
288 public override Type DeclaringType
292 int owner = module.GenericParam.records[index].Owner;
293 return (owner >> 24) == TypeDefTable.Index ? module.ResolveType(owner) : null;
297 public override MethodBase DeclaringMethod
301 int owner = module.GenericParam.records[index].Owner;
302 return (owner >> 24) == MethodDefTable.Index ? module.ResolveMethod(owner) : null;
306 public override Type[] GetGenericParameterConstraints()
308 IGenericContext context = (this.DeclaringMethod as IGenericContext) ?? this.DeclaringType;
309 List<Type> list = new List<Type>();
310 foreach (int i in module.GenericParamConstraint.Filter(this.MetadataToken))
312 list.Add(module.ResolveType(module.GenericParamConstraint.records[i].Constraint, context));
314 return list.ToArray();
317 public override GenericParameterAttributes GenericParameterAttributes
319 get { return (GenericParameterAttributes)module.GenericParam.records[index].Flags; }
322 internal override Type BindTypeParameters(IGenericBinder binder)
324 int owner = module.GenericParam.records[index].Owner;
325 if ((owner >> 24) == MethodDefTable.Index)
327 return binder.BindMethodParameter(this);
331 return binder.BindTypeParameter(this);