3492 lines · 118.7 KB · Rust
RawDownload
// SPDX-License-Identifier: Apache-2.0 OR MIT use crate::attr::Attribute;use crate::data::{Fields, FieldsNamed, Variant};use crate::derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput};use crate::expr::Expr;use crate::generics::{Generics, TypeParamBound};use crate::ident::Ident;use crate::lifetime::Lifetime;use crate::mac::Macro;use crate::pat::{Pat, PatType};use crate::path::Path;use crate::punctuated::Punctuated;use crate::restriction::Visibility;use crate::stmt::Block;use crate::token;use crate::ty::{Abi, ReturnType, Type};use proc_macro2::TokenStream;#[cfg(feature = "parsing")]use std::mem; ast_enum_of_structs! {    /// Things that can appear directly inside of a module or scope.    ///    /// # Syntax tree enum    ///    /// This type is a [syntax tree enum].    ///    /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    #[non_exhaustive]    pub enum Item {        /// A constant item: `const MAX: u16 = 65535`.        Const(ItemConst),         /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`.        Enum(ItemEnum),         /// An `extern crate` item: `extern crate serde`.        ExternCrate(ItemExternCrate),         /// A free-standing function: `fn process(n: usize) -> Result<()> { ...        /// }`.        Fn(ItemFn),         /// A block of foreign items: `extern "C" { ... }`.        ForeignMod(ItemForeignMod),         /// An impl block providing trait or associated items: `impl<A> Trait        /// for Data<A> { ... }`.        Impl(ItemImpl),         /// A macro invocation, which includes `macro_rules!` definitions.        Macro(ItemMacro),         /// A module or module declaration: `mod m` or `mod m { ... }`.        Mod(ItemMod),         /// A static item: `static BIKE: Shed = Shed(42)`.        Static(ItemStatic),         /// A struct definition: `struct Foo<A> { x: A }`.        Struct(ItemStruct),         /// A trait definition: `pub trait Iterator { ... }`.        Trait(ItemTrait),         /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.        TraitAlias(ItemTraitAlias),         /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.        Type(ItemType),         /// A union definition: `union Foo<A, B> { x: A, y: B }`.        Union(ItemUnion),         /// A use declaration: `use std::collections::HashMap`.        Use(ItemUse),         /// Tokens forming an item not interpreted by Syn.        Verbatim(TokenStream),         // For testing exhaustiveness in downstream code, use the following idiom:        //        //     match item {        //         #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))]        //        //         Item::Const(item) => {...}        //         Item::Enum(item) => {...}        //         ...        //         Item::Verbatim(item) => {...}        //        //         _ => { /* some sane fallback */ }        //     }        //        // This way we fail your tests but don't break your library when adding        // a variant. You will be notified by a test failure when a variant is        // added, so that you can add code to handle it, but your library will        // continue to compile and work for downstream users in the interim.    }} ast_struct! {    /// A constant item: `const MAX: u16 = 65535`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemConst {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub const_token: Token![const],        pub ident: Ident,        pub generics: Generics,        pub colon_token: Token![:],        pub ty: Box<Type>,        pub eq_token: Token![=],        pub expr: Box<Expr>,        pub semi_token: Token![;],    }} ast_struct! {    /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemEnum {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub enum_token: Token![enum],        pub ident: Ident,        pub generics: Generics,        pub brace_token: token::Brace,        pub variants: Punctuated<Variant, Token![,]>,    }} ast_struct! {    /// An `extern crate` item: `extern crate serde`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemExternCrate {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub extern_token: Token![extern],        pub crate_token: Token![crate],        pub ident: Ident,        pub rename: Option<(Token![as], Ident)>,        pub semi_token: Token![;],    }} ast_struct! {    /// A free-standing function: `fn process(n: usize) -> Result<()> { ... }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemFn {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub sig: Signature,        pub block: Box<Block>,    }} ast_struct! {    /// A block of foreign items: `extern "C" { ... }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemForeignMod {        pub attrs: Vec<Attribute>,        pub unsafety: Option<Token![unsafe]>,        pub abi: Abi,        pub brace_token: token::Brace,        pub items: Vec<ForeignItem>,    }} ast_struct! {    /// An impl block providing trait or associated items: `impl<A> Trait    /// for Data<A> { ... }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemImpl {        pub attrs: Vec<Attribute>,        pub defaultness: Option<Token![default]>,        pub unsafety: Option<Token![unsafe]>,        pub impl_token: Token![impl],        pub generics: Generics,        /// Trait this impl implements.        pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,        /// The Self type of the impl.        pub self_ty: Box<Type>,        pub brace_token: token::Brace,        pub items: Vec<ImplItem>,    }} ast_struct! {    /// A macro invocation, which includes `macro_rules!` definitions.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemMacro {        pub attrs: Vec<Attribute>,        /// The `example` in `macro_rules! example { ... }`.        pub ident: Option<Ident>,        pub mac: Macro,        pub semi_token: Option<Token![;]>,    }} ast_struct! {    /// A module or module declaration: `mod m` or `mod m { ... }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemMod {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub unsafety: Option<Token![unsafe]>,        pub mod_token: Token![mod],        pub ident: Ident,        pub content: Option<(token::Brace, Vec<Item>)>,        pub semi: Option<Token![;]>,    }} ast_struct! {    /// A static item: `static BIKE: Shed = Shed(42)`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemStatic {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub static_token: Token![static],        pub mutability: StaticMutability,        pub ident: Ident,        pub colon_token: Token![:],        pub ty: Box<Type>,        pub eq_token: Token![=],        pub expr: Box<Expr>,        pub semi_token: Token![;],    }} ast_struct! {    /// A struct definition: `struct Foo<A> { x: A }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemStruct {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub struct_token: Token![struct],        pub ident: Ident,        pub generics: Generics,        pub fields: Fields,        pub semi_token: Option<Token![;]>,    }} ast_struct! {    /// A trait definition: `pub trait Iterator { ... }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemTrait {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub unsafety: Option<Token![unsafe]>,        pub auto_token: Option<Token![auto]>,        pub restriction: Option<ImplRestriction>,        pub trait_token: Token![trait],        pub ident: Ident,        pub generics: Generics,        pub colon_token: Option<Token![:]>,        pub supertraits: Punctuated<TypeParamBound, Token![+]>,        pub brace_token: token::Brace,        pub items: Vec<TraitItem>,    }} ast_struct! {    /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemTraitAlias {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub trait_token: Token![trait],        pub ident: Ident,        pub generics: Generics,        pub eq_token: Token![=],        pub bounds: Punctuated<TypeParamBound, Token![+]>,        pub semi_token: Token![;],    }} ast_struct! {    /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemType {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub type_token: Token![type],        pub ident: Ident,        pub generics: Generics,        pub eq_token: Token![=],        pub ty: Box<Type>,        pub semi_token: Token![;],    }} ast_struct! {    /// A union definition: `union Foo<A, B> { x: A, y: B }`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemUnion {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub union_token: Token![union],        pub ident: Ident,        pub generics: Generics,        pub fields: FieldsNamed,    }} ast_struct! {    /// A use declaration: `use std::collections::HashMap`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct ItemUse {        pub attrs: Vec<Attribute>,        pub vis: Visibility,        pub use_token: Token![use],        pub leading_colon: Option<Token![::]>,        pub tree: UseTree,        pub semi_token: Token![;],    }} impl Item {    #[cfg(feature = "parsing")]    pub(crate) fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {        match self {            Item::Const(ItemConst { attrs, .. })            | Item::Enum(ItemEnum { attrs, .. })            | Item::ExternCrate(ItemExternCrate { attrs, .. })            | Item::Fn(ItemFn { attrs, .. })            | Item::ForeignMod(ItemForeignMod { attrs, .. })            | Item::Impl(ItemImpl { attrs, .. })            | Item::Macro(ItemMacro { attrs, .. })            | Item::Mod(ItemMod { attrs, .. })            | Item::Static(ItemStatic { attrs, .. })            | Item::Struct(ItemStruct { attrs, .. })            | Item::Trait(ItemTrait { attrs, .. })            | Item::TraitAlias(ItemTraitAlias { attrs, .. })            | Item::Type(ItemType { attrs, .. })            | Item::Union(ItemUnion { attrs, .. })            | Item::Use(ItemUse { attrs, .. }) => mem::replace(attrs, new),            Item::Verbatim(_) => Vec::new(),        }    }} impl From<DeriveInput> for Item {    fn from(input: DeriveInput) -> Item {        match input.data {            Data::Struct(data) => Item::Struct(ItemStruct {                attrs: input.attrs,                vis: input.vis,                struct_token: data.struct_token,                ident: input.ident,                generics: input.generics,                fields: data.fields,                semi_token: data.semi_token,            }),            Data::Enum(data) => Item::Enum(ItemEnum {                attrs: input.attrs,                vis: input.vis,                enum_token: data.enum_token,                ident: input.ident,                generics: input.generics,                brace_token: data.brace_token,                variants: data.variants,            }),            Data::Union(data) => Item::Union(ItemUnion {                attrs: input.attrs,                vis: input.vis,                union_token: data.union_token,                ident: input.ident,                generics: input.generics,                fields: data.fields,            }),        }    }} impl From<ItemStruct> for DeriveInput {    fn from(input: ItemStruct) -> DeriveInput {        DeriveInput {            attrs: input.attrs,            vis: input.vis,            ident: input.ident,            generics: input.generics,            data: Data::Struct(DataStruct {                struct_token: input.struct_token,                fields: input.fields,                semi_token: input.semi_token,            }),        }    }} impl From<ItemEnum> for DeriveInput {    fn from(input: ItemEnum) -> DeriveInput {        DeriveInput {            attrs: input.attrs,            vis: input.vis,            ident: input.ident,            generics: input.generics,            data: Data::Enum(DataEnum {                enum_token: input.enum_token,                brace_token: input.brace_token,                variants: input.variants,            }),        }    }} impl From<ItemUnion> for DeriveInput {    fn from(input: ItemUnion) -> DeriveInput {        DeriveInput {            attrs: input.attrs,            vis: input.vis,            ident: input.ident,            generics: input.generics,            data: Data::Union(DataUnion {                union_token: input.union_token,                fields: input.fields,            }),        }    }} ast_enum_of_structs! {    /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.    ///    /// # Syntax tree enum    ///    /// This type is a [syntax tree enum].    ///    /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub enum UseTree {        /// A path prefix of imports in a `use` item: `std::...`.        Path(UsePath),         /// An identifier imported by a `use` item: `HashMap`.        Name(UseName),         /// An renamed identifier imported by a `use` item: `HashMap as Map`.        Rename(UseRename),         /// A glob import in a `use` item: `*`.        Glob(UseGlob),         /// A braced group of imports in a `use` item: `{A, B, C}`.        Group(UseGroup),    }} ast_struct! {    /// A path prefix of imports in a `use` item: `std::...`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct UsePath {        pub ident: Ident,        pub colon2_token: Token![::],        pub tree: Box<UseTree>,    }} ast_struct! {    /// An identifier imported by a `use` item: `HashMap`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct UseName {        pub ident: Ident,    }} ast_struct! {    /// An renamed identifier imported by a `use` item: `HashMap as Map`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct UseRename {        pub ident: Ident,        pub as_token: Token![as],        pub rename: Ident,    }} ast_struct! {    /// A glob import in a `use` item: `*`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct UseGlob {        pub star_token: Token![*],    }} ast_struct! {    /// A braced group of imports in a `use` item: `{A, B, C}`.    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]    pub struct UseGroup {        pub brace_token: token::Brace,        pub items: Punctuated<UseTree, Token![,]>,    }} ast_enum_of_structs! {    /// An item within an `extern` block.    ///

Lines 1–500 of 3492.