void SweepCollection(IList list)
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (!Annotations.IsMarked((IMetadataTokenProvider)list [i]))
         {
             list.RemoveAt(i--);
         }
     }
 }
Ejemplo n.º 2
0
        bool FilterDispose(MethodDefinition m)
        {
#if DEBUG
            var sp = m.DebugInformation.GetSequencePoint(m.Body.Instructions [0]);
            if (sp != null)
            {
                string source = sp.Document.Url;
                if (!source.EndsWith(".g.cs", StringComparison.Ordinal))
                {
                    throw new InvalidProgramException(String.Format("Attempt at modifying non-generated code for {0} : {1}", m, source));
                }
            }
#endif
            bool remove_all_fields = true;
            var  il = m.Body.Instructions;
            for (int i = il.Count - 1; i >= 0; i--)
            {
                Instruction ins = il [i];
                if (ins.OpCode.Code != Code.Stfld)
                {
                    continue;
                }
                // if the field is not marked elsewhere (since we skipped Dispose)
                if (!Annotations.IsMarked(ins.Operand as FieldReference))
                {
                    // remove stfld, the previous ldnull and ldarg.0 instructions
                    ins.OpCode  = OpCodes.Nop;
                    ins.Operand = null;
                    ins         = ins.Previous;
#if DEBUG
                    if (ins.OpCode.Code != Code.Ldnull)
                    {
                        throw new InvalidProgramException(String.Format("Attempt at modifying wrong code pattern for {0}", m));
                    }
#endif
                    ins.OpCode  = OpCodes.Nop;
                    ins.Operand = null;
                    ins         = ins.Previous;
#if DEBUG
                    if (ins.OpCode.Code != Code.Ldarg_0)
                    {
                        throw new InvalidProgramException(String.Format("Attempt at modifying wrong code pattern for {0}", m));
                    }
#endif
                    ins.OpCode  = OpCodes.Nop;
                    ins.Operand = null;
                    i          -= 2;
                }
                else
                {
                    remove_all_fields = false;
                }
            }
            return(remove_all_fields);
        }
Ejemplo n.º 3
0
        void MarkProperty(TypeDefinition type, PropertyDefinition property, string[] accessors, bool required)
        {
            if (Annotations.IsMarked(property))
            {
                Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{property.FullName}' in '{_xmlDocumentLocation}'", 2025));
            }

            Annotations.Mark(property, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));

            MarkPropertyAccessors(type, property, accessors, required);
        }
Ejemplo n.º 4
0
 void ProcessMethods(Collection <MethodDefinition> methods, ModuleDefinition module)
 {
     foreach (var method in methods)
     {
         if (!Annotations.IsMarked(method))
         {
             EnsureBypassNGenAttribute(module);
             method.CustomAttributes.Add(bypassNGenAttribute);
         }
     }
 }
Ejemplo n.º 5
0
        void MarkMethod(MethodDefinition method)
        {
            if (Annotations.IsMarked(method))
            {
                Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {method.FullName}");
            }

            Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));
            Annotations.MarkIndirectlyCalledMethod(method);
            Annotations.SetAction(method, MethodAction.Parse);
        }
        void MarkMethod(MethodDefinition method)
        {
            if (Annotations.IsMarked(method))
            {
                Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {method.FullName}");
            }

            Annotations.Mark(method);
            Tracer.AddDirectDependency(this, method);
            Annotations.SetAction(method, MethodAction.Parse);
        }
Ejemplo n.º 7
0
 protected void SweepCollection <T> (IList <T> list) where T : IMetadataTokenProvider
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (!Annotations.IsMarked(list [i]))
         {
             ElementRemoved(list [i]);
             list.RemoveAt(i--);
         }
     }
 }
Ejemplo n.º 8
0
 protected void SweepCustomAttributes(ICustomAttributeProvider provider)
 {
     for (int i = provider.CustomAttributes.Count - 1; i >= 0; i--)
     {
         var attribute = provider.CustomAttributes [i];
         if (!Annotations.IsMarked(attribute))
         {
             CustomAttributeUsageRemoved(provider, attribute);
             provider.CustomAttributes.RemoveAt(i);
         }
     }
 }
Ejemplo n.º 9
0
        void ResolveAllTypeReferences(AssemblyDefinition assembly)
        {
            if (resolvedTypeReferences == null)
            {
                resolvedTypeReferences = new HashSet <AssemblyDefinition> ();
            }
            if (resolvedTypeReferences.Contains(assembly))
            {
                return;
            }
            resolvedTypeReferences.Add(assembly);

            var hash = new Dictionary <TypeReference, IMetadataScope> ();

            foreach (TypeReference tr in assembly.MainModule.GetTypeReferences())
            {
                if (hash.ContainsKey(tr))
                {
                    continue;
                }
                var            td    = tr.Resolve();
                IMetadataScope scope = tr.Scope;
                // at this stage reference might include things that can't be resolved
                // and if it is (resolved) it needs to be kept only if marked (#16213)
                if ((td != null) && Annotations.IsMarked(td))
                {
                    scope = assembly.MainModule.Import(td).Scope;
                }
                hash.Add(tr, scope);
            }
            if (assembly.MainModule.HasExportedTypes)
            {
                foreach (var et in assembly.MainModule.ExportedTypes)
                {
                    var            td    = et.Resolve();
                    IMetadataScope scope = et.Scope;
                    if ((td != null) && Annotations.IsMarked(td))
                    {
                        scope = assembly.MainModule.Import(td).Scope;
                        hash.Add(td, scope);
                    }
                }
            }

            // Resolve everything first before updating scopes.
            // If we set the scope to null, then calling Resolve() on any of its
            // nested types would crash.

            foreach (var e in hash)
            {
                e.Key.Scope = e.Value;
            }
        }
Ejemplo n.º 10
0
 protected void SweepInterfaces(TypeDefinition type)
 {
     for (int i = type.Interfaces.Count - 1; i >= 0; i--)
     {
         var iface = type.Interfaces [i];
         if (Annotations.IsMarked(iface.InterfaceType.Resolve()))
         {
             continue;
         }
         InterfaceRemoved(type, iface);
         type.Interfaces.RemoveAt(i);
     }
 }
Ejemplo n.º 11
0
        protected virtual void InitializeAssembly(AssemblyDefinition assembly)
        {
            MarkAssembly(assembly);
            foreach (TypeDefinition type in assembly.MainModule.Types)
            {
                if (!Annotations.IsMarked(type))
                {
                    continue;
                }

                InitializeType(type);
            }
        }
Ejemplo n.º 12
0
        void MarkEvent(TypeDefinition type, EventDefinition @event, bool required)
        {
            if (Annotations.IsMarked(@event))
            {
                Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{@event.FullName}' in '{_xmlDocumentLocation}'", 2025));
            }

            Annotations.Mark(@event, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));

            MarkMethod(type, @event.AddMethod, required);
            MarkMethod(type, @event.RemoveMethod, required);
            MarkMethodIfNotNull(type, @event.InvokeMethod, required);
        }
Ejemplo n.º 13
0
        protected override void ProcessProperty(TypeDefinition type, PropertyDefinition property, XPathNavigator nav, object customData, bool fromSignature)
        {
            string[] accessors = fromSignature ? GetAccessors(nav) : _accessorsAll;

            if (Annotations.IsMarked(property))
            {
                Context.LogWarning($"Duplicate preserve of '{property.FullName}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation);
            }

            Annotations.Mark(property, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));

            ProcessPropertyAccessors(type, property, accessors, customData);
        }
Ejemplo n.º 14
0
        protected override void ProcessEvent(TypeDefinition type, EventDefinition @event, XPathNavigator nav, object customData)
        {
            if (Annotations.IsMarked(@event))
            {
                Context.LogWarning($"Duplicate preserve of '{@event.FullName}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation);
            }

            Annotations.Mark(@event, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));

            ProcessMethod(type, @event.AddMethod, null, customData);
            ProcessMethod(type, @event.RemoveMethod, null, customData);
            ProcessMethodIfNotNull(type, @event.InvokeMethod, customData);
        }
Ejemplo n.º 15
0
        void UpdateForwardedTypesScope(AssemblyDefinition assembly)
        {
            var changed_types = new Dictionary <TypeReference, IMetadataScope> ();

            foreach (TypeReference tr in assembly.MainModule.GetTypeReferences())
            {
                if (tr.IsWindowsRuntimeProjection)
                {
                    continue;
                }

                var td = tr.Resolve();
                // at this stage reference might include things that can't be resolved
                // and if it is (resolved) it needs to be kept only if marked (#16213)
                if (td == null || !Annotations.IsMarked(td))
                {
                    continue;
                }

                IMetadataScope scope = assembly.MainModule.ImportReference(td).Scope;
                if (tr.Scope != scope)
                {
                    changed_types.Add(tr, scope);
                }
            }

            //
            // Resolved everything first before updating scopes.
            // If we set the scope to null, then calling Resolve() on any of its
            // nested types would crash.
            //
            foreach (var e in changed_types)
            {
                e.Key.Scope = e.Value;
            }

            if (assembly.MainModule.HasExportedTypes)
            {
                foreach (var et in assembly.MainModule.ExportedTypes)
                {
                    var td = et.Resolve();
                    if (td == null)
                    {
                        continue;
                    }

                    et.Scope = assembly.MainModule.ImportReference(td).Scope;
                }
            }
        }
Ejemplo n.º 16
0
        protected override bool ShouldMarkInterfaceImplementation(TypeDefinition type, InterfaceImplementation iface, TypeDefinition resolvedInterfaceType)
        {
            if (Annotations.IsMarked(iface))
            {
                return(false);
            }

            if (base.ShouldMarkInterfaceImplementation(type, iface, resolvedInterfaceType))
            {
                return(true);
            }

            return(resolvedInterfaceType.ImplementsIJavaObject() || resolvedInterfaceType.ImplementsIJavaPeerable());
        }
Ejemplo n.º 17
0
        protected virtual void SweepAssembly(AssemblyDefinition assembly)
        {
            var types = new List <TypeDefinition> ();

            foreach (TypeDefinition type in assembly.MainModule.Types)
            {
                if (Annotations.IsMarked(type))
                {
                    SweepType(type);
                    types.Add(type);
                    continue;
                }

                // Is <Module> type.
                if (type.MetadataToken.RID == 1)
                {
                    types.Add(type);
                }
                else
                {
                    ElementRemoved(type);
                }
            }

            assembly.MainModule.Types.Clear();
            foreach (TypeDefinition type in types)
            {
                assembly.MainModule.Types.Add(type);
            }

            SweepResources(assembly);
            SweepCustomAttributes(assembly);

            foreach (var module in assembly.Modules)
            {
                SweepCustomAttributes(module);
            }

            //
            // MainModule module references are used by pinvoke
            //
            if (assembly.MainModule.HasModuleReferences)
            {
                SweepCollectionMetadata(assembly.MainModule.ModuleReferences);
            }

            SweepTypeForwarders(assembly);

            UpdateForwardedTypesScope(assembly);
        }
        protected virtual void ProcessType(TypeDefinition type, XPathNavigator nav)
        {
            if (IsExcluded(nav))
            {
                return;
            }

            TypePreserve preserve = GetTypePreserve(nav);

            if (!IsRequired(nav))
            {
                Annotations.SetPreserve(type, preserve);
                return;
            }

            if (Annotations.IsMarked(type))
            {
                var existingLevel  = Annotations.IsPreserved(type) ? Annotations.GetPreserve(type) : TypePreserve.Nothing;
                var duplicateLevel = preserve != TypePreserve.Nothing ? preserve : nav.HasChildren ? TypePreserve.Nothing : TypePreserve.All;
                Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {type.FullName} ({existingLevel}).  Duplicate uses ({duplicateLevel})");
            }

            Annotations.MarkAndPush(type);
            Tracer.AddDirectDependency(this, type);

            if (type.IsNested)
            {
                var parent = type;
                while (parent.IsNested)
                {
                    parent = parent.DeclaringType;
                    Annotations.Mark(parent);
                }
            }

            if (preserve != TypePreserve.Nothing)
            {
                Annotations.SetPreserve(type, preserve);
            }

            if (nav.HasChildren)
            {
                MarkSelectedFields(nav, type);
                MarkSelectedMethods(nav, type);
                MarkSelectedEvents(nav, type);
                MarkSelectedProperties(nav, type);
            }
            Tracer.Pop();
        }
Ejemplo n.º 19
0
 void SweepNestedTypes(TypeDefinition type)
 {
     for (int i = 0; i < type.NestedTypes.Count; i++)
     {
         var nested = type.NestedTypes [i];
         if (Annotations.IsMarked(nested))
         {
             SweepType(nested);
         }
         else
         {
             type.NestedTypes.RemoveAt(i--);
         }
     }
 }
Ejemplo n.º 20
0
 bool AreMarked(List <OverrideInformation> list)
 {
     if (list == null)
     {
         return(false);
     }
     foreach (var m in list)
     {
         if (Annotations.IsMarked(m.Override))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 21
0
 bool IsAnyMarked(List <MethodDefinition> list)
 {
     if (list == null)
     {
         return(false);
     }
     foreach (var m in list)
     {
         if (Annotations.IsMarked(m))
         {
             return(true);
         }
     }
     return(false);
 }
        void MarkField(TypeDefinition type, FieldDefinition field, string signature)
        {
            if (field != null)
            {
                if (Annotations.IsMarked(field))
                {
                    Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {field.FullName}");
                }

                Annotations.Mark(field);
            }
            else
            {
                AddUnresolveMarker(string.Format("T: {0}; F: {1}", type, signature));
            }
        }
Ejemplo n.º 23
0
        void MarkMethod(TypeDefinition type, MethodDefinition method, bool required)
        {
            if (Annotations.IsMarked(method))
            {
                Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{method.FullName}' in '{_xmlDocumentLocation}'", 2025));
            }

            Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));
            Annotations.MarkIndirectlyCalledMethod(method);
            Annotations.SetAction(method, MethodAction.Parse);

            if (!required)
            {
                Annotations.AddPreservedMethod(type, method);
            }
        }
Ejemplo n.º 24
0
        bool IsAnyMarked(IEnumerable <OverrideInformation> list)
        {
            if (list == null)
            {
                return(false);
            }

            foreach (var m in list)
            {
                if (Annotations.IsMarked(m.Override))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 25
0
        protected override void ProcessMethod(TypeDefinition type, MethodDefinition method, XPathNavigator nav, object customData)
        {
            if (Annotations.IsMarked(method))
            {
                Context.LogWarning($"Duplicate preserve of '{method.GetDisplayName ()}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation);
            }

            Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));
            Annotations.MarkIndirectlyCalledMethod(method);
            Annotations.SetAction(method, MethodAction.Parse);

            if (!(bool)customData)
            {
                Annotations.AddPreservedMethod(type, method);
            }
        }
Ejemplo n.º 26
0
        protected override bool IsReferenced(ModuleDefinition module, string name)
        {
            if (!module.TryGetTypeReference(name, out var tr))
            {
                return(false);
            }
            // it might be there (and not cleaned) until it's saved back to disk
            // but it can't resolve anymore (since it's removed from the actual assembly)
            var td = tr.Resolve();

            if (td == null)
            {
                return(false);
            }
            // and, if it was (cache) then we can ask if it was marked (since we're post mark)
            return(Annotations.IsMarked(td));
        }
Ejemplo n.º 27
0
        protected IList <CustomAttribute> SweepCustomAttributes(ICustomAttributeProvider provider)
        {
            var removed = new List <CustomAttribute>();

            for (int i = provider.CustomAttributes.Count - 1; i >= 0; i--)
            {
                var attribute = provider.CustomAttributes [i];
                if (!Annotations.IsMarked(attribute))
                {
                    CustomAttributeUsageRemoved(provider, attribute);
                    removed.Add(provider.CustomAttributes [i]);
                    provider.CustomAttributes.RemoveAt(i);
                }
            }

            return(removed);
        }
Ejemplo n.º 28
0
 bool IsLinqProvider(TypeDefinition type)
 {
     // skip unmarked types.
     if (!Annotations.IsMarked(type))
     {
         return(false);
     }
     if (type.Namespace == "System.Linq")
     {
         return(false);                // we are not looking for default system types.
     }
     if (!type.HasInterfaces)
     {
         return(false);
     }
     return(type.Interfaces.Any(i => i.FullName == "System.Linq.IQueryProvider") ||
            type.Interfaces.Select(t => t.Resolve()).Any(t => t != null && IsLinqProvider(t)));
 }
        void MarkProperty(TypeDefinition type, PropertyDefinition property, string signature, string[] accessors)
        {
            if (property != null)
            {
                if (Annotations.IsMarked(property))
                {
                    Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {property.FullName}");
                }

                Annotations.Mark(property);

                MarkPropertyAccessors(type, property, accessors);
            }
            else
            {
                AddUnresolveMarker(string.Format("T: {0}; P: {1}", type, signature));
            }
        }
Ejemplo n.º 30
0
        protected virtual void ProcessType(TypeDefinition type, XPathNavigator nav)
        {
#if !FEATURE_ILLINK
            if (IsExcluded(nav))
            {
                return;
            }
#endif

            TypePreserve preserve = GetTypePreserve(nav);
            if (preserve != TypePreserve.Nothing)
            {
                Annotations.SetPreserve(type, preserve);
            }

            bool required = IsRequired(nav);
            MarkChildren(type, nav, required);

            if (!required)
            {
                return;
            }

            if (Annotations.IsMarked(type))
            {
                var existingLevel  = Annotations.TryGetPreserve(type, out TypePreserve existingPreserve) ? existingPreserve : TypePreserve.Nothing;
                var duplicateLevel = preserve != TypePreserve.Nothing ? preserve : nav.HasChildren ? TypePreserve.Nothing : TypePreserve.All;
                Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {type.FullName} ({existingLevel}).  Duplicate uses ({duplicateLevel})");
            }

            Annotations.Mark(type, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation));

            if (type.IsNested)
            {
                var currentType = type;
                while (currentType.IsNested)
                {
                    var parent = currentType.DeclaringType;
                    Context.Annotations.Mark(parent, new DependencyInfo(DependencyKind.DeclaringType, currentType));
                    currentType = parent;
                }
            }
        }