2014-05-26 14:48:54 -07:00
|
|
|
//! Used by `rustc` when compiling a plugin crate.
|
|
|
|
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2021-01-30 17:47:51 +01:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2013-12-25 11:10:33 -07:00
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
struct RegistrarFinder<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-01-30 17:47:51 +01:00
|
|
|
registrars: Vec<(LocalDefId, Span)>,
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
impl<'v, 'tcx> ItemLikeVisitor<'v> for RegistrarFinder<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &hir::Item<'_>) {
|
2019-09-26 17:51:36 +01:00
|
|
|
if let hir::ItemKind::Fn(..) = item.kind {
|
2021-01-24 13:17:54 +01:00
|
|
|
let attrs = self.tcx.hir().attrs(item.hir_id());
|
|
|
|
if self.tcx.sess.contains_name(attrs, sym::plugin_registrar) {
|
2021-01-30 17:47:51 +01:00
|
|
|
self.registrars.push((item.def_id, item.span));
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
|
2016-11-04 18:20:15 -04:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
|
2020-11-11 21:57:54 +01:00
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {}
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Finds the function marked with `#[plugin_registrar]`, if any.
|
2019-06-21 20:27:44 +02:00
|
|
|
pub fn find_plugin_registrar(tcx: TyCtxt<'_>) -> Option<DefId> {
|
2019-01-13 01:06:50 +01:00
|
|
|
tcx.plugin_registrar_fn(LOCAL_CRATE)
|
|
|
|
}
|
|
|
|
|
2019-06-21 20:27:44 +02:00
|
|
|
fn plugin_registrar_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<DefId> {
|
2019-01-13 01:06:50 +01:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
2016-01-29 15:04:07 -05:00
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
let mut finder = RegistrarFinder { tcx, registrars: Vec::new() };
|
2019-01-13 01:06:50 +01:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut finder);
|
2013-12-25 11:10:33 -07:00
|
|
|
|
2014-05-24 16:16:10 -07:00
|
|
|
match finder.registrars.len() {
|
2013-12-25 11:10:33 -07:00
|
|
|
0 => None,
|
|
|
|
1 => {
|
2021-01-30 17:47:51 +01:00
|
|
|
let (def_id, _) = finder.registrars.pop().unwrap();
|
|
|
|
Some(def_id.to_def_id())
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2013-12-25 11:10:33 -07:00
|
|
|
_ => {
|
2019-01-13 01:06:50 +01:00
|
|
|
let diagnostic = tcx.sess.diagnostic();
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut e = diagnostic.struct_err("multiple plugin registration functions found");
|
2015-01-31 12:20:46 -05:00
|
|
|
for &(_, span) in &finder.registrars {
|
2015-12-21 10:00:43 +13:00
|
|
|
e.span_note(span, "one is here");
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
2015-12-21 10:00:43 +13:00
|
|
|
e.emit();
|
2015-12-14 11:17:55 +13:00
|
|
|
diagnostic.abort_if_errors();
|
2013-12-25 11:10:33 -07:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 01:06:50 +01:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-22 17:42:04 -05:00
|
|
|
*providers = Providers { plugin_registrar_fn, ..*providers };
|
2019-01-13 01:06:50 +01:00
|
|
|
}
|