Skip to content

Commit

Permalink
WIP: Change header generation wrt. protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 26, 2023
1 parent 0f0d76d commit d1d1a2d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
18 changes: 16 additions & 2 deletions crates/header-translator/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pub struct Method {
pub result_type: Ty,
safe: bool,
mutating: bool,
is_protocol: bool,
}

impl Method {
Expand Down Expand Up @@ -319,7 +320,12 @@ pub struct PartialMethod<'tu> {
}

impl<'tu> PartialMethod<'tu> {
pub fn parse(self, data: MethodData, context: &Context<'_>) -> Option<(bool, Method)> {
pub fn parse(
self,
data: MethodData,
is_protocol: bool,
context: &Context<'_>,
) -> Option<(bool, Method)> {
let Self {
entity,
selector,
Expand Down Expand Up @@ -432,6 +438,7 @@ impl<'tu> PartialMethod<'tu> {
result_type,
safe: !data.unsafe_,
mutating: data.mutating,
is_protocol,
},
))
}
Expand All @@ -453,6 +460,7 @@ impl PartialProperty<'_> {
self,
getter_data: MethodData,
setter_data: Option<MethodData>,
is_protocol: bool,
context: &Context<'_>,
) -> (Option<Method>, Option<Method>) {
let Self {
Expand Down Expand Up @@ -502,6 +510,7 @@ impl PartialProperty<'_> {
result_type: ty,
safe: !getter_data.unsafe_,
mutating: getter_data.mutating,
is_protocol,
})
} else {
None
Expand All @@ -528,6 +537,7 @@ impl PartialProperty<'_> {
result_type: Ty::VOID_RESULT,
safe: !setter_data.unsafe_,
mutating: setter_data.mutating,
is_protocol,
})
} else {
None
Expand Down Expand Up @@ -587,7 +597,11 @@ impl fmt::Display for Method {
// Signature
//

write!(f, " pub ")?;
write!(f, " ")?;
if !self.is_protocol {
write!(f, "pub ")?;
}

if !self.safe {
write!(f, "unsafe ")?;
}
Expand Down
3 changes: 2 additions & 1 deletion crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,11 @@ impl fmt::Display for IdType {
}
Self::AnyObject { protocols } => match &**protocols {
[] => write!(f, "Object"),
[id] if id.name == "NSObject" => write!(f, "{}", id.path()),
[id] if id.name == "NSCopying" || id.name == "NSMutableCopying" => {
write!(f, "Object")
}
[id] => write!(f, "{}", id.path()),
[id] => write!(f, "ProtocolObject<dyn {}>", id.path()),
// TODO: Handle this better
_ => write!(f, "TodoProtocols"),
},
Expand Down
23 changes: 17 additions & 6 deletions crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ fn parse_objc_decl(

if !properties.remove(&(partial.is_class, partial.fn_name.clone())) {
let data = ClassData::get_method_data(data, &partial.fn_name);
if let Some((designated_initializer, method)) = partial.parse(data, context) {
if let Some((designated_initializer, method)) =
partial.parse(data, generics.is_none(), context)
{
if designated_initializer {
designated_initializers.push(method.fn_name.clone());
}
Expand All @@ -132,7 +134,8 @@ fn parse_objc_decl(
.as_ref()
.map(|setter_name| ClassData::get_method_data(data, setter_name));

let (getter, setter) = partial.parse(getter_data, setter_data, context);
let (getter, setter) =
partial.parse(getter_data, setter_data, generics.is_none(), context);
if let Some(getter) = getter {
if !properties.insert((getter.is_class, getter.fn_name.clone())) {
error!(?setter, "already exisiting property");
Expand Down Expand Up @@ -1123,14 +1126,20 @@ impl fmt::Display for Stmt {
Self::ProtocolDecl {
id,
availability,
protocols: _,
protocols,
methods,
} => {
writeln!(f, "extern_protocol!(")?;
write!(f, "{availability}")?;
writeln!(f, " pub struct {};", id.name)?;
writeln!(f)?;
writeln!(f, " unsafe impl ProtocolType for {} {{", id.name)?;
write!(f, " pub unsafe trait {} ", id.name)?;
let mut iter = protocols.iter();
if let Some(protocol) = iter.next() {
write!(f, "{}", protocol.path())?;
for protocol in iter {
write!(f, "+ {}", protocol.path())?;
}
}
writeln!(f, "{{", id.name)?;
for method in methods {
// Use a set to deduplicate features, and to have them in
// a consistent order
Expand Down Expand Up @@ -1160,6 +1169,8 @@ impl fmt::Display for Stmt {
writeln!(f, "{method}")?;
}
writeln!(f, " }}")?;
writeln!(f)?;
writeln!(f, " unsafe impl ProtocolType for dyn {} {{}}", id.name)?;
writeln!(f, ");")?;
}
Self::StructDecl {
Expand Down
6 changes: 3 additions & 3 deletions crates/icrate/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub(crate) use objc2::ffi::{NSInteger, NSIntegerMax, NSUInteger, NSUIntegerMax,
#[cfg(feature = "objective-c")]
pub(crate) use objc2::rc::{Allocated, Id, Owned, Ownership, Shared};
#[cfg(feature = "objective-c")]
pub(crate) use objc2::runtime::NSObject;
#[cfg(feature = "objective-c")]
pub(crate) use objc2::runtime::{Bool, Class, Object, Sel};
#[cfg(feature = "objective-c")]
pub(crate) use objc2::runtime::{NSObject, NSObjectProtocol};
#[cfg(feature = "objective-c")]
pub(crate) use objc2::{
__inner_extern_class, extern_class, extern_methods, extern_protocol, ClassType, Message,
ProtocolType,
ProtocolObject, ProtocolType,
};

#[cfg(feature = "block")]
Expand Down

0 comments on commit d1d1a2d

Please sign in to comment.