fuzzy_pickles/
ast.rs

1//! The components of a Rust Abstract Syntax Tree (AST)
2
3// Potential cleanups:
4//
5// - Split expressions, patterns, types into separate modules?
6// - Any struct with just an extent and a value should be checked for redundancy
7// - All types should have public fields
8
9use crate::{Extent, HasExtent};
10use crate::visit::{Visit, Visitor, VisitorMut};
11
12/// An entire Rust file
13#[derive(Debug, Visit)]
14pub struct File {
15    pub items: Vec<Attributed<Item>>,
16    pub whitespace: Vec<Whitespace>,
17}
18
19#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
20pub enum Item {
21    AttributeContaining(AttributeContaining),
22    Const(Const),
23    Enum(Enum),
24    ExternCrate(Crate),
25    ExternBlock(ExternBlock),
26    Function(Function),
27    Impl(Impl),
28    MacroCall(MacroCall),
29    Module(Module),
30    Static(Static),
31    Struct(Struct),
32    Trait(Trait),
33    TypeAlias(TypeAlias),
34    Use(Use),
35    Union(Union),
36}
37
38#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
39// TODO: rename to outer?
40pub enum Attribute {
41    DocCommentLine(Extent),
42    DocCommentBlock(Extent),
43    Literal(AttributeLiteral),
44}
45
46/// An attribute that applies to the subsequent element
47///
48/// ### Example Source
49///
50/// ```rust,ignore
51/// #[derive(Debug)]
52/// ```
53#[derive(Debug, HasExtent, ExtentIndex, Visit)]
54pub struct AttributeLiteral {
55    pub extent: Extent,
56    pub text: Extent,
57    pub whitespace: Vec<Whitespace>,
58}
59
60#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
61// TODO: rename to inner?
62pub enum AttributeContaining {
63    DocCommentLine(Extent),
64    DocCommentBlock(Extent),
65    Literal(AttributeContainingLiteral),
66}
67
68/// An attribute that applies to the containing element
69///
70/// ### Example Source
71///
72/// ```rust,ignore
73/// #![feature(nll)]
74/// ```
75#[derive(Debug, HasExtent, ExtentIndex, Visit)]
76pub struct AttributeContainingLiteral {
77    pub extent: Extent,
78    pub text: Extent,
79    pub whitespace: Vec<Whitespace>,
80}
81
82/// A lifetime identifier
83///
84/// ### Example Source
85///
86/// ```rust,ignore
87/// 'static
88/// ```
89#[derive(Debug, HasExtent, ExtentIndex, Visit)]
90pub struct Lifetime {
91    pub extent: Extent,
92    pub name: Ident,
93}
94
95#[derive(Debug, PartialEq, Eq, HasExtent, ExtentIndex, Visit, Decompose)]
96pub enum Whitespace {
97    Comment(Comment),
98    Whitespace(Extent),
99}
100
101#[derive(Debug, PartialEq, Eq, HasExtent, ExtentIndex, Visit, Decompose)]
102pub enum Comment {
103    Line(Extent),
104    Block(Extent),
105}
106
107/// A `use` item
108///
109/// ### Example Source
110///
111/// ```rust,ignore
112/// use std::collections::HashMap;
113/// ```
114#[derive(Debug, HasExtent, ExtentIndex, Visit)]
115pub struct Use {
116    pub extent: Extent,
117    pub visibility: Option<Visibility>,
118    pub path: UsePath,
119    pub whitespace: Vec<Whitespace>,
120}
121
122/// The names imported by the `use` item
123///
124/// ### Example Source
125///
126/// ```rust,ignore
127/// use std::collections::HashMap;
128/// //  ^^^^^^^^^^^^^^^^^^^^^^^^^
129/// ```
130#[derive(Debug, HasExtent, ExtentIndex, Visit)]
131pub struct UsePath {
132    pub extent: Extent,
133    pub path: Vec<Ident>,
134    pub tail: UseTail,
135    pub whitespace: Vec<Whitespace>,
136}
137
138#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
139pub enum UseTail {
140    Ident(UseTailIdent),
141    Glob(UseTailGlob),
142    Multi(UseTailMulti),
143}
144
145/// A single name imported by the `use` item
146///
147/// ### Example Source
148///
149/// ```rust,ignore
150/// use std::collections::HashMap;
151/// //                    ^^^^^^^
152/// ```
153#[derive(Debug, HasExtent, ExtentIndex, Visit)]
154pub struct UseTailIdent {
155    pub extent: Extent,
156    pub name: Ident,
157    pub rename: Option<Ident>,
158    pub whitespace: Vec<Whitespace>,
159}
160
161/// A wildcard name imported by the `use` item
162///
163/// ### Example Source
164///
165/// ```rust,ignore
166/// use std::collections::*;
167/// //                    ^
168/// ```
169#[derive(Debug, HasExtent, ExtentIndex, Visit)]
170pub struct UseTailGlob {
171    pub extent: Extent,
172}
173
174/// A collection of names imported by the `use` item
175///
176/// ### Example Source
177///
178/// ```rust,ignore
179/// use std::collections::{BTreeMap, HashMap};
180/// //                    ^^^^^^^^^^^^^^^^^^^
181/// ```
182// TODO: rename to "collection"?
183#[derive(Debug, HasExtent, ExtentIndex, Visit)]
184pub struct UseTailMulti {
185    pub extent: Extent,
186    pub paths: Vec<UsePath>,
187    pub whitespace: Vec<Whitespace>,
188}
189
190/// A function definition
191///
192/// ### Example Source
193///
194/// ```rust,ignore
195/// fn hello() {}
196/// ```
197// TODO: rename to "function definition"?
198#[derive(Debug, HasExtent, ExtentIndex, Visit)]
199pub struct Function {
200    pub extent: Extent,
201    pub header: FunctionHeader,
202    pub body: Block,
203    pub whitespace: Vec<Whitespace>,
204}
205
206/// A function definition's signature
207///
208/// ### Example Source
209///
210/// ```rust,ignore
211///     pub fn hello(x: i32) -> bool { false }
212/// //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
213/// ```
214// TODO: rename to "function signature"?
215// TODO: are we allowing `self` in free functions?
216#[derive(Debug, HasExtent, ExtentIndex, Visit)]
217pub struct FunctionHeader {
218    pub extent: Extent,
219    pub visibility: Option<Visibility>,
220    pub qualifiers: FunctionQualifiers,
221    pub name: Ident,
222    pub generics: Option<GenericDeclarations>,
223    pub arguments: Vec<Argument>,
224    pub return_type: Option<Type>,
225    pub wheres: Vec<Where>,
226    pub whitespace: Vec<Whitespace>,
227}
228
229/// Qualifiers that apply to functions
230///
231/// ### Example Source
232///
233/// ```rust,ignore
234///     const unsafe extern "C" fn example() {}
235/// //  ^^^^^^^^^^^^^^^^^^^^^^^
236/// ```
237#[derive(Debug, HasExtent, ExtentIndex, Visit)]
238pub struct FunctionQualifiers {
239    pub extent: Extent,
240    // TODO: do we allow parsing this on a free function?
241    pub is_default: Option<Extent>,
242    pub is_const: Option<Extent>,
243    pub is_unsafe: Option<Extent>,
244    pub is_async: Option<Extent>,
245    pub is_extern: Option<Extent>,
246    // TODO: abi should be predicated on `extern` being present
247    pub abi: Option<String>,
248    pub whitespace: Vec<Whitespace>,
249}
250
251/// The signature of a function in a trait declaration
252///
253/// ### Example Source
254///
255/// ```rust,ignore
256/// pub trait Monster { fn roar(&self) {} }
257/// //                  ^^^^^^^^^^^^^^
258/// ```
259#[derive(Debug, HasExtent, ExtentIndex, Visit)]
260pub struct TraitImplFunctionHeader {
261    pub extent: Extent,
262    pub visibility: Option<Visibility>,
263    pub qualifiers: FunctionQualifiers,
264    pub name: Ident,
265    pub generics: Option<GenericDeclarations>,
266    pub arguments: Vec<TraitImplArgument>,
267    pub return_type: Option<Type>,
268    pub wheres: Vec<Where>,
269    pub whitespace: Vec<Whitespace>,
270}
271
272/// Generic lifetime and type parameters
273///
274/// ### Example Source
275///
276/// ```rust,ignore
277/// struct A<'a, T> {}
278/// //      ^^^^^^^
279/// ```
280// TODO: rename to "parameters"?
281#[derive(Debug, HasExtent, ExtentIndex, Visit)]
282pub struct GenericDeclarations {
283    pub extent: Extent,
284    pub lifetimes: Vec<Attributed<GenericDeclarationLifetime>>,
285    pub types: Vec<Attributed<GenericDeclarationType>>,
286    pub consts: Vec<Attributed<GenericDeclarationConst>>,
287    pub whitespace: Vec<Whitespace>,
288}
289
290/// Generic lifetime parameters
291///
292/// ### Example Source
293///
294/// ```rust,ignore
295/// struct A<'a, 'b: 'a> {}
296/// //       ^^  ^^^^^^
297/// ```
298#[derive(Debug, HasExtent, ExtentIndex, Visit)]
299pub struct GenericDeclarationLifetime {
300    pub extent: Extent,
301    pub name: Lifetime,
302    pub bounds: Vec<Lifetime>,
303    pub whitespace: Vec<Whitespace>,
304}
305
306/// Generic type parameters
307///
308/// ### Example Source
309///
310/// ```rust,ignore
311/// struct A<T, U: Debug, V = i32> {}
312/// //       ^  ^^^^^^^^  ^^^^^^^
313/// ```
314#[derive(Debug, HasExtent, ExtentIndex, Visit)]
315pub struct GenericDeclarationType {
316    pub extent: Extent,
317    pub name: Ident,
318    pub bounds: Option<TraitBounds>,
319    pub default: Option<Type>,
320    pub whitespace: Vec<Whitespace>,
321}
322
323/// Generic constants
324///
325/// ### Example Source
326///
327/// ```rust,ignore
328/// struct A<const N: usize> {}
329/// //       ^^^^^^^^^^^^^^
330/// ```
331#[derive(Debug, HasExtent, ExtentIndex, Visit)]
332pub struct GenericDeclarationConst {
333    pub extent: Extent,
334    pub name: Ident,
335    pub typ: Type,
336    pub whitespace: Vec<Whitespace>,
337}
338
339/// A concrete type
340///
341/// ### Example Source
342///
343/// ```rust,ignore
344/// fn a(_: i32, _: [bool; 4]) -> Option<(f32, f64)>
345/// //      ^^^      ^^^^                 ^^^  ^^^
346/// //              ^^^^^^^^^            ^^^^^^^^^^
347/// //                            ^^^^^^^^^^^^^^^^^^
348/// ```
349#[derive(Debug, HasExtent, ExtentIndex, Visit)]
350pub struct Type {
351    pub extent: Extent,
352    pub kind: TypeKind,
353    pub additional: Vec<TypeAdditional>,
354    pub whitespace: Vec<Whitespace>,
355}
356
357#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
358pub enum TypeKind {
359    Array(TypeArray),
360    Disambiguation(TypeDisambiguation),
361    Function(TypeFunction),
362    HigherRankedTraitBounds(TypeHigherRankedTraitBounds),
363    DynTrait(TypeDynTrait),
364    ImplTrait(TypeImplTrait),
365    Named(TypeNamed),
366    Pointer(TypePointer),
367    Reference(TypeReference),
368    Slice(TypeSlice),
369    Tuple(TypeTuple),
370    Uninhabited(Extent),
371}
372
373/// A reference in a type
374///
375/// ### Example Source
376///
377/// ```rust,ignore
378/// fn a() -> &'a mut i32 {}
379/// //        ^^^^^^^^^^^
380/// ```
381#[derive(Debug, HasExtent, ExtentIndex, Visit)]
382pub struct TypeReference {
383    pub extent: Extent,
384    pub kind: TypeReferenceKind,
385    pub typ: Box<Type>,
386    pub whitespace: Vec<Whitespace>,
387}
388
389/// The qualifiers for a reference in a type
390///
391/// ### Example Source
392///
393/// ```rust,ignore
394/// fn a() -> &'a mut i32 {}
395/// //        ^^^^^^^
396/// ```
397// TODO: rename to qualifiers?
398#[derive(Debug, HasExtent, ExtentIndex, Visit)]
399pub struct TypeReferenceKind {
400    pub extent: Extent,
401    pub lifetime: Option<Lifetime>,
402    pub mutable: Option<Extent>,
403    pub whitespace: Vec<Whitespace>,
404}
405
406/// A pointer in a type
407///
408/// ### Example Source
409///
410/// ```rust,ignore
411/// fn a() -> *const bool {}
412/// //        ^^^^^^^^^^^
413/// ```
414#[derive(Debug, HasExtent, ExtentIndex, Visit)]
415pub struct TypePointer {
416    pub extent: Extent,
417    #[visit(ignore)]
418    pub kind: TypePointerKind,
419    pub typ: Box<Type>,
420    pub whitespace: Vec<Whitespace>,
421}
422
423#[derive(Debug, Copy, Clone, PartialEq, Eq)]
424pub enum TypePointerKind {
425    Const,
426    Mutable,
427}
428
429/// An array in a type
430///
431/// ### Example Source
432///
433/// ```rust,ignore
434/// fn a() -> [u8; 16] {}
435/// //        ^^^^^^^^
436/// ```
437#[derive(Debug, HasExtent, ExtentIndex, Visit)]
438pub struct TypeArray {
439    pub extent: Extent,
440    pub typ: Box<Type>,
441    pub count: Box<Attributed<Expression>>,
442    pub whitespace: Vec<Whitespace>,
443}
444
445/// A type with a higher-ranked trait bound
446///
447/// ### Example Source
448///
449/// ```rust,ignore
450/// fn a() -> for<'a> &'a i16 {}
451/// //        ^^^^^^^^^^^^^^^
452/// ```
453#[derive(Debug, HasExtent, ExtentIndex, Visit)]
454pub struct TypeHigherRankedTraitBounds {
455    pub extent: Extent,
456    pub lifetimes: Vec<Lifetime>,
457    pub child: TypeHigherRankedTraitBoundsChild,
458    pub whitespace: Vec<Whitespace>,
459}
460
461#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
462pub enum TypeHigherRankedTraitBoundsChild {
463    Named(TypeNamed),
464    Function(TypeFunction),
465    Reference(TypeReference),
466}
467
468/// A trait object
469///
470/// ### Example Source
471///
472/// ```rust,ignore
473/// fn a() -> Box<dyn Iterator<Item = u8>> {}
474/// //            ^^^^^^^^^^^^^^^^^^^^^^^
475/// ```
476#[derive(Debug, HasExtent, ExtentIndex, Visit)]
477pub struct TypeDynTrait {
478    pub extent: Extent,
479    pub name: TypeNamed,
480    pub whitespace: Vec<Whitespace>,
481}
482
483/// An unnamed implementation of a trait
484///
485/// ### Example Source
486///
487/// ```rust,ignore
488/// fn a() -> impl Iterator<Item = u8> {}
489/// //        ^^^^^^^^^^^^^^^^^^^^^^^^
490/// ```
491#[derive(Debug, HasExtent, ExtentIndex, Visit)]
492pub struct TypeImplTrait {
493    pub extent: Extent,
494    pub name: TypeNamed,
495    pub whitespace: Vec<Whitespace>,
496}
497
498#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
499pub enum TypeAdditional {
500    Named(TypeNamed),
501    Lifetime(Lifetime),
502}
503
504/// A named type
505///
506/// ### Example Source
507///
508/// ```rust,ignore
509/// fn a() -> ::std::collections::HashMap<u8, u8> {}
510/// //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
511/// ```
512#[derive(Debug, HasExtent, ExtentIndex, Visit)]
513pub struct TypeNamed {
514    pub extent: Extent,
515    pub path: Vec<TypeNamedComponent>,
516    pub whitespace: Vec<Whitespace>,
517}
518
519/// A component of a named type
520///
521/// ### Example Source
522///
523/// ```rust,ignore
524/// fn a() -> ::std::collections::HashMap<u8, u8> {}
525/// //          ^^^  ^^^^^^^^^^^  ^^^^^^^^^^^^^^^
526/// ```
527#[derive(Debug, HasExtent, ExtentIndex, Visit)]
528pub struct TypeNamedComponent {
529    pub extent: Extent,
530    pub ident: Ident,
531    pub generics: Option<TypeGenerics>,
532    pub whitespace: Vec<Whitespace>,
533}
534
535/// A disambiguation of a type
536///
537/// ### Example Source
538///
539/// ```rust,ignore
540/// fn a() -> <Vec<u8> as IntoIterator>::Item {}
541/// //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
542/// ```
543#[derive(Debug, HasExtent, ExtentIndex, Visit)]
544pub struct TypeDisambiguation {
545    pub extent: Extent,
546    pub from_type: Box<Type>,
547    pub to_type: Option<Box<TypeNamed>>,
548    pub path: Vec<TypeNamedComponent>,
549    pub whitespace: Vec<Whitespace>,
550}
551
552/// A slice as a type
553///
554/// ### Example Source
555///
556/// ```rust,ignore
557/// fn a() -> &[u8] {}
558/// //         ^^^^
559/// ```
560#[derive(Debug, HasExtent, ExtentIndex, Visit)]
561pub struct TypeSlice {
562    pub extent: Extent,
563    pub typ: Box<Type>,
564    pub whitespace: Vec<Whitespace>,
565}
566
567/// A tuple as a type
568///
569/// ### Example Source
570///
571/// ```rust,ignore
572/// fn a() -> (i32, u8) {}
573/// //        ^^^^^^^^^
574/// ```
575#[derive(Debug, HasExtent, ExtentIndex, Visit)]
576pub struct TypeTuple {
577    pub extent: Extent,
578    pub types: Vec<Type>,
579    pub whitespace: Vec<Whitespace>,
580}
581
582#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
583pub enum TypeGenerics {
584    Function(TypeGenericsFunction),
585    Angle(TypeGenericsAngle),
586}
587
588/// Generic parameter declarations in a function style
589///
590/// ### Example Source
591///
592/// ```rust,ignore
593/// fn a() -> Fn(i32) -> bool {}
594/// //          ^^^^^^^^^^^^^
595/// ```
596// TODO: rename to "parameters" / "declaration"?
597#[derive(Debug, HasExtent, ExtentIndex, Visit)]
598pub struct TypeGenericsFunction {
599    pub extent: Extent,
600    pub types: Vec<Type>,
601    pub return_type: Option<Box<Type>>,
602    pub whitespace: Vec<Whitespace>,
603}
604
605/// Generic parameter declarations in the basic style
606///
607/// ### Example Source
608///
609/// ```rust,ignore
610/// fn a() -> HashMap<i32, u8> {}
611/// //               ^^^^^^^^^
612/// ```
613#[derive(Debug, HasExtent, ExtentIndex, Visit)]
614pub struct TypeGenericsAngle {
615    pub extent: Extent,
616    pub members: Vec<TypeGenericsAngleMember>,
617    pub whitespace: Vec<Whitespace>,
618}
619
620#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
621pub enum TypeGenericsAngleMember {
622    Lifetime(Lifetime),
623    Type(Type),
624    AssociatedType(AssociatedType)
625}
626
627/// An associated item in a type with generics
628///
629/// ### Example Source
630///
631/// ```rust,ignore
632/// fn a() -> impl Iterator<Item = bool> {}
633/// //                      ^^^^^^^^^^^
634/// ```
635// TODO: prefix the name with "type"?
636#[derive(Debug, HasExtent, ExtentIndex, Visit)]
637pub struct AssociatedType {
638    pub extent: Extent,
639    pub name: Ident,
640    pub value: AssociatedTypeValue,
641    pub whitespace: Vec<Whitespace>,
642}
643
644#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
645pub enum AssociatedTypeValue {
646    Equal(AssociatedTypeValueEqual),
647    Bound(AssociatedTypeValueBound),
648}
649
650/// An exact associated item
651///
652/// ### Example Source
653///
654/// ```rust,ignore
655/// fn a() -> impl Iterator<Item = bool> {}
656/// //                           ^^^^^^
657/// ```
658#[derive(Debug, HasExtent, ExtentIndex, Visit)]
659pub struct AssociatedTypeValueEqual {
660    pub extent: Extent,
661    pub value: Type,
662    pub whitespace: Vec<Whitespace>,
663}
664
665/// An associated item with a trait bound
666///
667/// ### Example Source
668///
669/// ```rust,ignore
670/// fn a() -> impl Iterator<Item: Debug> {}
671/// //                          ^^^^^^^
672/// ```
673#[derive(Debug, HasExtent, ExtentIndex, Visit)]
674pub struct AssociatedTypeValueBound {
675    pub extent: Extent,
676    pub bounds: TraitBounds,
677    pub whitespace: Vec<Whitespace>,
678}
679
680/// A function pointer as a type
681///
682/// ### Example Source
683///
684/// ```rust,ignore
685/// fn a() -> fn(i8) -> bool {}
686/// //        ^^^^^^^^^^^^^^
687/// ```
688#[derive(Debug, HasExtent, ExtentIndex, Visit)]
689pub struct TypeFunction {
690    pub extent: Extent,
691    pub qualifiers: FunctionQualifiers,
692    pub arguments: Vec<TypeFunctionArgument>,
693    pub return_type: Option<Box<Type>>,
694    pub whitespace: Vec<Whitespace>,
695}
696
697#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
698pub enum TypeFunctionArgument {
699    Named(TypeFunctionArgumentNamed),
700    Variadic(Extent),
701}
702
703/// The named argument of a function pointer
704///
705/// ### Example Source
706///
707/// ```rust,ignore
708/// fn a() -> fn(a: i32) {}
709/// //           ^^^^^^
710/// ```
711#[derive(Debug, HasExtent, ExtentIndex, Visit)]
712pub struct TypeFunctionArgumentNamed {
713    pub extent: Extent,
714    pub name: Option<Ident>,
715    pub typ: Type,
716    pub whitespace: Vec<Whitespace>,
717}
718
719/// A single identifier
720///
721/// ### Example Source
722///
723/// ```rust,ignore
724/// fn main() {}
725/// // ^^^^
726/// ```
727#[derive(Debug, Copy, Clone, HasExtent, ExtentIndex, Visit)]
728pub struct Ident {
729    pub extent: Extent,
730}
731
732/// The path that an item is visible in
733///
734/// ### Example Source
735///
736/// ```rust,ignore
737/// pub(in foo) struct A;
738/// //     ^^^
739/// ```
740// TODO: make this a more specific name; `Path` is overly generic for this usage
741#[derive(Debug, HasExtent, ExtentIndex, Visit)]
742pub struct Path {
743    pub extent: Extent,
744    // TODO: Can we reuse the path from the `use` statement?
745    pub components: Vec<Ident>,
746    pub whitespace: Vec<Whitespace>,
747}
748
749/// A module-qualified identifier
750///
751/// ### Example Source
752///
753/// ```rust,ignore
754/// fn a() { ::std::thread::spawn(); }
755/// //       ^^^^^^^^^^^^^^^^^^^^
756/// ```
757// TODO: Can we reuse the path from the `use` statement?
758#[derive(Debug, HasExtent, ExtentIndex, Visit)]
759pub struct PathedIdent {
760    pub extent: Extent,
761    pub components: Vec<PathComponent>,
762    pub whitespace: Vec<Whitespace>,
763}
764
765impl From<Ident> for PathedIdent {
766    fn from(other: Ident) -> PathedIdent {
767        PathedIdent {
768            extent: other.extent,
769            components: vec![
770                PathComponent {
771                    extent: other.extent,
772                    ident: other,
773                    turbofish: None,
774                    whitespace: Vec::new(),
775                },
776            ],
777            whitespace: Vec::new(),
778        }
779    }
780}
781
782/// A component of a module-qualified identifier
783///
784/// ### Example Source
785///
786/// ```rust,ignore
787/// fn a() { ::std::thread::spawn(); }
788/// //         ^^^  ^^^^^^  ^^^^^
789/// ```
790#[derive(Debug, HasExtent, ExtentIndex, Visit)]
791pub struct PathComponent {
792    pub extent: Extent,
793    pub ident: Ident,
794    pub turbofish: Option<Turbofish>,
795    pub whitespace: Vec<Whitespace>,
796}
797
798/// Allows specifying concrete types
799///
800/// ### Example Source
801///
802/// ```rust,ignore
803/// fn a() { None::<u8>; }
804/// //           ^^^^^^
805/// ```
806#[derive(Debug, HasExtent, ExtentIndex, Visit)]
807pub struct Turbofish {
808    pub extent: Extent,
809    pub lifetimes: Vec<Lifetime>,
810    pub types: Vec<Type>,
811    pub whitespace: Vec<Whitespace>,
812}
813
814/// A constant value
815///
816/// ### Example Source
817///
818/// ```rust,ignore
819/// pub const NAME: &str = "Rust";
820/// ```
821#[derive(Debug, HasExtent, ExtentIndex, Visit)]
822pub struct Const {
823    pub extent: Extent,
824    pub visibility: Option<Visibility>,
825    pub name: Ident,
826    pub typ: Type,
827    pub value: Attributed<Expression>,
828    pub whitespace: Vec<Whitespace>,
829}
830
831/// A static value
832///
833/// ### Example Source
834///
835/// ```rust,ignore
836/// pub static NAME: &str = "Rust";
837/// ```
838#[derive(Debug, HasExtent, ExtentIndex, Visit)]
839pub struct Static {
840    pub extent: Extent,
841    pub visibility: Option<Visibility>,
842    pub is_mut: Option<Extent>,
843    pub name: Ident,
844    pub typ: Type,
845    pub value: Attributed<Expression>,
846    pub whitespace: Vec<Whitespace>,
847}
848
849/// A struct definition
850///
851/// ### Example Source
852///
853/// ```rust,ignore
854/// pub struct A { count: i32 }
855/// ```
856// TODO: rename to "definition"?
857#[derive(Debug, HasExtent, ExtentIndex, Visit)]
858pub struct Struct {
859    pub extent: Extent,
860    pub visibility: Option<Visibility>,
861    pub name: Ident,
862    pub generics: Option<GenericDeclarations>,
863    pub wheres: Vec<Where>,
864    pub body: StructDefinitionBody,
865    pub whitespace: Vec<Whitespace>,
866}
867
868#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
869pub enum StructDefinitionBody {
870    Brace(StructDefinitionBodyBrace),
871    Tuple(StructDefinitionBodyTuple),
872    Empty(Extent),
873}
874
875/// A struct defined using curly braces
876///
877/// ### Example Source
878///
879/// ```rust,ignore
880/// struct A { count: i32 }
881/// //       ^^^^^^^^^^^^^^
882/// ```
883#[derive(Debug, HasExtent, ExtentIndex, Visit)]
884pub struct StructDefinitionBodyBrace {
885    pub extent: Extent,
886    pub fields: Vec<Attributed<StructDefinitionFieldNamed>>,
887    pub whitespace: Vec<Whitespace>,
888}
889
890/// A named field of a struct
891///
892/// ### Example Source
893///
894/// ```rust,ignore
895/// struct A { pub count: i32 }
896/// //         ^^^^^^^^^^^^^^
897/// ```
898#[derive(Debug, HasExtent, ExtentIndex, Visit)]
899pub struct StructDefinitionFieldNamed {
900    pub extent: Extent,
901    pub visibility: Option<Visibility>,
902    pub name: Ident,
903    pub typ: Type,
904    pub whitespace: Vec<Whitespace>,
905}
906
907/// A struct defined using parenthesis
908///
909/// ### Example Source
910///
911/// ```rust,ignore
912/// struct Meters(u32);
913/// //           ^^^^^^
914/// ```
915#[derive(Debug, HasExtent, ExtentIndex, Visit)]
916pub struct StructDefinitionBodyTuple {
917    pub extent: Extent,
918    pub fields: Vec<Attributed<StructDefinitionFieldUnnamed>>,
919    pub whitespace: Vec<Whitespace>,
920}
921
922/// An unnamed field of a struct
923///
924/// ### Example Source
925///
926/// ```rust,ignore
927/// struct Meters(pub u32);
928/// //            ^^^^^^^
929/// ```
930#[derive(Debug, HasExtent, ExtentIndex, Visit)]
931pub struct StructDefinitionFieldUnnamed {
932    pub extent: Extent,
933    pub visibility: Option<Visibility>,
934    pub typ: Type,
935    pub whitespace: Vec<Whitespace>,
936}
937
938/// A union definition
939///
940/// ### Example Source
941///
942/// ```rust,ignore
943/// union Bits { big: u32, little: [u8; 4] }
944/// ```
945#[derive(Debug, HasExtent, ExtentIndex, Visit)]
946pub struct Union {
947    pub extent: Extent,
948    pub visibility: Option<Visibility>,
949    pub name: Ident,
950    pub generics: Option<GenericDeclarations>,
951    pub wheres: Vec<Where>,
952    pub fields: Vec<Attributed<StructDefinitionFieldNamed>>,
953    pub whitespace: Vec<Whitespace>,
954}
955
956/// An enumeration of multiple types
957///
958/// ### Example Source
959///
960/// ```rust,ignore
961/// pub enum Option<T> { Some(T), None }
962/// ```
963#[derive(Debug, HasExtent, ExtentIndex, Visit)]
964pub struct Enum {
965    pub extent: Extent,
966    pub visibility: Option<Visibility>,
967    pub name: Ident,
968    pub generics: Option<GenericDeclarations>,
969    pub wheres: Vec<Where>,
970    pub variants: Vec<Attributed<EnumVariant>>,
971    pub whitespace: Vec<Whitespace>,
972}
973
974/// A single member of an enum
975///
976/// ### Example Source
977///
978/// ```rust,ignore
979/// pub enum Option<T> { Some(T), None }
980/// //                   ^^^^^^^  ^^^^
981/// ```
982#[derive(Debug, HasExtent, ExtentIndex, Visit)]
983pub struct EnumVariant {
984    pub extent: Extent,
985    pub name: Ident,
986    pub body: EnumVariantBody,
987    pub whitespace: Vec<Whitespace>,
988}
989
990#[derive(Debug, Visit, Decompose)] // TODO: HasExtent?
991pub enum EnumVariantBody {
992    Tuple(Vec<Attributed<StructDefinitionFieldUnnamed>>),
993    Struct(StructDefinitionBodyBrace),
994    Unit(Option<Attributed<Expression>>),
995}
996
997#[derive(Debug, Visit, Decompose)] // TODO: HasExtent?
998pub enum Argument {
999    SelfArgument(SelfArgument),
1000    Named(NamedArgument),
1001}
1002
1003#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1004pub enum SelfArgument {
1005    Longhand(SelfArgumentLonghand),
1006    Shorthand(SelfArgumentShorthand),
1007}
1008
1009/// The `self` argument with an explicit type
1010///
1011/// ### Example Source
1012///
1013/// ```rust,ignore
1014/// impl A { fn b(self: Box<Self>) {} }
1015/// //            ^^^^^^^^^^^^^^^
1016/// ```
1017#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1018pub struct SelfArgumentLonghand {
1019    pub extent: Extent,
1020    pub is_mut: Option<Extent>,
1021    pub name: Ident,
1022    pub typ: Type,
1023    pub whitespace: Vec<Whitespace>,
1024}
1025
1026/// The `self` argument with an implicit type
1027///
1028/// ### Example Source
1029///
1030/// ```rust,ignore
1031/// impl A { fn b(&mut self) {} }
1032/// //            ^^^^^^^^^
1033/// ```
1034#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1035pub struct SelfArgumentShorthand {
1036    pub extent: Extent,
1037    pub qualifier: Option<SelfArgumentShorthandQualifier>,
1038    pub name: Ident,
1039    pub whitespace: Vec<Whitespace>,
1040}
1041
1042#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1043pub enum SelfArgumentShorthandQualifier {
1044    Reference(TypeReferenceKind),
1045    Mut(Extent),
1046}
1047
1048/// A function argument with a name
1049///
1050/// ### Example Source
1051///
1052/// ```rust,ignore
1053/// fn a(age: u8) {}
1054/// //   ^^^^^^^
1055/// ```
1056#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1057pub struct NamedArgument {
1058    pub extent: Extent,
1059    pub name: Pattern,
1060    pub typ: Type,
1061    pub whitespace: Vec<Whitespace>,
1062}
1063
1064#[derive(Debug, Visit, Decompose)] // HasExtent?
1065pub enum TraitImplArgument {
1066    SelfArgument(SelfArgument),
1067    Named(TraitImplArgumentNamed),
1068}
1069
1070/// An argument of a trait's function declaration
1071///
1072/// ### Example Source
1073///
1074/// ```rust,ignore
1075/// trait X { fn a(b: bool, i32); }
1076/// //             ^^^^^^^  ^^^
1077/// ```
1078// TODO: "Trait impl" sounds confusing; why not just trait or trait defn?
1079// TODO: "named" is a lie here, as well
1080#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1081pub struct TraitImplArgumentNamed {
1082    pub extent: Extent,
1083    pub name: Option<Pattern>,
1084    pub typ: Type,
1085    pub whitespace: Vec<Whitespace>,
1086}
1087
1088/// A single where clause
1089///
1090/// ### Example Source
1091///
1092/// ```rust,ignore
1093/// struct A where for<'a> &'a str: Sized {}
1094/// //             ^^^^^^^^^^^^^^^^^^^^^^
1095/// ```
1096// TODO: rename to where clause?
1097#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1098pub struct Where {
1099    pub extent: Extent,
1100    pub higher_ranked_trait_bounds: Vec<Lifetime>,
1101    pub kind: WhereKind,
1102    pub whitespace: Vec<Whitespace>,
1103}
1104
1105#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1106pub enum WhereKind {
1107    Lifetime(WhereLifetime),
1108    Type(WhereType),
1109}
1110
1111/// A single where clause applying to a lifetime
1112///
1113/// ### Example Source
1114///
1115/// ```rust,ignore
1116/// struct A<'a, 'b> where &'a: &'b {}
1117/// //                     ^^^^^^^^
1118/// ```
1119#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1120pub struct WhereLifetime {
1121    pub extent: Extent,
1122    pub name: Lifetime,
1123    pub bounds: Vec<Lifetime>,
1124    pub whitespace: Vec<Whitespace>,
1125}
1126
1127/// A single where clause applying to a type
1128///
1129/// ### Example Source
1130///
1131/// ```rust,ignore
1132/// struct A<T> where A: Debug {}
1133/// //                ^^^^^^^^
1134/// ```
1135#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1136pub struct WhereType {
1137    pub extent: Extent,
1138    pub name: Type,
1139    pub bounds: TraitBounds,
1140    pub whitespace: Vec<Whitespace>,
1141}
1142
1143/// The trait bounds of a type
1144///
1145/// ### Example Source
1146///
1147/// ```rust,ignore
1148/// struct A<'a, T> where A: 'a + ?Sized + Debug {}
1149/// //                       ^^^^^^^^^^^^^^^^^^^
1150/// ```
1151#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1152pub struct TraitBounds {
1153    pub extent: Extent,
1154    pub types: Vec<TraitBound>,
1155    pub whitespace: Vec<Whitespace>,
1156}
1157
1158#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1159pub enum TraitBound {
1160    Lifetime(TraitBoundLifetime),
1161    Normal(TraitBoundNormal),
1162    Relaxed(TraitBoundRelaxed),
1163}
1164
1165/// A lifetime bound on a type
1166///
1167/// ### Example Source
1168///
1169/// ```rust,ignore
1170/// struct A<'a, T> where A: 'a + ?Sized + Debug {}
1171/// //                       ^^
1172///
1173/// ```
1174#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1175pub struct TraitBoundLifetime {
1176    pub extent: Extent,
1177    pub lifetime: Lifetime,
1178    pub whitespace: Vec<Whitespace>,
1179}
1180
1181/// A standard trait bound on a type
1182///
1183/// ### Example Source
1184///
1185/// ```rust,ignore
1186/// struct A<'a, T> where A: 'a + ?Sized + Debug {}
1187/// //                                     ^^^^^
1188///
1189/// ```
1190#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1191pub struct TraitBoundNormal {
1192    pub extent: Extent,
1193    pub typ: TraitBoundType,
1194    pub whitespace: Vec<Whitespace>,
1195}
1196
1197/// A relaxed trait bound on a type
1198///
1199/// ### Example Source
1200///
1201/// ```rust,ignore
1202/// struct A<'a, T> where A: 'a + ?Sized + Debug {}
1203/// //                            ^^^^^^
1204/// ```
1205#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1206pub struct TraitBoundRelaxed {
1207    pub extent: Extent,
1208    pub typ: TraitBoundType,
1209    pub whitespace: Vec<Whitespace>,
1210}
1211
1212#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1213pub enum TraitBoundType {
1214    Named(TypeNamed),
1215    // TODO: HRTB Trait bounds don't really allow references or fn types, just named
1216    // We need to create a smaller enum here
1217    HigherRankedTraitBounds(TypeHigherRankedTraitBounds),
1218}
1219
1220/// A collection of statements and an optional final expression
1221///
1222/// ### Example Source
1223///
1224/// ```rust,ignore
1225/// fn a() { if true {} else {} }
1226/// //               ^^      ^^
1227/// //     ^^^^^^^^^^^^^^^^^^^^^^
1228/// ```
1229#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1230pub struct Block {
1231    pub extent: Extent,
1232    pub statements: Vec<Statement>,
1233    pub expression: Option<Attributed<Expression>>,
1234    pub whitespace: Vec<Whitespace>,
1235}
1236
1237/// A block which allows calling unsafe code
1238///
1239/// ### Example Source
1240///
1241/// ```rust,ignore
1242/// fn a() { unsafe {} }
1243/// //       ^^^^^^^^^
1244/// ```
1245#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1246pub struct UnsafeBlock {
1247    pub extent: Extent,
1248    pub body: Box<Block>,
1249    pub whitespace: Vec<Whitespace>,
1250}
1251
1252/// A block which allows calling async code
1253///
1254/// ### Example Source
1255///
1256/// ```rust,ignore
1257/// fn a() { async {} }
1258/// //       ^^^^^^^^
1259/// ```
1260#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1261pub struct AsyncBlock {
1262    pub extent: Extent,
1263    pub body: Box<Block>,
1264    pub is_move: Option<Extent>,
1265    pub whitespace: Vec<Whitespace>,
1266}
1267
1268/// An expression surrounded by parenthesis
1269///
1270/// ### Example Source
1271///
1272/// ```rust,ignore
1273/// fn a() { (1 + 1) }
1274/// //       ^^^^^^^
1275/// ```
1276#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1277pub struct Parenthetical {
1278    pub extent: Extent,
1279    pub expression: Box<Attributed<Expression>>,
1280    pub whitespace: Vec<Whitespace>,
1281}
1282
1283#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1284pub enum Statement {
1285    Expression(Attributed<Expression>),
1286    Item(Attributed<Item>),
1287    Empty(Extent),
1288}
1289
1290/// An element that can have attributes applied to it.
1291///
1292/// ### Example Source
1293///
1294/// ```rust,ignore
1295/// #[inline(never)] fn a() {}
1296/// ```
1297#[derive(Debug)]
1298pub struct Attributed<T> {
1299    pub extent: Extent,
1300    pub attributes: Vec<Attribute>,
1301    pub value: T,
1302    pub whitespace: Vec<Whitespace>,
1303}
1304
1305impl<T> HasExtent for Attributed<T> {
1306    fn extent(&self) -> Extent {
1307        self.extent
1308    }
1309}
1310
1311impl<T> std::ops::Deref for Attributed<T> {
1312    type Target = T;
1313    fn deref(&self) -> &Self::Target { &self.value }
1314}
1315
1316macro_rules! visit_attributed {
1317    ($typ:ty, $visit:ident, $exit:ident) => {
1318        impl Visit for Attributed<$typ> {
1319            fn visit<'ast, V>(&'ast self, v: &mut V)
1320            where
1321                V: Visitor<'ast>
1322            {
1323                v.$visit(self);
1324                self.attributes.visit(v);
1325                self.value.visit(v);
1326                v.$exit(self);
1327            }
1328
1329            fn visit_mut<V>(&mut self, v: &mut V)
1330            where
1331                V: VisitorMut
1332            {
1333                v.$visit(self);
1334                self.attributes.visit_mut(v);
1335                self.value.visit_mut(v);
1336                v.$exit(self);
1337            }
1338        }
1339    };
1340}
1341
1342visit_attributed!(EnumVariant, visit_attributed_enum_variant, exit_attributed_enum_variant);
1343visit_attributed!(Expression, visit_attributed_expression, exit_attributed_expression);
1344visit_attributed!(ExternBlockMember, visit_attributed_extern_block_member, exit_attributed_extern_block_member);
1345visit_attributed!(GenericDeclarationConst, visit_attributed_generic_declaration_const, exit_attributed_generic_declaration_const);
1346visit_attributed!(GenericDeclarationLifetime, visit_attributed_generic_declaration_lifetime, exit_attributed_generic_declaration_lifetime);
1347visit_attributed!(GenericDeclarationType, visit_attributed_generic_declaration_type, exit_attributed_generic_declaration_type);
1348visit_attributed!(ImplMember, visit_attributed_impl_member, exit_attributed_impl_member);
1349visit_attributed!(Item, visit_attributed_item, exit_attributed_item);
1350visit_attributed!(StructDefinitionFieldNamed, visit_attributed_struct_definition_field_named, exit_attributed_struct_definition_field_named);
1351visit_attributed!(StructDefinitionFieldUnnamed, visit_attributed_struct_definition_field_unnamed, exit_attributed_struct_definition_field_unnamed);
1352visit_attributed!(TraitMember, visit_attributed_trait_member, exit_attributed_trait_member);
1353
1354// Assumes that there are no attributes
1355impl From<Expression> for Attributed<Expression> {
1356    fn from(value: Expression) -> Attributed<Expression> {
1357        Attributed {
1358            extent: value.extent(),
1359            attributes: vec![],
1360            value,
1361            whitespace: Vec::new(),
1362        }
1363    }
1364}
1365
1366#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1367pub enum Expression {
1368    Array(Array),
1369    AsType(AsType),
1370    Ascription(Ascription),
1371    AsyncBlock(AsyncBlock),
1372    Binary(Binary),
1373    Block(Box<Block>),
1374    Box(ExpressionBox),
1375    Break(Break),
1376    Byte(Byte),
1377    ByteString(ByteString),
1378    Call(Call),
1379    Character(Character),
1380    Closure(Closure),
1381    Continue(Continue),
1382    Dereference(Dereference),
1383    Disambiguation(Disambiguation),
1384    FieldAccess(FieldAccess),
1385    ForLoop(ForLoop),
1386    If(If),
1387    IfLet(IfLet),
1388    Let(Let),
1389    Loop(Loop),
1390    MacroCall(MacroCall),
1391    Match(Match),
1392    Number(Number),
1393    Parenthetical(Parenthetical),
1394    Range(Range),
1395    RangeInclusive(RangeInclusive),
1396    Reference(Reference),
1397    Return(Return),
1398    Slice(Slice),
1399    String(String),
1400    Tuple(Tuple),
1401    TryOperator(TryOperator),
1402    Unary(Unary),
1403    UnsafeBlock(UnsafeBlock),
1404    Value(Value),
1405    While(While),
1406    WhileLet(WhileLet),
1407}
1408
1409impl Expression {
1410    pub(crate) fn may_terminate_statement(&self) -> bool {
1411        match *self {
1412            Expression::Block(_)       |
1413            Expression::ForLoop(_)     |
1414            Expression::If(_)          |
1415            Expression::IfLet(_)       |
1416            Expression::Loop(_)        |
1417            Expression::Match(_)       |
1418            Expression::UnsafeBlock(_) |
1419            Expression::While(_)       |
1420            Expression::WhileLet(_)    |
1421            Expression::MacroCall(MacroCall { args: MacroCallArgs::Curly(_), .. }) => true,
1422            _ => false,
1423        }
1424    }
1425}
1426
1427/// A single unexpanded macro
1428///
1429/// ### Example Source
1430///
1431/// ```rust,ignore
1432/// fn a() { println!("Hello, world!"); }
1433/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^
1434/// ```
1435#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1436pub struct MacroCall {
1437    pub extent: Extent,
1438    pub name: PathedIdent,
1439    pub arg: Option<Ident>,
1440    pub args: MacroCallArgs,
1441    pub whitespace: Vec<Whitespace>,
1442}
1443
1444#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1445pub enum MacroCallArgs {
1446    Paren(Extent),
1447    Curly(Extent),
1448    Square(Extent),
1449}
1450
1451/// A variable declaration
1452///
1453/// ### Example Source
1454///
1455/// ```rust,ignore
1456/// fn a() { let b: u8 = 42; }
1457/// //       ^^^^^^^^^^^^^^
1458/// ```
1459#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1460pub struct Let {
1461    pub extent: Extent,
1462    pub pattern: Pattern,
1463    pub typ: Option<Type>,
1464    pub value: Option<Box<Attributed<Expression>>>,
1465    pub whitespace: Vec<Whitespace>,
1466}
1467
1468/// A tuple expression
1469///
1470/// ### Example Source
1471///
1472/// ```rust,ignore
1473/// fn a() { (100, true, 42.42); }
1474/// //       ^^^^^^^^^^^^^^^^^^
1475/// ```
1476#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1477pub struct Tuple {
1478    pub extent: Extent,
1479    pub members: Vec<Attributed<Expression>>,
1480    pub whitespace: Vec<Whitespace>,
1481}
1482
1483/// The question mark / try / early exit operator
1484///
1485/// ### Example Source
1486///
1487/// ```rust,ignore
1488/// fn a() { 42?; }
1489/// //       ^^^
1490/// ```
1491#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1492pub struct TryOperator {
1493    pub extent: Extent,
1494    pub target: Box<Attributed<Expression>>,
1495    pub whitespace: Vec<Whitespace>,
1496}
1497
1498/// Access to a field of a struct
1499///
1500/// ### Example Source
1501///
1502/// ```rust,ignore
1503/// fn a() { thing.one; }
1504/// //       ^^^^^^^^^
1505/// ```
1506#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1507pub struct FieldAccess {
1508    pub extent: Extent,
1509    pub target: Box<Attributed<Expression>>,
1510    pub field: FieldName,
1511    pub whitespace: Vec<Whitespace>,
1512}
1513
1514#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1515pub enum FieldName {
1516    Path(PathComponent),
1517    Number(Extent),
1518}
1519
1520/// A number literal
1521///
1522/// ### Example Source
1523///
1524/// ```rust,ignore
1525/// fn a() { 0xDEAD_BEEF; }
1526/// //       ^^^^^^^^^^^
1527/// ```
1528#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1529pub struct Number {
1530    pub extent: Extent,
1531    pub is_negative: Option<Extent>,
1532    pub value: NumberValue,
1533    pub whitespace: Vec<Whitespace>,
1534}
1535
1536#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1537pub enum NumberValue {
1538    Binary(NumberBinary),
1539    Decimal(NumberDecimal),
1540    Hexadecimal(NumberHexadecimal),
1541    Octal(NumberOctal),
1542}
1543
1544/// A binary number literal
1545///
1546/// ### Example Source
1547///
1548/// ```rust,ignore
1549/// fn a() { 0b0110_u8; }
1550/// //       ^^^^^^^^^
1551/// ```
1552#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1553pub struct NumberBinary {
1554    pub extent: Extent,
1555    pub decimal: Extent,
1556    pub fraction: Option<Extent>,
1557    pub exponent: Option<Extent>,
1558    pub suffix: Option<Extent>,
1559}
1560
1561/// A decimal number literal
1562///
1563/// ### Example Source
1564///
1565/// ```rust,ignore
1566/// fn a() { 1234.5678_f32; }
1567/// //       ^^^^^^^^^^^^^
1568/// ```
1569#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1570pub struct NumberDecimal {
1571    pub extent: Extent,
1572    pub decimal: Extent,
1573    pub fraction: Option<Extent>,
1574    pub exponent: Option<Extent>,
1575    pub suffix: Option<Extent>,
1576}
1577
1578/// A hexadecimal number literal
1579///
1580/// ### Example Source
1581///
1582/// ```rust,ignore
1583/// fn a() { 0xAA_BB_CC_DD; }
1584/// //       ^^^^^^^^^^^^^
1585/// ```
1586#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1587pub struct NumberHexadecimal {
1588    pub extent: Extent,
1589    pub decimal: Extent,
1590    pub fraction: Option<Extent>,
1591    pub exponent: Option<Extent>,
1592    pub suffix: Option<Extent>,
1593}
1594
1595/// An octal number literal
1596///
1597/// ### Example Source
1598///
1599/// ```rust,ignore
1600/// fn a() { 0o0755; }
1601/// //       ^^^^^^
1602/// ```
1603#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1604pub struct NumberOctal {
1605    pub extent: Extent,
1606    pub decimal: Extent,
1607    pub fraction: Option<Extent>,
1608    pub exponent: Option<Extent>,
1609    pub suffix: Option<Extent>,
1610}
1611
1612/// A variable expression
1613///
1614/// ### Example Source
1615///
1616/// ```rust,ignore
1617/// fn a() { my_variable; }
1618/// //       ^^^^^^^^^^^
1619/// ```
1620// TODO: This name is too generic
1621#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1622pub struct Value {
1623    pub extent: Extent,
1624    pub name: PathedIdent,
1625    pub literal: Option<StructLiteral>,
1626    pub whitespace: Vec<Whitespace>,
1627}
1628
1629/// Literal creation of a struct
1630///
1631/// ### Example Source
1632///
1633/// ```rust,ignore
1634/// fn a() { Monster { hp: 42, gold: 100 } }
1635/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1636/// ```
1637#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1638pub struct StructLiteral {
1639    pub extent: Extent,
1640    pub fields: Vec<StructLiteralField>,
1641    pub splat: Option<Box<Attributed<Expression>>>,
1642    pub whitespace: Vec<Whitespace>,
1643}
1644
1645/// A field of a struct literal
1646///
1647/// ### Example Source
1648///
1649/// ```rust,ignore
1650/// fn a() { Monster { hp: 42, gold: 100 } }
1651/// //                 ^^^^^^  ^^^^^^^^^
1652/// ```
1653#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1654pub struct StructLiteralField {
1655    pub extent: Extent,
1656    pub name: Ident,
1657    pub value: Attributed<Expression>,
1658    pub whitespace: Vec<Whitespace>,
1659}
1660
1661/// A function, method, or closure call
1662///
1663/// ### Example Source
1664///
1665/// ```rust,ignore
1666/// fn a() { greet_user("Vivian"); }
1667/// //       ^^^^^^^^^^^^^^^^^^^^
1668/// ```
1669#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1670pub struct Call {
1671    pub extent: Extent,
1672    pub target: Box<Attributed<Expression>>,
1673    pub args: Vec<Attributed<Expression>>,
1674    pub whitespace: Vec<Whitespace>,
1675}
1676
1677/// The iterator-based loop
1678///
1679/// ### Example Source
1680///
1681/// ```rust,ignore
1682/// fn a() { for i in 0..10 {} }
1683/// //       ^^^^^^^^^^^^^^^^^
1684/// ```
1685#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1686pub struct ForLoop {
1687    pub extent: Extent,
1688    pub label: Option<Lifetime>,
1689    pub pattern: Pattern,
1690    pub iter: Box<Attributed<Expression>>,
1691    pub body: Box<Block>,
1692    pub whitespace: Vec<Whitespace>,
1693}
1694
1695/// The infinite loop
1696///
1697/// ### Example Source
1698///
1699/// ```rust,ignore
1700/// fn a() { loop {} }
1701/// //       ^^^^^^^
1702/// ```
1703#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1704pub struct Loop {
1705    pub extent: Extent,
1706    pub label: Option<Lifetime>,
1707    pub body: Box<Block>,
1708    pub whitespace: Vec<Whitespace>,
1709}
1710
1711/// The conditional, pattern-matching variable scope
1712///
1713/// ### Example Source
1714///
1715/// ```rust,ignore
1716/// fn a() { if let Some(name) = current_player {} }
1717/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1718/// ```
1719#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1720pub struct IfLet {
1721    pub extent: Extent,
1722    pub pattern: Pattern,
1723    pub value: Box<Attributed<Expression>>,
1724    pub body: Box<Block>,
1725    pub whitespace: Vec<Whitespace>,
1726}
1727
1728/// The boolean-based loop
1729///
1730/// ### Example Source
1731///
1732/// ```rust,ignore
1733/// fn a() { while players_count < 1 {} }
1734/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^
1735/// ```
1736#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1737pub struct While {
1738    pub extent: Extent,
1739    pub label: Option<Lifetime>,
1740    pub value: Box<Attributed<Expression>>,
1741    pub body: Box<Block>,
1742    pub whitespace: Vec<Whitespace>,
1743}
1744
1745/// The conditional, pattern-matching loop
1746///
1747/// ### Example Source
1748///
1749/// ```rust,ignore
1750/// fn a() { while let Some(i) = iterator.next() {} }
1751/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1752/// ```
1753#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1754pub struct WhileLet {
1755    pub extent: Extent,
1756    pub label: Option<Lifetime>,
1757    pub pattern: Pattern,
1758    pub value: Box<Attributed<Expression>>,
1759    pub body: Box<Block>,
1760    pub whitespace: Vec<Whitespace>,
1761}
1762
1763/// A unary operator
1764///
1765/// ### Example Source
1766///
1767/// ```rust,ignore
1768/// fn a() { !false; }
1769/// //       ^^^^^^
1770/// ```
1771// TODO: Should this be the same as dereference? What about reference?
1772#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1773pub struct Unary {
1774    pub extent: Extent,
1775    #[visit(ignore)]
1776    pub op: UnaryOp,
1777    pub value: Box<Attributed<Expression>>,
1778    pub whitespace: Vec<Whitespace>,
1779}
1780
1781#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1782pub enum UnaryOp {
1783    Negate,
1784    Not,
1785}
1786
1787/// A binary operator
1788///
1789/// ### Example Source
1790///
1791/// ```rust,ignore
1792/// fn a() { 1 + 1; }
1793/// //       ^^^^^
1794/// ```
1795#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1796pub struct Binary {
1797    pub extent: Extent,
1798    #[visit(ignore)]
1799    pub op: BinaryOp,
1800    pub lhs: Box<Attributed<Expression>>,
1801    pub rhs: Box<Attributed<Expression>>,
1802    pub whitespace: Vec<Whitespace>,
1803}
1804
1805#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1806pub enum BinaryOp {
1807    Add,
1808    AddAssign,
1809    Assign,
1810    BitwiseAnd,
1811    BitwiseAndAssign,
1812    BitwiseOr,
1813    BitwiseOrAssign,
1814    BitwiseXor,
1815    BitwiseXorAssign,
1816    BooleanAnd,
1817    BooleanOr,
1818    Div,
1819    DivAssign,
1820    Equal,
1821    GreaterThan,
1822    GreaterThanOrEqual,
1823    LessThan,
1824    LessThanOrEqual,
1825    Mod,
1826    ModAssign,
1827    Mul,
1828    MulAssign,
1829    NotEqual,
1830    ShiftLeft,
1831    ShiftLeftAssign,
1832    ShiftRight,
1833    ShiftRightAssign,
1834    Sub,
1835    SubAssign,
1836}
1837
1838/// Boolean conditional control flow
1839///
1840/// ### Example Source
1841///
1842/// ```rust,ignore
1843/// fn a() { if a {} else if b {} else {} }
1844/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1845/// ```
1846#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1847pub struct If {
1848    pub extent: Extent,
1849    pub condition: Box<Attributed<Expression>>,
1850    pub body: Box<Block>,
1851    pub more: Vec<If>,
1852    pub else_body: Option<Box<Block>>,
1853    pub whitespace: Vec<Whitespace>,
1854}
1855
1856/// Pattern-matching conditional control flow
1857///
1858/// ### Example Source
1859///
1860/// ```rust,ignore
1861/// fn a() { match 1 { 0 => true, 1 => { false } _ => true } }
1862/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1863/// ```
1864#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1865pub struct Match {
1866    pub extent: Extent,
1867    pub head: Box<Attributed<Expression>>,
1868    pub arms: Vec<MatchArm>,
1869    pub whitespace: Vec<Whitespace>,
1870}
1871
1872/// A single pattern of a `match`
1873///
1874/// ### Example Source
1875///
1876/// ```rust,ignore
1877/// fn a() { match 1 { 0 if false => true, _ => { true } } }
1878/// //                 ^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^
1879/// ```
1880#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1881pub struct MatchArm {
1882    pub extent: Extent,
1883    pub attributes: Vec<Attribute>,
1884    pub pattern: Vec<Pattern>,
1885    pub guard: Option<Attributed<Expression>>,
1886    pub hand: MatchHand,
1887    pub whitespace: Vec<Whitespace>,
1888}
1889
1890#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1891pub enum MatchHand {
1892    Brace(Attributed<Expression>),
1893    Expression(Attributed<Expression>),
1894}
1895
1896/// An exclusive range
1897///
1898/// ### Example Source
1899///
1900/// ```rust,ignore
1901/// fn a() { 0..10; }
1902/// //       ^^^^^
1903/// ```
1904#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1905// TODO: rename "exclusive"
1906pub struct Range {
1907    pub extent: Extent,
1908    pub lhs: Option<Box<Attributed<Expression>>>,
1909    pub rhs: Option<Box<Attributed<Expression>>>,
1910    pub whitespace: Vec<Whitespace>,
1911}
1912
1913/// An inclusive range
1914///
1915/// ### Example Source
1916///
1917/// ```rust,ignore
1918/// fn a() { 0..=10; }
1919/// //       ^^^^^^
1920/// ```
1921#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1922pub struct RangeInclusive {
1923    pub extent: Extent,
1924    pub lhs: Option<Box<Attributed<Expression>>>,
1925    #[visit(ignore)]
1926    pub operator: RangeInclusiveOperator,
1927    pub rhs: Option<Box<Attributed<Expression>>>,
1928    pub whitespace: Vec<Whitespace>,
1929}
1930
1931#[derive(Debug, HasExtent, ExtentIndex, Decompose)]
1932pub enum RangeInclusiveOperator {
1933    Legacy(Extent),
1934    Recommended(Extent),
1935}
1936
1937#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
1938pub enum Array {
1939    Explicit(ArrayExplicit),
1940    Repeated(ArrayRepeated),
1941}
1942
1943/// An array with all members explcitly listed
1944///
1945/// ### Example Source
1946///
1947/// ```rust,ignore
1948/// fn a() { [1, 2, 3]; }
1949/// //       ^^^^^^^^^
1950/// ```
1951#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1952pub struct ArrayExplicit {
1953    pub extent: Extent,
1954    pub values: Vec<Attributed<Expression>>,
1955    pub whitespace: Vec<Whitespace>,
1956}
1957
1958/// An array with an example value and a length
1959///
1960/// ### Example Source
1961///
1962/// ```rust,ignore
1963/// fn a() { [42; 10]; }
1964/// //       ^^^^^^^^
1965/// ```
1966#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1967pub struct ArrayRepeated {
1968    pub extent: Extent,
1969    pub value: Box<Attributed<Expression>>,
1970    pub count: Box<Attributed<Expression>>,
1971    pub whitespace: Vec<Whitespace>,
1972}
1973
1974/// The `box` keyword expression
1975///
1976/// ### Example Source
1977///
1978/// ```rust,ignore
1979/// fn a() { box 42; }
1980/// //       ^^^^^^
1981/// ```
1982// TODO: Rename this visitor function?
1983#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1984pub struct ExpressionBox {
1985    pub extent: Extent,
1986    pub target: Box<Attributed<Expression>>,
1987    pub whitespace: Vec<Whitespace>,
1988}
1989
1990/// Inline type conversion
1991///
1992/// ### Example Source
1993///
1994/// ```rust,ignore
1995/// fn a() { 42u8 as u64; }
1996/// //       ^^^^^^^^^^^
1997/// ```
1998#[derive(Debug, HasExtent, ExtentIndex, Visit)]
1999pub struct AsType {
2000    pub extent: Extent,
2001    pub target: Box<Attributed<Expression>>,
2002    pub typ: Type,
2003    pub whitespace: Vec<Whitespace>,
2004}
2005
2006/// Inline type specification
2007///
2008/// ### Example Source
2009///
2010/// ```rust,ignore
2011/// fn a() { iterator.collect() : Vec<_>; }
2012/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013/// ```
2014#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2015pub struct Ascription {
2016    pub extent: Extent,
2017    pub target: Box<Attributed<Expression>>,
2018    pub typ: Type,
2019    pub whitespace: Vec<Whitespace>,
2020}
2021
2022/// A character literal
2023///
2024/// ### Example Source
2025///
2026/// ```rust,ignore
2027/// fn a() { 'x'; }
2028/// //       ^^^
2029/// ```
2030#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2031pub struct Character {
2032    pub extent: Extent,
2033    pub value: Extent,
2034}
2035
2036/// A string literal
2037///
2038/// ### Example Source
2039///
2040/// ```rust,ignore
2041/// fn a() { "hello"; }
2042/// //       ^^^^^^^
2043/// ```
2044#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2045pub struct String {
2046    pub extent: Extent,
2047    pub value: Extent,
2048}
2049
2050/// A byte literal
2051///
2052/// ### Example Source
2053///
2054/// ```rust,ignore
2055/// fn a() { b'x'; }
2056/// //       ^^^^
2057/// ```
2058#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2059pub struct Byte {
2060    pub extent: Extent,
2061    pub value: Character,
2062}
2063
2064/// A byte string literal
2065///
2066/// ### Example Source
2067///
2068/// ```rust,ignore
2069/// fn a() { b"hello"; }
2070/// //       ^^^^^^^^
2071/// ```
2072#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2073pub struct ByteString {
2074    pub extent: Extent,
2075    pub value: String,
2076}
2077
2078/// The square-bracket operator for slicing and indexing
2079///
2080/// ### Example Source
2081///
2082/// ```rust,ignore
2083/// fn a() { value[0]; }
2084/// //       ^^^^^^^^
2085/// ```
2086// TODO: rename to "index"?
2087#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2088pub struct Slice {
2089    pub extent: Extent,
2090    pub target: Box<Attributed<Expression>>,
2091    pub index: Box<Attributed<Expression>>,
2092    pub whitespace: Vec<Whitespace>,
2093}
2094
2095/// A closure
2096///
2097/// ### Example Source
2098///
2099/// ```rust,ignore
2100/// fn a() { move |a| a + 2; }
2101/// //       ^^^^^^^^^^^^^^
2102/// ```
2103#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2104pub struct Closure {
2105    pub extent: Extent,
2106    pub is_async: Option<Extent>,
2107    pub is_move: Option<Extent>,
2108    pub args: Vec<ClosureArg>,
2109    pub return_type: Option<Type>,
2110    pub body: Box<Attributed<Expression>>,
2111    pub whitespace: Vec<Whitespace>,
2112}
2113
2114/// A closure's argument
2115///
2116/// ### Example Source
2117///
2118/// ```rust,ignore
2119/// fn a() { |a, b: i32| a + b; }
2120/// //        ^  ^^^^^^
2121/// ```
2122#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2123pub struct ClosureArg {
2124    pub extent: Extent,
2125    pub name: Pattern,
2126    pub typ: Option<Type>,
2127    pub whitespace: Vec<Whitespace>,
2128}
2129
2130/// A reference of an expression
2131///
2132/// ### Example Source
2133///
2134/// ```rust,ignore
2135/// fn a() { & mut 42; }
2136/// //       ^^^^^^^^
2137/// ```
2138#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2139pub struct Reference {
2140    pub extent: Extent,
2141    pub is_mutable: Option<Extent>,
2142    pub target: Box<Attributed<Expression>>,
2143    pub whitespace: Vec<Whitespace>,
2144}
2145
2146/// A dereference of an expression
2147///
2148/// ### Example Source
2149///
2150/// ```rust,ignore
2151/// fn a() { *42; }
2152/// //       ^^^
2153/// ```
2154#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2155pub struct Dereference {
2156    pub extent: Extent,
2157    pub target: Box<Attributed<Expression>>,
2158    pub whitespace: Vec<Whitespace>,
2159}
2160
2161/// Type disambiguation
2162///
2163/// ### Example Source
2164///
2165/// ```rust,ignore
2166/// fn a() { <Vec<u8> as IntoIterator>::into_iter(scores); }
2167/// //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2168/// ```
2169#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2170pub struct Disambiguation {
2171    pub extent: Extent,
2172    pub from_type: Type,
2173    pub to_type: Option<TypeNamed>,
2174    pub components: Vec<PathComponent>,
2175    pub whitespace: Vec<Whitespace>,
2176}
2177
2178/// Exit from a function or closure
2179///
2180/// ### Example Source
2181///
2182/// ```rust,ignore
2183/// fn a() { return 42; }
2184/// //       ^^^^^^^^^
2185/// ```
2186#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2187pub struct Return {
2188    pub extent: Extent,
2189    pub value: Option<Box<Attributed<Expression>>>,
2190    pub whitespace: Vec<Whitespace>,
2191}
2192
2193/// Advance to the next iteration of a loop
2194///
2195/// ### Example Source
2196///
2197/// ```rust,ignore
2198/// fn a() { 'v: loop { continue 'v; } }
2199/// //                  ^^^^^^^^^^^
2200/// ```
2201#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2202pub struct Continue {
2203    pub extent: Extent,
2204    pub label: Option<Lifetime>,
2205    pub whitespace: Vec<Whitespace>,
2206}
2207
2208/// Exit from a loop
2209///
2210/// ### Example Source
2211///
2212/// ```rust,ignore
2213/// fn a() { loop { break 42; } }
2214/// //              ^^^^^^^^
2215/// ```
2216#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2217pub struct Break {
2218    pub extent: Extent,
2219    pub label: Option<Lifetime>,
2220    pub value: Option<Box<Attributed<Expression>>>,
2221    pub whitespace: Vec<Whitespace>,
2222}
2223
2224/// A component used in pattern matching
2225///
2226/// ### Example Source
2227///
2228/// ```rust,ignore
2229/// fn a(mut b: i32) { if let Some(foo) = x {} }
2230/// //   ^^^^^                ^^^^^^^^^
2231/// ```
2232#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2233pub struct Pattern {
2234    pub extent: Extent,
2235    pub name: Option<PatternName>,
2236    pub kind: PatternKind,
2237    pub whitespace: Vec<Whitespace>,
2238}
2239
2240/// A renaming of a matched pattern
2241///
2242/// ### Example Source
2243///
2244/// ```rust,ignore
2245/// fn a() { let ref mut a @ _; }
2246/// //           ^^^^^^^^^^^
2247/// ```
2248// TODO: clarify name to show it's stranger?
2249#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2250pub struct PatternName {
2251    pub extent: Extent,
2252    pub is_ref: Option<Extent>,
2253    pub is_mut: Option<Extent>,
2254    pub name: Ident,
2255    pub whitespace: Vec<Whitespace>,
2256}
2257
2258#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2259pub enum PatternKind {
2260    Box(PatternBox),
2261    Byte(PatternByte),
2262    ByteString(PatternByteString),
2263    Character(PatternCharacter),
2264    Ident(PatternIdent), // TODO: split into ident and enumtuple
2265    MacroCall(PatternMacroCall),
2266    Number(PatternNumber),
2267    RangeExclusive(PatternRangeExclusive),
2268    RangeInclusive(PatternRangeInclusive),
2269    Reference(PatternReference),
2270    Slice(PatternSlice),
2271    String(PatternString),
2272    Struct(PatternStruct),
2273    Tuple(PatternTuple),
2274}
2275
2276/// A basic pattern match
2277///
2278/// ### Example Source
2279///
2280/// ```rust,ignore
2281/// fn a() { let ref mut b; }
2282/// //           ^^^^^^^^^
2283/// ```
2284#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2285pub struct PatternIdent {
2286    pub extent: Extent,
2287    pub is_ref: Option<Extent>,
2288    pub is_mut: Option<Extent>,
2289    pub ident: PathedIdent,
2290    pub tuple: Option<PatternTuple>,
2291    pub whitespace: Vec<Whitespace>,
2292}
2293
2294/// Pattern matching against a struct
2295///
2296/// ### Example Source
2297///
2298/// ```rust,ignore
2299/// fn a() { let Monster { name, .. }; }
2300/// //           ^^^^^^^^^^^^^^^^^^^^
2301/// ```
2302#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2303pub struct PatternStruct {
2304    pub extent: Extent,
2305    pub name: PathedIdent,
2306    pub fields: Vec<PatternStructField>,
2307    #[visit(ignore)]
2308    pub wildcard: bool,
2309    pub whitespace: Vec<Whitespace>,
2310}
2311
2312#[derive(Debug, Visit, Decompose)] // TODO: HasExtent?
2313pub enum PatternStructField {
2314    Long(PatternStructFieldLong),
2315    Short(PatternStructFieldShort),
2316}
2317
2318/// Pattern matching a struct's field with recursive patterns
2319///
2320/// ### Example Source
2321///
2322/// ```rust,ignore
2323/// fn a() { let Monster { name: scary_name }; }
2324/// //                     ^^^^^^^^^^^^^^^^
2325/// ```
2326#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2327pub struct PatternStructFieldLong {
2328    pub extent: Extent,
2329    pub name: Ident,
2330    pub pattern: Pattern,
2331    pub whitespace: Vec<Whitespace>,
2332}
2333
2334/// Pattern matching a struct's field with simple names
2335///
2336/// ### Example Source
2337///
2338/// ```rust,ignore
2339/// fn a() { let Monster { name }; }
2340/// //                     ^^^^
2341/// ```
2342#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2343pub struct PatternStructFieldShort {
2344    pub extent: Extent,
2345    pub ident: PatternIdent,
2346    pub whitespace: Vec<Whitespace>,
2347}
2348
2349/// Pattern matching a tuple
2350///
2351/// ### Example Source
2352///
2353/// ```rust,ignore
2354/// fn a() { let (tx, rx); }
2355/// //           ^^^^^^^^
2356/// ```
2357#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2358pub struct PatternTuple {
2359    pub extent: Extent,
2360    pub members: Vec<PatternTupleMember>,
2361    pub whitespace: Vec<Whitespace>,
2362}
2363
2364#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2365pub enum PatternTupleMember {
2366    Pattern(Pattern),
2367    Wildcard(Extent),
2368}
2369
2370/// Pattern matching a slice
2371///
2372/// ### Example Source
2373///
2374/// ```rust,ignore
2375/// fn a() { let [a, b, ..]; }
2376/// //           ^^^^^^^^^^
2377/// ```
2378#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2379pub struct PatternSlice {
2380    pub extent: Extent,
2381    pub members: Vec<PatternSliceMember>,
2382    pub whitespace: Vec<Whitespace>,
2383}
2384
2385#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2386pub enum PatternSliceMember {
2387    Pattern(Pattern),
2388    Subslice(PatternSliceSubslice),
2389    Wildcard(Extent),
2390}
2391
2392/// Pattern matching a subslice
2393///
2394/// ### Example Source
2395///
2396/// ```rust,ignore
2397/// fn a() { let [a, ref mut b..]; }
2398/// //               ^^^^^^^^^^^
2399/// ```
2400#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2401pub struct PatternSliceSubslice {
2402    pub extent: Extent,
2403    pub is_ref: Option<Extent>,
2404    pub is_mut: Option<Extent>,
2405    pub name: Ident,
2406    pub whitespace: Vec<Whitespace>,
2407}
2408
2409/// Pattern matching a byte literal
2410///
2411/// ### Example Source
2412///
2413/// ```rust,ignore
2414/// fn a() { let Some(b'x'); }
2415/// //                ^^^^
2416/// ```
2417#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2418pub struct PatternByte {
2419    pub extent: Extent,
2420    pub value: Byte,
2421}
2422
2423/// Pattern matching a character literal
2424///
2425/// ### Example Source
2426///
2427/// ```rust,ignore
2428/// fn a() { let Some('x') }
2429/// //                ^^^
2430/// ```
2431#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2432pub struct PatternCharacter {
2433    pub extent: Extent,
2434    pub value: Character,
2435}
2436
2437/// Pattern matching a byte string literal
2438///
2439/// ### Example Source
2440///
2441/// ```rust,ignore
2442/// fn a() { let Some(b"abc") }
2443/// //                ^^^^^^
2444/// ```
2445#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2446pub struct PatternByteString {
2447    pub extent: Extent,
2448    pub value: ByteString,
2449}
2450
2451/// Pattern matching a string literal
2452///
2453/// ### Example Source
2454///
2455/// ```rust,ignore
2456/// fn a() { let Some("abc"); }
2457/// //                ^^^^^
2458/// ```
2459#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2460pub struct PatternString {
2461    pub extent: Extent,
2462    pub value: String,
2463}
2464
2465/// Pattern matching a number literal
2466///
2467/// ### Example Source
2468///
2469/// ```rust,ignore
2470/// fn a() { let 0xDEAD_BEEF; }
2471/// ```
2472#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2473pub struct PatternNumber {
2474    pub extent: Extent,
2475    pub is_negative: Option<Extent>,
2476    pub value: Number,
2477    pub whitespace: Vec<Whitespace>,
2478}
2479
2480/// A macro call that expands into a pattern
2481///
2482/// ### Example Source
2483///
2484/// ```rust,ignore
2485/// fn a() { let magic!(); }
2486/// //           ^^^^^^^^
2487/// ```
2488#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2489pub struct PatternMacroCall {
2490    pub extent: Extent,
2491    pub value: MacroCall,
2492    pub whitespace: Vec<Whitespace>,
2493}
2494
2495/// Pattern matching against an exclusive range
2496///
2497/// ### Example Source
2498///
2499/// ```rust,ignore
2500/// fn a() { let 0..10; }
2501/// //           ^^^^^
2502/// ```
2503#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2504pub struct PatternRangeExclusive {
2505    pub extent: Extent,
2506    pub start: PatternRangeComponent,
2507    pub end: PatternRangeComponent,
2508    pub whitespace: Vec<Whitespace>,
2509}
2510
2511/// Pattern matching against an inclusive range
2512///
2513/// ### Example Source
2514///
2515/// ```rust,ignore
2516/// fn a() { let 0..=10; }
2517/// //           ^^^^^^
2518/// ```
2519#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2520pub struct PatternRangeInclusive {
2521    pub extent: Extent,
2522    pub start: PatternRangeComponent,
2523    #[visit(ignore)]
2524    pub operator: RangeInclusiveOperator,
2525    pub end: PatternRangeComponent,
2526    pub whitespace: Vec<Whitespace>,
2527}
2528
2529#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2530pub enum PatternRangeComponent {
2531    Ident(PathedIdent),
2532    Byte(Byte),
2533    Character(Character),
2534    Number(PatternNumber),
2535}
2536
2537/// Pattern matching against a reference
2538///
2539/// ### Example Source
2540///
2541/// ```rust,ignore
2542/// fn a() { let &mut x; }
2543/// //           ^^^^^^
2544/// ```
2545#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2546pub struct PatternReference {
2547    pub extent: Extent,
2548    pub is_mut: Option<Extent>,
2549    pub pattern: Box<Pattern>,
2550    pub whitespace: Vec<Whitespace>,
2551}
2552
2553/// Pattern matching against a box
2554///
2555/// ### Example Source
2556///
2557/// ```rust,ignore
2558/// fn a() { let box x; }
2559/// //           ^^^^^
2560/// ```
2561#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2562pub struct PatternBox {
2563    pub extent: Extent,
2564    pub pattern: Box<Pattern>,
2565    pub whitespace: Vec<Whitespace>,
2566}
2567
2568/// Defines a trait
2569///
2570/// ### Example Source
2571///
2572/// ```rust,ignore
2573/// pub trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; }
2574/// ```
2575#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2576pub struct Trait {
2577    pub extent: Extent,
2578    pub visibility: Option<Visibility>,
2579    pub is_unsafe: Option<Extent>,
2580    pub is_auto: Option<Extent>,
2581    pub name: Ident,
2582    pub generics: Option<GenericDeclarations>,
2583    pub bounds: Option<TraitBounds>,
2584    pub wheres: Vec<Where>,
2585    pub members: Vec<Attributed<TraitMember>>,
2586    pub whitespace: Vec<Whitespace>,
2587}
2588
2589#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2590pub enum TraitMember {
2591    Const(TraitMemberConst),
2592    Function(TraitMemberFunction),
2593    Type(TraitMemberType),
2594    MacroCall(MacroCall),
2595}
2596
2597/// A trait's function
2598///
2599/// ### Example Source
2600///
2601/// ```rust,ignore
2602/// pub trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; }
2603/// //                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2604/// ```
2605#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2606pub struct TraitMemberFunction {
2607    pub extent: Extent,
2608    pub header: TraitImplFunctionHeader,
2609    pub body: Option<Block>,
2610    pub whitespace: Vec<Whitespace>,
2611}
2612
2613/// A trait's associated type
2614///
2615/// ### Example Source
2616///
2617/// ```rust,ignore
2618/// pub trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; }
2619/// //                   ^^^^^^^^^^
2620/// ```
2621#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2622pub struct TraitMemberType {
2623    pub extent: Extent,
2624    pub name: Ident,
2625    pub bounds: Option<TraitBounds>,
2626    pub default: Option<Type>,
2627    pub whitespace: Vec<Whitespace>,
2628}
2629
2630/// A trait's associated constant
2631///
2632/// ### Example Source
2633///
2634/// ```rust,ignore
2635/// pub trait Number { const MAX: Self; }
2636/// //                 ^^^^^^^^^^^^^^^^
2637/// ```
2638#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2639pub struct TraitMemberConst {
2640    pub extent: Extent,
2641    pub name: Ident,
2642    pub typ: Type,
2643    pub value: Option<Attributed<Expression>>,
2644    pub whitespace: Vec<Whitespace>,
2645}
2646
2647/// Implementation details for a type
2648///
2649/// ### Example Source
2650///
2651/// ```rust,ignore
2652/// impl Ogre { const GOLD: u8 = 200; fn health(&self) -> u16 { 42 } }
2653/// ```
2654#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2655pub struct Impl {
2656    pub extent: Extent,
2657    pub is_unsafe: Option<Extent>,
2658    pub generics: Option<GenericDeclarations>,
2659    pub kind: ImplKind,
2660    pub wheres: Vec<Where>,
2661    pub body: Vec<Attributed<ImplMember>>,
2662    pub whitespace: Vec<Whitespace>,
2663}
2664
2665#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2666pub enum ImplKind {
2667    Trait(ImplOfTrait),
2668    Inherent(ImplOfInherent),
2669}
2670
2671/// An implementation of a trait for a type
2672///
2673/// ### Example Source
2674///
2675/// ```rust,ignore
2676/// impl Monster for Ogre { }
2677/// //   ^^^^^^^^^^^^^^^^
2678/// ```
2679#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2680pub struct ImplOfTrait {
2681    pub extent: Extent,
2682    pub is_negative: Option<Extent>,
2683    pub trait_name: Type, // TODO: namedtype only?
2684    pub type_name: ImplOfTraitType,
2685    pub whitespace: Vec<Whitespace>,
2686}
2687
2688/// Inherent implementation for a type
2689///
2690/// ### Example Source
2691///
2692/// ```rust,ignore
2693/// impl Ogre {}
2694/// //   ^^^^
2695/// ```
2696#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2697pub struct ImplOfInherent {
2698    pub extent: Extent,
2699    pub type_name: Type,
2700    pub whitespace: Vec<Whitespace>,
2701}
2702
2703#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2704pub enum ImplOfTraitType {
2705    Type(Type),
2706    Wildcard(Extent),
2707}
2708
2709#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2710pub enum ImplMember {
2711    Const(ImplConst),
2712    Function(ImplFunction),
2713    Type(ImplType),
2714    MacroCall(MacroCall),
2715}
2716
2717/// A function in an implementation block
2718///
2719/// ### Example Source
2720///
2721/// ```rust,ignore
2722/// impl Ogre { fn roar(&self) {} }
2723/// //          ^^^^^^^^^^^^^^^^^
2724/// ```
2725#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2726pub struct ImplFunction {
2727    pub extent: Extent,
2728    pub header: FunctionHeader,
2729    pub body: Block,
2730    pub whitespace: Vec<Whitespace>,
2731}
2732
2733/// A type in an implementation block
2734///
2735/// ### Example Source
2736///
2737/// ```rust,ignore
2738/// impl Monster for Ogre { type Gold = u8; }
2739/// //                      ^^^^^^^^^^^^^^^
2740/// ```
2741#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2742pub struct ImplType {
2743    pub extent: Extent,
2744    pub name: Ident,
2745    pub typ: Type,
2746    pub whitespace: Vec<Whitespace>,
2747}
2748
2749/// A constant in an implementation block
2750///
2751/// ### Example Source
2752///
2753/// ```rust,ignore
2754/// impl Ogre { const GOLD: u8 = 42; }
2755/// //          ^^^^^^^^^^^^^^^^^^^^
2756/// ```
2757#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2758pub struct ImplConst {
2759    pub extent: Extent,
2760    pub visibility: Option<Visibility>,
2761    pub name: Ident,
2762    pub typ: Type,
2763    pub value: Attributed<Expression>,
2764    pub whitespace: Vec<Whitespace>,
2765}
2766
2767/// An extern crate declaration
2768///
2769/// ### Example Source
2770///
2771/// ```rust,ignore
2772/// pub extern crate fuzzy_pickles as neat_code;
2773/// ```
2774#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2775pub struct Crate {
2776    pub extent: Extent,
2777    pub visibility: Option<Visibility>,
2778    pub name: Ident,
2779    pub rename: Option<Ident>,
2780    pub whitespace: Vec<Whitespace>,
2781}
2782
2783/// Functions, types, and variables provided via FFI
2784///
2785/// ### Example Source
2786///
2787/// ```rust,ignore
2788/// extern "C" { fn putc(c: u8); }
2789/// ```
2790#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2791pub struct ExternBlock {
2792    pub extent: Extent,
2793    pub abi: Option<String>,
2794    pub members: Vec<Attributed<ExternBlockMember>>,
2795    pub whitespace: Vec<Whitespace>,
2796}
2797
2798#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2799pub enum ExternBlockMember {
2800    Function(ExternBlockMemberFunction),
2801    Static(ExternBlockMemberStatic),
2802    Type(ExternBlockMemberType),
2803}
2804
2805/// A static variable accessed via FFI
2806///
2807/// ### Example Source
2808///
2809/// ```rust,ignore
2810/// extern "C" { static VERSION: *const u8; }
2811/// //           ^^^^^^^^^^^^^^^^^^^^^^^^^^
2812/// ```
2813#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2814pub struct ExternBlockMemberStatic {
2815    pub extent: Extent,
2816    pub visibility: Option<Visibility>,
2817    pub is_mut: Option<Extent>,
2818    pub name: Ident,
2819    pub typ: Type,
2820    pub whitespace: Vec<Whitespace>,
2821}
2822
2823/// An opaque FFI type
2824///
2825/// ### Example Source
2826///
2827/// ```rust,ignore
2828/// extern "C" { type CoolType; }
2829/// //           ^^^^^^^^^^^^^^
2830/// ```
2831#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2832pub struct ExternBlockMemberType {
2833    pub extent: Extent,
2834    pub visibility: Option<Visibility>,
2835    pub name: Ident,
2836    pub whitespace: Vec<Whitespace>,
2837}
2838
2839/// A function provided from FFI
2840///
2841/// ### Example Source
2842///
2843/// ```rust,ignore
2844/// extern "C" { fn putc(c: u8); }
2845/// //           ^^^^^^^^^^^^^^^
2846/// ```
2847#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2848pub struct ExternBlockMemberFunction {
2849    pub extent: Extent,
2850    pub visibility: Option<Visibility>,
2851    pub name: Ident,
2852    pub generics: Option<GenericDeclarations>,
2853    pub arguments: Vec<ExternBlockMemberFunctionArgument>,
2854    pub return_type: Option<Type>,
2855    pub wheres: Vec<Where>,
2856    pub whitespace: Vec<Whitespace>,
2857}
2858
2859#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2860pub enum ExternBlockMemberFunctionArgument {
2861    Named(ExternBlockMemberFunctionArgumentNamed),
2862    Variadic(ExternBlockMemberFunctionArgumentVariadic),
2863}
2864
2865/// A named argument to a FFI function
2866///
2867/// ### Example Source
2868///
2869/// ```rust,ignore
2870/// extern "C" { fn printf(s: *const u8, ...); }
2871/// //                     ^^^^^^^^^^^^
2872/// ```
2873#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2874pub struct ExternBlockMemberFunctionArgumentNamed {
2875    pub extent: Extent,
2876    pub name: Pattern,
2877    pub typ: Type,
2878    pub whitespace: Vec<Whitespace>,
2879}
2880
2881/// A variadic list of arguments to a FFI function
2882///
2883/// ### Example Source
2884///
2885/// ```rust,ignore
2886/// extern "C" { fn printf(s: *const u8, ...); }
2887/// //                                   ^^^
2888/// ```
2889#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2890pub struct ExternBlockMemberFunctionArgumentVariadic {
2891    pub extent: Extent,
2892}
2893
2894/// A type alias
2895///
2896/// ### Example Source
2897///
2898/// ```rust,ignore
2899/// type Point = (i32, i32);
2900/// ```
2901#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2902pub struct TypeAlias {
2903    pub extent: Extent,
2904    pub visibility: Option<Visibility>,
2905    pub name: Ident,
2906    pub generics: Option<GenericDeclarations>,
2907    pub wheres: Vec<Where>,
2908    pub defn: Type,
2909    pub whitespace: Vec<Whitespace>,
2910}
2911
2912/// A module of code
2913///
2914/// ### Example Source
2915///
2916/// ```rust,ignore
2917/// mod details {}
2918/// ```
2919#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2920pub struct Module {
2921    pub extent: Extent,
2922    pub visibility: Option<Visibility>,
2923    pub name: Ident,
2924    pub body: Option<Vec<Attributed<Item>>>,
2925    pub whitespace: Vec<Whitespace>,
2926}
2927
2928#[derive(Debug, HasExtent, ExtentIndex, Visit, Decompose)]
2929pub enum Visibility {
2930    Public(VisibilityPublic),
2931    Crate(Extent),
2932}
2933
2934/// Visibility modifiers for an element
2935///
2936/// ### Example Source
2937///
2938/// ```rust,ignore
2939///     pub(crate) struct Ogre;
2940/// //  ^^^^^^^^^^
2941/// ```
2942#[derive(Debug, HasExtent, ExtentIndex, Visit)]
2943pub struct VisibilityPublic {
2944    pub extent: Extent,
2945    #[visit(ignore)]
2946    pub qualifier: Option<VisibilityPublicQualifier>,
2947    pub whitespace: Vec<Whitespace>,
2948}
2949
2950#[derive(Debug)]
2951pub enum VisibilityPublicQualifier {
2952    Crate,
2953    SelfIdent,
2954    Path(Path),
2955}