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