cardinal_codegen/
module.rs

1//! A module that can contain Cardinal functions and global data.
2
3//! Exposes types for function declarations and definitions.
4
5use crate::entities::{AbiType, GlobalVariable};
6use crate::function::{Function, FunctionSignature};
7use std::collections::HashMap;
8
9// A module that contains Cardinal functions and global data.
10pub struct Module {
11
12    /// A list of functions defined in the module.
13    pub functions: HashMap<String, Function>,
14
15    /// A list of global data variables declared in the module.
16    pub data: HashMap<String, AbiType>,
17
18}
19
20impl Module {
21
22    /// Creates a new empty module.
23    pub fn new() -> Self {
24        Self {
25            functions: HashMap::new(),
26            data: HashMap::new(),
27        }
28    }
29
30    /// Declares a function with the specified name.
31    pub fn declare_function(&mut self, name: String) {
32        let func = Function::new(name.to_string(), FunctionSignature::new());
33        self.functions.insert(name, func);
34    }
35
36    /// Defines a function with the specified name.
37    pub fn define_function(&mut self, func: Function) {
38        self.functions.insert(func.name.to_string(), func);
39    }
40
41    /// Declares a variable in the module.
42    pub fn declare_variable(&mut self, name: String, val_type: AbiType) -> GlobalVariable {
43        self.data.insert(name.to_string(), val_type);
44        GlobalVariable(name)
45    }
46
47}