Skip to content

Commit f804f9e

Browse files
committed
codegen: Factor out some bits of root_import.
1 parent c6a9b3b commit f804f9e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/codegen/mod.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,47 @@ use std::cell::Cell;
2323
use std::collections::{HashSet, VecDeque};
2424
use std::collections::hash_map::{Entry, HashMap};
2525
use std::fmt::Write;
26-
use std::iter;
2726
use std::mem;
2827
use std::ops;
2928
use syntax::abi::Abi;
3029
use syntax::ast;
3130
use syntax::codemap::{Span, respan};
3231
use syntax::ptr::P;
3332

34-
fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
35-
assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up");
36-
assert!(module.is_module());
33+
fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize {
34+
if !ctx.options().enable_cxx_namespaces {
35+
return 0;
36+
}
37+
38+
item.ancestors(ctx)
39+
.filter(|id| ctx.resolve_item(*id).is_module())
40+
.fold(1, |i, _| i + 1)
41+
}
42+
43+
fn top_level_module_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
44+
let mut path = vec![ctx.rust_ident_raw("self")];
45+
46+
let super_ = ctx.rust_ident_raw("super");
47+
48+
for _ in 0..root_import_depth(ctx, item) {
49+
path.push(super_.clone());
50+
}
3751

3852
let root = ctx.root_module().canonical_name(ctx);
3953
let root_ident = ctx.rust_ident(&root);
4054

41-
let super_ = aster::AstBuilder::new().id("super");
42-
let supers = module.ancestors(ctx)
43-
.filter(|id| ctx.resolve_item(*id).is_module())
44-
.map(|_| super_.clone())
45-
.chain(iter::once(super_));
46-
47-
let self_ = iter::once(aster::AstBuilder::new().id("self"));
48-
let root_ident = iter::once(root_ident);
55+
path.push(root_ident);
56+
path
57+
}
4958

50-
let path = self_.chain(supers).chain(root_ident);
59+
fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
60+
assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up");
61+
assert!(module.is_module());
5162

5263
let use_root = aster::AstBuilder::new()
5364
.item()
5465
.use_()
55-
.ids(path)
66+
.ids(top_level_module_path(ctx, module))
5667
.build()
5768
.build();
5869

0 commit comments

Comments
 (0)