Skip to content

Commit

Permalink
[xcode16] Merge main into xcode16. (#21214)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne authored Sep 11, 2024
2 parents 674ded4 + 1417b22 commit 0f285bb
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update-single-platform-branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
set -ex
git config user.email "github-actions-single-platform-branch-updater@xamarin.com"
git config user.name "GitHub Actions Single Platform Branch Updater"
for platform in dotnet-iOS dotnet-tvOS dotnet-MacCatalyst dotnet-macOS dotnet legacy legacy-iOS legacy-macOS; do
for platform in dotnet-iOS dotnet-tvOS dotnet-MacCatalyst dotnet-macOS dotnet; do
git checkout -b release-test/only-$platform origin/release-test/only-$platform
git merge origin/main
git push
Expand Down
2 changes: 2 additions & 0 deletions docs/website/binding_types_reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,8 @@ Currently, these types are supported:

* `nint`
* `nuint`
* `int`, `uint`
* `long`, `ulong`
* `NSNumber`
* `NSString` (this is the default if none is specified)

Expand Down
5 changes: 5 additions & 0 deletions src/bgen/Caches/NamespaceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class NamespaceCache {
public ICollection<string> UINamespaces { get; private set; }
public ICollection<string> ImplicitNamespaces { get; private set; }
public ICollection<string> NamespacesThatConflictWithTypes { get; private set; }
public ICollection<string> TypesInMultipleNamespaces { get; private set; }

public NamespaceCache (PlatformName currentPlatform, string customObjCRuntimeNS, bool skipSystemDrawing)
{
Expand Down Expand Up @@ -154,6 +155,10 @@ public NamespaceCache (PlatformName currentPlatform, string customObjCRuntimeNS,
"AudioUnit",
};

TypesInMultipleNamespaces = new HashSet<string> {
"NWEndpoint", // Both in Network and NetworkExtension
};

if (!skipSystemDrawing)
ImplicitNamespaces.Add ("System.Drawing");
}
Expand Down
9 changes: 8 additions & 1 deletion src/bgen/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ void GenerateEnum (Type type)
var isBackingFieldValueType = backingFieldType.IsValueType;
var visibility = is_internal ? "internal" : "public";

if (backingFieldType != TypeCache.System_nint && backingFieldType != TypeCache.System_nuint && backingFieldType != TypeCache.NSString && backingFieldType != TypeCache.NSNumber) {
if (backingFieldType != TypeCache.System_nint &&
backingFieldType != TypeCache.System_nuint &&
backingFieldType != TypeCache.System_Int32 &&
backingFieldType != TypeCache.System_Int64 &&
backingFieldType != TypeCache.System_UInt32 &&
backingFieldType != TypeCache.System_UInt64 &&
backingFieldType != TypeCache.NSString &&
backingFieldType != TypeCache.NSNumber) {
exceptions.Add (ErrorHelper.CreateError (1088 /* The backing field type '{0}' is invalid. Valid backing field types are: "NSString", "NSNumber", "nint" and "nuint". */, backingFieldType.FullName));
backingFieldType = TypeCache.NSString;
}
Expand Down
29 changes: 27 additions & 2 deletions src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,22 @@ public bool IsNSObject (Type type)
return false;
}

return type.IsInterface;
// If any of the interfaces this type implements is an NSObject,
// then this type is also an NSObject
var ifaces = type.GetInterfaces ();
foreach (var iface in ifaces)
if (IsNSObject (iface))
return true;

if (type.IsInterface) {
var bta = ReflectionExtensions.GetBaseTypeAttribute (type, this);
if (bta?.BaseType is not null)
return IsNSObject (bta.BaseType);

return false;
}

return false;
}

public string PrimitiveType (Type t, bool formatted = false)
Expand Down Expand Up @@ -604,8 +619,10 @@ public TrampolineInfo MakeTrampoline (Type t)
invoke.AppendFormat (" Runtime.GetINativeObject<{1}> ({0}, false)!", safe_name, pi.ParameterType);
} else if (isForced) {
invoke.AppendFormat (" Runtime.GetINativeObject<{1}> ({0}, true, {2})!", safe_name, TypeManager.RenderType (pi.ParameterType), isForcedOwns);
} else {
} else if (IsNSObject (pi.ParameterType)) {
invoke.AppendFormat (" Runtime.GetNSObject<{1}> ({0})!", safe_name, TypeManager.RenderType (pi.ParameterType));
} else {
invoke.AppendFormat (" Runtime.GetINativeObject<{1}> ({0}, false)!", safe_name, TypeManager.RenderType (pi.ParameterType));
}
continue;
}
Expand Down Expand Up @@ -716,6 +733,11 @@ public TrampolineInfo MakeTrampoline (Type t)
invoke.AppendFormat ("CFArray.ArrayFromHandle<{0}> ({1})!", TypeManager.FormatType (null, et), safe_name);
continue;
}
if (TypeCache.INativeObject.IsAssignableFrom (et)) {
pars.Add (new TrampolineParameterInfo (NativeHandleType, safe_name));
invoke.AppendFormat ("NSArray.ArrayFromHandle<{0}> ({1})!", TypeManager.FormatType (t, et), safe_name);
continue;
}
}

if (pi.ParameterType.IsPointer && pi.ParameterType.GetElementType ().IsValueType) {
Expand Down Expand Up @@ -894,6 +916,9 @@ public string MarshalParameter (MethodInfo mi, ParameterInfo pi, bool null_allow
return safe_name + ".Handle";
}

if (TypeCache.INativeObject.IsAssignableFrom (pi.ParameterType))
return $"{safe_name}.GetHandle ()";

// This means you need to add a new MarshalType in the method "Go"
throw new BindingException (1002, true, pi.ParameterType.FullName, mi.DeclaringType.FullName, mi.Name.GetSafeParamName ());
}
Expand Down
18 changes: 17 additions & 1 deletion src/bgen/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,17 @@ public string FormatTypeUsedIn (string? usedInNamespace, Type? type)

if (t.Namespace is not null) {
string ns = t.Namespace;
if (NamespaceCache.ImplicitNamespaces.Contains (ns) || t.IsGenericType) {
var isImplicitNamespace = NamespaceCache.ImplicitNamespaces.Contains (ns);
var isInMultipleNamespaces = IsInMultipleNamespaces (t);
var nonGlobalCandidate = isImplicitNamespace && !isInMultipleNamespaces;
if (nonGlobalCandidate || t.IsGenericType) {
var targs = t.GetGenericArguments ();
if (targs.Length == 0)
return t.Name + nullable;
return $"global::{t.Namespace}." + t.Name.RemoveArity () + "<" + string.Join (", ", targs.Select (l => FormatTypeUsedIn (null, l)).ToArray ()) + ">" + nullable;
}
if (isInMultipleNamespaces)
return "global::" + t.FullName + nullable;
if (NamespaceCache.NamespacesThatConflictWithTypes.Contains (ns))
return "global::" + t.FullName + nullable;
if (t.Name == t.Namespace)
Expand All @@ -317,6 +322,17 @@ public string FormatTypeUsedIn (string? usedInNamespace, Type? type)
return t.FullName + nullable;
}

bool IsInMultipleNamespaces (Type? type)
{
if (type is null)
return false;

if (NamespaceCache.TypesInMultipleNamespaces.Contains (type.Name))
return true;

return IsInMultipleNamespaces (type.GetElementType ());
}

// TODO: If we ever have an API with nested properties of the same name more than
// 2 deep, we'll need to have this return a list of PropertyInfo and comb through them all.
public PropertyInfo? GetParentTypeWithSameNamedProperty (BaseTypeAttribute bta, string propertyName)
Expand Down
2 changes: 0 additions & 2 deletions system-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ function xcodebuild_download_selected_platforms ()
"$XCODE_DEVELOPER_ROOT/usr/bin/xcodebuild" -downloadPlatform iOS
log "Executing '$XCODE_DEVELOPER_ROOT/usr/bin/xcodebuild -downloadPlatform tvOS' $1"
"$XCODE_DEVELOPER_ROOT/usr/bin/xcodebuild" -downloadPlatform tvOS
log "Executing '$XCODE_DEVELOPER_ROOT/usr/bin/xcodebuild -downloadPlatform watchOS' $1"
"$XCODE_DEVELOPER_ROOT/usr/bin/xcodebuild" -downloadPlatform watchOS
}

function download_xcode_platforms ()
Expand Down
2 changes: 1 addition & 1 deletion tests/bgen/bgen-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>bgen_tests</RootNamespace>

<IsPackable>false</IsPackable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions tests/common/shared-dotnet.mk
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ endif

ifeq ($(RID),)
ifeq ($(PLATFORM),iOS)
RID=ios-arm64
RID=iossimulator-arm64
else ifeq ($(PLATFORM),tvOS)
RID=tvos-arm64
RID=tvossimulator-arm64
else ifeq ($(PLATFORM),MacCatalyst)
ifeq ($(CONFIG),Release)
RID=maccatalyst-x64;maccatalyst-arm64
Expand Down
43 changes: 34 additions & 9 deletions tests/generator/BGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,20 @@ public void Bug35176 ()
Assert.AreEqual (expectedAttributes, renderedAttributes, "Introduced attributes");
}

[Test]
[TestCase (Profile.iOS)]
public void INativeObjectsInBlocks (Profile profile)
{
var bgen = new BGenTool ();
bgen.Profile = profile;
bgen.Defines = BGenTool.GetDefaultDefines (bgen.Profile);
bgen.AddTestApiDefinition ("tests/inativeobjects-in-blocks.cs");
bgen.AddExtraSourcesRelativeToGeneratorDirectory ("tests/inativeobjects-in-blocks-sources.cs");
bgen.CreateTemporaryBinding ();
bgen.AssertExecute ("build");
bgen.AssertNoWarnings ();
}

[Test]
public void Bug36457 ()
{
Expand Down Expand Up @@ -526,6 +540,13 @@ public void StackOverflow20696157 ()
BuildFile (Profile.iOS, "sof20696157.cs");
}

[Test]
[TestCase (Profile.iOS)]
public void TypesInMultipleNamespaces (Profile profile)
{
BuildFile (profile, "tests/types-in-multiple-namespaces.cs");
}

[Test]
public void HyphenInName ()
{
Expand Down Expand Up @@ -598,6 +619,14 @@ public void MultipleApiDefinitions2 ()
BuildFile (Profile.iOS, "multiple-api-definitions2-a.cs", "multiple-api-definitions2-b.cs");
}


[Test]
[TestCase (Profile.iOS)]
public void INativeObjectArraysInBlocks (Profile profile)
{
BuildFile (profile, "tests/inativeobject-arrays-in-blocks.cs");
}

[Test]
[TestCase (Profile.iOS)]
public void ClassNameCollision (Profile profile)
Expand Down Expand Up @@ -1670,6 +1699,10 @@ public void BackingFieldType (Profile profile)
new { BackingFieldType = "NSNumber", NullableType = "Foundation.NSNumber", RenderedBackingFieldType = "Foundation.NSNumber", SimplifiedNullableType = "Foundation.NSNumber" },
new { BackingFieldType = "NSInteger", NullableType = $"System.Nullable`1<{nintName}>", RenderedBackingFieldType = nintName, SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "NSUInteger", NullableType = $"System.Nullable`1<{nuintName}>", RenderedBackingFieldType = nuintName, SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "Int32", NullableType = $"System.Nullable`1<System.Int32>", RenderedBackingFieldType = "System.Int32", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "Int64", NullableType = $"System.Nullable`1<System.Int64>", RenderedBackingFieldType = "System.Int64", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "UInt32", NullableType = $"System.Nullable`1<System.UInt32>", RenderedBackingFieldType = "System.UInt32", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "UInt64", NullableType = $"System.Nullable`1<System.UInt64>", RenderedBackingFieldType = "System.UInt64", SimplifiedNullableType = "System.Nullable`1" },
};

foreach (var tc in testCases) {
Expand Down Expand Up @@ -1698,15 +1731,7 @@ public void BackingFieldType (Profile profile)
public void UnderlyingFieldType (Profile profile)
{
Configuration.IgnoreIfIgnoredPlatform (profile.AsPlatform ());
var bgen = BuildFile (profile, true, true, "tests/underlyingfieldtype.cs");

#if NET
const string nintName = "System.IntPtr";
const string nuintName = "System.UIntPtr";
#else
const string nintName = "System.nint";
const string nuintName = "System.nuint";
#endif
BuildFile (profile, true, true, "tests/underlyingfieldtype.cs");
}

[Test]
Expand Down
14 changes: 14 additions & 0 deletions tests/generator/BGenTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BGenTool : Tool {

public List<string> ApiDefinitions = new List<string> ();
public List<string> Sources = new List<string> ();
public List<string> ExtraSources = new List<string> ();
public List<string> References = new List<string> ();
#if NET
public List<string>? CompileCommand = null;
Expand Down Expand Up @@ -72,6 +73,16 @@ public void AddTestApiDefinition (string filename)
ApiDefinitions.Add (Path.Combine (Configuration.SourceRoot, "tests", "generator", filename));
}

public void AddExtraSourcesRelativeToGeneratorDirectory (string pathRelativeToGeneratorDirectory)
{
ExtraSources.Add (GetFullPathRelativeToGeneratorDirectory (pathRelativeToGeneratorDirectory));
}

public string GetFullPathRelativeToGeneratorDirectory (string pathRelativeToGeneratorDirectory)
{
return Path.Combine (Configuration.SourceRoot, "tests", "generator", pathRelativeToGeneratorDirectory);
}

public AssemblyDefinition ApiAssembly {
get {
return LoadAssembly ();
Expand Down Expand Up @@ -170,6 +181,9 @@ string [] BuildArgumentArray ()
foreach (var s in Sources)
sb.Add ($"-s={s}");

foreach (var x in ExtraSources)
sb.Add ($"-x={x}");

if (ReferenceBclByDefault) {
if (tf is null) {
// do nothing
Expand Down
36 changes: 36 additions & 0 deletions tests/generator/tests/backingfieldtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ enum NSNumberFieldType {
C,
}

[BackingFieldType (typeof (Int32))]
enum Int32FieldType {
[Field ("DField", "__Internal")]
D,
}

[BackingFieldType (typeof (Int64))]
enum Int64FieldType {
[Field ("EField", "__Internal")]
E,
}

[BackingFieldType (typeof (UInt32))]
enum UInt32FieldType {
[Field ("FField", "__Internal")]
F,
}

[BackingFieldType (typeof (UInt64))]
enum UInt64FieldType {
[Field ("GField", "__Internal")]
G,
}

[BaseType (typeof (NSObject))]
interface SomeObj {
[Export ("nsIntegerField")]
Expand All @@ -30,5 +54,17 @@ interface SomeObj {

[Export ("nsNumberField")]
NSNumberFieldType NSNumberField { get; set; }

[Export ("int32Field")]
Int32FieldType Int32Field { get; set; }

[Export ("int64Field")]
Int64FieldType Int64Field { get; set; }

[Export ("uint32Field")]
UInt32FieldType UInt32Field { get; set; }

[Export ("uint64Field")]
UInt64FieldType UInt64Field { get; set; }
}
}
16 changes: 16 additions & 0 deletions tests/generator/tests/inativeobject-arrays-in-blocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

using CoreFoundation;
using CoreVideo;
using Foundation;
using ObjCRuntime;

namespace NS {
delegate void NEDatagramAndFlowEndpointsRead ([NullAllowed] CVPixelBuffer [] remoteEndpoints);

[BaseType (typeof (NSObject))]
interface INativeObjectInBlocks {
[Export ("someOtherProperty")]
NEDatagramAndFlowEndpointsRead SomeOtherProperty { get; set; }
}
}
9 changes: 9 additions & 0 deletions tests/generator/tests/inativeobjects-in-blocks-sources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

using CoreFoundation;
using Foundation;
using ObjCRuntime;

namespace NS {
public partial class DispatchData2 : NativeObject { }
}
21 changes: 21 additions & 0 deletions tests/generator/tests/inativeobjects-in-blocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

using CoreFoundation;
using Foundation;
using ObjCRuntime;

namespace NS {

[Partial]
interface DispatchData2 {
}

delegate void DispatchB (DispatchData2 data);
delegate void DispatchA (DispatchData2 data, [BlockCallback] DispatchB dispatch);

[BaseType (typeof (NSObject))]
interface INativeObjectInBlocks {
[Export ("someProperty")]
DispatchA SomeProperty { get; set; }
}
}
Loading

14 comments on commit 0f285bb

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.