sway_types/
ident.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use serde::Serialize;

use crate::{span::Span, Spanned};

use std::{
    cmp::{Ord, Ordering},
    fmt,
    hash::{Hash, Hasher},
    sync::Arc,
};

pub trait Named {
    fn name(&self) -> &BaseIdent;
}

#[derive(Clone)]
pub struct BaseIdent {
    name_override_opt: Option<Arc<String>>,
    span: Span,
    is_raw_ident: bool,
}

impl BaseIdent {
    pub fn as_str(&self) -> &str {
        self.name_override_opt
            .as_deref()
            .map(|x| x.as_str())
            .unwrap_or_else(|| self.span.as_str())
    }

    pub fn is_raw_ident(&self) -> bool {
        self.is_raw_ident
    }

    pub fn name_override_opt(&self) -> Option<&str> {
        self.name_override_opt.as_deref().map(|x| x.as_str())
    }

    pub fn new(span: Span) -> Ident {
        let span = span.trim();
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_no_trim(span: Span) -> Ident {
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_with_raw(span: Span, is_raw_ident: bool) -> Ident {
        let span = span.trim();
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident,
        }
    }

    pub fn new_with_override(name_override: String, span: Span) -> Ident {
        Ident {
            name_override_opt: Some(Arc::new(name_override)),
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_no_span(name: String) -> Ident {
        Ident {
            name_override_opt: Some(Arc::new(name)),
            span: Span::dummy(),
            is_raw_ident: false,
        }
    }

    pub fn dummy() -> Ident {
        Ident {
            name_override_opt: Some(Arc::new("foo".into())),
            span: Span::dummy(),
            is_raw_ident: false,
        }
    }
}

/// An [Ident] is an _identifier_ with a corresponding `span` from which it was derived.
/// It relies on a custom implementation of Hash which only looks at its textual name
/// representation, so that namespacing isn't reliant on the span itself, which will
/// often be different.
pub type Ident = BaseIdent;

impl Serialize for Ident {
    // Serialize an `Ident` struct with two fields: `to_string` and `span`.
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;

        let mut state = serializer.serialize_struct("Ident", 2)?;
        state.serialize_field("to_string", &self.to_string())?;
        state.serialize_field("span", &self.span)?;
        state.end()
    }
}

impl Hash for Ident {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl PartialEq for Ident {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Ord for Ident {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

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

impl Eq for Ident {}

impl Spanned for Ident {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

impl fmt::Display for Ident {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.as_str())
    }
}

impl fmt::Debug for Ident {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.as_str())
    }
}

/// An [IdentUnique] is an _identifier_ with a corresponding `span` from which it was derived.
/// Its hash and equality implementation takes the full span into account, meaning that identifiers
/// are considered unique if they originate from different files.
#[derive(Debug, Clone)]
pub struct IdentUnique(BaseIdent);

impl From<Ident> for IdentUnique {
    fn from(item: Ident) -> Self {
        IdentUnique(item)
    }
}

impl From<&Ident> for IdentUnique {
    fn from(item: &Ident) -> Self {
        IdentUnique(item.clone())
    }
}

impl From<&IdentUnique> for Ident {
    fn from(item: &IdentUnique) -> Self {
        Ident {
            name_override_opt: item.0.name_override_opt().map(|s| Arc::new(s.to_string())),
            span: item.0.span(),
            is_raw_ident: item.0.is_raw_ident(),
        }
    }
}

impl Hash for IdentUnique {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.span().hash(state);
        self.0.as_str().hash(state);
    }
}

impl PartialEq for IdentUnique {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_str() == other.0.as_str() && self.0.span() == other.0.span()
    }
}

impl Ord for IdentUnique {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0
            .span()
            .cmp(&other.0.span())
            .then(self.0.as_str().cmp(other.0.as_str()))
    }
}

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

impl Eq for IdentUnique {}

impl Spanned for IdentUnique {
    fn span(&self) -> Span {
        self.0.span()
    }
}

impl fmt::Display for IdentUnique {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.0.as_str())
    }
}