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