Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use delimited::Delimited; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 2 | use super::*; |
| 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_enum_of_structs! { |
| 5 | /// The different kinds of types recognized by the compiler |
| 6 | pub enum Ty { |
| 7 | /// A variable-length array (`[T]`) |
| 8 | pub Slice(TySlice { |
| 9 | pub ty: Box<Ty>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 10 | pub bracket_token: tokens::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 11 | }), |
| 12 | /// A fixed length array (`[T; n]`) |
| 13 | pub Array(TyArray { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 14 | pub bracket_token: tokens::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | pub ty: Box<Ty>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 16 | pub semi_token: tokens::Semi, |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame^] | 17 | pub amt: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | }), |
| 19 | /// A raw pointer (`*const T` or `*mut T`) |
| 20 | pub Ptr(TyPtr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 21 | pub star_token: tokens::Star, |
| 22 | pub const_token: Option<tokens::Const>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 23 | pub ty: Box<MutTy>, |
| 24 | }), |
| 25 | /// A reference (`&'a T` or `&'a mut T`) |
| 26 | pub Rptr(TyRptr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | pub and_token: tokens::And, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | pub lifetime: Option<Lifetime>, |
| 29 | pub ty: Box<MutTy>, |
| 30 | }), |
| 31 | /// A bare function (e.g. `fn(usize) -> bool`) |
| 32 | pub BareFn(TyBareFn { |
| 33 | pub ty: Box<BareFnTy>, |
| 34 | }), |
| 35 | /// The never type (`!`) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | pub Never(TyNever { |
| 37 | pub bang_token: tokens::Bang, |
| 38 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 39 | /// A tuple (`(A, B, C, D, ...)`) |
| 40 | pub Tup(TyTup { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 41 | pub paren_token: tokens::Paren, |
| 42 | pub tys: Delimited<Ty, tokens::Comma>, |
| 43 | pub lone_comma: Option<tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 44 | }), |
| 45 | /// A path (`module::module::...::Type`), optionally |
| 46 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 47 | /// |
| 48 | /// Type parameters are stored in the Path itself |
| 49 | pub Path(TyPath { |
| 50 | pub qself: Option<QSelf>, |
| 51 | pub path: Path, |
| 52 | }), |
| 53 | /// A trait object type `Bound1 + Bound2 + Bound3` |
| 54 | /// where `Bound` is a trait or a lifetime. |
| 55 | pub TraitObject(TyTraitObject { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | pub bounds: Delimited<TyParamBound, tokens::Add>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 57 | }), |
| 58 | /// An `impl Bound1 + Bound2 + Bound3` type |
| 59 | /// where `Bound` is a trait or a lifetime. |
| 60 | pub ImplTrait(TyImplTrait { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 61 | pub impl_token: tokens::Impl, |
| 62 | pub bounds: Delimited<TyParamBound, tokens::Add>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 63 | }), |
| 64 | /// No-op; kept solely so that we can pretty-print faithfully |
| 65 | pub Paren(TyParen { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 67 | pub ty: Box<Ty>, |
| 68 | }), |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 69 | /// No-op: kept solely so that we can pretty-print faithfully |
| 70 | pub Group(TyGroup { |
| 71 | pub group_token: tokens::Group, |
| 72 | pub ty: Box<Ty>, |
| 73 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | /// TyKind::Infer means the type should be inferred instead of it having been |
| 75 | /// specified. This can appear anywhere in a type. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 76 | pub Infer(TyInfer { |
| 77 | pub underscore_token: tokens::Underscore |
| 78 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 79 | /// A macro in the type position. |
| 80 | pub Mac(Mac), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ast_struct! { |
| 85 | pub struct MutTy { |
| 86 | pub ty: Ty, |
| 87 | pub mutability: Mutability, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 92 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 93 | pub enum Mutability { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 94 | Mutable(tokens::Mut), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 95 | Immutable, |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | ast_struct! { |
| 100 | /// A "Path" is essentially Rust's notion of a name. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 101 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 102 | /// It's represented as a sequence of identifiers, |
| 103 | /// along with a bunch of supporting information. |
| 104 | /// |
| 105 | /// E.g. `std::cmp::PartialEq` |
| 106 | pub struct Path { |
| 107 | /// A `::foo` path, is relative to the crate root rather than current |
| 108 | /// module (like paths in an import). |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 109 | pub leading_colon: Option<tokens::Colon2>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 110 | /// The segments in the path: the things separated by `::`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | pub segments: Delimited<PathSegment, tokens::Colon2>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 112 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 115 | impl Path { |
| 116 | pub fn global(&self) -> bool { |
| 117 | self.leading_colon.is_some() |
| 118 | } |
| 119 | } |
| 120 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | #[cfg(feature = "printing")] |
| 122 | ast_struct! { |
| 123 | pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path); |
| 124 | } |
| 125 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 126 | impl<T> From<T> for Path |
| 127 | where T: Into<PathSegment> |
| 128 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 129 | fn from(segment: T) -> Self { |
| 130 | Path { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 131 | leading_colon: None, |
| 132 | segments: vec![(segment.into(), None)].into(), |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 137 | ast_struct! { |
| 138 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 139 | /// |
| 140 | /// E.g. `std`, `String` or `Box<T>` |
| 141 | pub struct PathSegment { |
| 142 | /// The identifier portion of this path segment. |
| 143 | pub ident: Ident, |
| 144 | /// Type/lifetime parameters attached to this path. They come in |
| 145 | /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that |
| 146 | /// this is more than just simple syntactic sugar; the use of |
| 147 | /// parens affects the region binding rules, so we preserve the |
| 148 | /// distinction. |
| 149 | pub parameters: PathParameters, |
| 150 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 151 | } |
| 152 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 153 | impl<T> From<T> for PathSegment |
| 154 | where T: Into<Ident> |
| 155 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 156 | fn from(ident: T) -> Self { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 157 | PathSegment { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 158 | ident: ident.into(), |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 159 | parameters: PathParameters::None, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 164 | ast_enum! { |
| 165 | /// Parameters of a path segment. |
| 166 | /// |
| 167 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
| 168 | pub enum PathParameters { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 169 | None, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 170 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
| 171 | AngleBracketed(AngleBracketedParameterData), |
| 172 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
| 173 | Parenthesized(ParenthesizedParameterData), |
| 174 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 177 | impl Default for PathParameters { |
| 178 | fn default() -> Self { |
| 179 | PathParameters::None |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 180 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 181 | } |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 182 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 183 | impl PathParameters { |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 184 | pub fn is_empty(&self) -> bool { |
| 185 | match *self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 186 | PathParameters::None => true, |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 187 | PathParameters::AngleBracketed(ref bracketed) => { |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 188 | bracketed.lifetimes.is_empty() && bracketed.types.is_empty() && |
| 189 | bracketed.bindings.is_empty() |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 190 | } |
| 191 | PathParameters::Parenthesized(_) => false, |
| 192 | } |
| 193 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 196 | ast_struct! { |
| 197 | /// A path like `Foo<'a, T>` |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 198 | pub struct AngleBracketedParameterData { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 199 | pub turbofish: Option<tokens::Colon2>, |
| 200 | pub lt_token: tokens::Lt, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 201 | /// The lifetime parameters for this path segment. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 202 | pub lifetimes: Delimited<Lifetime, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 203 | /// The type parameters for this path segment, if present. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 204 | pub types: Delimited<Ty, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 205 | /// Bindings (equality constraints) on associated types, if present. |
| 206 | /// |
| 207 | /// E.g., `Foo<A=Bar>`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 208 | pub bindings: Delimited<TypeBinding, tokens::Comma>, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 209 | pub gt_token: tokens::Gt, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | ast_struct! { |
| 214 | /// Bind a type to an associated type: `A=Foo`. |
| 215 | pub struct TypeBinding { |
| 216 | pub ident: Ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 217 | pub eq_token: tokens::Eq, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 218 | pub ty: Ty, |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | ast_struct! { |
| 224 | /// A path like `Foo(A,B) -> C` |
| 225 | pub struct ParenthesizedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 226 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 227 | /// `(A, B)` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 228 | pub inputs: Delimited<Ty, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 229 | /// `C` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 230 | pub output: FunctionRetTy, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | ast_struct! { |
| 235 | pub struct PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 236 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 237 | pub bound_lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 238 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
| 239 | pub trait_ref: Path, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | ast_struct! { |
| 244 | /// The explicit Self type in a "qualified path". The actual |
| 245 | /// path, including the trait and the associated item, is stored |
| 246 | /// separately. `position` represents the index of the associated |
| 247 | /// item qualified with this Self type. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 248 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 249 | /// ```rust,ignore |
| 250 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 251 | /// ^~~~~ ~~~~~~~~~~~~~~^ |
| 252 | /// ty position = 3 |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 253 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 254 | /// <Vec<T>>::AssociatedItem |
| 255 | /// ^~~~~ ^ |
| 256 | /// ty position = 0 |
| 257 | /// ``` |
| 258 | pub struct QSelf { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 259 | pub lt_token: tokens::Lt, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 260 | pub ty: Box<Ty>, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 261 | pub position: Option<(tokens::As, usize)>, |
| 262 | pub gt_token: tokens::Gt, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | ast_struct! { |
| 267 | pub struct BareFnTy { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 268 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 269 | pub unsafety: Unsafety, |
| 270 | pub abi: Option<Abi>, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 271 | pub fn_token: tokens::Fn_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 272 | pub paren_token: tokens::Paren, |
| 273 | pub inputs: Delimited<BareFnArg, tokens::Comma>, |
| 274 | pub variadic: Option<tokens::Dot3>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 275 | pub output: FunctionRetTy, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 280 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 281 | pub enum Unsafety { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 282 | Unsafe(tokens::Unsafe), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | Normal, |
| 284 | } |
| 285 | } |
| 286 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 287 | ast_struct! { |
| 288 | pub struct Abi { |
| 289 | pub extern_token: tokens::Extern, |
| 290 | pub kind: AbiKind, |
| 291 | } |
| 292 | } |
| 293 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 294 | ast_enum! { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 295 | pub enum AbiKind { |
| 296 | Named(Lit), |
| 297 | Default, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | ast_struct! { |
| 302 | /// An argument in a function type. |
| 303 | /// |
| 304 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 305 | pub struct BareFnArg { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 306 | pub name: Option<(Ident, tokens::Colon)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 307 | pub ty: Ty, |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | ast_enum! { |
| 313 | pub enum FunctionRetTy { |
| 314 | /// Return type is not specified. |
| 315 | /// |
| 316 | /// Functions default to `()` and |
| 317 | /// closures default to inference. Span points to where return |
| 318 | /// type would be inserted. |
| 319 | Default, |
| 320 | /// Everything else |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 321 | Ty(Ty, tokens::RArrow), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 322 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 323 | } |
| 324 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 325 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 326 | pub mod parsing { |
| 327 | use super::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 328 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 329 | use synom::tokens::*; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 330 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 331 | impl Synom for Ty { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 332 | named!(parse -> Self, call!(ambig_ty, true)); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 333 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 334 | fn description() -> Option<&'static str> { |
| 335 | Some("type") |
| 336 | } |
| 337 | } |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 338 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 339 | impl Ty { |
| 340 | /// In some positions, types may not contain the `+` character, to |
| 341 | /// disambiguate them. For example in the expression `1 as T`, T may not |
| 342 | /// contain a `+` character. |
| 343 | /// |
| 344 | /// This parser does not allow a `+`, while the default parser does. |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 345 | named!(pub without_plus -> Self, call!(ambig_ty, false)); |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | named!(ambig_ty(allow_plus: bool) -> Ty, alt!( |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 349 | syn!(TyGroup) => { Ty::Group } |
| 350 | | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 351 | // must be before mac |
| 352 | syn!(TyParen) => { Ty::Paren } |
| 353 | | |
| 354 | // must be before path |
| 355 | syn!(Mac) => { Ty::Mac } |
| 356 | | |
| 357 | // must be before ty_poly_trait_ref |
| 358 | call!(ty_path, allow_plus) |
| 359 | | |
| 360 | syn!(TySlice) => { Ty::Slice } |
| 361 | | |
| 362 | syn!(TyArray) => { Ty::Array } |
| 363 | | |
| 364 | syn!(TyPtr) => { Ty::Ptr } |
| 365 | | |
| 366 | syn!(TyRptr) => { Ty::Rptr } |
| 367 | | |
| 368 | syn!(TyBareFn) => { Ty::BareFn } |
| 369 | | |
| 370 | syn!(TyNever) => { Ty::Never } |
| 371 | | |
| 372 | syn!(TyTup) => { Ty::Tup } |
| 373 | | |
| 374 | // Don't try parsing poly_trait_ref if we aren't allowing it |
| 375 | call!(ty_poly_trait_ref, allow_plus) |
| 376 | | |
| 377 | syn!(TyImplTrait) => { Ty::ImplTrait } |
| 378 | )); |
| 379 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 380 | impl Synom for TySlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 381 | named!(parse -> Self, map!( |
| 382 | brackets!(syn!(Ty)), |
| 383 | |(ty, b)| TySlice { |
| 384 | ty: Box::new(ty), |
| 385 | bracket_token: b, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 386 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 387 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 388 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 389 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 390 | impl Synom for TyArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 391 | named!(parse -> Self, map!( |
| 392 | brackets!(do_parse!( |
| 393 | elem: syn!(Ty) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 394 | semi: syn!(Semi) >> |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame^] | 395 | len: syn!(Expr) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 396 | (elem, semi, len) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 397 | )), |
| 398 | |((elem, semi, len), brackets)| { |
| 399 | TyArray { |
| 400 | ty: Box::new(elem), |
| 401 | amt: len, |
| 402 | bracket_token: brackets, |
| 403 | semi_token: semi, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 406 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 407 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 408 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 409 | impl Synom for TyPtr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 410 | named!(parse -> Self, do_parse!( |
| 411 | star: syn!(Star) >> |
| 412 | mutability: alt!( |
| 413 | syn!(Const) => { |c| (Mutability::Immutable, Some(c)) } |
| 414 | | |
| 415 | syn!(Mut) => { |m| (Mutability::Mutable(m), None) } |
| 416 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 417 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 418 | (TyPtr { |
| 419 | const_token: mutability.1, |
| 420 | star_token: star, |
| 421 | ty: Box::new(MutTy { |
| 422 | ty: target, |
| 423 | mutability: mutability.0, |
| 424 | }), |
| 425 | }) |
| 426 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 427 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 428 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 429 | impl Synom for TyRptr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 430 | named!(parse -> Self, do_parse!( |
| 431 | amp: syn!(And) >> |
| 432 | life: option!(syn!(Lifetime)) >> |
| 433 | mutability: syn!(Mutability) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 434 | // & binds tighter than +, so we don't allow + here. |
| 435 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 436 | (TyRptr { |
| 437 | lifetime: life, |
| 438 | ty: Box::new(MutTy { |
| 439 | ty: target, |
| 440 | mutability: mutability, |
| 441 | }), |
| 442 | and_token: amp, |
| 443 | }) |
| 444 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 445 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 446 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 447 | impl Synom for TyBareFn { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 448 | named!(parse -> Self, do_parse!( |
| 449 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 450 | unsafety: syn!(Unsafety) >> |
| 451 | abi: option!(syn!(Abi)) >> |
| 452 | fn_: syn!(Fn_) >> |
| 453 | parens: parens!(do_parse!( |
| 454 | inputs: call!(Delimited::parse_terminated) >> |
| 455 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
| 456 | syn!(Dot3))) >> |
| 457 | (inputs, variadic) |
| 458 | )) >> |
| 459 | output: syn!(FunctionRetTy) >> |
| 460 | (TyBareFn { |
| 461 | ty: Box::new(BareFnTy { |
| 462 | unsafety: unsafety, |
| 463 | abi: abi, |
| 464 | lifetimes: lifetimes, |
| 465 | output: output, |
| 466 | variadic: (parens.0).1, |
| 467 | fn_token: fn_, |
| 468 | paren_token: parens.1, |
| 469 | inputs: (parens.0).0, |
| 470 | }), |
| 471 | }) |
| 472 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 473 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 474 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 475 | impl Synom for TyNever { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 476 | named!(parse -> Self, map!( |
| 477 | syn!(Bang), |
| 478 | |b| TyNever { bang_token: b } |
| 479 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 480 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 481 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 482 | impl Synom for TyTup { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 483 | named!(parse -> Self, do_parse!( |
| 484 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 485 | (TyTup { |
| 486 | tys: data.0, |
| 487 | paren_token: data.1, |
| 488 | lone_comma: None, // TODO: does this just not parse? |
| 489 | }) |
| 490 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 491 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 492 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 493 | named!(ty_path(allow_plus: bool) -> Ty, do_parse!( |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 494 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 495 | parenthesized: cond!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 496 | qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 497 | option!(syn!(ParenthesizedParameterData)) |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 498 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 499 | // Only allow parsing additional bounds if allow_plus is true. |
| 500 | bounds: alt!( |
| 501 | cond_reduce!( |
| 502 | allow_plus, |
| 503 | many0!(tuple!(syn!(Add), syn!(TyParamBound))) |
| 504 | ) |
| 505 | | |
| 506 | value!(vec![]) |
| 507 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 508 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 509 | let (qself, mut path) = qpath; |
| 510 | if let Some(Some(parenthesized)) = parenthesized { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 511 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 512 | let len = path.segments.len(); |
| 513 | path.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 514 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 515 | if bounds.is_empty() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 516 | TyPath { qself: qself, path: path }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 517 | } else { |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 518 | let path = TyParamBound::Trait( |
| 519 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 520 | bound_lifetimes: None, |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 521 | trait_ref: path, |
| 522 | }, |
| 523 | TraitBoundModifier::None, |
| 524 | ); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 525 | let mut new_bounds = Delimited::new(); |
| 526 | new_bounds.push_first(path); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 527 | for (_tok, bound) in bounds { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 528 | new_bounds.push_default(bound); |
| 529 | } |
| 530 | TyTraitObject { bounds: new_bounds }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 531 | } |
| 532 | }) |
| 533 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 534 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 535 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 536 | map!(syn!(Path), |p| (None, p)) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 537 | | |
| 538 | do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 539 | lt: syn!(Lt) >> |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 540 | this: syn!(Ty) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 541 | path: option!(do_parse!( |
| 542 | as_: syn!(As) >> |
| 543 | path: syn!(Path) >> |
| 544 | (as_, path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 545 | )) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 546 | gt: syn!(Gt) >> |
| 547 | colon2: syn!(Colon2) >> |
| 548 | rest: call!(Delimited::parse_separated_nonempty) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 549 | ({ |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 550 | let (pos, path) = match path { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 551 | Some((as_, mut path)) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 552 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 553 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 554 | path.segments.push_trailing(colon2); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 555 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 556 | for item in rest { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 557 | path.segments.push(item); |
| 558 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 559 | (Some((as_, pos)), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 560 | } |
| 561 | None => { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 562 | (None, Path { |
| 563 | leading_colon: Some(colon2), |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 564 | segments: rest, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 565 | }) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 566 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 567 | }; |
| 568 | (Some(QSelf { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 569 | lt_token: lt, |
| 570 | ty: Box::new(this), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 571 | position: pos, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 572 | gt_token: gt, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 573 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 574 | }) |
| 575 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 576 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 577 | map!(syn!(Self_), |s| (None, s.into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 578 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 579 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 580 | impl Synom for ParenthesizedParameterData { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 581 | named!(parse -> Self, do_parse!( |
| 582 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 583 | output: syn!(FunctionRetTy) >> |
| 584 | (ParenthesizedParameterData { |
| 585 | paren_token: data.1, |
| 586 | inputs: data.0, |
| 587 | output: output, |
| 588 | }) |
| 589 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | impl Synom for FunctionRetTy { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 593 | named!(parse -> Self, alt!( |
| 594 | do_parse!( |
| 595 | arrow: syn!(RArrow) >> |
| 596 | ty: syn!(Ty) >> |
| 597 | (FunctionRetTy::Ty(ty, arrow)) |
| 598 | ) |
| 599 | | |
| 600 | epsilon!() => { |_| FunctionRetTy::Default } |
| 601 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 604 | // Only allow multiple trait references if allow_plus is true. |
| 605 | named!(ty_poly_trait_ref(allow_plus: bool) -> Ty, alt!( |
| 606 | cond_reduce!(allow_plus, call!(Delimited::parse_separated_nonempty)) => { |
| 607 | |x| TyTraitObject { bounds: x }.into() |
| 608 | } |
| 609 | | |
| 610 | syn!(TyParamBound) => { |
| 611 | |x| TyTraitObject { bounds: vec![x].into() }.into() |
| 612 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 613 | )); |
| 614 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 615 | impl Synom for TyImplTrait { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 616 | named!(parse -> Self, do_parse!( |
| 617 | impl_: syn!(Impl) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 618 | // NOTE: rust-lang/rust#34511 includes discussion about whether or |
| 619 | // not + should be allowed in ImplTrait directly without (). |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 620 | elem: call!(Delimited::parse_separated_nonempty) >> |
| 621 | (TyImplTrait { |
| 622 | impl_token: impl_, |
| 623 | bounds: elem, |
| 624 | }) |
| 625 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 626 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 627 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 628 | impl Synom for TyGroup { |
| 629 | named!(parse -> Self, do_parse!( |
| 630 | data: grouped!(syn!(Ty)) >> |
| 631 | (TyGroup { |
| 632 | group_token: data.1, |
| 633 | ty: Box::new(data.0), |
| 634 | }) |
| 635 | )); |
| 636 | } |
| 637 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 638 | impl Synom for TyParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 639 | named!(parse -> Self, do_parse!( |
| 640 | data: parens!(syn!(Ty)) >> |
| 641 | (TyParen { |
| 642 | paren_token: data.1, |
| 643 | ty: Box::new(data.0), |
| 644 | }) |
| 645 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 646 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 647 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 648 | impl Synom for Mutability { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 649 | named!(parse -> Self, alt!( |
| 650 | syn!(Mut) => { Mutability::Mutable } |
| 651 | | |
| 652 | epsilon!() => { |_| Mutability::Immutable } |
| 653 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 654 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 655 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 656 | impl Synom for Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 657 | named!(parse -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 658 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 659 | segments: call!(Delimited::parse_separated_nonempty) >> |
| 660 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 661 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 662 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 663 | }) |
| 664 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 665 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 666 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 667 | impl Synom for PathSegment { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 668 | named!(parse -> Self, alt!( |
| 669 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 670 | ident: syn!(Ident) >> |
| 671 | turbofish: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 672 | lt: syn!(Lt) >> |
| 673 | lifetimes: call!(Delimited::parse_terminated) >> |
| 674 | types: cond!( |
| 675 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 676 | call!(Delimited::parse_terminated_with, |
| 677 | ty_no_eq_after) |
| 678 | ) >> |
| 679 | bindings: cond!( |
| 680 | match types { |
| 681 | Some(ref t) => t.is_empty() || t.trailing_delim(), |
| 682 | None => lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 683 | }, |
| 684 | call!(Delimited::parse_terminated) |
| 685 | ) >> |
| 686 | gt: syn!(Gt) >> |
| 687 | (PathSegment { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 688 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 689 | parameters: PathParameters::AngleBracketed( |
| 690 | AngleBracketedParameterData { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 691 | turbofish: turbofish, |
| 692 | lt_token: lt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 693 | lifetimes: lifetimes, |
| 694 | types: types.unwrap_or_default(), |
| 695 | bindings: bindings.unwrap_or_default(), |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 696 | gt_token: gt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 697 | } |
| 698 | ), |
| 699 | }) |
| 700 | ) |
| 701 | | |
| 702 | mod_style_path_segment |
| 703 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 704 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 705 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 706 | named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq)))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 707 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 708 | impl Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 709 | named!(pub parse_mod_style -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 710 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 711 | segments: call!(Delimited::parse_separated_nonempty_with, |
| 712 | mod_style_path_segment) >> |
| 713 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 714 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 715 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 716 | }) |
| 717 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 718 | } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 719 | |
| 720 | named!(mod_style_path_segment -> PathSegment, alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 721 | map!(syn!(Ident), Into::into) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 722 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 723 | alt!( |
| 724 | syn!(Super) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 725 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 726 | syn!(Self_) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 727 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 728 | syn!(CapSelf) => { Into::into } |
| 729 | ) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 730 | )); |
| 731 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 732 | impl Synom for TypeBinding { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 733 | named!(parse -> Self, do_parse!( |
| 734 | id: syn!(Ident) >> |
| 735 | eq: syn!(Eq) >> |
| 736 | ty: syn!(Ty) >> |
| 737 | (TypeBinding { |
| 738 | ident: id, |
| 739 | eq_token: eq, |
| 740 | ty: ty, |
| 741 | }) |
| 742 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | impl Synom for PolyTraitRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 746 | named!(parse -> Self, do_parse!( |
| 747 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
| 748 | trait_ref: syn!(Path) >> |
| 749 | parenthesized: option!(cond_reduce!( |
| 750 | trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(), |
| 751 | syn!(ParenthesizedParameterData) |
| 752 | )) >> |
| 753 | ({ |
| 754 | let mut trait_ref = trait_ref; |
| 755 | if let Some(parenthesized) = parenthesized { |
| 756 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
| 757 | let len = trait_ref.segments.len(); |
| 758 | trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
| 759 | } |
| 760 | PolyTraitRef { |
| 761 | bound_lifetimes: bound_lifetimes, |
| 762 | trait_ref: trait_ref, |
| 763 | } |
| 764 | }) |
| 765 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 766 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 767 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 768 | impl Synom for BareFnArg { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 769 | named!(parse -> Self, do_parse!( |
| 770 | name: option!(do_parse!( |
| 771 | name: syn!(Ident) >> |
| 772 | not!(syn!(Colon2)) >> |
| 773 | colon: syn!(Colon) >> |
| 774 | (name, colon) |
| 775 | )) >> |
| 776 | ty: syn!(Ty) >> |
| 777 | (BareFnArg { |
| 778 | name: name, |
| 779 | ty: ty, |
| 780 | }) |
| 781 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 782 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 783 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 784 | impl Synom for Unsafety { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 785 | named!(parse -> Self, alt!( |
| 786 | syn!(Unsafe) => { Unsafety::Unsafe } |
| 787 | | |
| 788 | epsilon!() => { |_| Unsafety::Normal } |
| 789 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 790 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 791 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 792 | impl Synom for Abi { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 793 | named!(parse -> Self, do_parse!( |
| 794 | extern_: syn!(Extern) >> |
| 795 | // TODO: this parses all literals, not just strings |
| 796 | name: option!(syn!(Lit)) >> |
| 797 | (Abi { |
| 798 | extern_token: extern_, |
| 799 | kind: match name { |
| 800 | Some(name) => AbiKind::Named(name), |
| 801 | None => AbiKind::Default, |
| 802 | }, |
| 803 | }) |
| 804 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 805 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 806 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 807 | |
| 808 | #[cfg(feature = "printing")] |
| 809 | mod printing { |
| 810 | use super::*; |
| 811 | use quote::{Tokens, ToTokens}; |
| 812 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 813 | impl ToTokens for TySlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 814 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 815 | self.bracket_token.surround(tokens, |tokens| { |
| 816 | self.ty.to_tokens(tokens); |
| 817 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | impl ToTokens for TyArray { |
| 822 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 823 | self.bracket_token.surround(tokens, |tokens| { |
| 824 | self.ty.to_tokens(tokens); |
| 825 | self.semi_token.to_tokens(tokens); |
| 826 | self.amt.to_tokens(tokens); |
| 827 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | impl ToTokens for TyPtr { |
| 832 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 833 | self.star_token.to_tokens(tokens); |
| 834 | self.const_token.to_tokens(tokens); |
| 835 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 836 | self.ty.ty.to_tokens(tokens); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | impl ToTokens for TyRptr { |
| 841 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 842 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 843 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 844 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 845 | self.ty.ty.to_tokens(tokens); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | impl ToTokens for TyBareFn { |
| 850 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 851 | self.ty.to_tokens(tokens) |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | impl ToTokens for TyNever { |
| 856 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 857 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
| 861 | impl ToTokens for TyTup { |
| 862 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 863 | self.paren_token.surround(tokens, |tokens| { |
| 864 | self.tys.to_tokens(tokens); |
| 865 | self.lone_comma.to_tokens(tokens); |
| 866 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
| 870 | impl ToTokens for TyPath { |
| 871 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 872 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | impl<'a> ToTokens for PathTokens<'a> { |
| 877 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 878 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 879 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 880 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 881 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 882 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 883 | qself.ty.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 884 | let mut segments = self.1.segments.iter(); |
David Tolnay | b99e1b0 | 2017-06-03 19:00:55 -0700 | [diff] [blame] | 885 | if let Some((ref as_token, pos)) = qself.position { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 886 | as_token.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 887 | self.1.leading_colon.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 888 | for (i, segment) in (&mut segments).take(pos).enumerate() { |
| 889 | if i + 1 == pos { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 890 | segment.item().to_tokens(tokens); |
| 891 | qself.gt_token.to_tokens(tokens); |
| 892 | segment.delimiter().to_tokens(tokens); |
| 893 | } else { |
| 894 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 895 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 896 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 897 | } else { |
| 898 | qself.gt_token.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 899 | self.1.leading_colon.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 900 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 901 | for segment in segments { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 902 | segment.to_tokens(tokens); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | impl ToTokens for TyTraitObject { |
| 908 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 909 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | impl ToTokens for TyImplTrait { |
| 914 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 915 | self.impl_token.to_tokens(tokens); |
| 916 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 920 | impl ToTokens for TyGroup { |
| 921 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 922 | self.group_token.surround(tokens, |tokens| { |
| 923 | self.ty.to_tokens(tokens); |
| 924 | }); |
| 925 | } |
| 926 | } |
| 927 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 928 | impl ToTokens for TyParen { |
| 929 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 930 | self.paren_token.surround(tokens, |tokens| { |
| 931 | self.ty.to_tokens(tokens); |
| 932 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
| 936 | impl ToTokens for TyInfer { |
| 937 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 938 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 942 | impl ToTokens for Mutability { |
| 943 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 944 | if let Mutability::Mutable(ref t) = *self { |
| 945 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 950 | impl ToTokens for Path { |
| 951 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 952 | self.leading_colon.to_tokens(tokens); |
| 953 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | impl ToTokens for PathSegment { |
| 958 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 959 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 960 | self.parameters.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| 964 | impl ToTokens for PathParameters { |
| 965 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 966 | match *self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 967 | PathParameters::None => {} |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 968 | PathParameters::AngleBracketed(ref parameters) => { |
| 969 | parameters.to_tokens(tokens); |
| 970 | } |
| 971 | PathParameters::Parenthesized(ref parameters) => { |
| 972 | parameters.to_tokens(tokens); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | impl ToTokens for AngleBracketedParameterData { |
| 979 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 980 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 981 | self.lt_token.to_tokens(tokens); |
| 982 | self.lifetimes.to_tokens(tokens); |
| 983 | self.types.to_tokens(tokens); |
| 984 | self.bindings.to_tokens(tokens); |
| 985 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | impl ToTokens for TypeBinding { |
| 990 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 991 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 992 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 993 | self.ty.to_tokens(tokens); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | impl ToTokens for ParenthesizedParameterData { |
| 998 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 999 | self.paren_token.surround(tokens, |tokens| { |
| 1000 | self.inputs.to_tokens(tokens); |
| 1001 | }); |
| 1002 | self.output.to_tokens(tokens); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | impl ToTokens for FunctionRetTy { |
| 1007 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1008 | match *self { |
| 1009 | FunctionRetTy::Default => {} |
| 1010 | FunctionRetTy::Ty(ref ty, ref arrow) => { |
| 1011 | arrow.to_tokens(tokens); |
| 1012 | ty.to_tokens(tokens); |
| 1013 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | impl ToTokens for PolyTraitRef { |
| 1019 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1020 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1021 | self.trait_ref.to_tokens(tokens); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | impl ToTokens for BareFnTy { |
| 1026 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1027 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1028 | self.unsafety.to_tokens(tokens); |
| 1029 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1030 | self.fn_token.to_tokens(tokens); |
| 1031 | self.paren_token.surround(tokens, |tokens| { |
| 1032 | self.inputs.to_tokens(tokens); |
| 1033 | self.variadic.to_tokens(tokens); |
| 1034 | }); |
| 1035 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1039 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1040 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1041 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1042 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1043 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1044 | } |
| 1045 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1048 | |
| 1049 | impl ToTokens for Unsafety { |
| 1050 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1051 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1052 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1053 | Unsafety::Normal => { |
| 1054 | // nothing |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | impl ToTokens for Abi { |
| 1061 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1062 | self.extern_token.to_tokens(tokens); |
| 1063 | match self.kind { |
| 1064 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1065 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1069 | } |