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