blob: 6d16ef5749015bbb9cbfffec0563062ff650f24a [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
David Tolnaybb82ef02018-08-24 20:15:45 -0400139 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
142 pub Existential(ItemExistential {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub existential_token: Token![existential],
146 pub type_token: Token![type],
147 pub ident: Ident,
148 pub generics: Generics,
149 pub colon_token: Option<Token![:]>,
150 pub bounds: Punctuated<TypeParamBound, Token![+]>,
151 pub semi_token: Token![;],
152 }),
153
David Tolnay2b214082018-01-07 01:30:18 -0800154 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800155 ///
156 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 pub Struct(ItemStruct {
158 pub attrs: Vec<Attribute>,
159 pub vis: Visibility,
160 pub struct_token: Token![struct],
161 pub ident: Ident,
162 pub generics: Generics,
163 pub fields: Fields,
164 pub semi_token: Option<Token![;]>,
165 }),
David Tolnay2b214082018-01-07 01:30:18 -0800166
167 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800168 ///
169 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700174 pub ident: Ident,
175 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
David Tolnay2b214082018-01-07 01:30:18 -0800179
180 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800181 ///
182 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700187 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500189 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 }),
David Tolnay2b214082018-01-07 01:30:18 -0800191
192 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800193 ///
194 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700197 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500198 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500199 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800200 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700201 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500205 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub items: Vec<TraitItem>,
207 }),
David Tolnay2b214082018-01-07 01:30:18 -0800208
209 /// An impl block providing trait or associated items: `impl<A> Trait
210 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800211 ///
212 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800214 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500215 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500216 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700219 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500220 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700221 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub items: Vec<ImplItem>,
225 }),
David Tolnay2b214082018-01-07 01:30:18 -0800226
227 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800228 ///
229 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800230 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800231 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800232 /// The `example` in `macro_rules! example { ... }`.
233 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800234 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500235 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800236 }),
David Tolnay2b214082018-01-07 01:30:18 -0800237
238 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500241 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800242 pub attrs: Vec<Attribute>,
243 pub vis: Visibility,
244 pub macro_token: Token![macro],
245 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500246 pub paren_token: Paren,
247 pub args: TokenStream,
248 pub brace_token: Brace,
249 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500255 pub Verbatim(ItemVerbatim #manual_extra_traits {
256 pub tts: TokenStream,
257 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
David Tolnayb79ee962016-09-04 09:39:20 -0700259}
260
David Tolnay9c76bcb2017-12-26 23:14:59 -0500261#[cfg(feature = "extra-traits")]
262impl Eq for ItemMacro2 {}
263
264#[cfg(feature = "extra-traits")]
265impl PartialEq for ItemMacro2 {
266 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700267 self.attrs == other.attrs
268 && self.vis == other.vis
269 && self.macro_token == other.macro_token
270 && self.ident == other.ident
271 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500272 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
273 && self.brace_token == other.brace_token
274 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275 }
276}
277
278#[cfg(feature = "extra-traits")]
279impl Hash for ItemMacro2 {
280 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500281 where
282 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500283 {
284 self.attrs.hash(state);
285 self.vis.hash(state);
286 self.macro_token.hash(state);
287 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500288 self.paren_token.hash(state);
289 TokenStreamHelper(&self.args).hash(state);
290 self.brace_token.hash(state);
291 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500292 }
293}
294
David Tolnay2ae520a2017-12-29 11:19:50 -0500295#[cfg(feature = "extra-traits")]
296impl Eq for ItemVerbatim {}
297
298#[cfg(feature = "extra-traits")]
299impl PartialEq for ItemVerbatim {
300 fn eq(&self, other: &Self) -> bool {
301 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
302 }
303}
304
305#[cfg(feature = "extra-traits")]
306impl Hash for ItemVerbatim {
307 fn hash<H>(&self, state: &mut H)
308 where
309 H: Hasher,
310 {
311 TokenStreamHelper(&self.tts).hash(state);
312 }
313}
314
David Tolnay0e837402016-12-22 17:25:55 -0500315impl From<DeriveInput> for Item {
316 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500317 match input.data {
318 Data::Struct(data) => Item::Struct(ItemStruct {
319 attrs: input.attrs,
320 vis: input.vis,
321 struct_token: data.struct_token,
322 ident: input.ident,
323 generics: input.generics,
324 fields: data.fields,
325 semi_token: data.semi_token,
326 }),
327 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500328 attrs: input.attrs,
329 vis: input.vis,
330 enum_token: data.enum_token,
331 ident: input.ident,
332 generics: input.generics,
333 brace_token: data.brace_token,
334 variants: data.variants,
335 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500337 attrs: input.attrs,
338 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500339 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500340 ident: input.ident,
341 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500343 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700344 }
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800350 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800351 /// *This type is available if Syn is built with the `"full"` feature.*
352 ///
David Tolnay614a0142018-01-07 10:25:43 -0800353 /// # Syntax tree enum
354 ///
355 /// This type is a [syntax tree enum].
356 ///
357 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500358 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200359 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500362 pub Path(UsePath {
363 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200364 pub colon2_token: Token![::],
365 pub tree: Box<UseTree>,
366 }),
367
368 /// An identifier imported by a `use` item: `HashMap`.
369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
371 pub Name(UseName {
372 pub ident: Ident,
373 }),
374
375 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
378 pub Rename(UseRename {
379 pub ident: Ident,
380 pub as_token: Token![as],
381 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800383
David Tolnay05658502018-01-07 09:56:37 -0800384 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500387 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800390
David Tolnayd97a7d22018-03-31 19:17:01 +0200391 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800392 ///
393 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200394 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500396 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 }
399}
400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800402 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800403 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800404 /// *This type is available if Syn is built with the `"full"` feature.*
405 ///
David Tolnay614a0142018-01-07 10:25:43 -0800406 /// # Syntax tree enum
407 ///
408 /// This type is a [syntax tree enum].
409 ///
410 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800412 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800413 ///
414 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub attrs: Vec<Attribute>,
417 pub vis: Visibility,
418 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }),
David Tolnayebb72722018-01-07 01:14:13 -0800422
423 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800427 pub attrs: Vec<Attribute>,
428 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500430 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800431 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
David Tolnayebb72722018-01-07 01:14:13 -0800436
437 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800440 pub Type(ForeignItemType {
441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
443 pub type_token: Token![type],
444 pub ident: Ident,
445 pub semi_token: Token![;],
446 }),
David Tolnayebb72722018-01-07 01:14:13 -0800447
David Tolnay435c1782018-08-24 16:15:44 -0400448 /// A macro invocation within an extern block.
449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
451 pub Macro(ForeignItemMacro {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 pub semi_token: Option<Token![;]>,
455 }),
456
David Tolnayebb72722018-01-07 01:14:13 -0800457 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500460 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
461 pub tts: TokenStream,
462 }),
463 }
464}
465
466#[cfg(feature = "extra-traits")]
467impl Eq for ForeignItemVerbatim {}
468
469#[cfg(feature = "extra-traits")]
470impl PartialEq for ForeignItemVerbatim {
471 fn eq(&self, other: &Self) -> bool {
472 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
473 }
474}
475
476#[cfg(feature = "extra-traits")]
477impl Hash for ForeignItemVerbatim {
478 fn hash<H>(&self, state: &mut H)
479 where
480 H: Hasher,
481 {
482 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700484}
485
David Tolnayda705bd2017-11-10 21:58:05 -0800486ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800487 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800488 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800489 /// *This type is available if Syn is built with the `"full"` feature.*
490 ///
David Tolnay614a0142018-01-07 10:25:43 -0800491 /// # Syntax tree enum
492 ///
493 /// This type is a [syntax tree enum].
494 ///
495 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800496 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800497 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800498 ///
499 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800501 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700503 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub default: Option<(Token![=], Expr)>,
507 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
David Tolnayebb72722018-01-07 01:14:13 -0800509
510 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800514 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 pub sig: MethodSig,
516 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
David Tolnayebb72722018-01-07 01:14:13 -0800519
520 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700526 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500527 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800535 ///
536 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800537 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800539 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500540 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800541 }),
David Tolnayebb72722018-01-07 01:14:13 -0800542
543 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800544 ///
545 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500546 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
547 pub tts: TokenStream,
548 }),
549 }
550}
551
552#[cfg(feature = "extra-traits")]
553impl Eq for TraitItemVerbatim {}
554
555#[cfg(feature = "extra-traits")]
556impl PartialEq for TraitItemVerbatim {
557 fn eq(&self, other: &Self) -> bool {
558 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
559 }
560}
561
562#[cfg(feature = "extra-traits")]
563impl Hash for TraitItemVerbatim {
564 fn hash<H>(&self, state: &mut H)
565 where
566 H: Hasher,
567 {
568 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800573 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800574 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800575 /// *This type is available if Syn is built with the `"full"` feature.*
576 ///
David Tolnay614a0142018-01-07 10:25:43 -0800577 /// # Syntax tree enum
578 ///
579 /// This type is a [syntax tree enum].
580 ///
581 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800582 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800583 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700591 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }),
David Tolnayebb72722018-01-07 01:14:13 -0800598
599 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800600 ///
601 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700602 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700604 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500605 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 pub sig: MethodSig,
607 pub block: Block,
608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800614 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700615 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500616 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500619 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 }),
David Tolnayebb72722018-01-07 01:14:13 -0800624
David Tolnaybb82ef02018-08-24 20:15:45 -0400625 /// An existential type within an impl block.
626 ///
627 /// *This type is available if Syn is built with the `"full"` feature.*
628 pub Existential(ImplItemExistential {
629 pub attrs: Vec<Attribute>,
630 pub existential_token: Token![existential],
631 pub type_token: Token![type],
632 pub ident: Ident,
633 pub generics: Generics,
634 pub colon_token: Option<Token![:]>,
635 pub bounds: Punctuated<TypeParamBound, Token![+]>,
636 pub semi_token: Token![;],
637 }),
638
David Tolnayebb72722018-01-07 01:14:13 -0800639 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800642 pub Macro(ImplItemMacro {
643 pub attrs: Vec<Attribute>,
644 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500645 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800646 }),
David Tolnayebb72722018-01-07 01:14:13 -0800647
648 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800649 ///
650 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500651 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
652 pub tts: TokenStream,
653 }),
654 }
655}
656
657#[cfg(feature = "extra-traits")]
658impl Eq for ImplItemVerbatim {}
659
660#[cfg(feature = "extra-traits")]
661impl PartialEq for ImplItemVerbatim {
662 fn eq(&self, other: &Self) -> bool {
663 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
664 }
665}
666
667#[cfg(feature = "extra-traits")]
668impl Hash for ImplItemVerbatim {
669 fn hash<H>(&self, state: &mut H)
670 where
671 H: Hasher,
672 {
673 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700675}
676
677ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800678 /// A method's signature in a trait or implementation: `unsafe fn
679 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500683 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500684 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900685 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700687 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }
690}
691
692ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800693 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500698 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500699 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500700 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500701 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500702 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700704}
705
Alex Crichton62a0a592017-05-22 13:58:53 -0700706ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800707 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800708 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800709 /// *This type is available if Syn is built with the `"full"` feature.*
710 ///
David Tolnay614a0142018-01-07 10:25:43 -0800711 /// # Syntax tree enum
712 ///
713 /// This type is a [syntax tree enum].
714 ///
715 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// Self captured by reference in a function signature: `&self` or `&mut
718 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500725 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800727
David Tolnay3f559052018-01-06 23:59:48 -0800728 /// Self captured by value in a function signature: `self` or `mut
729 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800736
David Tolnay3f559052018-01-06 23:59:48 -0800737 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800738 ///
739 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 pub Captured(ArgCaptured {
741 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800743 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800745
David Tolnay3f559052018-01-06 23:59:48 -0800746 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500747 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800748 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }
David Tolnay62f374c2016-10-02 13:37:00 -0700751}
752
David Tolnayedf2b992016-09-23 20:43:45 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700756
David Tolnay2ad62c12018-08-26 19:00:35 -0400757 use parse::{Parse, ParseStream, Result};
David Tolnay3779bb72018-08-26 18:46:07 -0700758 use synom::ext::IdentExt;
David Tolnay9be32582018-07-31 22:37:26 -0700759 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700760
David Tolnay6a170ce2018-08-26 22:29:24 -0700761 impl Parse for Item {
762 fn parse(input: ParseStream) -> Result<Self> {
763 let ahead = input.fork();
764 ahead.call(Attribute::parse_outer)?;
765 let vis: Visibility = ahead.parse()?;
766
767 let lookahead = ahead.lookahead1();
768 if lookahead.peek(Token![extern]) {
769 ahead.parse::<Token![extern]>()?;
770 let lookahead = ahead.lookahead1();
771 if lookahead.peek(Token![crate]) {
772 input.parse().map(Item::ExternCrate)
773 } else if lookahead.peek(Token![fn]) {
774 input.parse().map(Item::Fn)
775 } else if lookahead.peek(token::Brace) {
776 input.parse().map(Item::ForeignMod)
777 } else if lookahead.peek(LitStr) {
778 ahead.parse::<LitStr>()?;
779 let lookahead = ahead.lookahead1();
780 if lookahead.peek(token::Brace) {
781 input.parse().map(Item::ForeignMod)
782 } else if lookahead.peek(Token![fn]) {
783 input.parse().map(Item::Fn)
784 } else {
785 Err(lookahead.error())
786 }
787 } else {
788 Err(lookahead.error())
789 }
790 } else if lookahead.peek(Token![use]) {
791 input.parse().map(Item::Use)
792 } else if lookahead.peek(Token![static]) {
793 input.parse().map(Item::Static)
794 } else if lookahead.peek(Token![const]) {
795 ahead.parse::<Token![const]>()?;
796 let lookahead = ahead.lookahead1();
797 if lookahead.peek(Ident) {
798 input.parse().map(Item::Const)
799 } else if lookahead.peek(Token![unsafe])
800 || lookahead.peek(Token![async])
801 || lookahead.peek(Token![extern])
802 || lookahead.peek(Token![fn])
803 {
804 input.parse().map(Item::Fn)
805 } else {
806 Err(lookahead.error())
807 }
808 } else if lookahead.peek(Token![unsafe]) {
809 ahead.parse::<Token![unsafe]>()?;
810 let lookahead = ahead.lookahead1();
811 if lookahead.peek(Token![trait])
812 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
813 {
814 input.parse().map(Item::Trait)
815 } else if lookahead.peek(Token![impl ]) {
816 input.parse().map(Item::Impl)
817 } else if lookahead.peek(Token![async])
818 || lookahead.peek(Token![extern])
819 || lookahead.peek(Token![fn])
820 {
821 input.parse().map(Item::Fn)
822 } else {
823 Err(lookahead.error())
824 }
825 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
826 input.parse().map(Item::Fn)
827 } else if lookahead.peek(Token![mod]) {
828 input.parse().map(Item::Mod)
829 } else if lookahead.peek(Token![type]) {
830 input.parse().map(Item::Type)
831 } else if lookahead.peek(Token![existential]) {
832 input.parse().map(Item::Existential)
833 } else if lookahead.peek(Token![struct]) {
834 input.parse().map(Item::Struct)
835 } else if lookahead.peek(Token![enum]) {
836 input.parse().map(Item::Enum)
837 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
838 input.parse().map(Item::Union)
839 } else if lookahead.peek(Token![trait])
840 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
841 {
842 input.parse().map(Item::Trait)
843 } else if lookahead.peek(Token![impl ])
844 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
845 {
846 input.parse().map(Item::Impl)
847 } else if lookahead.peek(Token![macro]) {
848 input.parse().map(Item::Macro2)
849 } else if vis.is_inherited()
850 && (lookahead.peek(Ident)
851 || lookahead.peek(Token![self])
852 || lookahead.peek(Token![super])
853 || lookahead.peek(Token![extern])
854 || lookahead.peek(Token![crate])
855 || lookahead.peek(Token![::]))
856 {
857 input.parse().map(Item::Macro)
858 } else {
859 Err(lookahead.error())
860 }
861 }
862 }
Alex Crichton954046c2017-05-30 21:49:42 -0700863
David Tolnay3779bb72018-08-26 18:46:07 -0700864 impl Parse for ItemMacro {
865 fn parse(input: ParseStream) -> Result<Self> {
866 let attrs = input.call(Attribute::parse_outer)?;
867 let path = input.call(Path::parse_mod_style)?;
868 let bang_token: Token![!] = input.parse()?;
869 let ident: Option<Ident> = input.parse()?;
870 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700871 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700872 Some(input.parse()?)
873 } else {
874 None
875 };
876 Ok(ItemMacro {
877 attrs: attrs,
878 ident: ident,
879 mac: Macro {
880 path: path,
881 bang_token: bang_token,
882 delimiter: delimiter,
883 tts: tts,
884 },
885 semi_token: semi_token,
886 })
887 }
888 }
David Tolnayedf2b992016-09-23 20:43:45 -0700889
David Tolnay500d8322017-12-18 00:32:51 -0800890 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700891 impl Parse for ItemMacro2 {
892 fn parse(input: ParseStream) -> Result<Self> {
893 let args;
894 let body;
895 Ok(ItemMacro2 {
896 attrs: input.call(Attribute::parse_outer)?,
897 vis: input.parse()?,
898 macro_token: input.parse()?,
899 ident: input.parse()?,
900 paren_token: parenthesized!(args in input),
901 args: args.parse()?,
902 brace_token: braced!(body in input),
903 body: body.parse()?,
904 })
905 }
906 }
David Tolnay500d8322017-12-18 00:32:51 -0800907
David Tolnay3779bb72018-08-26 18:46:07 -0700908 impl Parse for ItemExternCrate {
909 fn parse(input: ParseStream) -> Result<Self> {
910 Ok(ItemExternCrate {
911 attrs: input.call(Attribute::parse_outer)?,
912 vis: input.parse()?,
913 extern_token: input.parse()?,
914 crate_token: input.parse()?,
915 ident: input.parse()?,
916 rename: {
917 if input.peek(Token![as]) {
918 let as_token: Token![as] = input.parse()?;
919 let rename: Ident = input.parse()?;
920 Some((as_token, rename))
921 } else {
922 None
923 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800924 },
David Tolnay3779bb72018-08-26 18:46:07 -0700925 semi_token: input.parse()?,
926 })
927 }
928 }
929
930 impl Parse for ItemUse {
931 fn parse(input: ParseStream) -> Result<Self> {
932 Ok(ItemUse {
933 attrs: input.call(Attribute::parse_outer)?,
934 vis: input.parse()?,
935 use_token: input.parse()?,
936 leading_colon: input.parse()?,
937 tree: input.call(use_tree)?,
938 semi_token: input.parse()?,
939 })
940 }
941 }
942
943 fn use_tree(input: ParseStream) -> Result<UseTree> {
944 let lookahead = input.lookahead1();
945 if lookahead.peek(Ident)
946 || lookahead.peek(Token![self])
947 || lookahead.peek(Token![super])
948 || lookahead.peek(Token![crate])
949 || lookahead.peek(Token![extern])
950 {
951 let ident = input.call(Ident::parse_any2)?;
952 if input.peek(Token![::]) {
953 Ok(UseTree::Path(UsePath {
954 ident: ident,
955 colon2_token: input.parse()?,
956 tree: Box::new(input.call(use_tree)?),
957 }))
958 } else if input.peek(Token![as]) {
959 Ok(UseTree::Rename(UseRename {
960 ident: ident,
961 as_token: input.parse()?,
962 rename: input.parse()?,
963 }))
964 } else {
965 Ok(UseTree::Name(UseName { ident: ident }))
966 }
967 } else if lookahead.peek(Token![*]) {
968 Ok(UseTree::Glob(UseGlob {
969 star_token: input.parse()?,
970 }))
971 } else if lookahead.peek(token::Brace) {
972 let content;
973 Ok(UseTree::Group(UseGroup {
974 brace_token: braced!(content in input),
975 items: content.parse_terminated(use_tree)?,
976 }))
977 } else {
978 Err(lookahead.error())
979 }
980 }
981
982 impl Parse for ItemStatic {
983 fn parse(input: ParseStream) -> Result<Self> {
984 Ok(ItemStatic {
985 attrs: input.call(Attribute::parse_outer)?,
986 vis: input.parse()?,
987 static_token: input.parse()?,
988 mutability: input.parse()?,
989 ident: input.parse()?,
990 colon_token: input.parse()?,
991 ty: input.parse()?,
992 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700993 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -0700994 semi_token: input.parse()?,
995 })
996 }
997 }
998
999 impl Parse for ItemConst {
1000 fn parse(input: ParseStream) -> Result<Self> {
1001 Ok(ItemConst {
1002 attrs: input.call(Attribute::parse_outer)?,
1003 vis: input.parse()?,
1004 const_token: input.parse()?,
1005 ident: input.parse()?,
1006 colon_token: input.parse()?,
1007 ty: input.parse()?,
1008 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001009 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001010 semi_token: input.parse()?,
1011 })
1012 }
1013 }
1014
1015 impl Parse for ItemFn {
1016 fn parse(input: ParseStream) -> Result<Self> {
1017 let outer_attrs = input.call(Attribute::parse_outer)?;
1018 let vis: Visibility = input.parse()?;
1019 let constness: Option<Token![const]> = input.parse()?;
1020 let unsafety: Option<Token![unsafe]> = input.parse()?;
1021 let asyncness: Option<Token![async]> = input.parse()?;
1022 let abi: Option<Abi> = input.parse()?;
1023 let fn_token: Token![fn] = input.parse()?;
1024 let ident: Ident = input.parse()?;
1025 let generics: Generics = input.parse()?;
1026
1027 let content;
1028 let paren_token = parenthesized!(content in input);
1029 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1030
1031 let output: ReturnType = input.parse()?;
1032 let where_clause: Option<WhereClause> = input.parse()?;
1033
1034 let content;
1035 let brace_token = braced!(content in input);
1036 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001037 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001038
1039 Ok(ItemFn {
1040 attrs: {
1041 let mut attrs = outer_attrs;
1042 attrs.extend(inner_attrs);
1043 attrs
1044 },
1045 vis: vis,
1046 constness: constness,
1047 unsafety: unsafety,
1048 asyncness: asyncness,
1049 abi: abi,
1050 ident: ident,
1051 decl: Box::new(FnDecl {
1052 fn_token: fn_token,
1053 paren_token: paren_token,
1054 inputs: inputs,
1055 output: output,
1056 variadic: None,
1057 generics: Generics {
1058 where_clause: where_clause,
1059 ..generics
1060 },
1061 }),
1062 block: Box::new(Block {
1063 brace_token: brace_token,
1064 stmts: stmts,
1065 }),
1066 })
1067 }
1068 }
David Tolnay42602292016-10-01 22:25:45 -07001069
David Tolnay2ad62c12018-08-26 19:00:35 -04001070 impl Parse for FnArg {
1071 fn parse(input: ParseStream) -> Result<Self> {
1072 if input.peek(Token![&]) {
1073 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001074 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1075 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001076 }
1077 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001078
David Tolnay2ad62c12018-08-26 19:00:35 -04001079 if input.peek(Token![mut]) || input.peek(Token![self]) {
1080 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001081 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1082 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001083 }
1084 }
1085
1086 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001087 let err = match ahead.call(arg_captured) {
1088 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001089 Err(err) => err,
1090 };
1091
1092 let ahead = input.fork();
1093 if ahead.parse::<Type>().is_ok() {
1094 return input.parse().map(FnArg::Ignored);
1095 }
1096
1097 Err(err)
1098 }
1099 }
1100
David Tolnay3779bb72018-08-26 18:46:07 -07001101 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1102 Ok(ArgSelfRef {
1103 and_token: input.parse()?,
1104 lifetime: input.parse()?,
1105 mutability: input.parse()?,
1106 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001107 })
David Tolnay3779bb72018-08-26 18:46:07 -07001108 }
David Tolnay35902302016-10-06 01:11:08 -07001109
David Tolnay3779bb72018-08-26 18:46:07 -07001110 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1111 Ok(ArgSelf {
1112 mutability: input.parse()?,
1113 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001114 })
David Tolnay3779bb72018-08-26 18:46:07 -07001115 }
1116
1117 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1118 Ok(ArgCaptured {
1119 pat: input.parse_synom(Pat::parse)?,
1120 colon_token: input.parse()?,
1121 ty: input.parse()?,
1122 })
1123 }
1124
1125 impl Parse for ItemMod {
1126 fn parse(input: ParseStream) -> Result<Self> {
1127 let outer_attrs = input.call(Attribute::parse_outer)?;
1128 let vis: Visibility = input.parse()?;
1129 let mod_token: Token![mod] = input.parse()?;
1130 let ident: Ident = input.parse()?;
1131
1132 let lookahead = input.lookahead1();
1133 if lookahead.peek(Token![;]) {
1134 Ok(ItemMod {
1135 attrs: outer_attrs,
1136 vis: vis,
1137 mod_token: mod_token,
1138 ident: ident,
1139 content: None,
1140 semi: Some(input.parse()?),
1141 })
1142 } else if lookahead.peek(token::Brace) {
1143 let content;
1144 let brace_token = braced!(content in input);
1145 let inner_attrs = content.call(Attribute::parse_inner)?;
1146
1147 let mut items = Vec::new();
1148 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001149 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001150 }
1151
1152 Ok(ItemMod {
1153 attrs: {
1154 let mut attrs = outer_attrs;
1155 attrs.extend(inner_attrs);
1156 attrs
1157 },
1158 vis: vis,
1159 mod_token: mod_token,
1160 ident: ident,
1161 content: Some((brace_token, items)),
1162 semi: None,
1163 })
1164 } else {
1165 Err(lookahead.error())
1166 }
1167 }
1168 }
1169
1170 impl Parse for ItemForeignMod {
1171 fn parse(input: ParseStream) -> Result<Self> {
1172 let outer_attrs = input.call(Attribute::parse_outer)?;
1173 let abi: Abi = input.parse()?;
1174
1175 let content;
1176 let brace_token = braced!(content in input);
1177 let inner_attrs = content.call(Attribute::parse_inner)?;
1178 let mut items = Vec::new();
1179 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001180 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001181 }
1182
1183 Ok(ItemForeignMod {
1184 attrs: {
1185 let mut attrs = outer_attrs;
1186 attrs.extend(inner_attrs);
1187 attrs
1188 },
1189 abi: abi,
1190 brace_token: brace_token,
1191 items: items,
1192 })
1193 }
1194 }
David Tolnay35902302016-10-06 01:11:08 -07001195
David Tolnay6a170ce2018-08-26 22:29:24 -07001196 impl Parse for ForeignItem {
1197 fn parse(input: ParseStream) -> Result<Self> {
1198 let ahead = input.fork();
1199 ahead.call(Attribute::parse_outer)?;
1200 let vis: Visibility = ahead.parse()?;
1201
1202 let lookahead = ahead.lookahead1();
1203 if lookahead.peek(Token![fn]) {
1204 input.parse().map(ForeignItem::Fn)
1205 } else if lookahead.peek(Token![static]) {
1206 input.parse().map(ForeignItem::Static)
1207 } else if lookahead.peek(Token![type]) {
1208 input.parse().map(ForeignItem::Type)
1209 } else if vis.is_inherited()
1210 && (lookahead.peek(Ident)
1211 || lookahead.peek(Token![self])
1212 || lookahead.peek(Token![super])
1213 || lookahead.peek(Token![extern])
1214 || lookahead.peek(Token![crate])
1215 || lookahead.peek(Token![::]))
1216 {
1217 input.parse().map(ForeignItem::Macro)
1218 } else {
1219 Err(lookahead.error())
1220 }
1221 }
1222 }
David Tolnay35902302016-10-06 01:11:08 -07001223
David Tolnay3779bb72018-08-26 18:46:07 -07001224 impl Parse for ForeignItemFn {
1225 fn parse(input: ParseStream) -> Result<Self> {
1226 let attrs = input.call(Attribute::parse_outer)?;
1227 let vis: Visibility = input.parse()?;
1228 let fn_token: Token![fn] = input.parse()?;
1229 let ident: Ident = input.parse()?;
1230 let generics: Generics = input.parse()?;
1231
1232 let content;
1233 let paren_token = parenthesized!(content in input);
1234 let inputs = content.parse_synom(Punctuated::parse_terminated)?;
1235 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1236 content.parse()?
1237 } else {
1238 None
1239 };
1240
1241 let output: ReturnType = input.parse()?;
1242 let where_clause: Option<WhereClause> = input.parse()?;
1243 let semi_token: Token![;] = input.parse()?;
1244
1245 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001246 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001247 vis: vis,
1248 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001249 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001250 fn_token: fn_token,
1251 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001252 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001253 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001254 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001255 generics: Generics {
1256 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001257 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001258 },
1259 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001260 semi_token: semi_token,
1261 })
1262 }
1263 }
David Tolnay35902302016-10-06 01:11:08 -07001264
David Tolnay3779bb72018-08-26 18:46:07 -07001265 impl Parse for ForeignItemStatic {
1266 fn parse(input: ParseStream) -> Result<Self> {
1267 Ok(ForeignItemStatic {
1268 attrs: input.call(Attribute::parse_outer)?,
1269 vis: input.parse()?,
1270 static_token: input.parse()?,
1271 mutability: input.parse()?,
1272 ident: input.parse()?,
1273 colon_token: input.parse()?,
1274 ty: input.parse()?,
1275 semi_token: input.parse()?,
1276 })
1277 }
1278 }
David Tolnay35902302016-10-06 01:11:08 -07001279
David Tolnay3779bb72018-08-26 18:46:07 -07001280 impl Parse for ForeignItemType {
1281 fn parse(input: ParseStream) -> Result<Self> {
1282 Ok(ForeignItemType {
1283 attrs: input.call(Attribute::parse_outer)?,
1284 vis: input.parse()?,
1285 type_token: input.parse()?,
1286 ident: input.parse()?,
1287 semi_token: input.parse()?,
1288 })
1289 }
1290 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001291
David Tolnay3779bb72018-08-26 18:46:07 -07001292 impl Parse for ForeignItemMacro {
1293 fn parse(input: ParseStream) -> Result<Self> {
1294 let attrs = input.call(Attribute::parse_outer)?;
1295 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001296 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001297 None
1298 } else {
1299 Some(input.parse()?)
1300 };
1301 Ok(ForeignItemMacro {
1302 attrs: attrs,
1303 mac: mac,
1304 semi_token: semi_token,
1305 })
1306 }
1307 }
David Tolnay78572112018-08-01 00:36:18 -07001308
David Tolnay3779bb72018-08-26 18:46:07 -07001309 impl Parse for ItemType {
1310 fn parse(input: ParseStream) -> Result<Self> {
1311 Ok(ItemType {
1312 attrs: input.call(Attribute::parse_outer)?,
1313 vis: input.parse()?,
1314 type_token: input.parse()?,
1315 ident: input.parse()?,
1316 generics: {
1317 let mut generics: Generics = input.parse()?;
1318 generics.where_clause = input.parse()?;
1319 generics
1320 },
1321 eq_token: input.parse()?,
1322 ty: input.parse()?,
1323 semi_token: input.parse()?,
1324 })
1325 }
1326 }
David Tolnay3cf52982016-10-01 17:11:37 -07001327
David Tolnay3779bb72018-08-26 18:46:07 -07001328 impl Parse for ItemExistential {
1329 fn parse(input: ParseStream) -> Result<Self> {
1330 Ok(ItemExistential {
1331 attrs: input.call(Attribute::parse_outer)?,
1332 vis: input.parse()?,
1333 existential_token: input.parse()?,
1334 type_token: input.parse()?,
1335 ident: input.parse()?,
1336 generics: {
1337 let mut generics: Generics = input.parse()?;
1338 generics.where_clause = input.parse()?;
1339 generics
1340 },
1341 colon_token: Some(input.parse()?),
1342 bounds: input.parse_synom(Punctuated::parse_separated_nonempty)?,
1343 semi_token: input.parse()?,
1344 })
1345 }
1346 }
David Tolnay758ee132018-08-21 21:29:40 -04001347
David Tolnay6a170ce2018-08-26 22:29:24 -07001348 impl Parse for ItemStruct {
1349 fn parse(input: ParseStream) -> Result<Self> {
1350 let attrs = input.call(Attribute::parse_outer)?;
1351 let vis = input.parse::<Visibility>()?;
1352 let struct_token = input.parse::<Token![struct]>()?;
1353 let ident = input.parse::<Ident>()?;
1354 let generics = input.parse::<Generics>()?;
1355 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1356 Ok(ItemStruct {
1357 attrs: attrs,
1358 vis: vis,
1359 struct_token: struct_token,
1360 ident: ident,
1361 generics: Generics {
1362 where_clause: where_clause,
1363 ..generics
1364 },
1365 fields: fields,
1366 semi_token: semi_token,
1367 })
1368 }
1369 }
David Tolnay42602292016-10-01 22:25:45 -07001370
David Tolnay6a170ce2018-08-26 22:29:24 -07001371 impl Parse for ItemEnum {
1372 fn parse(input: ParseStream) -> Result<Self> {
1373 let attrs = input.call(Attribute::parse_outer)?;
1374 let vis = input.parse::<Visibility>()?;
1375 let enum_token = input.parse::<Token![enum]>()?;
1376 let ident = input.parse::<Ident>()?;
1377 let generics = input.parse::<Generics>()?;
1378 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1379 Ok(ItemEnum {
1380 attrs: attrs,
1381 vis: vis,
1382 enum_token: enum_token,
1383 ident: ident,
1384 generics: Generics {
1385 where_clause: where_clause,
1386 ..generics
1387 },
1388 brace_token: brace_token,
1389 variants: variants,
1390 })
1391 }
1392 }
David Tolnay4c614be2017-11-10 00:02:38 -08001393
David Tolnay6a170ce2018-08-26 22:29:24 -07001394 impl Parse for ItemUnion {
1395 fn parse(input: ParseStream) -> Result<Self> {
1396 let attrs = input.call(Attribute::parse_outer)?;
1397 let vis = input.parse::<Visibility>()?;
1398 let union_token = input.parse::<Token![union]>()?;
1399 let ident = input.parse::<Ident>()?;
1400 let generics = input.parse::<Generics>()?;
1401 let (where_clause, fields) = derive::parsing::data_union(input)?;
1402 Ok(ItemUnion {
1403 attrs: attrs,
1404 vis: vis,
1405 union_token: union_token,
1406 ident: ident,
1407 generics: Generics {
1408 where_clause: where_clause,
1409 ..generics
1410 },
1411 fields: fields,
1412 })
1413 }
1414 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001415
David Tolnay6a170ce2018-08-26 22:29:24 -07001416 impl Parse for ItemTrait {
1417 fn parse(input: ParseStream) -> Result<Self> {
1418 let attrs = input.call(Attribute::parse_outer)?;
1419 let vis: Visibility = input.parse()?;
1420 let unsafety: Option<Token![unsafe]> = input.parse()?;
1421 let auto_token: Option<Token![auto]> = input.parse()?;
1422 let trait_token: Token![trait] = input.parse()?;
1423 let ident: Ident = input.parse()?;
1424 let mut generics: Generics = input.parse()?;
1425 let colon_token: Option<Token![:]> = input.parse()?;
1426 let supertraits = if colon_token.is_some() {
1427 input.parse_synom(Punctuated::parse_separated_nonempty)?
1428 } else {
1429 Punctuated::new()
David Tolnay5859df12016-10-29 22:49:54 -07001430 };
David Tolnay6a170ce2018-08-26 22:29:24 -07001431 generics.where_clause = input.parse()?;
1432
1433 let content;
1434 let brace_token = braced!(content in input);
1435 let mut items = Vec::new();
1436 while !content.is_empty() {
1437 items.push(content.parse()?);
1438 }
1439
1440 Ok(ItemTrait {
1441 attrs: attrs,
1442 vis: vis,
1443 unsafety: unsafety,
1444 auto_token: auto_token,
1445 trait_token: trait_token,
1446 ident: ident,
1447 generics: generics,
1448 colon_token: colon_token,
1449 supertraits: supertraits,
1450 brace_token: brace_token,
1451 items: items,
1452 })
1453 }
1454 }
1455
1456 impl Parse for TraitItem {
1457 fn parse(input: ParseStream) -> Result<Self> {
1458 let ahead = input.fork();
1459 ahead.call(Attribute::parse_outer)?;
1460
1461 let lookahead = ahead.lookahead1();
1462 if lookahead.peek(Token![const]) {
1463 ahead.parse::<Token![const]>()?;
1464 let lookahead = ahead.lookahead1();
1465 if lookahead.peek(Ident) {
1466 input.parse().map(TraitItem::Const)
1467 } else if lookahead.peek(Token![unsafe])
1468 || lookahead.peek(Token![extern])
1469 || lookahead.peek(Token![fn])
1470 {
1471 input.parse().map(TraitItem::Method)
1472 } else {
1473 Err(lookahead.error())
1474 }
1475 } else if lookahead.peek(Token![unsafe])
1476 || lookahead.peek(Token![extern])
1477 || lookahead.peek(Token![fn])
1478 {
1479 input.parse().map(TraitItem::Method)
1480 } else if lookahead.peek(Token![type]) {
1481 input.parse().map(TraitItem::Type)
1482 } else if lookahead.peek(Ident)
1483 || lookahead.peek(Token![self])
1484 || lookahead.peek(Token![super])
1485 || lookahead.peek(Token![extern])
1486 || lookahead.peek(Token![crate])
1487 || lookahead.peek(Token![::])
1488 {
1489 input.parse().map(TraitItem::Macro)
1490 } else {
1491 Err(lookahead.error())
1492 }
1493 }
1494 }
1495
1496 impl Parse for TraitItemConst {
1497 fn parse(input: ParseStream) -> Result<Self> {
1498 Ok(TraitItemConst {
1499 attrs: input.call(Attribute::parse_outer)?,
1500 const_token: input.parse()?,
1501 ident: input.parse()?,
1502 colon_token: input.parse()?,
1503 ty: input.parse()?,
1504 default: {
1505 if input.peek(Token![=]) {
1506 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001507 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001508 Some((eq_token, default))
1509 } else {
1510 None
1511 }
1512 },
1513 semi_token: input.parse()?,
1514 })
1515 }
1516 }
1517
1518 impl Parse for TraitItemMethod {
1519 fn parse(input: ParseStream) -> Result<Self> {
1520 let outer_attrs = input.call(Attribute::parse_outer)?;
1521 let constness: Option<Token![const]> = input.parse()?;
1522 let unsafety: Option<Token![unsafe]> = input.parse()?;
1523 let abi: Option<Abi> = input.parse()?;
1524 let fn_token: Token![fn] = input.parse()?;
1525 let ident: Ident = input.parse()?;
1526 let generics: Generics = input.parse()?;
1527
1528 let content;
1529 let paren_token = parenthesized!(content in input);
1530 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1531
1532 let output: ReturnType = input.parse()?;
1533 let where_clause: Option<WhereClause> = input.parse()?;
1534
1535 let lookahead = input.lookahead1();
1536 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1537 let content;
1538 let brace_token = braced!(content in input);
1539 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001540 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001541 (Some(brace_token), inner_attrs, stmts, None)
1542 } else if lookahead.peek(Token![;]) {
1543 let semi_token: Token![;] = input.parse()?;
1544 (None, Vec::new(), Vec::new(), Some(semi_token))
1545 } else {
1546 return Err(lookahead.error());
1547 };
1548
1549 Ok(TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001550 attrs: {
1551 let mut attrs = outer_attrs;
1552 attrs.extend(inner_attrs);
1553 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001554 },
David Tolnayda705bd2017-11-10 21:58:05 -08001555 sig: MethodSig {
1556 constness: constness,
1557 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001558 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001559 abi: abi,
1560 ident: ident,
1561 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001562 fn_token: fn_token,
1563 paren_token: paren_token,
1564 inputs: inputs,
1565 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001566 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001567 generics: Generics {
1568 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001569 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001570 },
1571 },
David Tolnayda705bd2017-11-10 21:58:05 -08001572 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001573 default: brace_token.map(|brace_token| Block {
1574 brace_token: brace_token,
1575 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001576 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001577 semi_token: semi_token,
1578 })
1579 }
1580 }
1581
1582 impl Parse for TraitItemType {
1583 fn parse(input: ParseStream) -> Result<Self> {
1584 let attrs = input.call(Attribute::parse_outer)?;
1585 let type_token: Token![type] = input.parse()?;
1586 let ident: Ident = input.parse()?;
1587 let mut generics: Generics = input.parse()?;
1588 let colon_token: Option<Token![:]> = input.parse()?;
1589 let bounds = if colon_token.is_some() {
1590 input.parse_synom(Punctuated::parse_separated_nonempty)?
1591 } else {
1592 Punctuated::new()
1593 };
1594 generics.where_clause = input.parse()?;
1595 let default = if input.peek(Token![=]) {
1596 let eq_token: Token![=] = input.parse()?;
1597 let default: Type = input.parse()?;
1598 Some((eq_token, default))
1599 } else {
1600 None
1601 };
1602 let semi_token: Token![;] = input.parse()?;
1603
1604 Ok(TraitItemType {
1605 attrs: attrs,
1606 type_token: type_token,
1607 ident: ident,
1608 generics: generics,
1609 colon_token: colon_token,
1610 bounds: bounds,
1611 default: default,
1612 semi_token: semi_token,
1613 })
1614 }
1615 }
1616
1617 impl Parse for TraitItemMacro {
1618 fn parse(input: ParseStream) -> Result<Self> {
1619 let attrs = input.call(Attribute::parse_outer)?;
1620 let mac: Macro = input.parse()?;
1621 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1622 None
1623 } else {
1624 Some(input.parse()?)
1625 };
1626 Ok(TraitItemMacro {
1627 attrs: attrs,
1628 mac: mac,
1629 semi_token: semi_token,
1630 })
1631 }
1632 }
1633
1634 impl Parse for ItemImpl {
1635 fn parse(input: ParseStream) -> Result<Self> {
1636 let outer_attrs = input.call(Attribute::parse_outer)?;
1637 let defaultness: Option<Token![default]> = input.parse()?;
1638 let unsafety: Option<Token![unsafe]> = input.parse()?;
1639 let impl_token: Token![impl ] = input.parse()?;
1640 let generics: Generics = input.parse()?;
1641 let trait_ = {
1642 let ahead = input.fork();
1643 if ahead.parse::<Option<Token![!]>>().is_ok()
1644 && ahead.parse::<Path>().is_ok()
1645 && ahead.parse::<Token![for]>().is_ok()
1646 {
1647 let polarity: Option<Token![!]> = input.parse()?;
1648 let path: Path = input.parse()?;
1649 let for_token: Token![for] = input.parse()?;
1650 Some((polarity, path, for_token))
1651 } else {
1652 None
1653 }
1654 };
1655 let self_ty: Type = input.parse()?;
1656 let where_clause: Option<WhereClause> = input.parse()?;
1657
1658 let content;
1659 let brace_token = braced!(content in input);
1660 let inner_attrs = content.call(Attribute::parse_inner)?;
1661
1662 let mut items = Vec::new();
1663 while !content.is_empty() {
1664 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001665 }
David Tolnay0aecb732016-10-03 23:03:50 -07001666
David Tolnay6a170ce2018-08-26 22:29:24 -07001667 Ok(ItemImpl {
1668 attrs: {
1669 let mut attrs = outer_attrs;
1670 attrs.extend(inner_attrs);
1671 attrs
1672 },
1673 defaultness: defaultness,
1674 unsafety: unsafety,
1675 impl_token: impl_token,
1676 generics: Generics {
1677 where_clause: where_clause,
1678 ..generics
1679 },
1680 trait_: trait_,
1681 self_ty: Box::new(self_ty),
1682 brace_token: brace_token,
1683 items: items,
1684 })
1685 }
1686 }
David Tolnay0aecb732016-10-03 23:03:50 -07001687
David Tolnay6a170ce2018-08-26 22:29:24 -07001688 impl Parse for ImplItem {
1689 fn parse(input: ParseStream) -> Result<Self> {
1690 let ahead = input.fork();
1691 ahead.call(Attribute::parse_outer)?;
1692 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001693
David Tolnay6a170ce2018-08-26 22:29:24 -07001694 let mut lookahead = ahead.lookahead1();
1695 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1696 let defaultness: Token![default] = ahead.parse()?;
1697 lookahead = ahead.lookahead1();
1698 Some(defaultness)
1699 } else {
1700 None
1701 };
David Tolnay4c9be372016-10-06 00:47:37 -07001702
David Tolnay6a170ce2018-08-26 22:29:24 -07001703 if lookahead.peek(Token![const]) {
1704 ahead.parse::<Token![const]>()?;
1705 let lookahead = ahead.lookahead1();
1706 if lookahead.peek(Ident) {
1707 input.parse().map(ImplItem::Const)
1708 } else if lookahead.peek(Token![unsafe])
1709 || lookahead.peek(Token![async])
1710 || lookahead.peek(Token![extern])
1711 || lookahead.peek(Token![fn])
1712 {
1713 input.parse().map(ImplItem::Method)
1714 } else {
1715 Err(lookahead.error())
1716 }
1717 } else if lookahead.peek(Token![unsafe])
1718 || lookahead.peek(Token![async])
1719 || lookahead.peek(Token![extern])
1720 || lookahead.peek(Token![fn])
1721 {
1722 input.parse().map(ImplItem::Method)
1723 } else if lookahead.peek(Token![type]) {
1724 input.parse().map(ImplItem::Type)
1725 } else if vis.is_inherited()
1726 && defaultness.is_none()
1727 && lookahead.peek(Token![existential])
1728 {
1729 input.parse().map(ImplItem::Existential)
1730 } else if vis.is_inherited()
1731 && defaultness.is_none()
1732 && (lookahead.peek(Ident)
1733 || lookahead.peek(Token![self])
1734 || lookahead.peek(Token![super])
1735 || lookahead.peek(Token![extern])
1736 || lookahead.peek(Token![crate])
1737 || lookahead.peek(Token![::]))
1738 {
1739 input.parse().map(ImplItem::Macro)
1740 } else {
1741 Err(lookahead.error())
1742 }
1743 }
1744 }
David Tolnay4c9be372016-10-06 00:47:37 -07001745
David Tolnay3779bb72018-08-26 18:46:07 -07001746 impl Parse for ImplItemConst {
1747 fn parse(input: ParseStream) -> Result<Self> {
1748 Ok(ImplItemConst {
1749 attrs: input.call(Attribute::parse_outer)?,
1750 vis: input.parse()?,
1751 defaultness: input.parse()?,
1752 const_token: input.parse()?,
1753 ident: input.parse()?,
1754 colon_token: input.parse()?,
1755 ty: input.parse()?,
1756 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001757 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001758 semi_token: input.parse()?,
1759 })
1760 }
1761 }
David Tolnay4c9be372016-10-06 00:47:37 -07001762
David Tolnay6a170ce2018-08-26 22:29:24 -07001763 impl Parse for ImplItemMethod {
1764 fn parse(input: ParseStream) -> Result<Self> {
1765 let outer_attrs = input.call(Attribute::parse_outer)?;
1766 let vis: Visibility = input.parse()?;
1767 let defaultness: Option<Token![default]> = input.parse()?;
1768 let constness: Option<Token![const]> = input.parse()?;
1769 let unsafety: Option<Token![unsafe]> = input.parse()?;
1770 let asyncness: Option<Token![async]> = input.parse()?;
1771 let abi: Option<Abi> = input.parse()?;
1772 let fn_token: Token![fn] = input.parse()?;
1773 let ident: Ident = input.parse()?;
1774 let generics: Generics = input.parse()?;
1775
1776 let content;
1777 let paren_token = parenthesized!(content in input);
1778 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1779
1780 let output: ReturnType = input.parse()?;
1781 let where_clause: Option<WhereClause> = input.parse()?;
1782
1783 let content;
1784 let brace_token = braced!(content in input);
1785 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001786 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001787
1788 Ok(ImplItemMethod {
1789 attrs: {
1790 let mut attrs = outer_attrs;
1791 attrs.extend(inner_attrs);
1792 attrs
David Tolnay4c9be372016-10-06 00:47:37 -07001793 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001794 vis: vis,
1795 defaultness: defaultness,
1796 sig: MethodSig {
1797 constness: constness,
1798 unsafety: unsafety,
1799 asyncness: asyncness,
1800 abi: abi,
1801 ident: ident,
1802 decl: FnDecl {
1803 fn_token: fn_token,
1804 paren_token: paren_token,
1805 inputs: inputs,
1806 output: output,
1807 variadic: None,
1808 generics: Generics {
1809 where_clause: where_clause,
1810 ..generics
1811 },
1812 },
1813 },
1814 block: Block {
1815 brace_token: brace_token,
1816 stmts: stmts,
1817 },
1818 })
1819 }
1820 }
David Tolnay4c9be372016-10-06 00:47:37 -07001821
David Tolnay3779bb72018-08-26 18:46:07 -07001822 impl Parse for ImplItemType {
1823 fn parse(input: ParseStream) -> Result<Self> {
1824 Ok(ImplItemType {
1825 attrs: input.call(Attribute::parse_outer)?,
1826 vis: input.parse()?,
1827 defaultness: input.parse()?,
1828 type_token: input.parse()?,
1829 ident: input.parse()?,
1830 generics: {
1831 let mut generics: Generics = input.parse()?;
1832 generics.where_clause = input.parse()?;
1833 generics
1834 },
1835 eq_token: input.parse()?,
1836 ty: input.parse()?,
1837 semi_token: input.parse()?,
1838 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001839 }
David Tolnay3779bb72018-08-26 18:46:07 -07001840 }
David Tolnay758ee132018-08-21 21:29:40 -04001841
David Tolnay3779bb72018-08-26 18:46:07 -07001842 impl Parse for ImplItemExistential {
1843 fn parse(input: ParseStream) -> Result<Self> {
1844 let ety: ItemExistential = input.parse()?;
1845 Ok(ImplItemExistential {
1846 attrs: ety.attrs,
1847 existential_token: ety.existential_token,
1848 type_token: ety.type_token,
1849 ident: ety.ident,
1850 generics: ety.generics,
1851 colon_token: ety.colon_token,
1852 bounds: ety.bounds,
1853 semi_token: ety.semi_token,
1854 })
1855 }
1856 }
1857
1858 impl Parse for ImplItemMacro {
1859 fn parse(input: ParseStream) -> Result<Self> {
1860 let attrs = input.call(Attribute::parse_outer)?;
1861 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001862 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001863 None
1864 } else {
1865 Some(input.parse()?)
1866 };
1867 Ok(ImplItemMacro {
1868 attrs: attrs,
1869 mac: mac,
1870 semi_token: semi_token,
1871 })
1872 }
1873 }
David Tolnay4c9be372016-10-06 00:47:37 -07001874
David Tolnay6a170ce2018-08-26 22:29:24 -07001875 impl Visibility {
1876 fn is_inherited(&self) -> bool {
1877 match *self {
1878 Visibility::Inherited => true,
1879 _ => false,
1880 }
1881 }
1882 }
1883
1884 impl MacroDelimiter {
1885 fn is_brace(&self) -> bool {
1886 match *self {
1887 MacroDelimiter::Brace(_) => true,
1888 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1889 }
David Tolnay57292da2017-12-27 21:03:33 -05001890 }
1891 }
David Tolnayedf2b992016-09-23 20:43:45 -07001892}
David Tolnay4a51dc72016-10-01 00:40:31 -07001893
1894#[cfg(feature = "printing")]
1895mod printing {
1896 use super::*;
1897 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001898 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001899 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001900
David Tolnay1bfa7332017-11-11 12:41:20 -08001901 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001902 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001903 tokens.append_all(self.attrs.outer());
1904 self.vis.to_tokens(tokens);
1905 self.extern_token.to_tokens(tokens);
1906 self.crate_token.to_tokens(tokens);
1907 self.ident.to_tokens(tokens);
1908 if let Some((ref as_token, ref rename)) = self.rename {
1909 as_token.to_tokens(tokens);
1910 rename.to_tokens(tokens);
1911 }
1912 self.semi_token.to_tokens(tokens);
1913 }
1914 }
1915
1916 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001917 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001918 tokens.append_all(self.attrs.outer());
1919 self.vis.to_tokens(tokens);
1920 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001921 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001922 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001923 self.semi_token.to_tokens(tokens);
1924 }
1925 }
1926
1927 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001928 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001929 tokens.append_all(self.attrs.outer());
1930 self.vis.to_tokens(tokens);
1931 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001932 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001933 self.ident.to_tokens(tokens);
1934 self.colon_token.to_tokens(tokens);
1935 self.ty.to_tokens(tokens);
1936 self.eq_token.to_tokens(tokens);
1937 self.expr.to_tokens(tokens);
1938 self.semi_token.to_tokens(tokens);
1939 }
1940 }
1941
1942 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001943 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001944 tokens.append_all(self.attrs.outer());
1945 self.vis.to_tokens(tokens);
1946 self.const_token.to_tokens(tokens);
1947 self.ident.to_tokens(tokens);
1948 self.colon_token.to_tokens(tokens);
1949 self.ty.to_tokens(tokens);
1950 self.eq_token.to_tokens(tokens);
1951 self.expr.to_tokens(tokens);
1952 self.semi_token.to_tokens(tokens);
1953 }
1954 }
1955
1956 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001957 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001958 tokens.append_all(self.attrs.outer());
1959 self.vis.to_tokens(tokens);
1960 self.constness.to_tokens(tokens);
1961 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001962 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001963 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001964 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001965 self.block.brace_token.surround(tokens, |tokens| {
1966 tokens.append_all(self.attrs.inner());
1967 tokens.append_all(&self.block.stmts);
1968 });
1969 }
1970 }
1971
1972 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001973 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001974 tokens.append_all(self.attrs.outer());
1975 self.vis.to_tokens(tokens);
1976 self.mod_token.to_tokens(tokens);
1977 self.ident.to_tokens(tokens);
1978 if let Some((ref brace, ref items)) = self.content {
1979 brace.surround(tokens, |tokens| {
1980 tokens.append_all(self.attrs.inner());
1981 tokens.append_all(items);
1982 });
1983 } else {
1984 TokensOrDefault(&self.semi).to_tokens(tokens);
1985 }
1986 }
1987 }
1988
1989 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001990 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001991 tokens.append_all(self.attrs.outer());
1992 self.abi.to_tokens(tokens);
1993 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001994 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001995 tokens.append_all(&self.items);
1996 });
1997 }
1998 }
1999
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002000 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002001 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002002 tokens.append_all(self.attrs.outer());
2003 self.vis.to_tokens(tokens);
2004 self.type_token.to_tokens(tokens);
2005 self.ident.to_tokens(tokens);
2006 self.generics.to_tokens(tokens);
2007 self.generics.where_clause.to_tokens(tokens);
2008 self.eq_token.to_tokens(tokens);
2009 self.ty.to_tokens(tokens);
2010 self.semi_token.to_tokens(tokens);
2011 }
2012 }
2013
David Tolnaybb82ef02018-08-24 20:15:45 -04002014 impl ToTokens for ItemExistential {
2015 fn to_tokens(&self, tokens: &mut TokenStream) {
2016 tokens.append_all(self.attrs.outer());
2017 self.vis.to_tokens(tokens);
2018 self.existential_token.to_tokens(tokens);
2019 self.type_token.to_tokens(tokens);
2020 self.ident.to_tokens(tokens);
2021 self.generics.to_tokens(tokens);
2022 self.generics.where_clause.to_tokens(tokens);
2023 if !self.bounds.is_empty() {
2024 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2025 self.bounds.to_tokens(tokens);
2026 }
2027 self.semi_token.to_tokens(tokens);
2028 }
2029 }
2030
David Tolnay1bfa7332017-11-11 12:41:20 -08002031 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002032 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002033 tokens.append_all(self.attrs.outer());
2034 self.vis.to_tokens(tokens);
2035 self.enum_token.to_tokens(tokens);
2036 self.ident.to_tokens(tokens);
2037 self.generics.to_tokens(tokens);
2038 self.generics.where_clause.to_tokens(tokens);
2039 self.brace_token.surround(tokens, |tokens| {
2040 self.variants.to_tokens(tokens);
2041 });
2042 }
2043 }
2044
2045 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002046 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002047 tokens.append_all(self.attrs.outer());
2048 self.vis.to_tokens(tokens);
2049 self.struct_token.to_tokens(tokens);
2050 self.ident.to_tokens(tokens);
2051 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002052 match self.fields {
2053 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002054 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002055 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002056 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002057 Fields::Unnamed(ref fields) => {
2058 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002059 self.generics.where_clause.to_tokens(tokens);
2060 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002061 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002062 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002063 self.generics.where_clause.to_tokens(tokens);
2064 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002065 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002066 }
2067 }
2068 }
2069
2070 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002071 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002072 tokens.append_all(self.attrs.outer());
2073 self.vis.to_tokens(tokens);
2074 self.union_token.to_tokens(tokens);
2075 self.ident.to_tokens(tokens);
2076 self.generics.to_tokens(tokens);
2077 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002078 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002079 }
2080 }
2081
2082 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002083 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002084 tokens.append_all(self.attrs.outer());
2085 self.vis.to_tokens(tokens);
2086 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002087 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002088 self.trait_token.to_tokens(tokens);
2089 self.ident.to_tokens(tokens);
2090 self.generics.to_tokens(tokens);
2091 if !self.supertraits.is_empty() {
2092 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2093 self.supertraits.to_tokens(tokens);
2094 }
2095 self.generics.where_clause.to_tokens(tokens);
2096 self.brace_token.surround(tokens, |tokens| {
2097 tokens.append_all(&self.items);
2098 });
2099 }
2100 }
2101
David Tolnay1bfa7332017-11-11 12:41:20 -08002102 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002103 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002104 tokens.append_all(self.attrs.outer());
2105 self.defaultness.to_tokens(tokens);
2106 self.unsafety.to_tokens(tokens);
2107 self.impl_token.to_tokens(tokens);
2108 self.generics.to_tokens(tokens);
2109 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2110 polarity.to_tokens(tokens);
2111 path.to_tokens(tokens);
2112 for_token.to_tokens(tokens);
2113 }
2114 self.self_ty.to_tokens(tokens);
2115 self.generics.where_clause.to_tokens(tokens);
2116 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002117 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002118 tokens.append_all(&self.items);
2119 });
2120 }
2121 }
2122
2123 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002124 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002125 tokens.append_all(self.attrs.outer());
2126 self.mac.path.to_tokens(tokens);
2127 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002128 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002129 match self.mac.delimiter {
2130 MacroDelimiter::Paren(ref paren) => {
2131 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2132 }
2133 MacroDelimiter::Brace(ref brace) => {
2134 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2135 }
2136 MacroDelimiter::Bracket(ref bracket) => {
2137 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2138 }
2139 }
David Tolnay57292da2017-12-27 21:03:33 -05002140 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002141 }
2142 }
David Tolnay42602292016-10-01 22:25:45 -07002143
David Tolnay500d8322017-12-18 00:32:51 -08002144 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002145 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002146 tokens.append_all(self.attrs.outer());
2147 self.vis.to_tokens(tokens);
2148 self.macro_token.to_tokens(tokens);
2149 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002150 self.paren_token.surround(tokens, |tokens| {
2151 self.args.to_tokens(tokens);
2152 });
2153 self.brace_token.surround(tokens, |tokens| {
2154 self.body.to_tokens(tokens);
2155 });
David Tolnay500d8322017-12-18 00:32:51 -08002156 }
2157 }
2158
David Tolnay2ae520a2017-12-29 11:19:50 -05002159 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002160 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002161 self.tts.to_tokens(tokens);
2162 }
2163 }
2164
David Tolnay5f332a92017-12-26 00:42:45 -05002165 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002166 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002167 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002168 self.colon2_token.to_tokens(tokens);
2169 self.tree.to_tokens(tokens);
2170 }
2171 }
2172
2173 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002174 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002175 self.ident.to_tokens(tokens);
2176 }
2177 }
2178
2179 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002180 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002181 self.ident.to_tokens(tokens);
2182 self.as_token.to_tokens(tokens);
2183 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002184 }
2185 }
2186
David Tolnay5f332a92017-12-26 00:42:45 -05002187 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002188 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002189 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002190 }
2191 }
2192
David Tolnayd97a7d22018-03-31 19:17:01 +02002193 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002194 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002195 self.brace_token.surround(tokens, |tokens| {
2196 self.items.to_tokens(tokens);
2197 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002198 }
2199 }
2200
David Tolnay1bfa7332017-11-11 12:41:20 -08002201 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002202 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002203 tokens.append_all(self.attrs.outer());
2204 self.const_token.to_tokens(tokens);
2205 self.ident.to_tokens(tokens);
2206 self.colon_token.to_tokens(tokens);
2207 self.ty.to_tokens(tokens);
2208 if let Some((ref eq_token, ref default)) = self.default {
2209 eq_token.to_tokens(tokens);
2210 default.to_tokens(tokens);
2211 }
2212 self.semi_token.to_tokens(tokens);
2213 }
2214 }
2215
2216 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002217 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002218 tokens.append_all(self.attrs.outer());
2219 self.sig.to_tokens(tokens);
2220 match self.default {
2221 Some(ref block) => {
2222 block.brace_token.surround(tokens, |tokens| {
2223 tokens.append_all(self.attrs.inner());
2224 tokens.append_all(&block.stmts);
2225 });
David Tolnayca085422016-10-04 00:12:38 -07002226 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002227 None => {
2228 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002229 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002230 }
2231 }
2232 }
2233
2234 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002235 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002236 tokens.append_all(self.attrs.outer());
2237 self.type_token.to_tokens(tokens);
2238 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002239 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002240 if !self.bounds.is_empty() {
2241 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2242 self.bounds.to_tokens(tokens);
2243 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002244 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002245 if let Some((ref eq_token, ref default)) = self.default {
2246 eq_token.to_tokens(tokens);
2247 default.to_tokens(tokens);
2248 }
2249 self.semi_token.to_tokens(tokens);
2250 }
2251 }
2252
2253 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002254 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002255 tokens.append_all(self.attrs.outer());
2256 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002257 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002258 }
2259 }
2260
David Tolnay2ae520a2017-12-29 11:19:50 -05002261 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002262 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002263 self.tts.to_tokens(tokens);
2264 }
2265 }
2266
David Tolnay857628c2017-11-11 12:25:31 -08002267 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002268 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002269 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002270 self.vis.to_tokens(tokens);
2271 self.defaultness.to_tokens(tokens);
2272 self.const_token.to_tokens(tokens);
2273 self.ident.to_tokens(tokens);
2274 self.colon_token.to_tokens(tokens);
2275 self.ty.to_tokens(tokens);
2276 self.eq_token.to_tokens(tokens);
2277 self.expr.to_tokens(tokens);
2278 self.semi_token.to_tokens(tokens);
2279 }
2280 }
2281
2282 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002283 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002284 tokens.append_all(self.attrs.outer());
2285 self.vis.to_tokens(tokens);
2286 self.defaultness.to_tokens(tokens);
2287 self.sig.to_tokens(tokens);
2288 self.block.brace_token.surround(tokens, |tokens| {
2289 tokens.append_all(self.attrs.inner());
2290 tokens.append_all(&self.block.stmts);
2291 });
2292 }
2293 }
2294
2295 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002296 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002297 tokens.append_all(self.attrs.outer());
2298 self.vis.to_tokens(tokens);
2299 self.defaultness.to_tokens(tokens);
2300 self.type_token.to_tokens(tokens);
2301 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002302 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002303 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002304 self.eq_token.to_tokens(tokens);
2305 self.ty.to_tokens(tokens);
2306 self.semi_token.to_tokens(tokens);
2307 }
2308 }
2309
David Tolnaybb82ef02018-08-24 20:15:45 -04002310 impl ToTokens for ImplItemExistential {
2311 fn to_tokens(&self, tokens: &mut TokenStream) {
2312 tokens.append_all(self.attrs.outer());
2313 self.existential_token.to_tokens(tokens);
2314 self.type_token.to_tokens(tokens);
2315 self.ident.to_tokens(tokens);
2316 self.generics.to_tokens(tokens);
2317 self.generics.where_clause.to_tokens(tokens);
2318 if !self.bounds.is_empty() {
2319 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2320 self.bounds.to_tokens(tokens);
2321 }
2322 self.semi_token.to_tokens(tokens);
2323 }
2324 }
2325
David Tolnay857628c2017-11-11 12:25:31 -08002326 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002327 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002328 tokens.append_all(self.attrs.outer());
2329 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002330 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002331 }
2332 }
2333
David Tolnay2ae520a2017-12-29 11:19:50 -05002334 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002335 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002336 self.tts.to_tokens(tokens);
2337 }
2338 }
2339
David Tolnay8894f602017-11-11 12:11:04 -08002340 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002341 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002342 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002343 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002344 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002345 self.semi_token.to_tokens(tokens);
2346 }
2347 }
2348
2349 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002350 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002351 tokens.append_all(self.attrs.outer());
2352 self.vis.to_tokens(tokens);
2353 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002354 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002355 self.ident.to_tokens(tokens);
2356 self.colon_token.to_tokens(tokens);
2357 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002358 self.semi_token.to_tokens(tokens);
2359 }
2360 }
2361
David Tolnay199bcbb2017-11-12 10:33:52 -08002362 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002363 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002364 tokens.append_all(self.attrs.outer());
2365 self.vis.to_tokens(tokens);
2366 self.type_token.to_tokens(tokens);
2367 self.ident.to_tokens(tokens);
2368 self.semi_token.to_tokens(tokens);
2369 }
2370 }
2371
David Tolnay435c1782018-08-24 16:15:44 -04002372 impl ToTokens for ForeignItemMacro {
2373 fn to_tokens(&self, tokens: &mut TokenStream) {
2374 tokens.append_all(self.attrs.outer());
2375 self.mac.to_tokens(tokens);
2376 self.semi_token.to_tokens(tokens);
2377 }
2378 }
2379
David Tolnay2ae520a2017-12-29 11:19:50 -05002380 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002381 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002382 self.tts.to_tokens(tokens);
2383 }
2384 }
2385
David Tolnay570695e2017-06-03 16:15:13 -07002386 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002387 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002388 self.constness.to_tokens(tokens);
2389 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002390 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002391 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002392 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002393 }
2394 }
2395
Alex Crichtona74a1c82018-05-16 10:20:44 -07002396 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002397
2398 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002399 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002400 self.0.fn_token.to_tokens(tokens);
2401 self.1.to_tokens(tokens);
2402 self.0.generics.to_tokens(tokens);
2403 self.0.paren_token.surround(tokens, |tokens| {
2404 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002405 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2406 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002407 }
David Tolnayd2836e22017-12-27 23:13:00 -05002408 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002409 });
2410 self.0.output.to_tokens(tokens);
2411 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002412 }
2413 }
2414
Alex Crichton62a0a592017-05-22 13:58:53 -07002415 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002416 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002417 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002418 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002419 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002420 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002421 }
2422 }
2423
2424 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002425 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002426 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002427 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002428 }
2429 }
2430
2431 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002432 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002433 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002434 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002435 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002436 }
2437 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002438}