spade_common/id_tracker.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
use serde::{Deserialize, Serialize};
use crate::location_info::WithLocation;
macro_rules! def_id_tracker {
($name:ident) => {
#[derive(Debug, Serialize, Deserialize)]
pub struct $name {
id: u64,
}
impl $name {
pub fn new() -> Self {
Self { id: 0 }
}
pub fn new_at(id: u64) -> Self {
Self { id }
}
pub fn next(&mut self) -> u64 {
let result = self.id;
self.id += 1;
result
}
pub fn peek(&self) -> u64 {
self.id
}
/// Clone this ID tracker. After this is done, only one of the ID trackers may
/// be used otherwise duplicate IDs will be generated. It is up to the caller of this
/// method to make sure that no mutable references are handed out to one of the clonse
pub fn make_clone(&self) -> Self {
Self { id: self.id }
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
};
}
macro_rules! def_typed_id_tracker {
($name:ident, $type_name:ident) => {
#[derive(Debug, Serialize, Deserialize)]
pub struct $name {
id: u64,
}
impl $name {
pub fn new() -> Self {
Self { id: 0 }
}
pub fn new_at(id: u64) -> Self {
Self { id }
}
pub fn next(&mut self) -> $type_name {
let result = self.id;
self.id += 1;
$type_name(result)
}
pub fn peek(&self) -> u64 {
self.id
}
/// Clone this ID tracker. After this is done, only one of the ID trackers may
/// be used otherwise duplicate IDs will be generated. It is up to the caller of this
/// method to make sure that no mutable references are handed out to one of the clonse
pub fn make_clone(&self) -> Self {
Self { id: self.id }
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
};
}
pub struct NameIdInner(pub u64);
/// An ID of an expression-like thing. In practice something that has a type in the
/// type inferer.
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize, Debug)]
pub struct ExprID(pub u64);
impl WithLocation for ExprID {}
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize, Debug)]
pub struct ImplID(pub u64);
impl WithLocation for ImplID {}
def_typed_id_tracker!(ExprIdTracker, ExprID);
def_typed_id_tracker!(ImplIdTracker, ImplID);
def_id_tracker!(NameIdTracker);
def_id_tracker!(AAVarTracker);