pcb_core/
ty.rs

1//use super::Ctxt;
2use common::Interner;
3
4pub type TypeContext = Interner<Type>;
5
6impl Type {
7  pub fn int_size(&self) -> u32 {
8    match *self {
9      Type::Integer(size) => size,
10    }
11  }
12}
13
14#[derive(Debug, PartialEq, Eq, Hash)]
15pub enum Type {
16  Integer(u32),
17  /*
18  Void,
19  Bool,
20  Pointer,
21  // FnPtr
22  Aggregate(Vec<Type<'c>>),
23  */
24}
25
26#[derive(Clone, Debug, Hash, PartialEq, Eq)]
27pub struct Function<'t> {
28  pub inputs: Box<[&'t Type]>,
29  pub output: &'t Type,
30}
31
32mod fmt {
33  use std::fmt::{Display, Formatter, Error};
34  use super::{Type, Function};
35  impl Display for Type {
36    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
37      match *self {
38        Type::Integer(n) => write!(f, "i{}", n),
39        /*
40        TypeVariant::Bool => write!(f, "bool"),
41        TypeVariant::Pointer => write!(f, "ptr"),
42        TypeVariant::Aggregate(ref v) => {
43          try!(write!(f, "("));
44          if v.is_empty() {
45            write!(f, ")")
46          } else {
47            for el in &v[..v.len() - 1] {
48              try!(write!(f, "{}, ", el));
49            }
50            write!(f, "{})", &v[v.len() - 1])
51          }
52        }
53        */
54      }
55    }
56  }
57
58  impl<'a> Display for Function<'a> {
59    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
60      try!(write!(f, "("));
61      if !self.inputs.is_empty() {
62        for input in &self.inputs[..self.inputs.len() - 1] {
63          try!(write!(f, "{}, ", input));
64        }
65        try!(write!(f, "{}", self.inputs[self.inputs.len() - 1]));
66      }
67      write!(f, ") -> {}", self.output)
68    }
69  }
70}
71