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        dbg!(key, &value);
70        if key == "name" {
71            self.name = value.as_str().unwrap().into();
72        }
73    }
74
75    fn get_children(&self) -> Vec<&dyn NodeTrait> {
76        todo!()
77    }
78
79    fn get_mut_children(&mut self) -> Vec<&mut dyn NodeTrait> {
80        vec![]
81    }
82
83    fn get_extra(&self) -> &HashMap<String, Box<dyn Any>> {
84        todo!()
85    }
86}
87impl Function {
88    pub fn new(
89        name: String,
90        return_type: Type,
91        args: Vec<VariableDeclaration>,
92        body: Vec<StmtNode>,
93        is_extern: bool,
94    ) -> Self {
95        Self {
96            name,
97            return_type,
98            args,
99            body,
100            is_template: false,
101            function_generics: vec![],
102            binding_type: None,
103            is_generic: false,
104            is_extern,
105            is_vararg: false,
106            is_closure: false,
107            type_generics: vec![],
108            self_type: SelfType::None,
109        }
110    }
111    pub fn name(&self) -> String {
112        self.name.clone()
113    }
114    pub fn body(&self) -> &Vec<StmtNode> {
115        &self.body
116    }
117    pub fn mut_body(&mut self) -> &mut Vec<StmtNode> {
118        &mut self.body
119    }
120    pub fn args(&self) -> &Vec<VariableDeclaration> {
121        &self.args
122    }
123    pub fn args_mut(&mut self) -> &mut Vec<VariableDeclaration> {
124        &mut self.args
125    }
126    pub fn return_type(&self) -> &Type {
127        &self.return_type
128    }
129    pub fn with_function_generics(mut self, list: Vec<Type>) -> Self {
130        self.function_generics = list;
131        self
132    }
133    pub fn with_vararg(mut self, is_vararg: bool) -> Self {
134        self.is_vararg = is_vararg;
135        self
136    }
137    pub fn is_vararg(&self) -> bool {
138        self.is_vararg
139    }
140    pub fn set_vararg(&mut self, is_vararg: bool) {
141        self.is_vararg = is_vararg;
142    }
143    pub fn set_closure(&mut self, is_closure: bool) {
144        self.is_closure = is_closure;
145    }
146    pub fn with_extern(mut self, is_extern: bool) -> Self {
147        self.is_extern = is_extern;
148        self
149    }
150    pub fn with_template(mut self, is_template: bool) -> Self {
151        self.is_template = is_template;
152        self
153    }
154    pub fn add_function_generic(&mut self, g: Type) {
155        self.function_generics.push(g);
156    }
157    pub fn args_count(&self) -> usize {
158        self.args.len()
159    }
160    pub fn has_binding(&self) -> bool {
161        self.binding_type.is_some()
162    }
163    pub fn get_binding(&self) -> String {
164        self.binding_type.clone().unwrap()
165    }
166    pub fn insert_arg(&mut self, index: usize, vd: VariableDeclaration) {
167        self.args.insert(index, vd)
168    }
169    pub fn set_binding_type(&mut self, binding_struct: impl Into<String>) {
170        self.binding_type = Some(binding_struct.into());
171    }
172    pub fn set_name(&mut self, name: String) {
173        self.name = name;
174    }
175    pub fn set_body(&mut self, body: Vec<StmtNode>) {
176        self.body = body;
177    }
178    pub fn with_return_type(mut self, return_type: Type) -> Self {
179        self.return_type = return_type;
180        self
181    }
182    pub fn set_return_type(&mut self, return_type: Type) {
183        self.return_type = return_type;
184    }
185    pub fn with_name(mut self, name: String) -> Self {
186        self.name = name;
187        self
188    }
189    pub fn with_args(mut self, args: Vec<VariableDeclaration>) -> Self {
190        self.args = args;
191        self
192    }
193    pub fn get_param_type(&self, index: usize) -> Option<Type> {
194        if index < self.args.len() {
195            self.args[index].r#type()
196        } else {
197            None
198        }
199    }
200    pub fn get_type(&self) -> Type {
201        let mut args = vec![];
202        for i in &self.args {
203            args.push((i.name().to_string(), i.r#type().unwrap()));
204        }
205        Type::Function(Box::new(self.return_type.clone()), args, self.is_vararg)
206    }
207    pub fn set_template(&mut self, is_template: bool) {
208        self.is_template = is_template;
209    }
210    pub fn set_type_generics(&mut self, type_generics: Vec<Type>) {
211        self.type_generics = type_generics;
212    }
213
214    /// 获取方法的self参数类型
215    pub fn self_type(&self) -> &SelfType {
216        &self.self_type
217    }
218
219    /// 设置方法的self参数类型
220    pub fn set_self_type(&mut self, self_type: SelfType) {
221        self.self_type = self_type;
222    }
223
224    /// 链式设置方法的self参数类型
225    pub fn with_self_type(mut self, self_type: SelfType) -> Self {
226        self.self_type = self_type;
227        self
228    }
229
230    /// 检查方法是否有self参数
231    pub fn has_self(&self) -> bool {
232        self.self_type != SelfType::None
233    }
234}
235
236impl Default for Function {
237    fn default() -> Self {
238        Self {
239            name: "".to_string(),
240            return_type: Type::Unit,
241            is_generic: false,
242            args: vec![],
243            function_generics: vec![],
244            binding_type: None,
245            body: vec![],
246            is_extern: false,
247            is_template: false,
248            is_vararg: false,
249            is_closure: false,
250            type_generics: vec![],
251            self_type: SelfType::None,
252        }
253    }
254}