lust/ast/
items.rs

1use super::{Expr, Span, Stmt, Type};
2#[derive(Debug, Clone, PartialEq)]
3pub struct Item {
4    pub kind: ItemKind,
5    pub span: Span,
6}
7
8impl Item {
9    pub fn new(kind: ItemKind, span: Span) -> Self {
10        Self { kind, span }
11    }
12}
13
14#[derive(Debug, Clone, PartialEq)]
15pub enum ItemKind {
16    Script(Vec<Stmt>),
17    Function(FunctionDef),
18    Struct(StructDef),
19    Enum(EnumDef),
20    Trait(TraitDef),
21    Impl(ImplBlock),
22    TypeAlias {
23        name: String,
24        type_params: Vec<String>,
25        target: Type,
26    },
27    Module {
28        name: String,
29        items: Vec<Item>,
30    },
31    Use {
32        public: bool,
33        tree: UseTree,
34    },
35    Const {
36        name: String,
37        ty: Type,
38        value: Expr,
39    },
40    Static {
41        name: String,
42        mutable: bool,
43        ty: Type,
44        value: Expr,
45    },
46    Extern {
47        abi: String,
48        items: Vec<ExternItem>,
49    },
50}
51
52#[derive(Debug, Clone, PartialEq)]
53pub enum UseTree {
54    Path {
55        path: Vec<String>,
56        alias: Option<String>,
57        import_module: bool,
58    },
59    Group {
60        prefix: Vec<String>,
61        items: Vec<UseTreeItem>,
62    },
63    Glob {
64        prefix: Vec<String>,
65    },
66}
67
68#[derive(Debug, Clone, PartialEq)]
69pub struct UseTreeItem {
70    pub path: Vec<String>,
71    pub alias: Option<String>,
72}
73
74#[derive(Debug, Clone, PartialEq)]
75pub struct FunctionDef {
76    pub name: String,
77    pub type_params: Vec<String>,
78    pub trait_bounds: Vec<TraitBound>,
79    pub params: Vec<FunctionParam>,
80    pub return_type: Option<Type>,
81    pub body: Vec<Stmt>,
82    pub is_method: bool,
83    pub visibility: Visibility,
84}
85
86#[derive(Debug, Clone, PartialEq)]
87pub struct FunctionParam {
88    pub name: String,
89    pub ty: Type,
90    pub is_self: bool,
91}
92
93#[derive(Debug, Clone, PartialEq)]
94pub struct StructDef {
95    pub name: String,
96    pub type_params: Vec<String>,
97    pub trait_bounds: Vec<TraitBound>,
98    pub fields: Vec<StructField>,
99    pub visibility: Visibility,
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum FieldOwnership {
104    Strong,
105    Weak,
106}
107
108#[derive(Debug, Clone, PartialEq)]
109pub struct StructField {
110    pub name: String,
111    pub ty: Type,
112    pub visibility: Visibility,
113    pub ownership: FieldOwnership,
114    pub weak_target: Option<Type>,
115}
116
117#[derive(Debug, Clone, PartialEq)]
118pub struct EnumDef {
119    pub name: String,
120    pub type_params: Vec<String>,
121    pub trait_bounds: Vec<TraitBound>,
122    pub variants: Vec<EnumVariant>,
123    pub visibility: Visibility,
124}
125
126#[derive(Debug, Clone, PartialEq)]
127pub struct EnumVariant {
128    pub name: String,
129    pub fields: Option<Vec<Type>>,
130}
131
132#[derive(Debug, Clone, PartialEq)]
133pub struct TraitDef {
134    pub name: String,
135    pub type_params: Vec<String>,
136    pub methods: Vec<TraitMethod>,
137    pub visibility: Visibility,
138}
139
140#[derive(Debug, Clone, PartialEq)]
141pub struct TraitMethod {
142    pub name: String,
143    pub type_params: Vec<String>,
144    pub params: Vec<FunctionParam>,
145    pub return_type: Option<Type>,
146    pub default_impl: Option<Vec<Stmt>>,
147}
148
149#[derive(Debug, Clone, PartialEq)]
150pub struct ImplBlock {
151    pub type_params: Vec<String>,
152    pub trait_name: Option<String>,
153    pub target_type: Type,
154    pub methods: Vec<FunctionDef>,
155    pub where_clause: Vec<TraitBound>,
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub struct TraitBound {
160    pub type_param: String,
161    pub traits: Vec<String>,
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub enum Visibility {
166    Public,
167    Private,
168}
169
170#[derive(Debug, Clone, PartialEq)]
171pub enum ExternItem {
172    Function {
173        name: String,
174        params: Vec<Type>,
175        return_type: Option<Type>,
176    },
177}