blob: 0df30451dd60cda249529238d4dd09f9989a1be7 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -08002use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -07003use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -07004use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -08005use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -05006
7#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -05008use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -07009#[cfg(feature = "extra-traits")]
10use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080013 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080014 ///
David Tolnay461d98e2018-01-07 11:07:19 -080015 /// *This type is available if Syn is built with the `"full"` feature.*
16 ///
David Tolnay614a0142018-01-07 10:25:43 -080017 /// # Syntax tree enum
18 ///
19 /// This type is a [syntax tree enum].
20 ///
21 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080022 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080023 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080024 ///
25 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080027 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070028 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 pub extern_token: Token![extern],
30 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070031 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080032 pub rename: Option<(Token![as], Ident)>,
33 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
David Tolnay2b214082018-01-07 01:30:18 -080035
36 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080037 ///
38 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070039 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080040 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070041 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050043 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050044 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070046 }),
David Tolnay2b214082018-01-07 01:30:18 -080047
48 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080049 ///
50 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070051 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080052 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070053 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050055 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070056 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080057 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080058 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070060 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080061 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070062 }),
David Tolnay2b214082018-01-07 01:30:18 -080063
64 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080065 ///
66 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080068 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070069 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080070 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070071 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080072 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080073 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080074 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080076 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070077 }),
David Tolnay2b214082018-01-07 01:30:18 -080078
David Tolnay461d98e2018-01-07 11:07:19 -080079 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
80 /// }`.
81 ///
82 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080084 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070085 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050086 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050087 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090088 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070089 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070090 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050091 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070092 pub block: Box<Block>,
93 }),
David Tolnay2b214082018-01-07 01:30:18 -080094
95 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -080096 ///
97 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080099 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700100 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800101 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700102 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500103 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800104 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 }),
David Tolnay2b214082018-01-07 01:30:18 -0800106
107 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700110 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800111 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500113 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700114 pub items: Vec<ForeignItem>,
115 }),
David Tolnay2b214082018-01-07 01:30:18 -0800116
117 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800118 ///
119 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800120 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800121 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700122 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800123 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700124 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700125 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800127 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 }),
David Tolnay2b214082018-01-07 01:30:18 -0800130
David Tolnaybb82ef02018-08-24 20:15:45 -0400131 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
132 ///
133 /// *This type is available if Syn is built with the `"full"` feature.*
134 pub Existential(ItemExistential {
135 pub attrs: Vec<Attribute>,
136 pub vis: Visibility,
137 pub existential_token: Token![existential],
138 pub type_token: Token![type],
139 pub ident: Ident,
140 pub generics: Generics,
141 pub colon_token: Option<Token![:]>,
142 pub bounds: Punctuated<TypeParamBound, Token![+]>,
143 pub semi_token: Token![;],
144 }),
145
David Tolnay2b214082018-01-07 01:30:18 -0800146 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500149 pub Struct(ItemStruct {
150 pub attrs: Vec<Attribute>,
151 pub vis: Visibility,
152 pub struct_token: Token![struct],
153 pub ident: Ident,
154 pub generics: Generics,
155 pub fields: Fields,
156 pub semi_token: Option<Token![;]>,
157 }),
David Tolnay2b214082018-01-07 01:30:18 -0800158
159 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800163 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700164 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800165 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700166 pub ident: Ident,
167 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500168 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500169 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 }),
David Tolnay2b214082018-01-07 01:30:18 -0800171
172 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800173 ///
174 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800176 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700177 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800178 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700179 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500181 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 }),
David Tolnay2b214082018-01-07 01:30:18 -0800183
184 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800185 ///
186 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800188 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700189 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500190 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500191 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800192 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700193 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800195 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500196 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500197 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 pub items: Vec<TraitItem>,
199 }),
David Tolnay2b214082018-01-07 01:30:18 -0800200
David Tolnayc6b04dd2018-08-30 23:22:51 -0700201 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
202 ///
203 /// *This type is available if Syn is built with the `"full"` feature.*
204 pub TraitAlias(ItemTraitAlias {
205 pub attrs: Vec<Attribute>,
206 pub vis: Visibility,
207 pub trait_token: Token![trait],
208 pub ident: Ident,
209 pub generics: Generics,
210 pub eq_token: Token![=],
211 pub bounds: Punctuated<TypeParamBound, Token![+]>,
212 pub semi_token: Token![;],
213 }),
214
David Tolnay2b214082018-01-07 01:30:18 -0800215 /// An impl block providing trait or associated items: `impl<A> Trait
216 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800217 ///
218 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700219 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800220 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500221 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500222 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800223 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700225 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500226 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700227 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800228 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500229 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700230 pub items: Vec<ImplItem>,
231 }),
David Tolnay2b214082018-01-07 01:30:18 -0800232
233 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800234 ///
235 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800236 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800237 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800238 /// The `example` in `macro_rules! example { ... }`.
239 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800240 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500241 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800242 }),
David Tolnay2b214082018-01-07 01:30:18 -0800243
244 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800245 ///
246 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500247 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800248 pub attrs: Vec<Attribute>,
249 pub vis: Visibility,
250 pub macro_token: Token![macro],
251 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500252 pub paren_token: Paren,
253 pub args: TokenStream,
254 pub brace_token: Brace,
255 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800256 }),
David Tolnay2b214082018-01-07 01:30:18 -0800257
258 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800259 ///
260 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500261 pub Verbatim(ItemVerbatim #manual_extra_traits {
262 pub tts: TokenStream,
263 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 }
David Tolnayb79ee962016-09-04 09:39:20 -0700265}
266
David Tolnay9c76bcb2017-12-26 23:14:59 -0500267#[cfg(feature = "extra-traits")]
268impl Eq for ItemMacro2 {}
269
270#[cfg(feature = "extra-traits")]
271impl PartialEq for ItemMacro2 {
272 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700273 self.attrs == other.attrs
274 && self.vis == other.vis
275 && self.macro_token == other.macro_token
276 && self.ident == other.ident
277 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500278 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
279 && self.brace_token == other.brace_token
280 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500281 }
282}
283
284#[cfg(feature = "extra-traits")]
285impl Hash for ItemMacro2 {
286 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500287 where
288 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500289 {
290 self.attrs.hash(state);
291 self.vis.hash(state);
292 self.macro_token.hash(state);
293 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500294 self.paren_token.hash(state);
295 TokenStreamHelper(&self.args).hash(state);
296 self.brace_token.hash(state);
297 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500298 }
299}
300
David Tolnay2ae520a2017-12-29 11:19:50 -0500301#[cfg(feature = "extra-traits")]
302impl Eq for ItemVerbatim {}
303
304#[cfg(feature = "extra-traits")]
305impl PartialEq for ItemVerbatim {
306 fn eq(&self, other: &Self) -> bool {
307 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
308 }
309}
310
311#[cfg(feature = "extra-traits")]
312impl Hash for ItemVerbatim {
313 fn hash<H>(&self, state: &mut H)
314 where
315 H: Hasher,
316 {
317 TokenStreamHelper(&self.tts).hash(state);
318 }
319}
320
David Tolnay0e837402016-12-22 17:25:55 -0500321impl From<DeriveInput> for Item {
322 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500323 match input.data {
324 Data::Struct(data) => Item::Struct(ItemStruct {
325 attrs: input.attrs,
326 vis: input.vis,
327 struct_token: data.struct_token,
328 ident: input.ident,
329 generics: input.generics,
330 fields: data.fields,
331 semi_token: data.semi_token,
332 }),
333 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500334 attrs: input.attrs,
335 vis: input.vis,
336 enum_token: data.enum_token,
337 ident: input.ident,
338 generics: input.generics,
339 brace_token: data.brace_token,
340 variants: data.variants,
341 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500343 attrs: input.attrs,
344 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500345 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500346 ident: input.ident,
347 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500348 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500349 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700350 }
351 }
352}
353
Alex Crichton62a0a592017-05-22 13:58:53 -0700354ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800355 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800356 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800357 /// *This type is available if Syn is built with the `"full"` feature.*
358 ///
David Tolnay614a0142018-01-07 10:25:43 -0800359 /// # Syntax tree enum
360 ///
361 /// This type is a [syntax tree enum].
362 ///
363 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500364 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200365 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800366 ///
367 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500368 pub Path(UsePath {
369 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200370 pub colon2_token: Token![::],
371 pub tree: Box<UseTree>,
372 }),
373
374 /// An identifier imported by a `use` item: `HashMap`.
375 ///
376 /// *This type is available if Syn is built with the `"full"` feature.*
377 pub Name(UseName {
378 pub ident: Ident,
379 }),
380
381 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
382 ///
383 /// *This type is available if Syn is built with the `"full"` feature.*
384 pub Rename(UseRename {
385 pub ident: Ident,
386 pub as_token: Token![as],
387 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800389
David Tolnay05658502018-01-07 09:56:37 -0800390 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800391 ///
392 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500393 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800396
David Tolnayd97a7d22018-03-31 19:17:01 +0200397 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800398 ///
399 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200400 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500401 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500402 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 }),
404 }
405}
406
Alex Crichton62a0a592017-05-22 13:58:53 -0700407ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800408 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800409 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800410 /// *This type is available if Syn is built with the `"full"` feature.*
411 ///
David Tolnay614a0142018-01-07 10:25:43 -0800412 /// # Syntax tree enum
413 ///
414 /// This type is a [syntax tree enum].
415 ///
416 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800417 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800418 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800419 ///
420 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800422 pub attrs: Vec<Attribute>,
423 pub vis: Visibility,
424 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700425 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800426 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 }),
David Tolnayebb72722018-01-07 01:14:13 -0800428
429 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800430 ///
431 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800433 pub attrs: Vec<Attribute>,
434 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500436 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800437 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800438 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800439 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700441 }),
David Tolnayebb72722018-01-07 01:14:13 -0800442
443 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800444 ///
445 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800446 pub Type(ForeignItemType {
447 pub attrs: Vec<Attribute>,
448 pub vis: Visibility,
449 pub type_token: Token![type],
450 pub ident: Ident,
451 pub semi_token: Token![;],
452 }),
David Tolnayebb72722018-01-07 01:14:13 -0800453
David Tolnay435c1782018-08-24 16:15:44 -0400454 /// A macro invocation within an extern block.
455 ///
456 /// *This type is available if Syn is built with the `"full"` feature.*
457 pub Macro(ForeignItemMacro {
458 pub attrs: Vec<Attribute>,
459 pub mac: Macro,
460 pub semi_token: Option<Token![;]>,
461 }),
462
David Tolnayebb72722018-01-07 01:14:13 -0800463 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800464 ///
465 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500466 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
467 pub tts: TokenStream,
468 }),
469 }
470}
471
472#[cfg(feature = "extra-traits")]
473impl Eq for ForeignItemVerbatim {}
474
475#[cfg(feature = "extra-traits")]
476impl PartialEq for ForeignItemVerbatim {
477 fn eq(&self, other: &Self) -> bool {
478 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
479 }
480}
481
482#[cfg(feature = "extra-traits")]
483impl Hash for ForeignItemVerbatim {
484 fn hash<H>(&self, state: &mut H)
485 where
486 H: Hasher,
487 {
488 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700490}
491
David Tolnayda705bd2017-11-10 21:58:05 -0800492ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800493 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800494 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800495 /// *This type is available if Syn is built with the `"full"` feature.*
496 ///
David Tolnay614a0142018-01-07 10:25:43 -0800497 /// # Syntax tree enum
498 ///
499 /// This type is a [syntax tree enum].
500 ///
501 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800502 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800503 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800504 ///
505 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800507 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800508 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700509 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800511 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800512 pub default: Option<(Token![=], Expr)>,
513 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 }),
David Tolnayebb72722018-01-07 01:14:13 -0800515
516 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800517 ///
518 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700519 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800520 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700521 pub sig: MethodSig,
522 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800523 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700524 }),
David Tolnayebb72722018-01-07 01:14:13 -0800525
526 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800527 ///
528 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800530 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700532 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500533 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800534 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500535 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800536 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800537 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 }),
David Tolnayebb72722018-01-07 01:14:13 -0800539
540 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800541 ///
542 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800543 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800544 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800545 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500546 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800547 }),
David Tolnayebb72722018-01-07 01:14:13 -0800548
549 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800550 ///
551 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500552 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
553 pub tts: TokenStream,
554 }),
555 }
556}
557
558#[cfg(feature = "extra-traits")]
559impl Eq for TraitItemVerbatim {}
560
561#[cfg(feature = "extra-traits")]
562impl PartialEq for TraitItemVerbatim {
563 fn eq(&self, other: &Self) -> bool {
564 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
565 }
566}
567
568#[cfg(feature = "extra-traits")]
569impl Hash for TraitItemVerbatim {
570 fn hash<H>(&self, state: &mut H)
571 where
572 H: Hasher,
573 {
574 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700576}
577
Alex Crichton62a0a592017-05-22 13:58:53 -0700578ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800579 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800580 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800581 /// *This type is available if Syn is built with the `"full"` feature.*
582 ///
David Tolnay614a0142018-01-07 10:25:43 -0800583 /// # Syntax tree enum
584 ///
585 /// This type is a [syntax tree enum].
586 ///
587 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800588 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800589 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800590 ///
591 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800593 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700594 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500595 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700597 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800598 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800599 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800600 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700601 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700603 }),
David Tolnayebb72722018-01-07 01:14:13 -0800604
605 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800606 ///
607 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700608 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800609 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700610 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500611 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700612 pub sig: MethodSig,
613 pub block: Block,
614 }),
David Tolnayebb72722018-01-07 01:14:13 -0800615
616 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800617 ///
618 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700619 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800620 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700621 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500622 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800623 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700624 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500625 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800626 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800627 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700629 }),
David Tolnayebb72722018-01-07 01:14:13 -0800630
David Tolnaybb82ef02018-08-24 20:15:45 -0400631 /// An existential type within an impl block.
632 ///
633 /// *This type is available if Syn is built with the `"full"` feature.*
634 pub Existential(ImplItemExistential {
635 pub attrs: Vec<Attribute>,
636 pub existential_token: Token![existential],
637 pub type_token: Token![type],
638 pub ident: Ident,
639 pub generics: Generics,
640 pub colon_token: Option<Token![:]>,
641 pub bounds: Punctuated<TypeParamBound, Token![+]>,
642 pub semi_token: Token![;],
643 }),
644
David Tolnayebb72722018-01-07 01:14:13 -0800645 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800646 ///
647 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800648 pub Macro(ImplItemMacro {
649 pub attrs: Vec<Attribute>,
650 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500651 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800652 }),
David Tolnayebb72722018-01-07 01:14:13 -0800653
654 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800655 ///
656 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500657 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
658 pub tts: TokenStream,
659 }),
660 }
661}
662
663#[cfg(feature = "extra-traits")]
664impl Eq for ImplItemVerbatim {}
665
666#[cfg(feature = "extra-traits")]
667impl PartialEq for ImplItemVerbatim {
668 fn eq(&self, other: &Self) -> bool {
669 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
670 }
671}
672
673#[cfg(feature = "extra-traits")]
674impl Hash for ImplItemVerbatim {
675 fn hash<H>(&self, state: &mut H)
676 where
677 H: Hasher,
678 {
679 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700680 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700681}
682
683ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800684 /// A method's signature in a trait or implementation: `unsafe fn
685 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800686 ///
687 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500689 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500690 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900691 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700693 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 }
696}
697
698ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800699 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800700 ///
701 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800703 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500704 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500705 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500706 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500707 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500708 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700710}
711
Alex Crichton62a0a592017-05-22 13:58:53 -0700712ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800713 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800714 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800715 /// *This type is available if Syn is built with the `"full"` feature.*
716 ///
David Tolnay614a0142018-01-07 10:25:43 -0800717 /// # Syntax tree enum
718 ///
719 /// This type is a [syntax tree enum].
720 ///
721 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800723 /// Self captured by reference in a function signature: `&self` or `&mut
724 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800725 ///
726 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800728 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500730 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500731 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800733
David Tolnay3f559052018-01-06 23:59:48 -0800734 /// Self captured by value in a function signature: `self` or `mut
735 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800736 ///
737 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700738 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500739 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700741 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800742
David Tolnay3f559052018-01-06 23:59:48 -0800743 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800744 ///
745 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 pub Captured(ArgCaptured {
747 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800748 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800751
David Tolnay3f559052018-01-06 23:59:48 -0800752 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500753 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800754 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800755 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 }
David Tolnay62f374c2016-10-02 13:37:00 -0700757}
758
David Tolnayedf2b992016-09-23 20:43:45 -0700759#[cfg(feature = "parsing")]
760pub mod parsing {
761 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700762
David Tolnay94d304f2018-08-30 23:43:53 -0700763 use ext::IdentExt;
David Tolnay2ad62c12018-08-26 19:00:35 -0400764 use parse::{Parse, ParseStream, Result};
David Tolnay84aa0752016-10-02 23:01:13 -0700765
David Tolnay6a170ce2018-08-26 22:29:24 -0700766 impl Parse for Item {
767 fn parse(input: ParseStream) -> Result<Self> {
768 let ahead = input.fork();
769 ahead.call(Attribute::parse_outer)?;
770 let vis: Visibility = ahead.parse()?;
771
772 let lookahead = ahead.lookahead1();
773 if lookahead.peek(Token![extern]) {
774 ahead.parse::<Token![extern]>()?;
775 let lookahead = ahead.lookahead1();
776 if lookahead.peek(Token![crate]) {
777 input.parse().map(Item::ExternCrate)
778 } else if lookahead.peek(Token![fn]) {
779 input.parse().map(Item::Fn)
780 } else if lookahead.peek(token::Brace) {
781 input.parse().map(Item::ForeignMod)
782 } else if lookahead.peek(LitStr) {
783 ahead.parse::<LitStr>()?;
784 let lookahead = ahead.lookahead1();
785 if lookahead.peek(token::Brace) {
786 input.parse().map(Item::ForeignMod)
787 } else if lookahead.peek(Token![fn]) {
788 input.parse().map(Item::Fn)
789 } else {
790 Err(lookahead.error())
791 }
792 } else {
793 Err(lookahead.error())
794 }
795 } else if lookahead.peek(Token![use]) {
796 input.parse().map(Item::Use)
797 } else if lookahead.peek(Token![static]) {
798 input.parse().map(Item::Static)
799 } else if lookahead.peek(Token![const]) {
800 ahead.parse::<Token![const]>()?;
801 let lookahead = ahead.lookahead1();
David Tolnay5cd40fc2018-10-27 21:53:30 -0700802 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700803 input.parse().map(Item::Const)
804 } else if lookahead.peek(Token![unsafe])
805 || lookahead.peek(Token![async])
806 || lookahead.peek(Token![extern])
807 || lookahead.peek(Token![fn])
808 {
809 input.parse().map(Item::Fn)
810 } else {
811 Err(lookahead.error())
812 }
813 } else if lookahead.peek(Token![unsafe]) {
814 ahead.parse::<Token![unsafe]>()?;
815 let lookahead = ahead.lookahead1();
816 if lookahead.peek(Token![trait])
817 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
818 {
819 input.parse().map(Item::Trait)
820 } else if lookahead.peek(Token![impl ]) {
821 input.parse().map(Item::Impl)
822 } else if lookahead.peek(Token![async])
823 || lookahead.peek(Token![extern])
824 || lookahead.peek(Token![fn])
825 {
826 input.parse().map(Item::Fn)
827 } else {
828 Err(lookahead.error())
829 }
830 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
831 input.parse().map(Item::Fn)
832 } else if lookahead.peek(Token![mod]) {
833 input.parse().map(Item::Mod)
834 } else if lookahead.peek(Token![type]) {
835 input.parse().map(Item::Type)
836 } else if lookahead.peek(Token![existential]) {
837 input.parse().map(Item::Existential)
838 } else if lookahead.peek(Token![struct]) {
839 input.parse().map(Item::Struct)
840 } else if lookahead.peek(Token![enum]) {
841 input.parse().map(Item::Enum)
842 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
843 input.parse().map(Item::Union)
David Tolnayc4de0d72018-11-06 21:11:00 -0800844 } else if lookahead.peek(Token![trait]) {
845 input.call(parse_trait_or_trait_alias)
846 } else if lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700847 input.parse().map(Item::Trait)
848 } else if lookahead.peek(Token![impl ])
849 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
850 {
851 input.parse().map(Item::Impl)
852 } else if lookahead.peek(Token![macro]) {
853 input.parse().map(Item::Macro2)
854 } else if vis.is_inherited()
855 && (lookahead.peek(Ident)
856 || lookahead.peek(Token![self])
857 || lookahead.peek(Token![super])
858 || lookahead.peek(Token![extern])
859 || lookahead.peek(Token![crate])
860 || lookahead.peek(Token![::]))
861 {
862 input.parse().map(Item::Macro)
863 } else {
864 Err(lookahead.error())
865 }
866 }
867 }
Alex Crichton954046c2017-05-30 21:49:42 -0700868
David Tolnay3779bb72018-08-26 18:46:07 -0700869 impl Parse for ItemMacro {
870 fn parse(input: ParseStream) -> Result<Self> {
871 let attrs = input.call(Attribute::parse_outer)?;
872 let path = input.call(Path::parse_mod_style)?;
873 let bang_token: Token![!] = input.parse()?;
874 let ident: Option<Ident> = input.parse()?;
875 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700876 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700877 Some(input.parse()?)
878 } else {
879 None
880 };
881 Ok(ItemMacro {
882 attrs: attrs,
883 ident: ident,
884 mac: Macro {
885 path: path,
886 bang_token: bang_token,
887 delimiter: delimiter,
888 tts: tts,
889 },
890 semi_token: semi_token,
891 })
892 }
893 }
David Tolnayedf2b992016-09-23 20:43:45 -0700894
David Tolnay500d8322017-12-18 00:32:51 -0800895 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700896 impl Parse for ItemMacro2 {
897 fn parse(input: ParseStream) -> Result<Self> {
898 let args;
899 let body;
900 Ok(ItemMacro2 {
901 attrs: input.call(Attribute::parse_outer)?,
902 vis: input.parse()?,
903 macro_token: input.parse()?,
904 ident: input.parse()?,
905 paren_token: parenthesized!(args in input),
906 args: args.parse()?,
907 brace_token: braced!(body in input),
908 body: body.parse()?,
909 })
910 }
911 }
David Tolnay500d8322017-12-18 00:32:51 -0800912
David Tolnay3779bb72018-08-26 18:46:07 -0700913 impl Parse for ItemExternCrate {
914 fn parse(input: ParseStream) -> Result<Self> {
915 Ok(ItemExternCrate {
916 attrs: input.call(Attribute::parse_outer)?,
917 vis: input.parse()?,
918 extern_token: input.parse()?,
919 crate_token: input.parse()?,
David Tolnaycff361c2018-12-15 00:04:32 -0800920 ident: {
921 if input.peek(Token![self]) {
922 input.call(Ident::parse_any)?
923 } else {
924 input.parse()?
925 }
926 },
David Tolnay3779bb72018-08-26 18:46:07 -0700927 rename: {
928 if input.peek(Token![as]) {
929 let as_token: Token![as] = input.parse()?;
930 let rename: Ident = input.parse()?;
931 Some((as_token, rename))
932 } else {
933 None
934 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800935 },
David Tolnay3779bb72018-08-26 18:46:07 -0700936 semi_token: input.parse()?,
937 })
938 }
939 }
940
941 impl Parse for ItemUse {
942 fn parse(input: ParseStream) -> Result<Self> {
943 Ok(ItemUse {
944 attrs: input.call(Attribute::parse_outer)?,
945 vis: input.parse()?,
946 use_token: input.parse()?,
947 leading_colon: input.parse()?,
948 tree: input.call(use_tree)?,
949 semi_token: input.parse()?,
950 })
951 }
952 }
953
954 fn use_tree(input: ParseStream) -> Result<UseTree> {
955 let lookahead = input.lookahead1();
956 if lookahead.peek(Ident)
957 || lookahead.peek(Token![self])
958 || lookahead.peek(Token![super])
959 || lookahead.peek(Token![crate])
960 || lookahead.peek(Token![extern])
961 {
David Tolnay0dea1b92018-08-30 17:47:29 -0700962 let ident = input.call(Ident::parse_any)?;
David Tolnay3779bb72018-08-26 18:46:07 -0700963 if input.peek(Token![::]) {
964 Ok(UseTree::Path(UsePath {
965 ident: ident,
966 colon2_token: input.parse()?,
967 tree: Box::new(input.call(use_tree)?),
968 }))
969 } else if input.peek(Token![as]) {
970 Ok(UseTree::Rename(UseRename {
971 ident: ident,
972 as_token: input.parse()?,
David Tolnayd72b6452018-09-01 18:27:54 -0700973 rename: {
974 if input.peek(Ident) {
975 input.parse()?
976 } else if input.peek(Token![_]) {
977 Ident::from(input.parse::<Token![_]>()?)
978 } else {
979 return Err(input.error("expected identifier or underscore"));
980 }
981 },
David Tolnay3779bb72018-08-26 18:46:07 -0700982 }))
983 } else {
984 Ok(UseTree::Name(UseName { ident: ident }))
985 }
986 } else if lookahead.peek(Token![*]) {
987 Ok(UseTree::Glob(UseGlob {
988 star_token: input.parse()?,
989 }))
990 } else if lookahead.peek(token::Brace) {
991 let content;
992 Ok(UseTree::Group(UseGroup {
993 brace_token: braced!(content in input),
994 items: content.parse_terminated(use_tree)?,
995 }))
996 } else {
997 Err(lookahead.error())
998 }
999 }
1000
1001 impl Parse for ItemStatic {
1002 fn parse(input: ParseStream) -> Result<Self> {
1003 Ok(ItemStatic {
1004 attrs: input.call(Attribute::parse_outer)?,
1005 vis: input.parse()?,
1006 static_token: input.parse()?,
1007 mutability: input.parse()?,
1008 ident: input.parse()?,
1009 colon_token: input.parse()?,
1010 ty: input.parse()?,
1011 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001012 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001013 semi_token: input.parse()?,
1014 })
1015 }
1016 }
1017
1018 impl Parse for ItemConst {
1019 fn parse(input: ParseStream) -> Result<Self> {
1020 Ok(ItemConst {
1021 attrs: input.call(Attribute::parse_outer)?,
1022 vis: input.parse()?,
1023 const_token: input.parse()?,
David Tolnay5cd40fc2018-10-27 21:53:30 -07001024 ident: {
1025 let lookahead = input.lookahead1();
1026 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
1027 input.call(Ident::parse_any)?
1028 } else {
1029 return Err(lookahead.error());
1030 }
1031 },
David Tolnay3779bb72018-08-26 18:46:07 -07001032 colon_token: input.parse()?,
1033 ty: input.parse()?,
1034 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001035 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001036 semi_token: input.parse()?,
1037 })
1038 }
1039 }
1040
1041 impl Parse for ItemFn {
1042 fn parse(input: ParseStream) -> Result<Self> {
1043 let outer_attrs = input.call(Attribute::parse_outer)?;
1044 let vis: Visibility = input.parse()?;
1045 let constness: Option<Token![const]> = input.parse()?;
1046 let unsafety: Option<Token![unsafe]> = input.parse()?;
1047 let asyncness: Option<Token![async]> = input.parse()?;
1048 let abi: Option<Abi> = input.parse()?;
1049 let fn_token: Token![fn] = input.parse()?;
1050 let ident: Ident = input.parse()?;
1051 let generics: Generics = input.parse()?;
1052
1053 let content;
1054 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001055 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001056
1057 let output: ReturnType = input.parse()?;
1058 let where_clause: Option<WhereClause> = input.parse()?;
1059
1060 let content;
1061 let brace_token = braced!(content in input);
1062 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001063 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001064
1065 Ok(ItemFn {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001066 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001067 vis: vis,
1068 constness: constness,
1069 unsafety: unsafety,
1070 asyncness: asyncness,
1071 abi: abi,
1072 ident: ident,
1073 decl: Box::new(FnDecl {
1074 fn_token: fn_token,
1075 paren_token: paren_token,
1076 inputs: inputs,
1077 output: output,
1078 variadic: None,
1079 generics: Generics {
1080 where_clause: where_clause,
1081 ..generics
1082 },
1083 }),
1084 block: Box::new(Block {
1085 brace_token: brace_token,
1086 stmts: stmts,
1087 }),
1088 })
1089 }
1090 }
David Tolnay42602292016-10-01 22:25:45 -07001091
David Tolnay2ad62c12018-08-26 19:00:35 -04001092 impl Parse for FnArg {
1093 fn parse(input: ParseStream) -> Result<Self> {
1094 if input.peek(Token![&]) {
1095 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001096 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1097 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001098 }
1099 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001100
David Tolnay2ad62c12018-08-26 19:00:35 -04001101 if input.peek(Token![mut]) || input.peek(Token![self]) {
1102 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001103 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1104 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001105 }
1106 }
1107
1108 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001109 let err = match ahead.call(arg_captured) {
1110 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001111 Err(err) => err,
1112 };
1113
1114 let ahead = input.fork();
1115 if ahead.parse::<Type>().is_ok() {
1116 return input.parse().map(FnArg::Ignored);
1117 }
1118
1119 Err(err)
1120 }
1121 }
1122
David Tolnay3779bb72018-08-26 18:46:07 -07001123 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1124 Ok(ArgSelfRef {
1125 and_token: input.parse()?,
1126 lifetime: input.parse()?,
1127 mutability: input.parse()?,
1128 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001129 })
David Tolnay3779bb72018-08-26 18:46:07 -07001130 }
David Tolnay35902302016-10-06 01:11:08 -07001131
David Tolnay3779bb72018-08-26 18:46:07 -07001132 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1133 Ok(ArgSelf {
1134 mutability: input.parse()?,
1135 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001136 })
David Tolnay3779bb72018-08-26 18:46:07 -07001137 }
1138
1139 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1140 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001141 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001142 colon_token: input.parse()?,
1143 ty: input.parse()?,
1144 })
1145 }
1146
1147 impl Parse for ItemMod {
1148 fn parse(input: ParseStream) -> Result<Self> {
1149 let outer_attrs = input.call(Attribute::parse_outer)?;
1150 let vis: Visibility = input.parse()?;
1151 let mod_token: Token![mod] = input.parse()?;
1152 let ident: Ident = input.parse()?;
1153
1154 let lookahead = input.lookahead1();
1155 if lookahead.peek(Token![;]) {
1156 Ok(ItemMod {
1157 attrs: outer_attrs,
1158 vis: vis,
1159 mod_token: mod_token,
1160 ident: ident,
1161 content: None,
1162 semi: Some(input.parse()?),
1163 })
1164 } else if lookahead.peek(token::Brace) {
1165 let content;
1166 let brace_token = braced!(content in input);
1167 let inner_attrs = content.call(Attribute::parse_inner)?;
1168
1169 let mut items = Vec::new();
1170 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001171 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001172 }
1173
1174 Ok(ItemMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001175 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001176 vis: vis,
1177 mod_token: mod_token,
1178 ident: ident,
1179 content: Some((brace_token, items)),
1180 semi: None,
1181 })
1182 } else {
1183 Err(lookahead.error())
1184 }
1185 }
1186 }
1187
1188 impl Parse for ItemForeignMod {
1189 fn parse(input: ParseStream) -> Result<Self> {
1190 let outer_attrs = input.call(Attribute::parse_outer)?;
1191 let abi: Abi = input.parse()?;
1192
1193 let content;
1194 let brace_token = braced!(content in input);
1195 let inner_attrs = content.call(Attribute::parse_inner)?;
1196 let mut items = Vec::new();
1197 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001198 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001199 }
1200
1201 Ok(ItemForeignMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001202 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001203 abi: abi,
1204 brace_token: brace_token,
1205 items: items,
1206 })
1207 }
1208 }
David Tolnay35902302016-10-06 01:11:08 -07001209
David Tolnay6a170ce2018-08-26 22:29:24 -07001210 impl Parse for ForeignItem {
1211 fn parse(input: ParseStream) -> Result<Self> {
1212 let ahead = input.fork();
1213 ahead.call(Attribute::parse_outer)?;
1214 let vis: Visibility = ahead.parse()?;
1215
1216 let lookahead = ahead.lookahead1();
1217 if lookahead.peek(Token![fn]) {
1218 input.parse().map(ForeignItem::Fn)
1219 } else if lookahead.peek(Token![static]) {
1220 input.parse().map(ForeignItem::Static)
1221 } else if lookahead.peek(Token![type]) {
1222 input.parse().map(ForeignItem::Type)
1223 } else if vis.is_inherited()
1224 && (lookahead.peek(Ident)
1225 || lookahead.peek(Token![self])
1226 || lookahead.peek(Token![super])
1227 || lookahead.peek(Token![extern])
1228 || lookahead.peek(Token![crate])
1229 || lookahead.peek(Token![::]))
1230 {
1231 input.parse().map(ForeignItem::Macro)
1232 } else {
1233 Err(lookahead.error())
1234 }
1235 }
1236 }
David Tolnay35902302016-10-06 01:11:08 -07001237
David Tolnay3779bb72018-08-26 18:46:07 -07001238 impl Parse for ForeignItemFn {
1239 fn parse(input: ParseStream) -> Result<Self> {
1240 let attrs = input.call(Attribute::parse_outer)?;
1241 let vis: Visibility = input.parse()?;
1242 let fn_token: Token![fn] = input.parse()?;
1243 let ident: Ident = input.parse()?;
1244 let generics: Generics = input.parse()?;
1245
1246 let content;
1247 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001248 let mut inputs = Punctuated::new();
1249 while !content.is_empty() && !content.peek(Token![...]) {
1250 inputs.push_value(content.parse()?);
1251 if content.is_empty() {
1252 break;
1253 }
1254 inputs.push_punct(content.parse()?);
1255 }
David Tolnay3779bb72018-08-26 18:46:07 -07001256 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1257 content.parse()?
1258 } else {
1259 None
1260 };
1261
1262 let output: ReturnType = input.parse()?;
1263 let where_clause: Option<WhereClause> = input.parse()?;
1264 let semi_token: Token![;] = input.parse()?;
1265
1266 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001267 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001268 vis: vis,
1269 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001270 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001271 fn_token: fn_token,
1272 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001273 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001274 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001275 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001276 generics: Generics {
1277 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001278 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001279 },
1280 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001281 semi_token: semi_token,
1282 })
1283 }
1284 }
David Tolnay35902302016-10-06 01:11:08 -07001285
David Tolnay3779bb72018-08-26 18:46:07 -07001286 impl Parse for ForeignItemStatic {
1287 fn parse(input: ParseStream) -> Result<Self> {
1288 Ok(ForeignItemStatic {
1289 attrs: input.call(Attribute::parse_outer)?,
1290 vis: input.parse()?,
1291 static_token: input.parse()?,
1292 mutability: input.parse()?,
1293 ident: input.parse()?,
1294 colon_token: input.parse()?,
1295 ty: input.parse()?,
1296 semi_token: input.parse()?,
1297 })
1298 }
1299 }
David Tolnay35902302016-10-06 01:11:08 -07001300
David Tolnay3779bb72018-08-26 18:46:07 -07001301 impl Parse for ForeignItemType {
1302 fn parse(input: ParseStream) -> Result<Self> {
1303 Ok(ForeignItemType {
1304 attrs: input.call(Attribute::parse_outer)?,
1305 vis: input.parse()?,
1306 type_token: input.parse()?,
1307 ident: input.parse()?,
1308 semi_token: input.parse()?,
1309 })
1310 }
1311 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001312
David Tolnay3779bb72018-08-26 18:46:07 -07001313 impl Parse for ForeignItemMacro {
1314 fn parse(input: ParseStream) -> Result<Self> {
1315 let attrs = input.call(Attribute::parse_outer)?;
1316 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001317 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001318 None
1319 } else {
1320 Some(input.parse()?)
1321 };
1322 Ok(ForeignItemMacro {
1323 attrs: attrs,
1324 mac: mac,
1325 semi_token: semi_token,
1326 })
1327 }
1328 }
David Tolnay78572112018-08-01 00:36:18 -07001329
David Tolnay3779bb72018-08-26 18:46:07 -07001330 impl Parse for ItemType {
1331 fn parse(input: ParseStream) -> Result<Self> {
1332 Ok(ItemType {
1333 attrs: input.call(Attribute::parse_outer)?,
1334 vis: input.parse()?,
1335 type_token: input.parse()?,
1336 ident: input.parse()?,
1337 generics: {
1338 let mut generics: Generics = input.parse()?;
1339 generics.where_clause = input.parse()?;
1340 generics
1341 },
1342 eq_token: input.parse()?,
1343 ty: input.parse()?,
1344 semi_token: input.parse()?,
1345 })
1346 }
1347 }
David Tolnay3cf52982016-10-01 17:11:37 -07001348
David Tolnay3779bb72018-08-26 18:46:07 -07001349 impl Parse for ItemExistential {
1350 fn parse(input: ParseStream) -> Result<Self> {
1351 Ok(ItemExistential {
1352 attrs: input.call(Attribute::parse_outer)?,
1353 vis: input.parse()?,
1354 existential_token: input.parse()?,
1355 type_token: input.parse()?,
1356 ident: input.parse()?,
1357 generics: {
1358 let mut generics: Generics = input.parse()?;
1359 generics.where_clause = input.parse()?;
1360 generics
1361 },
1362 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001363 bounds: {
1364 let mut bounds = Punctuated::new();
1365 while !input.peek(Token![;]) {
1366 if !bounds.is_empty() {
1367 bounds.push_punct(input.parse()?);
1368 }
1369 bounds.push_value(input.parse()?);
1370 }
1371 bounds
1372 },
David Tolnay3779bb72018-08-26 18:46:07 -07001373 semi_token: input.parse()?,
1374 })
1375 }
1376 }
David Tolnay758ee132018-08-21 21:29:40 -04001377
David Tolnay6a170ce2018-08-26 22:29:24 -07001378 impl Parse for ItemStruct {
1379 fn parse(input: ParseStream) -> Result<Self> {
1380 let attrs = input.call(Attribute::parse_outer)?;
1381 let vis = input.parse::<Visibility>()?;
1382 let struct_token = input.parse::<Token![struct]>()?;
1383 let ident = input.parse::<Ident>()?;
1384 let generics = input.parse::<Generics>()?;
1385 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1386 Ok(ItemStruct {
1387 attrs: attrs,
1388 vis: vis,
1389 struct_token: struct_token,
1390 ident: ident,
1391 generics: Generics {
1392 where_clause: where_clause,
1393 ..generics
1394 },
1395 fields: fields,
1396 semi_token: semi_token,
1397 })
1398 }
1399 }
David Tolnay42602292016-10-01 22:25:45 -07001400
David Tolnay6a170ce2018-08-26 22:29:24 -07001401 impl Parse for ItemEnum {
1402 fn parse(input: ParseStream) -> Result<Self> {
1403 let attrs = input.call(Attribute::parse_outer)?;
1404 let vis = input.parse::<Visibility>()?;
1405 let enum_token = input.parse::<Token![enum]>()?;
1406 let ident = input.parse::<Ident>()?;
1407 let generics = input.parse::<Generics>()?;
1408 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1409 Ok(ItemEnum {
1410 attrs: attrs,
1411 vis: vis,
1412 enum_token: enum_token,
1413 ident: ident,
1414 generics: Generics {
1415 where_clause: where_clause,
1416 ..generics
1417 },
1418 brace_token: brace_token,
1419 variants: variants,
1420 })
1421 }
1422 }
David Tolnay4c614be2017-11-10 00:02:38 -08001423
David Tolnay6a170ce2018-08-26 22:29:24 -07001424 impl Parse for ItemUnion {
1425 fn parse(input: ParseStream) -> Result<Self> {
1426 let attrs = input.call(Attribute::parse_outer)?;
1427 let vis = input.parse::<Visibility>()?;
1428 let union_token = input.parse::<Token![union]>()?;
1429 let ident = input.parse::<Ident>()?;
1430 let generics = input.parse::<Generics>()?;
1431 let (where_clause, fields) = derive::parsing::data_union(input)?;
1432 Ok(ItemUnion {
1433 attrs: attrs,
1434 vis: vis,
1435 union_token: union_token,
1436 ident: ident,
1437 generics: Generics {
1438 where_clause: where_clause,
1439 ..generics
1440 },
1441 fields: fields,
1442 })
1443 }
1444 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001445
David Tolnayc4de0d72018-11-06 21:11:00 -08001446 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
1447 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1448 let lookahead = input.lookahead1();
1449 if lookahead.peek(token::Brace)
1450 || lookahead.peek(Token![:])
1451 || lookahead.peek(Token![where])
1452 {
1453 let unsafety = None;
1454 let auto_token = None;
1455 parse_rest_of_trait(
1456 input,
1457 attrs,
1458 vis,
1459 unsafety,
1460 auto_token,
1461 trait_token,
1462 ident,
1463 generics,
1464 )
1465 .map(Item::Trait)
1466 } else if lookahead.peek(Token![=]) {
1467 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1468 .map(Item::TraitAlias)
1469 } else {
1470 Err(lookahead.error())
1471 }
1472 }
1473
David Tolnay6a170ce2018-08-26 22:29:24 -07001474 impl Parse for ItemTrait {
1475 fn parse(input: ParseStream) -> Result<Self> {
1476 let attrs = input.call(Attribute::parse_outer)?;
1477 let vis: Visibility = input.parse()?;
1478 let unsafety: Option<Token![unsafe]> = input.parse()?;
1479 let auto_token: Option<Token![auto]> = input.parse()?;
1480 let trait_token: Token![trait] = input.parse()?;
1481 let ident: Ident = input.parse()?;
David Tolnayc4de0d72018-11-06 21:11:00 -08001482 let generics: Generics = input.parse()?;
1483 parse_rest_of_trait(
1484 input,
1485 attrs,
1486 vis,
1487 unsafety,
1488 auto_token,
1489 trait_token,
1490 ident,
1491 generics,
1492 )
1493 }
1494 }
David Tolnayf5ebc192018-08-30 18:23:46 -07001495
David Tolnayc4de0d72018-11-06 21:11:00 -08001496 fn parse_rest_of_trait(
1497 input: ParseStream,
1498 attrs: Vec<Attribute>,
1499 vis: Visibility,
1500 unsafety: Option<Token![unsafe]>,
1501 auto_token: Option<Token![auto]>,
1502 trait_token: Token![trait],
1503 ident: Ident,
1504 mut generics: Generics,
1505 ) -> Result<ItemTrait> {
1506 let colon_token: Option<Token![:]> = input.parse()?;
1507
1508 let mut supertraits = Punctuated::new();
1509 if colon_token.is_some() {
1510 loop {
1511 supertraits.push_value(input.parse()?);
1512 if input.peek(Token![where]) || input.peek(token::Brace) {
1513 break;
1514 }
1515 supertraits.push_punct(input.parse()?);
1516 if input.peek(Token![where]) || input.peek(token::Brace) {
1517 break;
David Tolnayf5ebc192018-08-30 18:23:46 -07001518 }
1519 }
David Tolnay6a170ce2018-08-26 22:29:24 -07001520 }
David Tolnayc4de0d72018-11-06 21:11:00 -08001521
1522 generics.where_clause = input.parse()?;
1523
1524 let content;
1525 let brace_token = braced!(content in input);
1526 let mut items = Vec::new();
1527 while !content.is_empty() {
1528 items.push(content.parse()?);
1529 }
1530
1531 Ok(ItemTrait {
1532 attrs: attrs,
1533 vis: vis,
1534 unsafety: unsafety,
1535 auto_token: auto_token,
1536 trait_token: trait_token,
1537 ident: ident,
1538 generics: generics,
1539 colon_token: colon_token,
1540 supertraits: supertraits,
1541 brace_token: brace_token,
1542 items: items,
1543 })
David Tolnay6a170ce2018-08-26 22:29:24 -07001544 }
1545
David Tolnayc6b04dd2018-08-30 23:22:51 -07001546 impl Parse for ItemTraitAlias {
1547 fn parse(input: ParseStream) -> Result<Self> {
David Tolnayc4de0d72018-11-06 21:11:00 -08001548 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1549 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
David Tolnayc6b04dd2018-08-30 23:22:51 -07001550 }
1551 }
1552
David Tolnayc4de0d72018-11-06 21:11:00 -08001553 fn parse_start_of_trait_alias(
1554 input: ParseStream,
1555 ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> {
1556 let attrs = input.call(Attribute::parse_outer)?;
1557 let vis: Visibility = input.parse()?;
1558 let trait_token: Token![trait] = input.parse()?;
1559 let ident: Ident = input.parse()?;
1560 let generics: Generics = input.parse()?;
1561 Ok((attrs, vis, trait_token, ident, generics))
1562 }
1563
1564 fn parse_rest_of_trait_alias(
1565 input: ParseStream,
1566 attrs: Vec<Attribute>,
1567 vis: Visibility,
1568 trait_token: Token![trait],
1569 ident: Ident,
1570 mut generics: Generics,
1571 ) -> Result<ItemTraitAlias> {
1572 let eq_token: Token![=] = input.parse()?;
1573
1574 let mut bounds = Punctuated::new();
1575 loop {
1576 if input.peek(Token![where]) || input.peek(Token![;]) {
1577 break;
1578 }
1579 bounds.push_value(input.parse()?);
1580 if input.peek(Token![where]) || input.peek(Token![;]) {
1581 break;
1582 }
1583 bounds.push_punct(input.parse()?);
1584 }
1585
1586 generics.where_clause = input.parse()?;
1587 let semi_token: Token![;] = input.parse()?;
1588
1589 Ok(ItemTraitAlias {
1590 attrs: attrs,
1591 vis: vis,
1592 trait_token: trait_token,
1593 ident: ident,
1594 generics: generics,
1595 eq_token: eq_token,
1596 bounds: bounds,
1597 semi_token: semi_token,
1598 })
1599 }
1600
David Tolnay6a170ce2018-08-26 22:29:24 -07001601 impl Parse for TraitItem {
1602 fn parse(input: ParseStream) -> Result<Self> {
1603 let ahead = input.fork();
1604 ahead.call(Attribute::parse_outer)?;
1605
1606 let lookahead = ahead.lookahead1();
1607 if lookahead.peek(Token![const]) {
1608 ahead.parse::<Token![const]>()?;
1609 let lookahead = ahead.lookahead1();
1610 if lookahead.peek(Ident) {
1611 input.parse().map(TraitItem::Const)
1612 } else if lookahead.peek(Token![unsafe])
1613 || lookahead.peek(Token![extern])
1614 || lookahead.peek(Token![fn])
1615 {
1616 input.parse().map(TraitItem::Method)
1617 } else {
1618 Err(lookahead.error())
1619 }
1620 } else if lookahead.peek(Token![unsafe])
1621 || lookahead.peek(Token![extern])
1622 || lookahead.peek(Token![fn])
1623 {
1624 input.parse().map(TraitItem::Method)
1625 } else if lookahead.peek(Token![type]) {
1626 input.parse().map(TraitItem::Type)
1627 } else if lookahead.peek(Ident)
1628 || lookahead.peek(Token![self])
1629 || lookahead.peek(Token![super])
1630 || lookahead.peek(Token![extern])
1631 || lookahead.peek(Token![crate])
1632 || lookahead.peek(Token![::])
1633 {
1634 input.parse().map(TraitItem::Macro)
1635 } else {
1636 Err(lookahead.error())
1637 }
1638 }
1639 }
1640
1641 impl Parse for TraitItemConst {
1642 fn parse(input: ParseStream) -> Result<Self> {
1643 Ok(TraitItemConst {
1644 attrs: input.call(Attribute::parse_outer)?,
1645 const_token: input.parse()?,
1646 ident: input.parse()?,
1647 colon_token: input.parse()?,
1648 ty: input.parse()?,
1649 default: {
1650 if input.peek(Token![=]) {
1651 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001652 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001653 Some((eq_token, default))
1654 } else {
1655 None
1656 }
1657 },
1658 semi_token: input.parse()?,
1659 })
1660 }
1661 }
1662
1663 impl Parse for TraitItemMethod {
1664 fn parse(input: ParseStream) -> Result<Self> {
1665 let outer_attrs = input.call(Attribute::parse_outer)?;
1666 let constness: Option<Token![const]> = input.parse()?;
1667 let unsafety: Option<Token![unsafe]> = input.parse()?;
1668 let abi: Option<Abi> = input.parse()?;
1669 let fn_token: Token![fn] = input.parse()?;
1670 let ident: Ident = input.parse()?;
1671 let generics: Generics = input.parse()?;
1672
1673 let content;
1674 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001675 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001676
1677 let output: ReturnType = input.parse()?;
1678 let where_clause: Option<WhereClause> = input.parse()?;
1679
1680 let lookahead = input.lookahead1();
1681 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1682 let content;
1683 let brace_token = braced!(content in input);
1684 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001685 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001686 (Some(brace_token), inner_attrs, stmts, None)
1687 } else if lookahead.peek(Token![;]) {
1688 let semi_token: Token![;] = input.parse()?;
1689 (None, Vec::new(), Vec::new(), Some(semi_token))
1690 } else {
1691 return Err(lookahead.error());
1692 };
1693
1694 Ok(TraitItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001695 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnayda705bd2017-11-10 21:58:05 -08001696 sig: MethodSig {
1697 constness: constness,
1698 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001699 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001700 abi: abi,
1701 ident: ident,
1702 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001703 fn_token: fn_token,
1704 paren_token: paren_token,
1705 inputs: inputs,
1706 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001707 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001708 generics: Generics {
1709 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001710 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001711 },
1712 },
David Tolnayda705bd2017-11-10 21:58:05 -08001713 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001714 default: brace_token.map(|brace_token| Block {
1715 brace_token: brace_token,
1716 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001717 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001718 semi_token: semi_token,
1719 })
1720 }
1721 }
1722
1723 impl Parse for TraitItemType {
1724 fn parse(input: ParseStream) -> Result<Self> {
1725 let attrs = input.call(Attribute::parse_outer)?;
1726 let type_token: Token![type] = input.parse()?;
1727 let ident: Ident = input.parse()?;
1728 let mut generics: Generics = input.parse()?;
1729 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001730
1731 let mut bounds = Punctuated::new();
1732 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001733 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1734 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001735 if !bounds.is_empty() {
1736 bounds.push_punct(input.parse()?);
1737 }
1738 bounds.push_value(input.parse()?);
1739 }
1740 }
1741
David Tolnay6a170ce2018-08-26 22:29:24 -07001742 generics.where_clause = input.parse()?;
1743 let default = if input.peek(Token![=]) {
1744 let eq_token: Token![=] = input.parse()?;
1745 let default: Type = input.parse()?;
1746 Some((eq_token, default))
1747 } else {
1748 None
1749 };
1750 let semi_token: Token![;] = input.parse()?;
1751
1752 Ok(TraitItemType {
1753 attrs: attrs,
1754 type_token: type_token,
1755 ident: ident,
1756 generics: generics,
1757 colon_token: colon_token,
1758 bounds: bounds,
1759 default: default,
1760 semi_token: semi_token,
1761 })
1762 }
1763 }
1764
1765 impl Parse for TraitItemMacro {
1766 fn parse(input: ParseStream) -> Result<Self> {
1767 let attrs = input.call(Attribute::parse_outer)?;
1768 let mac: Macro = input.parse()?;
1769 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1770 None
1771 } else {
1772 Some(input.parse()?)
1773 };
1774 Ok(TraitItemMacro {
1775 attrs: attrs,
1776 mac: mac,
1777 semi_token: semi_token,
1778 })
1779 }
1780 }
1781
1782 impl Parse for ItemImpl {
1783 fn parse(input: ParseStream) -> Result<Self> {
1784 let outer_attrs = input.call(Attribute::parse_outer)?;
1785 let defaultness: Option<Token![default]> = input.parse()?;
1786 let unsafety: Option<Token![unsafe]> = input.parse()?;
1787 let impl_token: Token![impl ] = input.parse()?;
David Tolnay3c29f6e2018-10-27 22:47:48 -07001788
1789 let has_generics = input.peek(Token![<])
1790 && (input.peek2(Token![>])
1791 || input.peek2(Token![#])
1792 || (input.peek2(Ident) || input.peek2(Lifetime))
David Tolnaye614f282018-10-27 22:50:12 -07001793 && (input.peek3(Token![:])
1794 || input.peek3(Token![,])
1795 || input.peek3(Token![>])));
David Tolnay3c29f6e2018-10-27 22:47:48 -07001796 let generics: Generics = if has_generics {
1797 input.parse()?
1798 } else {
1799 Generics::default()
1800 };
1801
David Tolnay6a170ce2018-08-26 22:29:24 -07001802 let trait_ = {
1803 let ahead = input.fork();
1804 if ahead.parse::<Option<Token![!]>>().is_ok()
1805 && ahead.parse::<Path>().is_ok()
1806 && ahead.parse::<Token![for]>().is_ok()
1807 {
1808 let polarity: Option<Token![!]> = input.parse()?;
1809 let path: Path = input.parse()?;
1810 let for_token: Token![for] = input.parse()?;
1811 Some((polarity, path, for_token))
1812 } else {
1813 None
1814 }
1815 };
1816 let self_ty: Type = input.parse()?;
1817 let where_clause: Option<WhereClause> = input.parse()?;
1818
1819 let content;
1820 let brace_token = braced!(content in input);
1821 let inner_attrs = content.call(Attribute::parse_inner)?;
1822
1823 let mut items = Vec::new();
1824 while !content.is_empty() {
1825 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001826 }
David Tolnay0aecb732016-10-03 23:03:50 -07001827
David Tolnay6a170ce2018-08-26 22:29:24 -07001828 Ok(ItemImpl {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001829 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07001830 defaultness: defaultness,
1831 unsafety: unsafety,
1832 impl_token: impl_token,
1833 generics: Generics {
1834 where_clause: where_clause,
1835 ..generics
1836 },
1837 trait_: trait_,
1838 self_ty: Box::new(self_ty),
1839 brace_token: brace_token,
1840 items: items,
1841 })
1842 }
1843 }
David Tolnay0aecb732016-10-03 23:03:50 -07001844
David Tolnay6a170ce2018-08-26 22:29:24 -07001845 impl Parse for ImplItem {
1846 fn parse(input: ParseStream) -> Result<Self> {
1847 let ahead = input.fork();
1848 ahead.call(Attribute::parse_outer)?;
1849 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001850
David Tolnay6a170ce2018-08-26 22:29:24 -07001851 let mut lookahead = ahead.lookahead1();
1852 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1853 let defaultness: Token![default] = ahead.parse()?;
1854 lookahead = ahead.lookahead1();
1855 Some(defaultness)
1856 } else {
1857 None
1858 };
David Tolnay4c9be372016-10-06 00:47:37 -07001859
David Tolnay6a170ce2018-08-26 22:29:24 -07001860 if lookahead.peek(Token![const]) {
1861 ahead.parse::<Token![const]>()?;
1862 let lookahead = ahead.lookahead1();
1863 if lookahead.peek(Ident) {
1864 input.parse().map(ImplItem::Const)
1865 } else if lookahead.peek(Token![unsafe])
1866 || lookahead.peek(Token![async])
1867 || lookahead.peek(Token![extern])
1868 || lookahead.peek(Token![fn])
1869 {
1870 input.parse().map(ImplItem::Method)
1871 } else {
1872 Err(lookahead.error())
1873 }
1874 } else if lookahead.peek(Token![unsafe])
1875 || lookahead.peek(Token![async])
1876 || lookahead.peek(Token![extern])
1877 || lookahead.peek(Token![fn])
1878 {
1879 input.parse().map(ImplItem::Method)
1880 } else if lookahead.peek(Token![type]) {
1881 input.parse().map(ImplItem::Type)
1882 } else if vis.is_inherited()
1883 && defaultness.is_none()
1884 && lookahead.peek(Token![existential])
1885 {
1886 input.parse().map(ImplItem::Existential)
1887 } else if vis.is_inherited()
1888 && defaultness.is_none()
1889 && (lookahead.peek(Ident)
1890 || lookahead.peek(Token![self])
1891 || lookahead.peek(Token![super])
1892 || lookahead.peek(Token![extern])
1893 || lookahead.peek(Token![crate])
1894 || lookahead.peek(Token![::]))
1895 {
1896 input.parse().map(ImplItem::Macro)
1897 } else {
1898 Err(lookahead.error())
1899 }
1900 }
1901 }
David Tolnay4c9be372016-10-06 00:47:37 -07001902
David Tolnay3779bb72018-08-26 18:46:07 -07001903 impl Parse for ImplItemConst {
1904 fn parse(input: ParseStream) -> Result<Self> {
1905 Ok(ImplItemConst {
1906 attrs: input.call(Attribute::parse_outer)?,
1907 vis: input.parse()?,
1908 defaultness: input.parse()?,
1909 const_token: input.parse()?,
1910 ident: input.parse()?,
1911 colon_token: input.parse()?,
1912 ty: input.parse()?,
1913 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001914 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001915 semi_token: input.parse()?,
1916 })
1917 }
1918 }
David Tolnay4c9be372016-10-06 00:47:37 -07001919
David Tolnay6a170ce2018-08-26 22:29:24 -07001920 impl Parse for ImplItemMethod {
1921 fn parse(input: ParseStream) -> Result<Self> {
1922 let outer_attrs = input.call(Attribute::parse_outer)?;
1923 let vis: Visibility = input.parse()?;
1924 let defaultness: Option<Token![default]> = input.parse()?;
1925 let constness: Option<Token![const]> = input.parse()?;
1926 let unsafety: Option<Token![unsafe]> = input.parse()?;
1927 let asyncness: Option<Token![async]> = input.parse()?;
1928 let abi: Option<Abi> = input.parse()?;
1929 let fn_token: Token![fn] = input.parse()?;
1930 let ident: Ident = input.parse()?;
1931 let generics: Generics = input.parse()?;
1932
1933 let content;
1934 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001935 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001936
1937 let output: ReturnType = input.parse()?;
1938 let where_clause: Option<WhereClause> = input.parse()?;
1939
1940 let content;
1941 let brace_token = braced!(content in input);
1942 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001943 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001944
1945 Ok(ImplItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001946 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07001947 vis: vis,
1948 defaultness: defaultness,
1949 sig: MethodSig {
1950 constness: constness,
1951 unsafety: unsafety,
1952 asyncness: asyncness,
1953 abi: abi,
1954 ident: ident,
1955 decl: FnDecl {
1956 fn_token: fn_token,
1957 paren_token: paren_token,
1958 inputs: inputs,
1959 output: output,
1960 variadic: None,
1961 generics: Generics {
1962 where_clause: where_clause,
1963 ..generics
1964 },
1965 },
1966 },
1967 block: Block {
1968 brace_token: brace_token,
1969 stmts: stmts,
1970 },
1971 })
1972 }
1973 }
David Tolnay4c9be372016-10-06 00:47:37 -07001974
David Tolnay3779bb72018-08-26 18:46:07 -07001975 impl Parse for ImplItemType {
1976 fn parse(input: ParseStream) -> Result<Self> {
1977 Ok(ImplItemType {
1978 attrs: input.call(Attribute::parse_outer)?,
1979 vis: input.parse()?,
1980 defaultness: input.parse()?,
1981 type_token: input.parse()?,
1982 ident: input.parse()?,
1983 generics: {
1984 let mut generics: Generics = input.parse()?;
1985 generics.where_clause = input.parse()?;
1986 generics
1987 },
1988 eq_token: input.parse()?,
1989 ty: input.parse()?,
1990 semi_token: input.parse()?,
1991 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001992 }
David Tolnay3779bb72018-08-26 18:46:07 -07001993 }
David Tolnay758ee132018-08-21 21:29:40 -04001994
David Tolnay3779bb72018-08-26 18:46:07 -07001995 impl Parse for ImplItemExistential {
1996 fn parse(input: ParseStream) -> Result<Self> {
1997 let ety: ItemExistential = input.parse()?;
1998 Ok(ImplItemExistential {
1999 attrs: ety.attrs,
2000 existential_token: ety.existential_token,
2001 type_token: ety.type_token,
2002 ident: ety.ident,
2003 generics: ety.generics,
2004 colon_token: ety.colon_token,
2005 bounds: ety.bounds,
2006 semi_token: ety.semi_token,
2007 })
2008 }
2009 }
2010
2011 impl Parse for ImplItemMacro {
2012 fn parse(input: ParseStream) -> Result<Self> {
2013 let attrs = input.call(Attribute::parse_outer)?;
2014 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07002015 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07002016 None
2017 } else {
2018 Some(input.parse()?)
2019 };
2020 Ok(ImplItemMacro {
2021 attrs: attrs,
2022 mac: mac,
2023 semi_token: semi_token,
2024 })
2025 }
2026 }
David Tolnay4c9be372016-10-06 00:47:37 -07002027
David Tolnay6a170ce2018-08-26 22:29:24 -07002028 impl Visibility {
2029 fn is_inherited(&self) -> bool {
2030 match *self {
2031 Visibility::Inherited => true,
2032 _ => false,
2033 }
2034 }
2035 }
2036
2037 impl MacroDelimiter {
2038 fn is_brace(&self) -> bool {
2039 match *self {
2040 MacroDelimiter::Brace(_) => true,
2041 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
2042 }
David Tolnay57292da2017-12-27 21:03:33 -05002043 }
2044 }
David Tolnayedf2b992016-09-23 20:43:45 -07002045}
David Tolnay4a51dc72016-10-01 00:40:31 -07002046
2047#[cfg(feature = "printing")]
2048mod printing {
2049 use super::*;
David Tolnay64023912018-08-31 09:51:12 -07002050
Alex Crichtona74a1c82018-05-16 10:20:44 -07002051 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07002052 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07002053
David Tolnay64023912018-08-31 09:51:12 -07002054 use attr::FilterAttrs;
2055 use print::TokensOrDefault;
2056
David Tolnay1bfa7332017-11-11 12:41:20 -08002057 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002058 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002059 tokens.append_all(self.attrs.outer());
2060 self.vis.to_tokens(tokens);
2061 self.extern_token.to_tokens(tokens);
2062 self.crate_token.to_tokens(tokens);
2063 self.ident.to_tokens(tokens);
2064 if let Some((ref as_token, ref rename)) = self.rename {
2065 as_token.to_tokens(tokens);
2066 rename.to_tokens(tokens);
2067 }
2068 self.semi_token.to_tokens(tokens);
2069 }
2070 }
2071
2072 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002073 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002074 tokens.append_all(self.attrs.outer());
2075 self.vis.to_tokens(tokens);
2076 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002077 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002078 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002079 self.semi_token.to_tokens(tokens);
2080 }
2081 }
2082
2083 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002084 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002085 tokens.append_all(self.attrs.outer());
2086 self.vis.to_tokens(tokens);
2087 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002088 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002089 self.ident.to_tokens(tokens);
2090 self.colon_token.to_tokens(tokens);
2091 self.ty.to_tokens(tokens);
2092 self.eq_token.to_tokens(tokens);
2093 self.expr.to_tokens(tokens);
2094 self.semi_token.to_tokens(tokens);
2095 }
2096 }
2097
2098 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002099 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002100 tokens.append_all(self.attrs.outer());
2101 self.vis.to_tokens(tokens);
2102 self.const_token.to_tokens(tokens);
2103 self.ident.to_tokens(tokens);
2104 self.colon_token.to_tokens(tokens);
2105 self.ty.to_tokens(tokens);
2106 self.eq_token.to_tokens(tokens);
2107 self.expr.to_tokens(tokens);
2108 self.semi_token.to_tokens(tokens);
2109 }
2110 }
2111
2112 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002113 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002114 tokens.append_all(self.attrs.outer());
2115 self.vis.to_tokens(tokens);
2116 self.constness.to_tokens(tokens);
2117 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002118 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002119 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002120 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002121 self.block.brace_token.surround(tokens, |tokens| {
2122 tokens.append_all(self.attrs.inner());
2123 tokens.append_all(&self.block.stmts);
2124 });
2125 }
2126 }
2127
2128 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002129 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002130 tokens.append_all(self.attrs.outer());
2131 self.vis.to_tokens(tokens);
2132 self.mod_token.to_tokens(tokens);
2133 self.ident.to_tokens(tokens);
2134 if let Some((ref brace, ref items)) = self.content {
2135 brace.surround(tokens, |tokens| {
2136 tokens.append_all(self.attrs.inner());
2137 tokens.append_all(items);
2138 });
2139 } else {
2140 TokensOrDefault(&self.semi).to_tokens(tokens);
2141 }
2142 }
2143 }
2144
2145 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002146 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002147 tokens.append_all(self.attrs.outer());
2148 self.abi.to_tokens(tokens);
2149 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002150 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002151 tokens.append_all(&self.items);
2152 });
2153 }
2154 }
2155
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002156 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002157 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002158 tokens.append_all(self.attrs.outer());
2159 self.vis.to_tokens(tokens);
2160 self.type_token.to_tokens(tokens);
2161 self.ident.to_tokens(tokens);
2162 self.generics.to_tokens(tokens);
2163 self.generics.where_clause.to_tokens(tokens);
2164 self.eq_token.to_tokens(tokens);
2165 self.ty.to_tokens(tokens);
2166 self.semi_token.to_tokens(tokens);
2167 }
2168 }
2169
David Tolnaybb82ef02018-08-24 20:15:45 -04002170 impl ToTokens for ItemExistential {
2171 fn to_tokens(&self, tokens: &mut TokenStream) {
2172 tokens.append_all(self.attrs.outer());
2173 self.vis.to_tokens(tokens);
2174 self.existential_token.to_tokens(tokens);
2175 self.type_token.to_tokens(tokens);
2176 self.ident.to_tokens(tokens);
2177 self.generics.to_tokens(tokens);
2178 self.generics.where_clause.to_tokens(tokens);
2179 if !self.bounds.is_empty() {
2180 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2181 self.bounds.to_tokens(tokens);
2182 }
2183 self.semi_token.to_tokens(tokens);
2184 }
2185 }
2186
David Tolnay1bfa7332017-11-11 12:41:20 -08002187 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002188 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002189 tokens.append_all(self.attrs.outer());
2190 self.vis.to_tokens(tokens);
2191 self.enum_token.to_tokens(tokens);
2192 self.ident.to_tokens(tokens);
2193 self.generics.to_tokens(tokens);
2194 self.generics.where_clause.to_tokens(tokens);
2195 self.brace_token.surround(tokens, |tokens| {
2196 self.variants.to_tokens(tokens);
2197 });
2198 }
2199 }
2200
2201 impl ToTokens for ItemStruct {
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.vis.to_tokens(tokens);
2205 self.struct_token.to_tokens(tokens);
2206 self.ident.to_tokens(tokens);
2207 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002208 match self.fields {
2209 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002210 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002211 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002212 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002213 Fields::Unnamed(ref fields) => {
2214 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002215 self.generics.where_clause.to_tokens(tokens);
2216 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002217 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002218 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002219 self.generics.where_clause.to_tokens(tokens);
2220 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002221 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002222 }
2223 }
2224 }
2225
2226 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002227 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002228 tokens.append_all(self.attrs.outer());
2229 self.vis.to_tokens(tokens);
2230 self.union_token.to_tokens(tokens);
2231 self.ident.to_tokens(tokens);
2232 self.generics.to_tokens(tokens);
2233 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002234 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002235 }
2236 }
2237
2238 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002239 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002240 tokens.append_all(self.attrs.outer());
2241 self.vis.to_tokens(tokens);
2242 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002243 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002244 self.trait_token.to_tokens(tokens);
2245 self.ident.to_tokens(tokens);
2246 self.generics.to_tokens(tokens);
2247 if !self.supertraits.is_empty() {
2248 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2249 self.supertraits.to_tokens(tokens);
2250 }
2251 self.generics.where_clause.to_tokens(tokens);
2252 self.brace_token.surround(tokens, |tokens| {
2253 tokens.append_all(&self.items);
2254 });
2255 }
2256 }
2257
David Tolnayc6b04dd2018-08-30 23:22:51 -07002258 impl ToTokens for ItemTraitAlias {
2259 fn to_tokens(&self, tokens: &mut TokenStream) {
2260 tokens.append_all(self.attrs.outer());
2261 self.vis.to_tokens(tokens);
2262 self.trait_token.to_tokens(tokens);
2263 self.ident.to_tokens(tokens);
2264 self.generics.to_tokens(tokens);
2265 self.eq_token.to_tokens(tokens);
2266 self.bounds.to_tokens(tokens);
2267 self.generics.where_clause.to_tokens(tokens);
2268 self.semi_token.to_tokens(tokens);
2269 }
2270 }
2271
David Tolnay1bfa7332017-11-11 12:41:20 -08002272 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002273 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002274 tokens.append_all(self.attrs.outer());
2275 self.defaultness.to_tokens(tokens);
2276 self.unsafety.to_tokens(tokens);
2277 self.impl_token.to_tokens(tokens);
2278 self.generics.to_tokens(tokens);
2279 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2280 polarity.to_tokens(tokens);
2281 path.to_tokens(tokens);
2282 for_token.to_tokens(tokens);
2283 }
2284 self.self_ty.to_tokens(tokens);
2285 self.generics.where_clause.to_tokens(tokens);
2286 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002287 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002288 tokens.append_all(&self.items);
2289 });
2290 }
2291 }
2292
2293 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002294 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002295 tokens.append_all(self.attrs.outer());
2296 self.mac.path.to_tokens(tokens);
2297 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002298 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002299 match self.mac.delimiter {
2300 MacroDelimiter::Paren(ref paren) => {
2301 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2302 }
2303 MacroDelimiter::Brace(ref brace) => {
2304 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2305 }
2306 MacroDelimiter::Bracket(ref bracket) => {
2307 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2308 }
2309 }
David Tolnay57292da2017-12-27 21:03:33 -05002310 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002311 }
2312 }
David Tolnay42602292016-10-01 22:25:45 -07002313
David Tolnay500d8322017-12-18 00:32:51 -08002314 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002315 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002316 tokens.append_all(self.attrs.outer());
2317 self.vis.to_tokens(tokens);
2318 self.macro_token.to_tokens(tokens);
2319 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002320 self.paren_token.surround(tokens, |tokens| {
2321 self.args.to_tokens(tokens);
2322 });
2323 self.brace_token.surround(tokens, |tokens| {
2324 self.body.to_tokens(tokens);
2325 });
David Tolnay500d8322017-12-18 00:32:51 -08002326 }
2327 }
2328
David Tolnay2ae520a2017-12-29 11:19:50 -05002329 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002330 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002331 self.tts.to_tokens(tokens);
2332 }
2333 }
2334
David Tolnay5f332a92017-12-26 00:42:45 -05002335 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002336 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002337 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002338 self.colon2_token.to_tokens(tokens);
2339 self.tree.to_tokens(tokens);
2340 }
2341 }
2342
2343 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002344 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002345 self.ident.to_tokens(tokens);
2346 }
2347 }
2348
2349 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002350 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002351 self.ident.to_tokens(tokens);
2352 self.as_token.to_tokens(tokens);
2353 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002354 }
2355 }
2356
David Tolnay5f332a92017-12-26 00:42:45 -05002357 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002358 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002359 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002360 }
2361 }
2362
David Tolnayd97a7d22018-03-31 19:17:01 +02002363 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002364 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002365 self.brace_token.surround(tokens, |tokens| {
2366 self.items.to_tokens(tokens);
2367 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002368 }
2369 }
2370
David Tolnay1bfa7332017-11-11 12:41:20 -08002371 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002372 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002373 tokens.append_all(self.attrs.outer());
2374 self.const_token.to_tokens(tokens);
2375 self.ident.to_tokens(tokens);
2376 self.colon_token.to_tokens(tokens);
2377 self.ty.to_tokens(tokens);
2378 if let Some((ref eq_token, ref default)) = self.default {
2379 eq_token.to_tokens(tokens);
2380 default.to_tokens(tokens);
2381 }
2382 self.semi_token.to_tokens(tokens);
2383 }
2384 }
2385
2386 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002387 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002388 tokens.append_all(self.attrs.outer());
2389 self.sig.to_tokens(tokens);
2390 match self.default {
2391 Some(ref block) => {
2392 block.brace_token.surround(tokens, |tokens| {
2393 tokens.append_all(self.attrs.inner());
2394 tokens.append_all(&block.stmts);
2395 });
David Tolnayca085422016-10-04 00:12:38 -07002396 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002397 None => {
2398 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002399 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002400 }
2401 }
2402 }
2403
2404 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002405 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002406 tokens.append_all(self.attrs.outer());
2407 self.type_token.to_tokens(tokens);
2408 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002409 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002410 if !self.bounds.is_empty() {
2411 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2412 self.bounds.to_tokens(tokens);
2413 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002414 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002415 if let Some((ref eq_token, ref default)) = self.default {
2416 eq_token.to_tokens(tokens);
2417 default.to_tokens(tokens);
2418 }
2419 self.semi_token.to_tokens(tokens);
2420 }
2421 }
2422
2423 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002424 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002425 tokens.append_all(self.attrs.outer());
2426 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002427 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002428 }
2429 }
2430
David Tolnay2ae520a2017-12-29 11:19:50 -05002431 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002432 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002433 self.tts.to_tokens(tokens);
2434 }
2435 }
2436
David Tolnay857628c2017-11-11 12:25:31 -08002437 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002438 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002439 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002440 self.vis.to_tokens(tokens);
2441 self.defaultness.to_tokens(tokens);
2442 self.const_token.to_tokens(tokens);
2443 self.ident.to_tokens(tokens);
2444 self.colon_token.to_tokens(tokens);
2445 self.ty.to_tokens(tokens);
2446 self.eq_token.to_tokens(tokens);
2447 self.expr.to_tokens(tokens);
2448 self.semi_token.to_tokens(tokens);
2449 }
2450 }
2451
2452 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002453 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002454 tokens.append_all(self.attrs.outer());
2455 self.vis.to_tokens(tokens);
2456 self.defaultness.to_tokens(tokens);
2457 self.sig.to_tokens(tokens);
2458 self.block.brace_token.surround(tokens, |tokens| {
2459 tokens.append_all(self.attrs.inner());
2460 tokens.append_all(&self.block.stmts);
2461 });
2462 }
2463 }
2464
2465 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002466 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002467 tokens.append_all(self.attrs.outer());
2468 self.vis.to_tokens(tokens);
2469 self.defaultness.to_tokens(tokens);
2470 self.type_token.to_tokens(tokens);
2471 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002472 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002473 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002474 self.eq_token.to_tokens(tokens);
2475 self.ty.to_tokens(tokens);
2476 self.semi_token.to_tokens(tokens);
2477 }
2478 }
2479
David Tolnaybb82ef02018-08-24 20:15:45 -04002480 impl ToTokens for ImplItemExistential {
2481 fn to_tokens(&self, tokens: &mut TokenStream) {
2482 tokens.append_all(self.attrs.outer());
2483 self.existential_token.to_tokens(tokens);
2484 self.type_token.to_tokens(tokens);
2485 self.ident.to_tokens(tokens);
2486 self.generics.to_tokens(tokens);
2487 self.generics.where_clause.to_tokens(tokens);
2488 if !self.bounds.is_empty() {
2489 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2490 self.bounds.to_tokens(tokens);
2491 }
2492 self.semi_token.to_tokens(tokens);
2493 }
2494 }
2495
David Tolnay857628c2017-11-11 12:25:31 -08002496 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002497 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002498 tokens.append_all(self.attrs.outer());
2499 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002500 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002501 }
2502 }
2503
David Tolnay2ae520a2017-12-29 11:19:50 -05002504 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002505 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002506 self.tts.to_tokens(tokens);
2507 }
2508 }
2509
David Tolnay8894f602017-11-11 12:11:04 -08002510 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002511 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002512 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002513 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002514 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002515 self.semi_token.to_tokens(tokens);
2516 }
2517 }
2518
2519 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002520 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002521 tokens.append_all(self.attrs.outer());
2522 self.vis.to_tokens(tokens);
2523 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002524 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002525 self.ident.to_tokens(tokens);
2526 self.colon_token.to_tokens(tokens);
2527 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002528 self.semi_token.to_tokens(tokens);
2529 }
2530 }
2531
David Tolnay199bcbb2017-11-12 10:33:52 -08002532 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002533 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002534 tokens.append_all(self.attrs.outer());
2535 self.vis.to_tokens(tokens);
2536 self.type_token.to_tokens(tokens);
2537 self.ident.to_tokens(tokens);
2538 self.semi_token.to_tokens(tokens);
2539 }
2540 }
2541
David Tolnay435c1782018-08-24 16:15:44 -04002542 impl ToTokens for ForeignItemMacro {
2543 fn to_tokens(&self, tokens: &mut TokenStream) {
2544 tokens.append_all(self.attrs.outer());
2545 self.mac.to_tokens(tokens);
2546 self.semi_token.to_tokens(tokens);
2547 }
2548 }
2549
David Tolnay2ae520a2017-12-29 11:19:50 -05002550 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002551 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002552 self.tts.to_tokens(tokens);
2553 }
2554 }
2555
David Tolnay570695e2017-06-03 16:15:13 -07002556 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002557 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002558 self.constness.to_tokens(tokens);
2559 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002560 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002561 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002562 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002563 }
2564 }
2565
Alex Crichtona74a1c82018-05-16 10:20:44 -07002566 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002567
2568 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002569 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002570 self.0.fn_token.to_tokens(tokens);
2571 self.1.to_tokens(tokens);
2572 self.0.generics.to_tokens(tokens);
2573 self.0.paren_token.surround(tokens, |tokens| {
2574 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002575 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2576 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002577 }
David Tolnayd2836e22017-12-27 23:13:00 -05002578 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002579 });
2580 self.0.output.to_tokens(tokens);
2581 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002582 }
2583 }
2584
Alex Crichton62a0a592017-05-22 13:58:53 -07002585 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002586 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002587 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002588 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002589 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002590 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002591 }
2592 }
2593
2594 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002595 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002596 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002597 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002598 }
2599 }
2600
2601 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002602 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002603 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002604 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002605 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002606 }
2607 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002608}