pipeline_script/context/
value.rs

1use crate::ast::module::Module;
2use crate::ast::r#type::Type;
3use crate::context::scope::Scope;
4use crate::llvm::builder::ir::IRBuilder;
5use crate::llvm::context::LLVMContext;
6use crate::llvm::module::LLVMModule;
7use crate::llvm::types::LLVMType;
8use crate::llvm::value::fucntion::FunctionValue;
9use llvm_sys::prelude::LLVMBasicBlockRef;
10use slotmap::DefaultKey;
11use std::collections::HashMap;
12use std::rc::Rc;
13use std::sync::{Arc, Mutex, RwLock};
14
15#[derive(Debug, Clone)]
16pub enum ContextValue {
17    Background,
18    Builder(Arc<IRBuilder>),
19    SymbolType(Arc<RwLock<HashMap<String, Type>>>),
20    AliasType(Arc<RwLock<HashMap<String, Type>>>),
21    // SymbolTable(Rc<Mutex<HashMap<String, LLVMValue>>>),
22    LLVMContext(Rc<Mutex<LLVMContext>>),
23    LLVMModule(Rc<RwLock<LLVMModule>>),
24    ModuleSlotMap(Arc<RwLock<slotmap::SlotMap<DefaultKey, Module>>>),
25    // 编译阶段使用
26    Scope(Scope),
27    Flag(Arc<RwLock<bool>>),
28    Function(FunctionValue),
29    // 编译阶段使用,存储当前函数类型
30    Type(Type),
31    // 预处理阶段分析闭包捕获的变量
32    LocalVariable(Arc<RwLock<Vec<String>>>),
33    CaptureVariable(Arc<RwLock<Vec<(String, Type)>>>),
34    TypeTable(Rc<RwLock<HashMap<Type, LLVMType>>>),
35    // 循环的基本块
36    LoopBlock(LLVMBasicBlockRef),
37    DefaultExpr(Arc<RwLock<HashMap<String, Box<crate::ast::expr::ExprNode>>>>),
38}
39
40impl Default for ContextValue {
41    fn default() -> Self {
42        Self::Background
43    }
44}
45
46impl ContextValue {
47    pub fn as_builder(&self) -> Arc<IRBuilder> {
48        match self {
49            ContextValue::Builder(b) => b.clone(),
50            _ => panic!("not a builder"),
51        }
52    }
53    pub fn as_module(&self) -> Rc<RwLock<LLVMModule>> {
54        match self {
55            ContextValue::LLVMModule(m) => m.clone(),
56            _ => panic!("not a module"),
57        }
58    }
59    pub fn as_type(&self) -> Type {
60        match self {
61            ContextValue::Type(t) => t.clone(),
62            _ => panic!("not a type"),
63        }
64    }
65    #[allow(unused)]
66    pub fn as_type_table(&self) -> Rc<RwLock<HashMap<Type, LLVMType>>> {
67        match self {
68            ContextValue::TypeTable(t) => t.clone(),
69            _ => panic!("not a type"),
70        }
71    }
72    pub fn as_scope(&self) -> Scope {
73        match self {
74            ContextValue::Scope(s) => s.clone(),
75            _ => panic!("not a scope"),
76        }
77    }
78    // pub fn as_symbol_table(&self) -> Rc<Mutex<HashMap<String, LLVMValue>>> {
79    //     match self {
80    //         ContextValue::SymbolTable(t) => t.clone(),
81    //         _ => panic!("not a symbol table"),
82    //     }
83    // }
84}