blob: 078c737194ca00a2c665adcc26c4f2069d3f12f9 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
David Tolnaybb82ef02018-08-24 20:15:45 -0400139 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
142 pub Existential(ItemExistential {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub existential_token: Token![existential],
146 pub type_token: Token![type],
147 pub ident: Ident,
148 pub generics: Generics,
149 pub colon_token: Option<Token![:]>,
150 pub bounds: Punctuated<TypeParamBound, Token![+]>,
151 pub semi_token: Token![;],
152 }),
153
David Tolnay2b214082018-01-07 01:30:18 -0800154 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800155 ///
156 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 pub Struct(ItemStruct {
158 pub attrs: Vec<Attribute>,
159 pub vis: Visibility,
160 pub struct_token: Token![struct],
161 pub ident: Ident,
162 pub generics: Generics,
163 pub fields: Fields,
164 pub semi_token: Option<Token![;]>,
165 }),
David Tolnay2b214082018-01-07 01:30:18 -0800166
167 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800168 ///
169 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700174 pub ident: Ident,
175 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
David Tolnay2b214082018-01-07 01:30:18 -0800179
180 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800181 ///
182 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700187 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500189 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 }),
David Tolnay2b214082018-01-07 01:30:18 -0800191
192 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800193 ///
194 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700197 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500198 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500199 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800200 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700201 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500205 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub items: Vec<TraitItem>,
207 }),
David Tolnay2b214082018-01-07 01:30:18 -0800208
209 /// An impl block providing trait or associated items: `impl<A> Trait
210 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800211 ///
212 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800214 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500215 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500216 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700219 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500220 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700221 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub items: Vec<ImplItem>,
225 }),
David Tolnay2b214082018-01-07 01:30:18 -0800226
227 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800228 ///
229 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800230 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800231 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800232 /// The `example` in `macro_rules! example { ... }`.
233 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800234 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500235 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800236 }),
David Tolnay2b214082018-01-07 01:30:18 -0800237
238 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500241 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800242 pub attrs: Vec<Attribute>,
243 pub vis: Visibility,
244 pub macro_token: Token![macro],
245 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500246 pub paren_token: Paren,
247 pub args: TokenStream,
248 pub brace_token: Brace,
249 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500255 pub Verbatim(ItemVerbatim #manual_extra_traits {
256 pub tts: TokenStream,
257 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
David Tolnayb79ee962016-09-04 09:39:20 -0700259}
260
David Tolnay9c76bcb2017-12-26 23:14:59 -0500261#[cfg(feature = "extra-traits")]
262impl Eq for ItemMacro2 {}
263
264#[cfg(feature = "extra-traits")]
265impl PartialEq for ItemMacro2 {
266 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700267 self.attrs == other.attrs
268 && self.vis == other.vis
269 && self.macro_token == other.macro_token
270 && self.ident == other.ident
271 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500272 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
273 && self.brace_token == other.brace_token
274 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275 }
276}
277
278#[cfg(feature = "extra-traits")]
279impl Hash for ItemMacro2 {
280 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500281 where
282 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500283 {
284 self.attrs.hash(state);
285 self.vis.hash(state);
286 self.macro_token.hash(state);
287 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500288 self.paren_token.hash(state);
289 TokenStreamHelper(&self.args).hash(state);
290 self.brace_token.hash(state);
291 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500292 }
293}
294
David Tolnay2ae520a2017-12-29 11:19:50 -0500295#[cfg(feature = "extra-traits")]
296impl Eq for ItemVerbatim {}
297
298#[cfg(feature = "extra-traits")]
299impl PartialEq for ItemVerbatim {
300 fn eq(&self, other: &Self) -> bool {
301 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
302 }
303}
304
305#[cfg(feature = "extra-traits")]
306impl Hash for ItemVerbatim {
307 fn hash<H>(&self, state: &mut H)
308 where
309 H: Hasher,
310 {
311 TokenStreamHelper(&self.tts).hash(state);
312 }
313}
314
David Tolnay0e837402016-12-22 17:25:55 -0500315impl From<DeriveInput> for Item {
316 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500317 match input.data {
318 Data::Struct(data) => Item::Struct(ItemStruct {
319 attrs: input.attrs,
320 vis: input.vis,
321 struct_token: data.struct_token,
322 ident: input.ident,
323 generics: input.generics,
324 fields: data.fields,
325 semi_token: data.semi_token,
326 }),
327 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500328 attrs: input.attrs,
329 vis: input.vis,
330 enum_token: data.enum_token,
331 ident: input.ident,
332 generics: input.generics,
333 brace_token: data.brace_token,
334 variants: data.variants,
335 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500337 attrs: input.attrs,
338 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500339 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500340 ident: input.ident,
341 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500343 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700344 }
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800350 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800351 /// *This type is available if Syn is built with the `"full"` feature.*
352 ///
David Tolnay614a0142018-01-07 10:25:43 -0800353 /// # Syntax tree enum
354 ///
355 /// This type is a [syntax tree enum].
356 ///
357 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500358 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200359 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500362 pub Path(UsePath {
363 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200364 pub colon2_token: Token![::],
365 pub tree: Box<UseTree>,
366 }),
367
368 /// An identifier imported by a `use` item: `HashMap`.
369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
371 pub Name(UseName {
372 pub ident: Ident,
373 }),
374
375 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
378 pub Rename(UseRename {
379 pub ident: Ident,
380 pub as_token: Token![as],
381 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800383
David Tolnay05658502018-01-07 09:56:37 -0800384 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500387 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800390
David Tolnayd97a7d22018-03-31 19:17:01 +0200391 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800392 ///
393 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200394 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500396 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 }
399}
400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800402 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800403 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800404 /// *This type is available if Syn is built with the `"full"` feature.*
405 ///
David Tolnay614a0142018-01-07 10:25:43 -0800406 /// # Syntax tree enum
407 ///
408 /// This type is a [syntax tree enum].
409 ///
410 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800412 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800413 ///
414 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub attrs: Vec<Attribute>,
417 pub vis: Visibility,
418 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }),
David Tolnayebb72722018-01-07 01:14:13 -0800422
423 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800427 pub attrs: Vec<Attribute>,
428 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500430 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800431 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
David Tolnayebb72722018-01-07 01:14:13 -0800436
437 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800440 pub Type(ForeignItemType {
441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
443 pub type_token: Token![type],
444 pub ident: Ident,
445 pub semi_token: Token![;],
446 }),
David Tolnayebb72722018-01-07 01:14:13 -0800447
David Tolnay435c1782018-08-24 16:15:44 -0400448 /// A macro invocation within an extern block.
449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
451 pub Macro(ForeignItemMacro {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 pub semi_token: Option<Token![;]>,
455 }),
456
David Tolnayebb72722018-01-07 01:14:13 -0800457 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500460 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
461 pub tts: TokenStream,
462 }),
463 }
464}
465
466#[cfg(feature = "extra-traits")]
467impl Eq for ForeignItemVerbatim {}
468
469#[cfg(feature = "extra-traits")]
470impl PartialEq for ForeignItemVerbatim {
471 fn eq(&self, other: &Self) -> bool {
472 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
473 }
474}
475
476#[cfg(feature = "extra-traits")]
477impl Hash for ForeignItemVerbatim {
478 fn hash<H>(&self, state: &mut H)
479 where
480 H: Hasher,
481 {
482 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700484}
485
David Tolnayda705bd2017-11-10 21:58:05 -0800486ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800487 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800488 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800489 /// *This type is available if Syn is built with the `"full"` feature.*
490 ///
David Tolnay614a0142018-01-07 10:25:43 -0800491 /// # Syntax tree enum
492 ///
493 /// This type is a [syntax tree enum].
494 ///
495 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800496 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800497 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800498 ///
499 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800501 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700503 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub default: Option<(Token![=], Expr)>,
507 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
David Tolnayebb72722018-01-07 01:14:13 -0800509
510 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800514 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 pub sig: MethodSig,
516 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
David Tolnayebb72722018-01-07 01:14:13 -0800519
520 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700526 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500527 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800535 ///
536 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800537 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800539 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500540 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800541 }),
David Tolnayebb72722018-01-07 01:14:13 -0800542
543 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800544 ///
545 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500546 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
547 pub tts: TokenStream,
548 }),
549 }
550}
551
552#[cfg(feature = "extra-traits")]
553impl Eq for TraitItemVerbatim {}
554
555#[cfg(feature = "extra-traits")]
556impl PartialEq for TraitItemVerbatim {
557 fn eq(&self, other: &Self) -> bool {
558 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
559 }
560}
561
562#[cfg(feature = "extra-traits")]
563impl Hash for TraitItemVerbatim {
564 fn hash<H>(&self, state: &mut H)
565 where
566 H: Hasher,
567 {
568 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800573 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800574 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800575 /// *This type is available if Syn is built with the `"full"` feature.*
576 ///
David Tolnay614a0142018-01-07 10:25:43 -0800577 /// # Syntax tree enum
578 ///
579 /// This type is a [syntax tree enum].
580 ///
581 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800582 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800583 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700591 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }),
David Tolnayebb72722018-01-07 01:14:13 -0800598
599 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800600 ///
601 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700602 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700604 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500605 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 pub sig: MethodSig,
607 pub block: Block,
608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800614 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700615 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500616 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500619 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 }),
David Tolnayebb72722018-01-07 01:14:13 -0800624
David Tolnaybb82ef02018-08-24 20:15:45 -0400625 /// An existential type within an impl block.
626 ///
627 /// *This type is available if Syn is built with the `"full"` feature.*
628 pub Existential(ImplItemExistential {
629 pub attrs: Vec<Attribute>,
630 pub existential_token: Token![existential],
631 pub type_token: Token![type],
632 pub ident: Ident,
633 pub generics: Generics,
634 pub colon_token: Option<Token![:]>,
635 pub bounds: Punctuated<TypeParamBound, Token![+]>,
636 pub semi_token: Token![;],
637 }),
638
David Tolnayebb72722018-01-07 01:14:13 -0800639 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800642 pub Macro(ImplItemMacro {
643 pub attrs: Vec<Attribute>,
644 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500645 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800646 }),
David Tolnayebb72722018-01-07 01:14:13 -0800647
648 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800649 ///
650 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500651 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
652 pub tts: TokenStream,
653 }),
654 }
655}
656
657#[cfg(feature = "extra-traits")]
658impl Eq for ImplItemVerbatim {}
659
660#[cfg(feature = "extra-traits")]
661impl PartialEq for ImplItemVerbatim {
662 fn eq(&self, other: &Self) -> bool {
663 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
664 }
665}
666
667#[cfg(feature = "extra-traits")]
668impl Hash for ImplItemVerbatim {
669 fn hash<H>(&self, state: &mut H)
670 where
671 H: Hasher,
672 {
673 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700675}
676
677ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800678 /// A method's signature in a trait or implementation: `unsafe fn
679 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500683 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500684 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900685 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700687 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }
690}
691
692ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800693 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500698 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500699 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500700 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500701 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500702 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700704}
705
Alex Crichton62a0a592017-05-22 13:58:53 -0700706ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800707 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800708 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800709 /// *This type is available if Syn is built with the `"full"` feature.*
710 ///
David Tolnay614a0142018-01-07 10:25:43 -0800711 /// # Syntax tree enum
712 ///
713 /// This type is a [syntax tree enum].
714 ///
715 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// Self captured by reference in a function signature: `&self` or `&mut
718 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500725 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800727
David Tolnay3f559052018-01-06 23:59:48 -0800728 /// Self captured by value in a function signature: `self` or `mut
729 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800736
David Tolnay3f559052018-01-06 23:59:48 -0800737 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800738 ///
739 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 pub Captured(ArgCaptured {
741 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800743 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800745
David Tolnay3f559052018-01-06 23:59:48 -0800746 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500747 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800748 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }
David Tolnay62f374c2016-10-02 13:37:00 -0700751}
752
David Tolnayedf2b992016-09-23 20:43:45 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700756
David Tolnay2ad62c12018-08-26 19:00:35 -0400757 use parse::{Parse, ParseStream, Result};
David Tolnay3779bb72018-08-26 18:46:07 -0700758 use synom::ext::IdentExt;
David Tolnay84aa0752016-10-02 23:01:13 -0700759
David Tolnay6a170ce2018-08-26 22:29:24 -0700760 impl Parse for Item {
761 fn parse(input: ParseStream) -> Result<Self> {
762 let ahead = input.fork();
763 ahead.call(Attribute::parse_outer)?;
764 let vis: Visibility = ahead.parse()?;
765
766 let lookahead = ahead.lookahead1();
767 if lookahead.peek(Token![extern]) {
768 ahead.parse::<Token![extern]>()?;
769 let lookahead = ahead.lookahead1();
770 if lookahead.peek(Token![crate]) {
771 input.parse().map(Item::ExternCrate)
772 } else if lookahead.peek(Token![fn]) {
773 input.parse().map(Item::Fn)
774 } else if lookahead.peek(token::Brace) {
775 input.parse().map(Item::ForeignMod)
776 } else if lookahead.peek(LitStr) {
777 ahead.parse::<LitStr>()?;
778 let lookahead = ahead.lookahead1();
779 if lookahead.peek(token::Brace) {
780 input.parse().map(Item::ForeignMod)
781 } else if lookahead.peek(Token![fn]) {
782 input.parse().map(Item::Fn)
783 } else {
784 Err(lookahead.error())
785 }
786 } else {
787 Err(lookahead.error())
788 }
789 } else if lookahead.peek(Token![use]) {
790 input.parse().map(Item::Use)
791 } else if lookahead.peek(Token![static]) {
792 input.parse().map(Item::Static)
793 } else if lookahead.peek(Token![const]) {
794 ahead.parse::<Token![const]>()?;
795 let lookahead = ahead.lookahead1();
796 if lookahead.peek(Ident) {
797 input.parse().map(Item::Const)
798 } else if lookahead.peek(Token![unsafe])
799 || lookahead.peek(Token![async])
800 || lookahead.peek(Token![extern])
801 || lookahead.peek(Token![fn])
802 {
803 input.parse().map(Item::Fn)
804 } else {
805 Err(lookahead.error())
806 }
807 } else if lookahead.peek(Token![unsafe]) {
808 ahead.parse::<Token![unsafe]>()?;
809 let lookahead = ahead.lookahead1();
810 if lookahead.peek(Token![trait])
811 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
812 {
813 input.parse().map(Item::Trait)
814 } else if lookahead.peek(Token![impl ]) {
815 input.parse().map(Item::Impl)
816 } else if lookahead.peek(Token![async])
817 || lookahead.peek(Token![extern])
818 || lookahead.peek(Token![fn])
819 {
820 input.parse().map(Item::Fn)
821 } else {
822 Err(lookahead.error())
823 }
824 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
825 input.parse().map(Item::Fn)
826 } else if lookahead.peek(Token![mod]) {
827 input.parse().map(Item::Mod)
828 } else if lookahead.peek(Token![type]) {
829 input.parse().map(Item::Type)
830 } else if lookahead.peek(Token![existential]) {
831 input.parse().map(Item::Existential)
832 } else if lookahead.peek(Token![struct]) {
833 input.parse().map(Item::Struct)
834 } else if lookahead.peek(Token![enum]) {
835 input.parse().map(Item::Enum)
836 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
837 input.parse().map(Item::Union)
838 } else if lookahead.peek(Token![trait])
839 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
840 {
841 input.parse().map(Item::Trait)
842 } else if lookahead.peek(Token![impl ])
843 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
844 {
845 input.parse().map(Item::Impl)
846 } else if lookahead.peek(Token![macro]) {
847 input.parse().map(Item::Macro2)
848 } else if vis.is_inherited()
849 && (lookahead.peek(Ident)
850 || lookahead.peek(Token![self])
851 || lookahead.peek(Token![super])
852 || lookahead.peek(Token![extern])
853 || lookahead.peek(Token![crate])
854 || lookahead.peek(Token![::]))
855 {
856 input.parse().map(Item::Macro)
857 } else {
858 Err(lookahead.error())
859 }
860 }
861 }
Alex Crichton954046c2017-05-30 21:49:42 -0700862
David Tolnay3779bb72018-08-26 18:46:07 -0700863 impl Parse for ItemMacro {
864 fn parse(input: ParseStream) -> Result<Self> {
865 let attrs = input.call(Attribute::parse_outer)?;
866 let path = input.call(Path::parse_mod_style)?;
867 let bang_token: Token![!] = input.parse()?;
868 let ident: Option<Ident> = input.parse()?;
869 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700870 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700871 Some(input.parse()?)
872 } else {
873 None
874 };
875 Ok(ItemMacro {
876 attrs: attrs,
877 ident: ident,
878 mac: Macro {
879 path: path,
880 bang_token: bang_token,
881 delimiter: delimiter,
882 tts: tts,
883 },
884 semi_token: semi_token,
885 })
886 }
887 }
David Tolnayedf2b992016-09-23 20:43:45 -0700888
David Tolnay500d8322017-12-18 00:32:51 -0800889 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700890 impl Parse for ItemMacro2 {
891 fn parse(input: ParseStream) -> Result<Self> {
892 let args;
893 let body;
894 Ok(ItemMacro2 {
895 attrs: input.call(Attribute::parse_outer)?,
896 vis: input.parse()?,
897 macro_token: input.parse()?,
898 ident: input.parse()?,
899 paren_token: parenthesized!(args in input),
900 args: args.parse()?,
901 brace_token: braced!(body in input),
902 body: body.parse()?,
903 })
904 }
905 }
David Tolnay500d8322017-12-18 00:32:51 -0800906
David Tolnay3779bb72018-08-26 18:46:07 -0700907 impl Parse for ItemExternCrate {
908 fn parse(input: ParseStream) -> Result<Self> {
909 Ok(ItemExternCrate {
910 attrs: input.call(Attribute::parse_outer)?,
911 vis: input.parse()?,
912 extern_token: input.parse()?,
913 crate_token: input.parse()?,
914 ident: input.parse()?,
915 rename: {
916 if input.peek(Token![as]) {
917 let as_token: Token![as] = input.parse()?;
918 let rename: Ident = input.parse()?;
919 Some((as_token, rename))
920 } else {
921 None
922 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800923 },
David Tolnay3779bb72018-08-26 18:46:07 -0700924 semi_token: input.parse()?,
925 })
926 }
927 }
928
929 impl Parse for ItemUse {
930 fn parse(input: ParseStream) -> Result<Self> {
931 Ok(ItemUse {
932 attrs: input.call(Attribute::parse_outer)?,
933 vis: input.parse()?,
934 use_token: input.parse()?,
935 leading_colon: input.parse()?,
936 tree: input.call(use_tree)?,
937 semi_token: input.parse()?,
938 })
939 }
940 }
941
942 fn use_tree(input: ParseStream) -> Result<UseTree> {
943 let lookahead = input.lookahead1();
944 if lookahead.peek(Ident)
945 || lookahead.peek(Token![self])
946 || lookahead.peek(Token![super])
947 || lookahead.peek(Token![crate])
948 || lookahead.peek(Token![extern])
949 {
950 let ident = input.call(Ident::parse_any2)?;
951 if input.peek(Token![::]) {
952 Ok(UseTree::Path(UsePath {
953 ident: ident,
954 colon2_token: input.parse()?,
955 tree: Box::new(input.call(use_tree)?),
956 }))
957 } else if input.peek(Token![as]) {
958 Ok(UseTree::Rename(UseRename {
959 ident: ident,
960 as_token: input.parse()?,
961 rename: input.parse()?,
962 }))
963 } else {
964 Ok(UseTree::Name(UseName { ident: ident }))
965 }
966 } else if lookahead.peek(Token![*]) {
967 Ok(UseTree::Glob(UseGlob {
968 star_token: input.parse()?,
969 }))
970 } else if lookahead.peek(token::Brace) {
971 let content;
972 Ok(UseTree::Group(UseGroup {
973 brace_token: braced!(content in input),
974 items: content.parse_terminated(use_tree)?,
975 }))
976 } else {
977 Err(lookahead.error())
978 }
979 }
980
981 impl Parse for ItemStatic {
982 fn parse(input: ParseStream) -> Result<Self> {
983 Ok(ItemStatic {
984 attrs: input.call(Attribute::parse_outer)?,
985 vis: input.parse()?,
986 static_token: input.parse()?,
987 mutability: input.parse()?,
988 ident: input.parse()?,
989 colon_token: input.parse()?,
990 ty: input.parse()?,
991 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700992 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -0700993 semi_token: input.parse()?,
994 })
995 }
996 }
997
998 impl Parse for ItemConst {
999 fn parse(input: ParseStream) -> Result<Self> {
1000 Ok(ItemConst {
1001 attrs: input.call(Attribute::parse_outer)?,
1002 vis: input.parse()?,
1003 const_token: input.parse()?,
1004 ident: input.parse()?,
1005 colon_token: input.parse()?,
1006 ty: input.parse()?,
1007 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001008 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001009 semi_token: input.parse()?,
1010 })
1011 }
1012 }
1013
1014 impl Parse for ItemFn {
1015 fn parse(input: ParseStream) -> Result<Self> {
1016 let outer_attrs = input.call(Attribute::parse_outer)?;
1017 let vis: Visibility = input.parse()?;
1018 let constness: Option<Token![const]> = input.parse()?;
1019 let unsafety: Option<Token![unsafe]> = input.parse()?;
1020 let asyncness: Option<Token![async]> = input.parse()?;
1021 let abi: Option<Abi> = input.parse()?;
1022 let fn_token: Token![fn] = input.parse()?;
1023 let ident: Ident = input.parse()?;
1024 let generics: Generics = input.parse()?;
1025
1026 let content;
1027 let paren_token = parenthesized!(content in input);
1028 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1029
1030 let output: ReturnType = input.parse()?;
1031 let where_clause: Option<WhereClause> = input.parse()?;
1032
1033 let content;
1034 let brace_token = braced!(content in input);
1035 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001036 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001037
1038 Ok(ItemFn {
1039 attrs: {
1040 let mut attrs = outer_attrs;
1041 attrs.extend(inner_attrs);
1042 attrs
1043 },
1044 vis: vis,
1045 constness: constness,
1046 unsafety: unsafety,
1047 asyncness: asyncness,
1048 abi: abi,
1049 ident: ident,
1050 decl: Box::new(FnDecl {
1051 fn_token: fn_token,
1052 paren_token: paren_token,
1053 inputs: inputs,
1054 output: output,
1055 variadic: None,
1056 generics: Generics {
1057 where_clause: where_clause,
1058 ..generics
1059 },
1060 }),
1061 block: Box::new(Block {
1062 brace_token: brace_token,
1063 stmts: stmts,
1064 }),
1065 })
1066 }
1067 }
David Tolnay42602292016-10-01 22:25:45 -07001068
David Tolnay2ad62c12018-08-26 19:00:35 -04001069 impl Parse for FnArg {
1070 fn parse(input: ParseStream) -> Result<Self> {
1071 if input.peek(Token![&]) {
1072 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001073 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1074 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001075 }
1076 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001077
David Tolnay2ad62c12018-08-26 19:00:35 -04001078 if input.peek(Token![mut]) || input.peek(Token![self]) {
1079 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001080 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1081 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001082 }
1083 }
1084
1085 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001086 let err = match ahead.call(arg_captured) {
1087 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001088 Err(err) => err,
1089 };
1090
1091 let ahead = input.fork();
1092 if ahead.parse::<Type>().is_ok() {
1093 return input.parse().map(FnArg::Ignored);
1094 }
1095
1096 Err(err)
1097 }
1098 }
1099
David Tolnay3779bb72018-08-26 18:46:07 -07001100 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1101 Ok(ArgSelfRef {
1102 and_token: input.parse()?,
1103 lifetime: input.parse()?,
1104 mutability: input.parse()?,
1105 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001106 })
David Tolnay3779bb72018-08-26 18:46:07 -07001107 }
David Tolnay35902302016-10-06 01:11:08 -07001108
David Tolnay3779bb72018-08-26 18:46:07 -07001109 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1110 Ok(ArgSelf {
1111 mutability: input.parse()?,
1112 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001113 })
David Tolnay3779bb72018-08-26 18:46:07 -07001114 }
1115
1116 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1117 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001118 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001119 colon_token: input.parse()?,
1120 ty: input.parse()?,
1121 })
1122 }
1123
1124 impl Parse for ItemMod {
1125 fn parse(input: ParseStream) -> Result<Self> {
1126 let outer_attrs = input.call(Attribute::parse_outer)?;
1127 let vis: Visibility = input.parse()?;
1128 let mod_token: Token![mod] = input.parse()?;
1129 let ident: Ident = input.parse()?;
1130
1131 let lookahead = input.lookahead1();
1132 if lookahead.peek(Token![;]) {
1133 Ok(ItemMod {
1134 attrs: outer_attrs,
1135 vis: vis,
1136 mod_token: mod_token,
1137 ident: ident,
1138 content: None,
1139 semi: Some(input.parse()?),
1140 })
1141 } else if lookahead.peek(token::Brace) {
1142 let content;
1143 let brace_token = braced!(content in input);
1144 let inner_attrs = content.call(Attribute::parse_inner)?;
1145
1146 let mut items = Vec::new();
1147 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001148 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001149 }
1150
1151 Ok(ItemMod {
1152 attrs: {
1153 let mut attrs = outer_attrs;
1154 attrs.extend(inner_attrs);
1155 attrs
1156 },
1157 vis: vis,
1158 mod_token: mod_token,
1159 ident: ident,
1160 content: Some((brace_token, items)),
1161 semi: None,
1162 })
1163 } else {
1164 Err(lookahead.error())
1165 }
1166 }
1167 }
1168
1169 impl Parse for ItemForeignMod {
1170 fn parse(input: ParseStream) -> Result<Self> {
1171 let outer_attrs = input.call(Attribute::parse_outer)?;
1172 let abi: Abi = input.parse()?;
1173
1174 let content;
1175 let brace_token = braced!(content in input);
1176 let inner_attrs = content.call(Attribute::parse_inner)?;
1177 let mut items = Vec::new();
1178 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001179 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001180 }
1181
1182 Ok(ItemForeignMod {
1183 attrs: {
1184 let mut attrs = outer_attrs;
1185 attrs.extend(inner_attrs);
1186 attrs
1187 },
1188 abi: abi,
1189 brace_token: brace_token,
1190 items: items,
1191 })
1192 }
1193 }
David Tolnay35902302016-10-06 01:11:08 -07001194
David Tolnay6a170ce2018-08-26 22:29:24 -07001195 impl Parse for ForeignItem {
1196 fn parse(input: ParseStream) -> Result<Self> {
1197 let ahead = input.fork();
1198 ahead.call(Attribute::parse_outer)?;
1199 let vis: Visibility = ahead.parse()?;
1200
1201 let lookahead = ahead.lookahead1();
1202 if lookahead.peek(Token![fn]) {
1203 input.parse().map(ForeignItem::Fn)
1204 } else if lookahead.peek(Token![static]) {
1205 input.parse().map(ForeignItem::Static)
1206 } else if lookahead.peek(Token![type]) {
1207 input.parse().map(ForeignItem::Type)
1208 } else if vis.is_inherited()
1209 && (lookahead.peek(Ident)
1210 || lookahead.peek(Token![self])
1211 || lookahead.peek(Token![super])
1212 || lookahead.peek(Token![extern])
1213 || lookahead.peek(Token![crate])
1214 || lookahead.peek(Token![::]))
1215 {
1216 input.parse().map(ForeignItem::Macro)
1217 } else {
1218 Err(lookahead.error())
1219 }
1220 }
1221 }
David Tolnay35902302016-10-06 01:11:08 -07001222
David Tolnay3779bb72018-08-26 18:46:07 -07001223 impl Parse for ForeignItemFn {
1224 fn parse(input: ParseStream) -> Result<Self> {
1225 let attrs = input.call(Attribute::parse_outer)?;
1226 let vis: Visibility = input.parse()?;
1227 let fn_token: Token![fn] = input.parse()?;
1228 let ident: Ident = input.parse()?;
1229 let generics: Generics = input.parse()?;
1230
1231 let content;
1232 let paren_token = parenthesized!(content in input);
1233 let inputs = content.parse_synom(Punctuated::parse_terminated)?;
1234 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1235 content.parse()?
1236 } else {
1237 None
1238 };
1239
1240 let output: ReturnType = input.parse()?;
1241 let where_clause: Option<WhereClause> = input.parse()?;
1242 let semi_token: Token![;] = input.parse()?;
1243
1244 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001245 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001246 vis: vis,
1247 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001248 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001249 fn_token: fn_token,
1250 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001251 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001252 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001253 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001254 generics: Generics {
1255 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001256 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001257 },
1258 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001259 semi_token: semi_token,
1260 })
1261 }
1262 }
David Tolnay35902302016-10-06 01:11:08 -07001263
David Tolnay3779bb72018-08-26 18:46:07 -07001264 impl Parse for ForeignItemStatic {
1265 fn parse(input: ParseStream) -> Result<Self> {
1266 Ok(ForeignItemStatic {
1267 attrs: input.call(Attribute::parse_outer)?,
1268 vis: input.parse()?,
1269 static_token: input.parse()?,
1270 mutability: input.parse()?,
1271 ident: input.parse()?,
1272 colon_token: input.parse()?,
1273 ty: input.parse()?,
1274 semi_token: input.parse()?,
1275 })
1276 }
1277 }
David Tolnay35902302016-10-06 01:11:08 -07001278
David Tolnay3779bb72018-08-26 18:46:07 -07001279 impl Parse for ForeignItemType {
1280 fn parse(input: ParseStream) -> Result<Self> {
1281 Ok(ForeignItemType {
1282 attrs: input.call(Attribute::parse_outer)?,
1283 vis: input.parse()?,
1284 type_token: input.parse()?,
1285 ident: input.parse()?,
1286 semi_token: input.parse()?,
1287 })
1288 }
1289 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001290
David Tolnay3779bb72018-08-26 18:46:07 -07001291 impl Parse for ForeignItemMacro {
1292 fn parse(input: ParseStream) -> Result<Self> {
1293 let attrs = input.call(Attribute::parse_outer)?;
1294 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001295 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001296 None
1297 } else {
1298 Some(input.parse()?)
1299 };
1300 Ok(ForeignItemMacro {
1301 attrs: attrs,
1302 mac: mac,
1303 semi_token: semi_token,
1304 })
1305 }
1306 }
David Tolnay78572112018-08-01 00:36:18 -07001307
David Tolnay3779bb72018-08-26 18:46:07 -07001308 impl Parse for ItemType {
1309 fn parse(input: ParseStream) -> Result<Self> {
1310 Ok(ItemType {
1311 attrs: input.call(Attribute::parse_outer)?,
1312 vis: input.parse()?,
1313 type_token: input.parse()?,
1314 ident: input.parse()?,
1315 generics: {
1316 let mut generics: Generics = input.parse()?;
1317 generics.where_clause = input.parse()?;
1318 generics
1319 },
1320 eq_token: input.parse()?,
1321 ty: input.parse()?,
1322 semi_token: input.parse()?,
1323 })
1324 }
1325 }
David Tolnay3cf52982016-10-01 17:11:37 -07001326
David Tolnay3779bb72018-08-26 18:46:07 -07001327 impl Parse for ItemExistential {
1328 fn parse(input: ParseStream) -> Result<Self> {
1329 Ok(ItemExistential {
1330 attrs: input.call(Attribute::parse_outer)?,
1331 vis: input.parse()?,
1332 existential_token: input.parse()?,
1333 type_token: input.parse()?,
1334 ident: input.parse()?,
1335 generics: {
1336 let mut generics: Generics = input.parse()?;
1337 generics.where_clause = input.parse()?;
1338 generics
1339 },
1340 colon_token: Some(input.parse()?),
1341 bounds: input.parse_synom(Punctuated::parse_separated_nonempty)?,
1342 semi_token: input.parse()?,
1343 })
1344 }
1345 }
David Tolnay758ee132018-08-21 21:29:40 -04001346
David Tolnay6a170ce2018-08-26 22:29:24 -07001347 impl Parse for ItemStruct {
1348 fn parse(input: ParseStream) -> Result<Self> {
1349 let attrs = input.call(Attribute::parse_outer)?;
1350 let vis = input.parse::<Visibility>()?;
1351 let struct_token = input.parse::<Token![struct]>()?;
1352 let ident = input.parse::<Ident>()?;
1353 let generics = input.parse::<Generics>()?;
1354 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1355 Ok(ItemStruct {
1356 attrs: attrs,
1357 vis: vis,
1358 struct_token: struct_token,
1359 ident: ident,
1360 generics: Generics {
1361 where_clause: where_clause,
1362 ..generics
1363 },
1364 fields: fields,
1365 semi_token: semi_token,
1366 })
1367 }
1368 }
David Tolnay42602292016-10-01 22:25:45 -07001369
David Tolnay6a170ce2018-08-26 22:29:24 -07001370 impl Parse for ItemEnum {
1371 fn parse(input: ParseStream) -> Result<Self> {
1372 let attrs = input.call(Attribute::parse_outer)?;
1373 let vis = input.parse::<Visibility>()?;
1374 let enum_token = input.parse::<Token![enum]>()?;
1375 let ident = input.parse::<Ident>()?;
1376 let generics = input.parse::<Generics>()?;
1377 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1378 Ok(ItemEnum {
1379 attrs: attrs,
1380 vis: vis,
1381 enum_token: enum_token,
1382 ident: ident,
1383 generics: Generics {
1384 where_clause: where_clause,
1385 ..generics
1386 },
1387 brace_token: brace_token,
1388 variants: variants,
1389 })
1390 }
1391 }
David Tolnay4c614be2017-11-10 00:02:38 -08001392
David Tolnay6a170ce2018-08-26 22:29:24 -07001393 impl Parse for ItemUnion {
1394 fn parse(input: ParseStream) -> Result<Self> {
1395 let attrs = input.call(Attribute::parse_outer)?;
1396 let vis = input.parse::<Visibility>()?;
1397 let union_token = input.parse::<Token![union]>()?;
1398 let ident = input.parse::<Ident>()?;
1399 let generics = input.parse::<Generics>()?;
1400 let (where_clause, fields) = derive::parsing::data_union(input)?;
1401 Ok(ItemUnion {
1402 attrs: attrs,
1403 vis: vis,
1404 union_token: union_token,
1405 ident: ident,
1406 generics: Generics {
1407 where_clause: where_clause,
1408 ..generics
1409 },
1410 fields: fields,
1411 })
1412 }
1413 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001414
David Tolnay6a170ce2018-08-26 22:29:24 -07001415 impl Parse for ItemTrait {
1416 fn parse(input: ParseStream) -> Result<Self> {
1417 let attrs = input.call(Attribute::parse_outer)?;
1418 let vis: Visibility = input.parse()?;
1419 let unsafety: Option<Token![unsafe]> = input.parse()?;
1420 let auto_token: Option<Token![auto]> = input.parse()?;
1421 let trait_token: Token![trait] = input.parse()?;
1422 let ident: Ident = input.parse()?;
1423 let mut generics: Generics = input.parse()?;
1424 let colon_token: Option<Token![:]> = input.parse()?;
1425 let supertraits = if colon_token.is_some() {
1426 input.parse_synom(Punctuated::parse_separated_nonempty)?
1427 } else {
1428 Punctuated::new()
David Tolnay5859df12016-10-29 22:49:54 -07001429 };
David Tolnay6a170ce2018-08-26 22:29:24 -07001430 generics.where_clause = input.parse()?;
1431
1432 let content;
1433 let brace_token = braced!(content in input);
1434 let mut items = Vec::new();
1435 while !content.is_empty() {
1436 items.push(content.parse()?);
1437 }
1438
1439 Ok(ItemTrait {
1440 attrs: attrs,
1441 vis: vis,
1442 unsafety: unsafety,
1443 auto_token: auto_token,
1444 trait_token: trait_token,
1445 ident: ident,
1446 generics: generics,
1447 colon_token: colon_token,
1448 supertraits: supertraits,
1449 brace_token: brace_token,
1450 items: items,
1451 })
1452 }
1453 }
1454
1455 impl Parse for TraitItem {
1456 fn parse(input: ParseStream) -> Result<Self> {
1457 let ahead = input.fork();
1458 ahead.call(Attribute::parse_outer)?;
1459
1460 let lookahead = ahead.lookahead1();
1461 if lookahead.peek(Token![const]) {
1462 ahead.parse::<Token![const]>()?;
1463 let lookahead = ahead.lookahead1();
1464 if lookahead.peek(Ident) {
1465 input.parse().map(TraitItem::Const)
1466 } else if lookahead.peek(Token![unsafe])
1467 || lookahead.peek(Token![extern])
1468 || lookahead.peek(Token![fn])
1469 {
1470 input.parse().map(TraitItem::Method)
1471 } else {
1472 Err(lookahead.error())
1473 }
1474 } else if lookahead.peek(Token![unsafe])
1475 || lookahead.peek(Token![extern])
1476 || lookahead.peek(Token![fn])
1477 {
1478 input.parse().map(TraitItem::Method)
1479 } else if lookahead.peek(Token![type]) {
1480 input.parse().map(TraitItem::Type)
1481 } else if lookahead.peek(Ident)
1482 || lookahead.peek(Token![self])
1483 || lookahead.peek(Token![super])
1484 || lookahead.peek(Token![extern])
1485 || lookahead.peek(Token![crate])
1486 || lookahead.peek(Token![::])
1487 {
1488 input.parse().map(TraitItem::Macro)
1489 } else {
1490 Err(lookahead.error())
1491 }
1492 }
1493 }
1494
1495 impl Parse for TraitItemConst {
1496 fn parse(input: ParseStream) -> Result<Self> {
1497 Ok(TraitItemConst {
1498 attrs: input.call(Attribute::parse_outer)?,
1499 const_token: input.parse()?,
1500 ident: input.parse()?,
1501 colon_token: input.parse()?,
1502 ty: input.parse()?,
1503 default: {
1504 if input.peek(Token![=]) {
1505 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001506 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001507 Some((eq_token, default))
1508 } else {
1509 None
1510 }
1511 },
1512 semi_token: input.parse()?,
1513 })
1514 }
1515 }
1516
1517 impl Parse for TraitItemMethod {
1518 fn parse(input: ParseStream) -> Result<Self> {
1519 let outer_attrs = input.call(Attribute::parse_outer)?;
1520 let constness: Option<Token![const]> = input.parse()?;
1521 let unsafety: Option<Token![unsafe]> = input.parse()?;
1522 let abi: Option<Abi> = input.parse()?;
1523 let fn_token: Token![fn] = input.parse()?;
1524 let ident: Ident = input.parse()?;
1525 let generics: Generics = input.parse()?;
1526
1527 let content;
1528 let paren_token = parenthesized!(content in input);
1529 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1530
1531 let output: ReturnType = input.parse()?;
1532 let where_clause: Option<WhereClause> = input.parse()?;
1533
1534 let lookahead = input.lookahead1();
1535 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1536 let content;
1537 let brace_token = braced!(content in input);
1538 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001539 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001540 (Some(brace_token), inner_attrs, stmts, None)
1541 } else if lookahead.peek(Token![;]) {
1542 let semi_token: Token![;] = input.parse()?;
1543 (None, Vec::new(), Vec::new(), Some(semi_token))
1544 } else {
1545 return Err(lookahead.error());
1546 };
1547
1548 Ok(TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001549 attrs: {
1550 let mut attrs = outer_attrs;
1551 attrs.extend(inner_attrs);
1552 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001553 },
David Tolnayda705bd2017-11-10 21:58:05 -08001554 sig: MethodSig {
1555 constness: constness,
1556 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001557 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001558 abi: abi,
1559 ident: ident,
1560 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001561 fn_token: fn_token,
1562 paren_token: paren_token,
1563 inputs: inputs,
1564 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001565 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001566 generics: Generics {
1567 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001568 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001569 },
1570 },
David Tolnayda705bd2017-11-10 21:58:05 -08001571 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001572 default: brace_token.map(|brace_token| Block {
1573 brace_token: brace_token,
1574 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001575 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001576 semi_token: semi_token,
1577 })
1578 }
1579 }
1580
1581 impl Parse for TraitItemType {
1582 fn parse(input: ParseStream) -> Result<Self> {
1583 let attrs = input.call(Attribute::parse_outer)?;
1584 let type_token: Token![type] = input.parse()?;
1585 let ident: Ident = input.parse()?;
1586 let mut generics: Generics = input.parse()?;
1587 let colon_token: Option<Token![:]> = input.parse()?;
1588 let bounds = if colon_token.is_some() {
1589 input.parse_synom(Punctuated::parse_separated_nonempty)?
1590 } else {
1591 Punctuated::new()
1592 };
1593 generics.where_clause = input.parse()?;
1594 let default = if input.peek(Token![=]) {
1595 let eq_token: Token![=] = input.parse()?;
1596 let default: Type = input.parse()?;
1597 Some((eq_token, default))
1598 } else {
1599 None
1600 };
1601 let semi_token: Token![;] = input.parse()?;
1602
1603 Ok(TraitItemType {
1604 attrs: attrs,
1605 type_token: type_token,
1606 ident: ident,
1607 generics: generics,
1608 colon_token: colon_token,
1609 bounds: bounds,
1610 default: default,
1611 semi_token: semi_token,
1612 })
1613 }
1614 }
1615
1616 impl Parse for TraitItemMacro {
1617 fn parse(input: ParseStream) -> Result<Self> {
1618 let attrs = input.call(Attribute::parse_outer)?;
1619 let mac: Macro = input.parse()?;
1620 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1621 None
1622 } else {
1623 Some(input.parse()?)
1624 };
1625 Ok(TraitItemMacro {
1626 attrs: attrs,
1627 mac: mac,
1628 semi_token: semi_token,
1629 })
1630 }
1631 }
1632
1633 impl Parse for ItemImpl {
1634 fn parse(input: ParseStream) -> Result<Self> {
1635 let outer_attrs = input.call(Attribute::parse_outer)?;
1636 let defaultness: Option<Token![default]> = input.parse()?;
1637 let unsafety: Option<Token![unsafe]> = input.parse()?;
1638 let impl_token: Token![impl ] = input.parse()?;
1639 let generics: Generics = input.parse()?;
1640 let trait_ = {
1641 let ahead = input.fork();
1642 if ahead.parse::<Option<Token![!]>>().is_ok()
1643 && ahead.parse::<Path>().is_ok()
1644 && ahead.parse::<Token![for]>().is_ok()
1645 {
1646 let polarity: Option<Token![!]> = input.parse()?;
1647 let path: Path = input.parse()?;
1648 let for_token: Token![for] = input.parse()?;
1649 Some((polarity, path, for_token))
1650 } else {
1651 None
1652 }
1653 };
1654 let self_ty: Type = input.parse()?;
1655 let where_clause: Option<WhereClause> = input.parse()?;
1656
1657 let content;
1658 let brace_token = braced!(content in input);
1659 let inner_attrs = content.call(Attribute::parse_inner)?;
1660
1661 let mut items = Vec::new();
1662 while !content.is_empty() {
1663 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001664 }
David Tolnay0aecb732016-10-03 23:03:50 -07001665
David Tolnay6a170ce2018-08-26 22:29:24 -07001666 Ok(ItemImpl {
1667 attrs: {
1668 let mut attrs = outer_attrs;
1669 attrs.extend(inner_attrs);
1670 attrs
1671 },
1672 defaultness: defaultness,
1673 unsafety: unsafety,
1674 impl_token: impl_token,
1675 generics: Generics {
1676 where_clause: where_clause,
1677 ..generics
1678 },
1679 trait_: trait_,
1680 self_ty: Box::new(self_ty),
1681 brace_token: brace_token,
1682 items: items,
1683 })
1684 }
1685 }
David Tolnay0aecb732016-10-03 23:03:50 -07001686
David Tolnay6a170ce2018-08-26 22:29:24 -07001687 impl Parse for ImplItem {
1688 fn parse(input: ParseStream) -> Result<Self> {
1689 let ahead = input.fork();
1690 ahead.call(Attribute::parse_outer)?;
1691 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001692
David Tolnay6a170ce2018-08-26 22:29:24 -07001693 let mut lookahead = ahead.lookahead1();
1694 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1695 let defaultness: Token![default] = ahead.parse()?;
1696 lookahead = ahead.lookahead1();
1697 Some(defaultness)
1698 } else {
1699 None
1700 };
David Tolnay4c9be372016-10-06 00:47:37 -07001701
David Tolnay6a170ce2018-08-26 22:29:24 -07001702 if lookahead.peek(Token![const]) {
1703 ahead.parse::<Token![const]>()?;
1704 let lookahead = ahead.lookahead1();
1705 if lookahead.peek(Ident) {
1706 input.parse().map(ImplItem::Const)
1707 } else if lookahead.peek(Token![unsafe])
1708 || lookahead.peek(Token![async])
1709 || lookahead.peek(Token![extern])
1710 || lookahead.peek(Token![fn])
1711 {
1712 input.parse().map(ImplItem::Method)
1713 } else {
1714 Err(lookahead.error())
1715 }
1716 } else if lookahead.peek(Token![unsafe])
1717 || lookahead.peek(Token![async])
1718 || lookahead.peek(Token![extern])
1719 || lookahead.peek(Token![fn])
1720 {
1721 input.parse().map(ImplItem::Method)
1722 } else if lookahead.peek(Token![type]) {
1723 input.parse().map(ImplItem::Type)
1724 } else if vis.is_inherited()
1725 && defaultness.is_none()
1726 && lookahead.peek(Token![existential])
1727 {
1728 input.parse().map(ImplItem::Existential)
1729 } else if vis.is_inherited()
1730 && defaultness.is_none()
1731 && (lookahead.peek(Ident)
1732 || lookahead.peek(Token![self])
1733 || lookahead.peek(Token![super])
1734 || lookahead.peek(Token![extern])
1735 || lookahead.peek(Token![crate])
1736 || lookahead.peek(Token![::]))
1737 {
1738 input.parse().map(ImplItem::Macro)
1739 } else {
1740 Err(lookahead.error())
1741 }
1742 }
1743 }
David Tolnay4c9be372016-10-06 00:47:37 -07001744
David Tolnay3779bb72018-08-26 18:46:07 -07001745 impl Parse for ImplItemConst {
1746 fn parse(input: ParseStream) -> Result<Self> {
1747 Ok(ImplItemConst {
1748 attrs: input.call(Attribute::parse_outer)?,
1749 vis: input.parse()?,
1750 defaultness: input.parse()?,
1751 const_token: input.parse()?,
1752 ident: input.parse()?,
1753 colon_token: input.parse()?,
1754 ty: input.parse()?,
1755 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001756 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001757 semi_token: input.parse()?,
1758 })
1759 }
1760 }
David Tolnay4c9be372016-10-06 00:47:37 -07001761
David Tolnay6a170ce2018-08-26 22:29:24 -07001762 impl Parse for ImplItemMethod {
1763 fn parse(input: ParseStream) -> Result<Self> {
1764 let outer_attrs = input.call(Attribute::parse_outer)?;
1765 let vis: Visibility = input.parse()?;
1766 let defaultness: Option<Token![default]> = input.parse()?;
1767 let constness: Option<Token![const]> = input.parse()?;
1768 let unsafety: Option<Token![unsafe]> = input.parse()?;
1769 let asyncness: Option<Token![async]> = input.parse()?;
1770 let abi: Option<Abi> = input.parse()?;
1771 let fn_token: Token![fn] = input.parse()?;
1772 let ident: Ident = input.parse()?;
1773 let generics: Generics = input.parse()?;
1774
1775 let content;
1776 let paren_token = parenthesized!(content in input);
1777 let inputs = content.parse_terminated(<FnArg as Parse>::parse)?;
1778
1779 let output: ReturnType = input.parse()?;
1780 let where_clause: Option<WhereClause> = input.parse()?;
1781
1782 let content;
1783 let brace_token = braced!(content in input);
1784 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001785 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001786
1787 Ok(ImplItemMethod {
1788 attrs: {
1789 let mut attrs = outer_attrs;
1790 attrs.extend(inner_attrs);
1791 attrs
David Tolnay4c9be372016-10-06 00:47:37 -07001792 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001793 vis: vis,
1794 defaultness: defaultness,
1795 sig: MethodSig {
1796 constness: constness,
1797 unsafety: unsafety,
1798 asyncness: asyncness,
1799 abi: abi,
1800 ident: ident,
1801 decl: FnDecl {
1802 fn_token: fn_token,
1803 paren_token: paren_token,
1804 inputs: inputs,
1805 output: output,
1806 variadic: None,
1807 generics: Generics {
1808 where_clause: where_clause,
1809 ..generics
1810 },
1811 },
1812 },
1813 block: Block {
1814 brace_token: brace_token,
1815 stmts: stmts,
1816 },
1817 })
1818 }
1819 }
David Tolnay4c9be372016-10-06 00:47:37 -07001820
David Tolnay3779bb72018-08-26 18:46:07 -07001821 impl Parse for ImplItemType {
1822 fn parse(input: ParseStream) -> Result<Self> {
1823 Ok(ImplItemType {
1824 attrs: input.call(Attribute::parse_outer)?,
1825 vis: input.parse()?,
1826 defaultness: input.parse()?,
1827 type_token: input.parse()?,
1828 ident: input.parse()?,
1829 generics: {
1830 let mut generics: Generics = input.parse()?;
1831 generics.where_clause = input.parse()?;
1832 generics
1833 },
1834 eq_token: input.parse()?,
1835 ty: input.parse()?,
1836 semi_token: input.parse()?,
1837 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001838 }
David Tolnay3779bb72018-08-26 18:46:07 -07001839 }
David Tolnay758ee132018-08-21 21:29:40 -04001840
David Tolnay3779bb72018-08-26 18:46:07 -07001841 impl Parse for ImplItemExistential {
1842 fn parse(input: ParseStream) -> Result<Self> {
1843 let ety: ItemExistential = input.parse()?;
1844 Ok(ImplItemExistential {
1845 attrs: ety.attrs,
1846 existential_token: ety.existential_token,
1847 type_token: ety.type_token,
1848 ident: ety.ident,
1849 generics: ety.generics,
1850 colon_token: ety.colon_token,
1851 bounds: ety.bounds,
1852 semi_token: ety.semi_token,
1853 })
1854 }
1855 }
1856
1857 impl Parse for ImplItemMacro {
1858 fn parse(input: ParseStream) -> Result<Self> {
1859 let attrs = input.call(Attribute::parse_outer)?;
1860 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001861 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001862 None
1863 } else {
1864 Some(input.parse()?)
1865 };
1866 Ok(ImplItemMacro {
1867 attrs: attrs,
1868 mac: mac,
1869 semi_token: semi_token,
1870 })
1871 }
1872 }
David Tolnay4c9be372016-10-06 00:47:37 -07001873
David Tolnay6a170ce2018-08-26 22:29:24 -07001874 impl Visibility {
1875 fn is_inherited(&self) -> bool {
1876 match *self {
1877 Visibility::Inherited => true,
1878 _ => false,
1879 }
1880 }
1881 }
1882
1883 impl MacroDelimiter {
1884 fn is_brace(&self) -> bool {
1885 match *self {
1886 MacroDelimiter::Brace(_) => true,
1887 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1888 }
David Tolnay57292da2017-12-27 21:03:33 -05001889 }
1890 }
David Tolnayedf2b992016-09-23 20:43:45 -07001891}
David Tolnay4a51dc72016-10-01 00:40:31 -07001892
1893#[cfg(feature = "printing")]
1894mod printing {
1895 use super::*;
1896 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001897 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001898 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001899
David Tolnay1bfa7332017-11-11 12:41:20 -08001900 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001901 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001902 tokens.append_all(self.attrs.outer());
1903 self.vis.to_tokens(tokens);
1904 self.extern_token.to_tokens(tokens);
1905 self.crate_token.to_tokens(tokens);
1906 self.ident.to_tokens(tokens);
1907 if let Some((ref as_token, ref rename)) = self.rename {
1908 as_token.to_tokens(tokens);
1909 rename.to_tokens(tokens);
1910 }
1911 self.semi_token.to_tokens(tokens);
1912 }
1913 }
1914
1915 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001916 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001917 tokens.append_all(self.attrs.outer());
1918 self.vis.to_tokens(tokens);
1919 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001920 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001921 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001922 self.semi_token.to_tokens(tokens);
1923 }
1924 }
1925
1926 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001927 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001928 tokens.append_all(self.attrs.outer());
1929 self.vis.to_tokens(tokens);
1930 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001931 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001932 self.ident.to_tokens(tokens);
1933 self.colon_token.to_tokens(tokens);
1934 self.ty.to_tokens(tokens);
1935 self.eq_token.to_tokens(tokens);
1936 self.expr.to_tokens(tokens);
1937 self.semi_token.to_tokens(tokens);
1938 }
1939 }
1940
1941 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001942 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001943 tokens.append_all(self.attrs.outer());
1944 self.vis.to_tokens(tokens);
1945 self.const_token.to_tokens(tokens);
1946 self.ident.to_tokens(tokens);
1947 self.colon_token.to_tokens(tokens);
1948 self.ty.to_tokens(tokens);
1949 self.eq_token.to_tokens(tokens);
1950 self.expr.to_tokens(tokens);
1951 self.semi_token.to_tokens(tokens);
1952 }
1953 }
1954
1955 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001956 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001957 tokens.append_all(self.attrs.outer());
1958 self.vis.to_tokens(tokens);
1959 self.constness.to_tokens(tokens);
1960 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001961 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001962 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001963 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001964 self.block.brace_token.surround(tokens, |tokens| {
1965 tokens.append_all(self.attrs.inner());
1966 tokens.append_all(&self.block.stmts);
1967 });
1968 }
1969 }
1970
1971 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001972 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001973 tokens.append_all(self.attrs.outer());
1974 self.vis.to_tokens(tokens);
1975 self.mod_token.to_tokens(tokens);
1976 self.ident.to_tokens(tokens);
1977 if let Some((ref brace, ref items)) = self.content {
1978 brace.surround(tokens, |tokens| {
1979 tokens.append_all(self.attrs.inner());
1980 tokens.append_all(items);
1981 });
1982 } else {
1983 TokensOrDefault(&self.semi).to_tokens(tokens);
1984 }
1985 }
1986 }
1987
1988 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001989 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001990 tokens.append_all(self.attrs.outer());
1991 self.abi.to_tokens(tokens);
1992 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001993 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001994 tokens.append_all(&self.items);
1995 });
1996 }
1997 }
1998
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001999 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002000 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002001 tokens.append_all(self.attrs.outer());
2002 self.vis.to_tokens(tokens);
2003 self.type_token.to_tokens(tokens);
2004 self.ident.to_tokens(tokens);
2005 self.generics.to_tokens(tokens);
2006 self.generics.where_clause.to_tokens(tokens);
2007 self.eq_token.to_tokens(tokens);
2008 self.ty.to_tokens(tokens);
2009 self.semi_token.to_tokens(tokens);
2010 }
2011 }
2012
David Tolnaybb82ef02018-08-24 20:15:45 -04002013 impl ToTokens for ItemExistential {
2014 fn to_tokens(&self, tokens: &mut TokenStream) {
2015 tokens.append_all(self.attrs.outer());
2016 self.vis.to_tokens(tokens);
2017 self.existential_token.to_tokens(tokens);
2018 self.type_token.to_tokens(tokens);
2019 self.ident.to_tokens(tokens);
2020 self.generics.to_tokens(tokens);
2021 self.generics.where_clause.to_tokens(tokens);
2022 if !self.bounds.is_empty() {
2023 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2024 self.bounds.to_tokens(tokens);
2025 }
2026 self.semi_token.to_tokens(tokens);
2027 }
2028 }
2029
David Tolnay1bfa7332017-11-11 12:41:20 -08002030 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002031 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002032 tokens.append_all(self.attrs.outer());
2033 self.vis.to_tokens(tokens);
2034 self.enum_token.to_tokens(tokens);
2035 self.ident.to_tokens(tokens);
2036 self.generics.to_tokens(tokens);
2037 self.generics.where_clause.to_tokens(tokens);
2038 self.brace_token.surround(tokens, |tokens| {
2039 self.variants.to_tokens(tokens);
2040 });
2041 }
2042 }
2043
2044 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002045 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002046 tokens.append_all(self.attrs.outer());
2047 self.vis.to_tokens(tokens);
2048 self.struct_token.to_tokens(tokens);
2049 self.ident.to_tokens(tokens);
2050 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002051 match self.fields {
2052 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002053 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002054 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002055 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002056 Fields::Unnamed(ref fields) => {
2057 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002058 self.generics.where_clause.to_tokens(tokens);
2059 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002060 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002061 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002062 self.generics.where_clause.to_tokens(tokens);
2063 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002064 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002065 }
2066 }
2067 }
2068
2069 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002070 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002071 tokens.append_all(self.attrs.outer());
2072 self.vis.to_tokens(tokens);
2073 self.union_token.to_tokens(tokens);
2074 self.ident.to_tokens(tokens);
2075 self.generics.to_tokens(tokens);
2076 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002077 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002078 }
2079 }
2080
2081 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002082 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002083 tokens.append_all(self.attrs.outer());
2084 self.vis.to_tokens(tokens);
2085 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002086 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002087 self.trait_token.to_tokens(tokens);
2088 self.ident.to_tokens(tokens);
2089 self.generics.to_tokens(tokens);
2090 if !self.supertraits.is_empty() {
2091 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2092 self.supertraits.to_tokens(tokens);
2093 }
2094 self.generics.where_clause.to_tokens(tokens);
2095 self.brace_token.surround(tokens, |tokens| {
2096 tokens.append_all(&self.items);
2097 });
2098 }
2099 }
2100
David Tolnay1bfa7332017-11-11 12:41:20 -08002101 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002102 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002103 tokens.append_all(self.attrs.outer());
2104 self.defaultness.to_tokens(tokens);
2105 self.unsafety.to_tokens(tokens);
2106 self.impl_token.to_tokens(tokens);
2107 self.generics.to_tokens(tokens);
2108 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2109 polarity.to_tokens(tokens);
2110 path.to_tokens(tokens);
2111 for_token.to_tokens(tokens);
2112 }
2113 self.self_ty.to_tokens(tokens);
2114 self.generics.where_clause.to_tokens(tokens);
2115 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002116 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002117 tokens.append_all(&self.items);
2118 });
2119 }
2120 }
2121
2122 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002123 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002124 tokens.append_all(self.attrs.outer());
2125 self.mac.path.to_tokens(tokens);
2126 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002127 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002128 match self.mac.delimiter {
2129 MacroDelimiter::Paren(ref paren) => {
2130 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2131 }
2132 MacroDelimiter::Brace(ref brace) => {
2133 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2134 }
2135 MacroDelimiter::Bracket(ref bracket) => {
2136 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2137 }
2138 }
David Tolnay57292da2017-12-27 21:03:33 -05002139 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002140 }
2141 }
David Tolnay42602292016-10-01 22:25:45 -07002142
David Tolnay500d8322017-12-18 00:32:51 -08002143 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002144 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002145 tokens.append_all(self.attrs.outer());
2146 self.vis.to_tokens(tokens);
2147 self.macro_token.to_tokens(tokens);
2148 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002149 self.paren_token.surround(tokens, |tokens| {
2150 self.args.to_tokens(tokens);
2151 });
2152 self.brace_token.surround(tokens, |tokens| {
2153 self.body.to_tokens(tokens);
2154 });
David Tolnay500d8322017-12-18 00:32:51 -08002155 }
2156 }
2157
David Tolnay2ae520a2017-12-29 11:19:50 -05002158 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002159 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002160 self.tts.to_tokens(tokens);
2161 }
2162 }
2163
David Tolnay5f332a92017-12-26 00:42:45 -05002164 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002165 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002166 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002167 self.colon2_token.to_tokens(tokens);
2168 self.tree.to_tokens(tokens);
2169 }
2170 }
2171
2172 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002173 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002174 self.ident.to_tokens(tokens);
2175 }
2176 }
2177
2178 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002179 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002180 self.ident.to_tokens(tokens);
2181 self.as_token.to_tokens(tokens);
2182 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002183 }
2184 }
2185
David Tolnay5f332a92017-12-26 00:42:45 -05002186 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002187 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002188 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002189 }
2190 }
2191
David Tolnayd97a7d22018-03-31 19:17:01 +02002192 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002193 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002194 self.brace_token.surround(tokens, |tokens| {
2195 self.items.to_tokens(tokens);
2196 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002197 }
2198 }
2199
David Tolnay1bfa7332017-11-11 12:41:20 -08002200 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002201 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002202 tokens.append_all(self.attrs.outer());
2203 self.const_token.to_tokens(tokens);
2204 self.ident.to_tokens(tokens);
2205 self.colon_token.to_tokens(tokens);
2206 self.ty.to_tokens(tokens);
2207 if let Some((ref eq_token, ref default)) = self.default {
2208 eq_token.to_tokens(tokens);
2209 default.to_tokens(tokens);
2210 }
2211 self.semi_token.to_tokens(tokens);
2212 }
2213 }
2214
2215 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002216 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002217 tokens.append_all(self.attrs.outer());
2218 self.sig.to_tokens(tokens);
2219 match self.default {
2220 Some(ref block) => {
2221 block.brace_token.surround(tokens, |tokens| {
2222 tokens.append_all(self.attrs.inner());
2223 tokens.append_all(&block.stmts);
2224 });
David Tolnayca085422016-10-04 00:12:38 -07002225 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002226 None => {
2227 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002228 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002229 }
2230 }
2231 }
2232
2233 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002234 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002235 tokens.append_all(self.attrs.outer());
2236 self.type_token.to_tokens(tokens);
2237 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002238 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002239 if !self.bounds.is_empty() {
2240 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2241 self.bounds.to_tokens(tokens);
2242 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002243 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002244 if let Some((ref eq_token, ref default)) = self.default {
2245 eq_token.to_tokens(tokens);
2246 default.to_tokens(tokens);
2247 }
2248 self.semi_token.to_tokens(tokens);
2249 }
2250 }
2251
2252 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002253 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002254 tokens.append_all(self.attrs.outer());
2255 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002256 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002257 }
2258 }
2259
David Tolnay2ae520a2017-12-29 11:19:50 -05002260 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002261 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002262 self.tts.to_tokens(tokens);
2263 }
2264 }
2265
David Tolnay857628c2017-11-11 12:25:31 -08002266 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002267 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002268 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002269 self.vis.to_tokens(tokens);
2270 self.defaultness.to_tokens(tokens);
2271 self.const_token.to_tokens(tokens);
2272 self.ident.to_tokens(tokens);
2273 self.colon_token.to_tokens(tokens);
2274 self.ty.to_tokens(tokens);
2275 self.eq_token.to_tokens(tokens);
2276 self.expr.to_tokens(tokens);
2277 self.semi_token.to_tokens(tokens);
2278 }
2279 }
2280
2281 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002282 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002283 tokens.append_all(self.attrs.outer());
2284 self.vis.to_tokens(tokens);
2285 self.defaultness.to_tokens(tokens);
2286 self.sig.to_tokens(tokens);
2287 self.block.brace_token.surround(tokens, |tokens| {
2288 tokens.append_all(self.attrs.inner());
2289 tokens.append_all(&self.block.stmts);
2290 });
2291 }
2292 }
2293
2294 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002295 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002296 tokens.append_all(self.attrs.outer());
2297 self.vis.to_tokens(tokens);
2298 self.defaultness.to_tokens(tokens);
2299 self.type_token.to_tokens(tokens);
2300 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002301 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002302 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002303 self.eq_token.to_tokens(tokens);
2304 self.ty.to_tokens(tokens);
2305 self.semi_token.to_tokens(tokens);
2306 }
2307 }
2308
David Tolnaybb82ef02018-08-24 20:15:45 -04002309 impl ToTokens for ImplItemExistential {
2310 fn to_tokens(&self, tokens: &mut TokenStream) {
2311 tokens.append_all(self.attrs.outer());
2312 self.existential_token.to_tokens(tokens);
2313 self.type_token.to_tokens(tokens);
2314 self.ident.to_tokens(tokens);
2315 self.generics.to_tokens(tokens);
2316 self.generics.where_clause.to_tokens(tokens);
2317 if !self.bounds.is_empty() {
2318 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2319 self.bounds.to_tokens(tokens);
2320 }
2321 self.semi_token.to_tokens(tokens);
2322 }
2323 }
2324
David Tolnay857628c2017-11-11 12:25:31 -08002325 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002326 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002327 tokens.append_all(self.attrs.outer());
2328 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002329 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002330 }
2331 }
2332
David Tolnay2ae520a2017-12-29 11:19:50 -05002333 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002334 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002335 self.tts.to_tokens(tokens);
2336 }
2337 }
2338
David Tolnay8894f602017-11-11 12:11:04 -08002339 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002340 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002341 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002342 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002343 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002344 self.semi_token.to_tokens(tokens);
2345 }
2346 }
2347
2348 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002349 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002350 tokens.append_all(self.attrs.outer());
2351 self.vis.to_tokens(tokens);
2352 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002353 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002354 self.ident.to_tokens(tokens);
2355 self.colon_token.to_tokens(tokens);
2356 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002357 self.semi_token.to_tokens(tokens);
2358 }
2359 }
2360
David Tolnay199bcbb2017-11-12 10:33:52 -08002361 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002362 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002363 tokens.append_all(self.attrs.outer());
2364 self.vis.to_tokens(tokens);
2365 self.type_token.to_tokens(tokens);
2366 self.ident.to_tokens(tokens);
2367 self.semi_token.to_tokens(tokens);
2368 }
2369 }
2370
David Tolnay435c1782018-08-24 16:15:44 -04002371 impl ToTokens for ForeignItemMacro {
2372 fn to_tokens(&self, tokens: &mut TokenStream) {
2373 tokens.append_all(self.attrs.outer());
2374 self.mac.to_tokens(tokens);
2375 self.semi_token.to_tokens(tokens);
2376 }
2377 }
2378
David Tolnay2ae520a2017-12-29 11:19:50 -05002379 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002380 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002381 self.tts.to_tokens(tokens);
2382 }
2383 }
2384
David Tolnay570695e2017-06-03 16:15:13 -07002385 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002386 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002387 self.constness.to_tokens(tokens);
2388 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002389 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002390 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002391 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002392 }
2393 }
2394
Alex Crichtona74a1c82018-05-16 10:20:44 -07002395 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002396
2397 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002398 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002399 self.0.fn_token.to_tokens(tokens);
2400 self.1.to_tokens(tokens);
2401 self.0.generics.to_tokens(tokens);
2402 self.0.paren_token.surround(tokens, |tokens| {
2403 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002404 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2405 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002406 }
David Tolnayd2836e22017-12-27 23:13:00 -05002407 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002408 });
2409 self.0.output.to_tokens(tokens);
2410 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002411 }
2412 }
2413
Alex Crichton62a0a592017-05-22 13:58:53 -07002414 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002415 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002416 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002417 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002418 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002419 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002420 }
2421 }
2422
2423 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002424 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002425 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002426 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002427 }
2428 }
2429
2430 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002431 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002432 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002433 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002434 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002435 }
2436 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002437}