5 use rustc_ast::visit::{self, Visitor};
6 use rustc_ast::{self as ast, NodeId};
7 use rustc_ast_pretty::pprust;
8 use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand};
9 use rustc_expand::expand::{AstFragment, ExpansionConfig};
10 use rustc_session::Session;
11 use rustc_span::hygiene::AstPass;
12 use rustc_span::source_map::SourceMap;
13 use rustc_span::symbol::{kw, sym, Ident, Symbol};
14 use rustc_span::{Span, DUMMY_SP};
15 use smallvec::smallvec;
17 struct ProcMacroDerive {
25 enum ProcMacroDefType {
34 def_type: ProcMacroDefType,
38 Derive(ProcMacroDerive),
42 struct CollectProcMacros<'a> {
44 macros: Vec<ProcMacro>,
46 handler: &'a rustc_errors::Handler,
47 source_map: &'a SourceMap,
48 is_proc_macro_crate: bool,
54 resolver: &mut dyn ResolverExpand,
55 mut krate: ast::Crate,
56 is_proc_macro_crate: bool,
57 has_proc_macro_decls: bool,
59 handler: &rustc_errors::Handler,
61 let ecfg = ExpansionConfig::default("proc_macro".to_string());
62 let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);
64 let mut collect = CollectProcMacros {
69 source_map: sess.source_map(),
74 if has_proc_macro_decls || is_proc_macro_crate {
75 visit::walk_crate(&mut collect, &krate);
77 let macros = collect.macros;
79 if !is_proc_macro_crate {
87 let decls = mk_decls(&mut cx, ¯os);
88 krate.items.push(decls);
93 impl<'a> CollectProcMacros<'a> {
94 fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) {
95 if self.is_proc_macro_crate && self.in_root && vis.kind.is_pub() {
96 self.handler.span_err(
98 "`proc-macro` crate types currently cannot export any items other \
99 than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, \
100 or `#[proc_macro_attribute]`",
105 fn collect_custom_derive(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) {
106 let Some((trait_name, proc_attrs)) = parse_macro_name_and_helper_attrs(self.handler, attr, "derive") else {
110 if self.in_root && item.vis.kind.is_pub() {
111 self.macros.push(ProcMacro::Derive(ProcMacroDerive {
115 function_name: item.ident,
119 let msg = if !self.in_root {
120 "functions tagged with `#[proc_macro_derive]` must \
121 currently reside in the root of the crate"
123 "functions tagged with `#[proc_macro_derive]` must be `pub`"
125 self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
129 fn collect_attr_proc_macro(&mut self, item: &'a ast::Item) {
130 if self.in_root && item.vis.kind.is_pub() {
131 self.macros.push(ProcMacro::Def(ProcMacroDef {
134 function_name: item.ident,
135 def_type: ProcMacroDefType::Attr,
138 let msg = if !self.in_root {
139 "functions tagged with `#[proc_macro_attribute]` must \
140 currently reside in the root of the crate"
142 "functions tagged with `#[proc_macro_attribute]` must be `pub`"
144 self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
148 fn collect_bang_proc_macro(&mut self, item: &'a ast::Item) {
149 if self.in_root && item.vis.kind.is_pub() {
150 self.macros.push(ProcMacro::Def(ProcMacroDef {
153 function_name: item.ident,
154 def_type: ProcMacroDefType::Bang,
157 let msg = if !self.in_root {
158 "functions tagged with `#[proc_macro]` must \
159 currently reside in the root of the crate"
161 "functions tagged with `#[proc_macro]` must be `pub`"
163 self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
168 impl<'a> Visitor<'a> for CollectProcMacros<'a> {
169 fn visit_item(&mut self, item: &'a ast::Item) {
170 if let ast::ItemKind::MacroDef(..) = item.kind {
171 if self.is_proc_macro_crate && self.sess.contains_name(&item.attrs, sym::macro_export) {
173 "cannot export macro_rules! macros from a `proc-macro` crate type currently";
174 self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
178 // First up, make sure we're checking a bare function. If we're not then
179 // we're just not interested in this item.
181 // If we find one, try to locate a `#[proc_macro_derive]` attribute on it.
182 let is_fn = matches!(item.kind, ast::ItemKind::Fn(..));
184 let mut found_attr: Option<&'a ast::Attribute> = None;
186 for attr in &item.attrs {
187 if self.sess.is_proc_macro_attr(&attr) {
188 if let Some(prev_attr) = found_attr {
189 let prev_item = prev_attr.get_normal_item();
190 let item = attr.get_normal_item();
191 let path_str = pprust::path_to_string(&item.path);
192 let msg = if item.path.segments[0].ident.name
193 == prev_item.path.segments[0].ident.name
196 "only one `#[{}]` attribute is allowed on any given function",
201 "`#[{}]` and `#[{}]` attributes cannot both be applied
202 to the same function",
204 pprust::path_to_string(&prev_item.path),
209 .struct_span_err(attr.span, &msg)
210 .span_label(prev_attr.span, "previous attribute here")
216 found_attr = Some(attr);
220 let Some(attr) = found_attr else {
221 self.check_not_pub_in_root(&item.vis, self.source_map.guess_head_span(item.span));
222 let prev_in_root = mem::replace(&mut self.in_root, false);
223 visit::walk_item(self, item);
224 self.in_root = prev_in_root;
230 "the `#[{}]` attribute may only be used on bare functions",
231 pprust::path_to_string(&attr.get_normal_item().path),
234 self.handler.span_err(attr.span, &msg);
238 if self.is_test_crate {
242 if !self.is_proc_macro_crate {
244 "the `#[{}]` attribute is only usable with crates of the `proc-macro` crate type",
245 pprust::path_to_string(&attr.get_normal_item().path),
248 self.handler.span_err(attr.span, &msg);
252 if attr.has_name(sym::proc_macro_derive) {
253 self.collect_custom_derive(item, attr);
254 } else if attr.has_name(sym::proc_macro_attribute) {
255 self.collect_attr_proc_macro(item);
256 } else if attr.has_name(sym::proc_macro) {
257 self.collect_bang_proc_macro(item);
260 let prev_in_root = mem::replace(&mut self.in_root, false);
261 visit::walk_item(self, item);
262 self.in_root = prev_in_root;
266 // Creates a new module which looks like:
269 // extern crate proc_macro;
271 // use proc_macro::bridge::client::ProcMacro;
273 // #[rustc_proc_macro_decls]
274 // #[allow(deprecated)]
275 // static DECLS: &[ProcMacro] = &[
276 // ProcMacro::custom_derive($name_trait1, &[], ::$name1);
277 // ProcMacro::custom_derive($name_trait2, &["attribute_name"], ::$name2);
281 fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
282 let expn_id = cx.resolver.expansion_for_ast_pass(
284 AstPass::ProcMacroHarness,
285 &[sym::rustc_attrs, sym::proc_macro_internals],
288 let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
290 let proc_macro = Ident::new(sym::proc_macro, span);
291 let krate = cx.item(span, proc_macro, Vec::new(), ast::ItemKind::ExternCrate(None));
293 let bridge = Ident::new(sym::bridge, span);
294 let client = Ident::new(sym::client, span);
295 let proc_macro_ty = Ident::new(sym::ProcMacro, span);
296 let custom_derive = Ident::new(sym::custom_derive, span);
297 let attr = Ident::new(sym::attr, span);
298 let bang = Ident::new(sym::bang, span);
300 // We add NodeIds to 'resolver.proc_macros' in the order
301 // that we generate expressions. The position of each NodeId
302 // in the 'proc_macros' Vec corresponds to its position
303 // in the static array that will be generated
305 let local_path = |cx: &ExtCtxt<'_>, sp: Span, name| {
306 cx.expr_path(cx.path(sp.with_ctxt(span.ctxt()), vec![name]))
308 let proc_macro_ty_method_path = |cx: &ExtCtxt<'_>, method| {
309 cx.expr_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty, method]))
314 ProcMacro::Derive(cd) => {
315 cx.resolver.declare_proc_macro(cd.id);
318 proc_macro_ty_method_path(cx, custom_derive),
320 cx.expr_str(cd.span, cd.trait_name),
325 .map(|&s| cx.expr_str(cd.span, s))
326 .collect::<Vec<_>>(),
328 local_path(cx, cd.span, cd.function_name),
332 ProcMacro::Def(ca) => {
333 cx.resolver.declare_proc_macro(ca.id);
334 let ident = match ca.def_type {
335 ProcMacroDefType::Attr => attr,
336 ProcMacroDefType::Bang => bang,
341 proc_macro_ty_method_path(cx, ident),
343 cx.expr_str(ca.span, ca.function_name.name),
344 local_path(cx, ca.span, ca.function_name),
352 let decls_static = cx
355 Ident::new(sym::_DECLS, span),
361 cx.ty_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty])),
365 ast::Mutability::Not,
367 ast::Mutability::Not,
368 cx.expr_vec_slice(span, decls),
371 let attr = cx.meta_word(span, sym::rustc_proc_macro_decls);
372 i.attrs.push(cx.attribute(attr));
374 let deprecated_attr = attr::mk_nested_word_item(Ident::new(sym::deprecated, span));
375 let allow_deprecated_attr =
376 attr::mk_list_item(Ident::new(sym::allow, span), vec![deprecated_attr]);
377 i.attrs.push(cx.attribute(allow_deprecated_attr));
382 let block = cx.expr_block(
383 cx.block(span, vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
386 let anon_constant = cx.item_const(
388 Ident::new(kw::Underscore, span),
389 cx.ty(span, ast::TyKind::Tup(Vec::new())),
393 // Integrate the new item into existing module structures.
394 let items = AstFragment::Items(smallvec![anon_constant]);
395 cx.monotonic_expander().fully_expand_fragment(items).make_items().pop().unwrap()