Skip to content

full support non rust naming conventions of generated code #580

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use syntax::codemap::{Span, respan};
use syntax::ptr::P;

fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize {
if !ctx.options().enable_cxx_namespaces {
if !(item.id() == ctx.root_module() || ctx.options().enable_cxx_namespaces) {
return 0;
}

Expand All @@ -51,7 +51,7 @@ fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize {
fn top_level_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
let mut path = vec![ctx.rust_ident_raw("self")];

if ctx.options().enable_cxx_namespaces {
if item.id() == ctx.root_module() || ctx.options().enable_cxx_namespaces {
let super_ = ctx.rust_ident_raw("super");

for _ in 0..root_import_depth(ctx, item) {
Expand All @@ -63,14 +63,18 @@ fn top_level_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
}

fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up");
assert!(module.id() == ctx.root_module() || ctx.options().enable_cxx_namespaces, "Somebody messed it up");
assert!(module.is_module());

let mut path = top_level_path(ctx, module);

let root = ctx.root_module().canonical_name(ctx);
let root_ident = ctx.rust_ident(&root);
path.push(root_ident);
if module.id() == ctx.root_module() && !ctx.options().enable_cxx_namespaces {
path.push(ctx.rust_ident_raw("*"));
} else {
let root = ctx.root_module().canonical_name(ctx);
let root_ident = ctx.rust_ident(&root);
path.push(root_ident);
}

let use_root = aster::AstBuilder::new()
.item()
Expand Down Expand Up @@ -361,7 +365,11 @@ impl CodeGenerator for Module {
}
}

if item.id() == ctx.root_module() {
if item.id() == ctx.sentinel_module() {
if result.saw_objc {
utils::prepend_objc_header(ctx, &mut *result);
}
} else if item.id() == ctx.root_module() {
if result.saw_union && !ctx.options().unstable_rust {
utils::prepend_union_types(ctx, &mut *result);
}
Expand All @@ -371,13 +379,11 @@ impl CodeGenerator for Module {
if ctx.need_bindegen_complex_type() {
utils::prepend_complex_type(ctx, &mut *result);
}
if result.saw_objc {
utils::prepend_objc_header(ctx, &mut *result);
}
}
};

if !ctx.options().enable_cxx_namespaces ||
let item_id = item.id();
if item_id == ctx.sentinel_module() || (item_id != ctx.root_module() && !ctx.options().enable_cxx_namespaces) ||
(self.is_inline() && !ctx.options().conservative_inline_namespaces) {
codegen_self(result, &mut false);
return;
Expand Down Expand Up @@ -2950,7 +2956,7 @@ pub fn codegen(context: &mut BindgenContext) -> Vec<P<ast::Item>> {
}
}

context.resolve_item(context.root_module())
context.resolve_item(context.sentinel_module())
.codegen(context, &mut result, &whitelisted_items, &());

result.items
Expand Down
31 changes: 27 additions & 4 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ pub struct BindgenContext<'ctx> {
/// A cursor to module map. Similar reason than above.
modules: HashMap<Cursor, ItemId>,

/// The sentinel module is the real root for the whole generated code.
/// It's always inline and should have exactly one child, namely the root_module.
sentinel_module: ItemId,

/// The root module, this is guaranteed to be an item of kind Module.
root_module: ItemId,

Expand Down Expand Up @@ -186,13 +190,21 @@ impl<'ctx> BindgenContext<'ctx> {
parse_options)
.expect("TranslationUnit::parse failed");

let root_module = Self::build_root_module(ItemId(0));
let mut sentinel_module = Self::build_sentinel_module(ItemId(0));
let root_module = Self::build_root_module(ItemId(1), ItemId(1));

{
let mut sm = sentinel_module.as_module_mut().unwrap();
sm.children_mut().push(ItemId(1));
}

let mut me = BindgenContext {
items: Default::default(),
types: Default::default(),
named_types: Default::default(),
modules: Default::default(),
next_item_id: ItemId(1),
next_item_id: ItemId(2),
sentinel_module: sentinel_module.id(),
root_module: root_module.id(),
current_module: root_module.id(),
currently_parsed_types: vec![],
Expand All @@ -208,6 +220,7 @@ impl<'ctx> BindgenContext<'ctx> {
used_template_parameters: None,
};

me.add_item(sentinel_module, None, None);
me.add_item(root_module, None, None);

me
Expand Down Expand Up @@ -662,11 +675,21 @@ impl<'ctx> BindgenContext<'ctx> {
assert!(old_item.is_none(), "Inserted type twice?");
}

fn build_root_module(id: ItemId) -> Item {
let module = Module::new(Some("root".into()), ModuleKind::Normal);
fn build_sentinel_module(id: ItemId) -> Item {
let module = Module::new(Some("<sentinel>".into()), ModuleKind::Inline);
Item::new(id, None, None, id, ItemKind::Module(module))
}

fn build_root_module(id: ItemId, parent_id: ItemId) -> Item {
let module = Module::new(Some("root".into()), ModuleKind::Normal);
Item::new(id, None, None, parent_id, ItemKind::Module(module))
}

/// Get the sentinel module
pub fn sentinel_module(&self) -> ItemId {
self.sentinel_module
}

/// Get the root module.
pub fn root_module(&self) -> ItemId {
self.root_module
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,7 @@ impl<'ctx> Bindings<'ctx> {

/// Write these bindings as source text to the given `Write`able.
pub fn write<'a>(&self, mut writer: Box<Write + 'a>) -> io::Result<()> {
try!(writer.write("/* automatically generated by rust-bindgen */\n\n"
.as_bytes()));
try!(write!(writer, "/* automatically generated by rust-bindgen */\n\n"));

for line in self.context.options().raw_lines.iter() {
try!(writer.write(line.as_bytes()));
Expand All @@ -829,6 +828,10 @@ impl<'ctx> Bindings<'ctx> {
try!(writer.write("\n".as_bytes()));
}

if !self.context.options().enable_cxx_namespaces && !self.module.items.is_empty() {
try!(write!(writer, "pub use self::root::*;\n\n"));
}

let mut ps = pprust::rust_printer(writer);
try!(ps.print_mod(&self.module, &[]));
try!(ps.print_remaining_comments());
Expand Down
Loading