Skip to content

Commit

Permalink
Fix const-ness progatation through typedefs
Browse files Browse the repository at this point in the history
Possible after d78b047.
  • Loading branch information
madsmtm committed Jan 17, 2025
1 parent 2a69b8f commit a0ec4f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
65 changes: 29 additions & 36 deletions crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;
use std::sync::LazyLock;
use std::{fmt, iter};
use std::{fmt, iter, mem};

use clang::{CallingConvention, Entity, EntityKind, Nullability, Type, TypeKind};
use proc_macro2::{TokenStream, TokenTree};
Expand Down Expand Up @@ -1184,10 +1184,10 @@ impl Ty {
typedef_name,
declaration.get_name().expect("typedef declaration name")
);
let to = declaration
let inner = declaration
.get_typedef_underlying_type()
.expect("typedef underlying type");
let _span = debug_span!("typedef", ?typedef_name, ?declaration, ?to).entered();
let _span = debug_span!("typedef", ?typedef_name, ?declaration, ?inner).entered();

let mut parser = AttributeParser::new(&attributed_name, &typedef_name);
let mut _is_kindof = parser.is_kindof(ParsePosition::Prefix);
Expand All @@ -1196,7 +1196,7 @@ impl Ty {

let is_const2 = parser.is_const(ParsePosition::Suffix);
lifetime.update(parser.lifetime(ParsePosition::Suffix));
let mut nullability = if let Some(nullability) = unexposed_nullability {
let nullability = if let Some(nullability) = unexposed_nullability {
nullability
} else {
check_nullability(&attributed_ty, parser.nullability(ParsePosition::Suffix))
Expand Down Expand Up @@ -1367,7 +1367,7 @@ impl Ty {
};
}

let to = Self::parse(to, Lifetime::Unspecified, context);
let mut inner = Self::parse(inner, Lifetime::Unspecified, context);

let id = ItemIdentifier::new(&declaration, context);

Expand Down Expand Up @@ -1397,22 +1397,24 @@ impl Ty {
// parameter.
if let Self::Pointer {
nullability: inner_nullability,
is_const: _,
is_const: inner_is_const,
lifetime: inner_lifetime,
pointee,
} = &to
} = &mut inner
{
// The outermost attribute, i.e. the one on the typedef,
// is the one that matters.
if nullability == Nullability::Unspecified {
nullability = *inner_nullability;
//
// We intentionally mutate the Pointer's data, such that
// this works regardless of what we do further down below.
if nullability != Nullability::Unspecified {
*inner_nullability = nullability;
}
// TODO
// if !is_const {
// is_const = *inner_is_const;
// }
if lifetime == Lifetime::Unspecified {
lifetime = *inner_lifetime;
if is_const {
*inner_is_const = is_const;
}
if lifetime != Lifetime::Unspecified {
*inner_lifetime = lifetime;
}

if pointee.is_direct_cf_type(&id.name, is_bridged(&declaration, context)) {
Expand All @@ -1421,25 +1423,16 @@ impl Ty {
// type is a CF type or not... But that's how it is
// currently.
let id = context.replace_typedef_name(id, true);
return Self::Pointer {
nullability,
is_const,
lifetime,
pointee: Box::new(Self::Pointee(PointeeTy::CFTypeDef { id })),
};
}

// Only do this when visiting object-like typedefs.
if pointee.is_object_like() || pointee.is_cf_type() {
if let Self::Pointee(pointee_ty) = &**pointee {
*pointee = Box::new(Self::Pointee(PointeeTy::CFTypeDef { id }));
return inner;
} else if pointee.is_object_like() || pointee.is_cf_type() {
if let Self::Pointee(pointee_ty) = &mut **pointee {
let id = context.replace_typedef_name(id, pointee_ty.is_cf_type());
let to = Box::new(pointee_ty.clone());
return Self::Pointer {
nullability,
is_const,
lifetime,
pointee: Box::new(Self::Pointee(PointeeTy::TypeDef { id, to })),
};
// Replace with a dummy type (will be re-replaced
// on the line below).
let to = Box::new(mem::replace(pointee_ty, PointeeTy::Self_));
*pointee = Box::new(Self::Pointee(PointeeTy::TypeDef { id, to }));
return inner;
} else {
error!(?pointee, "is_object_like/is_cf_type but not Pointee");
}
Expand All @@ -1455,7 +1448,7 @@ impl Ty {

Self::TypeDef {
id,
to: Box::new(to),
to: Box::new(inner),
}
}
// Assume that functions without a prototype simply have 0 arguments.
Expand Down Expand Up @@ -1945,8 +1938,8 @@ impl Ty {
Self::Pointer {
nullability: Nullability::Nullable,
lifetime: Lifetime::Unspecified,
is_const: false,
pointee,
..
} if pointee.is_static_object() => {
// NULL -> error
write!(
Expand All @@ -1959,8 +1952,8 @@ impl Ty {
Self::Pointer {
nullability: Nullability::Nullable,
lifetime: Lifetime::Unspecified,
is_const: false,
pointee,
..
} if pointee.is_object_like() || pointee.is_cf_type() => {
// NULL -> error
write!(
Expand Down
2 changes: 1 addition & 1 deletion generated
Submodule generated updated 80 files
+1 −1 AVFoundation/AVPlayerOutput.rs
+2 −2 AudioToolbox/AudioComponent.rs
+2 −2 AudioToolbox/AudioFile.rs
+10 −10 AudioToolbox/AudioUnitProperties.rs
+2 −2 AudioToolbox/MusicPlayer.rs
+2 −2 AudioToolbox/mod.rs
+5 −5 ColorSync/ColorSyncCMM.rs
+2 −2 CoreAudio/AudioServerPlugIn.rs
+3 −3 CoreFoundation/CFArray.rs
+3 −3 CoreFoundation/CFBag.rs
+1 −1 CoreFoundation/CFBase.rs
+8 −8 CoreFoundation/CFBinaryHeap.rs
+3 −3 CoreFoundation/CFDictionary.rs
+2 −2 CoreFoundation/CFFileDescriptor.rs
+4 −4 CoreFoundation/CFFileSecurity.rs
+2 −2 CoreFoundation/CFMachPort.rs
+9 −4 CoreFoundation/CFMessagePort.rs
+2 −2 CoreFoundation/CFNotificationCenter.rs
+6 −2 CoreFoundation/CFPlugIn.rs
+5 −5 CoreFoundation/CFPropertyList.rs
+14 −14 CoreFoundation/CFRunLoop.rs
+3 −3 CoreFoundation/CFSet.rs
+8 −8 CoreFoundation/CFSocket.rs
+2 −2 CoreFoundation/CFStream.rs
+2 −2 CoreFoundation/CFString.rs
+1 −1 CoreFoundation/CFTree.rs
+4 −4 CoreFoundation/CFURLAccess.rs
+1 −1 CoreFoundation/CFURLEnumerator.rs
+29 −25 CoreFoundation/CFXMLNode.rs
+9 −5 CoreFoundation/CFXMLParser.rs
+2 −2 CoreGraphics/CGConvertColorDataWithFormat.rs
+1 −1 CoreGraphics/CGDisplayStream.rs
+6 −6 CoreMIDI/MIDIServices.rs
+3 −3 CoreMIDI/MIDISetup.rs
+1 −1 CoreMedia/CMAudioDeviceClock.rs
+4 −4 CoreMedia/CMBufferQueue.rs
+15 −15 CoreMedia/CMFormatDescription.rs
+12 −12 CoreMedia/CMFormatDescriptionBridge.rs
+4 −4 CoreMedia/CMMetadata.rs
+9 −9 CoreMedia/CMTagCollection.rs
+1 −1 CoreMedia/CMTaggedBufferGroup.rs
+4 −4 CoreText/CTFont.rs
+2 −2 CoreText/CTFontDescriptor.rs
+2 −2 CoreText/CTFontManager.rs
+1 −1 CoreVideo/CVPixelBuffer.rs
+4 −4 CoreWLAN/CoreWLANUtil.rs
+10 −10 DiskArbitration/mod.rs
+2 −2 OpenDirectory/CFOpenDirectory/CFODNode.rs
+1 −1 OpenDirectory/CFOpenDirectory/CFODQuery.rs
+4 −4 OpenDirectory/CFOpenDirectory/CFODRecord.rs
+1 −1 Security/AuthorizationDB.rs
+6 −6 Security/AuthorizationPlugin.rs
+6 −6 Security/CMSDecoder.rs
+9 −9 Security/CMSEncoder.rs
+4 −4 Security/SecACL.rs
+3 −3 Security/SecAccess.rs
+2 −2 Security/SecCertificate.rs
+3 −3 Security/SecCode.rs
+3 −3 Security/SecCustomTransform.rs
+1 −1 Security/SecIdentity.rs
+17 −17 Security/SecImportExport.rs
+2 −2 Security/SecItem.rs
+2 −2 Security/SecKey.rs
+2 −2 Security/SecKeychain.rs
+1 −1 Security/SecKeychainItem.rs
+2 −2 Security/SecRequirement.rs
+1 −1 Security/SecSharedCredential.rs
+2 −2 Security/SecStaticCode.rs
+1 −1 Security/SecTransform.rs
+4 −4 Security/SecTrust.rs
+4 −4 Security/SecTrustSettings.rs
+1 −1 Security/SecTrustedApplication.rs
+4 −4 Security/SecureDownload.rs
+11 −6 Security/SecureTransport.rs
+6 −6 SystemConfiguration/SCNetworkConfiguration.rs
+4 −4 SystemConfiguration/SCNetworkConnection.rs
+2 −2 VideoToolbox/VTRAWProcessingSession.rs
+2 −2 VideoToolbox/VTSession.rs
+2 −2 VideoToolbox/VTUtilities.rs
+3 −3 VideoToolbox/VTVideoEncoderList.rs

0 comments on commit a0ec4f5

Please sign in to comment.