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. |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 80 | pub Macro(Macro), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 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")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 122 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 123 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 124 | pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 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>, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 261 | pub position: usize, |
| 262 | pub as_token: Option<tokens::As>, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 263 | pub gt_token: tokens::Gt, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | ast_struct! { |
| 268 | pub struct BareFnTy { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 269 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 270 | pub unsafety: Unsafety, |
| 271 | pub abi: Option<Abi>, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 272 | pub fn_token: tokens::Fn_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 273 | pub paren_token: tokens::Paren, |
| 274 | pub inputs: Delimited<BareFnArg, tokens::Comma>, |
| 275 | pub variadic: Option<tokens::Dot3>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 276 | pub output: FunctionRetTy, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 281 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 282 | pub enum Unsafety { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 283 | Unsafe(tokens::Unsafe), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 284 | Normal, |
| 285 | } |
| 286 | } |
| 287 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 288 | ast_struct! { |
| 289 | pub struct Abi { |
| 290 | pub extern_token: tokens::Extern, |
| 291 | pub kind: AbiKind, |
| 292 | } |
| 293 | } |
| 294 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 295 | ast_enum! { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 296 | pub enum AbiKind { |
| 297 | Named(Lit), |
| 298 | Default, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | ast_struct! { |
| 303 | /// An argument in a function type. |
| 304 | /// |
| 305 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 306 | pub struct BareFnArg { |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 307 | pub name: Option<(BareFnArgName, tokens::Colon)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 308 | pub ty: Ty, |
| 309 | } |
| 310 | } |
| 311 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 312 | ast_enum! { |
| 313 | /// Names of arguments in the `BareFnArg` structure |
| 314 | pub enum BareFnArgName { |
| 315 | /// Argument with the provided name |
| 316 | Named(Ident), |
| 317 | /// Argument matched with `_` |
| 318 | Wild(tokens::Underscore), |
| 319 | } |
| 320 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 321 | |
| 322 | ast_enum! { |
| 323 | pub enum FunctionRetTy { |
| 324 | /// Return type is not specified. |
| 325 | /// |
| 326 | /// Functions default to `()` and |
| 327 | /// closures default to inference. Span points to where return |
| 328 | /// type would be inserted. |
| 329 | Default, |
| 330 | /// Everything else |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 331 | Ty(Ty, tokens::RArrow), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 332 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 333 | } |
| 334 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 335 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 336 | pub mod parsing { |
| 337 | use super::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 338 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 339 | use synom::tokens::*; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 340 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 341 | impl Synom for Ty { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 342 | named!(parse -> Self, call!(ambig_ty, true)); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 343 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 344 | fn description() -> Option<&'static str> { |
| 345 | Some("type") |
| 346 | } |
| 347 | } |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 348 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 349 | impl Ty { |
| 350 | /// In some positions, types may not contain the `+` character, to |
| 351 | /// disambiguate them. For example in the expression `1 as T`, T may not |
| 352 | /// contain a `+` character. |
| 353 | /// |
| 354 | /// This parser does not allow a `+`, while the default parser does. |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 355 | named!(pub without_plus -> Self, call!(ambig_ty, false)); |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | named!(ambig_ty(allow_plus: bool) -> Ty, alt!( |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 359 | syn!(TyGroup) => { Ty::Group } |
| 360 | | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 361 | // must be before mac |
| 362 | syn!(TyParen) => { Ty::Paren } |
| 363 | | |
| 364 | // must be before path |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 365 | syn!(Macro) => { Ty::Macro } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 366 | | |
| 367 | // must be before ty_poly_trait_ref |
| 368 | call!(ty_path, allow_plus) |
| 369 | | |
| 370 | syn!(TySlice) => { Ty::Slice } |
| 371 | | |
| 372 | syn!(TyArray) => { Ty::Array } |
| 373 | | |
| 374 | syn!(TyPtr) => { Ty::Ptr } |
| 375 | | |
| 376 | syn!(TyRptr) => { Ty::Rptr } |
| 377 | | |
| 378 | syn!(TyBareFn) => { Ty::BareFn } |
| 379 | | |
| 380 | syn!(TyNever) => { Ty::Never } |
| 381 | | |
| 382 | syn!(TyTup) => { Ty::Tup } |
| 383 | | |
| 384 | // Don't try parsing poly_trait_ref if we aren't allowing it |
| 385 | call!(ty_poly_trait_ref, allow_plus) |
| 386 | | |
| 387 | syn!(TyImplTrait) => { Ty::ImplTrait } |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 388 | | |
| 389 | syn!(TyInfer) => { Ty::Infer } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 390 | )); |
| 391 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 392 | impl Synom for TySlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 393 | named!(parse -> Self, map!( |
| 394 | brackets!(syn!(Ty)), |
| 395 | |(ty, b)| TySlice { |
| 396 | ty: Box::new(ty), |
| 397 | bracket_token: b, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 398 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 399 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 400 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 401 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 402 | impl Synom for TyArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 403 | named!(parse -> Self, map!( |
| 404 | brackets!(do_parse!( |
| 405 | elem: syn!(Ty) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 406 | semi: syn!(Semi) >> |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame] | 407 | len: syn!(Expr) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 408 | (elem, semi, len) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 409 | )), |
| 410 | |((elem, semi, len), brackets)| { |
| 411 | TyArray { |
| 412 | ty: Box::new(elem), |
| 413 | amt: len, |
| 414 | bracket_token: brackets, |
| 415 | semi_token: semi, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 418 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 419 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 420 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 421 | impl Synom for TyPtr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 422 | named!(parse -> Self, do_parse!( |
| 423 | star: syn!(Star) >> |
| 424 | mutability: alt!( |
| 425 | syn!(Const) => { |c| (Mutability::Immutable, Some(c)) } |
| 426 | | |
| 427 | syn!(Mut) => { |m| (Mutability::Mutable(m), None) } |
| 428 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 429 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 430 | (TyPtr { |
| 431 | const_token: mutability.1, |
| 432 | star_token: star, |
| 433 | ty: Box::new(MutTy { |
| 434 | ty: target, |
| 435 | mutability: mutability.0, |
| 436 | }), |
| 437 | }) |
| 438 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 439 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 440 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 441 | impl Synom for TyRptr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 442 | named!(parse -> Self, do_parse!( |
| 443 | amp: syn!(And) >> |
| 444 | life: option!(syn!(Lifetime)) >> |
| 445 | mutability: syn!(Mutability) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 446 | // & binds tighter than +, so we don't allow + here. |
| 447 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 448 | (TyRptr { |
| 449 | lifetime: life, |
| 450 | ty: Box::new(MutTy { |
| 451 | ty: target, |
| 452 | mutability: mutability, |
| 453 | }), |
| 454 | and_token: amp, |
| 455 | }) |
| 456 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 457 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 458 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 459 | impl Synom for TyBareFn { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 460 | named!(parse -> Self, do_parse!( |
| 461 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 462 | unsafety: syn!(Unsafety) >> |
| 463 | abi: option!(syn!(Abi)) >> |
| 464 | fn_: syn!(Fn_) >> |
| 465 | parens: parens!(do_parse!( |
| 466 | inputs: call!(Delimited::parse_terminated) >> |
| 467 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
| 468 | syn!(Dot3))) >> |
| 469 | (inputs, variadic) |
| 470 | )) >> |
| 471 | output: syn!(FunctionRetTy) >> |
| 472 | (TyBareFn { |
| 473 | ty: Box::new(BareFnTy { |
| 474 | unsafety: unsafety, |
| 475 | abi: abi, |
| 476 | lifetimes: lifetimes, |
| 477 | output: output, |
| 478 | variadic: (parens.0).1, |
| 479 | fn_token: fn_, |
| 480 | paren_token: parens.1, |
| 481 | inputs: (parens.0).0, |
| 482 | }), |
| 483 | }) |
| 484 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 485 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 486 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 487 | impl Synom for TyNever { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 488 | named!(parse -> Self, map!( |
| 489 | syn!(Bang), |
| 490 | |b| TyNever { bang_token: b } |
| 491 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 492 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 493 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 494 | impl Synom for TyInfer { |
| 495 | named!(parse -> Self, map!( |
| 496 | syn!(Underscore), |
| 497 | |u| TyInfer { underscore_token: u } |
| 498 | )); |
| 499 | } |
| 500 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 501 | impl Synom for TyTup { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 502 | named!(parse -> Self, do_parse!( |
| 503 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 504 | (TyTup { |
| 505 | tys: data.0, |
| 506 | paren_token: data.1, |
| 507 | lone_comma: None, // TODO: does this just not parse? |
| 508 | }) |
| 509 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 510 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 511 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 512 | named!(ty_path(allow_plus: bool) -> Ty, do_parse!( |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 513 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 514 | parenthesized: cond!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 515 | 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] | 516 | option!(syn!(ParenthesizedParameterData)) |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 517 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 518 | // Only allow parsing additional bounds if allow_plus is true. |
| 519 | bounds: alt!( |
| 520 | cond_reduce!( |
| 521 | allow_plus, |
| 522 | many0!(tuple!(syn!(Add), syn!(TyParamBound))) |
| 523 | ) |
| 524 | | |
| 525 | value!(vec![]) |
| 526 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 527 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 528 | let (qself, mut path) = qpath; |
| 529 | if let Some(Some(parenthesized)) = parenthesized { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 530 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 531 | let len = path.segments.len(); |
| 532 | path.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 533 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 534 | if bounds.is_empty() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 535 | TyPath { qself: qself, path: path }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 536 | } else { |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 537 | let path = TyParamBound::Trait( |
| 538 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 539 | bound_lifetimes: None, |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 540 | trait_ref: path, |
| 541 | }, |
| 542 | TraitBoundModifier::None, |
| 543 | ); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 544 | let mut new_bounds = Delimited::new(); |
| 545 | new_bounds.push_first(path); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 546 | for (_tok, bound) in bounds { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 547 | new_bounds.push_default(bound); |
| 548 | } |
| 549 | TyTraitObject { bounds: new_bounds }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 550 | } |
| 551 | }) |
| 552 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 553 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 554 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 555 | map!(syn!(Path), |p| (None, p)) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 556 | | |
| 557 | do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 558 | lt: syn!(Lt) >> |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 559 | this: syn!(Ty) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 560 | path: option!(do_parse!( |
| 561 | as_: syn!(As) >> |
| 562 | path: syn!(Path) >> |
| 563 | (as_, path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 564 | )) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 565 | gt: syn!(Gt) >> |
| 566 | colon2: syn!(Colon2) >> |
| 567 | rest: call!(Delimited::parse_separated_nonempty) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 568 | ({ |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 569 | let (pos, as_, path) = match path { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 570 | Some((as_, mut path)) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 571 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 572 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 573 | path.segments.push_trailing(colon2); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 574 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 575 | for item in rest { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 576 | path.segments.push(item); |
| 577 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 578 | (pos, Some(as_), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 579 | } |
| 580 | None => { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 581 | (0, None, Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 582 | leading_colon: Some(colon2), |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 583 | segments: rest, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 584 | }) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 585 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 586 | }; |
| 587 | (Some(QSelf { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 588 | lt_token: lt, |
| 589 | ty: Box::new(this), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 590 | position: pos, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 591 | as_token: as_, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 592 | gt_token: gt, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 593 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 594 | }) |
| 595 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 596 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 597 | map!(syn!(Self_), |s| (None, s.into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 598 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 599 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 600 | impl Synom for ParenthesizedParameterData { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 601 | named!(parse -> Self, do_parse!( |
| 602 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 603 | output: syn!(FunctionRetTy) >> |
| 604 | (ParenthesizedParameterData { |
| 605 | paren_token: data.1, |
| 606 | inputs: data.0, |
| 607 | output: output, |
| 608 | }) |
| 609 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | impl Synom for FunctionRetTy { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 613 | named!(parse -> Self, alt!( |
| 614 | do_parse!( |
| 615 | arrow: syn!(RArrow) >> |
| 616 | ty: syn!(Ty) >> |
| 617 | (FunctionRetTy::Ty(ty, arrow)) |
| 618 | ) |
| 619 | | |
| 620 | epsilon!() => { |_| FunctionRetTy::Default } |
| 621 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 624 | // Only allow multiple trait references if allow_plus is true. |
| 625 | named!(ty_poly_trait_ref(allow_plus: bool) -> Ty, alt!( |
| 626 | cond_reduce!(allow_plus, call!(Delimited::parse_separated_nonempty)) => { |
| 627 | |x| TyTraitObject { bounds: x }.into() |
| 628 | } |
| 629 | | |
| 630 | syn!(TyParamBound) => { |
| 631 | |x| TyTraitObject { bounds: vec![x].into() }.into() |
| 632 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 633 | )); |
| 634 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 635 | impl Synom for TyImplTrait { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 636 | named!(parse -> Self, do_parse!( |
| 637 | impl_: syn!(Impl) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 638 | // NOTE: rust-lang/rust#34511 includes discussion about whether or |
| 639 | // not + should be allowed in ImplTrait directly without (). |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 640 | elem: call!(Delimited::parse_separated_nonempty) >> |
| 641 | (TyImplTrait { |
| 642 | impl_token: impl_, |
| 643 | bounds: elem, |
| 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 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 648 | impl Synom for TyGroup { |
| 649 | named!(parse -> Self, do_parse!( |
| 650 | data: grouped!(syn!(Ty)) >> |
| 651 | (TyGroup { |
| 652 | group_token: data.1, |
| 653 | ty: Box::new(data.0), |
| 654 | }) |
| 655 | )); |
| 656 | } |
| 657 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 658 | impl Synom for TyParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 659 | named!(parse -> Self, do_parse!( |
| 660 | data: parens!(syn!(Ty)) >> |
| 661 | (TyParen { |
| 662 | paren_token: data.1, |
| 663 | ty: Box::new(data.0), |
| 664 | }) |
| 665 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 666 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 667 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 668 | impl Synom for Mutability { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 669 | named!(parse -> Self, alt!( |
| 670 | syn!(Mut) => { Mutability::Mutable } |
| 671 | | |
| 672 | epsilon!() => { |_| Mutability::Immutable } |
| 673 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 674 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 675 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 676 | impl Synom for Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 677 | named!(parse -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 678 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 679 | segments: call!(Delimited::parse_separated_nonempty) >> |
| 680 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 681 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 682 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 683 | }) |
| 684 | )); |
Alex Crichton | 36e91bf | 2017-07-06 14:59:56 -0700 | [diff] [blame] | 685 | |
| 686 | fn description() -> Option<&'static str> { |
| 687 | Some("path") |
| 688 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 689 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 690 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 691 | impl Synom for PathSegment { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 692 | named!(parse -> Self, alt!( |
| 693 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 694 | ident: syn!(Ident) >> |
| 695 | turbofish: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 696 | lt: syn!(Lt) >> |
| 697 | lifetimes: call!(Delimited::parse_terminated) >> |
| 698 | types: cond!( |
| 699 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 700 | call!(Delimited::parse_terminated_with, |
| 701 | ty_no_eq_after) |
| 702 | ) >> |
| 703 | bindings: cond!( |
| 704 | match types { |
| 705 | Some(ref t) => t.is_empty() || t.trailing_delim(), |
| 706 | None => lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 707 | }, |
| 708 | call!(Delimited::parse_terminated) |
| 709 | ) >> |
| 710 | gt: syn!(Gt) >> |
| 711 | (PathSegment { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 712 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 713 | parameters: PathParameters::AngleBracketed( |
| 714 | AngleBracketedParameterData { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 715 | turbofish: turbofish, |
| 716 | lt_token: lt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 717 | lifetimes: lifetimes, |
| 718 | types: types.unwrap_or_default(), |
| 719 | bindings: bindings.unwrap_or_default(), |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 720 | gt_token: gt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 721 | } |
| 722 | ), |
| 723 | }) |
| 724 | ) |
| 725 | | |
| 726 | mod_style_path_segment |
| 727 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 728 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 729 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 730 | named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq)))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 731 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 732 | impl Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 733 | named!(pub parse_mod_style -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 734 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 735 | segments: call!(Delimited::parse_separated_nonempty_with, |
| 736 | mod_style_path_segment) >> |
| 737 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 738 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 739 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 740 | }) |
| 741 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 742 | } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 743 | |
| 744 | named!(mod_style_path_segment -> PathSegment, alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 745 | map!(syn!(Ident), Into::into) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 746 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 747 | alt!( |
| 748 | syn!(Super) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 749 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 750 | syn!(Self_) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 751 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 752 | syn!(CapSelf) => { Into::into } |
| 753 | ) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 754 | )); |
| 755 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 756 | impl Synom for TypeBinding { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 757 | named!(parse -> Self, do_parse!( |
| 758 | id: syn!(Ident) >> |
| 759 | eq: syn!(Eq) >> |
| 760 | ty: syn!(Ty) >> |
| 761 | (TypeBinding { |
| 762 | ident: id, |
| 763 | eq_token: eq, |
| 764 | ty: ty, |
| 765 | }) |
| 766 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | impl Synom for PolyTraitRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 770 | named!(parse -> Self, do_parse!( |
| 771 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
| 772 | trait_ref: syn!(Path) >> |
| 773 | parenthesized: option!(cond_reduce!( |
| 774 | trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(), |
| 775 | syn!(ParenthesizedParameterData) |
| 776 | )) >> |
| 777 | ({ |
| 778 | let mut trait_ref = trait_ref; |
| 779 | if let Some(parenthesized) = parenthesized { |
| 780 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
| 781 | let len = trait_ref.segments.len(); |
| 782 | trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
| 783 | } |
| 784 | PolyTraitRef { |
| 785 | bound_lifetimes: bound_lifetimes, |
| 786 | trait_ref: trait_ref, |
| 787 | } |
| 788 | }) |
| 789 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 790 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 791 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 792 | impl Synom for BareFnArg { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 793 | named!(parse -> Self, do_parse!( |
| 794 | name: option!(do_parse!( |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 795 | name: syn!(BareFnArgName) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 796 | not!(syn!(Colon2)) >> |
| 797 | colon: syn!(Colon) >> |
| 798 | (name, colon) |
| 799 | )) >> |
| 800 | ty: syn!(Ty) >> |
| 801 | (BareFnArg { |
| 802 | name: name, |
| 803 | ty: ty, |
| 804 | }) |
| 805 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 806 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 807 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 808 | impl Synom for BareFnArgName { |
| 809 | named!(parse -> Self, alt!( |
| 810 | map!(syn!(Ident), BareFnArgName::Named) |
| 811 | | |
| 812 | map!(syn!(Underscore), BareFnArgName::Wild) |
| 813 | )); |
| 814 | } |
| 815 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 816 | impl Synom for Unsafety { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 817 | named!(parse -> Self, alt!( |
| 818 | syn!(Unsafe) => { Unsafety::Unsafe } |
| 819 | | |
| 820 | epsilon!() => { |_| Unsafety::Normal } |
| 821 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 822 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 823 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 824 | impl Synom for Abi { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 825 | named!(parse -> Self, do_parse!( |
| 826 | extern_: syn!(Extern) >> |
| 827 | // TODO: this parses all literals, not just strings |
| 828 | name: option!(syn!(Lit)) >> |
| 829 | (Abi { |
| 830 | extern_token: extern_, |
| 831 | kind: match name { |
| 832 | Some(name) => AbiKind::Named(name), |
| 833 | None => AbiKind::Default, |
| 834 | }, |
| 835 | }) |
| 836 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 837 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 838 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 839 | |
| 840 | #[cfg(feature = "printing")] |
| 841 | mod printing { |
| 842 | use super::*; |
| 843 | use quote::{Tokens, ToTokens}; |
| 844 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 845 | impl ToTokens for TySlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 846 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 847 | self.bracket_token.surround(tokens, |tokens| { |
| 848 | self.ty.to_tokens(tokens); |
| 849 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | impl ToTokens for TyArray { |
| 854 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 855 | self.bracket_token.surround(tokens, |tokens| { |
| 856 | self.ty.to_tokens(tokens); |
| 857 | self.semi_token.to_tokens(tokens); |
| 858 | self.amt.to_tokens(tokens); |
| 859 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | impl ToTokens for TyPtr { |
| 864 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 865 | self.star_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 866 | match self.ty.mutability { |
| 867 | Mutability::Mutable(ref tok) => tok.to_tokens(tokens), |
| 868 | Mutability::Immutable => { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 869 | TokensOrDefault(&self.const_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 870 | } |
| 871 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 872 | self.ty.ty.to_tokens(tokens); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | impl ToTokens for TyRptr { |
| 877 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 878 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 879 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 880 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 881 | self.ty.ty.to_tokens(tokens); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | impl ToTokens for TyBareFn { |
| 886 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 887 | self.ty.to_tokens(tokens) |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | impl ToTokens for TyNever { |
| 892 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 893 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | impl ToTokens for TyTup { |
| 898 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 899 | self.paren_token.surround(tokens, |tokens| { |
| 900 | self.tys.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 901 | // XXX: I don't think (,) is a thing. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 902 | self.lone_comma.to_tokens(tokens); |
| 903 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | impl ToTokens for TyPath { |
| 908 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 909 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | impl<'a> ToTokens for PathTokens<'a> { |
| 914 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 915 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 916 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 917 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 918 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 919 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 920 | qself.ty.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 921 | |
| 922 | // XXX: Gross. |
| 923 | let pos = if qself.position > 0 && qself.position >= self.1.segments.len() { |
| 924 | self.1.segments.len() - 1 |
| 925 | } else { |
| 926 | qself.position |
| 927 | }; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 928 | let mut segments = self.1.segments.iter(); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 929 | if pos > 0 { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 930 | TokensOrDefault(&qself.as_token).to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 931 | self.1.leading_colon.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 932 | for (i, segment) in (&mut segments).take(pos).enumerate() { |
| 933 | if i + 1 == pos { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 934 | segment.item().to_tokens(tokens); |
| 935 | qself.gt_token.to_tokens(tokens); |
| 936 | segment.delimiter().to_tokens(tokens); |
| 937 | } else { |
| 938 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 939 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 940 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 941 | } else { |
| 942 | qself.gt_token.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 943 | self.1.leading_colon.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 944 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 945 | for segment in segments { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 946 | segment.to_tokens(tokens); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | impl ToTokens for TyTraitObject { |
| 952 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 953 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | impl ToTokens for TyImplTrait { |
| 958 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 959 | self.impl_token.to_tokens(tokens); |
| 960 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 964 | impl ToTokens for TyGroup { |
| 965 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 966 | self.group_token.surround(tokens, |tokens| { |
| 967 | self.ty.to_tokens(tokens); |
| 968 | }); |
| 969 | } |
| 970 | } |
| 971 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 972 | impl ToTokens for TyParen { |
| 973 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 974 | self.paren_token.surround(tokens, |tokens| { |
| 975 | self.ty.to_tokens(tokens); |
| 976 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | impl ToTokens for TyInfer { |
| 981 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 982 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 986 | impl ToTokens for Mutability { |
| 987 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 988 | if let Mutability::Mutable(ref t) = *self { |
| 989 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 994 | impl ToTokens for Path { |
| 995 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 996 | self.leading_colon.to_tokens(tokens); |
| 997 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | impl ToTokens for PathSegment { |
| 1002 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1003 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1004 | self.parameters.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | impl ToTokens for PathParameters { |
| 1009 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1010 | match *self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1011 | PathParameters::None => {} |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1012 | PathParameters::AngleBracketed(ref parameters) => { |
| 1013 | parameters.to_tokens(tokens); |
| 1014 | } |
| 1015 | PathParameters::Parenthesized(ref parameters) => { |
| 1016 | parameters.to_tokens(tokens); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | impl ToTokens for AngleBracketedParameterData { |
| 1023 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1024 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1025 | self.lt_token.to_tokens(tokens); |
| 1026 | self.lifetimes.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1027 | if !self.lifetimes.empty_or_trailing() && !self.types.is_empty() { |
| 1028 | tokens::Comma::default().to_tokens(tokens); |
| 1029 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1030 | self.types.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1031 | if ( |
| 1032 | // If we have no trailing delimiter after a non-empty types list, or |
| 1033 | !self.types.empty_or_trailing() || |
| 1034 | // If we have no trailing delimiter after a non-empty lifetimes |
| 1035 | // list before an empty types list, and |
| 1036 | (self.types.is_empty() && !self.lifetimes.empty_or_trailing())) && |
| 1037 | // We have some bindings, then we need a comma. |
| 1038 | !self.bindings.is_empty() |
| 1039 | { |
| 1040 | tokens::Comma::default().to_tokens(tokens); |
| 1041 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1042 | self.bindings.to_tokens(tokens); |
| 1043 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | impl ToTokens for TypeBinding { |
| 1048 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1049 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1050 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1051 | self.ty.to_tokens(tokens); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | impl ToTokens for ParenthesizedParameterData { |
| 1056 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1057 | self.paren_token.surround(tokens, |tokens| { |
| 1058 | self.inputs.to_tokens(tokens); |
| 1059 | }); |
| 1060 | self.output.to_tokens(tokens); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | impl ToTokens for FunctionRetTy { |
| 1065 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1066 | match *self { |
| 1067 | FunctionRetTy::Default => {} |
| 1068 | FunctionRetTy::Ty(ref ty, ref arrow) => { |
| 1069 | arrow.to_tokens(tokens); |
| 1070 | ty.to_tokens(tokens); |
| 1071 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | impl ToTokens for PolyTraitRef { |
| 1077 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1078 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1079 | self.trait_ref.to_tokens(tokens); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | impl ToTokens for BareFnTy { |
| 1084 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1085 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1086 | self.unsafety.to_tokens(tokens); |
| 1087 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1088 | self.fn_token.to_tokens(tokens); |
| 1089 | self.paren_token.surround(tokens, |tokens| { |
| 1090 | self.inputs.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1091 | if self.variadic.is_some() && !self.inputs.empty_or_trailing() { |
| 1092 | tokens::Comma::default().to_tokens(tokens); |
| 1093 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1094 | self.variadic.to_tokens(tokens); |
| 1095 | }); |
| 1096 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1100 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1101 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1102 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1103 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1104 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1105 | } |
| 1106 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1109 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 1110 | impl ToTokens for BareFnArgName { |
| 1111 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1112 | match *self { |
| 1113 | BareFnArgName::Named(ref t) => t.to_tokens(tokens), |
| 1114 | BareFnArgName::Wild(ref t) => t.to_tokens(tokens), |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1119 | impl ToTokens for Unsafety { |
| 1120 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1121 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1122 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1123 | Unsafety::Normal => { |
| 1124 | // nothing |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | impl ToTokens for Abi { |
| 1131 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1132 | self.extern_token.to_tokens(tokens); |
| 1133 | match self.kind { |
| 1134 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1135 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1139 | } |