solar-ast 0.2.0

Solidity and Yul AST type and visitor trait definitions
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::BoxSlice;
use semver::Op;
use solar_data_structures::smallvec::{SmallVec, smallvec};
use solar_interface::Span;
use std::{cmp::Ordering, fmt};

pub use semver::Op as SemverOp;

// We use the same approach as Solc.
// See [`SemverReq::dis`] field docs for more details on how the requirements are treated.

// Solc implementation notes:
// - uses `unsigned` (`u32`) for version integers: https://github.com/argotorg/solidity/blob/e81f2bdbd66e9c8780f74b8a8d67b4dc2c87945e/liblangutil/SemVerHandler.cpp#L258
// - version numbers can be `*/x/X`, which are represented as `u32::MAX`: https://github.com/argotorg/solidity/blob/e81f2bdbd66e9c8780f74b8a8d67b4dc2c87945e/liblangutil/SemVerHandler.cpp#L263
// - ranges are parsed as `>=start, <=end`: https://github.com/argotorg/solidity/blob/e81f2bdbd66e9c8780f74b8a8d67b4dc2c87945e/liblangutil/SemVerHandler.cpp#L209
//   we however dedicate a separate node for this: [`SemverReqComponentKind::Range`]

/// A SemVer version number.
#[derive(Clone, Copy)]
pub enum SemverVersionNumber {
    /// A number.
    Number(u32),
    /// `*`, `X`, or `x`.
    Wildcard,
}

impl From<u64> for SemverVersionNumber {
    #[inline]
    fn from(n: u64) -> Self {
        match n.try_into() {
            Ok(n) => Self::Number(n),
            Err(_) => Self::Wildcard,
        }
    }
}

impl From<SemverVersionNumber> for u64 {
    #[inline]
    fn from(value: SemverVersionNumber) -> Self {
        match value {
            SemverVersionNumber::Number(n) => n as Self,
            SemverVersionNumber::Wildcard => Self::MAX,
        }
    }
}

impl fmt::Display for SemverVersionNumber {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Number(n) => n.fmt(f),
            Self::Wildcard => "*".fmt(f),
        }
    }
}

impl fmt::Debug for SemverVersionNumber {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl PartialEq for SemverVersionNumber {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Wildcard, _) | (_, Self::Wildcard) => true,
            (Self::Number(a), Self::Number(b)) => a == b,
        }
    }
}

impl Eq for SemverVersionNumber {}

impl PartialOrd for SemverVersionNumber {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SemverVersionNumber {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (Self::Wildcard, _) | (_, Self::Wildcard) => Ordering::Equal,
            (Self::Number(a), Self::Number(b)) => a.cmp(b),
        }
    }
}

/// A SemVer version.
#[derive(Clone)]
pub struct SemverVersion {
    pub span: Span,
    /// Major version.
    pub major: SemverVersionNumber,
    /// Minor version. Optional.
    pub minor: Option<SemverVersionNumber>,
    /// Patch version. Optional.
    pub patch: Option<SemverVersionNumber>,
    // Pre-release and build metadata are not supported.
}

impl PartialEq for SemverVersion {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl Eq for SemverVersion {}

impl PartialOrd for SemverVersion {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SemverVersion {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        #[inline]
        fn cmp_opt(a: &Option<SemverVersionNumber>, b: &Option<SemverVersionNumber>) -> Ordering {
            match (a, b) {
                (Some(a), Some(b)) => a.cmp(b),
                _ => Ordering::Equal,
            }
        }

        self.major
            .cmp(&other.major)
            .then_with(|| cmp_opt(&self.minor, &other.minor))
            .then_with(|| cmp_opt(&self.patch, &other.patch))
    }
}

impl fmt::Display for SemverVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { span: _, major, minor, patch } = *self;
        write!(f, "{major}")?;
        if let Some(minor) = minor {
            write!(f, ".{minor}")?;
        }
        if let Some(patch) = patch {
            if minor.is_none() {
                f.write_str(".*")?;
            }
            write!(f, ".{patch}")?;
        }
        Ok(())
    }
}

impl fmt::Debug for SemverVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SemverVersion")
            .field("span", &self.span)
            .field("version", &format_args!("{self}"))
            .finish()
    }
}

impl From<semver::Version> for SemverVersion {
    #[inline]
    fn from(version: semver::Version) -> Self {
        Self {
            span: Span::DUMMY,
            major: version.major.into(),
            minor: Some(version.minor.into()),
            patch: Some(version.patch.into()),
        }
    }
}

impl From<SemverVersion> for semver::Version {
    #[inline]
    fn from(version: SemverVersion) -> Self {
        Self::new(
            version.major.into(),
            version.minor.map(Into::into).unwrap_or(0),
            version.patch.map(Into::into).unwrap_or(0),
        )
    }
}

impl SemverVersion {
    /// Creates a new [::semver] version from this version.
    #[inline]
    pub fn into_semver(self) -> semver::Version {
        self.into()
    }
}

/// A SemVer version requirement. This is a list of components, and is never empty.
#[derive(Debug)]
pub struct SemverReq<'ast> {
    /// The components of this requirement.
    ///
    /// Or-ed list of and-ed components, meaning that `matches` is evaluated as
    /// `any([all(c) for c in dis])`.
    /// E.g.: `^0 <=1 || 0.5.0 - 0.6.0 ... || ...` -> `[[^0, <=1], [0.5.0 - 0.6.0, ...], ...]`
    pub dis: BoxSlice<'ast, SemverReqCon<'ast>>,
}

impl fmt::Display for SemverReq<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, con) in self.dis.iter().enumerate() {
            if i > 0 {
                f.write_str(" || ")?;
            }
            write!(f, "{con}")?;
        }
        Ok(())
    }
}

impl SemverReq<'_> {
    /// Returns `true` if the given version satisfies this requirement.
    pub fn matches(&self, version: &SemverVersion) -> bool {
        self.dis.iter().any(|c| c.matches(version))
    }

    /// Converts this requirement to a [::semver] version requirement.
    pub fn to_semver(&self) -> SemverVersionReqCompat {
        SemverVersionReqCompat { reqs: self.dis.iter().map(SemverReqCon::to_semver).collect() }
    }
}

/// A list of or-ed [`semver::VersionReq`].
///
/// Obtained with [`SemverReq::to_semver`].
pub struct SemverVersionReqCompat {
    /// The list of requirements.
    pub reqs: Vec<semver::VersionReq>,
}

impl SemverVersionReqCompat {
    /// Returns `true` if the given version satisfies this requirement.
    pub fn matches(&self, version: &semver::Version) -> bool {
        self.reqs.iter().any(|r| r.matches(version))
    }
}

/// A list of conjoint SemVer version requirement components.
#[derive(Debug)]
pub struct SemverReqCon<'ast> {
    pub span: Span,
    /// The list of components. See [`SemverReq::dis`] for more details.
    pub components: BoxSlice<'ast, SemverReqComponent>,
}

impl fmt::Display for SemverReqCon<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (j, component) in self.components.iter().enumerate() {
            if j > 0 {
                f.write_str(" ")?;
            }
            write!(f, "{component}")?;
        }
        Ok(())
    }
}

impl SemverReqCon<'_> {
    /// Converts this requirement to a [::semver] version requirement.
    pub fn to_semver(&self) -> semver::VersionReq {
        semver::VersionReq {
            comparators: self.components.iter().flat_map(SemverReqComponent::to_semver).collect(),
        }
    }

    /// Returns `true` if the given version satisfies this requirement.
    pub fn matches(&self, version: &SemverVersion) -> bool {
        self.components.iter().all(|c| c.matches(version))
    }
}

/// A single SemVer version requirement component.
#[derive(Clone, Debug)]
pub struct SemverReqComponent {
    pub span: Span,
    pub kind: SemverReqComponentKind,
}

impl fmt::Display for SemverReqComponent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl SemverReqComponent {
    /// Converts this requirement component to a [::semver] comparator.
    pub fn to_semver(&self) -> SmallVec<[semver::Comparator; 2]> {
        self.kind.to_semver()
    }

    /// Returns `true` if the given version satisfies this requirement component.
    pub fn matches(&self, version: &SemverVersion) -> bool {
        self.kind.matches(version)
    }
}

/// A SemVer version requirement component.
#[derive(Clone, Debug)]
pub enum SemverReqComponentKind {
    /// `v`, `=v`
    Op(Option<Op>, SemverVersion),
    /// `l - r`
    Range(SemverVersion, SemverVersion),
}

impl fmt::Display for SemverReqComponentKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Op(op, version) => {
                if let Some(op) = op {
                    let op = match op {
                        Op::Exact => "=",
                        Op::Greater => ">",
                        Op::GreaterEq => ">=",
                        Op::Less => "<",
                        Op::LessEq => "<=",
                        Op::Tilde => "~",
                        Op::Caret => "^",
                        Op::Wildcard => "*",
                        _ => "",
                    };
                    f.write_str(op)?;
                }
                write!(f, "{version}")
            }
            Self::Range(left, right) => write!(f, "{left} - {right}"),
        }
    }
}

impl SemverReqComponentKind {
    /// Converts this requirement component to a [::semver] comparator.
    pub fn to_semver(&self) -> SmallVec<[semver::Comparator; 2]> {
        let cvt_op = |op: Option<Op>, version: &SemverVersion| semver::Comparator {
            op: op.unwrap_or(Op::Exact),
            major: version.major.into(),
            minor: version.minor.map(Into::into),
            patch: version.patch.map(Into::into),
            pre: Default::default(),
        };
        match self {
            Self::Op(op, version) => smallvec![cvt_op(*op, version)],
            Self::Range(start, end) => smallvec![
                cvt_op(Some(semver::Op::GreaterEq), start),
                cvt_op(Some(semver::Op::LessEq), end)
            ],
        }
    }

    /// Returns `true` if the given version satisfies this requirement component.
    pub fn matches(&self, version: &SemverVersion) -> bool {
        match self {
            Self::Op(op, other) => matches_op(op.unwrap_or(Op::Exact), version, other),
            Self::Range(start, end) => {
                matches_op(Op::GreaterEq, version, start) && matches_op(Op::LessEq, version, end)
            }
        }
    }
}

fn matches_op(op: Op, a: &SemverVersion, b: &SemverVersion) -> bool {
    match op {
        Op::Exact => a == b,
        Op::Greater => a > b,
        Op::GreaterEq => a >= b,
        Op::Less => a < b,
        Op::LessEq => a <= b,
        Op::Tilde => matches_tilde(a, b),
        Op::Caret => matches_caret(a, b),
        Op::Wildcard => true,
        _ => false,
    }
}

fn matches_tilde(a: &SemverVersion, b: &SemverVersion) -> bool {
    // https://github.com/argotorg/solidity/blob/e81f2bdbd66e9c8780f74b8a8d67b4dc2c87945e/liblangutil/SemVerHandler.cpp#L80
    if !matches_op(Op::GreaterEq, a, b) {
        return false;
    }

    let mut a = a.clone();
    a.patch = None;
    matches_op(Op::LessEq, &a, b)
}

fn matches_caret(a: &SemverVersion, b: &SemverVersion) -> bool {
    // https://github.com/argotorg/solidity/blob/e81f2bdbd66e9c8780f74b8a8d67b4dc2c87945e/liblangutil/SemVerHandler.cpp#L95
    if !matches_op(Op::GreaterEq, a, b) {
        return false;
    }

    let mut a = a.clone();
    if a.major > SemverVersionNumber::Number(0) {
        a.minor = None;
    }
    a.patch = None;
    matches_op(Op::LessEq, &a, b)
}

// Tests in `crates/parse/src/parser/item.rs`