lust/ast/
items.rs

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