blob: b0611799074b7db73e3d3e3921bd914c36efea47 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
David Tolnaybb82ef02018-08-24 20:15:45 -0400139 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
142 pub Existential(ItemExistential {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub existential_token: Token![existential],
146 pub type_token: Token![type],
147 pub ident: Ident,
148 pub generics: Generics,
149 pub colon_token: Option<Token![:]>,
150 pub bounds: Punctuated<TypeParamBound, Token![+]>,
151 pub semi_token: Token![;],
152 }),
153
David Tolnay2b214082018-01-07 01:30:18 -0800154 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800155 ///
156 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 pub Struct(ItemStruct {
158 pub attrs: Vec<Attribute>,
159 pub vis: Visibility,
160 pub struct_token: Token![struct],
161 pub ident: Ident,
162 pub generics: Generics,
163 pub fields: Fields,
164 pub semi_token: Option<Token![;]>,
165 }),
David Tolnay2b214082018-01-07 01:30:18 -0800166
167 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800168 ///
169 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700174 pub ident: Ident,
175 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
David Tolnay2b214082018-01-07 01:30:18 -0800179
180 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800181 ///
182 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700187 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500189 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 }),
David Tolnay2b214082018-01-07 01:30:18 -0800191
192 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800193 ///
194 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700197 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500198 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500199 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800200 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700201 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500205 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub items: Vec<TraitItem>,
207 }),
David Tolnay2b214082018-01-07 01:30:18 -0800208
209 /// An impl block providing trait or associated items: `impl<A> Trait
210 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800211 ///
212 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800214 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500215 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500216 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700219 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500220 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700221 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub items: Vec<ImplItem>,
225 }),
David Tolnay2b214082018-01-07 01:30:18 -0800226
227 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800228 ///
229 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800230 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800231 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800232 /// The `example` in `macro_rules! example { ... }`.
233 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800234 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500235 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800236 }),
David Tolnay2b214082018-01-07 01:30:18 -0800237
238 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500241 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800242 pub attrs: Vec<Attribute>,
243 pub vis: Visibility,
244 pub macro_token: Token![macro],
245 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500246 pub paren_token: Paren,
247 pub args: TokenStream,
248 pub brace_token: Brace,
249 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500255 pub Verbatim(ItemVerbatim #manual_extra_traits {
256 pub tts: TokenStream,
257 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
David Tolnayb79ee962016-09-04 09:39:20 -0700259}
260
David Tolnay9c76bcb2017-12-26 23:14:59 -0500261#[cfg(feature = "extra-traits")]
262impl Eq for ItemMacro2 {}
263
264#[cfg(feature = "extra-traits")]
265impl PartialEq for ItemMacro2 {
266 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700267 self.attrs == other.attrs
268 && self.vis == other.vis
269 && self.macro_token == other.macro_token
270 && self.ident == other.ident
271 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500272 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
273 && self.brace_token == other.brace_token
274 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275 }
276}
277
278#[cfg(feature = "extra-traits")]
279impl Hash for ItemMacro2 {
280 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500281 where
282 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500283 {
284 self.attrs.hash(state);
285 self.vis.hash(state);
286 self.macro_token.hash(state);
287 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500288 self.paren_token.hash(state);
289 TokenStreamHelper(&self.args).hash(state);
290 self.brace_token.hash(state);
291 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500292 }
293}
294
David Tolnay2ae520a2017-12-29 11:19:50 -0500295#[cfg(feature = "extra-traits")]
296impl Eq for ItemVerbatim {}
297
298#[cfg(feature = "extra-traits")]
299impl PartialEq for ItemVerbatim {
300 fn eq(&self, other: &Self) -> bool {
301 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
302 }
303}
304
305#[cfg(feature = "extra-traits")]
306impl Hash for ItemVerbatim {
307 fn hash<H>(&self, state: &mut H)
308 where
309 H: Hasher,
310 {
311 TokenStreamHelper(&self.tts).hash(state);
312 }
313}
314
David Tolnay0e837402016-12-22 17:25:55 -0500315impl From<DeriveInput> for Item {
316 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500317 match input.data {
318 Data::Struct(data) => Item::Struct(ItemStruct {
319 attrs: input.attrs,
320 vis: input.vis,
321 struct_token: data.struct_token,
322 ident: input.ident,
323 generics: input.generics,
324 fields: data.fields,
325 semi_token: data.semi_token,
326 }),
327 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500328 attrs: input.attrs,
329 vis: input.vis,
330 enum_token: data.enum_token,
331 ident: input.ident,
332 generics: input.generics,
333 brace_token: data.brace_token,
334 variants: data.variants,
335 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500337 attrs: input.attrs,
338 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500339 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500340 ident: input.ident,
341 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500343 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700344 }
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800350 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800351 /// *This type is available if Syn is built with the `"full"` feature.*
352 ///
David Tolnay614a0142018-01-07 10:25:43 -0800353 /// # Syntax tree enum
354 ///
355 /// This type is a [syntax tree enum].
356 ///
357 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500358 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200359 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500362 pub Path(UsePath {
363 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200364 pub colon2_token: Token![::],
365 pub tree: Box<UseTree>,
366 }),
367
368 /// An identifier imported by a `use` item: `HashMap`.
369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
371 pub Name(UseName {
372 pub ident: Ident,
373 }),
374
375 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
378 pub Rename(UseRename {
379 pub ident: Ident,
380 pub as_token: Token![as],
381 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800383
David Tolnay05658502018-01-07 09:56:37 -0800384 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500387 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800390
David Tolnayd97a7d22018-03-31 19:17:01 +0200391 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800392 ///
393 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200394 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500396 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 }
399}
400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800402 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800403 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800404 /// *This type is available if Syn is built with the `"full"` feature.*
405 ///
David Tolnay614a0142018-01-07 10:25:43 -0800406 /// # Syntax tree enum
407 ///
408 /// This type is a [syntax tree enum].
409 ///
410 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800412 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800413 ///
414 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub attrs: Vec<Attribute>,
417 pub vis: Visibility,
418 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }),
David Tolnayebb72722018-01-07 01:14:13 -0800422
423 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800427 pub attrs: Vec<Attribute>,
428 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500430 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800431 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
David Tolnayebb72722018-01-07 01:14:13 -0800436
437 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800440 pub Type(ForeignItemType {
441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
443 pub type_token: Token![type],
444 pub ident: Ident,
445 pub semi_token: Token![;],
446 }),
David Tolnayebb72722018-01-07 01:14:13 -0800447
David Tolnay435c1782018-08-24 16:15:44 -0400448 /// A macro invocation within an extern block.
449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
451 pub Macro(ForeignItemMacro {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 pub semi_token: Option<Token![;]>,
455 }),
456
David Tolnayebb72722018-01-07 01:14:13 -0800457 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500460 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
461 pub tts: TokenStream,
462 }),
463 }
464}
465
466#[cfg(feature = "extra-traits")]
467impl Eq for ForeignItemVerbatim {}
468
469#[cfg(feature = "extra-traits")]
470impl PartialEq for ForeignItemVerbatim {
471 fn eq(&self, other: &Self) -> bool {
472 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
473 }
474}
475
476#[cfg(feature = "extra-traits")]
477impl Hash for ForeignItemVerbatim {
478 fn hash<H>(&self, state: &mut H)
479 where
480 H: Hasher,
481 {
482 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700484}
485
David Tolnayda705bd2017-11-10 21:58:05 -0800486ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800487 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800488 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800489 /// *This type is available if Syn is built with the `"full"` feature.*
490 ///
David Tolnay614a0142018-01-07 10:25:43 -0800491 /// # Syntax tree enum
492 ///
493 /// This type is a [syntax tree enum].
494 ///
495 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800496 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800497 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800498 ///
499 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800501 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700503 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub default: Option<(Token![=], Expr)>,
507 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
David Tolnayebb72722018-01-07 01:14:13 -0800509
510 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800514 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 pub sig: MethodSig,
516 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
David Tolnayebb72722018-01-07 01:14:13 -0800519
520 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700526 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500527 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800535 ///
536 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800537 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800539 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500540 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800541 }),
David Tolnayebb72722018-01-07 01:14:13 -0800542
543 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800544 ///
545 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500546 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
547 pub tts: TokenStream,
548 }),
549 }
550}
551
552#[cfg(feature = "extra-traits")]
553impl Eq for TraitItemVerbatim {}
554
555#[cfg(feature = "extra-traits")]
556impl PartialEq for TraitItemVerbatim {
557 fn eq(&self, other: &Self) -> bool {
558 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
559 }
560}
561
562#[cfg(feature = "extra-traits")]
563impl Hash for TraitItemVerbatim {
564 fn hash<H>(&self, state: &mut H)
565 where
566 H: Hasher,
567 {
568 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800573 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800574 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800575 /// *This type is available if Syn is built with the `"full"` feature.*
576 ///
David Tolnay614a0142018-01-07 10:25:43 -0800577 /// # Syntax tree enum
578 ///
579 /// This type is a [syntax tree enum].
580 ///
581 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800582 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800583 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700591 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }),
David Tolnayebb72722018-01-07 01:14:13 -0800598
599 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800600 ///
601 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700602 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700604 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500605 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 pub sig: MethodSig,
607 pub block: Block,
608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800614 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700615 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500616 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500619 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 }),
David Tolnayebb72722018-01-07 01:14:13 -0800624
David Tolnaybb82ef02018-08-24 20:15:45 -0400625 /// An existential type within an impl block.
626 ///
627 /// *This type is available if Syn is built with the `"full"` feature.*
628 pub Existential(ImplItemExistential {
629 pub attrs: Vec<Attribute>,
630 pub existential_token: Token![existential],
631 pub type_token: Token![type],
632 pub ident: Ident,
633 pub generics: Generics,
634 pub colon_token: Option<Token![:]>,
635 pub bounds: Punctuated<TypeParamBound, Token![+]>,
636 pub semi_token: Token![;],
637 }),
638
David Tolnayebb72722018-01-07 01:14:13 -0800639 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800642 pub Macro(ImplItemMacro {
643 pub attrs: Vec<Attribute>,
644 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500645 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800646 }),
David Tolnayebb72722018-01-07 01:14:13 -0800647
648 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800649 ///
650 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500651 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
652 pub tts: TokenStream,
653 }),
654 }
655}
656
657#[cfg(feature = "extra-traits")]
658impl Eq for ImplItemVerbatim {}
659
660#[cfg(feature = "extra-traits")]
661impl PartialEq for ImplItemVerbatim {
662 fn eq(&self, other: &Self) -> bool {
663 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
664 }
665}
666
667#[cfg(feature = "extra-traits")]
668impl Hash for ImplItemVerbatim {
669 fn hash<H>(&self, state: &mut H)
670 where
671 H: Hasher,
672 {
673 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700675}
676
677ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800678 /// A method's signature in a trait or implementation: `unsafe fn
679 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500683 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500684 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900685 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700687 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }
690}
691
692ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800693 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500698 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500699 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500700 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500701 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500702 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700704}
705
Alex Crichton62a0a592017-05-22 13:58:53 -0700706ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800707 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800708 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800709 /// *This type is available if Syn is built with the `"full"` feature.*
710 ///
David Tolnay614a0142018-01-07 10:25:43 -0800711 /// # Syntax tree enum
712 ///
713 /// This type is a [syntax tree enum].
714 ///
715 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// Self captured by reference in a function signature: `&self` or `&mut
718 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500725 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800727
David Tolnay3f559052018-01-06 23:59:48 -0800728 /// Self captured by value in a function signature: `self` or `mut
729 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800736
David Tolnay3f559052018-01-06 23:59:48 -0800737 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800738 ///
739 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 pub Captured(ArgCaptured {
741 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800743 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800745
David Tolnay3f559052018-01-06 23:59:48 -0800746 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500747 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800748 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }
David Tolnay62f374c2016-10-02 13:37:00 -0700751}
752
David Tolnayedf2b992016-09-23 20:43:45 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700756
David Tolnay2ad62c12018-08-26 19:00:35 -0400757 use parse::{Parse, ParseStream, Result};
David Tolnay3779bb72018-08-26 18:46:07 -0700758 use synom::ext::IdentExt;
David Tolnay84aa0752016-10-02 23:01:13 -0700759
David Tolnay6a170ce2018-08-26 22:29:24 -0700760 impl Parse for Item {
761 fn parse(input: ParseStream) -> Result<Self> {
762 let ahead = input.fork();
763 ahead.call(Attribute::parse_outer)?;
764 let vis: Visibility = ahead.parse()?;
765
766 let lookahead = ahead.lookahead1();
767 if lookahead.peek(Token![extern]) {
768 ahead.parse::<Token![extern]>()?;
769 let lookahead = ahead.lookahead1();
770 if lookahead.peek(Token![crate]) {
771 input.parse().map(Item::ExternCrate)
772 } else if lookahead.peek(Token![fn]) {
773 input.parse().map(Item::Fn)
774 } else if lookahead.peek(token::Brace) {
775 input.parse().map(Item::ForeignMod)
776 } else if lookahead.peek(LitStr) {
777 ahead.parse::<LitStr>()?;
778 let lookahead = ahead.lookahead1();
779 if lookahead.peek(token::Brace) {
780 input.parse().map(Item::ForeignMod)
781 } else if lookahead.peek(Token![fn]) {
782 input.parse().map(Item::Fn)
783 } else {
784 Err(lookahead.error())
785 }
786 } else {
787 Err(lookahead.error())
788 }
789 } else if lookahead.peek(Token![use]) {
790 input.parse().map(Item::Use)
791 } else if lookahead.peek(Token![static]) {
792 input.parse().map(Item::Static)
793 } else if lookahead.peek(Token![const]) {
794 ahead.parse::<Token![const]>()?;
795 let lookahead = ahead.lookahead1();
796 if lookahead.peek(Ident) {
797 input.parse().map(Item::Const)
798 } else if lookahead.peek(Token![unsafe])
799 || lookahead.peek(Token![async])
800 || lookahead.peek(Token![extern])
801 || lookahead.peek(Token![fn])
802 {
803 input.parse().map(Item::Fn)
804 } else {
805 Err(lookahead.error())
806 }
807 } else if lookahead.peek(Token![unsafe]) {
808 ahead.parse::<Token![unsafe]>()?;
809 let lookahead = ahead.lookahead1();
810 if lookahead.peek(Token![trait])
811 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
812 {
813 input.parse().map(Item::Trait)
814 } else if lookahead.peek(Token![impl ]) {
815 input.parse().map(Item::Impl)
816 } else if lookahead.peek(Token![async])
817 || lookahead.peek(Token![extern])
818 || lookahead.peek(Token![fn])
819 {
820 input.parse().map(Item::Fn)
821 } else {
822 Err(lookahead.error())
823 }
824 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
825 input.parse().map(Item::Fn)
826 } else if lookahead.peek(Token![mod]) {
827 input.parse().map(Item::Mod)
828 } else if lookahead.peek(Token![type]) {
829 input.parse().map(Item::Type)
830 } else if lookahead.peek(Token![existential]) {
831 input.parse().map(Item::Existential)
832 } else if lookahead.peek(Token![struct]) {
833 input.parse().map(Item::Struct)
834 } else if lookahead.peek(Token![enum]) {
835 input.parse().map(Item::Enum)
836 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
837 input.parse().map(Item::Union)
838 } else if lookahead.peek(Token![trait])
839 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
840 {
841 input.parse().map(Item::Trait)
842 } else if lookahead.peek(Token![impl ])
843 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
844 {
845 input.parse().map(Item::Impl)
846 } else if lookahead.peek(Token![macro]) {
847 input.parse().map(Item::Macro2)
848 } else if vis.is_inherited()
849 && (lookahead.peek(Ident)
850 || lookahead.peek(Token![self])
851 || lookahead.peek(Token![super])
852 || lookahead.peek(Token![extern])
853 || lookahead.peek(Token![crate])
854 || lookahead.peek(Token![::]))
855 {
856 input.parse().map(Item::Macro)
857 } else {
858 Err(lookahead.error())
859 }
860 }
861 }
Alex Crichton954046c2017-05-30 21:49:42 -0700862
David Tolnay3779bb72018-08-26 18:46:07 -0700863 impl Parse for ItemMacro {
864 fn parse(input: ParseStream) -> Result<Self> {
865 let attrs = input.call(Attribute::parse_outer)?;
866 let path = input.call(Path::parse_mod_style)?;
867 let bang_token: Token![!] = input.parse()?;
868 let ident: Option<Ident> = input.parse()?;
869 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700870 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700871 Some(input.parse()?)
872 } else {
873 None
874 };
875 Ok(ItemMacro {
876 attrs: attrs,
877 ident: ident,
878 mac: Macro {
879 path: path,
880 bang_token: bang_token,
881 delimiter: delimiter,
882 tts: tts,
883 },
884 semi_token: semi_token,
885 })
886 }
887 }
David Tolnayedf2b992016-09-23 20:43:45 -0700888
David Tolnay500d8322017-12-18 00:32:51 -0800889 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700890 impl Parse for ItemMacro2 {
891 fn parse(input: ParseStream) -> Result<Self> {
892 let args;
893 let body;
894 Ok(ItemMacro2 {
895 attrs: input.call(Attribute::parse_outer)?,
896 vis: input.parse()?,
897 macro_token: input.parse()?,
898 ident: input.parse()?,
899 paren_token: parenthesized!(args in input),
900 args: args.parse()?,
901 brace_token: braced!(body in input),
902 body: body.parse()?,
903 })
904 }
905 }
David Tolnay500d8322017-12-18 00:32:51 -0800906
David Tolnay3779bb72018-08-26 18:46:07 -0700907 impl Parse for ItemExternCrate {
908 fn parse(input: ParseStream) -> Result<Self> {
909 Ok(ItemExternCrate {
910 attrs: input.call(Attribute::parse_outer)?,
911 vis: input.parse()?,
912 extern_token: input.parse()?,
913 crate_token: input.parse()?,
914 ident: input.parse()?,
915 rename: {
916 if input.peek(Token![as]) {
917 let as_token: Token![as] = input.parse()?;
918 let rename: Ident = input.parse()?;
919 Some((as_token, rename))
920 } else {
921 None
922 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800923 },
David Tolnay3779bb72018-08-26 18:46:07 -0700924 semi_token: input.parse()?,
925 })
926 }
927 }
928
929 impl Parse for ItemUse {
930 fn parse(input: ParseStream) -> Result<Self> {
931 Ok(ItemUse {
932 attrs: input.call(Attribute::parse_outer)?,
933 vis: input.parse()?,
934 use_token: input.parse()?,
935 leading_colon: input.parse()?,
936 tree: input.call(use_tree)?,
937 semi_token: input.parse()?,
938 })
939 }
940 }
941
942 fn use_tree(input: ParseStream) -> Result<UseTree> {
943 let lookahead = input.lookahead1();
944 if lookahead.peek(Ident)
945 || lookahead.peek(Token![self])
946 || lookahead.peek(Token![super])
947 || lookahead.peek(Token![crate])
948 || lookahead.peek(Token![extern])
949 {
David Tolnay0dea1b92018-08-30 17:47:29 -0700950 let ident = input.call(Ident::parse_any)?;
David Tolnay3779bb72018-08-26 18:46:07 -0700951 if input.peek(Token![::]) {
952 Ok(UseTree::Path(UsePath {
953 ident: ident,
954 colon2_token: input.parse()?,
955 tree: Box::new(input.call(use_tree)?),
956 }))
957 } else if input.peek(Token![as]) {
958 Ok(UseTree::Rename(UseRename {
959 ident: ident,
960 as_token: input.parse()?,
961 rename: input.parse()?,
962 }))
963 } else {
964 Ok(UseTree::Name(UseName { ident: ident }))
965 }
966 } else if lookahead.peek(Token![*]) {
967 Ok(UseTree::Glob(UseGlob {
968 star_token: input.parse()?,
969 }))
970 } else if lookahead.peek(token::Brace) {
971 let content;
972 Ok(UseTree::Group(UseGroup {
973 brace_token: braced!(content in input),
974 items: content.parse_terminated(use_tree)?,
975 }))
976 } else {
977 Err(lookahead.error())
978 }
979 }
980
981 impl Parse for ItemStatic {
982 fn parse(input: ParseStream) -> Result<Self> {
983 Ok(ItemStatic {
984 attrs: input.call(Attribute::parse_outer)?,
985 vis: input.parse()?,
986 static_token: input.parse()?,
987 mutability: input.parse()?,
988 ident: input.parse()?,
989 colon_token: input.parse()?,
990 ty: input.parse()?,
991 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700992 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -0700993 semi_token: input.parse()?,
994 })
995 }
996 }
997
998 impl Parse for ItemConst {
999 fn parse(input: ParseStream) -> Result<Self> {
1000 Ok(ItemConst {
1001 attrs: input.call(Attribute::parse_outer)?,
1002 vis: input.parse()?,
1003 const_token: input.parse()?,
1004 ident: input.parse()?,
1005 colon_token: input.parse()?,
1006 ty: input.parse()?,
1007 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001008 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001009 semi_token: input.parse()?,
1010 })
1011 }
1012 }
1013
1014 impl Parse for ItemFn {
1015 fn parse(input: ParseStream) -> Result<Self> {
1016 let outer_attrs = input.call(Attribute::parse_outer)?;
1017 let vis: Visibility = input.parse()?;
1018 let constness: Option<Token![const]> = input.parse()?;
1019 let unsafety: Option<Token![unsafe]> = input.parse()?;
1020 let asyncness: Option<Token![async]> = input.parse()?;
1021 let abi: Option<Abi> = input.parse()?;
1022 let fn_token: Token![fn] = input.parse()?;
1023 let ident: Ident = input.parse()?;
1024 let generics: Generics = input.parse()?;
1025
1026 let content;
1027 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001028 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001029
1030 let output: ReturnType = input.parse()?;
1031 let where_clause: Option<WhereClause> = input.parse()?;
1032
1033 let content;
1034 let brace_token = braced!(content in input);
1035 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001036 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001037
1038 Ok(ItemFn {
1039 attrs: {
1040 let mut attrs = outer_attrs;
1041 attrs.extend(inner_attrs);
1042 attrs
1043 },
1044 vis: vis,
1045 constness: constness,
1046 unsafety: unsafety,
1047 asyncness: asyncness,
1048 abi: abi,
1049 ident: ident,
1050 decl: Box::new(FnDecl {
1051 fn_token: fn_token,
1052 paren_token: paren_token,
1053 inputs: inputs,
1054 output: output,
1055 variadic: None,
1056 generics: Generics {
1057 where_clause: where_clause,
1058 ..generics
1059 },
1060 }),
1061 block: Box::new(Block {
1062 brace_token: brace_token,
1063 stmts: stmts,
1064 }),
1065 })
1066 }
1067 }
David Tolnay42602292016-10-01 22:25:45 -07001068
David Tolnay2ad62c12018-08-26 19:00:35 -04001069 impl Parse for FnArg {
1070 fn parse(input: ParseStream) -> Result<Self> {
1071 if input.peek(Token![&]) {
1072 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001073 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1074 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001075 }
1076 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001077
David Tolnay2ad62c12018-08-26 19:00:35 -04001078 if input.peek(Token![mut]) || input.peek(Token![self]) {
1079 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001080 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1081 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001082 }
1083 }
1084
1085 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001086 let err = match ahead.call(arg_captured) {
1087 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001088 Err(err) => err,
1089 };
1090
1091 let ahead = input.fork();
1092 if ahead.parse::<Type>().is_ok() {
1093 return input.parse().map(FnArg::Ignored);
1094 }
1095
1096 Err(err)
1097 }
1098 }
1099
David Tolnay3779bb72018-08-26 18:46:07 -07001100 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1101 Ok(ArgSelfRef {
1102 and_token: input.parse()?,
1103 lifetime: input.parse()?,
1104 mutability: input.parse()?,
1105 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001106 })
David Tolnay3779bb72018-08-26 18:46:07 -07001107 }
David Tolnay35902302016-10-06 01:11:08 -07001108
David Tolnay3779bb72018-08-26 18:46:07 -07001109 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1110 Ok(ArgSelf {
1111 mutability: input.parse()?,
1112 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001113 })
David Tolnay3779bb72018-08-26 18:46:07 -07001114 }
1115
1116 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1117 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001118 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001119 colon_token: input.parse()?,
1120 ty: input.parse()?,
1121 })
1122 }
1123
1124 impl Parse for ItemMod {
1125 fn parse(input: ParseStream) -> Result<Self> {
1126 let outer_attrs = input.call(Attribute::parse_outer)?;
1127 let vis: Visibility = input.parse()?;
1128 let mod_token: Token![mod] = input.parse()?;
1129 let ident: Ident = input.parse()?;
1130
1131 let lookahead = input.lookahead1();
1132 if lookahead.peek(Token![;]) {
1133 Ok(ItemMod {
1134 attrs: outer_attrs,
1135 vis: vis,
1136 mod_token: mod_token,
1137 ident: ident,
1138 content: None,
1139 semi: Some(input.parse()?),
1140 })
1141 } else if lookahead.peek(token::Brace) {
1142 let content;
1143 let brace_token = braced!(content in input);
1144 let inner_attrs = content.call(Attribute::parse_inner)?;
1145
1146 let mut items = Vec::new();
1147 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001148 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001149 }
1150
1151 Ok(ItemMod {
1152 attrs: {
1153 let mut attrs = outer_attrs;
1154 attrs.extend(inner_attrs);
1155 attrs
1156 },
1157 vis: vis,
1158 mod_token: mod_token,
1159 ident: ident,
1160 content: Some((brace_token, items)),
1161 semi: None,
1162 })
1163 } else {
1164 Err(lookahead.error())
1165 }
1166 }
1167 }
1168
1169 impl Parse for ItemForeignMod {
1170 fn parse(input: ParseStream) -> Result<Self> {
1171 let outer_attrs = input.call(Attribute::parse_outer)?;
1172 let abi: Abi = input.parse()?;
1173
1174 let content;
1175 let brace_token = braced!(content in input);
1176 let inner_attrs = content.call(Attribute::parse_inner)?;
1177 let mut items = Vec::new();
1178 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001179 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001180 }
1181
1182 Ok(ItemForeignMod {
1183 attrs: {
1184 let mut attrs = outer_attrs;
1185 attrs.extend(inner_attrs);
1186 attrs
1187 },
1188 abi: abi,
1189 brace_token: brace_token,
1190 items: items,
1191 })
1192 }
1193 }
David Tolnay35902302016-10-06 01:11:08 -07001194
David Tolnay6a170ce2018-08-26 22:29:24 -07001195 impl Parse for ForeignItem {
1196 fn parse(input: ParseStream) -> Result<Self> {
1197 let ahead = input.fork();
1198 ahead.call(Attribute::parse_outer)?;
1199 let vis: Visibility = ahead.parse()?;
1200
1201 let lookahead = ahead.lookahead1();
1202 if lookahead.peek(Token![fn]) {
1203 input.parse().map(ForeignItem::Fn)
1204 } else if lookahead.peek(Token![static]) {
1205 input.parse().map(ForeignItem::Static)
1206 } else if lookahead.peek(Token![type]) {
1207 input.parse().map(ForeignItem::Type)
1208 } else if vis.is_inherited()
1209 && (lookahead.peek(Ident)
1210 || lookahead.peek(Token![self])
1211 || lookahead.peek(Token![super])
1212 || lookahead.peek(Token![extern])
1213 || lookahead.peek(Token![crate])
1214 || lookahead.peek(Token![::]))
1215 {
1216 input.parse().map(ForeignItem::Macro)
1217 } else {
1218 Err(lookahead.error())
1219 }
1220 }
1221 }
David Tolnay35902302016-10-06 01:11:08 -07001222
David Tolnay3779bb72018-08-26 18:46:07 -07001223 impl Parse for ForeignItemFn {
1224 fn parse(input: ParseStream) -> Result<Self> {
1225 let attrs = input.call(Attribute::parse_outer)?;
1226 let vis: Visibility = input.parse()?;
1227 let fn_token: Token![fn] = input.parse()?;
1228 let ident: Ident = input.parse()?;
1229 let generics: Generics = input.parse()?;
1230
1231 let content;
1232 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001233 let mut inputs = Punctuated::new();
1234 while !content.is_empty() && !content.peek(Token![...]) {
1235 inputs.push_value(content.parse()?);
1236 if content.is_empty() {
1237 break;
1238 }
1239 inputs.push_punct(content.parse()?);
1240 }
David Tolnay3779bb72018-08-26 18:46:07 -07001241 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1242 content.parse()?
1243 } else {
1244 None
1245 };
1246
1247 let output: ReturnType = input.parse()?;
1248 let where_clause: Option<WhereClause> = input.parse()?;
1249 let semi_token: Token![;] = input.parse()?;
1250
1251 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001252 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001253 vis: vis,
1254 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001255 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001256 fn_token: fn_token,
1257 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001258 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001259 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001260 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001261 generics: Generics {
1262 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001263 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001264 },
1265 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001266 semi_token: semi_token,
1267 })
1268 }
1269 }
David Tolnay35902302016-10-06 01:11:08 -07001270
David Tolnay3779bb72018-08-26 18:46:07 -07001271 impl Parse for ForeignItemStatic {
1272 fn parse(input: ParseStream) -> Result<Self> {
1273 Ok(ForeignItemStatic {
1274 attrs: input.call(Attribute::parse_outer)?,
1275 vis: input.parse()?,
1276 static_token: input.parse()?,
1277 mutability: input.parse()?,
1278 ident: input.parse()?,
1279 colon_token: input.parse()?,
1280 ty: input.parse()?,
1281 semi_token: input.parse()?,
1282 })
1283 }
1284 }
David Tolnay35902302016-10-06 01:11:08 -07001285
David Tolnay3779bb72018-08-26 18:46:07 -07001286 impl Parse for ForeignItemType {
1287 fn parse(input: ParseStream) -> Result<Self> {
1288 Ok(ForeignItemType {
1289 attrs: input.call(Attribute::parse_outer)?,
1290 vis: input.parse()?,
1291 type_token: input.parse()?,
1292 ident: input.parse()?,
1293 semi_token: input.parse()?,
1294 })
1295 }
1296 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001297
David Tolnay3779bb72018-08-26 18:46:07 -07001298 impl Parse for ForeignItemMacro {
1299 fn parse(input: ParseStream) -> Result<Self> {
1300 let attrs = input.call(Attribute::parse_outer)?;
1301 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001302 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001303 None
1304 } else {
1305 Some(input.parse()?)
1306 };
1307 Ok(ForeignItemMacro {
1308 attrs: attrs,
1309 mac: mac,
1310 semi_token: semi_token,
1311 })
1312 }
1313 }
David Tolnay78572112018-08-01 00:36:18 -07001314
David Tolnay3779bb72018-08-26 18:46:07 -07001315 impl Parse for ItemType {
1316 fn parse(input: ParseStream) -> Result<Self> {
1317 Ok(ItemType {
1318 attrs: input.call(Attribute::parse_outer)?,
1319 vis: input.parse()?,
1320 type_token: input.parse()?,
1321 ident: input.parse()?,
1322 generics: {
1323 let mut generics: Generics = input.parse()?;
1324 generics.where_clause = input.parse()?;
1325 generics
1326 },
1327 eq_token: input.parse()?,
1328 ty: input.parse()?,
1329 semi_token: input.parse()?,
1330 })
1331 }
1332 }
David Tolnay3cf52982016-10-01 17:11:37 -07001333
David Tolnay3779bb72018-08-26 18:46:07 -07001334 impl Parse for ItemExistential {
1335 fn parse(input: ParseStream) -> Result<Self> {
1336 Ok(ItemExistential {
1337 attrs: input.call(Attribute::parse_outer)?,
1338 vis: input.parse()?,
1339 existential_token: input.parse()?,
1340 type_token: input.parse()?,
1341 ident: input.parse()?,
1342 generics: {
1343 let mut generics: Generics = input.parse()?;
1344 generics.where_clause = input.parse()?;
1345 generics
1346 },
1347 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001348 bounds: {
1349 let mut bounds = Punctuated::new();
1350 while !input.peek(Token![;]) {
1351 if !bounds.is_empty() {
1352 bounds.push_punct(input.parse()?);
1353 }
1354 bounds.push_value(input.parse()?);
1355 }
1356 bounds
1357 },
David Tolnay3779bb72018-08-26 18:46:07 -07001358 semi_token: input.parse()?,
1359 })
1360 }
1361 }
David Tolnay758ee132018-08-21 21:29:40 -04001362
David Tolnay6a170ce2018-08-26 22:29:24 -07001363 impl Parse for ItemStruct {
1364 fn parse(input: ParseStream) -> Result<Self> {
1365 let attrs = input.call(Attribute::parse_outer)?;
1366 let vis = input.parse::<Visibility>()?;
1367 let struct_token = input.parse::<Token![struct]>()?;
1368 let ident = input.parse::<Ident>()?;
1369 let generics = input.parse::<Generics>()?;
1370 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1371 Ok(ItemStruct {
1372 attrs: attrs,
1373 vis: vis,
1374 struct_token: struct_token,
1375 ident: ident,
1376 generics: Generics {
1377 where_clause: where_clause,
1378 ..generics
1379 },
1380 fields: fields,
1381 semi_token: semi_token,
1382 })
1383 }
1384 }
David Tolnay42602292016-10-01 22:25:45 -07001385
David Tolnay6a170ce2018-08-26 22:29:24 -07001386 impl Parse for ItemEnum {
1387 fn parse(input: ParseStream) -> Result<Self> {
1388 let attrs = input.call(Attribute::parse_outer)?;
1389 let vis = input.parse::<Visibility>()?;
1390 let enum_token = input.parse::<Token![enum]>()?;
1391 let ident = input.parse::<Ident>()?;
1392 let generics = input.parse::<Generics>()?;
1393 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1394 Ok(ItemEnum {
1395 attrs: attrs,
1396 vis: vis,
1397 enum_token: enum_token,
1398 ident: ident,
1399 generics: Generics {
1400 where_clause: where_clause,
1401 ..generics
1402 },
1403 brace_token: brace_token,
1404 variants: variants,
1405 })
1406 }
1407 }
David Tolnay4c614be2017-11-10 00:02:38 -08001408
David Tolnay6a170ce2018-08-26 22:29:24 -07001409 impl Parse for ItemUnion {
1410 fn parse(input: ParseStream) -> Result<Self> {
1411 let attrs = input.call(Attribute::parse_outer)?;
1412 let vis = input.parse::<Visibility>()?;
1413 let union_token = input.parse::<Token![union]>()?;
1414 let ident = input.parse::<Ident>()?;
1415 let generics = input.parse::<Generics>()?;
1416 let (where_clause, fields) = derive::parsing::data_union(input)?;
1417 Ok(ItemUnion {
1418 attrs: attrs,
1419 vis: vis,
1420 union_token: union_token,
1421 ident: ident,
1422 generics: Generics {
1423 where_clause: where_clause,
1424 ..generics
1425 },
1426 fields: fields,
1427 })
1428 }
1429 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001430
David Tolnay6a170ce2018-08-26 22:29:24 -07001431 impl Parse for ItemTrait {
1432 fn parse(input: ParseStream) -> Result<Self> {
1433 let attrs = input.call(Attribute::parse_outer)?;
1434 let vis: Visibility = input.parse()?;
1435 let unsafety: Option<Token![unsafe]> = input.parse()?;
1436 let auto_token: Option<Token![auto]> = input.parse()?;
1437 let trait_token: Token![trait] = input.parse()?;
1438 let ident: Ident = input.parse()?;
1439 let mut generics: Generics = input.parse()?;
1440 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001441
1442 let mut supertraits = Punctuated::new();
1443 if colon_token.is_some() {
1444 while !input.peek(Token![where]) && !input.peek(token::Brace) {
1445 if !supertraits.is_empty() {
1446 supertraits.push_punct(input.parse()?);
1447 }
1448 supertraits.push_value(input.parse()?);
1449 }
1450 }
1451
David Tolnay6a170ce2018-08-26 22:29:24 -07001452 generics.where_clause = input.parse()?;
1453
1454 let content;
1455 let brace_token = braced!(content in input);
1456 let mut items = Vec::new();
1457 while !content.is_empty() {
1458 items.push(content.parse()?);
1459 }
1460
1461 Ok(ItemTrait {
1462 attrs: attrs,
1463 vis: vis,
1464 unsafety: unsafety,
1465 auto_token: auto_token,
1466 trait_token: trait_token,
1467 ident: ident,
1468 generics: generics,
1469 colon_token: colon_token,
1470 supertraits: supertraits,
1471 brace_token: brace_token,
1472 items: items,
1473 })
1474 }
1475 }
1476
1477 impl Parse for TraitItem {
1478 fn parse(input: ParseStream) -> Result<Self> {
1479 let ahead = input.fork();
1480 ahead.call(Attribute::parse_outer)?;
1481
1482 let lookahead = ahead.lookahead1();
1483 if lookahead.peek(Token![const]) {
1484 ahead.parse::<Token![const]>()?;
1485 let lookahead = ahead.lookahead1();
1486 if lookahead.peek(Ident) {
1487 input.parse().map(TraitItem::Const)
1488 } else if lookahead.peek(Token![unsafe])
1489 || lookahead.peek(Token![extern])
1490 || lookahead.peek(Token![fn])
1491 {
1492 input.parse().map(TraitItem::Method)
1493 } else {
1494 Err(lookahead.error())
1495 }
1496 } else if lookahead.peek(Token![unsafe])
1497 || lookahead.peek(Token![extern])
1498 || lookahead.peek(Token![fn])
1499 {
1500 input.parse().map(TraitItem::Method)
1501 } else if lookahead.peek(Token![type]) {
1502 input.parse().map(TraitItem::Type)
1503 } else if lookahead.peek(Ident)
1504 || lookahead.peek(Token![self])
1505 || lookahead.peek(Token![super])
1506 || lookahead.peek(Token![extern])
1507 || lookahead.peek(Token![crate])
1508 || lookahead.peek(Token![::])
1509 {
1510 input.parse().map(TraitItem::Macro)
1511 } else {
1512 Err(lookahead.error())
1513 }
1514 }
1515 }
1516
1517 impl Parse for TraitItemConst {
1518 fn parse(input: ParseStream) -> Result<Self> {
1519 Ok(TraitItemConst {
1520 attrs: input.call(Attribute::parse_outer)?,
1521 const_token: input.parse()?,
1522 ident: input.parse()?,
1523 colon_token: input.parse()?,
1524 ty: input.parse()?,
1525 default: {
1526 if input.peek(Token![=]) {
1527 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001528 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001529 Some((eq_token, default))
1530 } else {
1531 None
1532 }
1533 },
1534 semi_token: input.parse()?,
1535 })
1536 }
1537 }
1538
1539 impl Parse for TraitItemMethod {
1540 fn parse(input: ParseStream) -> Result<Self> {
1541 let outer_attrs = input.call(Attribute::parse_outer)?;
1542 let constness: Option<Token![const]> = input.parse()?;
1543 let unsafety: Option<Token![unsafe]> = input.parse()?;
1544 let abi: Option<Abi> = input.parse()?;
1545 let fn_token: Token![fn] = input.parse()?;
1546 let ident: Ident = input.parse()?;
1547 let generics: Generics = input.parse()?;
1548
1549 let content;
1550 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001551 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001552
1553 let output: ReturnType = input.parse()?;
1554 let where_clause: Option<WhereClause> = input.parse()?;
1555
1556 let lookahead = input.lookahead1();
1557 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1558 let content;
1559 let brace_token = braced!(content in input);
1560 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001561 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001562 (Some(brace_token), inner_attrs, stmts, None)
1563 } else if lookahead.peek(Token![;]) {
1564 let semi_token: Token![;] = input.parse()?;
1565 (None, Vec::new(), Vec::new(), Some(semi_token))
1566 } else {
1567 return Err(lookahead.error());
1568 };
1569
1570 Ok(TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001571 attrs: {
1572 let mut attrs = outer_attrs;
1573 attrs.extend(inner_attrs);
1574 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001575 },
David Tolnayda705bd2017-11-10 21:58:05 -08001576 sig: MethodSig {
1577 constness: constness,
1578 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001579 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001580 abi: abi,
1581 ident: ident,
1582 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001583 fn_token: fn_token,
1584 paren_token: paren_token,
1585 inputs: inputs,
1586 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001587 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001588 generics: Generics {
1589 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001590 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001591 },
1592 },
David Tolnayda705bd2017-11-10 21:58:05 -08001593 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001594 default: brace_token.map(|brace_token| Block {
1595 brace_token: brace_token,
1596 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001597 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001598 semi_token: semi_token,
1599 })
1600 }
1601 }
1602
1603 impl Parse for TraitItemType {
1604 fn parse(input: ParseStream) -> Result<Self> {
1605 let attrs = input.call(Attribute::parse_outer)?;
1606 let type_token: Token![type] = input.parse()?;
1607 let ident: Ident = input.parse()?;
1608 let mut generics: Generics = input.parse()?;
1609 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001610
1611 let mut bounds = Punctuated::new();
1612 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001613 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1614 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001615 if !bounds.is_empty() {
1616 bounds.push_punct(input.parse()?);
1617 }
1618 bounds.push_value(input.parse()?);
1619 }
1620 }
1621
David Tolnay6a170ce2018-08-26 22:29:24 -07001622 generics.where_clause = input.parse()?;
1623 let default = if input.peek(Token![=]) {
1624 let eq_token: Token![=] = input.parse()?;
1625 let default: Type = input.parse()?;
1626 Some((eq_token, default))
1627 } else {
1628 None
1629 };
1630 let semi_token: Token![;] = input.parse()?;
1631
1632 Ok(TraitItemType {
1633 attrs: attrs,
1634 type_token: type_token,
1635 ident: ident,
1636 generics: generics,
1637 colon_token: colon_token,
1638 bounds: bounds,
1639 default: default,
1640 semi_token: semi_token,
1641 })
1642 }
1643 }
1644
1645 impl Parse for TraitItemMacro {
1646 fn parse(input: ParseStream) -> Result<Self> {
1647 let attrs = input.call(Attribute::parse_outer)?;
1648 let mac: Macro = input.parse()?;
1649 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1650 None
1651 } else {
1652 Some(input.parse()?)
1653 };
1654 Ok(TraitItemMacro {
1655 attrs: attrs,
1656 mac: mac,
1657 semi_token: semi_token,
1658 })
1659 }
1660 }
1661
1662 impl Parse for ItemImpl {
1663 fn parse(input: ParseStream) -> Result<Self> {
1664 let outer_attrs = input.call(Attribute::parse_outer)?;
1665 let defaultness: Option<Token![default]> = input.parse()?;
1666 let unsafety: Option<Token![unsafe]> = input.parse()?;
1667 let impl_token: Token![impl ] = input.parse()?;
1668 let generics: Generics = input.parse()?;
1669 let trait_ = {
1670 let ahead = input.fork();
1671 if ahead.parse::<Option<Token![!]>>().is_ok()
1672 && ahead.parse::<Path>().is_ok()
1673 && ahead.parse::<Token![for]>().is_ok()
1674 {
1675 let polarity: Option<Token![!]> = input.parse()?;
1676 let path: Path = input.parse()?;
1677 let for_token: Token![for] = input.parse()?;
1678 Some((polarity, path, for_token))
1679 } else {
1680 None
1681 }
1682 };
1683 let self_ty: Type = input.parse()?;
1684 let where_clause: Option<WhereClause> = input.parse()?;
1685
1686 let content;
1687 let brace_token = braced!(content in input);
1688 let inner_attrs = content.call(Attribute::parse_inner)?;
1689
1690 let mut items = Vec::new();
1691 while !content.is_empty() {
1692 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001693 }
David Tolnay0aecb732016-10-03 23:03:50 -07001694
David Tolnay6a170ce2018-08-26 22:29:24 -07001695 Ok(ItemImpl {
1696 attrs: {
1697 let mut attrs = outer_attrs;
1698 attrs.extend(inner_attrs);
1699 attrs
1700 },
1701 defaultness: defaultness,
1702 unsafety: unsafety,
1703 impl_token: impl_token,
1704 generics: Generics {
1705 where_clause: where_clause,
1706 ..generics
1707 },
1708 trait_: trait_,
1709 self_ty: Box::new(self_ty),
1710 brace_token: brace_token,
1711 items: items,
1712 })
1713 }
1714 }
David Tolnay0aecb732016-10-03 23:03:50 -07001715
David Tolnay6a170ce2018-08-26 22:29:24 -07001716 impl Parse for ImplItem {
1717 fn parse(input: ParseStream) -> Result<Self> {
1718 let ahead = input.fork();
1719 ahead.call(Attribute::parse_outer)?;
1720 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001721
David Tolnay6a170ce2018-08-26 22:29:24 -07001722 let mut lookahead = ahead.lookahead1();
1723 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1724 let defaultness: Token![default] = ahead.parse()?;
1725 lookahead = ahead.lookahead1();
1726 Some(defaultness)
1727 } else {
1728 None
1729 };
David Tolnay4c9be372016-10-06 00:47:37 -07001730
David Tolnay6a170ce2018-08-26 22:29:24 -07001731 if lookahead.peek(Token![const]) {
1732 ahead.parse::<Token![const]>()?;
1733 let lookahead = ahead.lookahead1();
1734 if lookahead.peek(Ident) {
1735 input.parse().map(ImplItem::Const)
1736 } else if lookahead.peek(Token![unsafe])
1737 || lookahead.peek(Token![async])
1738 || lookahead.peek(Token![extern])
1739 || lookahead.peek(Token![fn])
1740 {
1741 input.parse().map(ImplItem::Method)
1742 } else {
1743 Err(lookahead.error())
1744 }
1745 } else if lookahead.peek(Token![unsafe])
1746 || lookahead.peek(Token![async])
1747 || lookahead.peek(Token![extern])
1748 || lookahead.peek(Token![fn])
1749 {
1750 input.parse().map(ImplItem::Method)
1751 } else if lookahead.peek(Token![type]) {
1752 input.parse().map(ImplItem::Type)
1753 } else if vis.is_inherited()
1754 && defaultness.is_none()
1755 && lookahead.peek(Token![existential])
1756 {
1757 input.parse().map(ImplItem::Existential)
1758 } else if vis.is_inherited()
1759 && defaultness.is_none()
1760 && (lookahead.peek(Ident)
1761 || lookahead.peek(Token![self])
1762 || lookahead.peek(Token![super])
1763 || lookahead.peek(Token![extern])
1764 || lookahead.peek(Token![crate])
1765 || lookahead.peek(Token![::]))
1766 {
1767 input.parse().map(ImplItem::Macro)
1768 } else {
1769 Err(lookahead.error())
1770 }
1771 }
1772 }
David Tolnay4c9be372016-10-06 00:47:37 -07001773
David Tolnay3779bb72018-08-26 18:46:07 -07001774 impl Parse for ImplItemConst {
1775 fn parse(input: ParseStream) -> Result<Self> {
1776 Ok(ImplItemConst {
1777 attrs: input.call(Attribute::parse_outer)?,
1778 vis: input.parse()?,
1779 defaultness: input.parse()?,
1780 const_token: input.parse()?,
1781 ident: input.parse()?,
1782 colon_token: input.parse()?,
1783 ty: input.parse()?,
1784 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001785 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001786 semi_token: input.parse()?,
1787 })
1788 }
1789 }
David Tolnay4c9be372016-10-06 00:47:37 -07001790
David Tolnay6a170ce2018-08-26 22:29:24 -07001791 impl Parse for ImplItemMethod {
1792 fn parse(input: ParseStream) -> Result<Self> {
1793 let outer_attrs = input.call(Attribute::parse_outer)?;
1794 let vis: Visibility = input.parse()?;
1795 let defaultness: Option<Token![default]> = input.parse()?;
1796 let constness: Option<Token![const]> = input.parse()?;
1797 let unsafety: Option<Token![unsafe]> = input.parse()?;
1798 let asyncness: Option<Token![async]> = input.parse()?;
1799 let abi: Option<Abi> = input.parse()?;
1800 let fn_token: Token![fn] = input.parse()?;
1801 let ident: Ident = input.parse()?;
1802 let generics: Generics = input.parse()?;
1803
1804 let content;
1805 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001806 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001807
1808 let output: ReturnType = input.parse()?;
1809 let where_clause: Option<WhereClause> = input.parse()?;
1810
1811 let content;
1812 let brace_token = braced!(content in input);
1813 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001814 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001815
1816 Ok(ImplItemMethod {
1817 attrs: {
1818 let mut attrs = outer_attrs;
1819 attrs.extend(inner_attrs);
1820 attrs
David Tolnay4c9be372016-10-06 00:47:37 -07001821 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001822 vis: vis,
1823 defaultness: defaultness,
1824 sig: MethodSig {
1825 constness: constness,
1826 unsafety: unsafety,
1827 asyncness: asyncness,
1828 abi: abi,
1829 ident: ident,
1830 decl: FnDecl {
1831 fn_token: fn_token,
1832 paren_token: paren_token,
1833 inputs: inputs,
1834 output: output,
1835 variadic: None,
1836 generics: Generics {
1837 where_clause: where_clause,
1838 ..generics
1839 },
1840 },
1841 },
1842 block: Block {
1843 brace_token: brace_token,
1844 stmts: stmts,
1845 },
1846 })
1847 }
1848 }
David Tolnay4c9be372016-10-06 00:47:37 -07001849
David Tolnay3779bb72018-08-26 18:46:07 -07001850 impl Parse for ImplItemType {
1851 fn parse(input: ParseStream) -> Result<Self> {
1852 Ok(ImplItemType {
1853 attrs: input.call(Attribute::parse_outer)?,
1854 vis: input.parse()?,
1855 defaultness: input.parse()?,
1856 type_token: input.parse()?,
1857 ident: input.parse()?,
1858 generics: {
1859 let mut generics: Generics = input.parse()?;
1860 generics.where_clause = input.parse()?;
1861 generics
1862 },
1863 eq_token: input.parse()?,
1864 ty: input.parse()?,
1865 semi_token: input.parse()?,
1866 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001867 }
David Tolnay3779bb72018-08-26 18:46:07 -07001868 }
David Tolnay758ee132018-08-21 21:29:40 -04001869
David Tolnay3779bb72018-08-26 18:46:07 -07001870 impl Parse for ImplItemExistential {
1871 fn parse(input: ParseStream) -> Result<Self> {
1872 let ety: ItemExistential = input.parse()?;
1873 Ok(ImplItemExistential {
1874 attrs: ety.attrs,
1875 existential_token: ety.existential_token,
1876 type_token: ety.type_token,
1877 ident: ety.ident,
1878 generics: ety.generics,
1879 colon_token: ety.colon_token,
1880 bounds: ety.bounds,
1881 semi_token: ety.semi_token,
1882 })
1883 }
1884 }
1885
1886 impl Parse for ImplItemMacro {
1887 fn parse(input: ParseStream) -> Result<Self> {
1888 let attrs = input.call(Attribute::parse_outer)?;
1889 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001890 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001891 None
1892 } else {
1893 Some(input.parse()?)
1894 };
1895 Ok(ImplItemMacro {
1896 attrs: attrs,
1897 mac: mac,
1898 semi_token: semi_token,
1899 })
1900 }
1901 }
David Tolnay4c9be372016-10-06 00:47:37 -07001902
David Tolnay6a170ce2018-08-26 22:29:24 -07001903 impl Visibility {
1904 fn is_inherited(&self) -> bool {
1905 match *self {
1906 Visibility::Inherited => true,
1907 _ => false,
1908 }
1909 }
1910 }
1911
1912 impl MacroDelimiter {
1913 fn is_brace(&self) -> bool {
1914 match *self {
1915 MacroDelimiter::Brace(_) => true,
1916 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1917 }
David Tolnay57292da2017-12-27 21:03:33 -05001918 }
1919 }
David Tolnayedf2b992016-09-23 20:43:45 -07001920}
David Tolnay4a51dc72016-10-01 00:40:31 -07001921
1922#[cfg(feature = "printing")]
1923mod printing {
1924 use super::*;
1925 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001926 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001927 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001928
David Tolnay1bfa7332017-11-11 12:41:20 -08001929 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001930 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001931 tokens.append_all(self.attrs.outer());
1932 self.vis.to_tokens(tokens);
1933 self.extern_token.to_tokens(tokens);
1934 self.crate_token.to_tokens(tokens);
1935 self.ident.to_tokens(tokens);
1936 if let Some((ref as_token, ref rename)) = self.rename {
1937 as_token.to_tokens(tokens);
1938 rename.to_tokens(tokens);
1939 }
1940 self.semi_token.to_tokens(tokens);
1941 }
1942 }
1943
1944 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001945 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001946 tokens.append_all(self.attrs.outer());
1947 self.vis.to_tokens(tokens);
1948 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001949 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001950 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001951 self.semi_token.to_tokens(tokens);
1952 }
1953 }
1954
1955 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001956 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001957 tokens.append_all(self.attrs.outer());
1958 self.vis.to_tokens(tokens);
1959 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001960 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001961 self.ident.to_tokens(tokens);
1962 self.colon_token.to_tokens(tokens);
1963 self.ty.to_tokens(tokens);
1964 self.eq_token.to_tokens(tokens);
1965 self.expr.to_tokens(tokens);
1966 self.semi_token.to_tokens(tokens);
1967 }
1968 }
1969
1970 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001971 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001972 tokens.append_all(self.attrs.outer());
1973 self.vis.to_tokens(tokens);
1974 self.const_token.to_tokens(tokens);
1975 self.ident.to_tokens(tokens);
1976 self.colon_token.to_tokens(tokens);
1977 self.ty.to_tokens(tokens);
1978 self.eq_token.to_tokens(tokens);
1979 self.expr.to_tokens(tokens);
1980 self.semi_token.to_tokens(tokens);
1981 }
1982 }
1983
1984 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001985 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001986 tokens.append_all(self.attrs.outer());
1987 self.vis.to_tokens(tokens);
1988 self.constness.to_tokens(tokens);
1989 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001990 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001991 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001992 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001993 self.block.brace_token.surround(tokens, |tokens| {
1994 tokens.append_all(self.attrs.inner());
1995 tokens.append_all(&self.block.stmts);
1996 });
1997 }
1998 }
1999
2000 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002001 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002002 tokens.append_all(self.attrs.outer());
2003 self.vis.to_tokens(tokens);
2004 self.mod_token.to_tokens(tokens);
2005 self.ident.to_tokens(tokens);
2006 if let Some((ref brace, ref items)) = self.content {
2007 brace.surround(tokens, |tokens| {
2008 tokens.append_all(self.attrs.inner());
2009 tokens.append_all(items);
2010 });
2011 } else {
2012 TokensOrDefault(&self.semi).to_tokens(tokens);
2013 }
2014 }
2015 }
2016
2017 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002018 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002019 tokens.append_all(self.attrs.outer());
2020 self.abi.to_tokens(tokens);
2021 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002022 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002023 tokens.append_all(&self.items);
2024 });
2025 }
2026 }
2027
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002028 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002029 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002030 tokens.append_all(self.attrs.outer());
2031 self.vis.to_tokens(tokens);
2032 self.type_token.to_tokens(tokens);
2033 self.ident.to_tokens(tokens);
2034 self.generics.to_tokens(tokens);
2035 self.generics.where_clause.to_tokens(tokens);
2036 self.eq_token.to_tokens(tokens);
2037 self.ty.to_tokens(tokens);
2038 self.semi_token.to_tokens(tokens);
2039 }
2040 }
2041
David Tolnaybb82ef02018-08-24 20:15:45 -04002042 impl ToTokens for ItemExistential {
2043 fn to_tokens(&self, tokens: &mut TokenStream) {
2044 tokens.append_all(self.attrs.outer());
2045 self.vis.to_tokens(tokens);
2046 self.existential_token.to_tokens(tokens);
2047 self.type_token.to_tokens(tokens);
2048 self.ident.to_tokens(tokens);
2049 self.generics.to_tokens(tokens);
2050 self.generics.where_clause.to_tokens(tokens);
2051 if !self.bounds.is_empty() {
2052 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2053 self.bounds.to_tokens(tokens);
2054 }
2055 self.semi_token.to_tokens(tokens);
2056 }
2057 }
2058
David Tolnay1bfa7332017-11-11 12:41:20 -08002059 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002060 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002061 tokens.append_all(self.attrs.outer());
2062 self.vis.to_tokens(tokens);
2063 self.enum_token.to_tokens(tokens);
2064 self.ident.to_tokens(tokens);
2065 self.generics.to_tokens(tokens);
2066 self.generics.where_clause.to_tokens(tokens);
2067 self.brace_token.surround(tokens, |tokens| {
2068 self.variants.to_tokens(tokens);
2069 });
2070 }
2071 }
2072
2073 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002074 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002075 tokens.append_all(self.attrs.outer());
2076 self.vis.to_tokens(tokens);
2077 self.struct_token.to_tokens(tokens);
2078 self.ident.to_tokens(tokens);
2079 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002080 match self.fields {
2081 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002082 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002083 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002084 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002085 Fields::Unnamed(ref fields) => {
2086 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002087 self.generics.where_clause.to_tokens(tokens);
2088 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002089 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002090 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002091 self.generics.where_clause.to_tokens(tokens);
2092 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002093 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002094 }
2095 }
2096 }
2097
2098 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002099 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002100 tokens.append_all(self.attrs.outer());
2101 self.vis.to_tokens(tokens);
2102 self.union_token.to_tokens(tokens);
2103 self.ident.to_tokens(tokens);
2104 self.generics.to_tokens(tokens);
2105 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002106 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002107 }
2108 }
2109
2110 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002111 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002112 tokens.append_all(self.attrs.outer());
2113 self.vis.to_tokens(tokens);
2114 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002115 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002116 self.trait_token.to_tokens(tokens);
2117 self.ident.to_tokens(tokens);
2118 self.generics.to_tokens(tokens);
2119 if !self.supertraits.is_empty() {
2120 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2121 self.supertraits.to_tokens(tokens);
2122 }
2123 self.generics.where_clause.to_tokens(tokens);
2124 self.brace_token.surround(tokens, |tokens| {
2125 tokens.append_all(&self.items);
2126 });
2127 }
2128 }
2129
David Tolnay1bfa7332017-11-11 12:41:20 -08002130 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002131 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002132 tokens.append_all(self.attrs.outer());
2133 self.defaultness.to_tokens(tokens);
2134 self.unsafety.to_tokens(tokens);
2135 self.impl_token.to_tokens(tokens);
2136 self.generics.to_tokens(tokens);
2137 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2138 polarity.to_tokens(tokens);
2139 path.to_tokens(tokens);
2140 for_token.to_tokens(tokens);
2141 }
2142 self.self_ty.to_tokens(tokens);
2143 self.generics.where_clause.to_tokens(tokens);
2144 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002145 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002146 tokens.append_all(&self.items);
2147 });
2148 }
2149 }
2150
2151 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002152 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002153 tokens.append_all(self.attrs.outer());
2154 self.mac.path.to_tokens(tokens);
2155 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002156 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002157 match self.mac.delimiter {
2158 MacroDelimiter::Paren(ref paren) => {
2159 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2160 }
2161 MacroDelimiter::Brace(ref brace) => {
2162 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2163 }
2164 MacroDelimiter::Bracket(ref bracket) => {
2165 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2166 }
2167 }
David Tolnay57292da2017-12-27 21:03:33 -05002168 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002169 }
2170 }
David Tolnay42602292016-10-01 22:25:45 -07002171
David Tolnay500d8322017-12-18 00:32:51 -08002172 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002173 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002174 tokens.append_all(self.attrs.outer());
2175 self.vis.to_tokens(tokens);
2176 self.macro_token.to_tokens(tokens);
2177 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002178 self.paren_token.surround(tokens, |tokens| {
2179 self.args.to_tokens(tokens);
2180 });
2181 self.brace_token.surround(tokens, |tokens| {
2182 self.body.to_tokens(tokens);
2183 });
David Tolnay500d8322017-12-18 00:32:51 -08002184 }
2185 }
2186
David Tolnay2ae520a2017-12-29 11:19:50 -05002187 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002188 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002189 self.tts.to_tokens(tokens);
2190 }
2191 }
2192
David Tolnay5f332a92017-12-26 00:42:45 -05002193 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002194 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002195 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002196 self.colon2_token.to_tokens(tokens);
2197 self.tree.to_tokens(tokens);
2198 }
2199 }
2200
2201 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002202 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002203 self.ident.to_tokens(tokens);
2204 }
2205 }
2206
2207 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002208 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002209 self.ident.to_tokens(tokens);
2210 self.as_token.to_tokens(tokens);
2211 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002212 }
2213 }
2214
David Tolnay5f332a92017-12-26 00:42:45 -05002215 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002216 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002217 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002218 }
2219 }
2220
David Tolnayd97a7d22018-03-31 19:17:01 +02002221 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002222 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002223 self.brace_token.surround(tokens, |tokens| {
2224 self.items.to_tokens(tokens);
2225 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002226 }
2227 }
2228
David Tolnay1bfa7332017-11-11 12:41:20 -08002229 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002230 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002231 tokens.append_all(self.attrs.outer());
2232 self.const_token.to_tokens(tokens);
2233 self.ident.to_tokens(tokens);
2234 self.colon_token.to_tokens(tokens);
2235 self.ty.to_tokens(tokens);
2236 if let Some((ref eq_token, ref default)) = self.default {
2237 eq_token.to_tokens(tokens);
2238 default.to_tokens(tokens);
2239 }
2240 self.semi_token.to_tokens(tokens);
2241 }
2242 }
2243
2244 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002245 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002246 tokens.append_all(self.attrs.outer());
2247 self.sig.to_tokens(tokens);
2248 match self.default {
2249 Some(ref block) => {
2250 block.brace_token.surround(tokens, |tokens| {
2251 tokens.append_all(self.attrs.inner());
2252 tokens.append_all(&block.stmts);
2253 });
David Tolnayca085422016-10-04 00:12:38 -07002254 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002255 None => {
2256 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002257 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002258 }
2259 }
2260 }
2261
2262 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002263 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002264 tokens.append_all(self.attrs.outer());
2265 self.type_token.to_tokens(tokens);
2266 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002267 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002268 if !self.bounds.is_empty() {
2269 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2270 self.bounds.to_tokens(tokens);
2271 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002272 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002273 if let Some((ref eq_token, ref default)) = self.default {
2274 eq_token.to_tokens(tokens);
2275 default.to_tokens(tokens);
2276 }
2277 self.semi_token.to_tokens(tokens);
2278 }
2279 }
2280
2281 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002282 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002283 tokens.append_all(self.attrs.outer());
2284 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002285 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002286 }
2287 }
2288
David Tolnay2ae520a2017-12-29 11:19:50 -05002289 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002290 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002291 self.tts.to_tokens(tokens);
2292 }
2293 }
2294
David Tolnay857628c2017-11-11 12:25:31 -08002295 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002296 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002297 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002298 self.vis.to_tokens(tokens);
2299 self.defaultness.to_tokens(tokens);
2300 self.const_token.to_tokens(tokens);
2301 self.ident.to_tokens(tokens);
2302 self.colon_token.to_tokens(tokens);
2303 self.ty.to_tokens(tokens);
2304 self.eq_token.to_tokens(tokens);
2305 self.expr.to_tokens(tokens);
2306 self.semi_token.to_tokens(tokens);
2307 }
2308 }
2309
2310 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002311 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002312 tokens.append_all(self.attrs.outer());
2313 self.vis.to_tokens(tokens);
2314 self.defaultness.to_tokens(tokens);
2315 self.sig.to_tokens(tokens);
2316 self.block.brace_token.surround(tokens, |tokens| {
2317 tokens.append_all(self.attrs.inner());
2318 tokens.append_all(&self.block.stmts);
2319 });
2320 }
2321 }
2322
2323 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002324 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002325 tokens.append_all(self.attrs.outer());
2326 self.vis.to_tokens(tokens);
2327 self.defaultness.to_tokens(tokens);
2328 self.type_token.to_tokens(tokens);
2329 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002330 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002331 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002332 self.eq_token.to_tokens(tokens);
2333 self.ty.to_tokens(tokens);
2334 self.semi_token.to_tokens(tokens);
2335 }
2336 }
2337
David Tolnaybb82ef02018-08-24 20:15:45 -04002338 impl ToTokens for ImplItemExistential {
2339 fn to_tokens(&self, tokens: &mut TokenStream) {
2340 tokens.append_all(self.attrs.outer());
2341 self.existential_token.to_tokens(tokens);
2342 self.type_token.to_tokens(tokens);
2343 self.ident.to_tokens(tokens);
2344 self.generics.to_tokens(tokens);
2345 self.generics.where_clause.to_tokens(tokens);
2346 if !self.bounds.is_empty() {
2347 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2348 self.bounds.to_tokens(tokens);
2349 }
2350 self.semi_token.to_tokens(tokens);
2351 }
2352 }
2353
David Tolnay857628c2017-11-11 12:25:31 -08002354 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002355 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002356 tokens.append_all(self.attrs.outer());
2357 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002358 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002359 }
2360 }
2361
David Tolnay2ae520a2017-12-29 11:19:50 -05002362 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002363 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002364 self.tts.to_tokens(tokens);
2365 }
2366 }
2367
David Tolnay8894f602017-11-11 12:11:04 -08002368 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002369 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002370 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002371 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002372 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002373 self.semi_token.to_tokens(tokens);
2374 }
2375 }
2376
2377 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002378 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002379 tokens.append_all(self.attrs.outer());
2380 self.vis.to_tokens(tokens);
2381 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002382 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002383 self.ident.to_tokens(tokens);
2384 self.colon_token.to_tokens(tokens);
2385 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002386 self.semi_token.to_tokens(tokens);
2387 }
2388 }
2389
David Tolnay199bcbb2017-11-12 10:33:52 -08002390 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002391 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002392 tokens.append_all(self.attrs.outer());
2393 self.vis.to_tokens(tokens);
2394 self.type_token.to_tokens(tokens);
2395 self.ident.to_tokens(tokens);
2396 self.semi_token.to_tokens(tokens);
2397 }
2398 }
2399
David Tolnay435c1782018-08-24 16:15:44 -04002400 impl ToTokens for ForeignItemMacro {
2401 fn to_tokens(&self, tokens: &mut TokenStream) {
2402 tokens.append_all(self.attrs.outer());
2403 self.mac.to_tokens(tokens);
2404 self.semi_token.to_tokens(tokens);
2405 }
2406 }
2407
David Tolnay2ae520a2017-12-29 11:19:50 -05002408 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002409 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002410 self.tts.to_tokens(tokens);
2411 }
2412 }
2413
David Tolnay570695e2017-06-03 16:15:13 -07002414 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002415 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002416 self.constness.to_tokens(tokens);
2417 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002418 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002419 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002420 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002421 }
2422 }
2423
Alex Crichtona74a1c82018-05-16 10:20:44 -07002424 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002425
2426 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002427 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002428 self.0.fn_token.to_tokens(tokens);
2429 self.1.to_tokens(tokens);
2430 self.0.generics.to_tokens(tokens);
2431 self.0.paren_token.surround(tokens, |tokens| {
2432 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002433 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2434 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002435 }
David Tolnayd2836e22017-12-27 23:13:00 -05002436 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002437 });
2438 self.0.output.to_tokens(tokens);
2439 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002440 }
2441 }
2442
Alex Crichton62a0a592017-05-22 13:58:53 -07002443 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002444 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002445 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002446 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002447 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002448 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002449 }
2450 }
2451
2452 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002453 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002454 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002455 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002456 }
2457 }
2458
2459 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002460 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002461 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002462 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002463 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002464 }
2465 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002466}