blob: d95019d2423c92df71e96f8a1a5b1442dbad678f [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
David Tolnayc6b04dd2018-08-30 23:22:51 -0700209 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
210 ///
211 /// *This type is available if Syn is built with the `"full"` feature.*
212 pub TraitAlias(ItemTraitAlias {
213 pub attrs: Vec<Attribute>,
214 pub vis: Visibility,
215 pub trait_token: Token![trait],
216 pub ident: Ident,
217 pub generics: Generics,
218 pub eq_token: Token![=],
219 pub bounds: Punctuated<TypeParamBound, Token![+]>,
220 pub semi_token: Token![;],
221 }),
222
David Tolnay2b214082018-01-07 01:30:18 -0800223 /// An impl block providing trait or associated items: `impl<A> Trait
224 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800225 ///
226 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800228 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500229 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500230 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800231 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700233 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500234 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700235 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800236 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500237 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700238 pub items: Vec<ImplItem>,
239 }),
David Tolnay2b214082018-01-07 01:30:18 -0800240
241 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800242 ///
243 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800244 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800245 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800246 /// The `example` in `macro_rules! example { ... }`.
247 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800248 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500249 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800250 }),
David Tolnay2b214082018-01-07 01:30:18 -0800251
252 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500255 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800256 pub attrs: Vec<Attribute>,
257 pub vis: Visibility,
258 pub macro_token: Token![macro],
259 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500260 pub paren_token: Paren,
261 pub args: TokenStream,
262 pub brace_token: Brace,
263 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800264 }),
David Tolnay2b214082018-01-07 01:30:18 -0800265
266 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800267 ///
268 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500269 pub Verbatim(ItemVerbatim #manual_extra_traits {
270 pub tts: TokenStream,
271 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 }
David Tolnayb79ee962016-09-04 09:39:20 -0700273}
274
David Tolnay9c76bcb2017-12-26 23:14:59 -0500275#[cfg(feature = "extra-traits")]
276impl Eq for ItemMacro2 {}
277
278#[cfg(feature = "extra-traits")]
279impl PartialEq for ItemMacro2 {
280 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700281 self.attrs == other.attrs
282 && self.vis == other.vis
283 && self.macro_token == other.macro_token
284 && self.ident == other.ident
285 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500286 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
287 && self.brace_token == other.brace_token
288 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500289 }
290}
291
292#[cfg(feature = "extra-traits")]
293impl Hash for ItemMacro2 {
294 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500295 where
296 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500297 {
298 self.attrs.hash(state);
299 self.vis.hash(state);
300 self.macro_token.hash(state);
301 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500302 self.paren_token.hash(state);
303 TokenStreamHelper(&self.args).hash(state);
304 self.brace_token.hash(state);
305 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500306 }
307}
308
David Tolnay2ae520a2017-12-29 11:19:50 -0500309#[cfg(feature = "extra-traits")]
310impl Eq for ItemVerbatim {}
311
312#[cfg(feature = "extra-traits")]
313impl PartialEq for ItemVerbatim {
314 fn eq(&self, other: &Self) -> bool {
315 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
316 }
317}
318
319#[cfg(feature = "extra-traits")]
320impl Hash for ItemVerbatim {
321 fn hash<H>(&self, state: &mut H)
322 where
323 H: Hasher,
324 {
325 TokenStreamHelper(&self.tts).hash(state);
326 }
327}
328
David Tolnay0e837402016-12-22 17:25:55 -0500329impl From<DeriveInput> for Item {
330 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500331 match input.data {
332 Data::Struct(data) => Item::Struct(ItemStruct {
333 attrs: input.attrs,
334 vis: input.vis,
335 struct_token: data.struct_token,
336 ident: input.ident,
337 generics: input.generics,
338 fields: data.fields,
339 semi_token: data.semi_token,
340 }),
341 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500342 attrs: input.attrs,
343 vis: input.vis,
344 enum_token: data.enum_token,
345 ident: input.ident,
346 generics: input.generics,
347 brace_token: data.brace_token,
348 variants: data.variants,
349 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500350 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500351 attrs: input.attrs,
352 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500353 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500354 ident: input.ident,
355 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500356 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500357 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700358 }
359 }
360}
361
Alex Crichton62a0a592017-05-22 13:58:53 -0700362ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800363 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800364 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800365 /// *This type is available if Syn is built with the `"full"` feature.*
366 ///
David Tolnay614a0142018-01-07 10:25:43 -0800367 /// # Syntax tree enum
368 ///
369 /// This type is a [syntax tree enum].
370 ///
371 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500372 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200373 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800374 ///
375 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500376 pub Path(UsePath {
377 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200378 pub colon2_token: Token![::],
379 pub tree: Box<UseTree>,
380 }),
381
382 /// An identifier imported by a `use` item: `HashMap`.
383 ///
384 /// *This type is available if Syn is built with the `"full"` feature.*
385 pub Name(UseName {
386 pub ident: Ident,
387 }),
388
389 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
390 ///
391 /// *This type is available if Syn is built with the `"full"` feature.*
392 pub Rename(UseRename {
393 pub ident: Ident,
394 pub as_token: Token![as],
395 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800397
David Tolnay05658502018-01-07 09:56:37 -0800398 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800399 ///
400 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500401 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800402 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800404
David Tolnayd97a7d22018-03-31 19:17:01 +0200405 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800406 ///
407 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200408 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500409 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500410 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 }),
412 }
413}
414
Alex Crichton62a0a592017-05-22 13:58:53 -0700415ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800416 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800417 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800418 /// *This type is available if Syn is built with the `"full"` feature.*
419 ///
David Tolnay614a0142018-01-07 10:25:43 -0800420 /// # Syntax tree enum
421 ///
422 /// This type is a [syntax tree enum].
423 ///
424 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800425 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800426 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800427 ///
428 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800430 pub attrs: Vec<Attribute>,
431 pub vis: Visibility,
432 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 pub decl: Box<FnDecl>,
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 static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700440 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800441 pub attrs: Vec<Attribute>,
442 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500444 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800445 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800446 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800447 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800448 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700449 }),
David Tolnayebb72722018-01-07 01:14:13 -0800450
451 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800452 ///
453 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800454 pub Type(ForeignItemType {
455 pub attrs: Vec<Attribute>,
456 pub vis: Visibility,
457 pub type_token: Token![type],
458 pub ident: Ident,
459 pub semi_token: Token![;],
460 }),
David Tolnayebb72722018-01-07 01:14:13 -0800461
David Tolnay435c1782018-08-24 16:15:44 -0400462 /// A macro invocation within an extern block.
463 ///
464 /// *This type is available if Syn is built with the `"full"` feature.*
465 pub Macro(ForeignItemMacro {
466 pub attrs: Vec<Attribute>,
467 pub mac: Macro,
468 pub semi_token: Option<Token![;]>,
469 }),
470
David Tolnayebb72722018-01-07 01:14:13 -0800471 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800472 ///
473 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500474 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
475 pub tts: TokenStream,
476 }),
477 }
478}
479
480#[cfg(feature = "extra-traits")]
481impl Eq for ForeignItemVerbatim {}
482
483#[cfg(feature = "extra-traits")]
484impl PartialEq for ForeignItemVerbatim {
485 fn eq(&self, other: &Self) -> bool {
486 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
487 }
488}
489
490#[cfg(feature = "extra-traits")]
491impl Hash for ForeignItemVerbatim {
492 fn hash<H>(&self, state: &mut H)
493 where
494 H: Hasher,
495 {
496 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700498}
499
David Tolnayda705bd2017-11-10 21:58:05 -0800500ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800501 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800502 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800503 /// *This type is available if Syn is built with the `"full"` feature.*
504 ///
David Tolnay614a0142018-01-07 10:25:43 -0800505 /// # Syntax tree enum
506 ///
507 /// This type is a [syntax tree enum].
508 ///
509 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800510 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800511 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800512 ///
513 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800515 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700517 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800518 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800519 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800520 pub default: Option<(Token![=], Expr)>,
521 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700522 }),
David Tolnayebb72722018-01-07 01:14:13 -0800523
524 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800525 ///
526 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700527 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800528 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 pub sig: MethodSig,
530 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
David Tolnayebb72722018-01-07 01:14:13 -0800533
534 /// An associated type 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.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800538 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800539 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700540 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500541 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500543 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800544 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800545 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 }),
David Tolnayebb72722018-01-07 01:14:13 -0800547
548 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800549 ///
550 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800551 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800552 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800553 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500554 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800555 }),
David Tolnayebb72722018-01-07 01:14:13 -0800556
557 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800558 ///
559 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500560 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
561 pub tts: TokenStream,
562 }),
563 }
564}
565
566#[cfg(feature = "extra-traits")]
567impl Eq for TraitItemVerbatim {}
568
569#[cfg(feature = "extra-traits")]
570impl PartialEq for TraitItemVerbatim {
571 fn eq(&self, other: &Self) -> bool {
572 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
573 }
574}
575
576#[cfg(feature = "extra-traits")]
577impl Hash for TraitItemVerbatim {
578 fn hash<H>(&self, state: &mut H)
579 where
580 H: Hasher,
581 {
582 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700584}
585
Alex Crichton62a0a592017-05-22 13:58:53 -0700586ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800587 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800588 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800589 /// *This type is available if Syn is built with the `"full"` feature.*
590 ///
David Tolnay614a0142018-01-07 10:25:43 -0800591 /// # Syntax tree enum
592 ///
593 /// This type is a [syntax tree enum].
594 ///
595 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800596 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800597 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800598 ///
599 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700600 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800601 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700602 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500603 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800604 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700605 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800607 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800608 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800610 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700611 }),
David Tolnayebb72722018-01-07 01:14:13 -0800612
613 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800614 ///
615 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700616 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800617 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700618 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500619 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700620 pub sig: MethodSig,
621 pub block: Block,
622 }),
David Tolnayebb72722018-01-07 01:14:13 -0800623
624 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800625 ///
626 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700627 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800628 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700629 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500630 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800631 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700632 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500633 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800634 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800635 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800636 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700637 }),
David Tolnayebb72722018-01-07 01:14:13 -0800638
David Tolnaybb82ef02018-08-24 20:15:45 -0400639 /// An existential type within an impl block.
640 ///
641 /// *This type is available if Syn is built with the `"full"` feature.*
642 pub Existential(ImplItemExistential {
643 pub attrs: Vec<Attribute>,
644 pub existential_token: Token![existential],
645 pub type_token: Token![type],
646 pub ident: Ident,
647 pub generics: Generics,
648 pub colon_token: Option<Token![:]>,
649 pub bounds: Punctuated<TypeParamBound, Token![+]>,
650 pub semi_token: Token![;],
651 }),
652
David Tolnayebb72722018-01-07 01:14:13 -0800653 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800654 ///
655 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800656 pub Macro(ImplItemMacro {
657 pub attrs: Vec<Attribute>,
658 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500659 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800660 }),
David Tolnayebb72722018-01-07 01:14:13 -0800661
662 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800663 ///
664 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500665 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
666 pub tts: TokenStream,
667 }),
668 }
669}
670
671#[cfg(feature = "extra-traits")]
672impl Eq for ImplItemVerbatim {}
673
674#[cfg(feature = "extra-traits")]
675impl PartialEq for ImplItemVerbatim {
676 fn eq(&self, other: &Self) -> bool {
677 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
678 }
679}
680
681#[cfg(feature = "extra-traits")]
682impl Hash for ImplItemVerbatim {
683 fn hash<H>(&self, state: &mut H)
684 where
685 H: Hasher,
686 {
687 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700689}
690
691ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800692 /// A method's signature in a trait or implementation: `unsafe fn
693 /// initialize(&self)`.
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 MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500697 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500698 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900699 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700701 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
704}
705
706ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800707 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800708 ///
709 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800711 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500712 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500713 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500714 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500715 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500716 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700717 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700718}
719
Alex Crichton62a0a592017-05-22 13:58:53 -0700720ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800721 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800722 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800723 /// *This type is available if Syn is built with the `"full"` feature.*
724 ///
David Tolnay614a0142018-01-07 10:25:43 -0800725 /// # Syntax tree enum
726 ///
727 /// This type is a [syntax tree enum].
728 ///
729 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700730 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800731 /// Self captured by reference in a function signature: `&self` or `&mut
732 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800733 ///
734 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800736 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500738 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500739 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800741
David Tolnay3f559052018-01-06 23:59:48 -0800742 /// Self captured by value in a function signature: `self` or `mut
743 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800744 ///
745 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500747 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800748 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800750
David Tolnay3f559052018-01-06 23:59:48 -0800751 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800752 ///
753 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700754 pub Captured(ArgCaptured {
755 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800756 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800757 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800759
David Tolnay3f559052018-01-06 23:59:48 -0800760 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500761 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800762 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800763 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700764 }
David Tolnay62f374c2016-10-02 13:37:00 -0700765}
766
David Tolnayedf2b992016-09-23 20:43:45 -0700767#[cfg(feature = "parsing")]
768pub mod parsing {
769 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700770
David Tolnay94d304f2018-08-30 23:43:53 -0700771 use ext::IdentExt;
David Tolnay2ad62c12018-08-26 19:00:35 -0400772 use parse::{Parse, ParseStream, Result};
David Tolnay84aa0752016-10-02 23:01:13 -0700773
David Tolnay6a170ce2018-08-26 22:29:24 -0700774 impl Parse for Item {
775 fn parse(input: ParseStream) -> Result<Self> {
776 let ahead = input.fork();
777 ahead.call(Attribute::parse_outer)?;
778 let vis: Visibility = ahead.parse()?;
779
780 let lookahead = ahead.lookahead1();
781 if lookahead.peek(Token![extern]) {
782 ahead.parse::<Token![extern]>()?;
783 let lookahead = ahead.lookahead1();
784 if lookahead.peek(Token![crate]) {
785 input.parse().map(Item::ExternCrate)
786 } else if lookahead.peek(Token![fn]) {
787 input.parse().map(Item::Fn)
788 } else if lookahead.peek(token::Brace) {
789 input.parse().map(Item::ForeignMod)
790 } else if lookahead.peek(LitStr) {
791 ahead.parse::<LitStr>()?;
792 let lookahead = ahead.lookahead1();
793 if lookahead.peek(token::Brace) {
794 input.parse().map(Item::ForeignMod)
795 } else if lookahead.peek(Token![fn]) {
796 input.parse().map(Item::Fn)
797 } else {
798 Err(lookahead.error())
799 }
800 } else {
801 Err(lookahead.error())
802 }
803 } else if lookahead.peek(Token![use]) {
804 input.parse().map(Item::Use)
805 } else if lookahead.peek(Token![static]) {
806 input.parse().map(Item::Static)
807 } else if lookahead.peek(Token![const]) {
808 ahead.parse::<Token![const]>()?;
809 let lookahead = ahead.lookahead1();
810 if lookahead.peek(Ident) {
811 input.parse().map(Item::Const)
812 } else if lookahead.peek(Token![unsafe])
813 || lookahead.peek(Token![async])
814 || lookahead.peek(Token![extern])
815 || lookahead.peek(Token![fn])
816 {
817 input.parse().map(Item::Fn)
818 } else {
819 Err(lookahead.error())
820 }
821 } else if lookahead.peek(Token![unsafe]) {
822 ahead.parse::<Token![unsafe]>()?;
823 let lookahead = ahead.lookahead1();
824 if lookahead.peek(Token![trait])
825 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
826 {
827 input.parse().map(Item::Trait)
828 } else if lookahead.peek(Token![impl ]) {
829 input.parse().map(Item::Impl)
830 } else if lookahead.peek(Token![async])
831 || lookahead.peek(Token![extern])
832 || lookahead.peek(Token![fn])
833 {
834 input.parse().map(Item::Fn)
835 } else {
836 Err(lookahead.error())
837 }
838 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
839 input.parse().map(Item::Fn)
840 } else if lookahead.peek(Token![mod]) {
841 input.parse().map(Item::Mod)
842 } else if lookahead.peek(Token![type]) {
843 input.parse().map(Item::Type)
844 } else if lookahead.peek(Token![existential]) {
845 input.parse().map(Item::Existential)
846 } else if lookahead.peek(Token![struct]) {
847 input.parse().map(Item::Struct)
848 } else if lookahead.peek(Token![enum]) {
849 input.parse().map(Item::Enum)
850 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
851 input.parse().map(Item::Union)
852 } else if lookahead.peek(Token![trait])
853 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
854 {
855 input.parse().map(Item::Trait)
856 } else if lookahead.peek(Token![impl ])
857 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
858 {
859 input.parse().map(Item::Impl)
860 } else if lookahead.peek(Token![macro]) {
861 input.parse().map(Item::Macro2)
862 } else if vis.is_inherited()
863 && (lookahead.peek(Ident)
864 || lookahead.peek(Token![self])
865 || lookahead.peek(Token![super])
866 || lookahead.peek(Token![extern])
867 || lookahead.peek(Token![crate])
868 || lookahead.peek(Token![::]))
869 {
870 input.parse().map(Item::Macro)
871 } else {
872 Err(lookahead.error())
873 }
874 }
875 }
Alex Crichton954046c2017-05-30 21:49:42 -0700876
David Tolnay3779bb72018-08-26 18:46:07 -0700877 impl Parse for ItemMacro {
878 fn parse(input: ParseStream) -> Result<Self> {
879 let attrs = input.call(Attribute::parse_outer)?;
880 let path = input.call(Path::parse_mod_style)?;
881 let bang_token: Token![!] = input.parse()?;
882 let ident: Option<Ident> = input.parse()?;
883 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
David Tolnay6a170ce2018-08-26 22:29:24 -0700884 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -0700885 Some(input.parse()?)
886 } else {
887 None
888 };
889 Ok(ItemMacro {
890 attrs: attrs,
891 ident: ident,
892 mac: Macro {
893 path: path,
894 bang_token: bang_token,
895 delimiter: delimiter,
896 tts: tts,
897 },
898 semi_token: semi_token,
899 })
900 }
901 }
David Tolnayedf2b992016-09-23 20:43:45 -0700902
David Tolnay500d8322017-12-18 00:32:51 -0800903 // TODO: figure out the actual grammar; is body required to be braced?
David Tolnay3779bb72018-08-26 18:46:07 -0700904 impl Parse for ItemMacro2 {
905 fn parse(input: ParseStream) -> Result<Self> {
906 let args;
907 let body;
908 Ok(ItemMacro2 {
909 attrs: input.call(Attribute::parse_outer)?,
910 vis: input.parse()?,
911 macro_token: input.parse()?,
912 ident: input.parse()?,
913 paren_token: parenthesized!(args in input),
914 args: args.parse()?,
915 brace_token: braced!(body in input),
916 body: body.parse()?,
917 })
918 }
919 }
David Tolnay500d8322017-12-18 00:32:51 -0800920
David Tolnay3779bb72018-08-26 18:46:07 -0700921 impl Parse for ItemExternCrate {
922 fn parse(input: ParseStream) -> Result<Self> {
923 Ok(ItemExternCrate {
924 attrs: input.call(Attribute::parse_outer)?,
925 vis: input.parse()?,
926 extern_token: input.parse()?,
927 crate_token: input.parse()?,
928 ident: input.parse()?,
929 rename: {
930 if input.peek(Token![as]) {
931 let as_token: Token![as] = input.parse()?;
932 let rename: Ident = input.parse()?;
933 Some((as_token, rename))
934 } else {
935 None
936 }
David Tolnayc6b55bc2017-11-09 22:48:38 -0800937 },
David Tolnay3779bb72018-08-26 18:46:07 -0700938 semi_token: input.parse()?,
939 })
940 }
941 }
942
943 impl Parse for ItemUse {
944 fn parse(input: ParseStream) -> Result<Self> {
945 Ok(ItemUse {
946 attrs: input.call(Attribute::parse_outer)?,
947 vis: input.parse()?,
948 use_token: input.parse()?,
949 leading_colon: input.parse()?,
950 tree: input.call(use_tree)?,
951 semi_token: input.parse()?,
952 })
953 }
954 }
955
956 fn use_tree(input: ParseStream) -> Result<UseTree> {
957 let lookahead = input.lookahead1();
958 if lookahead.peek(Ident)
959 || lookahead.peek(Token![self])
960 || lookahead.peek(Token![super])
961 || lookahead.peek(Token![crate])
962 || lookahead.peek(Token![extern])
963 {
David Tolnay0dea1b92018-08-30 17:47:29 -0700964 let ident = input.call(Ident::parse_any)?;
David Tolnay3779bb72018-08-26 18:46:07 -0700965 if input.peek(Token![::]) {
966 Ok(UseTree::Path(UsePath {
967 ident: ident,
968 colon2_token: input.parse()?,
969 tree: Box::new(input.call(use_tree)?),
970 }))
971 } else if input.peek(Token![as]) {
972 Ok(UseTree::Rename(UseRename {
973 ident: ident,
974 as_token: input.parse()?,
975 rename: input.parse()?,
976 }))
977 } else {
978 Ok(UseTree::Name(UseName { ident: ident }))
979 }
980 } else if lookahead.peek(Token![*]) {
981 Ok(UseTree::Glob(UseGlob {
982 star_token: input.parse()?,
983 }))
984 } else if lookahead.peek(token::Brace) {
985 let content;
986 Ok(UseTree::Group(UseGroup {
987 brace_token: braced!(content in input),
988 items: content.parse_terminated(use_tree)?,
989 }))
990 } else {
991 Err(lookahead.error())
992 }
993 }
994
995 impl Parse for ItemStatic {
996 fn parse(input: ParseStream) -> Result<Self> {
997 Ok(ItemStatic {
998 attrs: input.call(Attribute::parse_outer)?,
999 vis: input.parse()?,
1000 static_token: input.parse()?,
1001 mutability: input.parse()?,
1002 ident: input.parse()?,
1003 colon_token: input.parse()?,
1004 ty: input.parse()?,
1005 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001006 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001007 semi_token: input.parse()?,
1008 })
1009 }
1010 }
1011
1012 impl Parse for ItemConst {
1013 fn parse(input: ParseStream) -> Result<Self> {
1014 Ok(ItemConst {
1015 attrs: input.call(Attribute::parse_outer)?,
1016 vis: input.parse()?,
1017 const_token: input.parse()?,
1018 ident: input.parse()?,
1019 colon_token: input.parse()?,
1020 ty: input.parse()?,
1021 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001022 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001023 semi_token: input.parse()?,
1024 })
1025 }
1026 }
1027
1028 impl Parse for ItemFn {
1029 fn parse(input: ParseStream) -> Result<Self> {
1030 let outer_attrs = input.call(Attribute::parse_outer)?;
1031 let vis: Visibility = input.parse()?;
1032 let constness: Option<Token![const]> = input.parse()?;
1033 let unsafety: Option<Token![unsafe]> = input.parse()?;
1034 let asyncness: Option<Token![async]> = input.parse()?;
1035 let abi: Option<Abi> = input.parse()?;
1036 let fn_token: Token![fn] = input.parse()?;
1037 let ident: Ident = input.parse()?;
1038 let generics: Generics = input.parse()?;
1039
1040 let content;
1041 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001042 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001043
1044 let output: ReturnType = input.parse()?;
1045 let where_clause: Option<WhereClause> = input.parse()?;
1046
1047 let content;
1048 let brace_token = braced!(content in input);
1049 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001050 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001051
1052 Ok(ItemFn {
1053 attrs: {
1054 let mut attrs = outer_attrs;
1055 attrs.extend(inner_attrs);
1056 attrs
1057 },
1058 vis: vis,
1059 constness: constness,
1060 unsafety: unsafety,
1061 asyncness: asyncness,
1062 abi: abi,
1063 ident: ident,
1064 decl: Box::new(FnDecl {
1065 fn_token: fn_token,
1066 paren_token: paren_token,
1067 inputs: inputs,
1068 output: output,
1069 variadic: None,
1070 generics: Generics {
1071 where_clause: where_clause,
1072 ..generics
1073 },
1074 }),
1075 block: Box::new(Block {
1076 brace_token: brace_token,
1077 stmts: stmts,
1078 }),
1079 })
1080 }
1081 }
David Tolnay42602292016-10-01 22:25:45 -07001082
David Tolnay2ad62c12018-08-26 19:00:35 -04001083 impl Parse for FnArg {
1084 fn parse(input: ParseStream) -> Result<Self> {
1085 if input.peek(Token![&]) {
1086 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001087 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1088 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001089 }
1090 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001091
David Tolnay2ad62c12018-08-26 19:00:35 -04001092 if input.peek(Token![mut]) || input.peek(Token![self]) {
1093 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001094 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1095 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001096 }
1097 }
1098
1099 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001100 let err = match ahead.call(arg_captured) {
1101 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001102 Err(err) => err,
1103 };
1104
1105 let ahead = input.fork();
1106 if ahead.parse::<Type>().is_ok() {
1107 return input.parse().map(FnArg::Ignored);
1108 }
1109
1110 Err(err)
1111 }
1112 }
1113
David Tolnay3779bb72018-08-26 18:46:07 -07001114 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1115 Ok(ArgSelfRef {
1116 and_token: input.parse()?,
1117 lifetime: input.parse()?,
1118 mutability: input.parse()?,
1119 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001120 })
David Tolnay3779bb72018-08-26 18:46:07 -07001121 }
David Tolnay35902302016-10-06 01:11:08 -07001122
David Tolnay3779bb72018-08-26 18:46:07 -07001123 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1124 Ok(ArgSelf {
1125 mutability: input.parse()?,
1126 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001127 })
David Tolnay3779bb72018-08-26 18:46:07 -07001128 }
1129
1130 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1131 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001132 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001133 colon_token: input.parse()?,
1134 ty: input.parse()?,
1135 })
1136 }
1137
1138 impl Parse for ItemMod {
1139 fn parse(input: ParseStream) -> Result<Self> {
1140 let outer_attrs = input.call(Attribute::parse_outer)?;
1141 let vis: Visibility = input.parse()?;
1142 let mod_token: Token![mod] = input.parse()?;
1143 let ident: Ident = input.parse()?;
1144
1145 let lookahead = input.lookahead1();
1146 if lookahead.peek(Token![;]) {
1147 Ok(ItemMod {
1148 attrs: outer_attrs,
1149 vis: vis,
1150 mod_token: mod_token,
1151 ident: ident,
1152 content: None,
1153 semi: Some(input.parse()?),
1154 })
1155 } else if lookahead.peek(token::Brace) {
1156 let content;
1157 let brace_token = braced!(content in input);
1158 let inner_attrs = content.call(Attribute::parse_inner)?;
1159
1160 let mut items = Vec::new();
1161 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001162 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001163 }
1164
1165 Ok(ItemMod {
1166 attrs: {
1167 let mut attrs = outer_attrs;
1168 attrs.extend(inner_attrs);
1169 attrs
1170 },
1171 vis: vis,
1172 mod_token: mod_token,
1173 ident: ident,
1174 content: Some((brace_token, items)),
1175 semi: None,
1176 })
1177 } else {
1178 Err(lookahead.error())
1179 }
1180 }
1181 }
1182
1183 impl Parse for ItemForeignMod {
1184 fn parse(input: ParseStream) -> Result<Self> {
1185 let outer_attrs = input.call(Attribute::parse_outer)?;
1186 let abi: Abi = input.parse()?;
1187
1188 let content;
1189 let brace_token = braced!(content in input);
1190 let inner_attrs = content.call(Attribute::parse_inner)?;
1191 let mut items = Vec::new();
1192 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001193 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001194 }
1195
1196 Ok(ItemForeignMod {
1197 attrs: {
1198 let mut attrs = outer_attrs;
1199 attrs.extend(inner_attrs);
1200 attrs
1201 },
1202 abi: abi,
1203 brace_token: brace_token,
1204 items: items,
1205 })
1206 }
1207 }
David Tolnay35902302016-10-06 01:11:08 -07001208
David Tolnay6a170ce2018-08-26 22:29:24 -07001209 impl Parse for ForeignItem {
1210 fn parse(input: ParseStream) -> Result<Self> {
1211 let ahead = input.fork();
1212 ahead.call(Attribute::parse_outer)?;
1213 let vis: Visibility = ahead.parse()?;
1214
1215 let lookahead = ahead.lookahead1();
1216 if lookahead.peek(Token![fn]) {
1217 input.parse().map(ForeignItem::Fn)
1218 } else if lookahead.peek(Token![static]) {
1219 input.parse().map(ForeignItem::Static)
1220 } else if lookahead.peek(Token![type]) {
1221 input.parse().map(ForeignItem::Type)
1222 } else if vis.is_inherited()
1223 && (lookahead.peek(Ident)
1224 || lookahead.peek(Token![self])
1225 || lookahead.peek(Token![super])
1226 || lookahead.peek(Token![extern])
1227 || lookahead.peek(Token![crate])
1228 || lookahead.peek(Token![::]))
1229 {
1230 input.parse().map(ForeignItem::Macro)
1231 } else {
1232 Err(lookahead.error())
1233 }
1234 }
1235 }
David Tolnay35902302016-10-06 01:11:08 -07001236
David Tolnay3779bb72018-08-26 18:46:07 -07001237 impl Parse for ForeignItemFn {
1238 fn parse(input: ParseStream) -> Result<Self> {
1239 let attrs = input.call(Attribute::parse_outer)?;
1240 let vis: Visibility = input.parse()?;
1241 let fn_token: Token![fn] = input.parse()?;
1242 let ident: Ident = input.parse()?;
1243 let generics: Generics = input.parse()?;
1244
1245 let content;
1246 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001247 let mut inputs = Punctuated::new();
1248 while !content.is_empty() && !content.peek(Token![...]) {
1249 inputs.push_value(content.parse()?);
1250 if content.is_empty() {
1251 break;
1252 }
1253 inputs.push_punct(content.parse()?);
1254 }
David Tolnay3779bb72018-08-26 18:46:07 -07001255 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1256 content.parse()?
1257 } else {
1258 None
1259 };
1260
1261 let output: ReturnType = input.parse()?;
1262 let where_clause: Option<WhereClause> = input.parse()?;
1263 let semi_token: Token![;] = input.parse()?;
1264
1265 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001266 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001267 vis: vis,
1268 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001269 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001270 fn_token: fn_token,
1271 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001272 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001273 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001274 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001275 generics: Generics {
1276 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001277 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001278 },
1279 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001280 semi_token: semi_token,
1281 })
1282 }
1283 }
David Tolnay35902302016-10-06 01:11:08 -07001284
David Tolnay3779bb72018-08-26 18:46:07 -07001285 impl Parse for ForeignItemStatic {
1286 fn parse(input: ParseStream) -> Result<Self> {
1287 Ok(ForeignItemStatic {
1288 attrs: input.call(Attribute::parse_outer)?,
1289 vis: input.parse()?,
1290 static_token: input.parse()?,
1291 mutability: input.parse()?,
1292 ident: input.parse()?,
1293 colon_token: input.parse()?,
1294 ty: input.parse()?,
1295 semi_token: input.parse()?,
1296 })
1297 }
1298 }
David Tolnay35902302016-10-06 01:11:08 -07001299
David Tolnay3779bb72018-08-26 18:46:07 -07001300 impl Parse for ForeignItemType {
1301 fn parse(input: ParseStream) -> Result<Self> {
1302 Ok(ForeignItemType {
1303 attrs: input.call(Attribute::parse_outer)?,
1304 vis: input.parse()?,
1305 type_token: input.parse()?,
1306 ident: input.parse()?,
1307 semi_token: input.parse()?,
1308 })
1309 }
1310 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001311
David Tolnay3779bb72018-08-26 18:46:07 -07001312 impl Parse for ForeignItemMacro {
1313 fn parse(input: ParseStream) -> Result<Self> {
1314 let attrs = input.call(Attribute::parse_outer)?;
1315 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001316 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001317 None
1318 } else {
1319 Some(input.parse()?)
1320 };
1321 Ok(ForeignItemMacro {
1322 attrs: attrs,
1323 mac: mac,
1324 semi_token: semi_token,
1325 })
1326 }
1327 }
David Tolnay78572112018-08-01 00:36:18 -07001328
David Tolnay3779bb72018-08-26 18:46:07 -07001329 impl Parse for ItemType {
1330 fn parse(input: ParseStream) -> Result<Self> {
1331 Ok(ItemType {
1332 attrs: input.call(Attribute::parse_outer)?,
1333 vis: input.parse()?,
1334 type_token: input.parse()?,
1335 ident: input.parse()?,
1336 generics: {
1337 let mut generics: Generics = input.parse()?;
1338 generics.where_clause = input.parse()?;
1339 generics
1340 },
1341 eq_token: input.parse()?,
1342 ty: input.parse()?,
1343 semi_token: input.parse()?,
1344 })
1345 }
1346 }
David Tolnay3cf52982016-10-01 17:11:37 -07001347
David Tolnay3779bb72018-08-26 18:46:07 -07001348 impl Parse for ItemExistential {
1349 fn parse(input: ParseStream) -> Result<Self> {
1350 Ok(ItemExistential {
1351 attrs: input.call(Attribute::parse_outer)?,
1352 vis: input.parse()?,
1353 existential_token: input.parse()?,
1354 type_token: input.parse()?,
1355 ident: input.parse()?,
1356 generics: {
1357 let mut generics: Generics = input.parse()?;
1358 generics.where_clause = input.parse()?;
1359 generics
1360 },
1361 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001362 bounds: {
1363 let mut bounds = Punctuated::new();
1364 while !input.peek(Token![;]) {
1365 if !bounds.is_empty() {
1366 bounds.push_punct(input.parse()?);
1367 }
1368 bounds.push_value(input.parse()?);
1369 }
1370 bounds
1371 },
David Tolnay3779bb72018-08-26 18:46:07 -07001372 semi_token: input.parse()?,
1373 })
1374 }
1375 }
David Tolnay758ee132018-08-21 21:29:40 -04001376
David Tolnay6a170ce2018-08-26 22:29:24 -07001377 impl Parse for ItemStruct {
1378 fn parse(input: ParseStream) -> Result<Self> {
1379 let attrs = input.call(Attribute::parse_outer)?;
1380 let vis = input.parse::<Visibility>()?;
1381 let struct_token = input.parse::<Token![struct]>()?;
1382 let ident = input.parse::<Ident>()?;
1383 let generics = input.parse::<Generics>()?;
1384 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1385 Ok(ItemStruct {
1386 attrs: attrs,
1387 vis: vis,
1388 struct_token: struct_token,
1389 ident: ident,
1390 generics: Generics {
1391 where_clause: where_clause,
1392 ..generics
1393 },
1394 fields: fields,
1395 semi_token: semi_token,
1396 })
1397 }
1398 }
David Tolnay42602292016-10-01 22:25:45 -07001399
David Tolnay6a170ce2018-08-26 22:29:24 -07001400 impl Parse for ItemEnum {
1401 fn parse(input: ParseStream) -> Result<Self> {
1402 let attrs = input.call(Attribute::parse_outer)?;
1403 let vis = input.parse::<Visibility>()?;
1404 let enum_token = input.parse::<Token![enum]>()?;
1405 let ident = input.parse::<Ident>()?;
1406 let generics = input.parse::<Generics>()?;
1407 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1408 Ok(ItemEnum {
1409 attrs: attrs,
1410 vis: vis,
1411 enum_token: enum_token,
1412 ident: ident,
1413 generics: Generics {
1414 where_clause: where_clause,
1415 ..generics
1416 },
1417 brace_token: brace_token,
1418 variants: variants,
1419 })
1420 }
1421 }
David Tolnay4c614be2017-11-10 00:02:38 -08001422
David Tolnay6a170ce2018-08-26 22:29:24 -07001423 impl Parse for ItemUnion {
1424 fn parse(input: ParseStream) -> Result<Self> {
1425 let attrs = input.call(Attribute::parse_outer)?;
1426 let vis = input.parse::<Visibility>()?;
1427 let union_token = input.parse::<Token![union]>()?;
1428 let ident = input.parse::<Ident>()?;
1429 let generics = input.parse::<Generics>()?;
1430 let (where_clause, fields) = derive::parsing::data_union(input)?;
1431 Ok(ItemUnion {
1432 attrs: attrs,
1433 vis: vis,
1434 union_token: union_token,
1435 ident: ident,
1436 generics: Generics {
1437 where_clause: where_clause,
1438 ..generics
1439 },
1440 fields: fields,
1441 })
1442 }
1443 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001444
David Tolnay6a170ce2018-08-26 22:29:24 -07001445 impl Parse for ItemTrait {
1446 fn parse(input: ParseStream) -> Result<Self> {
1447 let attrs = input.call(Attribute::parse_outer)?;
1448 let vis: Visibility = input.parse()?;
1449 let unsafety: Option<Token![unsafe]> = input.parse()?;
1450 let auto_token: Option<Token![auto]> = input.parse()?;
1451 let trait_token: Token![trait] = input.parse()?;
1452 let ident: Ident = input.parse()?;
1453 let mut generics: Generics = input.parse()?;
1454 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001455
1456 let mut supertraits = Punctuated::new();
1457 if colon_token.is_some() {
David Tolnaya22a5742018-08-30 22:52:47 -07001458 loop {
David Tolnayf5ebc192018-08-30 18:23:46 -07001459 supertraits.push_value(input.parse()?);
David Tolnaya22a5742018-08-30 22:52:47 -07001460 if input.peek(Token![where]) || input.peek(token::Brace) {
1461 break;
1462 }
1463 supertraits.push_punct(input.parse()?);
1464 if input.peek(Token![where]) || input.peek(token::Brace) {
1465 break;
1466 }
David Tolnayf5ebc192018-08-30 18:23:46 -07001467 }
1468 }
1469
David Tolnay6a170ce2018-08-26 22:29:24 -07001470 generics.where_clause = input.parse()?;
1471
1472 let content;
1473 let brace_token = braced!(content in input);
1474 let mut items = Vec::new();
1475 while !content.is_empty() {
1476 items.push(content.parse()?);
1477 }
1478
1479 Ok(ItemTrait {
1480 attrs: attrs,
1481 vis: vis,
1482 unsafety: unsafety,
1483 auto_token: auto_token,
1484 trait_token: trait_token,
1485 ident: ident,
1486 generics: generics,
1487 colon_token: colon_token,
1488 supertraits: supertraits,
1489 brace_token: brace_token,
1490 items: items,
1491 })
1492 }
1493 }
1494
David Tolnayc6b04dd2018-08-30 23:22:51 -07001495 impl Parse for ItemTraitAlias {
1496 fn parse(input: ParseStream) -> Result<Self> {
1497 let attrs = input.call(Attribute::parse_outer)?;
1498 let vis: Visibility = input.parse()?;
1499 let trait_token: Token![trait] = input.parse()?;
1500 let ident: Ident = input.parse()?;
1501 let mut generics: Generics = input.parse()?;
1502 let eq_token: Token![=] = input.parse()?;
1503
1504 let mut bounds = Punctuated::new();
1505 loop {
1506 if input.peek(Token![where]) || input.peek(Token![;]) {
1507 break;
1508 }
1509 bounds.push_value(input.parse()?);
1510 if input.peek(Token![where]) || input.peek(Token![;]) {
1511 break;
1512 }
1513 bounds.push_punct(input.parse()?);
1514 }
1515
1516 generics.where_clause = input.parse()?;
1517 let semi_token: Token![;] = input.parse()?;
1518
1519 Ok(ItemTraitAlias {
1520 attrs: attrs,
1521 vis: vis,
1522 trait_token: trait_token,
1523 ident: ident,
1524 generics: generics,
1525 eq_token: eq_token,
1526 bounds: bounds,
1527 semi_token: semi_token,
1528 })
1529 }
1530 }
1531
David Tolnay6a170ce2018-08-26 22:29:24 -07001532 impl Parse for TraitItem {
1533 fn parse(input: ParseStream) -> Result<Self> {
1534 let ahead = input.fork();
1535 ahead.call(Attribute::parse_outer)?;
1536
1537 let lookahead = ahead.lookahead1();
1538 if lookahead.peek(Token![const]) {
1539 ahead.parse::<Token![const]>()?;
1540 let lookahead = ahead.lookahead1();
1541 if lookahead.peek(Ident) {
1542 input.parse().map(TraitItem::Const)
1543 } else if lookahead.peek(Token![unsafe])
1544 || lookahead.peek(Token![extern])
1545 || lookahead.peek(Token![fn])
1546 {
1547 input.parse().map(TraitItem::Method)
1548 } else {
1549 Err(lookahead.error())
1550 }
1551 } else if lookahead.peek(Token![unsafe])
1552 || lookahead.peek(Token![extern])
1553 || lookahead.peek(Token![fn])
1554 {
1555 input.parse().map(TraitItem::Method)
1556 } else if lookahead.peek(Token![type]) {
1557 input.parse().map(TraitItem::Type)
1558 } else if lookahead.peek(Ident)
1559 || lookahead.peek(Token![self])
1560 || lookahead.peek(Token![super])
1561 || lookahead.peek(Token![extern])
1562 || lookahead.peek(Token![crate])
1563 || lookahead.peek(Token![::])
1564 {
1565 input.parse().map(TraitItem::Macro)
1566 } else {
1567 Err(lookahead.error())
1568 }
1569 }
1570 }
1571
1572 impl Parse for TraitItemConst {
1573 fn parse(input: ParseStream) -> Result<Self> {
1574 Ok(TraitItemConst {
1575 attrs: input.call(Attribute::parse_outer)?,
1576 const_token: input.parse()?,
1577 ident: input.parse()?,
1578 colon_token: input.parse()?,
1579 ty: input.parse()?,
1580 default: {
1581 if input.peek(Token![=]) {
1582 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001583 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001584 Some((eq_token, default))
1585 } else {
1586 None
1587 }
1588 },
1589 semi_token: input.parse()?,
1590 })
1591 }
1592 }
1593
1594 impl Parse for TraitItemMethod {
1595 fn parse(input: ParseStream) -> Result<Self> {
1596 let outer_attrs = input.call(Attribute::parse_outer)?;
1597 let constness: Option<Token![const]> = input.parse()?;
1598 let unsafety: Option<Token![unsafe]> = input.parse()?;
1599 let abi: Option<Abi> = input.parse()?;
1600 let fn_token: Token![fn] = input.parse()?;
1601 let ident: Ident = input.parse()?;
1602 let generics: Generics = input.parse()?;
1603
1604 let content;
1605 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001606 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001607
1608 let output: ReturnType = input.parse()?;
1609 let where_clause: Option<WhereClause> = input.parse()?;
1610
1611 let lookahead = input.lookahead1();
1612 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1613 let content;
1614 let brace_token = braced!(content in input);
1615 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001616 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001617 (Some(brace_token), inner_attrs, stmts, None)
1618 } else if lookahead.peek(Token![;]) {
1619 let semi_token: Token![;] = input.parse()?;
1620 (None, Vec::new(), Vec::new(), Some(semi_token))
1621 } else {
1622 return Err(lookahead.error());
1623 };
1624
1625 Ok(TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001626 attrs: {
1627 let mut attrs = outer_attrs;
1628 attrs.extend(inner_attrs);
1629 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001630 },
David Tolnayda705bd2017-11-10 21:58:05 -08001631 sig: MethodSig {
1632 constness: constness,
1633 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001634 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001635 abi: abi,
1636 ident: ident,
1637 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001638 fn_token: fn_token,
1639 paren_token: paren_token,
1640 inputs: inputs,
1641 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001642 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001643 generics: Generics {
1644 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001645 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001646 },
1647 },
David Tolnayda705bd2017-11-10 21:58:05 -08001648 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001649 default: brace_token.map(|brace_token| Block {
1650 brace_token: brace_token,
1651 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001652 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001653 semi_token: semi_token,
1654 })
1655 }
1656 }
1657
1658 impl Parse for TraitItemType {
1659 fn parse(input: ParseStream) -> Result<Self> {
1660 let attrs = input.call(Attribute::parse_outer)?;
1661 let type_token: Token![type] = input.parse()?;
1662 let ident: Ident = input.parse()?;
1663 let mut generics: Generics = input.parse()?;
1664 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001665
1666 let mut bounds = Punctuated::new();
1667 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001668 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1669 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001670 if !bounds.is_empty() {
1671 bounds.push_punct(input.parse()?);
1672 }
1673 bounds.push_value(input.parse()?);
1674 }
1675 }
1676
David Tolnay6a170ce2018-08-26 22:29:24 -07001677 generics.where_clause = input.parse()?;
1678 let default = if input.peek(Token![=]) {
1679 let eq_token: Token![=] = input.parse()?;
1680 let default: Type = input.parse()?;
1681 Some((eq_token, default))
1682 } else {
1683 None
1684 };
1685 let semi_token: Token![;] = input.parse()?;
1686
1687 Ok(TraitItemType {
1688 attrs: attrs,
1689 type_token: type_token,
1690 ident: ident,
1691 generics: generics,
1692 colon_token: colon_token,
1693 bounds: bounds,
1694 default: default,
1695 semi_token: semi_token,
1696 })
1697 }
1698 }
1699
1700 impl Parse for TraitItemMacro {
1701 fn parse(input: ParseStream) -> Result<Self> {
1702 let attrs = input.call(Attribute::parse_outer)?;
1703 let mac: Macro = input.parse()?;
1704 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1705 None
1706 } else {
1707 Some(input.parse()?)
1708 };
1709 Ok(TraitItemMacro {
1710 attrs: attrs,
1711 mac: mac,
1712 semi_token: semi_token,
1713 })
1714 }
1715 }
1716
1717 impl Parse for ItemImpl {
1718 fn parse(input: ParseStream) -> Result<Self> {
1719 let outer_attrs = input.call(Attribute::parse_outer)?;
1720 let defaultness: Option<Token![default]> = input.parse()?;
1721 let unsafety: Option<Token![unsafe]> = input.parse()?;
1722 let impl_token: Token![impl ] = input.parse()?;
1723 let generics: Generics = input.parse()?;
1724 let trait_ = {
1725 let ahead = input.fork();
1726 if ahead.parse::<Option<Token![!]>>().is_ok()
1727 && ahead.parse::<Path>().is_ok()
1728 && ahead.parse::<Token![for]>().is_ok()
1729 {
1730 let polarity: Option<Token![!]> = input.parse()?;
1731 let path: Path = input.parse()?;
1732 let for_token: Token![for] = input.parse()?;
1733 Some((polarity, path, for_token))
1734 } else {
1735 None
1736 }
1737 };
1738 let self_ty: Type = input.parse()?;
1739 let where_clause: Option<WhereClause> = input.parse()?;
1740
1741 let content;
1742 let brace_token = braced!(content in input);
1743 let inner_attrs = content.call(Attribute::parse_inner)?;
1744
1745 let mut items = Vec::new();
1746 while !content.is_empty() {
1747 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001748 }
David Tolnay0aecb732016-10-03 23:03:50 -07001749
David Tolnay6a170ce2018-08-26 22:29:24 -07001750 Ok(ItemImpl {
1751 attrs: {
1752 let mut attrs = outer_attrs;
1753 attrs.extend(inner_attrs);
1754 attrs
1755 },
1756 defaultness: defaultness,
1757 unsafety: unsafety,
1758 impl_token: impl_token,
1759 generics: Generics {
1760 where_clause: where_clause,
1761 ..generics
1762 },
1763 trait_: trait_,
1764 self_ty: Box::new(self_ty),
1765 brace_token: brace_token,
1766 items: items,
1767 })
1768 }
1769 }
David Tolnay0aecb732016-10-03 23:03:50 -07001770
David Tolnay6a170ce2018-08-26 22:29:24 -07001771 impl Parse for ImplItem {
1772 fn parse(input: ParseStream) -> Result<Self> {
1773 let ahead = input.fork();
1774 ahead.call(Attribute::parse_outer)?;
1775 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001776
David Tolnay6a170ce2018-08-26 22:29:24 -07001777 let mut lookahead = ahead.lookahead1();
1778 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1779 let defaultness: Token![default] = ahead.parse()?;
1780 lookahead = ahead.lookahead1();
1781 Some(defaultness)
1782 } else {
1783 None
1784 };
David Tolnay4c9be372016-10-06 00:47:37 -07001785
David Tolnay6a170ce2018-08-26 22:29:24 -07001786 if lookahead.peek(Token![const]) {
1787 ahead.parse::<Token![const]>()?;
1788 let lookahead = ahead.lookahead1();
1789 if lookahead.peek(Ident) {
1790 input.parse().map(ImplItem::Const)
1791 } else if lookahead.peek(Token![unsafe])
1792 || lookahead.peek(Token![async])
1793 || lookahead.peek(Token![extern])
1794 || lookahead.peek(Token![fn])
1795 {
1796 input.parse().map(ImplItem::Method)
1797 } else {
1798 Err(lookahead.error())
1799 }
1800 } else if lookahead.peek(Token![unsafe])
1801 || lookahead.peek(Token![async])
1802 || lookahead.peek(Token![extern])
1803 || lookahead.peek(Token![fn])
1804 {
1805 input.parse().map(ImplItem::Method)
1806 } else if lookahead.peek(Token![type]) {
1807 input.parse().map(ImplItem::Type)
1808 } else if vis.is_inherited()
1809 && defaultness.is_none()
1810 && lookahead.peek(Token![existential])
1811 {
1812 input.parse().map(ImplItem::Existential)
1813 } else if vis.is_inherited()
1814 && defaultness.is_none()
1815 && (lookahead.peek(Ident)
1816 || lookahead.peek(Token![self])
1817 || lookahead.peek(Token![super])
1818 || lookahead.peek(Token![extern])
1819 || lookahead.peek(Token![crate])
1820 || lookahead.peek(Token![::]))
1821 {
1822 input.parse().map(ImplItem::Macro)
1823 } else {
1824 Err(lookahead.error())
1825 }
1826 }
1827 }
David Tolnay4c9be372016-10-06 00:47:37 -07001828
David Tolnay3779bb72018-08-26 18:46:07 -07001829 impl Parse for ImplItemConst {
1830 fn parse(input: ParseStream) -> Result<Self> {
1831 Ok(ImplItemConst {
1832 attrs: input.call(Attribute::parse_outer)?,
1833 vis: input.parse()?,
1834 defaultness: input.parse()?,
1835 const_token: input.parse()?,
1836 ident: input.parse()?,
1837 colon_token: input.parse()?,
1838 ty: input.parse()?,
1839 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001840 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001841 semi_token: input.parse()?,
1842 })
1843 }
1844 }
David Tolnay4c9be372016-10-06 00:47:37 -07001845
David Tolnay6a170ce2018-08-26 22:29:24 -07001846 impl Parse for ImplItemMethod {
1847 fn parse(input: ParseStream) -> Result<Self> {
1848 let outer_attrs = input.call(Attribute::parse_outer)?;
1849 let vis: Visibility = input.parse()?;
1850 let defaultness: Option<Token![default]> = input.parse()?;
1851 let constness: Option<Token![const]> = input.parse()?;
1852 let unsafety: Option<Token![unsafe]> = input.parse()?;
1853 let asyncness: Option<Token![async]> = input.parse()?;
1854 let abi: Option<Abi> = input.parse()?;
1855 let fn_token: Token![fn] = input.parse()?;
1856 let ident: Ident = input.parse()?;
1857 let generics: Generics = input.parse()?;
1858
1859 let content;
1860 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001861 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001862
1863 let output: ReturnType = input.parse()?;
1864 let where_clause: Option<WhereClause> = input.parse()?;
1865
1866 let content;
1867 let brace_token = braced!(content in input);
1868 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001869 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001870
1871 Ok(ImplItemMethod {
1872 attrs: {
1873 let mut attrs = outer_attrs;
1874 attrs.extend(inner_attrs);
1875 attrs
David Tolnay4c9be372016-10-06 00:47:37 -07001876 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001877 vis: vis,
1878 defaultness: defaultness,
1879 sig: MethodSig {
1880 constness: constness,
1881 unsafety: unsafety,
1882 asyncness: asyncness,
1883 abi: abi,
1884 ident: ident,
1885 decl: FnDecl {
1886 fn_token: fn_token,
1887 paren_token: paren_token,
1888 inputs: inputs,
1889 output: output,
1890 variadic: None,
1891 generics: Generics {
1892 where_clause: where_clause,
1893 ..generics
1894 },
1895 },
1896 },
1897 block: Block {
1898 brace_token: brace_token,
1899 stmts: stmts,
1900 },
1901 })
1902 }
1903 }
David Tolnay4c9be372016-10-06 00:47:37 -07001904
David Tolnay3779bb72018-08-26 18:46:07 -07001905 impl Parse for ImplItemType {
1906 fn parse(input: ParseStream) -> Result<Self> {
1907 Ok(ImplItemType {
1908 attrs: input.call(Attribute::parse_outer)?,
1909 vis: input.parse()?,
1910 defaultness: input.parse()?,
1911 type_token: input.parse()?,
1912 ident: input.parse()?,
1913 generics: {
1914 let mut generics: Generics = input.parse()?;
1915 generics.where_clause = input.parse()?;
1916 generics
1917 },
1918 eq_token: input.parse()?,
1919 ty: input.parse()?,
1920 semi_token: input.parse()?,
1921 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001922 }
David Tolnay3779bb72018-08-26 18:46:07 -07001923 }
David Tolnay758ee132018-08-21 21:29:40 -04001924
David Tolnay3779bb72018-08-26 18:46:07 -07001925 impl Parse for ImplItemExistential {
1926 fn parse(input: ParseStream) -> Result<Self> {
1927 let ety: ItemExistential = input.parse()?;
1928 Ok(ImplItemExistential {
1929 attrs: ety.attrs,
1930 existential_token: ety.existential_token,
1931 type_token: ety.type_token,
1932 ident: ety.ident,
1933 generics: ety.generics,
1934 colon_token: ety.colon_token,
1935 bounds: ety.bounds,
1936 semi_token: ety.semi_token,
1937 })
1938 }
1939 }
1940
1941 impl Parse for ImplItemMacro {
1942 fn parse(input: ParseStream) -> Result<Self> {
1943 let attrs = input.call(Attribute::parse_outer)?;
1944 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001945 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001946 None
1947 } else {
1948 Some(input.parse()?)
1949 };
1950 Ok(ImplItemMacro {
1951 attrs: attrs,
1952 mac: mac,
1953 semi_token: semi_token,
1954 })
1955 }
1956 }
David Tolnay4c9be372016-10-06 00:47:37 -07001957
David Tolnay6a170ce2018-08-26 22:29:24 -07001958 impl Visibility {
1959 fn is_inherited(&self) -> bool {
1960 match *self {
1961 Visibility::Inherited => true,
1962 _ => false,
1963 }
1964 }
1965 }
1966
1967 impl MacroDelimiter {
1968 fn is_brace(&self) -> bool {
1969 match *self {
1970 MacroDelimiter::Brace(_) => true,
1971 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1972 }
David Tolnay57292da2017-12-27 21:03:33 -05001973 }
1974 }
David Tolnayedf2b992016-09-23 20:43:45 -07001975}
David Tolnay4a51dc72016-10-01 00:40:31 -07001976
1977#[cfg(feature = "printing")]
1978mod printing {
1979 use super::*;
1980 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001981 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001982 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001983
David Tolnay1bfa7332017-11-11 12:41:20 -08001984 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001985 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001986 tokens.append_all(self.attrs.outer());
1987 self.vis.to_tokens(tokens);
1988 self.extern_token.to_tokens(tokens);
1989 self.crate_token.to_tokens(tokens);
1990 self.ident.to_tokens(tokens);
1991 if let Some((ref as_token, ref rename)) = self.rename {
1992 as_token.to_tokens(tokens);
1993 rename.to_tokens(tokens);
1994 }
1995 self.semi_token.to_tokens(tokens);
1996 }
1997 }
1998
1999 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002000 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002001 tokens.append_all(self.attrs.outer());
2002 self.vis.to_tokens(tokens);
2003 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002004 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002005 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002006 self.semi_token.to_tokens(tokens);
2007 }
2008 }
2009
2010 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002011 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002012 tokens.append_all(self.attrs.outer());
2013 self.vis.to_tokens(tokens);
2014 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002015 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002016 self.ident.to_tokens(tokens);
2017 self.colon_token.to_tokens(tokens);
2018 self.ty.to_tokens(tokens);
2019 self.eq_token.to_tokens(tokens);
2020 self.expr.to_tokens(tokens);
2021 self.semi_token.to_tokens(tokens);
2022 }
2023 }
2024
2025 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002026 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002027 tokens.append_all(self.attrs.outer());
2028 self.vis.to_tokens(tokens);
2029 self.const_token.to_tokens(tokens);
2030 self.ident.to_tokens(tokens);
2031 self.colon_token.to_tokens(tokens);
2032 self.ty.to_tokens(tokens);
2033 self.eq_token.to_tokens(tokens);
2034 self.expr.to_tokens(tokens);
2035 self.semi_token.to_tokens(tokens);
2036 }
2037 }
2038
2039 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002040 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002041 tokens.append_all(self.attrs.outer());
2042 self.vis.to_tokens(tokens);
2043 self.constness.to_tokens(tokens);
2044 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002045 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002046 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002047 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002048 self.block.brace_token.surround(tokens, |tokens| {
2049 tokens.append_all(self.attrs.inner());
2050 tokens.append_all(&self.block.stmts);
2051 });
2052 }
2053 }
2054
2055 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002056 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002057 tokens.append_all(self.attrs.outer());
2058 self.vis.to_tokens(tokens);
2059 self.mod_token.to_tokens(tokens);
2060 self.ident.to_tokens(tokens);
2061 if let Some((ref brace, ref items)) = self.content {
2062 brace.surround(tokens, |tokens| {
2063 tokens.append_all(self.attrs.inner());
2064 tokens.append_all(items);
2065 });
2066 } else {
2067 TokensOrDefault(&self.semi).to_tokens(tokens);
2068 }
2069 }
2070 }
2071
2072 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002073 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002074 tokens.append_all(self.attrs.outer());
2075 self.abi.to_tokens(tokens);
2076 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002077 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002078 tokens.append_all(&self.items);
2079 });
2080 }
2081 }
2082
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002083 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002084 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002085 tokens.append_all(self.attrs.outer());
2086 self.vis.to_tokens(tokens);
2087 self.type_token.to_tokens(tokens);
2088 self.ident.to_tokens(tokens);
2089 self.generics.to_tokens(tokens);
2090 self.generics.where_clause.to_tokens(tokens);
2091 self.eq_token.to_tokens(tokens);
2092 self.ty.to_tokens(tokens);
2093 self.semi_token.to_tokens(tokens);
2094 }
2095 }
2096
David Tolnaybb82ef02018-08-24 20:15:45 -04002097 impl ToTokens for ItemExistential {
2098 fn to_tokens(&self, tokens: &mut TokenStream) {
2099 tokens.append_all(self.attrs.outer());
2100 self.vis.to_tokens(tokens);
2101 self.existential_token.to_tokens(tokens);
2102 self.type_token.to_tokens(tokens);
2103 self.ident.to_tokens(tokens);
2104 self.generics.to_tokens(tokens);
2105 self.generics.where_clause.to_tokens(tokens);
2106 if !self.bounds.is_empty() {
2107 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2108 self.bounds.to_tokens(tokens);
2109 }
2110 self.semi_token.to_tokens(tokens);
2111 }
2112 }
2113
David Tolnay1bfa7332017-11-11 12:41:20 -08002114 impl ToTokens for ItemEnum {
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.enum_token.to_tokens(tokens);
2119 self.ident.to_tokens(tokens);
2120 self.generics.to_tokens(tokens);
2121 self.generics.where_clause.to_tokens(tokens);
2122 self.brace_token.surround(tokens, |tokens| {
2123 self.variants.to_tokens(tokens);
2124 });
2125 }
2126 }
2127
2128 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002129 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002130 tokens.append_all(self.attrs.outer());
2131 self.vis.to_tokens(tokens);
2132 self.struct_token.to_tokens(tokens);
2133 self.ident.to_tokens(tokens);
2134 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002135 match self.fields {
2136 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002137 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002138 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002139 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002140 Fields::Unnamed(ref fields) => {
2141 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002142 self.generics.where_clause.to_tokens(tokens);
2143 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002144 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002145 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002146 self.generics.where_clause.to_tokens(tokens);
2147 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002148 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002149 }
2150 }
2151 }
2152
2153 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002154 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002155 tokens.append_all(self.attrs.outer());
2156 self.vis.to_tokens(tokens);
2157 self.union_token.to_tokens(tokens);
2158 self.ident.to_tokens(tokens);
2159 self.generics.to_tokens(tokens);
2160 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002161 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002162 }
2163 }
2164
2165 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002166 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002167 tokens.append_all(self.attrs.outer());
2168 self.vis.to_tokens(tokens);
2169 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002170 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002171 self.trait_token.to_tokens(tokens);
2172 self.ident.to_tokens(tokens);
2173 self.generics.to_tokens(tokens);
2174 if !self.supertraits.is_empty() {
2175 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2176 self.supertraits.to_tokens(tokens);
2177 }
2178 self.generics.where_clause.to_tokens(tokens);
2179 self.brace_token.surround(tokens, |tokens| {
2180 tokens.append_all(&self.items);
2181 });
2182 }
2183 }
2184
David Tolnayc6b04dd2018-08-30 23:22:51 -07002185 impl ToTokens for ItemTraitAlias {
2186 fn to_tokens(&self, tokens: &mut TokenStream) {
2187 tokens.append_all(self.attrs.outer());
2188 self.vis.to_tokens(tokens);
2189 self.trait_token.to_tokens(tokens);
2190 self.ident.to_tokens(tokens);
2191 self.generics.to_tokens(tokens);
2192 self.eq_token.to_tokens(tokens);
2193 self.bounds.to_tokens(tokens);
2194 self.generics.where_clause.to_tokens(tokens);
2195 self.semi_token.to_tokens(tokens);
2196 }
2197 }
2198
David Tolnay1bfa7332017-11-11 12:41:20 -08002199 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002200 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002201 tokens.append_all(self.attrs.outer());
2202 self.defaultness.to_tokens(tokens);
2203 self.unsafety.to_tokens(tokens);
2204 self.impl_token.to_tokens(tokens);
2205 self.generics.to_tokens(tokens);
2206 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2207 polarity.to_tokens(tokens);
2208 path.to_tokens(tokens);
2209 for_token.to_tokens(tokens);
2210 }
2211 self.self_ty.to_tokens(tokens);
2212 self.generics.where_clause.to_tokens(tokens);
2213 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002214 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002215 tokens.append_all(&self.items);
2216 });
2217 }
2218 }
2219
2220 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002221 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002222 tokens.append_all(self.attrs.outer());
2223 self.mac.path.to_tokens(tokens);
2224 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002225 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002226 match self.mac.delimiter {
2227 MacroDelimiter::Paren(ref paren) => {
2228 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2229 }
2230 MacroDelimiter::Brace(ref brace) => {
2231 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2232 }
2233 MacroDelimiter::Bracket(ref bracket) => {
2234 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2235 }
2236 }
David Tolnay57292da2017-12-27 21:03:33 -05002237 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002238 }
2239 }
David Tolnay42602292016-10-01 22:25:45 -07002240
David Tolnay500d8322017-12-18 00:32:51 -08002241 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002242 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002243 tokens.append_all(self.attrs.outer());
2244 self.vis.to_tokens(tokens);
2245 self.macro_token.to_tokens(tokens);
2246 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002247 self.paren_token.surround(tokens, |tokens| {
2248 self.args.to_tokens(tokens);
2249 });
2250 self.brace_token.surround(tokens, |tokens| {
2251 self.body.to_tokens(tokens);
2252 });
David Tolnay500d8322017-12-18 00:32:51 -08002253 }
2254 }
2255
David Tolnay2ae520a2017-12-29 11:19:50 -05002256 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002257 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002258 self.tts.to_tokens(tokens);
2259 }
2260 }
2261
David Tolnay5f332a92017-12-26 00:42:45 -05002262 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002263 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002264 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002265 self.colon2_token.to_tokens(tokens);
2266 self.tree.to_tokens(tokens);
2267 }
2268 }
2269
2270 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002271 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002272 self.ident.to_tokens(tokens);
2273 }
2274 }
2275
2276 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002277 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002278 self.ident.to_tokens(tokens);
2279 self.as_token.to_tokens(tokens);
2280 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002281 }
2282 }
2283
David Tolnay5f332a92017-12-26 00:42:45 -05002284 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002285 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002286 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002287 }
2288 }
2289
David Tolnayd97a7d22018-03-31 19:17:01 +02002290 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002291 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002292 self.brace_token.surround(tokens, |tokens| {
2293 self.items.to_tokens(tokens);
2294 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002295 }
2296 }
2297
David Tolnay1bfa7332017-11-11 12:41:20 -08002298 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002299 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002300 tokens.append_all(self.attrs.outer());
2301 self.const_token.to_tokens(tokens);
2302 self.ident.to_tokens(tokens);
2303 self.colon_token.to_tokens(tokens);
2304 self.ty.to_tokens(tokens);
2305 if let Some((ref eq_token, ref default)) = self.default {
2306 eq_token.to_tokens(tokens);
2307 default.to_tokens(tokens);
2308 }
2309 self.semi_token.to_tokens(tokens);
2310 }
2311 }
2312
2313 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002314 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002315 tokens.append_all(self.attrs.outer());
2316 self.sig.to_tokens(tokens);
2317 match self.default {
2318 Some(ref block) => {
2319 block.brace_token.surround(tokens, |tokens| {
2320 tokens.append_all(self.attrs.inner());
2321 tokens.append_all(&block.stmts);
2322 });
David Tolnayca085422016-10-04 00:12:38 -07002323 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002324 None => {
2325 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002326 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002327 }
2328 }
2329 }
2330
2331 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002332 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002333 tokens.append_all(self.attrs.outer());
2334 self.type_token.to_tokens(tokens);
2335 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002336 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002337 if !self.bounds.is_empty() {
2338 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2339 self.bounds.to_tokens(tokens);
2340 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002341 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002342 if let Some((ref eq_token, ref default)) = self.default {
2343 eq_token.to_tokens(tokens);
2344 default.to_tokens(tokens);
2345 }
2346 self.semi_token.to_tokens(tokens);
2347 }
2348 }
2349
2350 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002351 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002352 tokens.append_all(self.attrs.outer());
2353 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002354 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002355 }
2356 }
2357
David Tolnay2ae520a2017-12-29 11:19:50 -05002358 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002359 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002360 self.tts.to_tokens(tokens);
2361 }
2362 }
2363
David Tolnay857628c2017-11-11 12:25:31 -08002364 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002365 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002366 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002367 self.vis.to_tokens(tokens);
2368 self.defaultness.to_tokens(tokens);
2369 self.const_token.to_tokens(tokens);
2370 self.ident.to_tokens(tokens);
2371 self.colon_token.to_tokens(tokens);
2372 self.ty.to_tokens(tokens);
2373 self.eq_token.to_tokens(tokens);
2374 self.expr.to_tokens(tokens);
2375 self.semi_token.to_tokens(tokens);
2376 }
2377 }
2378
2379 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002380 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002381 tokens.append_all(self.attrs.outer());
2382 self.vis.to_tokens(tokens);
2383 self.defaultness.to_tokens(tokens);
2384 self.sig.to_tokens(tokens);
2385 self.block.brace_token.surround(tokens, |tokens| {
2386 tokens.append_all(self.attrs.inner());
2387 tokens.append_all(&self.block.stmts);
2388 });
2389 }
2390 }
2391
2392 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002393 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002394 tokens.append_all(self.attrs.outer());
2395 self.vis.to_tokens(tokens);
2396 self.defaultness.to_tokens(tokens);
2397 self.type_token.to_tokens(tokens);
2398 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002399 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002400 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002401 self.eq_token.to_tokens(tokens);
2402 self.ty.to_tokens(tokens);
2403 self.semi_token.to_tokens(tokens);
2404 }
2405 }
2406
David Tolnaybb82ef02018-08-24 20:15:45 -04002407 impl ToTokens for ImplItemExistential {
2408 fn to_tokens(&self, tokens: &mut TokenStream) {
2409 tokens.append_all(self.attrs.outer());
2410 self.existential_token.to_tokens(tokens);
2411 self.type_token.to_tokens(tokens);
2412 self.ident.to_tokens(tokens);
2413 self.generics.to_tokens(tokens);
2414 self.generics.where_clause.to_tokens(tokens);
2415 if !self.bounds.is_empty() {
2416 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2417 self.bounds.to_tokens(tokens);
2418 }
2419 self.semi_token.to_tokens(tokens);
2420 }
2421 }
2422
David Tolnay857628c2017-11-11 12:25:31 -08002423 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002424 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002425 tokens.append_all(self.attrs.outer());
2426 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002427 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002428 }
2429 }
2430
David Tolnay2ae520a2017-12-29 11:19:50 -05002431 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002432 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002433 self.tts.to_tokens(tokens);
2434 }
2435 }
2436
David Tolnay8894f602017-11-11 12:11:04 -08002437 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002438 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002439 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002440 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002441 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002442 self.semi_token.to_tokens(tokens);
2443 }
2444 }
2445
2446 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002447 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002448 tokens.append_all(self.attrs.outer());
2449 self.vis.to_tokens(tokens);
2450 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002451 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002452 self.ident.to_tokens(tokens);
2453 self.colon_token.to_tokens(tokens);
2454 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002455 self.semi_token.to_tokens(tokens);
2456 }
2457 }
2458
David Tolnay199bcbb2017-11-12 10:33:52 -08002459 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002460 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002461 tokens.append_all(self.attrs.outer());
2462 self.vis.to_tokens(tokens);
2463 self.type_token.to_tokens(tokens);
2464 self.ident.to_tokens(tokens);
2465 self.semi_token.to_tokens(tokens);
2466 }
2467 }
2468
David Tolnay435c1782018-08-24 16:15:44 -04002469 impl ToTokens for ForeignItemMacro {
2470 fn to_tokens(&self, tokens: &mut TokenStream) {
2471 tokens.append_all(self.attrs.outer());
2472 self.mac.to_tokens(tokens);
2473 self.semi_token.to_tokens(tokens);
2474 }
2475 }
2476
David Tolnay2ae520a2017-12-29 11:19:50 -05002477 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002478 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002479 self.tts.to_tokens(tokens);
2480 }
2481 }
2482
David Tolnay570695e2017-06-03 16:15:13 -07002483 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002484 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002485 self.constness.to_tokens(tokens);
2486 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002487 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002488 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002489 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002490 }
2491 }
2492
Alex Crichtona74a1c82018-05-16 10:20:44 -07002493 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002494
2495 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002496 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002497 self.0.fn_token.to_tokens(tokens);
2498 self.1.to_tokens(tokens);
2499 self.0.generics.to_tokens(tokens);
2500 self.0.paren_token.surround(tokens, |tokens| {
2501 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002502 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2503 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002504 }
David Tolnayd2836e22017-12-27 23:13:00 -05002505 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002506 });
2507 self.0.output.to_tokens(tokens);
2508 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002509 }
2510 }
2511
Alex Crichton62a0a592017-05-22 13:58:53 -07002512 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002513 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002514 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002515 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002516 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002517 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002518 }
2519 }
2520
2521 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002522 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002523 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002524 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002525 }
2526 }
2527
2528 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002529 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002530 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002531 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002532 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002533 }
2534 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002535}