Skip to content

Commit

Permalink
Validate some lifetime attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Nov 24, 2022
1 parent 08909b5 commit 1f6e1ff
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 68 deletions.
4 changes: 2 additions & 2 deletions crates/header-translator/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Method {
});

let ty = entity.get_type().expect("argument type");
let ty = RustType::parse(ty, false, is_consumed);
let ty = RustType::parse_argument(ty, is_consumed);

(name, qualifier, ty)
})
Expand All @@ -210,7 +210,7 @@ impl Method {
);
}
let result_type = if result_type.get_kind() != TypeKind::Void {
Some(RustType::parse(result_type, true, false))
Some(RustType::parse_return(result_type))
} else {
None
};
Expand Down
56 changes: 55 additions & 1 deletion crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl RustType {
matches!(self, Self::Id { .. })
}

pub fn parse(ty: Type<'_>, is_return: bool, is_consumed: bool) -> Self {
fn parse(ty: Type<'_>, is_return: bool, is_consumed: bool) -> Self {
use TypeKind::*;

// println!("{:?}, {:?}", ty, ty.get_class_type());
Expand Down Expand Up @@ -450,6 +450,60 @@ impl RustType {
}
}

impl RustType {
fn visit_lifetime(&self, mut f: impl FnMut(Lifetime)) {
match self {
Self::Id { lifetime, .. } => f(*lifetime),
Self::Pointer { pointee, .. } => pointee.visit_lifetime(f),
Self::Array { element_type, .. } => element_type.visit_lifetime(f),
_ => {}
}
}

pub fn parse_return(ty: Type<'_>) -> Self {
let this = Self::parse(ty, true, false);

this.visit_lifetime(|lifetime| {
if lifetime != Lifetime::Unspecified {
panic!("unexpected lifetime in return {this:?}");
}
});

this
}

pub fn parse_argument(ty: Type<'_>, is_consumed: bool) -> Self {
let this = Self::parse(ty, false, is_consumed);

match &this {
Self::Pointer { pointee, .. } => pointee.visit_lifetime(|lifetime| {
if lifetime != Lifetime::Autoreleasing && lifetime != Lifetime::Unspecified {
panic!("unexpected lifetime {lifetime:?} in pointer argument {ty:?}");
}
}),
_ => this.visit_lifetime(|lifetime| {
if lifetime != Lifetime::Strong && lifetime != Lifetime::Unspecified {
panic!("unexpected lifetime {lifetime:?} in argument {ty:?}");
}
}),
}

this
}

pub fn parse_typedef(ty: Type<'_>) -> Self {
let this = Self::parse(ty, false, false);

this.visit_lifetime(|lifetime| {
if lifetime != Lifetime::Unspecified {
panic!("unexpected lifetime in typedef {this:?}");
}
});

this
}
}

impl ToTokens for RustType {
fn to_tokens(&self, tokens: &mut TokenStream) {
use RustType::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Stmt {
})
}
TypeKind::Typedef => {
let type_ = RustType::parse(ty, false, false);
let type_ = RustType::parse_typedef(ty);
Some(Self::AliasDecl { name, type_ })
}
_ => {
Expand Down
13 changes: 13 additions & 0 deletions crates/icrate/src/Foundation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ initWithString = { unsafe = false }
[class.NSBlockOperation.methods]
# Uses `NSArray<void (^)(void)>`, which is difficult to handle
executionBlocks = { skipped = true }

# These use `Class<NSItemProvider...>`, which is unsupported
[class.NSItemProvider.methods]
registerObjectOfClass_visibility_loadHandler = { skipped = true }
canLoadObjectOfClass = { skipped = true }
loadObjectOfClass_completionHandler = { skipped = true }

# These use `SomeObject * __strong *`, which is unsupported
[class.NSNetService.methods]
getInputStream_outputStream = { skipped = true }
[class.NSPropertyListSerialization.methods]
dataFromPropertyList_format_errorDescription = { skipped = true }
propertyListFromData_mutabilityOption_format_errorDescription = { skipped = true }
27 changes: 0 additions & 27 deletions crates/icrate/src/generated/Foundation/NSItemProvider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,6 @@ impl NSItemProvider {
) {
msg_send![self, registerObject: object, visibility: visibility]
}
pub unsafe fn registerObjectOfClass_visibility_loadHandler(
&self,
aClass: &TodoProtocols,
visibility: NSItemProviderRepresentationVisibility,
loadHandler: TodoBlock,
) {
msg_send![
self,
registerObjectOfClass: aClass,
visibility: visibility,
loadHandler: loadHandler
]
}
pub unsafe fn canLoadObjectOfClass(&self, aClass: &TodoProtocols) -> bool {
msg_send![self, canLoadObjectOfClass: aClass]
}
pub unsafe fn loadObjectOfClass_completionHandler(
&self,
aClass: &TodoProtocols,
completionHandler: TodoBlock,
) -> Id<NSProgress, Shared> {
msg_send_id![
self,
loadObjectOfClass: aClass,
completionHandler: completionHandler
]
}
pub unsafe fn initWithItem_typeIdentifier(
&self,
item: Option<&NSSecureCoding>,
Expand Down
11 changes: 0 additions & 11 deletions crates/icrate/src/generated/Foundation/NSNetServices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ impl NSNetService {
pub unsafe fn resolveWithTimeout(&self, timeout: NSTimeInterval) {
msg_send![self, resolveWithTimeout: timeout]
}
pub unsafe fn getInputStream_outputStream(
&self,
inputStream: *mut *mut NSInputStream,
outputStream: *mut *mut NSOutputStream,
) -> bool {
msg_send![
self,
getInputStream: inputStream,
outputStream: outputStream
]
}
pub unsafe fn setTXTRecordData(&self, recordData: Option<&NSData>) -> bool {
msg_send![self, setTXTRecordData: recordData]
}
Expand Down
26 changes: 0 additions & 26 deletions crates/icrate/src/generated/Foundation/NSPropertyList.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,4 @@ impl NSPropertyListSerialization {
error: error
]
}
pub unsafe fn dataFromPropertyList_format_errorDescription(
plist: &Object,
format: NSPropertyListFormat,
errorString: *mut *mut NSString,
) -> Option<Id<NSData, Shared>> {
msg_send_id![
Self::class(),
dataFromPropertyList: plist,
format: format,
errorDescription: errorString
]
}
pub unsafe fn propertyListFromData_mutabilityOption_format_errorDescription(
data: &NSData,
opt: NSPropertyListMutabilityOptions,
format: *mut NSPropertyListFormat,
errorString: *mut *mut NSString,
) -> Option<Id<Object, Shared>> {
msg_send_id![
Self::class(),
propertyListFromData: data,
mutabilityOption: opt,
format: format,
errorDescription: errorString
]
}
}

0 comments on commit 1f6e1ff

Please sign in to comment.