|
| 1 | +use std::path::PathBuf; |
| 2 | + |
1 | 3 | use rustc_ast::{LitIntType, LitKind, MetaItemLit}; |
| 4 | +use rustc_hir::attrs::{BorrowckGraphvizFormatKind, RustcMirKind}; |
2 | 5 | use rustc_session::errors; |
3 | 6 |
|
4 | 7 | use super::prelude::*; |
@@ -329,3 +332,92 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser { |
329 | 332 | const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]); |
330 | 333 | const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel; |
331 | 334 | } |
| 335 | + |
| 336 | +pub(crate) struct RustcMirParser; |
| 337 | + |
| 338 | +impl<S: Stage> CombineAttributeParser<S> for RustcMirParser { |
| 339 | + const PATH: &[rustc_span::Symbol] = &[sym::rustc_mir]; |
| 340 | + |
| 341 | + type Item = RustcMirKind; |
| 342 | + |
| 343 | + const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcMir(items); |
| 344 | + |
| 345 | + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ |
| 346 | + Allow(Target::Fn), |
| 347 | + Allow(Target::Method(MethodKind::Inherent)), |
| 348 | + Allow(Target::Method(MethodKind::TraitImpl)), |
| 349 | + Allow(Target::Method(MethodKind::Trait { body: false })), |
| 350 | + Allow(Target::Method(MethodKind::Trait { body: true })), |
| 351 | + ]); |
| 352 | + |
| 353 | + const TEMPLATE: AttributeTemplate = template!(List: &["arg1, arg2, ..."]); |
| 354 | + |
| 355 | + fn extend( |
| 356 | + cx: &mut AcceptContext<'_, '_, S>, |
| 357 | + args: &ArgParser, |
| 358 | + ) -> impl IntoIterator<Item = Self::Item> { |
| 359 | + let Some(list) = args.list() else { |
| 360 | + cx.expected_list(cx.attr_span, args); |
| 361 | + return ThinVec::new(); |
| 362 | + }; |
| 363 | + |
| 364 | + list.mixed() |
| 365 | + .filter_map(|arg| arg.meta_item()) |
| 366 | + .filter_map(|mi| { |
| 367 | + if let Some(ident) = mi.ident() { |
| 368 | + match ident.name { |
| 369 | + sym::rustc_peek_maybe_init => Some(RustcMirKind::PeekMaybeInit), |
| 370 | + sym::rustc_peek_maybe_uninit => Some(RustcMirKind::PeekMaybeUninit), |
| 371 | + sym::rustc_peek_liveness => Some(RustcMirKind::PeekLiveness), |
| 372 | + sym::stop_after_dataflow => Some(RustcMirKind::StopAfterDataflow), |
| 373 | + sym::borrowck_graphviz_postflow => { |
| 374 | + let Some(nv) = mi.args().name_value() else { |
| 375 | + cx.expected_name_value( |
| 376 | + mi.span(), |
| 377 | + Some(sym::borrowck_graphviz_postflow), |
| 378 | + ); |
| 379 | + return None; |
| 380 | + }; |
| 381 | + let Some(path) = nv.value_as_str() else { |
| 382 | + cx.expected_string_literal(nv.value_span, None); |
| 383 | + return None; |
| 384 | + }; |
| 385 | + let path = PathBuf::from(path.to_string()); |
| 386 | + if path.file_name().is_some() { |
| 387 | + Some(RustcMirKind::BorrowckGraphvizPostflow { path }) |
| 388 | + } else { |
| 389 | + cx.expected_filename_literal(nv.value_span); |
| 390 | + None |
| 391 | + } |
| 392 | + } |
| 393 | + sym::borrowck_graphviz_format => { |
| 394 | + let Some(nv) = mi.args().name_value() else { |
| 395 | + cx.expected_name_value( |
| 396 | + mi.span(), |
| 397 | + Some(sym::borrowck_graphviz_format), |
| 398 | + ); |
| 399 | + return None; |
| 400 | + }; |
| 401 | + let Some(format) = nv.value_as_ident() else { |
| 402 | + cx.expected_identifier(nv.value_span); |
| 403 | + return None; |
| 404 | + }; |
| 405 | + match format.name { |
| 406 | + sym::two_phase => Some(RustcMirKind::BorrowckGraphvizFormat { |
| 407 | + format: BorrowckGraphvizFormatKind::TwoPhase, |
| 408 | + }), |
| 409 | + _ => { |
| 410 | + cx.expected_specific_argument(format.span, &[sym::two_phase]); |
| 411 | + None |
| 412 | + } |
| 413 | + } |
| 414 | + } |
| 415 | + _ => None, |
| 416 | + } |
| 417 | + } else { |
| 418 | + None |
| 419 | + } |
| 420 | + }) |
| 421 | + .collect() |
| 422 | + } |
| 423 | +} |
0 commit comments