@@ -9,27 +9,15 @@ use std::borrow::ToOwned;
99use std:: env;
1010use std:: mem;
1111use std:: path:: PathBuf ;
12- use syntax:: ast;
12+ use syntax:: ast:: { Crate , Ident } ;
1313use syntax:: struct_span_err;
14- use syntax:: symbol:: { Symbol , kw , sym} ;
15- use syntax_pos:: { Span , DUMMY_SP } ;
14+ use syntax:: symbol:: sym;
15+ use syntax_pos:: Span ;
1616
1717use rustc_error_codes:: * ;
1818
1919/// Pointer to a registrar function.
20- pub type PluginRegistrarFun =
21- fn ( & mut Registry < ' _ > ) ;
22-
23- pub struct PluginRegistrar {
24- pub fun : PluginRegistrarFun ,
25- pub args : Vec < ast:: NestedMetaItem > ,
26- }
27-
28- struct PluginLoader < ' a > {
29- sess : & ' a Session ,
30- metadata_loader : & ' a dyn MetadataLoader ,
31- plugins : Vec < PluginRegistrar > ,
32- }
20+ type PluginRegistrarFn = fn ( & mut Registry < ' _ > ) ;
3321
3422fn call_malformed_plugin_attribute ( sess : & Session , span : Span ) {
3523 struct_span_err ! ( sess, span, E0498 , "malformed `plugin` attribute" )
@@ -40,98 +28,81 @@ fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
4028/// Read plugin metadata and dynamically load registrar functions.
4129pub fn load_plugins ( sess : & Session ,
4230 metadata_loader : & dyn MetadataLoader ,
43- krate : & ast:: Crate ,
44- addl_plugins : Option < Vec < String > > ) -> Vec < PluginRegistrar > {
45- let mut loader = PluginLoader { sess, metadata_loader, plugins : Vec :: new ( ) } ;
46-
47- // do not report any error now. since crate attributes are
48- // not touched by expansion, every use of plugin without
49- // the feature enabled will result in an error later...
50- if sess. features_untracked ( ) . plugin {
51- for attr in & krate. attrs {
52- if !attr. check_name ( sym:: plugin) {
53- continue ;
54- }
55-
56- let plugins = match attr. meta_item_list ( ) {
57- Some ( xs) => xs,
58- None => continue ,
59- } ;
31+ krate : & Crate ,
32+ addl_plugins : Option < Vec < String > > ) -> Vec < PluginRegistrarFn > {
33+ let mut plugins = Vec :: new ( ) ;
34+ let mut load_plugin = |ident| load_plugin ( & mut plugins, sess, metadata_loader, ident) ;
35+
36+ for attr in & krate. attrs {
37+ if !attr. check_name ( sym:: plugin) {
38+ continue ;
39+ }
6040
61- for plugin in plugins {
62- // plugins must have a name and can't be key = value
63- let name = plugin. name_or_empty ( ) ;
64- if name != kw:: Invalid && !plugin. is_value_str ( ) {
65- let args = plugin. meta_item_list ( ) . map ( ToOwned :: to_owned) ;
66- loader. load_plugin ( plugin. span ( ) , name, args. unwrap_or_default ( ) ) ;
67- } else {
68- call_malformed_plugin_attribute ( sess, attr. span ) ;
69- }
41+ for plugin in attr. meta_item_list ( ) . unwrap_or_default ( ) {
42+ match plugin. ident ( ) {
43+ Some ( ident) if plugin. is_word ( ) => load_plugin ( ident) ,
44+ _ => call_malformed_plugin_attribute ( sess, plugin. span ( ) ) ,
7045 }
7146 }
7247 }
7348
74- if let Some ( plugins) = addl_plugins {
75- for plugin in plugins {
76- loader. load_plugin ( DUMMY_SP , Symbol :: intern ( & plugin) , vec ! [ ] ) ;
77- }
49+ for plugin in addl_plugins. unwrap_or_default ( ) {
50+ load_plugin ( Ident :: from_str ( & plugin) ) ;
7851 }
7952
80- loader . plugins
53+ plugins
8154}
8255
83- impl < ' a > PluginLoader < ' a > {
84- fn load_plugin ( & mut self , span : Span , name : Symbol , args : Vec < ast:: NestedMetaItem > ) {
85- let registrar = locator:: find_plugin_registrar ( self . sess , self . metadata_loader , span, name) ;
86-
87- if let Some ( ( lib, disambiguator) ) = registrar {
88- let symbol = self . sess . generate_plugin_registrar_symbol ( disambiguator) ;
89- let fun = self . dylink_registrar ( span, lib, symbol) ;
90- self . plugins . push ( PluginRegistrar {
91- fun,
92- args,
93- } ) ;
94- }
56+ fn load_plugin ( plugins : & mut Vec < PluginRegistrarFn > ,
57+ sess : & Session ,
58+ metadata_loader : & dyn MetadataLoader ,
59+ ident : Ident ) {
60+ let registrar = locator:: find_plugin_registrar ( sess, metadata_loader, ident. span , ident. name ) ;
61+
62+ if let Some ( ( lib, disambiguator) ) = registrar {
63+ let symbol = sess. generate_plugin_registrar_symbol ( disambiguator) ;
64+ let fun = dylink_registrar ( sess, ident. span , lib, symbol) ;
65+ plugins. push ( fun) ;
9566 }
67+ }
9668
97- // Dynamically link a registrar function into the compiler process.
98- fn dylink_registrar ( & mut self ,
99- span : Span ,
100- path : PathBuf ,
101- symbol : String ) -> PluginRegistrarFun {
102- use rustc_metadata:: dynamic_lib:: DynamicLibrary ;
103-
104- // Make sure the path contains a / or the linker will search for it.
105- let path = env:: current_dir ( ) . unwrap ( ) . join ( & path) ;
106-
107- let lib = match DynamicLibrary :: open ( Some ( & path) ) {
108- Ok ( lib) => lib,
109- // this is fatal: there are almost certainly macros we need
110- // inside this crate, so continue would spew "macro undefined"
111- // errors
112- Err ( err) => {
113- self . sess . span_fatal ( span, & err)
114- }
115- } ;
116-
117- unsafe {
118- let registrar =
119- match lib. symbol ( & symbol) {
120- Ok ( registrar) => {
121- mem:: transmute :: < * mut u8 , PluginRegistrarFun > ( registrar)
122- }
123- // again fatal if we can't register macros
124- Err ( err) => {
125- self . sess . span_fatal ( span, & err)
126- }
127- } ;
128-
129- // Intentionally leak the dynamic library. We can't ever unload it
130- // since the library can make things that will live arbitrarily long
131- // (e.g., an @-box cycle or a thread).
132- mem:: forget ( lib) ;
133-
134- registrar
69+ // Dynamically link a registrar function into the compiler process.
70+ fn dylink_registrar ( sess : & Session ,
71+ span : Span ,
72+ path : PathBuf ,
73+ symbol : String ) -> PluginRegistrarFn {
74+ use rustc_metadata:: dynamic_lib:: DynamicLibrary ;
75+
76+ // Make sure the path contains a / or the linker will search for it.
77+ let path = env:: current_dir ( ) . unwrap ( ) . join ( & path) ;
78+
79+ let lib = match DynamicLibrary :: open ( Some ( & path) ) {
80+ Ok ( lib) => lib,
81+ // this is fatal: there are almost certainly macros we need
82+ // inside this crate, so continue would spew "macro undefined"
83+ // errors
84+ Err ( err) => {
85+ sess. span_fatal ( span, & err)
13586 }
87+ } ;
88+
89+ unsafe {
90+ let registrar =
91+ match lib. symbol ( & symbol) {
92+ Ok ( registrar) => {
93+ mem:: transmute :: < * mut u8 , PluginRegistrarFn > ( registrar)
94+ }
95+ // again fatal if we can't register macros
96+ Err ( err) => {
97+ sess. span_fatal ( span, & err)
98+ }
99+ } ;
100+
101+ // Intentionally leak the dynamic library. We can't ever unload it
102+ // since the library can make things that will live arbitrarily long
103+ // (e.g., an @-box cycle or a thread).
104+ mem:: forget ( lib) ;
105+
106+ registrar
136107 }
137108}
0 commit comments