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