Skip to content

Commit

Permalink
Auto-format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions Autoformatter committed Jan 23, 2025
1 parent 2706f5e commit 15a79c6
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static bool ShouldMarshalNativeExceptions<T> (this ExportData<T> self) wh
ObjCBindings.Property.CustomMarshalDirective),
_ => false,
};

public static CustomMarshalDirective? ToCustomMarshalDirective<T> (this ExportData<T> self) where T : Enum
{
var present = self switch {
Expand All @@ -33,5 +33,5 @@ public static bool ShouldMarshalNativeExceptions<T> (this ExportData<T> self) wh

return null;
}

}
2 changes: 1 addition & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
45 changes: 19 additions & 26 deletions src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Macios.Generator.DataModel;
/// Readonly structure that represents a change in a method return type.
/// </summary>
readonly partial struct TypeInfo : IEquatable<TypeInfo> {

public static TypeInfo Void = new ("void", SpecialType.System_Void) { Parents = ["System.ValueType", "object"], };

readonly string fullyQualifiedName = string.Empty;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public string FullyQualifiedName {
/// Returns if the return type is a reference type.
/// </summary>
public bool IsReferenceType { get; }

/// <summary>
/// Returns if the type is a struct.
/// </summary>
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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 ()) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.Macios.Generator.Extensions;

static class EnumExtensions {

public static bool HasCustomMarshalDirective (this Enum self)
{
return self switch {
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static class SpecialTypeExtensions {

public static string? GetKeyword (this SpecialType? self)
=> self?.GetKeyword ();

/// <summary>
/// Return the keyword for a given special type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Example {
}
";
yield return [nfloatProperty, "nfloat"];

const string otherNfloatProperty = @"
using System;
using System.Runtime.InteropServices;
Expand All @@ -56,9 +56,9 @@ public class Example {
public string Texturing { get; set; }
}
";

yield return [systemString, "NativeHandle"];

const string nsstring = @"
using System;
using Foundation;
Expand All @@ -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;
Expand All @@ -93,7 +93,7 @@ public class Example {
}
";
yield return [nativeEnumInt64, "IntPtr"];

const string nativeEnumUInt64 = @"
using System;
using ObjCRuntime;
Expand Down Expand Up @@ -149,7 +149,7 @@ public class Example {
";

yield return [normalEnum, "ulong"];

const string boolProperty = @"
using System;
Expand Down Expand Up @@ -188,7 +188,7 @@ public class Example {
";
yield return [structureProperty, "Point"];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}

Expand Down
Loading

0 comments on commit 15a79c6

Please sign in to comment.