@@ -121,12 +121,10 @@ fn register_native_lib(sess: &Session,
121
121
sess. cstore . add_used_library ( name, kind) ;
122
122
}
123
123
124
- pub struct PluginMetadata < ' a > {
125
- sess : & ' a Session ,
124
+ // Extra info about a crate loaded for plugins or exported macros.
125
+ struct ExtensionCrate {
126
126
metadata : PMDSource ,
127
127
dylib : Option < Path > ,
128
- info : CrateInfo ,
129
- vi_span : Span ,
130
128
target_only : bool ,
131
129
}
132
130
@@ -451,21 +449,7 @@ impl<'a> CrateReader<'a> {
451
449
} ) . collect ( )
452
450
}
453
451
454
- pub fn read_plugin_metadata < ' b > ( & ' b mut self ,
455
- krate : CrateOrString < ' b > ) -> PluginMetadata < ' b > {
456
- let ( info, span) = match krate {
457
- CrateOrString :: Krate ( c) => {
458
- ( self . extract_crate_info ( c) . unwrap ( ) , c. span )
459
- }
460
- CrateOrString :: Str ( sp, s) => {
461
- ( CrateInfo {
462
- name : s. to_string ( ) ,
463
- ident : s. to_string ( ) ,
464
- id : ast:: DUMMY_NODE_ID ,
465
- should_link : false ,
466
- } , sp)
467
- }
468
- } ;
452
+ fn read_extension_crate ( & mut self , span : Span , info : & CrateInfo ) -> ExtensionCrate {
469
453
let target_triple = & self . sess . opts . target_triple [ ] ;
470
454
let is_cross = target_triple != config:: host_triple ( ) ;
471
455
let mut should_link = info. should_link && !is_cross;
@@ -517,30 +501,21 @@ impl<'a> CrateReader<'a> {
517
501
PMDSource :: Owned ( library. metadata )
518
502
} ;
519
503
520
- PluginMetadata {
521
- sess : self . sess ,
504
+ ExtensionCrate {
522
505
metadata : metadata,
523
506
dylib : dylib. map ( |p| p. 0 ) ,
524
- info : info,
525
- vi_span : span,
526
507
target_only : target_only,
527
508
}
528
509
}
529
- }
530
510
531
- #[ derive( Copy ) ]
532
- pub enum CrateOrString < ' a > {
533
- Krate ( & ' a ast:: Item ) ,
534
- Str ( Span , & ' a str )
535
- }
511
+ /// Read exported macros.
512
+ pub fn read_exported_macros ( & mut self , krate : & ast:: Item ) -> Vec < ast:: MacroDef > {
513
+ let ci = self . extract_crate_info ( krate) . unwrap ( ) ;
514
+ let ekrate = self . read_extension_crate ( krate. span , & ci) ;
536
515
537
- impl < ' a > PluginMetadata < ' a > {
538
- /// Read exported macros
539
- pub fn exported_macros ( & self ) -> Vec < ast:: MacroDef > {
540
- let imported_from = Some ( token:: intern ( & self . info . ident [ ] ) . ident ( ) ) ;
541
- let source_name = format ! ( "<{} macros>" , & self . info. ident[ ] ) ;
516
+ let source_name = format ! ( "<{} macros>" , krate. ident) ;
542
517
let mut macros = vec ! [ ] ;
543
- decoder:: each_exported_macro ( self . metadata . as_slice ( ) ,
518
+ decoder:: each_exported_macro ( ekrate . metadata . as_slice ( ) ,
544
519
& * self . sess . cstore . intr ,
545
520
|name, attrs, body| {
546
521
// NB: Don't use parse::parse_tts_from_source_str because it parses with
@@ -558,7 +533,7 @@ impl<'a> PluginMetadata<'a> {
558
533
attrs : attrs,
559
534
id : ast:: DUMMY_NODE_ID ,
560
535
span : span,
561
- imported_from : imported_from ,
536
+ imported_from : Some ( krate . ident ) ,
562
537
// overridden in plugin/load.rs
563
538
export : false ,
564
539
use_locally : false ,
@@ -572,28 +547,35 @@ impl<'a> PluginMetadata<'a> {
572
547
}
573
548
574
549
/// Look for a plugin registrar. Returns library path and symbol name.
575
- pub fn plugin_registrar ( & self ) -> Option < ( Path , String ) > {
576
- if self . target_only {
550
+ pub fn find_plugin_registrar ( & mut self , span : Span , name : & str ) -> Option < ( Path , String ) > {
551
+ let ekrate = self . read_extension_crate ( span, & CrateInfo {
552
+ name : name. to_string ( ) ,
553
+ ident : name. to_string ( ) ,
554
+ id : ast:: DUMMY_NODE_ID ,
555
+ should_link : false ,
556
+ } ) ;
557
+
558
+ if ekrate. target_only {
577
559
// Need to abort before syntax expansion.
578
- let message = format ! ( "plugin crate `{}` is not available for triple `{}` \
560
+ let message = format ! ( "plugin `{}` is not available for triple `{}` \
579
561
(only found {})",
580
- self . info . ident ,
562
+ name ,
581
563
config:: host_triple( ) ,
582
564
self . sess. opts. target_triple) ;
583
- self . sess . span_err ( self . vi_span , & message[ ] ) ;
565
+ self . sess . span_err ( span , & message[ ] ) ;
584
566
self . sess . abort_if_errors ( ) ;
585
567
}
586
568
587
- let registrar = decoder:: get_plugin_registrar_fn ( self . metadata . as_slice ( ) )
588
- . map ( |id| decoder:: get_symbol ( self . metadata . as_slice ( ) , id) ) ;
569
+ let registrar = decoder:: get_plugin_registrar_fn ( ekrate . metadata . as_slice ( ) )
570
+ . map ( |id| decoder:: get_symbol ( ekrate . metadata . as_slice ( ) , id) ) ;
589
571
590
- match ( self . dylib . as_ref ( ) , registrar) {
572
+ match ( ekrate . dylib . as_ref ( ) , registrar) {
591
573
( Some ( dylib) , Some ( reg) ) => Some ( ( dylib. clone ( ) , reg) ) ,
592
574
( None , Some ( _) ) => {
593
- let message = format ! ( "plugin crate `{}` only found in rlib format, \
575
+ let message = format ! ( "plugin `{}` only found in rlib format, \
594
576
but must be available in dylib format",
595
- self . info . ident ) ;
596
- self . sess . span_err ( self . vi_span , & message[ ] ) ;
577
+ name ) ;
578
+ self . sess . span_err ( span , & message[ ] ) ;
597
579
// No need to abort because the loading code will just ignore this
598
580
// empty dylib.
599
581
None
0 commit comments