| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
// Based on: |
| 4 |
// - https://github.com/dtolnay/syn/blob/1.0.70/src/item.rs |
| 5 |
// - https://github.com/dtolnay/syn/blob/1.0.70/src/generics.rs |
| 6 |
|
| 7 |
use std::fmt; |
| 8 |
|
| 9 |
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree}; |
| 10 |
|
| 11 |
#[derive(Clone)] |
| 12 |
pub(crate) struct Lifetime { |
| 13 |
pub(crate) apostrophe: Span, |
| 14 |
pub(crate) ident: Ident, |
| 15 |
} |
| 16 |
|
| 17 |
#[derive(Clone, Default)] |
| 18 |
pub(crate) struct Generics { |
| 19 |
pub(crate) lt_token: Option<Punct>, |
| 20 |
pub(crate) params: Vec<(GenericParam, Option<Punct>)>, |
| 21 |
pub(crate) gt_token: Option<Punct>, |
| 22 |
pub(crate) where_clause: Option<WhereClause>, |
| 23 |
} |
| 24 |
|
| 25 |
impl Generics { |
| 26 |
pub(crate) fn make_where_clause(&mut self) -> &mut WhereClause { |
| 27 |
self.where_clause.get_or_insert_with(|| WhereClause { |
| 28 |
where_token: Ident::new("where", Span::call_site()), |
| 29 |
predicates: vec![], |
| 30 |
}) |
| 31 |
} |
| 32 |
|
| 33 |
pub(crate) fn impl_generics(&self) -> ImplGenerics<'_> { |
| 34 |
ImplGenerics(self) |
| 35 |
} |
| 36 |
|
| 37 |
pub(crate) fn ty_generics(&self) -> TypeGenerics<'_> { |
| 38 |
TypeGenerics(self) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[derive(Clone)] |
| 43 |
pub(crate) enum GenericParam { |
| 44 |
/// A generic type parameter: `T: Into<String>`. |
| 45 |
Type(TypeParam), |
| 46 |
/// A lifetime definition: `'a: 'b + 'c + 'd`. |
| 47 |
Lifetime(LifetimeDef), |
| 48 |
/// A const generic parameter: `const LENGTH: usize`. |
| 49 |
Const(ConstParam), |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Clone)] |
| 53 |
pub(crate) struct TypeParam { |
| 54 |
pub(crate) attrs: Vec<Attribute>, |
| 55 |
pub(crate) ident: Ident, |
| 56 |
pub(crate) colon_token: Option<Punct>, |
| 57 |
pub(crate) bounds: Vec<(TypeParamBound, Option<Punct>)>, |
| 58 |
pub(crate) eq_token: Option<Punct>, |
| 59 |
pub(crate) default: Option<TokenStream>, |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Clone)] |
| 63 |
pub(crate) struct TypeParamBound { |
| 64 |
pub(crate) tokens: TokenStream, |
| 65 |
pub(crate) is_maybe: bool, |
| 66 |
} |
| 67 |
|
| 68 |
impl TypeParamBound { |
| 69 |
fn new(tokens: Vec<TokenTree>, is_maybe: bool) -> Self { |
| 70 |
Self { tokens: tokens.into_iter().collect(), is_maybe } |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#[derive(Clone)] |
| 75 |
pub(crate) struct LifetimeDef { |
| 76 |
pub(crate) attrs: Vec<Attribute>, |
| 77 |
pub(crate) lifetime: Lifetime, |
| 78 |
pub(crate) colon_token: Option<Punct>, |
| 79 |
pub(crate) bounds: TokenStream, |
| 80 |
} |
| 81 |
|
| 82 |
#[derive(Clone)] |
| 83 |
pub(crate) struct BoundLifetimes { |
| 84 |
pub(crate) for_token: Ident, |
| 85 |
pub(crate) lt_token: Punct, |
| 86 |
pub(crate) lifetimes: Vec<(LifetimeDef, Option<Punct>)>, |
| 87 |
pub(crate) gt_token: Punct, |
| 88 |
} |
| 89 |
|
| 90 |
#[derive(Clone)] |
| 91 |
pub(crate) struct ConstParam { |
| 92 |
pub(crate) attrs: Vec<Attribute>, |
| 93 |
pub(crate) const_token: Ident, |
| 94 |
pub(crate) ident: Ident, |
| 95 |
pub(crate) colon_token: Punct, |
| 96 |
pub(crate) ty: TokenStream, |
| 97 |
pub(crate) eq_token: Option<Punct>, |
| 98 |
pub(crate) default: Option<TokenStream>, |
| 99 |
} |
| 100 |
|
| 101 |
pub(crate) struct ImplGenerics<'a>(&'a Generics); |
| 102 |
pub(crate) struct TypeGenerics<'a>(&'a Generics); |
| 103 |
|
| 104 |
#[derive(Clone)] |
| 105 |
pub(crate) struct WhereClause { |
| 106 |
pub(crate) where_token: Ident, |
| 107 |
pub(crate) predicates: Vec<(WherePredicate, Option<Punct>)>, |
| 108 |
} |
| 109 |
|
| 110 |
#[derive(Clone)] |
| 111 |
pub(crate) enum WherePredicate { |
| 112 |
Type(PredicateType), |
| 113 |
Lifetime(PredicateLifetime), |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Clone)] |
| 117 |
pub(crate) struct PredicateType { |
| 118 |
pub(crate) lifetimes: Option<BoundLifetimes>, |
| 119 |
pub(crate) bounded_ty: TokenStream, |
| 120 |
pub(crate) colon_token: Punct, |
| 121 |
pub(crate) bounds: Vec<(TypeParamBound, Option<Punct>)>, |
| 122 |
} |
| 123 |
|
| 124 |
#[derive(Clone)] |
| 125 |
pub(crate) struct PredicateLifetime { |
| 126 |
pub(crate) lifetime: Lifetime, |
| 127 |
pub(crate) colon_token: Punct, |
| 128 |
pub(crate) bounds: Vec<(Lifetime, Option<Punct>)>, |
| 129 |
} |
| 130 |
|
| 131 |
// Outer attribute |
| 132 |
#[derive(Clone)] |
| 133 |
pub(crate) struct Attribute { |
| 134 |
// `#` |
| 135 |
pub(crate) pound_token: Punct, |
| 136 |
// `[...]` |
| 137 |
pub(crate) tokens: Group, |
| 138 |
pub(crate) kind: AttributeKind, |
| 139 |
} |
| 140 |
|
| 141 |
#[derive(Clone, Copy, PartialEq)] |
| 142 |
pub(crate) enum AttributeKind { |
| 143 |
// #[doc ...] |
| 144 |
Doc, |
| 145 |
// #[inline ...] |
| 146 |
Inline, |
| 147 |
Other, |
| 148 |
} |
| 149 |
|
| 150 |
impl Attribute { |
| 151 |
pub(crate) fn new(tokens: Vec<TokenTree>) -> Self { |
| 152 |
Self { |
| 153 |
pound_token: Punct::new('#', Spacing::Alone), |
| 154 |
tokens: Group::new(Delimiter::Bracket, tokens.into_iter().collect()), |
| 155 |
kind: AttributeKind::Other, |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[derive(Clone)] |
| 161 |
pub(crate) enum Visibility { |
| 162 |
// `pub`. |
| 163 |
Public(Ident), |
| 164 |
//`pub(self)`, `pub(super)`, `pub(crate)`, or `pub(in some::module)` |
| 165 |
Restricted(Ident, Group), |
| 166 |
Inherited, |
| 167 |
} |
| 168 |
|
| 169 |
impl Visibility { |
| 170 |
pub(crate) fn is_inherited(&self) -> bool { |
| 171 |
match self { |
| 172 |
Visibility::Inherited => true, |
| 173 |
_ => false, |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
impl PartialEq for Visibility { |
| 179 |
fn eq(&self, other: &Self) -> bool { |
| 180 |
match (self, other) { |
| 181 |
(Visibility::Public(_), Visibility::Public(_)) |
| 182 |
| (Visibility::Inherited, Visibility::Inherited) => true, |
| 183 |
(Visibility::Restricted(_, x), Visibility::Restricted(_, y)) => { |
| 184 |
x.stream().to_string() == y.stream().to_string() |
| 185 |
} |
| 186 |
_ => false, |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
impl fmt::Display for Visibility { |
| 192 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 193 |
match self { |
| 194 |
Visibility::Public(_) => f.write_str("pub"), |
| 195 |
Visibility::Inherited => Ok(()), |
| 196 |
Visibility::Restricted(_, g) => write!(f, "pub{}", g), |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
pub(crate) struct ItemImpl { |
| 202 |
pub(crate) attrs: Vec<Attribute>, |
| 203 |
pub(crate) vis: Visibility, |
| 204 |
pub(crate) defaultness: Option<Ident>, |
| 205 |
pub(crate) unsafety: Option<Ident>, |
| 206 |
pub(crate) impl_token: Ident, |
| 207 |
pub(crate) generics: Generics, |
| 208 |
pub(crate) const_token: Option<Ident>, |
| 209 |
pub(crate) trait_: Option<(Ident, TokenStream, Ident)>, |
| 210 |
pub(crate) self_ty: Vec<TokenTree>, |
| 211 |
pub(crate) brace_token: Span, |
| 212 |
pub(crate) items: Vec<ImplItem>, |
| 213 |
} |
| 214 |
|
| 215 |
pub(crate) enum ImplItem { |
| 216 |
Const(ImplItemConst), |
| 217 |
Method(ImplItemMethod), |
| 218 |
Type(ImplItemType), |
| 219 |
} |
| 220 |
|
| 221 |
pub(crate) struct ImplItemConst { |
| 222 |
pub(crate) attrs: Vec<Attribute>, |
| 223 |
pub(crate) vis: Visibility, |
| 224 |
pub(crate) defaultness: Option<Ident>, |
| 225 |
pub(crate) const_token: Ident, |
| 226 |
pub(crate) ident: Ident, |
| 227 |
pub(crate) colon_token: Punct, |
| 228 |
pub(crate) ty: TokenStream, |
| 229 |
pub(crate) eq_token: Punct, |
| 230 |
pub(crate) expr: Vec<TokenTree>, |
| 231 |
pub(crate) semi_token: Punct, |
| 232 |
} |
| 233 |
|
| 234 |
pub(crate) struct ImplItemMethod { |
| 235 |
pub(crate) attrs: Vec<Attribute>, |
| 236 |
pub(crate) vis: Visibility, |
| 237 |
pub(crate) defaultness: Option<Ident>, |
| 238 |
pub(crate) sig: Signature, |
| 239 |
pub(crate) body: Group, |
| 240 |
} |
| 241 |
|
| 242 |
pub(crate) struct ImplItemType { |
| 243 |
pub(crate) attrs: Vec<Attribute>, |
| 244 |
pub(crate) vis: Visibility, |
| 245 |
pub(crate) defaultness: Option<Ident>, |
| 246 |
pub(crate) type_token: Ident, |
| 247 |
pub(crate) ident: Ident, |
| 248 |
pub(crate) generics: Generics, |
| 249 |
pub(crate) eq_token: Punct, |
| 250 |
pub(crate) ty: Vec<TokenTree>, |
| 251 |
pub(crate) semi_token: Punct, |
| 252 |
} |
| 253 |
|
| 254 |
#[derive(Clone)] |
| 255 |
pub(crate) struct Signature { |
| 256 |
// [const] [async] [unsafe] [extern [<abi>]] fn |
| 257 |
pub(crate) before_ident: Vec<TokenTree>, |
| 258 |
pub(crate) ident: Ident, |
| 259 |
pub(crate) generics: Generics, |
| 260 |
pub(crate) paren_token: Span, |
| 261 |
pub(crate) inputs: Vec<FnArg>, |
| 262 |
pub(crate) output: Option<TokenStream>, |
| 263 |
} |
| 264 |
|
| 265 |
#[derive(Clone)] |
| 266 |
pub(crate) enum FnArg { |
| 267 |
Receiver(TokenStream, Option<TokenTree>), |
| 268 |
Typed(TokenStream, Punct, TokenStream, Option<TokenTree>), |
| 269 |
} |
| 270 |
|
| 271 |
pub(crate) struct ItemTrait { |
| 272 |
pub(crate) attrs: Vec<Attribute>, |
| 273 |
pub(crate) vis: Visibility, |
| 274 |
pub(crate) unsafety: Option<Ident>, |
| 275 |
pub(crate) trait_token: Ident, |
| 276 |
pub(crate) ident: Ident, |
| 277 |
pub(crate) generics: Generics, |
| 278 |
pub(crate) brace_token: Span, |
| 279 |
pub(crate) items: Vec<TraitItem>, |
| 280 |
} |
| 281 |
|
| 282 |
pub(crate) enum TraitItem { |
| 283 |
Const(TraitItemConst), |
| 284 |
Method(TraitItemMethod), |
| 285 |
Type(TraitItemType), |
| 286 |
} |
| 287 |
|
| 288 |
pub(crate) struct TraitItemConst { |
| 289 |
pub(crate) attrs: Vec<Attribute>, |
| 290 |
pub(crate) const_token: Ident, |
| 291 |
pub(crate) ident: Ident, |
| 292 |
pub(crate) colon_token: Punct, |
| 293 |
pub(crate) ty: TokenStream, |
| 294 |
pub(crate) semi_token: Punct, |
| 295 |
} |
| 296 |
|
| 297 |
pub(crate) struct TraitItemMethod { |
| 298 |
pub(crate) attrs: Vec<Attribute>, |
| 299 |
pub(crate) sig: Signature, |
| 300 |
pub(crate) semi_token: Punct, |
| 301 |
} |
| 302 |
|
| 303 |
pub(crate) struct TraitItemType { |
| 304 |
pub(crate) attrs: Vec<Attribute>, |
| 305 |
pub(crate) type_token: Ident, |
| 306 |
pub(crate) ident: Ident, |
| 307 |
pub(crate) generics: Generics, |
| 308 |
pub(crate) semi_token: Punct, |
| 309 |
} |
| 310 |
|
| 311 |
pub(crate) mod parsing { |
| 312 |
use std::iter::FromIterator; |
| 313 |
|
| 314 |
use proc_macro::{Delimiter, Punct, Spacing, TokenStream, TokenTree}; |
| 315 |
|
| 316 |
use super::{ |
| 317 |
Attribute, AttributeKind, BoundLifetimes, ConstParam, FnArg, GenericParam, Generics, |
| 318 |
ImplItem, ImplItemConst, ImplItemMethod, ImplItemType, ItemImpl, Lifetime, LifetimeDef, |
| 319 |
PredicateLifetime, PredicateType, Signature, TypeParam, TypeParamBound, Visibility, |
| 320 |
WhereClause, WherePredicate, |
| 321 |
}; |
| 322 |
use crate::{error::Result, iter::TokenIter, to_tokens::ToTokens}; |
| 323 |
|
| 324 |
fn parse_until_punct(input: &mut TokenIter, ch: char) -> Result<(Vec<TokenTree>, Punct)> { |
| 325 |
let mut buf = vec![]; |
| 326 |
loop { |
| 327 |
let tt = input.next(); |
| 328 |
match tt { |
| 329 |
Some(TokenTree::Punct(ref p)) |
| 330 |
if p.as_char() == ch && p.spacing() == Spacing::Alone => |
| 331 |
{ |
| 332 |
if let Some(TokenTree::Punct(p)) = tt { |
| 333 |
return Ok((buf, p)); |
| 334 |
} |
| 335 |
unreachable!(); |
| 336 |
} |
| 337 |
None => { |
| 338 |
// TODO: pass scope span |
| 339 |
bail!(TokenStream::new(), "expected `{}`", ch); |
| 340 |
} |
| 341 |
Some(tt) => buf.push(tt), |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
fn append_tokens_until( |
| 347 |
input: &mut TokenIter, |
| 348 |
buf: &mut Vec<TokenTree>, |
| 349 |
visit_first_angle_bracket: bool, |
| 350 |
f: fn(Option<&TokenTree>) -> bool, |
| 351 |
) -> Result<()> { |
| 352 |
let mut angle_bracket: i32 = 0 - visit_first_angle_bracket as i32; |
| 353 |
loop { |
| 354 |
let tt = input.peek(); |
| 355 |
match tt { |
| 356 |
Some(TokenTree::Punct(p)) if p.as_char() == '<' => { |
| 357 |
angle_bracket += 1; |
| 358 |
} |
| 359 |
Some(TokenTree::Punct(p)) if p.as_char() == '>' => { |
| 360 |
match buf.last() { |
| 361 |
Some(TokenTree::Punct(p)) |
| 362 |
if p.as_char() == '-' && p.spacing() == Spacing::Joint => |
| 363 |
{ |
| 364 |
// `->` |
| 365 |
// It's so confusing with `>`, so do not visit it. |
| 366 |
buf.push(input.next().unwrap()); |
| 367 |
continue; |
| 368 |
} |
| 369 |
_ => {} |
| 370 |
} |
| 371 |
angle_bracket -= 1; |
| 372 |
if angle_bracket >= 0 { |
| 373 |
buf.push(input.next().unwrap()); |
| 374 |
continue; |
| 375 |
} |
| 376 |
} |
| 377 |
Some(_) | None => {} |
| 378 |
} |
| 379 |
if angle_bracket <= 0 && f(tt) { |
| 380 |
return Ok(()); |
| 381 |
} |
| 382 |
buf.push(input.next().ok_or_else(|| { |
| 383 |
// TODO: pass scope span |
| 384 |
format_err!(TokenStream::new(), "unexpected end of input") |
| 385 |
})?); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
fn parse_attrs(input: &mut TokenIter) -> Result<Vec<Attribute>> { |
| 390 |
let mut attrs = vec![]; |
| 391 |
while input.peek_t(&'#') { |
| 392 |
let pound_token = input.parse_punct('#')?; |
| 393 |
let tokens = input.parse_group(Delimiter::Bracket)?; |
| 394 |
let mut kind = AttributeKind::Other; |
| 395 |
let mut iter = TokenIter::new(tokens.stream()); |
| 396 |
if let Some(TokenTree::Ident(i)) = iter.next() { |
| 397 |
match iter.next() { |
| 398 |
// ignore #[path ...] |
| 399 |
Some(TokenTree::Punct(ref p)) if p.as_char() == ':' => {} |
| 400 |
_ => match &*i.to_string() { |
| 401 |
"doc" => kind = AttributeKind::Doc, |
| 402 |
"inline" => kind = AttributeKind::Inline, |
| 403 |
_ => {} |
| 404 |
}, |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
let attr = Attribute { pound_token, tokens, kind }; |
| 409 |
attrs.push(attr); |
| 410 |
} |
| 411 |
Ok(attrs) |
| 412 |
} |
| 413 |
|
| 414 |
fn parse_generics(input: &mut TokenIter) -> Result<Generics> { |
| 415 |
if !input.peek_t(&'<') { |
| 416 |
return Ok(Generics::default()); |
| 417 |
} |
| 418 |
|
| 419 |
let lt_token = input.parse_punct('<')?; |
| 420 |
|
| 421 |
let mut params = vec![]; |
| 422 |
loop { |
| 423 |
if input.peek_t(&'>') { |
| 424 |
break; |
| 425 |
} |
| 426 |
|
| 427 |
let attrs = parse_attrs(input)?; |
| 428 |
let value = if input.peek_lifetime() { |
| 429 |
GenericParam::Lifetime(LifetimeDef { attrs, ..parse_lifetime_def(input)? }) |
| 430 |
} else if input.peek_t(&"const") { |
| 431 |
GenericParam::Const(ConstParam { attrs, ..parse_const_param(input)? }) |
| 432 |
} else if input.peek_t(&"_") { |
| 433 |
GenericParam::Type(TypeParam { |
| 434 |
attrs, |
| 435 |
ident: input.parse_ident()?, |
| 436 |
colon_token: None, |
| 437 |
bounds: vec![], |
| 438 |
eq_token: None, |
| 439 |
default: None, |
| 440 |
}) |
| 441 |
} else if input.peek_ident().is_some() { |
| 442 |
GenericParam::Type(TypeParam { attrs, ..parse_type_param(input)? }) |
| 443 |
} else { |
| 444 |
bail!(input.next(), "expected one of: lifetime, identifier, `const`, `_`"); |
| 445 |
}; |
| 446 |
|
| 447 |
if input.peek_t(&'>') { |
| 448 |
params.push((value, None)); |
| 449 |
break; |
| 450 |
} |
| 451 |
let punct = input.parse_punct(',')?; |
| 452 |
params.push((value, Some(punct))); |
| 453 |
} |
| 454 |
|
| 455 |
let gt_token = input.parse_punct('>')?; |
| 456 |
|
| 457 |
Ok(Generics { |
| 458 |
lt_token: Some(lt_token), |
| 459 |
params, |
| 460 |
gt_token: Some(gt_token), |
| 461 |
where_clause: None, |
| 462 |
}) |
| 463 |
} |
| 464 |
|
| 465 |
fn parse_lifetime(input: &mut TokenIter) -> Result<Lifetime> { |
| 466 |
let tt = input.next(); |
| 467 |
match &tt { |
| 468 |
Some(TokenTree::Punct(p)) if p.as_char() == '\'' && p.spacing() == Spacing::Joint => { |
| 469 |
match input.next() { |
| 470 |
Some(TokenTree::Ident(ident)) => Ok(Lifetime { apostrophe: p.span(), ident }), |
| 471 |
Some(tt2) => { |
| 472 |
bail!(TokenStream::from_iter(vec![tt.unwrap(), tt2]), "expected lifetime") |
| 473 |
} |
| 474 |
None => bail!(p, "expected lifetime"), |
| 475 |
} |
| 476 |
} |
| 477 |
// TODO: pass scope span if tt is None |
| 478 |
tt => bail!(tt, "expected lifetime"), |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
fn parse_lifetime_def(input: &mut TokenIter) -> Result<LifetimeDef> { |
| 483 |
let attrs = parse_attrs(input)?; |
| 484 |
let lifetime = parse_lifetime(input)?; |
| 485 |
let colon_token = input.parse_punct_opt(':'); |
| 486 |
|
| 487 |
let mut bounds = TokenStream::new(); |
| 488 |
if colon_token.is_some() { |
| 489 |
loop { |
| 490 |
if input.peek_t(&',') || input.peek_t(&'>') { |
| 491 |
break; |
| 492 |
} |
| 493 |
let value = parse_lifetime(input)?; |
| 494 |
value.to_tokens(&mut bounds); |
| 495 |
if !input.peek_t(&'+') { |
| 496 |
break; |
| 497 |
} |
| 498 |
let punct = input.parse_punct('+')?; |
| 499 |
punct.to_tokens(&mut bounds); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
Ok(LifetimeDef { attrs, lifetime, colon_token, bounds }) |
| 504 |
} |
| 505 |
|
| 506 |
fn parse_bound_lifetimes(input: &mut TokenIter) -> Result<BoundLifetimes> { |
| 507 |
Ok(BoundLifetimes { |
| 508 |
for_token: input.parse_kw("for")?, |
| 509 |
lt_token: input.parse_punct('<')?, |
| 510 |
lifetimes: { |
| 511 |
let mut lifetimes = vec![]; |
| 512 |
while !input.peek_t(&'>') { |
| 513 |
let lifetime = parse_lifetime_def(input)?; |
| 514 |
if input.peek_t(&'>') { |
| 515 |
lifetimes.push((lifetime, None)); |
| 516 |
break; |
| 517 |
} |
| 518 |
let punct = input.parse_punct(',')?; |
| 519 |
lifetimes.push((lifetime, Some(punct))); |
| 520 |
} |
| 521 |
lifetimes |
| 522 |
}, |
| 523 |
gt_token: input.parse_punct('>')?, |
| 524 |
}) |
| 525 |
} |
| 526 |
|
| 527 |
fn parse_type_param(input: &mut TokenIter) -> Result<TypeParam> { |
| 528 |
let attrs = parse_attrs(input)?; |
| 529 |
let ident = input.parse_ident()?; |
| 530 |
let colon_token = input.parse_punct_opt(':'); |
| 531 |
|
| 532 |
let mut bounds = vec![]; |
| 533 |
if colon_token.is_some() { |
| 534 |
loop { |
| 535 |
if input.peek_t(&',') || input.peek_t(&'>') || input.peek_t(&'=') { |
| 536 |
break; |
| 537 |
} |
| 538 |
|
| 539 |
let is_maybe = input.peek_t(&'?') && !input.peek2_t(&"const"); |
| 540 |
let mut value = vec![]; |
| 541 |
append_tokens_until(input, &mut value, false, |next| match next { |
| 542 |
Some(TokenTree::Punct(p)) |
| 543 |
if p.as_char() == ',' |
| 544 |
|| p.as_char() == '>' |
| 545 |
|| p.as_char() == '=' |
| 546 |
|| p.as_char() == '+' => |
| 547 |
{ |
| 548 |
true |
| 549 |
} |
| 550 |
None => true, |
| 551 |
_ => false, |
| 552 |
})?; |
| 553 |
if !input.peek_t(&'+') { |
| 554 |
bounds.push((TypeParamBound::new(value, is_maybe), None)); |
| 555 |
break; |
| 556 |
} |
| 557 |
let punct = input.parse_punct('+')?; |
| 558 |
bounds.push((TypeParamBound::new(value, is_maybe), Some(punct))); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
let mut default = None; |
| 563 |
let eq_token = input.parse_punct_opt('='); |
| 564 |
if eq_token.is_some() { |
| 565 |
default = Some({ |
| 566 |
let mut ty = vec![]; |
| 567 |
append_tokens_until(input, &mut ty, false, |next| match next { |
| 568 |
Some(TokenTree::Punct(p)) if p.as_char() == '>' || p.as_char() == ',' => true, |
| 569 |
None => true, |
| 570 |
_ => false, |
| 571 |
})?; |
| 572 |
ty.into_iter().collect() |
| 573 |
}); |
| 574 |
} |
| 575 |
|
| 576 |
Ok(TypeParam { attrs, ident, colon_token, bounds, eq_token, default }) |
| 577 |
} |
| 578 |
|
| 579 |
fn const_argument(input: &mut TokenIter) -> Result<TokenTree> { |
| 580 |
let tt = input.next(); |
| 581 |
match &tt { |
| 582 |
Some(TokenTree::Literal(_)) | Some(TokenTree::Ident(_)) => Ok(tt.unwrap()), |
| 583 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => Ok(tt.unwrap()), |
| 584 |
// TODO: pass scope span if tt is None |
| 585 |
_ => bail!(tt, "expected one of: literal, ident, `{`"), |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
fn parse_const_param(input: &mut TokenIter) -> Result<ConstParam> { |
| 590 |
let attrs = parse_attrs(input)?; |
| 591 |
let const_token = input.parse_kw("const")?; |
| 592 |
let ident = input.parse_ident()?; |
| 593 |
let colon_token = input.parse_punct(':')?; |
| 594 |
|
| 595 |
let mut ty = vec![]; |
| 596 |
append_tokens_until(input, &mut ty, false, |next| match next { |
| 597 |
Some(TokenTree::Punct(p)) |
| 598 |
if p.as_char() == '>' |
| 599 |
|| p.as_char() == '=' && p.spacing() == Spacing::Alone |
| 600 |
|| p.as_char() == ',' && p.spacing() == Spacing::Alone => |
| 601 |
{ |
| 602 |
true |
| 603 |
} |
| 604 |
None => true, |
| 605 |
_ => false, |
| 606 |
})?; |
| 607 |
let mut default = None; |
| 608 |
let eq_token = if input.peek_t(&'=') { |
| 609 |
let eq_token = input.parse_punct('=')?; |
| 610 |
default = Some(std::iter::once(const_argument(input)?).collect()); |
| 611 |
Some(eq_token) |
| 612 |
} else { |
| 613 |
None |
| 614 |
}; |
| 615 |
|
| 616 |
Ok(ConstParam { |
| 617 |
attrs, |
| 618 |
const_token, |
| 619 |
ident, |
| 620 |
colon_token, |
| 621 |
ty: ty.into_iter().collect(), |
| 622 |
eq_token, |
| 623 |
default, |
| 624 |
}) |
| 625 |
} |
| 626 |
|
| 627 |
fn parse_where_clause(input: &mut TokenIter) -> Result<WhereClause> { |
| 628 |
let where_token = input.parse_kw("where")?; |
| 629 |
let mut predicates = vec![]; |
| 630 |
loop { |
| 631 |
if input.is_empty() |
| 632 |
|| input.peek_t(&Delimiter::Brace) |
| 633 |
|| input.peek_t(&',') |
| 634 |
|| input.peek_t(&';') |
| 635 |
|| input.peek_t(&':') && !input.peek2_t(&':') |
| 636 |
|| input.peek_t(&'=') |
| 637 |
{ |
| 638 |
break; |
| 639 |
} |
| 640 |
let value = parse_where_predicate(input)?; |
| 641 |
if !input.peek_t(&',') { |
| 642 |
predicates.push((value, None)); |
| 643 |
break; |
| 644 |
} |
| 645 |
let punct = input.parse_punct(',')?; |
| 646 |
predicates.push((value, Some(punct))); |
| 647 |
} |
| 648 |
Ok(WhereClause { where_token, predicates }) |
| 649 |
} |
| 650 |
|
| 651 |
fn parse_where_predicate(input: &mut TokenIter) -> Result<WherePredicate> { |
| 652 |
if input.peek_lifetime() && input.peek3_t(&':') { |
| 653 |
Ok(WherePredicate::Lifetime(PredicateLifetime { |
| 654 |
lifetime: parse_lifetime(input)?, |
| 655 |
colon_token: input.parse_punct(':')?, |
| 656 |
bounds: { |
| 657 |
let mut bounds = vec![]; |
| 658 |
loop { |
| 659 |
if input.is_empty() |
| 660 |
|| input.peek_t(&Delimiter::Brace) |
| 661 |
|| input.peek_t(&',') |
| 662 |
|| input.peek_t(&';') |
| 663 |
|| input.peek_t(&':') |
| 664 |
|| input.peek_t(&'=') |
| 665 |
{ |
| 666 |
break; |
| 667 |
} |
| 668 |
let value = parse_lifetime(input)?; |
| 669 |
if !input.peek_t(&'+') { |
| 670 |
bounds.push((value, None)); |
| 671 |
break; |
| 672 |
} |
| 673 |
let punct = input.parse_punct('+')?; |
| 674 |
bounds.push((value, Some(punct))); |
| 675 |
} |
| 676 |
bounds |
| 677 |
}, |
| 678 |
})) |
| 679 |
} else { |
| 680 |
Ok(WherePredicate::Type(PredicateType { |
| 681 |
lifetimes: { |
| 682 |
if input.peek_t(&"for") { Some(parse_bound_lifetimes(input)?) } else { None } |
| 683 |
}, |
| 684 |
bounded_ty: { |
| 685 |
let mut ty = vec![]; |
| 686 |
append_tokens_until(input, &mut ty, false, |next| match next { |
| 687 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => true, |
| 688 |
Some(TokenTree::Punct(p)) |
| 689 |
if p.as_char() == ',' |
| 690 |
|| p.as_char() == '=' && p.spacing() == Spacing::Alone |
| 691 |
|| p.as_char() == ':' && p.spacing() == Spacing::Alone => |
| 692 |
{ |
| 693 |
true |
| 694 |
} |
| 695 |
_ => false, |
| 696 |
})?; |
| 697 |
ty.into_iter().collect() |
| 698 |
}, |
| 699 |
colon_token: input.parse_punct(':')?, |
| 700 |
bounds: { |
| 701 |
let mut bounds = vec![]; |
| 702 |
loop { |
| 703 |
if input.is_empty() |
| 704 |
|| input.peek_t(&Delimiter::Brace) |
| 705 |
|| input.peek_t(&',') |
| 706 |
|| input.peek_t(&';') |
| 707 |
|| input.peek_t(&':') && !input.peek2_t(&':') |
| 708 |
|| input.peek_t(&'=') |
| 709 |
{ |
| 710 |
break; |
| 711 |
} |
| 712 |
|
| 713 |
let is_maybe = input.peek_t(&'?') && !input.peek2_t(&"const"); |
| 714 |
let mut value = vec![]; |
| 715 |
append_tokens_until(input, &mut value, false, |next| match next { |
| 716 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => true, |
| 717 |
Some(TokenTree::Punct(p)) |
| 718 |
if p.as_char() == ',' |
| 719 |
|| p.as_char() == '>' |
| 720 |
|| p.as_char() == '=' |
| 721 |
|| p.as_char() == '+' => |
| 722 |
{ |
| 723 |
true |
| 724 |
} |
| 725 |
None => true, |
| 726 |
_ => false, |
| 727 |
})?; |
| 728 |
if !input.peek_t(&'+') { |
| 729 |
bounds.push((TypeParamBound::new(value, is_maybe), None)); |
| 730 |
break; |
| 731 |
} |
| 732 |
let punct = input.parse_punct('+')?; |
| 733 |
bounds.push((TypeParamBound::new(value, is_maybe), Some(punct))); |
| 734 |
} |
| 735 |
bounds |
| 736 |
}, |
| 737 |
})) |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
pub(crate) fn parse_visibility(input: &mut TokenIter) -> Result<Visibility> { |
| 742 |
if input.peek_t(&"pub") { |
| 743 |
let pub_token = input.parse_ident()?; |
| 744 |
match input.peek() { |
| 745 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis {} => { |
| 746 |
let g = input.parse_group(Delimiter::Parenthesis)?; |
| 747 |
Ok(Visibility::Restricted(pub_token, g)) |
| 748 |
} |
| 749 |
_ => Ok(Visibility::Public(pub_token)), |
| 750 |
} |
| 751 |
} else { |
| 752 |
Ok(Visibility::Inherited) |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
pub(crate) fn parse_inputs(input: TokenStream) -> Result<Vec<FnArg>> { |
| 757 |
let input = &mut TokenIter::new(input); |
| 758 |
let mut inputs = vec![]; |
| 759 |
|
| 760 |
loop { |
| 761 |
let mut pat = vec![]; |
| 762 |
append_tokens_until(input, &mut pat, false, |next| match next { |
| 763 |
Some(TokenTree::Punct(p)) if p.as_char() == ',' || p.as_char() == ':' => true, |
| 764 |
None => true, |
| 765 |
_ => false, |
| 766 |
})?; |
| 767 |
if !input.peek_t(&':') { |
| 768 |
if input.peek_t(&',') { |
| 769 |
inputs.push(FnArg::Receiver( |
| 770 |
pat.into_iter().collect(), |
| 771 |
Some(input.next().unwrap()), |
| 772 |
)); |
| 773 |
continue; |
| 774 |
} |
| 775 |
assert!(input.next().is_none()); |
| 776 |
inputs.push(FnArg::Receiver(pat.into_iter().collect(), None)); |
| 777 |
break; |
| 778 |
} |
| 779 |
let colon = input.parse_punct(':')?; |
| 780 |
let mut ty = vec![]; |
| 781 |
append_tokens_until(input, &mut ty, false, |next| match next { |
| 782 |
Some(TokenTree::Punct(p)) if p.as_char() == ',' => true, |
| 783 |
None => true, |
| 784 |
_ => false, |
| 785 |
})?; |
| 786 |
if input.peek_t(&',') { |
| 787 |
inputs.push(FnArg::Typed( |
| 788 |
pat.into_iter().collect(), |
| 789 |
colon, |
| 790 |
ty.into_iter().collect(), |
| 791 |
Some(input.next().unwrap()), |
| 792 |
)); |
| 793 |
continue; |
| 794 |
} |
| 795 |
assert!(input.next().is_none()); |
| 796 |
inputs.push(FnArg::Typed( |
| 797 |
pat.into_iter().collect(), |
| 798 |
colon, |
| 799 |
ty.into_iter().collect(), |
| 800 |
None, |
| 801 |
)); |
| 802 |
break; |
| 803 |
} |
| 804 |
|
| 805 |
Ok(inputs) |
| 806 |
} |
| 807 |
|
| 808 |
pub(crate) fn parse_impl(input: &mut TokenIter) -> Result<ItemImpl> { |
| 809 |
let attrs = parse_attrs(input)?; |
| 810 |
let vis: Visibility = parse_visibility(input)?; |
| 811 |
let defaultness = input.parse_kw_opt("default"); |
| 812 |
let unsafety = input.parse_kw_opt("unsafe"); |
| 813 |
let impl_token = input.parse_kw("impl")?; |
| 814 |
|
| 815 |
let has_generics = input.peek_t(&'<') |
| 816 |
&& (input.peek2_t(&'>') |
| 817 |
|| input.peek2_t(&'#') |
| 818 |
|| input.peek2_ident().is_some() |
| 819 |
&& (input.peek3_t(&':') |
| 820 |
|| input.peek3_t(&',') |
| 821 |
|| input.peek3_t(&'>') |
| 822 |
|| input.peek3_t(&'=')) |
| 823 |
|| input.peek2_lifetime() |
| 824 |
&& (input.peek4_t(&':') |
| 825 |
|| input.peek4_t(&',') |
| 826 |
|| input.peek4_t(&'>') |
| 827 |
|| input.peek4_t(&'=')) |
| 828 |
|| input.peek2_t(&"const")); |
| 829 |
let mut generics: Generics = |
| 830 |
if has_generics { parse_generics(input)? } else { Generics::default() }; |
| 831 |
|
| 832 |
let const_token = input.parse_kw_opt("const"); |
| 833 |
|
| 834 |
let mut self_ty = vec![]; |
| 835 |
append_tokens_until(input, &mut self_ty, false, |next| match next { |
| 836 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => true, |
| 837 |
Some(TokenTree::Ident(i)) if i.to_string() == "where" => true, |
| 838 |
_ => false, |
| 839 |
})?; |
| 840 |
|
| 841 |
if input.peek_t(&"where") { |
| 842 |
generics.where_clause = Some(parse_where_clause(input)?); |
| 843 |
} |
| 844 |
|
| 845 |
let g = input.parse_group(Delimiter::Brace)?; |
| 846 |
let brace_token = g.span(); |
| 847 |
let content = &mut TokenIter::new(g.stream()); |
| 848 |
|
| 849 |
let mut items = vec![]; |
| 850 |
while !content.is_empty() { |
| 851 |
items.push(parse_impl_item(content)?); |
| 852 |
} |
| 853 |
|
| 854 |
Ok(ItemImpl { |
| 855 |
attrs, |
| 856 |
vis, |
| 857 |
defaultness, |
| 858 |
unsafety, |
| 859 |
impl_token, |
| 860 |
generics, |
| 861 |
const_token, |
| 862 |
trait_: None, |
| 863 |
self_ty, |
| 864 |
brace_token, |
| 865 |
items, |
| 866 |
}) |
| 867 |
} |
| 868 |
|
| 869 |
fn parse_impl_item(input: &mut TokenIter) -> Result<ImplItem> { |
| 870 |
let attrs = parse_attrs(input)?; |
| 871 |
let vis = parse_visibility(input)?; |
| 872 |
|
| 873 |
let defaultness = if input.peek_t(&"default") && !input.peek2_t(&'!') { |
| 874 |
Some(input.parse_kw("default")?) |
| 875 |
} else { |
| 876 |
None |
| 877 |
}; |
| 878 |
|
| 879 |
if peek_signature(input) { |
| 880 |
let sig = parse_signature(input)?; |
| 881 |
let body = input.parse_group(Delimiter::Brace)?; |
| 882 |
Ok(ImplItem::Method(ImplItemMethod { attrs, vis, defaultness, sig, body })) |
| 883 |
} else if input.peek_t(&"const") { |
| 884 |
let const_token = input.parse_kw("const")?; |
| 885 |
let ident = input.parse_ident()?; |
| 886 |
let colon_token = input.parse_punct(':')?; |
| 887 |
|
| 888 |
let mut ty = vec![]; |
| 889 |
append_tokens_until(input, &mut ty, false, |next| match next { |
| 890 |
Some(TokenTree::Punct(p)) |
| 891 |
if p.as_char() == '=' && p.spacing() == Spacing::Alone |
| 892 |
|| p.as_char() == ';' && p.spacing() == Spacing::Alone => |
| 893 |
{ |
| 894 |
true |
| 895 |
} |
| 896 |
_ => false, |
| 897 |
})?; |
| 898 |
let eq_token = input.parse_punct('=')?; |
| 899 |
|
| 900 |
let (expr, semi_token) = parse_until_punct(input, ';')?; |
| 901 |
|
| 902 |
Ok(ImplItem::Const(ImplItemConst { |
| 903 |
attrs, |
| 904 |
vis, |
| 905 |
defaultness, |
| 906 |
const_token, |
| 907 |
ident, |
| 908 |
colon_token, |
| 909 |
ty: ty.into_iter().collect(), |
| 910 |
eq_token, |
| 911 |
expr, |
| 912 |
semi_token, |
| 913 |
})) |
| 914 |
} else if input.peek_t(&"type") { |
| 915 |
let type_token = input.parse_kw("type")?; |
| 916 |
let ident = input.parse_ident()?; |
| 917 |
let mut generics = parse_generics(input)?; |
| 918 |
|
| 919 |
if input.peek_t(&"where") { |
| 920 |
generics.where_clause = Some(parse_where_clause(input)?); |
| 921 |
} |
| 922 |
|
| 923 |
let eq_token = input.parse_punct('=')?; |
| 924 |
|
| 925 |
let (ty, semi_token) = parse_until_punct(input, ';')?; |
| 926 |
|
| 927 |
Ok(ImplItem::Type(ImplItemType { |
| 928 |
attrs, |
| 929 |
vis, |
| 930 |
defaultness, |
| 931 |
type_token, |
| 932 |
ident, |
| 933 |
generics, |
| 934 |
eq_token, |
| 935 |
ty, |
| 936 |
semi_token, |
| 937 |
})) |
| 938 |
} else { |
| 939 |
bail!(input.next(), "expected one of: `default`, `fn`, `const`, `type`") |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
fn peek_signature(input: &TokenIter) -> bool { |
| 944 |
let fork = &mut input.clone(); |
| 945 |
fork.parse_kw_opt("const"); |
| 946 |
fork.parse_kw_opt("async"); |
| 947 |
fork.parse_kw_opt("unsafe"); |
| 948 |
if fork.peek_t(&"extern") { |
| 949 |
let _extern_token = fork.parse_kw("extern"); |
| 950 |
fork.parse_literal_opt(); |
| 951 |
} |
| 952 |
fork.peek_t(&"fn") |
| 953 |
} |
| 954 |
|
| 955 |
fn parse_signature(input: &mut TokenIter) -> Result<Signature> { |
| 956 |
let mut before_ident = vec![]; |
| 957 |
loop { |
| 958 |
let tt = input.tt()?; |
| 959 |
match &tt { |
| 960 |
TokenTree::Ident(i) if i.to_string() == "fn" => { |
| 961 |
before_ident.push(tt); |
| 962 |
break; |
| 963 |
} |
| 964 |
TokenTree::Group(g) if g.delimiter() == Delimiter::None => { |
| 965 |
let mut iter = g.stream().into_iter(); |
| 966 |
if let Some(TokenTree::Ident(i)) = iter.next() { |
| 967 |
if iter.next().is_none() && i.to_string() == "fn" { |
| 968 |
before_ident.push(tt); |
| 969 |
break; |
| 970 |
} |
| 971 |
} |
| 972 |
before_ident.push(tt); |
| 973 |
} |
| 974 |
_ => before_ident.push(tt), |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
let ident = input.parse_ident()?; |
| 979 |
let mut generics = parse_generics(input)?; |
| 980 |
|
| 981 |
let inputs = input.parse_group(Delimiter::Parenthesis)?; |
| 982 |
let paren_token = inputs.span(); |
| 983 |
let inputs = parse_inputs(inputs.stream())?; |
| 984 |
|
| 985 |
let output = if input.peek_punct('-').map_or(false, |p| p.spacing() == Spacing::Joint) |
| 986 |
&& input.peek2_t(&'>') |
| 987 |
{ |
| 988 |
let arrow1 = input.tt()?; |
| 989 |
let arrow2 = input.tt()?; |
| 990 |
let mut tokens = vec![arrow1, arrow2]; |
| 991 |
append_tokens_until(input, &mut tokens, false, |next| match next { |
| 992 |
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => true, |
| 993 |
Some(TokenTree::Ident(i)) if i.to_string() == "where" => true, |
| 994 |
None => true, |
| 995 |
_ => false, |
| 996 |
})?; |
| 997 |
Some(tokens.into_iter().collect()) |
| 998 |
} else { |
| 999 |
None |
| 1000 |
}; |
| 1001 |
|
| 1002 |
if input.peek_t(&"where") { |
| 1003 |
generics.where_clause = Some(parse_where_clause(input)?); |
| 1004 |
} |
| 1005 |
|
| 1006 |
Ok(Signature { before_ident, ident, generics, paren_token, inputs, output }) |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
pub(crate) mod printing { |
| 1011 |
use proc_macro::{Delimiter, Group, Punct, Spacing, Span, TokenStream}; |
| 1012 |
|
| 1013 |
use super::{ |
| 1014 |
Attribute, BoundLifetimes, ConstParam, FnArg, GenericParam, Generics, ImplGenerics, |
| 1015 |
ImplItem, ImplItemConst, ImplItemMethod, ImplItemType, ItemImpl, ItemTrait, Lifetime, |
| 1016 |
LifetimeDef, PredicateLifetime, PredicateType, Signature, TraitItem, TraitItemConst, |
| 1017 |
TraitItemMethod, TraitItemType, TypeGenerics, TypeParam, TypeParamBound, Visibility, |
| 1018 |
WhereClause, WherePredicate, |
| 1019 |
}; |
| 1020 |
use crate::to_tokens::ToTokens; |
| 1021 |
|
| 1022 |
fn tokens_or_default(p: Option<&Punct>, ch: char, tokens: &mut TokenStream) { |
| 1023 |
match p { |
| 1024 |
Some(p) => p.to_tokens(tokens), |
| 1025 |
None => Punct::new(ch, Spacing::Alone).to_tokens(tokens), |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
impl ToTokens for Generics { |
| 1030 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1031 |
if self.params.is_empty() { |
| 1032 |
return; |
| 1033 |
} |
| 1034 |
|
| 1035 |
tokens_or_default(self.lt_token.as_ref(), '<', tokens); |
| 1036 |
|
| 1037 |
// Print lifetimes before types and consts, regardless of their |
| 1038 |
// order in self.params. |
| 1039 |
// |
| 1040 |
// TODO: ordering rules for const parameters vs type parameters have |
| 1041 |
// not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 1042 |
let mut trailing_or_empty = true; |
| 1043 |
for (param, p) in &self.params { |
| 1044 |
if let GenericParam::Lifetime(_) = param { |
| 1045 |
param.to_tokens(tokens); |
| 1046 |
p.to_tokens(tokens); |
| 1047 |
trailing_or_empty = p.is_some(); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
for (param, p) in &self.params { |
| 1051 |
match param { |
| 1052 |
GenericParam::Type(_) | GenericParam::Const(_) => { |
| 1053 |
if !trailing_or_empty { |
| 1054 |
Punct::new(',', Spacing::Alone).to_tokens(tokens); |
| 1055 |
trailing_or_empty = true; |
| 1056 |
} |
| 1057 |
param.to_tokens(tokens); |
| 1058 |
p.to_tokens(tokens); |
| 1059 |
} |
| 1060 |
GenericParam::Lifetime(_) => {} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
tokens_or_default(self.gt_token.as_ref(), '>', tokens); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
impl ToTokens for GenericParam { |
| 1069 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1070 |
match self { |
| 1071 |
GenericParam::Const(p) => p.to_tokens(tokens), |
| 1072 |
GenericParam::Lifetime(l) => l.to_tokens(tokens), |
| 1073 |
GenericParam::Type(t) => t.to_tokens(tokens), |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
impl ToTokens for BoundLifetimes { |
| 1079 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1080 |
self.for_token.to_tokens(tokens); |
| 1081 |
self.lt_token.to_tokens(tokens); |
| 1082 |
self.lifetimes.to_tokens(tokens); |
| 1083 |
self.gt_token.to_tokens(tokens); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
impl ToTokens for Lifetime { |
| 1088 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1089 |
let mut apostrophe = Punct::new('\'', Spacing::Joint); |
| 1090 |
apostrophe.set_span(self.apostrophe); |
| 1091 |
apostrophe.to_tokens(tokens); |
| 1092 |
self.ident.to_tokens(tokens); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
impl ToTokens for LifetimeDef { |
| 1097 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1098 |
self.attrs.to_tokens(tokens); |
| 1099 |
self.lifetime.to_tokens(tokens); |
| 1100 |
if !self.bounds.is_empty() { |
| 1101 |
tokens_or_default(self.colon_token.as_ref(), ':', tokens); |
| 1102 |
self.bounds.to_tokens(tokens); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
impl ToTokens for TypeParam { |
| 1108 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1109 |
self.attrs.to_tokens(tokens); |
| 1110 |
self.ident.to_tokens(tokens); |
| 1111 |
if !self.bounds.is_empty() { |
| 1112 |
tokens_or_default(self.colon_token.as_ref(), ':', tokens); |
| 1113 |
for (bound, punct) in &self.bounds { |
| 1114 |
bound.to_tokens(tokens); |
| 1115 |
punct.to_tokens(tokens); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
if let Some(default) = &self.default { |
| 1119 |
tokens_or_default(self.eq_token.as_ref(), '=', tokens); |
| 1120 |
default.to_tokens(tokens); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
impl ToTokens for TypeParamBound { |
| 1126 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1127 |
self.tokens.to_tokens(tokens); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
impl ToTokens for ConstParam { |
| 1132 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1133 |
self.attrs.to_tokens(tokens); |
| 1134 |
self.const_token.to_tokens(tokens); |
| 1135 |
self.ident.to_tokens(tokens); |
| 1136 |
self.colon_token.to_tokens(tokens); |
| 1137 |
self.ty.to_tokens(tokens); |
| 1138 |
if let Some(default) = &self.default { |
| 1139 |
tokens_or_default(self.eq_token.as_ref(), '=', tokens); |
| 1140 |
default.to_tokens(tokens); |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
impl ToTokens for ImplGenerics<'_> { |
| 1146 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1147 |
if self.0.params.is_empty() { |
| 1148 |
return; |
| 1149 |
} |
| 1150 |
|
| 1151 |
tokens_or_default(self.0.lt_token.as_ref(), '<', tokens); |
| 1152 |
|
| 1153 |
// Print lifetimes before types and consts, regardless of their |
| 1154 |
// order in self.params. |
| 1155 |
// |
| 1156 |
// TODO: ordering rules for const parameters vs type parameters have |
| 1157 |
// not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 1158 |
let mut trailing_or_empty = true; |
| 1159 |
for (param, p) in &self.0.params { |
| 1160 |
if let GenericParam::Lifetime(_) = param { |
| 1161 |
param.to_tokens(tokens); |
| 1162 |
p.to_tokens(tokens); |
| 1163 |
trailing_or_empty = p.is_some(); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
for (param, p) in &self.0.params { |
| 1167 |
if let GenericParam::Lifetime(_) = param { |
| 1168 |
continue; |
| 1169 |
} |
| 1170 |
if !trailing_or_empty { |
| 1171 |
Punct::new(',', Spacing::Alone).to_tokens(tokens); |
| 1172 |
trailing_or_empty = true; |
| 1173 |
} |
| 1174 |
match param { |
| 1175 |
GenericParam::Lifetime(_) => unreachable!(), |
| 1176 |
GenericParam::Type(param) => { |
| 1177 |
// Leave off the type parameter defaults |
| 1178 |
param.attrs.to_tokens(tokens); |
| 1179 |
param.ident.to_tokens(tokens); |
| 1180 |
if !param.bounds.is_empty() { |
| 1181 |
tokens_or_default(param.colon_token.as_ref(), ':', tokens); |
| 1182 |
param.bounds.to_tokens(tokens); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
GenericParam::Const(param) => { |
| 1186 |
// Leave off the const parameter defaults |
| 1187 |
param.attrs.to_tokens(tokens); |
| 1188 |
param.const_token.to_tokens(tokens); |
| 1189 |
param.ident.to_tokens(tokens); |
| 1190 |
param.colon_token.to_tokens(tokens); |
| 1191 |
param.ty.to_tokens(tokens); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
p.to_tokens(tokens); |
| 1195 |
} |
| 1196 |
|
| 1197 |
tokens_or_default(self.0.gt_token.as_ref(), '>', tokens); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
impl ToTokens for TypeGenerics<'_> { |
| 1202 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1203 |
if self.0.params.is_empty() { |
| 1204 |
return; |
| 1205 |
} |
| 1206 |
|
| 1207 |
tokens_or_default(self.0.lt_token.as_ref(), '<', tokens); |
| 1208 |
|
| 1209 |
// Print lifetimes before types and consts, regardless of their |
| 1210 |
// order in self.params. |
| 1211 |
// |
| 1212 |
// TODO: ordering rules for const parameters vs type parameters have |
| 1213 |
// not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 1214 |
let mut trailing_or_empty = true; |
| 1215 |
for (param, p) in &self.0.params { |
| 1216 |
if let GenericParam::Lifetime(def) = param { |
| 1217 |
// Leave off the lifetime bounds and attributes |
| 1218 |
def.lifetime.to_tokens(tokens); |
| 1219 |
p.to_tokens(tokens); |
| 1220 |
trailing_or_empty = p.is_some(); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
for (param, p) in &self.0.params { |
| 1224 |
if let GenericParam::Lifetime(_) = param { |
| 1225 |
continue; |
| 1226 |
} |
| 1227 |
if !trailing_or_empty { |
| 1228 |
Punct::new(',', Spacing::Alone).to_tokens(tokens); |
| 1229 |
trailing_or_empty = true; |
| 1230 |
} |
| 1231 |
match param { |
| 1232 |
GenericParam::Lifetime(_) => unreachable!(), |
| 1233 |
GenericParam::Type(param) => { |
| 1234 |
// Leave off the type parameter defaults |
| 1235 |
param.ident.to_tokens(tokens); |
| 1236 |
} |
| 1237 |
GenericParam::Const(param) => { |
| 1238 |
// Leave off the const parameter defaults |
| 1239 |
param.ident.to_tokens(tokens); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
p.to_tokens(tokens); |
| 1243 |
} |
| 1244 |
|
| 1245 |
tokens_or_default(self.0.gt_token.as_ref(), '>', tokens); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
impl ToTokens for WhereClause { |
| 1250 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1251 |
if !self.predicates.is_empty() { |
| 1252 |
self.where_token.to_tokens(tokens); |
| 1253 |
self.predicates.to_tokens(tokens); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
impl ToTokens for WherePredicate { |
| 1259 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1260 |
match self { |
| 1261 |
WherePredicate::Lifetime(l) => l.to_tokens(tokens), |
| 1262 |
WherePredicate::Type(t) => t.to_tokens(tokens), |
| 1263 |
} |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
impl ToTokens for PredicateType { |
| 1268 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1269 |
self.lifetimes.to_tokens(tokens); |
| 1270 |
self.bounded_ty.to_tokens(tokens); |
| 1271 |
self.colon_token.to_tokens(tokens); |
| 1272 |
self.bounds.to_tokens(tokens); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
impl ToTokens for PredicateLifetime { |
| 1277 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1278 |
self.lifetime.to_tokens(tokens); |
| 1279 |
self.colon_token.to_tokens(tokens); |
| 1280 |
self.bounds.to_tokens(tokens); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
impl ToTokens for Visibility { |
| 1285 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1286 |
match self { |
| 1287 |
Visibility::Public(i) => i.to_tokens(tokens), |
| 1288 |
Visibility::Restricted(i, g) => { |
| 1289 |
i.to_tokens(tokens); |
| 1290 |
g.to_tokens(tokens); |
| 1291 |
} |
| 1292 |
Visibility::Inherited => {} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
impl ToTokens for Attribute { |
| 1298 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1299 |
self.pound_token.to_tokens(tokens); |
| 1300 |
self.tokens.to_tokens(tokens); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
fn group( |
| 1305 |
span: Span, |
| 1306 |
delimiter: Delimiter, |
| 1307 |
tokens: &mut TokenStream, |
| 1308 |
f: &dyn Fn(&mut TokenStream), |
| 1309 |
) { |
| 1310 |
let mut inner = TokenStream::new(); |
| 1311 |
f(&mut inner); |
| 1312 |
let mut g = Group::new(delimiter, inner); |
| 1313 |
g.set_span(span); |
| 1314 |
g.to_tokens(tokens); |
| 1315 |
} |
| 1316 |
|
| 1317 |
impl ToTokens for ItemTrait { |
| 1318 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1319 |
self.attrs.to_tokens(tokens); |
| 1320 |
self.vis.to_tokens(tokens); |
| 1321 |
self.unsafety.to_tokens(tokens); |
| 1322 |
self.trait_token.to_tokens(tokens); |
| 1323 |
self.ident.to_tokens(tokens); |
| 1324 |
self.generics.to_tokens(tokens); |
| 1325 |
self.generics.where_clause.to_tokens(tokens); |
| 1326 |
group(self.brace_token, Delimiter::Brace, tokens, &|tokens| { |
| 1327 |
self.items.to_tokens(tokens); |
| 1328 |
}); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
impl ToTokens for ItemImpl { |
| 1333 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1334 |
self.attrs.to_tokens(tokens); |
| 1335 |
self.defaultness.to_tokens(tokens); |
| 1336 |
self.unsafety.to_tokens(tokens); |
| 1337 |
self.impl_token.to_tokens(tokens); |
| 1338 |
self.generics.impl_generics().to_tokens(tokens); |
| 1339 |
self.const_token.to_tokens(tokens); |
| 1340 |
if let Some((path, generics, for_)) = &self.trait_ { |
| 1341 |
path.to_tokens(tokens); |
| 1342 |
generics.to_tokens(tokens); |
| 1343 |
for_.to_tokens(tokens); |
| 1344 |
} |
| 1345 |
self.self_ty.to_tokens(tokens); |
| 1346 |
self.generics.where_clause.to_tokens(tokens); |
| 1347 |
group(self.brace_token, Delimiter::Brace, tokens, &|tokens| { |
| 1348 |
self.items.to_tokens(tokens); |
| 1349 |
}); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
impl ToTokens for TraitItem { |
| 1354 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1355 |
match self { |
| 1356 |
TraitItem::Const(i) => i.to_tokens(tokens), |
| 1357 |
TraitItem::Method(i) => i.to_tokens(tokens), |
| 1358 |
TraitItem::Type(i) => i.to_tokens(tokens), |
| 1359 |
} |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
impl ToTokens for TraitItemConst { |
| 1364 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1365 |
self.attrs.to_tokens(tokens); |
| 1366 |
self.const_token.to_tokens(tokens); |
| 1367 |
self.ident.to_tokens(tokens); |
| 1368 |
self.colon_token.to_tokens(tokens); |
| 1369 |
self.ty.to_tokens(tokens); |
| 1370 |
self.semi_token.to_tokens(tokens); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
impl ToTokens for TraitItemMethod { |
| 1375 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1376 |
self.attrs.to_tokens(tokens); |
| 1377 |
self.sig.to_tokens(tokens); |
| 1378 |
self.semi_token.to_tokens(tokens); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
impl ToTokens for TraitItemType { |
| 1383 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1384 |
self.attrs.to_tokens(tokens); |
| 1385 |
self.type_token.to_tokens(tokens); |
| 1386 |
self.ident.to_tokens(tokens); |
| 1387 |
self.generics.to_tokens(tokens); |
| 1388 |
self.generics.where_clause.to_tokens(tokens); |
| 1389 |
self.semi_token.to_tokens(tokens); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
impl ToTokens for ImplItem { |
| 1394 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1395 |
match self { |
| 1396 |
ImplItem::Const(i) => i.to_tokens(tokens), |
| 1397 |
ImplItem::Method(i) => i.to_tokens(tokens), |
| 1398 |
ImplItem::Type(i) => i.to_tokens(tokens), |
| 1399 |
} |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
impl ToTokens for ImplItemConst { |
| 1404 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1405 |
self.attrs.to_tokens(tokens); |
| 1406 |
self.vis.to_tokens(tokens); |
| 1407 |
self.defaultness.to_tokens(tokens); |
| 1408 |
self.const_token.to_tokens(tokens); |
| 1409 |
self.ident.to_tokens(tokens); |
| 1410 |
self.colon_token.to_tokens(tokens); |
| 1411 |
self.ty.to_tokens(tokens); |
| 1412 |
self.eq_token.to_tokens(tokens); |
| 1413 |
self.expr.to_tokens(tokens); |
| 1414 |
self.semi_token.to_tokens(tokens); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
impl ToTokens for ImplItemMethod { |
| 1419 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1420 |
self.attrs.to_tokens(tokens); |
| 1421 |
self.vis.to_tokens(tokens); |
| 1422 |
self.defaultness.to_tokens(tokens); |
| 1423 |
self.sig.to_tokens(tokens); |
| 1424 |
self.body.to_tokens(tokens); |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
impl ToTokens for ImplItemType { |
| 1429 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1430 |
self.attrs.to_tokens(tokens); |
| 1431 |
self.vis.to_tokens(tokens); |
| 1432 |
self.defaultness.to_tokens(tokens); |
| 1433 |
self.type_token.to_tokens(tokens); |
| 1434 |
self.ident.to_tokens(tokens); |
| 1435 |
self.generics.to_tokens(tokens); |
| 1436 |
self.generics.where_clause.to_tokens(tokens); |
| 1437 |
self.eq_token.to_tokens(tokens); |
| 1438 |
self.ty.to_tokens(tokens); |
| 1439 |
self.semi_token.to_tokens(tokens); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
impl ToTokens for Signature { |
| 1444 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1445 |
self.before_ident.to_tokens(tokens); |
| 1446 |
self.ident.to_tokens(tokens); |
| 1447 |
self.generics.to_tokens(tokens); |
| 1448 |
group(self.paren_token, Delimiter::Parenthesis, tokens, &|tokens| { |
| 1449 |
for arg in &self.inputs { |
| 1450 |
arg.to_tokens(tokens); |
| 1451 |
} |
| 1452 |
}); |
| 1453 |
self.output.to_tokens(tokens); |
| 1454 |
self.generics.where_clause.to_tokens(tokens); |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
impl ToTokens for FnArg { |
| 1459 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1460 |
match self { |
| 1461 |
FnArg::Receiver(pat, p) => { |
| 1462 |
pat.to_tokens(tokens); |
| 1463 |
p.to_tokens(tokens); |
| 1464 |
} |
| 1465 |
FnArg::Typed(pat, colon, ty, p) => { |
| 1466 |
pat.to_tokens(tokens); |
| 1467 |
colon.to_tokens(tokens); |
| 1468 |
ty.to_tokens(tokens); |
| 1469 |
p.to_tokens(tokens); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
} |