Skip to content

Commit 6b784ba

Browse files
author
Keegan McAllister
committed
creader: Clean up macro/plugin API
Step towards #22198.
1 parent cca1cf6 commit 6b784ba

File tree

3 files changed

+43
-75
lines changed

3 files changed

+43
-75
lines changed

src/librustc/metadata/creader.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/librustc/plugin/load.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Used by `rustc` when loading a plugin, or a crate with exported macros.
1212
1313
use session::Session;
14-
use metadata::creader::{CrateOrString, CrateReader};
14+
use metadata::creader::CrateReader;
1515
use plugin::registry::Registry;
1616

1717
use std::mem;
@@ -102,14 +102,13 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate,
102102
}
103103

104104
let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
105-
loader.load_plugin(CrateOrString::Str(plugin.span, &*plugin.name()),
106-
args);
105+
loader.load_plugin(plugin.span, &*plugin.name(), args);
107106
}
108107
}
109108

110109
if let Some(plugins) = addl_plugins {
111110
for plugin in plugins {
112-
loader.load_plugin(CrateOrString::Str(COMMAND_LINE_SP, &plugin), vec![]);
111+
loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
113112
}
114113
}
115114

@@ -211,10 +210,10 @@ impl<'a> PluginLoader<'a> {
211210
return;
212211
}
213212

214-
let pmd = self.reader.read_plugin_metadata(CrateOrString::Krate(vi));
215-
213+
let macros = self.reader.read_exported_macros(vi);
216214
let mut seen = HashSet::new();
217-
for mut def in pmd.exported_macros() {
215+
216+
for mut def in macros {
218217
let name = token::get_ident(def.ident);
219218
seen.insert(name.clone());
220219

@@ -241,16 +240,11 @@ impl<'a> PluginLoader<'a> {
241240
}
242241
}
243242

244-
pub fn load_plugin<'b>(&mut self,
245-
c: CrateOrString<'b>,
246-
args: Vec<P<ast::MetaItem>>) {
247-
let registrar = {
248-
let pmd = self.reader.read_plugin_metadata(c);
249-
pmd.plugin_registrar()
250-
};
243+
pub fn load_plugin(&mut self, span: Span, name: &str, args: Vec<P<ast::MetaItem>>) {
244+
let registrar = self.reader.find_plugin_registrar(span, name);
251245

252246
if let Some((lib, symbol)) = registrar {
253-
let fun = self.dylink_registrar(c, lib, symbol);
247+
let fun = self.dylink_registrar(span, lib, symbol);
254248
self.plugins.registrars.push(PluginRegistrar {
255249
fun: fun,
256250
args: args,
@@ -259,8 +253,8 @@ impl<'a> PluginLoader<'a> {
259253
}
260254

261255
// Dynamically link a registrar function into the compiler process.
262-
fn dylink_registrar<'b>(&mut self,
263-
c: CrateOrString<'b>,
256+
fn dylink_registrar(&mut self,
257+
span: Span,
264258
path: Path,
265259
symbol: String) -> PluginRegistrarFun {
266260
// Make sure the path contains a / or the linker will search for it.
@@ -272,11 +266,7 @@ impl<'a> PluginLoader<'a> {
272266
// inside this crate, so continue would spew "macro undefined"
273267
// errors
274268
Err(err) => {
275-
if let CrateOrString::Krate(cr) = c {
276-
self.sess.span_fatal(cr.span, &err[])
277-
} else {
278-
self.sess.fatal(&err[])
279-
}
269+
self.sess.span_fatal(span, &err[])
280270
}
281271
};
282272

@@ -288,11 +278,7 @@ impl<'a> PluginLoader<'a> {
288278
}
289279
// again fatal if we can't register macros
290280
Err(err) => {
291-
if let CrateOrString::Krate(cr) = c {
292-
self.sess.span_fatal(cr.span, &err[])
293-
} else {
294-
self.sess.fatal(&err[])
295-
}
281+
self.sess.span_fatal(span, &err[])
296282
}
297283
};
298284

src/test/compile-fail-fulldeps/macro-crate-rlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
#![feature(plugin)]
1818
#![plugin(rlib_crate_test)]
19-
//~^ ERROR: plugin crate `rlib_crate_test` only found in rlib format, but must be available in dylib format
19+
//~^ ERROR: plugin `rlib_crate_test` only found in rlib format, but must be available in dylib format
2020

2121
fn main() {}

0 commit comments

Comments
 (0)