David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 10 | use proc_macro2::TokenStream; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 11 | use punctuated::Punctuated; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 12 | #[cfg(feature = "extra-traits")] |
| 13 | use std::hash::{Hash, Hasher}; |
| 14 | #[cfg(feature = "extra-traits")] |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 15 | use tt::TokenStreamHelper; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 16 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 17 | ast_enum_of_structs! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 18 | /// The possible types that a Rust value could have. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 19 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 20 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 21 | /// feature.* |
| 22 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 23 | /// # Syntax tree enum |
| 24 | /// |
| 25 | /// This type is a [syntax tree enum]. |
| 26 | /// |
| 27 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 28 | pub enum Type { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 29 | /// A dynamically sized slice type: `[T]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 30 | /// |
| 31 | /// *This type is available if Syn is built with the `"derive"` or |
| 32 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 33 | pub Slice(TypeSlice { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 34 | pub bracket_token: token::Bracket, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 35 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 36 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 37 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 38 | /// A fixed size array type: `[T; n]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 39 | /// |
| 40 | /// *This type is available if Syn is built with the `"derive"` or |
| 41 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 42 | pub Array(TypeArray { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 43 | pub bracket_token: token::Bracket, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 44 | pub elem: Box<Type>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 45 | pub semi_token: Token![;], |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 46 | pub len: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 47 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 48 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 49 | /// A raw pointer type: `*const T` or `*mut T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 50 | /// |
| 51 | /// *This type is available if Syn is built with the `"derive"` or |
| 52 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 53 | pub Ptr(TypePtr { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 54 | pub star_token: Token![*], |
| 55 | pub const_token: Option<Token![const]>, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 56 | pub mutability: Option<Token![mut]>, |
| 57 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 58 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 59 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 60 | /// A reference type: `&'a T` or `&'a mut T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 61 | /// |
| 62 | /// *This type is available if Syn is built with the `"derive"` or |
| 63 | /// `"full"` feature.* |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 64 | pub Reference(TypeReference { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 65 | pub and_token: Token![&], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 66 | pub lifetime: Option<Lifetime>, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 67 | pub mutability: Option<Token![mut]>, |
| 68 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 69 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 70 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 71 | /// A bare function type: `fn(usize) -> bool`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 72 | /// |
| 73 | /// *This type is available if Syn is built with the `"derive"` or |
| 74 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 75 | pub BareFn(TypeBareFn { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 76 | pub lifetimes: Option<BoundLifetimes>, |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 77 | pub unsafety: Option<Token![unsafe]>, |
| 78 | pub abi: Option<Abi>, |
| 79 | pub fn_token: Token![fn], |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 80 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 81 | pub inputs: Punctuated<BareFnArg, Token![,]>, |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 82 | pub variadic: Option<Token![...]>, |
| 83 | pub output: ReturnType, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 84 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 85 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 86 | /// The never type: `!`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 87 | /// |
| 88 | /// *This type is available if Syn is built with the `"derive"` or |
| 89 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 90 | pub Never(TypeNever { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 91 | pub bang_token: Token![!], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 92 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 93 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 94 | /// A tuple type: `(A, B, C, String)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 95 | /// |
| 96 | /// *This type is available if Syn is built with the `"derive"` or |
| 97 | /// `"full"` feature.* |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 98 | pub Tuple(TypeTuple { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 99 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 100 | pub elems: Punctuated<Type, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 101 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 102 | |
| 103 | /// A path like `std::slice::Iter`, optionally qualified with a |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 104 | /// self-type as in `<Vec<T> as SomeTrait>::Associated`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 105 | /// |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 106 | /// Type arguments are stored in the Path itself. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 107 | /// |
| 108 | /// *This type is available if Syn is built with the `"derive"` or |
| 109 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 110 | pub Path(TypePath { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 111 | pub qself: Option<QSelf>, |
| 112 | pub path: Path, |
| 113 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 114 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 115 | /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a |
| 116 | /// trait or a lifetime. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 117 | /// |
| 118 | /// *This type is available if Syn is built with the `"derive"` or |
| 119 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 120 | pub TraitObject(TypeTraitObject { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 121 | pub dyn_token: Option<Token![dyn]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 122 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 123 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 124 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 125 | /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or |
| 126 | /// a lifetime. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 127 | /// |
| 128 | /// *This type is available if Syn is built with the `"derive"` or |
| 129 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 130 | pub ImplTrait(TypeImplTrait { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 131 | pub impl_token: Token![impl], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 132 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 133 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 134 | |
| 135 | /// A parenthesized type equivalent to the inner type. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 136 | /// |
| 137 | /// *This type is available if Syn is built with the `"derive"` or |
| 138 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 139 | pub Paren(TypeParen { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 140 | pub paren_token: token::Paren, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 141 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 142 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 143 | |
| 144 | /// A type contained within invisible delimiters. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 145 | /// |
| 146 | /// *This type is available if Syn is built with the `"derive"` or |
| 147 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 148 | pub Group(TypeGroup { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 149 | pub group_token: token::Group, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 150 | pub elem: Box<Type>, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 151 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 152 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 153 | /// Indication that a type should be inferred by the compiler: `_`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 154 | /// |
| 155 | /// *This type is available if Syn is built with the `"derive"` or |
| 156 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 157 | pub Infer(TypeInfer { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 158 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 159 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 160 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 161 | /// A macro in the type position. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 162 | /// |
| 163 | /// *This type is available if Syn is built with the `"derive"` or |
| 164 | /// `"full"` feature.* |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 165 | pub Macro(TypeMacro { |
| 166 | pub mac: Macro, |
| 167 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 168 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 169 | /// Tokens in type position not interpreted by Syn. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 170 | /// |
| 171 | /// *This type is available if Syn is built with the `"derive"` or |
| 172 | /// `"full"` feature.* |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 173 | pub Verbatim(TypeVerbatim #manual_extra_traits { |
| 174 | pub tts: TokenStream, |
| 175 | }), |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | #[cfg(feature = "extra-traits")] |
| 180 | impl Eq for TypeVerbatim {} |
| 181 | |
| 182 | #[cfg(feature = "extra-traits")] |
| 183 | impl PartialEq for TypeVerbatim { |
| 184 | fn eq(&self, other: &Self) -> bool { |
| 185 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | #[cfg(feature = "extra-traits")] |
| 190 | impl Hash for TypeVerbatim { |
| 191 | fn hash<H>(&self, state: &mut H) |
| 192 | where |
| 193 | H: Hasher, |
| 194 | { |
| 195 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | ast_struct! { |
David Tolnay | f01e37b | 2018-01-06 23:38:26 -0800 | [diff] [blame] | 200 | /// The binary interface of a function: `extern "C"`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 201 | /// |
| 202 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 203 | /// feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 204 | pub struct Abi { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 205 | pub extern_token: Token![extern], |
David Tolnay | f01e37b | 2018-01-06 23:38:26 -0800 | [diff] [blame] | 206 | pub name: Option<LitStr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | ast_struct! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 211 | /// An argument in a function type: the `usize` in `fn(usize) -> bool`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 212 | /// |
| 213 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 214 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 215 | pub struct BareFnArg { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 216 | pub name: Option<(BareFnArgName, Token![:])>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 217 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 221 | ast_enum! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 222 | /// Name of an argument in a function type: the `n` in `fn(n: usize)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 223 | /// |
| 224 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 225 | /// feature.* |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 226 | pub enum BareFnArgName { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 227 | /// Argument given a name. |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 228 | Named(Ident), |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 229 | /// Argument not given a name, matched with `_`. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 230 | Wild(Token![_]), |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 233 | |
| 234 | ast_enum! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 235 | /// Return type of a function signature. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 236 | /// |
| 237 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 238 | /// feature.* |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 239 | pub enum ReturnType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 240 | /// Return type is not specified. |
| 241 | /// |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 242 | /// Functions default to `()` and closures default to type inference. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 243 | Default, |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 244 | /// A particular type is returned. |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 245 | Type(Token![->], Box<Type>), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 246 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 247 | } |
| 248 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 249 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 250 | pub mod parsing { |
| 251 | use super::*; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 252 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 253 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 254 | use path; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 255 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 256 | impl Parse for Type { |
| 257 | fn parse(input: ParseStream) -> Result<Self> { |
| 258 | ambig_ty(input, true) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 261 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 262 | impl Type { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 263 | /// In some positions, types may not contain the `+` character, to |
| 264 | /// disambiguate them. For example in the expression `1 as T`, T may not |
| 265 | /// contain a `+` character. |
| 266 | /// |
| 267 | /// This parser does not allow a `+`, while the default parser does. |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 268 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 269 | ambig_ty(input, false) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 270 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 271 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 272 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 273 | fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> { |
| 274 | if input.peek(token::Group) { |
| 275 | return input.parse().map(Type::Group); |
| 276 | } |
| 277 | |
| 278 | let mut lifetimes = None::<BoundLifetimes>; |
| 279 | let mut lookahead = input.lookahead1(); |
| 280 | if lookahead.peek(Token![for]) { |
| 281 | lifetimes = input.parse()?; |
| 282 | lookahead = input.lookahead1(); |
| 283 | if !lookahead.peek(Ident) |
| 284 | && !lookahead.peek(Token![fn]) |
| 285 | && !lookahead.peek(Token![unsafe]) |
| 286 | && !lookahead.peek(Token![extern]) |
| 287 | && !lookahead.peek(Token![super]) |
| 288 | && !lookahead.peek(Token![self]) |
| 289 | && !lookahead.peek(Token![Self]) |
| 290 | && !lookahead.peek(Token![crate]) |
| 291 | { |
| 292 | return Err(lookahead.error()); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if lookahead.peek(token::Paren) { |
| 297 | let content; |
| 298 | let paren_token = parenthesized!(content in input); |
| 299 | if content.is_empty() { |
| 300 | return Ok(Type::Tuple(TypeTuple { |
| 301 | paren_token: paren_token, |
| 302 | elems: Punctuated::new(), |
| 303 | })); |
| 304 | } |
| 305 | if content.peek(Lifetime) { |
| 306 | return Ok(Type::Paren(TypeParen { |
| 307 | paren_token: paren_token, |
| 308 | elem: Box::new(Type::TraitObject(content.parse()?)), |
| 309 | })); |
| 310 | } |
| 311 | let first: Type = content.parse()?; |
| 312 | if content.peek(Token![,]) { |
| 313 | Ok(Type::Tuple(TypeTuple { |
| 314 | paren_token: paren_token, |
| 315 | elems: { |
| 316 | let mut elems = Punctuated::new(); |
| 317 | elems.push_value(first); |
| 318 | elems.push_punct(content.parse()?); |
| 319 | let rest: Punctuated<Type, Token![,]> = |
| 320 | content.parse_terminated(Parse::parse)?; |
| 321 | elems.extend(rest); |
| 322 | elems |
| 323 | }, |
| 324 | })) |
| 325 | } else { |
| 326 | Ok(Type::Paren(TypeParen { |
| 327 | paren_token: paren_token, |
| 328 | elem: Box::new(first), |
| 329 | })) |
| 330 | } |
| 331 | } else if lookahead.peek(Token![fn]) |
| 332 | || lookahead.peek(Token![unsafe]) |
| 333 | || lookahead.peek(Token![extern]) && !input.peek2(Token![::]) |
| 334 | { |
| 335 | let mut bare_fn: TypeBareFn = input.parse()?; |
| 336 | bare_fn.lifetimes = lifetimes; |
| 337 | Ok(Type::BareFn(bare_fn)) |
| 338 | } else if lookahead.peek(Ident) |
| 339 | || input.peek(Token![super]) |
| 340 | || input.peek(Token![self]) |
| 341 | || input.peek(Token![Self]) |
| 342 | || input.peek(Token![crate]) |
| 343 | || input.peek(Token![extern]) |
| 344 | || lookahead.peek(Token![::]) |
| 345 | || lookahead.peek(Token![<]) |
| 346 | { |
| 347 | if input.peek(Token![dyn]) { |
| 348 | let mut trait_object: TypeTraitObject = input.parse()?; |
| 349 | if lifetimes.is_some() { |
| 350 | match *trait_object.bounds.iter_mut().next().unwrap() { |
| 351 | TypeParamBound::Trait(ref mut trait_bound) => { |
| 352 | trait_bound.lifetimes = lifetimes; |
| 353 | } |
| 354 | TypeParamBound::Lifetime(_) => unreachable!(), |
| 355 | } |
| 356 | } |
| 357 | return Ok(Type::TraitObject(trait_object)); |
| 358 | } |
| 359 | |
| 360 | let ty: TypePath = input.parse()?; |
| 361 | if ty.qself.is_some() { |
| 362 | return Ok(Type::Path(ty)); |
| 363 | } |
| 364 | |
| 365 | if input.peek(Token![!]) && !input.peek(Token![!=]) { |
| 366 | let mut contains_arguments = false; |
| 367 | for segment in &ty.path.segments { |
| 368 | match segment.arguments { |
| 369 | PathArguments::None => {} |
| 370 | PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => { |
| 371 | contains_arguments = true; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if !contains_arguments { |
| 377 | let bang_token: Token![!] = input.parse()?; |
| 378 | let (delimiter, tts) = mac::parse_delimiter(input)?; |
| 379 | return Ok(Type::Macro(TypeMacro { |
| 380 | mac: Macro { |
| 381 | path: ty.path, |
| 382 | bang_token: bang_token, |
| 383 | delimiter: delimiter, |
| 384 | tts: tts, |
| 385 | }, |
| 386 | })); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if lifetimes.is_some() || allow_plus && input.peek(Token![+]) { |
| 391 | let mut bounds = Punctuated::new(); |
| 392 | bounds.push_value(TypeParamBound::Trait(TraitBound { |
| 393 | paren_token: None, |
| 394 | modifier: TraitBoundModifier::None, |
| 395 | lifetimes: lifetimes, |
| 396 | path: ty.path, |
| 397 | })); |
| 398 | if allow_plus && input.peek(Token![+]) { |
| 399 | bounds.push_punct(input.parse()?); |
| 400 | let rest: Punctuated<TypeParamBound, Token![+]> = |
| 401 | input.parse_synom(Punctuated::parse_terminated_nonempty)?; |
| 402 | bounds.extend(rest); |
| 403 | } |
| 404 | return Ok(Type::TraitObject(TypeTraitObject { |
| 405 | dyn_token: None, |
| 406 | bounds: bounds, |
| 407 | })); |
| 408 | } |
| 409 | |
| 410 | Ok(Type::Path(ty)) |
| 411 | } else if lookahead.peek(token::Bracket) { |
| 412 | let content; |
| 413 | let bracket_token = bracketed!(content in input); |
| 414 | let elem: Type = content.parse()?; |
| 415 | if content.peek(Token![;]) { |
| 416 | Ok(Type::Array(TypeArray { |
| 417 | bracket_token: bracket_token, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 418 | elem: Box::new(elem), |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 419 | semi_token: content.parse()?, |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 420 | len: content.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 421 | })) |
| 422 | } else { |
| 423 | Ok(Type::Slice(TypeSlice { |
| 424 | bracket_token: bracket_token, |
| 425 | elem: Box::new(elem), |
| 426 | })) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 427 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 428 | } else if lookahead.peek(Token![*]) { |
| 429 | input.parse().map(Type::Ptr) |
| 430 | } else if lookahead.peek(Token![&]) { |
| 431 | input.parse().map(Type::Reference) |
| 432 | } else if lookahead.peek(Token![!]) && !input.peek(Token![=]) { |
| 433 | input.parse().map(Type::Never) |
| 434 | } else if lookahead.peek(Token![impl ]) { |
| 435 | input.parse().map(Type::ImplTrait) |
| 436 | } else if lookahead.peek(Token![_]) { |
| 437 | input.parse().map(Type::Infer) |
| 438 | } else if lookahead.peek(Lifetime) { |
| 439 | input.parse().map(Type::TraitObject) |
| 440 | } else { |
| 441 | Err(lookahead.error()) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 442 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 443 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 444 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 445 | impl Parse for TypeSlice { |
| 446 | fn parse(input: ParseStream) -> Result<Self> { |
| 447 | let content; |
| 448 | Ok(TypeSlice { |
| 449 | bracket_token: bracketed!(content in input), |
| 450 | elem: content.parse()?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 451 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 452 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 453 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 454 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 455 | impl Parse for TypeArray { |
| 456 | fn parse(input: ParseStream) -> Result<Self> { |
| 457 | let content; |
| 458 | Ok(TypeArray { |
| 459 | bracket_token: bracketed!(content in input), |
| 460 | elem: content.parse()?, |
| 461 | semi_token: content.parse()?, |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 462 | len: content.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 463 | }) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | impl Parse for TypePtr { |
| 468 | fn parse(input: ParseStream) -> Result<Self> { |
| 469 | let star_token: Token![*] = input.parse()?; |
| 470 | |
| 471 | let lookahead = input.lookahead1(); |
| 472 | let (const_token, mutability) = if lookahead.peek(Token![const]) { |
| 473 | (Some(input.parse()?), None) |
| 474 | } else if lookahead.peek(Token![mut]) { |
| 475 | (None, Some(input.parse()?)) |
| 476 | } else { |
| 477 | return Err(lookahead.error()); |
| 478 | }; |
| 479 | |
| 480 | Ok(TypePtr { |
| 481 | star_token: star_token, |
| 482 | const_token: const_token, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 483 | mutability: mutability, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 484 | elem: Box::new(input.call(Type::without_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 485 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 486 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 487 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 488 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 489 | impl Parse for TypeReference { |
| 490 | fn parse(input: ParseStream) -> Result<Self> { |
| 491 | Ok(TypeReference { |
| 492 | and_token: input.parse()?, |
| 493 | lifetime: input.parse()?, |
| 494 | mutability: input.parse()?, |
| 495 | // & binds tighter than +, so we don't allow + here. |
| 496 | elem: Box::new(input.call(Type::without_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 497 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 498 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 499 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 500 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 501 | impl Parse for TypeBareFn { |
| 502 | fn parse(input: ParseStream) -> Result<Self> { |
| 503 | let args; |
| 504 | let allow_variadic; |
| 505 | Ok(TypeBareFn { |
| 506 | lifetimes: input.parse()?, |
| 507 | unsafety: input.parse()?, |
| 508 | abi: input.parse()?, |
| 509 | fn_token: input.parse()?, |
| 510 | paren_token: parenthesized!(args in input), |
| 511 | inputs: { |
| 512 | let inputs = args.parse_synom(Punctuated::parse_terminated)?; |
| 513 | allow_variadic = inputs.empty_or_trailing(); |
| 514 | inputs |
| 515 | }, |
| 516 | variadic: { |
| 517 | if allow_variadic && args.peek(Token![...]) { |
| 518 | Some(args.parse()?) |
| 519 | } else { |
| 520 | None |
| 521 | } |
| 522 | }, |
| 523 | output: input.call(ReturnType::without_plus)?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 524 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 525 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 526 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 527 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 528 | impl Parse for TypeNever { |
| 529 | fn parse(input: ParseStream) -> Result<Self> { |
| 530 | Ok(TypeNever { |
| 531 | bang_token: input.parse()?, |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 532 | }) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | impl Parse for TypeInfer { |
| 537 | fn parse(input: ParseStream) -> Result<Self> { |
| 538 | Ok(TypeInfer { |
| 539 | underscore_token: input.parse()?, |
| 540 | }) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | impl Parse for TypeTuple { |
| 545 | fn parse(input: ParseStream) -> Result<Self> { |
| 546 | let content; |
| 547 | Ok(TypeTuple { |
| 548 | paren_token: parenthesized!(content in input), |
| 549 | elems: content.parse_terminated(<Type as Parse>::parse)?, |
| 550 | }) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | impl Parse for TypeMacro { |
| 555 | fn parse(input: ParseStream) -> Result<Self> { |
| 556 | Ok(TypeMacro { |
| 557 | mac: input.parse()?, |
| 558 | }) |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | impl Parse for TypePath { |
| 563 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 564 | let (qself, mut path) = path::parsing::qpath(input, false)?; |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 565 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 566 | if path.segments.last().unwrap().value().arguments.is_empty() |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 567 | && input.peek(token::Paren) |
| 568 | { |
| 569 | let args: ParenthesizedGenericArguments = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 570 | let parenthesized = PathArguments::Parenthesized(args); |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 571 | path.segments.last_mut().unwrap().value_mut().arguments = parenthesized; |
| 572 | } |
| 573 | |
| 574 | Ok(TypePath { |
| 575 | qself: qself, |
| 576 | path: path, |
| 577 | }) |
| 578 | } |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 579 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 580 | |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 581 | impl ReturnType { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 582 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 583 | Self::parse(input, false) |
| 584 | } |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 585 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 586 | pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 587 | if input.peek(Token![->]) { |
| 588 | let arrow = input.parse()?; |
| 589 | let ty = ambig_ty(input, allow_plus)?; |
| 590 | Ok(ReturnType::Type(arrow, Box::new(ty))) |
| 591 | } else { |
| 592 | Ok(ReturnType::Default) |
| 593 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 594 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 595 | } |
| 596 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 597 | impl Parse for ReturnType { |
| 598 | fn parse(input: ParseStream) -> Result<Self> { |
| 599 | Self::parse(input, true) |
| 600 | } |
| 601 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 602 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 603 | impl Parse for TypeTraitObject { |
| 604 | fn parse(input: ParseStream) -> Result<Self> { |
| 605 | Self::parse(input, true) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 606 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 607 | } |
| 608 | |
David Tolnay | e39b070 | 2018-01-09 17:57:31 -0800 | [diff] [blame] | 609 | fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool { |
| 610 | for bound in bounds { |
| 611 | if let TypeParamBound::Trait(_) = *bound { |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | false |
| 616 | } |
| 617 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 618 | impl TypeTraitObject { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 619 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 620 | Self::parse(input, false) |
| 621 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 622 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 623 | // Only allow multiple trait references if allow_plus is true. |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 624 | pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 625 | Ok(TypeTraitObject { |
| 626 | dyn_token: input.parse()?, |
| 627 | bounds: { |
| 628 | let bounds = if allow_plus { |
| 629 | input.parse_synom(Punctuated::parse_terminated_nonempty)? |
| 630 | } else { |
| 631 | let mut bounds = Punctuated::new(); |
| 632 | bounds.push_value(input.parse()?); |
| 633 | bounds |
| 634 | }; |
| 635 | // Just lifetimes like `'a + 'b` is not a TraitObject. |
| 636 | if !at_least_one_type(&bounds) { |
| 637 | return Err(input.error("expected at least one type")); |
| 638 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 639 | bounds |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 640 | }, |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 641 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 642 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 643 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 644 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 645 | impl Parse for TypeImplTrait { |
| 646 | fn parse(input: ParseStream) -> Result<Self> { |
| 647 | Ok(TypeImplTrait { |
| 648 | impl_token: input.parse()?, |
| 649 | // NOTE: rust-lang/rust#34511 includes discussion about whether |
| 650 | // or not + should be allowed in ImplTrait directly without (). |
| 651 | bounds: input.parse_synom(Punctuated::parse_terminated_nonempty)?, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 652 | }) |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 653 | } |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 654 | } |
| 655 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 656 | impl Parse for TypeGroup { |
| 657 | fn parse(input: ParseStream) -> Result<Self> { |
| 658 | let content; |
| 659 | Ok(TypeGroup { |
| 660 | group_token: grouped!(content in input), |
| 661 | elem: content.parse()?, |
| 662 | }) |
| 663 | } |
| 664 | } |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 665 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 666 | impl Parse for TypeParen { |
| 667 | fn parse(input: ParseStream) -> Result<Self> { |
| 668 | Self::parse(input, false) |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 669 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | impl TypeParen { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 673 | fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 674 | let content; |
| 675 | Ok(TypeParen { |
| 676 | paren_token: parenthesized!(content in input), |
| 677 | elem: Box::new(ambig_ty(&content, allow_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 678 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 679 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 680 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 681 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 682 | impl Parse for BareFnArg { |
| 683 | fn parse(input: ParseStream) -> Result<Self> { |
| 684 | Ok(BareFnArg { |
| 685 | name: { |
| 686 | if (input.peek(Ident) || input.peek(Token![_])) |
| 687 | && !input.peek2(Token![::]) |
| 688 | && input.peek2(Token![:]) |
| 689 | { |
| 690 | let name: BareFnArgName = input.parse()?; |
| 691 | let colon: Token![:] = input.parse()?; |
| 692 | Some((name, colon)) |
| 693 | } else { |
| 694 | None |
| 695 | } |
| 696 | }, |
| 697 | ty: input.parse()?, |
| 698 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 699 | } |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 700 | } |
| 701 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 702 | impl Parse for BareFnArgName { |
| 703 | fn parse(input: ParseStream) -> Result<Self> { |
| 704 | let lookahead = input.lookahead1(); |
| 705 | if lookahead.peek(Ident) { |
| 706 | input.parse().map(BareFnArgName::Named) |
| 707 | } else if lookahead.peek(Token![_]) { |
| 708 | input.parse().map(BareFnArgName::Wild) |
| 709 | } else { |
| 710 | Err(lookahead.error()) |
| 711 | } |
| 712 | } |
| 713 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 714 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 715 | impl Parse for Abi { |
| 716 | fn parse(input: ParseStream) -> Result<Self> { |
| 717 | Ok(Abi { |
| 718 | extern_token: input.parse()?, |
| 719 | name: input.parse()?, |
| 720 | }) |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | impl Parse for Option<Abi> { |
| 725 | fn parse(input: ParseStream) -> Result<Self> { |
| 726 | if input.peek(Token![extern]) { |
| 727 | input.parse().map(Some) |
| 728 | } else { |
| 729 | Ok(None) |
| 730 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 731 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 732 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 733 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 734 | |
| 735 | #[cfg(feature = "printing")] |
| 736 | mod printing { |
| 737 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 738 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 739 | use quote::ToTokens; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 740 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 741 | impl ToTokens for TypeSlice { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 742 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 743 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 744 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 745 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 749 | impl ToTokens for TypeArray { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 750 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 751 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 752 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 753 | self.semi_token.to_tokens(tokens); |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 754 | self.len.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 755 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 759 | impl ToTokens for TypePtr { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 760 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 761 | self.star_token.to_tokens(tokens); |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 762 | match self.mutability { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 763 | Some(ref tok) => tok.to_tokens(tokens), |
| 764 | None => { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 765 | TokensOrDefault(&self.const_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 766 | } |
| 767 | } |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 768 | self.elem.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 772 | impl ToTokens for TypeReference { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 773 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 774 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 775 | self.lifetime.to_tokens(tokens); |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 776 | self.mutability.to_tokens(tokens); |
| 777 | self.elem.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 781 | impl ToTokens for TypeBareFn { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 782 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 783 | self.lifetimes.to_tokens(tokens); |
| 784 | self.unsafety.to_tokens(tokens); |
| 785 | self.abi.to_tokens(tokens); |
| 786 | self.fn_token.to_tokens(tokens); |
| 787 | self.paren_token.surround(tokens, |tokens| { |
| 788 | self.inputs.to_tokens(tokens); |
| 789 | if let Some(ref variadic) = self.variadic { |
| 790 | if !self.inputs.empty_or_trailing() { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 791 | let span = variadic.spans[0]; |
| 792 | Token.to_tokens(tokens); |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 793 | } |
| 794 | variadic.to_tokens(tokens); |
| 795 | } |
| 796 | }); |
| 797 | self.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 801 | impl ToTokens for TypeNever { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 802 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 803 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 807 | impl ToTokens for TypeTuple { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 808 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 809 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 810 | self.elems.to_tokens(tokens); |
David Tolnay | db40206 | 2018-03-31 22:23:30 +0200 | [diff] [blame] | 811 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 815 | impl ToTokens for TypePath { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 816 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 817 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 818 | } |
| 819 | } |
| 820 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 821 | impl ToTokens for TypeTraitObject { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 822 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 823 | self.dyn_token.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 824 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 828 | impl ToTokens for TypeImplTrait { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 829 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 830 | self.impl_token.to_tokens(tokens); |
| 831 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 835 | impl ToTokens for TypeGroup { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 836 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 837 | self.group_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 838 | self.elem.to_tokens(tokens); |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 839 | }); |
| 840 | } |
| 841 | } |
| 842 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 843 | impl ToTokens for TypeParen { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 844 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 845 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 846 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 847 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 851 | impl ToTokens for TypeInfer { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 852 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 853 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 857 | impl ToTokens for TypeMacro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 858 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 859 | self.mac.to_tokens(tokens); |
| 860 | } |
| 861 | } |
| 862 | |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 863 | impl ToTokens for TypeVerbatim { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 864 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 865 | self.tts.to_tokens(tokens); |
| 866 | } |
| 867 | } |
| 868 | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 869 | impl ToTokens for ReturnType { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 870 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 871 | match *self { |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 872 | ReturnType::Default => {} |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 873 | ReturnType::Type(ref arrow, ref ty) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 874 | arrow.to_tokens(tokens); |
| 875 | ty.to_tokens(tokens); |
| 876 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 881 | impl ToTokens for BareFnArg { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 882 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 883 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 884 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 885 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 886 | } |
| 887 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 890 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 891 | impl ToTokens for BareFnArgName { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 892 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 893 | match *self { |
| 894 | BareFnArgName::Named(ref t) => t.to_tokens(tokens), |
| 895 | BareFnArgName::Wild(ref t) => t.to_tokens(tokens), |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 900 | impl ToTokens for Abi { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 901 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 902 | self.extern_token.to_tokens(tokens); |
David Tolnay | d512576 | 2017-12-29 02:42:17 -0500 | [diff] [blame] | 903 | self.name.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 906 | } |