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
use std::collections::HashMap;

use syn::{visit::Visit as _, visit_mut::VisitMut as _, Generics, Type};

use crate::{alias, visitor, Alias};

#[derive(Debug)]
pub struct Map {
    group: alias::Group,
    primary: alias::Details,
    // Maps exact type to index
    lookup: HashMap<Type, usize>,
    // Maps index to de-Self'ed type
    list: Vec<alias::Details>,
}

impl Map {
    pub(crate) fn new(group: alias::Group, parameters: Generics, primary_type: Type) -> Self {
        Self {
            group,
            primary: alias::Details::new(parameters, primary_type),
            lookup: HashMap::new(),
            list: vec![],
        }
    }

    pub(crate) fn insert(&mut self, ty: &Type) -> Option<usize> {
        if !self.lookup.contains_key(ty) {
            let mut deselfed_ty = ty.clone();
            let mut visitor = visitor::ApplyPrimaryAlias::new(self);
            visitor.visit_type_mut(&mut deselfed_ty);

            let index = self.list.len();
            self.lookup.insert(ty.clone(), index);

            let mut visitor = visitor::UnusedParams::new();
            visitor.visit_type(&deselfed_ty);
            let mut parameters = self.primary.parameters.clone();
            visitor.remove_unused(&mut parameters);

            self.list.push(alias::Details::new(parameters, deselfed_ty));

            Some(index)
        } else {
            None
        }
    }

    pub(crate) fn group(&self) -> &alias::Group {
        &self.group
    }

    /// Look up the [Index](alias::Index) of a given type.  
    /// Returns [None] if there is no alias for the type.  
    /// Note that lookup is done based on exact token equality, not type equality.
    pub fn get_index(&self, ty: &Type) -> Option<alias::Index> {
        let mut deselfed_ty = ty.clone();
        let mut visitor = visitor::ApplyPrimaryAlias::new(self);
        visitor.visit_type_mut(&mut deselfed_ty);

        if self.primary.aliased_type == deselfed_ty {
            Some(alias::Index::Primary)
        } else if let Some(&index) = self.lookup.get(&deselfed_ty) {
            Some(alias::Index::Secondary(index))
        } else {
            None
        }
    }

    /// Look up the [Alias] of a given type.  
    /// Returns [None] if there is no alias for the type.  
    /// Note that lookup is done based on exact token equality, not type equality.  
    /// Equivalent to [Self::get_index] followed by [Self::alias].
    pub fn alias_of(&self, ty: &Type) -> Option<Alias> {
        let index = self.get_index(ty)?;
        Some(self.alias(index))
    }

    /// Get the [Alias] corresponding to an [alias::Index].
    pub fn alias(&self, index: alias::Index) -> Alias {
        let details = match index {
            alias::Index::Primary => &self.primary,
            alias::Index::Secondary(index) => &self.list[index],
        };
        Alias::new(self, index, details)
    }

    /// Iterate through all [alias::Index]es.
    pub fn iter(&self) -> Iter {
        Iter::new(self)
    }

    /// Create a visitor which can be used to replace all types found in this map
    /// with their aliases.
    pub fn visitor(&self) -> visitor::ApplySecondaryAliases {
        visitor::ApplySecondaryAliases::new(self)
    }
}

/// Iterator over [alias::Index]es
pub struct Iter<'m> {
    map: &'m alias::Map,
    state: IterState,
}

enum IterState {
    Primary,
    Secondary(usize),
}

impl<'m> Iter<'m> {
    fn new(map: &'m alias::Map) -> Self {
        Self {
            map,
            state: IterState::Primary,
        }
    }
}

impl<'m> Iterator for Iter<'m> {
    type Item = Alias<'m>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.state {
            IterState::Primary => {
                self.state = IterState::Secondary(0);
                Some(Alias::new(self.map, alias::Index::Primary, &self.map.primary))
            }
            IterState::Secondary(index) => {
                let details = self.map.list.get(*index)?;
                let n = Alias::new(self.map, alias::Index::Secondary(*index), details);
                *index += 1;
                Some(n)
            }
        }
    }
}

impl<'m> IntoIterator for &'m alias::Map {
    type Item = Alias<'m>;

    type IntoIter = Iter<'m>;

    fn into_iter(self) -> Self::IntoIter {
        Iter::new(self)
    }
}