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, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 17 | pub amt: ConstExpr, |
| 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) >> |
| 395 | len: array_len >> |
| 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 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 409 | #[cfg(not(feature = "full"))] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 410 | named!(array_len -> ConstExpr, syn!(ConstExpr)); |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 411 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 412 | #[cfg(feature = "full")] |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 413 | named!(array_len -> ConstExpr, alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 414 | terminated!(syn!(ConstExpr), input_end!()) |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 415 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 416 | terminated!(syn!(Expr), input_end!()) => { ConstExpr::Other } |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 417 | )); |
| 418 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 419 | impl Synom for TyPtr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 420 | named!(parse -> Self, do_parse!( |
| 421 | star: syn!(Star) >> |
| 422 | mutability: alt!( |
| 423 | syn!(Const) => { |c| (Mutability::Immutable, Some(c)) } |
| 424 | | |
| 425 | syn!(Mut) => { |m| (Mutability::Mutable(m), None) } |
| 426 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 427 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 428 | (TyPtr { |
| 429 | const_token: mutability.1, |
| 430 | star_token: star, |
| 431 | ty: Box::new(MutTy { |
| 432 | ty: target, |
| 433 | mutability: mutability.0, |
| 434 | }), |
| 435 | }) |
| 436 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 437 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 438 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 439 | impl Synom for TyRptr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 440 | named!(parse -> Self, do_parse!( |
| 441 | amp: syn!(And) >> |
| 442 | life: option!(syn!(Lifetime)) >> |
| 443 | mutability: syn!(Mutability) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 444 | // & binds tighter than +, so we don't allow + here. |
| 445 | target: call!(Ty::without_plus) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 446 | (TyRptr { |
| 447 | lifetime: life, |
| 448 | ty: Box::new(MutTy { |
| 449 | ty: target, |
| 450 | mutability: mutability, |
| 451 | }), |
| 452 | and_token: amp, |
| 453 | }) |
| 454 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 455 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 456 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 457 | impl Synom for TyBareFn { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 458 | named!(parse -> Self, do_parse!( |
| 459 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 460 | unsafety: syn!(Unsafety) >> |
| 461 | abi: option!(syn!(Abi)) >> |
| 462 | fn_: syn!(Fn_) >> |
| 463 | parens: parens!(do_parse!( |
| 464 | inputs: call!(Delimited::parse_terminated) >> |
| 465 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
| 466 | syn!(Dot3))) >> |
| 467 | (inputs, variadic) |
| 468 | )) >> |
| 469 | output: syn!(FunctionRetTy) >> |
| 470 | (TyBareFn { |
| 471 | ty: Box::new(BareFnTy { |
| 472 | unsafety: unsafety, |
| 473 | abi: abi, |
| 474 | lifetimes: lifetimes, |
| 475 | output: output, |
| 476 | variadic: (parens.0).1, |
| 477 | fn_token: fn_, |
| 478 | paren_token: parens.1, |
| 479 | inputs: (parens.0).0, |
| 480 | }), |
| 481 | }) |
| 482 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 483 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 484 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 485 | impl Synom for TyNever { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 486 | named!(parse -> Self, map!( |
| 487 | syn!(Bang), |
| 488 | |b| TyNever { bang_token: b } |
| 489 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 490 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 491 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 492 | impl Synom for TyTup { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 493 | named!(parse -> Self, do_parse!( |
| 494 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 495 | (TyTup { |
| 496 | tys: data.0, |
| 497 | paren_token: data.1, |
| 498 | lone_comma: None, // TODO: does this just not parse? |
| 499 | }) |
| 500 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 501 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 502 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 503 | named!(ty_path(allow_plus: bool) -> Ty, do_parse!( |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 504 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 505 | parenthesized: cond!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 506 | 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] | 507 | option!(syn!(ParenthesizedParameterData)) |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 508 | ) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 509 | // Only allow parsing additional bounds if allow_plus is true. |
| 510 | bounds: alt!( |
| 511 | cond_reduce!( |
| 512 | allow_plus, |
| 513 | many0!(tuple!(syn!(Add), syn!(TyParamBound))) |
| 514 | ) |
| 515 | | |
| 516 | value!(vec![]) |
| 517 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 518 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 519 | let (qself, mut path) = qpath; |
| 520 | if let Some(Some(parenthesized)) = parenthesized { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 521 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 522 | let len = path.segments.len(); |
| 523 | path.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 524 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 525 | if bounds.is_empty() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 526 | TyPath { qself: qself, path: path }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 527 | } else { |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 528 | let path = TyParamBound::Trait( |
| 529 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 530 | bound_lifetimes: None, |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 531 | trait_ref: path, |
| 532 | }, |
| 533 | TraitBoundModifier::None, |
| 534 | ); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 535 | let mut new_bounds = Delimited::new(); |
| 536 | new_bounds.push_first(path); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 537 | for (_tok, bound) in bounds { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 538 | new_bounds.push_default(bound); |
| 539 | } |
| 540 | TyTraitObject { bounds: new_bounds }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 541 | } |
| 542 | }) |
| 543 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 544 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 545 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 546 | map!(syn!(Path), |p| (None, p)) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 547 | | |
| 548 | do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 549 | lt: syn!(Lt) >> |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 550 | this: syn!(Ty) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 551 | path: option!(do_parse!( |
| 552 | as_: syn!(As) >> |
| 553 | path: syn!(Path) >> |
| 554 | (as_, path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 555 | )) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 556 | gt: syn!(Gt) >> |
| 557 | colon2: syn!(Colon2) >> |
| 558 | rest: call!(Delimited::parse_separated_nonempty) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 559 | ({ |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 560 | let (pos, path) = match path { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 561 | Some((as_, mut path)) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 562 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 563 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 564 | path.segments.push_trailing(colon2); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 565 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 566 | for item in rest { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 567 | path.segments.push(item); |
| 568 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 569 | (Some((as_, pos)), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 570 | } |
| 571 | None => { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 572 | (None, Path { |
| 573 | leading_colon: Some(colon2), |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 574 | segments: rest, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 575 | }) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 576 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 577 | }; |
| 578 | (Some(QSelf { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 579 | lt_token: lt, |
| 580 | ty: Box::new(this), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 581 | position: pos, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 582 | gt_token: gt, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 583 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 584 | }) |
| 585 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 586 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 587 | map!(syn!(Self_), |s| (None, s.into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 588 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 589 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 590 | impl Synom for ParenthesizedParameterData { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 591 | named!(parse -> Self, do_parse!( |
| 592 | data: parens!(call!(Delimited::parse_terminated)) >> |
| 593 | output: syn!(FunctionRetTy) >> |
| 594 | (ParenthesizedParameterData { |
| 595 | paren_token: data.1, |
| 596 | inputs: data.0, |
| 597 | output: output, |
| 598 | }) |
| 599 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | impl Synom for FunctionRetTy { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 603 | named!(parse -> Self, alt!( |
| 604 | do_parse!( |
| 605 | arrow: syn!(RArrow) >> |
| 606 | ty: syn!(Ty) >> |
| 607 | (FunctionRetTy::Ty(ty, arrow)) |
| 608 | ) |
| 609 | | |
| 610 | epsilon!() => { |_| FunctionRetTy::Default } |
| 611 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 614 | // Only allow multiple trait references if allow_plus is true. |
| 615 | named!(ty_poly_trait_ref(allow_plus: bool) -> Ty, alt!( |
| 616 | cond_reduce!(allow_plus, call!(Delimited::parse_separated_nonempty)) => { |
| 617 | |x| TyTraitObject { bounds: x }.into() |
| 618 | } |
| 619 | | |
| 620 | syn!(TyParamBound) => { |
| 621 | |x| TyTraitObject { bounds: vec![x].into() }.into() |
| 622 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 623 | )); |
| 624 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 625 | impl Synom for TyImplTrait { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 626 | named!(parse -> Self, do_parse!( |
| 627 | impl_: syn!(Impl) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 628 | // NOTE: rust-lang/rust#34511 includes discussion about whether or |
| 629 | // not + should be allowed in ImplTrait directly without (). |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 630 | elem: call!(Delimited::parse_separated_nonempty) >> |
| 631 | (TyImplTrait { |
| 632 | impl_token: impl_, |
| 633 | bounds: elem, |
| 634 | }) |
| 635 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 636 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 637 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame^] | 638 | impl Synom for TyGroup { |
| 639 | named!(parse -> Self, do_parse!( |
| 640 | data: grouped!(syn!(Ty)) >> |
| 641 | (TyGroup { |
| 642 | group_token: data.1, |
| 643 | ty: Box::new(data.0), |
| 644 | }) |
| 645 | )); |
| 646 | } |
| 647 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 648 | impl Synom for TyParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 649 | named!(parse -> Self, do_parse!( |
| 650 | data: parens!(syn!(Ty)) >> |
| 651 | (TyParen { |
| 652 | paren_token: data.1, |
| 653 | ty: Box::new(data.0), |
| 654 | }) |
| 655 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 656 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 657 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 658 | impl Synom for Mutability { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 659 | named!(parse -> Self, alt!( |
| 660 | syn!(Mut) => { Mutability::Mutable } |
| 661 | | |
| 662 | epsilon!() => { |_| Mutability::Immutable } |
| 663 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 664 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 665 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 666 | impl Synom for Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 667 | named!(parse -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 668 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 669 | segments: call!(Delimited::parse_separated_nonempty) >> |
| 670 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 671 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 672 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 673 | }) |
| 674 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 675 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 676 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 677 | impl Synom for PathSegment { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 678 | named!(parse -> Self, alt!( |
| 679 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 680 | ident: syn!(Ident) >> |
| 681 | turbofish: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 682 | lt: syn!(Lt) >> |
| 683 | lifetimes: call!(Delimited::parse_terminated) >> |
| 684 | types: cond!( |
| 685 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 686 | call!(Delimited::parse_terminated_with, |
| 687 | ty_no_eq_after) |
| 688 | ) >> |
| 689 | bindings: cond!( |
| 690 | match types { |
| 691 | Some(ref t) => t.is_empty() || t.trailing_delim(), |
| 692 | None => lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 693 | }, |
| 694 | call!(Delimited::parse_terminated) |
| 695 | ) >> |
| 696 | gt: syn!(Gt) >> |
| 697 | (PathSegment { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 698 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 699 | parameters: PathParameters::AngleBracketed( |
| 700 | AngleBracketedParameterData { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 701 | turbofish: turbofish, |
| 702 | lt_token: lt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 703 | lifetimes: lifetimes, |
| 704 | types: types.unwrap_or_default(), |
| 705 | bindings: bindings.unwrap_or_default(), |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 706 | gt_token: gt, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 707 | } |
| 708 | ), |
| 709 | }) |
| 710 | ) |
| 711 | | |
| 712 | mod_style_path_segment |
| 713 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 714 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 715 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 716 | named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq)))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 717 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 718 | impl Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 719 | named!(pub parse_mod_style -> Self, do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 720 | colon: option!(syn!(Colon2)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 721 | segments: call!(Delimited::parse_separated_nonempty_with, |
| 722 | mod_style_path_segment) >> |
| 723 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 724 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 725 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 726 | }) |
| 727 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 728 | } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 729 | |
| 730 | named!(mod_style_path_segment -> PathSegment, alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 731 | map!(syn!(Ident), Into::into) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 732 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 733 | alt!( |
| 734 | syn!(Super) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 735 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 736 | syn!(Self_) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 737 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 738 | syn!(CapSelf) => { Into::into } |
| 739 | ) |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 740 | )); |
| 741 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 742 | impl Synom for TypeBinding { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 743 | named!(parse -> Self, do_parse!( |
| 744 | id: syn!(Ident) >> |
| 745 | eq: syn!(Eq) >> |
| 746 | ty: syn!(Ty) >> |
| 747 | (TypeBinding { |
| 748 | ident: id, |
| 749 | eq_token: eq, |
| 750 | ty: ty, |
| 751 | }) |
| 752 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | impl Synom for PolyTraitRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 756 | named!(parse -> Self, do_parse!( |
| 757 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
| 758 | trait_ref: syn!(Path) >> |
| 759 | parenthesized: option!(cond_reduce!( |
| 760 | trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(), |
| 761 | syn!(ParenthesizedParameterData) |
| 762 | )) >> |
| 763 | ({ |
| 764 | let mut trait_ref = trait_ref; |
| 765 | if let Some(parenthesized) = parenthesized { |
| 766 | let parenthesized = PathParameters::Parenthesized(parenthesized); |
| 767 | let len = trait_ref.segments.len(); |
| 768 | trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
| 769 | } |
| 770 | PolyTraitRef { |
| 771 | bound_lifetimes: bound_lifetimes, |
| 772 | trait_ref: trait_ref, |
| 773 | } |
| 774 | }) |
| 775 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 776 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 777 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 778 | impl Synom for BareFnArg { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 779 | named!(parse -> Self, do_parse!( |
| 780 | name: option!(do_parse!( |
| 781 | name: syn!(Ident) >> |
| 782 | not!(syn!(Colon2)) >> |
| 783 | colon: syn!(Colon) >> |
| 784 | (name, colon) |
| 785 | )) >> |
| 786 | ty: syn!(Ty) >> |
| 787 | (BareFnArg { |
| 788 | name: name, |
| 789 | ty: ty, |
| 790 | }) |
| 791 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 792 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 793 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 794 | impl Synom for Unsafety { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 795 | named!(parse -> Self, alt!( |
| 796 | syn!(Unsafe) => { Unsafety::Unsafe } |
| 797 | | |
| 798 | epsilon!() => { |_| Unsafety::Normal } |
| 799 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 800 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 801 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 802 | impl Synom for Abi { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 803 | named!(parse -> Self, do_parse!( |
| 804 | extern_: syn!(Extern) >> |
| 805 | // TODO: this parses all literals, not just strings |
| 806 | name: option!(syn!(Lit)) >> |
| 807 | (Abi { |
| 808 | extern_token: extern_, |
| 809 | kind: match name { |
| 810 | Some(name) => AbiKind::Named(name), |
| 811 | None => AbiKind::Default, |
| 812 | }, |
| 813 | }) |
| 814 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 815 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 816 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 817 | |
| 818 | #[cfg(feature = "printing")] |
| 819 | mod printing { |
| 820 | use super::*; |
| 821 | use quote::{Tokens, ToTokens}; |
| 822 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 823 | impl ToTokens for TySlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 824 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 825 | self.bracket_token.surround(tokens, |tokens| { |
| 826 | self.ty.to_tokens(tokens); |
| 827 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | impl ToTokens for TyArray { |
| 832 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 833 | self.bracket_token.surround(tokens, |tokens| { |
| 834 | self.ty.to_tokens(tokens); |
| 835 | self.semi_token.to_tokens(tokens); |
| 836 | self.amt.to_tokens(tokens); |
| 837 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | impl ToTokens for TyPtr { |
| 842 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 843 | self.star_token.to_tokens(tokens); |
| 844 | self.const_token.to_tokens(tokens); |
| 845 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 846 | self.ty.ty.to_tokens(tokens); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | impl ToTokens for TyRptr { |
| 851 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 852 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 853 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 854 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 855 | self.ty.ty.to_tokens(tokens); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | impl ToTokens for TyBareFn { |
| 860 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 861 | self.ty.to_tokens(tokens) |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | impl ToTokens for TyNever { |
| 866 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 867 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | impl ToTokens for TyTup { |
| 872 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 873 | self.paren_token.surround(tokens, |tokens| { |
| 874 | self.tys.to_tokens(tokens); |
| 875 | self.lone_comma.to_tokens(tokens); |
| 876 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | impl ToTokens for TyPath { |
| 881 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 882 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | impl<'a> ToTokens for PathTokens<'a> { |
| 887 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 888 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 889 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 890 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 891 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 892 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 893 | qself.ty.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 894 | let mut segments = self.1.segments.iter(); |
David Tolnay | b99e1b0 | 2017-06-03 19:00:55 -0700 | [diff] [blame] | 895 | if let Some((ref as_token, pos)) = qself.position { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 896 | as_token.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 897 | self.1.leading_colon.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 898 | for (i, segment) in (&mut segments).take(pos).enumerate() { |
| 899 | if i + 1 == pos { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 900 | segment.item().to_tokens(tokens); |
| 901 | qself.gt_token.to_tokens(tokens); |
| 902 | segment.delimiter().to_tokens(tokens); |
| 903 | } else { |
| 904 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 905 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 906 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 907 | } else { |
| 908 | qself.gt_token.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 909 | self.1.leading_colon.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 910 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 911 | for segment in segments { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 912 | segment.to_tokens(tokens); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | impl ToTokens for TyTraitObject { |
| 918 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 919 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | impl ToTokens for TyImplTrait { |
| 924 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 925 | self.impl_token.to_tokens(tokens); |
| 926 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame^] | 930 | impl ToTokens for TyGroup { |
| 931 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 932 | self.group_token.surround(tokens, |tokens| { |
| 933 | self.ty.to_tokens(tokens); |
| 934 | }); |
| 935 | } |
| 936 | } |
| 937 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 938 | impl ToTokens for TyParen { |
| 939 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 940 | self.paren_token.surround(tokens, |tokens| { |
| 941 | self.ty.to_tokens(tokens); |
| 942 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| 946 | impl ToTokens for TyInfer { |
| 947 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 948 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 952 | impl ToTokens for Mutability { |
| 953 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 954 | if let Mutability::Mutable(ref t) = *self { |
| 955 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 960 | impl ToTokens for Path { |
| 961 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 962 | self.leading_colon.to_tokens(tokens); |
| 963 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | impl ToTokens for PathSegment { |
| 968 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 969 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 970 | self.parameters.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | impl ToTokens for PathParameters { |
| 975 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 976 | match *self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 977 | PathParameters::None => {} |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 978 | PathParameters::AngleBracketed(ref parameters) => { |
| 979 | parameters.to_tokens(tokens); |
| 980 | } |
| 981 | PathParameters::Parenthesized(ref parameters) => { |
| 982 | parameters.to_tokens(tokens); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | impl ToTokens for AngleBracketedParameterData { |
| 989 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 990 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 991 | self.lt_token.to_tokens(tokens); |
| 992 | self.lifetimes.to_tokens(tokens); |
| 993 | self.types.to_tokens(tokens); |
| 994 | self.bindings.to_tokens(tokens); |
| 995 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
| 999 | impl ToTokens for TypeBinding { |
| 1000 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1001 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1002 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1003 | self.ty.to_tokens(tokens); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | impl ToTokens for ParenthesizedParameterData { |
| 1008 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1009 | self.paren_token.surround(tokens, |tokens| { |
| 1010 | self.inputs.to_tokens(tokens); |
| 1011 | }); |
| 1012 | self.output.to_tokens(tokens); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | impl ToTokens for FunctionRetTy { |
| 1017 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1018 | match *self { |
| 1019 | FunctionRetTy::Default => {} |
| 1020 | FunctionRetTy::Ty(ref ty, ref arrow) => { |
| 1021 | arrow.to_tokens(tokens); |
| 1022 | ty.to_tokens(tokens); |
| 1023 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | impl ToTokens for PolyTraitRef { |
| 1029 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1030 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1031 | self.trait_ref.to_tokens(tokens); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | impl ToTokens for BareFnTy { |
| 1036 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1037 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1038 | self.unsafety.to_tokens(tokens); |
| 1039 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1040 | self.fn_token.to_tokens(tokens); |
| 1041 | self.paren_token.surround(tokens, |tokens| { |
| 1042 | self.inputs.to_tokens(tokens); |
| 1043 | self.variadic.to_tokens(tokens); |
| 1044 | }); |
| 1045 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1049 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1050 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1051 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1052 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1053 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1054 | } |
| 1055 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1056 | } |
| 1057 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1058 | |
| 1059 | impl ToTokens for Unsafety { |
| 1060 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1061 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1062 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1063 | Unsafety::Normal => { |
| 1064 | // nothing |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | impl ToTokens for Abi { |
| 1071 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1072 | self.extern_token.to_tokens(tokens); |
| 1073 | match self.kind { |
| 1074 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1075 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1079 | } |