More linker fixes for moonlight
[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                 LinkContext _context;
42                 Queue _methods;
43                 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 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                                         InitializeType (nested);
92                 }
93
94                 void InitializeFields (TypeDefinition type)
95                 {
96                         foreach (FieldDefinition field in type.Fields)
97                                 if (Annotations.IsMarked (field))
98                                         MarkField (field);
99                 }
100
101                 void InitializeMethods (ICollection methods)
102                 {
103                         foreach (MethodDefinition method in methods)
104                                 if (Annotations.IsMarked (method))
105                                         EnqueueMethod (method);
106                 }
107
108                 void Process ()
109                 {
110                         if (QueueIsEmpty ())
111                                 throw new InvalidOperationException ("No entry methods");
112
113                         while (!QueueIsEmpty ()) {
114                                 ProcessQueue ();
115                                 ProcessVirtualMethods ();
116                         }
117                 }
118
119                 void ProcessQueue ()
120                 {
121                         while (!QueueIsEmpty ()) {
122                                 MethodDefinition method = (MethodDefinition) _methods.Dequeue ();
123                                 ProcessMethod (method);
124                         }
125                 }
126
127                 bool QueueIsEmpty ()
128                 {
129                         return _methods.Count == 0;
130                 }
131
132                 protected virtual void EnqueueMethod (MethodDefinition method)
133                 {
134                         _methods.Enqueue (method);
135                 }
136
137                 void ProcessVirtualMethods ()
138                 {
139                         foreach (MethodDefinition method in _virtual_methods)
140                                 ProcessVirtualMethod (method);
141                 }
142
143                 void ProcessVirtualMethod (MethodDefinition method)
144                 {
145                         IList overrides = Annotations.GetOverrides (method);
146                         if (overrides == null)
147                                 return;
148
149                         foreach (MethodDefinition @override in overrides)
150                                 ProcessOverride (@override);
151                 }
152
153                 void ProcessOverride (MethodDefinition method)
154                 {
155                         if (!Annotations.IsMarked (method.DeclaringType))
156                                 return;
157
158                         if (Annotations.IsProcessed (method))
159                                 return;
160
161                         if (Annotations.IsMarked (method))
162                                 return;
163
164                         MarkMethod (method);
165                         ProcessVirtualMethod (method);
166                 }
167
168                 void MarkMarshalSpec (IMarshalInfoProvider spec)
169                 {
170                         if (!spec.HasMarshalInfo)
171                                 return;
172
173                         var marshaler = spec.MarshalInfo as CustomMarshalInfo;
174                         if (marshaler == null)
175                                 return;
176
177                         MarkType (marshaler.ManagedType);
178                 }
179
180                 void MarkCustomAttributes (ICustomAttributeProvider provider)
181                 {
182                         if (!provider.HasCustomAttributes)
183                                 return;
184
185                         foreach (CustomAttribute ca in provider.CustomAttributes)
186                                 MarkCustomAttribute (ca);
187                 }
188
189                 void MarkCustomAttribute (CustomAttribute ca)
190                 {
191                         MarkMethod (ca.Constructor);
192
193                         MarkCustomAttributeArguments (ca);
194
195                         TypeReference constructor_type = ca.Constructor.DeclaringType;
196                         TypeDefinition type = constructor_type.Resolve ();
197                         if (type == null)
198                                 throw new ResolutionException (constructor_type);
199
200                         MarkCustomAttributeProperties (ca, type);
201                         MarkCustomAttributeFields (ca, type);
202                 }
203
204                 void MarkCustomAttributeProperties (CustomAttribute ca, TypeDefinition attribute)
205                 {
206                         foreach (var named_argument in ca.Properties) {
207                                 PropertyDefinition property = GetProperty (attribute, named_argument.Name);
208                                 if (property != null)
209                                         MarkMethod (property.SetMethod);
210
211                                 MarkIfType (named_argument.Argument);
212                         }
213                 }
214
215                 PropertyDefinition GetProperty (TypeDefinition type, string propertyname)
216                 {
217                         while (type != null) {
218                                 PropertyDefinition property = type.Properties.FirstOrDefault (p => p.Name == propertyname);
219                                 if (property != null)
220                                         return property;
221
222                                 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
223                         }
224
225                         return null;
226                 }
227
228                 void MarkCustomAttributeFields (CustomAttribute ca, TypeDefinition attribute)
229                 {
230                         foreach (var named_argument in ca.Fields) {
231                                 FieldDefinition field = GetField (attribute, named_argument.Name);
232                                 if (field != null)
233                                         MarkField (field);
234
235                                 MarkIfType (named_argument.Argument);
236                         }
237                 }
238
239                 FieldDefinition GetField (TypeDefinition type, string fieldname)
240                 {
241                         while (type != null) {
242                                 FieldDefinition field = type.Fields.FirstOrDefault (f => f.Name == fieldname);
243                                 if (field != null)
244                                         return field;
245
246                                 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
247                         }
248
249                         return null;
250                 }
251
252                 void MarkCustomAttributeArguments (CustomAttribute ca)
253                 {
254                         foreach (var argument in ca.ConstructorArguments)
255                                 MarkIfType (argument);
256                 }
257
258                 void MarkIfType (CustomAttributeArgument argument)
259                 {
260                         if (argument.Type.FullName != "System.Type")
261                                 return;
262
263                         MarkType (argument.Type);
264                         MarkType ((TypeReference) argument.Value);
265                 }
266
267                 protected bool CheckProcessed (IMetadataTokenProvider provider)
268                 {
269                         if (Annotations.IsProcessed (provider))
270                                 return true;
271
272                         Annotations.Processed (provider);
273                         return false;
274                 }
275
276                 void MarkAssembly (AssemblyDefinition assembly)
277                 {
278                         if (CheckProcessed (assembly))
279                                 return;
280
281                         MarkCustomAttributes (assembly);
282
283                         foreach (ModuleDefinition module in assembly.Modules)
284                                 MarkCustomAttributes (module);
285                 }
286
287                 void MarkField (FieldReference reference)
288                 {
289 //                      if (IgnoreScope (reference.DeclaringType.Scope))
290 //                              return;
291
292                         FieldDefinition field = ResolveFieldDefinition (reference);
293
294                         if (field == null)
295                                 throw new ResolutionException (reference);
296
297                         if (CheckProcessed (field))
298                                 return;
299
300                         MarkType (field.DeclaringType);
301                         MarkType (field.FieldType);
302                         MarkCustomAttributes (field);
303                         MarkMarshalSpec (field);
304
305                         Annotations.Mark (field);
306                 }
307
308                 protected virtual bool IgnoreScope (IMetadataScope scope)
309                 {
310                         AssemblyDefinition assembly = ResolveAssembly (scope);
311                         return Annotations.GetAction (assembly) != AssemblyAction.Link;
312                 }
313
314                 FieldDefinition ResolveFieldDefinition (FieldReference field)
315                 {
316                         FieldDefinition fd = field as FieldDefinition;
317                         if (fd == null)
318                                 fd = field.Resolve ();
319
320                         return fd;
321                 }
322
323                 void MarkScope (IMetadataScope scope)
324                 {
325                         var provider = scope as IMetadataTokenProvider;
326                         if (provider == null)
327                                 return;
328
329                         Annotations.Mark (provider);
330                 }
331
332                 protected virtual void MarkType (TypeReference reference)
333                 {
334                         if (reference == null)
335                                 return;
336
337                         reference = GetOriginalType (reference);
338
339                         if (reference is GenericParameter)
340                                 return;
341
342 //                      if (IgnoreScope (reference.Scope))
343 //                              return;
344
345                         TypeDefinition type = ResolveTypeDefinition (reference);
346
347                         if (type == null)
348                                 throw new ResolutionException (reference);
349
350                         if (CheckProcessed (type))
351                                 return;
352
353                         MarkScope (type.Scope);
354                         MarkType (type.BaseType);
355                         MarkType (type.DeclaringType);
356                         MarkCustomAttributes (type);
357
358                         if (IsMulticastDelegate (type)) {
359                                 MarkMethodCollection (type.Methods);
360                         }
361
362                         if (IsSerializable (type) && type.HasMethods) {
363                                 MarkMethodsIf (type.Methods, IsDefaultConstructorPredicate);
364                                 MarkMethodsIf (type.Methods, IsSpecialSerializationConstructorPredicate);
365                         }
366
367                         MarkTypeSpecialCustomAttributes (type);
368
369                         MarkGenericParameterProvider (type);
370
371                         if (type.IsValueType)
372                                 MarkFields (type);
373
374                         if (type.HasInterfaces) {
375                                 foreach (TypeReference iface in type.Interfaces)
376                                         MarkType (iface);
377                         }
378
379                         if (type.HasMethods) {
380                                 MarkMethodsIf (type.Methods, IsVirtualAndHasPreservedParent);
381                                 MarkMethodsIf (type.Methods, IsStaticConstructorPredicate);
382                         }
383
384                         Annotations.Mark (type);
385
386                         ApplyPreserveInfo (type);
387                 }
388
389                 void MarkTypeSpecialCustomAttributes (TypeDefinition type)
390                 {
391                         if (!type.HasCustomAttributes)
392                                 return;
393
394                         foreach (CustomAttribute attribute in type.CustomAttributes) {
395                                 switch (attribute.Constructor.DeclaringType.FullName) {
396                                 case "System.Xml.Serialization.XmlSchemaProviderAttribute":
397                                         MarkXmlSchemaProvider (type, attribute);
398                                         break;
399                                 }
400                         }
401                 }
402
403                 void MarkMethodSpecialCustomAttributes (MethodDefinition method)
404                 {
405                         if (!method.HasCustomAttributes)
406                                 return;
407
408                         foreach (CustomAttribute attribute in method.CustomAttributes) {
409                                 switch (attribute.Constructor.DeclaringType.FullName) {
410                                 case "System.Web.Services.Protocols.SoapHeaderAttribute":
411                                         MarkSoapHeader (method, attribute);
412                                         break;
413                                 }
414                         }
415                 }
416
417                 void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
418                 {
419                         string method_name;
420                         if (!TryGetStringArgument (attribute, out method_name))
421                                 return;
422
423                         MarkNamedMethod (type, method_name);
424                 }
425
426                 static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
427                 {
428                         argument = null;
429
430                         if (attribute.ConstructorArguments.Count < 1)
431                                 return false;
432
433                         argument = attribute.ConstructorArguments [0].Value as string;
434
435                         return argument != null;
436                 }
437
438                 void MarkNamedMethod (TypeDefinition type, string method_name)
439                 {
440                         if (!type.HasMethods)
441                                 return;
442
443                         foreach (MethodDefinition method in type.Methods) {
444                                 if (method.Name != method_name)
445                                         continue;
446
447                                 MarkMethod (method);
448                         }
449                 }
450
451                 void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
452                 {
453                         string member_name;
454                         if (!TryGetStringArgument (attribute, out member_name))
455                                 return;
456
457                         MarkNamedField (method.DeclaringType, member_name);
458                         MarkNamedProperty (method.DeclaringType, member_name);
459                 }
460
461                 void MarkNamedField (TypeDefinition type, string field_name)
462                 {
463                         if (!type.HasFields)
464                                 return;
465
466                         foreach (FieldDefinition field in type.Fields) {
467                                 if (field.Name != field_name)
468                                         continue;
469
470                                 MarkField (field);
471                         }
472                 }
473
474                 void MarkNamedProperty (TypeDefinition type, string property_name)
475                 {
476                         if (!type.HasProperties)
477                                 return;
478
479                         foreach (PropertyDefinition property in type.Properties) {
480                                 if (property.Name != property_name)
481                                         continue;
482
483                                 MarkMethod (property.GetMethod);
484                                 MarkMethod (property.SetMethod);
485                         }
486                 }
487
488                 void MarkGenericParameterProvider (IGenericParameterProvider provider)
489                 {
490                         if (!provider.HasGenericParameters)
491                                 return;
492
493                         foreach (GenericParameter parameter in provider.GenericParameters)
494                                 MarkGenericParameter (parameter);
495                 }
496
497                 void MarkGenericParameter (GenericParameter parameter)
498                 {
499                         MarkCustomAttributes (parameter);
500                         foreach (TypeReference constraint in parameter.Constraints)
501                                 MarkType (constraint);
502                 }
503
504                 bool IsVirtualAndHasPreservedParent (MethodDefinition method)
505                 {
506                         if (!method.IsVirtual)
507                                 return false;
508
509                         var base_list = Annotations.GetBaseMethods (method);
510                         if (base_list == null)
511                                 return false;
512
513                         foreach (MethodDefinition @base in base_list) {
514                                 if (IgnoreScope (@base.DeclaringType.Scope))
515                                         return true;
516
517                                 if (IsVirtualAndHasPreservedParent (@base))
518                                         return true;
519                         }
520
521                         return false;
522                 }
523
524                 static MethodPredicate IsSpecialSerializationConstructorPredicate = new MethodPredicate (IsSpecialSerializationConstructor);
525
526                 static bool IsSpecialSerializationConstructor (MethodDefinition method)
527                 {
528                         if (!IsConstructor (method))
529                                 return false;
530
531                         var parameters = method.Parameters;
532                         if (parameters.Count != 2)
533                                 return false;
534
535                         return parameters [0].ParameterType.Name == "SerializationInfo" &&
536                                 parameters [1].ParameterType.Name == "StreamingContext";
537                 }
538
539                 delegate bool MethodPredicate (MethodDefinition method);
540
541                 void MarkMethodsIf (ICollection methods, MethodPredicate predicate)
542                 {
543                         foreach (MethodDefinition method in methods)
544                                 if (predicate (method))
545                                         MarkMethod (method);
546                 }
547
548                 static MethodPredicate IsDefaultConstructorPredicate = new MethodPredicate (IsDefaultConstructor);
549
550                 static bool IsDefaultConstructor (MethodDefinition method)
551                 {
552                         return IsConstructor (method) && method.Parameters.Count == 0;
553                 }
554
555                 static bool IsConstructor (MethodDefinition method)
556                 {
557                         return method.IsConstructor && !method.IsStatic;
558                 }
559
560                 static MethodPredicate IsStaticConstructorPredicate = new MethodPredicate (IsStaticConstructor);
561
562                 static bool IsStaticConstructor (MethodDefinition method)
563                 {
564                         return method.IsConstructor && method.IsStatic;
565                 }
566
567                 static bool IsSerializable (TypeDefinition td)
568                 {
569                         return (td.Attributes & TypeAttributes.Serializable) != 0;
570                 }
571
572                 static bool IsMulticastDelegate (TypeDefinition td)
573                 {
574                         return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
575                 }
576
577                 TypeDefinition ResolveTypeDefinition (TypeReference type)
578                 {
579                         TypeDefinition td = type as TypeDefinition;
580                         if (td == null)
581                                 td = type.Resolve ();
582
583                         return td;
584                 }
585
586                 protected TypeReference GetOriginalType (TypeReference type)
587                 {
588                         while (type is TypeSpecification) {
589                                 GenericInstanceType git = type as GenericInstanceType;
590                                 if (git != null)
591                                         MarkGenericArguments (git);
592
593                                 var mod = type as IModifierType;
594                                 if (mod != null)
595                                         MarkModifierType (mod);
596
597                                 type = ((TypeSpecification) type).ElementType;
598                         }
599
600                         return type;
601                 }
602
603                 void MarkModifierType (IModifierType mod)
604                 {
605                         MarkType (mod.ModifierType);
606                 }
607
608                 void MarkGenericArguments (IGenericInstance instance)
609                 {
610                         foreach (TypeReference argument in instance.GenericArguments)
611                                 MarkType (argument);
612
613                         MarkGenericArgumentConstructors (instance);
614                 }
615
616                 void MarkGenericArgumentConstructors (IGenericInstance instance)
617                 {
618                         var arguments = instance.GenericArguments;
619
620                         var generic_element = GetGenericProviderFromInstance (instance);
621                         if (generic_element == null)
622                                 return;
623
624                         var parameters = generic_element.GenericParameters;
625
626                         if (arguments.Count != parameters.Count)
627                                 return;
628
629                         for (int i = 0; i < arguments.Count; i++) {
630                                 var argument = arguments [i];
631                                 var parameter = parameters [i];
632
633                                 if (!parameter.HasDefaultConstructorConstraint)
634                                         continue;
635
636                                 var argument_definition = ResolveTypeDefinition (argument);
637                                 if (argument_definition == null)
638                                         continue;
639
640                                 MarkMethodsIf (argument_definition.Methods, ctor => !ctor.IsStatic && !ctor.HasParameters);
641                         }
642                 }
643
644                 IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
645                 {
646                         var method = instance as GenericInstanceMethod;
647                         if (method != null)
648                                 return method.ElementMethod;
649
650                         var type = instance as GenericInstanceType;
651                         if (type != null)
652                                 return type.ElementType;
653
654                         return null;
655                 }
656
657                 void ApplyPreserveInfo (TypeDefinition type)
658                 {
659                         ApplyPreserveMethods (type);
660
661                         if (!Annotations.IsPreserved (type))
662                                 return;
663
664                         switch (Annotations.GetPreserve (type)) {
665                         case TypePreserve.All:
666                                 MarkFields (type);
667                                 MarkMethods (type);
668                                 break;
669                         case TypePreserve.Fields:
670                                 MarkFields (type);
671                                 break;
672                         case TypePreserve.Methods:
673                                 MarkMethods (type);
674                                 break;
675                         }
676                 }
677
678                 void ApplyPreserveMethods (TypeDefinition type)
679                 {
680                         var list = Annotations.GetPreservedMethods (type);
681                         if (list == null)
682                                 return;
683
684                         foreach (MethodDefinition method in list)
685                                 MarkMethod (method);
686                 }
687
688                 void MarkFields (TypeDefinition type)
689                 {
690                         if (!type.HasFields)
691                                 return;
692
693                         foreach (FieldDefinition field in type.Fields)
694                                 MarkField (field);
695                 }
696
697                 void MarkMethods (TypeDefinition type)
698                 {
699                         if (type.HasMethods)
700                                 MarkMethodCollection (type.Methods);
701                 }
702
703                 void MarkMethodCollection (IEnumerable methods)
704                 {
705                         foreach (MethodDefinition method in methods)
706                                 MarkMethod (method);
707                 }
708
709                 void MarkMethod (MethodReference reference)
710                 {
711                         reference = GetOriginalMethod (reference);
712
713                         if (reference.DeclaringType is ArrayType)
714                                 return;
715
716 //                      if (IgnoreScope (reference.DeclaringType.Scope))
717 //                              return;
718
719                         MethodDefinition method = ResolveMethodDefinition (reference);
720
721                         if (method == null)
722                                 throw new ResolutionException (reference);
723
724                         if (Annotations.GetAction (method) == MethodAction.Nothing)
725                                 Annotations.SetAction (method, MethodAction.Parse);
726
727                         EnqueueMethod (method);
728                 }
729
730                 AssemblyDefinition ResolveAssembly (IMetadataScope scope)
731                 {
732                         AssemblyDefinition assembly = _context.Resolve (scope);
733                         MarkAssembly (assembly);
734                         return assembly;
735                 }
736
737                 MethodReference GetOriginalMethod (MethodReference method)
738                 {
739                         while (method is MethodSpecification) {
740                                 GenericInstanceMethod gim = method as GenericInstanceMethod;
741                                 if (gim != null)
742                                         MarkGenericArguments (gim);
743
744                                 method = ((MethodSpecification) method).ElementMethod;
745                         }
746
747                         return method;
748                 }
749
750                 MethodDefinition ResolveMethodDefinition (MethodReference method)
751                 {
752                         MethodDefinition md = method as MethodDefinition;
753                         if (md == null)
754                                 md = method.Resolve ();
755
756                         return md;
757                 }
758
759                 void ProcessMethod (MethodDefinition method)
760                 {
761                         if (CheckProcessed (method))
762                                 return;
763
764                         MarkType (method.DeclaringType);
765                         MarkCustomAttributes (method);
766
767                         MarkGenericParameterProvider (method);
768
769                         if (IsPropertyMethod (method))
770                                 MarkProperty (GetProperty (method));
771                         else if (IsEventMethod (method))
772                                 MarkEvent (GetEvent (method));
773
774                         if (method.HasParameters) {
775                                 foreach (ParameterDefinition pd in method.Parameters) {
776                                         MarkType (pd.ParameterType);
777                                         MarkCustomAttributes (pd);
778                                         MarkMarshalSpec (pd);
779                                 }
780                         }
781
782                         if (method.HasOverrides) {
783                                 foreach (MethodReference ov in method.Overrides)
784                                         MarkMethod (ov);
785                         }
786
787                         MarkMethodSpecialCustomAttributes (method);
788
789                         if (method.IsVirtual)
790                                 _virtual_methods.Add (method);
791
792                         MarkBaseMethods (method);
793
794                         MarkType (method.ReturnType);
795                         MarkCustomAttributes (method.MethodReturnType);
796                         MarkMarshalSpec (method.MethodReturnType);
797
798                         if (ShouldParseMethodBody (method))
799                                 MarkMethodBody (method.Body);
800
801                         Annotations.Mark (method);
802                 }
803
804                 void MarkBaseMethods (MethodDefinition method)
805                 {
806                         IList base_methods = Annotations.GetBaseMethods (method);
807                         if (base_methods == null)
808                                 return;
809
810                         foreach (MethodDefinition base_method in base_methods) {
811                                 MarkMethod (base_method);
812                                 MarkBaseMethods (base_method);
813                         }
814                 }
815
816                 bool ShouldParseMethodBody (MethodDefinition method)
817                 {
818                         if (!method.HasBody)
819                                 return false;
820
821                         AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
822                         return (Annotations.GetAction (method) == MethodAction.ForceParse ||
823                                 (Annotations.GetAction (assembly) == AssemblyAction.Link && Annotations.GetAction (method) == MethodAction.Parse));
824                 }
825
826                 static bool IsPropertyMethod (MethodDefinition md)
827                 {
828                         return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
829                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
830                 }
831
832                 static bool IsEventMethod (MethodDefinition md)
833                 {
834                         return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
835                                 (md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
836                                 (md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
837                 }
838
839                 static PropertyDefinition GetProperty (MethodDefinition md)
840                 {
841                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
842                         foreach (PropertyDefinition prop in declaringType.Properties)
843                                 if (prop.GetMethod == md || prop.SetMethod == md)
844                                         return prop;
845
846                         return null;
847                 }
848
849                 static EventDefinition GetEvent (MethodDefinition md)
850                 {
851                         TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
852                         foreach (EventDefinition evt in declaringType.Events)
853                                 if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
854                                         return evt;
855
856                         return null;
857                 }
858
859                 void MarkProperty (PropertyDefinition prop)
860                 {
861                         MarkCustomAttributes (prop);
862                 }
863
864                 void MarkEvent (EventDefinition evt)
865                 {
866                         MarkCustomAttributes (evt);
867                         MarkMethodIfNotNull (evt.AddMethod);
868                         MarkMethodIfNotNull (evt.InvokeMethod);
869                         MarkMethodIfNotNull (evt.RemoveMethod);
870                 }
871
872                 void MarkMethodIfNotNull (MethodReference method)
873                 {
874                         if (method == null)
875                                 return;
876
877                         MarkMethod (method);
878                 }
879
880                 void MarkMethodBody (MethodBody body)
881                 {
882                         foreach (VariableDefinition var in body.Variables)
883                                 MarkType (var.VariableType);
884
885                         foreach (ExceptionHandler eh in body.ExceptionHandlers)
886                                 if (eh.HandlerType == ExceptionHandlerType.Catch)
887                                         MarkType (eh.CatchType);
888
889                         foreach (Instruction instruction in body.Instructions)
890                                 MarkInstruction (instruction);
891                 }
892
893                 void MarkInstruction (Instruction instruction)
894                 {
895                         switch (instruction.OpCode.OperandType) {
896                         case OperandType.InlineField:
897                                 MarkField ((FieldReference) instruction.Operand);
898                                 break;
899                         case OperandType.InlineMethod:
900                                 MarkMethod ((MethodReference) instruction.Operand);
901                                 break;
902                         case OperandType.InlineTok:
903                                 object token = instruction.Operand;
904                                 if (token is TypeReference)
905                                         MarkType ((TypeReference) token);
906                                 else if (token is MethodReference)
907                                         MarkMethod ((MethodReference) token);
908                                 else
909                                         MarkField ((FieldReference) token);
910                                 break;
911                         case OperandType.InlineType:
912                                 MarkType ((TypeReference) instruction.Operand);
913                                 break;
914                         default:
915                                 break;
916                         }
917                 }
918         }
919 }