@@ -23,36 +23,47 @@ use std::cell::Cell;
2323use std:: collections:: { HashSet , VecDeque } ;
2424use std:: collections:: hash_map:: { Entry , HashMap } ;
2525use std:: fmt:: Write ;
26- use std:: iter;
2726use std:: mem;
2827use std:: ops;
2928use syntax:: abi:: Abi ;
3029use syntax:: ast;
3130use syntax:: codemap:: { Span , respan} ;
3231use 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