-
Notifications
You must be signed in to change notification settings - Fork 748
Add initial Objective C support #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+394
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ pub mod module; | |
pub mod ty; | ||
pub mod type_collector; | ||
pub mod var; | ||
pub mod objc; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//! Objective C types | ||
|
||
use clang; | ||
use clang_sys::CXChildVisit_Continue; | ||
use clang_sys::CXCursor_ObjCInstanceMethodDecl; | ||
// use clang_sys::CXCursor_ObjCSuperClassRef; | ||
use super::context::BindgenContext; | ||
|
||
/// Objective C interface as used in TypeKind | ||
/// | ||
/// Also protocols are parsed as this type | ||
#[derive(Debug)] | ||
pub struct ObjCInterface { | ||
/// The name | ||
/// like, NSObject | ||
name: String, | ||
|
||
/// List of the methods defined in this interfae | ||
methods: Vec<ObjCInstanceMethod>, | ||
} | ||
|
||
/// The objective c methods | ||
#[derive(Debug)] | ||
pub struct ObjCInstanceMethod { | ||
/// The original method selector name | ||
/// like, dataWithBytes:length: | ||
name: String, | ||
|
||
/// Method name as converted to rust | ||
/// like, dataWithBytes_length_ | ||
rust_name: String, | ||
} | ||
|
||
impl ObjCInterface { | ||
fn new(name: &str) -> ObjCInterface { | ||
ObjCInterface { | ||
name: name.to_owned(), | ||
methods: Vec::new(), | ||
} | ||
} | ||
|
||
/// The name | ||
/// like, NSObject | ||
pub fn name(&self) -> &str { | ||
self.name.as_ref() | ||
} | ||
|
||
/// List of the methods defined in this interfae | ||
pub fn methods(&self) -> &Vec<ObjCInstanceMethod> { | ||
&self.methods | ||
} | ||
|
||
/// Parses the Objective C interface from the cursor | ||
pub fn from_ty(cursor: &clang::Cursor, | ||
_ctx: &mut BindgenContext) | ||
-> Option<Self> { | ||
let name = cursor.spelling(); | ||
let mut interface = Self::new(&name); | ||
|
||
cursor.visit(|cursor| { | ||
match cursor.kind() { | ||
CXCursor_ObjCInstanceMethodDecl => { | ||
let name = cursor.spelling(); | ||
let method = ObjCInstanceMethod::new(&name); | ||
|
||
interface.methods.push(method); | ||
} | ||
_ => {} | ||
} | ||
CXChildVisit_Continue | ||
}); | ||
Some(interface) | ||
} | ||
} | ||
|
||
impl ObjCInstanceMethod { | ||
fn new(name: &str) -> ObjCInstanceMethod { | ||
let split_name: Vec<&str> = name.split(':').collect(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may also go away if you want :) |
||
let rust_name = split_name.join("_"); | ||
|
||
ObjCInstanceMethod { | ||
name: name.to_owned(), | ||
rust_name: rust_name.to_owned(), | ||
} | ||
} | ||
|
||
/// The original method selector name | ||
/// like, dataWithBytes:length: | ||
pub fn name(&self) -> &str { | ||
self.name.as_ref() | ||
} | ||
|
||
/// Method name as converted to rust | ||
/// like, dataWithBytes_length_ | ||
pub fn rust_name(&self) -> &str { | ||
self.rust_name.as_ref() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix the alignment in this block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damnit, I fixed this once already, but somehow rustfmt got really confused in this block :(