David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 3 | /// The different kinds of types recognized by the compiler |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 4 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 5 | pub enum Ty { |
| 6 | /// A variable-length array (`[T]`) |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 7 | Slice(Box<Ty>), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | /// A fixed length array (`[T; n]`) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 9 | Array(Box<Ty>, ConstExpr), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 10 | /// A raw pointer (`*const T` or `*mut T`) |
| 11 | Ptr(Box<MutTy>), |
| 12 | /// A reference (`&'a T` or `&'a mut T`) |
| 13 | Rptr(Option<Lifetime>, Box<MutTy>), |
| 14 | /// A bare function (e.g. `fn(usize) -> bool`) |
| 15 | BareFn(Box<BareFnTy>), |
| 16 | /// The never type (`!`) |
| 17 | Never, |
| 18 | /// A tuple (`(A, B, C, D, ...)`) |
| 19 | Tup(Vec<Ty>), |
| 20 | /// A path (`module::module::...::Type`), optionally |
| 21 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 22 | /// |
| 23 | /// Type parameters are stored in the Path itself |
| 24 | Path(Option<QSelf>, Path), |
| 25 | /// Something like `A+B`. Note that `B` must always be a path. |
| 26 | ObjectSum(Box<Ty>, Vec<TyParamBound>), |
| 27 | /// A type like `for<'a> Foo<&'a Bar>` |
| 28 | PolyTraitRef(Vec<TyParamBound>), |
| 29 | /// An `impl TraitA+TraitB` type. |
| 30 | ImplTrait(Vec<TyParamBound>), |
| 31 | /// No-op; kept solely so that we can pretty-print faithfully |
| 32 | Paren(Box<Ty>), |
| 33 | /// TyKind::Infer means the type should be inferred instead of it having been |
| 34 | /// specified. This can appear anywhere in a type. |
| 35 | Infer, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 39 | pub struct MutTy { |
| 40 | pub ty: Ty, |
| 41 | pub mutability: Mutability, |
| 42 | } |
| 43 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 44 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 45 | pub enum Mutability { |
| 46 | Mutable, |
| 47 | Immutable, |
| 48 | } |
| 49 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 50 | /// A "Path" is essentially Rust's notion of a name. |
| 51 | /// |
| 52 | /// It's represented as a sequence of identifiers, |
| 53 | /// along with a bunch of supporting information. |
| 54 | /// |
| 55 | /// E.g. `std::cmp::PartialEq` |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 56 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 57 | pub struct Path { |
| 58 | pub global: bool, |
| 59 | pub segments: Vec<PathSegment>, |
| 60 | } |
| 61 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 62 | impl<T> From<T> for Path |
| 63 | where T: Into<PathSegment> |
| 64 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 65 | fn from(segment: T) -> Self { |
| 66 | Path { |
| 67 | global: false, |
| 68 | segments: vec![segment.into()], |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 73 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 74 | /// |
| 75 | /// E.g. `std`, `String` or `Box<T>` |
| 76 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 77 | pub struct PathSegment { |
| 78 | pub ident: Ident, |
| 79 | pub parameters: PathParameters, |
| 80 | } |
| 81 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 82 | impl<T> From<T> for PathSegment |
| 83 | where T: Into<Ident> |
| 84 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 85 | fn from(ident: T) -> Self { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 86 | PathSegment { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 87 | ident: ident.into(), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 88 | parameters: PathParameters::none(), |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// Parameters of a path segment. |
| 94 | /// |
| 95 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
| 96 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 97 | pub enum PathParameters { |
| 98 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
| 99 | AngleBracketed(AngleBracketedParameterData), |
| 100 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
| 101 | Parenthesized(ParenthesizedParameterData), |
| 102 | } |
| 103 | |
| 104 | impl PathParameters { |
| 105 | pub fn none() -> Self { |
| 106 | PathParameters::AngleBracketed(AngleBracketedParameterData::default()) |
| 107 | } |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame^] | 108 | |
| 109 | pub fn is_empty(&self) -> bool { |
| 110 | match *self { |
| 111 | PathParameters::AngleBracketed(ref bracketed) => { |
| 112 | bracketed.lifetimes.is_empty() && bracketed.types.is_empty() && bracketed.bindings.is_empty() |
| 113 | } |
| 114 | PathParameters::Parenthesized(_) => false, |
| 115 | } |
| 116 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /// A path like `Foo<'a, T>` |
| 120 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 121 | pub struct AngleBracketedParameterData { |
| 122 | /// The lifetime parameters for this path segment. |
| 123 | pub lifetimes: Vec<Lifetime>, |
| 124 | /// The type parameters for this path segment, if present. |
| 125 | pub types: Vec<Ty>, |
| 126 | /// Bindings (equality constraints) on associated types, if present. |
| 127 | /// |
| 128 | /// E.g., `Foo<A=Bar>`. |
| 129 | pub bindings: Vec<TypeBinding>, |
| 130 | } |
| 131 | |
| 132 | /// Bind a type to an associated type: `A=Foo`. |
| 133 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 134 | pub struct TypeBinding { |
| 135 | pub ident: Ident, |
| 136 | pub ty: Ty, |
| 137 | } |
| 138 | |
| 139 | /// A path like `Foo(A,B) -> C` |
| 140 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 141 | pub struct ParenthesizedParameterData { |
| 142 | /// `(A, B)` |
| 143 | pub inputs: Vec<Ty>, |
| 144 | /// `C` |
| 145 | pub output: Option<Ty>, |
| 146 | } |
| 147 | |
| 148 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 149 | pub struct PolyTraitRef { |
| 150 | /// The `'a` in `<'a> Foo<&'a T>` |
| 151 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 152 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
| 153 | pub trait_ref: Path, |
| 154 | } |
| 155 | |
| 156 | /// The explicit Self type in a "qualified path". The actual |
| 157 | /// path, including the trait and the associated item, is stored |
| 158 | /// separately. `position` represents the index of the associated |
| 159 | /// item qualified with this Self type. |
| 160 | /// |
| 161 | /// ```rust,ignore |
| 162 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 163 | /// ^~~~~ ~~~~~~~~~~~~~~^ |
| 164 | /// ty position = 3 |
| 165 | /// |
| 166 | /// <Vec<T>>::AssociatedItem |
| 167 | /// ^~~~~ ^ |
| 168 | /// ty position = 0 |
| 169 | /// ``` |
| 170 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 171 | pub struct QSelf { |
| 172 | pub ty: Box<Ty>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 173 | pub position: usize, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 177 | pub struct BareFnTy { |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 178 | pub unsafety: Unsafety, |
| 179 | pub abi: Option<Abi>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 180 | pub lifetimes: Vec<LifetimeDef>, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 181 | pub inputs: Vec<BareFnArg>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 182 | pub output: FunctionRetTy, |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 183 | pub variadic: bool, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 184 | } |
| 185 | |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 186 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 187 | pub enum Unsafety { |
| 188 | Unsafe, |
| 189 | Normal, |
| 190 | } |
| 191 | |
| 192 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 193 | pub enum Abi { |
| 194 | Named(String), |
| 195 | Extern, |
| 196 | } |
| 197 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 198 | /// An argument in a function type. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 199 | /// |
| 200 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 201 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 202 | pub struct BareFnArg { |
| 203 | pub name: Option<Ident>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 204 | pub ty: Ty, |
| 205 | } |
| 206 | |
| 207 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 208 | pub enum FunctionRetTy { |
| 209 | /// Return type is not specified. |
| 210 | /// |
| 211 | /// Functions default to `()` and |
| 212 | /// closures default to inference. Span points to where return |
| 213 | /// type would be inserted. |
| 214 | Default, |
| 215 | /// Everything else |
| 216 | Ty(Ty), |
| 217 | } |
| 218 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 219 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 220 | pub mod parsing { |
| 221 | use super::*; |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 222 | #[cfg(feature = "full")] |
| 223 | use ConstExpr; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 224 | use constant::parsing::const_expr; |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 225 | #[cfg(feature = "full")] |
| 226 | use expr::parsing::expr; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 227 | use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 228 | use ident::parsing::ident; |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 229 | use lit::parsing::quoted_string; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 230 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 231 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 232 | named!(pub ty -> Ty, alt!( |
David Tolnay | d040d77 | 2016-10-25 21:33:51 -0700 | [diff] [blame] | 233 | ty_paren // must be before ty_tup |
| 234 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 235 | ty_path // must be before ty_poly_trait_ref |
| 236 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 237 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 238 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 239 | ty_array |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 240 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 241 | ty_ptr |
| 242 | | |
| 243 | ty_rptr |
| 244 | | |
| 245 | ty_bare_fn |
| 246 | | |
| 247 | ty_never |
| 248 | | |
| 249 | ty_tup |
| 250 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 251 | ty_poly_trait_ref |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 252 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 253 | ty_impl_trait |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 254 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 255 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 256 | named!(ty_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 257 | punct!("[") >> |
| 258 | elem: ty >> |
| 259 | punct!("]") >> |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 260 | (Ty::Slice(Box::new(elem))) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 261 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 262 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 263 | #[cfg(not(feature = "full"))] |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 264 | named!(ty_array -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 265 | punct!("[") >> |
| 266 | elem: ty >> |
| 267 | punct!(";") >> |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 268 | len: const_expr >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 269 | punct!("]") >> |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 270 | (Ty::Array(Box::new(elem), len)) |
| 271 | )); |
| 272 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 273 | #[cfg(feature = "full")] |
| 274 | named!(ty_array -> Ty, do_parse!( |
| 275 | punct!("[") >> |
| 276 | elem: ty >> |
| 277 | punct!(";") >> |
| 278 | len: alt!( |
| 279 | terminated!(const_expr, punct!("]")) |
| 280 | | |
| 281 | terminated!(expr, punct!("]")) => { ConstExpr::Other } |
| 282 | ) >> |
| 283 | (Ty::Array(Box::new(elem), len)) |
| 284 | )); |
| 285 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 286 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 287 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 288 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 289 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 290 | | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 291 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 292 | ) >> |
| 293 | target: ty >> |
| 294 | (Ty::Ptr(Box::new(MutTy { |
| 295 | ty: target, |
| 296 | mutability: mutability, |
| 297 | }))) |
| 298 | )); |
| 299 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 300 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 301 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 302 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 303 | mutability: mutability >> |
| 304 | target: ty >> |
| 305 | (Ty::Rptr(life, Box::new(MutTy { |
| 306 | ty: target, |
| 307 | mutability: mutability, |
| 308 | }))) |
| 309 | )); |
| 310 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 311 | named!(ty_bare_fn -> Ty, do_parse!( |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 312 | lifetimes: opt_vec!(do_parse!( |
| 313 | keyword!("for") >> |
| 314 | punct!("<") >> |
| 315 | lifetimes: terminated_list!(punct!(","), lifetime_def) >> |
| 316 | punct!(">") >> |
| 317 | (lifetimes) |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 318 | )) >> |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 319 | unsafety: unsafety >> |
| 320 | abi: option!(abi) >> |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 321 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 322 | punct!("(") >> |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 323 | inputs: separated_list!(punct!(","), fn_arg) >> |
| 324 | trailing_comma: option!(punct!(",")) >> |
| 325 | variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 326 | punct!(")") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 327 | output: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 328 | punct!("->"), |
| 329 | ty |
| 330 | )) >> |
| 331 | (Ty::BareFn(Box::new(BareFnTy { |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 332 | unsafety: unsafety, |
| 333 | abi: abi, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 334 | lifetimes: lifetimes, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 335 | inputs: inputs, |
| 336 | output: match output { |
| 337 | Some(ty) => FunctionRetTy::Ty(ty), |
| 338 | None => FunctionRetTy::Default, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 339 | }, |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 340 | variadic: variadic.is_some(), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 341 | }))) |
| 342 | )); |
| 343 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 344 | named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never)); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 345 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 346 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 347 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 348 | elems: terminated_list!(punct!(","), ty) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 349 | punct!(")") >> |
| 350 | (Ty::Tup(elems)) |
| 351 | )); |
| 352 | |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 353 | named!(ty_path -> Ty, do_parse!( |
| 354 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 355 | parenthesized: cond!( |
| 356 | qpath.1.segments.last().unwrap().parameters == PathParameters::none(), |
| 357 | option!(parenthesized_parameter_data) |
| 358 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 359 | bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >> |
| 360 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 361 | let (qself, mut path) = qpath; |
| 362 | if let Some(Some(parenthesized)) = parenthesized { |
| 363 | path.segments.last_mut().unwrap().parameters = parenthesized; |
| 364 | } |
| 365 | let path = Ty::Path(qself, path); |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 366 | if bounds.is_empty() { |
| 367 | path |
| 368 | } else { |
| 369 | Ty::ObjectSum(Box::new(path), bounds) |
| 370 | } |
| 371 | }) |
| 372 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 373 | |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 374 | named!(parenthesized_parameter_data -> PathParameters, do_parse!( |
| 375 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 376 | inputs: terminated_list!(punct!(","), ty) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 377 | punct!(")") >> |
| 378 | output: option!(preceded!( |
| 379 | punct!("->"), |
| 380 | ty |
| 381 | )) >> |
| 382 | (PathParameters::Parenthesized( |
| 383 | ParenthesizedParameterData { |
| 384 | inputs: inputs, |
| 385 | output: output, |
| 386 | }, |
| 387 | )) |
| 388 | )); |
| 389 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 390 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
| 391 | map!(path, |p| (None, p)) |
| 392 | | |
| 393 | do_parse!( |
| 394 | punct!("<") >> |
| 395 | this: map!(ty, Box::new) >> |
| 396 | path: option!(preceded!( |
| 397 | keyword!("as"), |
| 398 | path |
| 399 | )) >> |
| 400 | punct!(">") >> |
| 401 | punct!("::") >> |
| 402 | rest: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 403 | ({ |
| 404 | match path { |
| 405 | Some(mut path) => { |
| 406 | let pos = path.segments.len(); |
| 407 | path.segments.extend(rest); |
| 408 | (Some(QSelf { ty: this, position: pos }), path) |
| 409 | } |
| 410 | None => { |
| 411 | (Some(QSelf { ty: this, position: 0 }), Path { |
| 412 | global: false, |
| 413 | segments: rest, |
| 414 | }) |
| 415 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 416 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 417 | }) |
| 418 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 419 | | |
| 420 | map!(keyword!("self"), |_| (None, "self".into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 421 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 422 | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 423 | named!(ty_poly_trait_ref -> Ty, map!( |
David Tolnay | 8eb6c45 | 2016-10-30 13:19:15 -0700 | [diff] [blame] | 424 | separated_nonempty_list!(punct!("+"), ty_param_bound), |
| 425 | Ty::PolyTraitRef |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 426 | )); |
| 427 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 428 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 429 | keyword!("impl") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 430 | elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 431 | (Ty::ImplTrait(elem)) |
| 432 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 433 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 434 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 435 | punct!("(") >> |
| 436 | elem: ty >> |
| 437 | punct!(")") >> |
| 438 | (Ty::Paren(Box::new(elem))) |
| 439 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 440 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 441 | named!(pub mutability -> Mutability, alt!( |
David Tolnay | bd76e57 | 2016-10-02 13:43:16 -0700 | [diff] [blame] | 442 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 443 | | |
| 444 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 445 | )); |
| 446 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 447 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 448 | global: option!(punct!("::")) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 449 | segments: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 450 | (Path { |
| 451 | global: global.is_some(), |
| 452 | segments: segments, |
| 453 | }) |
| 454 | )); |
| 455 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 456 | named!(path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 457 | do_parse!( |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 458 | id: option!(ident) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 459 | punct!("<") >> |
| 460 | lifetimes: separated_list!(punct!(","), lifetime) >> |
| 461 | types: opt_vec!(preceded!( |
| 462 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 463 | separated_nonempty_list!( |
| 464 | punct!(","), |
| 465 | terminated!(ty, not!(peek!(punct!("=")))) |
| 466 | ) |
| 467 | )) >> |
| 468 | bindings: opt_vec!(preceded!( |
| 469 | cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")), |
| 470 | separated_nonempty_list!(punct!(","), type_binding) |
| 471 | )) >> |
David Tolnay | 82a47d5 | 2016-10-30 13:01:38 -0700 | [diff] [blame] | 472 | cond!(!lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty(), option!(punct!(","))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 473 | punct!(">") >> |
| 474 | (PathSegment { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 475 | ident: id.unwrap_or_else(|| "".into()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 476 | parameters: PathParameters::AngleBracketed( |
| 477 | AngleBracketedParameterData { |
| 478 | lifetimes: lifetimes, |
| 479 | types: types, |
| 480 | bindings: bindings, |
| 481 | } |
| 482 | ), |
| 483 | }) |
| 484 | ) |
| 485 | | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 486 | map!(ident, Into::into) |
David Tolnay | 7780722 | 2016-10-24 22:30:15 -0700 | [diff] [blame] | 487 | | |
David Tolnay | e14e3be | 2016-10-24 22:53:07 -0700 | [diff] [blame] | 488 | map!(alt!( |
| 489 | keyword!("super") |
| 490 | | |
| 491 | keyword!("self") |
| 492 | | |
| 493 | keyword!("Self") |
| 494 | ), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 495 | )); |
| 496 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 497 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 498 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 499 | punct!("=") >> |
| 500 | ty: ty >> |
| 501 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 502 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 503 | ty: ty, |
| 504 | }) |
| 505 | )); |
| 506 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 507 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 508 | bound_lifetimes: bound_lifetimes >> |
| 509 | trait_ref: path >> |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 510 | parenthesized: option!(cond_reduce!( |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 511 | trait_ref.segments.last().unwrap().parameters == PathParameters::none(), |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 512 | parenthesized_parameter_data |
| 513 | )) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 514 | ({ |
| 515 | let mut trait_ref = trait_ref; |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 516 | if let Some(parenthesized) = parenthesized { |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 517 | trait_ref.segments.last_mut().unwrap().parameters = parenthesized; |
| 518 | } |
| 519 | PolyTraitRef { |
| 520 | bound_lifetimes: bound_lifetimes, |
| 521 | trait_ref: trait_ref, |
| 522 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 523 | }) |
| 524 | )); |
| 525 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 526 | named!(pub fn_arg -> BareFnArg, do_parse!( |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 527 | name: option!(do_parse!( |
| 528 | name: ident >> |
| 529 | punct!(":") >> |
| 530 | not!(peek!(tag!(":"))) >> // not :: |
| 531 | (name) |
| 532 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 533 | ty: ty >> |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 534 | (BareFnArg { |
| 535 | name: name, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 536 | ty: ty, |
| 537 | }) |
| 538 | )); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 539 | |
| 540 | named!(pub unsafety -> Unsafety, alt!( |
| 541 | keyword!("unsafe") => { |_| Unsafety::Unsafe } |
| 542 | | |
| 543 | epsilon!() => { |_| Unsafety::Normal } |
| 544 | )); |
| 545 | |
| 546 | named!(pub abi -> Abi, do_parse!( |
| 547 | keyword!("extern") >> |
| 548 | name: option!(quoted_string) >> |
| 549 | (match name { |
| 550 | Some(name) => Abi::Named(name), |
| 551 | None => Abi::Extern, |
| 552 | }) |
| 553 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 554 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 555 | |
| 556 | #[cfg(feature = "printing")] |
| 557 | mod printing { |
| 558 | use super::*; |
| 559 | use quote::{Tokens, ToTokens}; |
| 560 | |
| 561 | impl ToTokens for Ty { |
| 562 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 563 | match *self { |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 564 | Ty::Slice(ref inner) => { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 565 | tokens.append("["); |
| 566 | inner.to_tokens(tokens); |
| 567 | tokens.append("]"); |
| 568 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 569 | Ty::Array(ref inner, ref len) => { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 570 | tokens.append("["); |
| 571 | inner.to_tokens(tokens); |
| 572 | tokens.append(";"); |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 573 | len.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 574 | tokens.append("]"); |
| 575 | } |
| 576 | Ty::Ptr(ref target) => { |
| 577 | tokens.append("*"); |
| 578 | match target.mutability { |
| 579 | Mutability::Mutable => tokens.append("mut"), |
| 580 | Mutability::Immutable => tokens.append("const"), |
| 581 | } |
| 582 | target.ty.to_tokens(tokens); |
| 583 | } |
| 584 | Ty::Rptr(ref lifetime, ref target) => { |
| 585 | tokens.append("&"); |
| 586 | lifetime.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 587 | target.mutability.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 588 | target.ty.to_tokens(tokens); |
| 589 | } |
| 590 | Ty::BareFn(ref func) => { |
| 591 | func.to_tokens(tokens); |
| 592 | } |
| 593 | Ty::Never => { |
| 594 | tokens.append("!"); |
| 595 | } |
| 596 | Ty::Tup(ref elems) => { |
| 597 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 598 | tokens.append_separated(elems, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 599 | if elems.len() == 1 { |
| 600 | tokens.append(","); |
| 601 | } |
| 602 | tokens.append(")"); |
| 603 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 604 | Ty::Path(None, ref path) => { |
| 605 | path.to_tokens(tokens); |
| 606 | } |
| 607 | Ty::Path(Some(ref qself), ref path) => { |
| 608 | tokens.append("<"); |
| 609 | qself.ty.to_tokens(tokens); |
| 610 | if qself.position > 0 { |
| 611 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 612 | for (i, segment) in path.segments |
| 613 | .iter() |
| 614 | .take(qself.position) |
| 615 | .enumerate() { |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 616 | if i > 0 || path.global { |
| 617 | tokens.append("::"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 618 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 619 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 620 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 621 | } |
| 622 | tokens.append(">"); |
| 623 | for segment in path.segments.iter().skip(qself.position) { |
| 624 | tokens.append("::"); |
| 625 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 628 | Ty::ObjectSum(ref ty, ref bounds) => { |
| 629 | ty.to_tokens(tokens); |
| 630 | for bound in bounds { |
| 631 | tokens.append("+"); |
| 632 | bound.to_tokens(tokens); |
| 633 | } |
| 634 | } |
| 635 | Ty::PolyTraitRef(ref bounds) => { |
| 636 | tokens.append_separated(bounds, "+"); |
| 637 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 638 | Ty::ImplTrait(ref bounds) => { |
| 639 | tokens.append("impl"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 640 | tokens.append_separated(bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 641 | } |
| 642 | Ty::Paren(ref inner) => { |
| 643 | tokens.append("("); |
| 644 | inner.to_tokens(tokens); |
| 645 | tokens.append(")"); |
| 646 | } |
| 647 | Ty::Infer => { |
| 648 | tokens.append("_"); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 654 | impl ToTokens for Mutability { |
| 655 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 656 | if let Mutability::Mutable = *self { |
| 657 | tokens.append("mut"); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 662 | impl ToTokens for Path { |
| 663 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 664 | for (i, segment) in self.segments.iter().enumerate() { |
| 665 | if i > 0 || self.global { |
| 666 | tokens.append("::"); |
| 667 | } |
| 668 | segment.to_tokens(tokens); |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | impl ToTokens for PathSegment { |
| 674 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 675 | self.ident.to_tokens(tokens); |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame^] | 676 | if self.ident.as_ref().is_empty() && self.parameters.is_empty() { |
| 677 | tokens.append("<"); |
| 678 | tokens.append(">"); |
| 679 | } else { |
| 680 | self.parameters.to_tokens(tokens); |
| 681 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | impl ToTokens for PathParameters { |
| 686 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 687 | match *self { |
| 688 | PathParameters::AngleBracketed(ref parameters) => { |
| 689 | parameters.to_tokens(tokens); |
| 690 | } |
| 691 | PathParameters::Parenthesized(ref parameters) => { |
| 692 | parameters.to_tokens(tokens); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | impl ToTokens for AngleBracketedParameterData { |
| 699 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 700 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 701 | let has_types = !self.types.is_empty(); |
| 702 | let has_bindings = !self.bindings.is_empty(); |
| 703 | if !has_lifetimes && !has_types && !has_bindings { |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | tokens.append("<"); |
| 708 | |
| 709 | let mut first = true; |
| 710 | for lifetime in &self.lifetimes { |
| 711 | if !first { |
| 712 | tokens.append(","); |
| 713 | } |
| 714 | lifetime.to_tokens(tokens); |
| 715 | first = false; |
| 716 | } |
| 717 | for ty in &self.types { |
| 718 | if !first { |
| 719 | tokens.append(","); |
| 720 | } |
| 721 | ty.to_tokens(tokens); |
| 722 | first = false; |
| 723 | } |
| 724 | for binding in &self.bindings { |
| 725 | if !first { |
| 726 | tokens.append(","); |
| 727 | } |
| 728 | binding.to_tokens(tokens); |
| 729 | first = false; |
| 730 | } |
| 731 | |
| 732 | tokens.append(">"); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | impl ToTokens for TypeBinding { |
| 737 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 738 | self.ident.to_tokens(tokens); |
| 739 | tokens.append("="); |
| 740 | self.ty.to_tokens(tokens); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | impl ToTokens for ParenthesizedParameterData { |
| 745 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 746 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 747 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 748 | tokens.append(")"); |
| 749 | if let Some(ref output) = self.output { |
| 750 | tokens.append("->"); |
| 751 | output.to_tokens(tokens); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | impl ToTokens for PolyTraitRef { |
| 757 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 758 | if !self.bound_lifetimes.is_empty() { |
David Tolnay | e8796aa | 2016-09-04 14:48:22 -0700 | [diff] [blame] | 759 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 760 | tokens.append("<"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 761 | tokens.append_separated(&self.bound_lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 762 | tokens.append(">"); |
| 763 | } |
| 764 | self.trait_ref.to_tokens(tokens); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | impl ToTokens for BareFnTy { |
| 769 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 770 | if !self.lifetimes.is_empty() { |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 771 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 772 | tokens.append("<"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 773 | tokens.append_separated(&self.lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 774 | tokens.append(">"); |
| 775 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 776 | self.unsafety.to_tokens(tokens); |
| 777 | self.abi.to_tokens(tokens); |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 778 | tokens.append("fn"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 779 | tokens.append("("); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 780 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 781 | if self.variadic { |
| 782 | if !self.inputs.is_empty() { |
| 783 | tokens.append(","); |
| 784 | } |
| 785 | tokens.append("..."); |
| 786 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 787 | tokens.append(")"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 788 | if let FunctionRetTy::Ty(ref ty) = self.output { |
| 789 | tokens.append("->"); |
| 790 | ty.to_tokens(tokens); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 795 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 796 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 797 | if let Some(ref name) = self.name { |
| 798 | name.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 799 | tokens.append(":"); |
| 800 | } |
| 801 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 804 | |
| 805 | impl ToTokens for Unsafety { |
| 806 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 807 | match *self { |
| 808 | Unsafety::Unsafe => tokens.append("unsafe"), |
| 809 | Unsafety::Normal => { |
| 810 | // nothing |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | impl ToTokens for Abi { |
| 817 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 818 | tokens.append("extern"); |
| 819 | match *self { |
| 820 | Abi::Named(ref named) => named.to_tokens(tokens), |
| 821 | Abi::Extern => {} |
| 822 | } |
| 823 | } |
| 824 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 825 | } |