swc_common/
pos.rs

1use std::{borrow::Cow, rc::Rc, sync::Arc};
2
3pub use crate::syntax_pos::{
4    hygiene, BytePos, CharPos, FileName, Globals, Loc, LocWithOpt, Mark, MultiSpan, SourceFile,
5    SourceFileAndBytePos, SourceFileAndLine, Span, SpanLinesError, SyntaxContext, DUMMY_SP,
6    GLOBALS, NO_EXPANSION,
7};
8
9///
10/// # Derive
11/// This trait can be derived with `#[derive(Spanned)]`.
12pub trait Spanned {
13    /// Get span of `self`.
14    fn span(&self) -> Span;
15
16    #[inline]
17    fn span_lo(&self) -> BytePos {
18        self.span().lo
19    }
20
21    #[inline]
22    fn span_hi(&self) -> BytePos {
23        self.span().hi
24    }
25}
26
27impl<T> Spanned for Cow<'_, T>
28where
29    T: Spanned + Clone,
30{
31    #[inline]
32    fn span(&self) -> Span {
33        (**self).span()
34    }
35
36    #[inline]
37    fn span_lo(&self) -> BytePos {
38        (**self).span_lo()
39    }
40
41    #[inline]
42    fn span_hi(&self) -> BytePos {
43        (**self).span_hi()
44    }
45}
46
47impl Spanned for Span {
48    #[inline(always)]
49    fn span(&self) -> Span {
50        *self
51    }
52}
53
54impl Spanned for BytePos {
55    /// Creates a new single-byte span.
56    #[inline(always)]
57    fn span(&self) -> Span {
58        Span::new(*self, *self)
59    }
60}
61
62impl<S> Spanned for Option<S>
63where
64    S: Spanned,
65{
66    #[inline]
67    fn span(&self) -> Span {
68        match *self {
69            Some(ref s) => s.span(),
70            None => DUMMY_SP,
71        }
72    }
73
74    #[inline]
75    fn span_lo(&self) -> BytePos {
76        match *self {
77            Some(ref s) => s.span_lo(),
78            None => BytePos::DUMMY,
79        }
80    }
81
82    #[inline]
83    fn span_hi(&self) -> BytePos {
84        match *self {
85            Some(ref s) => s.span_hi(),
86            None => BytePos::DUMMY,
87        }
88    }
89}
90
91impl<S> Spanned for Rc<S>
92where
93    S: ?Sized + Spanned,
94{
95    fn span(&self) -> Span {
96        <S as Spanned>::span(self)
97    }
98
99    #[inline]
100    fn span_lo(&self) -> BytePos {
101        <S as Spanned>::span_lo(self)
102    }
103
104    #[inline]
105    fn span_hi(&self) -> BytePos {
106        <S as Spanned>::span_hi(self)
107    }
108}
109
110impl<S> Spanned for Arc<S>
111where
112    S: ?Sized + Spanned,
113{
114    fn span(&self) -> Span {
115        <S as Spanned>::span(self)
116    }
117
118    #[inline]
119    fn span_lo(&self) -> BytePos {
120        <S as Spanned>::span_lo(self)
121    }
122
123    #[inline]
124    fn span_hi(&self) -> BytePos {
125        <S as Spanned>::span_hi(self)
126    }
127}
128
129impl<S> Spanned for Box<S>
130where
131    S: ?Sized + Spanned,
132{
133    fn span(&self) -> Span {
134        <S as Spanned>::span(self)
135    }
136
137    #[inline]
138    fn span_lo(&self) -> BytePos {
139        <S as Spanned>::span_lo(self)
140    }
141
142    #[inline]
143    fn span_hi(&self) -> BytePos {
144        <S as Spanned>::span_hi(self)
145    }
146}
147
148impl<S> Spanned for &S
149where
150    S: ?Sized + Spanned,
151{
152    fn span(&self) -> Span {
153        <S as Spanned>::span(self)
154    }
155
156    #[inline]
157    fn span_lo(&self) -> BytePos {
158        <S as Spanned>::span_lo(self)
159    }
160
161    #[inline]
162    fn span_hi(&self) -> BytePos {
163        <S as Spanned>::span_hi(self)
164    }
165}
166
167impl<A, B> Spanned for ::either::Either<A, B>
168where
169    A: Spanned,
170    B: Spanned,
171{
172    fn span(&self) -> Span {
173        match *self {
174            ::either::Either::Left(ref n) => n.span(),
175            ::either::Either::Right(ref n) => n.span(),
176        }
177    }
178
179    fn span_lo(&self) -> BytePos {
180        match *self {
181            ::either::Either::Left(ref n) => n.span_lo(),
182            ::either::Either::Right(ref n) => n.span_lo(),
183        }
184    }
185
186    fn span_hi(&self) -> BytePos {
187        match *self {
188            ::either::Either::Left(ref n) => n.span_hi(),
189            ::either::Either::Right(ref n) => n.span_hi(),
190        }
191    }
192}
193
194// swc_allocator::nightly_only!(
195//     impl<T> Spanned for swc_allocator::boxed::Box<T>
196//     where
197//         T: Spanned,
198//     {
199//         fn span(&self) -> Span {
200//             self.as_ref().span()
201//         }
202//     }
203// );