430f2c2c8851e6cc7a0d08e6c6ea532e8bc87ae1
[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                         if (argument.Type.FullName != "System.Type")
273                                 return;
274
275                         MarkType (argument.Type);
276                         MarkType ((TypeReference) argument.Value);
277                 }
278
279                 protected bool CheckProcessed (IMetadataTokenProvider provider)
280                 {
281                         if (Annotations.IsProcessed (provider))
282                                 return true;
283
284                         Annotations.Processed (provider);
285                         return false;
286                 }
287
288                 void MarkAssembly (AssemblyDefinition assembly)
289                 {
290                         if (CheckProcessed (assembly))
291                                 return;
292
293                         ProcessModule (assembly);
294
295                         MarkCustomAttributes (assembly);
296
297                         foreach (ModuleDefinition module in assembly.Modules)
298                                 MarkCustomAttributes (module);
299                 }
300
301                 void ProcessModule (AssemblyDefinition assembly)
302                 {
303                         // Pre-mark <Module> if there is any methods as they need to be executed 
304                         // at assembly load time
305                         foreach (TypeDefinition type in assembly.MainModule.Types)
306                         {
307                                 if (type.Name == "<Module>" && type.HasMethods)
308                                 {
309                                         MarkType (type);
310                                         break;
311                                 }
312                         }
313                 }
314
315                 protected void MarkField (FieldReference reference)
316                 {
317 //                      if (IgnoreScope (reference.DeclaringType.Scope))
318 //                              return;
319
320                         if (reference.DeclaringType is GenericInstanceType)
321                                 MarkType (reference.DeclaringType);
322
323                         FieldDefinition field = ResolveFieldDefinition (reference);
324
325                         if (field == null)
326                                 throw new ResolutionException (reference);
327
328                         if (CheckProcessed (field))
329                                 return;
330
331                         MarkType (field.DeclaringType);
332                         MarkType (field.FieldType);
333                         MarkCustomAttributes (field);
334                         MarkMarshalSpec (field);
335
336                         Annotations.Mark (field);
337                 }
338
339                 protected virtual bool IgnoreScope (IMetadataScope scope)
340                 {
341                         AssemblyDefinition assembly = ResolveAssembly (scope);
342                         return Annotations.GetAction (assembly) != AssemblyAction.Link;
343                 }
344
345                 FieldDefinition ResolveFieldDefinition (FieldReference field)
346                 {
347                         FieldDefinition fd = field as FieldDefinition;
348                         if (fd == null)
349                                 fd = field.Resolve ();
350
351                         return fd;
352                 }
353
354                 void MarkScope (IMetadataScope scope)
355                 {
356                         var provider = scope as IMetadataTokenProvider;
357                         if (provider == null)
358                                 return;
359
360                         Annotations.Mark (provider);
361                 }
362
363                 protected virtual void MarkSerializable (TypeDefinition type)
364                 {
365                         MarkDefaultConstructor (type);
366                         MarkMethodsIf (type.Methods, IsSpecialSerializationConstructorPredicate);
367                 }
368
369                 protected virtual TypeDefinition MarkType (TypeReference reference)
370                 {
371                         if (reference == null)
372                                 return null;
373
374                         reference = GetOriginalType (reference);
375
376                         if (reference is GenericParameter)
377                                 return null;
378
379 //                      if (IgnoreScope (reference.Scope))
380 //                              return;
381
382                         TypeDefinition type = ResolveTypeDefinition (reference);
383
384                         if (type == null)
385                                 throw new ResolutionException (reference);
386
387                         if (CheckProcessed (type))
388                                 return null;
389
390                         MarkScope (type.Scope);
391                         MarkType (type.BaseType);
392                         MarkType (type.DeclaringType);
393                         MarkCustomAttributes (type);
394
395                         if (IsMulticastDelegate (type)) {
396                                 MarkMethodCollection (type.Methods);
397                         }
398
399                         if (IsSerializable (type))
400                                 MarkSerializable (type);
401
402                         MarkTypeSpecialCustomAttributes (type);
403
404                         MarkGenericParameterProvider (type);
405
406                         // keep fields for value-types and for classes with LayoutKind.Sequential or Explicit
407                         if (type.IsValueType || !type.IsAutoLayout)
408                                 MarkFields (type, type.IsEnum);
409
410                         if (type.HasInterfaces) {
411                                 foreach (TypeReference iface in type.Interfaces)
412                                         MarkType (iface);
413                         }
414
415                         if (type.HasMethods) {
416                                 MarkMethodsIf (type.Methods, IsVirtualAndHasPreservedParent);
417                                 MarkMethodsIf (type.Methods, IsStaticConstructorPredicate);
418                         }
419
420                         Annotations.Mark (type);
421
422                         ApplyPreserveInfo (type);
423
424                         return type;
425                 }
426
427                 void MarkTypeSpecialCustomAttributes (TypeDefinition type)
428                 {
429                         if (!type.HasCustomAttributes)
430                                 return;
431
432                         foreach (CustomAttribute attribute in type.CustomAttributes) {
433                                 switch (attribute.Constructor.DeclaringType.FullName) {
434                                 case "System.Xml.Serialization.XmlSchemaProviderAttribute":
435                                         MarkXmlSchemaProvider (type, attribute);
436                                         break;
437                                 }
438                         }
439                 }
440
441                 void MarkMethodSpecialCustomAttributes (MethodDefinition method)
442                 {
443                         if (!method.HasCustomAttributes)
444                                 return;
445
446                         foreach (CustomAttribute attribute in method.CustomAttributes) {
447                                 switch (attribute.Constructor.DeclaringType.FullName) {
448                                 case "System.Web.Services.Protocols.SoapHeaderAttribute":
449                                         MarkSoapHeader (method, attribute);
450                                         break;
451                                 }
452                         }
453                 }
454
455                 void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
456                 {
457                         string method_name;
458                         if (!TryGetStringArgument (attribute, out method_name))
459                                 return;
460
461                         MarkNamedMethod (type, method_name);
462                 }
463
464                 static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
465                 {
466                         argument = null;
467
468                         if (attribute.ConstructorArguments.Count < 1)
469                                 return false;
470
471                         argument = attribute.ConstructorArguments [0].Value as string;
472
473                         return argument != null;
474                 }
475
476                 protected void MarkNamedMethod (TypeDefinition type, string method_name)
477                 {
478                         if (!type.HasMethods)
479                                 return;
480
481                         foreach (MethodDefinition method in type.Methods) {
482                                 if (method.Name != method_name)
483                                         continue;
484
485                                 MarkMethod (method);
486                         }
487                 }
488
489                 void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
490                 {
491                         string member_name;
492                         if (!TryGetStringArgument (attribute, out member_name))
493                                 return;
494
495                         MarkNamedField (method.DeclaringType, member_name);
496                         MarkNamedProperty (method.DeclaringType, member_name);
497                 }
498
499                 void MarkNamedField (TypeDefinition type, string field_name)
500                 {
501                         if (!type.HasFields)
502                                 return;
503
504                         foreach (FieldDefinition field in type.Fields) {
505                                 if (field.Name != field_name)
506                                         continue;
507
508                                 MarkField (field);
509                         }
510                 }
511
512                 void MarkNamedProperty (TypeDefinition type, string property_name)
513                 {
514                         if (!type.HasProperties)
515                                 return;
516
517                         foreach (PropertyDefinition property in type.Properties) {
518                                 if (property.Name != property_name)
519                                         continue;
520
521                                 MarkMethod (property.GetMethod);
522                                 MarkMethod (property.SetMethod);
523                         }
524                 }
525
526                 void MarkGenericParameterProvider (IGenericParameterProvider provider)
527                 {
528                         if (!provider.HasGenericParameters)
529                                 return;
530
531                         foreach (GenericParameter parameter in provider.GenericParameters)
532                                 MarkGenericParameter (parameter);
533                 }
534
535                 void MarkGenericParameter (GenericParameter parameter)
536                 {
537                         MarkCustomAttributes (parameter);
538                         foreach (TypeReference constraint in parameter.Constraints)
539                                 MarkType (constraint);
540                 }
541
542                 bool IsVirtualAndHasPreservedParent (MethodDefinition method)
543                 {
544                         if (!method.IsVirtual)
545                                 return false;
546
547                         var base_list = Annotations.GetBaseMethods (method);
548                         if (base_list == null)
549                                 return false;
550
551                         foreach (MethodDefinition @base in base_list) {
552                                 if (IgnoreScope (@base.DeclaringType.Scope))
553                                         return true;
554
555                                 if (IsVirtualAndHasPreservedParent (@base))
556                                         return true;
557                         }
558
559                         return false;
560                 }
561
562                 static MethodPredicate IsSpecialSerializationConstructorPredicate = new MethodPredicate (IsSpecialSerializationConstructor);
563
564                 static bool IsSpecialSerializationConstructor (MethodDefinition method)
565                 {
566                         if (!IsConstructor (method))
567                                 return false;
568
569                         var parameters = method.Parameters;
570                         if (parameters.Count != 2)
571                                 return false;
572
573                         return parameters [0].ParameterType.Name == "SerializationInfo" &&
574                                 parameters [1].ParameterType.Name == "StreamingContext";
575                 }
576
577                 delegate bool MethodPredicate (MethodDefinition method);
578
579                 void MarkMethodsIf (ICollection methods, MethodPredicate predicate)
580                 {
581                         foreach (MethodDefinition method in methods)
582                                 if (predicate (method))
583                                         MarkMethod (method);
584                 }
585
586                 static MethodPredicate IsDefaultConstructorPredicate = new MethodPredicate (IsDefaultConstructor);
587
588                 static bool IsDefaultConstructor (MethodDefinition method)
589                 {
590                         return IsConstructor (method) && !method.HasParameters;
591                 }
592
593                 static bool IsConstructor (MethodDefinition method)
594                 {
595                         return method.IsConstructor && !method.IsStatic;
596                 }
597
598                 protected void MarkDefaultConstructor (TypeDefinition type)
599                 {
600                         if ((type == null) || !type.HasMethods)
601                                 return;
602
603                         MarkMethodsIf (type.Methods, IsDefaultConstructorPredicate);
604                 }
605
606                 static MethodPredicate IsStaticConstructorPredicate = new MethodPredicate (IsStaticConstructor);
607
608                 static bool IsStaticConstructor (MethodDefinition method)
609                 {
610                         return method.IsConstructor && method.IsStatic;
611                 }
612
613                 static bool IsSerializable (TypeDefinition td)
614                 {
615                         return (td.Attributes & TypeAttributes.Serializable) != 0;
616                 }
617
618                 static bool IsMulticastDelegate (TypeDefinition td)
619                 {
620                         return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
621                 }
622
623                 protected TypeDefinition ResolveTypeDefinition (TypeReference type)
624                 {
625                         TypeDefinition td = type as TypeDefinition;
626                         if (td == null)
627                                 td = type.Resolve ();
628
629                         return td;
630                 }
631
632                 protected TypeReference GetOriginalType (TypeReference type)
633                 {
634                         while (type is TypeSpecification) {
635                                 GenericInstanceType git = type as GenericInstanceType;
636                                 if (git != null)
637                                         MarkGenericArguments (git);
638
639                                 var mod = type as IModifierType;
640                                 if (mod != null)
641                                         MarkModifierType (mod);
642
643                                 type = ((TypeSpecification) type).ElementType;
644                         }
645
646                         return type;
647                 }
648
649                 void MarkModifierType (IModifierType mod)
650                 {
651                         MarkType (mod.ModifierType);
652                 }
653
654                 void MarkGenericArguments (IGenericInstance instance)
655                 {
656                         foreach (TypeReference argument in instance.GenericArguments)
657                                 MarkType (argument);
658
659                         MarkGenericArgumentConstructors (instance);
660                 }
661
662                 void MarkGenericArgumentConstructors (IGenericInstance instance)
663                 {
664                         var arguments = instance.GenericArguments;
665
666                         var generic_element = GetGenericProviderFromInstance (instance);
667                         if (generic_element == null)
668                                 return;
669
670                         var parameters = generic_element.GenericParameters;
671
672                         if (arguments.Count != parameters.Count)
673                                 return;
674
675                         for (int i = 0; i < arguments.Count; i++) {
676                                 var argument = arguments [i];
677                                 var parameter = parameters [i];
678
679                                 if (!parameter.HasDefaultConstructorConstraint)
680                                         continue;
681
682                                 var argument_definition = ResolveTypeDefinition (argument);
683                                 if (argument_definition == null)
684                                         continue;
685
686                                 MarkMethodsIf (argument_definition.Methods, ctor => !ctor.IsStatic && !ctor.HasParameters);
687                         }
688                 }
689
690                 IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
691                 {
692                         var method = instance as GenericInstanceMethod;
693                         if (method != null)
694                                 return ResolveMethodDefinition (method.ElementMethod);
695
696                         var type = instance as GenericInstanceType;
697                         if (type != null)
698                                 return ResolveTypeDefinition (type.ElementType);
699
700                         return null;
701                 }
702
703                 void ApplyPreserveInfo (TypeDefinition type)
704                 {
705                         ApplyPreserveMethods (type);
706
707                         if (!Annotations.IsPreserved (type))
708                                 return;
709
710                         switch (Annotations.GetPreserve (type)) {
711                         case TypePreserve.All:
712                                 MarkFields (type, true);
713                                 MarkMethods (type);
714                                 break;
715                         case TypePreserve.Fields:
716                                 MarkFields (type, true);
717                                 break;
718                         case TypePreserve.Methods:
719                                 MarkMethods (type);
720                                 break;
721                         }
722                 }
723
724                 void ApplyPreserveMethods (TypeDefinition type)
725                 {
726                         var list = Annotations.GetPreservedMethods (type);
727                         if (list == null)
728                                 return;
729
730                         MarkMethodCollection (list);
731                 }
732
733                 void ApplyPreserveMethods (MethodDefinition method)
734                 {
735                         var list = Annotations.GetPreservedMethods (method);
736                         if (list == null)
737                                 return;
738
739                         MarkMethodCollection (list);
740                 }
741
742                 protected void MarkFields (TypeDefinition type, bool includeStatic)
743                 {
744                         if (!type.HasFields)
745                                 return;
746
747                         foreach (FieldDefinition field in type.Fields) {
748                                 if (!includeStatic && field.IsStatic)
749                                         continue;
750                                 MarkField (field);
751                         }
752                 }
753
754                 protected virtual void MarkMethods (TypeDefinition type)
755                 {
756                         if (type.HasMethods)
757                                 MarkMethodCollection (type.Methods);
758                 }
759
760                 void MarkMethodCollection (IEnumerable methods)
761                 {
762                         foreach (MethodDefinition method in methods)
763                                 MarkMethod (method);
764                 }
765
766                 protected virtual MethodDefinition MarkMethod (MethodReference reference)
767                 {
768                         reference = GetOriginalMethod (reference);
769
770                         if (reference.DeclaringType is ArrayType)
771                                 return null;
772
773                         if (reference.DeclaringType is GenericInstanceType)
774                                 MarkType (reference.DeclaringType);
775
776 //                      if (IgnoreScope (reference.DeclaringType.Scope))
777 //                              return;
778
779                         MethodDefinition method = ResolveMethodDefinition (reference);
780
781                         if (method == null)
782                                 throw new ResolutionException (reference);
783
784                         if (Annotations.GetAction (method) == MethodAction.Nothing)
785                                 Annotations.SetAction (method, MethodAction.Parse);
786
787                         EnqueueMethod (method);
788                         return method;
789                 }
790
791                 AssemblyDefinition ResolveAssembly (IMetadataScope scope)
792                 {
793                         AssemblyDefinition assembly = _context.Resolve (scope);
794                         MarkAssembly (assembly);
795                         return assembly;
796                 }
797
798                 protected MethodReference GetOriginalMethod (MethodReference method)
799                 {
800                         while (method is MethodSpecification) {
801                                 GenericInstanceMethod gim = method as GenericInstanceMethod;
802                                 if (gim != null)
803                                         MarkGenericArguments (gim);
804
805                                 method = ((MethodSpecification) method).ElementMethod;
806                         }
807
808                         return method;
809                 }
810
811                 MethodDefinition ResolveMethodDefinition (MethodReference method)
812                 {
813                         MethodDefinition md = method as MethodDefinition;
814                         if (md == null)
815                                 md = method.Resolve ();
816
817                         return md;
818                 }
819
820                 protected virtual void ProcessMethod (MethodDefinition method)
821                 {
822                         if (CheckProcessed (method))
823                                 return;
824
825                         MarkType (method.DeclaringType);
826                         MarkCustomAttributes (method);
827
828                         MarkGenericParameterProvider (method);
829
830                         if (IsPropertyMethod (method))
831                                 MarkProperty (GetProperty (method));
832                         else if (IsEventMethod (method))
833                                 MarkEvent (GetEvent (method));
834
835                         if (method.HasParameters) {
836                                 foreach (ParameterDefinition pd in method.Parameters) {
837                                         MarkType (pd.ParameterType);
838                                         MarkCustomAttributes (pd);
839                                         MarkMarshalSpec (pd);
840                                 }
841                         }
842
843                         if (method.HasOverrides) {
844                                 foreach (MethodReference ov in method.Overrides)
845                                         MarkMethod (ov);
846                         }
847
848                         MarkMethodSpecialCustomAttributes (method);
849
850                         if (method.IsVirtual)
851                                 _virtual_methods.Add (method);
852
853                         MarkBaseMethods (method);
854
855                         MarkType (method.ReturnType);
856                         MarkCustomAttributes (method.MethodReturnType);
857                         MarkMarshalSpec (method.MethodReturnType);
858
859                         if (ShouldParseMethodBody (method))
860                                 MarkMethodBody (method.Body);
861
862                         Annotations.Mark (method);
863
864                         ApplyPreserveMethods (method);
865                 }
866
867                 void MarkBaseMethods (MethodDefinition method)
868                 {
869                         IList base_methods = Annotations.GetBaseMethods (method);
870                         if (base_methods == null)
871                                 return;
872
873                         foreach (MethodDefinition base_method in base_methods) {
874                                 if (base_method.DeclaringType.IsInterface && !method.DeclaringType.IsInterface)
875                                         continue;
876
877                                 MarkMethod (base_method);
878                                 MarkBaseMethods (base_method);
879                         }
880                 }
881
882                 bool ShouldParseMethodBody (MethodDefinition method)
883                 {
884                         if (!method.HasBody)
885                                 return false;
886
887                         AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
888                         return (Annotations.GetAction (method) == MethodAction.ForceParse ||
889                                 (Annotations.GetAction (assembly) == AssemblyAction.Link && Annotations.GetAction (method) == MethodAction.Parse));
890                 }
891
892                 static internal bool IsPropertyMethod (MethodDefinition md)
893                 {
894                         return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
895                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
896                 }
897
898                 static bool IsEventMethod (MethodDefinition md)
899                 {
900                         return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
901                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
902                                 (md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
903                 }
904
905                 static internal PropertyDefinition GetProperty (MethodDefinition md)
906                 {
907                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
908                         foreach (PropertyDefinition prop in declaringType.Properties)
909                                 if (prop.GetMethod == md || prop.SetMethod == md)
910                                         return prop;
911
912                         return null;
913                 }
914
915                 static EventDefinition GetEvent (MethodDefinition md)
916                 {
917                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
918                         foreach (EventDefinition evt in declaringType.Events)
919                                 if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
920                                         return evt;
921
922                         return null;
923                 }
924
925                 protected void MarkProperty (PropertyDefinition prop)
926                 {
927                         MarkCustomAttributes (prop);
928                 }
929
930                 protected void MarkEvent (EventDefinition evt)
931                 {
932                         MarkCustomAttributes (evt);
933                         MarkMethodIfNotNull (evt.AddMethod);
934                         MarkMethodIfNotNull (evt.InvokeMethod);
935                         MarkMethodIfNotNull (evt.RemoveMethod);
936                 }
937
938                 void MarkMethodIfNotNull (MethodReference method)
939                 {
940                         if (method == null)
941                                 return;
942
943                         MarkMethod (method);
944                 }
945
946                 protected virtual void MarkMethodBody (MethodBody body)
947                 {
948                         foreach (VariableDefinition var in body.Variables)
949                                 MarkType (var.VariableType);
950
951                         foreach (ExceptionHandler eh in body.ExceptionHandlers)
952                                 if (eh.HandlerType == ExceptionHandlerType.Catch)
953                                         MarkType (eh.CatchType);
954
955                         foreach (Instruction instruction in body.Instructions)
956                                 MarkInstruction (instruction);
957                 }
958
959                 protected virtual void MarkInstruction (Instruction instruction)
960                 {
961                         switch (instruction.OpCode.OperandType) {
962                         case OperandType.InlineField:
963                                 MarkField ((FieldReference) instruction.Operand);
964                                 break;
965                         case OperandType.InlineMethod:
966                                 MarkMethod ((MethodReference) instruction.Operand);
967                                 break;
968                         case OperandType.InlineTok:
969                                 object token = instruction.Operand;
970                                 if (token is TypeReference)
971                                         MarkType ((TypeReference) token);
972                                 else if (token is MethodReference)
973                                         MarkMethod ((MethodReference) token);
974                                 else
975                                         MarkField ((FieldReference) token);
976                                 break;
977                         case OperandType.InlineType:
978                                 MarkType ((TypeReference) instruction.Operand);
979                                 break;
980                         default:
981                                 break;
982                         }
983                 }
984         }
985 }