Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/bgen/AttributeConversionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#nullable enable

public static class AttributeConversionManager {
public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute, PlatformName platform)
public static Attribute ConvertPlatformAttribute (AttributeManager attributeManager, CustomAttributeData attribute, PlatformName platform)
{
var constructorArguments = new object [attribute.ConstructorArguments.Count];
for (int i = 0; i < attribute.ConstructorArguments.Count; ++i)
Expand All @@ -23,7 +23,7 @@ public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute,

if (AttributeFactory.ConstructorArguments.TryGetCtorArguments (
constructorArguments, platform, out var ctorValues, out var ctorTypes)) {
return AttributeFactory.CreateNewAttribute<IntroducedAttribute> (ctorTypes!, ctorValues!);
return AttributeFactory.CreateNewAttribute<IntroducedAttribute> (attributeManager, ctorTypes!, ctorValues!);
}

throw new NotImplementedException (unknownFormatError ());
Expand Down Expand Up @@ -99,7 +99,7 @@ static ParsedAvailabilityInfo DetermineOldAvailabilityVersion (CustomAttributeNa
throw new NotImplementedException ($"Unknown version format \"{enumName}\" in DetermineOldAvailabilityVersion");
}

public static IEnumerable<Attribute> ConvertAvailability (CustomAttributeData attribute)
public static IEnumerable<Attribute> ConvertAvailability (AttributeManager attributeManager, CustomAttributeData attribute)
{
string? message = null;
if (attribute.NamedArguments is null)
Expand All @@ -112,22 +112,22 @@ public static IEnumerable<Attribute> ConvertAvailability (CustomAttributeData at
switch (arg.MemberName) {
case "Introduced": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateNewAttribute<IntroducedAttribute> (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
yield return AttributeFactory.CreateNewAttribute<IntroducedAttribute> (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Deprecated": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateNewAttribute<DeprecatedAttribute> (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
yield return AttributeFactory.CreateNewAttribute<DeprecatedAttribute> (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Obsoleted": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateNewAttribute<ObsoletedAttribute> (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
yield return AttributeFactory.CreateNewAttribute<ObsoletedAttribute> (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Unavailable": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateNewAttribute<UnavailableAttribute> (availInfo.Platform, message: message);
yield return AttributeFactory.CreateNewAttribute<UnavailableAttribute> (attributeManager, availInfo.Platform, message: message);
continue;
}
case "Message":
Expand Down
17 changes: 10 additions & 7 deletions src/bgen/AttributeFactory.ConstructorArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
public static partial class AttributeFactory {
public static readonly Type PlatformEnum = typeof (PlatformName);

static readonly Type [] ctorTypes_platform_message = new [] { PlatformEnum, typeof (string) };
static readonly Type [] ctorTypes_platform_major_minor_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) };
static readonly Type [] ctorTypes_platform_major_minor_build_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) };

public readonly struct ConstructorArguments {
readonly PlatformName platform;
readonly int? major;
Expand Down Expand Up @@ -52,13 +56,12 @@ public ConstructorArguments (PlatformName platformIn, string? messageIn)

public Type [] GetCtorTypes ()
{
if (major is null || minor is null) {
return new [] { PlatformEnum, typeof (string) };
}
if (major is null || minor is null)
return ctorTypes_platform_message;

if (build is null)
return new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) };
return new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) };
return ctorTypes_platform_major_minor_message;
return ctorTypes_platform_major_minor_build_message;
}

public static bool TryGetCtorArguments (object [] constructorArguments, PlatformName platform, [NotNullWhen (true)] out object? []? ctorValues, [NotNullWhen (true)] out Type []? ctorTypes)
Expand All @@ -71,7 +74,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform
if (constructorArguments [0] is byte &&
constructorArguments [1] is byte) {
ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], null };
ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) };
ctorTypes = ctorTypes_platform_major_minor_message;
return true;
}

Expand All @@ -81,7 +84,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform
constructorArguments [1] is byte &&
constructorArguments [2] is byte) {
ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], (int) (byte) constructorArguments [2], null };
ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) };
ctorTypes = ctorTypes_platform_major_minor_build_message;
return true;
}
return false;
Expand Down
81 changes: 67 additions & 14 deletions src/bgen/AttributeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Comment thread
rolfbjarne marked this conversation as resolved.
Resolved

#nullable enable

public static partial class AttributeFactory {

public static T CreateNewAttribute<T> (Type [] ctorTypes, object? [] ctorValues)
public static T CreateNewAttribute<T> (AttributeManager attributeManager, Type [] ctorTypes, object? [] ctorValues)
where T : Attribute
{
var constructorCache = attributeManager.constructorCache;
var attribType = typeof (T);
var ctor = attribType.GetConstructor (ctorTypes);
if (ctor is null)
throw ErrorHelper.CreateError (1058, attribType.FullName);
if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) {
ctor = attribType.GetConstructor (ctorTypes);
if (ctor is null)
throw ErrorHelper.CreateError (1058, attribType.FullName);
constructorCache [(attribType, ctorTypes)] = ctor;
}

return (T) ctor.Invoke (ctorValues);
}

public static T CreateNewAttribute<T> (PlatformName platform, int major, int minor, string? message = null)
public static T CreateNewAttribute<T> (AttributeManager attributeManager, PlatformName platform, int major, int minor, string? message = null)
where T : Attribute
{
var args = new ConstructorArguments (platform, major, minor, message);
return CreateNewAttribute<T> (args.GetCtorTypes (), args.GetCtorValues ());
return CreateNewAttribute<T> (attributeManager, args.GetCtorTypes (), args.GetCtorValues ());
}

public static T CreateNewAttribute<T> (PlatformName platform, int major, int minor, int build, string? message = null)
public static T CreateNewAttribute<T> (AttributeManager attributeManager, PlatformName platform, int major, int minor, int build, string? message = null)
where T : Attribute
{
var args = new ConstructorArguments (platform, major, minor, build, message);
return CreateNewAttribute<T> (args.GetCtorTypes (), args.GetCtorValues ());
return CreateNewAttribute<T> (attributeManager, args.GetCtorTypes (), args.GetCtorValues ());
}

public static T CreateNewAttribute<T> (PlatformName platform, string? message = null) where T : Attribute
public static T CreateNewAttribute<T> (AttributeManager attributeManager, PlatformName platform, string? message = null) where T : Attribute
{
var args = new ConstructorArguments (platform, message);
return CreateNewAttribute<T> (args.GetCtorTypes (), args.GetCtorValues ());
return CreateNewAttribute<T> (attributeManager, args.GetCtorTypes (), args.GetCtorValues ());
}

static readonly IntroducedAttribute [] noVersionSupportedCache = new IntroducedAttribute [] {
new (PlatformName.iOS),
new (PlatformName.TvOS),
new (PlatformName.MacOSX),
new (PlatformName.MacCatalyst),
};

static readonly UnavailableAttribute [] unsupportedCache = new UnavailableAttribute [] {
new (PlatformName.iOS),
new (PlatformName.MacCatalyst),
new (PlatformName.MacOSX),
new (PlatformName.TvOS),
};

public static IntroducedAttribute CreateNoVersionSupportedAttribute (PlatformName platform)
{
switch (platform) {
case PlatformName.iOS:
return noVersionSupportedCache [0];
case PlatformName.TvOS:
return noVersionSupportedCache [1];
case PlatformName.MacOSX:
return noVersionSupportedCache [2];
case PlatformName.MacCatalyst:
return new (platform);
return noVersionSupportedCache [3];
case PlatformName.WatchOS:
throw new InvalidOperationException ("CreateNoVersionSupportedAttribute for WatchOS never makes sense");
default:
Expand All @@ -55,10 +77,13 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat
{
switch (platform) {
case PlatformName.iOS:
return unsupportedCache [0];
case PlatformName.MacCatalyst:
return unsupportedCache [1];
case PlatformName.MacOSX:
return unsupportedCache [2];
case PlatformName.TvOS:
return new (platform);
return unsupportedCache [3];
case PlatformName.WatchOS:
throw new InvalidOperationException ("CreateUnsupportedAttribute for WatchOS never makes sense");
default:
Expand All @@ -68,6 +93,16 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat

public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBaseAttribute attr, PlatformName platform)
{
if (attr.Version is null && string.IsNullOrEmpty (attr.Message)) {
// For no-version, no-message attributes, return the cached singletons
switch (attr.AvailabilityKind) {
case AvailabilityKind.Introduced:
return CreateNoVersionSupportedAttribute (platform);
case AvailabilityKind.Unavailable:
return CreateUnsupportedAttribute (platform);
}
}

if (attr.Version is null) {
switch (attr.AvailabilityKind) {
case AvailabilityKind.Introduced:
Expand All @@ -88,8 +123,26 @@ public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBase
// So determine if the build is -1, and use the 2 or 3 param ctor...
var version = attr.Version;
var minimum = Xamarin.SdkVersions.GetMinVersion (platform.AsApplePlatform ());
if (version < minimum)
version = minimum;
if (version <= minimum) {
// If the version is at or below the platform minimum, it's redundant,
// so create a no-version attribute instead of clamping.
switch (attr.AvailabilityKind) {
case AvailabilityKind.Introduced:
return string.IsNullOrEmpty (attr.Message)
? CreateNoVersionSupportedAttribute (platform)
: new IntroducedAttribute (platform, message: attr.Message);
case AvailabilityKind.Deprecated:
return new DeprecatedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message);
case AvailabilityKind.Obsoleted:
return new ObsoletedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message);
case AvailabilityKind.Unavailable:
return string.IsNullOrEmpty (attr.Message)
? CreateUnsupportedAttribute (platform)
: new UnavailableAttribute (platform, message: attr.Message);
default:
throw new NotImplementedException ();
}
}

if (version.Build == -1) {
switch (attr.AvailabilityKind) {
Expand Down
Loading
Loading