1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use generational_arena::Arena;
use crate::{
asm::AsmBlockContent, block::BlockContent, function::FunctionContent, irtype::AggregateContent,
metadata::Metadatum, module::ModuleContent, module::ModuleIterator, pointer::PointerContent,
value::ValueContent,
};
#[derive(Default)]
pub struct Context {
pub modules: Arena<ModuleContent>,
pub functions: Arena<FunctionContent>,
pub blocks: Arena<BlockContent>,
pub values: Arena<ValueContent>,
pub pointers: Arena<PointerContent>,
pub aggregates: Arena<AggregateContent>,
pub asm_blocks: Arena<AsmBlockContent>,
pub metadata: Arena<Metadatum>,
next_unique_sym_tag: u64,
}
impl Context {
pub fn module_iter(&self) -> ModuleIterator {
ModuleIterator::new(self)
}
pub fn get_unique_name(&mut self) -> String {
format!("anon_{}", self.get_unique_id())
}
pub fn get_unique_id(&mut self) -> u64 {
let sym = self.next_unique_sym_tag;
self.next_unique_sym_tag += 1;
sym
}
}
use std::fmt::{Display, Error, Formatter};
impl Display for Context {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", crate::printer::to_string(self))
}
}
impl From<Context> for String {
fn from(context: Context) -> Self {
crate::printer::to_string(&context)
}
}