blob: c0b9325fe4f0b54aacf0ba476b0b5a6dfd57f8ee [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
David Tolnaybb82ef02018-08-24 20:15:45 -0400139 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
142 pub Existential(ItemExistential {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub existential_token: Token![existential],
146 pub type_token: Token![type],
147 pub ident: Ident,
148 pub generics: Generics,
149 pub colon_token: Option<Token![:]>,
150 pub bounds: Punctuated<TypeParamBound, Token![+]>,
151 pub semi_token: Token![;],
152 }),
153
David Tolnay2b214082018-01-07 01:30:18 -0800154 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800155 ///
156 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 pub Struct(ItemStruct {
158 pub attrs: Vec<Attribute>,
159 pub vis: Visibility,
160 pub struct_token: Token![struct],
161 pub ident: Ident,
162 pub generics: Generics,
163 pub fields: Fields,
164 pub semi_token: Option<Token![;]>,
165 }),
David Tolnay2b214082018-01-07 01:30:18 -0800166
167 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800168 ///
169 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700174 pub ident: Ident,
175 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
David Tolnay2b214082018-01-07 01:30:18 -0800179
180 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800181 ///
182 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700187 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500189 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 }),
David Tolnay2b214082018-01-07 01:30:18 -0800191
192 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800193 ///
194 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700197 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500198 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500199 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800200 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700201 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500205 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub items: Vec<TraitItem>,
207 }),
David Tolnay2b214082018-01-07 01:30:18 -0800208
209 /// An impl block providing trait or associated items: `impl<A> Trait
210 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800211 ///
212 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800214 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500215 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500216 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700219 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500220 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700221 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub items: Vec<ImplItem>,
225 }),
David Tolnay2b214082018-01-07 01:30:18 -0800226
227 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800228 ///
229 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800230 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800231 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800232 /// The `example` in `macro_rules! example { ... }`.
233 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800234 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500235 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800236 }),
David Tolnay2b214082018-01-07 01:30:18 -0800237
238 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500241 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800242 pub attrs: Vec<Attribute>,
243 pub vis: Visibility,
244 pub macro_token: Token![macro],
245 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500246 pub paren_token: Paren,
247 pub args: TokenStream,
248 pub brace_token: Brace,
249 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500255 pub Verbatim(ItemVerbatim #manual_extra_traits {
256 pub tts: TokenStream,
257 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
David Tolnayb79ee962016-09-04 09:39:20 -0700259}
260
David Tolnay9c76bcb2017-12-26 23:14:59 -0500261#[cfg(feature = "extra-traits")]
262impl Eq for ItemMacro2 {}
263
264#[cfg(feature = "extra-traits")]
265impl PartialEq for ItemMacro2 {
266 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700267 self.attrs == other.attrs
268 && self.vis == other.vis
269 && self.macro_token == other.macro_token
270 && self.ident == other.ident
271 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500272 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
273 && self.brace_token == other.brace_token
274 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275 }
276}
277
278#[cfg(feature = "extra-traits")]
279impl Hash for ItemMacro2 {
280 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500281 where
282 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500283 {
284 self.attrs.hash(state);
285 self.vis.hash(state);
286 self.macro_token.hash(state);
287 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500288 self.paren_token.hash(state);
289 TokenStreamHelper(&self.args).hash(state);
290 self.brace_token.hash(state);
291 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500292 }
293}
294
David Tolnay2ae520a2017-12-29 11:19:50 -0500295#[cfg(feature = "extra-traits")]
296impl Eq for ItemVerbatim {}
297
298#[cfg(feature = "extra-traits")]
299impl PartialEq for ItemVerbatim {
300 fn eq(&self, other: &Self) -> bool {
301 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
302 }
303}
304
305#[cfg(feature = "extra-traits")]
306impl Hash for ItemVerbatim {
307 fn hash<H>(&self, state: &mut H)
308 where
309 H: Hasher,
310 {
311 TokenStreamHelper(&self.tts).hash(state);
312 }
313}
314
David Tolnay0e837402016-12-22 17:25:55 -0500315impl From<DeriveInput> for Item {
316 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500317 match input.data {
318 Data::Struct(data) => Item::Struct(ItemStruct {
319 attrs: input.attrs,
320 vis: input.vis,
321 struct_token: data.struct_token,
322 ident: input.ident,
323 generics: input.generics,
324 fields: data.fields,
325 semi_token: data.semi_token,
326 }),
327 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500328 attrs: input.attrs,
329 vis: input.vis,
330 enum_token: data.enum_token,
331 ident: input.ident,
332 generics: input.generics,
333 brace_token: data.brace_token,
334 variants: data.variants,
335 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500337 attrs: input.attrs,
338 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500339 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500340 ident: input.ident,
341 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500343 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700344 }
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800350 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800351 /// *This type is available if Syn is built with the `"full"` feature.*
352 ///
David Tolnay614a0142018-01-07 10:25:43 -0800353 /// # Syntax tree enum
354 ///
355 /// This type is a [syntax tree enum].
356 ///
357 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500358 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200359 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500362 pub Path(UsePath {
363 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200364 pub colon2_token: Token![::],
365 pub tree: Box<UseTree>,
366 }),
367
368 /// An identifier imported by a `use` item: `HashMap`.
369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
371 pub Name(UseName {
372 pub ident: Ident,
373 }),
374
375 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
378 pub Rename(UseRename {
379 pub ident: Ident,
380 pub as_token: Token![as],
381 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800383
David Tolnay05658502018-01-07 09:56:37 -0800384 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500387 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800390
David Tolnayd97a7d22018-03-31 19:17:01 +0200391 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800392 ///
393 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200394 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500396 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 }
399}
400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800402 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800403 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800404 /// *This type is available if Syn is built with the `"full"` feature.*
405 ///
David Tolnay614a0142018-01-07 10:25:43 -0800406 /// # Syntax tree enum
407 ///
408 /// This type is a [syntax tree enum].
409 ///
410 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800412 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800413 ///
414 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub attrs: Vec<Attribute>,
417 pub vis: Visibility,
418 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }),
David Tolnayebb72722018-01-07 01:14:13 -0800422
423 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800427 pub attrs: Vec<Attribute>,
428 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500430 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800431 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
David Tolnayebb72722018-01-07 01:14:13 -0800436
437 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800440 pub Type(ForeignItemType {
441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
443 pub type_token: Token![type],
444 pub ident: Ident,
445 pub semi_token: Token![;],
446 }),
David Tolnayebb72722018-01-07 01:14:13 -0800447
David Tolnay435c1782018-08-24 16:15:44 -0400448 /// A macro invocation within an extern block.
449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
451 pub Macro(ForeignItemMacro {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 pub semi_token: Option<Token![;]>,
455 }),
456
David Tolnayebb72722018-01-07 01:14:13 -0800457 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500460 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
461 pub tts: TokenStream,
462 }),
463 }
464}
465
466#[cfg(feature = "extra-traits")]
467impl Eq for ForeignItemVerbatim {}
468
469#[cfg(feature = "extra-traits")]
470impl PartialEq for ForeignItemVerbatim {
471 fn eq(&self, other: &Self) -> bool {
472 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
473 }
474}
475
476#[cfg(feature = "extra-traits")]
477impl Hash for ForeignItemVerbatim {
478 fn hash<H>(&self, state: &mut H)
479 where
480 H: Hasher,
481 {
482 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700484}
485
David Tolnayda705bd2017-11-10 21:58:05 -0800486ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800487 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800488 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800489 /// *This type is available if Syn is built with the `"full"` feature.*
490 ///
David Tolnay614a0142018-01-07 10:25:43 -0800491 /// # Syntax tree enum
492 ///
493 /// This type is a [syntax tree enum].
494 ///
495 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800496 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800497 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800498 ///
499 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800501 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700503 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub default: Option<(Token![=], Expr)>,
507 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
David Tolnayebb72722018-01-07 01:14:13 -0800509
510 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800514 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 pub sig: MethodSig,
516 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
David Tolnayebb72722018-01-07 01:14:13 -0800519
520 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700526 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500527 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800535 ///
536 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800537 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800539 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500540 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800541 }),
David Tolnayebb72722018-01-07 01:14:13 -0800542
543 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800544 ///
545 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500546 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
547 pub tts: TokenStream,
548 }),
549 }
550}
551
552#[cfg(feature = "extra-traits")]
553impl Eq for TraitItemVerbatim {}
554
555#[cfg(feature = "extra-traits")]
556impl PartialEq for TraitItemVerbatim {
557 fn eq(&self, other: &Self) -> bool {
558 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
559 }
560}
561
562#[cfg(feature = "extra-traits")]
563impl Hash for TraitItemVerbatim {
564 fn hash<H>(&self, state: &mut H)
565 where
566 H: Hasher,
567 {
568 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800573 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800574 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800575 /// *This type is available if Syn is built with the `"full"` feature.*
576 ///
David Tolnay614a0142018-01-07 10:25:43 -0800577 /// # Syntax tree enum
578 ///
579 /// This type is a [syntax tree enum].
580 ///
581 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800582 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800583 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700591 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }),
David Tolnayebb72722018-01-07 01:14:13 -0800598
599 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800600 ///
601 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700602 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700604 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500605 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 pub sig: MethodSig,
607 pub block: Block,
608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800614 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700615 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500616 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500619 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 }),
David Tolnayebb72722018-01-07 01:14:13 -0800624
David Tolnaybb82ef02018-08-24 20:15:45 -0400625 /// An existential type within an impl block.
626 ///
627 /// *This type is available if Syn is built with the `"full"` feature.*
628 pub Existential(ImplItemExistential {
629 pub attrs: Vec<Attribute>,
630 pub existential_token: Token![existential],
631 pub type_token: Token![type],
632 pub ident: Ident,
633 pub generics: Generics,
634 pub colon_token: Option<Token![:]>,
635 pub bounds: Punctuated<TypeParamBound, Token![+]>,
636 pub semi_token: Token![;],
637 }),
638
David Tolnayebb72722018-01-07 01:14:13 -0800639 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800642 pub Macro(ImplItemMacro {
643 pub attrs: Vec<Attribute>,
644 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500645 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800646 }),
David Tolnayebb72722018-01-07 01:14:13 -0800647
648 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800649 ///
650 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500651 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
652 pub tts: TokenStream,
653 }),
654 }
655}
656
657#[cfg(feature = "extra-traits")]
658impl Eq for ImplItemVerbatim {}
659
660#[cfg(feature = "extra-traits")]
661impl PartialEq for ImplItemVerbatim {
662 fn eq(&self, other: &Self) -> bool {
663 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
664 }
665}
666
667#[cfg(feature = "extra-traits")]
668impl Hash for ImplItemVerbatim {
669 fn hash<H>(&self, state: &mut H)
670 where
671 H: Hasher,
672 {
673 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700675}
676
677ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800678 /// A method's signature in a trait or implementation: `unsafe fn
679 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500683 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500684 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900685 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700687 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }
690}
691
692ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800693 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500698 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500699 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500700 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500701 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500702 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700704}
705
Alex Crichton62a0a592017-05-22 13:58:53 -0700706ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800707 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800708 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800709 /// *This type is available if Syn is built with the `"full"` feature.*
710 ///
David Tolnay614a0142018-01-07 10:25:43 -0800711 /// # Syntax tree enum
712 ///
713 /// This type is a [syntax tree enum].
714 ///
715 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// Self captured by reference in a function signature: `&self` or `&mut
718 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500725 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800727
David Tolnay3f559052018-01-06 23:59:48 -0800728 /// Self captured by value in a function signature: `self` or `mut
729 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800736
David Tolnay3f559052018-01-06 23:59:48 -0800737 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800738 ///
739 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 pub Captured(ArgCaptured {
741 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800743 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800745
David Tolnay3f559052018-01-06 23:59:48 -0800746 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500747 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800748 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 }
David Tolnay62f374c2016-10-02 13:37:00 -0700751}
752
David Tolnayedf2b992016-09-23 20:43:45 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700756
David Tolnay2ad62c12018-08-26 19:00:35 -0400757 use parse::{Parse, ParseStream, Result};
David Tolnay3779bb72018-08-26 18:46:07 -0700758 use synom::ext::IdentExt;
David Tolnay84aa0752016-10-02 23:01:13 -0700759
David Tolnay6a170ce2018-08-26 22:29:24 -0700760 impl Parse for Item {
761 fn parse(input: ParseStream) -> Result<Self> {
762 let ahead = input.fork();
763 ahead.call(Attribute::parse_outer)?;
764 let vis: Visibility = ahead.parse()?;
765
766 let lookahead = ahead.lookahead1();
767 if lookahead.peek(Token![extern]) {
768 ahead.parse::<Token![extern]>()?;
769 let lookahead = ahead.lookahead1();
770 if lookahead.peek(Token![crate]) {
771 input.parse().map(Item::ExternCrate)
772 } else if lookahead.peek(Token![fn]) {
773 input.parse().map(Item::Fn)
774 } else if lookahead.peek(token::Brace) {
775 input.parse().map(Item::ForeignMod)
776 } else if lookahead.peek(LitStr) {
777 ahead.parse::<LitStr>()?;
778 let lookahead = ahead.lookahead1();
779 if lookahead.peek(token::Brace) {
780 input.parse().map(Item::ForeignMod)
781 } else if lookahead.peek(Token![fn]) {
782 input.parse().map(Item::Fn)
783 } else {
784 Err(lookahead.error())
785 }
786 } else {
787 Err(lookahead.error())
788 }
789 } else if lookahead.peek(Token![use]) {
790 input.parse().map(Item::Use)
791 } else if lookahead.peek(Token![static]) {
792 input.parse().map(Item::Static)
793 } else if lookahead.peek(Token![const]) {
794 ahead.parse::<Token![const]>()?;
795 let lookahead = ahead.lookahead1();
796 if lookahead.peek(Ident) {
797 input.parse().map(Item::Const)
798 } else if lookahead.peek(Token![unsafe])
799 || lookahead.peek(Token![async])
800 || lookahead.peek(Token![extern])
801 || lookahead.peek(Token![fn])
802 {
803 input.parse().map(Item::Fn)
804 } else {
805 Err(lookahead.error())
806 }
807 } else if lookahead.peek(Token![unsafe]) {
808 ahead.parse::<Token![unsafe]>()?;
809 let lookahead = ahead.lookahead1();
810 if lookahead.peek(Token![trait])
811 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
812 {
813 input.parse().map(Item::Trait)
814 } else if lookahead.peek(Token![impl ]) {
815 input.parse().map(Item::Impl)
816 } else if lookahead.peek(Token![async])
817 || lookahead.peek(Token![extern])
818 || lookahead.peek(Token![fn])
819 {
820 input.parse().map(Item::Fn)
821 } else {
822 Err(lookahead.error())
823 }
824 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
825 input.parse().map(Item::Fn)
826 } else if lookahead.peek(Token![mod]) {
827 input.parse().map(Item::Mod)
828 } else if lookahead.peek(Token![type]) {
829 input.parse().map(Item::Type)
830 } else if lookahead.peek(Token![existential]) {
831 input.parse().map(Item::Existential)
832 } else if lookahead.peek(Token![struct]) {
833 input.parse().map(Item::Struct)
834 } else if lookahead.peek(Token![enum]) {
835 input.parse().map(Item::Enum)
836 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
837 input.parse().map(Item::Union)
838 } else if lookahead.peek(Token![trait])
839 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
840 {
841 input.parse().map(Item::Trait)
842 } else if lookahead.peek(Token![impl ])
843 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
844 {
845 input.parse().map(Item::Impl)
846 } else if lookahead.peek(Token![macro]) {
847 input.parse().map(Item::Macro2)
848 } else if vis.is_inherited()
849 && (lookahead.peek(Ident)
850 || lookahead.peek(Token![self])
851 || lookahead.peek(Token![super])
852 || lookahead.peek(Token![extern])
853 || lookahead.peek(Token![crate])
854 || lookahead.peek(Token![::]))
855 {
856 input.parse().map(Item::Macro)
857 } else {
858 Err(lookahead.error())
859 }
860 }
861 }
Alex Crichton954046c2017-05-30 21:49:42 -0700862
David Tolnay3779bb72018-08-26 18:46:07 -0700863 impl Parse for ItemMacro {
864 fn parse(input: ParseStream) -> Result<Self> {
865 let attrs = input.call(Attribute::parse_outer)?;
866 let path = input.call(Path::parse_mod_style)?;
867 let bang_token: Token![!] = input.parse()?;
868 let ident: Option<Ident> = input.parse()?;
869 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700870 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700871 Some(input.parse()?)
872 } else {
873 None
874 };
875 Ok(ItemMacro {
876 attrs: attrs,
877 ident: ident,
878 mac: Macro {
879 path: path,
880 bang_token: bang_token,
881 delimiter: delimiter,
882 tts: tts,
883 },
884 semi_token: semi_token,
885 })
886 }
887 }
David Tolnayedf2b992016-09-23 20:43:45 -0700888
David Tolnay500d8322017-12-18 00:32:51 -0800889 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700890 impl Parse for ItemMacro2 {
891 fn parse(input: ParseStream) -> Result<Self> {
892 let args;
893 let body;
894 Ok(ItemMacro2 {
895 attrs: input.call(Attribute::parse_outer)?,
896 vis: input.parse()?,
897 macro_token: input.parse()?,
898 ident: input.parse()?,
899 paren_token: parenthesized!(args in input),
900 args: args.parse()?,
901 brace_token: braced!(body in input),
902 body: body.parse()?,
903 })
904 }
905 }
David Tolnay500d8322017-12-18 00:32:51 -0800906
David Tolnay3779bb72018-08-26 18:46:07 -0700907 impl Parse for ItemExternCrate {
908 fn parse(input: ParseStream) -> Result<Self> {
909 Ok(ItemExternCrate {
910 attrs: input.call(Attribute::parse_outer)?,
911 vis: input.parse()?,
912 extern_token: input.parse()?,
913 crate_token: input.parse()?,
914 ident: input.parse()?,
915 rename: {
916 if input.peek(Token![as]) {
917 let as_token: Token![as] = input.parse()?;
918 let rename: Ident = input.parse()?;
919 Some((as_token, rename))
920 } else {
921 None
922 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800923 },
David Tolnay3779bb72018-08-26 18:46:07 -0700924 semi_token: input.parse()?,
925 })
926 }
927 }
928
929 impl Parse for ItemUse {
930 fn parse(input: ParseStream) -> Result<Self> {
931 Ok(ItemUse {
932 attrs: input.call(Attribute::parse_outer)?,
933 vis: input.parse()?,
934 use_token: input.parse()?,
935 leading_colon: input.parse()?,
936 tree: input.call(use_tree)?,
937 semi_token: input.parse()?,
938 })
939 }
940 }
941
942 fn use_tree(input: ParseStream) -> Result<UseTree> {
943 let lookahead = input.lookahead1();
944 if lookahead.peek(Ident)
945 || lookahead.peek(Token![self])
946 || lookahead.peek(Token![super])
947 || lookahead.peek(Token![crate])
948 || lookahead.peek(Token![extern])
949 {
David Tolnay0dea1b92018-08-30 17:47:29 -0700950 let ident = input.call(Ident::parse_any)?;
David Tolnay3779bb72018-08-26 18:46:07 -0700951 if input.peek(Token![::]) {
952 Ok(UseTree::Path(UsePath {
953 ident: ident,
954 colon2_token: input.parse()?,
955 tree: Box::new(input.call(use_tree)?),
956 }))
957 } else if input.peek(Token![as]) {
958 Ok(UseTree::Rename(UseRename {
959 ident: ident,
960 as_token: input.parse()?,
961 rename: input.parse()?,
962 }))
963 } else {
964 Ok(UseTree::Name(UseName { ident: ident }))
965 }
966 } else if lookahead.peek(Token![*]) {
967 Ok(UseTree::Glob(UseGlob {
968 star_token: input.parse()?,
969 }))
970 } else if lookahead.peek(token::Brace) {
971 let content;
972 Ok(UseTree::Group(UseGroup {
973 brace_token: braced!(content in input),
974 items: content.parse_terminated(use_tree)?,
975 }))
976 } else {
977 Err(lookahead.error())
978 }
979 }
980
981 impl Parse for ItemStatic {
982 fn parse(input: ParseStream) -> Result<Self> {
983 Ok(ItemStatic {
984 attrs: input.call(Attribute::parse_outer)?,
985 vis: input.parse()?,
986 static_token: input.parse()?,
987 mutability: input.parse()?,
988 ident: input.parse()?,
989 colon_token: input.parse()?,
990 ty: input.parse()?,
991 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700992 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -0700993 semi_token: input.parse()?,
994 })
995 }
996 }
997
998 impl Parse for ItemConst {
999 fn parse(input: ParseStream) -> Result<Self> {
1000 Ok(ItemConst {
1001 attrs: input.call(Attribute::parse_outer)?,
1002 vis: input.parse()?,
1003 const_token: input.parse()?,
1004 ident: input.parse()?,
1005 colon_token: input.parse()?,
1006 ty: input.parse()?,
1007 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001008 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001009 semi_token: input.parse()?,
1010 })
1011 }
1012 }
1013
1014 impl Parse for ItemFn {
1015 fn parse(input: ParseStream) -> Result<Self> {
1016 let outer_attrs = input.call(Attribute::parse_outer)?;
1017 let vis: Visibility = input.parse()?;
1018 let constness: Option<Token![const]> = input.parse()?;
1019 let unsafety: Option<Token![unsafe]> = input.parse()?;
1020 let asyncness: Option<Token![async]> = input.parse()?;
1021 let abi: Option<Abi> = input.parse()?;
1022 let fn_token: Token![fn] = input.parse()?;
1023 let ident: Ident = input.parse()?;
1024 let generics: Generics = input.parse()?;
1025
1026 let content;
1027 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001028 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001029
1030 let output: ReturnType = input.parse()?;
1031 let where_clause: Option<WhereClause> = input.parse()?;
1032
1033 let content;
1034 let brace_token = braced!(content in input);
1035 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001036 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001037
1038 Ok(ItemFn {
1039 attrs: {
1040 let mut attrs = outer_attrs;
1041 attrs.extend(inner_attrs);
1042 attrs
1043 },
1044 vis: vis,
1045 constness: constness,
1046 unsafety: unsafety,
1047 asyncness: asyncness,
1048 abi: abi,
1049 ident: ident,
1050 decl: Box::new(FnDecl {
1051 fn_token: fn_token,
1052 paren_token: paren_token,
1053 inputs: inputs,
1054 output: output,
1055 variadic: None,
1056 generics: Generics {
1057 where_clause: where_clause,
1058 ..generics
1059 },
1060 }),
1061 block: Box::new(Block {
1062 brace_token: brace_token,
1063 stmts: stmts,
1064 }),
1065 })
1066 }
1067 }
David Tolnay42602292016-10-01 22:25:45 -07001068
David Tolnay2ad62c12018-08-26 19:00:35 -04001069 impl Parse for FnArg {
1070 fn parse(input: ParseStream) -> Result<Self> {
1071 if input.peek(Token![&]) {
1072 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001073 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1074 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001075 }
1076 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001077
David Tolnay2ad62c12018-08-26 19:00:35 -04001078 if input.peek(Token![mut]) || input.peek(Token![self]) {
1079 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001080 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1081 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001082 }
1083 }
1084
1085 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001086 let err = match ahead.call(arg_captured) {
1087 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001088 Err(err) => err,
1089 };
1090
1091 let ahead = input.fork();
1092 if ahead.parse::<Type>().is_ok() {
1093 return input.parse().map(FnArg::Ignored);
1094 }
1095
1096 Err(err)
1097 }
1098 }
1099
David Tolnay3779bb72018-08-26 18:46:07 -07001100 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1101 Ok(ArgSelfRef {
1102 and_token: input.parse()?,
1103 lifetime: input.parse()?,
1104 mutability: input.parse()?,
1105 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001106 })
David Tolnay3779bb72018-08-26 18:46:07 -07001107 }
David Tolnay35902302016-10-06 01:11:08 -07001108
David Tolnay3779bb72018-08-26 18:46:07 -07001109 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1110 Ok(ArgSelf {
1111 mutability: input.parse()?,
1112 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001113 })
David Tolnay3779bb72018-08-26 18:46:07 -07001114 }
1115
1116 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1117 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001118 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001119 colon_token: input.parse()?,
1120 ty: input.parse()?,
1121 })
1122 }
1123
1124 impl Parse for ItemMod {
1125 fn parse(input: ParseStream) -> Result<Self> {
1126 let outer_attrs = input.call(Attribute::parse_outer)?;
1127 let vis: Visibility = input.parse()?;
1128 let mod_token: Token![mod] = input.parse()?;
1129 let ident: Ident = input.parse()?;
1130
1131 let lookahead = input.lookahead1();
1132 if lookahead.peek(Token![;]) {
1133 Ok(ItemMod {
1134 attrs: outer_attrs,
1135 vis: vis,
1136 mod_token: mod_token,
1137 ident: ident,
1138 content: None,
1139 semi: Some(input.parse()?),
1140 })
1141 } else if lookahead.peek(token::Brace) {
1142 let content;
1143 let brace_token = braced!(content in input);
1144 let inner_attrs = content.call(Attribute::parse_inner)?;
1145
1146 let mut items = Vec::new();
1147 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001148 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001149 }
1150
1151 Ok(ItemMod {
1152 attrs: {
1153 let mut attrs = outer_attrs;
1154 attrs.extend(inner_attrs);
1155 attrs
1156 },
1157 vis: vis,
1158 mod_token: mod_token,
1159 ident: ident,
1160 content: Some((brace_token, items)),
1161 semi: None,
1162 })
1163 } else {
1164 Err(lookahead.error())
1165 }
1166 }
1167 }
1168
1169 impl Parse for ItemForeignMod {
1170 fn parse(input: ParseStream) -> Result<Self> {
1171 let outer_attrs = input.call(Attribute::parse_outer)?;
1172 let abi: Abi = input.parse()?;
1173
1174 let content;
1175 let brace_token = braced!(content in input);
1176 let inner_attrs = content.call(Attribute::parse_inner)?;
1177 let mut items = Vec::new();
1178 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001179 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001180 }
1181
1182 Ok(ItemForeignMod {
1183 attrs: {
1184 let mut attrs = outer_attrs;
1185 attrs.extend(inner_attrs);
1186 attrs
1187 },
1188 abi: abi,
1189 brace_token: brace_token,
1190 items: items,
1191 })
1192 }
1193 }
David Tolnay35902302016-10-06 01:11:08 -07001194
David Tolnay6a170ce2018-08-26 22:29:24 -07001195 impl Parse for ForeignItem {
1196 fn parse(input: ParseStream) -> Result<Self> {
1197 let ahead = input.fork();
1198 ahead.call(Attribute::parse_outer)?;
1199 let vis: Visibility = ahead.parse()?;
1200
1201 let lookahead = ahead.lookahead1();
1202 if lookahead.peek(Token![fn]) {
1203 input.parse().map(ForeignItem::Fn)
1204 } else if lookahead.peek(Token![static]) {
1205 input.parse().map(ForeignItem::Static)
1206 } else if lookahead.peek(Token![type]) {
1207 input.parse().map(ForeignItem::Type)
1208 } else if vis.is_inherited()
1209 && (lookahead.peek(Ident)
1210 || lookahead.peek(Token![self])
1211 || lookahead.peek(Token![super])
1212 || lookahead.peek(Token![extern])
1213 || lookahead.peek(Token![crate])
1214 || lookahead.peek(Token![::]))
1215 {
1216 input.parse().map(ForeignItem::Macro)
1217 } else {
1218 Err(lookahead.error())
1219 }
1220 }
1221 }
David Tolnay35902302016-10-06 01:11:08 -07001222
David Tolnay3779bb72018-08-26 18:46:07 -07001223 impl Parse for ForeignItemFn {
1224 fn parse(input: ParseStream) -> Result<Self> {
1225 let attrs = input.call(Attribute::parse_outer)?;
1226 let vis: Visibility = input.parse()?;
1227 let fn_token: Token![fn] = input.parse()?;
1228 let ident: Ident = input.parse()?;
1229 let generics: Generics = input.parse()?;
1230
1231 let content;
1232 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001233 let mut inputs = Punctuated::new();
1234 while !content.is_empty() && !content.peek(Token![...]) {
1235 inputs.push_value(content.parse()?);
1236 if content.is_empty() {
1237 break;
1238 }
1239 inputs.push_punct(content.parse()?);
1240 }
David Tolnay3779bb72018-08-26 18:46:07 -07001241 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1242 content.parse()?
1243 } else {
1244 None
1245 };
1246
1247 let output: ReturnType = input.parse()?;
1248 let where_clause: Option<WhereClause> = input.parse()?;
1249 let semi_token: Token![;] = input.parse()?;
1250
1251 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001252 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001253 vis: vis,
1254 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001255 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001256 fn_token: fn_token,
1257 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001258 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001259 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001260 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001261 generics: Generics {
1262 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001263 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001264 },
1265 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001266 semi_token: semi_token,
1267 })
1268 }
1269 }
David Tolnay35902302016-10-06 01:11:08 -07001270
David Tolnay3779bb72018-08-26 18:46:07 -07001271 impl Parse for ForeignItemStatic {
1272 fn parse(input: ParseStream) -> Result<Self> {
1273 Ok(ForeignItemStatic {
1274 attrs: input.call(Attribute::parse_outer)?,
1275 vis: input.parse()?,
1276 static_token: input.parse()?,
1277 mutability: input.parse()?,
1278 ident: input.parse()?,
1279 colon_token: input.parse()?,
1280 ty: input.parse()?,
1281 semi_token: input.parse()?,
1282 })
1283 }
1284 }
David Tolnay35902302016-10-06 01:11:08 -07001285
David Tolnay3779bb72018-08-26 18:46:07 -07001286 impl Parse for ForeignItemType {
1287 fn parse(input: ParseStream) -> Result<Self> {
1288 Ok(ForeignItemType {
1289 attrs: input.call(Attribute::parse_outer)?,
1290 vis: input.parse()?,
1291 type_token: input.parse()?,
1292 ident: input.parse()?,
1293 semi_token: input.parse()?,
1294 })
1295 }
1296 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001297
David Tolnay3779bb72018-08-26 18:46:07 -07001298 impl Parse for ForeignItemMacro {
1299 fn parse(input: ParseStream) -> Result<Self> {
1300 let attrs = input.call(Attribute::parse_outer)?;
1301 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001302 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001303 None
1304 } else {
1305 Some(input.parse()?)
1306 };
1307 Ok(ForeignItemMacro {
1308 attrs: attrs,
1309 mac: mac,
1310 semi_token: semi_token,
1311 })
1312 }
1313 }
David Tolnay78572112018-08-01 00:36:18 -07001314
David Tolnay3779bb72018-08-26 18:46:07 -07001315 impl Parse for ItemType {
1316 fn parse(input: ParseStream) -> Result<Self> {
1317 Ok(ItemType {
1318 attrs: input.call(Attribute::parse_outer)?,
1319 vis: input.parse()?,
1320 type_token: input.parse()?,
1321 ident: input.parse()?,
1322 generics: {
1323 let mut generics: Generics = input.parse()?;
1324 generics.where_clause = input.parse()?;
1325 generics
1326 },
1327 eq_token: input.parse()?,
1328 ty: input.parse()?,
1329 semi_token: input.parse()?,
1330 })
1331 }
1332 }
David Tolnay3cf52982016-10-01 17:11:37 -07001333
David Tolnay3779bb72018-08-26 18:46:07 -07001334 impl Parse for ItemExistential {
1335 fn parse(input: ParseStream) -> Result<Self> {
1336 Ok(ItemExistential {
1337 attrs: input.call(Attribute::parse_outer)?,
1338 vis: input.parse()?,
1339 existential_token: input.parse()?,
1340 type_token: input.parse()?,
1341 ident: input.parse()?,
1342 generics: {
1343 let mut generics: Generics = input.parse()?;
1344 generics.where_clause = input.parse()?;
1345 generics
1346 },
1347 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001348 bounds: {
1349 let mut bounds = Punctuated::new();
1350 while !input.peek(Token![;]) {
1351 if !bounds.is_empty() {
1352 bounds.push_punct(input.parse()?);
1353 }
1354 bounds.push_value(input.parse()?);
1355 }
1356 bounds
1357 },
David Tolnay3779bb72018-08-26 18:46:07 -07001358 semi_token: input.parse()?,
1359 })
1360 }
1361 }
David Tolnay758ee132018-08-21 21:29:40 -04001362
David Tolnay6a170ce2018-08-26 22:29:24 -07001363 impl Parse for ItemStruct {
1364 fn parse(input: ParseStream) -> Result<Self> {
1365 let attrs = input.call(Attribute::parse_outer)?;
1366 let vis = input.parse::<Visibility>()?;
1367 let struct_token = input.parse::<Token![struct]>()?;
1368 let ident = input.parse::<Ident>()?;
1369 let generics = input.parse::<Generics>()?;
1370 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1371 Ok(ItemStruct {
1372 attrs: attrs,
1373 vis: vis,
1374 struct_token: struct_token,
1375 ident: ident,
1376 generics: Generics {
1377 where_clause: where_clause,
1378 ..generics
1379 },
1380 fields: fields,
1381 semi_token: semi_token,
1382 })
1383 }
1384 }
David Tolnay42602292016-10-01 22:25:45 -07001385
David Tolnay6a170ce2018-08-26 22:29:24 -07001386 impl Parse for ItemEnum {
1387 fn parse(input: ParseStream) -> Result<Self> {
1388 let attrs = input.call(Attribute::parse_outer)?;
1389 let vis = input.parse::<Visibility>()?;
1390 let enum_token = input.parse::<Token![enum]>()?;
1391 let ident = input.parse::<Ident>()?;
1392 let generics = input.parse::<Generics>()?;
1393 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1394 Ok(ItemEnum {
1395 attrs: attrs,
1396 vis: vis,
1397 enum_token: enum_token,
1398 ident: ident,
1399 generics: Generics {
1400 where_clause: where_clause,
1401 ..generics
1402 },
1403 brace_token: brace_token,
1404 variants: variants,
1405 })
1406 }
1407 }
David Tolnay4c614be2017-11-10 00:02:38 -08001408
David Tolnay6a170ce2018-08-26 22:29:24 -07001409 impl Parse for ItemUnion {
1410 fn parse(input: ParseStream) -> Result<Self> {
1411 let attrs = input.call(Attribute::parse_outer)?;
1412 let vis = input.parse::<Visibility>()?;
1413 let union_token = input.parse::<Token![union]>()?;
1414 let ident = input.parse::<Ident>()?;
1415 let generics = input.parse::<Generics>()?;
1416 let (where_clause, fields) = derive::parsing::data_union(input)?;
1417 Ok(ItemUnion {
1418 attrs: attrs,
1419 vis: vis,
1420 union_token: union_token,
1421 ident: ident,
1422 generics: Generics {
1423 where_clause: where_clause,
1424 ..generics
1425 },
1426 fields: fields,
1427 })
1428 }
1429 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001430
David Tolnay6a170ce2018-08-26 22:29:24 -07001431 impl Parse for ItemTrait {
1432 fn parse(input: ParseStream) -> Result<Self> {
1433 let attrs = input.call(Attribute::parse_outer)?;
1434 let vis: Visibility = input.parse()?;
1435 let unsafety: Option<Token![unsafe]> = input.parse()?;
1436 let auto_token: Option<Token![auto]> = input.parse()?;
1437 let trait_token: Token![trait] = input.parse()?;
1438 let ident: Ident = input.parse()?;
1439 let mut generics: Generics = input.parse()?;
1440 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001441
1442 let mut supertraits = Punctuated::new();
1443 if colon_token.is_some() {
David Tolnaya22a5742018-08-30 22:52:47 -07001444 loop {
David Tolnayf5ebc192018-08-30 18:23:46 -07001445 supertraits.push_value(input.parse()?);
David Tolnaya22a5742018-08-30 22:52:47 -07001446 if input.peek(Token![where]) || input.peek(token::Brace) {
1447 break;
1448 }
1449 supertraits.push_punct(input.parse()?);
1450 if input.peek(Token![where]) || input.peek(token::Brace) {
1451 break;
1452 }
David Tolnayf5ebc192018-08-30 18:23:46 -07001453 }
1454 }
1455
David Tolnay6a170ce2018-08-26 22:29:24 -07001456 generics.where_clause = input.parse()?;
1457
1458 let content;
1459 let brace_token = braced!(content in input);
1460 let mut items = Vec::new();
1461 while !content.is_empty() {
1462 items.push(content.parse()?);
1463 }
1464
1465 Ok(ItemTrait {
1466 attrs: attrs,
1467 vis: vis,
1468 unsafety: unsafety,
1469 auto_token: auto_token,
1470 trait_token: trait_token,
1471 ident: ident,
1472 generics: generics,
1473 colon_token: colon_token,
1474 supertraits: supertraits,
1475 brace_token: brace_token,
1476 items: items,
1477 })
1478 }
1479 }
1480
1481 impl Parse for TraitItem {
1482 fn parse(input: ParseStream) -> Result<Self> {
1483 let ahead = input.fork();
1484 ahead.call(Attribute::parse_outer)?;
1485
1486 let lookahead = ahead.lookahead1();
1487 if lookahead.peek(Token![const]) {
1488 ahead.parse::<Token![const]>()?;
1489 let lookahead = ahead.lookahead1();
1490 if lookahead.peek(Ident) {
1491 input.parse().map(TraitItem::Const)
1492 } else if lookahead.peek(Token![unsafe])
1493 || lookahead.peek(Token![extern])
1494 || lookahead.peek(Token![fn])
1495 {
1496 input.parse().map(TraitItem::Method)
1497 } else {
1498 Err(lookahead.error())
1499 }
1500 } else if lookahead.peek(Token![unsafe])
1501 || lookahead.peek(Token![extern])
1502 || lookahead.peek(Token![fn])
1503 {
1504 input.parse().map(TraitItem::Method)
1505 } else if lookahead.peek(Token![type]) {
1506 input.parse().map(TraitItem::Type)
1507 } else if lookahead.peek(Ident)
1508 || lookahead.peek(Token![self])
1509 || lookahead.peek(Token![super])
1510 || lookahead.peek(Token![extern])
1511 || lookahead.peek(Token![crate])
1512 || lookahead.peek(Token![::])
1513 {
1514 input.parse().map(TraitItem::Macro)
1515 } else {
1516 Err(lookahead.error())
1517 }
1518 }
1519 }
1520
1521 impl Parse for TraitItemConst {
1522 fn parse(input: ParseStream) -> Result<Self> {
1523 Ok(TraitItemConst {
1524 attrs: input.call(Attribute::parse_outer)?,
1525 const_token: input.parse()?,
1526 ident: input.parse()?,
1527 colon_token: input.parse()?,
1528 ty: input.parse()?,
1529 default: {
1530 if input.peek(Token![=]) {
1531 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001532 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001533 Some((eq_token, default))
1534 } else {
1535 None
1536 }
1537 },
1538 semi_token: input.parse()?,
1539 })
1540 }
1541 }
1542
1543 impl Parse for TraitItemMethod {
1544 fn parse(input: ParseStream) -> Result<Self> {
1545 let outer_attrs = input.call(Attribute::parse_outer)?;
1546 let constness: Option<Token![const]> = input.parse()?;
1547 let unsafety: Option<Token![unsafe]> = input.parse()?;
1548 let abi: Option<Abi> = input.parse()?;
1549 let fn_token: Token![fn] = input.parse()?;
1550 let ident: Ident = input.parse()?;
1551 let generics: Generics = input.parse()?;
1552
1553 let content;
1554 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001555 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001556
1557 let output: ReturnType = input.parse()?;
1558 let where_clause: Option<WhereClause> = input.parse()?;
1559
1560 let lookahead = input.lookahead1();
1561 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1562 let content;
1563 let brace_token = braced!(content in input);
1564 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001565 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001566 (Some(brace_token), inner_attrs, stmts, None)
1567 } else if lookahead.peek(Token![;]) {
1568 let semi_token: Token![;] = input.parse()?;
1569 (None, Vec::new(), Vec::new(), Some(semi_token))
1570 } else {
1571 return Err(lookahead.error());
1572 };
1573
1574 Ok(TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001575 attrs: {
1576 let mut attrs = outer_attrs;
1577 attrs.extend(inner_attrs);
1578 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001579 },
David Tolnayda705bd2017-11-10 21:58:05 -08001580 sig: MethodSig {
1581 constness: constness,
1582 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001583 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001584 abi: abi,
1585 ident: ident,
1586 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001587 fn_token: fn_token,
1588 paren_token: paren_token,
1589 inputs: inputs,
1590 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001591 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001592 generics: Generics {
1593 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001594 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001595 },
1596 },
David Tolnayda705bd2017-11-10 21:58:05 -08001597 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001598 default: brace_token.map(|brace_token| Block {
1599 brace_token: brace_token,
1600 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001601 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001602 semi_token: semi_token,
1603 })
1604 }
1605 }
1606
1607 impl Parse for TraitItemType {
1608 fn parse(input: ParseStream) -> Result<Self> {
1609 let attrs = input.call(Attribute::parse_outer)?;
1610 let type_token: Token![type] = input.parse()?;
1611 let ident: Ident = input.parse()?;
1612 let mut generics: Generics = input.parse()?;
1613 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001614
1615 let mut bounds = Punctuated::new();
1616 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001617 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1618 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001619 if !bounds.is_empty() {
1620 bounds.push_punct(input.parse()?);
1621 }
1622 bounds.push_value(input.parse()?);
1623 }
1624 }
1625
David Tolnay6a170ce2018-08-26 22:29:24 -07001626 generics.where_clause = input.parse()?;
1627 let default = if input.peek(Token![=]) {
1628 let eq_token: Token![=] = input.parse()?;
1629 let default: Type = input.parse()?;
1630 Some((eq_token, default))
1631 } else {
1632 None
1633 };
1634 let semi_token: Token![;] = input.parse()?;
1635
1636 Ok(TraitItemType {
1637 attrs: attrs,
1638 type_token: type_token,
1639 ident: ident,
1640 generics: generics,
1641 colon_token: colon_token,
1642 bounds: bounds,
1643 default: default,
1644 semi_token: semi_token,
1645 })
1646 }
1647 }
1648
1649 impl Parse for TraitItemMacro {
1650 fn parse(input: ParseStream) -> Result<Self> {
1651 let attrs = input.call(Attribute::parse_outer)?;
1652 let mac: Macro = input.parse()?;
1653 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1654 None
1655 } else {
1656 Some(input.parse()?)
1657 };
1658 Ok(TraitItemMacro {
1659 attrs: attrs,
1660 mac: mac,
1661 semi_token: semi_token,
1662 })
1663 }
1664 }
1665
1666 impl Parse for ItemImpl {
1667 fn parse(input: ParseStream) -> Result<Self> {
1668 let outer_attrs = input.call(Attribute::parse_outer)?;
1669 let defaultness: Option<Token![default]> = input.parse()?;
1670 let unsafety: Option<Token![unsafe]> = input.parse()?;
1671 let impl_token: Token![impl ] = input.parse()?;
1672 let generics: Generics = input.parse()?;
1673 let trait_ = {
1674 let ahead = input.fork();
1675 if ahead.parse::<Option<Token![!]>>().is_ok()
1676 && ahead.parse::<Path>().is_ok()
1677 && ahead.parse::<Token![for]>().is_ok()
1678 {
1679 let polarity: Option<Token![!]> = input.parse()?;
1680 let path: Path = input.parse()?;
1681 let for_token: Token![for] = input.parse()?;
1682 Some((polarity, path, for_token))
1683 } else {
1684 None
1685 }
1686 };
1687 let self_ty: Type = input.parse()?;
1688 let where_clause: Option<WhereClause> = input.parse()?;
1689
1690 let content;
1691 let brace_token = braced!(content in input);
1692 let inner_attrs = content.call(Attribute::parse_inner)?;
1693
1694 let mut items = Vec::new();
1695 while !content.is_empty() {
1696 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001697 }
David Tolnay0aecb732016-10-03 23:03:50 -07001698
David Tolnay6a170ce2018-08-26 22:29:24 -07001699 Ok(ItemImpl {
1700 attrs: {
1701 let mut attrs = outer_attrs;
1702 attrs.extend(inner_attrs);
1703 attrs
1704 },
1705 defaultness: defaultness,
1706 unsafety: unsafety,
1707 impl_token: impl_token,
1708 generics: Generics {
1709 where_clause: where_clause,
1710 ..generics
1711 },
1712 trait_: trait_,
1713 self_ty: Box::new(self_ty),
1714 brace_token: brace_token,
1715 items: items,
1716 })
1717 }
1718 }
David Tolnay0aecb732016-10-03 23:03:50 -07001719
David Tolnay6a170ce2018-08-26 22:29:24 -07001720 impl Parse for ImplItem {
1721 fn parse(input: ParseStream) -> Result<Self> {
1722 let ahead = input.fork();
1723 ahead.call(Attribute::parse_outer)?;
1724 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001725
David Tolnay6a170ce2018-08-26 22:29:24 -07001726 let mut lookahead = ahead.lookahead1();
1727 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1728 let defaultness: Token![default] = ahead.parse()?;
1729 lookahead = ahead.lookahead1();
1730 Some(defaultness)
1731 } else {
1732 None
1733 };
David Tolnay4c9be372016-10-06 00:47:37 -07001734
David Tolnay6a170ce2018-08-26 22:29:24 -07001735 if lookahead.peek(Token![const]) {
1736 ahead.parse::<Token![const]>()?;
1737 let lookahead = ahead.lookahead1();
1738 if lookahead.peek(Ident) {
1739 input.parse().map(ImplItem::Const)
1740 } else if lookahead.peek(Token![unsafe])
1741 || lookahead.peek(Token![async])
1742 || lookahead.peek(Token![extern])
1743 || lookahead.peek(Token![fn])
1744 {
1745 input.parse().map(ImplItem::Method)
1746 } else {
1747 Err(lookahead.error())
1748 }
1749 } else if lookahead.peek(Token![unsafe])
1750 || lookahead.peek(Token![async])
1751 || lookahead.peek(Token![extern])
1752 || lookahead.peek(Token![fn])
1753 {
1754 input.parse().map(ImplItem::Method)
1755 } else if lookahead.peek(Token![type]) {
1756 input.parse().map(ImplItem::Type)
1757 } else if vis.is_inherited()
1758 && defaultness.is_none()
1759 && lookahead.peek(Token![existential])
1760 {
1761 input.parse().map(ImplItem::Existential)
1762 } else if vis.is_inherited()
1763 && defaultness.is_none()
1764 && (lookahead.peek(Ident)
1765 || lookahead.peek(Token![self])
1766 || lookahead.peek(Token![super])
1767 || lookahead.peek(Token![extern])
1768 || lookahead.peek(Token![crate])
1769 || lookahead.peek(Token![::]))
1770 {
1771 input.parse().map(ImplItem::Macro)
1772 } else {
1773 Err(lookahead.error())
1774 }
1775 }
1776 }
David Tolnay4c9be372016-10-06 00:47:37 -07001777
David Tolnay3779bb72018-08-26 18:46:07 -07001778 impl Parse for ImplItemConst {
1779 fn parse(input: ParseStream) -> Result<Self> {
1780 Ok(ImplItemConst {
1781 attrs: input.call(Attribute::parse_outer)?,
1782 vis: input.parse()?,
1783 defaultness: input.parse()?,
1784 const_token: input.parse()?,
1785 ident: input.parse()?,
1786 colon_token: input.parse()?,
1787 ty: input.parse()?,
1788 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001789 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001790 semi_token: input.parse()?,
1791 })
1792 }
1793 }
David Tolnay4c9be372016-10-06 00:47:37 -07001794
David Tolnay6a170ce2018-08-26 22:29:24 -07001795 impl Parse for ImplItemMethod {
1796 fn parse(input: ParseStream) -> Result<Self> {
1797 let outer_attrs = input.call(Attribute::parse_outer)?;
1798 let vis: Visibility = input.parse()?;
1799 let defaultness: Option<Token![default]> = input.parse()?;
1800 let constness: Option<Token![const]> = input.parse()?;
1801 let unsafety: Option<Token![unsafe]> = input.parse()?;
1802 let asyncness: Option<Token![async]> = input.parse()?;
1803 let abi: Option<Abi> = input.parse()?;
1804 let fn_token: Token![fn] = input.parse()?;
1805 let ident: Ident = input.parse()?;
1806 let generics: Generics = input.parse()?;
1807
1808 let content;
1809 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001810 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001811
1812 let output: ReturnType = input.parse()?;
1813 let where_clause: Option<WhereClause> = input.parse()?;
1814
1815 let content;
1816 let brace_token = braced!(content in input);
1817 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001818 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001819
1820 Ok(ImplItemMethod {
1821 attrs: {
1822 let mut attrs = outer_attrs;
1823 attrs.extend(inner_attrs);
1824 attrs
David Tolnay4c9be372016-10-06 00:47:37 -07001825 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001826 vis: vis,
1827 defaultness: defaultness,
1828 sig: MethodSig {
1829 constness: constness,
1830 unsafety: unsafety,
1831 asyncness: asyncness,
1832 abi: abi,
1833 ident: ident,
1834 decl: FnDecl {
1835 fn_token: fn_token,
1836 paren_token: paren_token,
1837 inputs: inputs,
1838 output: output,
1839 variadic: None,
1840 generics: Generics {
1841 where_clause: where_clause,
1842 ..generics
1843 },
1844 },
1845 },
1846 block: Block {
1847 brace_token: brace_token,
1848 stmts: stmts,
1849 },
1850 })
1851 }
1852 }
David Tolnay4c9be372016-10-06 00:47:37 -07001853
David Tolnay3779bb72018-08-26 18:46:07 -07001854 impl Parse for ImplItemType {
1855 fn parse(input: ParseStream) -> Result<Self> {
1856 Ok(ImplItemType {
1857 attrs: input.call(Attribute::parse_outer)?,
1858 vis: input.parse()?,
1859 defaultness: input.parse()?,
1860 type_token: input.parse()?,
1861 ident: input.parse()?,
1862 generics: {
1863 let mut generics: Generics = input.parse()?;
1864 generics.where_clause = input.parse()?;
1865 generics
1866 },
1867 eq_token: input.parse()?,
1868 ty: input.parse()?,
1869 semi_token: input.parse()?,
1870 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001871 }
David Tolnay3779bb72018-08-26 18:46:07 -07001872 }
David Tolnay758ee132018-08-21 21:29:40 -04001873
David Tolnay3779bb72018-08-26 18:46:07 -07001874 impl Parse for ImplItemExistential {
1875 fn parse(input: ParseStream) -> Result<Self> {
1876 let ety: ItemExistential = input.parse()?;
1877 Ok(ImplItemExistential {
1878 attrs: ety.attrs,
1879 existential_token: ety.existential_token,
1880 type_token: ety.type_token,
1881 ident: ety.ident,
1882 generics: ety.generics,
1883 colon_token: ety.colon_token,
1884 bounds: ety.bounds,
1885 semi_token: ety.semi_token,
1886 })
1887 }
1888 }
1889
1890 impl Parse for ImplItemMacro {
1891 fn parse(input: ParseStream) -> Result<Self> {
1892 let attrs = input.call(Attribute::parse_outer)?;
1893 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001894 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001895 None
1896 } else {
1897 Some(input.parse()?)
1898 };
1899 Ok(ImplItemMacro {
1900 attrs: attrs,
1901 mac: mac,
1902 semi_token: semi_token,
1903 })
1904 }
1905 }
David Tolnay4c9be372016-10-06 00:47:37 -07001906
David Tolnay6a170ce2018-08-26 22:29:24 -07001907 impl Visibility {
1908 fn is_inherited(&self) -> bool {
1909 match *self {
1910 Visibility::Inherited => true,
1911 _ => false,
1912 }
1913 }
1914 }
1915
1916 impl MacroDelimiter {
1917 fn is_brace(&self) -> bool {
1918 match *self {
1919 MacroDelimiter::Brace(_) => true,
1920 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1921 }
David Tolnay57292da2017-12-27 21:03:33 -05001922 }
1923 }
David Tolnayedf2b992016-09-23 20:43:45 -07001924}
David Tolnay4a51dc72016-10-01 00:40:31 -07001925
1926#[cfg(feature = "printing")]
1927mod printing {
1928 use super::*;
1929 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001930 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001931 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001932
David Tolnay1bfa7332017-11-11 12:41:20 -08001933 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001934 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001935 tokens.append_all(self.attrs.outer());
1936 self.vis.to_tokens(tokens);
1937 self.extern_token.to_tokens(tokens);
1938 self.crate_token.to_tokens(tokens);
1939 self.ident.to_tokens(tokens);
1940 if let Some((ref as_token, ref rename)) = self.rename {
1941 as_token.to_tokens(tokens);
1942 rename.to_tokens(tokens);
1943 }
1944 self.semi_token.to_tokens(tokens);
1945 }
1946 }
1947
1948 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001949 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001950 tokens.append_all(self.attrs.outer());
1951 self.vis.to_tokens(tokens);
1952 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001953 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001954 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001955 self.semi_token.to_tokens(tokens);
1956 }
1957 }
1958
1959 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001960 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001961 tokens.append_all(self.attrs.outer());
1962 self.vis.to_tokens(tokens);
1963 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001964 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001965 self.ident.to_tokens(tokens);
1966 self.colon_token.to_tokens(tokens);
1967 self.ty.to_tokens(tokens);
1968 self.eq_token.to_tokens(tokens);
1969 self.expr.to_tokens(tokens);
1970 self.semi_token.to_tokens(tokens);
1971 }
1972 }
1973
1974 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001975 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001976 tokens.append_all(self.attrs.outer());
1977 self.vis.to_tokens(tokens);
1978 self.const_token.to_tokens(tokens);
1979 self.ident.to_tokens(tokens);
1980 self.colon_token.to_tokens(tokens);
1981 self.ty.to_tokens(tokens);
1982 self.eq_token.to_tokens(tokens);
1983 self.expr.to_tokens(tokens);
1984 self.semi_token.to_tokens(tokens);
1985 }
1986 }
1987
1988 impl ToTokens for ItemFn {
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.vis.to_tokens(tokens);
1992 self.constness.to_tokens(tokens);
1993 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001994 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001995 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001996 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001997 self.block.brace_token.surround(tokens, |tokens| {
1998 tokens.append_all(self.attrs.inner());
1999 tokens.append_all(&self.block.stmts);
2000 });
2001 }
2002 }
2003
2004 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002005 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002006 tokens.append_all(self.attrs.outer());
2007 self.vis.to_tokens(tokens);
2008 self.mod_token.to_tokens(tokens);
2009 self.ident.to_tokens(tokens);
2010 if let Some((ref brace, ref items)) = self.content {
2011 brace.surround(tokens, |tokens| {
2012 tokens.append_all(self.attrs.inner());
2013 tokens.append_all(items);
2014 });
2015 } else {
2016 TokensOrDefault(&self.semi).to_tokens(tokens);
2017 }
2018 }
2019 }
2020
2021 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002022 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002023 tokens.append_all(self.attrs.outer());
2024 self.abi.to_tokens(tokens);
2025 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002026 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002027 tokens.append_all(&self.items);
2028 });
2029 }
2030 }
2031
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002032 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002033 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002034 tokens.append_all(self.attrs.outer());
2035 self.vis.to_tokens(tokens);
2036 self.type_token.to_tokens(tokens);
2037 self.ident.to_tokens(tokens);
2038 self.generics.to_tokens(tokens);
2039 self.generics.where_clause.to_tokens(tokens);
2040 self.eq_token.to_tokens(tokens);
2041 self.ty.to_tokens(tokens);
2042 self.semi_token.to_tokens(tokens);
2043 }
2044 }
2045
David Tolnaybb82ef02018-08-24 20:15:45 -04002046 impl ToTokens for ItemExistential {
2047 fn to_tokens(&self, tokens: &mut TokenStream) {
2048 tokens.append_all(self.attrs.outer());
2049 self.vis.to_tokens(tokens);
2050 self.existential_token.to_tokens(tokens);
2051 self.type_token.to_tokens(tokens);
2052 self.ident.to_tokens(tokens);
2053 self.generics.to_tokens(tokens);
2054 self.generics.where_clause.to_tokens(tokens);
2055 if !self.bounds.is_empty() {
2056 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2057 self.bounds.to_tokens(tokens);
2058 }
2059 self.semi_token.to_tokens(tokens);
2060 }
2061 }
2062
David Tolnay1bfa7332017-11-11 12:41:20 -08002063 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002064 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002065 tokens.append_all(self.attrs.outer());
2066 self.vis.to_tokens(tokens);
2067 self.enum_token.to_tokens(tokens);
2068 self.ident.to_tokens(tokens);
2069 self.generics.to_tokens(tokens);
2070 self.generics.where_clause.to_tokens(tokens);
2071 self.brace_token.surround(tokens, |tokens| {
2072 self.variants.to_tokens(tokens);
2073 });
2074 }
2075 }
2076
2077 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002078 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002079 tokens.append_all(self.attrs.outer());
2080 self.vis.to_tokens(tokens);
2081 self.struct_token.to_tokens(tokens);
2082 self.ident.to_tokens(tokens);
2083 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002084 match self.fields {
2085 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002086 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002087 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002088 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002089 Fields::Unnamed(ref fields) => {
2090 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002091 self.generics.where_clause.to_tokens(tokens);
2092 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002093 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002094 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002095 self.generics.where_clause.to_tokens(tokens);
2096 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002097 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002098 }
2099 }
2100 }
2101
2102 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002103 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002104 tokens.append_all(self.attrs.outer());
2105 self.vis.to_tokens(tokens);
2106 self.union_token.to_tokens(tokens);
2107 self.ident.to_tokens(tokens);
2108 self.generics.to_tokens(tokens);
2109 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002110 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002111 }
2112 }
2113
2114 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002115 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002116 tokens.append_all(self.attrs.outer());
2117 self.vis.to_tokens(tokens);
2118 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002119 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002120 self.trait_token.to_tokens(tokens);
2121 self.ident.to_tokens(tokens);
2122 self.generics.to_tokens(tokens);
2123 if !self.supertraits.is_empty() {
2124 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2125 self.supertraits.to_tokens(tokens);
2126 }
2127 self.generics.where_clause.to_tokens(tokens);
2128 self.brace_token.surround(tokens, |tokens| {
2129 tokens.append_all(&self.items);
2130 });
2131 }
2132 }
2133
David Tolnay1bfa7332017-11-11 12:41:20 -08002134 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002135 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002136 tokens.append_all(self.attrs.outer());
2137 self.defaultness.to_tokens(tokens);
2138 self.unsafety.to_tokens(tokens);
2139 self.impl_token.to_tokens(tokens);
2140 self.generics.to_tokens(tokens);
2141 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2142 polarity.to_tokens(tokens);
2143 path.to_tokens(tokens);
2144 for_token.to_tokens(tokens);
2145 }
2146 self.self_ty.to_tokens(tokens);
2147 self.generics.where_clause.to_tokens(tokens);
2148 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002149 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002150 tokens.append_all(&self.items);
2151 });
2152 }
2153 }
2154
2155 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002156 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002157 tokens.append_all(self.attrs.outer());
2158 self.mac.path.to_tokens(tokens);
2159 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002160 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002161 match self.mac.delimiter {
2162 MacroDelimiter::Paren(ref paren) => {
2163 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2164 }
2165 MacroDelimiter::Brace(ref brace) => {
2166 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2167 }
2168 MacroDelimiter::Bracket(ref bracket) => {
2169 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2170 }
2171 }
David Tolnay57292da2017-12-27 21:03:33 -05002172 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002173 }
2174 }
David Tolnay42602292016-10-01 22:25:45 -07002175
David Tolnay500d8322017-12-18 00:32:51 -08002176 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002177 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002178 tokens.append_all(self.attrs.outer());
2179 self.vis.to_tokens(tokens);
2180 self.macro_token.to_tokens(tokens);
2181 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002182 self.paren_token.surround(tokens, |tokens| {
2183 self.args.to_tokens(tokens);
2184 });
2185 self.brace_token.surround(tokens, |tokens| {
2186 self.body.to_tokens(tokens);
2187 });
David Tolnay500d8322017-12-18 00:32:51 -08002188 }
2189 }
2190
David Tolnay2ae520a2017-12-29 11:19:50 -05002191 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002192 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002193 self.tts.to_tokens(tokens);
2194 }
2195 }
2196
David Tolnay5f332a92017-12-26 00:42:45 -05002197 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002198 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002199 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002200 self.colon2_token.to_tokens(tokens);
2201 self.tree.to_tokens(tokens);
2202 }
2203 }
2204
2205 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002206 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002207 self.ident.to_tokens(tokens);
2208 }
2209 }
2210
2211 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002212 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002213 self.ident.to_tokens(tokens);
2214 self.as_token.to_tokens(tokens);
2215 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002216 }
2217 }
2218
David Tolnay5f332a92017-12-26 00:42:45 -05002219 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002220 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002221 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002222 }
2223 }
2224
David Tolnayd97a7d22018-03-31 19:17:01 +02002225 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002226 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002227 self.brace_token.surround(tokens, |tokens| {
2228 self.items.to_tokens(tokens);
2229 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002230 }
2231 }
2232
David Tolnay1bfa7332017-11-11 12:41:20 -08002233 impl ToTokens for TraitItemConst {
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.const_token.to_tokens(tokens);
2237 self.ident.to_tokens(tokens);
2238 self.colon_token.to_tokens(tokens);
2239 self.ty.to_tokens(tokens);
2240 if let Some((ref eq_token, ref default)) = self.default {
2241 eq_token.to_tokens(tokens);
2242 default.to_tokens(tokens);
2243 }
2244 self.semi_token.to_tokens(tokens);
2245 }
2246 }
2247
2248 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002249 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002250 tokens.append_all(self.attrs.outer());
2251 self.sig.to_tokens(tokens);
2252 match self.default {
2253 Some(ref block) => {
2254 block.brace_token.surround(tokens, |tokens| {
2255 tokens.append_all(self.attrs.inner());
2256 tokens.append_all(&block.stmts);
2257 });
David Tolnayca085422016-10-04 00:12:38 -07002258 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002259 None => {
2260 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002261 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002262 }
2263 }
2264 }
2265
2266 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002267 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002268 tokens.append_all(self.attrs.outer());
2269 self.type_token.to_tokens(tokens);
2270 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002271 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002272 if !self.bounds.is_empty() {
2273 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2274 self.bounds.to_tokens(tokens);
2275 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002276 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002277 if let Some((ref eq_token, ref default)) = self.default {
2278 eq_token.to_tokens(tokens);
2279 default.to_tokens(tokens);
2280 }
2281 self.semi_token.to_tokens(tokens);
2282 }
2283 }
2284
2285 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002286 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002287 tokens.append_all(self.attrs.outer());
2288 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002289 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002290 }
2291 }
2292
David Tolnay2ae520a2017-12-29 11:19:50 -05002293 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002294 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002295 self.tts.to_tokens(tokens);
2296 }
2297 }
2298
David Tolnay857628c2017-11-11 12:25:31 -08002299 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002300 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002301 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002302 self.vis.to_tokens(tokens);
2303 self.defaultness.to_tokens(tokens);
2304 self.const_token.to_tokens(tokens);
2305 self.ident.to_tokens(tokens);
2306 self.colon_token.to_tokens(tokens);
2307 self.ty.to_tokens(tokens);
2308 self.eq_token.to_tokens(tokens);
2309 self.expr.to_tokens(tokens);
2310 self.semi_token.to_tokens(tokens);
2311 }
2312 }
2313
2314 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002315 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002316 tokens.append_all(self.attrs.outer());
2317 self.vis.to_tokens(tokens);
2318 self.defaultness.to_tokens(tokens);
2319 self.sig.to_tokens(tokens);
2320 self.block.brace_token.surround(tokens, |tokens| {
2321 tokens.append_all(self.attrs.inner());
2322 tokens.append_all(&self.block.stmts);
2323 });
2324 }
2325 }
2326
2327 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002328 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002329 tokens.append_all(self.attrs.outer());
2330 self.vis.to_tokens(tokens);
2331 self.defaultness.to_tokens(tokens);
2332 self.type_token.to_tokens(tokens);
2333 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002334 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002335 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002336 self.eq_token.to_tokens(tokens);
2337 self.ty.to_tokens(tokens);
2338 self.semi_token.to_tokens(tokens);
2339 }
2340 }
2341
David Tolnaybb82ef02018-08-24 20:15:45 -04002342 impl ToTokens for ImplItemExistential {
2343 fn to_tokens(&self, tokens: &mut TokenStream) {
2344 tokens.append_all(self.attrs.outer());
2345 self.existential_token.to_tokens(tokens);
2346 self.type_token.to_tokens(tokens);
2347 self.ident.to_tokens(tokens);
2348 self.generics.to_tokens(tokens);
2349 self.generics.where_clause.to_tokens(tokens);
2350 if !self.bounds.is_empty() {
2351 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2352 self.bounds.to_tokens(tokens);
2353 }
2354 self.semi_token.to_tokens(tokens);
2355 }
2356 }
2357
David Tolnay857628c2017-11-11 12:25:31 -08002358 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002359 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002360 tokens.append_all(self.attrs.outer());
2361 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002362 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002363 }
2364 }
2365
David Tolnay2ae520a2017-12-29 11:19:50 -05002366 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002367 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002368 self.tts.to_tokens(tokens);
2369 }
2370 }
2371
David Tolnay8894f602017-11-11 12:11:04 -08002372 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002373 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002374 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002375 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002376 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002377 self.semi_token.to_tokens(tokens);
2378 }
2379 }
2380
2381 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002382 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002383 tokens.append_all(self.attrs.outer());
2384 self.vis.to_tokens(tokens);
2385 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002386 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002387 self.ident.to_tokens(tokens);
2388 self.colon_token.to_tokens(tokens);
2389 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002390 self.semi_token.to_tokens(tokens);
2391 }
2392 }
2393
David Tolnay199bcbb2017-11-12 10:33:52 -08002394 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002395 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002396 tokens.append_all(self.attrs.outer());
2397 self.vis.to_tokens(tokens);
2398 self.type_token.to_tokens(tokens);
2399 self.ident.to_tokens(tokens);
2400 self.semi_token.to_tokens(tokens);
2401 }
2402 }
2403
David Tolnay435c1782018-08-24 16:15:44 -04002404 impl ToTokens for ForeignItemMacro {
2405 fn to_tokens(&self, tokens: &mut TokenStream) {
2406 tokens.append_all(self.attrs.outer());
2407 self.mac.to_tokens(tokens);
2408 self.semi_token.to_tokens(tokens);
2409 }
2410 }
2411
David Tolnay2ae520a2017-12-29 11:19:50 -05002412 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002413 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002414 self.tts.to_tokens(tokens);
2415 }
2416 }
2417
David Tolnay570695e2017-06-03 16:15:13 -07002418 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002419 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002420 self.constness.to_tokens(tokens);
2421 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002422 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002423 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002424 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002425 }
2426 }
2427
Alex Crichtona74a1c82018-05-16 10:20:44 -07002428 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002429
2430 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002431 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002432 self.0.fn_token.to_tokens(tokens);
2433 self.1.to_tokens(tokens);
2434 self.0.generics.to_tokens(tokens);
2435 self.0.paren_token.surround(tokens, |tokens| {
2436 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002437 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2438 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002439 }
David Tolnayd2836e22017-12-27 23:13:00 -05002440 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002441 });
2442 self.0.output.to_tokens(tokens);
2443 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002444 }
2445 }
2446
Alex Crichton62a0a592017-05-22 13:58:53 -07002447 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002448 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002449 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002450 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002451 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002452 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002453 }
2454 }
2455
2456 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002457 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002458 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002459 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002460 }
2461 }
2462
2463 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002464 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002465 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002466 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002467 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002468 }
2469 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002470}