@@ -37,32 +37,12 @@ use syntax::visit;
3737use util:: fs;
3838use log;
3939
40- struct Env < ' a > {
40+ pub struct CrateReader < ' a > {
4141 sess : & ' a Session ,
4242 next_crate_num : ast:: CrateNum ,
4343}
4444
45- // Traverses an AST, reading all the information about use'd crates and extern
46- // libraries necessary for later resolving, typechecking, linking, etc.
47- pub fn read_crates ( sess : & Session ,
48- krate : & ast:: Crate ) {
49- let mut e = Env {
50- sess : sess,
51- next_crate_num : sess. cstore . next_crate_num ( ) ,
52- } ;
53- e. visit_crate ( krate) ;
54- visit:: walk_crate ( & mut e, krate) ;
55- if log_enabled ! ( log:: DEBUG ) {
56- dump_crates ( & sess. cstore ) ;
57- }
58- warn_if_multiple_versions ( sess. diagnostic ( ) , & sess. cstore ) ;
59-
60- for & ( ref name, kind) in sess. opts . libs . iter ( ) {
61- register_native_lib ( sess, None , name. clone ( ) , kind) ;
62- }
63- }
64-
65- impl < ' a , ' v > visit:: Visitor < ' v > for Env < ' a > {
45+ impl < ' a , ' v > visit:: Visitor < ' v > for CrateReader < ' a > {
6646 fn visit_view_item ( & mut self , a : & ast:: ViewItem ) {
6747 self . process_view_item ( a) ;
6848 visit:: walk_view_item ( self , a) ;
@@ -173,8 +153,31 @@ fn register_native_lib(sess: &Session,
173153 sess. cstore . add_used_library ( name, kind) ;
174154}
175155
176- impl < ' a > Env < ' a > {
177- fn visit_crate ( & self , c : & ast:: Crate ) {
156+ impl < ' a > CrateReader < ' a > {
157+ pub fn new ( sess : & ' a Session ) -> CrateReader < ' a > {
158+ CrateReader {
159+ sess : sess,
160+ next_crate_num : sess. cstore . next_crate_num ( ) ,
161+ }
162+ }
163+
164+ // Traverses an AST, reading all the information about use'd crates and extern
165+ // libraries necessary for later resolving, typechecking, linking, etc.
166+ pub fn read_crates ( & mut self , krate : & ast:: Crate ) {
167+ self . process_crate ( krate) ;
168+ visit:: walk_crate ( self , krate) ;
169+
170+ if log_enabled ! ( log:: DEBUG ) {
171+ dump_crates ( & self . sess . cstore ) ;
172+ }
173+ warn_if_multiple_versions ( self . sess . diagnostic ( ) , & self . sess . cstore ) ;
174+
175+ for & ( ref name, kind) in self . sess . opts . libs . iter ( ) {
176+ register_native_lib ( self . sess , None , name. clone ( ) , kind) ;
177+ }
178+ }
179+
180+ fn process_crate ( & self , c : & ast:: Crate ) {
178181 for a in c. attrs . iter ( ) . filter ( |m| m. name ( ) == "link_args" ) {
179182 match a. value_str ( ) {
180183 Some ( ref linkarg) => self . sess . cstore . add_used_link_args ( linkarg. get ( ) ) ,
@@ -445,35 +448,20 @@ impl<'a> Env<'a> {
445448 ( dep. cnum , local_cnum)
446449 } ) . collect ( )
447450 }
448- }
449-
450- pub struct PluginMetadataReader < ' a > {
451- env : Env < ' a > ,
452- }
453-
454- impl < ' a > PluginMetadataReader < ' a > {
455- pub fn new ( sess : & ' a Session ) -> PluginMetadataReader < ' a > {
456- PluginMetadataReader {
457- env : Env {
458- sess : sess,
459- next_crate_num : sess. cstore . next_crate_num ( ) ,
460- }
461- }
462- }
463451
464452 pub fn read_plugin_metadata ( & mut self ,
465453 krate : & ast:: ViewItem ) -> PluginMetadata {
466- let info = self . env . extract_crate_info ( krate) . unwrap ( ) ;
467- let target_triple = self . env . sess . opts . target_triple [ ] ;
454+ let info = self . extract_crate_info ( krate) . unwrap ( ) ;
455+ let target_triple = self . sess . opts . target_triple [ ] ;
468456 let is_cross = target_triple != config:: host_triple ( ) ;
469457 let mut should_link = info. should_link && !is_cross;
470458 let mut load_ctxt = loader:: Context {
471- sess : self . env . sess ,
459+ sess : self . sess ,
472460 span : krate. span ,
473461 ident : info. ident [ ] ,
474462 crate_name : info. name [ ] ,
475463 hash : None ,
476- filesearch : self . env . sess . host_filesearch ( PathKind :: Crate ) ,
464+ filesearch : self . sess . host_filesearch ( PathKind :: Crate ) ,
477465 triple : config:: host_triple ( ) ,
478466 root : & None ,
479467 rejected_via_hash : vec ! ( ) ,
@@ -486,17 +474,17 @@ impl<'a> PluginMetadataReader<'a> {
486474 // try loading from target crates (only valid if there are
487475 // no syntax extensions)
488476 load_ctxt. triple = target_triple;
489- load_ctxt. filesearch = self . env . sess . target_filesearch ( PathKind :: Crate ) ;
477+ load_ctxt. filesearch = self . sess . target_filesearch ( PathKind :: Crate ) ;
490478 let lib = load_ctxt. load_library_crate ( ) ;
491479 if decoder:: get_plugin_registrar_fn ( lib. metadata . as_slice ( ) ) . is_some ( ) {
492480 let message = format ! ( "crate `{}` contains a plugin_registrar fn but \
493- only a version for triple `{}` could be found (need {})",
494- info. ident, target_triple, config:: host_triple( ) ) ;
495- self . env . sess . span_err ( krate. span , message[ ] ) ;
481+ only a version for triple `{}` could be found (need {})",
482+ info. ident, target_triple, config:: host_triple( ) ) ;
483+ self . sess . span_err ( krate. span , message[ ] ) ;
496484 // need to abort now because the syntax expansion
497485 // code will shortly attempt to load and execute
498486 // code from the found library.
499- self . env . sess . abort_if_errors ( ) ;
487+ self . sess . abort_if_errors ( ) ;
500488 }
501489 should_link = info. should_link ;
502490 lib
@@ -510,8 +498,8 @@ impl<'a> PluginMetadataReader<'a> {
510498 if library. dylib . is_none ( ) && registrar. is_some ( ) {
511499 let message = format ! ( "plugin crate `{}` only found in rlib format, \
512500 but must be available in dylib format",
513- info. ident) ;
514- self . env . sess . span_err ( krate. span , message[ ] ) ;
501+ info. ident) ;
502+ self . sess . span_err ( krate. span , message[ ] ) ;
515503 // No need to abort because the loading code will just ignore this
516504 // empty dylib.
517505 }
@@ -520,10 +508,10 @@ impl<'a> PluginMetadataReader<'a> {
520508 macros : macros,
521509 registrar_symbol : registrar,
522510 } ;
523- if should_link && self . env . existing_match ( info. name [ ] , None ) . is_none ( ) {
511+ if should_link && self . existing_match ( info. name [ ] , None ) . is_none ( ) {
524512 // register crate now to avoid double-reading metadata
525- self . env . register_crate ( & None , info. ident [ ] ,
526- info. name [ ] , krate. span , library) ;
513+ self . register_crate ( & None , info. ident [ ] ,
514+ info. name [ ] , krate. span , library) ;
527515 }
528516 pc
529517 }
0 commit comments