1use {std, ty};
2use function::{Function, FuncContext};
3
4pub struct Ctxt {
5 pub type_ctxt: ty::TypeContext,
6 pub func_ctxt: FuncContext<'static>, pub optimize: bool,
8}
9
10impl Ctxt {
11 pub fn new(opt: bool) -> Self {
12 Ctxt {
13 type_ctxt: ty::TypeContext::new(),
14 func_ctxt: FuncContext::new(),
15 optimize: opt,
16 }
17 }
18
19 pub fn add_function<'c>(&'c self, name: &str, ty: ty::Function<'c>)
20 -> &'c Function<'c> {
21 use std::mem::transmute;
22 use function::{Value, ValueKind, ValueContext, BlockContext};
23
24 let ret = unsafe {
25 let ret = self.func_ctxt.push(Function {
26 name: name.to_owned(),
27 ty: transmute::<ty::Function<'c>, ty::Function<'static>>(ty),
28 values: ValueContext::new(),
29 blocks: BlockContext::new(),
30 });
31 transmute::<&'c Function<'static>, &'c Function<'c>>(ret)
32 };
33 for param_ty in &ret.ty.inputs[..] {
34 ret.values.push(Value {
35 number: ret.values.len() as u32,
36 kind: ValueKind::Parameter(param_ty),
37 func: ret,
38 });
39 }
40 ret
41 }
42
43 pub fn get_type(&self, ty: ty::Type) -> &ty::Type {
44 self.type_ctxt.get(ty)
45 }
46}
47
48impl std::fmt::Display for Ctxt {
49 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
50 for func in &self.func_ctxt {
51 try!(writeln!(f, "{}", func));
52 }
53 Ok(())
54 }
55}