Skip to content

Commit

Permalink
Initial stuff for changing ownership and adding inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay authored and emilio committed Sep 16, 2020
1 parent f4d10c3 commit 840b738
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 51 deletions.
17 changes: 14 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3806,7 +3806,7 @@ fn objc_method_codegen(
}
} else {
let fn_args = fn_args.clone();
let args = iter::once(quote! { self }).chain(fn_args.into_iter());
let args = iter::once(quote! { &self }).chain(fn_args.into_iter());
quote! {
( #( #args ),* ) #fn_ret
}
Expand All @@ -3825,7 +3825,7 @@ fn objc_method_codegen(
}
} else {
quote! {
msg_send!(self, #methods_and_args)
msg_send!(*self, #methods_and_args)
}
};

Expand Down Expand Up @@ -3901,7 +3901,7 @@ impl CodeGenerator for ObjCInterface {
if !self.is_category() && !self.is_protocol() {
let struct_block = quote! {
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct #class_name(pub id);
impl std::ops::Deref for #class_name {
type Target = objc::runtime::Object;
Expand Down Expand Up @@ -3962,6 +3962,17 @@ impl CodeGenerator for ObjCInterface {
}
};
result.push(impl_trait);
if !parent.is_template() {
let parent_struct_name = ctx.rust_ident(parent.name());
let from_block = quote! {
impl From<#class_name> for #parent_struct_name {
fn from(child: #class_name) -> #parent_struct_name {
#parent_struct_name(child.0)
}
}
};
result.push(from_block);
}
parent.parent_class
} else {
None
Expand Down
21 changes: 18 additions & 3 deletions tests/expectations/tests/libclang-9/objc_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -28,7 +28,7 @@ impl Foo {
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {}
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Bar(pub id);
impl std::ops::Deref for Bar {
type Target = objc::runtime::Object;
Expand All @@ -43,10 +43,15 @@ impl Bar {
}
}
impl IFoo for Bar {}
impl From<Bar> for Foo {
fn from(child: Bar) -> Foo {
Foo(child.0)
}
}
impl IBar for Bar {}
pub trait IBar: Sized + std::ops::Deref {}
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Baz(pub id);
impl std::ops::Deref for Baz {
type Target = objc::runtime::Object;
Expand All @@ -61,6 +66,16 @@ impl Baz {
}
}
impl IBar for Baz {}
impl From<Baz> for Bar {
fn from(child: Baz) -> Bar {
Bar(child.0)
}
}
impl IFoo for Baz {}
impl From<Baz> for Foo {
fn from(child: Baz) -> Foo {
Foo(child.0)
}
}
impl IBaz for Baz {}
pub trait IBaz: Sized + std::ops::Deref {}
12 changes: 6 additions & 6 deletions tests/expectations/tests/libclang-9/objc_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -27,15 +27,15 @@ impl Foo {
}
impl<ObjectType: 'static> IFoo<ObjectType> for Foo {}
pub trait IFoo<ObjectType>: Sized + std::ops::Deref {
unsafe fn get(self) -> u64
unsafe fn get(&self) -> u64
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, get)
msg_send!(*self, get)
}
}
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct FooMultiGeneric(pub id);
impl std::ops::Deref for FooMultiGeneric {
type Target = objc::runtime::Object;
Expand All @@ -56,10 +56,10 @@ impl<KeyType: 'static, ObjectType: 'static>
pub trait IFooMultiGeneric<KeyType, ObjectType>:
Sized + std::ops::Deref
{
unsafe fn objectForKey_(self, key: u64) -> u64
unsafe fn objectForKey_(&self, key: u64) -> u64
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, objectForKey: key)
msg_send!(*self, objectForKey: key)
}
}
10 changes: 5 additions & 5 deletions tests/expectations/tests/objc_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -27,19 +27,19 @@ impl Foo {
}
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn method(self)
unsafe fn method(&self)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, method)
msg_send!(*self, method)
}
}
impl Foo_BarCategory for Foo {}
pub trait Foo_BarCategory: Sized + std::ops::Deref {
unsafe fn categoryMethod(self)
unsafe fn categoryMethod(&self)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, categoryMethod)
msg_send!(*self, categoryMethod)
}
}
6 changes: 3 additions & 3 deletions tests/expectations/tests/objc_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" {
pub static mut fooVar: Foo;
}
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -30,10 +30,10 @@ impl Foo {
}
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn method(self)
unsafe fn method(&self)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, method)
msg_send!(*self, method)
}
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/objc_class_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/objc_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/objc_interface_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand Down
30 changes: 15 additions & 15 deletions tests/expectations/tests/objc_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -27,48 +27,48 @@ impl Foo {
}
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn method(self)
unsafe fn method(&self)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, method)
msg_send!(*self, method)
}
unsafe fn methodWithInt_(self, foo: ::std::os::raw::c_int)
unsafe fn methodWithInt_(&self, foo: ::std::os::raw::c_int)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, methodWithInt: foo)
msg_send!(*self, methodWithInt: foo)
}
unsafe fn methodWithFoo_(self, foo: Foo)
unsafe fn methodWithFoo_(&self, foo: Foo)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, methodWithFoo: foo)
msg_send!(*self, methodWithFoo: foo)
}
unsafe fn methodReturningInt(self) -> ::std::os::raw::c_int
unsafe fn methodReturningInt(&self) -> ::std::os::raw::c_int
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, methodReturningInt)
msg_send!(*self, methodReturningInt)
}
unsafe fn methodReturningFoo(self) -> Foo
unsafe fn methodReturningFoo(&self) -> Foo
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, methodReturningFoo)
msg_send!(*self, methodReturningFoo)
}
unsafe fn methodWithArg1_andArg2_andArg3_(
self,
&self,
intvalue: ::std::os::raw::c_int,
ptr: *mut ::std::os::raw::c_char,
floatvalue: f32,
) where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send ! ( self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue )
msg_send ! ( * self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue )
}
unsafe fn methodWithAndWithoutKeywords_arg2Name__arg4Name_(
self,
&self,
arg1: ::std::os::raw::c_int,
arg2: f32,
arg3: f32,
Expand All @@ -77,7 +77,7 @@ pub trait IFoo: Sized + std::ops::Deref {
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send ! ( self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4 )
msg_send ! ( * self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4 )
}
}
pub type instancetype = id;
6 changes: 3 additions & 3 deletions tests/expectations/tests/objc_method_clash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -27,11 +27,11 @@ impl Foo {
}
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn foo(self)
unsafe fn foo(&self)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, foo)
msg_send!(*self, foo)
}
unsafe fn class_foo()
where
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/tests/objc_pointer_return_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Bar(pub id);
impl std::ops::Deref for Bar {
type Target = objc::runtime::Object;
Expand All @@ -28,7 +28,7 @@ impl Bar {
impl IBar for Bar {}
pub trait IBar: Sized + std::ops::Deref {}
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -44,11 +44,11 @@ impl Foo {
}
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn methodUsingBar_(self, my_bar: Bar)
unsafe fn methodUsingBar_(&self, my_bar: Bar)
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, methodUsingBar: my_bar)
msg_send!(*self, methodUsingBar: my_bar)
}
unsafe fn methodReturningBar() -> Bar
where
Expand Down
10 changes: 5 additions & 5 deletions tests/expectations/tests/objc_property_fnptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate objc;
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Foo(pub id);
impl std::ops::Deref for Foo {
type Target = objc::runtime::Object;
Expand All @@ -28,7 +28,7 @@ impl Foo {
impl IFoo for Foo {}
pub trait IFoo: Sized + std::ops::Deref {
unsafe fn func(
self,
&self,
) -> ::std::option::Option<
unsafe extern "C" fn(
arg1: ::std::os::raw::c_char,
Expand All @@ -39,10 +39,10 @@ pub trait IFoo: Sized + std::ops::Deref {
where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, func)
msg_send!(*self, func)
}
unsafe fn setFunc_(
self,
&self,
func: ::std::option::Option<
unsafe extern "C" fn(
arg1: ::std::os::raw::c_char,
Expand All @@ -53,6 +53,6 @@ pub trait IFoo: Sized + std::ops::Deref {
) where
<Self as std::ops::Deref>::Target: objc::Message + Sized,
{
msg_send!(self, setFunc: func)
msg_send!(*self, setFunc: func)
}
}
Loading

0 comments on commit 840b738

Please sign in to comment.