@@ -23,36 +23,47 @@ use std::cell::Cell;
23
23
use std:: collections:: { HashSet , VecDeque } ;
24
24
use std:: collections:: hash_map:: { Entry , HashMap } ;
25
25
use std:: fmt:: Write ;
26
- use std:: iter;
27
26
use std:: mem;
28
27
use std:: ops;
29
28
use syntax:: abi:: Abi ;
30
29
use syntax:: ast;
31
30
use syntax:: codemap:: { Span , respan} ;
32
31
use syntax:: ptr:: P ;
33
32
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
+ }
37
51
38
52
let root = ctx. root_module ( ) . canonical_name ( ctx) ;
39
53
let root_ident = ctx. rust_ident ( & root) ;
40
54
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
+ }
49
58
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( ) ) ;
51
62
52
63
let use_root = aster:: AstBuilder :: new ( )
53
64
. item ( )
54
65
. use_ ( )
55
- . ids ( path )
66
+ . ids ( top_level_module_path ( ctx , module ) )
56
67
. build ( )
57
68
. build ( ) ;
58
69
0 commit comments