Skip to content

Commit 92d2e94

Browse files
authored
Merge pull request #97 from sand7000/fix_macros
Remove basic_json_impl! and basic_json_enum_impl! macros
2 parents f336d18 + 2cfcd42 commit 92d2e94

File tree

3 files changed

+155
-47
lines changed

3 files changed

+155
-47
lines changed

src/analyz/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ use analyz::ty_json::*;
3434
use lib_util::{self, JsonOutput, EntryKind};
3535
use schema_ver::SCHEMA_VER;
3636

37-
basic_json_enum_impl!(mir::BinOp);
38-
39-
basic_json_enum_impl!(mir::NullOp);
40-
basic_json_enum_impl!(mir::UnOp);
41-
4237
impl<'tcx> ToJson<'tcx> for abi::VariantIdx {
4338
fn to_json(&self, _: &mut MirState) -> serde_json::Value {
4439
self.as_usize().into()
@@ -311,16 +306,12 @@ impl<'tcx> ToJson<'tcx> for mir::PlaceElem<'tcx> {
311306
}
312307
}
313308

314-
basic_json_impl!(mir::BasicBlock);
315-
316309
impl ToJson<'_> for mir::Field {
317310
fn to_json(&self, _mir: &mut MirState) -> serde_json::Value {
318311
json!(self.index())
319312
}
320313
}
321314

322-
basic_json_impl!(mir::AssertMessage<'a>, 'a);
323-
324315
impl<'tcx> ToJson<'tcx> for mir::Operand<'tcx> {
325316
fn to_json(&self, mir: &mut MirState<'_, 'tcx>) -> serde_json::Value {
326317
match self {

src/analyz/to_json.rs

Lines changed: 155 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
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};
46
use rustc_session::Session;
57
use rustc_span::Span;
68
use rustc_span::symbol::Symbol;
9+
use rustc_target::spec::abi::Abi;
710
use serde_json;
811
use std::collections::BTreeMap;
912
use std::collections::{HashMap, HashSet, hash_map};
13+
use std::fmt::Write;
1014
use std::hash::{Hash, Hasher};
1115
use std::ops::Deref;
1216
use std::mem;
1317

18+
1419
pub struct CompileState<'a, 'tcx> {
1520
pub session: &'a Session,
1621
pub tcx: TyCtxt<'tcx>,
@@ -396,50 +401,172 @@ impl<'tcx, K, V> ToJson<'tcx> for BTreeMap<K, V>
396401
}
397402
}
398403

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 {
404406
let mut s = String::new();
405407
write!(&mut s, "{:?}", self).unwrap();
406408
json!(s)
407409
}
408410
}
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 {
413414
let mut s = String::new();
414415
write!(&mut s, "{:?}", self).unwrap();
415416
json!(s)
416417
}
417418
}
418419

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+
}
420456
}
421457

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+
}
430479
}
431480
}
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+
}
439527
}
440528
}
441529

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+
}
443570
}
444571

445572
impl<'tcx, A, B> ToJson<'tcx> for (A, B)

src/analyz/ty_json.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ use rustc_middle::ty;
1111
use rustc_middle::ty::{AdtKind, DynKind, TyCtxt, TypeVisitable};
1212
use rustc_middle::ty::util::{IntTypeExt};
1313
use rustc_query_system::ich::StableHashingContext;
14-
use rustc_target::spec::abi;
1514
use rustc_target::abi::{Align, FieldsShape, HasDataLayout, Size};
1615
use rustc_span::DUMMY_SP;
1716
use serde_json;
18-
use std::fmt::Write as FmtWrite;
1917
use std::usize;
2018

2119
use analyz::to_json::*;
@@ -33,14 +31,6 @@ impl<'tcx, T> ToJson<'tcx> for ty::List<T>
3331
}
3432
}
3533

36-
basic_json_enum_impl!(ty::FloatTy);
37-
basic_json_enum_impl!(ty::IntTy);
38-
basic_json_enum_impl!(ty::UintTy);
39-
basic_json_enum_impl!(hir::Mutability);
40-
basic_json_enum_impl!(hir::def::CtorKind);
41-
basic_json_enum_impl!(mir::CastKind);
42-
basic_json_enum_impl!(abi::Abi);
43-
4434
impl ToJson<'_> for mir::BorrowKind {
4535
fn to_json(&self, _mir: &mut MirState) -> serde_json::Value {
4636
match self {

0 commit comments

Comments
 (0)