Define a Variable struct so that frontends don't have to.

Frontends can still use their own types with `ILBuilder` and
`FunctionBuilder`. This just provides a basic `Variable` struct
for frontends that want it.
This commit is contained in:
Dan Gohman
2018-03-09 15:01:13 -08:00
parent 56c7d85727
commit 1c72ccfe0a
4 changed files with 61 additions and 75 deletions

View File

@@ -0,0 +1,24 @@
//! A basic `Variable` implementation.
//!
//! `ILBuilder`, `FunctionBuilder`, and related types have a `Variable`
//! type parameter, to allow frontends that identify variables with
//! their own index types to use them directly. Frontends which don't
//! can use the `Variable` defined here.
use cretonne::entity::EntityRef;
use std::u32;
///! An opaque reference to a variable.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Variable(u32);
impl EntityRef for Variable {
fn new(index: usize) -> Self {
assert!(index < (u32::MAX as usize));
Variable(index as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}