Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions docs/code/class-handles.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ It's highly desirable to use a direct native reference to Objective-C classes wh

On the other hand there's one scenario when a direct native reference is not desirable: when the native Objective-C class does not exist.

This can happen for third-party bindings that declare a `[BaseType (typeof (NSObject))]`
type for something that is a protocol - and not a class - natively (bgen generates a
`[Protocol]` attribute on such a type). There's no native Objective-C class for these types
(the static registrar doesn't even register them), so a direct native reference would turn
into a hard link error (`Undefined symbols: _OBJC_CLASS_$_TheClass`), even if the class is
never actually used at runtime.

To avoid this, we don't inline `Class.GetHandle` when the target class has a `[Protocol]`
attribute: the call falls back to a runtime lookup instead, which returns a zero handle
(`IntPtr.Zero`) for a missing native class - the same behavior these bindings had before
`Class.GetHandle` inlining existed.

In order to create a direct native reference to Objective-C classes, we need to know the names of those Objective-C classes.

## The `InlineClassGetHandle` property
Expand Down
4 changes: 1 addition & 3 deletions tests/linker/link all/dotnet/shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@
<ImageAsset Include="$(ThisTestDirectory)\Assets.xcassets\AppIcons.appiconset\icon-1024.png" />
</ItemGroup>
<ItemGroup>
<!-- The following three classes are managed classes with [Protocol]+[BaseType], in which case we don't create a corresponding Objective-C class, so there's no native Objective-C symbol to link with -->
<ReferenceNativeSymbol Include="P2" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
<ReferenceNativeSymbol Include="ProtocolWithBlockProperties" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
<!-- TestProtocolRegister is a managed class registered with an Objective-C name, but there's no native Objective-C class with that name (a test verifies Class.GetHandle returns zero for it), so there's no native symbol to link with. Classes with a [Protocol] attribute (such as P2 and ProtocolWithBlockProperties) are handled automatically by InlineClassGetHandleStep and don't need to be listed here. -->
<ReferenceNativeSymbol Include="TestProtocolRegister" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions tests/linker/trimmode link/dotnet/shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@
<ImageAsset Include="$(ThisTestDirectory)\Assets.xcassets\AppIcons.appiconset\icon-1024.png" />
</ItemGroup>
<ItemGroup>
<!-- The following three classes are managed classes with [Protocol]+[BaseType], in which case we don't create a corresponding Objective-C class, so there's no native Objective-C symbol to link with -->
<ReferenceNativeSymbol Include="P2" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
<ReferenceNativeSymbol Include="ProtocolWithBlockProperties" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
<!-- TestProtocolRegister is a managed class registered with an Objective-C name, but there's no native Objective-C class with that name (a test verifies Class.GetHandle returns zero for it), so there's no native symbol to link with. Classes with a [Protocol] attribute (such as P2 and ProtocolWithBlockProperties) are handled automatically by InlineClassGetHandleStep and don't need to be listed here. -->
<ReferenceNativeSymbol Include="TestProtocolRegister" SymbolType="ObjectiveCClass" SymbolMode="Ignore" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ bool isOurOwnCode ()
}

if (objCType is not null) {
if (objCType.IsFakeProtocol) {
// This is a class with a [Protocol] attribute (typically a binding that declares a
// [BaseType (typeof (NSObject))] type for something that is a protocol - and not a class -
// natively). The static registrar doesn't register these classes, so there's no native
// Objective-C class to reference, which means inlining the call to Class.GetHandle would
// turn into a hard link error ('Undefined symbols: _OBJC_CLASS_$_X'). Don't inline it, so
// that the call falls back to a runtime lookup (which returns a zero handle for a missing class).
App.Log (3, "Not inlining the call to Class.GetHandle (\"{0}\") in method {1} because the class has a [Protocol] attribute, so there's no native Objective-C class to reference.", objectiveCClassName, FormatMethod (method));
continue;
}

if (DerivedLinkContext.App.IsSimulatorBuild) {
if (DerivedLinkContext.HasAvailabilityAttributesShowingUnavailableInSimulator (objCType.Type.Resolve (), method)) {
App.Log (3, "Not inlining the call to Class.GetHandle (\"{0}\") in method {1} because the type is marked with an attribute indicating it's not available in the simulator.", objectiveCClassName, FormatMethod (method));
Expand Down