blob: 2e14a499cb09dd9b7f1d63702af7c230123807da [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
David Tolnaybb82ef02018-08-24 20:15:45 -0400139 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
142 pub Existential(ItemExistential {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub existential_token: Token![existential],
146 pub type_token: Token![type],
147 pub ident: Ident,
148 pub generics: Generics,
149 pub colon_token: Option<Token![:]>,
150 pub bounds: Punctuated<TypeParamBound, Token![+]>,
151 pub semi_token: Token![;],
152 }),
153
David Tolnay2b214082018-01-07 01:30:18 -0800154 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800155 ///
156 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 pub Struct(ItemStruct {
158 pub attrs: Vec<Attribute>,
159 pub vis: Visibility,
160 pub struct_token: Token![struct],
161 pub ident: Ident,
162 pub generics: Generics,
163 pub fields: Fields,
164 pub semi_token: Option<Token![;]>,
165 }),
David Tolnay2b214082018-01-07 01:30:18 -0800166
167 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800168 ///
169 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700174 pub ident: Ident,
175 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
David Tolnay2b214082018-01-07 01:30:18 -0800179
180 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800181 ///
182 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700187 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500189 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 }),
David Tolnay2b214082018-01-07 01:30:18 -0800191
192 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800193 ///
194 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700197 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500198 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500199 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800200 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700201 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500205 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub items: Vec<TraitItem>,
207 }),
David Tolnay2b214082018-01-07 01:30:18 -0800208
209 /// An impl block providing trait or associated items: `impl<A> Trait
210 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800211 ///
212 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800214 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500215 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500216 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700219 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500220 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700221 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub items: Vec<ImplItem>,
225 }),
David Tolnay2b214082018-01-07 01:30:18 -0800226
227 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800228 ///
229 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800230 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800231 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800232 /// The `example` in `macro_rules! example { ... }`.
233 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800234 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500235 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800236 }),
David Tolnay2b214082018-01-07 01:30:18 -0800237
238 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500241 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800242 pub attrs: Vec<Attribute>,
243 pub vis: Visibility,
244 pub macro_token: Token![macro],
245 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500246 pub paren_token: Paren,
247 pub args: TokenStream,
248 pub brace_token: Brace,
249 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500255 pub Verbatim(ItemVerbatim #manual_extra_traits {
256 pub tts: TokenStream,
257 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
David Tolnayb79ee962016-09-04 09:39:20 -0700259}
260
David Tolnay9c76bcb2017-12-26 23:14:59 -0500261#[cfg(feature = "extra-traits")]
262impl Eq for ItemMacro2 {}
263
264#[cfg(feature = "extra-traits")]
265impl PartialEq for ItemMacro2 {
266 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700267 self.attrs == other.attrs
268 && self.vis == other.vis
269 && self.macro_token == other.macro_token
270 && self.ident == other.ident
271 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500272 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
273 && self.brace_token == other.brace_token
274 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275 }
276}
277
278#[cfg(feature = "extra-traits")]
279impl Hash for ItemMacro2 {
280 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500281 where
282 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500283 {
284 self.attrs.hash(state);
285 self.vis.hash(state);
286 self.macro_token.hash(state);
287 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500288 self.paren_token.hash(state);
289 TokenStreamHelper(&self.args).hash(state);
290 self.brace_token.hash(state);
291 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500292 }
293}
294
David Tolnay2ae520a2017-12-29 11:19:50 -0500295#[cfg(feature = "extra-traits")]
296impl Eq for ItemVerbatim {}
297
298#[cfg(feature = "extra-traits")]
299impl PartialEq for ItemVerbatim {
300 fn eq(&self, other: &Self) -> bool {
301 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
302 }
303}
304
305#[cfg(feature = "extra-traits")]
306impl Hash for ItemVerbatim {
307 fn hash<H>(&self, state: &mut H)
308 where
309 H: Hasher,
310 {
311 TokenStreamHelper(&self.tts).hash(state);
312 }
313}
314
David Tolnay0e837402016-12-22 17:25:55 -0500315impl From<DeriveInput> for Item {
316 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500317 match input.data {
318 Data::Struct(data) => Item::Struct(ItemStruct {
319 attrs: input.attrs,
320 vis: input.vis,
321 struct_token: data.struct_token,
322 ident: input.ident,
323 generics: input.generics,
324 fields: data.fields,
325 semi_token: data.semi_token,
326 }),
327 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500328 attrs: input.attrs,
329 vis: input.vis,
330 enum_token: data.enum_token,
331 ident: input.ident,
332 generics: input.generics,
333 brace_token: data.brace_token,
334 variants: data.variants,
335 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500337 attrs: input.attrs,
338 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500339 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500340 ident: input.ident,
341 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500343 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700344 }
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800350 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800351 /// *This type is available if Syn is built with the `"full"` feature.*
352 ///
David Tolnay614a0142018-01-07 10:25:43 -0800353 /// # Syntax tree enum
354 ///
355 /// This type is a [syntax tree enum].
356 ///
357 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500358 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200359 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500362 pub Path(UsePath {
363 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200364 pub colon2_token: Token![::],
365 pub tree: Box<UseTree>,
366 }),
367
368 /// An identifier imported by a `use` item: `HashMap`.
369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
371 pub Name(UseName {
372 pub ident: Ident,
373 }),
374
375 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
378 pub Rename(UseRename {
379 pub ident: Ident,
380 pub as_token: Token![as],
381 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800383
David Tolnay05658502018-01-07 09:56:37 -0800384 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500387 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800390
David Tolnayd97a7d22018-03-31 19:17:01 +0200391 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800392 ///
393 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200394 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500396 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 }
399}
400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800402 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800403 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800404 /// *This type is available if Syn is built with the `"full"` feature.*
405 ///
David Tolnay614a0142018-01-07 10:25:43 -0800406 /// # Syntax tree enum
407 ///
408 /// This type is a [syntax tree enum].
409 ///
410 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800412 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800413 ///
414 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub attrs: Vec<Attribute>,
417 pub vis: Visibility,
418 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }),
David Tolnayebb72722018-01-07 01:14:13 -0800422
423 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800427 pub attrs: Vec<Attribute>,
428 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500430 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800431 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
David Tolnayebb72722018-01-07 01:14:13 -0800436
437 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800440 pub Type(ForeignItemType {
441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
443 pub type_token: Token![type],
444 pub ident: Ident,
445 pub semi_token: Token![;],
446 }),
David Tolnayebb72722018-01-07 01:14:13 -0800447
David Tolnay435c1782018-08-24 16:15:44 -0400448 /// A macro invocation within an extern block.
449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
451 pub Macro(ForeignItemMacro {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 pub semi_token: Option<Token![;]>,
455 }),
456
David Tolnayebb72722018-01-07 01:14:13 -0800457 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500460 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
461 pub tts: TokenStream,
462 }),
463 }
464}
465
466#[cfg(feature = "extra-traits")]
467impl Eq for ForeignItemVerbatim {}
468
469#[cfg(feature = "extra-traits")]
470impl PartialEq for ForeignItemVerbatim {
471 fn eq(&self, other: &Self) -> bool {
472 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
473 }
474}
475
476#[cfg(feature = "extra-traits")]
477impl Hash for ForeignItemVerbatim {
478 fn hash<H>(&self, state: &mut H)
479 where
480 H: Hasher,
481 {
482 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700484}
485
David Tolnayda705bd2017-11-10 21:58:05 -0800486ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800487 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800488 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800489 /// *This type is available if Syn is built with the `"full"` feature.*
490 ///
David Tolnay614a0142018-01-07 10:25:43 -0800491 /// # Syntax tree enum
492 ///
493 /// This type is a [syntax tree enum].
494 ///
495 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800496 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800497 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800498 ///
499 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800501 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700503 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub default: Option<(Token![=], Expr)>,
507 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
David Tolnayebb72722018-01-07 01:14:13 -0800509
510 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800514 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 pub sig: MethodSig,
516 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
David Tolnayebb72722018-01-07 01:14:13 -0800519
520 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700526 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500527 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800535 ///
536 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800537 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800539 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500540 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800541 }),
David Tolnayebb72722018-01-07 01:14:13 -0800542
543 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800544 ///
545 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500546 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
547 pub tts: TokenStream,
548 }),
549 }
550}
551
552#[cfg(feature = "extra-traits")]
553impl Eq for TraitItemVerbatim {}
554
555#[cfg(feature = "extra-traits")]
556impl PartialEq for TraitItemVerbatim {
557 fn eq(&self, other: &Self) -> bool {
558 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
559 }
560}
561
562#[cfg(feature = "extra-traits")]
563impl Hash for TraitItemVerbatim {
564 fn hash<H>(&self, state: &mut H)
565 where
566 H: Hasher,
567 {
568 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800573 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800574 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800575 /// *This type is available if Syn is built with the `"full"` feature.*
576 ///
David Tolnay614a0142018-01-07 10:25:43 -0800577 /// # Syntax tree enum
578 ///
579 /// This type is a [syntax tree enum].
580 ///
581 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800582 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800583 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700591 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }),
David Tolnayebb72722018-01-07 01:14:13 -0800598
599 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800600 ///
601 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700602 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700604 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500605 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 pub sig: MethodSig,
607 pub block: Block,
608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800614 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700615 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500616 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500619 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 }),
David Tolnayebb72722018-01-07 01:14:13 -0800624
David Tolnaybb82ef02018-08-24 20:15:45 -0400625 /// An existential type within an impl block.
626 ///
627 /// *This type is available if Syn is built with the `"full"` feature.*
628 pub Existential(ImplItemExistential {
629 pub attrs: Vec<Attribute>,
630 pub existential_token: Token![existential],
631 pub type_token: Token![type],
632 pub ident: Ident,
633 pub generics: Generics,
634 pub colon_token: Option<Token![:]>,
635 pub bounds: Punctuated<TypeParamBound, Token![+]>,
636 pub semi_token: Token![;],
637 }),
638
David Tolnayebb72722018-01-07 01:14:13 -0800639 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800642 pub Macro(ImplItemMacro {
643 pub attrs: Vec<Attribute>,
644 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500645 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800646 }),
David Tolnayebb72722018-01-07 01:14:13 -0800647
648 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800649 ///
650 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500651 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
652 pub tts: TokenStream,
653 }),
654 }
655}
656
657#[cfg(feature = "extra-traits")]
658impl Eq for ImplItemVerbatim {}
659
660#[cfg(feature = "extra-traits")]
661impl PartialEq for ImplItemVerbatim {
662 fn eq(&self, other: &Self) -> bool {
663 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
664 }
665}
666
667#[cfg(feature = "extra-traits")]
668impl Hash for ImplItemVerbatim {
669 fn hash<H>(&self, state: &mut H)
670 where
671 H: Hasher,
672 {
673 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700675}
676
677ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800678 /// A method's signature in a trait or implementation: `unsafe fn
679 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500683 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500684 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900685 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700687 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }
690}
691
692ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800693 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500698 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500699 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500700 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500701 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500702 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700704}
705
Alex Crichton62a0a592017-05-22 13:58:53 -0700706ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800707 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800708 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800709 /// *This type is available if Syn is built with the `"full"` feature.*
710 ///
David Tolnay614a0142018-01-07 10:25:43 -0800711 /// # Syntax tree enum
712 ///
713 /// This type is a [syntax tree enum].
714 ///
715 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// Self captured by reference in a function signature: `&self` or `&mut
718 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500725 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800727
David Tolnay3f559052018-01-06 23:59:48 -0800728 /// Self captured by value in a function signature: `self` or `mut
729 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800736
David Tolnay3f559052018-01-06 23:59:48 -0800737 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800738 ///
739 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 pub Captured(ArgCaptured {
741 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800743 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800745
David Tolnay3f559052018-01-06 23:59:48 -0800746 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500747 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800748 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }
David Tolnay62f374c2016-10-02 13:37:00 -0700751}
752
David Tolnayedf2b992016-09-23 20:43:45 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700756
David Tolnay2ad62c12018-08-26 19:00:35 -0400757 use parse::{Parse, ParseStream, Result};
David Tolnay3779bb72018-08-26 18:46:07 -0700758 use synom::ext::IdentExt;
David Tolnay9be32582018-07-31 22:37:26 -0700759 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700760
David Tolnay4c614be2017-11-10 00:02:38 -0800761 impl_synom!(Item "item" alt!(
762 syn!(ItemExternCrate) => { Item::ExternCrate }
763 |
764 syn!(ItemUse) => { Item::Use }
765 |
766 syn!(ItemStatic) => { Item::Static }
767 |
768 syn!(ItemConst) => { Item::Const }
769 |
770 syn!(ItemFn) => { Item::Fn }
771 |
772 syn!(ItemMod) => { Item::Mod }
773 |
774 syn!(ItemForeignMod) => { Item::ForeignMod }
775 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800776 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800777 |
David Tolnaybb82ef02018-08-24 20:15:45 -0400778 syn!(ItemExistential) => { Item::Existential }
David Tolnay758ee132018-08-21 21:29:40 -0400779 |
David Tolnay4c614be2017-11-10 00:02:38 -0800780 syn!(ItemStruct) => { Item::Struct }
781 |
782 syn!(ItemEnum) => { Item::Enum }
783 |
784 syn!(ItemUnion) => { Item::Union }
785 |
786 syn!(ItemTrait) => { Item::Trait }
787 |
David Tolnay4c614be2017-11-10 00:02:38 -0800788 syn!(ItemImpl) => { Item::Impl }
789 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800790 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800791 |
792 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800793 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700794
David Tolnay3779bb72018-08-26 18:46:07 -0700795 impl Parse for ItemMacro {
796 fn parse(input: ParseStream) -> Result<Self> {
797 let attrs = input.call(Attribute::parse_outer)?;
798 let path = input.call(Path::parse_mod_style)?;
799 let bang_token: Token![!] = input.parse()?;
800 let ident: Option<Ident> = input.parse()?;
801 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
802 let semi_token: Option<Token![;]> = if !is_brace(&delimiter) {
803 Some(input.parse()?)
804 } else {
805 None
806 };
807 Ok(ItemMacro {
808 attrs: attrs,
809 ident: ident,
810 mac: Macro {
811 path: path,
812 bang_token: bang_token,
813 delimiter: delimiter,
814 tts: tts,
815 },
816 semi_token: semi_token,
817 })
818 }
819 }
David Tolnayedf2b992016-09-23 20:43:45 -0700820
David Tolnay500d8322017-12-18 00:32:51 -0800821 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700822 impl Parse for ItemMacro2 {
823 fn parse(input: ParseStream) -> Result<Self> {
824 let args;
825 let body;
826 Ok(ItemMacro2 {
827 attrs: input.call(Attribute::parse_outer)?,
828 vis: input.parse()?,
829 macro_token: input.parse()?,
830 ident: input.parse()?,
831 paren_token: parenthesized!(args in input),
832 args: args.parse()?,
833 brace_token: braced!(body in input),
834 body: body.parse()?,
835 })
836 }
837 }
David Tolnay500d8322017-12-18 00:32:51 -0800838
David Tolnay3779bb72018-08-26 18:46:07 -0700839 impl Parse for ItemExternCrate {
840 fn parse(input: ParseStream) -> Result<Self> {
841 Ok(ItemExternCrate {
842 attrs: input.call(Attribute::parse_outer)?,
843 vis: input.parse()?,
844 extern_token: input.parse()?,
845 crate_token: input.parse()?,
846 ident: input.parse()?,
847 rename: {
848 if input.peek(Token![as]) {
849 let as_token: Token![as] = input.parse()?;
850 let rename: Ident = input.parse()?;
851 Some((as_token, rename))
852 } else {
853 None
854 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800855 },
David Tolnay3779bb72018-08-26 18:46:07 -0700856 semi_token: input.parse()?,
857 })
858 }
859 }
860
861 impl Parse for ItemUse {
862 fn parse(input: ParseStream) -> Result<Self> {
863 Ok(ItemUse {
864 attrs: input.call(Attribute::parse_outer)?,
865 vis: input.parse()?,
866 use_token: input.parse()?,
867 leading_colon: input.parse()?,
868 tree: input.call(use_tree)?,
869 semi_token: input.parse()?,
870 })
871 }
872 }
873
874 fn use_tree(input: ParseStream) -> Result<UseTree> {
875 let lookahead = input.lookahead1();
876 if lookahead.peek(Ident)
877 || lookahead.peek(Token![self])
878 || lookahead.peek(Token![super])
879 || lookahead.peek(Token![crate])
880 || lookahead.peek(Token![extern])
881 {
882 let ident = input.call(Ident::parse_any2)?;
883 if input.peek(Token![::]) {
884 Ok(UseTree::Path(UsePath {
885 ident: ident,
886 colon2_token: input.parse()?,
887 tree: Box::new(input.call(use_tree)?),
888 }))
889 } else if input.peek(Token![as]) {
890 Ok(UseTree::Rename(UseRename {
891 ident: ident,
892 as_token: input.parse()?,
893 rename: input.parse()?,
894 }))
895 } else {
896 Ok(UseTree::Name(UseName { ident: ident }))
897 }
898 } else if lookahead.peek(Token![*]) {
899 Ok(UseTree::Glob(UseGlob {
900 star_token: input.parse()?,
901 }))
902 } else if lookahead.peek(token::Brace) {
903 let content;
904 Ok(UseTree::Group(UseGroup {
905 brace_token: braced!(content in input),
906 items: content.parse_terminated(use_tree)?,
907 }))
908 } else {
909 Err(lookahead.error())
910 }
911 }
912
913 impl Parse for ItemStatic {
914 fn parse(input: ParseStream) -> Result<Self> {
915 Ok(ItemStatic {
916 attrs: input.call(Attribute::parse_outer)?,
917 vis: input.parse()?,
918 static_token: input.parse()?,
919 mutability: input.parse()?,
920 ident: input.parse()?,
921 colon_token: input.parse()?,
922 ty: input.parse()?,
923 eq_token: input.parse()?,
924 expr: Box::new(input.parse_synom(Expr::parse)?),
925 semi_token: input.parse()?,
926 })
927 }
928 }
929
930 impl Parse for ItemConst {
931 fn parse(input: ParseStream) -> Result<Self> {
932 Ok(ItemConst {
933 attrs: input.call(Attribute::parse_outer)?,
934 vis: input.parse()?,
935 const_token: input.parse()?,
936 ident: input.parse()?,
937 colon_token: input.parse()?,
938 ty: input.parse()?,
939 eq_token: input.parse()?,
940 expr: Box::new(input.parse_synom(Expr::parse)?),
941 semi_token: input.parse()?,
942 })
943 }
944 }
945
946 impl Parse for ItemFn {
947 fn parse(input: ParseStream) -> Result<Self> {
948 let outer_attrs = input.call(Attribute::parse_outer)?;
949 let vis: Visibility = input.parse()?;
950 let constness: Option<Token![const]> = input.parse()?;
951 let unsafety: Option<Token![unsafe]> = input.parse()?;
952 let asyncness: Option<Token![async]> = input.parse()?;
953 let abi: Option<Abi> = input.parse()?;
954 let fn_token: Token![fn] = input.parse()?;
955 let ident: Ident = input.parse()?;
956 let generics: Generics = input.parse()?;
957
958 let content;
959 let paren_token = parenthesized!(content in input);
960 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
961
962 let output: ReturnType = input.parse()?;
963 let where_clause: Option<WhereClause> = input.parse()?;
964
965 let content;
966 let brace_token = braced!(content in input);
967 let inner_attrs = content.call(Attribute::parse_inner)?;
968 let stmts = content.parse_synom(Block::parse_within)?;
969
970 Ok(ItemFn {
971 attrs: {
972 let mut attrs = outer_attrs;
973 attrs.extend(inner_attrs);
974 attrs
975 },
976 vis: vis,
977 constness: constness,
978 unsafety: unsafety,
979 asyncness: asyncness,
980 abi: abi,
981 ident: ident,
982 decl: Box::new(FnDecl {
983 fn_token: fn_token,
984 paren_token: paren_token,
985 inputs: inputs,
986 output: output,
987 variadic: None,
988 generics: Generics {
989 where_clause: where_clause,
990 ..generics
991 },
992 }),
993 block: Box::new(Block {
994 brace_token: brace_token,
995 stmts: stmts,
996 }),
997 })
998 }
999 }
David Tolnay42602292016-10-01 22:25:45 -07001000
David Tolnay2ad62c12018-08-26 19:00:35 -04001001 impl Parse for FnArg {
1002 fn parse(input: ParseStream) -> Result<Self> {
1003 if input.peek(Token![&]) {
1004 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001005 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1006 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001007 }
1008 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001009
David Tolnay2ad62c12018-08-26 19:00:35 -04001010 if input.peek(Token![mut]) || input.peek(Token![self]) {
1011 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001012 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1013 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001014 }
1015 }
1016
1017 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001018 let err = match ahead.call(arg_captured) {
1019 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001020 Err(err) => err,
1021 };
1022
1023 let ahead = input.fork();
1024 if ahead.parse::<Type>().is_ok() {
1025 return input.parse().map(FnArg::Ignored);
1026 }
1027
1028 Err(err)
1029 }
1030 }
1031
David Tolnay3779bb72018-08-26 18:46:07 -07001032 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1033 Ok(ArgSelfRef {
1034 and_token: input.parse()?,
1035 lifetime: input.parse()?,
1036 mutability: input.parse()?,
1037 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001038 })
David Tolnay3779bb72018-08-26 18:46:07 -07001039 }
David Tolnay35902302016-10-06 01:11:08 -07001040
David Tolnay3779bb72018-08-26 18:46:07 -07001041 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1042 Ok(ArgSelf {
1043 mutability: input.parse()?,
1044 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001045 })
David Tolnay3779bb72018-08-26 18:46:07 -07001046 }
1047
1048 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1049 Ok(ArgCaptured {
1050 pat: input.parse_synom(Pat::parse)?,
1051 colon_token: input.parse()?,
1052 ty: input.parse()?,
1053 })
1054 }
1055
1056 impl Parse for ItemMod {
1057 fn parse(input: ParseStream) -> Result<Self> {
1058 let outer_attrs = input.call(Attribute::parse_outer)?;
1059 let vis: Visibility = input.parse()?;
1060 let mod_token: Token![mod] = input.parse()?;
1061 let ident: Ident = input.parse()?;
1062
1063 let lookahead = input.lookahead1();
1064 if lookahead.peek(Token![;]) {
1065 Ok(ItemMod {
1066 attrs: outer_attrs,
1067 vis: vis,
1068 mod_token: mod_token,
1069 ident: ident,
1070 content: None,
1071 semi: Some(input.parse()?),
1072 })
1073 } else if lookahead.peek(token::Brace) {
1074 let content;
1075 let brace_token = braced!(content in input);
1076 let inner_attrs = content.call(Attribute::parse_inner)?;
1077
1078 let mut items = Vec::new();
1079 while !content.is_empty() {
1080 items.push(content.parse_synom(Item::parse)?);
1081 }
1082
1083 Ok(ItemMod {
1084 attrs: {
1085 let mut attrs = outer_attrs;
1086 attrs.extend(inner_attrs);
1087 attrs
1088 },
1089 vis: vis,
1090 mod_token: mod_token,
1091 ident: ident,
1092 content: Some((brace_token, items)),
1093 semi: None,
1094 })
1095 } else {
1096 Err(lookahead.error())
1097 }
1098 }
1099 }
1100
1101 impl Parse for ItemForeignMod {
1102 fn parse(input: ParseStream) -> Result<Self> {
1103 let outer_attrs = input.call(Attribute::parse_outer)?;
1104 let abi: Abi = input.parse()?;
1105
1106 let content;
1107 let brace_token = braced!(content in input);
1108 let inner_attrs = content.call(Attribute::parse_inner)?;
1109 let mut items = Vec::new();
1110 while !content.is_empty() {
1111 items.push(content.parse_synom(ForeignItem::parse)?);
1112 }
1113
1114 Ok(ItemForeignMod {
1115 attrs: {
1116 let mut attrs = outer_attrs;
1117 attrs.extend(inner_attrs);
1118 attrs
1119 },
1120 abi: abi,
1121 brace_token: brace_token,
1122 items: items,
1123 })
1124 }
1125 }
David Tolnay35902302016-10-06 01:11:08 -07001126
David Tolnay8894f602017-11-11 12:11:04 -08001127 impl_synom!(ForeignItem "foreign item" alt!(
1128 syn!(ForeignItemFn) => { ForeignItem::Fn }
1129 |
1130 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001131 |
1132 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay78572112018-08-01 00:36:18 -07001133 |
David Tolnay435c1782018-08-24 16:15:44 -04001134 syn!(ForeignItemMacro) => { ForeignItem::Macro }
David Tolnay8894f602017-11-11 12:11:04 -08001135 ));
David Tolnay35902302016-10-06 01:11:08 -07001136
David Tolnay3779bb72018-08-26 18:46:07 -07001137 impl Parse for ForeignItemFn {
1138 fn parse(input: ParseStream) -> Result<Self> {
1139 let attrs = input.call(Attribute::parse_outer)?;
1140 let vis: Visibility = input.parse()?;
1141 let fn_token: Token![fn] = input.parse()?;
1142 let ident: Ident = input.parse()?;
1143 let generics: Generics = input.parse()?;
1144
1145 let content;
1146 let paren_token = parenthesized!(content in input);
1147 let inputs = content.parse_synom(Punctuated::parse_terminated)?;
1148 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1149 content.parse()?
1150 } else {
1151 None
1152 };
1153
1154 let output: ReturnType = input.parse()?;
1155 let where_clause: Option<WhereClause> = input.parse()?;
1156 let semi_token: Token![;] = input.parse()?;
1157
1158 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001159 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001160 vis: vis,
1161 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001162 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001163 fn_token: fn_token,
1164 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001165 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001166 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001167 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001168 generics: Generics {
1169 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001170 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001171 },
1172 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001173 semi_token: semi_token,
1174 })
1175 }
1176 }
David Tolnay35902302016-10-06 01:11:08 -07001177
David Tolnay3779bb72018-08-26 18:46:07 -07001178 impl Parse for ForeignItemStatic {
1179 fn parse(input: ParseStream) -> Result<Self> {
1180 Ok(ForeignItemStatic {
1181 attrs: input.call(Attribute::parse_outer)?,
1182 vis: input.parse()?,
1183 static_token: input.parse()?,
1184 mutability: input.parse()?,
1185 ident: input.parse()?,
1186 colon_token: input.parse()?,
1187 ty: input.parse()?,
1188 semi_token: input.parse()?,
1189 })
1190 }
1191 }
David Tolnay35902302016-10-06 01:11:08 -07001192
David Tolnay3779bb72018-08-26 18:46:07 -07001193 impl Parse for ForeignItemType {
1194 fn parse(input: ParseStream) -> Result<Self> {
1195 Ok(ForeignItemType {
1196 attrs: input.call(Attribute::parse_outer)?,
1197 vis: input.parse()?,
1198 type_token: input.parse()?,
1199 ident: input.parse()?,
1200 semi_token: input.parse()?,
1201 })
1202 }
1203 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001204
David Tolnay3779bb72018-08-26 18:46:07 -07001205 impl Parse for ForeignItemMacro {
1206 fn parse(input: ParseStream) -> Result<Self> {
1207 let attrs = input.call(Attribute::parse_outer)?;
1208 let mac: Macro = input.parse()?;
1209 let semi_token: Option<Token![;]> = if is_brace(&mac.delimiter) {
1210 None
1211 } else {
1212 Some(input.parse()?)
1213 };
1214 Ok(ForeignItemMacro {
1215 attrs: attrs,
1216 mac: mac,
1217 semi_token: semi_token,
1218 })
1219 }
1220 }
David Tolnay78572112018-08-01 00:36:18 -07001221
David Tolnay3779bb72018-08-26 18:46:07 -07001222 impl Parse for ItemType {
1223 fn parse(input: ParseStream) -> Result<Self> {
1224 Ok(ItemType {
1225 attrs: input.call(Attribute::parse_outer)?,
1226 vis: input.parse()?,
1227 type_token: input.parse()?,
1228 ident: input.parse()?,
1229 generics: {
1230 let mut generics: Generics = input.parse()?;
1231 generics.where_clause = input.parse()?;
1232 generics
1233 },
1234 eq_token: input.parse()?,
1235 ty: input.parse()?,
1236 semi_token: input.parse()?,
1237 })
1238 }
1239 }
David Tolnay3cf52982016-10-01 17:11:37 -07001240
David Tolnay3779bb72018-08-26 18:46:07 -07001241 impl Parse for ItemExistential {
1242 fn parse(input: ParseStream) -> Result<Self> {
1243 Ok(ItemExistential {
1244 attrs: input.call(Attribute::parse_outer)?,
1245 vis: input.parse()?,
1246 existential_token: input.parse()?,
1247 type_token: input.parse()?,
1248 ident: input.parse()?,
1249 generics: {
1250 let mut generics: Generics = input.parse()?;
1251 generics.where_clause = input.parse()?;
1252 generics
1253 },
1254 colon_token: Some(input.parse()?),
1255 bounds: input.parse_synom(Punctuated::parse_separated_nonempty)?,
1256 semi_token: input.parse()?,
1257 })
1258 }
1259 }
David Tolnay758ee132018-08-21 21:29:40 -04001260
David Tolnay4c614be2017-11-10 00:02:38 -08001261 impl_synom!(ItemStruct "struct item" switch!(
1262 map!(syn!(DeriveInput), Into::into),
1263 Item::Struct(item) => value!(item)
1264 |
1265 _ => reject!()
1266 ));
David Tolnay42602292016-10-01 22:25:45 -07001267
David Tolnay4c614be2017-11-10 00:02:38 -08001268 impl_synom!(ItemEnum "enum item" switch!(
1269 map!(syn!(DeriveInput), Into::into),
1270 Item::Enum(item) => value!(item)
1271 |
1272 _ => reject!()
1273 ));
1274
1275 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001276 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001277 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001278 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001279 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001280 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001281 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001282 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001283 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001284 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001285 vis: vis,
1286 union_token: union_,
1287 ident: ident,
1288 generics: Generics {
1289 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001290 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001291 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001292 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001293 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001294 ));
1295
David Tolnay4c614be2017-11-10 00:02:38 -08001296 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001297 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001298 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001299 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001300 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001301 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001302 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001303 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001304 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001305 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001306 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001307 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001308 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001309 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001310 vis: vis,
1311 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001312 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001313 trait_token: trait_,
1314 ident: ident,
1315 generics: Generics {
1316 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001317 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001318 },
1319 colon_token: colon,
1320 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001321 brace_token: body.0,
1322 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001323 })
David Tolnay0aecb732016-10-03 23:03:50 -07001324 ));
1325
David Tolnayda705bd2017-11-10 21:58:05 -08001326 impl_synom!(TraitItem "trait item" alt!(
1327 syn!(TraitItemConst) => { TraitItem::Const }
1328 |
1329 syn!(TraitItemMethod) => { TraitItem::Method }
1330 |
1331 syn!(TraitItemType) => { TraitItem::Type }
1332 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001333 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001334 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001335
David Tolnayda705bd2017-11-10 21:58:05 -08001336 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001337 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001338 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001339 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001340 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001341 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001342 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1343 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001344 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001345 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001346 const_token: const_,
1347 ident: ident,
1348 colon_token: colon,
1349 ty: ty,
1350 default: default,
1351 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001352 })
1353 ));
1354
David Tolnayda705bd2017-11-10 21:58:05 -08001355 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001356 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001357 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001358 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001359 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001360 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001361 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001362 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001363 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001364 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001365 where_clause: option!(syn!(WhereClause)) >>
David Tolnay76178be2018-07-31 23:06:15 -07001366 body: option!(braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001367 many0!(Attribute::old_parse_inner),
David Tolnay76178be2018-07-31 23:06:15 -07001368 call!(Block::parse_within),
1369 ))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001370 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001371 ({
1372 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001373 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001374 None => (Vec::new(), None),
1375 };
David Tolnayda705bd2017-11-10 21:58:05 -08001376 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001377 attrs: {
1378 let mut attrs = outer_attrs;
1379 attrs.extend(inner_attrs);
1380 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001381 },
David Tolnayda705bd2017-11-10 21:58:05 -08001382 sig: MethodSig {
1383 constness: constness,
1384 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001385 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001386 abi: abi,
1387 ident: ident,
1388 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001389 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001390 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001391 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001392 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001393 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001394 generics: Generics {
1395 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001396 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001397 },
1398 },
David Tolnayda705bd2017-11-10 21:58:05 -08001399 },
1400 default: stmts.map(|stmts| {
1401 Block {
1402 stmts: stmts.0,
1403 brace_token: stmts.1,
1404 }
1405 }),
1406 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001407 }
David Tolnay0aecb732016-10-03 23:03:50 -07001408 })
1409 ));
1410
David Tolnayda705bd2017-11-10 21:58:05 -08001411 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001412 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001413 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001414 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001415 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001416 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001417 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001418 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001419 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001420 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001421 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001422 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001423 type_token: type_,
1424 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001425 generics: Generics {
1426 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001427 ..generics
Nika Layzell0183ca32017-12-05 15:24:01 -05001428 },
David Tolnayda705bd2017-11-10 21:58:05 -08001429 colon_token: colon,
1430 bounds: bounds.unwrap_or_default(),
1431 default: default,
1432 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001433 })
1434 ));
1435
David Tolnaydecf28d2017-11-11 11:56:45 -08001436 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001437 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001438 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001439 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001440 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001441 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001442 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001443 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001444 })
1445 ));
1446
David Tolnay4c614be2017-11-10 00:02:38 -08001447 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001448 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001449 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001450 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001451 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001452 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001453 polarity_path: alt!(
1454 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001455 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001456 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001457 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001458 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001459 )
1460 |
David Tolnay570695e2017-06-03 16:15:13 -07001461 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001462 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001463 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001464 where_clause: option!(syn!(WhereClause)) >>
David Tolnaycf3697a2018-03-31 20:51:15 +02001465 inner: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001466 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001467 many0!(ImplItem::parse),
David Tolnaycf3697a2018-03-31 20:51:15 +02001468 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001469 (ItemImpl {
David Tolnaycf3697a2018-03-31 20:51:15 +02001470 attrs: {
1471 let mut attrs = outer_attrs;
1472 attrs.extend((inner.1).0);
1473 attrs
1474 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001475 defaultness: defaultness,
1476 unsafety: unsafety,
1477 impl_token: impl_,
1478 generics: Generics {
1479 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001480 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001481 },
1482 trait_: polarity_path,
1483 self_ty: Box::new(self_ty),
David Tolnaycf3697a2018-03-31 20:51:15 +02001484 brace_token: inner.0,
1485 items: (inner.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001486 })
David Tolnay4c9be372016-10-06 00:47:37 -07001487 ));
1488
David Tolnay857628c2017-11-11 12:25:31 -08001489 impl_synom!(ImplItem "item in impl block" alt!(
1490 syn!(ImplItemConst) => { ImplItem::Const }
1491 |
1492 syn!(ImplItemMethod) => { ImplItem::Method }
1493 |
1494 syn!(ImplItemType) => { ImplItem::Type }
1495 |
David Tolnaybb82ef02018-08-24 20:15:45 -04001496 syn!(ImplItemExistential) => { ImplItem::Existential }
David Tolnay758ee132018-08-21 21:29:40 -04001497 |
David Tolnay857628c2017-11-11 12:25:31 -08001498 syn!(ImplItemMacro) => { ImplItem::Macro }
1499 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001500
David Tolnay3779bb72018-08-26 18:46:07 -07001501 impl Parse for ImplItemConst {
1502 fn parse(input: ParseStream) -> Result<Self> {
1503 Ok(ImplItemConst {
1504 attrs: input.call(Attribute::parse_outer)?,
1505 vis: input.parse()?,
1506 defaultness: input.parse()?,
1507 const_token: input.parse()?,
1508 ident: input.parse()?,
1509 colon_token: input.parse()?,
1510 ty: input.parse()?,
1511 eq_token: input.parse()?,
1512 expr: input.parse_synom(Expr::parse)?,
1513 semi_token: input.parse()?,
1514 })
1515 }
1516 }
David Tolnay4c9be372016-10-06 00:47:37 -07001517
David Tolnay857628c2017-11-11 12:25:31 -08001518 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001519 outer_attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001520 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001521 defaultness: option!(keyword!(default)) >>
1522 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001523 unsafety: option!(keyword!(unsafe)) >>
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001524 asyncness: option!(keyword!(async)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001525 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001527 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001528 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001529 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001530 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001531 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001532 inner_attrs_stmts: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001533 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001534 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -04001535 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001536 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001537 attrs: {
1538 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001539 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001540 attrs
1541 },
David Tolnay857628c2017-11-11 12:25:31 -08001542 vis: vis,
1543 defaultness: defaultness,
1544 sig: MethodSig {
1545 constness: constness,
1546 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001547 asyncness: asyncness,
David Tolnay857628c2017-11-11 12:25:31 -08001548 abi: abi,
1549 ident: ident,
1550 decl: FnDecl {
1551 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001552 paren_token: inputs.0,
1553 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001554 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001555 generics: Generics {
1556 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001557 ..generics
David Tolnay4c9be372016-10-06 00:47:37 -07001558 },
David Tolnayd2836e22017-12-27 23:13:00 -05001559 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001560 },
David Tolnay857628c2017-11-11 12:25:31 -08001561 },
1562 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001563 brace_token: inner_attrs_stmts.0,
1564 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001565 },
David Tolnay4c9be372016-10-06 00:47:37 -07001566 })
1567 ));
1568
David Tolnay3779bb72018-08-26 18:46:07 -07001569 impl Parse for ImplItemType {
1570 fn parse(input: ParseStream) -> Result<Self> {
1571 Ok(ImplItemType {
1572 attrs: input.call(Attribute::parse_outer)?,
1573 vis: input.parse()?,
1574 defaultness: input.parse()?,
1575 type_token: input.parse()?,
1576 ident: input.parse()?,
1577 generics: {
1578 let mut generics: Generics = input.parse()?;
1579 generics.where_clause = input.parse()?;
1580 generics
1581 },
1582 eq_token: input.parse()?,
1583 ty: input.parse()?,
1584 semi_token: input.parse()?,
1585 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001586 }
David Tolnay3779bb72018-08-26 18:46:07 -07001587 }
David Tolnay758ee132018-08-21 21:29:40 -04001588
David Tolnay3779bb72018-08-26 18:46:07 -07001589 impl Parse for ImplItemExistential {
1590 fn parse(input: ParseStream) -> Result<Self> {
1591 let ety: ItemExistential = input.parse()?;
1592 Ok(ImplItemExistential {
1593 attrs: ety.attrs,
1594 existential_token: ety.existential_token,
1595 type_token: ety.type_token,
1596 ident: ety.ident,
1597 generics: ety.generics,
1598 colon_token: ety.colon_token,
1599 bounds: ety.bounds,
1600 semi_token: ety.semi_token,
1601 })
1602 }
1603 }
1604
1605 impl Parse for ImplItemMacro {
1606 fn parse(input: ParseStream) -> Result<Self> {
1607 let attrs = input.call(Attribute::parse_outer)?;
1608 let mac: Macro = input.parse()?;
1609 let semi_token: Option<Token![;]> = if is_brace(&mac.delimiter) {
1610 None
1611 } else {
1612 Some(input.parse()?)
1613 };
1614 Ok(ImplItemMacro {
1615 attrs: attrs,
1616 mac: mac,
1617 semi_token: semi_token,
1618 })
1619 }
1620 }
David Tolnay4c9be372016-10-06 00:47:37 -07001621
David Tolnayab919512017-12-30 23:31:51 -05001622 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1623 match *delimiter {
1624 MacroDelimiter::Brace(_) => true,
1625 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001626 }
1627 }
David Tolnayedf2b992016-09-23 20:43:45 -07001628}
David Tolnay4a51dc72016-10-01 00:40:31 -07001629
1630#[cfg(feature = "printing")]
1631mod printing {
1632 use super::*;
1633 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001634 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001635 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001636
David Tolnay1bfa7332017-11-11 12:41:20 -08001637 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001638 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001639 tokens.append_all(self.attrs.outer());
1640 self.vis.to_tokens(tokens);
1641 self.extern_token.to_tokens(tokens);
1642 self.crate_token.to_tokens(tokens);
1643 self.ident.to_tokens(tokens);
1644 if let Some((ref as_token, ref rename)) = self.rename {
1645 as_token.to_tokens(tokens);
1646 rename.to_tokens(tokens);
1647 }
1648 self.semi_token.to_tokens(tokens);
1649 }
1650 }
1651
1652 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001653 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001654 tokens.append_all(self.attrs.outer());
1655 self.vis.to_tokens(tokens);
1656 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001657 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001658 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001659 self.semi_token.to_tokens(tokens);
1660 }
1661 }
1662
1663 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001664 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001665 tokens.append_all(self.attrs.outer());
1666 self.vis.to_tokens(tokens);
1667 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001668 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001669 self.ident.to_tokens(tokens);
1670 self.colon_token.to_tokens(tokens);
1671 self.ty.to_tokens(tokens);
1672 self.eq_token.to_tokens(tokens);
1673 self.expr.to_tokens(tokens);
1674 self.semi_token.to_tokens(tokens);
1675 }
1676 }
1677
1678 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001679 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001680 tokens.append_all(self.attrs.outer());
1681 self.vis.to_tokens(tokens);
1682 self.const_token.to_tokens(tokens);
1683 self.ident.to_tokens(tokens);
1684 self.colon_token.to_tokens(tokens);
1685 self.ty.to_tokens(tokens);
1686 self.eq_token.to_tokens(tokens);
1687 self.expr.to_tokens(tokens);
1688 self.semi_token.to_tokens(tokens);
1689 }
1690 }
1691
1692 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001693 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001694 tokens.append_all(self.attrs.outer());
1695 self.vis.to_tokens(tokens);
1696 self.constness.to_tokens(tokens);
1697 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001698 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001699 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001700 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001701 self.block.brace_token.surround(tokens, |tokens| {
1702 tokens.append_all(self.attrs.inner());
1703 tokens.append_all(&self.block.stmts);
1704 });
1705 }
1706 }
1707
1708 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001709 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001710 tokens.append_all(self.attrs.outer());
1711 self.vis.to_tokens(tokens);
1712 self.mod_token.to_tokens(tokens);
1713 self.ident.to_tokens(tokens);
1714 if let Some((ref brace, ref items)) = self.content {
1715 brace.surround(tokens, |tokens| {
1716 tokens.append_all(self.attrs.inner());
1717 tokens.append_all(items);
1718 });
1719 } else {
1720 TokensOrDefault(&self.semi).to_tokens(tokens);
1721 }
1722 }
1723 }
1724
1725 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001726 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001727 tokens.append_all(self.attrs.outer());
1728 self.abi.to_tokens(tokens);
1729 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001730 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001731 tokens.append_all(&self.items);
1732 });
1733 }
1734 }
1735
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001736 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001737 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001738 tokens.append_all(self.attrs.outer());
1739 self.vis.to_tokens(tokens);
1740 self.type_token.to_tokens(tokens);
1741 self.ident.to_tokens(tokens);
1742 self.generics.to_tokens(tokens);
1743 self.generics.where_clause.to_tokens(tokens);
1744 self.eq_token.to_tokens(tokens);
1745 self.ty.to_tokens(tokens);
1746 self.semi_token.to_tokens(tokens);
1747 }
1748 }
1749
David Tolnaybb82ef02018-08-24 20:15:45 -04001750 impl ToTokens for ItemExistential {
1751 fn to_tokens(&self, tokens: &mut TokenStream) {
1752 tokens.append_all(self.attrs.outer());
1753 self.vis.to_tokens(tokens);
1754 self.existential_token.to_tokens(tokens);
1755 self.type_token.to_tokens(tokens);
1756 self.ident.to_tokens(tokens);
1757 self.generics.to_tokens(tokens);
1758 self.generics.where_clause.to_tokens(tokens);
1759 if !self.bounds.is_empty() {
1760 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1761 self.bounds.to_tokens(tokens);
1762 }
1763 self.semi_token.to_tokens(tokens);
1764 }
1765 }
1766
David Tolnay1bfa7332017-11-11 12:41:20 -08001767 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001768 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001769 tokens.append_all(self.attrs.outer());
1770 self.vis.to_tokens(tokens);
1771 self.enum_token.to_tokens(tokens);
1772 self.ident.to_tokens(tokens);
1773 self.generics.to_tokens(tokens);
1774 self.generics.where_clause.to_tokens(tokens);
1775 self.brace_token.surround(tokens, |tokens| {
1776 self.variants.to_tokens(tokens);
1777 });
1778 }
1779 }
1780
1781 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001782 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001783 tokens.append_all(self.attrs.outer());
1784 self.vis.to_tokens(tokens);
1785 self.struct_token.to_tokens(tokens);
1786 self.ident.to_tokens(tokens);
1787 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001788 match self.fields {
1789 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001790 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001791 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001792 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001793 Fields::Unnamed(ref fields) => {
1794 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001795 self.generics.where_clause.to_tokens(tokens);
1796 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001797 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001798 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001799 self.generics.where_clause.to_tokens(tokens);
1800 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001801 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001802 }
1803 }
1804 }
1805
1806 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001807 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001808 tokens.append_all(self.attrs.outer());
1809 self.vis.to_tokens(tokens);
1810 self.union_token.to_tokens(tokens);
1811 self.ident.to_tokens(tokens);
1812 self.generics.to_tokens(tokens);
1813 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001814 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001815 }
1816 }
1817
1818 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001819 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001820 tokens.append_all(self.attrs.outer());
1821 self.vis.to_tokens(tokens);
1822 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001823 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001824 self.trait_token.to_tokens(tokens);
1825 self.ident.to_tokens(tokens);
1826 self.generics.to_tokens(tokens);
1827 if !self.supertraits.is_empty() {
1828 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1829 self.supertraits.to_tokens(tokens);
1830 }
1831 self.generics.where_clause.to_tokens(tokens);
1832 self.brace_token.surround(tokens, |tokens| {
1833 tokens.append_all(&self.items);
1834 });
1835 }
1836 }
1837
David Tolnay1bfa7332017-11-11 12:41:20 -08001838 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001839 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001840 tokens.append_all(self.attrs.outer());
1841 self.defaultness.to_tokens(tokens);
1842 self.unsafety.to_tokens(tokens);
1843 self.impl_token.to_tokens(tokens);
1844 self.generics.to_tokens(tokens);
1845 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1846 polarity.to_tokens(tokens);
1847 path.to_tokens(tokens);
1848 for_token.to_tokens(tokens);
1849 }
1850 self.self_ty.to_tokens(tokens);
1851 self.generics.where_clause.to_tokens(tokens);
1852 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02001853 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001854 tokens.append_all(&self.items);
1855 });
1856 }
1857 }
1858
1859 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001860 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001861 tokens.append_all(self.attrs.outer());
1862 self.mac.path.to_tokens(tokens);
1863 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001864 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001865 match self.mac.delimiter {
1866 MacroDelimiter::Paren(ref paren) => {
1867 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1868 }
1869 MacroDelimiter::Brace(ref brace) => {
1870 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1871 }
1872 MacroDelimiter::Bracket(ref bracket) => {
1873 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1874 }
1875 }
David Tolnay57292da2017-12-27 21:03:33 -05001876 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001877 }
1878 }
David Tolnay42602292016-10-01 22:25:45 -07001879
David Tolnay500d8322017-12-18 00:32:51 -08001880 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001881 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08001882 tokens.append_all(self.attrs.outer());
1883 self.vis.to_tokens(tokens);
1884 self.macro_token.to_tokens(tokens);
1885 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001886 self.paren_token.surround(tokens, |tokens| {
1887 self.args.to_tokens(tokens);
1888 });
1889 self.brace_token.surround(tokens, |tokens| {
1890 self.body.to_tokens(tokens);
1891 });
David Tolnay500d8322017-12-18 00:32:51 -08001892 }
1893 }
1894
David Tolnay2ae520a2017-12-29 11:19:50 -05001895 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001896 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001897 self.tts.to_tokens(tokens);
1898 }
1899 }
1900
David Tolnay5f332a92017-12-26 00:42:45 -05001901 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001902 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05001903 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02001904 self.colon2_token.to_tokens(tokens);
1905 self.tree.to_tokens(tokens);
1906 }
1907 }
1908
1909 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001910 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001911 self.ident.to_tokens(tokens);
1912 }
1913 }
1914
1915 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001916 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001917 self.ident.to_tokens(tokens);
1918 self.as_token.to_tokens(tokens);
1919 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001920 }
1921 }
1922
David Tolnay5f332a92017-12-26 00:42:45 -05001923 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001924 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001925 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001926 }
1927 }
1928
David Tolnayd97a7d22018-03-31 19:17:01 +02001929 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001930 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001931 self.brace_token.surround(tokens, |tokens| {
1932 self.items.to_tokens(tokens);
1933 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001934 }
1935 }
1936
David Tolnay1bfa7332017-11-11 12:41:20 -08001937 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001938 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001939 tokens.append_all(self.attrs.outer());
1940 self.const_token.to_tokens(tokens);
1941 self.ident.to_tokens(tokens);
1942 self.colon_token.to_tokens(tokens);
1943 self.ty.to_tokens(tokens);
1944 if let Some((ref eq_token, ref default)) = self.default {
1945 eq_token.to_tokens(tokens);
1946 default.to_tokens(tokens);
1947 }
1948 self.semi_token.to_tokens(tokens);
1949 }
1950 }
1951
1952 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001953 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001954 tokens.append_all(self.attrs.outer());
1955 self.sig.to_tokens(tokens);
1956 match self.default {
1957 Some(ref block) => {
1958 block.brace_token.surround(tokens, |tokens| {
1959 tokens.append_all(self.attrs.inner());
1960 tokens.append_all(&block.stmts);
1961 });
David Tolnayca085422016-10-04 00:12:38 -07001962 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001963 None => {
1964 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001965 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001966 }
1967 }
1968 }
1969
1970 impl ToTokens for TraitItemType {
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.type_token.to_tokens(tokens);
1974 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001975 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001976 if !self.bounds.is_empty() {
1977 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1978 self.bounds.to_tokens(tokens);
1979 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001980 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001981 if let Some((ref eq_token, ref default)) = self.default {
1982 eq_token.to_tokens(tokens);
1983 default.to_tokens(tokens);
1984 }
1985 self.semi_token.to_tokens(tokens);
1986 }
1987 }
1988
1989 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001990 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001991 tokens.append_all(self.attrs.outer());
1992 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001993 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001994 }
1995 }
1996
David Tolnay2ae520a2017-12-29 11:19:50 -05001997 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001998 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001999 self.tts.to_tokens(tokens);
2000 }
2001 }
2002
David Tolnay857628c2017-11-11 12:25:31 -08002003 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002004 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002005 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002006 self.vis.to_tokens(tokens);
2007 self.defaultness.to_tokens(tokens);
2008 self.const_token.to_tokens(tokens);
2009 self.ident.to_tokens(tokens);
2010 self.colon_token.to_tokens(tokens);
2011 self.ty.to_tokens(tokens);
2012 self.eq_token.to_tokens(tokens);
2013 self.expr.to_tokens(tokens);
2014 self.semi_token.to_tokens(tokens);
2015 }
2016 }
2017
2018 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002019 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002020 tokens.append_all(self.attrs.outer());
2021 self.vis.to_tokens(tokens);
2022 self.defaultness.to_tokens(tokens);
2023 self.sig.to_tokens(tokens);
2024 self.block.brace_token.surround(tokens, |tokens| {
2025 tokens.append_all(self.attrs.inner());
2026 tokens.append_all(&self.block.stmts);
2027 });
2028 }
2029 }
2030
2031 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002032 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002033 tokens.append_all(self.attrs.outer());
2034 self.vis.to_tokens(tokens);
2035 self.defaultness.to_tokens(tokens);
2036 self.type_token.to_tokens(tokens);
2037 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002038 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002039 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002040 self.eq_token.to_tokens(tokens);
2041 self.ty.to_tokens(tokens);
2042 self.semi_token.to_tokens(tokens);
2043 }
2044 }
2045
David Tolnaybb82ef02018-08-24 20:15:45 -04002046 impl ToTokens for ImplItemExistential {
2047 fn to_tokens(&self, tokens: &mut TokenStream) {
2048 tokens.append_all(self.attrs.outer());
2049 self.existential_token.to_tokens(tokens);
2050 self.type_token.to_tokens(tokens);
2051 self.ident.to_tokens(tokens);
2052 self.generics.to_tokens(tokens);
2053 self.generics.where_clause.to_tokens(tokens);
2054 if !self.bounds.is_empty() {
2055 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2056 self.bounds.to_tokens(tokens);
2057 }
2058 self.semi_token.to_tokens(tokens);
2059 }
2060 }
2061
David Tolnay857628c2017-11-11 12:25:31 -08002062 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002063 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002064 tokens.append_all(self.attrs.outer());
2065 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002066 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002067 }
2068 }
2069
David Tolnay2ae520a2017-12-29 11:19:50 -05002070 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002071 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002072 self.tts.to_tokens(tokens);
2073 }
2074 }
2075
David Tolnay8894f602017-11-11 12:11:04 -08002076 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002077 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002078 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002079 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002080 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002081 self.semi_token.to_tokens(tokens);
2082 }
2083 }
2084
2085 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002086 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002087 tokens.append_all(self.attrs.outer());
2088 self.vis.to_tokens(tokens);
2089 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002090 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002091 self.ident.to_tokens(tokens);
2092 self.colon_token.to_tokens(tokens);
2093 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002094 self.semi_token.to_tokens(tokens);
2095 }
2096 }
2097
David Tolnay199bcbb2017-11-12 10:33:52 -08002098 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002099 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002100 tokens.append_all(self.attrs.outer());
2101 self.vis.to_tokens(tokens);
2102 self.type_token.to_tokens(tokens);
2103 self.ident.to_tokens(tokens);
2104 self.semi_token.to_tokens(tokens);
2105 }
2106 }
2107
David Tolnay435c1782018-08-24 16:15:44 -04002108 impl ToTokens for ForeignItemMacro {
2109 fn to_tokens(&self, tokens: &mut TokenStream) {
2110 tokens.append_all(self.attrs.outer());
2111 self.mac.to_tokens(tokens);
2112 self.semi_token.to_tokens(tokens);
2113 }
2114 }
2115
David Tolnay2ae520a2017-12-29 11:19:50 -05002116 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002117 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002118 self.tts.to_tokens(tokens);
2119 }
2120 }
2121
David Tolnay570695e2017-06-03 16:15:13 -07002122 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002123 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002124 self.constness.to_tokens(tokens);
2125 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002126 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002127 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002128 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002129 }
2130 }
2131
Alex Crichtona74a1c82018-05-16 10:20:44 -07002132 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002133
2134 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002135 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002136 self.0.fn_token.to_tokens(tokens);
2137 self.1.to_tokens(tokens);
2138 self.0.generics.to_tokens(tokens);
2139 self.0.paren_token.surround(tokens, |tokens| {
2140 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002141 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2142 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002143 }
David Tolnayd2836e22017-12-27 23:13:00 -05002144 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002145 });
2146 self.0.output.to_tokens(tokens);
2147 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002148 }
2149 }
2150
Alex Crichton62a0a592017-05-22 13:58:53 -07002151 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002152 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002153 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002154 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002155 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002156 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002157 }
2158 }
2159
2160 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002161 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002162 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002163 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002164 }
2165 }
2166
2167 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002168 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002169 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002170 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002171 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002172 }
2173 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002174}