juniper_codegen/common/parse/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//! Common functions, definitions and extensions for parsing, normalizing and modifying Rust syntax,
//! used by this crate.

pub(crate) mod attr;
pub(crate) mod downcaster;

use std::{any::TypeId, iter, mem};

use proc_macro2::Span;
use quote::quote;
use syn::{
    ext::IdentExt as _,
    parse::{Parse, ParseBuffer},
    parse_quote,
    punctuated::Punctuated,
    token::{self, Token},
    visit_mut::VisitMut,
};

/// Extension of [`ParseBuffer`] providing common function widely used by this crate for parsing.
pub(crate) trait ParseBufferExt {
    /// Tries to parse `T` as the next token.
    ///
    /// Doesn't move [`ParseStream`]'s cursor if there is no `T`.
    fn try_parse<T: Default + Parse + Token>(&self) -> syn::Result<Option<T>>;

    /// Checks whether next token is `T`.
    ///
    /// Doesn't move [`ParseStream`]'s cursor.
    #[must_use]
    fn is_next<T: Default + Token>(&self) -> bool;

    /// Parses next token as [`syn::Ident`] _allowing_ Rust keywords, while default [`Parse`]
    /// implementation for [`syn::Ident`] disallows keywords.
    ///
    /// Always moves [`ParseStream`]'s cursor.
    fn parse_any_ident(&self) -> syn::Result<syn::Ident>;

    /// Checks whether next token is a wrapper `W` and if yes, then parses the wrapped tokens as `T`
    /// [`Punctuated`] with `P`. Otherwise, parses just `T`.
    ///
    /// Always moves [`ParseStream`]'s cursor.
    fn parse_maybe_wrapped_and_punctuated<T, W, P>(&self) -> syn::Result<Punctuated<T, P>>
    where
        T: Parse,
        W: Default + Token + 'static,
        P: Default + Parse + Token;
}

impl<'a> ParseBufferExt for ParseBuffer<'a> {
    fn try_parse<T: Default + Parse + Token>(&self) -> syn::Result<Option<T>> {
        Ok(if self.is_next::<T>() {
            Some(self.parse()?)
        } else {
            None
        })
    }

    fn is_next<T: Default + Token>(&self) -> bool {
        self.lookahead1().peek(|_| T::default())
    }

    fn parse_any_ident(&self) -> syn::Result<syn::Ident> {
        self.call(syn::Ident::parse_any)
    }

    fn parse_maybe_wrapped_and_punctuated<T, W, P>(&self) -> syn::Result<Punctuated<T, P>>
    where
        T: Parse,
        W: Default + Token + 'static,
        P: Default + Parse + Token,
    {
        Ok(if self.is_next::<W>() {
            let inner;
            if TypeId::of::<W>() == TypeId::of::<token::Bracket>() {
                let _ = syn::bracketed!(inner in self);
            } else if TypeId::of::<W>() == TypeId::of::<token::Brace>() {
                let _ = syn::braced!(inner in self);
            } else if TypeId::of::<W>() == TypeId::of::<token::Paren>() {
                let _ = syn::parenthesized!(inner in self);
            } else {
                unimplemented!(
                    "ParseBufferExt::parse_maybe_wrapped_and_punctuated supports only brackets, \
                     braces and parentheses as wrappers.",
                );
            }
            Punctuated::parse_terminated(&inner)?
        } else {
            Punctuated::from_iter(iter::once(self.parse::<T>()?))
        })
    }
}

/// Extension of [`syn::Type`] providing common function widely used by this crate for parsing.
pub(crate) trait TypeExt {
    /// Retrieves the innermost non-parenthesized [`syn::Type`] from the given
    /// one (unwraps nested [`syn::TypeParen`]s asap).
    #[must_use]
    fn unparenthesized(&self) -> &Self;

    /// Retrieves the inner [`syn::Type`] from the given reference type, or just
    /// returns "as is" if the type is not a reference.
    ///
    /// Also, makes the type [`TypeExt::unparenthesized`], if possible.
    #[must_use]
    fn unreferenced(&self) -> &Self;

    /// Iterates mutably over all the lifetime parameters of this [`syn::Type`]
    /// with the given `func`tion.
    fn lifetimes_iter_mut<F: FnMut(&mut syn::Lifetime)>(&mut self, func: &mut F);

    /// Anonymizes all the lifetime parameters of this [`syn::Type`] (except
    /// the `'static` ones), making it suitable for using in contexts with
    /// inferring.
    fn lifetimes_anonymized(&mut self);

    /// Returns the topmost [`syn::Ident`] of this [`syn::TypePath`], if any.
    #[must_use]
    fn topmost_ident(&self) -> Option<&syn::Ident>;
}

impl TypeExt for syn::Type {
    fn unparenthesized(&self) -> &Self {
        match self {
            Self::Paren(ty) => ty.elem.unparenthesized(),
            Self::Group(ty) => ty.elem.unparenthesized(),
            ty => ty,
        }
    }

    fn unreferenced(&self) -> &Self {
        match self.unparenthesized() {
            Self::Reference(ref_ty) => &ref_ty.elem,
            ty => ty,
        }
    }

    fn lifetimes_iter_mut<F: FnMut(&mut syn::Lifetime)>(&mut self, func: &mut F) {
        use syn::{GenericArgument as GA, Type as T};

        fn iter_path<F: FnMut(&mut syn::Lifetime)>(path: &mut syn::Path, func: &mut F) {
            for seg in path.segments.iter_mut() {
                match &mut seg.arguments {
                    syn::PathArguments::AngleBracketed(angle) => {
                        for arg in angle.args.iter_mut() {
                            match arg {
                                GA::Lifetime(lt) => func(lt),
                                GA::Type(ty) => ty.lifetimes_iter_mut(func),
                                GA::AssocType(a) => a.ty.lifetimes_iter_mut(func),
                                GA::Constraint(_) | GA::AssocConst(_) | GA::Const(_) => {}
                                // Following the `syn` idiom for exhaustive matching on `Type`:
                                // https://docs.rs/syn/2.0.38/src/syn/ty.rs.html#64-79
                                // TODO: #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
                                //       https://github.com/rust-lang/rust/issues/89554
                                _ => unimplemented!(),
                            }
                        }
                    }
                    syn::PathArguments::Parenthesized(args) => {
                        for ty in args.inputs.iter_mut() {
                            ty.lifetimes_iter_mut(func)
                        }
                        if let syn::ReturnType::Type(_, ty) = &mut args.output {
                            (*ty).lifetimes_iter_mut(func)
                        }
                    }
                    syn::PathArguments::None => {}
                }
            }
        }

        match self {
            T::Array(syn::TypeArray { elem, .. })
            | T::Group(syn::TypeGroup { elem, .. })
            | T::Paren(syn::TypeParen { elem, .. })
            | T::Ptr(syn::TypePtr { elem, .. })
            | T::Slice(syn::TypeSlice { elem, .. }) => (*elem).lifetimes_iter_mut(func),

            T::Tuple(syn::TypeTuple { elems, .. }) => {
                for ty in elems.iter_mut() {
                    ty.lifetimes_iter_mut(func)
                }
            }

            T::ImplTrait(syn::TypeImplTrait { bounds, .. })
            | T::TraitObject(syn::TypeTraitObject { bounds, .. }) => {
                for bound in bounds.iter_mut() {
                    match bound {
                        syn::TypeParamBound::Lifetime(lt) => func(lt),
                        syn::TypeParamBound::Trait(bound) => {
                            if bound.lifetimes.is_some() {
                                todo!("Iterating over HRTB lifetimes in trait is not yet supported")
                            }
                            iter_path(&mut bound.path, func)
                        }
                        syn::TypeParamBound::Verbatim(_) => {}
                        // Following the `syn` idiom for exhaustive matching on `Type`:
                        // https://docs.rs/syn/2.0.38/src/syn/ty.rs.html#64-79
                        // TODO: #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
                        //       https://github.com/rust-lang/rust/issues/89554
                        _ => unimplemented!(),
                    }
                }
            }

            T::Reference(ref_ty) => {
                if let Some(lt) = ref_ty.lifetime.as_mut() {
                    func(lt)
                }
                (*ref_ty.elem).lifetimes_iter_mut(func)
            }

            T::Path(ty) => iter_path(&mut ty.path, func),

            // These types unlikely will be used as GraphQL types.
            T::BareFn(_) | T::Infer(_) | T::Macro(_) | T::Never(_) | T::Verbatim(_) => {}

            // Following the `syn` idiom for exhaustive matching on `Type`:
            // https://docs.rs/syn/2.0.38/src/syn/ty.rs.html#64-79
            // TODO: #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
            //       https://github.com/rust-lang/rust/issues/89554
            _ => unimplemented!(),
        }
    }

    fn lifetimes_anonymized(&mut self) {
        self.lifetimes_iter_mut(&mut |lt| {
            if lt.ident != "_" && lt.ident != "static" {
                lt.ident = syn::Ident::new("_", Span::call_site());
            }
        });
    }

    fn topmost_ident(&self) -> Option<&syn::Ident> {
        match self.unparenthesized() {
            syn::Type::Path(p) => Some(&p.path),
            syn::Type::Reference(r) => match (*r.elem).unparenthesized() {
                syn::Type::Path(p) => Some(&p.path),
                syn::Type::TraitObject(o) => match o.bounds.iter().next().unwrap() {
                    syn::TypeParamBound::Trait(b) => Some(&b.path),
                    _ => None,
                },
                _ => None,
            },
            _ => None,
        }?
        .segments
        .last()
        .map(|s| &s.ident)
    }
}

/// Extension of [`syn::Generics`] providing common function widely used by this crate for parsing.
pub(crate) trait GenericsExt {
    /// Removes all default types out of type parameters and const parameters in these
    /// [`syn::Generics`].
    fn remove_defaults(&mut self);

    /// Moves all trait and lifetime bounds of these [`syn::Generics`] to its [`syn::WhereClause`].
    fn move_bounds_to_where_clause(&mut self);

    /// Replaces generic parameters in the given [`syn::Type`] with default
    /// ones, provided by these [`syn::Generics`].
    fn replace_type_with_defaults(&self, ty: &mut syn::Type);

    /// Replaces generic parameters in the given [`syn::TypePath`] with default
    /// ones, provided by these [`syn::Generics`].
    fn replace_type_path_with_defaults(&self, ty: &mut syn::TypePath);
}

impl GenericsExt for syn::Generics {
    fn remove_defaults(&mut self) {
        use syn::GenericParam as P;

        for p in &mut self.params {
            match p {
                P::Type(p) => {
                    p.eq_token = None;
                    p.default = None;
                }
                P::Lifetime(_) => {}
                P::Const(p) => {
                    p.eq_token = None;
                    p.default = None;
                }
            }
        }
    }

    fn move_bounds_to_where_clause(&mut self) {
        use syn::GenericParam as P;

        let _ = self.make_where_clause();
        let where_clause = self.where_clause.as_mut().unwrap();

        for p in &mut self.params {
            match p {
                P::Type(p) => {
                    if p.colon_token.is_some() {
                        p.colon_token = None;
                        let bounds = mem::take(&mut p.bounds);
                        let ty = &p.ident;
                        where_clause.predicates.push(parse_quote! { #ty: #bounds });
                    }
                }
                P::Lifetime(p) => {
                    if p.colon_token.is_some() {
                        p.colon_token = None;
                        let bounds = mem::take(&mut p.bounds);
                        let lt = &p.lifetime;
                        where_clause.predicates.push(parse_quote! { #lt: #bounds });
                    }
                }
                P::Const(_) => {}
            }
        }
    }

    fn replace_type_with_defaults(&self, ty: &mut syn::Type) {
        ReplaceWithDefaults(self).visit_type_mut(ty)
    }

    fn replace_type_path_with_defaults(&self, ty: &mut syn::TypePath) {
        ReplaceWithDefaults(self).visit_type_path_mut(ty)
    }
}

/// Replaces [`Generics`] with default values:
/// - `'static` for [`Lifetime`]s;
/// - `::juniper::DefaultScalarValue` for [`Type`]s.
///
/// [`Generics`]: syn::Generics
/// [`Lifetime`]: syn::Lifetime
/// [`Type`]: syn::Type
struct ReplaceWithDefaults<'a>(&'a syn::Generics);

impl<'a> VisitMut for ReplaceWithDefaults<'a> {
    fn visit_generic_argument_mut(&mut self, arg: &mut syn::GenericArgument) {
        match arg {
            syn::GenericArgument::Lifetime(lf) => {
                *lf = parse_quote! { 'static };
            }
            syn::GenericArgument::Type(ty) => {
                let is_generic = self
                    .0
                    .params
                    .iter()
                    .filter_map(|par| match par {
                        syn::GenericParam::Type(ty) => Some(&ty.ident),
                        _ => None,
                    })
                    .any(|par| {
                        let par = quote! { #par }.to_string();
                        let ty = quote! { #ty }.to_string();
                        par == ty
                    });

                if is_generic {
                    // Replace with `DefaultScalarValue` instead of `()`
                    // because generic parameter may be scalar.
                    *ty = parse_quote!(::juniper::DefaultScalarValue);
                }
            }
            _ => {}
        }
    }
}