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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::sync::Arc;
use std::{fmt, hash, ops};

/// Builds a new ID type based off indexing in to a `Vec` lookup table
///
/// This stores the value internally at $ty (typically `u32`) while the interface uses `usize` to
/// support easy indexing.
#[macro_export]
macro_rules! new_indexing_id_type {
    ($name:ident, $ty:ty) => {
        #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd)]
        pub struct $name($ty);

        impl $name {
            pub fn new(value: usize) -> $name {
                $name(value as $ty)
            }

            #[allow(unused)]
            pub fn new_entry_id<T>(lookup_vec: &mut Vec<T>, entry: T) -> $name {
                let id = Self::new(lookup_vec.len());
                lookup_vec.push(entry);
                id
            }

            #[allow(unused)]
            pub fn to_usize(self) -> usize {
                self.0 as usize
            }
        }
    };
}

/// Builds a new ID type using a global counter
///
/// This allows allocating IDs without threading a mutable counter through multiple layers of
/// code.
#[macro_export]
macro_rules! new_global_id_type {
    ($id_name:ident) => {
        new_global_id_type!(
            $id_name,
            usize,
            std::sync::atomic::AtomicUsize,
            std::num::NonZeroUsize
        );
    };
    ($id_name:ident, $native_type:ty, $atomic_type:ty, $non_zero_type:ty) => {
        // These counters are very hot and shared between threads
        // They're not strongly correlated with each other so put them on different cachelines to
        // avoid bouncing them between CPUs. The value of 64 is just a guess; it's a typical value
        // and isn't needed for correctness.
        #[repr(align(64))]
        struct AlignedAtomic($atomic_type);

        static NEXT_VALUE: AlignedAtomic = AlignedAtomic(<$atomic_type>::new(1));

        #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord)]
        pub struct $id_name($non_zero_type);

        impl $id_name {
            /// Allocates a ID unique for the duration of compiler's execution
            pub fn alloc() -> Self {
                use std::sync::atomic::Ordering;

                // We used relaxed ordering because the order doesn't actually matter; these are
                // used only for uniqueness
                let raw_id = NEXT_VALUE.0.fetch_add(1, Ordering::Relaxed);
                Self::new(raw_id)
            }

            #[allow(unused)]
            pub fn get(&self) -> $native_type {
                self.0.into()
            }

            fn new(raw_id: $native_type) -> Self {
                $id_name(<$non_zero_type>::new(raw_id).unwrap())
            }
        }
    };
}

/// Builds a new ID type based off an arbitrary counter
#[macro_export]
macro_rules! new_counting_id_type {
    ($counter_name:ident, $id_name:ident) => {
        #[derive(Clone)]
        pub struct $counter_name(u32);

        impl $counter_name {
            pub fn new() -> $counter_name {
                $counter_name(1)
            }

            pub fn alloc(&mut self) -> $id_name {
                let id = $id_name(self.0);
                self.0 += 1;
                id
            }
        }

        #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd)]
        pub struct $id_name(u32);

        impl $id_name {
            #[allow(unused)]
            pub fn new(value: u32) -> $id_name {
                $id_name(value)
            }

            #[allow(unused)]
            pub fn to_u32(self) -> u32 {
                self.0
            }
        }
    };
}

/// Reference-counted pointer that uses pointer identity
///
/// Traits such as `Hash`, `Eq`, `Ord` etc. are implemented in terms of the value's memory location.
/// This means that the value returned by `ArcId::new()` is considered equal to itself and its
/// clones regardless of the value it points to.
pub struct ArcId<T> {
    inner: Arc<T>,
}

impl<T> ArcId<T> {
    pub fn new(value: T) -> Self {
        ArcId {
            inner: Arc::new(value),
        }
    }
}

impl<T> Clone for ArcId<T> {
    fn clone(&self) -> Self {
        ArcId {
            inner: self.inner.clone(),
        }
    }
}

impl<T> ops::Deref for ArcId<T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.inner.deref()
    }
}

impl<T> PartialEq for ArcId<T> {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl<T> Eq for ArcId<T> {}

impl<T> hash::Hash for ArcId<T> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        state.write_usize(self.inner.as_ref() as *const T as usize)
    }
}

impl<T> PartialOrd for ArcId<T> {
    fn partial_cmp(&self, other: &ArcId<T>) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T> Ord for ArcId<T> {
    fn cmp(&self, other: &ArcId<T>) -> std::cmp::Ordering {
        (self.inner.as_ref() as *const T as usize).cmp(&(other.inner.as_ref() as *const T as usize))
    }
}

impl<T: fmt::Debug> fmt::Debug for ArcId<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.inner.fmt(formatter)
    }
}