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