|
1 | 1 | use rustc_hir::def_id::{DefId, LOCAL_CRATE}; |
2 | | -use rustc_middle::mir::{Body, interpret}; |
3 | | -use rustc_middle::ty::{self, TyCtxt, DynKind}; |
| 2 | +use rustc_hir::def::CtorKind; |
| 3 | +use rustc_hir::Mutability; |
| 4 | +use rustc_middle::mir::{AssertMessage, BasicBlock, BinOp, Body, CastKind, interpret, NullOp, UnOp}; |
| 5 | +use rustc_middle::ty::{self, DynKind, FloatTy, IntTy, TyCtxt, UintTy}; |
4 | 6 | use rustc_session::Session; |
5 | 7 | use rustc_span::Span; |
6 | 8 | use rustc_span::symbol::Symbol; |
| 9 | +use rustc_target::spec::abi::Abi; |
7 | 10 | use serde_json; |
8 | 11 | use std::collections::BTreeMap; |
9 | 12 | use std::collections::{HashMap, HashSet, hash_map}; |
| 13 | +use std::fmt::Write; |
10 | 14 | use std::hash::{Hash, Hasher}; |
11 | 15 | use std::ops::Deref; |
12 | 16 | use std::mem; |
13 | 17 |
|
| 18 | + |
14 | 19 | pub struct CompileState<'a, 'tcx> { |
15 | 20 | pub session: &'a Session, |
16 | 21 | pub tcx: TyCtxt<'tcx>, |
@@ -396,50 +401,172 @@ impl<'tcx, K, V> ToJson<'tcx> for BTreeMap<K, V> |
396 | 401 | } |
397 | 402 | } |
398 | 403 |
|
399 | | -#[macro_export] |
400 | | -macro_rules! basic_json_impl { |
401 | | - ($n : path) => { |
402 | | - impl ToJson<'_> for $n { |
403 | | - fn to_json(&self, _ : &mut MirState) -> serde_json::Value { |
| 404 | +impl<'a> ToJson<'_> for AssertMessage<'a> { |
| 405 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
404 | 406 | let mut s = String::new(); |
405 | 407 | write!(&mut s, "{:?}", self).unwrap(); |
406 | 408 | json!(s) |
407 | 409 | } |
408 | 410 | } |
409 | | -}; |
410 | | - ($n : path, $lt : tt) => { |
411 | | - impl<$lt> ToJson<'_> for $n { |
412 | | - fn to_json(&self, _ : &mut MirState) -> serde_json::Value { |
| 411 | + |
| 412 | +impl ToJson<'_> for BasicBlock { |
| 413 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
413 | 414 | let mut s = String::new(); |
414 | 415 | write!(&mut s, "{:?}", self).unwrap(); |
415 | 416 | json!(s) |
416 | 417 | } |
417 | 418 | } |
418 | 419 |
|
419 | | -}; |
| 420 | +// enum handlers |
| 421 | + |
| 422 | +impl ToJson<'_> for Abi { |
| 423 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 424 | + match self { |
| 425 | + Abi::Rust => json!({ "kind": "Rust" }), |
| 426 | + Abi::PtxKernel => json!({ "kind": "PtxKernel" }), |
| 427 | + Abi::Msp430Interrupt => json!({ "kind": "Msp430Interrupt" }), |
| 428 | + Abi::X86Interrupt => json!({ "kind": "X86Interrupt" }), |
| 429 | + Abi::AmdGpuKernel => json!({ "kind": "AmdGpuKernel" }), |
| 430 | + Abi::EfiApi => json!({ "kind": "EfiApi" }), |
| 431 | + Abi::AvrInterrupt => json!({ "kind": "AvrInterrupt" }), |
| 432 | + Abi::AvrNonBlockingInterrupt => json!({ "kind": "AvrNonBlockingInterrupt" }), |
| 433 | + Abi::CCmseNonSecureCall => json!({ "kind": "CCmseNonSecureCall" }), |
| 434 | + Abi::Wasm => json!({ "kind": "Wasm" }), |
| 435 | + Abi::RustIntrinsic => json!({ "kind": "RustIntrinsic" }), |
| 436 | + Abi::RustCall => json!({ "kind": "RustCall" }), |
| 437 | + Abi::PlatformIntrinsic => json!({ "kind": "PlatformIntrinsic" }), |
| 438 | + Abi::Unadjusted => json!({ "kind": "Unadjusted" }), |
| 439 | + Abi::RustCold => json!({ "kind": "RustCold" }), |
| 440 | + |
| 441 | + // Data-carrying variants — use Debug formatting |
| 442 | + Abi::C { .. } |
| 443 | + | Abi::Cdecl { .. } |
| 444 | + | Abi::Stdcall { .. } |
| 445 | + | Abi::Fastcall { .. } |
| 446 | + | Abi::Vectorcall { .. } |
| 447 | + | Abi::Thiscall { .. } |
| 448 | + | Abi::Aapcs { .. } |
| 449 | + | Abi::Win64 { .. } |
| 450 | + | Abi::SysV64 { .. } |
| 451 | + | Abi::System { .. } => { |
| 452 | + json!({ "kind": format!("{:?}", self) }) |
| 453 | + } |
| 454 | + } |
| 455 | + } |
420 | 456 | } |
421 | 457 |
|
422 | | -#[macro_export] |
423 | | -macro_rules! basic_json_enum_impl { |
424 | | - ($n : path) => { |
425 | | - impl ToJson<'_> for $n { |
426 | | - fn to_json(&self, _ : &mut MirState) -> serde_json::Value { |
427 | | - let mut s = String::new(); |
428 | | - write!(&mut s, "{:?}", self).unwrap(); |
429 | | - json!({"kind": s}) |
| 458 | +impl ToJson<'_> for BinOp { |
| 459 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 460 | + match self { |
| 461 | + BinOp::Add => json!({ "kind": "Add" }), |
| 462 | + BinOp::Sub => json!({ "kind": "Sub" }), |
| 463 | + BinOp::Mul => json!({ "kind": "Mul" }), |
| 464 | + BinOp::Div => json!({ "kind": "Div" }), |
| 465 | + BinOp::Rem => json!({ "kind": "Rem" }), |
| 466 | + BinOp::BitXor => json!({ "kind": "BitXor" }), |
| 467 | + BinOp::BitAnd => json!({ "kind": "BitAnd" }), |
| 468 | + BinOp::BitOr => json!({ "kind": "BitOr" }), |
| 469 | + BinOp::Shl => json!({ "kind": "Shl" }), |
| 470 | + BinOp::Shr => json!({ "kind": "Shr" }), |
| 471 | + BinOp::Eq => json!({ "kind": "Eq" }), |
| 472 | + BinOp::Lt => json!({ "kind": "Lt" }), |
| 473 | + BinOp::Le => json!({ "kind": "Le" }), |
| 474 | + BinOp::Ne => json!({ "kind": "Ne" }), |
| 475 | + BinOp::Ge => json!({ "kind": "Ge" }), |
| 476 | + BinOp::Gt => json!({ "kind": "Gt" }), |
| 477 | + BinOp::Offset => json!({ "kind": "Offset" }), |
| 478 | + } |
430 | 479 | } |
431 | 480 | } |
432 | | -}; |
433 | | - ($n : path, $lt : tt) => { |
434 | | - impl<$lt> ToJson<'_> for $n { |
435 | | - fn to_json(&self, _ : &mut MirState) -> serde_json::Value { |
436 | | - let mut s = String::new(); |
437 | | - write!(&mut s, "{:?}", self).unwrap(); |
438 | | - json!({"kind": s}) |
| 481 | + |
| 482 | +impl ToJson<'_> for CastKind { |
| 483 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 484 | + match self { |
| 485 | + CastKind::PointerExposeAddress => json!({ "kind": "PointerExposeAddress" }), |
| 486 | + CastKind::PointerFromExposedAddress => json!({ "kind": "PointerFromExposedAddress" }), |
| 487 | + CastKind::DynStar => json!({ "kind": "DynStar" }), |
| 488 | + CastKind::IntToInt => json!({ "kind": "IntToInt" }), |
| 489 | + CastKind::FloatToInt => json!({ "kind": "FloatToInt" }), |
| 490 | + CastKind::FloatToFloat => json!({ "kind": "FloatToFloat" }), |
| 491 | + CastKind::IntToFloat => json!({ "kind": "IntToFloat" }), |
| 492 | + CastKind::PtrToPtr => json!({ "kind": "PtrToPtr" }), |
| 493 | + CastKind::FnPtrToPtr => json!({ "kind": "FnPtrToPtr" }), |
| 494 | + CastKind::Pointer(_) => json!({ "kind": format!("{:?}", self) }), |
| 495 | + } |
| 496 | + } |
| 497 | +} |
| 498 | + |
| 499 | +impl ToJson<'_> for CtorKind { |
| 500 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 501 | + match self { |
| 502 | + CtorKind::Fn => json!({ "kind": "Fn" }), |
| 503 | + CtorKind::Const => json!({ "kind": "Const" }), |
| 504 | + } |
| 505 | + } |
| 506 | +} |
| 507 | + |
| 508 | +impl ToJson<'_> for FloatTy { |
| 509 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 510 | + match self { |
| 511 | + FloatTy::F32 => json!({ "kind": "F32" }), |
| 512 | + FloatTy::F64 => json!({ "kind": "F64" }), |
| 513 | + } |
| 514 | + } |
| 515 | +} |
| 516 | + |
| 517 | +impl ToJson<'_> for IntTy { |
| 518 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 519 | + match self { |
| 520 | + IntTy::Isize => json!({ "kind": "Isize" }), |
| 521 | + IntTy::I8 => json!({ "kind": "I8" }), |
| 522 | + IntTy::I16 => json!({ "kind": "I16" }), |
| 523 | + IntTy::I32 => json!({ "kind": "I32" }), |
| 524 | + IntTy::I64 => json!({ "kind": "I64" }), |
| 525 | + IntTy::I128 => json!({ "kind": "I128" }), |
| 526 | + } |
439 | 527 | } |
440 | 528 | } |
441 | 529 |
|
442 | | -}; |
| 530 | +impl ToJson<'_> for Mutability { |
| 531 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 532 | + match self { |
| 533 | + Mutability::Not => json!({ "kind": "Not" }), |
| 534 | + Mutability::Mut => json!({ "kind": "Mut" }), |
| 535 | + } |
| 536 | + } |
| 537 | +} |
| 538 | + |
| 539 | +impl ToJson<'_> for NullOp { |
| 540 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 541 | + match self { |
| 542 | + NullOp::SizeOf => json!({ "kind": "SizeOf" }), |
| 543 | + NullOp::AlignOf => json!({ "kind": "AlignOf" }), |
| 544 | + } |
| 545 | + } |
| 546 | +} |
| 547 | + |
| 548 | +impl ToJson<'_> for UintTy { |
| 549 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 550 | + match self { |
| 551 | + UintTy::Usize => json!({ "kind": "Usize" }), |
| 552 | + UintTy::U8 => json!({ "kind": "U8" }), |
| 553 | + UintTy::U16 => json!({ "kind": "U16" }), |
| 554 | + UintTy::U32 => json!({ "kind": "U32" }), |
| 555 | + UintTy::U64 => json!({ "kind": "U64" }), |
| 556 | + UintTy::U128 => json!({ "kind": "U128" }), |
| 557 | + } |
| 558 | + } |
| 559 | +} |
| 560 | + |
| 561 | +// end enum handlers |
| 562 | + |
| 563 | +impl ToJson<'_> for UnOp { |
| 564 | + fn to_json(&self, _: &mut MirState) -> serde_json::Value { |
| 565 | + match self { |
| 566 | + UnOp::Not => json!({ "kind": "Not" }), |
| 567 | + UnOp::Neg => json!({ "kind": "Neg" }), |
| 568 | + } |
| 569 | + } |
443 | 570 | } |
444 | 571 |
|
445 | 572 | impl<'tcx, A, B> ToJson<'tcx> for (A, B) |
|
0 commit comments