use super::{
Attribute, Block, Expr, Generics, Ident, Mutability, NodeId, Path, Pattern, Type, TypeBound,
Visibility,
};
use crate::lexer::Span;
#[derive(Debug, Clone, PartialEq)]
pub struct Item {
pub kind: ItemKind,
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub span: Span,
pub id: NodeId,
}
impl Item {
pub fn new(kind: ItemKind, vis: Visibility, attrs: Vec<Attribute>, span: Span) -> Self {
Self {
kind,
vis,
attrs,
span,
id: NodeId::DUMMY,
}
}
pub fn name(&self) -> Option<&Ident> {
match &self.kind {
ItemKind::Function(f) => Some(&f.name),
ItemKind::Struct(s) => Some(&s.name),
ItemKind::Enum(e) => Some(&e.name),
ItemKind::Trait(t) => Some(&t.name),
ItemKind::TypeAlias(t) => Some(&t.name),
ItemKind::Const(c) => Some(&c.name),
ItemKind::Static(s) => Some(&s.name),
ItemKind::Mod(m) => Some(&m.name),
ItemKind::Impl(_) => None,
ItemKind::Use(_) => None,
ItemKind::ExternCrate(e) => Some(&e.name),
ItemKind::ExternBlock(_) => None,
ItemKind::Macro(m) => m.name.as_ref(),
ItemKind::MacroRules(m) => Some(&m.name),
ItemKind::Effect(e) => Some(&e.name),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ItemKind {
Function(Box<FnDef>),
Struct(Box<StructDef>),
Enum(Box<EnumDef>),
Trait(Box<TraitDef>),
Impl(Box<ImplDef>),
TypeAlias(Box<TypeAliasDef>),
Const(Box<ConstDef>),
Static(Box<StaticDef>),
Mod(Box<ModDef>),
Use(Box<UseDef>),
ExternCrate(Box<ExternCrateDef>),
ExternBlock(Box<ExternBlockDef>),
Macro(Box<MacroDef>),
MacroRules(Box<MacroRulesDef>),
Effect(Box<EffectDef>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct FnDef {
pub name: Ident,
pub generics: Generics,
pub sig: FnSig,
pub body: Option<Box<Block>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FnSig {
pub is_unsafe: bool,
pub is_async: bool,
pub is_const: bool,
pub abi: Option<String>,
pub params: Vec<Param>,
pub return_ty: Option<Box<Type>>,
pub effects: Vec<Path>,
}
impl Default for FnSig {
fn default() -> Self {
Self {
is_unsafe: false,
is_async: false,
is_const: false,
abi: None,
params: Vec::new(),
return_ty: None,
effects: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
pub attrs: Vec<Attribute>,
pub pattern: Pattern,
pub ty: Box<Type>,
pub default: Option<Box<Expr>>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct StructDef {
pub name: Ident,
pub generics: Generics,
pub fields: StructFields,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StructFields {
Named(Vec<FieldDef>),
Tuple(Vec<TupleFieldDef>),
Unit,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FieldDef {
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub name: Ident,
pub ty: Box<Type>,
pub default: Option<Box<Expr>>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TupleFieldDef {
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub ty: Box<Type>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumDef {
pub name: Ident,
pub generics: Generics,
pub variants: Vec<EnumVariant>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumVariant {
pub attrs: Vec<Attribute>,
pub name: Ident,
pub fields: StructFields,
pub discriminant: Option<Box<Expr>>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TraitDef {
pub name: Ident,
pub is_unsafe: bool,
pub is_auto: bool,
pub generics: Generics,
pub supertraits: Vec<TypeBound>,
pub items: Vec<TraitItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TraitItem {
pub attrs: Vec<Attribute>,
pub kind: TraitItemKind,
pub span: Span,
pub id: NodeId,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TraitItemKind {
Function(Box<FnDef>),
Type {
name: Ident,
generics: Generics,
bounds: Vec<TypeBound>,
default: Option<Box<Type>>,
},
Const {
name: Ident,
ty: Box<Type>,
default: Option<Box<Expr>>,
},
Macro {
path: Path,
tokens: Vec<super::TokenTree>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImplDef {
pub is_unsafe: bool,
pub is_negative: bool,
pub generics: Generics,
pub trait_ref: Option<TraitRef>,
pub self_ty: Box<Type>,
pub items: Vec<ImplItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TraitRef {
pub path: Path,
pub is_negative: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImplItem {
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub is_default: bool,
pub kind: ImplItemKind,
pub span: Span,
pub id: NodeId,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImplItemKind {
Function(Box<FnDef>),
Type {
name: Ident,
generics: Generics,
ty: Box<Type>,
},
Const {
name: Ident,
ty: Box<Type>,
value: Box<Expr>,
},
Macro {
path: Path,
tokens: Vec<super::TokenTree>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct TypeAliasDef {
pub name: Ident,
pub generics: Generics,
pub bounds: Vec<TypeBound>,
pub ty: Option<Box<Type>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConstDef {
pub name: Ident,
pub ty: Box<Type>,
pub value: Option<Box<Expr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct StaticDef {
pub name: Ident,
pub mutability: Mutability,
pub ty: Box<Type>,
pub value: Option<Box<Expr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ModDef {
pub name: Ident,
pub content: Option<ModContent>,
pub is_unsafe: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ModContent {
pub attrs: Vec<Attribute>,
pub items: Vec<Item>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UseDef {
pub tree: UseTree,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UseTree {
pub kind: UseTreeKind,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum UseTreeKind {
Simple { path: Path, rename: Option<Ident> },
Glob(Path),
Nested { path: Path, trees: Vec<UseTree> },
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExternCrateDef {
pub name: Ident,
pub rename: Option<Ident>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExternBlockDef {
pub is_unsafe: bool,
pub abi: Option<String>,
pub items: Vec<ForeignItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForeignItem {
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub kind: ForeignItemKind,
pub span: Span,
pub id: NodeId,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ForeignItemKind {
Fn(Box<FnDef>),
Static {
name: Ident,
mutability: Mutability,
ty: Box<Type>,
},
Type { name: Ident, bounds: Vec<TypeBound> },
Macro {
path: Path,
tokens: Vec<super::TokenTree>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct MacroDef {
pub name: Option<Ident>,
pub body: Vec<super::TokenTree>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MacroRulesDef {
pub name: Ident,
pub rules: Vec<MacroRule>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MacroRule {
pub pattern: Vec<super::TokenTree>,
pub body: Vec<super::TokenTree>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EffectDef {
pub name: Ident,
pub generics: Generics,
pub operations: Vec<EffectOperation>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EffectOperation {
pub attrs: Vec<Attribute>,
pub name: Ident,
pub params: Vec<Param>,
pub return_ty: Option<Box<Type>>,
pub span: Span,
}