diff --git a/README.md b/README.md index ed957cc..5fcdcb3 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,26 @@ To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `11.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically. +It also includes the following optional runtime-supported polyfills: +- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)): + - `[DynamicallyAccessedMembers]` + - `[DynamicDependency]` + - `[RequiresUnreferencedCode]` + - `[UnconditionalSuppressMessage]` +- `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/)) +- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute))) +- Platform support annotation attributes (see [docs](https://learn.microsoft.com/dotnet/standard/analyzers/platform-compat-analyzer)): + - `[ObsoletedOSPlatform]` + - `[SupportedOSPlatform]` + - `[SupportedOSPlatformGuard]` + - `[TargetPlatform]` + - `[UnsupportedOSPlatform]` + - `[UnsupportedOSPlatformGuard]` + # Options ⚙️ **PolySharp**'s generation can be configured through some MSBuild properties to set in consuming projects. The following properties are available: - "PolySharpUsePublicAccessibilityForGeneratedTypes": changes the accessibility of generated types from `internal` to `public`. +- "PolySharpIncludeRuntimeSupportedAttributes": enables polyfills for (dummy) runtime-supported attributes too. \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute.cs new file mode 100644 index 0000000..2fe23b3 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute.cs @@ -0,0 +1,135 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// States a dependency that one member has on another. + /// + /// + /// This can be used to inform tooling of a dependency that is otherwise not evident purely from + /// metadata and IL, for example a member relied on via reflection. + /// + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class DynamicDependencyAttribute : Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified signature of a member on the same type as the consumer. + /// + /// The signature of the member depended on. + public DynamicDependencyAttribute(string memberSignature) + { + MemberSignature = memberSignature; + } + + /// + /// Initializes a new instance of the class + /// with the specified signature of a member on a . + /// + /// The signature of the member depended on. + /// The containing . + public DynamicDependencyAttribute(string memberSignature, Type type) + { + MemberSignature = memberSignature; + Type = type; + } + + /// + /// Initializes a new instance of the class + /// with the specified signature of a member on a type in an assembly. + /// + /// The signature of the member depended on. + /// The full name of the type containing the specified member. + /// The assembly name of the type containing the specified member. + public DynamicDependencyAttribute(string memberSignature, string typeName, string assemblyName) + { + MemberSignature = memberSignature; + TypeName = typeName; + AssemblyName = assemblyName; + } + + /// + /// Initializes a new instance of the class + /// with the specified types of members on a . + /// + /// The types of members depended on. + /// The containing the specified members. + public DynamicDependencyAttribute(DynamicallyAccessedMemberTypes memberTypes, Type type) + { + MemberTypes = memberTypes; + Type = type; + } + + /// + /// Initializes a new instance of the class + /// with the specified types of members on a type in an assembly. + /// + /// The types of members depended on. + /// The full name of the type containing the specified members. + /// The assembly name of the type containing the specified members. + public DynamicDependencyAttribute(DynamicallyAccessedMemberTypes memberTypes, string typeName, string assemblyName) + { + MemberTypes = memberTypes; + TypeName = typeName; + AssemblyName = assemblyName; + } + + /// + /// Gets the signature of the member depended on. + /// + /// + /// Either must be a valid string or + /// must not equal , but not both. + /// + public string? MemberSignature { get; } + + /// + /// Gets the which specifies the type + /// of members depended on. + /// + /// + /// Either must be a valid string or + /// must not equal , but not both. + /// + public DynamicallyAccessedMemberTypes MemberTypes { get; } + + /// + /// Gets the containing the specified member. + /// + /// + /// If neither nor are specified, + /// the type of the consumer is assumed. + /// + public Type? Type { get; } + + /// + /// Gets the full name of the type containing the specified member. + /// + /// + /// If neither nor are specified, + /// the type of the consumer is assumed. + /// + public string? TypeName { get; } + + /// + /// Gets the assembly name of the specified type. + /// + /// + /// is only valid when is specified. + /// + public string? AssemblyName { get; } + + /// + /// Gets or sets the condition in which the dependency is applicable, e.g. "DEBUG". + /// + public string? Condition { get; set; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.cs new file mode 100644 index 0000000..2fbe5b6 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.cs @@ -0,0 +1,99 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Specifies the types of members that are dynamically accessed. + /// + /// This enumeration has a attribute that allows a + /// bitwise combination of its member values. + /// + [Flags] + internal enum DynamicallyAccessedMemberTypes + { + /// + /// Specifies no members. + /// + None = 0, + + /// + /// Specifies the default, parameterless public constructor. + /// + PublicParameterlessConstructor = 0x0001, + + /// + /// Specifies all public constructors. + /// + PublicConstructors = 0x0002 | PublicParameterlessConstructor, + + /// + /// Specifies all non-public constructors. + /// + NonPublicConstructors = 0x0004, + + /// + /// Specifies all public methods. + /// + PublicMethods = 0x0008, + + /// + /// Specifies all non-public methods. + /// + NonPublicMethods = 0x0010, + + /// + /// Specifies all public fields. + /// + PublicFields = 0x0020, + + /// + /// Specifies all non-public fields. + /// + NonPublicFields = 0x0040, + + /// + /// Specifies all public nested types. + /// + PublicNestedTypes = 0x0080, + + /// + /// Specifies all non-public nested types. + /// + NonPublicNestedTypes = 0x0100, + + /// + /// Specifies all public properties. + /// + PublicProperties = 0x0200, + + /// + /// Specifies all non-public properties. + /// + NonPublicProperties = 0x0400, + + /// + /// Specifies all public events. + /// + PublicEvents = 0x0800, + + /// + /// Specifies all non-public events. + /// + NonPublicEvents = 0x1000, + + /// + /// Specifies all interfaces implemented by the type. + /// + Interfaces = 0x2000, + + /// + /// Specifies all members. + /// + All = ~None + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute.cs new file mode 100644 index 0000000..354ec75 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute.cs @@ -0,0 +1,56 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that certain members on a specified are accessed dynamically, + /// for example through . + /// + /// + /// This allows tools to understand which members are being accessed during the execution + /// of a program. + /// + /// This attribute is valid on members whose type is or . + /// + /// When this attribute is applied to a location of type , the assumption is + /// that the string represents a fully qualified type name. + /// + /// When this attribute is applied to a class, interface, or struct, the members specified + /// can be accessed dynamically on instances returned from calling + /// on instances of that class, interface, or struct. + /// + /// If the attribute is applied to a method it's treated as a special case and it implies + /// the attribute should be applied to the "this" parameter of the method. As such the attribute + /// should only be used on instance methods of types assignable to System.Type (or string, but no methods + /// will use it there). + /// + [AttributeUsage( + AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | + AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, + Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class DynamicallyAccessedMembersAttribute : Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified member types. + /// + /// The types of members dynamically accessed. + public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) + { + MemberTypes = memberTypes; + } + + /// + /// Gets the which specifies the type + /// of members dynamically accessed. + /// + public DynamicallyAccessedMemberTypes MemberTypes { get; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.cs new file mode 100644 index 0000000..5157dd2 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.cs @@ -0,0 +1,45 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified method requires dynamic access to code that is not referenced + /// statically, for example through . + /// + /// + /// This allows tools to understand which methods are unsafe to call when removing unreferenced + /// code from an application. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class RequiresUnreferencedCodeAttribute : Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified message. + /// + /// + /// A message that contains information about the usage of unreferenced code. + /// + public RequiresUnreferencedCodeAttribute(string message) + { + Message = message; + } + + /// + /// Gets a message that contains information about the usage of unreferenced code. + /// + public string Message { get; } + + /// + /// Gets or sets an optional URL that contains more information about the method, + /// why it requires unreferenced code, and what options a consumer has to deal with it. + /// + public string? Url { get; set; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute.cs new file mode 100644 index 0000000..b05821f --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute.cs @@ -0,0 +1,90 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a + /// single code artifact. + /// + /// + /// is different than + /// in that it doesn't have a + /// . So it is always preserved in the compiled assembly. + /// + [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class UnconditionalSuppressMessageAttribute : Attribute + { + /// + /// Initializes a new instance of the + /// class, specifying the category of the tool and the identifier for an analysis rule. + /// + /// The category for the attribute. + /// The identifier of the analysis rule the attribute applies to. + public UnconditionalSuppressMessageAttribute(string category, string checkId) + { + Category = category; + CheckId = checkId; + } + + /// + /// Gets the category identifying the classification of the attribute. + /// + /// + /// The property describes the tool or tool analysis category + /// for which a message suppression attribute applies. + /// + public string Category { get; } + + /// + /// Gets the identifier of the analysis tool rule to be suppressed. + /// + /// + /// Concatenated together, the and + /// properties form a unique check identifier. + /// + public string CheckId { get; } + + /// + /// Gets or sets the scope of the code that is relevant for the attribute. + /// + /// + /// The Scope property is an optional argument that specifies the metadata scope for which + /// the attribute is relevant. + /// + public string? Scope { get; set; } + + /// + /// Gets or sets a fully qualified path that represents the target of the attribute. + /// + /// + /// The property is an optional argument identifying the analysis target + /// of the attribute. An example value is "System.IO.Stream.ctor():System.Void". + /// Because it is fully qualified, it can be long, particularly for targets such as parameters. + /// The analysis tool user interface should be capable of automatically formatting the parameter. + /// + public string? Target { get; set; } + + /// + /// Gets or sets an optional argument expanding on exclusion criteria. + /// + /// + /// The property is an optional argument that specifies additional + /// exclusion where the literal metadata target is not sufficiently precise. For example, + /// the cannot be applied within a method, + /// and it may be desirable to suppress a violation against a statement in the method that will + /// give a rule violation, but not against all statements in the method. + /// + public string? MessageId { get; set; } + + /// + /// Gets or sets the justification for suppressing the code analysis message. + /// + public string? Justification { get; set; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.StackTraceHiddenAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.StackTraceHiddenAttribute.cs new file mode 100644 index 0000000..8b14b4b --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Diagnostics.StackTraceHiddenAttribute.cs @@ -0,0 +1,23 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics +{ + /// + /// Types and Methods attributed with StackTraceHidden will be omitted from the stack trace text shown in StackTrace.ToString() + /// and Exception.StackTrace + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Struct, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class StackTraceHiddenAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + public StackTraceHiddenAttribute() { } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute.cs new file mode 100644 index 0000000..902a982 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute.cs @@ -0,0 +1,43 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.InteropServices +{ + /// + /// Any method marked with can be directly called from + /// native code. The function token can be loaded to a local variable using the address-of operator + /// in C# and passed as a callback to a native method. + /// + /// + /// Methods marked with this attribute have the following restrictions: + /// * Method must be marked "static". + /// * Must not be called from managed code. + /// * Must only have blittable arguments. + /// + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + [AttributeUsage(AttributeTargets.Method, Inherited = false)] + internal sealed class UnmanagedCallersOnlyAttribute : Attribute + { + public UnmanagedCallersOnlyAttribute() + { + } + + /// + /// Optional. If omitted, the runtime will use the default platform calling convention. + /// + /// + /// Supplied types must be from the official "System.Runtime.CompilerServices" namespace and + /// be of the form "CallConvXXX". + /// + public Type[]? CallConvs; + + /// + /// Optional. If omitted, no named export is emitted during compilation. + /// + public string? EntryPoint; + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.ObsoletedOSPlatformAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.ObsoletedOSPlatformAttribute.cs new file mode 100644 index 0000000..ab4e237 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.ObsoletedOSPlatformAttribute.cs @@ -0,0 +1,46 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Marks APIs that were obsoleted in a given operating system version. + /// + /// + /// Primarily used by OS bindings to indicate APIs that should not be used anymore. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Enum | + AttributeTargets.Event | + AttributeTargets.Field | + AttributeTargets.Interface | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class ObsoletedOSPlatformAttribute : Attribute // OSPlatformAttribute + { + public ObsoletedOSPlatformAttribute(string platformName) + // : base(platformName) + { + } + + public ObsoletedOSPlatformAttribute(string platformName, string? message) + // : base(platformName) + { + Message = message; + } + + public string? Message { get; } + + public string? Url { get; set; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformAttribute.cs new file mode 100644 index 0000000..d5bf143 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformAttribute.cs @@ -0,0 +1,40 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Records the operating system (and minimum version) that supports an API. Multiple attributes can be + /// applied to indicate support on multiple operating systems. + /// + /// + /// Callers can apply a + /// or use guards to prevent calls to APIs on unsupported operating systems. + /// + /// A given platform should only be specified once. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Enum | + AttributeTargets.Event | + AttributeTargets.Field | + AttributeTargets.Interface | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class SupportedOSPlatformAttribute : Attribute // OSPlatformAttribute + { + public SupportedOSPlatformAttribute(string platformName) + // : base(platformName) + { + } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformGuardAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformGuardAttribute.cs new file mode 100644 index 0000000..acf23d0 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.SupportedOSPlatformGuardAttribute.cs @@ -0,0 +1,32 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Annotates a custom guard field, property or method with a supported platform name and optional version. + /// Multiple attributes can be applied to indicate guard for multiple supported platforms. + /// + /// + /// Callers can apply a to a field, property or method + /// and use that field, property or method in a conditional or assert statements in order to safely call platform specific APIs. + /// + /// The type of the field or property should be boolean, the method return type should be boolean in order to be used as platform guard. + /// + [AttributeUsage(AttributeTargets.Field | + AttributeTargets.Method | + AttributeTargets.Property, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class SupportedOSPlatformGuardAttribute : Attribute // OSPlatformAttribute + { + public SupportedOSPlatformGuardAttribute(string platformName) + // : base(platformName) + { + } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.TargetPlatformAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.TargetPlatformAttribute.cs new file mode 100644 index 0000000..0f0360f --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.TargetPlatformAttribute.cs @@ -0,0 +1,22 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Records the platform that the project targeted. + /// + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class TargetPlatformAttribute : Attribute // OSPlatformAttribute + { + public TargetPlatformAttribute(string platformName) + // : base(platformName) + { + } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformAttribute.cs new file mode 100644 index 0000000..3a69efc --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformAttribute.cs @@ -0,0 +1,44 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Marks APIs that were removed in a given operating system version. + /// + /// + /// Primarily used by OS bindings to indicate APIs that are only available in + /// earlier versions. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Enum | + AttributeTargets.Event | + AttributeTargets.Field | + AttributeTargets.Interface | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class UnsupportedOSPlatformAttribute : Attribute // OSPlatformAttribute + { + public UnsupportedOSPlatformAttribute(string platformName) + // : base(platformName) + { + } + public UnsupportedOSPlatformAttribute(string platformName, string? message) + // : base(platformName) + { + Message = message; + } + + public string? Message { get; } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute.cs new file mode 100644 index 0000000..a0748c3 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/Compatibility/System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute.cs @@ -0,0 +1,32 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.Versioning +{ + /// + /// Annotates the custom guard field, property or method with an unsupported platform name and optional version. + /// Multiple attributes can be applied to indicate guard for multiple unsupported platforms. + /// + /// + /// Callers can apply a to a field, property or method + /// and use that field, property or method in a conditional or assert statements as a guard to safely call APIs unsupported on those platforms. + /// + /// The type of the field or property should be boolean, the method return type should be boolean in order to be used as platform guard. + /// + [AttributeUsage(AttributeTargets.Field | + AttributeTargets.Method | + AttributeTargets.Property, + AllowMultiple = true, Inherited = false)] + [global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] + internal sealed class UnsupportedOSPlatformGuardAttribute : Attribute // OSPlatformAttribute + { + public UnsupportedOSPlatformGuardAttribute(string platformName) + // : base(platformName) + { + } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/Extensions/IncrementalValueProviderExtensions.cs b/src/PolySharp.SourceGenerators/Extensions/IncrementalValueProviderExtensions.cs new file mode 100644 index 0000000..31af032 --- /dev/null +++ b/src/PolySharp.SourceGenerators/Extensions/IncrementalValueProviderExtensions.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CodeAnalysis; + +namespace PolySharp.SourceGenerators.Extensions; + +/// +/// Extension methods for the type. +/// +internal static class IncrementalValueProviderExtensions +{ + /// + /// Combines three instances. + /// + /// The type of values produced by the first input. + /// The type of values produced by the second input. + /// The type of values produced by the third input. + /// The first input. + /// The second input. + /// The third input. + /// The resulting combined result. + public static IncrementalValueProvider<(T1, T2, T3)> Combine( + this IncrementalValueProvider source1, + IncrementalValueProvider source2, + IncrementalValueProvider source3) + { + return + source1 + .Combine(source2) + .Combine(source3) + .Select(static (items, _) => (items.Left.Left, items.Left.Right, items.Right)); + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/PolySharp.SourceGenerators.csproj b/src/PolySharp.SourceGenerators/PolySharp.SourceGenerators.csproj index cd333ce..a154b68 100644 --- a/src/PolySharp.SourceGenerators/PolySharp.SourceGenerators.csproj +++ b/src/PolySharp.SourceGenerators/PolySharp.SourceGenerators.csproj @@ -15,7 +15,7 @@ - + PreserveNewest diff --git a/src/PolySharp.SourceGenerators/PolySharp.targets b/src/PolySharp.SourceGenerators/PolySharp.targets index 8004ccd..e35cab6 100644 --- a/src/PolySharp.SourceGenerators/PolySharp.targets +++ b/src/PolySharp.SourceGenerators/PolySharp.targets @@ -37,9 +37,10 @@ - + +