@@ -121,12 +121,10 @@ fn register_native_lib(sess: &Session,
121121 sess. cstore . add_used_library ( name, kind) ;
122122}
123123
124- pub struct PluginMetadata < ' a > {
125- sess : & ' a Session ,
124+ // Extra info about a crate loaded for plugins or exported macros.
125+ struct ExtensionCrate {
126126 metadata : PMDSource ,
127127 dylib : Option < Path > ,
128- info : CrateInfo ,
129- vi_span : Span ,
130128 target_only : bool ,
131129}
132130
@@ -451,21 +449,7 @@ impl<'a> CrateReader<'a> {
451449 } ) . collect ( )
452450 }
453451
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 {
469453 let target_triple = & self . sess . opts . target_triple [ ] ;
470454 let is_cross = target_triple != config:: host_triple ( ) ;
471455 let mut should_link = info. should_link && !is_cross;
@@ -517,30 +501,21 @@ impl<'a> CrateReader<'a> {
517501 PMDSource :: Owned ( library. metadata )
518502 } ;
519503
520- PluginMetadata {
521- sess : self . sess ,
504+ ExtensionCrate {
522505 metadata : metadata,
523506 dylib : dylib. map ( |p| p. 0 ) ,
524- info : info,
525- vi_span : span,
526507 target_only : target_only,
527508 }
528509 }
529- }
530510
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) ;
536515
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) ;
542517 let mut macros = vec ! [ ] ;
543- decoder:: each_exported_macro ( self . metadata . as_slice ( ) ,
518+ decoder:: each_exported_macro ( ekrate . metadata . as_slice ( ) ,
544519 & * self . sess . cstore . intr ,
545520 |name, attrs, body| {
546521 // NB: Don't use parse::parse_tts_from_source_str because it parses with
@@ -558,7 +533,7 @@ impl<'a> PluginMetadata<'a> {
558533 attrs : attrs,
559534 id : ast:: DUMMY_NODE_ID ,
560535 span : span,
561- imported_from : imported_from ,
536+ imported_from : Some ( krate . ident ) ,
562537 // overridden in plugin/load.rs
563538 export : false ,
564539 use_locally : false ,
@@ -572,28 +547,35 @@ impl<'a> PluginMetadata<'a> {
572547 }
573548
574549 /// 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 {
577559 // 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 `{}` \
579561 (only found {})",
580- self . info . ident ,
562+ name ,
581563 config:: host_triple( ) ,
582564 self . sess. opts. target_triple) ;
583- self . sess . span_err ( self . vi_span , & message[ ] ) ;
565+ self . sess . span_err ( span , & message[ ] ) ;
584566 self . sess . abort_if_errors ( ) ;
585567 }
586568
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) ) ;
589571
590- match ( self . dylib . as_ref ( ) , registrar) {
572+ match ( ekrate . dylib . as_ref ( ) , registrar) {
591573 ( Some ( dylib) , Some ( reg) ) => Some ( ( dylib. clone ( ) , reg) ) ,
592574 ( None , Some ( _) ) => {
593- let message = format ! ( "plugin crate `{}` only found in rlib format, \
575+ let message = format ! ( "plugin `{}` only found in rlib format, \
594576 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[ ] ) ;
597579 // No need to abort because the loading code will just ignore this
598580 // empty dylib.
599581 None
0 commit comments