@@ -37,32 +37,12 @@ use syntax::visit;
37
37
use util:: fs;
38
38
use log;
39
39
40
- struct Env < ' a > {
40
+ pub struct CrateReader < ' a > {
41
41
sess : & ' a Session ,
42
42
next_crate_num : ast:: CrateNum ,
43
43
}
44
44
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 > {
66
46
fn visit_view_item ( & mut self , a : & ast:: ViewItem ) {
67
47
self . process_view_item ( a) ;
68
48
visit:: walk_view_item ( self , a) ;
@@ -173,8 +153,31 @@ fn register_native_lib(sess: &Session,
173
153
sess. cstore . add_used_library ( name, kind) ;
174
154
}
175
155
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 ) {
178
181
for a in c. attrs . iter ( ) . filter ( |m| m. name ( ) == "link_args" ) {
179
182
match a. value_str ( ) {
180
183
Some ( ref linkarg) => self . sess . cstore . add_used_link_args ( linkarg. get ( ) ) ,
@@ -445,35 +448,20 @@ impl<'a> Env<'a> {
445
448
( dep. cnum , local_cnum)
446
449
} ) . collect ( )
447
450
}
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
- }
463
451
464
452
pub fn read_plugin_metadata ( & mut self ,
465
453
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 [ ] ;
468
456
let is_cross = target_triple != config:: host_triple ( ) ;
469
457
let mut should_link = info. should_link && !is_cross;
470
458
let mut load_ctxt = loader:: Context {
471
- sess : self . env . sess ,
459
+ sess : self . sess ,
472
460
span : krate. span ,
473
461
ident : info. ident [ ] ,
474
462
crate_name : info. name [ ] ,
475
463
hash : None ,
476
- filesearch : self . env . sess . host_filesearch ( PathKind :: Crate ) ,
464
+ filesearch : self . sess . host_filesearch ( PathKind :: Crate ) ,
477
465
triple : config:: host_triple ( ) ,
478
466
root : & None ,
479
467
rejected_via_hash : vec ! ( ) ,
@@ -486,17 +474,17 @@ impl<'a> PluginMetadataReader<'a> {
486
474
// try loading from target crates (only valid if there are
487
475
// no syntax extensions)
488
476
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 ) ;
490
478
let lib = load_ctxt. load_library_crate ( ) ;
491
479
if decoder:: get_plugin_registrar_fn ( lib. metadata . as_slice ( ) ) . is_some ( ) {
492
480
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[ ] ) ;
496
484
// need to abort now because the syntax expansion
497
485
// code will shortly attempt to load and execute
498
486
// code from the found library.
499
- self . env . sess . abort_if_errors ( ) ;
487
+ self . sess . abort_if_errors ( ) ;
500
488
}
501
489
should_link = info. should_link ;
502
490
lib
@@ -510,8 +498,8 @@ impl<'a> PluginMetadataReader<'a> {
510
498
if library. dylib . is_none ( ) && registrar. is_some ( ) {
511
499
let message = format ! ( "plugin crate `{}` only found in rlib format, \
512
500
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[ ] ) ;
515
503
// No need to abort because the loading code will just ignore this
516
504
// empty dylib.
517
505
}
@@ -520,10 +508,10 @@ impl<'a> PluginMetadataReader<'a> {
520
508
macros : macros,
521
509
registrar_symbol : registrar,
522
510
} ;
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 ( ) {
524
512
// 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) ;
527
515
}
528
516
pc
529
517
}
0 commit comments