blob: 7c9066c39245783722e1b9fc9eb0771d86c07092 [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();
David Tolnay5cd40fc2018-10-27 21:53:30 -0700810 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700811 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)
David Tolnayc4de0d72018-11-06 21:11:00 -0800852 } else if lookahead.peek(Token![trait]) {
853 input.call(parse_trait_or_trait_alias)
854 } else if lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) {
David Tolnay6a170ce2018-08-26 22:29:24 -0700855 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()?,
David Tolnayd72b6452018-09-01 18:27:54 -0700975 rename: {
976 if input.peek(Ident) {
977 input.parse()?
978 } else if input.peek(Token![_]) {
979 Ident::from(input.parse::<Token![_]>()?)
980 } else {
981 return Err(input.error("expected identifier or underscore"));
982 }
983 },
David Tolnay3779bb72018-08-26 18:46:07 -0700984 }))
985 } else {
986 Ok(UseTree::Name(UseName { ident: ident }))
987 }
988 } else if lookahead.peek(Token![*]) {
989 Ok(UseTree::Glob(UseGlob {
990 star_token: input.parse()?,
991 }))
992 } else if lookahead.peek(token::Brace) {
993 let content;
994 Ok(UseTree::Group(UseGroup {
995 brace_token: braced!(content in input),
996 items: content.parse_terminated(use_tree)?,
997 }))
998 } else {
999 Err(lookahead.error())
1000 }
1001 }
1002
1003 impl Parse for ItemStatic {
1004 fn parse(input: ParseStream) -> Result<Self> {
1005 Ok(ItemStatic {
1006 attrs: input.call(Attribute::parse_outer)?,
1007 vis: input.parse()?,
1008 static_token: input.parse()?,
1009 mutability: input.parse()?,
1010 ident: input.parse()?,
1011 colon_token: input.parse()?,
1012 ty: input.parse()?,
1013 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001014 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001015 semi_token: input.parse()?,
1016 })
1017 }
1018 }
1019
1020 impl Parse for ItemConst {
1021 fn parse(input: ParseStream) -> Result<Self> {
1022 Ok(ItemConst {
1023 attrs: input.call(Attribute::parse_outer)?,
1024 vis: input.parse()?,
1025 const_token: input.parse()?,
David Tolnay5cd40fc2018-10-27 21:53:30 -07001026 ident: {
1027 let lookahead = input.lookahead1();
1028 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
1029 input.call(Ident::parse_any)?
1030 } else {
1031 return Err(lookahead.error());
1032 }
1033 },
David Tolnay3779bb72018-08-26 18:46:07 -07001034 colon_token: input.parse()?,
1035 ty: input.parse()?,
1036 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001037 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001038 semi_token: input.parse()?,
1039 })
1040 }
1041 }
1042
1043 impl Parse for ItemFn {
1044 fn parse(input: ParseStream) -> Result<Self> {
1045 let outer_attrs = input.call(Attribute::parse_outer)?;
1046 let vis: Visibility = input.parse()?;
1047 let constness: Option<Token![const]> = input.parse()?;
1048 let unsafety: Option<Token![unsafe]> = input.parse()?;
1049 let asyncness: Option<Token![async]> = input.parse()?;
1050 let abi: Option<Abi> = input.parse()?;
1051 let fn_token: Token![fn] = input.parse()?;
1052 let ident: Ident = input.parse()?;
1053 let generics: Generics = input.parse()?;
1054
1055 let content;
1056 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001057 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001058
1059 let output: ReturnType = input.parse()?;
1060 let where_clause: Option<WhereClause> = input.parse()?;
1061
1062 let content;
1063 let brace_token = braced!(content in input);
1064 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001065 let stmts = content.call(Block::parse_within)?;
David Tolnay3779bb72018-08-26 18:46:07 -07001066
1067 Ok(ItemFn {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001068 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001069 vis: vis,
1070 constness: constness,
1071 unsafety: unsafety,
1072 asyncness: asyncness,
1073 abi: abi,
1074 ident: ident,
1075 decl: Box::new(FnDecl {
1076 fn_token: fn_token,
1077 paren_token: paren_token,
1078 inputs: inputs,
1079 output: output,
1080 variadic: None,
1081 generics: Generics {
1082 where_clause: where_clause,
1083 ..generics
1084 },
1085 }),
1086 block: Box::new(Block {
1087 brace_token: brace_token,
1088 stmts: stmts,
1089 }),
1090 })
1091 }
1092 }
David Tolnay42602292016-10-01 22:25:45 -07001093
David Tolnay2ad62c12018-08-26 19:00:35 -04001094 impl Parse for FnArg {
1095 fn parse(input: ParseStream) -> Result<Self> {
1096 if input.peek(Token![&]) {
1097 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001098 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1099 return input.call(arg_self_ref).map(FnArg::SelfRef);
David Tolnay2ad62c12018-08-26 19:00:35 -04001100 }
1101 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001102
David Tolnay2ad62c12018-08-26 19:00:35 -04001103 if input.peek(Token![mut]) || input.peek(Token![self]) {
1104 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001105 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1106 return input.call(arg_self).map(FnArg::SelfValue);
David Tolnay2ad62c12018-08-26 19:00:35 -04001107 }
1108 }
1109
1110 let ahead = input.fork();
David Tolnay3779bb72018-08-26 18:46:07 -07001111 let err = match ahead.call(arg_captured) {
1112 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
David Tolnay2ad62c12018-08-26 19:00:35 -04001113 Err(err) => err,
1114 };
1115
1116 let ahead = input.fork();
1117 if ahead.parse::<Type>().is_ok() {
1118 return input.parse().map(FnArg::Ignored);
1119 }
1120
1121 Err(err)
1122 }
1123 }
1124
David Tolnay3779bb72018-08-26 18:46:07 -07001125 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1126 Ok(ArgSelfRef {
1127 and_token: input.parse()?,
1128 lifetime: input.parse()?,
1129 mutability: input.parse()?,
1130 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001131 })
David Tolnay3779bb72018-08-26 18:46:07 -07001132 }
David Tolnay35902302016-10-06 01:11:08 -07001133
David Tolnay3779bb72018-08-26 18:46:07 -07001134 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1135 Ok(ArgSelf {
1136 mutability: input.parse()?,
1137 self_token: input.parse()?,
David Tolnay4c614be2017-11-10 00:02:38 -08001138 })
David Tolnay3779bb72018-08-26 18:46:07 -07001139 }
1140
1141 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1142 Ok(ArgCaptured {
David Tolnay60291082018-08-28 09:54:49 -07001143 pat: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001144 colon_token: input.parse()?,
1145 ty: input.parse()?,
1146 })
1147 }
1148
1149 impl Parse for ItemMod {
1150 fn parse(input: ParseStream) -> Result<Self> {
1151 let outer_attrs = input.call(Attribute::parse_outer)?;
1152 let vis: Visibility = input.parse()?;
1153 let mod_token: Token![mod] = input.parse()?;
1154 let ident: Ident = input.parse()?;
1155
1156 let lookahead = input.lookahead1();
1157 if lookahead.peek(Token![;]) {
1158 Ok(ItemMod {
1159 attrs: outer_attrs,
1160 vis: vis,
1161 mod_token: mod_token,
1162 ident: ident,
1163 content: None,
1164 semi: Some(input.parse()?),
1165 })
1166 } else if lookahead.peek(token::Brace) {
1167 let content;
1168 let brace_token = braced!(content in input);
1169 let inner_attrs = content.call(Attribute::parse_inner)?;
1170
1171 let mut items = Vec::new();
1172 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001173 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001174 }
1175
1176 Ok(ItemMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001177 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001178 vis: vis,
1179 mod_token: mod_token,
1180 ident: ident,
1181 content: Some((brace_token, items)),
1182 semi: None,
1183 })
1184 } else {
1185 Err(lookahead.error())
1186 }
1187 }
1188 }
1189
1190 impl Parse for ItemForeignMod {
1191 fn parse(input: ParseStream) -> Result<Self> {
1192 let outer_attrs = input.call(Attribute::parse_outer)?;
1193 let abi: Abi = input.parse()?;
1194
1195 let content;
1196 let brace_token = braced!(content in input);
1197 let inner_attrs = content.call(Attribute::parse_inner)?;
1198 let mut items = Vec::new();
1199 while !content.is_empty() {
David Tolnay6a170ce2018-08-26 22:29:24 -07001200 items.push(content.parse()?);
David Tolnay3779bb72018-08-26 18:46:07 -07001201 }
1202
1203 Ok(ItemForeignMod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001204 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay3779bb72018-08-26 18:46:07 -07001205 abi: abi,
1206 brace_token: brace_token,
1207 items: items,
1208 })
1209 }
1210 }
David Tolnay35902302016-10-06 01:11:08 -07001211
David Tolnay6a170ce2018-08-26 22:29:24 -07001212 impl Parse for ForeignItem {
1213 fn parse(input: ParseStream) -> Result<Self> {
1214 let ahead = input.fork();
1215 ahead.call(Attribute::parse_outer)?;
1216 let vis: Visibility = ahead.parse()?;
1217
1218 let lookahead = ahead.lookahead1();
1219 if lookahead.peek(Token![fn]) {
1220 input.parse().map(ForeignItem::Fn)
1221 } else if lookahead.peek(Token![static]) {
1222 input.parse().map(ForeignItem::Static)
1223 } else if lookahead.peek(Token![type]) {
1224 input.parse().map(ForeignItem::Type)
1225 } else if vis.is_inherited()
1226 && (lookahead.peek(Ident)
1227 || lookahead.peek(Token![self])
1228 || lookahead.peek(Token![super])
1229 || lookahead.peek(Token![extern])
1230 || lookahead.peek(Token![crate])
1231 || lookahead.peek(Token![::]))
1232 {
1233 input.parse().map(ForeignItem::Macro)
1234 } else {
1235 Err(lookahead.error())
1236 }
1237 }
1238 }
David Tolnay35902302016-10-06 01:11:08 -07001239
David Tolnay3779bb72018-08-26 18:46:07 -07001240 impl Parse for ForeignItemFn {
1241 fn parse(input: ParseStream) -> Result<Self> {
1242 let attrs = input.call(Attribute::parse_outer)?;
1243 let vis: Visibility = input.parse()?;
1244 let fn_token: Token![fn] = input.parse()?;
1245 let ident: Ident = input.parse()?;
1246 let generics: Generics = input.parse()?;
1247
1248 let content;
1249 let paren_token = parenthesized!(content in input);
David Tolnayf5ebc192018-08-30 18:23:46 -07001250 let mut inputs = Punctuated::new();
1251 while !content.is_empty() && !content.peek(Token![...]) {
1252 inputs.push_value(content.parse()?);
1253 if content.is_empty() {
1254 break;
1255 }
1256 inputs.push_punct(content.parse()?);
1257 }
David Tolnay3779bb72018-08-26 18:46:07 -07001258 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1259 content.parse()?
1260 } else {
1261 None
1262 };
1263
1264 let output: ReturnType = input.parse()?;
1265 let where_clause: Option<WhereClause> = input.parse()?;
1266 let semi_token: Token![;] = input.parse()?;
1267
1268 Ok(ForeignItemFn {
Alex Crichton954046c2017-05-30 21:49:42 -07001269 attrs: attrs,
David Tolnay3779bb72018-08-26 18:46:07 -07001270 vis: vis,
1271 ident: ident,
David Tolnay8894f602017-11-11 12:11:04 -08001272 decl: Box::new(FnDecl {
David Tolnay3779bb72018-08-26 18:46:07 -07001273 fn_token: fn_token,
1274 paren_token: paren_token,
David Tolnay8894f602017-11-11 12:11:04 -08001275 inputs: inputs,
David Tolnay3779bb72018-08-26 18:46:07 -07001276 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001277 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001278 generics: Generics {
1279 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001280 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001281 },
1282 }),
David Tolnay3779bb72018-08-26 18:46:07 -07001283 semi_token: semi_token,
1284 })
1285 }
1286 }
David Tolnay35902302016-10-06 01:11:08 -07001287
David Tolnay3779bb72018-08-26 18:46:07 -07001288 impl Parse for ForeignItemStatic {
1289 fn parse(input: ParseStream) -> Result<Self> {
1290 Ok(ForeignItemStatic {
1291 attrs: input.call(Attribute::parse_outer)?,
1292 vis: input.parse()?,
1293 static_token: input.parse()?,
1294 mutability: input.parse()?,
1295 ident: input.parse()?,
1296 colon_token: input.parse()?,
1297 ty: input.parse()?,
1298 semi_token: input.parse()?,
1299 })
1300 }
1301 }
David Tolnay35902302016-10-06 01:11:08 -07001302
David Tolnay3779bb72018-08-26 18:46:07 -07001303 impl Parse for ForeignItemType {
1304 fn parse(input: ParseStream) -> Result<Self> {
1305 Ok(ForeignItemType {
1306 attrs: input.call(Attribute::parse_outer)?,
1307 vis: input.parse()?,
1308 type_token: input.parse()?,
1309 ident: input.parse()?,
1310 semi_token: input.parse()?,
1311 })
1312 }
1313 }
David Tolnay199bcbb2017-11-12 10:33:52 -08001314
David Tolnay3779bb72018-08-26 18:46:07 -07001315 impl Parse for ForeignItemMacro {
1316 fn parse(input: ParseStream) -> Result<Self> {
1317 let attrs = input.call(Attribute::parse_outer)?;
1318 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001319 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07001320 None
1321 } else {
1322 Some(input.parse()?)
1323 };
1324 Ok(ForeignItemMacro {
1325 attrs: attrs,
1326 mac: mac,
1327 semi_token: semi_token,
1328 })
1329 }
1330 }
David Tolnay78572112018-08-01 00:36:18 -07001331
David Tolnay3779bb72018-08-26 18:46:07 -07001332 impl Parse for ItemType {
1333 fn parse(input: ParseStream) -> Result<Self> {
1334 Ok(ItemType {
1335 attrs: input.call(Attribute::parse_outer)?,
1336 vis: input.parse()?,
1337 type_token: input.parse()?,
1338 ident: input.parse()?,
1339 generics: {
1340 let mut generics: Generics = input.parse()?;
1341 generics.where_clause = input.parse()?;
1342 generics
1343 },
1344 eq_token: input.parse()?,
1345 ty: input.parse()?,
1346 semi_token: input.parse()?,
1347 })
1348 }
1349 }
David Tolnay3cf52982016-10-01 17:11:37 -07001350
David Tolnay3779bb72018-08-26 18:46:07 -07001351 impl Parse for ItemExistential {
1352 fn parse(input: ParseStream) -> Result<Self> {
1353 Ok(ItemExistential {
1354 attrs: input.call(Attribute::parse_outer)?,
1355 vis: input.parse()?,
1356 existential_token: input.parse()?,
1357 type_token: input.parse()?,
1358 ident: input.parse()?,
1359 generics: {
1360 let mut generics: Generics = input.parse()?;
1361 generics.where_clause = input.parse()?;
1362 generics
1363 },
1364 colon_token: Some(input.parse()?),
David Tolnayf5ebc192018-08-30 18:23:46 -07001365 bounds: {
1366 let mut bounds = Punctuated::new();
1367 while !input.peek(Token![;]) {
1368 if !bounds.is_empty() {
1369 bounds.push_punct(input.parse()?);
1370 }
1371 bounds.push_value(input.parse()?);
1372 }
1373 bounds
1374 },
David Tolnay3779bb72018-08-26 18:46:07 -07001375 semi_token: input.parse()?,
1376 })
1377 }
1378 }
David Tolnay758ee132018-08-21 21:29:40 -04001379
David Tolnay6a170ce2018-08-26 22:29:24 -07001380 impl Parse for ItemStruct {
1381 fn parse(input: ParseStream) -> Result<Self> {
1382 let attrs = input.call(Attribute::parse_outer)?;
1383 let vis = input.parse::<Visibility>()?;
1384 let struct_token = input.parse::<Token![struct]>()?;
1385 let ident = input.parse::<Ident>()?;
1386 let generics = input.parse::<Generics>()?;
1387 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1388 Ok(ItemStruct {
1389 attrs: attrs,
1390 vis: vis,
1391 struct_token: struct_token,
1392 ident: ident,
1393 generics: Generics {
1394 where_clause: where_clause,
1395 ..generics
1396 },
1397 fields: fields,
1398 semi_token: semi_token,
1399 })
1400 }
1401 }
David Tolnay42602292016-10-01 22:25:45 -07001402
David Tolnay6a170ce2018-08-26 22:29:24 -07001403 impl Parse for ItemEnum {
1404 fn parse(input: ParseStream) -> Result<Self> {
1405 let attrs = input.call(Attribute::parse_outer)?;
1406 let vis = input.parse::<Visibility>()?;
1407 let enum_token = input.parse::<Token![enum]>()?;
1408 let ident = input.parse::<Ident>()?;
1409 let generics = input.parse::<Generics>()?;
1410 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1411 Ok(ItemEnum {
1412 attrs: attrs,
1413 vis: vis,
1414 enum_token: enum_token,
1415 ident: ident,
1416 generics: Generics {
1417 where_clause: where_clause,
1418 ..generics
1419 },
1420 brace_token: brace_token,
1421 variants: variants,
1422 })
1423 }
1424 }
David Tolnay4c614be2017-11-10 00:02:38 -08001425
David Tolnay6a170ce2018-08-26 22:29:24 -07001426 impl Parse for ItemUnion {
1427 fn parse(input: ParseStream) -> Result<Self> {
1428 let attrs = input.call(Attribute::parse_outer)?;
1429 let vis = input.parse::<Visibility>()?;
1430 let union_token = input.parse::<Token![union]>()?;
1431 let ident = input.parse::<Ident>()?;
1432 let generics = input.parse::<Generics>()?;
1433 let (where_clause, fields) = derive::parsing::data_union(input)?;
1434 Ok(ItemUnion {
1435 attrs: attrs,
1436 vis: vis,
1437 union_token: union_token,
1438 ident: ident,
1439 generics: Generics {
1440 where_clause: where_clause,
1441 ..generics
1442 },
1443 fields: fields,
1444 })
1445 }
1446 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001447
David Tolnayc4de0d72018-11-06 21:11:00 -08001448 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
1449 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1450 let lookahead = input.lookahead1();
1451 if lookahead.peek(token::Brace)
1452 || lookahead.peek(Token![:])
1453 || lookahead.peek(Token![where])
1454 {
1455 let unsafety = None;
1456 let auto_token = None;
1457 parse_rest_of_trait(
1458 input,
1459 attrs,
1460 vis,
1461 unsafety,
1462 auto_token,
1463 trait_token,
1464 ident,
1465 generics,
1466 )
1467 .map(Item::Trait)
1468 } else if lookahead.peek(Token![=]) {
1469 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1470 .map(Item::TraitAlias)
1471 } else {
1472 Err(lookahead.error())
1473 }
1474 }
1475
David Tolnay6a170ce2018-08-26 22:29:24 -07001476 impl Parse for ItemTrait {
1477 fn parse(input: ParseStream) -> Result<Self> {
1478 let attrs = input.call(Attribute::parse_outer)?;
1479 let vis: Visibility = input.parse()?;
1480 let unsafety: Option<Token![unsafe]> = input.parse()?;
1481 let auto_token: Option<Token![auto]> = input.parse()?;
1482 let trait_token: Token![trait] = input.parse()?;
1483 let ident: Ident = input.parse()?;
David Tolnayc4de0d72018-11-06 21:11:00 -08001484 let generics: Generics = input.parse()?;
1485 parse_rest_of_trait(
1486 input,
1487 attrs,
1488 vis,
1489 unsafety,
1490 auto_token,
1491 trait_token,
1492 ident,
1493 generics,
1494 )
1495 }
1496 }
David Tolnayf5ebc192018-08-30 18:23:46 -07001497
David Tolnayc4de0d72018-11-06 21:11:00 -08001498 fn parse_rest_of_trait(
1499 input: ParseStream,
1500 attrs: Vec<Attribute>,
1501 vis: Visibility,
1502 unsafety: Option<Token![unsafe]>,
1503 auto_token: Option<Token![auto]>,
1504 trait_token: Token![trait],
1505 ident: Ident,
1506 mut generics: Generics,
1507 ) -> Result<ItemTrait> {
1508 let colon_token: Option<Token![:]> = input.parse()?;
1509
1510 let mut supertraits = Punctuated::new();
1511 if colon_token.is_some() {
1512 loop {
1513 supertraits.push_value(input.parse()?);
1514 if input.peek(Token![where]) || input.peek(token::Brace) {
1515 break;
1516 }
1517 supertraits.push_punct(input.parse()?);
1518 if input.peek(Token![where]) || input.peek(token::Brace) {
1519 break;
David Tolnayf5ebc192018-08-30 18:23:46 -07001520 }
1521 }
David Tolnay6a170ce2018-08-26 22:29:24 -07001522 }
David Tolnayc4de0d72018-11-06 21:11:00 -08001523
1524 generics.where_clause = input.parse()?;
1525
1526 let content;
1527 let brace_token = braced!(content in input);
1528 let mut items = Vec::new();
1529 while !content.is_empty() {
1530 items.push(content.parse()?);
1531 }
1532
1533 Ok(ItemTrait {
1534 attrs: attrs,
1535 vis: vis,
1536 unsafety: unsafety,
1537 auto_token: auto_token,
1538 trait_token: trait_token,
1539 ident: ident,
1540 generics: generics,
1541 colon_token: colon_token,
1542 supertraits: supertraits,
1543 brace_token: brace_token,
1544 items: items,
1545 })
David Tolnay6a170ce2018-08-26 22:29:24 -07001546 }
1547
David Tolnayc6b04dd2018-08-30 23:22:51 -07001548 impl Parse for ItemTraitAlias {
1549 fn parse(input: ParseStream) -> Result<Self> {
David Tolnayc4de0d72018-11-06 21:11:00 -08001550 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1551 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
David Tolnayc6b04dd2018-08-30 23:22:51 -07001552 }
1553 }
1554
David Tolnayc4de0d72018-11-06 21:11:00 -08001555 fn parse_start_of_trait_alias(
1556 input: ParseStream,
1557 ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> {
1558 let attrs = input.call(Attribute::parse_outer)?;
1559 let vis: Visibility = input.parse()?;
1560 let trait_token: Token![trait] = input.parse()?;
1561 let ident: Ident = input.parse()?;
1562 let generics: Generics = input.parse()?;
1563 Ok((attrs, vis, trait_token, ident, generics))
1564 }
1565
1566 fn parse_rest_of_trait_alias(
1567 input: ParseStream,
1568 attrs: Vec<Attribute>,
1569 vis: Visibility,
1570 trait_token: Token![trait],
1571 ident: Ident,
1572 mut generics: Generics,
1573 ) -> Result<ItemTraitAlias> {
1574 let eq_token: Token![=] = input.parse()?;
1575
1576 let mut bounds = Punctuated::new();
1577 loop {
1578 if input.peek(Token![where]) || input.peek(Token![;]) {
1579 break;
1580 }
1581 bounds.push_value(input.parse()?);
1582 if input.peek(Token![where]) || input.peek(Token![;]) {
1583 break;
1584 }
1585 bounds.push_punct(input.parse()?);
1586 }
1587
1588 generics.where_clause = input.parse()?;
1589 let semi_token: Token![;] = input.parse()?;
1590
1591 Ok(ItemTraitAlias {
1592 attrs: attrs,
1593 vis: vis,
1594 trait_token: trait_token,
1595 ident: ident,
1596 generics: generics,
1597 eq_token: eq_token,
1598 bounds: bounds,
1599 semi_token: semi_token,
1600 })
1601 }
1602
David Tolnay6a170ce2018-08-26 22:29:24 -07001603 impl Parse for TraitItem {
1604 fn parse(input: ParseStream) -> Result<Self> {
1605 let ahead = input.fork();
1606 ahead.call(Attribute::parse_outer)?;
1607
1608 let lookahead = ahead.lookahead1();
1609 if lookahead.peek(Token![const]) {
1610 ahead.parse::<Token![const]>()?;
1611 let lookahead = ahead.lookahead1();
1612 if lookahead.peek(Ident) {
1613 input.parse().map(TraitItem::Const)
1614 } else if lookahead.peek(Token![unsafe])
1615 || lookahead.peek(Token![extern])
1616 || lookahead.peek(Token![fn])
1617 {
1618 input.parse().map(TraitItem::Method)
1619 } else {
1620 Err(lookahead.error())
1621 }
1622 } else if lookahead.peek(Token![unsafe])
1623 || lookahead.peek(Token![extern])
1624 || lookahead.peek(Token![fn])
1625 {
1626 input.parse().map(TraitItem::Method)
1627 } else if lookahead.peek(Token![type]) {
1628 input.parse().map(TraitItem::Type)
1629 } else if lookahead.peek(Ident)
1630 || lookahead.peek(Token![self])
1631 || lookahead.peek(Token![super])
1632 || lookahead.peek(Token![extern])
1633 || lookahead.peek(Token![crate])
1634 || lookahead.peek(Token![::])
1635 {
1636 input.parse().map(TraitItem::Macro)
1637 } else {
1638 Err(lookahead.error())
1639 }
1640 }
1641 }
1642
1643 impl Parse for TraitItemConst {
1644 fn parse(input: ParseStream) -> Result<Self> {
1645 Ok(TraitItemConst {
1646 attrs: input.call(Attribute::parse_outer)?,
1647 const_token: input.parse()?,
1648 ident: input.parse()?,
1649 colon_token: input.parse()?,
1650 ty: input.parse()?,
1651 default: {
1652 if input.peek(Token![=]) {
1653 let eq_token: Token![=] = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -07001654 let default: Expr = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001655 Some((eq_token, default))
1656 } else {
1657 None
1658 }
1659 },
1660 semi_token: input.parse()?,
1661 })
1662 }
1663 }
1664
1665 impl Parse for TraitItemMethod {
1666 fn parse(input: ParseStream) -> Result<Self> {
1667 let outer_attrs = input.call(Attribute::parse_outer)?;
1668 let constness: Option<Token![const]> = input.parse()?;
1669 let unsafety: Option<Token![unsafe]> = input.parse()?;
1670 let abi: Option<Abi> = input.parse()?;
1671 let fn_token: Token![fn] = input.parse()?;
1672 let ident: Ident = input.parse()?;
1673 let generics: Generics = input.parse()?;
1674
1675 let content;
1676 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001677 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001678
1679 let output: ReturnType = input.parse()?;
1680 let where_clause: Option<WhereClause> = input.parse()?;
1681
1682 let lookahead = input.lookahead1();
1683 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1684 let content;
1685 let brace_token = braced!(content in input);
1686 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001687 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001688 (Some(brace_token), inner_attrs, stmts, None)
1689 } else if lookahead.peek(Token![;]) {
1690 let semi_token: Token![;] = input.parse()?;
1691 (None, Vec::new(), Vec::new(), Some(semi_token))
1692 } else {
1693 return Err(lookahead.error());
1694 };
1695
1696 Ok(TraitItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001697 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnayda705bd2017-11-10 21:58:05 -08001698 sig: MethodSig {
1699 constness: constness,
1700 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001701 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001702 abi: abi,
1703 ident: ident,
1704 decl: FnDecl {
David Tolnay6a170ce2018-08-26 22:29:24 -07001705 fn_token: fn_token,
1706 paren_token: paren_token,
1707 inputs: inputs,
1708 output: output,
David Tolnayd2836e22017-12-27 23:13:00 -05001709 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001710 generics: Generics {
1711 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001712 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001713 },
1714 },
David Tolnayda705bd2017-11-10 21:58:05 -08001715 },
David Tolnay6a170ce2018-08-26 22:29:24 -07001716 default: brace_token.map(|brace_token| Block {
1717 brace_token: brace_token,
1718 stmts: stmts,
David Tolnayda705bd2017-11-10 21:58:05 -08001719 }),
David Tolnay6a170ce2018-08-26 22:29:24 -07001720 semi_token: semi_token,
1721 })
1722 }
1723 }
1724
1725 impl Parse for TraitItemType {
1726 fn parse(input: ParseStream) -> Result<Self> {
1727 let attrs = input.call(Attribute::parse_outer)?;
1728 let type_token: Token![type] = input.parse()?;
1729 let ident: Ident = input.parse()?;
1730 let mut generics: Generics = input.parse()?;
1731 let colon_token: Option<Token![:]> = input.parse()?;
David Tolnayf5ebc192018-08-30 18:23:46 -07001732
1733 let mut bounds = Punctuated::new();
1734 if colon_token.is_some() {
David Tolnay73b7ca12018-08-30 21:05:13 -07001735 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1736 {
David Tolnayf5ebc192018-08-30 18:23:46 -07001737 if !bounds.is_empty() {
1738 bounds.push_punct(input.parse()?);
1739 }
1740 bounds.push_value(input.parse()?);
1741 }
1742 }
1743
David Tolnay6a170ce2018-08-26 22:29:24 -07001744 generics.where_clause = input.parse()?;
1745 let default = if input.peek(Token![=]) {
1746 let eq_token: Token![=] = input.parse()?;
1747 let default: Type = input.parse()?;
1748 Some((eq_token, default))
1749 } else {
1750 None
1751 };
1752 let semi_token: Token![;] = input.parse()?;
1753
1754 Ok(TraitItemType {
1755 attrs: attrs,
1756 type_token: type_token,
1757 ident: ident,
1758 generics: generics,
1759 colon_token: colon_token,
1760 bounds: bounds,
1761 default: default,
1762 semi_token: semi_token,
1763 })
1764 }
1765 }
1766
1767 impl Parse for TraitItemMacro {
1768 fn parse(input: ParseStream) -> Result<Self> {
1769 let attrs = input.call(Attribute::parse_outer)?;
1770 let mac: Macro = input.parse()?;
1771 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1772 None
1773 } else {
1774 Some(input.parse()?)
1775 };
1776 Ok(TraitItemMacro {
1777 attrs: attrs,
1778 mac: mac,
1779 semi_token: semi_token,
1780 })
1781 }
1782 }
1783
1784 impl Parse for ItemImpl {
1785 fn parse(input: ParseStream) -> Result<Self> {
1786 let outer_attrs = input.call(Attribute::parse_outer)?;
1787 let defaultness: Option<Token![default]> = input.parse()?;
1788 let unsafety: Option<Token![unsafe]> = input.parse()?;
1789 let impl_token: Token![impl ] = input.parse()?;
David Tolnay3c29f6e2018-10-27 22:47:48 -07001790
1791 let has_generics = input.peek(Token![<])
1792 && (input.peek2(Token![>])
1793 || input.peek2(Token![#])
1794 || (input.peek2(Ident) || input.peek2(Lifetime))
David Tolnaye614f282018-10-27 22:50:12 -07001795 && (input.peek3(Token![:])
1796 || input.peek3(Token![,])
1797 || input.peek3(Token![>])));
David Tolnay3c29f6e2018-10-27 22:47:48 -07001798 let generics: Generics = if has_generics {
1799 input.parse()?
1800 } else {
1801 Generics::default()
1802 };
1803
David Tolnay6a170ce2018-08-26 22:29:24 -07001804 let trait_ = {
1805 let ahead = input.fork();
1806 if ahead.parse::<Option<Token![!]>>().is_ok()
1807 && ahead.parse::<Path>().is_ok()
1808 && ahead.parse::<Token![for]>().is_ok()
1809 {
1810 let polarity: Option<Token![!]> = input.parse()?;
1811 let path: Path = input.parse()?;
1812 let for_token: Token![for] = input.parse()?;
1813 Some((polarity, path, for_token))
1814 } else {
1815 None
1816 }
1817 };
1818 let self_ty: Type = input.parse()?;
1819 let where_clause: Option<WhereClause> = input.parse()?;
1820
1821 let content;
1822 let brace_token = braced!(content in input);
1823 let inner_attrs = content.call(Attribute::parse_inner)?;
1824
1825 let mut items = Vec::new();
1826 while !content.is_empty() {
1827 items.push(content.parse()?);
David Tolnay5859df12016-10-29 22:49:54 -07001828 }
David Tolnay0aecb732016-10-03 23:03:50 -07001829
David Tolnay6a170ce2018-08-26 22:29:24 -07001830 Ok(ItemImpl {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001831 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07001832 defaultness: defaultness,
1833 unsafety: unsafety,
1834 impl_token: impl_token,
1835 generics: Generics {
1836 where_clause: where_clause,
1837 ..generics
1838 },
1839 trait_: trait_,
1840 self_ty: Box::new(self_ty),
1841 brace_token: brace_token,
1842 items: items,
1843 })
1844 }
1845 }
David Tolnay0aecb732016-10-03 23:03:50 -07001846
David Tolnay6a170ce2018-08-26 22:29:24 -07001847 impl Parse for ImplItem {
1848 fn parse(input: ParseStream) -> Result<Self> {
1849 let ahead = input.fork();
1850 ahead.call(Attribute::parse_outer)?;
1851 let vis: Visibility = ahead.parse()?;
David Tolnay0aecb732016-10-03 23:03:50 -07001852
David Tolnay6a170ce2018-08-26 22:29:24 -07001853 let mut lookahead = ahead.lookahead1();
1854 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1855 let defaultness: Token![default] = ahead.parse()?;
1856 lookahead = ahead.lookahead1();
1857 Some(defaultness)
1858 } else {
1859 None
1860 };
David Tolnay4c9be372016-10-06 00:47:37 -07001861
David Tolnay6a170ce2018-08-26 22:29:24 -07001862 if lookahead.peek(Token![const]) {
1863 ahead.parse::<Token![const]>()?;
1864 let lookahead = ahead.lookahead1();
1865 if lookahead.peek(Ident) {
1866 input.parse().map(ImplItem::Const)
1867 } else if lookahead.peek(Token![unsafe])
1868 || lookahead.peek(Token![async])
1869 || lookahead.peek(Token![extern])
1870 || lookahead.peek(Token![fn])
1871 {
1872 input.parse().map(ImplItem::Method)
1873 } else {
1874 Err(lookahead.error())
1875 }
1876 } else if lookahead.peek(Token![unsafe])
1877 || lookahead.peek(Token![async])
1878 || lookahead.peek(Token![extern])
1879 || lookahead.peek(Token![fn])
1880 {
1881 input.parse().map(ImplItem::Method)
1882 } else if lookahead.peek(Token![type]) {
1883 input.parse().map(ImplItem::Type)
1884 } else if vis.is_inherited()
1885 && defaultness.is_none()
1886 && lookahead.peek(Token![existential])
1887 {
1888 input.parse().map(ImplItem::Existential)
1889 } else if vis.is_inherited()
1890 && defaultness.is_none()
1891 && (lookahead.peek(Ident)
1892 || lookahead.peek(Token![self])
1893 || lookahead.peek(Token![super])
1894 || lookahead.peek(Token![extern])
1895 || lookahead.peek(Token![crate])
1896 || lookahead.peek(Token![::]))
1897 {
1898 input.parse().map(ImplItem::Macro)
1899 } else {
1900 Err(lookahead.error())
1901 }
1902 }
1903 }
David Tolnay4c9be372016-10-06 00:47:37 -07001904
David Tolnay3779bb72018-08-26 18:46:07 -07001905 impl Parse for ImplItemConst {
1906 fn parse(input: ParseStream) -> Result<Self> {
1907 Ok(ImplItemConst {
1908 attrs: input.call(Attribute::parse_outer)?,
1909 vis: input.parse()?,
1910 defaultness: input.parse()?,
1911 const_token: input.parse()?,
1912 ident: input.parse()?,
1913 colon_token: input.parse()?,
1914 ty: input.parse()?,
1915 eq_token: input.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -07001916 expr: input.parse()?,
David Tolnay3779bb72018-08-26 18:46:07 -07001917 semi_token: input.parse()?,
1918 })
1919 }
1920 }
David Tolnay4c9be372016-10-06 00:47:37 -07001921
David Tolnay6a170ce2018-08-26 22:29:24 -07001922 impl Parse for ImplItemMethod {
1923 fn parse(input: ParseStream) -> Result<Self> {
1924 let outer_attrs = input.call(Attribute::parse_outer)?;
1925 let vis: Visibility = input.parse()?;
1926 let defaultness: Option<Token![default]> = input.parse()?;
1927 let constness: Option<Token![const]> = input.parse()?;
1928 let unsafety: Option<Token![unsafe]> = input.parse()?;
1929 let asyncness: Option<Token![async]> = input.parse()?;
1930 let abi: Option<Abi> = input.parse()?;
1931 let fn_token: Token![fn] = input.parse()?;
1932 let ident: Ident = input.parse()?;
1933 let generics: Generics = input.parse()?;
1934
1935 let content;
1936 let paren_token = parenthesized!(content in input);
David Tolnay60484fe2018-08-30 18:43:04 -07001937 let inputs = content.parse_terminated(FnArg::parse)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001938
1939 let output: ReturnType = input.parse()?;
1940 let where_clause: Option<WhereClause> = input.parse()?;
1941
1942 let content;
1943 let brace_token = braced!(content in input);
1944 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay9389c382018-08-27 09:13:37 -07001945 let stmts = content.call(Block::parse_within)?;
David Tolnay6a170ce2018-08-26 22:29:24 -07001946
1947 Ok(ImplItemMethod {
David Tolnayb5f6fc02018-09-01 02:18:50 -07001948 attrs: private::attrs(outer_attrs, inner_attrs),
David Tolnay6a170ce2018-08-26 22:29:24 -07001949 vis: vis,
1950 defaultness: defaultness,
1951 sig: MethodSig {
1952 constness: constness,
1953 unsafety: unsafety,
1954 asyncness: asyncness,
1955 abi: abi,
1956 ident: ident,
1957 decl: FnDecl {
1958 fn_token: fn_token,
1959 paren_token: paren_token,
1960 inputs: inputs,
1961 output: output,
1962 variadic: None,
1963 generics: Generics {
1964 where_clause: where_clause,
1965 ..generics
1966 },
1967 },
1968 },
1969 block: Block {
1970 brace_token: brace_token,
1971 stmts: stmts,
1972 },
1973 })
1974 }
1975 }
David Tolnay4c9be372016-10-06 00:47:37 -07001976
David Tolnay3779bb72018-08-26 18:46:07 -07001977 impl Parse for ImplItemType {
1978 fn parse(input: ParseStream) -> Result<Self> {
1979 Ok(ImplItemType {
1980 attrs: input.call(Attribute::parse_outer)?,
1981 vis: input.parse()?,
1982 defaultness: input.parse()?,
1983 type_token: input.parse()?,
1984 ident: input.parse()?,
1985 generics: {
1986 let mut generics: Generics = input.parse()?;
1987 generics.where_clause = input.parse()?;
1988 generics
1989 },
1990 eq_token: input.parse()?,
1991 ty: input.parse()?,
1992 semi_token: input.parse()?,
1993 })
David Tolnaybb82ef02018-08-24 20:15:45 -04001994 }
David Tolnay3779bb72018-08-26 18:46:07 -07001995 }
David Tolnay758ee132018-08-21 21:29:40 -04001996
David Tolnay3779bb72018-08-26 18:46:07 -07001997 impl Parse for ImplItemExistential {
1998 fn parse(input: ParseStream) -> Result<Self> {
1999 let ety: ItemExistential = input.parse()?;
2000 Ok(ImplItemExistential {
2001 attrs: ety.attrs,
2002 existential_token: ety.existential_token,
2003 type_token: ety.type_token,
2004 ident: ety.ident,
2005 generics: ety.generics,
2006 colon_token: ety.colon_token,
2007 bounds: ety.bounds,
2008 semi_token: ety.semi_token,
2009 })
2010 }
2011 }
2012
2013 impl Parse for ImplItemMacro {
2014 fn parse(input: ParseStream) -> Result<Self> {
2015 let attrs = input.call(Attribute::parse_outer)?;
2016 let mac: Macro = input.parse()?;
David Tolnay6a170ce2018-08-26 22:29:24 -07002017 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
David Tolnay3779bb72018-08-26 18:46:07 -07002018 None
2019 } else {
2020 Some(input.parse()?)
2021 };
2022 Ok(ImplItemMacro {
2023 attrs: attrs,
2024 mac: mac,
2025 semi_token: semi_token,
2026 })
2027 }
2028 }
David Tolnay4c9be372016-10-06 00:47:37 -07002029
David Tolnay6a170ce2018-08-26 22:29:24 -07002030 impl Visibility {
2031 fn is_inherited(&self) -> bool {
2032 match *self {
2033 Visibility::Inherited => true,
2034 _ => false,
2035 }
2036 }
2037 }
2038
2039 impl MacroDelimiter {
2040 fn is_brace(&self) -> bool {
2041 match *self {
2042 MacroDelimiter::Brace(_) => true,
2043 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
2044 }
David Tolnay57292da2017-12-27 21:03:33 -05002045 }
2046 }
David Tolnayedf2b992016-09-23 20:43:45 -07002047}
David Tolnay4a51dc72016-10-01 00:40:31 -07002048
2049#[cfg(feature = "printing")]
2050mod printing {
2051 use super::*;
David Tolnay64023912018-08-31 09:51:12 -07002052
Alex Crichtona74a1c82018-05-16 10:20:44 -07002053 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07002054 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07002055
David Tolnay64023912018-08-31 09:51:12 -07002056 use attr::FilterAttrs;
2057 use print::TokensOrDefault;
2058
David Tolnay1bfa7332017-11-11 12:41:20 -08002059 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002060 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002061 tokens.append_all(self.attrs.outer());
2062 self.vis.to_tokens(tokens);
2063 self.extern_token.to_tokens(tokens);
2064 self.crate_token.to_tokens(tokens);
2065 self.ident.to_tokens(tokens);
2066 if let Some((ref as_token, ref rename)) = self.rename {
2067 as_token.to_tokens(tokens);
2068 rename.to_tokens(tokens);
2069 }
2070 self.semi_token.to_tokens(tokens);
2071 }
2072 }
2073
2074 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002075 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002076 tokens.append_all(self.attrs.outer());
2077 self.vis.to_tokens(tokens);
2078 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002079 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05002080 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002081 self.semi_token.to_tokens(tokens);
2082 }
2083 }
2084
2085 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002086 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002087 tokens.append_all(self.attrs.outer());
2088 self.vis.to_tokens(tokens);
2089 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002090 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002091 self.ident.to_tokens(tokens);
2092 self.colon_token.to_tokens(tokens);
2093 self.ty.to_tokens(tokens);
2094 self.eq_token.to_tokens(tokens);
2095 self.expr.to_tokens(tokens);
2096 self.semi_token.to_tokens(tokens);
2097 }
2098 }
2099
2100 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002101 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002102 tokens.append_all(self.attrs.outer());
2103 self.vis.to_tokens(tokens);
2104 self.const_token.to_tokens(tokens);
2105 self.ident.to_tokens(tokens);
2106 self.colon_token.to_tokens(tokens);
2107 self.ty.to_tokens(tokens);
2108 self.eq_token.to_tokens(tokens);
2109 self.expr.to_tokens(tokens);
2110 self.semi_token.to_tokens(tokens);
2111 }
2112 }
2113
2114 impl ToTokens for ItemFn {
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.constness.to_tokens(tokens);
2119 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002120 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002121 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002122 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002123 self.block.brace_token.surround(tokens, |tokens| {
2124 tokens.append_all(self.attrs.inner());
2125 tokens.append_all(&self.block.stmts);
2126 });
2127 }
2128 }
2129
2130 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002131 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002132 tokens.append_all(self.attrs.outer());
2133 self.vis.to_tokens(tokens);
2134 self.mod_token.to_tokens(tokens);
2135 self.ident.to_tokens(tokens);
2136 if let Some((ref brace, ref items)) = self.content {
2137 brace.surround(tokens, |tokens| {
2138 tokens.append_all(self.attrs.inner());
2139 tokens.append_all(items);
2140 });
2141 } else {
2142 TokensOrDefault(&self.semi).to_tokens(tokens);
2143 }
2144 }
2145 }
2146
2147 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002148 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002149 tokens.append_all(self.attrs.outer());
2150 self.abi.to_tokens(tokens);
2151 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07002152 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002153 tokens.append_all(&self.items);
2154 });
2155 }
2156 }
2157
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002158 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002159 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002160 tokens.append_all(self.attrs.outer());
2161 self.vis.to_tokens(tokens);
2162 self.type_token.to_tokens(tokens);
2163 self.ident.to_tokens(tokens);
2164 self.generics.to_tokens(tokens);
2165 self.generics.where_clause.to_tokens(tokens);
2166 self.eq_token.to_tokens(tokens);
2167 self.ty.to_tokens(tokens);
2168 self.semi_token.to_tokens(tokens);
2169 }
2170 }
2171
David Tolnaybb82ef02018-08-24 20:15:45 -04002172 impl ToTokens for ItemExistential {
2173 fn to_tokens(&self, tokens: &mut TokenStream) {
2174 tokens.append_all(self.attrs.outer());
2175 self.vis.to_tokens(tokens);
2176 self.existential_token.to_tokens(tokens);
2177 self.type_token.to_tokens(tokens);
2178 self.ident.to_tokens(tokens);
2179 self.generics.to_tokens(tokens);
2180 self.generics.where_clause.to_tokens(tokens);
2181 if !self.bounds.is_empty() {
2182 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2183 self.bounds.to_tokens(tokens);
2184 }
2185 self.semi_token.to_tokens(tokens);
2186 }
2187 }
2188
David Tolnay1bfa7332017-11-11 12:41:20 -08002189 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002190 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002191 tokens.append_all(self.attrs.outer());
2192 self.vis.to_tokens(tokens);
2193 self.enum_token.to_tokens(tokens);
2194 self.ident.to_tokens(tokens);
2195 self.generics.to_tokens(tokens);
2196 self.generics.where_clause.to_tokens(tokens);
2197 self.brace_token.surround(tokens, |tokens| {
2198 self.variants.to_tokens(tokens);
2199 });
2200 }
2201 }
2202
2203 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002204 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002205 tokens.append_all(self.attrs.outer());
2206 self.vis.to_tokens(tokens);
2207 self.struct_token.to_tokens(tokens);
2208 self.ident.to_tokens(tokens);
2209 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002210 match self.fields {
2211 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002212 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002213 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002214 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002215 Fields::Unnamed(ref fields) => {
2216 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002217 self.generics.where_clause.to_tokens(tokens);
2218 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002219 }
David Tolnaye3d41b72017-12-31 15:24:00 -05002220 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08002221 self.generics.where_clause.to_tokens(tokens);
2222 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07002223 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002224 }
2225 }
2226 }
2227
2228 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002229 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002230 tokens.append_all(self.attrs.outer());
2231 self.vis.to_tokens(tokens);
2232 self.union_token.to_tokens(tokens);
2233 self.ident.to_tokens(tokens);
2234 self.generics.to_tokens(tokens);
2235 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05002236 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002237 }
2238 }
2239
2240 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002241 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002242 tokens.append_all(self.attrs.outer());
2243 self.vis.to_tokens(tokens);
2244 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05002245 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002246 self.trait_token.to_tokens(tokens);
2247 self.ident.to_tokens(tokens);
2248 self.generics.to_tokens(tokens);
2249 if !self.supertraits.is_empty() {
2250 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2251 self.supertraits.to_tokens(tokens);
2252 }
2253 self.generics.where_clause.to_tokens(tokens);
2254 self.brace_token.surround(tokens, |tokens| {
2255 tokens.append_all(&self.items);
2256 });
2257 }
2258 }
2259
David Tolnayc6b04dd2018-08-30 23:22:51 -07002260 impl ToTokens for ItemTraitAlias {
2261 fn to_tokens(&self, tokens: &mut TokenStream) {
2262 tokens.append_all(self.attrs.outer());
2263 self.vis.to_tokens(tokens);
2264 self.trait_token.to_tokens(tokens);
2265 self.ident.to_tokens(tokens);
2266 self.generics.to_tokens(tokens);
2267 self.eq_token.to_tokens(tokens);
2268 self.bounds.to_tokens(tokens);
2269 self.generics.where_clause.to_tokens(tokens);
2270 self.semi_token.to_tokens(tokens);
2271 }
2272 }
2273
David Tolnay1bfa7332017-11-11 12:41:20 -08002274 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002275 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002276 tokens.append_all(self.attrs.outer());
2277 self.defaultness.to_tokens(tokens);
2278 self.unsafety.to_tokens(tokens);
2279 self.impl_token.to_tokens(tokens);
2280 self.generics.to_tokens(tokens);
2281 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2282 polarity.to_tokens(tokens);
2283 path.to_tokens(tokens);
2284 for_token.to_tokens(tokens);
2285 }
2286 self.self_ty.to_tokens(tokens);
2287 self.generics.where_clause.to_tokens(tokens);
2288 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02002289 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08002290 tokens.append_all(&self.items);
2291 });
2292 }
2293 }
2294
2295 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002296 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002297 tokens.append_all(self.attrs.outer());
2298 self.mac.path.to_tokens(tokens);
2299 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08002300 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002301 match self.mac.delimiter {
2302 MacroDelimiter::Paren(ref paren) => {
2303 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2304 }
2305 MacroDelimiter::Brace(ref brace) => {
2306 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2307 }
2308 MacroDelimiter::Bracket(ref bracket) => {
2309 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2310 }
2311 }
David Tolnay57292da2017-12-27 21:03:33 -05002312 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07002313 }
2314 }
David Tolnay42602292016-10-01 22:25:45 -07002315
David Tolnay500d8322017-12-18 00:32:51 -08002316 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002317 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08002318 tokens.append_all(self.attrs.outer());
2319 self.vis.to_tokens(tokens);
2320 self.macro_token.to_tokens(tokens);
2321 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05002322 self.paren_token.surround(tokens, |tokens| {
2323 self.args.to_tokens(tokens);
2324 });
2325 self.brace_token.surround(tokens, |tokens| {
2326 self.body.to_tokens(tokens);
2327 });
David Tolnay500d8322017-12-18 00:32:51 -08002328 }
2329 }
2330
David Tolnay2ae520a2017-12-29 11:19:50 -05002331 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002332 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002333 self.tts.to_tokens(tokens);
2334 }
2335 }
2336
David Tolnay5f332a92017-12-26 00:42:45 -05002337 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002338 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05002339 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02002340 self.colon2_token.to_tokens(tokens);
2341 self.tree.to_tokens(tokens);
2342 }
2343 }
2344
2345 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002346 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002347 self.ident.to_tokens(tokens);
2348 }
2349 }
2350
2351 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002352 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02002353 self.ident.to_tokens(tokens);
2354 self.as_token.to_tokens(tokens);
2355 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07002356 }
2357 }
2358
David Tolnay5f332a92017-12-26 00:42:45 -05002359 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002360 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002361 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002362 }
2363 }
2364
David Tolnayd97a7d22018-03-31 19:17:01 +02002365 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002366 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002367 self.brace_token.surround(tokens, |tokens| {
2368 self.items.to_tokens(tokens);
2369 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002370 }
2371 }
2372
David Tolnay1bfa7332017-11-11 12:41:20 -08002373 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002374 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002375 tokens.append_all(self.attrs.outer());
2376 self.const_token.to_tokens(tokens);
2377 self.ident.to_tokens(tokens);
2378 self.colon_token.to_tokens(tokens);
2379 self.ty.to_tokens(tokens);
2380 if let Some((ref eq_token, ref default)) = self.default {
2381 eq_token.to_tokens(tokens);
2382 default.to_tokens(tokens);
2383 }
2384 self.semi_token.to_tokens(tokens);
2385 }
2386 }
2387
2388 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002389 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002390 tokens.append_all(self.attrs.outer());
2391 self.sig.to_tokens(tokens);
2392 match self.default {
2393 Some(ref block) => {
2394 block.brace_token.surround(tokens, |tokens| {
2395 tokens.append_all(self.attrs.inner());
2396 tokens.append_all(&block.stmts);
2397 });
David Tolnayca085422016-10-04 00:12:38 -07002398 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002399 None => {
2400 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002401 }
David Tolnay1bfa7332017-11-11 12:41:20 -08002402 }
2403 }
2404 }
2405
2406 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002407 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002408 tokens.append_all(self.attrs.outer());
2409 self.type_token.to_tokens(tokens);
2410 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002411 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002412 if !self.bounds.is_empty() {
2413 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2414 self.bounds.to_tokens(tokens);
2415 }
Nika Layzell0183ca32017-12-05 15:24:01 -05002416 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08002417 if let Some((ref eq_token, ref default)) = self.default {
2418 eq_token.to_tokens(tokens);
2419 default.to_tokens(tokens);
2420 }
2421 self.semi_token.to_tokens(tokens);
2422 }
2423 }
2424
2425 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002426 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002427 tokens.append_all(self.attrs.outer());
2428 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002429 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002430 }
2431 }
2432
David Tolnay2ae520a2017-12-29 11:19:50 -05002433 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002434 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002435 self.tts.to_tokens(tokens);
2436 }
2437 }
2438
David Tolnay857628c2017-11-11 12:25:31 -08002439 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002440 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002441 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002442 self.vis.to_tokens(tokens);
2443 self.defaultness.to_tokens(tokens);
2444 self.const_token.to_tokens(tokens);
2445 self.ident.to_tokens(tokens);
2446 self.colon_token.to_tokens(tokens);
2447 self.ty.to_tokens(tokens);
2448 self.eq_token.to_tokens(tokens);
2449 self.expr.to_tokens(tokens);
2450 self.semi_token.to_tokens(tokens);
2451 }
2452 }
2453
2454 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002455 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002456 tokens.append_all(self.attrs.outer());
2457 self.vis.to_tokens(tokens);
2458 self.defaultness.to_tokens(tokens);
2459 self.sig.to_tokens(tokens);
2460 self.block.brace_token.surround(tokens, |tokens| {
2461 tokens.append_all(self.attrs.inner());
2462 tokens.append_all(&self.block.stmts);
2463 });
2464 }
2465 }
2466
2467 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002468 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002469 tokens.append_all(self.attrs.outer());
2470 self.vis.to_tokens(tokens);
2471 self.defaultness.to_tokens(tokens);
2472 self.type_token.to_tokens(tokens);
2473 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002474 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002475 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002476 self.eq_token.to_tokens(tokens);
2477 self.ty.to_tokens(tokens);
2478 self.semi_token.to_tokens(tokens);
2479 }
2480 }
2481
David Tolnaybb82ef02018-08-24 20:15:45 -04002482 impl ToTokens for ImplItemExistential {
2483 fn to_tokens(&self, tokens: &mut TokenStream) {
2484 tokens.append_all(self.attrs.outer());
2485 self.existential_token.to_tokens(tokens);
2486 self.type_token.to_tokens(tokens);
2487 self.ident.to_tokens(tokens);
2488 self.generics.to_tokens(tokens);
2489 self.generics.where_clause.to_tokens(tokens);
2490 if !self.bounds.is_empty() {
2491 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2492 self.bounds.to_tokens(tokens);
2493 }
2494 self.semi_token.to_tokens(tokens);
2495 }
2496 }
2497
David Tolnay857628c2017-11-11 12:25:31 -08002498 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002499 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002500 tokens.append_all(self.attrs.outer());
2501 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002502 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002503 }
2504 }
2505
David Tolnay2ae520a2017-12-29 11:19:50 -05002506 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002507 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002508 self.tts.to_tokens(tokens);
2509 }
2510 }
2511
David Tolnay8894f602017-11-11 12:11:04 -08002512 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002513 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002514 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002515 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002516 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002517 self.semi_token.to_tokens(tokens);
2518 }
2519 }
2520
2521 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002522 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002523 tokens.append_all(self.attrs.outer());
2524 self.vis.to_tokens(tokens);
2525 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002526 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002527 self.ident.to_tokens(tokens);
2528 self.colon_token.to_tokens(tokens);
2529 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002530 self.semi_token.to_tokens(tokens);
2531 }
2532 }
2533
David Tolnay199bcbb2017-11-12 10:33:52 -08002534 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002535 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002536 tokens.append_all(self.attrs.outer());
2537 self.vis.to_tokens(tokens);
2538 self.type_token.to_tokens(tokens);
2539 self.ident.to_tokens(tokens);
2540 self.semi_token.to_tokens(tokens);
2541 }
2542 }
2543
David Tolnay435c1782018-08-24 16:15:44 -04002544 impl ToTokens for ForeignItemMacro {
2545 fn to_tokens(&self, tokens: &mut TokenStream) {
2546 tokens.append_all(self.attrs.outer());
2547 self.mac.to_tokens(tokens);
2548 self.semi_token.to_tokens(tokens);
2549 }
2550 }
2551
David Tolnay2ae520a2017-12-29 11:19:50 -05002552 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002553 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002554 self.tts.to_tokens(tokens);
2555 }
2556 }
2557
David Tolnay570695e2017-06-03 16:15:13 -07002558 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002559 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002560 self.constness.to_tokens(tokens);
2561 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002562 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002563 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002564 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002565 }
2566 }
2567
Alex Crichtona74a1c82018-05-16 10:20:44 -07002568 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002569
2570 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002571 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002572 self.0.fn_token.to_tokens(tokens);
2573 self.1.to_tokens(tokens);
2574 self.0.generics.to_tokens(tokens);
2575 self.0.paren_token.surround(tokens, |tokens| {
2576 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002577 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2578 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002579 }
David Tolnayd2836e22017-12-27 23:13:00 -05002580 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002581 });
2582 self.0.output.to_tokens(tokens);
2583 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002584 }
2585 }
2586
Alex Crichton62a0a592017-05-22 13:58:53 -07002587 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002588 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002589 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002590 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002591 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002592 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 }
2594 }
2595
2596 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002597 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002598 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002599 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002600 }
2601 }
2602
2603 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002604 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002605 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002606 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002607 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002608 }
2609 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002610}