blob: 8026fdad9bdb6bc1666a2d6f152c8bc6939ea5f2 [file] [log] [blame]
Alex Crichtona914a612018-04-04 07:48:44 -07001#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
Alex Crichtonaf5bad42018-03-27 14:45:10 -07002
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07003use std::fmt;
4use std::iter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07005use std::str::FromStr;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07006use std::panic;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07007
8use proc_macro;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07009use stable;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
Alex Crichtonf3888432018-05-16 09:11:05 -070011use {Delimiter, Group, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070012
13#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070014pub enum TokenStream {
15 Nightly(proc_macro::TokenStream),
16 Stable(stable::TokenStream),
17}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070018
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019pub enum LexError {
20 Nightly(proc_macro::LexError),
21 Stable(stable::LexError),
22}
23
24fn nightly_works() -> bool {
25 use std::sync::atomic::*;
26 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
27
28 match WORKS.load(Ordering::SeqCst) {
29 1 => return false,
30 2 => return true,
31 _ => {}
32 }
33 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
34 WORKS.store(works as usize + 1, Ordering::SeqCst);
35 works
36}
37
38fn mismatch() -> ! {
39 panic!("stable/nightly mismatch")
40}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070041
42impl TokenStream {
43 pub fn empty() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070044 if nightly_works() {
45 TokenStream::Nightly(proc_macro::TokenStream::empty())
46 } else {
47 TokenStream::Stable(stable::TokenStream::empty())
48 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070049 }
50
51 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070052 match self {
53 TokenStream::Nightly(tts) => tts.is_empty(),
54 TokenStream::Stable(tts) => tts.is_empty(),
55 }
56 }
57
58 fn unwrap_nightly(self) -> proc_macro::TokenStream {
59 match self {
60 TokenStream::Nightly(s) => s,
61 TokenStream::Stable(_) => mismatch(),
62 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070063 }
64}
65
66impl FromStr for TokenStream {
67 type Err = LexError;
68
69 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070070 if nightly_works() {
71 Ok(TokenStream::Nightly(src.parse()?))
72 } else {
73 Ok(TokenStream::Stable(src.parse()?))
74 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070075 }
76}
77
78impl fmt::Display for TokenStream {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070080 match self {
81 TokenStream::Nightly(tts) => tts.fmt(f),
82 TokenStream::Stable(tts) => tts.fmt(f),
83 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070084 }
85}
86
87impl From<proc_macro::TokenStream> for TokenStream {
88 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070090 }
91}
92
93impl From<TokenStream> for proc_macro::TokenStream {
94 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070095 match inner {
96 TokenStream::Nightly(inner) => inner,
97 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
98 }
99 }
100}
101
102impl From<stable::TokenStream> for TokenStream {
103 fn from(inner: stable::TokenStream) -> TokenStream {
104 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700105 }
106}
107
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700109 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700110 if !nightly_works() {
111 return TokenStream::Stable(token.into())
112 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700113 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700114 TokenTree::Group(tt) => {
115 let delim = match tt.delimiter() {
116 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
117 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
118 Delimiter::Brace => proc_macro::Delimiter::Brace,
119 Delimiter::None => proc_macro::Delimiter::None,
120 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700121 let span = tt.span();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700122 let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly());
123 group.set_span(span.inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700124 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700125 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700126 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700127 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700128 Spacing::Joint => proc_macro::Spacing::Joint,
129 Spacing::Alone => proc_macro::Spacing::Alone,
130 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700131 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700132 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700133 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700134 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700135 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700136 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700137 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700138 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700139 }
140}
141
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700142impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700143 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
144 if nightly_works() {
145 let trees = trees.into_iter()
146 .map(TokenStream::from)
147 .flat_map(|t| {
148 match t {
149 TokenStream::Nightly(s) => s,
150 TokenStream::Stable(_) => mismatch(),
151 }
152 });
153 TokenStream::Nightly(trees.collect())
154 } else {
155 TokenStream::Stable(trees.into_iter().collect())
156 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700157 }
158}
159
Alex Crichtonf3888432018-05-16 09:11:05 -0700160impl Extend<TokenTree> for TokenStream {
161 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
162 match self {
163 TokenStream::Nightly(tts) => {
164 *tts = tts.clone()
165 .into_iter()
166 .chain(
167 streams.into_iter()
168 .map(TokenStream::from)
169 .flat_map(|t| {
170 match t {
171 TokenStream::Nightly(tts) => tts.into_iter(),
172 _ => panic!()
173 }
174 })
175 )
176 .collect();
177 }
178 TokenStream::Stable(tts) => tts.extend(streams),
179 }
180 }
181}
182
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700183impl fmt::Debug for TokenStream {
184 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700185 match self {
186 TokenStream::Nightly(tts) => tts.fmt(f),
187 TokenStream::Stable(tts) => tts.fmt(f),
188 }
189 }
190}
191
192impl From<proc_macro::LexError> for LexError {
193 fn from(e: proc_macro::LexError) -> LexError {
194 LexError::Nightly(e)
195 }
196}
197
198impl From<stable::LexError> for LexError {
199 fn from(e: stable::LexError) -> LexError {
200 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700201 }
202}
203
204impl fmt::Debug for LexError {
205 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700206 match self {
207 LexError::Nightly(e) => e.fmt(f),
208 LexError::Stable(e) => e.fmt(f),
209 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700210 }
211}
212
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700213pub enum TokenTreeIter {
214 Nightly(proc_macro::token_stream::IntoIter),
215 Stable(stable::TokenTreeIter),
216}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700217
218impl IntoIterator for TokenStream {
219 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700220 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700221
Alex Crichton1a7f7622017-07-05 17:47:15 -0700222 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700223 match self {
224 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
225 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
226 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700227 }
228}
229
Alex Crichton1a7f7622017-07-05 17:47:15 -0700230impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700231 type Item = TokenTree;
232
233 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700234 let token = match self {
235 TokenTreeIter::Nightly(iter) => iter.next()?,
236 TokenTreeIter::Stable(iter) => return iter.next(),
237 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700238 Some(match token {
239 proc_macro::TokenTree::Group(tt) => {
240 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700241 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
242 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
243 proc_macro::Delimiter::Brace => Delimiter::Brace,
244 proc_macro::Delimiter::None => Delimiter::None,
245 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700246 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700247 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700248 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700249 g.into()
250 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700251 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700252 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700253 proc_macro::Spacing::Joint => Spacing::Joint,
254 proc_macro::Spacing::Alone => Spacing::Alone,
255 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700256 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700257 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700258 o.into()
259 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700260 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700261 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700262 })
263 }
264
265 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700266 match self {
267 TokenTreeIter::Nightly(tts) => tts.size_hint(),
268 TokenTreeIter::Stable(tts) => tts.size_hint(),
269 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700270 }
271}
272
Alex Crichton1a7f7622017-07-05 17:47:15 -0700273impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700274 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700275 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700276 }
277}
278
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700279pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500280
281// NOTE: We have to generate our own filename object here because we can't wrap
282// the one provided by proc_macro.
283#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700284pub enum SourceFile {
285 Nightly(proc_macro::SourceFile, FileName),
286 Stable(stable::SourceFile),
287}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500288
289impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700290 fn nightly(sf: proc_macro::SourceFile) -> Self {
291 let filename = stable::file_name(sf.path().to_string());
292 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500293 }
294
Nika Layzellf8d5f212017-12-11 14:07:02 -0500295 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500296 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700297 match self {
298 SourceFile::Nightly(_, f) => f,
299 SourceFile::Stable(a) => a.path(),
300 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500301 }
302
303 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700304 match self {
305 SourceFile::Nightly(a, _) => a.is_real(),
306 SourceFile::Stable(a) => a.is_real(),
307 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500308 }
309}
310
Nika Layzellb35a9a32017-12-30 14:34:35 -0500311impl AsRef<FileName> for SourceFile {
312 fn as_ref(&self) -> &FileName {
313 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500314 }
315}
316
317impl fmt::Debug for SourceFile {
318 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700319 match self {
320 SourceFile::Nightly(a, _) => a.fmt(f),
321 SourceFile::Stable(a) => a.fmt(f),
322 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500323 }
324}
325
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500326pub struct LineColumn {
327 pub line: usize,
328 pub column: usize,
329}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500330
Alex Crichton9cd80a62018-04-05 17:46:58 -0700331#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700332pub enum Span {
333 Nightly(proc_macro::Span),
334 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800335}
336
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700337impl Span {
338 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700339 if nightly_works() {
340 Span::Nightly(proc_macro::Span::call_site())
341 } else {
342 Span::Stable(stable::Span::call_site())
343 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700344 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700345
Alex Crichtone6085b72017-11-21 07:24:25 -0800346 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700347 if nightly_works() {
348 Span::Nightly(proc_macro::Span::def_site())
349 } else {
350 Span::Stable(stable::Span::def_site())
351 }
Alex Crichton998f6422017-11-19 08:06:27 -0800352 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500353
David Tolnay4e8e3972018-01-05 18:10:22 -0800354 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700355 match (self, other) {
356 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
357 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
358 _ => mismatch(),
359 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800360 }
361
362 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700363 match (self, other) {
364 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
365 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
366 _ => mismatch(),
367 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800368 }
369
David Tolnay16a17202017-12-31 10:47:24 -0500370 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700371 match self {
372 Span::Nightly(s) => s,
373 Span::Stable(_) => mismatch(),
374 }
David Tolnay16a17202017-12-31 10:47:24 -0500375 }
376
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700377 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500378 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700379 match self {
380 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
381 Span::Stable(s) => SourceFile::Stable(s.source_file()),
382 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500383 }
384
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700385 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500386 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700387 match self {
388 Span::Nightly(s) => {
389 let proc_macro::LineColumn { line, column } = s.start();
390 LineColumn { line, column }
391 }
392 Span::Stable(s) => {
393 let stable::LineColumn { line, column } = s.start();
394 LineColumn { line, column }
395 }
396 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500397 }
398
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700399 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500400 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700401 match self {
402 Span::Nightly(s) => {
403 let proc_macro::LineColumn { line, column } = s.end();
404 LineColumn { line, column }
405 }
406 Span::Stable(s) => {
407 let stable::LineColumn { line, column } = s.end();
408 LineColumn { line, column }
409 }
410 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500411 }
412
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700413 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500414 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700415 let ret = match (self, other) {
416 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
417 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
418 _ => return None,
419 };
420 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500421 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700422
423 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700424 match (self, other) {
425 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
426 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
427 _ => false,
428 }
429 }
430
431 fn unwrap_nightly(self) -> proc_macro::Span {
432 match self {
433 Span::Nightly(s) => s,
434 Span::Stable(_) => mismatch(),
435 }
436 }
437}
438
439impl From<proc_macro::Span> for ::Span {
440 fn from(proc_span: proc_macro::Span) -> ::Span {
441 ::Span::_new(Span::Nightly(proc_span))
442 }
443}
444
445impl From<stable::Span> for Span {
446 fn from(inner: stable::Span) -> Span {
447 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700448 }
Alex Crichton998f6422017-11-19 08:06:27 -0800449}
450
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700451impl fmt::Debug for Span {
452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700453 match self {
454 Span::Nightly(s) => s.fmt(f),
455 Span::Stable(s) => s.fmt(f),
456 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700457 }
458}
459
Alex Crichtonf3888432018-05-16 09:11:05 -0700460#[derive(Clone)]
461pub enum Ident {
462 Nightly(proc_macro::Ident),
463 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700464}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700465
Alex Crichtonf3888432018-05-16 09:11:05 -0700466impl Ident {
467 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700468 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700469 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
470 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700471 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700472 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700473
Alex Crichtonf3888432018-05-16 09:11:05 -0700474 pub fn new_raw(string: &str, span: Span) -> Ident {
475 match span {
476 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
477 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700478 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700479 }
480
481 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700482 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700483 Ident::Nightly(t) => Span::Nightly(t.span()),
484 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700485 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700486 }
487
488 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700489 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700490 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
491 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700492 _ => mismatch(),
493 }
494 }
495
Alex Crichtonf3888432018-05-16 09:11:05 -0700496 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700497 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700498 Ident::Nightly(s) => s,
499 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700500 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700501 }
502}
503
Alex Crichtonf3888432018-05-16 09:11:05 -0700504impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700505 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700506 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700507 Ident::Nightly(t) => t.fmt(f),
508 Ident::Stable(t) => t.fmt(f),
509 }
510 }
511}
512
513impl fmt::Debug for Ident {
514 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
515 match self {
516 Ident::Nightly(t) => t.fmt(f),
517 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700518 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700519 }
520}
521
522#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700523pub enum Literal {
524 Nightly(proc_macro::Literal),
525 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700526}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700527
Alex Crichtona914a612018-04-04 07:48:44 -0700528macro_rules! suffixed_numbers {
529 ($($name:ident => $kind:ident,)*) => ($(
530 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700531 if nightly_works() {
532 Literal::Nightly(proc_macro::Literal::$name(n))
533 } else {
534 Literal::Stable(stable::Literal::$name(n))
535 }
Alex Crichtona914a612018-04-04 07:48:44 -0700536 }
537 )*)
538}
539
540macro_rules! unsuffixed_integers {
541 ($($name:ident => $kind:ident,)*) => ($(
542 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700543 if nightly_works() {
544 Literal::Nightly(proc_macro::Literal::$name(n))
545 } else {
546 Literal::Stable(stable::Literal::$name(n))
547 }
Alex Crichtona914a612018-04-04 07:48:44 -0700548 }
549 )*)
550}
551
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700552impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700553 suffixed_numbers! {
554 u8_suffixed => u8,
555 u16_suffixed => u16,
556 u32_suffixed => u32,
557 u64_suffixed => u64,
558 usize_suffixed => usize,
559 i8_suffixed => i8,
560 i16_suffixed => i16,
561 i32_suffixed => i32,
562 i64_suffixed => i64,
563 isize_suffixed => isize,
564
565 f32_suffixed => f32,
566 f64_suffixed => f64,
567 }
568
569 unsuffixed_integers! {
570 u8_unsuffixed => u8,
571 u16_unsuffixed => u16,
572 u32_unsuffixed => u32,
573 u64_unsuffixed => u64,
574 usize_unsuffixed => usize,
575 i8_unsuffixed => i8,
576 i16_unsuffixed => i16,
577 i32_unsuffixed => i32,
578 i64_unsuffixed => i64,
579 isize_unsuffixed => isize,
580 }
581
582 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700583 if nightly_works() {
584 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
585 } else {
586 Literal::Stable(stable::Literal::f32_unsuffixed(f))
587 }
Alex Crichtona914a612018-04-04 07:48:44 -0700588 }
589
590 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700591 if nightly_works() {
592 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
593 } else {
594 Literal::Stable(stable::Literal::f64_unsuffixed(f))
595 }
Alex Crichtona914a612018-04-04 07:48:44 -0700596 }
597
Alex Crichtona914a612018-04-04 07:48:44 -0700598 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700599 if nightly_works() {
600 Literal::Nightly(proc_macro::Literal::string(t))
601 } else {
602 Literal::Stable(stable::Literal::string(t))
603 }
Alex Crichtona914a612018-04-04 07:48:44 -0700604 }
605
606 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700607 if nightly_works() {
608 Literal::Nightly(proc_macro::Literal::character(t))
609 } else {
610 Literal::Stable(stable::Literal::character(t))
611 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700612 }
613
614 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700615 if nightly_works() {
616 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
617 } else {
618 Literal::Stable(stable::Literal::byte_string(bytes))
619 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700620 }
621
Alex Crichtonb2c94622018-04-04 07:36:41 -0700622 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700623 match self {
624 Literal::Nightly(lit) => Span::Nightly(lit.span()),
625 Literal::Stable(lit) => Span::Stable(lit.span()),
626 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700627 }
628
Alex Crichtonb2c94622018-04-04 07:36:41 -0700629 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700630 match (self, span) {
631 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
632 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
633 _ => mismatch(),
634 }
635 }
636
637 fn unwrap_nightly(self) -> proc_macro::Literal {
638 match self {
639 Literal::Nightly(s) => s,
640 Literal::Stable(_) => mismatch(),
641 }
642 }
643}
644
645impl From<stable::Literal> for Literal {
646 fn from(s: stable::Literal) -> Literal {
647 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700648 }
649}
650
651impl fmt::Display for Literal {
652 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700653 match self {
654 Literal::Nightly(t) => t.fmt(f),
655 Literal::Stable(t) => t.fmt(f),
656 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700657 }
658}
659
660impl fmt::Debug for Literal {
661 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700662 match self {
663 Literal::Nightly(t) => t.fmt(f),
664 Literal::Stable(t) => t.fmt(f),
665 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700666 }
667}