diff --git a/src/NearbyInteraction/Enums.cs b/src/NearbyInteraction/Enums.cs index 652c1157621b..2af5fb9afdcb 100644 --- a/src/NearbyInteraction/Enums.cs +++ b/src/NearbyInteraction/Enums.cs @@ -7,14 +7,24 @@ // Copyright 2020 Microsoft Inc. // +using System.ComponentModel; + namespace NearbyInteraction { +#if XAMCORE_5_0 + [NoTV, NoMac, iOS (14, 0)] +#else [NoTV, Mac (12, 0), iOS (14, 0)] +#endif [MacCatalyst (14, 0)] #if !__MACOS__ [ErrorDomain ("NIErrorDomain")] #endif [Native] +#if __MACOS__ && !XAMCORE_5_0 + [Obsolete ("Not available on this platform.")] + [EditorBrowsable (EditorBrowsableState.Never)] +#endif public enum NIErrorCode : long { UnsupportedPlatform = -5889, InvalidConfiguration = -5888, diff --git a/src/Network/NWEnums.cs b/src/Network/NWEnums.cs index 877dabc8c4fa..d9266f4d2a2b 100644 --- a/src/Network/NWEnums.cs +++ b/src/Network/NWEnums.cs @@ -96,6 +96,7 @@ public enum NWReportResolutionSource { [NoTV, NoiOS] [NoMacCatalyst] + [NativeName ("nw_ethernet_channel_state_t")] public enum NWEthernetChannelState { Invalid = 0, Waiting = 1, diff --git a/tests/sharpie/Tests/EnumTypedefAvailability.h b/tests/sharpie/Tests/EnumTypedefAvailability.h new file mode 100644 index 000000000000..5d9e4bfcb3c7 --- /dev/null +++ b/tests/sharpie/Tests/EnumTypedefAvailability.h @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +// RUN objc: -x objective-c + +// Regression test for https://github.com/dotnet/macios/issues/26166 +// Clang attaches an availability attribute written after a `typedef enum { } Name;` +// declaration to the TypedefDecl that names the enum, not to the EnumDecl itself. +// Sharpie must still propagate that availability to the generated C# enum. + +typedef enum { + MacUnavailableEnumOne, + MacUnavailableEnumTwo +} MacUnavailableEnum __attribute__((availability(macosx,unavailable))); diff --git a/tests/sharpie/Tests/EnumTypedefAvailability.objc.cs b/tests/sharpie/Tests/EnumTypedefAvailability.objc.cs new file mode 100644 index 000000000000..60c1e6bc2d6e --- /dev/null +++ b/tests/sharpie/Tests/EnumTypedefAvailability.objc.cs @@ -0,0 +1,5 @@ +[NoMac] +public enum MacUnavailableEnum : uint { + One, + Two +} diff --git a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-AVFoundation.ignore b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-AVFoundation.ignore index a35c2b5ea741..6ed1696f6298 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-AVFoundation.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-AVFoundation.ignore @@ -41,3 +41,6 @@ !missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaCharacteristic(System.String,System.Action`2,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1 !missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0 !missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1 + +# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration. +!unknown-native-enum! AVAudioRoutingArbitrationCategory bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-CoreMotion.ignore b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-CoreMotion.ignore index 05ed300f6628..67027d28df1e 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-CoreMotion.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-CoreMotion.ignore @@ -1,6 +1,3 @@ -## The following types are only available on watchOS (not iOS, so definitively not Catalyst) -!missing-enum! CMFallDetectionEventUserResolution not bound - ## not available on Catalyst (tested with macOS 12 b3) !missing-selector! +CMAltimeter::isAbsoluteAltitudeAvailable not bound !missing-selector! CMAltimeter::startAbsoluteAltitudeUpdatesToQueue:withHandler: not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-FileProvider.ignore b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-FileProvider.ignore index 48e77adae063..cb1e25d635eb 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-FileProvider.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-FileProvider.ignore @@ -1,5 +1,3 @@ -# Documentation as of Xcode 13 as macOS only -!missing-enum! NSFileProviderFileSystemFlags not bound -!missing-enum! NSFileProviderDomainTestingModes not bound -# Header clearly says this enum is not available on Mac Catalyst, not sure why xtro thinks it is. -!missing-enum! NSFileProviderContentPolicy not bound +# These enums are unavailable on Mac Catalyst natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! NSFileProviderItemCapabilities bound +!unknown-native-enum! NSFileProviderKnownFolders bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-NearbyInteraction.ignore b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-NearbyInteraction.ignore index 2fa8bab27b54..c8f4b53a3927 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-NearbyInteraction.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-NearbyInteraction.ignore @@ -1,5 +1,2 @@ # This selector requires ARKit, which doesn't work on Mac Catalyst !missing-selector! NISession::setARSession: not bound -# Headers are clear that these two enums are not supported on Mac Catalyst, don't know why xtro thinks they are -!missing-enum! NIDLTDOACoordinatesType not bound -!missing-enum! NIDLTDOAMeasurementType not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/common-Network.ignore b/tests/xtro-sharpie/api-annotations-dotnet/common-Network.ignore index 21a6b7185f5e..a73b37e21f8d 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/common-Network.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/common-Network.ignore @@ -54,7 +54,6 @@ !missing-enum! nw_data_transfer_report_state_t not bound !missing-enum! nw_endpoint_type_t not bound !missing-enum! nw_error_domain_t not bound -!missing-enum! nw_ethernet_channel_state_t not bound !missing-enum! nw_framer_start_result_t not bound !missing-enum! nw_interface_radio_type_t not bound !missing-enum! nw_interface_type_t not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/iOS-AVFoundation.ignore b/tests/xtro-sharpie/api-annotations-dotnet/iOS-AVFoundation.ignore index a9cf66f62d8c..37c5a1e69974 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/iOS-AVFoundation.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/iOS-AVFoundation.ignore @@ -34,6 +34,9 @@ !missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0 !missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1 +# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration. +!unknown-native-enum! AVAudioRoutingArbitrationCategory bound + ## bound selectors with no native declaration reachable from the class in the current SDK headers: ## private/SPI selectors, protocol/category members Apple inlines without declaring the conformance on ## the class, managed callback infrastructure, or API Apple removed/privatized - kept for compatibility diff --git a/tests/xtro-sharpie/api-annotations-dotnet/iOS-AudioToolbox.ignore b/tests/xtro-sharpie/api-annotations-dotnet/iOS-AudioToolbox.ignore index 648c963fb86d..9268424ba42d 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/iOS-AudioToolbox.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/iOS-AudioToolbox.ignore @@ -1,5 +1,2 @@ !missing-selector! AUAudioUnit::messageChannelFor: not bound !missing-protocol! AUMessageChannel not bound - -# this enum is declared as not available on iOS, not sure why it shows up (it's visionOS-only) -!missing-enum! CASoundStageSize not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/iOS-CoreMotion.ignore b/tests/xtro-sharpie/api-annotations-dotnet/iOS-CoreMotion.ignore index 0e3e0e9fd839..0a63c57a9373 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/iOS-CoreMotion.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/iOS-CoreMotion.ignore @@ -1,5 +1,3 @@ -!missing-enum! CMFallDetectionEventUserResolution not bound - ## no generator support for FastEnumeration - https://bugzilla.xamarin.com/show_bug.cgi?id=4391 !missing-protocol-conformance! CMSensorDataList should conform to NSFastEnumeration diff --git a/tests/xtro-sharpie/api-annotations-dotnet/iOS-FileProvider.ignore b/tests/xtro-sharpie/api-annotations-dotnet/iOS-FileProvider.ignore index 3f5cc9d54f5c..03c9c220decc 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/iOS-FileProvider.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/iOS-FileProvider.ignore @@ -44,3 +44,6 @@ !missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::SignalEnumerator(System.String,System.Action`1)' is missing a '?' on parameter 'completion' block parameter #0 !missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::SignalErrorResolved(Foundation.NSError,System.Action`1)' is missing a '?' on parameter 'completionHandler' block parameter #0 !missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::WaitForChangesOnItemsBelowItem(System.String,System.Action`1)' is missing a '?' on parameter 'completionHandler' block parameter #0 + +# NSFileProviderKnownFolders is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by bound API. +!unknown-native-enum! NSFileProviderKnownFolders bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/macOS-ARKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/macOS-ARKit.ignore index c1d32acdd2f8..1aba9724f67b 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/macOS-ARKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/macOS-ARKit.ignore @@ -64,4 +64,3 @@ !missing-enum! ar_device_anchor_query_status_t not bound !missing-enum! ar_device_anchor_tracking_state_t not bound !missing-enum! ar_session_error_code_t not bound -!missing-enum! ar_world_tracking_error_code_t not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreLocation.ignore b/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreLocation.ignore index 09744620088c..8665fd6abaae 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreLocation.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreLocation.ignore @@ -1,3 +1,6 @@ # Added but deprecated, so there is not point to add them !missing-protocol-member! CLLocationManagerDelegate::locationManager:didRangeBeacons:inRegion: not found !missing-protocol-member! CLLocationManagerDelegate::locationManager:rangingBeaconsDidFailForRegion:withError: not found + +# CLServiceSessionAuthorizationRequirement is unavailable on macOS natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by CLServiceSession. +!unknown-native-enum! CLServiceSessionAuthorizationRequirement bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreMotion.ignore b/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreMotion.ignore index ecda3d7c3bdc..0e1baa35e38c 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreMotion.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/macOS-CoreMotion.ignore @@ -1,6 +1,5 @@ ## available in headers but not used in any (available) API !missing-enum! CMAttitudeReferenceFrame not bound -!missing-enum! CMFallDetectionEventUserResolution not bound ## available in headers but do not make much sense for macOS !missing-selector! CMAmbientPressureData::pressure not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/macOS-NearbyInteraction.ignore b/tests/xtro-sharpie/api-annotations-dotnet/macOS-NearbyInteraction.ignore index 7a7ba1b0dd3a..020742c448ea 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/macOS-NearbyInteraction.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/macOS-NearbyInteraction.ignore @@ -1,3 +1,4 @@ -# Headers are clear that these two enums are not supported on macOS, don't know why xtro thinks they are -!missing-enum! NIDLTDOACoordinatesType not bound -!missing-enum! NIDLTDOAMeasurementType not bound +# These enums are unavailable on macOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! NIAlgorithmConvergenceStatus bound +!unknown-native-enum! NINearbyObjectRemovalReason bound +!unknown-native-enum! NINearbyObjectVerticalDirectionEstimate bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AVFoundation.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AVFoundation.ignore index 6075ddf1c7ed..0dd0094f7ebf 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AVFoundation.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AVFoundation.ignore @@ -23,3 +23,6 @@ !missing-type! AVCaptionRenderer not bound !missing-null-allowed! 'System.Void AVFoundation.AVCapturePhotoOutput::SetPreparedPhotoSettings(AVFoundation.AVCapturePhotoSettings[],System.Action`2)' is missing a '?' on parameter 'completionHandler' block parameter #1 + +# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration. +!unknown-native-enum! AVAudioRoutingArbitrationCategory bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AudioToolbox.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AudioToolbox.ignore index 4d0f84eeba21..ae7092e29ce5 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AudioToolbox.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AudioToolbox.ignore @@ -5,6 +5,3 @@ # the enum is available on tvOS, but none of its values are, so just don't bind the enum itself !missing-enum! AUAudioMixRenderingStyle not bound - -# this enum is declared as not available on tvOS, not sure why it shows up (it's visionOS-only) -!missing-enum! CASoundStageSize not bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AuthenticationServices.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AuthenticationServices.ignore index 34fc736a0a58..f5f62343f9a2 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AuthenticationServices.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-AuthenticationServices.ignore @@ -3,3 +3,8 @@ !extra-enum-value! Managed value 1008 for ASAuthorizationError.CredentialExport not found in native headers !extra-enum-value! Managed value 1009 for ASAuthorizationError.PreferSignInWithApple not found in native headers !extra-enum-value! Managed value 1010 for ASAuthorizationError.DeviceNotConfiguredForPasskeyCreation not found in native headers + +# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! ASAuthorizationPublicKeyCredentialAttachment bound +!unknown-native-enum! ASAuthorizationWebBrowserPublicKeyCredentialManagerAuthorizationState bound +!unknown-native-enum! ASPublicKeyCredentialClientDataCrossOriginValue bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-CoreSpotlight.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-CoreSpotlight.ignore new file mode 100644 index 000000000000..92c653f67132 --- /dev/null +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-CoreSpotlight.ignore @@ -0,0 +1,3 @@ +# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! CSSuggestionKind bound +!unknown-native-enum! CSUserInteraction bound diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-ReplayKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-ReplayKit.ignore index 20dec69e186b..9b8cd435763b 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-ReplayKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-ReplayKit.ignore @@ -1,6 +1,3 @@ -## Not used on tvOS (no camera) -!missing-enum! RPCameraPosition not bound - # SCStreamErrorDomain is from ScreenCaptureKit, but only macOS and Mac Catalyst. # However, Apple seems to also have added it to ReplayKit for iOS and tvOS, which is why xtro detects it as such. # It doesn't seem useful though in ReplayKit, so just skip binding it. diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore index ea300039a525..4f502156c771 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore @@ -362,6 +362,12 @@ !missing-null-allowed! 'System.Void UIKit.UIImageReader::GetImage(Foundation.NSData,System.Action`1)' is missing a '?' on parameter 'completion' block parameter #0 !missing-null-allowed! 'System.Void UIKit.UIImageReader::GetImage(Foundation.NSUrl,System.Action`1)' is missing a '?' on parameter 'completion' block parameter #0 +# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! UIBehavioralStyle bound +!unknown-native-enum! UICornerCurve bound +!unknown-native-enum! UIFindSessionSearchResultDisplayStyle bound +!unknown-native-enum! UITextSearchMatchMethod bound + ## bound selectors with no native declaration reachable from the class in the current SDK headers: ## private/SPI selectors, protocol/category members Apple inlines without declaring the conformance on ## the class, managed callback infrastructure, or API Apple removed/privatized - kept for compatibility diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-VideoToolbox.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-VideoToolbox.ignore index c48750dd36ac..41829f729dad 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-VideoToolbox.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-VideoToolbox.ignore @@ -1,11 +1,6 @@ -# The headers are quite clear that these enums aren't available on tvOS; so not adding them to tvOS for now -!missing-enum! VTFrameRateConversionConfigurationQualityPrioritization not bound -!missing-enum! VTFrameRateConversionConfigurationRevision not bound -!missing-enum! VTFrameRateConversionParametersSubmissionMode not bound -!missing-enum! VTMotionBlurConfigurationQualityPrioritization not bound -!missing-enum! VTMotionBlurConfigurationRevision not bound -!missing-enum! VTMotionBlurParametersSubmissionMode not bound -!missing-enum! VTOpticalFlowConfigurationQualityPrioritization not bound -!missing-enum! VTOpticalFlowConfigurationRevision not bound -!missing-enum! VTOpticalFlowParametersSubmissionMode not bound - +# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API. +!unknown-native-enum! VTSuperResolutionScalerConfigurationInputType bound +!unknown-native-enum! VTSuperResolutionScalerConfigurationModelStatus bound +!unknown-native-enum! VTSuperResolutionScalerConfigurationQualityPrioritization bound +!unknown-native-enum! VTSuperResolutionScalerConfigurationRevision bound +!unknown-native-enum! VTSuperResolutionScalerParametersSubmissionMode bound diff --git a/tests/xtro-sharpie/xtro-sharpie/EnumCheck.cs b/tests/xtro-sharpie/xtro-sharpie/EnumCheck.cs index d85e49bd7e46..0d0ff4f46ea0 100644 --- a/tests/xtro-sharpie/xtro-sharpie/EnumCheck.cs +++ b/tests/xtro-sharpie/xtro-sharpie/EnumCheck.cs @@ -11,6 +11,11 @@ class ManagedValue { Dictionary managed_values = new Dictionary (); Dictionary native_values = new Dictionary (); + // Cache, per DeclContext, the names of the typedefs that name an enum and are unavailable. + // This avoids rescanning the whole DeclContext for every enum we visit (which would be + // O(enums × siblings)); each context is scanned once and every enum check is then O(1). + Dictionary> unavailable_enum_typedefs = new Dictionary> (); + public EnumCheck (BindingResult bindingResult) : base (bindingResult) { @@ -67,6 +72,12 @@ public override void VisitEnumDecl (EnumDecl decl) if (!decl.IsAvailable ()) return; + // The availability attribute (e.g. API_UNAVAILABLE(maccatalyst)) is sometimes attached to + // the typedef that names the enum instead of to the enum declaration itself, in which case + // decl.IsAvailable () above won't detect it. Look for the corresponding typedef and check it too. + if (IsUnavailableViaTypedef (decl)) + return; + var framework = Helpers.GetFramework (decl); if (framework is null) return; @@ -284,6 +295,33 @@ public override void VisitEnumDecl (EnumDecl decl) Log.On (framework).Add ($"!wrong-enum-size! {name} managed {managed_size} vs native {native_size}"); } + // The availability attribute for an enum can be attached to the typedef that names the enum + // (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`) instead of to + // the enum declaration itself. In that case the attribute lands on the TypedefDecl, not the + // EnumDecl, so we look for the corresponding sibling typedef and check its availability. The + // per-context set of unavailable enum typedefs is computed once and cached (see the field). + bool IsUnavailableViaTypedef (EnumDecl decl) + { + var context = decl.DeclContext; + if (context is null) + return false; + + if (!unavailable_enum_typedefs.TryGetValue (context, out var names)) { + names = new HashSet (StringComparer.Ordinal); + foreach (var sibling in context.Decls) { + if (sibling is not TypedefDecl typedef) + continue; + if (typedef.UnderlyingType.UnqualifiedDesugaredType is not EnumType) + continue; + if (!typedef.IsAvailable ()) + names.Add (typedef.Name); + } + unavailable_enum_typedefs [context] = names; + } + + return decl.Name is string name && names.Contains (name); + } + static bool IsErrorEnum (string typeName) { if (typeName.EndsWith ("Error", StringComparison.Ordinal)) diff --git a/tools/sharpie/Sharpie.Bind/BindingGenerator.cs b/tools/sharpie/Sharpie.Bind/BindingGenerator.cs index 8ed1450de34b..04f8f37e58c3 100644 --- a/tools/sharpie/Sharpie.Bind/BindingGenerator.cs +++ b/tools/sharpie/Sharpie.Bind/BindingGenerator.cs @@ -93,9 +93,16 @@ public override void VisitTypedefDecl (TypedefDecl decl) var underlyingDesugaredType = decl.UnderlyingType.UnqualifiedDesugaredType; switch (underlyingDesugaredType?.TypeClass) { case CX_TypeClass.CX_TypeClass_Enum: - var boundEnum = ((EnumType) underlyingDesugaredType).Decl.Annotation (); - if (boundEnum is not null) + var enumDecl = ((EnumType) underlyingDesugaredType).Decl; + var boundEnum = enumDecl.Annotation (); + if (boundEnum is not null) { boundEnum.Name = decl.Name; + // The availability attributes for an enum may be attached to this typedef + // (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`) + // instead of to the enum declaration itself. Link the typedef to the enum so + // the AvailabilityMassager can pick up those attributes. + enumDecl.AddAnnotation (decl); + } break; case CX_TypeClass.CX_TypeClass_Record: var boundRecord = ((RecordType) underlyingDesugaredType).Decl.Annotation (); diff --git a/tools/sharpie/Sharpie.Bind/Massagers/AvailabilityMassager.cs b/tools/sharpie/Sharpie.Bind/Massagers/AvailabilityMassager.cs index 0da33dbe7ce4..7bbb7d1cfdbe 100644 --- a/tools/sharpie/Sharpie.Bind/Massagers/AvailabilityMassager.cs +++ b/tools/sharpie/Sharpie.Bind/Massagers/AvailabilityMassager.cs @@ -41,12 +41,24 @@ void Massage (EntityDeclaration entity) if (decl is null) return; - if (decl.Attrs.IsUnavailableAttr ()) { + // For enums, the availability attributes may be attached to the typedef that names + // the enum (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`) + // rather than to the enum declaration itself. BindingGenerator.VisitTypedefDecl links + // such typedefs to the enum, so include the attributes of the typedef that gives the + // enum its name here as well. An enum can be linked to more than one typedef (e.g. in + // typedef chains), so match by name to pick the one that actually names this enum and + // avoid applying availability from an unrelated typedef. + IEnumerable attrs = decl.Attrs; + var typedef = decl.GetAnnotations ().FirstOrDefault (t => t is not null && t.Name == entity.Name); + if (typedef is not null) + attrs = attrs.Concat (typedef.Attrs); + + if (attrs.IsUnavailableAttr ()) { entity.Remove (); return; } - foreach (var attr in decl.Attrs.GetAvailabilityAttributes ().SelectMany (AvailabilityBaseAttribute.FromClang)) + foreach (var attr in attrs.GetAvailabilityAttributes ().SelectMany (AvailabilityBaseAttribute.FromClang)) entity.AddAttribute (attr); } }