Skip to content

Commit de71f7d

Browse files
Merge branch 'main' into dev/mandel/transformer-thread-safe-data
2 parents 624397d + 3b647ca commit de71f7d

File tree

16 files changed

+158
-15
lines changed

16 files changed

+158
-15
lines changed

msbuild/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
-->
1818
<MessagingVersion Condition="'$(MessagingVersion)' == ''">[3.0.13]</MessagingVersion>
19-
<HotRestartVersion>[17.14.51-gd03e25086a]</HotRestartVersion>
19+
<HotRestartVersion>[17.14.83-g77e8f56103]</HotRestartVersion>
2020
</PropertyGroup>
2121
<Import Project="$(MSBuildThisFileDirectory)../Directory.Build.props" />
2222
</Project>

msbuild/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ IOS_WINDOWS_TARGETS = \
5353

5454
TASK_ASSEMBLIES = Xamarin.MacDev.Tasks $(LOCALIZATION_ASSEMBLIES)
5555
IOS_WINDOWS_TASK_ASSEMBLIES = Xamarin.iOS.Tasks.Windows
56-
IOS_WINDOWS_DEPENDENCIES = Xamarin.iOS.Windows.Client iSign.Core ws2_32 System.Diagnostics.Tracer System.Formats.Asn1 System.Buffers System.Memory System.Numerics.Vectors System.Runtime.CompilerServices.Unsafe System.Security.Cryptography.ProtectedData System.Security.Cryptography.Pkcs Microsoft.Win32.Registry System.Security.AccessControl System.Security.Principal.Windows
56+
IOS_WINDOWS_DEPENDENCIES = Xamarin.iOS.Windows.Client iSign.Core ws2_32 System.Diagnostics.Tracer System.Formats.Asn1 System.Buffers System.Memory System.Numerics.Vectors System.Runtime.CompilerServices.Unsafe System.Security.Cryptography.ProtectedData System.Security.Cryptography.Pkcs Microsoft.Win32.Registry
5757
IOS_WINDOWS_MOBILEDEVICE_TOOLS = iMobileDevice-net bz2 getopt ideviceactivation idevicebackup idevicebackup2 idevicecrashreport idevicedate idevicedebug idevicedebugserverproxy idevicediagnostics ideviceenterrecovery ideviceimagemounter ideviceinfo ideviceinstaller idevicename idevicenotificationproxy idevicepair ideviceprovision idevicerestore idevicescreenshot idevicesyslog idevice_id imobiledevice-net-lighthouse imobiledevice ios_webkit_debug_proxy iproxy irecovery irecovery libcharset libcurl LIBEAY32 libiconv libusb-1.0 libusb0 libxml2 lzma pcre pcreposix plist plistutil plist_cmp plist_test pthreadVC3 readline SSLEAY32 usbmuxd usbmuxd vcruntime140 zip zlib1
5858

5959
PROPS_AND_TARGETS = \

msbuild/Xamarin.iOS.Tasks.Windows/Xamarin.iOS.Tasks.Windows.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="Xamarin.Messaging.Build.Client" Version="$(MessagingVersion)" IncludeAssets="contentFiles" />
2323
<PackageReference Include="Xamarin.Messaging.Core" Version="$(MessagingVersion)" IncludeAssets="build" />
2424
<PackageReference Include="Xamarin.Messaging.Server" Version="$(MessagingVersion)" IncludeAssets="contentFiles" />
25+
<PackageReference Include="iSign.Core" Version="$(HotRestartVersion)" GeneratePathProperty="true" />
2526
<PackageReference Include="Xamarin.iOS.HotRestart.Client" Version="$(HotRestartVersion)" GeneratePathProperty="true" />
2627
<PackageReference Include="System.Diagnostics.Tracer" Version="2.1.0-alpha" GeneratePathProperty="true" />
2728
<!-- GitInfo is pulled in because of Xamarin.Messaging from above, but we don't want it, so just exclude all assets from it -->
@@ -38,7 +39,7 @@
3839
<None Include="*.targets">
3940
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4041
</None>
41-
<None Include="$(PkgXamarin_iOS_HotRestart_Client)\lib\netstandard2.0\iSign.Core.pdb" CopyToOutputDirectory="PreserveNewest" />
42+
<None Include="$(PkgiSign_Core)\lib\netstandard2.0\iSign.Core.pdb" CopyToOutputDirectory="PreserveNewest" />
4243
<None Include="$(PkgXamarin_iOS_HotRestart_Client)\lib\netstandard2.0\Xamarin.iOS.Windows.Client.pdb" CopyToOutputDirectory="PreserveNewest" />
4344
<Compile Include="../Versions.g.cs" />
4445
<Compile Include="..\Xamarin.MacDev.Tasks\PublishFolderType.cs">

src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public override int GetHashCode ()
235235
const string IntPtr = "IntPtr";
236236
const string UIntPtr = "UIntPtr";
237237

238-
public string? ToMarshallType (ReferenceKind referenceKind)
238+
public string? ToMarshallType ()
239239
{
240240
#pragma warning disable format
241241
var type = this switch {

src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,77 @@
55
using System.Collections.Immutable;
66
using System.Linq;
77
using System.Text;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.CSharp.Syntax;
811
using Microsoft.Macios.Generator.Attributes;
912
using Microsoft.Macios.Generator.DataModel;
1013
using Microsoft.Macios.Generator.Extensions;
1114
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;
15+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1216

1317
namespace Microsoft.Macios.Generator.Emitters;
1418

1519
static partial class BindingSyntaxFactory {
1620
readonly static string objc_msgSend = "objc_msgSend";
1721
readonly static string objc_msgSendSuper = "objc_msgSendSuper";
1822

23+
/// <summary>
24+
/// Returns the expression needed to cast a parameter to its native type.
25+
/// </summary>
26+
/// <param name="parameter">The parameter whose castin we need to generate. The type info has to be
27+
/// and enum and be marked as native. If it is not the method returns null</param>
28+
/// <returns></returns>
29+
internal static CastExpressionSyntax? CastToNative (in Parameter parameter)
30+
{
31+
// not an enum and not a native value. we cannot calculate the casting expression.
32+
if (!parameter.Type.IsEnum || !parameter.Type.IsNativeEnum)
33+
return null;
34+
35+
// build a casting expression based on the marshall type of the typeinfo
36+
var marshalType = parameter.Type.ToMarshallType ();
37+
if (marshalType is null)
38+
// cannot calculate the marshal, return null
39+
return null;
40+
41+
var enumBackingValue = parameter.Type.EnumUnderlyingType.Value.GetKeyword ();
42+
var castExpression = CastExpression (IdentifierName (marshalType), // (IntPtr/UIntPtr) cast
43+
CastExpression (
44+
IdentifierName (enumBackingValue),
45+
IdentifierName (parameter.Name)
46+
.WithLeadingTrivia (Space))
47+
.WithLeadingTrivia (Space)); // (backingfield) (variable) cast
48+
return castExpression;
49+
}
50+
51+
/// <summary>
52+
/// Returns the expression needed to cast a bool to a byte to be used in a native call.
53+
/// </summary>
54+
/// <param name="parameter">The parameter to cast.</param>
55+
/// <returns>A conditional expression that casts a bool to a byte.</returns>
56+
internal static ConditionalExpressionSyntax? CastToByte (in Parameter parameter)
57+
{
58+
if (parameter.Type.SpecialType != SpecialType.System_Boolean)
59+
return null;
60+
// (byte) 1
61+
var castOne = CastExpression (
62+
PredefinedType (Token (SyntaxKind.ByteKeyword)),
63+
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (1)).WithLeadingTrivia (Space).WithTrailingTrivia (Space)
64+
);
65+
// (byte) 0
66+
var castZero = CastExpression (
67+
PredefinedType (Token (SyntaxKind.ByteKeyword)),
68+
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (0)).WithLeadingTrivia (Space)
69+
).WithLeadingTrivia (Space);
70+
71+
// with this exact space count
72+
// foo ? (byte) 1 : (byte) 0
73+
return ConditionalExpression (
74+
condition: IdentifierName (parameter.Name).WithTrailingTrivia (Space),
75+
whenTrue: castOne.WithLeadingTrivia (Space),
76+
whenFalse: castZero);
77+
}
78+
1979
static string? GetObjCMessageSendMethodName<T> (ExportData<T> exportData,
2080
TypeInfo returnType, ImmutableArray<Parameter> parameters, bool isSuper = false, bool isStret = false) where T : Enum
2181
{
@@ -42,7 +102,7 @@ static partial class BindingSyntaxFactory {
42102
}
43103

44104
// return types do not have a reference kind
45-
sb.Append (returnType.ToMarshallType (ReferenceKind.None));
105+
sb.Append (returnType.ToMarshallType ());
46106
sb.Append ('_');
47107
// append the msg method based if it is for super or not, do not append '_' intimidatingly, since if we do
48108
// not have parameters, we are done
@@ -53,7 +113,7 @@ static partial class BindingSyntaxFactory {
53113
// loop over params and get their native handler name
54114
if (parameters.Length > 0) {
55115
sb.Append ('_');
56-
sb.AppendJoin ('_', parameters.Select (p => p.Type.ToMarshallType (p.ReferenceKind)));
116+
sb.AppendJoin ('_', parameters.Select (p => p.Type.ToMarshallType ()));
57117
}
58118

59119
// check if we do have a custom marshall exception set for the export

src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static class SpecialTypeExtensions {
1717
/// </summary>
1818
/// <param name="self">The special type to convert.</param>
1919
/// <returns>The string representation of the keyword.</returns>
20-
public static string? GetKeyword (this SpecialType self)
20+
public static string GetKeyword (this SpecialType self)
2121
{
2222
var kind = self switch {
2323
SpecialType.System_Void => SyntaxKind.VoidKeyword,

src/rgen/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator/XamarinBindingAPIGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ void GenerateCode (SourceProductionContext context, Compilation compilation,
242242
var models = new (string Model, AttributeTargets[] Targets) [] {
243243
("EnumMember", [AttributeTargets.Field]),
244244
("Parameter", [AttributeTargets.Parameter]),
245+
("Accessor", [AttributeTargets.Property]),
245246
("Property", [AttributeTargets.Property]),
246247
("Method", [AttributeTargets.Method]),
247248
("Binding", [AttributeTargets.Interface, AttributeTargets.Class, AttributeTargets.Enum, AttributeTargets.Struct]),

src/rgen/Microsoft.Macios.Transformer/DataModel/Accessor.Transformer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ namespace Microsoft.Macios.Generator.DataModel;
88
readonly partial struct Accessor {
99

1010
public ExportData? ExportPropertyData { get; init; }
11+
12+
public Accessor () { }
1113
}

tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoToMarshallTypeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void ToMarshallType (ApplePlatform platform, string inputText, string expectedTy
205205
Assert.NotNull (declaration);
206206
Assert.True (Property.TryCreate (declaration, semanticModel, out var changes));
207207
Assert.NotNull (changes);
208-
var marshall = changes.Value.ReturnType.ToMarshallType (ReferenceKind.None);
208+
var marshall = changes.Value.ReturnType.ToMarshallType ();
209209
Assert.NotNull (marshall);
210210
Assert.Equal (expectedTypeName, marshall);
211211
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.Macios.Generator.DataModel;
8+
using Xunit;
9+
using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory;
10+
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
11+
12+
namespace Microsoft.Macios.Generator.Tests.Emitters;
13+
14+
public class BindingSyntaxFactoryObjCRuntimeTests {
15+
16+
class TestDataCastToNativeTests : IEnumerable<object []> {
17+
public IEnumerator<object []> GetEnumerator ()
18+
{
19+
20+
// not enum parameter
21+
var boolParam = new Parameter (
22+
position: 0,
23+
type: ReturnTypeForBool (),
24+
name: "myParam");
25+
yield return [boolParam, null!];
26+
27+
// not smart enum parameter
28+
var enumParam = new Parameter (
29+
position: 0,
30+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false),
31+
name: "myParam");
32+
33+
yield return [enumParam, null!];
34+
35+
// int64
36+
var byteEnum = new Parameter (
37+
position: 0,
38+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: true, underlyingType: SpecialType.System_Int64),
39+
name: "myParam");
40+
yield return [byteEnum, "(IntPtr) (long) myParam"];
41+
42+
// uint64
43+
var int64Enum = new Parameter (
44+
position: 0,
45+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: true, underlyingType: SpecialType.System_UInt64),
46+
name: "myParam");
47+
yield return [int64Enum, "(UIntPtr) (ulong) myParam"];
48+
}
49+
50+
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
51+
}
52+
53+
[Theory]
54+
[ClassData (typeof (TestDataCastToNativeTests))]
55+
void CastToNativeTests (Parameter parameter, string? expectedCast)
56+
{
57+
var expression = CastToNative (parameter);
58+
if (expectedCast is null) {
59+
Assert.Null (expression);
60+
} else {
61+
Assert.NotNull (expression);
62+
Assert.Equal (expectedCast, expression?.ToString ());
63+
}
64+
}
65+
66+
[Fact]
67+
void CastToByteTests ()
68+
{
69+
var boolParameter = new Parameter (0, ReturnTypeForBool (), "myParameter");
70+
var conditionalExpr = CastToByte (boolParameter);
71+
Assert.NotNull (conditionalExpr);
72+
Assert.Equal ("myParameter ? (byte) 1 : (byte) 0", conditionalExpr.ToString ());
73+
74+
var intParameter = new Parameter (1, ReturnTypeForInt (), "myParameter");
75+
conditionalExpr = CastToByte (intParameter);
76+
Assert.Null (conditionalExpr);
77+
}
78+
}

0 commit comments

Comments
 (0)