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