diff options
author | Mikko Lehtonen <scoopr@iki.fi> | 2017-02-22 01:31:55 +0200 |
---|---|---|
committer | Mikko Lehtonen <scoopr@iki.fi> | 2017-02-22 01:31:55 +0200 |
commit | fe06265567b9ee1207bc95077932da85c0ed0788 (patch) | |
tree | 631adaffc82fc0ecebbe9df3da4a6f3a33227543 | |
parent | 5f7636ea752da80dc222d05239dac74a434013ef (diff) |
objc: Rename protocol traits with prefix
Apparently protocols and interface names live in a separate namespace,
so there is a chance of collision, like the NSObject interface implements NSObject category
-rw-r--r-- | src/ir/objc.rs | 15 | ||||
-rw-r--r-- | tests/expectations/tests/objc_interface.rs | 4 | ||||
-rw-r--r-- | tests/expectations/tests/objc_protocol.rs | 15 | ||||
-rw-r--r-- | tests/headers/objc_protocol.h | 8 |
4 files changed, 39 insertions, 3 deletions
diff --git a/src/ir/objc.rs b/src/ir/objc.rs index ff36c7b6..963c8e20 100644 --- a/src/ir/objc.rs +++ b/src/ir/objc.rs @@ -7,6 +7,7 @@ use clang_sys::CXChildVisit_Continue; use clang_sys::CXCursor_ObjCCategoryDecl; use clang_sys::CXCursor_ObjCClassRef; use clang_sys::CXCursor_ObjCInstanceMethodDecl; +use clang_sys::CXCursor_ObjCProtocolDecl; /// Objective C interface as used in TypeKind /// @@ -19,6 +20,8 @@ pub struct ObjCInterface { category: Option<String>, + is_protocol: bool, + /// List of the methods defined in this interfae methods: Vec<ObjCInstanceMethod>, } @@ -42,6 +45,7 @@ impl ObjCInterface { ObjCInterface { name: name.to_owned(), category: None, + is_protocol: false, methods: Vec::new(), } } @@ -54,11 +58,16 @@ impl ObjCInterface { /// Formats the name for rust /// Can be like NSObject, but with categories might be like NSObject_NSCoderMethods + /// and protocols are like protocol_NSObject pub fn rust_name(&self) -> String { if let Some(ref cat) = self.category { format!("{}_{}", self.name(), cat) } else { - self.name().to_owned() + if self.is_protocol { + format!("protocol_{}", self.name()) + } else { + self.name().to_owned() + } } } @@ -74,6 +83,10 @@ impl ObjCInterface { let name = cursor.spelling(); let mut interface = Self::new(&name); + if cursor.kind() == CXCursor_ObjCProtocolDecl { + interface.is_protocol = true; + } + cursor.visit(|c| { match c.kind() { CXCursor_ObjCClassRef => { diff --git a/tests/expectations/tests/objc_interface.rs b/tests/expectations/tests/objc_interface.rs index 027cf57e..3ca67b89 100644 --- a/tests/expectations/tests/objc_interface.rs +++ b/tests/expectations/tests/objc_interface.rs @@ -11,5 +11,5 @@ extern crate objc; pub type id = *mut objc::runtime::Object; pub trait Foo { } impl Foo for id { } -pub trait bar { } -impl bar for id { } +pub trait protocol_bar { } +impl protocol_bar for id { } diff --git a/tests/expectations/tests/objc_protocol.rs b/tests/expectations/tests/objc_protocol.rs new file mode 100644 index 00000000..a21d4baa --- /dev/null +++ b/tests/expectations/tests/objc_protocol.rs @@ -0,0 +1,15 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + +#![cfg(target_os="macos")] + +#[macro_use] +extern crate objc; +#[allow(non_camel_case_types)] +pub type id = *mut objc::runtime::Object; +pub trait protocol_Foo { } +impl protocol_Foo for id { } +pub trait Foo { } +impl Foo for id { } diff --git a/tests/headers/objc_protocol.h b/tests/headers/objc_protocol.h new file mode 100644 index 00000000..0c760fa5 --- /dev/null +++ b/tests/headers/objc_protocol.h @@ -0,0 +1,8 @@ +// bindgen-flags: --objc-extern-crate -- -x objective-c +// bindgen-osx-only + +@protocol Foo +@end + +@interface Foo <Foo> +@end |