[linker] Ignore null type arguments in MarkWithResolvedScope
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / MarkStep.cs
1 //
2 // MarkStep.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2006 Jb Evain
8 // (C) 2007 Novell, Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.Linq;
33
34 using Mono.Cecil;
35 using Mono.Cecil.Cil;
36
37 namespace Mono.Linker.Steps {
38
39         public class MarkStep : IStep {
40
41                 protected LinkContext _context;
42                 protected Queue _methods;
43                 protected ArrayList _virtual_methods;
44
45                 public AnnotationStore Annotations {
46                         get { return _context.Annotations; }
47                 }
48
49                 public MarkStep ()
50                 {
51                         _methods = new Queue ();
52                         _virtual_methods = new ArrayList ();
53                 }
54
55                 public virtual void Process (LinkContext context)
56                 {
57                         _context = context;
58
59                         Initialize ();
60                         Process ();
61                 }
62
63                 void Initialize ()
64                 {
65                         foreach (AssemblyDefinition assembly in _context.GetAssemblies ())
66                                 InitializeAssembly (assembly);
67                 }
68
69                 protected virtual void InitializeAssembly (AssemblyDefinition assembly)
70                 {
71                         MarkAssembly (assembly);
72                         foreach (TypeDefinition type in assembly.MainModule.Types) {
73                                 if (!Annotations.IsMarked (type))
74                                         continue;
75
76                                 InitializeType (type);
77                         }
78                 }
79
80                 void InitializeType (TypeDefinition type)
81                 {
82                         MarkType (type);
83
84                         if (type.HasFields)
85                                 InitializeFields (type);
86                         if (type.HasMethods)
87                                 InitializeMethods (type.Methods);
88
89                         if (type.HasNestedTypes) {
90                                 foreach (var nested in type.NestedTypes) {
91                                         if (Annotations.IsMarked (nested))
92                                                 InitializeType (nested);
93                                 }
94                         }
95                 }
96
97                 void InitializeFields (TypeDefinition type)
98                 {
99                         foreach (FieldDefinition field in type.Fields)
100                                 if (Annotations.IsMarked (field))
101                                         MarkField (field);
102                 }
103
104                 void InitializeMethods (ICollection methods)
105                 {
106                         foreach (MethodDefinition method in methods)
107                                 if (Annotations.IsMarked (method))
108                                         EnqueueMethod (method);
109                 }
110
111                 void Process ()
112                 {
113                         if (QueueIsEmpty ())
114                                 throw new InvalidOperationException ("No entry methods");
115
116                         while (!QueueIsEmpty ()) {
117                                 ProcessQueue ();
118                                 ProcessVirtualMethods ();
119                         }
120                 }
121
122                 void ProcessQueue ()
123                 {
124                         while (!QueueIsEmpty ()) {
125                                 MethodDefinition method = (MethodDefinition) _methods.Dequeue ();
126                                 ProcessMethod (method);
127                         }
128                 }
129
130                 bool QueueIsEmpty ()
131                 {
132                         return _methods.Count == 0;
133                 }
134
135                 protected virtual void EnqueueMethod (MethodDefinition method)
136                 {
137                         _methods.Enqueue (method);
138                 }
139
140                 void ProcessVirtualMethods ()
141                 {
142                         foreach (MethodDefinition method in _virtual_methods)
143                                 ProcessVirtualMethod (method);
144                 }
145
146                 void ProcessVirtualMethod (MethodDefinition method)
147                 {
148                         IList overrides = Annotations.GetOverrides (method);
149                         if (overrides == null)
150                                 return;
151
152                         foreach (MethodDefinition @override in overrides)
153                                 ProcessOverride (@override);
154                 }
155
156                 void ProcessOverride (MethodDefinition method)
157                 {
158                         if (!Annotations.IsMarked (method.DeclaringType))
159                                 return;
160
161                         if (Annotations.IsProcessed (method))
162                                 return;
163
164                         if (Annotations.IsMarked (method))
165                                 return;
166
167                         MarkMethod (method);
168                         ProcessVirtualMethod (method);
169                 }
170
171                 void MarkMarshalSpec (IMarshalInfoProvider spec)
172                 {
173                         if (!spec.HasMarshalInfo)
174                                 return;
175
176                         var marshaler = spec.MarshalInfo as CustomMarshalInfo;
177                         if (marshaler == null)
178                                 return;
179
180                         MarkType (marshaler.ManagedType);
181                 }
182
183                 void MarkCustomAttributes (ICustomAttributeProvider provider)
184                 {
185                         if (!provider.HasCustomAttributes)
186                                 return;
187
188                         foreach (CustomAttribute ca in provider.CustomAttributes)
189                                 MarkCustomAttribute (ca);
190                 }
191
192                 protected virtual void MarkCustomAttribute (CustomAttribute ca)
193                 {
194                         MarkMethod (ca.Constructor);
195
196                         MarkCustomAttributeArguments (ca);
197
198                         TypeReference constructor_type = ca.Constructor.DeclaringType;
199                         TypeDefinition type = constructor_type.Resolve ();
200                         if (type == null)
201                                 throw new ResolutionException (constructor_type);
202
203                         MarkCustomAttributeProperties (ca, type);
204                         MarkCustomAttributeFields (ca, type);
205                 }
206
207                 void MarkCustomAttributeProperties (CustomAttribute ca, TypeDefinition attribute)
208                 {
209                         if (!ca.HasProperties)
210                                 return;
211
212                         foreach (var named_argument in ca.Properties) {
213                                 PropertyDefinition property = GetProperty (attribute, named_argument.Name);
214                                 if (property != null)
215                                         MarkMethod (property.SetMethod);
216
217                                 MarkIfType (named_argument.Argument);
218                         }
219                 }
220
221                 PropertyDefinition GetProperty (TypeDefinition type, string propertyname)
222                 {
223                         while (type != null) {
224                                 PropertyDefinition property = type.Properties.FirstOrDefault (p => p.Name == propertyname);
225                                 if (property != null)
226                                         return property;
227
228                                 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
229                         }
230
231                         return null;
232                 }
233
234                 void MarkCustomAttributeFields (CustomAttribute ca, TypeDefinition attribute)
235                 {
236                         if (!ca.HasFields)
237                                 return;
238
239                         foreach (var named_argument in ca.Fields) {
240                                 FieldDefinition field = GetField (attribute, named_argument.Name);
241                                 if (field != null)
242                                         MarkField (field);
243
244                                 MarkIfType (named_argument.Argument);
245                         }
246                 }
247
248                 FieldDefinition GetField (TypeDefinition type, string fieldname)
249                 {
250                         while (type != null) {
251                                 FieldDefinition field = type.Fields.FirstOrDefault (f => f.Name == fieldname);
252                                 if (field != null)
253                                         return field;
254
255                                 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
256                         }
257
258                         return null;
259                 }
260
261                 void MarkCustomAttributeArguments (CustomAttribute ca)
262                 {
263                         if (!ca.HasConstructorArguments)
264                                 return;
265
266                         foreach (var argument in ca.ConstructorArguments)
267                                 MarkIfType (argument);
268                 }
269
270                 void MarkIfType (CustomAttributeArgument argument)
271                 {
272                         var at = argument.Type;
273                         if (at.IsArray) {
274                                 var et = at.GetElementType ();
275                                 if (et.Namespace != "System" || et.Name != "Type")
276                                         return;
277
278                                 MarkType (et);
279                                 foreach (var cac in (CustomAttributeArgument[]) argument.Value)
280                                         MarkWithResolvedScope ((TypeReference) cac.Value);
281                         } else if (at.Namespace == "System" && at.Name == "Type") {
282                                 MarkType (argument.Type);
283                                 MarkWithResolvedScope ((TypeReference) argument.Value);
284                         }
285                 }
286
287                 // custom attributes encoding means it's possible to have a scope that will point into a PCL facade
288                 // even if we (just before saving) will resolve all type references (bug #26752)
289                 void MarkWithResolvedScope (TypeReference type)
290                 {
291                         if (type == null)
292                                 return;
293                         var td = type.Resolve ();
294                         if (td != null)
295                                 type.Scope = td.Scope;
296                         MarkType (type);
297                 }
298
299                 protected bool CheckProcessed (IMetadataTokenProvider provider)
300                 {
301                         if (Annotations.IsProcessed (provider))
302                                 return true;
303
304                         Annotations.Processed (provider);
305                         return false;
306                 }
307
308                 void MarkAssembly (AssemblyDefinition assembly)
309                 {
310                         if (CheckProcessed (assembly))
311                                 return;
312
313                         ProcessModule (assembly);
314
315                         MarkCustomAttributes (assembly);
316
317                         foreach (ModuleDefinition module in assembly.Modules)
318                                 MarkCustomAttributes (module);
319                 }
320
321                 void ProcessModule (AssemblyDefinition assembly)
322                 {
323                         // Pre-mark <Module> if there is any methods as they need to be executed 
324                         // at assembly load time
325                         foreach (TypeDefinition type in assembly.MainModule.Types)
326                         {
327                                 if (type.Name == "<Module>" && type.HasMethods)
328                                 {
329                                         MarkType (type);
330                                         break;
331                                 }
332                         }
333                 }
334
335                 protected void MarkField (FieldReference reference)
336                 {
337 //                      if (IgnoreScope (reference.DeclaringType.Scope))
338 //                              return;
339
340                         if (reference.DeclaringType is GenericInstanceType)
341                                 MarkType (reference.DeclaringType);
342
343                         FieldDefinition field = ResolveFieldDefinition (reference);
344
345                         if (field == null)
346                                 throw new ResolutionException (reference);
347
348                         if (CheckProcessed (field))
349                                 return;
350
351                         MarkType (field.DeclaringType);
352                         MarkType (field.FieldType);
353                         MarkCustomAttributes (field);
354                         MarkMarshalSpec (field);
355
356                         Annotations.Mark (field);
357                 }
358
359                 protected virtual bool IgnoreScope (IMetadataScope scope)
360                 {
361                         AssemblyDefinition assembly = ResolveAssembly (scope);
362                         return Annotations.GetAction (assembly) != AssemblyAction.Link;
363                 }
364
365                 FieldDefinition ResolveFieldDefinition (FieldReference field)
366                 {
367                         FieldDefinition fd = field as FieldDefinition;
368                         if (fd == null)
369                                 fd = field.Resolve ();
370
371                         return fd;
372                 }
373
374                 void MarkScope (IMetadataScope scope)
375                 {
376                         var provider = scope as IMetadataTokenProvider;
377                         if (provider == null)
378                                 return;
379
380                         Annotations.Mark (provider);
381                 }
382
383                 protected virtual void MarkSerializable (TypeDefinition type)
384                 {
385                         MarkDefaultConstructor (type);
386                         MarkMethodsIf (type.Methods, IsSpecialSerializationConstructorPredicate);
387                 }
388
389                 protected virtual TypeDefinition MarkType (TypeReference reference)
390                 {
391                         if (reference == null)
392                                 return null;
393
394                         reference = GetOriginalType (reference);
395
396                         if (reference is GenericParameter)
397                                 return null;
398
399 //                      if (IgnoreScope (reference.Scope))
400 //                              return;
401
402                         TypeDefinition type = ResolveTypeDefinition (reference);
403
404                         if (type == null)
405                                 throw new ResolutionException (reference);
406
407                         if (CheckProcessed (type))
408                                 return null;
409
410                         MarkScope (type.Scope);
411                         MarkType (type.BaseType);
412                         MarkType (type.DeclaringType);
413                         MarkCustomAttributes (type);
414
415                         if (IsMulticastDelegate (type)) {
416                                 MarkMethodCollection (type.Methods);
417                         }
418
419                         if (IsSerializable (type))
420                                 MarkSerializable (type);
421
422                         MarkTypeSpecialCustomAttributes (type);
423
424                         MarkGenericParameterProvider (type);
425
426                         // keep fields for value-types and for classes with LayoutKind.Sequential or Explicit
427                         if (type.IsValueType || !type.IsAutoLayout)
428                                 MarkFields (type, type.IsEnum);
429
430                         if (type.HasInterfaces) {
431                                 foreach (TypeReference iface in type.Interfaces)
432                                         MarkType (iface);
433                         }
434
435                         if (type.HasMethods) {
436                                 MarkMethodsIf (type.Methods, IsVirtualAndHasPreservedParent);
437                                 MarkMethodsIf (type.Methods, IsStaticConstructorPredicate);
438                         }
439
440                         Annotations.Mark (type);
441
442                         ApplyPreserveInfo (type);
443
444                         return type;
445                 }
446
447                 void MarkTypeSpecialCustomAttributes (TypeDefinition type)
448                 {
449                         if (!type.HasCustomAttributes)
450                                 return;
451
452                         foreach (CustomAttribute attribute in type.CustomAttributes) {
453                                 switch (attribute.Constructor.DeclaringType.FullName) {
454                                 case "System.Xml.Serialization.XmlSchemaProviderAttribute":
455                                         MarkXmlSchemaProvider (type, attribute);
456                                         break;
457                                 }
458                         }
459                 }
460
461                 void MarkMethodSpecialCustomAttributes (MethodDefinition method)
462                 {
463                         if (!method.HasCustomAttributes)
464                                 return;
465
466                         foreach (CustomAttribute attribute in method.CustomAttributes) {
467                                 switch (attribute.Constructor.DeclaringType.FullName) {
468                                 case "System.Web.Services.Protocols.SoapHeaderAttribute":
469                                         MarkSoapHeader (method, attribute);
470                                         break;
471                                 }
472                         }
473                 }
474
475                 void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
476                 {
477                         string method_name;
478                         if (!TryGetStringArgument (attribute, out method_name))
479                                 return;
480
481                         MarkNamedMethod (type, method_name);
482                 }
483
484                 static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
485                 {
486                         argument = null;
487
488                         if (attribute.ConstructorArguments.Count < 1)
489                                 return false;
490
491                         argument = attribute.ConstructorArguments [0].Value as string;
492
493                         return argument != null;
494                 }
495
496                 protected void MarkNamedMethod (TypeDefinition type, string method_name)
497                 {
498                         if (!type.HasMethods)
499                                 return;
500
501                         foreach (MethodDefinition method in type.Methods) {
502                                 if (method.Name != method_name)
503                                         continue;
504
505                                 MarkMethod (method);
506                         }
507                 }
508
509                 void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
510                 {
511                         string member_name;
512                         if (!TryGetStringArgument (attribute, out member_name))
513                                 return;
514
515                         MarkNamedField (method.DeclaringType, member_name);
516                         MarkNamedProperty (method.DeclaringType, member_name);
517                 }
518
519                 void MarkNamedField (TypeDefinition type, string field_name)
520                 {
521                         if (!type.HasFields)
522                                 return;
523
524                         foreach (FieldDefinition field in type.Fields) {
525                                 if (field.Name != field_name)
526                                         continue;
527
528                                 MarkField (field);
529                         }
530                 }
531
532                 void MarkNamedProperty (TypeDefinition type, string property_name)
533                 {
534                         if (!type.HasProperties)
535                                 return;
536
537                         foreach (PropertyDefinition property in type.Properties) {
538                                 if (property.Name != property_name)
539                                         continue;
540
541                                 MarkMethod (property.GetMethod);
542                                 MarkMethod (property.SetMethod);
543                         }
544                 }
545
546                 void MarkGenericParameterProvider (IGenericParameterProvider provider)
547                 {
548                         if (!provider.HasGenericParameters)
549                                 return;
550
551                         foreach (GenericParameter parameter in provider.GenericParameters)
552                                 MarkGenericParameter (parameter);
553                 }
554
555                 void MarkGenericParameter (GenericParameter parameter)
556                 {
557                         MarkCustomAttributes (parameter);
558                         foreach (TypeReference constraint in parameter.Constraints)
559                                 MarkType (constraint);
560                 }
561
562                 bool IsVirtualAndHasPreservedParent (MethodDefinition method)
563                 {
564                         if (!method.IsVirtual)
565                                 return false;
566
567                         var base_list = Annotations.GetBaseMethods (method);
568                         if (base_list == null)
569                                 return false;
570
571                         foreach (MethodDefinition @base in base_list) {
572                                 if (IgnoreScope (@base.DeclaringType.Scope))
573                                         return true;
574
575                                 if (IsVirtualAndHasPreservedParent (@base))
576                                         return true;
577                         }
578
579                         return false;
580                 }
581
582                 static MethodPredicate IsSpecialSerializationConstructorPredicate = new MethodPredicate (IsSpecialSerializationConstructor);
583
584                 static bool IsSpecialSerializationConstructor (MethodDefinition method)
585                 {
586                         if (!IsConstructor (method))
587                                 return false;
588
589                         var parameters = method.Parameters;
590                         if (parameters.Count != 2)
591                                 return false;
592
593                         return parameters [0].ParameterType.Name == "SerializationInfo" &&
594                                 parameters [1].ParameterType.Name == "StreamingContext";
595                 }
596
597                 delegate bool MethodPredicate (MethodDefinition method);
598
599                 void MarkMethodsIf (ICollection methods, MethodPredicate predicate)
600                 {
601                         foreach (MethodDefinition method in methods)
602                                 if (predicate (method))
603                                         MarkMethod (method);
604                 }
605
606                 static MethodPredicate IsDefaultConstructorPredicate = new MethodPredicate (IsDefaultConstructor);
607
608                 static bool IsDefaultConstructor (MethodDefinition method)
609                 {
610                         return IsConstructor (method) && !method.HasParameters;
611                 }
612
613                 static bool IsConstructor (MethodDefinition method)
614                 {
615                         return method.IsConstructor && !method.IsStatic;
616                 }
617
618                 protected void MarkDefaultConstructor (TypeDefinition type)
619                 {
620                         if ((type == null) || !type.HasMethods)
621                                 return;
622
623                         MarkMethodsIf (type.Methods, IsDefaultConstructorPredicate);
624                 }
625
626                 static MethodPredicate IsStaticConstructorPredicate = new MethodPredicate (IsStaticConstructor);
627
628                 static bool IsStaticConstructor (MethodDefinition method)
629                 {
630                         return method.IsConstructor && method.IsStatic;
631                 }
632
633                 static bool IsSerializable (TypeDefinition td)
634                 {
635                         return (td.Attributes & TypeAttributes.Serializable) != 0;
636                 }
637
638                 static bool IsMulticastDelegate (TypeDefinition td)
639                 {
640                         return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
641                 }
642
643                 protected TypeDefinition ResolveTypeDefinition (TypeReference type)
644                 {
645                         TypeDefinition td = type as TypeDefinition;
646                         if (td == null)
647                                 td = type.Resolve ();
648
649                         return td;
650                 }
651
652                 protected TypeReference GetOriginalType (TypeReference type)
653                 {
654                         while (type is TypeSpecification) {
655                                 GenericInstanceType git = type as GenericInstanceType;
656                                 if (git != null)
657                                         MarkGenericArguments (git);
658
659                                 var mod = type as IModifierType;
660                                 if (mod != null)
661                                         MarkModifierType (mod);
662
663                                 type = ((TypeSpecification) type).ElementType;
664                         }
665
666                         return type;
667                 }
668
669                 void MarkModifierType (IModifierType mod)
670                 {
671                         MarkType (mod.ModifierType);
672                 }
673
674                 void MarkGenericArguments (IGenericInstance instance)
675                 {
676                         foreach (TypeReference argument in instance.GenericArguments)
677                                 MarkType (argument);
678
679                         MarkGenericArgumentConstructors (instance);
680                 }
681
682                 void MarkGenericArgumentConstructors (IGenericInstance instance)
683                 {
684                         var arguments = instance.GenericArguments;
685
686                         var generic_element = GetGenericProviderFromInstance (instance);
687                         if (generic_element == null)
688                                 return;
689
690                         var parameters = generic_element.GenericParameters;
691
692                         if (arguments.Count != parameters.Count)
693                                 return;
694
695                         for (int i = 0; i < arguments.Count; i++) {
696                                 var argument = arguments [i];
697                                 var parameter = parameters [i];
698
699                                 if (!parameter.HasDefaultConstructorConstraint)
700                                         continue;
701
702                                 var argument_definition = ResolveTypeDefinition (argument);
703                                 if (argument_definition == null)
704                                         continue;
705
706                                 MarkMethodsIf (argument_definition.Methods, ctor => !ctor.IsStatic && !ctor.HasParameters);
707                         }
708                 }
709
710                 IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
711                 {
712                         var method = instance as GenericInstanceMethod;
713                         if (method != null)
714                                 return ResolveMethodDefinition (method.ElementMethod);
715
716                         var type = instance as GenericInstanceType;
717                         if (type != null)
718                                 return ResolveTypeDefinition (type.ElementType);
719
720                         return null;
721                 }
722
723                 void ApplyPreserveInfo (TypeDefinition type)
724                 {
725                         ApplyPreserveMethods (type);
726
727                         if (!Annotations.IsPreserved (type))
728                                 return;
729
730                         switch (Annotations.GetPreserve (type)) {
731                         case TypePreserve.All:
732                                 MarkFields (type, true);
733                                 MarkMethods (type);
734                                 break;
735                         case TypePreserve.Fields:
736                                 MarkFields (type, true);
737                                 break;
738                         case TypePreserve.Methods:
739                                 MarkMethods (type);
740                                 break;
741                         }
742                 }
743
744                 void ApplyPreserveMethods (TypeDefinition type)
745                 {
746                         var list = Annotations.GetPreservedMethods (type);
747                         if (list == null)
748                                 return;
749
750                         MarkMethodCollection (list);
751                 }
752
753                 void ApplyPreserveMethods (MethodDefinition method)
754                 {
755                         var list = Annotations.GetPreservedMethods (method);
756                         if (list == null)
757                                 return;
758
759                         MarkMethodCollection (list);
760                 }
761
762                 protected void MarkFields (TypeDefinition type, bool includeStatic)
763                 {
764                         if (!type.HasFields)
765                                 return;
766
767                         foreach (FieldDefinition field in type.Fields) {
768                                 if (!includeStatic && field.IsStatic)
769                                         continue;
770                                 MarkField (field);
771                         }
772                 }
773
774                 protected virtual void MarkMethods (TypeDefinition type)
775                 {
776                         if (type.HasMethods)
777                                 MarkMethodCollection (type.Methods);
778                 }
779
780                 void MarkMethodCollection (IEnumerable methods)
781                 {
782                         foreach (MethodDefinition method in methods)
783                                 MarkMethod (method);
784                 }
785
786                 protected virtual MethodDefinition MarkMethod (MethodReference reference)
787                 {
788                         reference = GetOriginalMethod (reference);
789
790                         if (reference.DeclaringType is ArrayType)
791                                 return null;
792
793                         if (reference.DeclaringType is GenericInstanceType)
794                                 MarkType (reference.DeclaringType);
795
796 //                      if (IgnoreScope (reference.DeclaringType.Scope))
797 //                              return;
798
799                         MethodDefinition method = ResolveMethodDefinition (reference);
800
801                         if (method == null)
802                                 throw new ResolutionException (reference);
803
804                         if (Annotations.GetAction (method) == MethodAction.Nothing)
805                                 Annotations.SetAction (method, MethodAction.Parse);
806
807                         EnqueueMethod (method);
808                         return method;
809                 }
810
811                 AssemblyDefinition ResolveAssembly (IMetadataScope scope)
812                 {
813                         AssemblyDefinition assembly = _context.Resolve (scope);
814                         MarkAssembly (assembly);
815                         return assembly;
816                 }
817
818                 protected MethodReference GetOriginalMethod (MethodReference method)
819                 {
820                         while (method is MethodSpecification) {
821                                 GenericInstanceMethod gim = method as GenericInstanceMethod;
822                                 if (gim != null)
823                                         MarkGenericArguments (gim);
824
825                                 method = ((MethodSpecification) method).ElementMethod;
826                         }
827
828                         return method;
829                 }
830
831                 MethodDefinition ResolveMethodDefinition (MethodReference method)
832                 {
833                         MethodDefinition md = method as MethodDefinition;
834                         if (md == null)
835                                 md = method.Resolve ();
836
837                         return md;
838                 }
839
840                 protected virtual void ProcessMethod (MethodDefinition method)
841                 {
842                         if (CheckProcessed (method))
843                                 return;
844
845                         MarkType (method.DeclaringType);
846                         MarkCustomAttributes (method);
847
848                         MarkGenericParameterProvider (method);
849
850                         if (IsPropertyMethod (method))
851                                 MarkProperty (GetProperty (method));
852                         else if (IsEventMethod (method))
853                                 MarkEvent (GetEvent (method));
854
855                         if (method.HasParameters) {
856                                 foreach (ParameterDefinition pd in method.Parameters) {
857                                         MarkType (pd.ParameterType);
858                                         MarkCustomAttributes (pd);
859                                         MarkMarshalSpec (pd);
860                                 }
861                         }
862
863                         if (method.HasOverrides) {
864                                 foreach (MethodReference ov in method.Overrides)
865                                         MarkMethod (ov);
866                         }
867
868                         MarkMethodSpecialCustomAttributes (method);
869
870                         if (method.IsVirtual)
871                                 _virtual_methods.Add (method);
872
873                         MarkBaseMethods (method);
874
875                         MarkType (method.ReturnType);
876                         MarkCustomAttributes (method.MethodReturnType);
877                         MarkMarshalSpec (method.MethodReturnType);
878
879                         if (ShouldParseMethodBody (method))
880                                 MarkMethodBody (method.Body);
881
882                         Annotations.Mark (method);
883
884                         ApplyPreserveMethods (method);
885                 }
886
887                 void MarkBaseMethods (MethodDefinition method)
888                 {
889                         IList base_methods = Annotations.GetBaseMethods (method);
890                         if (base_methods == null)
891                                 return;
892
893                         foreach (MethodDefinition base_method in base_methods) {
894                                 if (base_method.DeclaringType.IsInterface && !method.DeclaringType.IsInterface)
895                                         continue;
896
897                                 MarkMethod (base_method);
898                                 MarkBaseMethods (base_method);
899                         }
900                 }
901
902                 bool ShouldParseMethodBody (MethodDefinition method)
903                 {
904                         if (!method.HasBody)
905                                 return false;
906
907                         AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
908                         return (Annotations.GetAction (method) == MethodAction.ForceParse ||
909                                 (Annotations.GetAction (assembly) == AssemblyAction.Link && Annotations.GetAction (method) == MethodAction.Parse));
910                 }
911
912                 static internal bool IsPropertyMethod (MethodDefinition md)
913                 {
914                         return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
915                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
916                 }
917
918                 static bool IsEventMethod (MethodDefinition md)
919                 {
920                         return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
921                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
922                                 (md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
923                 }
924
925                 static internal PropertyDefinition GetProperty (MethodDefinition md)
926                 {
927                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
928                         foreach (PropertyDefinition prop in declaringType.Properties)
929                                 if (prop.GetMethod == md || prop.SetMethod == md)
930                                         return prop;
931
932                         return null;
933                 }
934
935                 static EventDefinition GetEvent (MethodDefinition md)
936                 {
937                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
938                         foreach (EventDefinition evt in declaringType.Events)
939                                 if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
940                                         return evt;
941
942                         return null;
943                 }
944
945                 protected void MarkProperty (PropertyDefinition prop)
946                 {
947                         MarkCustomAttributes (prop);
948                 }
949
950                 protected void MarkEvent (EventDefinition evt)
951                 {
952                         MarkCustomAttributes (evt);
953                         MarkMethodIfNotNull (evt.AddMethod);
954                         MarkMethodIfNotNull (evt.InvokeMethod);
955                         MarkMethodIfNotNull (evt.RemoveMethod);
956                 }
957
958                 void MarkMethodIfNotNull (MethodReference method)
959                 {
960                         if (method == null)
961                                 return;
962
963                         MarkMethod (method);
964                 }
965
966                 protected virtual void MarkMethodBody (MethodBody body)
967                 {
968                         foreach (VariableDefinition var in body.Variables)
969                                 MarkType (var.VariableType);
970
971                         foreach (ExceptionHandler eh in body.ExceptionHandlers)
972                                 if (eh.HandlerType == ExceptionHandlerType.Catch)
973                                         MarkType (eh.CatchType);
974
975                         foreach (Instruction instruction in body.Instructions)
976                                 MarkInstruction (instruction);
977                 }
978
979                 protected virtual void MarkInstruction (Instruction instruction)
980                 {
981                         switch (instruction.OpCode.OperandType) {
982                         case OperandType.InlineField:
983                                 MarkField ((FieldReference) instruction.Operand);
984                                 break;
985                         case OperandType.InlineMethod:
986                                 MarkMethod ((MethodReference) instruction.Operand);
987                                 break;
988                         case OperandType.InlineTok:
989                                 object token = instruction.Operand;
990                                 if (token is TypeReference)
991                                         MarkType ((TypeReference) token);
992                                 else if (token is MethodReference)
993                                         MarkMethod ((MethodReference) token);
994                                 else
995                                         MarkField ((FieldReference) token);
996                                 break;
997                         case OperandType.InlineType:
998                                 MarkType ((TypeReference) instruction.Operand);
999                                 break;
1000                         default:
1001                                 break;
1002                         }
1003                 }
1004         }
1005 }