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
use std::{cell::RefCell, collections::HashMap};

use super::{
    block::Block,
    dialect::Dialect,
    mnemonic::MnemonicSegment,
    op::OpObj,
    region::Region,
    symbol::NameManager,
    ty::TyObj,
    value::Value,
};
use crate::support::{
    cast::CasterStorage,
    storage::{Arena, UniqueArena},
};

/// The context of the whole IR.
///
/// The context stores all the entities using a simple arena system.
#[derive(Default)]
pub struct Context {
    /// The values.
    pub values: Arena<Value>,
    /// The blocks.
    pub blocks: Arena<Block>,
    /// The regions.
    pub regions: Arena<Region>,
    /// The operations.
    pub ops: Arena<OpObj>,
    /// The types.
    pub tys: UniqueArena<TyObj>,
    /// The dialects.
    pub dialects: HashMap<MnemonicSegment, Dialect>,
    /// The caster storage.
    ///
    /// This is used for interface casting.
    pub casters: CasterStorage,
    /// The name of values.
    pub(crate) value_names: RefCell<NameManager<Value>>,
}

impl Context {
    pub fn register_dialect(&mut self, dialect: Dialect) {
        self.dialects.insert(dialect.mnemonic(), dialect);
    }
}