|
| 1 | +use crate::errors; |
| 2 | +use rustc_ast::{ |
| 3 | + attr::mk_attr, |
| 4 | + ptr::P, |
| 5 | + token, |
| 6 | + tokenstream::{DelimSpan, TokenStream, TokenTree}, |
| 7 | + AssocItem, AssocItemKind, AttrArgs, AttrStyle, Attribute, DelimArgs, Item, ItemKind, Path, |
| 8 | + Stmt, StmtKind, |
| 9 | +}; |
| 10 | +use rustc_errors::PResult; |
| 11 | +use rustc_expand::base::{DummyResult, ExtCtxt, MacResult}; |
| 12 | +use rustc_parse::parser::{ForceCollect, Parser}; |
| 13 | +use rustc_span::{ |
| 14 | + symbol::{kw, sym, Ident}, |
| 15 | + Span, |
| 16 | +}; |
| 17 | +use smallvec::SmallVec; |
| 18 | + |
| 19 | +type ItemOffset = (usize, bool, TokenStream); |
| 20 | + |
| 21 | +// ```rust |
| 22 | +// cfg_match! { |
| 23 | +// cfg(unix) => { fn foo() -> i32 { 1 } }, |
| 24 | +// _ => { fn foo() -> i32 { 2 } }, |
| 25 | +// } |
| 26 | +// ``` |
| 27 | +// |
| 28 | +// The above `cfg_match!` is expanded to the following elements: |
| 29 | +// |
| 30 | +// ```rust |
| 31 | +// #[cfg(all(unix, not(any())))] |
| 32 | +// fn foo() -> i32 { 1 } |
| 33 | +// |
| 34 | +// #[cfg(all(not(any(unix))))] |
| 35 | +// fn foo() -> i32 { 2 } |
| 36 | +// ``` |
| 37 | +pub fn expand_cfg_match( |
| 38 | + cx: &mut ExtCtxt<'_>, |
| 39 | + sp: Span, |
| 40 | + tts: TokenStream, |
| 41 | +) -> Box<dyn MacResult + 'static> { |
| 42 | + let rslt = || { |
| 43 | + let (does_not_have_wildcard, mut items, mut items_offsets) = parse(cx, tts)?; |
| 44 | + iter( |
| 45 | + cx, |
| 46 | + &mut items, |
| 47 | + &mut items_offsets, |
| 48 | + sp, |
| 49 | + |local_cx, no, yes, items| { |
| 50 | + let attr = mk_cfg(local_cx, no, sp, Some(yes)); |
| 51 | + items.iter_mut().for_each(|item| item.attrs.push(attr.clone())); |
| 52 | + }, |
| 53 | + |local_cx, no, yes, items| { |
| 54 | + let attr = mk_cfg(local_cx, no, sp, does_not_have_wildcard.then_some(yes)); |
| 55 | + items.iter_mut().for_each(|item| item.attrs.push(attr.clone())); |
| 56 | + }, |
| 57 | + )?; |
| 58 | + PResult::Ok(items) |
| 59 | + }; |
| 60 | + match rslt() { |
| 61 | + Err(mut err) => { |
| 62 | + err.emit(); |
| 63 | + return DummyResult::any(sp); |
| 64 | + } |
| 65 | + Ok(items) => Box::new(CfgMatchOutput(items)), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn iter<'session>( |
| 70 | + cx: &mut ExtCtxt<'session>, |
| 71 | + items: &mut [P<Item>], |
| 72 | + items_offsets: &[ItemOffset], |
| 73 | + sp: Span, |
| 74 | + mut cb: impl FnMut(&mut ExtCtxt<'session>, &[ItemOffset], &TokenStream, &mut [P<Item>]), |
| 75 | + mut cb_last: impl FnMut(&mut ExtCtxt<'session>, &[ItemOffset], &TokenStream, &mut [P<Item>]), |
| 76 | +) -> PResult<'session, ()> { |
| 77 | + match items_offsets { |
| 78 | + [] => {} |
| 79 | + [first] => { |
| 80 | + if first.1 { |
| 81 | + return Err(cx.sess.create_err(errors::CfgMatchMeaninglessArms { span: sp })); |
| 82 | + } |
| 83 | + cb_last(cx, &[], &first.2, items.get_mut(..first.0).unwrap_or_default()); |
| 84 | + } |
| 85 | + [first, rest @ .., last] => { |
| 86 | + let mut no_idx = 1; |
| 87 | + let mut offset = first.0; |
| 88 | + let mut prev_offset = 0; |
| 89 | + cb(cx, &[], &first.2, items.get_mut(prev_offset..offset).unwrap_or_default()); |
| 90 | + for elem in rest { |
| 91 | + prev_offset = offset; |
| 92 | + offset = elem.0; |
| 93 | + cb( |
| 94 | + cx, |
| 95 | + items_offsets.get(..no_idx).unwrap_or_default(), |
| 96 | + &elem.2, |
| 97 | + items.get_mut(prev_offset..offset).unwrap_or_default(), |
| 98 | + ); |
| 99 | + no_idx = no_idx.wrapping_add(1); |
| 100 | + } |
| 101 | + prev_offset = offset; |
| 102 | + offset = last.0; |
| 103 | + cb_last( |
| 104 | + cx, |
| 105 | + items_offsets.get(..no_idx).unwrap_or_default(), |
| 106 | + &last.2, |
| 107 | + items.get_mut(prev_offset..offset).unwrap_or_default(), |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + Ok(()) |
| 112 | +} |
| 113 | + |
| 114 | +// #[cfg(all(** YES **, not(any(** NO **, ** NO **, ..))))] |
| 115 | +fn mk_cfg( |
| 116 | + cx: &mut ExtCtxt<'_>, |
| 117 | + no: &[ItemOffset], |
| 118 | + sp: Span, |
| 119 | + yes: Option<&TokenStream>, |
| 120 | +) -> Attribute { |
| 121 | + let mut any_tokens = TokenStream::new(Vec::with_capacity(4)); |
| 122 | + if let [first, ref rest @ ..] = no { |
| 123 | + any_tokens.push_stream(first.2.clone()); |
| 124 | + for elem in rest.iter() { |
| 125 | + any_tokens.push_tree(TokenTree::token_alone(token::Comma, sp)); |
| 126 | + any_tokens.push_stream(elem.2.clone()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + let mut not_tokens = TokenStream::new(Vec::with_capacity(2)); |
| 131 | + not_tokens.push_tree(TokenTree::token_alone(token::Ident(sym::any, false), sp)); |
| 132 | + not_tokens.push_tree(TokenTree::Delimited( |
| 133 | + DelimSpan::from_single(sp), |
| 134 | + token::Delimiter::Parenthesis, |
| 135 | + any_tokens, |
| 136 | + )); |
| 137 | + |
| 138 | + let mut all_tokens = TokenStream::new(Vec::with_capacity(4)); |
| 139 | + if let Some(elem) = yes { |
| 140 | + all_tokens.push_stream(elem.clone()); |
| 141 | + all_tokens.push_tree(TokenTree::token_alone(token::Comma, sp)); |
| 142 | + } |
| 143 | + all_tokens.push_tree(TokenTree::token_alone(token::Ident(sym::not, false), sp)); |
| 144 | + all_tokens.push_tree(TokenTree::Delimited( |
| 145 | + DelimSpan::from_single(sp), |
| 146 | + token::Delimiter::Parenthesis, |
| 147 | + not_tokens, |
| 148 | + )); |
| 149 | + |
| 150 | + let mut tokens = TokenStream::new(Vec::with_capacity(2)); |
| 151 | + tokens.push_tree(TokenTree::token_alone(token::Ident(sym::all, false), sp)); |
| 152 | + tokens.push_tree(TokenTree::Delimited( |
| 153 | + DelimSpan::from_single(sp), |
| 154 | + token::Delimiter::Parenthesis, |
| 155 | + all_tokens, |
| 156 | + )); |
| 157 | + |
| 158 | + mk_attr( |
| 159 | + &cx.sess.parse_sess.attr_id_generator, |
| 160 | + AttrStyle::Outer, |
| 161 | + Path::from_ident(Ident::new(sym::cfg, sp)), |
| 162 | + AttrArgs::Delimited(DelimArgs { |
| 163 | + dspan: DelimSpan::from_single(sp), |
| 164 | + delim: token::Delimiter::Parenthesis, |
| 165 | + tokens, |
| 166 | + }), |
| 167 | + sp, |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +fn parse<'session>( |
| 172 | + cx: &mut ExtCtxt<'session>, |
| 173 | + tts: TokenStream, |
| 174 | +) -> PResult<'session, (bool, Vec<P<Item>>, Vec<ItemOffset>)> { |
| 175 | + let mut parser = cx.new_parser_from_tts(tts); |
| 176 | + if parser.token == token::Eof { |
| 177 | + return parser.unexpected(); |
| 178 | + } |
| 179 | + let mut does_not_have_wildcard = true; |
| 180 | + let mut items = Vec::with_capacity(4); |
| 181 | + let mut items_offsets = Vec::with_capacity(4); |
| 182 | + loop { |
| 183 | + match parse_cfg_arm(&mut items, &mut parser)? { |
| 184 | + None => break, |
| 185 | + Some((has_single_elem, ts)) => { |
| 186 | + items_offsets.push((items.len(), has_single_elem, ts)); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + if parser.token != token::Eof && !items.is_empty() { |
| 191 | + let has_single_elem = parse_wildcard_arm(&mut items, &mut parser)?; |
| 192 | + does_not_have_wildcard = false; |
| 193 | + items_offsets.push((items.len(), has_single_elem, TokenStream::new(vec![]))); |
| 194 | + } |
| 195 | + if parser.token != token::Eof { |
| 196 | + return parser.unexpected(); |
| 197 | + } |
| 198 | + Ok((does_not_have_wildcard, items, items_offsets)) |
| 199 | +} |
| 200 | + |
| 201 | +fn parse_arbitrary_arm_block<'session>( |
| 202 | + items: &mut Vec<P<Item>>, |
| 203 | + mandatory_comma: bool, |
| 204 | + parser: &mut Parser<'session>, |
| 205 | +) -> PResult<'session, bool> { |
| 206 | + if parser.eat(&token::OpenDelim(token::Delimiter::Brace)) { |
| 207 | + loop { |
| 208 | + let item = match parser.parse_item(ForceCollect::No) { |
| 209 | + Ok(Some(elem)) => elem, |
| 210 | + _ => break, |
| 211 | + }; |
| 212 | + items.push(item); |
| 213 | + } |
| 214 | + parser.expect(&token::CloseDelim(token::Delimiter::Brace))?; |
| 215 | + Ok(false) |
| 216 | + } else { |
| 217 | + let Ok(Some(item)) = parser.parse_item(ForceCollect::No) else { |
| 218 | + return parser.unexpected(); |
| 219 | + }; |
| 220 | + if !matches!(item.kind, ItemKind::Fn(_)) { |
| 221 | + return Err(parser |
| 222 | + .sess |
| 223 | + .create_err(errors::CfgMatchBadSingleArm { span: parser.token.span })); |
| 224 | + } |
| 225 | + let has_comma = parser.eat(&token::Comma); |
| 226 | + if mandatory_comma && !has_comma { |
| 227 | + return Err(parser |
| 228 | + .sess |
| 229 | + .create_err(errors::CfgMatchMissingComma { span: parser.token.span })); |
| 230 | + } |
| 231 | + items.push(item); |
| 232 | + Ok(true) |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +fn parse_cfg_arm<'session>( |
| 237 | + items: &mut Vec<P<Item>>, |
| 238 | + parser: &mut Parser<'session>, |
| 239 | +) -> PResult<'session, Option<(bool, TokenStream)>> { |
| 240 | + if !parser.eat_keyword(sym::cfg) { |
| 241 | + return Ok(None); |
| 242 | + } |
| 243 | + let TokenTree::Delimited(_, delim, tokens) = parser.parse_token_tree() else { |
| 244 | + return parser.unexpected(); |
| 245 | + }; |
| 246 | + if delim != token::Delimiter::Parenthesis || !parser.eat(&token::FatArrow) { |
| 247 | + return Err(parser.sess.create_err(errors::CfgMatchBadArm { span: parser.token.span })); |
| 248 | + } |
| 249 | + let has_single_elem = parse_arbitrary_arm_block(items, true, parser)?; |
| 250 | + Ok(Some((has_single_elem, tokens))) |
| 251 | +} |
| 252 | + |
| 253 | +fn parse_wildcard_arm<'session>( |
| 254 | + items: &mut Vec<P<Item>>, |
| 255 | + parser: &mut Parser<'session>, |
| 256 | +) -> PResult<'session, bool> { |
| 257 | + if !parser.eat_keyword(kw::Underscore) || !parser.eat(&token::FatArrow) { |
| 258 | + return Err(parser |
| 259 | + .sess |
| 260 | + .create_err(errors::CfgMatchBadWildcard { span: parser.token.span })); |
| 261 | + } |
| 262 | + parse_arbitrary_arm_block(items, false, parser) |
| 263 | +} |
| 264 | + |
| 265 | +struct CfgMatchOutput(Vec<P<Item>>); |
| 266 | + |
| 267 | +impl MacResult for CfgMatchOutput { |
| 268 | + fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<AssocItem>; 1]>> { |
| 269 | + let mut rslt = SmallVec::with_capacity(self.0.len()); |
| 270 | + rslt.extend(self.0.into_iter().filter_map(|item| { |
| 271 | + let Item { attrs, id, span, vis, ident, kind, tokens } = item.into_inner(); |
| 272 | + let ItemKind::Fn(fun) = kind else { |
| 273 | + return None; |
| 274 | + }; |
| 275 | + Some(P(Item { attrs, id, ident, kind: AssocItemKind::Fn(fun), span, tokens, vis })) |
| 276 | + })); |
| 277 | + Some(rslt) |
| 278 | + } |
| 279 | + |
| 280 | + fn make_items(self: Box<Self>) -> Option<SmallVec<[P<Item>; 1]>> { |
| 281 | + Some(<_>::from(self.0)) |
| 282 | + } |
| 283 | + |
| 284 | + fn make_stmts(self: Box<Self>) -> Option<SmallVec<[Stmt; 1]>> { |
| 285 | + let mut rslt = SmallVec::with_capacity(self.0.len()); |
| 286 | + rslt.extend(self.0.into_iter().map(|item| { |
| 287 | + let id = item.id; |
| 288 | + let span = item.span; |
| 289 | + Stmt { id, kind: StmtKind::Item(item), span } |
| 290 | + })); |
| 291 | + Some(rslt) |
| 292 | + } |
| 293 | +} |
0 commit comments