Provide an fmt::Debug impl for entity references.

Instead of deriving a Debug impl: Ebb(45), use the Display version for
Debug too: ebb45.

This is more readable, and no information is lost.
This commit is contained in:
Jakob Stoklund Olesen
2017-11-21 10:00:30 -08:00
parent 2d7b54373f
commit 2e0b931590
5 changed files with 25 additions and 13 deletions

View File

@@ -97,7 +97,7 @@ trait Forest {
} }
/// A reference to a B+-tree node. /// A reference to a B+-tree node.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
struct Node(u32); struct Node(u32);
entity_impl!(Node, "node"); entity_impl!(Node, "node");

View File

@@ -87,5 +87,11 @@ macro_rules! entity_impl {
write!(f, "{}{}", $display_prefix, self.0) write!(f, "{}{}", $display_prefix, self.0)
} }
} }
impl ::std::fmt::Debug for $entity {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
(self as &::std::fmt::Display).fmt(f)
}
}
} }
} }

View File

@@ -23,7 +23,7 @@ use std::fmt;
use std::u32; use std::u32;
/// An opaque reference to an extended basic block in a function. /// An opaque reference to an extended basic block in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Ebb(u32); pub struct Ebb(u32);
entity_impl!(Ebb, "ebb"); entity_impl!(Ebb, "ebb");
@@ -37,7 +37,7 @@ impl Ebb {
} }
/// An opaque reference to an SSA value. /// An opaque reference to an SSA value.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Value(u32); pub struct Value(u32);
entity_impl!(Value, "v"); entity_impl!(Value, "v");
@@ -56,17 +56,17 @@ impl Value {
} }
/// An opaque reference to an instruction in a function. /// An opaque reference to an instruction in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Inst(u32); pub struct Inst(u32);
entity_impl!(Inst, "inst"); entity_impl!(Inst, "inst");
/// An opaque reference to a stack slot. /// An opaque reference to a stack slot.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct StackSlot(u32); pub struct StackSlot(u32);
entity_impl!(StackSlot, "ss"); entity_impl!(StackSlot, "ss");
/// An opaque reference to a global variable. /// An opaque reference to a global variable.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct GlobalVar(u32); pub struct GlobalVar(u32);
entity_impl!(GlobalVar, "gv"); entity_impl!(GlobalVar, "gv");
@@ -84,27 +84,27 @@ impl GlobalVar {
} }
/// An opaque reference to a jump table. /// An opaque reference to a jump table.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct JumpTable(u32); pub struct JumpTable(u32);
entity_impl!(JumpTable, "jt"); entity_impl!(JumpTable, "jt");
/// A reference to an external function. /// A reference to an external function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FuncRef(u32); pub struct FuncRef(u32);
entity_impl!(FuncRef, "fn"); entity_impl!(FuncRef, "fn");
/// A reference to a function signature. /// A reference to a function signature.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct SigRef(u32); pub struct SigRef(u32);
entity_impl!(SigRef, "sig"); entity_impl!(SigRef, "sig");
/// A reference to a heap. /// A reference to a heap.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Heap(u32); pub struct Heap(u32);
entity_impl!(Heap, "heap"); entity_impl!(Heap, "heap");
/// A reference to any of the entities defined in this module. /// A reference to any of the entities defined in this module.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum AnyEntity { pub enum AnyEntity {
/// The whole function. /// The whole function.
Function, Function,
@@ -145,6 +145,12 @@ impl fmt::Display for AnyEntity {
} }
} }
impl fmt::Debug for AnyEntity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Display).fmt(f)
}
}
impl From<Ebb> for AnyEntity { impl From<Ebb> for AnyEntity {
fn from(r: Ebb) -> AnyEntity { fn from(r: Ebb) -> AnyEntity {
AnyEntity::Ebb(r) AnyEntity::Ebb(r)

View File

@@ -9,7 +9,7 @@ use ir::{Function, Ebb, Layout};
use packed_option::PackedOption; use packed_option::PackedOption;
/// A opaque reference to a code loop. /// A opaque reference to a code loop.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Loop(u32); pub struct Loop(u32);
entity_impl!(Loop, "loop"); entity_impl!(Loop, "loop");

View File

@@ -18,7 +18,7 @@ use packed_option::PackedOption;
use ref_slice::ref_slice; use ref_slice::ref_slice;
/// A virtual register reference. /// A virtual register reference.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct VirtReg(u32); pub struct VirtReg(u32);
entity_impl!(VirtReg, "vreg"); entity_impl!(VirtReg, "vreg");