Move EntityRef and entity_impl! into a new module.

The EntityRef trait is used by more than just the EntityMap now, so it
should live in its own module.

Also move the entity_impl! macro into the new module so it can be used
for defining new entity references anywhere.
This commit is contained in:
Jakob Stoklund Olesen
2017-06-20 10:07:23 -07:00
parent 0c7316ae28
commit b6cff6a98a
14 changed files with 85 additions and 100 deletions

View File

@@ -19,46 +19,9 @@
//! The entity references all implement the `Display` trait in a way that matches the textual IL
//! format.
use entity_map::EntityRef;
use packed_option::ReservedValue;
use std::fmt::{self, Display, Formatter};
use std::fmt;
use std::u32;
// Implement the common traits for a 32-bit entity reference.
macro_rules! entity_impl {
// Basic traits.
($entity:ident) => {
impl EntityRef for $entity {
fn new(index: usize) -> Self {
assert!(index < (u32::MAX as usize));
$entity(index as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
impl ReservedValue for $entity {
fn reserved_value() -> $entity {
$entity(u32::MAX)
}
}
};
// Include basic `Display` impl using the given display prefix.
// Display an `Ebb` reference as "ebb12".
($entity:ident, $display_prefix:expr) => {
entity_impl!($entity);
impl Display for $entity {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}{}", $display_prefix, self.0)
}
}
}
}
/// An opaque reference to an extended basic block in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub struct Ebb(u32);
@@ -138,17 +101,17 @@ pub enum AnyEntity {
SigRef(SigRef),
}
impl Display for AnyEntity {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
impl fmt::Display for AnyEntity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AnyEntity::Function => write!(fmt, "function"),
AnyEntity::Ebb(r) => r.fmt(fmt),
AnyEntity::Inst(r) => r.fmt(fmt),
AnyEntity::Value(r) => r.fmt(fmt),
AnyEntity::StackSlot(r) => r.fmt(fmt),
AnyEntity::JumpTable(r) => r.fmt(fmt),
AnyEntity::FuncRef(r) => r.fmt(fmt),
AnyEntity::SigRef(r) => r.fmt(fmt),
AnyEntity::Function => write!(f, "function"),
AnyEntity::Ebb(r) => r.fmt(f),
AnyEntity::Inst(r) => r.fmt(f),
AnyEntity::Value(r) => r.fmt(f),
AnyEntity::StackSlot(r) => r.fmt(f),
AnyEntity::JumpTable(r) => r.fmt(f),
AnyEntity::FuncRef(r) => r.fmt(f),
AnyEntity::SigRef(r) => r.fmt(f),
}
}
}

View File

@@ -122,7 +122,7 @@ impl Display for JumpTableData {
mod tests {
use super::JumpTableData;
use ir::Ebb;
use entity_map::EntityRef;
use entity_ref::EntityRef;
#[test]
fn empty() {

View File

@@ -982,7 +982,7 @@ impl<'f> Cursor<'f> {
#[cfg(test)]
mod tests {
use super::{Layout, Cursor, CursorPosition};
use entity_map::EntityRef;
use entity_ref::EntityRef;
use ir::{Ebb, Inst, ProgramOrder};
use std::cmp::Ordering;

View File

@@ -1,6 +1,6 @@
//! Program points.
use entity_map::EntityRef;
use entity_ref::EntityRef;
use ir::{Ebb, Inst, ValueDef};
use std::fmt;
use std::u32;
@@ -122,7 +122,7 @@ pub trait ProgramOrder {
#[cfg(test)]
mod tests {
use super::*;
use entity_map::EntityRef;
use entity_ref::EntityRef;
use ir::{Inst, Ebb};
#[test]