pipeline_script/ast/
function.rs

1use crate::ast::data::Data;
2use crate::ast::declaration::VariableDeclaration;
3use crate::ast::r#type::Type;
4use crate::ast::NodeTrait;
5use std::any::Any;
6use std::collections::HashMap;
7use std::vec;
8
9use crate::ast::stmt::StmtNode;
10
11/// 表示方法中self参数的类型
12#[derive(Clone, Debug, PartialEq)]
13pub enum SelfType {
14    /// 无self参数
15    None,
16    /// 值类型self参数
17    Value,
18    /// 引用类型self参数
19    Reference,
20}
21
22impl SelfType {
23    pub fn is_value(&self) -> bool {
24        matches!(self, SelfType::Value)
25    }
26    pub fn is_reference(&self) -> bool {
27        matches!(self, SelfType::Reference)
28    }
29    pub fn is_none(&self) -> bool {
30        matches!(self, SelfType::None)
31    }
32}
33#[derive(Clone, Debug)]
34pub struct Function {
35    name: String,
36    return_type: Type,
37    // 泛型模版,用来区分实例和模版,模版不用于生成llvm ir
38    pub is_template: bool,
39    pub function_generics: Vec<Type>,
40    args: Vec<VariableDeclaration>,
41    body: Vec<StmtNode>,
42    #[allow(unused)]
43    is_vararg: bool,
44    #[allow(unused)]
45    is_generic: bool,
46    is_closure: bool,
47    binding_type: Option<String>,
48    #[allow(unused)]
49    pub(crate) is_extern: bool,
50    // 只有在是绑定方法时(self_type不为None)才会有值
51    pub type_generics: Vec<Type>,
52    /// 方法的self参数类型
53    self_type: SelfType,
54}
55impl NodeTrait for Function {
56    fn get_id(&self) -> &str {
57        "Function"
58    }
59
60    fn get_data(&self, key: &str) -> Option<Data> {
61        match key {
62            "name" => Some(Data::String(self.name.clone())),
63            "binding_struct" => self.binding_type.as_ref().map(|s| Data::String(s.clone())),
64            _ => None,
65        }
66    }
67
68    fn set_data(&mut self, key: &str, value: Data) {
69        if key == "name" {
70            self.name = value.as_str().unwrap().into();
71        }
72    }
73
74    fn get_children(&self) -> Vec<&dyn NodeTrait> {
75        todo!()
76    }
77
78    fn get_mut_children(&mut self) -> Vec<&mut dyn NodeTrait> {
79        vec![]
80    }
81
82    fn get_extra(&self) -> &HashMap<String, Box<dyn Any>> {
83        todo!()
84    }
85}
86impl Function {
87    pub fn new(
88        name: String,
89        return_type: Type,
90        args: Vec<VariableDeclaration>,
91        body: Vec<StmtNode>,
92        is_extern: bool,
93    ) -> Self {
94        Self {
95            name,
96            return_type,
97            args,
98            body,
99            is_template: false,
100            function_generics: vec![],
101            binding_type: None,
102            is_generic: false,
103            is_extern,
104            is_vararg: false,
105            is_closure: false,
106            type_generics: vec![],
107            self_type: SelfType::None,
108        }
109    }
110    pub fn name(&self) -> String {
111        self.name.clone()
112    }
113    pub fn body(&self) -> &Vec<StmtNode> {
114        &self.body
115    }
116    pub fn mut_body(&mut self) -> &mut Vec<StmtNode> {
117        &mut self.body
118    }
119    pub fn args(&self) -> &Vec<VariableDeclaration> {
120        &self.args
121    }
122    pub fn args_mut(&mut self) -> &mut Vec<VariableDeclaration> {
123        &mut self.args
124    }
125    pub fn return_type(&self) -> &Type {
126        &self.return_type
127    }
128    pub fn with_function_generics(mut self, list: Vec<Type>) -> Self {
129        self.function_generics = list;
130        self
131    }
132    pub fn with_vararg(mut self, is_vararg: bool) -> Self {
133        self.is_vararg = is_vararg;
134        self
135    }
136    pub fn is_vararg(&self) -> bool {
137        self.is_vararg
138    }
139    pub fn set_vararg(&mut self, is_vararg: bool) {
140        self.is_vararg = is_vararg;
141    }
142    pub fn set_closure(&mut self, is_closure: bool) {
143        self.is_closure = is_closure;
144    }
145    pub fn with_extern(mut self, is_extern: bool) -> Self {
146        self.is_extern = is_extern;
147        self
148    }
149    pub fn with_template(mut self, is_template: bool) -> Self {
150        self.is_template = is_template;
151        self
152    }
153    pub fn add_function_generic(&mut self, g: Type) {
154        self.function_generics.push(g);
155    }
156    pub fn args_count(&self) -> usize {
157        self.args.len()
158    }
159    pub fn has_binding(&self) -> bool {
160        self.binding_type.is_some()
161    }
162    pub fn get_binding(&self) -> String {
163        self.binding_type.clone().unwrap()
164    }
165    pub fn insert_arg(&mut self, index: usize, vd: VariableDeclaration) {
166        self.args.insert(index, vd)
167    }
168    pub fn set_binding_type(&mut self, binding_struct: impl Into<String>) {
169        self.binding_type = Some(binding_struct.into());
170    }
171    pub fn set_name(&mut self, name: String) {
172        self.name = name;
173    }
174    pub fn set_body(&mut self, body: Vec<StmtNode>) {
175        self.body = body;
176    }
177    pub fn with_return_type(mut self, return_type: Type) -> Self {
178        self.return_type = return_type;
179        self
180    }
181    pub fn set_return_type(&mut self, return_type: Type) {
182        self.return_type = return_type;
183    }
184    pub fn with_name(mut self, name: String) -> Self {
185        self.name = name;
186        self
187    }
188    pub fn with_args(mut self, args: Vec<VariableDeclaration>) -> Self {
189        self.args = args;
190        self
191    }
192    pub fn get_param_type(&self, index: usize) -> Option<Type> {
193        if index < self.args.len() {
194            self.args[index].r#type()
195        } else {
196            None
197        }
198    }
199    pub fn get_type(&self) -> Type {
200        let mut args = vec![];
201        for i in &self.args {
202            args.push((i.name().to_string(), i.r#type().unwrap()));
203        }
204        Type::Function(Box::new(self.return_type.clone()), args, self.is_vararg)
205    }
206    pub fn set_template(&mut self, is_template: bool) {
207        self.is_template = is_template;
208    }
209    pub fn set_type_generics(&mut self, type_generics: Vec<Type>) {
210        self.type_generics = type_generics;
211    }
212
213    /// 获取方法的self参数类型
214    pub fn self_type(&self) -> &SelfType {
215        &self.self_type
216    }
217
218    /// 设置方法的self参数类型
219    pub fn set_self_type(&mut self, self_type: SelfType) {
220        self.self_type = self_type;
221    }
222
223    /// 链式设置方法的self参数类型
224    pub fn with_self_type(mut self, self_type: SelfType) -> Self {
225        self.self_type = self_type;
226        self
227    }
228
229    /// 检查方法是否有self参数
230    pub fn has_self(&self) -> bool {
231        self.self_type != SelfType::None
232    }
233}
234
235impl Default for Function {
236    fn default() -> Self {
237        Self {
238            name: "".to_string(),
239            return_type: Type::Unit,
240            is_generic: false,
241            args: vec![],
242            function_generics: vec![],
243            binding_type: None,
244            body: vec![],
245            is_extern: false,
246            is_template: false,
247            is_vararg: false,
248            is_closure: false,
249            type_generics: vec![],
250            self_type: SelfType::None,
251        }
252    }
253}