| 1 |
/* |
| 2 |
Copyright (C) 2009-2012 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 IKVM.Reflection.Metadata; |
| 28 |
|
| 29 |
namespace IKVM.Reflection.Reader |
| 30 |
{ |
| 31 |
abstract class TypeParameterType : TypeInfo |
| 32 |
{ |
| 33 |
protected TypeParameterType(byte sigElementType) |
| 34 |
: base(sigElementType) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
public sealed override string AssemblyQualifiedName |
| 39 |
{ |
| 40 |
get { return null; } |
| 41 |
} |
| 42 |
|
| 43 |
public sealed override bool IsValueType |
| 44 |
{ |
| 45 |
get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; } |
| 46 |
} |
| 47 |
|
| 48 |
public sealed override Type BaseType |
| 49 |
{ |
| 50 |
get |
| 51 |
{ |
| 52 |
foreach (Type type in GetGenericParameterConstraints()) |
| 53 |
{ |
| 54 |
if (!type.IsInterface && !type.IsGenericParameter) |
| 55 |
{ |
| 56 |
return type; |
| 57 |
} |
| 58 |
} |
| 59 |
return this.IsValueType ? this.Module.universe.System_ValueType : this.Module.universe.System_Object; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public override Type[] __GetDeclaredInterfaces() |
| 64 |
{ |
| 65 |
List<Type> list = new List<Type>(); |
| 66 |
foreach (Type type in GetGenericParameterConstraints()) |
| 67 |
{ |
| 68 |
if (type.IsInterface) |
| 69 |
{ |
| 70 |
list.Add(type); |
| 71 |
} |
| 72 |
} |
| 73 |
return list.ToArray(); |
| 74 |
} |
| 75 |
|
| 76 |
public sealed override TypeAttributes Attributes |
| 77 |
{ |
| 78 |
get { return TypeAttributes.Public; } |
| 79 |
} |
| 80 |
|
| 81 |
public sealed override string FullName |
| 82 |
{ |
| 83 |
get { return null; } |
| 84 |
} |
| 85 |
|
| 86 |
public sealed override string ToString() |
| 87 |
{ |
| 88 |
return this.Name; |
| 89 |
} |
| 90 |
|
| 91 |
protected sealed override bool ContainsMissingTypeImpl |
| 92 |
{ |
| 93 |
get { return ContainsMissingType(GetGenericParameterConstraints()); } |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
sealed class UnboundGenericMethodParameter : TypeParameterType |
| 98 |
{ |
| 99 |
private static readonly DummyModule module = new DummyModule(); |
| 100 |
private readonly int position; |
| 101 |
|
| 102 |
private sealed class DummyModule : NonPEModule |
| 103 |
{ |
| 104 |
internal DummyModule() |
| 105 |
: base(new Universe()) |
| 106 |
{ |
| 107 |
} |
| 108 |
|
| 109 |
protected override Exception NotSupportedException() |
| 110 |
{ |
| 111 |
return new InvalidOperationException(); |
| 112 |
} |
| 113 |
|
| 114 |
protected override Exception ArgumentOutOfRangeException() |
| 115 |
{ |
| 116 |
return new InvalidOperationException(); |
| 117 |
} |
| 118 |
|
| 119 |
public override bool Equals(object obj) |
| 120 |
{ |
| 121 |
throw new InvalidOperationException(); |
| 122 |
} |
| 123 |
|
| 124 |
public override int GetHashCode() |
| 125 |
{ |
| 126 |
throw new InvalidOperationException(); |
| 127 |
} |
| 128 |
|
| 129 |
public override string ToString() |
| 130 |
{ |
| 131 |
throw new InvalidOperationException(); |
| 132 |
} |
| 133 |
|
| 134 |
public override int MDStreamVersion |
| 135 |
{ |
| 136 |
get { throw new InvalidOperationException(); } |
| 137 |
} |
| 138 |
|
| 139 |
public override Assembly Assembly |
| 140 |
{ |
| 141 |
get { throw new InvalidOperationException(); } |
| 142 |
} |
| 143 |
|
| 144 |
internal override Type FindType(TypeName typeName) |
| 145 |
{ |
| 146 |
throw new InvalidOperationException(); |
| 147 |
} |
| 148 |
|
| 149 |
internal override Type FindTypeIgnoreCase(TypeName lowerCaseName) |
| 150 |
{ |
| 151 |
throw new InvalidOperationException(); |
| 152 |
} |
| 153 |
|
| 154 |
internal override void GetTypesImpl(List<Type> list) |
| 155 |
{ |
| 156 |
throw new InvalidOperationException(); |
| 157 |
} |
| 158 |
|
| 159 |
public override string FullyQualifiedName |
| 160 |
{ |
| 161 |
get { throw new InvalidOperationException(); } |
| 162 |
} |
| 163 |
|
| 164 |
public override string Name |
| 165 |
{ |
| 166 |
get { throw new InvalidOperationException(); } |
| 167 |
} |
| 168 |
|
| 169 |
public override Guid ModuleVersionId |
| 170 |
{ |
| 171 |
get { throw new InvalidOperationException(); } |
| 172 |
} |
| 173 |
|
| 174 |
public override string ScopeName |
| 175 |
{ |
| 176 |
get { throw new InvalidOperationException(); } |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
internal static Type Make(int position) |
| 181 |
{ |
| 182 |
return module.universe.CanonicalizeType(new UnboundGenericMethodParameter(position)); |
| 183 |
} |
| 184 |
|
| 185 |
private UnboundGenericMethodParameter(int position) |
| 186 |
: base(Signature.ELEMENT_TYPE_MVAR) |
| 187 |
{ |
| 188 |
this.position = position; |
| 189 |
} |
| 190 |
|
| 191 |
public override bool Equals(object obj) |
| 192 |
{ |
| 193 |
UnboundGenericMethodParameter other = obj as UnboundGenericMethodParameter; |
| 194 |
return other != null && other.position == position; |
| 195 |
} |
| 196 |
|
| 197 |
public override int GetHashCode() |
| 198 |
{ |
| 199 |
return position; |
| 200 |
} |
| 201 |
|
| 202 |
public override string Namespace |
| 203 |
{ |
| 204 |
get { throw new InvalidOperationException(); } |
| 205 |
} |
| 206 |
|
| 207 |
public override string Name |
| 208 |
{ |
| 209 |
get { throw new InvalidOperationException(); } |
| 210 |
} |
| 211 |
|
| 212 |
public override int MetadataToken |
| 213 |
{ |
| 214 |
get { throw new InvalidOperationException(); } |
| 215 |
} |
| 216 |
|
| 217 |
public override Module Module |
| 218 |
{ |
| 219 |
get { return module; } |
| 220 |
} |
| 221 |
|
| 222 |
public override int GenericParameterPosition |
| 223 |
{ |
| 224 |
get { return position; } |
| 225 |
} |
| 226 |
|
| 227 |
public override Type DeclaringType |
| 228 |
{ |
| 229 |
get { return null; } |
| 230 |
} |
| 231 |
|
| 232 |
public override MethodBase DeclaringMethod |
| 233 |
{ |
| 234 |
get { throw new InvalidOperationException(); } |
| 235 |
} |
| 236 |
|
| 237 |
public override Type[] GetGenericParameterConstraints() |
| 238 |
{ |
| 239 |
throw new InvalidOperationException(); |
| 240 |
} |
| 241 |
|
| 242 |
public override CustomModifiers[] __GetGenericParameterConstraintCustomModifiers() |
| 243 |
{ |
| 244 |
throw new InvalidOperationException(); |
| 245 |
} |
| 246 |
|
| 247 |
public override GenericParameterAttributes GenericParameterAttributes |
| 248 |
{ |
| 249 |
get { throw new InvalidOperationException(); } |
| 250 |
} |
| 251 |
|
| 252 |
internal override Type BindTypeParameters(IGenericBinder binder) |
| 253 |
{ |
| 254 |
return binder.BindMethodParameter(this); |
| 255 |
} |
| 256 |
|
| 257 |
internal override bool IsBaked |
| 258 |
{ |
| 259 |
get { throw new InvalidOperationException(); } |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
sealed class GenericTypeParameter : TypeParameterType |
| 264 |
{ |
| 265 |
private readonly ModuleReader module; |
| 266 |
private readonly int index; |
| 267 |
|
| 268 |
internal GenericTypeParameter(ModuleReader module, int index, byte sigElementType) |
| 269 |
: base(sigElementType) |
| 270 |
{ |
| 271 |
this.module = module; |
| 272 |
this.index = index; |
| 273 |
} |
| 274 |
|
| 275 |
public override bool Equals(object obj) |
| 276 |
{ |
| 277 |
return base.Equals(obj); |
| 278 |
} |
| 279 |
|
| 280 |
public override int GetHashCode() |
| 281 |
{ |
| 282 |
return base.GetHashCode(); |
| 283 |
} |
| 284 |
|
| 285 |
public override string Namespace |
| 286 |
{ |
| 287 |
get { return DeclaringType.Namespace; } |
| 288 |
} |
| 289 |
|
| 290 |
public override string Name |
| 291 |
{ |
| 292 |
get { return module.GetString(module.GenericParam.records[index].Name); } |
| 293 |
} |
| 294 |
|
| 295 |
public override Module Module |
| 296 |
{ |
| 297 |
get { return module; } |
| 298 |
} |
| 299 |
|
| 300 |
public override int MetadataToken |
| 301 |
{ |
| 302 |
get { return (GenericParamTable.Index << 24) + index + 1; } |
| 303 |
} |
| 304 |
|
| 305 |
public override int GenericParameterPosition |
| 306 |
{ |
| 307 |
get { return module.GenericParam.records[index].Number; } |
| 308 |
} |
| 309 |
|
| 310 |
public override Type DeclaringType |
| 311 |
{ |
| 312 |
get |
| 313 |
{ |
| 314 |
int owner = module.GenericParam.records[index].Owner; |
| 315 |
return (owner >> 24) == TypeDefTable.Index ? module.ResolveType(owner) : null; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
public override MethodBase DeclaringMethod |
| 320 |
{ |
| 321 |
get |
| 322 |
{ |
| 323 |
int owner = module.GenericParam.records[index].Owner; |
| 324 |
return (owner >> 24) == MethodDefTable.Index ? module.ResolveMethod(owner) : null; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
public override Type[] GetGenericParameterConstraints() |
| 329 |
{ |
| 330 |
IGenericContext context = (this.DeclaringMethod as IGenericContext) ?? this.DeclaringType; |
| 331 |
List<Type> list = new List<Type>(); |
| 332 |
foreach (int i in module.GenericParamConstraint.Filter(this.MetadataToken)) |
| 333 |
{ |
| 334 |
list.Add(module.ResolveType(module.GenericParamConstraint.records[i].Constraint, context)); |
| 335 |
} |
| 336 |
return list.ToArray(); |
| 337 |
} |
| 338 |
|
| 339 |
public override CustomModifiers[] __GetGenericParameterConstraintCustomModifiers() |
| 340 |
{ |
| 341 |
IGenericContext context = (this.DeclaringMethod as IGenericContext) ?? this.DeclaringType; |
| 342 |
List<CustomModifiers> list = new List<CustomModifiers>(); |
| 343 |
foreach (int i in module.GenericParamConstraint.Filter(this.MetadataToken)) |
| 344 |
{ |
| 345 |
CustomModifiers mods = new CustomModifiers(); |
| 346 |
int metadataToken = module.GenericParamConstraint.records[i].Constraint; |
| 347 |
if ((metadataToken >> 24) == TypeSpecTable.Index) |
| 348 |
{ |
| 349 |
int index = (metadataToken & 0xFFFFFF) - 1; |
| 350 |
mods = CustomModifiers.Read(module, module.GetBlob(module.TypeSpec.records[index]), context); |
| 351 |
} |
| 352 |
list.Add(mods); |
| 353 |
} |
| 354 |
return list.ToArray(); |
| 355 |
} |
| 356 |
|
| 357 |
public override GenericParameterAttributes GenericParameterAttributes |
| 358 |
{ |
| 359 |
get { return (GenericParameterAttributes)module.GenericParam.records[index].Flags; } |
| 360 |
} |
| 361 |
|
| 362 |
internal override Type BindTypeParameters(IGenericBinder binder) |
| 363 |
{ |
| 364 |
int owner = module.GenericParam.records[index].Owner; |
| 365 |
if ((owner >> 24) == MethodDefTable.Index) |
| 366 |
{ |
| 367 |
return binder.BindMethodParameter(this); |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
return binder.BindTypeParameter(this); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
internal override bool IsBaked |
| 376 |
{ |
| 377 |
get { return true; } |
| 378 |
} |
| 379 |
} |
| 380 |
} |