1use serde::{Deserialize, Serialize};
23use crate::location_info::WithLocation;
45macro_rules! def_id_tracker {
6 ($name:ident) => {
7#[derive(Debug, Serialize, Deserialize)]
8pub struct $name {
9 id: u64,
10 }
1112impl $name {
13pub fn new() -> Self {
14Self { id: 0 }
15 }
1617pub fn new_at(id: u64) -> Self {
18Self { id }
19 }
2021pub fn next(&mut self) -> u64 {
22let result = self.id;
23self.id += 1;
24 result
25 }
2627pub fn peek(&self) -> u64 {
28self.id
29 }
3031/// Clone this ID tracker. After this is done, only one of the ID trackers may
32 /// be used otherwise duplicate IDs will be generated. It is up to the caller of this
33 /// method to make sure that no mutable references are handed out to one of the clonse
34pub fn make_clone(&self) -> Self {
35Self { id: self.id }
36 }
37 }
38impl Default for $name {
39fn default() -> Self {
40Self::new()
41 }
42 }
43 };
44}
4546macro_rules! def_typed_id_tracker {
47 ($name:ident, $type_name:ident) => {
48#[derive(Debug, Serialize, Deserialize)]
49pub struct $name {
50 id: u64,
51 }
5253impl $name {
54pub fn new() -> Self {
55Self { id: 0 }
56 }
5758pub fn new_at(id: u64) -> Self {
59Self { id }
60 }
6162pub fn next(&mut self) -> $type_name {
63let result = self.id;
64self.id += 1;
65$type_name(result)
66 }
6768pub fn peek(&self) -> u64 {
69self.id
70 }
7172/// Clone this ID tracker. After this is done, only one of the ID trackers may
73 /// be used otherwise duplicate IDs will be generated. It is up to the caller of this
74 /// method to make sure that no mutable references are handed out to one of the clonse
75pub fn make_clone(&self) -> Self {
76Self { id: self.id }
77 }
78 }
79impl Default for $name {
80fn default() -> Self {
81Self::new()
82 }
83 }
84 };
85}
8687pub struct NameIdInner(pub u64);
8889/// An ID of an expression-like thing. In practice something that has a type in the
90/// type inferer.
91#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize, Debug)]
92pub struct ExprID(pub u64);
93impl WithLocation for ExprID {}
9495#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize, Debug)]
96pub struct ImplID(pub u64);
97impl WithLocation for ImplID {}
9899def_typed_id_tracker!(ExprIdTracker, ExprID);
100def_typed_id_tracker!(ImplIdTracker, ImplID);
101def_id_tracker!(NameIdTracker);
102def_id_tracker!(AAVarTracker);