diff --git a/src/rgen/Microsoft.Macios.Generator/Attributes/MarshalDirective.cs b/src/rgen/Microsoft.Macios.Generator/Attributes/MarshalDirective.cs index e9296c269b2..a4dbfdbc47f 100644 --- a/src/rgen/Microsoft.Macios.Generator/Attributes/MarshalDirective.cs +++ b/src/rgen/Microsoft.Macios.Generator/Attributes/MarshalDirective.cs @@ -17,7 +17,7 @@ public static bool ShouldMarshalNativeExceptions (this ExportData self) wh ObjCBindings.Property.CustomMarshalDirective), _ => false, }; - + public static CustomMarshalDirective? ToCustomMarshalDirective (this ExportData self) where T : Enum { var present = self switch { @@ -33,5 +33,5 @@ public static bool ShouldMarshalNativeExceptions (this ExportData self) wh return null; } - + } diff --git a/src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs b/src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs index 2c6c5c84fb2..1b8f31ec340 100644 --- a/src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs +++ b/src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs @@ -31,7 +31,7 @@ public TypeInfo ReturnType { get => returnType; private init { returnType = value; - ValueParameter = new Parameter(0, returnType, "value"); + ValueParameter = new Parameter (0, returnType, "value"); } } diff --git a/src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs b/src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs index 2c050c33619..5717c87b524 100644 --- a/src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs +++ b/src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs @@ -14,7 +14,7 @@ namespace Microsoft.Macios.Generator.DataModel; /// Readonly structure that represents a change in a method return type. /// readonly partial struct TypeInfo : IEquatable { - + public static TypeInfo Void = new ("void", SpecialType.System_Void) { Parents = ["System.ValueType", "object"], }; readonly string fullyQualifiedName = string.Empty; @@ -26,8 +26,8 @@ public string FullyQualifiedName { init { fullyQualifiedName = value; var index = fullyQualifiedName.LastIndexOf ('.'); - Name = index != -1 - ? fullyQualifiedName.Substring (index + 1) + Name = index != -1 + ? fullyQualifiedName.Substring (index + 1) : fullyQualifiedName; } } @@ -80,7 +80,7 @@ public string FullyQualifiedName { /// Returns if the return type is a reference type. /// public bool IsReferenceType { get; } - + /// /// Returns if the type is a struct. /// @@ -225,43 +225,36 @@ public override int GetHashCode () const string NativeHandle = "NativeHandle"; const string IntPtr = "IntPtr"; const string UIntPtr = "UIntPtr"; - + public string? ToMarshallType (ReferenceKind referenceKind) { var type = this switch { // special cases based on name - { Name: "nfloat" or "NFloat" } => "nfloat", - { Name: "nint" or "nuint"} => MetadataName, + { Name: "nfloat" or "NFloat" } => "nfloat", { Name: "nint" or "nuint" } => MetadataName, // special string case - { SpecialType: SpecialType.System_String} => NativeHandle, // use a NSString when we get a string - + { SpecialType: SpecialType.System_String } => NativeHandle, // use a NSString when we get a string + // NSObject should use the native handle - { IsNSObject: true } => NativeHandle, - { IsINativeObject: true } => NativeHandle, - + { IsNSObject: true } => NativeHandle, { IsINativeObject: true } => NativeHandle, + // structs will use their name - { IsStruct: true, SpecialType: SpecialType.System_Double } => "Double", - { IsStruct: true } => Name, - + { IsStruct: true, SpecialType: SpecialType.System_Double } => "Double", { IsStruct: true } => Name, + // enums: // IsSmartEnum: We are using a nsstring, so it should be a native handle. // IsNativeEnum: Depends if the enum backing field kind. // GeneralEnum: Depends on the EnumUnderlyingType - - { IsSmartEnum: true } => NativeHandle, - { IsNativeEnum: true, EnumUnderlyingType: SpecialType.System_Int64 } => IntPtr, - { IsNativeEnum: true, EnumUnderlyingType: SpecialType.System_UInt64 } => UIntPtr, - { IsEnum: true, EnumUnderlyingType: not null } => EnumUnderlyingType.GetKeyword (), - + + { IsSmartEnum: true } => NativeHandle, { IsNativeEnum: true, EnumUnderlyingType: SpecialType.System_Int64 } => IntPtr, { IsNativeEnum: true, EnumUnderlyingType: SpecialType.System_UInt64 } => UIntPtr, { IsEnum: true, EnumUnderlyingType: not null } => EnumUnderlyingType.GetKeyword (), + // special type that is a keyword (none would be a ref type) - { SpecialType: SpecialType.System_Void } => SpecialType.GetKeyword (), - { SpecialType: not SpecialType.None } => MetadataName, - - + { SpecialType: SpecialType.System_Void } => SpecialType.GetKeyword (), { SpecialType: not SpecialType.None } => MetadataName, + + // This should not happen in bindings because all of the types should either be native objects // nsobjects, or structs { IsReferenceType: false } => Name, - + _ => null, }; return type; diff --git a/src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs b/src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs index 3dbc1d9b013..cac09be1974 100644 --- a/src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs +++ b/src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs @@ -12,7 +12,7 @@ namespace Microsoft.Macios.Generator.Emitters; -static partial class BindingSyntaxFactory{ +static partial class BindingSyntaxFactory { readonly static string objc_msgSend = "objc_msgSend"; readonly static string objc_msgSendSuper = "objc_msgSendSuper"; @@ -23,12 +23,12 @@ static partial class BindingSyntaxFactory{ if (flags is null) // flags are not set, should be a bug, but we will return null return null; - + // the name of the objcSend method is calculated in the following way // {CustomMarshallPrefix}_{MarshallTypeOfReturnType}_{objcSendMsg}{stret?_stret}_{string.Join('_', MarshallTypeArgs)}{nativeException?_exception}{CustomMarsahllPostfix} // we will sue a sb to make things easy to follow var sb = new StringBuilder (); - + // first, decide if the user created a custom marshalling by checking the flags of the export data CustomMarshalDirective? customMarshalDirective = null; if (flags.HasCustomMarshalDirective ()) { @@ -43,7 +43,7 @@ static partial class BindingSyntaxFactory{ // return types do not have a reference kind sb.Append (returnType.ToMarshallType (ReferenceKind.None)); - sb.Append ('_'); + sb.Append ('_'); // append the msg method based if it is for super or not, do not append '_' intimidatingly, since if we do // not have parameters, we are done sb.Append (isSuper ? objc_msgSendSuper : objc_msgSend); @@ -53,11 +53,11 @@ static partial class BindingSyntaxFactory{ // loop over params and get their native handler name if (parameters.Length > 0) { sb.Append ('_'); - sb.AppendJoin ('_', parameters.Select ( p => p.Type.ToMarshallType (p.ReferenceKind))); + sb.AppendJoin ('_', parameters.Select (p => p.Type.ToMarshallType (p.ReferenceKind))); } // check if we do have a custom marshall exception set for the export - + // check any possible custom postfix naming if (customMarshalDirective?.NativeSuffix is not null) { sb.Append (customMarshalDirective.NativeSuffix); @@ -76,17 +76,17 @@ public static (string? Getter, string? Setter) GetObjCMessageSendMethods (in Pro var getter = property.GetAccessor (AccessorKind.Getter); string? getterMsgSend = null; if (getter is not null) { - var getterExportData = getter.Value.ExportPropertyData ?? property.ExportPropertyData; + var getterExportData = getter.Value.ExportPropertyData ?? property.ExportPropertyData; if (getterExportData is not null) { getterMsgSend = GetObjCMessageSendMethodName (getterExportData.Value, property.ReturnType, [], isSuper, isStret); } } - + var setter = property.GetAccessor (AccessorKind.Setter); string? setterMsgSend = null; if (setter is not null) { - var setterExportData = setter.Value.ExportPropertyData ?? property.ExportPropertyData; + var setterExportData = setter.Value.ExportPropertyData ?? property.ExportPropertyData; if (setterExportData is not null) { setterMsgSend = GetObjCMessageSendMethodName (setterExportData.Value, TypeInfo.Void, [property.ValueParameter], isSuper, isStret); @@ -99,6 +99,6 @@ public static (string? Getter, string? Setter) GetObjCMessageSendMethods (in Pro } public static string? GetObjCMessageSendMethod (in Method method, bool isSuper = false, bool isStret = false) - => GetObjCMessageSendMethodName (method.ExportMethodData, method.ReturnType, method.Parameters, isSuper, isStret); - + => GetObjCMessageSendMethodName (method.ExportMethodData, method.ReturnType, method.Parameters, isSuper, isStret); + } diff --git a/src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs b/src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs index f6ac4c6e0db..5c8bfc63c28 100644 --- a/src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs +++ b/src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs @@ -6,7 +6,7 @@ namespace Microsoft.Macios.Generator.Extensions; static class EnumExtensions { - + public static bool HasCustomMarshalDirective (this Enum self) { return self switch { @@ -16,8 +16,9 @@ public static bool HasCustomMarshalDirective (this Enum self) _ => false }; } - - public static bool HasMarshalNativeExceptions (this Enum self) { + + public static bool HasMarshalNativeExceptions (this Enum self) + { return self switch { // cast two the flags we know that could have the value and return it ObjCBindings.Method methodFlag => methodFlag.HasFlag (ObjCBindings.Method.MarshalNativeExceptions), diff --git a/src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs b/src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs index b2bdef72d3a..6e14d5ca519 100644 --- a/src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs +++ b/src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs @@ -11,7 +11,7 @@ static class SpecialTypeExtensions { public static string? GetKeyword (this SpecialType? self) => self?.GetKeyword (); - + /// /// Return the keyword for a given special type. /// diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoToMarshallTypeTests.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoToMarshallTypeTests.cs index dc0e6fbd74e..4ac0910c840 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoToMarshallTypeTests.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoToMarshallTypeTests.cs @@ -30,7 +30,7 @@ public class Example { } "; yield return [nfloatProperty, "nfloat"]; - + const string otherNfloatProperty = @" using System; using System.Runtime.InteropServices; @@ -56,9 +56,9 @@ public class Example { public string Texturing { get; set; } } "; - + yield return [systemString, "NativeHandle"]; - + const string nsstring = @" using System; using Foundation; @@ -70,11 +70,11 @@ public class Example { public NSString Texturing { get; set; } } "; - + yield return [nsstring, "NativeHandle"]; - + //const string nsobject - + const string nativeEnumInt64 = @" using System; using ObjCRuntime; @@ -93,7 +93,7 @@ public class Example { } "; yield return [nativeEnumInt64, "IntPtr"]; - + const string nativeEnumUInt64 = @" using System; using ObjCRuntime; @@ -149,7 +149,7 @@ public class Example { "; yield return [normalEnum, "ulong"]; - + const string boolProperty = @" using System; @@ -188,7 +188,7 @@ public class Example { "; yield return [structureProperty, "Point"]; } - + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/GetObjCMessageSendMethodNameTests.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/GetObjCMessageSendMethodNameTests.cs index eebe3dfd677..1ce2de7caf4 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/GetObjCMessageSendMethodNameTests.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/GetObjCMessageSendMethodNameTests.cs @@ -36,7 +36,7 @@ class ARAnchor { } "; yield return [propertyNSObjectGetter, "NativeHandle_objc_msgSend", null!, false, false]; - + const string propertyNSObjectGetterSetter = @" using System; using ARKit; @@ -71,9 +71,9 @@ class ARAnchor { public string Name { get; } } "; - + yield return [propertyNSStringGetter, "NativeHandle_objc_msgSend", null!, false, false]; - + const string propertyNSStringGetterSetter = @" using System; using ARKit; @@ -90,7 +90,7 @@ class ARAnchor { public string Name { get; set; } } "; - + yield return [propertyNSStringGetterSetter, "NativeHandle_objc_msgSend", "void_objc_msgSend_NativeHandle", false, false]; const string customMarshall = @" @@ -117,7 +117,7 @@ NMatrix4 Transform { } } "; - + yield return [customMarshall, "xamarin_simd__NMatrix4_objc_msgSend", null!, false, false]; const string floatPropertyGetterSetter = @" @@ -191,7 +191,7 @@ class ARAnchor { } "; - + yield return [nativeEnumUnsigned, "UIntPtr_objc_msgSend", "void_objc_msgSend_UIntPtr", false, false]; const string boolProperty = @" @@ -212,9 +212,9 @@ class ARAnchor { } "; - + yield return [boolProperty, "bool_objc_msgSend", "void_objc_msgSend_bool", false, false]; - + const string nfloatProperty = @" using System; using ARKit; @@ -233,7 +233,7 @@ class ARAnchor { } "; - + yield return [nfloatProperty, "nfloat_objc_msgSend", "void_objc_msgSend_nfloat", false, false]; const string nintProperty = @" @@ -254,9 +254,9 @@ class ARAnchor { } "; - + yield return [nintProperty, "IntPtr_objc_msgSend", "void_objc_msgSend_IntPtr", false, false]; - + const string nuintProperty = @" using System; using ARKit; @@ -275,9 +275,9 @@ class ARAnchor { } "; - + yield return [nuintProperty, "UIntPtr_objc_msgSend", "void_objc_msgSend_UIntPtr", false, false]; - + const string cgsizeProperty = @" using System; using ARKit; @@ -296,9 +296,9 @@ class ARAnchor { } "; - + yield return [cgsizeProperty, "CGSize_objc_msgSend", "void_objc_msgSend_CGSize", false, false]; - + const string doubleProperty = @" using System; using ARKit; @@ -317,13 +317,13 @@ class ARAnchor { } "; - + yield return [doubleProperty, "Double_objc_msgSend", "void_objc_msgSend_Double", false, false]; } - + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); } - + class TestDataGetObjCMessageSendMethodNameSuper : IEnumerable { public IEnumerator GetEnumerator () { @@ -331,30 +331,30 @@ public IEnumerator GetEnumerator () foreach (var args in simple) { // modify the first argument to be the correct msg send args [1] = ((string) args [1]).Replace ("objc_msgSend", "objc_msgSendSuper"); - if (args[2] is not null) + if (args [2] is not null) args [2] = ((string) args [2]).Replace ("objc_msgSend", "objc_msgSendSuper"); args [3] = true; yield return args; } } - + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); } - - class TestDataGetObjCMessageSendMethodNameStret: IEnumerable { + + class TestDataGetObjCMessageSendMethodNameStret : IEnumerable { public IEnumerator GetEnumerator () { var simple = new TestDataGetObjCMessageSendMethodName (); foreach (var args in simple) { // modify the first argument to be the correct msg send args [1] = ((string) args [1]).Replace ("objc_msgSend", "objc_msgSend_stret"); - if (args[2] is not null) + if (args [2] is not null) args [2] = ((string) args [2]).Replace ("objc_msgSend", "objc_msgSend_stret"); args [4] = true; yield return args; } } - + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/Extensions/EnumExtensionTests.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/Extensions/EnumExtensionTests.cs index e71ada00598..f6553d75bd8 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/Extensions/EnumExtensionTests.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/Extensions/EnumExtensionTests.cs @@ -18,10 +18,10 @@ public class EnumExtensionTests { [InlineData (ObjCBindings.Method.CustomMarshalDirective, true)] [InlineData (StringComparison.Ordinal, false)] public void HasCustomMarshalDirective (T enumValue, bool expected) where T : Enum - => Assert.Equal(enumValue.HasCustomMarshalDirective (), expected); - + => Assert.Equal (enumValue.HasCustomMarshalDirective (), expected); + [Theory] [InlineData (ObjCBindings.Property.Notification, false)] public void HasMarshalNativeExceptions (T enumValue, bool expected) where T : Enum - => Assert.Equal(enumValue.HasMarshalNativeExceptions (), expected); + => Assert.Equal (enumValue.HasMarshalNativeExceptions (), expected); }