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);
73 foreach (TypeDefinition type in assembly.MainModule.Types)
74 InitializeType (type);
77 void InitializeType (TypeDefinition type)
79 if (type.HasNestedTypes) {
80 foreach (var nested in type.NestedTypes)
81 InitializeType (nested);
84 if (!Annotations.IsMarked (type))
90 InitializeFields (type);
92 InitializeMethods (type.Methods);
95 void InitializeFields (TypeDefinition type)
97 foreach (FieldDefinition field in type.Fields)
98 if (Annotations.IsMarked (field))
102 void InitializeMethods (ICollection methods)
104 foreach (MethodDefinition method in methods)
105 if (Annotations.IsMarked (method))
106 EnqueueMethod (method);
112 throw new InvalidOperationException ("No entry methods");
114 while (!QueueIsEmpty ()) {
116 ProcessVirtualMethods ();
122 while (!QueueIsEmpty ()) {
123 MethodDefinition method = (MethodDefinition) _methods.Dequeue ();
124 Annotations.Push (method);
125 ProcessMethod (method);
132 return _methods.Count == 0;
135 protected virtual void EnqueueMethod (MethodDefinition method)
137 _methods.Enqueue (method);
140 void ProcessVirtualMethods ()
142 foreach (MethodDefinition method in _virtual_methods) {
143 Annotations.Push (method);
144 ProcessVirtualMethod (method);
149 void ProcessVirtualMethod (MethodDefinition method)
151 IList overrides = Annotations.GetOverrides (method);
152 if (overrides == null)
155 foreach (MethodDefinition @override in overrides)
156 ProcessOverride (@override);
159 void ProcessOverride (MethodDefinition method)
161 if (!Annotations.IsMarked (method.DeclaringType))
164 if (Annotations.IsProcessed (method))
167 if (Annotations.IsMarked (method))
171 ProcessVirtualMethod (method);
174 void MarkMarshalSpec (IMarshalInfoProvider spec)
176 if (!spec.HasMarshalInfo)
179 var marshaler = spec.MarshalInfo as CustomMarshalInfo;
180 if (marshaler == null)
183 MarkType (marshaler.ManagedType);
186 void MarkCustomAttributes (ICustomAttributeProvider provider)
188 if (!provider.HasCustomAttributes)
191 foreach (CustomAttribute ca in provider.CustomAttributes)
192 MarkCustomAttribute (ca);
195 protected virtual void MarkCustomAttribute (CustomAttribute ca)
197 Annotations.Push (ca);
198 MarkMethod (ca.Constructor);
200 MarkCustomAttributeArguments (ca);
202 TypeReference constructor_type = ca.Constructor.DeclaringType;
203 TypeDefinition type = constructor_type.Resolve ();
206 throw new ResolutionException (constructor_type);
209 MarkCustomAttributeProperties (ca, type);
210 MarkCustomAttributeFields (ca, type);
214 protected void MarkSecurityDeclarations (ISecurityDeclarationProvider provider)
216 // most security declarations are removed (if linked) but user code might still have some
217 // and if the attribtues references types then they need to be marked too
218 if ((provider == null) || !provider.HasSecurityDeclarations)
221 foreach (var sd in provider.SecurityDeclarations)
222 MarkSecurityDeclaration (sd);
225 protected virtual void MarkSecurityDeclaration (SecurityDeclaration sd)
227 if (!sd.HasSecurityAttributes)
230 foreach (var sa in sd.SecurityAttributes)
231 MarkSecurityAttribute (sa);
234 protected virtual void MarkSecurityAttribute (SecurityAttribute sa)
236 TypeReference security_type = sa.AttributeType;
237 TypeDefinition type = security_type.Resolve ();
239 throw new ResolutionException (security_type);
241 MarkType (security_type);
242 MarkSecurityAttributeProperties (sa, type);
243 MarkSecurityAttributeFields (sa, type);
246 protected void MarkSecurityAttributeProperties (SecurityAttribute sa, TypeDefinition attribute)
248 if (!sa.HasProperties)
251 foreach (var named_argument in sa.Properties)
252 MarkCustomAttributeProperty (named_argument, attribute);
255 protected void MarkSecurityAttributeFields (SecurityAttribute sa, TypeDefinition attribute)
260 foreach (var named_argument in sa.Fields)
261 MarkCustomAttributeField (named_argument, attribute);
264 protected void MarkCustomAttributeProperties (CustomAttribute ca, TypeDefinition attribute)
266 if (!ca.HasProperties)
269 foreach (var named_argument in ca.Properties)
270 MarkCustomAttributeProperty (named_argument, attribute);
273 protected void MarkCustomAttributeProperty (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
275 PropertyDefinition property = GetProperty (attribute, namedArgument.Name);
276 Annotations.Push (property);
277 if (property != null)
278 MarkMethod (property.SetMethod);
280 MarkIfType (namedArgument.Argument);
284 PropertyDefinition GetProperty (TypeDefinition type, string propertyname)
286 while (type != null) {
287 PropertyDefinition property = type.Properties.FirstOrDefault (p => p.Name == propertyname);
288 if (property != null)
291 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
297 protected void MarkCustomAttributeFields (CustomAttribute ca, TypeDefinition attribute)
302 foreach (var named_argument in ca.Fields)
303 MarkCustomAttributeField (named_argument, attribute);
306 protected void MarkCustomAttributeField (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
308 FieldDefinition field = GetField (attribute, namedArgument.Name);
312 MarkIfType (namedArgument.Argument);
315 FieldDefinition GetField (TypeDefinition type, string fieldname)
317 while (type != null) {
318 FieldDefinition field = type.Fields.FirstOrDefault (f => f.Name == fieldname);
322 type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
328 void MarkCustomAttributeArguments (CustomAttribute ca)
330 if (!ca.HasConstructorArguments)
333 foreach (var argument in ca.ConstructorArguments)
334 MarkIfType (argument);
337 void MarkIfType (CustomAttributeArgument argument)
339 var at = argument.Type;
341 var et = at.GetElementType ();
342 if (et.Namespace != "System" || et.Name != "Type")
346 if (argument.Value == null)
349 foreach (var cac in (CustomAttributeArgument[]) argument.Value)
350 MarkWithResolvedScope ((TypeReference) cac.Value);
351 } else if (at.Namespace == "System" && at.Name == "Type") {
352 MarkType (argument.Type);
353 MarkWithResolvedScope ((TypeReference) argument.Value);
357 // custom attributes encoding means it's possible to have a scope that will point into a PCL facade
358 // even if we (just before saving) will resolve all type references (bug #26752)
359 void MarkWithResolvedScope (TypeReference type)
364 // a GenericInstanceType can could contains generic arguments with scope that
365 // needs to be updated out of the PCL facade (bug #28823)
366 var git = (type as GenericInstanceType);
367 if ((git != null) && git.HasGenericArguments) {
368 foreach (var ga in git.GenericArguments)
369 MarkWithResolvedScope (ga);
371 // we cannot set the Scope of a TypeSpecification but it's element type can be set
372 // e.g. System.String[] -> System.String
373 var ts = (type as TypeSpecification);
375 MarkWithResolvedScope (ts.GetElementType ());
379 var td = type.Resolve ();
381 type.Scope = td.Scope;
385 protected bool CheckProcessed (IMetadataTokenProvider provider)
387 if (Annotations.IsProcessed (provider))
390 Annotations.Processed (provider);
394 protected void MarkAssembly (AssemblyDefinition assembly)
396 if (CheckProcessed (assembly))
399 ProcessModule (assembly);
401 MarkCustomAttributes (assembly);
402 MarkSecurityDeclarations (assembly);
404 foreach (ModuleDefinition module in assembly.Modules)
405 MarkCustomAttributes (module);
408 void ProcessModule (AssemblyDefinition assembly)
410 // Pre-mark <Module> if there is any methods as they need to be executed
411 // at assembly load time
412 foreach (TypeDefinition type in assembly.MainModule.Types)
414 if (type.Name == "<Module>" && type.HasMethods)
422 protected void MarkField (FieldReference reference)
424 // if (IgnoreScope (reference.DeclaringType.Scope))
427 if (reference.DeclaringType is GenericInstanceType)
428 MarkType (reference.DeclaringType);
430 FieldDefinition field = ResolveFieldDefinition (reference);
433 throw new ResolutionException (reference);
435 if (CheckProcessed (field))
438 MarkType (field.DeclaringType);
439 MarkType (field.FieldType);
440 MarkCustomAttributes (field);
441 MarkMarshalSpec (field);
443 Annotations.Mark (field);
446 protected virtual bool IgnoreScope (IMetadataScope scope)
448 AssemblyDefinition assembly = ResolveAssembly (scope);
449 return Annotations.GetAction (assembly) != AssemblyAction.Link;
452 FieldDefinition ResolveFieldDefinition (FieldReference field)
454 FieldDefinition fd = field as FieldDefinition;
456 fd = field.Resolve ();
461 void MarkScope (IMetadataScope scope)
463 var provider = scope as IMetadataTokenProvider;
464 if (provider == null)
467 Annotations.Mark (provider);
470 protected virtual void MarkSerializable (TypeDefinition type)
472 MarkDefaultConstructor (type);
473 MarkMethodsIf (type.Methods, IsSpecialSerializationConstructorPredicate);
476 protected virtual TypeDefinition MarkType (TypeReference reference)
478 if (reference == null)
481 reference = GetOriginalType (reference);
483 if (reference is GenericParameter)
486 // if (IgnoreScope (reference.Scope))
489 TypeDefinition type = ResolveTypeDefinition (reference);
492 throw new ResolutionException (reference);
494 if (CheckProcessed (type))
497 Annotations.Push (type);
499 MarkScope (type.Scope);
500 MarkType (type.BaseType);
501 MarkType (type.DeclaringType);
502 MarkCustomAttributes (type);
503 MarkSecurityDeclarations (type);
505 if (IsMulticastDelegate (type)) {
506 MarkMethodCollection (type.Methods);
509 if (IsSerializable (type))
510 MarkSerializable (type);
512 MarkTypeSpecialCustomAttributes (type);
514 MarkGenericParameterProvider (type);
516 // keep fields for value-types and for classes with LayoutKind.Sequential or Explicit
517 if (type.IsValueType || !type.IsAutoLayout)
518 MarkFields (type, type.IsEnum);
520 if (type.HasInterfaces) {
521 foreach (var iface in type.Interfaces)
522 MarkType (iface.InterfaceType);
525 if (type.HasMethods) {
526 MarkMethodsIf (type.Methods, IsVirtualAndHasPreservedParent);
527 MarkMethodsIf (type.Methods, IsStaticConstructorPredicate);
528 MarkMethodsIf (type.Methods, HasSerializationAttribute);
531 DoAdditionalTypeProcessing (type);
535 Annotations.Mark (type);
537 ApplyPreserveInfo (type);
542 // Allow subclassers to mark additional things when marking a method
543 protected virtual void DoAdditionalTypeProcessing (TypeDefinition method)
547 void MarkTypeSpecialCustomAttributes (TypeDefinition type)
549 if (!type.HasCustomAttributes)
552 foreach (CustomAttribute attribute in type.CustomAttributes) {
553 switch (attribute.Constructor.DeclaringType.FullName) {
554 case "System.Xml.Serialization.XmlSchemaProviderAttribute":
555 MarkXmlSchemaProvider (type, attribute);
561 void MarkMethodSpecialCustomAttributes (MethodDefinition method)
563 if (!method.HasCustomAttributes)
566 foreach (CustomAttribute attribute in method.CustomAttributes) {
567 switch (attribute.Constructor.DeclaringType.FullName) {
568 case "System.Web.Services.Protocols.SoapHeaderAttribute":
569 MarkSoapHeader (method, attribute);
575 void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
578 if (!TryGetStringArgument (attribute, out method_name))
581 MarkNamedMethod (type, method_name);
584 static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
588 if (attribute.ConstructorArguments.Count < 1)
591 argument = attribute.ConstructorArguments [0].Value as string;
593 return argument != null;
596 protected int MarkNamedMethod (TypeDefinition type, string method_name)
598 if (!type.HasMethods)
602 foreach (MethodDefinition method in type.Methods) {
603 if (method.Name != method_name)
613 void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
616 if (!TryGetStringArgument (attribute, out member_name))
619 MarkNamedField (method.DeclaringType, member_name);
620 MarkNamedProperty (method.DeclaringType, member_name);
623 void MarkNamedField (TypeDefinition type, string field_name)
628 foreach (FieldDefinition field in type.Fields) {
629 if (field.Name != field_name)
636 void MarkNamedProperty (TypeDefinition type, string property_name)
638 if (!type.HasProperties)
641 foreach (PropertyDefinition property in type.Properties) {
642 if (property.Name != property_name)
645 Annotations.Push (property);
646 MarkMethod (property.GetMethod);
647 MarkMethod (property.SetMethod);
652 void MarkGenericParameterProvider (IGenericParameterProvider provider)
654 if (!provider.HasGenericParameters)
657 foreach (GenericParameter parameter in provider.GenericParameters)
658 MarkGenericParameter (parameter);
661 void MarkGenericParameter (GenericParameter parameter)
663 MarkCustomAttributes (parameter);
664 foreach (TypeReference constraint in parameter.Constraints)
665 MarkType (constraint);
668 bool IsVirtualAndHasPreservedParent (MethodDefinition method)
670 if (!method.IsVirtual)
673 var base_list = Annotations.GetBaseMethods (method);
674 if (base_list == null)
677 foreach (MethodDefinition @base in base_list) {
678 if (IgnoreScope (@base.DeclaringType.Scope))
681 if (IsVirtualAndHasPreservedParent (@base))
688 static MethodPredicate IsSpecialSerializationConstructorPredicate = new MethodPredicate (IsSpecialSerializationConstructor);
690 static bool IsSpecialSerializationConstructor (MethodDefinition method)
692 if (!IsConstructor (method))
695 var parameters = method.Parameters;
696 if (parameters.Count != 2)
699 return parameters [0].ParameterType.Name == "SerializationInfo" &&
700 parameters [1].ParameterType.Name == "StreamingContext";
703 delegate bool MethodPredicate (MethodDefinition method);
705 void MarkMethodsIf (ICollection methods, MethodPredicate predicate)
707 foreach (MethodDefinition method in methods)
708 if (predicate (method)) {
709 Annotations.Push (predicate);
715 static MethodPredicate IsDefaultConstructorPredicate = new MethodPredicate (IsDefaultConstructor);
717 static bool IsDefaultConstructor (MethodDefinition method)
719 return IsConstructor (method) && !method.HasParameters;
722 static bool IsConstructor (MethodDefinition method)
724 return method.IsConstructor && !method.IsStatic;
727 protected void MarkDefaultConstructor (TypeDefinition type)
729 if ((type == null) || !type.HasMethods)
732 MarkMethodsIf (type.Methods, IsDefaultConstructorPredicate);
735 static MethodPredicate IsStaticConstructorPredicate = new MethodPredicate (IsStaticConstructor);
737 static bool IsStaticConstructor (MethodDefinition method)
739 return method.IsConstructor && method.IsStatic;
742 static bool HasSerializationAttribute (MethodDefinition method)
744 if (!method.HasCustomAttributes)
746 foreach (var ca in method.CustomAttributes) {
747 var cat = ca.AttributeType;
748 if (cat.Namespace != "System.Runtime.Serialization")
751 case "OnDeserializedAttribute":
752 case "OnDeserializingAttribute":
753 case "OnSerializedAttribute":
754 case "OnSerializingAttribute":
761 static bool IsSerializable (TypeDefinition td)
763 return (td.Attributes & TypeAttributes.Serializable) != 0;
766 static bool IsMulticastDelegate (TypeDefinition td)
768 return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
771 protected TypeDefinition ResolveTypeDefinition (TypeReference type)
773 TypeDefinition td = type as TypeDefinition;
775 td = type.Resolve ();
780 protected TypeReference GetOriginalType (TypeReference type)
782 while (type is TypeSpecification) {
783 GenericInstanceType git = type as GenericInstanceType;
785 MarkGenericArguments (git);
787 var mod = type as IModifierType;
789 MarkModifierType (mod);
791 type = ((TypeSpecification) type).ElementType;
797 void MarkModifierType (IModifierType mod)
799 MarkType (mod.ModifierType);
802 void MarkGenericArguments (IGenericInstance instance)
804 foreach (TypeReference argument in instance.GenericArguments)
807 MarkGenericArgumentConstructors (instance);
810 void MarkGenericArgumentConstructors (IGenericInstance instance)
812 var arguments = instance.GenericArguments;
814 var generic_element = GetGenericProviderFromInstance (instance);
815 if (generic_element == null)
818 var parameters = generic_element.GenericParameters;
820 if (arguments.Count != parameters.Count)
823 for (int i = 0; i < arguments.Count; i++) {
824 var argument = arguments [i];
825 var parameter = parameters [i];
827 if (!parameter.HasDefaultConstructorConstraint)
830 var argument_definition = ResolveTypeDefinition (argument);
831 if (argument_definition == null)
834 MarkMethodsIf (argument_definition.Methods, ctor => !ctor.IsStatic && !ctor.HasParameters);
838 IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
840 var method = instance as GenericInstanceMethod;
842 return ResolveMethodDefinition (method.ElementMethod);
844 var type = instance as GenericInstanceType;
846 return ResolveTypeDefinition (type.ElementType);
851 void ApplyPreserveInfo (TypeDefinition type)
853 ApplyPreserveMethods (type);
855 if (!Annotations.IsPreserved (type))
858 switch (Annotations.GetPreserve (type)) {
859 case TypePreserve.All:
860 MarkFields (type, true);
863 case TypePreserve.Fields:
864 MarkFields (type, true);
866 case TypePreserve.Methods:
872 void ApplyPreserveMethods (TypeDefinition type)
874 var list = Annotations.GetPreservedMethods (type);
878 MarkMethodCollection (list);
881 void ApplyPreserveMethods (MethodDefinition method)
883 var list = Annotations.GetPreservedMethods (method);
887 MarkMethodCollection (list);
890 protected void MarkFields (TypeDefinition type, bool includeStatic)
895 foreach (FieldDefinition field in type.Fields) {
896 if (!includeStatic && field.IsStatic)
902 protected virtual void MarkMethods (TypeDefinition type)
905 MarkMethodCollection (type.Methods);
908 void MarkMethodCollection (IEnumerable methods)
910 foreach (MethodDefinition method in methods)
914 protected virtual MethodDefinition MarkMethod (MethodReference reference)
916 reference = GetOriginalMethod (reference);
918 if (reference.DeclaringType is ArrayType)
921 Annotations.Push (reference);
922 if (reference.DeclaringType is GenericInstanceType)
923 MarkType (reference.DeclaringType);
925 // if (IgnoreScope (reference.DeclaringType.Scope))
928 MethodDefinition method = ResolveMethodDefinition (reference);
930 if (method == null) {
932 throw new ResolutionException (reference);
935 if (Annotations.GetAction (method) == MethodAction.Nothing)
936 Annotations.SetAction (method, MethodAction.Parse);
938 EnqueueMethod (method);
941 Annotations.AddDependency (method);
946 AssemblyDefinition ResolveAssembly (IMetadataScope scope)
948 AssemblyDefinition assembly = _context.Resolve (scope);
949 MarkAssembly (assembly);
953 protected MethodReference GetOriginalMethod (MethodReference method)
955 while (method is MethodSpecification) {
956 GenericInstanceMethod gim = method as GenericInstanceMethod;
958 MarkGenericArguments (gim);
960 method = ((MethodSpecification) method).ElementMethod;
966 MethodDefinition ResolveMethodDefinition (MethodReference method)
968 MethodDefinition md = method as MethodDefinition;
970 md = method.Resolve ();
975 protected virtual void ProcessMethod (MethodDefinition method)
977 if (CheckProcessed (method))
980 Annotations.Push (method);
981 MarkType (method.DeclaringType);
982 MarkCustomAttributes (method);
983 MarkSecurityDeclarations (method);
985 MarkGenericParameterProvider (method);
987 if (IsPropertyMethod (method))
988 MarkProperty (GetProperty (method));
989 else if (IsEventMethod (method))
990 MarkEvent (GetEvent (method));
992 if (method.HasParameters) {
993 foreach (ParameterDefinition pd in method.Parameters) {
994 MarkType (pd.ParameterType);
995 MarkCustomAttributes (pd);
996 MarkMarshalSpec (pd);
1000 if (method.HasOverrides) {
1001 foreach (MethodReference ov in method.Overrides)
1005 MarkMethodSpecialCustomAttributes (method);
1007 if (method.IsVirtual)
1008 _virtual_methods.Add (method);
1010 MarkBaseMethods (method);
1012 MarkType (method.ReturnType);
1013 MarkCustomAttributes (method.MethodReturnType);
1014 MarkMarshalSpec (method.MethodReturnType);
1016 if (ShouldParseMethodBody (method))
1017 MarkMethodBody (method.Body);
1019 DoAdditionalMethodProcessing (method);
1021 Annotations.Mark (method);
1023 ApplyPreserveMethods (method);
1027 // Allow subclassers to mark additional things when marking a method
1028 protected virtual void DoAdditionalMethodProcessing (MethodDefinition method)
1032 void MarkBaseMethods (MethodDefinition method)
1034 IList base_methods = Annotations.GetBaseMethods (method);
1035 if (base_methods == null)
1038 foreach (MethodDefinition base_method in base_methods) {
1039 if (base_method.DeclaringType.IsInterface && !method.DeclaringType.IsInterface)
1042 MarkMethod (base_method);
1043 MarkBaseMethods (base_method);
1047 bool ShouldParseMethodBody (MethodDefinition method)
1049 if (!method.HasBody)
1052 AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
1053 return (Annotations.GetAction (method) == MethodAction.ForceParse ||
1054 (Annotations.GetAction (assembly) == AssemblyAction.Link && Annotations.GetAction (method) == MethodAction.Parse));
1057 static internal bool IsPropertyMethod (MethodDefinition md)
1059 return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
1060 (md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
1063 static bool IsEventMethod (MethodDefinition md)
1065 return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
1066 (md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
1067 (md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
1070 static internal PropertyDefinition GetProperty (MethodDefinition md)
1072 TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
1073 foreach (PropertyDefinition prop in declaringType.Properties)
1074 if (prop.GetMethod == md || prop.SetMethod == md)
1080 static EventDefinition GetEvent (MethodDefinition md)
1082 TypeDefinition declaringType = (TypeDefinition) md.DeclaringType;
1083 foreach (EventDefinition evt in declaringType.Events)
1084 if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
1090 protected void MarkProperty (PropertyDefinition prop)
1092 MarkCustomAttributes (prop);
1095 protected void MarkEvent (EventDefinition evt)
1097 MarkCustomAttributes (evt);
1098 MarkMethodIfNotNull (evt.AddMethod);
1099 MarkMethodIfNotNull (evt.InvokeMethod);
1100 MarkMethodIfNotNull (evt.RemoveMethod);
1103 void MarkMethodIfNotNull (MethodReference method)
1108 MarkMethod (method);
1111 protected virtual void MarkMethodBody (MethodBody body)
1113 foreach (VariableDefinition var in body.Variables)
1114 MarkType (var.VariableType);
1116 foreach (ExceptionHandler eh in body.ExceptionHandlers)
1117 if (eh.HandlerType == ExceptionHandlerType.Catch)
1118 MarkType (eh.CatchType);
1120 foreach (Instruction instruction in body.Instructions)
1121 MarkInstruction (instruction);
1124 protected virtual void MarkInstruction (Instruction instruction)
1126 switch (instruction.OpCode.OperandType) {
1127 case OperandType.InlineField:
1128 MarkField ((FieldReference) instruction.Operand);
1130 case OperandType.InlineMethod:
1131 MarkMethod ((MethodReference) instruction.Operand);
1133 case OperandType.InlineTok:
1134 object token = instruction.Operand;
1135 if (token is TypeReference)
1136 MarkType ((TypeReference) token);
1137 else if (token is MethodReference)
1138 MarkMethod ((MethodReference) token);
1140 MarkField ((FieldReference) token);
1142 case OperandType.InlineType:
1143 MarkType ((TypeReference) instruction.Operand);