spade_common/
id_tracker.rs

1use serde::{Deserialize, Serialize};
2
3use crate::location_info::WithLocation;
4
5macro_rules! def_id_tracker {
6    ($name:ident) => {
7        #[derive(Debug, Serialize, Deserialize)]
8        pub struct $name {
9            id: u64,
10        }
11
12        impl $name {
13            pub fn new() -> Self {
14                Self { id: 0 }
15            }
16
17            pub fn new_at(id: u64) -> Self {
18                Self { id }
19            }
20
21            pub fn next(&mut self) -> u64 {
22                let result = self.id;
23                self.id += 1;
24                result
25            }
26
27            pub fn peek(&self) -> u64 {
28                self.id
29            }
30
31            /// 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
34            pub fn make_clone(&self) -> Self {
35                Self { id: self.id }
36            }
37        }
38        impl Default for $name {
39            fn default() -> Self {
40                Self::new()
41            }
42        }
43    };
44}
45
46macro_rules! def_typed_id_tracker {
47    ($name:ident, $type_name:ident) => {
48        #[derive(Debug, Serialize, Deserialize)]
49        pub struct $name {
50            id: u64,
51        }
52
53        impl $name {
54            pub fn new() -> Self {
55                Self { id: 0 }
56            }
57
58            pub fn new_at(id: u64) -> Self {
59                Self { id }
60            }
61
62            pub fn next(&mut self) -> $type_name {
63                let result = self.id;
64                self.id += 1;
65                $type_name(result)
66            }
67
68            pub fn peek(&self) -> u64 {
69                self.id
70            }
71
72            /// 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
75            pub fn make_clone(&self) -> Self {
76                Self { id: self.id }
77            }
78        }
79        impl Default for $name {
80            fn default() -> Self {
81                Self::new()
82            }
83        }
84    };
85}
86
87pub struct NameIdInner(pub u64);
88
89/// 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 {}
94
95#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize, Debug)]
96pub struct ImplID(pub u64);
97impl WithLocation for ImplID {}
98
99def_typed_id_tracker!(ExprIdTracker, ExprID);
100def_typed_id_tracker!(ImplIdTracker, ImplID);
101def_id_tracker!(NameIdTracker);
102def_id_tracker!(AAVarTracker);