5 // Jb Evain (jbevain@gmail.com)
8 // (C) 2007 Novell, Inc.
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
37 namespace Mono.Linker.Steps {
39 public class MarkStep : IStep {
41 protected LinkContext _context;
42 protected Queue _methods;
43 protected ArrayList _virtual_methods;
45 public AnnotationStore Annotations {
46 get { return _context.Annotations; }
51 _methods = new Queue ();
52 _virtual_methods = new ArrayList ();
55 public virtual void Process (LinkContext context)
65 foreach (AssemblyDefinition assembly in _context.GetAssemblies ())
66 InitializeAssembly (assembly);
69 protected virtual void InitializeAssembly (AssemblyDefinition assembly)
71 MarkAssembly (assembly);
72 foreach (TypeDefinition type in assembly.MainModule.Types) {
73 if (!Annotations.IsMarked (type))
76 InitializeType (type);
80 void InitializeType (TypeDefinition type)
85 InitializeFields (type);
87 InitializeMethods (type.Methods);
89 if (type.HasNestedTypes) {
90 foreach (var nested in type.NestedTypes) {
91 if (Annotations.IsMarked (nested))
92 InitializeType (nested);
97 void InitializeFields (TypeDefinition type)
99 foreach (FieldDefinition field in type.Fields)
100 if (Annotations.IsMarked (field))
104 void InitializeMethods (ICollection methods)
106 foreach (MethodDefinition method in methods)
107 if (Annotations.IsMarked (method))
108 EnqueueMethod (method);
114 throw new InvalidOperationException ("No entry methods");
116 while (!QueueIsEmpty ()) {
118 ProcessVirtualMethods ();
124 while (!QueueIsEmpty ()) {
125 MethodDefinition method = (MethodDefinition) _methods.Dequeue ();
126 Annotations.Push (method);
127 ProcessMethod (method);
134 return _methods.Count == 0;
137 protected virtual void EnqueueMethod (MethodDefinition method)
139 _methods.Enqueue (method);
142 void ProcessVirtualMethods ()
144 foreach (MethodDefinition method in _virtual_methods) {
145 Annotations.Push (method);
146 ProcessVirtualMethod (method);
151 void ProcessVirtualMethod (MethodDefinition method)
153 IList overrides = Annotations.GetOverrides (method);
154 if (overrides == null)
157 foreach (MethodDefinition @override in overrides)
158 ProcessOverride (@override);
161 void ProcessOverride (MethodDefinition method)
163 if (!Annotations.IsMarked (method.DeclaringType))
166 if (Annotations.IsProcessed (method))
169 if (Annotations.IsMarked (method))
173 ProcessVirtualMethod (method);
176 void MarkMarshalSpec (IMarshalInfoProvider spec)
178 if (!spec.HasMarshalInfo)
181 var marshaler = spec.MarshalInfo as CustomMarshalInfo;
182 if (marshaler == null)
185 MarkType (marshaler.ManagedType);
188 void MarkCustomAttributes (ICustomAttributeProvider provider)
190 if (!provider.HasCustomAttributes)
193 foreach (CustomAttribute ca in provider.CustomAttributes)
194 MarkCustomAttribute (ca);
197 protected virtual void MarkCustomAttribute (CustomAttribute ca)
199 Annotations.Push (ca);
200 MarkMethod (ca.Constructor);
202 MarkCustomAttributeArguments (ca);
204 TypeReference constructor_type = ca.Constructor.DeclaringType;
205 TypeDefinition type = constructor_type.Resolve ();
208 throw new ResolutionException (constructor_type);
211 MarkCustomAttributeProperties (ca, type);
212 MarkCustomAttributeFields (ca, type);
216 protected void MarkSecurityDeclarations (ISecurityDeclarationProvider provider)
218 // most security declarations are removed (if linked) but user code might still have some
219 // and if the attribtues references types then they need to be marked too
220 if ((provider == null) || !provider.HasSecurityDeclarations)
223 foreach (var sd in provider.SecurityDeclarations)
224 MarkSecurityDeclaration (sd);
227 protected virtual void MarkSecurityDeclaration (SecurityDeclaration sd)
229 if (!sd.HasSecurityAttributes)
232 foreach (var sa in sd.SecurityAttributes)
233 MarkSecurityAttribute (sa);
236 protected virtual void MarkSecurityAttribute (SecurityAttribute sa)
238 TypeReference security_type = sa.AttributeType;
239 TypeDefinition type = security_type.Resolve ();
241 throw new ResolutionException (security_type);
243 MarkType (security_type);
244 MarkSecurityAttributeProperties (sa, type);
245 MarkSecurityAttributeFields (sa, type);
248 protected void MarkSecurityAttributeProperties (SecurityAttribute sa, TypeDefinition attribute)
250 if (!sa.HasProperties)
253 foreach (var named_argument in sa.Properties)
254 MarkCustomAttributeProperty (named_argument, attribute);
257 protected void MarkSecurityAttributeFields (SecurityAttribute sa, TypeDefinition attribute)
262 foreach (var named_argument in sa.Fields)
263 MarkCustomAttributeField (named_argument, attribute);
266 protected void MarkCustomAttributeProperties (CustomAttribute ca, TypeDefinition attribute)
268 if (!ca.HasProperties)
271 foreach (var named_argument in ca.Properties)
272 MarkCustomAttributeProperty (named_argument, attribute);
275 protected void MarkCustomAttributeProperty (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
277 PropertyDefinition property = GetProperty (attribute, namedArgument.Name);
278 Annotations.Push (property);
279 if (property != null)
280 MarkMethod (property.SetMethod);
282 MarkIfType (namedArgument.Argument);
286 PropertyDefinition GetProperty (TypeDefinition type, string propertyname)
288 while (type != null) {
289 PropertyDefinition property = type.Properties.FirstOrDefault (p => p.Name == propertyname);
290 if (property != null)
293 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
299 protected void MarkCustomAttributeFields (CustomAttribute ca, TypeDefinition attribute)
304 foreach (var named_argument in ca.Fields)
305 MarkCustomAttributeField (named_argument, attribute);
308 protected void MarkCustomAttributeField (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
310 FieldDefinition field = GetField (attribute, namedArgument.Name);
314 MarkIfType (namedArgument.Argument);
317 FieldDefinition GetField (TypeDefinition type, string fieldname)
319 while (type != null) {
320 FieldDefinition field = type.Fields.FirstOrDefault (f => f.Name == fieldname);
324 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
330 void MarkCustomAttributeArguments (CustomAttribute ca)
332 if (!ca.HasConstructorArguments)
335 foreach (var argument in ca.ConstructorArguments)
336 MarkIfType (argument);
339 void MarkIfType (CustomAttributeArgument argument)
341 var at = argument.Type;
343 var et = at.GetElementType ();
344 if (et.Namespace != "System" || et.Name != "Type")
348 if (argument.Value == null)
351 foreach (var cac in (CustomAttributeArgument[]) argument.Value)
352 MarkWithResolvedScope ((TypeReference) cac.Value);
353 } else if (at.Namespace == "System" && at.Name == "Type") {
354 MarkType (argument.Type);
355 MarkWithResolvedScope ((TypeReference) argument.Value);
359 // custom attributes encoding means it's possible to have a scope that will point into a PCL facade
360 // even if we (just before saving) will resolve all type references (bug #26752)
361 void MarkWithResolvedScope (TypeReference type)
366 // a GenericInstanceType can could contains generic arguments with scope that
367 // needs to be updated out of the PCL facade (bug #28823)
368 var git = (type as GenericInstanceType);
369 if ((git != null) && git.HasGenericArguments) {
370 foreach (var ga in git.GenericArguments)
371 MarkWithResolvedScope (ga);
373 // we cannot set the Scope of a TypeSpecification but it's element type can be set
374 // e.g. System.String[] -> System.String
375 var ts = (type as TypeSpecification);
377 MarkWithResolvedScope (ts.GetElementType ());
381 var td = type.Resolve ();
383 type.Scope = td.Scope;
387 protected bool CheckProcessed (IMetadataTokenProvider provider)
389 if (Annotations.IsProcessed (provider))
392 Annotations.Processed (provider);
396 protected void MarkAssembly (AssemblyDefinition assembly)
398 if (CheckProcessed (assembly))
401 ProcessModule (assembly);
403 MarkCustomAttributes (assembly);
404 MarkSecurityDeclarations (assembly);
406 foreach (ModuleDefinition module in assembly.Modules)
407 MarkCustomAttributes (module);
410 void ProcessModule (AssemblyDefinition assembly)
412 // Pre-mark <Module> if there is any methods as they need to be executed
413 // at assembly load time
414 foreach (TypeDefinition type in assembly.MainModule.Types)
416 if (type.Name == "<Module>" && type.HasMethods)
424 protected void MarkField (FieldReference reference)
426 // if (IgnoreScope (reference.DeclaringType.Scope))
429 if (reference.DeclaringType is GenericInstanceType)
430 MarkType (reference.DeclaringType);
432 FieldDefinition field = ResolveFieldDefinition (reference);
435 throw new ResolutionException (reference);
437 if (CheckProcessed (field))
440 MarkType (field.DeclaringType);
441 MarkType (field.FieldType);
442 MarkCustomAttributes (field);
443 MarkMarshalSpec (field);
445 Annotations.Mark (field);
448 protected virtual bool IgnoreScope (IMetadataScope scope)
450 AssemblyDefinition assembly = ResolveAssembly (scope);
451 return Annotations.GetAction (assembly) != AssemblyAction.Link;
454 FieldDefinition ResolveFieldDefinition (FieldReference field)
456 FieldDefinition fd = field as FieldDefinition;
458 fd = field.Resolve ();
463 void MarkScope (IMetadataScope scope)
465 var provider = scope as IMetadataTokenProvider;
466 if (provider == null)
469 Annotations.Mark (provider);
472 protected virtual void MarkSerializable (TypeDefinition type)
474 MarkDefaultConstructor (type);
475 MarkMethodsIf (type.Methods, IsSpecialSerializationConstructorPredicate);
478 protected virtual TypeDefinition MarkType (TypeReference reference)
480 if (reference == null)
483 reference = GetOriginalType (reference);
485 if (reference is GenericParameter)
488 // if (IgnoreScope (reference.Scope))
491 TypeDefinition type = ResolveTypeDefinition (reference);
494 throw new ResolutionException (reference);
496 if (CheckProcessed (type))
499 Annotations.Push (type);
501 MarkScope (type.Scope);
502 MarkType (type.BaseType);
503 MarkType (type.DeclaringType);
504 MarkCustomAttributes (type);
505 MarkSecurityDeclarations (type);
507 if (IsMulticastDelegate (type)) {
508 MarkMethodCollection (type.Methods);
511 if (IsSerializable (type))
512 MarkSerializable (type);
514 MarkTypeSpecialCustomAttributes (type);
516 MarkGenericParameterProvider (type);
518 // keep fields for value-types and for classes with LayoutKind.Sequential or Explicit
519 if (type.IsValueType || !type.IsAutoLayout)
520 MarkFields (type, type.IsEnum);
522 if (type.HasInterfaces) {
523 foreach (TypeReference iface in type.Interfaces)
527 if (type.HasMethods) {
528 MarkMethodsIf (type.Methods, IsVirtualAndHasPreservedParent);
529 MarkMethodsIf (type.Methods, IsStaticConstructorPredicate);
530 MarkMethodsIf (type.Methods, HasSerializationAttribute);
533 DoAdditionalTypeProcessing (type);
537 Annotations.Mark (type);
539 ApplyPreserveInfo (type);
544 // Allow subclassers to mark additional things when marking a method
545 protected virtual void DoAdditionalTypeProcessing (TypeDefinition method)
549 void MarkTypeSpecialCustomAttributes (TypeDefinition type)
551 if (!type.HasCustomAttributes)
554 foreach (CustomAttribute attribute in type.CustomAttributes) {
555 switch (attribute.Constructor.DeclaringType.FullName) {
556 case "System.Xml.Serialization.XmlSchemaProviderAttribute":
557 MarkXmlSchemaProvider (type, attribute);
563 void MarkMethodSpecialCustomAttributes (MethodDefinition method)
565 if (!method.HasCustomAttributes)
568 foreach (CustomAttribute attribute in method.CustomAttributes) {
569 switch (attribute.Constructor.DeclaringType.FullName) {
570 case "System.Web.Services.Protocols.SoapHeaderAttribute":
571 MarkSoapHeader (method, attribute);
577 void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
580 if (!TryGetStringArgument (attribute, out method_name))
583 MarkNamedMethod (type, method_name);
586 static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
590 if (attribute.ConstructorArguments.Count < 1)
593 argument = attribute.ConstructorArguments [0].Value as string;
595 return argument != null;
598 protected int MarkNamedMethod (TypeDefinition type, string method_name)
600 if (!type.HasMethods)
604 foreach (MethodDefinition method in type.Methods) {
605 if (method.Name != method_name)
615 void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
618 if (!TryGetStringArgument (attribute, out member_name))
621 MarkNamedField (method.DeclaringType, member_name);
622 MarkNamedProperty (method.DeclaringType, member_name);
625 void MarkNamedField (TypeDefinition type, string field_name)
630 foreach (FieldDefinition field in type.Fields) {
631 if (field.Name != field_name)
638 void MarkNamedProperty (TypeDefinition type, string property_name)
640 if (!type.HasProperties)
643 foreach (PropertyDefinition property in type.Properties) {
644 if (property.Name != property_name)
647 Annotations.Push (property);
648 MarkMethod (property.GetMethod);
649 MarkMethod (property.SetMethod);
654 void MarkGenericParameterProvider (IGenericParameterProvider provider)
656 if (!provider.HasGenericParameters)
659 foreach (GenericParameter parameter in provider.GenericParameters)
660 MarkGenericParameter (parameter);
663 void MarkGenericParameter (GenericParameter parameter)
665 MarkCustomAttributes (parameter);
666 foreach (TypeReference constraint in parameter.Constraints)
667 MarkType (constraint);
670 bool IsVirtualAndHasPreservedParent (MethodDefinition method)
672 if (!method.IsVirtual)
675 var base_list = Annotations.GetBaseMethods (method);
676 if (base_list == null)
679 foreach (MethodDefinition @base in base_list) {
680 if (IgnoreScope (@base.DeclaringType.Scope))
683 if (IsVirtualAndHasPreservedParent (@base))
690 static MethodPredicate IsSpecialSerializationConstructorPredicate = new MethodPredicate (IsSpecialSerializationConstructor);
692 static bool IsSpecialSerializationConstructor (MethodDefinition method)
694 if (!IsConstructor (method))
697 var parameters = method.Parameters;
698 if (parameters.Count != 2)
701 return parameters [0].ParameterType.Name == "SerializationInfo" &&
702 parameters [1].ParameterType.Name == "StreamingContext";
705 delegate bool MethodPredicate (MethodDefinition method);
707 void MarkMethodsIf (ICollection methods, MethodPredicate predicate)
709 foreach (MethodDefinition method in methods)
710 if (predicate (method)) {
711 Annotations.Push (predicate);
717 static MethodPredicate IsDefaultConstructorPredicate = new MethodPredicate (IsDefaultConstructor);
719 static bool IsDefaultConstructor (MethodDefinition method)
721 return IsConstructor (method) && !method.HasParameters;
724 static bool IsConstructor (MethodDefinition method)
726 return method.IsConstructor && !method.IsStatic;
729 protected void MarkDefaultConstructor (TypeDefinition type)
731 if ((type == null) || !type.HasMethods)
734 MarkMethodsIf (type.Methods, IsDefaultConstructorPredicate);
737 static MethodPredicate IsStaticConstructorPredicate = new MethodPredicate (IsStaticConstructor);
739 static bool IsStaticConstructor (MethodDefinition method)
741 return method.IsConstructor && method.IsStatic;
744 static bool HasSerializationAttribute (MethodDefinition method)
746 if (!method.HasCustomAttributes)
748 foreach (var ca in method.CustomAttributes) {
749 var cat = ca.AttributeType;
750 if (cat.Namespace != "System.Runtime.Serialization")
753 case "OnDeserializedAttribute":
754 case "OnDeserializingAttribute":
755 case "OnSerializedAttribute":
756 case "OnSerializingAttribute":
763 static bool IsSerializable (TypeDefinition td)
765 return (td.Attributes & TypeAttributes.Serializable) != 0;
768 static bool IsMulticastDelegate (TypeDefinition td)
770 return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
773 protected TypeDefinition ResolveTypeDefinition (TypeReference type)
775 TypeDefinition td = type as TypeDefinition;
777 td = type.Resolve ();
782 protected TypeReference GetOriginalType (TypeReference type)
784 while (type is TypeSpecification) {
785 GenericInstanceType git = type as GenericInstanceType;
787 MarkGenericArguments (git);
789 var mod = type as IModifierType;
791 MarkModifierType (mod);
793 type = ((TypeSpecification) type).ElementType;
799 void MarkModifierType (IModifierType mod)
801 MarkType (mod.ModifierType);
804 void MarkGenericArguments (IGenericInstance instance)
806 foreach (TypeReference argument in instance.GenericArguments)
809 MarkGenericArgumentConstructors (instance);
812 void MarkGenericArgumentConstructors (IGenericInstance instance)
814 var arguments = instance.GenericArguments;
816 var generic_element = GetGenericProviderFromInstance (instance);
817 if (generic_element == null)
820 var parameters = generic_element.GenericParameters;
822 if (arguments.Count != parameters.Count)
825 for (int i = 0; i < arguments.Count; i++) {
826 var argument = arguments [i];
827 var parameter = parameters [i];
829 if (!parameter.HasDefaultConstructorConstraint)
832 var argument_definition = ResolveTypeDefinition (argument);
833 if (argument_definition == null)
836 MarkMethodsIf (argument_definition.Methods, ctor => !ctor.IsStatic && !ctor.HasParameters);
840 IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
842 var method = instance as GenericInstanceMethod;
844 return ResolveMethodDefinition (method.ElementMethod);
846 var type = instance as GenericInstanceType;
848 return ResolveTypeDefinition (type.ElementType);
853 void ApplyPreserveInfo (TypeDefinition type)
855 ApplyPreserveMethods (type);
857 if (!Annotations.IsPreserved (type))
860 switch (Annotations.GetPreserve (type)) {
861 case TypePreserve.All:
862 MarkFields (type, true);
865 case TypePreserve.Fields:
866 MarkFields (type, true);
868 case TypePreserve.Methods:
874 void ApplyPreserveMethods (TypeDefinition type)
876 var list = Annotations.GetPreservedMethods (type);
880 MarkMethodCollection (list);
883 void ApplyPreserveMethods (MethodDefinition method)
885 var list = Annotations.GetPreservedMethods (method);
889 MarkMethodCollection (list);
892 protected void MarkFields (TypeDefinition type, bool includeStatic)
897 foreach (FieldDefinition field in type.Fields) {
898 if (!includeStatic && field.IsStatic)
904 protected virtual void MarkMethods (TypeDefinition type)
907 MarkMethodCollection (type.Methods);
910 void MarkMethodCollection (IEnumerable methods)
912 foreach (MethodDefinition method in methods)
916 protected virtual MethodDefinition MarkMethod (MethodReference reference)
918 reference = GetOriginalMethod (reference);
920 if (reference.DeclaringType is ArrayType)
923 Annotations.Push (reference);
924 if (reference.DeclaringType is GenericInstanceType)
925 MarkType (reference.DeclaringType);
927 // if (IgnoreScope (reference.DeclaringType.Scope))
930 MethodDefinition method = ResolveMethodDefinition (reference);
932 if (method == null) {
934 throw new ResolutionException (reference);
937 if (Annotations.GetAction (method) == MethodAction.Nothing)
938 Annotations.SetAction (method, MethodAction.Parse);
940 EnqueueMethod (method);
943 Annotations.AddDependency (method);
948 AssemblyDefinition ResolveAssembly (IMetadataScope scope)
950 AssemblyDefinition assembly = _context.Resolve (scope);
951 MarkAssembly (assembly);
955 protected MethodReference GetOriginalMethod (MethodReference method)
957 while (method is MethodSpecification) {
958 GenericInstanceMethod gim = method as GenericInstanceMethod;
960 MarkGenericArguments (gim);
962 method = ((MethodSpecification) method).ElementMethod;
968 MethodDefinition ResolveMethodDefinition (MethodReference method)
970 MethodDefinition md = method as MethodDefinition;
972 md = method.Resolve ();
977 protected virtual void ProcessMethod (MethodDefinition method)
979 if (CheckProcessed (method))
982 Annotations.Push (method);
983 MarkType (method.DeclaringType);
984 MarkCustomAttributes (method);
985 MarkSecurityDeclarations (method);
987 MarkGenericParameterProvider (method);
989 if (IsPropertyMethod (method))
990 MarkProperty (GetProperty (method));
991 else if (IsEventMethod (method))
992 MarkEvent (GetEvent (method));
994 if (method.HasParameters) {
995 foreach (ParameterDefinition pd in method.Parameters) {
996 MarkType (pd.ParameterType);
997 MarkCustomAttributes (pd);
998 MarkMarshalSpec (pd);
1002 if (method.HasOverrides) {
1003 foreach (MethodReference ov in method.Overrides)
1007 MarkMethodSpecialCustomAttributes (method);
1009 if (method.IsVirtual)
1010 _virtual_methods.Add (method);
1012 MarkBaseMethods (method);
1014 MarkType (method.ReturnType);
1015 MarkCustomAttributes (method.MethodReturnType);
1016 MarkMarshalSpec (method.MethodReturnType);
1018 if (ShouldParseMethodBody (method))
1019 MarkMethodBody (method.Body);
1021 DoAdditionalMethodProcessing (method);
1023 Annotations.Mark (method);
1025 ApplyPreserveMethods (method);
1029 // Allow subclassers to mark additional things when marking a method
1030 protected virtual void DoAdditionalMethodProcessing (MethodDefinition method)
1034 void MarkBaseMethods (MethodDefinition method)
1036 IList base_methods = Annotations.GetBaseMethods (method);
1037 if (base_methods == null)
1040 foreach (MethodDefinition base_method in base_methods) {
1041 if (base_method.DeclaringType.IsInterface && !method.DeclaringType.IsInterface)
1044 MarkMethod (base_method);
1045 MarkBaseMethods (base_method);
1049 bool ShouldParseMethodBody (MethodDefinition method)
1051 if (!method.HasBody)
1054 AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
1055 return (Annotations.GetAction (method) == MethodAction.ForceParse ||
1056 (Annotations.GetAction (assembly) == AssemblyAction.Link && Annotations.GetAction (method) == MethodAction.Parse));
1059 static internal bool IsPropertyMethod (MethodDefinition md)
1061 return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
1062 (md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
1065 static bool IsEventMethod (MethodDefinition md)
1067 return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
1068 (md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
1069 (md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
1072 static internal PropertyDefinition GetProperty (MethodDefinition md)
1074 TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
1075 foreach (PropertyDefinition prop in declaringType.Properties)
1076 if (prop.GetMethod == md || prop.SetMethod == md)
1082 static EventDefinition GetEvent (MethodDefinition md)
1084 TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
1085 foreach (EventDefinition evt in declaringType.Events)
1086 if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
1092 protected void MarkProperty (PropertyDefinition prop)
1094 MarkCustomAttributes (prop);
1097 protected void MarkEvent (EventDefinition evt)
1099 MarkCustomAttributes (evt);
1100 MarkMethodIfNotNull (evt.AddMethod);
1101 MarkMethodIfNotNull (evt.InvokeMethod);
1102 MarkMethodIfNotNull (evt.RemoveMethod);
1105 void MarkMethodIfNotNull (MethodReference method)
1110 MarkMethod (method);
1113 protected virtual void MarkMethodBody (MethodBody body)
1115 foreach (VariableDefinition var in body.Variables)
1116 MarkType (var.VariableType);
1118 foreach (ExceptionHandler eh in body.ExceptionHandlers)
1119 if (eh.HandlerType == ExceptionHandlerType.Catch)
1120 MarkType (eh.CatchType);
1122 foreach (Instruction instruction in body.Instructions)
1123 MarkInstruction (instruction);
1126 protected virtual void MarkInstruction (Instruction instruction)
1128 switch (instruction.OpCode.OperandType) {
1129 case OperandType.InlineField:
1130 MarkField ((FieldReference) instruction.Operand);
1132 case OperandType.InlineMethod:
1133 MarkMethod ((MethodReference) instruction.Operand);
1135 case OperandType.InlineTok:
1136 object token = instruction.Operand;
1137 if (token is TypeReference)
1138 MarkType ((TypeReference) token);
1139 else if (token is MethodReference)
1140 MarkMethod ((MethodReference) token);
1142 MarkField ((FieldReference) token);
1144 case OperandType.InlineType:
1145 MarkType ((TypeReference) instruction.Operand);