xamarin-macios/tools/linker/CoreRemoveAttributes.cs

112 строки
3.7 KiB
C#
Исходник Обычный вид История

2016-04-21 15:57:02 +03:00
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Xamarin.Tuner;
2016-04-21 15:57:02 +03:00
namespace Xamarin.Linker {
// this can be used (directly) with Xamarin.Mac and as the base of Xamarin.iOS step
public class CoreRemoveAttributes : MobileRemoveAttributes {
2022-10-11 23:36:58 +03:00
protected DerivedLinkContext LinkContext {
get {
return (DerivedLinkContext) base.context;
}
}
public override bool IsActiveFor (AssemblyDefinition assembly)
{
if (LinkContext.App.Optimizations.CustomAttributesRemoval != true)
return false;
return base.IsActiveFor (assembly);
}
2016-04-21 15:57:02 +03:00
protected override bool IsRemovedAttribute (CustomAttribute attribute)
{
// note: this also avoid calling FullName (which allocates a string)
var attr_type = attribute.Constructor.DeclaringType;
switch (attr_type.Name) {
case "AdviceAttribute":
case "FieldAttribute":
2022-10-11 23:36:58 +03:00
case "PreserveAttribute": // the ApplyPreserveAttribute substep is executed before this
2016-04-21 15:57:02 +03:00
case "LinkerSafeAttribute":
return attr_type.Namespace == Namespaces.Foundation;
// used for documentation, not at runtime
case "AvailabilityAttribute":
case "AvailabilityBaseAttribute":
case "DeprecatedAttribute":
case "IntroducedAttribute":
case "NotImplementedAttribute":
2016-04-21 15:57:02 +03:00
case "ObsoletedAttribute":
case "ThreadSafeAttribute":
case "UnavailableAttribute":
case "LinkWithAttribute":
case "DesignatedInitializerAttribute":
case "RequiresSuperAttribute":
Add a BindingImpl attribute and use to to teach the linker look for it to search for optimizable code. (#3299) * [ObjCRuntime] Add a BindingImplAttribute. * [linker] Make ProviderToString an extension method on ICustomAttributeProvider to make it more discoverable. * [generator] Use [BindingImpl] instead of [CompilerGenerated]. The entire diff is big (89MB), so it can't be gisted. However, most of it is either removal of `using System.Runtime.CompilerServices;` or the change from `[CompilerGenerated]` to `[BindingImpl (...)]` like this: https://gist.github.com/rolfbjarne/8bfda3ed37b956d0342a1c1e9b079244 If I remove those parts of the diff, there's nothing significant left: https://gist.github.com/rolfbjarne/4156164d6bdb1376366200394eb8a091 * [linker] Teach the linker about the new [BindingImpl] attribute. In addition to the existing logic where the linker would optimize some [CompilerGenerated] code (sometimes with additional requirements), it will now also optimize all [BindingImpl (Optimizable)] code (without any additional requirements). * [tests] Add tests to make sure [BindingImpl (Optimizable)] works as expected. * [linker] Check for [BindingImpl] before [CompilerGenerated] and stop checking for [CompilerGenerated] in XAMCORE_4_0. Check for [BindingImpl] before checking for [CompilerGenerated], since the former is more common. Also stop checking for [CompilerGenerated] (at least to mean that code is optimizable) in our next non-compatible evolutionary leap (XAMCORE_4_0): * [introspection] Impl a better typo check.
2018-01-26 20:38:23 +03:00
case "BindingImplAttribute":
case "NoiOSAttribute":
case "NoMacAttribute":
case "NoTVAttribute":
case "NoWatchAttribute":
// special subclasses of IntroducedAttribute
case "TVAttribute":
case "MacCatalystAttribute":
case "WatchAttribute":
2016-04-21 15:57:02 +03:00
return attr_type.Namespace == Namespaces.ObjCRuntime;
// special subclasses of IntroducedAttribute
case "iOSAttribute":
case "MacAttribute":
return String.IsNullOrEmpty (attr_type.Namespace);
case "AdoptsAttribute":
return attr_type.Namespace == Namespaces.ObjCRuntime && LinkContext.App.Optimizations.RegisterProtocols == true;
case "ProtocolAttribute":
case "ProtocolMemberAttribute":
return attr_type.Namespace == Namespaces.Foundation && LinkContext.App.Optimizations.RegisterProtocols == true;
2016-04-21 15:57:02 +03:00
default:
return base.IsRemovedAttribute (attribute);
}
}
protected override void WillRemoveAttribute (ICustomAttributeProvider provider, CustomAttribute attribute)
{
var attr_type = attribute.Constructor.DeclaringType;
if (attr_type.Namespace == Namespaces.ObjCRuntime) {
switch (attr_type.Name) {
2022-10-11 23:36:58 +03:00
case "AvailabilityAttribute": // obsolete (could be present in user code)
case "AvailabilityBaseAttribute": // base type for IntroducedAttribute and DeprecatedAttribute (could be in user code)
2016-04-21 15:57:02 +03:00
case "DeprecatedAttribute":
case "IntroducedAttribute":
// they are subclasses of ObjCRuntime.IntroducedAttribute
case "TVAttribute":
case "MacCatalystAttribute":
case "WatchAttribute":
LinkContext.StoreCustomAttribute (provider, attribute, "Availability");
2016-04-21 15:57:02 +03:00
break;
case "AdoptsAttribute":
Add a BindingImpl attribute and use to to teach the linker look for it to search for optimizable code. (#3299) * [ObjCRuntime] Add a BindingImplAttribute. * [linker] Make ProviderToString an extension method on ICustomAttributeProvider to make it more discoverable. * [generator] Use [BindingImpl] instead of [CompilerGenerated]. The entire diff is big (89MB), so it can't be gisted. However, most of it is either removal of `using System.Runtime.CompilerServices;` or the change from `[CompilerGenerated]` to `[BindingImpl (...)]` like this: https://gist.github.com/rolfbjarne/8bfda3ed37b956d0342a1c1e9b079244 If I remove those parts of the diff, there's nothing significant left: https://gist.github.com/rolfbjarne/4156164d6bdb1376366200394eb8a091 * [linker] Teach the linker about the new [BindingImpl] attribute. In addition to the existing logic where the linker would optimize some [CompilerGenerated] code (sometimes with additional requirements), it will now also optimize all [BindingImpl (Optimizable)] code (without any additional requirements). * [tests] Add tests to make sure [BindingImpl (Optimizable)] works as expected. * [linker] Check for [BindingImpl] before [CompilerGenerated] and stop checking for [CompilerGenerated] in XAMCORE_4_0. Check for [BindingImpl] before checking for [CompilerGenerated], since the former is more common. Also stop checking for [CompilerGenerated] (at least to mean that code is optimizable) in our next non-compatible evolutionary leap (XAMCORE_4_0): * [introspection] Impl a better typo check.
2018-01-26 20:38:23 +03:00
case "BindingImplAttribute":
LinkContext.StoreCustomAttribute (provider, attribute);
break;
2016-04-21 15:57:02 +03:00
}
} else if (attr_type.Namespace == Namespaces.Foundation) {
switch (attr_type.Name) {
case "ProtocolAttribute":
case "ProtocolMemberAttribute":
LinkContext.StoreCustomAttribute (provider, attribute);
break;
}
} else if (String.IsNullOrEmpty (attr_type.Namespace)) {
switch (attr_type.Name) {
// they are subclasses of ObjCRuntime.IntroducedAttribute
case "iOSAttribute":
case "MacAttribute":
LinkContext.StoreCustomAttribute (provider, attribute, "Availability");
break;
}
2016-04-21 15:57:02 +03:00
}
base.WillRemoveAttribute (provider, attribute);
}
}
}