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