-
Notifications
You must be signed in to change notification settings - Fork 750
Some function pointers, typedefs, and OSX's stdlib. #58
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
821252d
Some fixes for function pointers, typedefs, and OSX's stdlib.h.
emilio 5065315
Add Int128 types.
emilio 6ed0253
Index unnamed types by canonical declaration.
emilio dd45e45
Test weird edge cases with template params and stuff.
emilio cc8ed87
Represent block pointers as *mut c_void instead.
emilio 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 |
---|---|---|
|
@@ -14,6 +14,18 @@ use syntax::ext::base::ExtCtxt; | |
use parse::ClangItemParser; | ||
use BindgenOptions; | ||
|
||
/// A key used to index a resolved type, so we only process it once. | ||
/// | ||
/// This is almost always a USR string (an unique identifier generated by | ||
/// clang), but it can also be the canonical declaration if the type is unnamed, | ||
/// in which case clang may generate the same USR for multiple nested unnamed | ||
/// types. | ||
#[derive(Eq, PartialEq, Hash, Debug)] | ||
enum TypeKey { | ||
USR(String), | ||
Declaration(Cursor), | ||
} | ||
|
||
// This is just convenience to avoid creating a manual debug impl for the | ||
// context. | ||
struct GenContext<'ctx>(ExtCtxt<'ctx>); | ||
|
@@ -33,11 +45,9 @@ pub struct BindgenContext<'ctx> { | |
/// output. | ||
items: BTreeMap<ItemId, Item>, | ||
|
||
/// Clang cursor to type map. This is needed to be able to associate types | ||
/// with item ids during parsing. | ||
/// | ||
/// The cursor used for storage is the definition cursor. | ||
types: HashMap<Cursor, ItemId>, | ||
/// Clang USR to type map. This is needed to be able to associate types with | ||
/// item ids during parsing. | ||
types: HashMap<TypeKey, ItemId>, | ||
|
||
/// A cursor to module map. Similar reason than above. | ||
modules: HashMap<Cursor, ItemId>, | ||
|
@@ -131,29 +141,46 @@ impl<'ctx> BindgenContext<'ctx> { | |
|
||
let id = item.id(); | ||
let is_type = item.kind().is_type(); | ||
let is_unnamed = is_type && item.expect_type().name().is_none(); | ||
let old_item = self.items.insert(id, item); | ||
assert!(old_item.is_none(), "Inserted type twice?"); | ||
|
||
// Unnamed items can have an USR, but they can't be referenced from | ||
// other sites explicitly and the USR can match if the unnamed items are | ||
// nested, so don't bother tracking them. | ||
if is_type && declaration.is_some() { | ||
let declaration = declaration.unwrap(); | ||
debug_assert_eq!(declaration, declaration.canonical()); | ||
if declaration.is_valid() { | ||
let old = self.types.insert(declaration, id); | ||
debug_assert_eq!(old, None); | ||
} else if location.is_some() && | ||
(location.unwrap().kind() == CXCursor_ClassTemplate || | ||
location.unwrap().kind() == CXCursor_ClassTemplatePartialSpecialization) { | ||
let old = self.types.insert(location.unwrap().canonical(), id); | ||
debug_assert_eq!(old, None); | ||
} else { | ||
let mut declaration = declaration.unwrap(); | ||
if !declaration.is_valid() { | ||
if let Some(location) = location { | ||
if location.kind() == CXCursor_ClassTemplate || | ||
location.kind() == CXCursor_ClassTemplatePartialSpecialization { | ||
declaration = location; | ||
} | ||
} | ||
} | ||
declaration = declaration.canonical(); | ||
if !declaration.is_valid() { | ||
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. Here you rebound What does the |
||
// This could happen, for example, with types like `int*` or | ||
// similar. | ||
// | ||
// Fortunately, we don't care about those types being | ||
// duplicated, so we can just ignore them. | ||
debug!("Invalid declaration {:?} found for type {:?}", | ||
declaration, self.items.get(&id).unwrap().kind().expect_type()); | ||
return; | ||
} | ||
|
||
let key = if is_unnamed { | ||
TypeKey::Declaration(declaration) | ||
} else if let Some(usr) = declaration.usr() { | ||
TypeKey::USR(usr) | ||
} else { | ||
error!("Valid declaration with no USR: {:?}, {:?}", declaration, location); | ||
return; | ||
}; | ||
|
||
let old = self.types.insert(key, id); | ||
debug_assert_eq!(old, None); | ||
} | ||
} | ||
|
||
|
@@ -206,7 +233,7 @@ impl<'ctx> BindgenContext<'ctx> { | |
self.collected_typerefs | ||
} | ||
|
||
fn collect_typerefs(&mut self) -> Vec<(ItemId, clang::Type, Option<clang::Cursor>)> { | ||
fn collect_typerefs(&mut self) -> Vec<(ItemId, clang::Type, Option<clang::Cursor>, Option<ItemId>)> { | ||
debug_assert!(!self.collected_typerefs); | ||
self.collected_typerefs = true; | ||
let mut typerefs = vec![]; | ||
|
@@ -218,8 +245,8 @@ impl<'ctx> BindgenContext<'ctx> { | |
}; | ||
|
||
match *ty.kind() { | ||
TypeKind::UnresolvedTypeRef(ref ty, loc) => { | ||
typerefs.push((*id, ty.clone(), loc)); | ||
TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) => { | ||
typerefs.push((*id, ty.clone(), loc, parent_id)); | ||
} | ||
_ => {}, | ||
}; | ||
|
@@ -230,9 +257,9 @@ impl<'ctx> BindgenContext<'ctx> { | |
fn resolve_typerefs(&mut self) { | ||
let typerefs = self.collect_typerefs(); | ||
|
||
for (id, ty, loc) in typerefs { | ||
for (id, ty, loc, parent_id) in typerefs { | ||
let _resolved = { | ||
let resolved = Item::from_ty(&ty, loc, None, self) | ||
let resolved = Item::from_ty(&ty, loc, parent_id, self) | ||
.expect("What happened?"); | ||
let mut item = self.items.get_mut(&id).unwrap(); | ||
|
||
|
@@ -494,8 +521,15 @@ impl<'ctx> BindgenContext<'ctx> { | |
} | ||
let canonical_declaration = declaration.canonical(); | ||
if canonical_declaration.is_valid() { | ||
// First lookup to see if we already have it resolved. | ||
let id = self.types.get(&canonical_declaration).map(|id| *id); | ||
let id = | ||
self.types.get(&TypeKey::Declaration(canonical_declaration)) | ||
.map(|id| *id) | ||
.or_else(|| { | ||
canonical_declaration.usr().and_then(|usr| { | ||
self.types.get(&TypeKey::USR(usr)) | ||
}) | ||
.map(|id| *id) | ||
}); | ||
if let Some(id) = id { | ||
debug!("Already resolved ty {:?}, {:?}, {:?} {:?}", | ||
id, declaration, ty, location); | ||
|
@@ -572,6 +606,8 @@ impl<'ctx> BindgenContext<'ctx> { | |
CXType_ULong => TypeKind::Int(IntKind::ULong), | ||
CXType_LongLong => TypeKind::Int(IntKind::LongLong), | ||
CXType_ULongLong => TypeKind::Int(IntKind::ULongLong), | ||
CXType_Int128 => TypeKind::Int(IntKind::I128), | ||
CXType_UInt128 => TypeKind::Int(IntKind::U128), | ||
CXType_Float => TypeKind::Float(FloatKind::Float), | ||
CXType_Double => TypeKind::Float(FloatKind::Double), | ||
CXType_LongDouble => TypeKind::Float(FloatKind::LongDouble), | ||
|
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
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.
Nit: maybe use a
match
here?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.
Yeah, I want to use the
matches!
crate instead, here and in a few other places, but I'll do that in a follow-up.