blob: 70b3da4368f2b140a1b0cd4742716976c2017253 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// Represents lifetimes and type parameters attached to a declaration
6 /// of a function, enum, trait, etc.
7 #[derive(Default)]
8 pub struct Generics {
David Tolnayf8db7ba2017-11-11 22:52:16 -08009 pub lt_token: Option<Token![<]>,
David Tolnayc2f1aba2017-11-12 20:29:22 -080010 pub params: Delimited<GenericParam, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080011 pub gt_token: Option<Token![>]>,
David Tolnayac997dd2017-12-27 23:18:22 -050012 pub where_clause: Option<WhereClause>,
Alex Crichton62a0a592017-05-22 13:58:53 -070013 }
David Tolnayb79ee962016-09-04 09:39:20 -070014}
15
David Tolnayc2f1aba2017-11-12 20:29:22 -080016ast_enum_of_structs! {
17 pub enum GenericParam {
18 /// A lifetime definition, e.g. `'a: 'b+'c+'d`
19 pub Lifetime(LifetimeDef {
20 pub attrs: Vec<Attribute>,
21 pub lifetime: Lifetime,
22 pub colon_token: Option<Token![:]>,
23 pub bounds: Delimited<Lifetime, Token![+]>,
24 }),
25 /// A generic type parameter, e.g. `T: Into<String>`.
26 pub Type(TypeParam {
27 pub attrs: Vec<Attribute>,
28 pub ident: Ident,
29 pub colon_token: Option<Token![:]>,
30 pub bounds: Delimited<TypeParamBound, Token![+]>,
31 pub eq_token: Option<Token![=]>,
32 pub default: Option<Type>,
33 }),
Nika Layzellf1fdc0b2017-12-04 19:58:32 -050034 /// A generic const parameter, e.g. `const LENGTH: usize`.
35 pub Const(ConstParam {
36 pub attrs: Vec<Attribute>,
37 pub const_token: Token![const],
38 pub ident: Ident,
39 pub colon_token: Token![:],
40 pub ty: Type,
41 pub eq_token: Option<Token![=]>,
42 pub default: Option<Expr>,
43 }),
David Tolnayc2f1aba2017-11-12 20:29:22 -080044 }
45}
46
David Tolnaye7678922016-10-13 20:44:03 -070047#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040048#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
49#[cfg_attr(feature = "clone-impls", derive(Clone))]
50/// Returned by `Generics::split_for_impl`.
51pub struct ImplGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070052
53#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040054#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
55#[cfg_attr(feature = "clone-impls", derive(Clone))]
56/// Returned by `Generics::split_for_impl`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080057pub struct TypeGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070058
David Tolnayc879a502017-01-25 15:51:32 -080059#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040060#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
61#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062/// Returned by `TypeGenerics::as_turbofish`.
Nika Layzell6b38b132017-10-24 23:09:39 -040063pub struct Turbofish<'a>(&'a Generics);
David Tolnayc879a502017-01-25 15:51:32 -080064
David Tolnaye95cc9f2017-01-25 15:57:09 -080065#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -070066impl Generics {
67 /// Split a type's generics into the pieces required for impl'ing a trait
68 /// for that type.
69 ///
70 /// ```
71 /// # extern crate syn;
72 /// # #[macro_use]
73 /// # extern crate quote;
74 /// # fn main() {
75 /// # let generics: syn::Generics = Default::default();
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 /// # let name = syn::Ident::from("MyType");
David Tolnayb153dbc2016-10-04 23:39:10 -070077 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
78 /// quote! {
79 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
80 /// // ...
81 /// }
82 /// }
83 /// # ;
84 /// # }
85 /// ```
David Tolnayac997dd2017-12-27 23:18:22 -050086 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
87 (ImplGenerics(self), TypeGenerics(self), self.where_clause.as_ref())
David Tolnayb153dbc2016-10-04 23:39:10 -070088 }
89}
90
David Tolnaye95cc9f2017-01-25 15:57:09 -080091#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -080093 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
94 pub fn as_turbofish(&self) -> Turbofish {
95 Turbofish(self.0)
96 }
97}
98
Alex Crichton62a0a592017-05-22 13:58:53 -070099ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 /// A set of bound lifetimes, e.g. `for<'a, 'b, 'c>`
101 #[derive(Default)]
102 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800103 pub for_token: Token![for],
104 pub lt_token: Token![<],
105 pub lifetimes: Delimited<LifetimeDef, Token![,]>,
106 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107 }
108}
109
David Tolnayf9505b52016-10-02 09:18:52 -0700110impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700111 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700112 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700113 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700114 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 colon_token: None,
116 bounds: Delimited::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700117 }
118 }
119}
120
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800121impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700122 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800123 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700124 attrs: vec![],
125 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 colon_token: None,
127 bounds: Delimited::new(),
128 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700129 default: None,
130 }
131 }
132}
133
Alex Crichton62a0a592017-05-22 13:58:53 -0700134ast_enum! {
135 /// The AST represents all type param bounds as types.
136 /// `typeck::collect::compute_bounds` matches these against
137 /// the "special" built-in traits (see `middle::lang_items`) and
138 /// detects Copy, Send and Sync.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub enum TypeParamBound {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 Trait(PolyTraitRef, TraitBoundModifier),
141 Region(Lifetime),
142 }
David Tolnay55337722016-09-11 12:58:56 -0700143}
144
Alex Crichton62a0a592017-05-22 13:58:53 -0700145ast_enum! {
146 /// A modifier on a bound, currently this is only used for `?Sized`, where the
147 /// modifier is `Maybe`. Negative bounds should also be handled here.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700148 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 pub enum TraitBoundModifier {
150 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800151 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 }
David Tolnay55337722016-09-11 12:58:56 -0700153}
154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155ast_struct! {
156 /// A `where` clause in a definition
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub struct WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500158 pub where_token: Token![where],
David Tolnayf8db7ba2017-11-11 22:52:16 -0800159 pub predicates: Delimited<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 }
David Tolnayb79ee962016-09-04 09:39:20 -0700161}
162
Alex Crichton62a0a592017-05-22 13:58:53 -0700163ast_enum_of_structs! {
164 /// A single predicate in a `where` clause
165 pub enum WherePredicate {
166 /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
167 pub BoundPredicate(WhereBoundPredicate {
168 /// Any lifetimes from a `for` binding
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700169 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800171 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800172 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800174 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700176
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// A lifetime predicate, e.g. `'a: 'b+'c`
178 pub RegionPredicate(WhereRegionPredicate {
179 pub lifetime: Lifetime,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800180 pub colon_token: Option<Token![:]>,
181 pub bounds: Delimited<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700183
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 /// An equality predicate (unsupported)
185 pub EqPredicate(WhereEqPredicate {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800186 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800188 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }),
190 }
David Tolnayf8e08832017-01-23 00:04:32 -0800191}
192
David Tolnay86eca752016-09-04 11:26:41 -0700193#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700194pub mod parsing {
195 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700196
David Tolnay63e3dee2017-06-03 20:13:17 -0700197 use synom::Synom;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500198 use delimited::Element;
Alex Crichton954046c2017-05-30 21:49:42 -0700199
200 impl Synom for Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400201 named!(parse -> Self, map!(
202 alt!(
203 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800204 lt: punct!(<) >>
David Tolnayc2f1aba2017-11-12 20:29:22 -0800205 lifetimes: call!(Delimited::<LifetimeDef, Token![,]>::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400206 ty_params: cond!(
David Tolnayc2f1aba2017-11-12 20:29:22 -0800207 lifetimes.empty_or_trailing(),
David Tolnaye64213b2017-12-30 00:24:20 -0500208 Delimited::<TypeParam, Token![,]>::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -0400209 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800210 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400211 (lifetimes, ty_params, Some(lt), Some(gt))
212 )
213 |
214 epsilon!() => { |_| (Delimited::new(), None, None, None) }
215 ),
David Tolnaybc7d7d92017-06-03 20:54:05 -0700216 |(lifetimes, ty_params, lt, gt)| Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400217 lt_token: lt,
David Tolnayc2f1aba2017-11-12 20:29:22 -0800218 params: lifetimes.into_iter()
219 .map(Element::into_tuple)
220 .map(|(lifetime, comma)| (GenericParam::Lifetime(lifetime), comma))
221 .chain(ty_params.unwrap_or_default()
222 .into_iter()
223 .map(Element::into_tuple)
224 .map(|(ty_param, comma)| (GenericParam::Type(ty_param), comma)))
225 .collect::<Vec<_>>()
226 .into(),
227 gt_token: gt,
David Tolnayac997dd2017-12-27 23:18:22 -0500228 where_clause: None,
Michael Layzell416724e2017-05-24 21:12:34 -0400229 }
Michael Layzell92639a52017-06-01 00:07:44 -0400230 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700231 }
232
Alex Crichton954046c2017-05-30 21:49:42 -0700233 impl Synom for LifetimeDef {
Michael Layzell92639a52017-06-01 00:07:44 -0400234 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500235 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400236 life: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800237 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400238 bounds: cond!(
239 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500240 Delimited::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400241 ) >>
242 (LifetimeDef {
243 attrs: attrs,
244 lifetime: life,
245 bounds: bounds.unwrap_or_default(),
David Tolnay6af8f1d2017-12-27 23:08:43 -0500246 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400247 })
248 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700249 }
250
251 impl Synom for BoundLifetimes {
Michael Layzell92639a52017-06-01 00:07:44 -0400252 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800253 for_: keyword!(for) >>
254 lt: punct!(<) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400255 lifetimes: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800256 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400257 (BoundLifetimes {
258 for_token: for_,
259 lt_token: lt,
260 gt_token: gt,
261 lifetimes: lifetimes,
262 })
263 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700264 }
David Tolnay55337722016-09-11 12:58:56 -0700265
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800266 impl Synom for TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400267 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500268 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400269 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800270 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400271 bounds: cond!(
272 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500273 Delimited::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400274 ) >>
275 default: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800276 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800277 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400278 (eq, ty)
279 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800280 (TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400281 attrs: attrs,
282 ident: id,
283 bounds: bounds.unwrap_or_default(),
284 colon_token: colon,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800285 eq_token: default.as_ref().map(|d| Token![=]((d.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400286 default: default.map(|d| d.1),
287 })
288 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700289 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700290
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800291 impl Synom for TypeParamBound {
Michael Layzell92639a52017-06-01 00:07:44 -0400292 named!(parse -> Self, alt!(
293 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800294 question: punct!(?) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400295 poly: syn!(PolyTraitRef) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800296 (TypeParamBound::Trait(poly, TraitBoundModifier::Maybe(question)))
Michael Layzell92639a52017-06-01 00:07:44 -0400297 )
298 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800299 syn!(Lifetime) => { TypeParamBound::Region }
Michael Layzell92639a52017-06-01 00:07:44 -0400300 |
301 syn!(PolyTraitRef) => {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800302 |poly| TypeParamBound::Trait(poly, TraitBoundModifier::None)
Alex Crichton954046c2017-05-30 21:49:42 -0700303 }
David Tolnayd9faf442017-12-25 23:36:05 -0500304 |
305 parens!(syn!(PolyTraitRef)) => {
306 |poly| TypeParamBound::Trait(poly.0, TraitBoundModifier::None)
307 }
Michael Layzell92639a52017-06-01 00:07:44 -0400308 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700309
310 fn description() -> Option<&'static str> {
Nika Layzellc691cb42017-12-04 13:44:38 -0500311 Some("type parameter bound")
Alex Crichton954046c2017-05-30 21:49:42 -0700312 }
313 }
314
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500315 impl Synom for ConstParam {
316 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500317 attrs: many0!(Attribute::parse_outer) >>
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500318 const_: keyword!(const) >>
319 ident: syn!(Ident) >>
320 colon: punct!(:) >>
321 ty: syn!(Type) >>
322 eq_def: option!(tuple!(punct!(=), syn!(Expr))) >>
David Tolnay78ee5202017-12-04 22:17:54 -0800323 ({
324 let (eq_token, default) = match eq_def {
325 Some((eq_token, default)) => (Some(eq_token), Some(default)),
326 None => (None, None),
327 };
328 ConstParam {
329 attrs: attrs,
330 const_token: const_,
331 ident: ident,
332 colon_token: colon,
333 ty: ty,
334 eq_token: eq_token,
335 default: default,
336 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500337 })
338 ));
339 }
340
Alex Crichton954046c2017-05-30 21:49:42 -0700341 impl Synom for WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500342 named!(parse -> Self, do_parse!(
343 where_: keyword!(where) >>
344 predicates: call!(Delimited::parse_terminated) >>
345 (WhereClause {
346 predicates: predicates,
347 where_token: where_,
348 })
Michael Layzell92639a52017-06-01 00:07:44 -0400349 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700350
351 fn description() -> Option<&'static str> {
352 Some("where clause")
353 }
354 }
355
356 impl Synom for WherePredicate {
Michael Layzell92639a52017-06-01 00:07:44 -0400357 named!(parse -> Self, alt!(
358 do_parse!(
359 ident: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400361 bounds: cond!(
362 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500363 Delimited::parse_separated
Michael Layzell92639a52017-06-01 00:07:44 -0400364 ) >>
365 (WherePredicate::RegionPredicate(WhereRegionPredicate {
366 lifetime: ident,
367 bounds: bounds.unwrap_or_default(),
368 colon_token: colon,
369 }))
370 )
371 |
372 do_parse!(
373 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800374 bounded_ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400376 bounds: call!(Delimited::parse_separated_nonempty) >>
377 (WherePredicate::BoundPredicate(WhereBoundPredicate {
378 bound_lifetimes: bound_lifetimes,
379 bounded_ty: bounded_ty,
380 bounds: bounds,
381 colon_token: colon,
382 }))
383 )
384 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700385 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700386}
David Tolnay87d0b442016-09-04 11:52:12 -0700387
388#[cfg(feature = "printing")]
389mod printing {
390 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700391 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500392 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700393
Michael Layzell3936ceb2017-07-08 00:28:36 -0400394 /// Returns true if the generics object has no lifetimes or ty_params.
395 fn empty_normal_generics(generics: &Generics) -> bool {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800396 generics.params.is_empty()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400397 }
398
David Tolnay8ef93042016-09-04 14:08:40 -0700399 impl ToTokens for Generics {
400 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400401 if empty_normal_generics(self) {
402 return;
403 }
404
Alex Crichton259ee532017-07-14 06:51:02 -0700405 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800406 self.params.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700407 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700408 }
409 }
410
David Tolnaye7678922016-10-13 20:44:03 -0700411 impl<'a> ToTokens for ImplGenerics<'a> {
412 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500413 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400414 return;
415 }
416
Alex Crichton259ee532017-07-14 06:51:02 -0700417 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800418 for param in self.0.params.iter() {
419 match **param.item() {
420 GenericParam::Lifetime(ref param) => {
421 param.to_tokens(tokens);
422 }
423 GenericParam::Type(ref param) => {
424 // Leave off the type parameter defaults
425 tokens.append_all(param.attrs.outer());
426 param.ident.to_tokens(tokens);
427 if !param.bounds.is_empty() {
428 TokensOrDefault(&param.colon_token).to_tokens(tokens);
429 param.bounds.to_tokens(tokens);
430 }
431 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500432 GenericParam::Const(ref param) => {
433 // Leave off the const parameter defaults
434 tokens.append_all(param.attrs.outer());
435 param.const_token.to_tokens(tokens);
436 param.ident.to_tokens(tokens);
437 param.colon_token.to_tokens(tokens);
438 param.ty.to_tokens(tokens);
439 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400440 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700441 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700442 }
Alex Crichton259ee532017-07-14 06:51:02 -0700443 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700444 }
445 }
446
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800447 impl<'a> ToTokens for TypeGenerics<'a> {
David Tolnaye7678922016-10-13 20:44:03 -0700448 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500449 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400450 return;
451 }
452
Alex Crichton259ee532017-07-14 06:51:02 -0700453 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800454 for param in self.0.params.iter() {
455 match **param.item() {
456 GenericParam::Lifetime(ref param) => {
457 // Leave off the lifetime bounds and attributes
458 param.lifetime.to_tokens(tokens);
459 }
460 GenericParam::Type(ref param) => {
461 // Leave off the type parameter defaults
462 param.ident.to_tokens(tokens);
463 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500464 GenericParam::Const(ref param) => {
465 // Leave off the const parameter defaults
466 param.ident.to_tokens(tokens);
467 }
David Tolnayc2f1aba2017-11-12 20:29:22 -0800468 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700469 param.delimiter().to_tokens(tokens);
470 }
Alex Crichton259ee532017-07-14 06:51:02 -0700471 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700472 }
473 }
474
David Tolnayc879a502017-01-25 15:51:32 -0800475 impl<'a> ToTokens for Turbofish<'a> {
476 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500477 if !empty_normal_generics(self.0) {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800478 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800479 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800480 }
481 }
482 }
483
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700484 impl ToTokens for BoundLifetimes {
485 fn to_tokens(&self, tokens: &mut Tokens) {
486 self.for_token.to_tokens(tokens);
487 self.lt_token.to_tokens(tokens);
488 self.lifetimes.to_tokens(tokens);
489 self.gt_token.to_tokens(tokens);
490 }
491 }
492
David Tolnay87d0b442016-09-04 11:52:12 -0700493 impl ToTokens for LifetimeDef {
494 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700495 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700496 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400497 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700498 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400499 self.bounds.to_tokens(tokens);
500 }
David Tolnay87d0b442016-09-04 11:52:12 -0700501 }
502 }
503
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800504 impl ToTokens for TypeParam {
David Tolnay8ef93042016-09-04 14:08:40 -0700505 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700506 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700507 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400508 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700509 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400510 self.bounds.to_tokens(tokens);
511 }
512 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -0700513 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400514 self.default.to_tokens(tokens);
515 }
David Tolnay8ef93042016-09-04 14:08:40 -0700516 }
517 }
518
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800519 impl ToTokens for TypeParamBound {
David Tolnay87d0b442016-09-04 11:52:12 -0700520 fn to_tokens(&self, tokens: &mut Tokens) {
521 match *self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800522 TypeParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens),
523 TypeParamBound::Trait(ref trait_ref, ref modifier) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700524 modifier.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700525 trait_ref.to_tokens(tokens);
526 }
527 }
528 }
529 }
530
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700531 impl ToTokens for TraitBoundModifier {
532 fn to_tokens(&self, tokens: &mut Tokens) {
533 match *self {
534 TraitBoundModifier::None => {}
535 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
536 }
537 }
538 }
539
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500540 impl ToTokens for ConstParam {
541 fn to_tokens(&self, tokens: &mut Tokens) {
542 tokens.append_all(self.attrs.outer());
543 self.const_token.to_tokens(tokens);
544 self.ident.to_tokens(tokens);
545 self.colon_token.to_tokens(tokens);
546 self.ty.to_tokens(tokens);
547 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -0800548 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500549 self.default.to_tokens(tokens);
550 }
551 }
552 }
553
David Tolnay55337722016-09-11 12:58:56 -0700554 impl ToTokens for WhereClause {
555 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayac997dd2017-12-27 23:18:22 -0500556 self.where_token.to_tokens(tokens);
557 self.predicates.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700558 }
559 }
David Tolnay8ef93042016-09-04 14:08:40 -0700560
David Tolnay8ef93042016-09-04 14:08:40 -0700561 impl ToTokens for WhereBoundPredicate {
562 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700563 self.bound_lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700564 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 self.colon_token.to_tokens(tokens);
566 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700567 }
568 }
569
570 impl ToTokens for WhereRegionPredicate {
571 fn to_tokens(&self, tokens: &mut Tokens) {
572 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400573 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700574 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400575 self.bounds.to_tokens(tokens);
576 }
David Tolnay8ef93042016-09-04 14:08:40 -0700577 }
578 }
David Tolnayf8e08832017-01-23 00:04:32 -0800579
580 impl ToTokens for WhereEqPredicate {
581 fn to_tokens(&self, tokens: &mut Tokens) {
582 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700583 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800584 self.rhs_ty.to_tokens(tokens);
585 }
586 }
David Tolnay87d0b442016-09-04 11:52:12 -0700587}