|
| 1 | +use rustc_attr_data_structures::AttributeKind; |
| 2 | +use rustc_feature::{AttributeTemplate, template}; |
| 3 | +use rustc_span::{Span, Symbol, sym}; |
| 4 | +use thin_vec::ThinVec; |
| 5 | + |
| 6 | +use crate::attributes::{ |
| 7 | + AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser, |
| 8 | +}; |
| 9 | +use crate::context::{AcceptContext, Stage}; |
| 10 | +use crate::parser::ArgParser; |
| 11 | + |
| 12 | +pub(crate) struct ProcMacroParser; |
| 13 | +impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroParser { |
| 14 | + const PATH: &[Symbol] = &[sym::proc_macro]; |
| 15 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; |
| 16 | + const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacro; |
| 17 | +} |
| 18 | + |
| 19 | +pub(crate) struct ProcMacroAttributeParser; |
| 20 | +impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroAttributeParser { |
| 21 | + const PATH: &[Symbol] = &[sym::proc_macro_attribute]; |
| 22 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; |
| 23 | + const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacroAttribute; |
| 24 | +} |
| 25 | + |
| 26 | +pub(crate) struct ProcMacroDeriveParser; |
| 27 | +impl<S: Stage> SingleAttributeParser<S> for ProcMacroDeriveParser { |
| 28 | + const PATH: &[Symbol] = &[sym::proc_macro_derive]; |
| 29 | + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast; |
| 30 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; |
| 31 | + const TEMPLATE: AttributeTemplate = |
| 32 | + template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"); |
| 33 | + |
| 34 | + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { |
| 35 | + let (trait_name, helper_attrs) = parse_derive_like(cx, args, true)?; |
| 36 | + Some(AttributeKind::ProcMacroDerive { |
| 37 | + trait_name: trait_name.expect("Trait name is mandatory, so it is present"), |
| 38 | + helper_attrs, |
| 39 | + span: cx.attr_span, |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +pub(crate) struct RustcBuiltinMacroParser; |
| 45 | +impl<S: Stage> SingleAttributeParser<S> for RustcBuiltinMacroParser { |
| 46 | + const PATH: &[Symbol] = &[sym::rustc_builtin_macro]; |
| 47 | + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast; |
| 48 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; |
| 49 | + const TEMPLATE: AttributeTemplate = |
| 50 | + template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"); |
| 51 | + |
| 52 | + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { |
| 53 | + let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?; |
| 54 | + Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs, span: cx.attr_span }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn parse_derive_like<S: Stage>( |
| 59 | + cx: &mut AcceptContext<'_, '_, S>, |
| 60 | + args: &ArgParser<'_>, |
| 61 | + trait_name_mandatory: bool, |
| 62 | +) -> Option<(Option<Symbol>, ThinVec<Symbol>)> { |
| 63 | + let Some(list) = args.list() else { |
| 64 | + cx.expected_list(cx.attr_span); |
| 65 | + return None; |
| 66 | + }; |
| 67 | + let mut items = list.mixed(); |
| 68 | + |
| 69 | + // Parse the name of the trait that is derived. |
| 70 | + let Some(trait_attr) = items.next() else { |
| 71 | + // For #[rustc_builtin_macro], it is permitted to leave out the trait name |
| 72 | + if !trait_name_mandatory { |
| 73 | + return None; |
| 74 | + } |
| 75 | + cx.expected_at_least_one_argument(list.span); |
| 76 | + return None; |
| 77 | + }; |
| 78 | + let Some(trait_attr) = trait_attr.meta_item() else { |
| 79 | + cx.unexpected_literal(trait_attr.span()); |
| 80 | + return None; |
| 81 | + }; |
| 82 | + let Some(trait_ident) = trait_attr.path().word() else { |
| 83 | + cx.expected_identifier(trait_attr.path().span()); |
| 84 | + return None; |
| 85 | + }; |
| 86 | + if !trait_ident.name.can_be_raw() { |
| 87 | + cx.expected_identifier(trait_ident.span); |
| 88 | + return None; |
| 89 | + } |
| 90 | + if let Err(e) = trait_attr.args().no_args() { |
| 91 | + cx.expected_no_args(e); |
| 92 | + return None; |
| 93 | + }; |
| 94 | + |
| 95 | + // Parse optional attributes |
| 96 | + let mut attributes = ThinVec::new(); |
| 97 | + if let Some(attrs) = items.next() { |
| 98 | + let Some(attr_list) = attrs.meta_item() else { |
| 99 | + cx.expected_list(attrs.span()); |
| 100 | + return None; |
| 101 | + }; |
| 102 | + if !attr_list.path().word_is(sym::attributes) { |
| 103 | + cx.expected_specific_argument(attrs.span(), vec!["attributes"]); |
| 104 | + return None; |
| 105 | + } |
| 106 | + let Some(attr_list) = attr_list.args().list() else { |
| 107 | + cx.expected_list(attrs.span()); |
| 108 | + return None; |
| 109 | + }; |
| 110 | + |
| 111 | + // Parse item in `attributes(...)` argument |
| 112 | + for attr in attr_list.mixed() { |
| 113 | + let Some(attr) = attr.meta_item() else { |
| 114 | + cx.expected_identifier(attr.span()); |
| 115 | + return None; |
| 116 | + }; |
| 117 | + if let Err(e) = attr.args().no_args() { |
| 118 | + cx.expected_no_args(e); |
| 119 | + return None; |
| 120 | + }; |
| 121 | + let Some(ident) = attr.path().word() else { |
| 122 | + cx.expected_identifier(attr.path().span()); |
| 123 | + return None; |
| 124 | + }; |
| 125 | + if !ident.name.can_be_raw() { |
| 126 | + cx.expected_identifier(ident.span); |
| 127 | + return None; |
| 128 | + } |
| 129 | + attributes.push(ident.name); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // If anything else is specified, we should reject it |
| 134 | + if let Some(next) = items.next() { |
| 135 | + cx.expected_no_args(next.span()); |
| 136 | + } |
| 137 | + |
| 138 | + Some((Some(trait_ident.name), attributes)) |
| 139 | +} |
0 commit comments