David Tolnay | 4b4c4b6 | 2018-01-06 13:48:05 -0800 | [diff] [blame] | 1 | //! This crate automatically generates the definition of the `Visit`, |
| 2 | //! `VisitMut`, and `Fold` traits in `syn` based on the `syn` source. It |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 3 | //! discovers structs and enums declared with the `ast_*` macros and generates |
| 4 | //! the functions for those types. |
| 5 | //! |
| 6 | //! It makes a few assumptions about the target crate: |
| 7 | //! 1. All structs which are discovered must be re-exported in the root of the |
| 8 | //! crate, even if they were declared in a submodule. |
| 9 | //! 2. This code cannot discover submodules which are located in subdirectories |
| 10 | //! - only submodules located in the same directory. |
| 11 | //! 3. The path to `syn` is hardcoded. |
| 12 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 13 | #![recursion_limit = "128"] |
David Tolnay | c8d3e4a | 2018-11-21 01:36:58 -0800 | [diff] [blame^] | 14 | #![allow( |
| 15 | clippy::needless_pass_by_value, |
| 16 | clippy::redundant_closure, |
| 17 | clippy::write_with_newline, |
David Tolnay | 6b46a70 | 2018-08-01 23:51:32 -0700 | [diff] [blame] | 18 | )] |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 19 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 20 | #[macro_use] |
| 21 | extern crate failure; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 22 | extern crate inflections; |
David Tolnay | 2e0dba1 | 2017-12-27 01:54:40 -0500 | [diff] [blame] | 23 | extern crate proc_macro2; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 24 | #[macro_use] |
| 25 | extern crate quote; |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 26 | #[macro_use] |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 27 | extern crate syn; |
David Tolnay | 8c81f62 | 2018-07-31 23:34:35 -0700 | [diff] [blame] | 28 | extern crate rustfmt_nightly as rustfmt; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 29 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 30 | use failure::{err_msg, Error}; |
David Tolnay | e303b7c | 2018-05-20 16:46:35 -0700 | [diff] [blame] | 31 | use proc_macro2::{Span, TokenStream}; |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 32 | use quote::ToTokens; |
| 33 | use syn::{Attribute, Data, DataStruct, DeriveInput, Ident, Item}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 34 | |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 35 | use std::collections::BTreeMap; |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 36 | use std::fmt::{self, Debug}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 37 | use std::fs::File; |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 38 | use std::io::{Read, Write}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 39 | use std::path::Path; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 40 | |
| 41 | const SYN_CRATE_ROOT: &str = "../src/lib.rs"; |
| 42 | |
| 43 | const FOLD_SRC: &str = "../src/gen/fold.rs"; |
| 44 | const VISIT_SRC: &str = "../src/gen/visit.rs"; |
| 45 | const VISIT_MUT_SRC: &str = "../src/gen/visit_mut.rs"; |
| 46 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 47 | const IGNORED_MODS: &[&str] = &["fold", "visit", "visit_mut"]; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 48 | |
Alex Crichton | 131308c | 2018-05-18 14:00:24 -0700 | [diff] [blame] | 49 | const EXTRA_TYPES: &[&str] = &["Lifetime"]; |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 50 | |
Alex Crichton | d261d09 | 2018-05-18 13:47:35 -0700 | [diff] [blame] | 51 | const TERMINAL_TYPES: &[&str] = &["Span", "Ident"]; |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 52 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 53 | fn path_eq(a: &syn::Path, b: &str) -> bool { |
David Tolnay | 78b1614 | 2018-09-01 16:53:07 -0700 | [diff] [blame] | 54 | a.leading_colon.is_none() |
| 55 | && a.segments.len() == 1 |
| 56 | && a.segments[0].ident == b |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 59 | fn get_features(attrs: &[Attribute], mut features: TokenStream) -> TokenStream { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 60 | for attr in attrs { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 61 | if path_eq(&attr.path, "cfg") { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 62 | attr.to_tokens(&mut features); |
| 63 | } |
| 64 | } |
| 65 | features |
| 66 | } |
| 67 | |
| 68 | #[derive(Clone)] |
| 69 | pub struct AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 70 | ast: DeriveInput, |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 71 | features: TokenStream, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 72 | // True if this is an ast_enum_of_structs! item with a #full annotation. |
| 73 | eos_full: bool, |
| 74 | } |
| 75 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 76 | impl Debug for AstItem { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 77 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 78 | f.debug_struct("AstItem") |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 79 | .field("ast", &self.ast) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 80 | .field("features", &self.features.to_string()) |
| 81 | .finish() |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // NOTE: BTreeMap is used here instead of HashMap to have deterministic output. |
David Tolnay | c47f842 | 2017-12-28 17:30:46 -0500 | [diff] [blame] | 86 | type Lookup = BTreeMap<Ident, AstItem>; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 87 | |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 88 | fn load_file<P: AsRef<Path>>( |
| 89 | name: P, |
| 90 | features: &TokenStream, |
| 91 | lookup: &mut Lookup, |
| 92 | ) -> Result<(), Error> { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 93 | let name = name.as_ref(); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 94 | let parent = name.parent().ok_or_else(|| err_msg("no parent path"))?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 95 | |
| 96 | let mut f = File::open(name)?; |
| 97 | let mut src = String::new(); |
| 98 | f.read_to_string(&mut src)?; |
| 99 | |
| 100 | // Parse the file |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 101 | let file = |
| 102 | syn::parse_file(&src).map_err(|_| format_err!("failed to parse {}", name.display()))?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 103 | |
| 104 | // Collect all of the interesting AstItems declared in this file or submodules. |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 105 | 'items: for item in file.items { |
| 106 | match item { |
| 107 | Item::Mod(item) => { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 108 | // Don't inspect inline modules. |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 109 | if item.content.is_some() { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | |
| 113 | // We don't want to try to load the generated rust files and |
| 114 | // parse them, so we ignore them here. |
| 115 | for name in IGNORED_MODS { |
David Tolnay | 446f7d6 | 2018-05-20 17:58:15 -0700 | [diff] [blame] | 116 | if item.ident == name { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 117 | continue 'items; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Lookup any #[cfg()] attributes on the module and add them to |
| 122 | // the feature set. |
David Tolnay | 0a0d78c | 2018-01-05 15:24:01 -0800 | [diff] [blame] | 123 | // |
| 124 | // The derive module is weird because it is built with either |
| 125 | // `full` or `derive` but exported only under `derive`. |
David Tolnay | 446f7d6 | 2018-05-20 17:58:15 -0700 | [diff] [blame] | 126 | let features = if item.ident == "derive" { |
David Tolnay | 0a0d78c | 2018-01-05 15:24:01 -0800 | [diff] [blame] | 127 | quote!(#[cfg(feature = "derive")]) |
| 128 | } else { |
| 129 | get_features(&item.attrs, features.clone()) |
| 130 | }; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 131 | |
| 132 | // Look up the submodule file, and recursively parse it. |
| 133 | // XXX: Only handles same-directory .rs file submodules. |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 134 | let path = parent.join(&format!("{}.rs", item.ident)); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 135 | load_file(path, &features, lookup)?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 136 | } |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 137 | Item::Macro(item) => { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 138 | // Lookip any #[cfg()] attributes directly on the macro |
| 139 | // invocation, and add them to the feature set. |
| 140 | let features = get_features(&item.attrs, features.clone()); |
| 141 | |
| 142 | // Try to parse the AstItem declaration out of the item. |
David Tolnay | 01a7758 | 2018-01-01 20:00:51 -0800 | [diff] [blame] | 143 | let tts = &item.mac.tts; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 144 | let found = if path_eq(&item.mac.path, "ast_struct") { |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 145 | syn::parse2::<parsing::AstStruct>(quote!(#tts)) |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 146 | .map_err(|_| err_msg("failed to parse ast_struct"))? |
| 147 | .0 |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 148 | } else if path_eq(&item.mac.path, "ast_enum") { |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 149 | syn::parse2::<parsing::AstEnum>(quote!(#tts)) |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 150 | .map_err(|_| err_msg("failed to parse ast_enum"))? |
| 151 | .0 |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 152 | } else if path_eq(&item.mac.path, "ast_enum_of_structs") { |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 153 | syn::parse2::<parsing::AstEnumOfStructs>(quote!(#tts)) |
David Tolnay | 1cf8091 | 2017-12-31 18:35:12 -0500 | [diff] [blame] | 154 | .map_err(|_| err_msg("failed to parse ast_enum_of_structs"))? |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 155 | .0 |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 156 | } else { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 157 | continue; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | // Record our features on the parsed AstItems. |
| 161 | for mut item in found { |
| 162 | features.to_tokens(&mut item.features); |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 163 | lookup.insert(item.ast.ident.clone(), item); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 164 | } |
| 165 | } |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 166 | Item::Struct(item) => { |
| 167 | let ident = item.ident; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 168 | if EXTRA_TYPES.contains(&&ident.to_string()[..]) { |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 169 | lookup.insert( |
| 170 | ident.clone(), |
| 171 | AstItem { |
| 172 | ast: DeriveInput { |
David Tolnay | 6b46a70 | 2018-08-01 23:51:32 -0700 | [diff] [blame] | 173 | ident, |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 174 | vis: item.vis, |
| 175 | attrs: item.attrs, |
| 176 | generics: item.generics, |
| 177 | data: Data::Struct(DataStruct { |
| 178 | fields: item.fields, |
| 179 | struct_token: item.struct_token, |
| 180 | semi_token: item.semi_token, |
| 181 | }), |
| 182 | }, |
| 183 | features: features.clone(), |
| 184 | eos_full: false, |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 185 | }, |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 186 | ); |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 187 | } |
| 188 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 189 | _ => {} |
| 190 | } |
| 191 | } |
| 192 | Ok(()) |
| 193 | } |
| 194 | |
| 195 | mod parsing { |
| 196 | use super::AstItem; |
| 197 | |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 198 | use proc_macro2::TokenStream; |
David Tolnay | 1cf8091 | 2017-12-31 18:35:12 -0500 | [diff] [blame] | 199 | use syn; |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 200 | use syn::parse::{Parse, ParseStream, Result}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 201 | use syn::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 202 | |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 203 | fn peek_tag(input: ParseStream, tag: &str) -> bool { |
| 204 | let ahead = input.fork(); |
David Tolnay | 86cb945 | 2018-08-31 10:35:09 -0700 | [diff] [blame] | 205 | ahead.parse::<Token![#]>().is_ok() && ahead |
| 206 | .parse::<Ident>() |
| 207 | .map(|ident| ident == tag) |
| 208 | .unwrap_or(false) |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 211 | // Parses #full - returns #[cfg(feature = "full")] if it is present, and |
| 212 | // nothing otherwise. |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 213 | fn full(input: ParseStream) -> (TokenStream, bool) { |
| 214 | if peek_tag(input, "full") { |
| 215 | input.parse::<Token![#]>().unwrap(); |
| 216 | input.parse::<Ident>().unwrap(); |
| 217 | (quote!(#[cfg(feature = "full")]), true) |
| 218 | } else { |
| 219 | (quote!(), false) |
| 220 | } |
| 221 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 222 | |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 223 | fn skip_manual_extra_traits(input: ParseStream) { |
| 224 | if peek_tag(input, "manual_extra_traits") { |
| 225 | input.parse::<Token![#]>().unwrap(); |
| 226 | input.parse::<Ident>().unwrap(); |
| 227 | } |
| 228 | } |
David Tolnay | 28c5a46 | 2017-12-27 01:59:30 -0500 | [diff] [blame] | 229 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 230 | // Parses a simple AstStruct without the `pub struct` prefix. |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 231 | fn ast_struct_inner(input: ParseStream) -> Result<AstItem> { |
| 232 | let ident: Ident = input.parse()?; |
| 233 | let (features, eos_full) = full(input); |
| 234 | skip_manual_extra_traits(input); |
| 235 | let rest: TokenStream = input.parse()?; |
| 236 | Ok(AstItem { |
| 237 | ast: syn::parse2(quote! { |
| 238 | pub struct #ident #rest |
| 239 | })?, |
| 240 | features: features, |
| 241 | eos_full: eos_full, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 242 | }) |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 243 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 244 | |
| 245 | // ast_struct! parsing |
| 246 | pub struct AstStruct(pub Vec<AstItem>); |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 247 | impl Parse for AstStruct { |
| 248 | fn parse(input: ParseStream) -> Result<Self> { |
| 249 | input.call(Attribute::parse_outer)?; |
| 250 | input.parse::<Token![pub]>()?; |
| 251 | input.parse::<Token![struct]>()?; |
| 252 | let res = input.call(ast_struct_inner)?; |
| 253 | Ok(AstStruct(vec![res])) |
| 254 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 255 | } |
| 256 | |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 257 | fn no_visit(input: ParseStream) -> bool { |
| 258 | if peek_tag(input, "no_visit") { |
| 259 | input.parse::<Token![#]>().unwrap(); |
| 260 | input.parse::<Ident>().unwrap(); |
| 261 | true |
| 262 | } else { |
| 263 | false |
| 264 | } |
| 265 | } |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 266 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 267 | // ast_enum! parsing |
| 268 | pub struct AstEnum(pub Vec<AstItem>); |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 269 | impl Parse for AstEnum { |
| 270 | fn parse(input: ParseStream) -> Result<Self> { |
| 271 | input.call(Attribute::parse_outer)?; |
| 272 | input.parse::<Token![pub]>()?; |
| 273 | input.parse::<Token![enum]>()?; |
| 274 | let ident: Ident = input.parse()?; |
| 275 | let no_visit = no_visit(input); |
| 276 | let rest: TokenStream = input.parse()?; |
| 277 | Ok(AstEnum(if no_visit { |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 278 | vec![] |
| 279 | } else { |
| 280 | vec![AstItem { |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 281 | ast: syn::parse2(quote! { |
| 282 | pub enum #ident #rest |
| 283 | })?, |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 284 | features: quote!(), |
| 285 | eos_full: false, |
| 286 | }] |
| 287 | })) |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 288 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // A single variant of an ast_enum_of_structs! |
| 292 | struct EosVariant { |
| 293 | name: Ident, |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 294 | member: Option<Path>, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 295 | inner: Option<AstItem>, |
| 296 | } |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 297 | fn eos_variant(input: ParseStream) -> Result<EosVariant> { |
| 298 | input.call(Attribute::parse_outer)?; |
| 299 | input.parse::<Token![pub]>()?; |
| 300 | let variant: Ident = input.parse()?; |
| 301 | let (member, inner) = if input.peek(token::Paren) { |
| 302 | let content; |
| 303 | parenthesized!(content in input); |
| 304 | if content.fork().call(ast_struct_inner).is_ok() { |
| 305 | let item = content.call(ast_struct_inner)?; |
| 306 | (Some(Path::from(item.ast.ident.clone())), Some(item)) |
| 307 | } else { |
| 308 | let path: Path = content.parse()?; |
| 309 | (Some(path), None) |
| 310 | } |
| 311 | } else { |
| 312 | (None, None) |
| 313 | }; |
| 314 | input.parse::<Token![,]>()?; |
| 315 | Ok(EosVariant { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 316 | name: variant, |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 317 | member: member, |
| 318 | inner: inner, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 319 | }) |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 320 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 321 | |
| 322 | // ast_enum_of_structs! parsing |
| 323 | pub struct AstEnumOfStructs(pub Vec<AstItem>); |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 324 | impl Parse for AstEnumOfStructs { |
| 325 | fn parse(input: ParseStream) -> Result<Self> { |
| 326 | input.call(Attribute::parse_outer)?; |
| 327 | input.parse::<Token![pub]>()?; |
| 328 | input.parse::<Token![enum]>()?; |
| 329 | let ident: Ident = input.parse()?; |
| 330 | |
| 331 | let content; |
| 332 | braced!(content in input); |
| 333 | let mut variants = Vec::new(); |
| 334 | while !content.is_empty() { |
| 335 | variants.push(content.call(eos_variant)?); |
| 336 | } |
| 337 | |
| 338 | if let Some(ident) = input.parse::<Option<Ident>>()? { |
| 339 | assert_eq!(ident, "do_not_generate_to_tokens"); |
| 340 | } |
| 341 | |
| 342 | let enum_item = { |
| 343 | let variants = variants.iter().map(|v| { |
| 344 | let name = v.name.clone(); |
| 345 | match v.member { |
| 346 | Some(ref member) => quote!(#name(#member)), |
| 347 | None => quote!(#name), |
David Tolnay | b7ccc4f | 2018-08-02 00:41:32 -0700 | [diff] [blame] | 348 | } |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 349 | }); |
| 350 | parse_quote! { |
| 351 | pub enum #ident { |
| 352 | #(#variants),* |
| 353 | } |
| 354 | } |
| 355 | }; |
| 356 | let mut items = vec![AstItem { |
| 357 | ast: enum_item, |
| 358 | features: quote!(), |
David Tolnay | 86cb945 | 2018-08-31 10:35:09 -0700 | [diff] [blame] | 359 | eos_full: false, |
David Tolnay | 64f0384 | 2018-08-30 21:34:40 -0700 | [diff] [blame] | 360 | }]; |
| 361 | items.extend(variants.into_iter().filter_map(|v| v.inner)); |
| 362 | Ok(AstEnumOfStructs(items)) |
| 363 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | mod codegen { |
| 368 | use super::{AstItem, Lookup}; |
David Tolnay | 6b46a70 | 2018-08-01 23:51:32 -0700 | [diff] [blame] | 369 | use inflections::Inflect; |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 370 | use proc_macro2::{Span, TokenStream}; |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 371 | use quote::{ToTokens, TokenStreamExt}; |
David Tolnay | 86cb945 | 2018-08-31 10:35:09 -0700 | [diff] [blame] | 372 | use syn::ext::IdentExt; |
| 373 | use syn::parse::Parser; |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 374 | use syn::punctuated::Punctuated; |
| 375 | use syn::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 376 | |
| 377 | #[derive(Default)] |
| 378 | pub struct State { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 379 | pub visit_trait: TokenStream, |
| 380 | pub visit_impl: TokenStream, |
| 381 | pub visit_mut_trait: TokenStream, |
| 382 | pub visit_mut_impl: TokenStream, |
| 383 | pub fold_trait: TokenStream, |
| 384 | pub fold_impl: TokenStream, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 385 | } |
| 386 | |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 387 | fn under_name(name: Ident) -> Ident { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 388 | Ident::new(&name.to_string().to_snake_case(), Span::call_site()) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 389 | } |
| 390 | |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 391 | enum RelevantType<'a> { |
| 392 | Box(&'a Type), |
| 393 | Vec(&'a Type), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 394 | Punctuated(&'a Type), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 395 | Option(&'a Type), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 396 | Tuple(&'a Punctuated<Type, Token![,]>), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 397 | Simple(&'a AstItem), |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 398 | TokenPunct(TokenStream), |
| 399 | TokenKeyword(TokenStream), |
| 400 | TokenGroup(Ident), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 401 | Pass, |
| 402 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 403 | |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 404 | fn classify<'a>(ty: &'a Type, lookup: &'a Lookup) -> RelevantType<'a> { |
| 405 | match *ty { |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 406 | Type::Path(TypePath { |
| 407 | qself: None, |
| 408 | ref path, |
| 409 | }) => { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 410 | let last = path.segments.last().unwrap().into_value(); |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 411 | match &last.ident.to_string()[..] { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 412 | "Box" => RelevantType::Box(first_arg(&last.arguments)), |
| 413 | "Vec" => RelevantType::Vec(first_arg(&last.arguments)), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 414 | "Punctuated" => RelevantType::Punctuated(first_arg(&last.arguments)), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 415 | "Option" => RelevantType::Option(first_arg(&last.arguments)), |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 416 | "Brace" | "Bracket" | "Paren" | "Group" => { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 417 | RelevantType::TokenGroup(last.ident.clone()) |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 418 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 419 | _ => { |
| 420 | if let Some(item) = lookup.get(&last.ident) { |
| 421 | RelevantType::Simple(item) |
| 422 | } else { |
| 423 | RelevantType::Pass |
| 424 | } |
| 425 | } |
| 426 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 427 | } |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 428 | Type::Tuple(TypeTuple { ref elems, .. }) => RelevantType::Tuple(elems), |
| 429 | Type::Macro(TypeMacro { ref mac }) |
| 430 | if mac.path.segments.last().unwrap().into_value().ident == "Token" => |
| 431 | { |
David Tolnay | 86cb945 | 2018-08-31 10:35:09 -0700 | [diff] [blame] | 432 | let is_ident = Ident::parse_any.parse2(mac.tts.clone()).is_ok(); |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 433 | let is_underscore = parse2::<Token![_]>(mac.tts.clone()).is_ok(); |
| 434 | if is_ident && !is_underscore { |
| 435 | RelevantType::TokenKeyword(mac.into_token_stream()) |
| 436 | } else { |
| 437 | RelevantType::TokenPunct(mac.into_token_stream()) |
| 438 | } |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 439 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 440 | _ => RelevantType::Pass, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | #[derive(Debug, Eq, PartialEq, Copy, Clone)] |
| 445 | enum Kind { |
| 446 | Visit, |
| 447 | VisitMut, |
| 448 | Fold, |
| 449 | } |
| 450 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 451 | enum Operand { |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 452 | Borrowed(TokenStream), |
| 453 | Owned(TokenStream), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 454 | } |
| 455 | |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 456 | use self::Kind::*; |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 457 | use self::Operand::*; |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 458 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 459 | impl Operand { |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 460 | fn tokens(&self) -> &TokenStream { |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 461 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 462 | Borrowed(ref n) | Owned(ref n) => n, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 466 | fn ref_tokens(&self) -> TokenStream { |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 467 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 468 | Borrowed(ref n) => n.clone(), |
| 469 | Owned(ref n) => quote!(&#n), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 473 | fn ref_mut_tokens(&self) -> TokenStream { |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 474 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 475 | Borrowed(ref n) => n.clone(), |
| 476 | Owned(ref n) => quote!(&mut #n), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Alex Crichton | 715862b | 2018-05-17 12:31:49 -0700 | [diff] [blame] | 480 | fn owned_tokens(&self) -> TokenStream { |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 481 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 482 | Borrowed(ref n) => quote!(*#n), |
| 483 | Owned(ref n) => n.clone(), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 488 | fn first_arg(params: &PathArguments) -> &Type { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 489 | let data = match *params { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 490 | PathArguments::AngleBracketed(ref data) => data, |
| 491 | _ => panic!("Expected at least 1 type argument here"), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 492 | }; |
| 493 | |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 494 | match **data |
| 495 | .args |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 496 | .first() |
| 497 | .expect("Expected at least 1 type argument here") |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 498 | .value() |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 499 | { |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 500 | GenericArgument::Type(ref ty) => ty, |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 501 | _ => panic!("Expected at least 1 type argument here"), |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 502 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 503 | } |
| 504 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 505 | fn simple_visit(item: &AstItem, kind: Kind, name: &Operand) -> TokenStream { |
| 506 | let ident = under_name(item.ast.ident.clone()); |
| 507 | |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 508 | match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 509 | Visit => { |
| 510 | let method = Ident::new(&format!("visit_{}", ident), Span::call_site()); |
| 511 | let name = name.ref_tokens(); |
| 512 | quote! { |
| 513 | _visitor.#method(#name) |
| 514 | } |
| 515 | } |
| 516 | VisitMut => { |
| 517 | let method = Ident::new(&format!("visit_{}_mut", ident), Span::call_site()); |
| 518 | let name = name.ref_mut_tokens(); |
| 519 | quote! { |
| 520 | _visitor.#method(#name) |
| 521 | } |
| 522 | } |
| 523 | Fold => { |
| 524 | let method = Ident::new(&format!("fold_{}", ident), Span::call_site()); |
| 525 | let name = name.owned_tokens(); |
| 526 | quote! { |
| 527 | _visitor.#method(#name) |
| 528 | } |
| 529 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 533 | fn box_visit(elem: &Type, lookup: &Lookup, kind: Kind, name: &Operand) -> Option<TokenStream> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 534 | let name = name.owned_tokens(); |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 535 | let res = visit(elem, lookup, kind, &Owned(quote!(*#name)))?; |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 536 | Some(match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 537 | Fold => quote! { |
| 538 | Box::new(#res) |
| 539 | }, |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 540 | Visit | VisitMut => res, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 541 | }) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 542 | } |
| 543 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 544 | fn vec_visit(elem: &Type, lookup: &Lookup, kind: Kind, name: &Operand) -> Option<TokenStream> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 545 | let operand = match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 546 | Visit | VisitMut => Borrowed(quote!(it)), |
| 547 | Fold => Owned(quote!(it)), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 548 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 549 | let val = visit(elem, lookup, kind, &operand)?; |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 550 | Some(match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 551 | Visit => { |
| 552 | let name = name.ref_tokens(); |
| 553 | quote! { |
| 554 | for it in #name { |
| 555 | #val |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | VisitMut => { |
| 560 | let name = name.ref_mut_tokens(); |
| 561 | quote! { |
| 562 | for it in #name { |
| 563 | #val |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | Fold => { |
| 568 | let name = name.owned_tokens(); |
| 569 | quote! { |
| 570 | FoldHelper::lift(#name, |it| { #val }) |
| 571 | } |
| 572 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 573 | }) |
| 574 | } |
| 575 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 576 | fn punctuated_visit( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 577 | elem: &Type, |
| 578 | lookup: &Lookup, |
| 579 | kind: Kind, |
| 580 | name: &Operand, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 581 | ) -> Option<TokenStream> { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 582 | let operand = match kind { |
| 583 | Visit | VisitMut => Borrowed(quote!(it)), |
| 584 | Fold => Owned(quote!(it)), |
| 585 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 586 | let val = visit(elem, lookup, kind, &operand)?; |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 587 | Some(match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 588 | Visit => { |
| 589 | let name = name.ref_tokens(); |
| 590 | quote! { |
| 591 | for el in Punctuated::pairs(#name) { |
| 592 | let it = el.value(); |
| 593 | #val |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | VisitMut => { |
| 598 | let name = name.ref_mut_tokens(); |
| 599 | quote! { |
| 600 | for mut el in Punctuated::pairs_mut(#name) { |
| 601 | let it = el.value_mut(); |
| 602 | #val |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | Fold => { |
| 607 | let name = name.owned_tokens(); |
| 608 | quote! { |
| 609 | FoldHelper::lift(#name, |it| { #val }) |
| 610 | } |
| 611 | } |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 612 | }) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 613 | } |
| 614 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 615 | fn option_visit( |
| 616 | elem: &Type, |
| 617 | lookup: &Lookup, |
| 618 | kind: Kind, |
| 619 | name: &Operand, |
| 620 | ) -> Option<TokenStream> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 621 | let it = match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 622 | Visit | VisitMut => Borrowed(quote!(it)), |
| 623 | Fold => Owned(quote!(it)), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 624 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 625 | let val = visit(elem, lookup, kind, &it)?; |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 626 | let name = name.owned_tokens(); |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 627 | Some(match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 628 | Visit => quote! { |
| 629 | if let Some(ref it) = #name { |
| 630 | #val |
| 631 | } |
| 632 | }, |
| 633 | VisitMut => quote! { |
| 634 | if let Some(ref mut it) = #name { |
| 635 | #val |
| 636 | } |
| 637 | }, |
| 638 | Fold => quote! { |
| 639 | (#name).map(|it| { #val }) |
| 640 | }, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 641 | }) |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 642 | } |
| 643 | |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 644 | fn tuple_visit( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 645 | elems: &Punctuated<Type, Token![,]>, |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 646 | lookup: &Lookup, |
| 647 | kind: Kind, |
| 648 | name: &Operand, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 649 | ) -> Option<TokenStream> { |
| 650 | if elems.is_empty() { |
| 651 | return None; |
| 652 | } |
| 653 | |
| 654 | let mut code = TokenStream::new(); |
David Tolnay | f047c7a | 2017-12-31 02:29:04 -0500 | [diff] [blame] | 655 | for (i, elem) in elems.iter().enumerate() { |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 656 | let name = name.tokens(); |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 657 | let i = Index::from(i); |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 658 | let it = Owned(quote!((#name).#i)); |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 659 | let val = visit(elem, lookup, kind, &it).unwrap_or_else(|| noop_visit(kind, &it)); |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 660 | code.append_all(val); |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 661 | match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 662 | Fold => code.append_all(quote!(,)), |
| 663 | Visit | VisitMut => code.append_all(quote!(;)), |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 664 | } |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 665 | } |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 666 | Some(match kind { |
| 667 | Fold => quote! { |
| 668 | (#code) |
| 669 | }, |
| 670 | Visit | VisitMut => code, |
| 671 | }) |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 672 | } |
| 673 | |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 674 | fn token_punct_visit(ty: TokenStream, kind: Kind, name: &Operand) -> TokenStream { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 675 | let name = name.tokens(); |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 676 | match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 677 | Fold => quote! { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 678 | #ty(tokens_helper(_visitor, &#name.spans)) |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 679 | }, |
| 680 | Visit => quote! { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 681 | tokens_helper(_visitor, &#name.spans) |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 682 | }, |
| 683 | VisitMut => quote! { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 684 | tokens_helper(_visitor, &mut #name.spans) |
| 685 | }, |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | fn token_keyword_visit(ty: TokenStream, kind: Kind, name: &Operand) -> TokenStream { |
| 690 | let name = name.tokens(); |
| 691 | match kind { |
| 692 | Fold => quote! { |
| 693 | #ty(tokens_helper(_visitor, &#name.span)) |
| 694 | }, |
| 695 | Visit => quote! { |
| 696 | tokens_helper(_visitor, &#name.span) |
| 697 | }, |
| 698 | VisitMut => quote! { |
| 699 | tokens_helper(_visitor, &mut #name.span) |
| 700 | }, |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | fn token_group_visit(ty: Ident, kind: Kind, name: &Operand) -> TokenStream { |
| 705 | let name = name.tokens(); |
| 706 | match kind { |
| 707 | Fold => quote! { |
| 708 | #ty(tokens_helper(_visitor, &#name.span)) |
| 709 | }, |
| 710 | Visit => quote! { |
| 711 | tokens_helper(_visitor, &#name.span) |
| 712 | }, |
| 713 | VisitMut => quote! { |
| 714 | tokens_helper(_visitor, &mut #name.span) |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 715 | }, |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 719 | fn noop_visit(kind: Kind, name: &Operand) -> TokenStream { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 720 | match kind { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 721 | Fold => name.owned_tokens(), |
| 722 | Visit | VisitMut => { |
| 723 | let name = name.tokens(); |
| 724 | quote! { |
| 725 | skip!(#name) |
| 726 | } |
| 727 | } |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 731 | fn visit(ty: &Type, lookup: &Lookup, kind: Kind, name: &Operand) -> Option<TokenStream> { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 732 | match classify(ty, lookup) { |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 733 | RelevantType::Box(elem) => box_visit(elem, lookup, kind, name), |
| 734 | RelevantType::Vec(elem) => vec_visit(elem, lookup, kind, name), |
| 735 | RelevantType::Punctuated(elem) => punctuated_visit(elem, lookup, kind, name), |
| 736 | RelevantType::Option(elem) => option_visit(elem, lookup, kind, name), |
| 737 | RelevantType::Tuple(elems) => tuple_visit(elems, lookup, kind, name), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 738 | RelevantType::Simple(item) => { |
| 739 | let mut res = simple_visit(item, kind, name); |
| 740 | Some(if item.eos_full { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 741 | quote! { |
| 742 | full!(#res) |
| 743 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 744 | } else { |
| 745 | res |
| 746 | }) |
| 747 | } |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 748 | RelevantType::TokenPunct(ty) => Some(token_punct_visit(ty, kind, name)), |
| 749 | RelevantType::TokenKeyword(ty) => Some(token_keyword_visit(ty, kind, name)), |
| 750 | RelevantType::TokenGroup(ty) => Some(token_group_visit(ty, kind, name)), |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 751 | RelevantType::Pass => None, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 752 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | pub fn generate(state: &mut State, lookup: &Lookup, s: &AstItem) { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 756 | let features = &s.features; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 757 | let under_name = under_name(s.ast.ident.clone()); |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 758 | let ty = &s.ast.ident; |
| 759 | let visit_fn = Ident::new(&format!("visit_{}", under_name), Span::call_site()); |
| 760 | let visit_mut_fn = Ident::new(&format!("visit_{}_mut", under_name), Span::call_site()); |
| 761 | let fold_fn = Ident::new(&format!("fold_{}", under_name), Span::call_site()); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 762 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 763 | let mut visit_impl = TokenStream::new(); |
| 764 | let mut visit_mut_impl = TokenStream::new(); |
| 765 | let mut fold_impl = TokenStream::new(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 766 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 767 | match s.ast.data { |
| 768 | Data::Enum(ref e) => { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 769 | let mut visit_variants = TokenStream::new(); |
| 770 | let mut visit_mut_variants = TokenStream::new(); |
| 771 | let mut fold_variants = TokenStream::new(); |
| 772 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 773 | for variant in &e.variants { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 774 | let variant_ident = &variant.ident; |
| 775 | |
| 776 | match variant.fields { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 777 | Fields::Named(..) => panic!("Doesn't support enum struct variants"), |
| 778 | Fields::Unnamed(ref fields) => { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 779 | let mut bind_visit_fields = TokenStream::new(); |
| 780 | let mut bind_visit_mut_fields = TokenStream::new(); |
| 781 | let mut bind_fold_fields = TokenStream::new(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 782 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 783 | let mut visit_fields = TokenStream::new(); |
| 784 | let mut visit_mut_fields = TokenStream::new(); |
| 785 | let mut fold_fields = TokenStream::new(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 786 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 787 | for (idx, field) in fields.unnamed.iter().enumerate() { |
| 788 | let name = format!("_binding_{}", idx); |
| 789 | let binding = Ident::new(&name, Span::call_site()); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 790 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 791 | bind_visit_fields.append_all(quote! { |
| 792 | ref #binding, |
| 793 | }); |
| 794 | bind_visit_mut_fields.append_all(quote! { |
| 795 | ref mut #binding, |
| 796 | }); |
| 797 | bind_fold_fields.append_all(quote! { |
| 798 | #binding, |
| 799 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 800 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 801 | let borrowed_binding = Borrowed(quote!(#binding)); |
| 802 | let owned_binding = Owned(quote!(#binding)); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 803 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 804 | visit_fields.append_all( |
| 805 | visit(&field.ty, lookup, Visit, &borrowed_binding) |
| 806 | .unwrap_or_else(|| noop_visit(Visit, &borrowed_binding)), |
| 807 | ); |
| 808 | visit_mut_fields.append_all( |
| 809 | visit(&field.ty, lookup, VisitMut, &borrowed_binding) |
| 810 | .unwrap_or_else(|| noop_visit(VisitMut, &borrowed_binding)), |
| 811 | ); |
| 812 | fold_fields.append_all( |
| 813 | visit(&field.ty, lookup, Fold, &owned_binding) |
| 814 | .unwrap_or_else(|| noop_visit(Fold, &owned_binding)), |
| 815 | ); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 816 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 817 | visit_fields.append_all(quote!(;)); |
| 818 | visit_mut_fields.append_all(quote!(;)); |
| 819 | fold_fields.append_all(quote!(,)); |
| 820 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 821 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 822 | visit_variants.append_all(quote! { |
| 823 | #ty::#variant_ident(#bind_visit_fields) => { |
| 824 | #visit_fields |
| 825 | } |
| 826 | }); |
| 827 | |
| 828 | visit_mut_variants.append_all(quote! { |
| 829 | #ty::#variant_ident(#bind_visit_mut_fields) => { |
| 830 | #visit_mut_fields |
| 831 | } |
| 832 | }); |
| 833 | |
| 834 | fold_variants.append_all(quote! { |
| 835 | #ty::#variant_ident(#bind_fold_fields) => { |
| 836 | #ty::#variant_ident( |
| 837 | #fold_fields |
| 838 | ) |
| 839 | } |
| 840 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 841 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 842 | Fields::Unit => { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 843 | visit_variants.append_all(quote! { |
| 844 | #ty::#variant_ident => {} |
| 845 | }); |
| 846 | visit_mut_variants.append_all(quote! { |
| 847 | #ty::#variant_ident => {} |
| 848 | }); |
| 849 | fold_variants.append_all(quote! { |
| 850 | #ty::#variant_ident => { |
| 851 | #ty::#variant_ident |
| 852 | } |
| 853 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 854 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 855 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 856 | } |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 857 | |
| 858 | visit_impl.append_all(quote! { |
| 859 | match *_i { |
| 860 | #visit_variants |
| 861 | } |
| 862 | }); |
| 863 | |
| 864 | visit_mut_impl.append_all(quote! { |
| 865 | match *_i { |
| 866 | #visit_mut_variants |
| 867 | } |
| 868 | }); |
| 869 | |
| 870 | fold_impl.append_all(quote! { |
| 871 | match _i { |
| 872 | #fold_variants |
| 873 | } |
| 874 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 875 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 876 | Data::Struct(ref v) => { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 877 | let mut fold_fields = TokenStream::new(); |
| 878 | |
David Tolnay | bc5b2c0 | 2018-08-02 00:18:23 -0700 | [diff] [blame] | 879 | for (idx, field) in v.fields.iter().enumerate() { |
| 880 | let id = match field.ident { |
| 881 | Some(ref ident) => Member::Named(ident.clone()), |
| 882 | None => Member::Unnamed(Index::from(idx)), |
| 883 | }; |
| 884 | let ref_toks = Owned(quote!(_i.#id)); |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 885 | let visit_field = visit(&field.ty, lookup, Visit, &ref_toks) |
| 886 | .unwrap_or_else(|| noop_visit(Visit, &ref_toks)); |
| 887 | visit_impl.append_all(quote! { |
| 888 | #visit_field; |
| 889 | }); |
| 890 | let visit_mut_field = visit(&field.ty, lookup, VisitMut, &ref_toks) |
| 891 | .unwrap_or_else(|| noop_visit(VisitMut, &ref_toks)); |
| 892 | visit_mut_impl.append_all(quote! { |
| 893 | #visit_mut_field; |
| 894 | }); |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 895 | let fold = visit(&field.ty, lookup, Fold, &ref_toks) |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 896 | .unwrap_or_else(|| noop_visit(Fold, &ref_toks)); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 897 | if let Some(ref name) = field.ident { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 898 | fold_fields.append_all(quote! { |
| 899 | #name: #fold, |
| 900 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 901 | } else { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 902 | fold_fields.append_all(quote! { |
| 903 | #fold, |
| 904 | }); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 908 | match v.fields { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 909 | Fields::Named(..) | Fields::Unnamed(..) => fold_impl.append_all(quote! { |
| 910 | #ty { |
| 911 | #fold_fields |
| 912 | } |
| 913 | }), |
| 914 | Fields::Unit => { |
| 915 | if ty == "Ident" { |
| 916 | fold_impl.append_all(quote! { |
| 917 | let mut _i = _i; |
| 918 | let span = _visitor.fold_span(_i.span()); |
| 919 | _i.set_span(span); |
| 920 | }); |
| 921 | } |
| 922 | fold_impl.append_all(quote! { |
| 923 | _i |
| 924 | }); |
| 925 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 926 | }; |
| 927 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 928 | Data::Union(..) => panic!("Union not supported"), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 929 | } |
| 930 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 931 | let mut include_fold_impl = true; |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 932 | if let Data::Struct(ref data) = s.ast.data { |
| 933 | if let Fields::Named(ref fields) = data.fields { |
David Tolnay | 01ed0ce | 2018-05-20 20:18:14 -0700 | [diff] [blame] | 934 | if fields |
| 935 | .named |
| 936 | .iter() |
| 937 | .any(|field| field.vis == Visibility::Inherited) |
| 938 | { |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 939 | // Discard the generated impl if there are private fields. |
| 940 | // These have to be handwritten. |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 941 | include_fold_impl = false; |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
David Tolnay | d0adf52 | 2017-12-29 01:30:07 -0500 | [diff] [blame] | 944 | } |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 945 | |
| 946 | state.visit_trait.append_all(quote! { |
| 947 | #features |
| 948 | fn #visit_fn(&mut self, i: &'ast #ty) { |
| 949 | #visit_fn(self, i) |
| 950 | } |
| 951 | }); |
| 952 | |
| 953 | state.visit_impl.append_all(quote! { |
| 954 | #features |
| 955 | pub fn #visit_fn<'ast, V: Visit<'ast> + ?Sized>( |
| 956 | _visitor: &mut V, _i: &'ast #ty |
| 957 | ) { |
| 958 | #visit_impl |
| 959 | } |
| 960 | }); |
| 961 | |
| 962 | state.visit_mut_trait.append_all(quote! { |
| 963 | #features |
| 964 | fn #visit_mut_fn(&mut self, i: &mut #ty) { |
| 965 | #visit_mut_fn(self, i) |
| 966 | } |
| 967 | }); |
| 968 | |
| 969 | state.visit_mut_impl.append_all(quote! { |
| 970 | #features |
| 971 | pub fn #visit_mut_fn<V: VisitMut + ?Sized>( |
| 972 | _visitor: &mut V, _i: &mut #ty |
| 973 | ) { |
| 974 | #visit_mut_impl |
| 975 | } |
| 976 | }); |
| 977 | |
| 978 | state.fold_trait.append_all(quote! { |
| 979 | #features |
| 980 | fn #fold_fn(&mut self, i: #ty) -> #ty { |
| 981 | #fold_fn(self, i) |
| 982 | } |
| 983 | }); |
| 984 | |
| 985 | if include_fold_impl { |
| 986 | state.fold_impl.append_all(quote! { |
| 987 | #features |
| 988 | pub fn #fold_fn<V: Fold + ?Sized>( |
| 989 | _visitor: &mut V, _i: #ty |
| 990 | ) -> #ty { |
| 991 | #fold_impl |
| 992 | } |
| 993 | }); |
| 994 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 998 | fn write_file(path: &str, content: TokenStream) { |
David Tolnay | 8c81f62 | 2018-07-31 23:34:35 -0700 | [diff] [blame] | 999 | let mut file = File::create(path).unwrap(); |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1000 | write!( |
| 1001 | file, |
| 1002 | "// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT\n\n" |
| 1003 | ).unwrap(); |
David Tolnay | 8c81f62 | 2018-07-31 23:34:35 -0700 | [diff] [blame] | 1004 | let mut config = rustfmt::Config::default(); |
| 1005 | config.set().emit_mode(rustfmt::EmitMode::Stdout); |
| 1006 | config.set().verbose(rustfmt::Verbosity::Quiet); |
David Tolnay | 280202f | 2018-08-02 00:29:54 -0700 | [diff] [blame] | 1007 | config.set().format_macro_matchers(true); |
David Tolnay | 8c81f62 | 2018-07-31 23:34:35 -0700 | [diff] [blame] | 1008 | let mut session = rustfmt::Session::new(config, Some(&mut file)); |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1009 | session |
| 1010 | .format(rustfmt::Input::Text(content.to_string())) |
| 1011 | .unwrap(); |
David Tolnay | 8c81f62 | 2018-07-31 23:34:35 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1014 | fn main() { |
| 1015 | let mut lookup = BTreeMap::new(); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 1016 | load_file(SYN_CRATE_ROOT, "e!(), &mut lookup).unwrap(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1017 | |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 1018 | // Load in any terminal types |
| 1019 | for &tt in TERMINAL_TYPES { |
| 1020 | use syn::*; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1021 | lookup.insert( |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1022 | Ident::new(&tt, Span::call_site()), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1023 | AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 1024 | ast: DeriveInput { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1025 | ident: Ident::new(tt, Span::call_site()), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1026 | vis: Visibility::Public(VisPublic { |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1027 | pub_token: <Token![pub]>::default(), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1028 | }), |
| 1029 | attrs: vec![], |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1030 | generics: Generics::default(), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 1031 | data: Data::Struct(DataStruct { |
| 1032 | fields: Fields::Unit, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1033 | struct_token: <Token![struct]>::default(), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1034 | semi_token: None, |
| 1035 | }), |
| 1036 | }, |
hcpl | aa51179 | 2018-05-29 07:13:01 +0300 | [diff] [blame] | 1037 | features: TokenStream::new(), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1038 | eos_full: false, |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 1039 | }, |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1040 | ); |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1043 | let mut state = codegen::State::default(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1044 | for s in lookup.values() { |
| 1045 | codegen::generate(&mut state, &lookup, s); |
| 1046 | } |
| 1047 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1048 | let full_macro = quote! { |
| 1049 | #[cfg(feature = "full")] |
| 1050 | macro_rules! full { |
| 1051 | ($e:expr) => { |
| 1052 | $e |
| 1053 | }; |
| 1054 | } |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1055 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1056 | #[cfg(all(feature = "derive", not(feature = "full")))] |
| 1057 | macro_rules! full { |
| 1058 | ($e:expr) => { |
| 1059 | unreachable!() |
| 1060 | }; |
| 1061 | } |
| 1062 | }; |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1063 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1064 | let skip_macro = quote! { |
| 1065 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1066 | macro_rules! skip { |
| 1067 | ($($tt:tt)*) => {}; |
| 1068 | } |
| 1069 | }; |
| 1070 | |
| 1071 | let fold_trait = state.fold_trait; |
| 1072 | let fold_impl = state.fold_impl; |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1073 | write_file( |
| 1074 | FOLD_SRC, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1075 | quote! { |
| 1076 | // Unreachable code is generated sometimes without the full feature. |
| 1077 | #![allow(unreachable_code)] |
| 1078 | #![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1079 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1080 | use *; |
| 1081 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1082 | use token::{Brace, Bracket, Paren, Group}; |
| 1083 | use proc_macro2::Span; |
| 1084 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1085 | use gen::helper::fold::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1086 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1087 | #full_macro |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1088 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1089 | /// Syntax tree traversal to transform the nodes of an owned syntax tree. |
| 1090 | /// |
| 1091 | /// See the [module documentation] for details. |
| 1092 | /// |
| 1093 | /// [module documentation]: index.html |
| 1094 | /// |
| 1095 | /// *This trait is available if Syn is built with the `"fold"` feature.* |
| 1096 | pub trait Fold { |
| 1097 | #fold_trait |
| 1098 | } |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1099 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1100 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1101 | macro_rules! fold_span_only { |
| 1102 | ($f:ident : $t:ident) => { |
| 1103 | pub fn $f<V: Fold + ?Sized>(_visitor: &mut V, mut _i: $t) -> $t { |
| 1104 | let span = _visitor.fold_span(_i.span()); |
| 1105 | _i.set_span(span); |
| 1106 | _i |
| 1107 | } |
| 1108 | } |
| 1109 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1110 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1111 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1112 | fold_span_only!(fold_lit_byte: LitByte); |
| 1113 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1114 | fold_span_only!(fold_lit_byte_str: LitByteStr); |
| 1115 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1116 | fold_span_only!(fold_lit_char: LitChar); |
| 1117 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1118 | fold_span_only!(fold_lit_float: LitFloat); |
| 1119 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1120 | fold_span_only!(fold_lit_int: LitInt); |
| 1121 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1122 | fold_span_only!(fold_lit_str: LitStr); |
David Tolnay | d0adf52 | 2017-12-29 01:30:07 -0500 | [diff] [blame] | 1123 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1124 | #fold_impl |
| 1125 | }, |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1126 | ); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1127 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1128 | let visit_trait = state.visit_trait; |
| 1129 | let visit_impl = state.visit_impl; |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1130 | write_file( |
| 1131 | VISIT_SRC, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1132 | quote! { |
| 1133 | #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1134 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1135 | use *; |
| 1136 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1137 | use punctuated::Punctuated; |
| 1138 | use proc_macro2::Span; |
| 1139 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1140 | use gen::helper::visit::*; |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 1141 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1142 | #full_macro |
| 1143 | #skip_macro |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1144 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1145 | /// Syntax tree traversal to walk a shared borrow of a syntax tree. |
| 1146 | /// |
| 1147 | /// See the [module documentation] for details. |
| 1148 | /// |
| 1149 | /// [module documentation]: index.html |
| 1150 | /// |
| 1151 | /// *This trait is available if Syn is built with the `"visit"` feature.* |
| 1152 | pub trait Visit<'ast> { |
| 1153 | #visit_trait |
| 1154 | } |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1155 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1156 | #visit_impl |
| 1157 | }, |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1158 | ); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1159 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1160 | let visit_mut_trait = state.visit_mut_trait; |
| 1161 | let visit_mut_impl = state.visit_mut_impl; |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1162 | write_file( |
| 1163 | VISIT_MUT_SRC, |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1164 | quote! { |
| 1165 | #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1166 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1167 | use *; |
| 1168 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1169 | use punctuated::Punctuated; |
| 1170 | use proc_macro2::Span; |
| 1171 | #[cfg(any(feature = "full", feature = "derive"))] |
| 1172 | use gen::helper::visit_mut::*; |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 1173 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1174 | #full_macro |
| 1175 | #skip_macro |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1176 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1177 | /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in |
| 1178 | /// place. |
| 1179 | /// |
| 1180 | /// See the [module documentation] for details. |
| 1181 | /// |
| 1182 | /// [module documentation]: index.html |
| 1183 | /// |
| 1184 | /// *This trait is available if Syn is built with the `"visit-mut"` feature.* |
| 1185 | pub trait VisitMut { |
| 1186 | #visit_mut_trait |
| 1187 | } |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1188 | |
David Tolnay | 6af4899 | 2018-08-01 11:16:28 -0700 | [diff] [blame] | 1189 | #visit_mut_impl |
| 1190 | }, |
David Tolnay | ae0009e | 2018-08-01 00:40:00 -0700 | [diff] [blame] | 1191 | ); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1192 | } |