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