kasl_ir/function/mod.rs
1//
2// Copyright 2026 Shuntaro Kasatani
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17mod block_sort;
18
19use crate::{Block, BlockData, IRType, Value, Variable};
20use std::{
21 collections::{BTreeMap, HashMap},
22 fmt::Display,
23};
24
25#[derive(Default, Clone)]
26pub struct Function {
27 /// The blocks in the function.
28 pub(crate) blocks: BTreeMap<Block, BlockData>,
29 /// The entry block of the function.
30 pub(crate) entry_block: Option<Block>,
31 /// The map from value IDs to their types.
32 pub(crate) values: HashMap<Value, IRType>,
33 /// The map from variable IDs to their types.
34 pub(crate) variables: HashMap<Variable, IRType>,
35}
36
37impl Function {
38 /// Returns all blocks in the function.
39 pub fn get_blocks(&self) -> Vec<Block> {
40 self.blocks.keys().copied().collect()
41 }
42
43 /// Returns a reference to the block data.
44 pub fn get_block(&self, block: &Block) -> Option<&BlockData> {
45 self.blocks.get(block)
46 }
47
48 /// Returns a mutable reference to the block data.
49 pub fn get_block_mut(&mut self, block: &Block) -> Option<&mut BlockData> {
50 self.blocks.get_mut(block)
51 }
52
53 /// Returns the entry block of the function.
54 pub fn entry_block(&self) -> Option<Block> {
55 self.entry_block
56 }
57
58 /// Checks if the given value has the specified type.
59 pub(crate) fn is_val_type(&self, val: Value, ty: IRType) -> bool {
60 self.get_val_type(val) == ty
61 }
62
63 /// Returns the type of the given value.
64 /// The caller must call this method with the value generated by this builder.
65 pub fn get_val_type(&self, val: Value) -> IRType {
66 self.values[&val]
67 }
68
69 /// Returns all variables in the function.
70 pub fn get_vars(&self) -> Vec<Variable> {
71 self.variables.keys().copied().collect()
72 }
73
74 /// Returns all values in the function.
75 pub fn get_vals(&self) -> Vec<Value> {
76 self.values.keys().copied().collect()
77 }
78
79 /// Returns the map of the types of all variables in the function.
80 pub fn get_var_types(&self) -> &HashMap<Variable, IRType> {
81 &self.variables
82 }
83
84 /// Returns the map of the types of all values in the function.
85 pub fn get_val_types(&self) -> &HashMap<Value, IRType> {
86 &self.values
87 }
88
89 /// Returns the type of the given variable.
90 /// The caller must call this method with the variable generated by this builder.
91 pub fn get_var_type(&self, var: Variable) -> IRType {
92 self.variables[&var]
93 }
94}
95
96impl Display for Function {
97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98 for (block, data) in self.blocks.iter() {
99 write!(f, "{}{}", block, data)?;
100 }
101 Ok(())
102 }
103}