pipeline_script/ast/
struct.rs

1use crate::ast::r#type::Type;
2use crate::llvm::global::Global;
3use crate::llvm::types::LLVMType;
4#[derive(Clone, Debug)]
5pub struct Struct {
6    pub name: String,
7    pub fields: Vec<StructField>,
8    pub generics: Vec<Type>,
9}
10
11impl Struct {
12    pub fn new(name: String, fields: Vec<StructField>, generics: Vec<Type>) -> Self {
13        Self {
14            name,
15            fields,
16            generics,
17        }
18    }
19    pub fn get_llvm_type(&self) -> LLVMType {
20        Global::struct_type(
21            self.name.clone(),
22            self.fields
23                .iter()
24                .map(|f| (f.name.clone(), f.field_type.as_llvm_type()))
25                .collect(),
26        )
27    }
28    pub fn get_field_type(&self, field_name: &str) -> Option<Type> {
29        for field in self.fields.clone() {
30            if field.name == field_name {
31                return Some(field.field_type);
32            }
33        }
34        None
35    }
36    pub fn has_generic(&self) -> bool {
37        !self.generics.is_empty()
38    }
39    pub fn get_generics(&self) -> &Vec<Type> {
40        &self.generics
41    }
42    pub fn get_type(&self) -> Type {
43        let mut m = vec![];
44        for field in self.fields.clone() {
45            m.push((field.name, field.field_type))
46        }
47        Type::Struct(Some(self.name.clone()), m)
48    }
49    pub fn get_props(&self) -> Vec<(String, Type)> {
50        self.fields
51            .iter()
52            .map(|f| (f.name.clone(), f.field_type.clone()))
53            .collect()
54    }
55    pub fn get_name(&self) -> &str {
56        &self.name
57    }
58    pub fn get_fields(&self) -> &Vec<StructField> {
59        &self.fields
60    }
61}
62#[derive(Clone, Debug)]
63
64pub struct StructField {
65    pub name: String,
66    pub field_type: Type,
67}
68
69impl StructField {
70    pub fn new(name: String, field_type: Type) -> Self {
71        Self { name, field_type }
72    }
73}