blob: 9b5b7eaac00eb575275e6f84c47273167b898018 [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 Tolnay87041a52018-12-15 00:48:03 -0800765 use proc_macro2::{Punct, Spacing, TokenTree};
766 use std::iter::FromIterator;
David Tolnay84aa0752016-10-02 23:01:13 -0700767
David Tolnay6a170ce2018-08-26 22:29:24 -0700768 impl Parse for Item {
769 fn parse(input: ParseStream) -> Result<Self> {
770 let ahead = input.fork();
771 ahead.call(Attribute::parse_outer)?;
772 let vis: Visibility = ahead.parse()?;
773
774 let lookahead = ahead.lookahead1();
775 if lookahead.peek(Token![extern]) {
776 ahead.parse::<Token![extern]>()?;
777 let lookahead = ahead.lookahead1();
778 if lookahead.peek(Token![crate]) {
779 input.parse().map(Item::ExternCrate)
780 } else if lookahead.peek(Token![fn]) {
781 input.parse().map(Item::Fn)
782 } else if lookahead.peek(token::Brace) {
783 input.parse().map(Item::ForeignMod)
784 } else if lookahead.peek(LitStr) {
785 ahead.parse::<LitStr>()?;
786 let lookahead = ahead.lookahead1();
787 if lookahead.peek(token::Brace) {
788 input.parse().map(Item::ForeignMod)
789 } else if lookahead.peek(Token![fn]) {
790 input.parse().map(Item::Fn)
791 } else {
792 Err(lookahead.error())
793 }
794 } else {
795 Err(lookahead.error())
796 }
797 } else if lookahead.peek(Token![use]) {
798 input.parse().map(Item::Use)
799 } else if lookahead.peek(Token![static]) {
800 input.parse().map(Item::Static)
801 } else if lookahead.peek(Token![const]) {
802 ahead.parse::<Token![const]>()?;
803 let lookahead = ahead.lookahead1();
David Tolnay5cd40fc2018-10-27 21:53:30 -0700804 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700805 input.parse().map(Item::Const)
806 } else if lookahead.peek(Token![unsafe])
807 || lookahead.peek(Token![async])
808 || lookahead.peek(Token![extern])
809 || lookahead.peek(Token![fn])
810 {
811 input.parse().map(Item::Fn)
812 } else {
813 Err(lookahead.error())
814 }
815 } else if lookahead.peek(Token![unsafe]) {
816 ahead.parse::<Token![unsafe]>()?;
817 let lookahead = ahead.lookahead1();
818 if lookahead.peek(Token![trait])
819 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
820 {
821 input.parse().map(Item::Trait)
David Tolnay2beee042019-04-03 08:36:59 -0700822 } else if lookahead.peek(Token![impl]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700823 input.parse().map(Item::Impl)
824 } else if lookahead.peek(Token![async])
825 || lookahead.peek(Token![extern])
826 || lookahead.peek(Token![fn])
827 {
828 input.parse().map(Item::Fn)
829 } else {
830 Err(lookahead.error())
831 }
832 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
833 input.parse().map(Item::Fn)
834 } else if lookahead.peek(Token![mod]) {
835 input.parse().map(Item::Mod)
836 } else if lookahead.peek(Token![type]) {
837 input.parse().map(Item::Type)
838 } else if lookahead.peek(Token![existential]) {
839 input.parse().map(Item::Existential)
840 } else if lookahead.peek(Token![struct]) {
841 input.parse().map(Item::Struct)
842 } else if lookahead.peek(Token![enum]) {
843 input.parse().map(Item::Enum)
844 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
845 input.parse().map(Item::Union)
David Tolnayc4de0d72018-11-06 21:11:00 -0800846 } else if lookahead.peek(Token![trait]) {
847 input.call(parse_trait_or_trait_alias)
848 } else if lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700849 input.parse().map(Item::Trait)
David Tolnay2beee042019-04-03 08:36:59 -0700850 } else if lookahead.peek(Token![impl])
David Tolnay6a170ce2018-08-26 22:29:24 -0700851 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
852 {
853 input.parse().map(Item::Impl)
854 } else if lookahead.peek(Token![macro]) {
855 input.parse().map(Item::Macro2)
856 } else if vis.is_inherited()
857 && (lookahead.peek(Ident)
858 || lookahead.peek(Token![self])
859 || lookahead.peek(Token![super])
860 || lookahead.peek(Token![extern])
861 || lookahead.peek(Token![crate])
862 || lookahead.peek(Token![::]))
863 {
864 input.parse().map(Item::Macro)
865 } else {
866 Err(lookahead.error())
867 }
868 }
869 }
Alex Crichton954046c2017-05-30 21:49:42 -0700870
David Tolnay3779bb72018-08-26 18:46:07 -0700871 impl Parse for ItemMacro {
872 fn parse(input: ParseStream) -> Result<Self> {
873 let attrs = input.call(Attribute::parse_outer)?;
874 let path = input.call(Path::parse_mod_style)?;
875 let bang_token: Token![!] = input.parse()?;
876 let ident: Option<Ident> = input.parse()?;
877 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700878 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700879 Some(input.parse()?)
880 } else {
881 None
882 };
883 Ok(ItemMacro {
884 attrs: attrs,
885 ident: ident,
886 mac: Macro {
887 path: path,
888 bang_token: bang_token,
889 delimiter: delimiter,
890 tts: tts,
891 },
892 semi_token: semi_token,
893 })
894 }
895 }
David Tolnayedf2b992016-09-23 20:43:45 -0700896
David Tolnay500d8322017-12-18 00:32:51 -0800897 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700898 impl Parse for ItemMacro2 {
899 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay87041a52018-12-15 00:48:03 -0800900 let attrs = input.call(Attribute::parse_outer)?;
901 let vis: Visibility = input.parse()?;
902 let macro_token: Token![macro] = input.parse()?;
903 let ident: Ident = input.parse()?;
904
905 let paren_token;
David Tolnay3779bb72018-08-26 18:46:07 -0700906 let args;
David Tolnay87041a52018-12-15 00:48:03 -0800907 let brace_token;
David Tolnay3779bb72018-08-26 18:46:07 -0700908 let body;
David Tolnay87041a52018-12-15 00:48:03 -0800909 let lookahead = input.lookahead1();
910 if lookahead.peek(token::Paren) {
911 let paren_content;
912 paren_token = parenthesized!(paren_content in input);
913 args = paren_content.parse()?;
914
915 let brace_content;
916 brace_token = braced!(brace_content in input);
917 body = brace_content.parse()?;
918 } else if lookahead.peek(token::Brace) {
919 // Hack: the ItemMacro2 syntax tree will need to change so that
920 // we can store None for the args.
921 //
922 // https://github.com/dtolnay/syn/issues/548
923 //
924 // For now, store some sentinel tokens that are otherwise
925 // illegal.
926 paren_token = token::Paren::default();
927 args = TokenStream::from_iter(vec![
928 TokenTree::Punct(Punct::new('$', Spacing::Alone)),
929 TokenTree::Punct(Punct::new('$', Spacing::Alone)),
930 ]);
931
932 let brace_content;
933 brace_token = braced!(brace_content in input);
934 body = brace_content.parse()?;
935 } else {
936 return Err(lookahead.error());
937 }
938
David Tolnay3779bb72018-08-26 18:46:07 -0700939 Ok(ItemMacro2 {
David Tolnay87041a52018-12-15 00:48:03 -0800940 attrs: attrs,
941 vis: vis,
942 macro_token: macro_token,
943 ident: ident,
944 paren_token: paren_token,
945 args: args,
946 brace_token: brace_token,
947 body: body,
David Tolnay3779bb72018-08-26 18:46:07 -0700948 })
949 }
950 }
David Tolnay500d8322017-12-18 00:32:51 -0800951
David Tolnay3779bb72018-08-26 18:46:07 -0700952 impl Parse for ItemExternCrate {
953 fn parse(input: ParseStream) -> Result<Self> {
954 Ok(ItemExternCrate {
955 attrs: input.call(Attribute::parse_outer)?,
956 vis: input.parse()?,
957 extern_token: input.parse()?,
958 crate_token: input.parse()?,
David Tolnaycff361c2018-12-15 00:04:32 -0800959 ident: {
960 if input.peek(Token![self]) {
961 input.call(Ident::parse_any)?
962 } else {
963 input.parse()?
964 }
965 },
David Tolnay3779bb72018-08-26 18:46:07 -0700966 rename: {
967 if input.peek(Token![as]) {
968 let as_token: Token![as] = input.parse()?;
969 let rename: Ident = input.parse()?;
970 Some((as_token, rename))
971 } else {
972 None
973 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800974 },
David Tolnay3779bb72018-08-26 18:46:07 -0700975 semi_token: input.parse()?,
976 })
977 }
978 }
979
980 impl Parse for ItemUse {
981 fn parse(input: ParseStream) -> Result<Self> {
982 Ok(ItemUse {
983 attrs: input.call(Attribute::parse_outer)?,
984 vis: input.parse()?,
985 use_token: input.parse()?,
986 leading_colon: input.parse()?,
cad97dbdcb662019-04-13 17:50:45 -0400987 tree: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -0700988 semi_token: input.parse()?,
989 })
990 }
991 }
992
cad97dbdcb662019-04-13 17:50:45 -0400993 impl Parse for UseTree {
994 fn parse(input: ParseStream) -> Result<UseTree> {
995 let lookahead = input.lookahead1();
996 if lookahead.peek(Ident)
997 || lookahead.peek(Token![self])
998 || lookahead.peek(Token![super])
999 || lookahead.peek(Token![crate])
1000 || lookahead.peek(Token![extern])
1001 {
1002 let ident = input.call(Ident::parse_any)?;
1003 if input.peek(Token![::]) {
1004 Ok(UseTree::Path(UsePath {
1005 ident: ident,
1006 colon2_token: input.parse()?,
1007 tree: Box::new(input.parse()?),
1008 }))
1009 } else if input.peek(Token![as]) {
1010 Ok(UseTree::Rename(UseRename {
1011 ident: ident,
1012 as_token: input.parse()?,
1013 rename: {
1014 if input.peek(Ident) {
1015 input.parse()?
1016 } else if input.peek(Token![_]) {
1017 Ident::from(input.parse::<Token![_]>()?)
1018 } else {
1019 return Err(input.error("expected identifier or underscore"));
1020 }
1021 },
1022 }))
1023 } else {
1024 Ok(UseTree::Name(UseName { ident: ident }))
1025 }
1026 } else if lookahead.peek(Token![*]) {
1027 Ok(UseTree::Glob(UseGlob {
1028 star_token: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001029 }))
cad97dbdcb662019-04-13 17:50:45 -04001030 } else if lookahead.peek(token::Brace) {
1031 let content;
1032 Ok(UseTree::Group(UseGroup {
1033 brace_token: braced!(content in input),
1034 items: content.parse_terminated(UseTree::parse)?,
David Tolnay3779bb72018-08-26 18:46:07 -07001035 }))
1036 } else {
cad97dbdcb662019-04-13 17:50:45 -04001037 Err(lookahead.error())
David Tolnay3779bb72018-08-26 18:46:07 -07001038 }
David Tolnay3779bb72018-08-26 18:46:07 -07001039 }
1040 }
1041
1042 impl Parse for ItemStatic {
1043 fn parse(input: ParseStream) -> Result<Self> {
1044 Ok(ItemStatic {
1045 attrs: input.call(Attribute::parse_outer)?,
1046 vis: input.parse()?,
1047 static_token: input.parse()?,
1048 mutability: input.parse()?,
1049 ident: input.parse()?,
1050 colon_token: input.parse()?,
1051 ty: input.parse()?,
1052 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001053 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001054 semi_token: input.parse()?,
1055 })
1056 }
1057 }
1058
1059 impl Parse for ItemConst {
1060 fn parse(input: ParseStream) -> Result<Self> {
1061 Ok(ItemConst {
1062 attrs: input.call(Attribute::parse_outer)?,
1063 vis: input.parse()?,
1064 const_token: input.parse()?,
David Tolnay5cd40fc2018-10-27 21:53:30 -07001065 ident: {
1066 let lookahead = input.lookahead1();
1067 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
1068 input.call(Ident::parse_any)?
1069 } else {
1070 return Err(lookahead.error());
1071 }
1072 },
David Tolnay3779bb72018-08-26 18:46:07 -07001073 colon_token: input.parse()?,
1074 ty: input.parse()?,
1075 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001076 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001077 semi_token: input.parse()?,
1078 })
1079 }
1080 }
1081
1082 impl Parse for ItemFn {
1083 fn parse(input: ParseStream) -> Result<Self> {
1084 let outer_attrs = input.call(Attribute::parse_outer)?;
1085 let vis: Visibility = input.parse()?;
1086 let constness: Option<Token![const]> = input.parse()?;
1087 let unsafety: Option<Token![unsafe]> = input.parse()?;
1088 let asyncness: Option<Token![async]> = input.parse()?;
1089 let abi: Option<Abi> = input.parse()?;
1090 let fn_token: Token![fn] = input.parse()?;
1091 let ident: Ident = input.parse()?;
1092 let generics: Generics = input.parse()?;
1093
1094 let content;
1095 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001096 let inputs = content.parse_terminated(FnArg::parse)?;
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001097 let variadic: Option<Token![...]> = match inputs.last() {
1098 Some(punctuated::Pair::End(&FnArg::Captured(ArgCaptured {
David Tolnayfac872d2019-02-28 22:45:31 -08001099 ty: Type::Verbatim(TypeVerbatim { ref tts }),
1100 ..
1101 }))) => parse2(tts.clone()).ok(),
1102 _ => None,
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001103 };
David Tolnay3779bb72018-08-26 18:46:07 -07001104
1105 let output: ReturnType = input.parse()?;
1106 let where_clause: Option<WhereClause> = input.parse()?;
1107
1108 let content;
1109 let brace_token = braced!(content in input);
1110 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001111 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001112
1113 Ok(ItemFn {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001114 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001115 vis: vis,
1116 constness: constness,
1117 unsafety: unsafety,
1118 asyncness: asyncness,
1119 abi: abi,
1120 ident: ident,
1121 decl: Box::new(FnDecl {
1122 fn_token: fn_token,
1123 paren_token: paren_token,
1124 inputs: inputs,
1125 output: output,
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001126 variadic: variadic,
David Tolnay3779bb72018-08-26 18:46:07 -07001127 generics: Generics {
1128 where_clause: where_clause,
1129 ..generics
1130 },
1131 }),
1132 block: Box::new(Block {
1133 brace_token: brace_token,
1134 stmts: stmts,
1135 }),
1136 })
1137 }
1138 }
David Tolnay42602292016-10-01 22:25:45 -07001139
David Tolnay2ad62c12018-08-26 19:00:35 -04001140 impl Parse for FnArg {
1141 fn parse(input: ParseStream) -> Result<Self> {
1142 if input.peek(Token![&]) {
1143 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001144 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1145 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001146 }
1147 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001148
David Tolnay2ad62c12018-08-26 19:00:35 -04001149 if input.peek(Token![mut]) || input.peek(Token![self]) {
1150 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001151 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1152 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001153 }
1154 }
1155
1156 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001157 let err = match ahead.call(arg_captured) {
1158 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001159 Err(err) => err,
1160 };
1161
1162 let ahead = input.fork();
1163 if ahead.parse::<Type>().is_ok() {
1164 return input.parse().map(FnArg::Ignored);
1165 }
1166
1167 Err(err)
1168 }
1169 }
1170
David Tolnay3779bb72018-08-26 18:46:07 -07001171 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1172 Ok(ArgSelfRef {
1173 and_token: input.parse()?,
1174 lifetime: input.parse()?,
1175 mutability: input.parse()?,
1176 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001177 })
David Tolnay3779bb72018-08-26 18:46:07 -07001178 }
David Tolnay35902302016-10-06 01:11:08 -07001179
David Tolnay3779bb72018-08-26 18:46:07 -07001180 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1181 Ok(ArgSelf {
1182 mutability: input.parse()?,
1183 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001184 })
David Tolnay3779bb72018-08-26 18:46:07 -07001185 }
1186
1187 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1188 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001189 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001190 colon_token: input.parse()?,
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001191 ty: match input.parse::<Token![...]>() {
1192 Ok(dot3) => {
1193 let mut args = vec![
1194 TokenTree::Punct(Punct::new('.', Spacing::Joint)),
1195 TokenTree::Punct(Punct::new('.', Spacing::Joint)),
1196 TokenTree::Punct(Punct::new('.', Spacing::Alone)),
1197 ];
David Tolnayfac872d2019-02-28 22:45:31 -08001198 let tokens = TokenStream::from_iter(args.into_iter().zip(&dot3.spans).map(
1199 |(mut arg, span)| {
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001200 arg.set_span(*span);
1201 arg
David Tolnayfac872d2019-02-28 22:45:31 -08001202 },
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001203 ));
David Tolnayfac872d2019-02-28 22:45:31 -08001204 Type::Verbatim(TypeVerbatim { tts: tokens })
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001205 }
David Tolnayfac872d2019-02-28 22:45:31 -08001206 Err(_) => input.parse()?,
Dan Robertsonb7aa8072019-02-08 18:38:27 +00001207 },
David Tolnay3779bb72018-08-26 18:46:07 -07001208 })
1209 }
1210
1211 impl Parse for ItemMod {
1212 fn parse(input: ParseStream) -> Result<Self> {
1213 let outer_attrs = input.call(Attribute::parse_outer)?;
1214 let vis: Visibility = input.parse()?;
1215 let mod_token: Token![mod] = input.parse()?;
1216 let ident: Ident = input.parse()?;
1217
1218 let lookahead = input.lookahead1();
1219 if lookahead.peek(Token![;]) {
1220 Ok(ItemMod {
1221 attrs: outer_attrs,
1222 vis: vis,
1223 mod_token: mod_token,
1224 ident: ident,
1225 content: None,
1226 semi: Some(input.parse()?),
1227 })
1228 } else if lookahead.peek(token::Brace) {
1229 let content;
1230 let brace_token = braced!(content in input);
1231 let inner_attrs = content.call(Attribute::parse_inner)?;
1232
1233 let mut items = Vec::new();
1234 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001235 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001236 }
1237
1238 Ok(ItemMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001239 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001240 vis: vis,
1241 mod_token: mod_token,
1242 ident: ident,
1243 content: Some((brace_token, items)),
1244 semi: None,
1245 })
1246 } else {
1247 Err(lookahead.error())
1248 }
1249 }
1250 }
1251
1252 impl Parse for ItemForeignMod {
1253 fn parse(input: ParseStream) -> Result<Self> {
1254 let outer_attrs = input.call(Attribute::parse_outer)?;
1255 let abi: Abi = input.parse()?;
1256
1257 let content;
1258 let brace_token = braced!(content in input);
1259 let inner_attrs = content.call(Attribute::parse_inner)?;
1260 let mut items = Vec::new();
1261 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001262 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001263 }
1264
1265 Ok(ItemForeignMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001266 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001267 abi: abi,
1268 brace_token: brace_token,
1269 items: items,
1270 })
1271 }
1272 }
David Tolnay35902302016-10-06 01:11:08 -07001273
David Tolnay6a170ce2018-08-26 22:29:24 -07001274 impl Parse for ForeignItem {
1275 fn parse(input: ParseStream) -> Result<Self> {
1276 let ahead = input.fork();
1277 ahead.call(Attribute::parse_outer)?;
1278 let vis: Visibility = ahead.parse()?;
1279
1280 let lookahead = ahead.lookahead1();
1281 if lookahead.peek(Token![fn]) {
1282 input.parse().map(ForeignItem::Fn)
1283 } else if lookahead.peek(Token![static]) {
1284 input.parse().map(ForeignItem::Static)
1285 } else if lookahead.peek(Token![type]) {
1286 input.parse().map(ForeignItem::Type)
1287 } else if vis.is_inherited()
1288 && (lookahead.peek(Ident)
1289 || lookahead.peek(Token![self])
1290 || lookahead.peek(Token![super])
1291 || lookahead.peek(Token![extern])
1292 || lookahead.peek(Token![crate])
1293 || lookahead.peek(Token![::]))
1294 {
1295 input.parse().map(ForeignItem::Macro)
1296 } else {
1297 Err(lookahead.error())
1298 }
1299 }
1300 }
David Tolnay35902302016-10-06 01:11:08 -07001301
David Tolnay3779bb72018-08-26 18:46:07 -07001302 impl Parse for ForeignItemFn {
1303 fn parse(input: ParseStream) -> Result<Self> {
1304 let attrs = input.call(Attribute::parse_outer)?;
1305 let vis: Visibility = input.parse()?;
1306 let fn_token: Token![fn] = input.parse()?;
1307 let ident: Ident = input.parse()?;
1308 let generics: Generics = input.parse()?;
1309
1310 let content;
1311 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001312 let mut inputs = Punctuated::new();
1313 while !content.is_empty() && !content.peek(Token![...]) {
1314 inputs.push_value(content.parse()?);
1315 if content.is_empty() {
1316 break;
1317 }
1318 inputs.push_punct(content.parse()?);
1319 }
David Tolnay3779bb72018-08-26 18:46:07 -07001320 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1321 content.parse()?
1322 } else {
1323 None
1324 };
1325
1326 let output: ReturnType = input.parse()?;
1327 let where_clause: Option<WhereClause> = input.parse()?;
1328 let semi_token: Token![;] = input.parse()?;
1329
1330 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001331 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001332 vis: vis,
1333 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001334 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001335 fn_token: fn_token,
1336 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001337 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001338 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001339 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001340 generics: Generics {
1341 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001342 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001343 },
1344 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001345 semi_token: semi_token,
1346 })
1347 }
1348 }
David Tolnay35902302016-10-06 01:11:08 -07001349
David Tolnay3779bb72018-08-26 18:46:07 -07001350 impl Parse for ForeignItemStatic {
1351 fn parse(input: ParseStream) -> Result<Self> {
1352 Ok(ForeignItemStatic {
1353 attrs: input.call(Attribute::parse_outer)?,
1354 vis: input.parse()?,
1355 static_token: input.parse()?,
1356 mutability: input.parse()?,
1357 ident: input.parse()?,
1358 colon_token: input.parse()?,
1359 ty: input.parse()?,
1360 semi_token: input.parse()?,
1361 })
1362 }
1363 }
David Tolnay35902302016-10-06 01:11:08 -07001364
David Tolnay3779bb72018-08-26 18:46:07 -07001365 impl Parse for ForeignItemType {
1366 fn parse(input: ParseStream) -> Result<Self> {
1367 Ok(ForeignItemType {
1368 attrs: input.call(Attribute::parse_outer)?,
1369 vis: input.parse()?,
1370 type_token: input.parse()?,
1371 ident: input.parse()?,
1372 semi_token: input.parse()?,
1373 })
1374 }
1375 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001376
David Tolnay3779bb72018-08-26 18:46:07 -07001377 impl Parse for ForeignItemMacro {
1378 fn parse(input: ParseStream) -> Result<Self> {
1379 let attrs = input.call(Attribute::parse_outer)?;
1380 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001381 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001382 None
1383 } else {
1384 Some(input.parse()?)
1385 };
1386 Ok(ForeignItemMacro {
1387 attrs: attrs,
1388 mac: mac,
1389 semi_token: semi_token,
1390 })
1391 }
1392 }
David Tolnay78572112018-08-01 00:36:18 -07001393
David Tolnay3779bb72018-08-26 18:46:07 -07001394 impl Parse for ItemType {
1395 fn parse(input: ParseStream) -> Result<Self> {
1396 Ok(ItemType {
1397 attrs: input.call(Attribute::parse_outer)?,
1398 vis: input.parse()?,
1399 type_token: input.parse()?,
1400 ident: input.parse()?,
1401 generics: {
1402 let mut generics: Generics = input.parse()?;
1403 generics.where_clause = input.parse()?;
1404 generics
1405 },
1406 eq_token: input.parse()?,
1407 ty: input.parse()?,
1408 semi_token: input.parse()?,
1409 })
1410 }
1411 }
David Tolnay3cf52982016-10-01 17:11:37 -07001412
David Tolnay3779bb72018-08-26 18:46:07 -07001413 impl Parse for ItemExistential {
1414 fn parse(input: ParseStream) -> Result<Self> {
1415 Ok(ItemExistential {
1416 attrs: input.call(Attribute::parse_outer)?,
1417 vis: input.parse()?,
1418 existential_token: input.parse()?,
1419 type_token: input.parse()?,
1420 ident: input.parse()?,
1421 generics: {
1422 let mut generics: Generics = input.parse()?;
1423 generics.where_clause = input.parse()?;
1424 generics
1425 },
1426 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001427 bounds: {
1428 let mut bounds = Punctuated::new();
1429 while !input.peek(Token![;]) {
1430 if !bounds.is_empty() {
1431 bounds.push_punct(input.parse()?);
1432 }
1433 bounds.push_value(input.parse()?);
1434 }
1435 bounds
1436 },
David Tolnay3779bb72018-08-26 18:46:07 -07001437 semi_token: input.parse()?,
1438 })
1439 }
1440 }
David Tolnay758ee132018-08-21 21:29:40 -04001441
David Tolnay6a170ce2018-08-26 22:29:24 -07001442 impl Parse for ItemStruct {
1443 fn parse(input: ParseStream) -> Result<Self> {
1444 let attrs = input.call(Attribute::parse_outer)?;
1445 let vis = input.parse::<Visibility>()?;
1446 let struct_token = input.parse::<Token![struct]>()?;
1447 let ident = input.parse::<Ident>()?;
1448 let generics = input.parse::<Generics>()?;
1449 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1450 Ok(ItemStruct {
1451 attrs: attrs,
1452 vis: vis,
1453 struct_token: struct_token,
1454 ident: ident,
1455 generics: Generics {
1456 where_clause: where_clause,
1457 ..generics
1458 },
1459 fields: fields,
1460 semi_token: semi_token,
1461 })
1462 }
1463 }
David Tolnay42602292016-10-01 22:25:45 -07001464
David Tolnay6a170ce2018-08-26 22:29:24 -07001465 impl Parse for ItemEnum {
1466 fn parse(input: ParseStream) -> Result<Self> {
1467 let attrs = input.call(Attribute::parse_outer)?;
1468 let vis = input.parse::<Visibility>()?;
1469 let enum_token = input.parse::<Token![enum]>()?;
1470 let ident = input.parse::<Ident>()?;
1471 let generics = input.parse::<Generics>()?;
1472 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1473 Ok(ItemEnum {
1474 attrs: attrs,
1475 vis: vis,
1476 enum_token: enum_token,
1477 ident: ident,
1478 generics: Generics {
1479 where_clause: where_clause,
1480 ..generics
1481 },
1482 brace_token: brace_token,
1483 variants: variants,
1484 })
1485 }
1486 }
David Tolnay4c614be2017-11-10 00:02:38 -08001487
David Tolnay6a170ce2018-08-26 22:29:24 -07001488 impl Parse for ItemUnion {
1489 fn parse(input: ParseStream) -> Result<Self> {
1490 let attrs = input.call(Attribute::parse_outer)?;
1491 let vis = input.parse::<Visibility>()?;
1492 let union_token = input.parse::<Token![union]>()?;
1493 let ident = input.parse::<Ident>()?;
1494 let generics = input.parse::<Generics>()?;
1495 let (where_clause, fields) = derive::parsing::data_union(input)?;
1496 Ok(ItemUnion {
1497 attrs: attrs,
1498 vis: vis,
1499 union_token: union_token,
1500 ident: ident,
1501 generics: Generics {
1502 where_clause: where_clause,
1503 ..generics
1504 },
1505 fields: fields,
1506 })
1507 }
1508 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001509
David Tolnayc4de0d72018-11-06 21:11:00 -08001510 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
1511 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1512 let lookahead = input.lookahead1();
1513 if lookahead.peek(token::Brace)
1514 || lookahead.peek(Token![:])
1515 || lookahead.peek(Token![where])
1516 {
1517 let unsafety = None;
1518 let auto_token = None;
1519 parse_rest_of_trait(
1520 input,
1521 attrs,
1522 vis,
1523 unsafety,
1524 auto_token,
1525 trait_token,
1526 ident,
1527 generics,
1528 )
1529 .map(Item::Trait)
1530 } else if lookahead.peek(Token![=]) {
1531 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1532 .map(Item::TraitAlias)
1533 } else {
1534 Err(lookahead.error())
1535 }
1536 }
1537
David Tolnay6a170ce2018-08-26 22:29:24 -07001538 impl Parse for ItemTrait {
1539 fn parse(input: ParseStream) -> Result<Self> {
1540 let attrs = input.call(Attribute::parse_outer)?;
1541 let vis: Visibility = input.parse()?;
1542 let unsafety: Option<Token![unsafe]> = input.parse()?;
1543 let auto_token: Option<Token![auto]> = input.parse()?;
1544 let trait_token: Token![trait] = input.parse()?;
1545 let ident: Ident = input.parse()?;
David Tolnayc4de0d72018-11-06 21:11:00 -08001546 let generics: Generics = input.parse()?;
1547 parse_rest_of_trait(
1548 input,
1549 attrs,
1550 vis,
1551 unsafety,
1552 auto_token,
1553 trait_token,
1554 ident,
1555 generics,
1556 )
1557 }
1558 }
David Tolnayf5ebc192018-08-30 18:23:46 -07001559
David Tolnayc4de0d72018-11-06 21:11:00 -08001560 fn parse_rest_of_trait(
1561 input: ParseStream,
1562 attrs: Vec<Attribute>,
1563 vis: Visibility,
1564 unsafety: Option<Token![unsafe]>,
1565 auto_token: Option<Token![auto]>,
1566 trait_token: Token![trait],
1567 ident: Ident,
1568 mut generics: Generics,
1569 ) -> Result<ItemTrait> {
1570 let colon_token: Option<Token![:]> = input.parse()?;
1571
1572 let mut supertraits = Punctuated::new();
1573 if colon_token.is_some() {
1574 loop {
1575 supertraits.push_value(input.parse()?);
1576 if input.peek(Token![where]) || input.peek(token::Brace) {
1577 break;
1578 }
1579 supertraits.push_punct(input.parse()?);
1580 if input.peek(Token![where]) || input.peek(token::Brace) {
1581 break;
David Tolnayf5ebc192018-08-30 18:23:46 -07001582 }
1583 }
David Tolnay6a170ce2018-08-26 22:29:24 -07001584 }
David Tolnayc4de0d72018-11-06 21:11:00 -08001585
1586 generics.where_clause = input.parse()?;
1587
1588 let content;
1589 let brace_token = braced!(content in input);
1590 let mut items = Vec::new();
1591 while !content.is_empty() {
1592 items.push(content.parse()?);
1593 }
1594
1595 Ok(ItemTrait {
1596 attrs: attrs,
1597 vis: vis,
1598 unsafety: unsafety,
1599 auto_token: auto_token,
1600 trait_token: trait_token,
1601 ident: ident,
1602 generics: generics,
1603 colon_token: colon_token,
1604 supertraits: supertraits,
1605 brace_token: brace_token,
1606 items: items,
1607 })
David Tolnay6a170ce2018-08-26 22:29:24 -07001608 }
1609
David Tolnayc6b04dd2018-08-30 23:22:51 -07001610 impl Parse for ItemTraitAlias {
1611 fn parse(input: ParseStream) -> Result<Self> {
David Tolnayc4de0d72018-11-06 21:11:00 -08001612 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1613 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
David Tolnayc6b04dd2018-08-30 23:22:51 -07001614 }
1615 }
1616
David Tolnayc4de0d72018-11-06 21:11:00 -08001617 fn parse_start_of_trait_alias(
1618 input: ParseStream,
1619 ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> {
1620 let attrs = input.call(Attribute::parse_outer)?;
1621 let vis: Visibility = input.parse()?;
1622 let trait_token: Token![trait] = input.parse()?;
1623 let ident: Ident = input.parse()?;
1624 let generics: Generics = input.parse()?;
1625 Ok((attrs, vis, trait_token, ident, generics))
1626 }
1627
1628 fn parse_rest_of_trait_alias(
1629 input: ParseStream,
1630 attrs: Vec<Attribute>,
1631 vis: Visibility,
1632 trait_token: Token![trait],
1633 ident: Ident,
1634 mut generics: Generics,
1635 ) -> Result<ItemTraitAlias> {
1636 let eq_token: Token![=] = input.parse()?;
1637
1638 let mut bounds = Punctuated::new();
1639 loop {
1640 if input.peek(Token![where]) || input.peek(Token![;]) {
1641 break;
1642 }
1643 bounds.push_value(input.parse()?);
1644 if input.peek(Token![where]) || input.peek(Token![;]) {
1645 break;
1646 }
1647 bounds.push_punct(input.parse()?);
1648 }
1649
1650 generics.where_clause = input.parse()?;
1651 let semi_token: Token![;] = input.parse()?;
1652
1653 Ok(ItemTraitAlias {
1654 attrs: attrs,
1655 vis: vis,
1656 trait_token: trait_token,
1657 ident: ident,
1658 generics: generics,
1659 eq_token: eq_token,
1660 bounds: bounds,
1661 semi_token: semi_token,
1662 })
1663 }
1664
David Tolnay6a170ce2018-08-26 22:29:24 -07001665 impl Parse for TraitItem {
1666 fn parse(input: ParseStream) -> Result<Self> {
1667 let ahead = input.fork();
1668 ahead.call(Attribute::parse_outer)?;
1669
1670 let lookahead = ahead.lookahead1();
1671 if lookahead.peek(Token![const]) {
1672 ahead.parse::<Token![const]>()?;
1673 let lookahead = ahead.lookahead1();
1674 if lookahead.peek(Ident) {
1675 input.parse().map(TraitItem::Const)
1676 } else if lookahead.peek(Token![unsafe])
1677 || lookahead.peek(Token![extern])
1678 || lookahead.peek(Token![fn])
1679 {
1680 input.parse().map(TraitItem::Method)
1681 } else {
1682 Err(lookahead.error())
1683 }
1684 } else if lookahead.peek(Token![unsafe])
1685 || lookahead.peek(Token![extern])
1686 || lookahead.peek(Token![fn])
1687 {
1688 input.parse().map(TraitItem::Method)
1689 } else if lookahead.peek(Token![type]) {
1690 input.parse().map(TraitItem::Type)
1691 } else if lookahead.peek(Ident)
1692 || lookahead.peek(Token![self])
1693 || lookahead.peek(Token![super])
1694 || lookahead.peek(Token![extern])
1695 || lookahead.peek(Token![crate])
1696 || lookahead.peek(Token![::])
1697 {
1698 input.parse().map(TraitItem::Macro)
1699 } else {
1700 Err(lookahead.error())
1701 }
1702 }
1703 }
1704
1705 impl Parse for TraitItemConst {
1706 fn parse(input: ParseStream) -> Result<Self> {
1707 Ok(TraitItemConst {
1708 attrs: input.call(Attribute::parse_outer)?,
1709 const_token: input.parse()?,
1710 ident: input.parse()?,
1711 colon_token: input.parse()?,
1712 ty: input.parse()?,
1713 default: {
1714 if input.peek(Token![=]) {
1715 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001716 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001717 Some((eq_token, default))
1718 } else {
1719 None
1720 }
1721 },
1722 semi_token: input.parse()?,
1723 })
1724 }
1725 }
1726
1727 impl Parse for TraitItemMethod {
1728 fn parse(input: ParseStream) -> Result<Self> {
1729 let outer_attrs = input.call(Attribute::parse_outer)?;
1730 let constness: Option<Token![const]> = input.parse()?;
1731 let unsafety: Option<Token![unsafe]> = input.parse()?;
1732 let abi: Option<Abi> = input.parse()?;
1733 let fn_token: Token![fn] = input.parse()?;
1734 let ident: Ident = input.parse()?;
1735 let generics: Generics = input.parse()?;
1736
1737 let content;
1738 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001739 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001740
1741 let output: ReturnType = input.parse()?;
1742 let where_clause: Option<WhereClause> = input.parse()?;
1743
1744 let lookahead = input.lookahead1();
1745 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1746 let content;
1747 let brace_token = braced!(content in input);
1748 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001749 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001750 (Some(brace_token), inner_attrs, stmts, None)
1751 } else if lookahead.peek(Token![;]) {
1752 let semi_token: Token![;] = input.parse()?;
1753 (None, Vec::new(), Vec::new(), Some(semi_token))
1754 } else {
1755 return Err(lookahead.error());
1756 };
1757
1758 Ok(TraitItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001759 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnayda705bd2017-11-10 21:58:05 -08001760 sig: MethodSig {
1761 constness: constness,
1762 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001763 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001764 abi: abi,
1765 ident: ident,
1766 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001767 fn_token: fn_token,
1768 paren_token: paren_token,
1769 inputs: inputs,
1770 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001771 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001772 generics: Generics {
1773 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001774 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001775 },
1776 },
David Tolnayda705bd2017-11-10 21:58:05 -08001777 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001778 default: brace_token.map(|brace_token| Block {
1779 brace_token: brace_token,
1780 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001781 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001782 semi_token: semi_token,
1783 })
1784 }
1785 }
1786
1787 impl Parse for TraitItemType {
1788 fn parse(input: ParseStream) -> Result<Self> {
1789 let attrs = input.call(Attribute::parse_outer)?;
1790 let type_token: Token![type] = input.parse()?;
1791 let ident: Ident = input.parse()?;
1792 let mut generics: Generics = input.parse()?;
1793 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001794
1795 let mut bounds = Punctuated::new();
1796 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001797 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1798 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001799 if !bounds.is_empty() {
1800 bounds.push_punct(input.parse()?);
1801 }
1802 bounds.push_value(input.parse()?);
1803 }
1804 }
1805
David Tolnay6a170ce2018-08-26 22:29:24 -07001806 generics.where_clause = input.parse()?;
1807 let default = if input.peek(Token![=]) {
1808 let eq_token: Token![=] = input.parse()?;
1809 let default: Type = input.parse()?;
1810 Some((eq_token, default))
1811 } else {
1812 None
1813 };
1814 let semi_token: Token![;] = input.parse()?;
1815
1816 Ok(TraitItemType {
1817 attrs: attrs,
1818 type_token: type_token,
1819 ident: ident,
1820 generics: generics,
1821 colon_token: colon_token,
1822 bounds: bounds,
1823 default: default,
1824 semi_token: semi_token,
1825 })
1826 }
1827 }
1828
1829 impl Parse for TraitItemMacro {
1830 fn parse(input: ParseStream) -> Result<Self> {
1831 let attrs = input.call(Attribute::parse_outer)?;
1832 let mac: Macro = input.parse()?;
1833 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1834 None
1835 } else {
1836 Some(input.parse()?)
1837 };
1838 Ok(TraitItemMacro {
1839 attrs: attrs,
1840 mac: mac,
1841 semi_token: semi_token,
1842 })
1843 }
1844 }
1845
1846 impl Parse for ItemImpl {
1847 fn parse(input: ParseStream) -> Result<Self> {
1848 let outer_attrs = input.call(Attribute::parse_outer)?;
1849 let defaultness: Option<Token![default]> = input.parse()?;
1850 let unsafety: Option<Token![unsafe]> = input.parse()?;
David Tolnay2beee042019-04-03 08:36:59 -07001851 let impl_token: Token![impl] = input.parse()?;
David Tolnay3c29f6e2018-10-27 22:47:48 -07001852
1853 let has_generics = input.peek(Token![<])
1854 && (input.peek2(Token![>])
1855 || input.peek2(Token![#])
1856 || (input.peek2(Ident) || input.peek2(Lifetime))
David Tolnaye614f282018-10-27 22:50:12 -07001857 && (input.peek3(Token![:])
1858 || input.peek3(Token![,])
1859 || input.peek3(Token![>])));
David Tolnay3c29f6e2018-10-27 22:47:48 -07001860 let generics: Generics = if has_generics {
1861 input.parse()?
1862 } else {
1863 Generics::default()
1864 };
1865
David Tolnay6a170ce2018-08-26 22:29:24 -07001866 let trait_ = {
1867 let ahead = input.fork();
1868 if ahead.parse::<Option<Token![!]>>().is_ok()
1869 && ahead.parse::<Path>().is_ok()
1870 && ahead.parse::<Token![for]>().is_ok()
1871 {
1872 let polarity: Option<Token![!]> = input.parse()?;
1873 let path: Path = input.parse()?;
1874 let for_token: Token![for] = input.parse()?;
1875 Some((polarity, path, for_token))
1876 } else {
1877 None
1878 }
1879 };
1880 let self_ty: Type = input.parse()?;
1881 let where_clause: Option<WhereClause> = input.parse()?;
1882
1883 let content;
1884 let brace_token = braced!(content in input);
1885 let inner_attrs = content.call(Attribute::parse_inner)?;
1886
1887 let mut items = Vec::new();
1888 while !content.is_empty() {
1889 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001890 }
David Tolnay0aecb732016-10-03 23:03:50 -07001891
David Tolnay6a170ce2018-08-26 22:29:24 -07001892 Ok(ItemImpl {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001893 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07001894 defaultness: defaultness,
1895 unsafety: unsafety,
1896 impl_token: impl_token,
1897 generics: Generics {
1898 where_clause: where_clause,
1899 ..generics
1900 },
1901 trait_: trait_,
1902 self_ty: Box::new(self_ty),
1903 brace_token: brace_token,
1904 items: items,
1905 })
1906 }
1907 }
David Tolnay0aecb732016-10-03 23:03:50 -07001908
David Tolnay6a170ce2018-08-26 22:29:24 -07001909 impl Parse for ImplItem {
1910 fn parse(input: ParseStream) -> Result<Self> {
1911 let ahead = input.fork();
1912 ahead.call(Attribute::parse_outer)?;
1913 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001914
David Tolnay6a170ce2018-08-26 22:29:24 -07001915 let mut lookahead = ahead.lookahead1();
1916 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1917 let defaultness: Token![default] = ahead.parse()?;
1918 lookahead = ahead.lookahead1();
1919 Some(defaultness)
1920 } else {
1921 None
1922 };
David Tolnay4c9be372016-10-06 00:47:37 -07001923
David Tolnay6a170ce2018-08-26 22:29:24 -07001924 if lookahead.peek(Token![const]) {
1925 ahead.parse::<Token![const]>()?;
1926 let lookahead = ahead.lookahead1();
1927 if lookahead.peek(Ident) {
1928 input.parse().map(ImplItem::Const)
1929 } else if lookahead.peek(Token![unsafe])
1930 || lookahead.peek(Token![async])
1931 || lookahead.peek(Token![extern])
1932 || lookahead.peek(Token![fn])
1933 {
1934 input.parse().map(ImplItem::Method)
1935 } else {
1936 Err(lookahead.error())
1937 }
1938 } else if lookahead.peek(Token![unsafe])
1939 || lookahead.peek(Token![async])
1940 || lookahead.peek(Token![extern])
1941 || lookahead.peek(Token![fn])
1942 {
1943 input.parse().map(ImplItem::Method)
1944 } else if lookahead.peek(Token![type]) {
1945 input.parse().map(ImplItem::Type)
1946 } else if vis.is_inherited()
1947 && defaultness.is_none()
1948 && lookahead.peek(Token![existential])
1949 {
1950 input.parse().map(ImplItem::Existential)
1951 } else if vis.is_inherited()
1952 && defaultness.is_none()
1953 && (lookahead.peek(Ident)
1954 || lookahead.peek(Token![self])
1955 || lookahead.peek(Token![super])
1956 || lookahead.peek(Token![extern])
1957 || lookahead.peek(Token![crate])
1958 || lookahead.peek(Token![::]))
1959 {
1960 input.parse().map(ImplItem::Macro)
1961 } else {
1962 Err(lookahead.error())
1963 }
1964 }
1965 }
David Tolnay4c9be372016-10-06 00:47:37 -07001966
David Tolnay3779bb72018-08-26 18:46:07 -07001967 impl Parse for ImplItemConst {
1968 fn parse(input: ParseStream) -> Result<Self> {
1969 Ok(ImplItemConst {
1970 attrs: input.call(Attribute::parse_outer)?,
1971 vis: input.parse()?,
1972 defaultness: input.parse()?,
1973 const_token: input.parse()?,
1974 ident: input.parse()?,
1975 colon_token: input.parse()?,
1976 ty: input.parse()?,
1977 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001978 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001979 semi_token: input.parse()?,
1980 })
1981 }
1982 }
David Tolnay4c9be372016-10-06 00:47:37 -07001983
David Tolnay6a170ce2018-08-26 22:29:24 -07001984 impl Parse for ImplItemMethod {
1985 fn parse(input: ParseStream) -> Result<Self> {
1986 let outer_attrs = input.call(Attribute::parse_outer)?;
1987 let vis: Visibility = input.parse()?;
1988 let defaultness: Option<Token![default]> = input.parse()?;
1989 let constness: Option<Token![const]> = input.parse()?;
1990 let unsafety: Option<Token![unsafe]> = input.parse()?;
1991 let asyncness: Option<Token![async]> = input.parse()?;
1992 let abi: Option<Abi> = input.parse()?;
1993 let fn_token: Token![fn] = input.parse()?;
1994 let ident: Ident = input.parse()?;
1995 let generics: Generics = input.parse()?;
1996
1997 let content;
1998 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001999 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07002000
2001 let output: ReturnType = input.parse()?;
2002 let where_clause: Option<WhereClause> = input.parse()?;
2003
2004 let content;
2005 let brace_token = braced!(content in input);
2006 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07002007 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07002008
2009 Ok(ImplItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07002010 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07002011 vis: vis,
2012 defaultness: defaultness,
2013 sig: MethodSig {
2014 constness: constness,
2015 unsafety: unsafety,
2016 asyncness: asyncness,
2017 abi: abi,
2018 ident: ident,
2019 decl: FnDecl {
2020 fn_token: fn_token,
2021 paren_token: paren_token,
2022 inputs: inputs,
2023 output: output,
2024 variadic: None,
2025 generics: Generics {
2026 where_clause: where_clause,
2027 ..generics
2028 },
2029 },
2030 },
2031 block: Block {
2032 brace_token: brace_token,
2033 stmts: stmts,
2034 },
2035 })
2036 }
2037 }
David Tolnay4c9be372016-10-06 00:47:37 -07002038
David Tolnay3779bb72018-08-26 18:46:07 -07002039 impl Parse for ImplItemType {
2040 fn parse(input: ParseStream) -> Result<Self> {
2041 Ok(ImplItemType {
2042 attrs: input.call(Attribute::parse_outer)?,
2043 vis: input.parse()?,
2044 defaultness: input.parse()?,
2045 type_token: input.parse()?,
2046 ident: input.parse()?,
2047 generics: {
2048 let mut generics: Generics = input.parse()?;
2049 generics.where_clause = input.parse()?;
2050 generics
2051 },
2052 eq_token: input.parse()?,
2053 ty: input.parse()?,
2054 semi_token: input.parse()?,
2055 })
David Tolnaybb82ef02018-08-24 20:15:45 -04002056 }
David Tolnay3779bb72018-08-26 18:46:07 -07002057 }
David Tolnay758ee132018-08-21 21:29:40 -04002058
David Tolnay3779bb72018-08-26 18:46:07 -07002059 impl Parse for ImplItemExistential {
2060 fn parse(input: ParseStream) -> Result<Self> {
2061 let ety: ItemExistential = input.parse()?;
2062 Ok(ImplItemExistential {
2063 attrs: ety.attrs,
2064 existential_token: ety.existential_token,
2065 type_token: ety.type_token,
2066 ident: ety.ident,
2067 generics: ety.generics,
2068 colon_token: ety.colon_token,
2069 bounds: ety.bounds,
2070 semi_token: ety.semi_token,
2071 })
2072 }
2073 }
2074
2075 impl Parse for ImplItemMacro {
2076 fn parse(input: ParseStream) -> Result<Self> {
2077 let attrs = input.call(Attribute::parse_outer)?;
2078 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07002079 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07002080 None
2081 } else {
2082 Some(input.parse()?)
2083 };
2084 Ok(ImplItemMacro {
2085 attrs: attrs,
2086 mac: mac,
2087 semi_token: semi_token,
2088 })
2089 }
2090 }
David Tolnay4c9be372016-10-06 00:47:37 -07002091
David Tolnay6a170ce2018-08-26 22:29:24 -07002092 impl Visibility {
2093 fn is_inherited(&self) -> bool {
2094 match *self {
2095 Visibility::Inherited => true,
2096 _ => false,
2097 }
2098 }
2099 }
2100
2101 impl MacroDelimiter {
2102 fn is_brace(&self) -> bool {
2103 match *self {
2104 MacroDelimiter::Brace(_) => true,
2105 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
2106 }
David Tolnay57292da2017-12-27 21:03:33 -05002107 }
2108 }
David Tolnayedf2b992016-09-23 20:43:45 -07002109}
David Tolnay4a51dc72016-10-01 00:40:31 -07002110
2111#[cfg(feature = "printing")]
2112mod printing {
2113 use super::*;
David Tolnay64023912018-08-31 09:51:12 -07002114
Alex Crichtona74a1c82018-05-16 10:20:44 -07002115 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07002116 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07002117
David Tolnay64023912018-08-31 09:51:12 -07002118 use attr::FilterAttrs;
2119 use print::TokensOrDefault;
2120
David Tolnay1bfa7332017-11-11 12:41:20 -08002121 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002122 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002123 tokens.append_all(self.attrs.outer());
2124 self.vis.to_tokens(tokens);
2125 self.extern_token.to_tokens(tokens);
2126 self.crate_token.to_tokens(tokens);
2127 self.ident.to_tokens(tokens);
2128 if let Some((ref as_token, ref rename)) = self.rename {
2129 as_token.to_tokens(tokens);
2130 rename.to_tokens(tokens);
2131 }
2132 self.semi_token.to_tokens(tokens);
2133 }
2134 }
2135
2136 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002137 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002138 tokens.append_all(self.attrs.outer());
2139 self.vis.to_tokens(tokens);
2140 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002141 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002142 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002143 self.semi_token.to_tokens(tokens);
2144 }
2145 }
2146
2147 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002148 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002149 tokens.append_all(self.attrs.outer());
2150 self.vis.to_tokens(tokens);
2151 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002152 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002153 self.ident.to_tokens(tokens);
2154 self.colon_token.to_tokens(tokens);
2155 self.ty.to_tokens(tokens);
2156 self.eq_token.to_tokens(tokens);
2157 self.expr.to_tokens(tokens);
2158 self.semi_token.to_tokens(tokens);
2159 }
2160 }
2161
2162 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002163 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002164 tokens.append_all(self.attrs.outer());
2165 self.vis.to_tokens(tokens);
2166 self.const_token.to_tokens(tokens);
2167 self.ident.to_tokens(tokens);
2168 self.colon_token.to_tokens(tokens);
2169 self.ty.to_tokens(tokens);
2170 self.eq_token.to_tokens(tokens);
2171 self.expr.to_tokens(tokens);
2172 self.semi_token.to_tokens(tokens);
2173 }
2174 }
2175
2176 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002177 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002178 tokens.append_all(self.attrs.outer());
2179 self.vis.to_tokens(tokens);
2180 self.constness.to_tokens(tokens);
2181 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002182 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002183 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002184 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002185 self.block.brace_token.surround(tokens, |tokens| {
2186 tokens.append_all(self.attrs.inner());
2187 tokens.append_all(&self.block.stmts);
2188 });
2189 }
2190 }
2191
2192 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002193 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002194 tokens.append_all(self.attrs.outer());
2195 self.vis.to_tokens(tokens);
2196 self.mod_token.to_tokens(tokens);
2197 self.ident.to_tokens(tokens);
2198 if let Some((ref brace, ref items)) = self.content {
2199 brace.surround(tokens, |tokens| {
2200 tokens.append_all(self.attrs.inner());
2201 tokens.append_all(items);
2202 });
2203 } else {
2204 TokensOrDefault(&self.semi).to_tokens(tokens);
2205 }
2206 }
2207 }
2208
2209 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002210 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002211 tokens.append_all(self.attrs.outer());
2212 self.abi.to_tokens(tokens);
2213 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002214 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002215 tokens.append_all(&self.items);
2216 });
2217 }
2218 }
2219
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002220 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002221 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002222 tokens.append_all(self.attrs.outer());
2223 self.vis.to_tokens(tokens);
2224 self.type_token.to_tokens(tokens);
2225 self.ident.to_tokens(tokens);
2226 self.generics.to_tokens(tokens);
2227 self.generics.where_clause.to_tokens(tokens);
2228 self.eq_token.to_tokens(tokens);
2229 self.ty.to_tokens(tokens);
2230 self.semi_token.to_tokens(tokens);
2231 }
2232 }
2233
David Tolnaybb82ef02018-08-24 20:15:45 -04002234 impl ToTokens for ItemExistential {
2235 fn to_tokens(&self, tokens: &mut TokenStream) {
2236 tokens.append_all(self.attrs.outer());
2237 self.vis.to_tokens(tokens);
2238 self.existential_token.to_tokens(tokens);
2239 self.type_token.to_tokens(tokens);
2240 self.ident.to_tokens(tokens);
2241 self.generics.to_tokens(tokens);
2242 self.generics.where_clause.to_tokens(tokens);
2243 if !self.bounds.is_empty() {
2244 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2245 self.bounds.to_tokens(tokens);
2246 }
2247 self.semi_token.to_tokens(tokens);
2248 }
2249 }
2250
David Tolnay1bfa7332017-11-11 12:41:20 -08002251 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002252 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002253 tokens.append_all(self.attrs.outer());
2254 self.vis.to_tokens(tokens);
2255 self.enum_token.to_tokens(tokens);
2256 self.ident.to_tokens(tokens);
2257 self.generics.to_tokens(tokens);
2258 self.generics.where_clause.to_tokens(tokens);
2259 self.brace_token.surround(tokens, |tokens| {
2260 self.variants.to_tokens(tokens);
2261 });
2262 }
2263 }
2264
2265 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002266 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002267 tokens.append_all(self.attrs.outer());
2268 self.vis.to_tokens(tokens);
2269 self.struct_token.to_tokens(tokens);
2270 self.ident.to_tokens(tokens);
2271 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002272 match self.fields {
2273 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002274 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002275 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002276 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002277 Fields::Unnamed(ref fields) => {
2278 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002279 self.generics.where_clause.to_tokens(tokens);
2280 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002281 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002282 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002283 self.generics.where_clause.to_tokens(tokens);
2284 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002285 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002286 }
2287 }
2288 }
2289
2290 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002291 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002292 tokens.append_all(self.attrs.outer());
2293 self.vis.to_tokens(tokens);
2294 self.union_token.to_tokens(tokens);
2295 self.ident.to_tokens(tokens);
2296 self.generics.to_tokens(tokens);
2297 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002298 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002299 }
2300 }
2301
2302 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002303 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002304 tokens.append_all(self.attrs.outer());
2305 self.vis.to_tokens(tokens);
2306 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002307 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002308 self.trait_token.to_tokens(tokens);
2309 self.ident.to_tokens(tokens);
2310 self.generics.to_tokens(tokens);
2311 if !self.supertraits.is_empty() {
2312 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2313 self.supertraits.to_tokens(tokens);
2314 }
2315 self.generics.where_clause.to_tokens(tokens);
2316 self.brace_token.surround(tokens, |tokens| {
2317 tokens.append_all(&self.items);
2318 });
2319 }
2320 }
2321
David Tolnayc6b04dd2018-08-30 23:22:51 -07002322 impl ToTokens for ItemTraitAlias {
2323 fn to_tokens(&self, tokens: &mut TokenStream) {
2324 tokens.append_all(self.attrs.outer());
2325 self.vis.to_tokens(tokens);
2326 self.trait_token.to_tokens(tokens);
2327 self.ident.to_tokens(tokens);
2328 self.generics.to_tokens(tokens);
2329 self.eq_token.to_tokens(tokens);
2330 self.bounds.to_tokens(tokens);
2331 self.generics.where_clause.to_tokens(tokens);
2332 self.semi_token.to_tokens(tokens);
2333 }
2334 }
2335
David Tolnay1bfa7332017-11-11 12:41:20 -08002336 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002337 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002338 tokens.append_all(self.attrs.outer());
2339 self.defaultness.to_tokens(tokens);
2340 self.unsafety.to_tokens(tokens);
2341 self.impl_token.to_tokens(tokens);
2342 self.generics.to_tokens(tokens);
2343 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2344 polarity.to_tokens(tokens);
2345 path.to_tokens(tokens);
2346 for_token.to_tokens(tokens);
2347 }
2348 self.self_ty.to_tokens(tokens);
2349 self.generics.where_clause.to_tokens(tokens);
2350 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002351 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002352 tokens.append_all(&self.items);
2353 });
2354 }
2355 }
2356
2357 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002358 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002359 tokens.append_all(self.attrs.outer());
2360 self.mac.path.to_tokens(tokens);
2361 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002362 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002363 match self.mac.delimiter {
2364 MacroDelimiter::Paren(ref paren) => {
2365 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2366 }
2367 MacroDelimiter::Brace(ref brace) => {
2368 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2369 }
2370 MacroDelimiter::Bracket(ref bracket) => {
2371 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2372 }
2373 }
David Tolnay57292da2017-12-27 21:03:33 -05002374 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002375 }
2376 }
David Tolnay42602292016-10-01 22:25:45 -07002377
David Tolnay500d8322017-12-18 00:32:51 -08002378 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002379 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002380 tokens.append_all(self.attrs.outer());
2381 self.vis.to_tokens(tokens);
2382 self.macro_token.to_tokens(tokens);
2383 self.ident.to_tokens(tokens);
David Tolnay87041a52018-12-15 00:48:03 -08002384
2385 // Hack: see comment in impl Parse for ItemMacro2.
2386 if self.args.to_string() != "$ $" {
2387 self.paren_token.surround(tokens, |tokens| {
2388 self.args.to_tokens(tokens);
2389 });
2390 }
2391
David Tolnayab919512017-12-30 23:31:51 -05002392 self.brace_token.surround(tokens, |tokens| {
2393 self.body.to_tokens(tokens);
2394 });
David Tolnay500d8322017-12-18 00:32:51 -08002395 }
2396 }
2397
David Tolnay2ae520a2017-12-29 11:19:50 -05002398 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002399 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002400 self.tts.to_tokens(tokens);
2401 }
2402 }
2403
David Tolnay5f332a92017-12-26 00:42:45 -05002404 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002405 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002406 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002407 self.colon2_token.to_tokens(tokens);
2408 self.tree.to_tokens(tokens);
2409 }
2410 }
2411
2412 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002413 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002414 self.ident.to_tokens(tokens);
2415 }
2416 }
2417
2418 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002419 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002420 self.ident.to_tokens(tokens);
2421 self.as_token.to_tokens(tokens);
2422 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002423 }
2424 }
2425
David Tolnay5f332a92017-12-26 00:42:45 -05002426 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002427 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002428 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002429 }
2430 }
2431
David Tolnayd97a7d22018-03-31 19:17:01 +02002432 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002433 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002434 self.brace_token.surround(tokens, |tokens| {
2435 self.items.to_tokens(tokens);
2436 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002437 }
2438 }
2439
David Tolnay1bfa7332017-11-11 12:41:20 -08002440 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002441 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002442 tokens.append_all(self.attrs.outer());
2443 self.const_token.to_tokens(tokens);
2444 self.ident.to_tokens(tokens);
2445 self.colon_token.to_tokens(tokens);
2446 self.ty.to_tokens(tokens);
2447 if let Some((ref eq_token, ref default)) = self.default {
2448 eq_token.to_tokens(tokens);
2449 default.to_tokens(tokens);
2450 }
2451 self.semi_token.to_tokens(tokens);
2452 }
2453 }
2454
2455 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002456 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002457 tokens.append_all(self.attrs.outer());
2458 self.sig.to_tokens(tokens);
2459 match self.default {
2460 Some(ref block) => {
2461 block.brace_token.surround(tokens, |tokens| {
2462 tokens.append_all(self.attrs.inner());
2463 tokens.append_all(&block.stmts);
2464 });
David Tolnayca085422016-10-04 00:12:38 -07002465 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002466 None => {
2467 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002468 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002469 }
2470 }
2471 }
2472
2473 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002474 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002475 tokens.append_all(self.attrs.outer());
2476 self.type_token.to_tokens(tokens);
2477 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002478 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002479 if !self.bounds.is_empty() {
2480 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2481 self.bounds.to_tokens(tokens);
2482 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002483 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002484 if let Some((ref eq_token, ref default)) = self.default {
2485 eq_token.to_tokens(tokens);
2486 default.to_tokens(tokens);
2487 }
2488 self.semi_token.to_tokens(tokens);
2489 }
2490 }
2491
2492 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002493 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002494 tokens.append_all(self.attrs.outer());
2495 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002496 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002497 }
2498 }
2499
David Tolnay2ae520a2017-12-29 11:19:50 -05002500 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002501 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002502 self.tts.to_tokens(tokens);
2503 }
2504 }
2505
David Tolnay857628c2017-11-11 12:25:31 -08002506 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002507 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002508 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002509 self.vis.to_tokens(tokens);
2510 self.defaultness.to_tokens(tokens);
2511 self.const_token.to_tokens(tokens);
2512 self.ident.to_tokens(tokens);
2513 self.colon_token.to_tokens(tokens);
2514 self.ty.to_tokens(tokens);
2515 self.eq_token.to_tokens(tokens);
2516 self.expr.to_tokens(tokens);
2517 self.semi_token.to_tokens(tokens);
2518 }
2519 }
2520
2521 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002522 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002523 tokens.append_all(self.attrs.outer());
2524 self.vis.to_tokens(tokens);
2525 self.defaultness.to_tokens(tokens);
2526 self.sig.to_tokens(tokens);
2527 self.block.brace_token.surround(tokens, |tokens| {
2528 tokens.append_all(self.attrs.inner());
2529 tokens.append_all(&self.block.stmts);
2530 });
2531 }
2532 }
2533
2534 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002535 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002536 tokens.append_all(self.attrs.outer());
2537 self.vis.to_tokens(tokens);
2538 self.defaultness.to_tokens(tokens);
2539 self.type_token.to_tokens(tokens);
2540 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002541 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002542 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002543 self.eq_token.to_tokens(tokens);
2544 self.ty.to_tokens(tokens);
2545 self.semi_token.to_tokens(tokens);
2546 }
2547 }
2548
David Tolnaybb82ef02018-08-24 20:15:45 -04002549 impl ToTokens for ImplItemExistential {
2550 fn to_tokens(&self, tokens: &mut TokenStream) {
2551 tokens.append_all(self.attrs.outer());
2552 self.existential_token.to_tokens(tokens);
2553 self.type_token.to_tokens(tokens);
2554 self.ident.to_tokens(tokens);
2555 self.generics.to_tokens(tokens);
2556 self.generics.where_clause.to_tokens(tokens);
2557 if !self.bounds.is_empty() {
2558 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2559 self.bounds.to_tokens(tokens);
2560 }
2561 self.semi_token.to_tokens(tokens);
2562 }
2563 }
2564
David Tolnay857628c2017-11-11 12:25:31 -08002565 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002566 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002567 tokens.append_all(self.attrs.outer());
2568 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002569 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002570 }
2571 }
2572
David Tolnay2ae520a2017-12-29 11:19:50 -05002573 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002574 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002575 self.tts.to_tokens(tokens);
2576 }
2577 }
2578
David Tolnay8894f602017-11-11 12:11:04 -08002579 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002580 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002581 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002582 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002583 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002584 self.semi_token.to_tokens(tokens);
2585 }
2586 }
2587
2588 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002589 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002590 tokens.append_all(self.attrs.outer());
2591 self.vis.to_tokens(tokens);
2592 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002593 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002594 self.ident.to_tokens(tokens);
2595 self.colon_token.to_tokens(tokens);
2596 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002597 self.semi_token.to_tokens(tokens);
2598 }
2599 }
2600
David Tolnay199bcbb2017-11-12 10:33:52 -08002601 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002602 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002603 tokens.append_all(self.attrs.outer());
2604 self.vis.to_tokens(tokens);
2605 self.type_token.to_tokens(tokens);
2606 self.ident.to_tokens(tokens);
2607 self.semi_token.to_tokens(tokens);
2608 }
2609 }
2610
David Tolnay435c1782018-08-24 16:15:44 -04002611 impl ToTokens for ForeignItemMacro {
2612 fn to_tokens(&self, tokens: &mut TokenStream) {
2613 tokens.append_all(self.attrs.outer());
2614 self.mac.to_tokens(tokens);
2615 self.semi_token.to_tokens(tokens);
2616 }
2617 }
2618
David Tolnay2ae520a2017-12-29 11:19:50 -05002619 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002620 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayfac872d2019-02-28 22:45:31 -08002621 self.tts.to_tokens(tokens);
2622 }
David Tolnay2ae520a2017-12-29 11:19:50 -05002623 }
2624
David Tolnay570695e2017-06-03 16:15:13 -07002625 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002626 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002627 self.constness.to_tokens(tokens);
2628 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002629 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002630 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002631 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002632 }
2633 }
2634
Alex Crichtona74a1c82018-05-16 10:20:44 -07002635 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002636
2637 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002638 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002639 self.0.fn_token.to_tokens(tokens);
2640 self.1.to_tokens(tokens);
2641 self.0.generics.to_tokens(tokens);
2642 self.0.paren_token.surround(tokens, |tokens| {
2643 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002644 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2645 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002646 }
David Tolnayd2836e22017-12-27 23:13:00 -05002647 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002648 });
2649 self.0.output.to_tokens(tokens);
2650 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002651 }
2652 }
2653
Alex Crichton62a0a592017-05-22 13:58:53 -07002654 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002655 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002656 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002657 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002658 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002659 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002660 }
2661 }
2662
2663 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002664 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002665 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002666 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002667 }
2668 }
2669
2670 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002671 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002672 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002673 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002675 }
2676 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002677}