Merge pull request #225 from mistoll/master
[mono.git] / mcs / class / IKVM.Reflection / Missing.cs
1 /*
2   Copyright (C) 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   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Runtime.InteropServices;
27
28 namespace IKVM.Reflection
29 {
30         [Serializable]
31         public sealed class MissingAssemblyException : InvalidOperationException
32         {
33                 [NonSerialized]
34                 private readonly MissingAssembly assembly;
35
36                 internal MissingAssemblyException(MissingAssembly assembly)
37                         : base("Assembly '" + assembly.FullName + "' is a missing assembly and does not support the requested operation.")
38                 {
39                         this.assembly = assembly;
40                 }
41
42                 private MissingAssemblyException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
43                         : base(info, context)
44                 {
45                 }
46
47                 public Assembly Assembly
48                 {
49                         get { return assembly; }
50                 }
51         }
52
53         [Serializable]
54         public sealed class MissingModuleException : InvalidOperationException
55         {
56                 [NonSerialized]
57                 private readonly MissingModule module;
58
59                 internal MissingModuleException(MissingModule module)
60                         : base("Module from missing assembly '" + module.Assembly.FullName + "' does not support the requested operation.")
61                 {
62                         this.module = module;
63                 }
64
65                 private MissingModuleException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
66                         : base(info, context)
67                 {
68                 }
69
70                 public Module Module
71                 {
72                         get { return module; }
73                 }
74         }
75
76         [Serializable]
77         public sealed class MissingMemberException : InvalidOperationException
78         {
79                 [NonSerialized]
80                 private readonly MemberInfo member;
81
82                 internal MissingMemberException(MemberInfo member)
83                         : base("Member '" + member + "' is a missing member and does not support the requested operation.")
84                 {
85                         this.member = member;
86                 }
87
88                 private MissingMemberException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
89                         : base(info, context)
90                 {
91                 }
92
93                 public MemberInfo MemberInfo
94                 {
95                         get { return member; }
96                 }
97         }
98
99         public struct MissingGenericMethodBuilder
100         {
101                 private readonly MissingMethod method;
102
103                 public MissingGenericMethodBuilder(Type declaringType, CallingConventions callingConvention, string name, int genericParameterCount)
104                 {
105                         method = new MissingMethod(declaringType, name, new MethodSignature(null, null, new PackedCustomModifiers(), callingConvention, genericParameterCount));
106                 }
107
108                 public Type[] GetGenericArguments()
109                 {
110                         return method.GetGenericArguments();
111                 }
112
113                 public void SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
114                 {
115                         method.signature = new MethodSignature(
116                                 returnType ?? method.Module.universe.System_Void,
117                                 Util.Copy(parameterTypes),
118                                 PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length),
119                                 method.signature.CallingConvention,
120                                 method.signature.GenericParameterCount);
121                 }
122
123                 [Obsolete("Please use SetSignature(Type, CustomModifiers, Type[], CustomModifiers[]) instead.")]
124                 public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
125                 {
126                         method.signature = new MethodSignature(
127                                 returnType ?? method.Module.universe.System_Void,
128                                 Util.Copy(parameterTypes),
129                                 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length),
130                                 method.signature.CallingConvention,
131                                 method.signature.GenericParameterCount);
132                 }
133
134                 public MethodInfo Finish()
135                 {
136                         return method;
137                 }
138         }
139
140         sealed class MissingAssembly : Assembly
141         {
142                 private readonly MissingModule module;
143
144                 internal MissingAssembly(Universe universe, string name)
145                         : base(universe)
146                 {
147                         module = new MissingModule(this);
148                         this.fullName = name;
149                 }
150
151                 public override Type[] GetTypes()
152                 {
153                         throw new MissingAssemblyException(this);
154                 }
155
156                 public override AssemblyName GetName()
157                 {
158                         return new AssemblyName(fullName);
159                 }
160
161                 public override string ImageRuntimeVersion
162                 {
163                         get { throw new MissingAssemblyException(this); }
164                 }
165
166                 public override Module ManifestModule
167                 {
168                         get { return module; }
169                 }
170
171                 public override MethodInfo EntryPoint
172                 {
173                         get { throw new MissingAssemblyException(this); }
174                 }
175
176                 public override string Location
177                 {
178                         get { throw new MissingAssemblyException(this); }
179                 }
180
181                 public override AssemblyName[] GetReferencedAssemblies()
182                 {
183                         throw new MissingAssemblyException(this);
184                 }
185
186                 public override Module[] GetModules(bool getResourceModules)
187                 {
188                         throw new MissingAssemblyException(this);
189                 }
190
191                 public override Module[] GetLoadedModules(bool getResourceModules)
192                 {
193                         throw new MissingAssemblyException(this);
194                 }
195
196                 public override Module GetModule(string name)
197                 {
198                         throw new MissingAssemblyException(this);
199                 }
200
201                 public override string[] GetManifestResourceNames()
202                 {
203                         throw new MissingAssemblyException(this);
204                 }
205
206                 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
207                 {
208                         throw new MissingAssemblyException(this);
209                 }
210
211                 public override System.IO.Stream GetManifestResourceStream(string resourceName)
212                 {
213                         throw new MissingAssemblyException(this);
214                 }
215
216                 public override bool __IsMissing
217                 {
218                         get { return true; }
219                 }
220
221                 internal override Type FindType(TypeName typeName)
222                 {
223                         return null;
224                 }
225
226                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
227                 {
228                         throw new MissingAssemblyException(this);
229                 }
230         }
231
232         sealed class MissingModule : NonPEModule
233         {
234                 private readonly MissingAssembly assembly;
235
236                 internal MissingModule(MissingAssembly assembly)
237                         : base(assembly.universe)
238                 {
239                         this.assembly = assembly;
240                 }
241
242                 public override int MDStreamVersion
243                 {
244                         get { throw new MissingModuleException(this); }
245                 }
246
247                 public override Assembly Assembly
248                 {
249                         get { return assembly; }
250                 }
251
252                 public override string FullyQualifiedName
253                 {
254                         get { throw new MissingModuleException(this); }
255                 }
256
257                 public override string Name
258                 {
259                         get { throw new MissingModuleException(this); }
260                 }
261
262                 public override Guid ModuleVersionId
263                 {
264                         get { throw new MissingModuleException(this); }
265                 }
266
267                 public override string ScopeName
268                 {
269                         get { throw new MissingModuleException(this); }
270                 }
271
272                 internal override Type FindType(TypeName typeName)
273                 {
274                         return null;
275                 }
276
277                 internal override void GetTypesImpl(System.Collections.Generic.List<Type> list)
278                 {
279                         throw new MissingModuleException(this);
280                 }
281
282                 public override void __GetDataDirectoryEntry(int index, out int rva, out int length)
283                 {
284                         throw new MissingModuleException(this);
285                 }
286
287                 public override IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
288                 {
289                         throw new MissingModuleException(this);
290                 }
291
292                 public override long __RelativeVirtualAddressToFileOffset(int rva)
293                 {
294                         throw new MissingModuleException(this);
295                 }
296
297                 public override __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
298                 {
299                         throw new MissingModuleException(this);
300                 }
301
302                 public override int __Subsystem
303                 {
304                         get { throw new MissingModuleException(this); }
305                 }
306
307                 internal override void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
308                 {
309                         throw new MissingModuleException(this);
310                 }
311
312                 public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
313                 {
314                         throw new MissingModuleException(this);
315                 }
316
317                 public override bool __IsMissing
318                 {
319                         get { return true; }
320                 }
321
322                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
323                 {
324                         throw new MissingModuleException(this);
325                 }
326
327                 protected override Exception InvalidOperationException()
328                 {
329                         return new MissingModuleException(this);
330                 }
331
332                 protected override Exception NotSupportedException()
333                 {
334                         return new MissingModuleException(this);
335                 }
336
337                 protected override Exception ArgumentOutOfRangeException()
338                 {
339                         return new MissingModuleException(this);
340                 }
341         }
342
343         sealed class MissingType : Type
344         {
345                 private readonly Module module;
346                 private readonly Type declaringType;
347                 private readonly string ns;
348                 private readonly string name;
349                 private Type[] typeArgs;
350                 private int token;
351
352                 internal MissingType(Module module, Type declaringType, string ns, string name)
353                 {
354                         this.module = module;
355                         this.declaringType = declaringType;
356                         this.ns = ns;
357                         this.name = name;
358                         MarkEnumOrValueType(ns, name);
359                 }
360
361                 internal override MethodBase FindMethod(string name, MethodSignature signature)
362                 {
363                         MethodInfo method = new MissingMethod(this, name, signature);
364                         if (name == ".ctor")
365                         {
366                                 return new ConstructorInfoImpl(method);
367                         }
368                         return method;
369                 }
370
371                 internal override FieldInfo FindField(string name, FieldSignature signature)
372                 {
373                         return new MissingField(this, name, signature);
374                 }
375
376                 internal override Type FindNestedType(TypeName name)
377                 {
378                         return null;
379                 }
380
381                 public override bool __IsMissing
382                 {
383                         get { return true; }
384                 }
385
386                 public override Type DeclaringType
387                 {
388                         get { return declaringType; }
389                 }
390
391                 public override string __Name
392                 {
393                         get { return name; }
394                 }
395
396                 public override string __Namespace
397                 {
398                         get { return ns; }
399                 }
400
401                 public override string Name
402                 {
403                         get { return TypeNameParser.Escape(name); }
404                 }
405
406                 public override string FullName
407                 {
408                         get { return GetFullName(); }
409                 }
410
411                 public override Module Module
412                 {
413                         get { return module; }
414                 }
415
416                 public override int MetadataToken
417                 {
418                         get { return token; }
419                 }
420
421                 public override bool IsValueType
422                 {
423                         get
424                         {
425                                 switch (typeFlags & (TypeFlags.ValueType | TypeFlags.NotValueType))
426                                 {
427                                         case TypeFlags.ValueType:
428                                                 return true;
429                                         case TypeFlags.NotValueType:
430                                                 return false;
431                                         default:
432                                                 throw new MissingMemberException(this);
433                                 }
434                         }
435                 }
436
437                 public override Type BaseType
438                 {
439                         get { throw new MissingMemberException(this); }
440                 }
441
442                 public override TypeAttributes Attributes
443                 {
444                         get { throw new MissingMemberException(this); }
445                 }
446
447                 public override Type[] __GetDeclaredTypes()
448                 {
449                         throw new MissingMemberException(this);
450                 }
451
452                 public override Type[] __GetDeclaredInterfaces()
453                 {
454                         throw new MissingMemberException(this);
455                 }
456
457                 public override MethodBase[] __GetDeclaredMethods()
458                 {
459                         throw new MissingMemberException(this);
460                 }
461
462                 public override __MethodImplMap __GetMethodImplMap()
463                 {
464                         throw new MissingMemberException(this);
465                 }
466
467                 public override FieldInfo[] __GetDeclaredFields()
468                 {
469                         throw new MissingMemberException(this);
470                 }
471
472                 public override EventInfo[] __GetDeclaredEvents()
473                 {
474                         throw new MissingMemberException(this);
475                 }
476
477                 public override PropertyInfo[] __GetDeclaredProperties()
478                 {
479                         throw new MissingMemberException(this);
480                 }
481
482                 public override CustomModifiers __GetCustomModifiers()
483                 {
484                         throw new MissingMemberException(this);
485                 }
486
487                 public override Type[] GetGenericArguments()
488                 {
489                         throw new MissingMemberException(this);
490                 }
491
492                 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
493                 {
494                         throw new MissingMemberException(this);
495                 }
496
497                 public override StructLayoutAttribute StructLayoutAttribute
498                 {
499                         get { throw new MissingMemberException(this); }
500                 }
501
502                 public override bool IsGenericType
503                 {
504                         get { throw new MissingMemberException(this); }
505                 }
506
507                 public override bool IsGenericTypeDefinition
508                 {
509                         get { throw new MissingMemberException(this); }
510                 }
511
512                 internal override Type GetGenericTypeArgument(int index)
513                 {
514                         if (typeArgs == null)
515                         {
516                                 typeArgs = new Type[index + 1];
517                         }
518                         else if (typeArgs.Length <= index)
519                         {
520                                 Array.Resize(ref typeArgs, index + 1);
521                         }
522                         return typeArgs[index] ?? (typeArgs[index] = new MissingTypeParameter(this, index));
523                 }
524
525                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
526                 {
527                         throw new MissingMemberException(this);
528                 }
529
530                 internal override Type BindTypeParameters(IGenericBinder binder)
531                 {
532                         return this;
533                 }
534
535                 internal override Type SetMetadataTokenForMissing(int token)
536                 {
537                         this.token = token;
538                         return this;
539                 }
540         }
541
542         sealed class MissingTypeParameter : IKVM.Reflection.Reader.TypeParameterType
543         {
544                 private readonly MemberInfo owner;
545                 private readonly int index;
546
547                 internal MissingTypeParameter(MemberInfo owner, int index)
548                 {
549                         this.owner = owner;
550                         this.index = index;
551                 }
552
553                 public override Module Module
554                 {
555                         get { return owner.Module; }
556                 }
557
558                 public override string Name
559                 {
560                         get { return null; }
561                 }
562
563                 public override int GenericParameterPosition
564                 {
565                         get { return index; }
566                 }
567
568                 public override MethodBase DeclaringMethod
569                 {
570                         get { return owner as MethodBase; }
571                 }
572
573                 public override Type DeclaringType
574                 {
575                         get { return owner as Type; }
576                 }
577
578                 internal override Type BindTypeParameters(IGenericBinder binder)
579                 {
580                         if (owner is MethodBase)
581                         {
582                                 return binder.BindMethodParameter(this);
583                         }
584                         else
585                         {
586                                 return binder.BindTypeParameter(this);
587                         }
588                 }
589         }
590
591         sealed class MissingMethod : MethodInfo
592         {
593                 private readonly Type declaringType;
594                 private readonly string name;
595                 internal MethodSignature signature;
596                 private MethodInfo forwarder;
597                 private Type[] typeArgs;
598
599                 internal MissingMethod(Type declaringType, string name, MethodSignature signature)
600                 {
601                         this.declaringType = declaringType;
602                         this.name = name;
603                         this.signature = signature;
604                 }
605
606                 private MethodInfo Forwarder
607                 {
608                         get
609                         {
610                                 MethodInfo method = TryGetForwarder();
611                                 if (method == null)
612                                 {
613                                         throw new MissingMemberException(this);
614                                 }
615                                 return method;
616                         }
617                 }
618
619                 private MethodInfo TryGetForwarder()
620                 {
621                         if (forwarder == null && !declaringType.__IsMissing)
622                         {
623                                 MethodBase mb = declaringType.FindMethod(name, signature);
624                                 ConstructorInfo ci = mb as ConstructorInfo;
625                                 if (ci != null)
626                                 {
627                                         forwarder = ci.GetMethodInfo();
628                                 }
629                                 else
630                                 {
631                                         forwarder = (MethodInfo)mb;
632                                 }
633                         }
634                         return forwarder;
635                 }
636
637                 public override bool __IsMissing
638                 {
639                         get { return TryGetForwarder() == null; }
640                 }
641
642                 public override Type ReturnType
643                 {
644                         get { return signature.GetReturnType(this); }
645                 }
646
647                 public override ParameterInfo ReturnParameter
648                 {
649                         get { return new ParameterInfoImpl(this, -1); }
650                 }
651
652                 internal override MethodSignature MethodSignature
653                 {
654                         get { return signature; }
655                 }
656
657                 internal override int ParameterCount
658                 {
659                         get { return signature.GetParameterCount(); }
660                 }
661
662                 private sealed class ParameterInfoImpl : ParameterInfo
663                 {
664                         private readonly MissingMethod method;
665                         private readonly int index;
666
667                         internal ParameterInfoImpl(MissingMethod method, int index)
668                         {
669                                 this.method = method;
670                                 this.index = index;
671                         }
672
673                         private ParameterInfo Forwarder
674                         {
675                                 get { return index == -1 ? method.Forwarder.ReturnParameter : method.Forwarder.GetParameters()[index]; }
676                         }
677
678                         public override string Name
679                         {
680                                 get { return Forwarder.Name; }
681                         }
682
683                         public override Type ParameterType
684                         {
685                                 get { return index == -1 ? method.signature.GetReturnType(method) : method.signature.GetParameterType(method, index); }
686                         }
687
688                         public override ParameterAttributes Attributes
689                         {
690                                 get { return Forwarder.Attributes; }
691                         }
692
693                         public override int Position
694                         {
695                                 get { return index; }
696                         }
697
698                         public override object RawDefaultValue
699                         {
700                                 get { return Forwarder.RawDefaultValue; }
701                         }
702
703                         public override CustomModifiers __GetCustomModifiers()
704                         {
705                                 return index == -1
706                                         ? method.signature.GetReturnTypeCustomModifiers(method)
707                                         : method.signature.GetParameterCustomModifiers(method, index);
708                         }
709
710                         public override MemberInfo Member
711                         {
712                                 get { return method; }
713                         }
714
715                         public override int MetadataToken
716                         {
717                                 get { return Forwarder.MetadataToken; }
718                         }
719
720                         internal override Module Module
721                         {
722                                 get { return method.Module; }
723                         }
724
725                         internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
726                         {
727                                 return Forwarder.GetCustomAttributesData(attributeType);
728                         }
729
730                         public override string ToString()
731                         {
732                                 return Forwarder.ToString();
733                         }
734                 }
735
736                 public override ParameterInfo[] GetParameters()
737                 {
738                         ParameterInfo[] parameters = new ParameterInfo[signature.GetParameterCount()];
739                         for (int i = 0; i < parameters.Length; i++)
740                         {
741                                 parameters[i] = new ParameterInfoImpl(this, i);
742                         }
743                         return parameters;
744                 }
745
746                 public override MethodAttributes Attributes
747                 {
748                         get { return Forwarder.Attributes; }
749                 }
750
751                 public override MethodImplAttributes GetMethodImplementationFlags()
752                 {
753                         return Forwarder.GetMethodImplementationFlags();
754                 }
755
756                 public override MethodBody GetMethodBody()
757                 {
758                         return Forwarder.GetMethodBody();
759                 }
760
761                 public override int __MethodRVA
762                 {
763                         get { return Forwarder.__MethodRVA; }
764                 }
765
766                 public override CallingConventions CallingConvention
767                 {
768                         get { return signature.CallingConvention; }
769                 }
770
771                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
772                 {
773                         MethodInfo method = TryGetForwarder();
774                         if (method != null)
775                         {
776                                 return method.ImportTo(module);
777                         }
778                         return module.ImportMethodOrField(declaringType, this.Name, this.MethodSignature);
779                 }
780
781                 public override string Name
782                 {
783                         get { return name; }
784                 }
785
786                 public override Type DeclaringType
787                 {
788                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
789                 }
790
791                 public override Module Module
792                 {
793                         get { return declaringType.Module; }
794                 }
795
796                 public override bool Equals(object obj)
797                 {
798                         MissingMethod other = obj as MissingMethod;
799                         return other != null
800                                 && other.declaringType == declaringType
801                                 && other.name == name
802                                 && other.signature.Equals(signature);
803                 }
804
805                 public override int GetHashCode()
806                 {
807                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
808                 }
809
810                 internal override MethodBase BindTypeParameters(Type type)
811                 {
812                         MethodInfo forwarder = TryGetForwarder();
813                         if (forwarder != null)
814                         {
815                                 return forwarder.BindTypeParameters(type);
816                         }
817                         return new GenericMethodInstance(type, this, null);
818                 }
819
820                 public override bool ContainsGenericParameters
821                 {
822                         get { return Forwarder.ContainsGenericParameters; }
823                 }
824
825                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
826                 {
827                         return Forwarder.GetCustomAttributesData(attributeType);
828                 }
829
830                 public override Type[] GetGenericArguments()
831                 {
832                         MethodInfo method = TryGetForwarder();
833                         if (method != null)
834                         {
835                                 return Forwarder.GetGenericArguments();
836                         }
837                         if (typeArgs == null)
838                         {
839                                 typeArgs = new Type[signature.GenericParameterCount];
840                                 for (int i = 0; i < typeArgs.Length; i++)
841                                 {
842                                         typeArgs[i] = new MissingTypeParameter(this, i);
843                                 }
844                         }
845                         return Util.Copy(typeArgs);
846                 }
847
848                 internal override Type GetGenericMethodArgument(int index)
849                 {
850                         return GetGenericArguments()[index];
851                 }
852
853                 internal override int GetGenericMethodArgumentCount()
854                 {
855                         return Forwarder.GetGenericMethodArgumentCount();
856                 }
857
858                 public override MethodInfo GetGenericMethodDefinition()
859                 {
860                         return Forwarder.GetGenericMethodDefinition();
861                 }
862
863                 internal override MethodInfo GetMethodOnTypeDefinition()
864                 {
865                         return Forwarder.GetMethodOnTypeDefinition();
866                 }
867
868                 internal override bool HasThis
869                 {
870                         get { return (signature.CallingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis; }
871                 }
872
873                 public override bool IsGenericMethod
874                 {
875                         get { return IsGenericMethodDefinition; }
876                 }
877
878                 public override bool IsGenericMethodDefinition
879                 {
880                         get { return signature.GenericParameterCount != 0; }
881                 }
882
883                 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
884                 {
885                         MethodInfo method = TryGetForwarder();
886                         if (method != null)
887                         {
888                                 return method.MakeGenericMethod(typeArguments);
889                         }
890                         return new GenericMethodInstance(declaringType, this, typeArguments);
891                 }
892
893                 public override int MetadataToken
894                 {
895                         get { return Forwarder.MetadataToken; }
896                 }
897         }
898
899         sealed class MissingField : FieldInfo
900         {
901                 private readonly Type declaringType;
902                 private readonly string name;
903                 private readonly FieldSignature signature;
904                 private FieldInfo forwarder;
905
906                 internal MissingField(Type declaringType, string name, FieldSignature signature)
907                 {
908                         this.declaringType = declaringType;
909                         this.name = name;
910                         this.signature = signature;
911                 }
912
913                 private FieldInfo Forwarder
914                 {
915                         get
916                         {
917                                 FieldInfo field = TryGetForwarder();
918                                 if (field == null)
919                                 {
920                                         throw new MissingMemberException(this);
921                                 }
922                                 return field;
923                         }
924                 }
925
926                 private FieldInfo TryGetForwarder()
927                 {
928                         if (forwarder == null && !declaringType.__IsMissing)
929                         {
930                                 forwarder = declaringType.FindField(name, signature);
931                         }
932                         return forwarder;
933                 }
934
935                 public override bool __IsMissing
936                 {
937                         get { return TryGetForwarder() == null; }
938                 }
939
940                 public override FieldAttributes Attributes
941                 {
942                         get { return Forwarder.Attributes; }
943                 }
944
945                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
946                 {
947                         Forwarder.__GetDataFromRVA(data, offset, length);
948                 }
949
950                 public override int __FieldRVA
951                 {
952                         get { return Forwarder.__FieldRVA; }
953                 }
954
955                 public override object GetRawConstantValue()
956                 {
957                         return Forwarder.GetRawConstantValue();
958                 }
959
960                 internal override FieldSignature FieldSignature
961                 {
962                         get { return signature; }
963                 }
964
965                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
966                 {
967                         FieldInfo field = TryGetForwarder();
968                         if (field != null)
969                         {
970                                 return field.ImportTo(module);
971                         }
972                         return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
973                 }
974
975                 public override string Name
976                 {
977                         get { return name; }
978                 }
979
980                 public override Type DeclaringType
981                 {
982                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
983                 }
984
985                 public override Module Module
986                 {
987                         get { return declaringType.Module; }
988                 }
989
990                 internal override FieldInfo BindTypeParameters(Type type)
991                 {
992                         FieldInfo forwarder = TryGetForwarder();
993                         if (forwarder != null)
994                         {
995                                 return forwarder.BindTypeParameters(type);
996                         }
997                         return new GenericFieldInstance(type, this);
998                 }
999
1000                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1001                 {
1002                         return Forwarder.GetCustomAttributesData(attributeType);
1003                 }
1004
1005                 public override int MetadataToken
1006                 {
1007                         get { return Forwarder.MetadataToken; }
1008                 }
1009
1010                 public override bool Equals(object obj)
1011                 {
1012                         MissingField other = obj as MissingField;
1013                         return other != null
1014                                 && other.declaringType == declaringType
1015                                 && other.name == name
1016                                 && other.signature.Equals(signature);
1017                 }
1018
1019                 public override int GetHashCode()
1020                 {
1021                         return declaringType.GetHashCode() ^ name.GetHashCode() ^ signature.GetHashCode();
1022                 }
1023
1024                 public override string ToString()
1025                 {
1026                         return this.FieldType.Name + " " + this.Name;
1027                 }
1028         }
1029
1030         // NOTE this is currently only used by CustomAttributeData (because there is no other way to refer to a property)
1031         sealed class MissingProperty : PropertyInfo
1032         {
1033                 private readonly Type declaringType;
1034                 private readonly string name;
1035                 private readonly PropertySignature signature;
1036
1037                 internal MissingProperty(Type declaringType, string name, PropertySignature signature)
1038                 {
1039                         this.declaringType = declaringType;
1040                         this.name = name;
1041                         this.signature = signature;
1042                 }
1043
1044                 public override PropertyAttributes Attributes
1045                 {
1046                         get { throw new MissingMemberException(this); }
1047                 }
1048
1049                 public override bool CanRead
1050                 {
1051                         get { throw new MissingMemberException(this); }
1052                 }
1053
1054                 public override bool CanWrite
1055                 {
1056                         get { throw new MissingMemberException(this); }
1057                 }
1058
1059                 public override MethodInfo GetGetMethod(bool nonPublic)
1060                 {
1061                         throw new MissingMemberException(this);
1062                 }
1063
1064                 public override MethodInfo GetSetMethod(bool nonPublic)
1065                 {
1066                         throw new MissingMemberException(this);
1067                 }
1068
1069                 public override MethodInfo[] GetAccessors(bool nonPublic)
1070                 {
1071                         throw new MissingMemberException(this);
1072                 }
1073
1074                 public override object GetRawConstantValue()
1075                 {
1076                         throw new MissingMemberException(this);
1077                 }
1078
1079                 internal override bool IsPublic
1080                 {
1081                         get { throw new MissingMemberException(this); }
1082                 }
1083
1084                 internal override bool IsStatic
1085                 {
1086                         get { throw new MissingMemberException(this); }
1087                 }
1088
1089                 internal override PropertySignature PropertySignature
1090                 {
1091                         get { return signature; }
1092                 }
1093
1094                 public override string Name
1095                 {
1096                         get { return name; }
1097                 }
1098
1099                 public override Type DeclaringType
1100                 {
1101                         get { return declaringType; }
1102                 }
1103
1104                 public override Module Module
1105                 {
1106                         get { return declaringType.Module; }
1107                 }
1108         }
1109 }