cardinal_codegen/
function.rs1use crate::entities::{AbiParam, AbiType, Block, Type, Variable};
4use crate::instruction::{InstBlock, BlockType};
5use std::collections::HashMap;
6
7pub struct Function {
9
10 pub variables: HashMap<String, AbiType>,
12
13 pub blocks: Vec<InstBlock>,
15
16 pub name: String,
18
19 pub signature: FunctionSignature,
21
22}
23
24pub struct FunctionSignature {
26
27 pub arguments: Vec<AbiParam>,
30
31 pub returns: AbiType,
33
34}
35
36impl FunctionSignature {
37
38 pub fn new() -> Self {
39 Self {
40 arguments: vec![],
41 returns: AbiType("void".into(), Type::Plain)
42 }
43 }
44
45}
46
47impl Function {
48
49 pub fn new(name: String, sig: FunctionSignature) -> Self {
51 Self {
52 name,
53 signature: sig,
54 variables: HashMap::new(),
55 blocks: vec![],
56 }
57 }
58
59 pub fn declare_var(&mut self, name: String, var_type: AbiType) -> Variable {
61 let val = Variable(name.to_string());
62 self.variables.insert(name, var_type);
63
64 val
65 }
66
67 pub fn use_block(&mut self, block: Block) -> &mut InstBlock {
69 self.blocks.get_mut(block.0 as usize).unwrap()
70 }
71
72 pub fn create_block(&mut self) -> Block {
74 let block = InstBlock {
75 block_type: BlockType::Basic,
76 blocks: vec![],
77 else_block: None,
78 elses: vec![],
79 imports: vec![],
80 insts: vec![],
81 values: vec![],
82 };
83
84 let val = Block(self.blocks.len() as u32);
85 self.blocks.push(block);
86
87 val
88 }
89
90}